.ai -> .git/ai

This commit is contained in:
Alex Gleason committed 2025-09-25 15:11:01 -05:00
1 parent 374d2804ad
commit f62b2f807c
3 files changed
+30 -115

No files matched your search

+4 -6
View File
@@ -98,8 +98,6 @@ invalid json line
it('should create history directory if it does not exist', async () => {
// Mock historyDirExists to return false initially
vi.spyOn(dotAI, 'historyDirExists').mockResolvedValue(false);
// Mock setupAiHistoryDir
vi.spyOn(dotAI, 'setupAiHistoryDir').mockResolvedValue();
const messages: OpenAI.Chat.Completions.ChatCompletionMessageParam[] = [
{ role: 'user', content: 'Hello' }
@@ -107,8 +105,8 @@ invalid json line
await dotAI.setHistory('test-session', messages);
// Verify setupAiHistoryDir was called
expect(dotAI.setupAiHistoryDir).toHaveBeenCalled();
// Verify mkdir was called with recursive option
expect(mockFS.mkdir).toHaveBeenCalledWith('/test-project/.git/ai/history', { recursive: true });
});
it('should write messages as JSONL format', async () => {
@@ -124,7 +122,7 @@ invalid json line
// Verify writeFile was called with correct content
expect(mockFS.writeFile).toHaveBeenCalledWith(
'/test-project/.ai/history/test-session.jsonl',
'/test-project/.git/ai/history/test-session.jsonl',
'{"role":"user","content":"Hello"}\n{"role":"assistant","content":"Hi there!"}\n'
);
});
@@ -137,7 +135,7 @@ invalid json line
// Verify writeFile was called with empty content
expect(mockFS.writeFile).toHaveBeenCalledWith(
'/test-project/.ai/history/test-session.jsonl',
'/test-project/.git/ai/history/test-session.jsonl',
''
);
});
+16 -86
View File
@@ -2,7 +2,7 @@ import { join } from "path-browserify";
import type OpenAI from "openai";
import type { JSRuntimeFS } from "./JSRuntime";
/** AI message history manager and .ai directory utilities */
/** AI message history manager and .git/ai directory utilities */
export class DotAI {
readonly fs: JSRuntimeFS;
readonly historyDir: string;
@@ -11,7 +11,7 @@ export class DotAI {
constructor(fs: JSRuntimeFS, workingDir: string = "/") {
this.fs = fs;
this.workingDir = workingDir;
this.historyDir = join(workingDir, ".ai", "history");
this.historyDir = join(workingDir, ".git", "ai", "history");
}
/** Generate a unique session name based on the current date and time */
@@ -31,8 +31,6 @@ export class DotAI {
return filename;
}
/** Check if the history directory exists */
async historyDirExists(): Promise<boolean> {
try {
@@ -51,10 +49,9 @@ export class DotAI {
// Validate messages before saving
this.validateMessages(messages);
// Always create the .ai directory and write session history
// Create the history directory if it doesn't exist
if (!(await this.historyDirExists())) {
await this.setupAiHistoryDir();
await this.fs.mkdir(this.historyDir, { recursive: true });
}
const sessionFile = join(this.historyDir, sessionName + ".jsonl");
@@ -122,29 +119,11 @@ export class DotAI {
}
/**
* Creates .ai/history directory and ensures .ai/ is gitignored at the project root.
*/
async setupAiHistoryDir(): Promise<void> {
const aiHistoryDir = join(this.workingDir, ".ai", "history");
try {
// Create .ai/history directory
await this.fs.mkdir(aiHistoryDir, { recursive: true });
// Ensure .ai/ is gitignored at the project root
await this.ensureAiGitignored();
} catch (error) {
// Log error but don't fail the main operation
console.warn(`Failed to setup .ai/history directory: ${error}`);
}
}
/**
* Read the model from .ai/MODEL file
* Read the model from .git/ai/MODEL file
*/
async readAiModel(): Promise<string | undefined> {
try {
const modelPath = join(this.workingDir, ".ai", "MODEL");
const modelPath = join(this.workingDir, ".git", "ai", "MODEL");
const content = await this.fs.readFile(modelPath, "utf8");
return content.trim();
} catch {
@@ -153,32 +132,29 @@ export class DotAI {
}
/**
* Write the model to .ai/MODEL file
* Write the model to .git/ai/MODEL file
*/
async writeAiModel(model: string): Promise<void> {
const aiDir = join(this.workingDir, ".ai");
const aiDir = join(this.workingDir, ".git", "ai");
const modelPath = join(aiDir, "MODEL");
try {
// Ensure .ai directory exists
// Ensure .git/ai directory exists
await this.fs.mkdir(aiDir, { recursive: true });
// Ensure .ai/ is gitignored
await this.ensureAiGitignored();
// Write the model
await this.fs.writeFile(modelPath, model.trim() + "\n");
} catch (error) {
console.warn(`Failed to write .ai/MODEL file: ${error}`);
console.warn(`Failed to write .git/ai/MODEL file: ${error}`);
}
}
/**
* Read parameters from .ai/PARAMETERS file
* Read parameters from .git/ai/PARAMETERS file
*/
async readAiParameters(): Promise<Record<string, string>> {
try {
const parametersPath = join(this.workingDir, ".ai", "PARAMETERS");
const parametersPath = join(this.workingDir, ".git", "ai", "PARAMETERS");
const content = await this.fs.readFile(parametersPath, "utf8");
const parameters: Record<string, string> = {};
@@ -203,19 +179,16 @@ export class DotAI {
}
/**
* Write parameters to .ai/PARAMETERS file
* Write parameters to .git/ai/PARAMETERS file
*/
async writeAiParameters(parameters: Record<string, string>): Promise<void> {
const aiDir = join(this.workingDir, ".ai");
const aiDir = join(this.workingDir, ".git", "ai");
const parametersPath = join(aiDir, "PARAMETERS");
try {
// Ensure .ai directory exists
// Ensure .git/ai directory exists
await this.fs.mkdir(aiDir, { recursive: true });
// Ensure .ai/ is gitignored
await this.ensureAiGitignored();
// Format parameters as key=value pairs
const lines = Object.entries(parameters).map(([key, value]) =>
`${key}=${value}`
@@ -225,12 +198,12 @@ export class DotAI {
// Write the parameters
await this.fs.writeFile(parametersPath, content);
} catch (error) {
console.warn(`Failed to write .ai/PARAMETERS file: ${error}`);
console.warn(`Failed to write .git/ai/PARAMETERS file: ${error}`);
}
}
/**
* Update a single parameter in .ai/PARAMETERS file
* Update a single parameter in .git/ai/PARAMETERS file
*/
async updateAiParameter(key: string, value: string): Promise<void> {
const currentParameters = await this.readAiParameters();
@@ -301,47 +274,4 @@ export class DotAI {
return null;
}
}
/**
* Ensures that .ai/ is gitignored at the project root level.
* Creates a .gitignore file if it doesn't exist, or adds the line if not already present.
*/
async ensureAiGitignored(): Promise<void> {
const gitignorePath = join(this.workingDir, ".gitignore");
const aiPattern = ".ai/";
try {
// Try to read existing .gitignore
let gitignoreContent = "";
try {
gitignoreContent = await this.fs.readFile(gitignorePath, "utf8");
} catch {
// .gitignore doesn't exist, will create it
}
// Check if .ai/ is already gitignored
const lines = gitignoreContent.split("\n").map((line) => line.trim());
const hasAiPattern = lines.some((line) => {
if (line.startsWith("#") || line === "") {
return false;
}
// Check for various .ai patterns
return line === ".ai" || line === ".ai/" || line === "/.ai" ||
line === "/.ai/" ||
line === ".ai/*" || line === "/.ai/*";
});
if (!hasAiPattern) {
// Add .ai/ to .gitignore
const newContent = gitignoreContent.trim() === ""
? aiPattern
: gitignoreContent.trim() + "\n" + aiPattern;
await this.fs.writeFile(gitignorePath, newContent + "\n");
}
} catch (error) {
// Log error but don't fail the main operation
console.warn(`Failed to ensure .ai/ is gitignored: ${error}`);
}
}
}
+10 -23
View File
@@ -46,12 +46,6 @@ export class ProjectsManager {
async createProject(name: string, customId?: string): Promise<Project> {
const project = await this.cloneProject(name, GIT_TEMPLATE_URL, customId, { depth: 1 });
try {
await this.fs.mkdir(this.dir + `/${project.id}/.ai/history`, { recursive: true });
} catch {
// Directory might already exist
}
// Delete README.md if it exists
try {
await this.fs.unlink(`${project.path}/README.md`);
@@ -75,8 +69,8 @@ export class ProjectsManager {
// Add all files to git
const files = await this.getAllFiles(project.path);
for (const file of files) {
// Skip .git directory and other hidden files we don't want to track
if (!file.startsWith('.git/') && !file.startsWith('.ai/')) {
// Skip .git directory
if (!file.startsWith('.git/')) {
await this.git.add({
dir: project.path,
filepath: file,
@@ -144,12 +138,12 @@ export class ProjectsManager {
// Create project directory (or ensure it exists for overwrite)
await this.fs.mkdir(projectPath, { recursive: true });
// If overwriting, delete existing files except .ai directory and .git directory
// If overwriting, delete existing files except .git directory
if (projectExists && overwrite) {
const existingFiles = await this.getAllFiles(projectPath);
for (const file of existingFiles) {
// Preserve .ai directory and .git directory
if (!file.startsWith('.ai/') && !file.startsWith('.git/')) {
// Preserve .git directory
if (!file.startsWith('.git/')) {
try {
await this.fs.unlink(`${projectPath}/${file}`);
} catch {
@@ -212,8 +206,8 @@ export class ProjectsManager {
if (overwrite) {
const projectFiles = await this.getAllFiles(projectPath);
for (const file of projectFiles) {
// Skip .git directory and .ai directory
if (!file.startsWith('.git/') && !file.startsWith('.ai/')) {
// Skip .git directory
if (!file.startsWith('.git/')) {
try {
await this.git.add({
dir: projectPath,
@@ -242,11 +236,11 @@ export class ProjectsManager {
defaultBranch: 'main',
});
// Add all files to git (excluding .ai directory)
// Add all files to git
const projectFiles = await this.getAllFiles(projectPath);
for (const file of projectFiles) {
// Skip .git directory and .ai directory
if (!file.startsWith('.git/') && !file.startsWith('.ai/')) {
// Skip .git directory
if (!file.startsWith('.git/')) {
await this.git.add({
dir: projectPath,
filepath: file,
@@ -265,13 +259,6 @@ export class ProjectsManager {
});
}
// Create .ai/history directory
try {
await this.fs.mkdir(`${projectPath}/.ai/history`, { recursive: true });
} catch {
// Directory might already exist
}
return {
id,
name: projectName,