From 5cf19c61cc5d4cab94e4e094679c3c7575e79d70 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sat, 20 Sep 2025 22:26:06 -0500 Subject: [PATCH] DotAI: always write --- src/lib/DotAI.test.ts | 52 +++--------------------------------- src/lib/DotAI.ts | 61 +++++++------------------------------------ 2 files changed, 13 insertions(+), 100 deletions(-) diff --git a/src/lib/DotAI.test.ts b/src/lib/DotAI.test.ts index f48abd82..218ebfe6 100644 --- a/src/lib/DotAI.test.ts +++ b/src/lib/DotAI.test.ts @@ -28,17 +28,8 @@ describe('DotAI', () => { }); describe('readLastSessionHistory', () => { - it('should return null when AI is not enabled', async () => { - // Mock isEnabled to return false - vi.spyOn(dotAI, 'isEnabled').mockResolvedValue(false); - - const result = await dotAI.readLastSessionHistory(); - expect(result).toBeNull(); - }); - it('should return null when history directory does not exist', async () => { - // Mock isEnabled to return true but historyDirExists to return false - vi.spyOn(dotAI, 'isEnabled').mockResolvedValue(true); + // Mock historyDirExists to return false vi.spyOn(dotAI, 'historyDirExists').mockResolvedValue(false); const result = await dotAI.readLastSessionHistory(); @@ -46,8 +37,7 @@ describe('DotAI', () => { }); it('should return null when no session files exist', async () => { - // Mock isEnabled and historyDirExists to return true - vi.spyOn(dotAI, 'isEnabled').mockResolvedValue(true); + // Mock historyDirExists to return true vi.spyOn(dotAI, 'historyDirExists').mockResolvedValue(true); // Mock readdir to return empty array @@ -58,8 +48,7 @@ describe('DotAI', () => { }); it('should return messages and session name from most recent file', async () => { - // Mock isEnabled and historyDirExists to return true - vi.spyOn(dotAI, 'isEnabled').mockResolvedValue(true); + // Mock historyDirExists to return true vi.spyOn(dotAI, 'historyDirExists').mockResolvedValue(true); // Mock readdir to return session files @@ -84,8 +73,7 @@ describe('DotAI', () => { }); it('should handle malformed JSON lines gracefully', async () => { - // Mock isEnabled and historyDirExists to return true - vi.spyOn(dotAI, 'isEnabled').mockResolvedValue(true); + // Mock historyDirExists to return true vi.spyOn(dotAI, 'historyDirExists').mockResolvedValue(true); // Mock readdir to return session files @@ -107,24 +95,7 @@ invalid json line }); describe('setHistory', () => { - it('should not save when AI is not enabled', async () => { - // Mock isEnabled to return false - vi.spyOn(dotAI, 'isEnabled').mockResolvedValue(false); - - const messages: OpenAI.Chat.Completions.ChatCompletionMessageParam[] = [ - { role: 'user', content: 'Hello' }, - { role: 'assistant', content: 'Hi there!' } - ]; - - await dotAI.setHistory('test-session', messages); - - // Verify writeFile was not called - expect(mockFS.writeFile).not.toHaveBeenCalled(); - }); - it('should create history directory if it does not exist', async () => { - // Mock isEnabled to return true - vi.spyOn(dotAI, 'isEnabled').mockResolvedValue(true); // Mock historyDirExists to return false initially vi.spyOn(dotAI, 'historyDirExists').mockResolvedValue(false); // Mock setupAiHistoryDir @@ -141,8 +112,6 @@ invalid json line }); it('should write messages as JSONL format', async () => { - // Mock isEnabled to return true - vi.spyOn(dotAI, 'isEnabled').mockResolvedValue(true); // Mock historyDirExists to return true vi.spyOn(dotAI, 'historyDirExists').mockResolvedValue(true); @@ -161,8 +130,6 @@ invalid json line }); it('should handle empty messages array', async () => { - // Mock isEnabled to return true - vi.spyOn(dotAI, 'isEnabled').mockResolvedValue(true); // Mock historyDirExists to return true vi.spyOn(dotAI, 'historyDirExists').mockResolvedValue(true); @@ -176,8 +143,6 @@ invalid json line }); it('should handle write errors gracefully', async () => { - // Mock isEnabled to return true - vi.spyOn(dotAI, 'isEnabled').mockResolvedValue(true); // Mock historyDirExists to return true vi.spyOn(dotAI, 'historyDirExists').mockResolvedValue(true); // Mock writeFile to throw an error @@ -190,9 +155,6 @@ invalid json line }); it('should validate tool messages have matching tool_call_id', async () => { - // Mock isEnabled to return true - vi.spyOn(dotAI, 'isEnabled').mockResolvedValue(true); - const validMessages: OpenAI.Chat.Completions.ChatCompletionMessageParam[] = [ { role: 'user', content: 'Hello' }, { @@ -249,9 +211,6 @@ invalid json line }); it('should validate multiple tool messages correctly', async () => { - // Mock isEnabled to return true - vi.spyOn(dotAI, 'isEnabled').mockResolvedValue(true); - const validMessages: OpenAI.Chat.Completions.ChatCompletionMessageParam[] = [ { role: 'user', content: 'Hello' }, { @@ -271,9 +230,6 @@ invalid json line }); it('should allow tool messages to reference tool calls from the most recent assistant message', async () => { - // Mock isEnabled to return true - vi.spyOn(dotAI, 'isEnabled').mockResolvedValue(true); - const validMessages: OpenAI.Chat.Completions.ChatCompletionMessageParam[] = [ { role: 'user', content: 'First request' }, { diff --git a/src/lib/DotAI.ts b/src/lib/DotAI.ts index bc9961bb..8a073ee5 100644 --- a/src/lib/DotAI.ts +++ b/src/lib/DotAI.ts @@ -31,45 +31,7 @@ export class DotAI { return filename; } - /** Check if AI history should be enabled based on the new criteria */ - async isEnabled(): Promise { - // Check if .ai directory exists - try { - const stat = await this.fs.stat(join(this.workingDir, ".ai")); - if (stat.isDirectory()) { - return true; - } - } catch { - // Continue to next check - } - // Check if .ai/ or .ai is gitignored - try { - const gitignorePath = join(this.workingDir, ".gitignore"); - const gitignoreContent = await this.fs.readFile(gitignorePath, "utf8"); - const lines = gitignoreContent.split("\n").map((line) => line.trim()); - - // Check for .ai/ or .ai patterns (ignore comments and empty lines) - for (const line of lines) { - if (line.startsWith("#") || line === "") { - continue; - } - - // Check for various .ai patterns - if ( - line === ".ai" || line === ".ai/" || line === "/.ai" || - line === "/.ai/" || - line === ".ai/*" || line === "/.ai/*" - ) { - return true; - } - } - } catch { - // .gitignore doesn't exist or can't be read - } - - return false; - } /** Check if the history directory exists */ async historyDirExists(): Promise { @@ -89,11 +51,7 @@ export class DotAI { // Validate messages before saving this.validateMessages(messages); - // Only save if history should be enabled based on the new criteria - if (!(await this.isEnabled())) { - return; - } - + // 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(); @@ -205,6 +163,9 @@ export class DotAI { // Ensure .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) { @@ -245,13 +206,6 @@ export class DotAI { * Write parameters to .ai/PARAMETERS file */ async writeAiParameters(parameters: Record): Promise { - // Check if we should create the .ai directory based on the same logic as MODEL - const shouldCreate = await this.isEnabled(); - - if (!shouldCreate) { - return; - } - const aiDir = join(this.workingDir, ".ai"); const parametersPath = join(aiDir, "PARAMETERS"); @@ -259,6 +213,9 @@ export class DotAI { // Ensure .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}` @@ -290,8 +247,8 @@ export class DotAI { sessionName: string; } | null> { try { - // Check if AI history is enabled for this project - if (!(await this.isEnabled()) || !(await this.historyDirExists())) { + // Check if history directory exists + if (!(await this.historyDirExists())) { return null; }