Save last finish_reason, display a "Resume" button

This commit is contained in:
Alex Gleason committed 2025-12-28 16:49:51 -06:00
1 parent dc0144a477
commit 44dfd25a84
10 files changed
+102 -7

No files matched your search

+35
View File
@@ -358,4 +358,39 @@ export class DotAI {
console.warn(`Failed to write .git/shakespeare/template.json file: ${error}`);
}
}
/**
* Read the last finish reason from .git/ai/FINISH_REASON file
* @returns The finish reason string, or null if file doesn't exist or is empty
*/
async readFinishReason(): Promise<string | null> {
try {
const finishReasonPath = join(this.workingDir, ".git", "ai", "FINISH_REASON");
const content = await this.fs.readFile(finishReasonPath, "utf8");
const trimmed = content.trim();
return trimmed || null;
} catch {
return null;
}
}
/**
* Write the finish reason to .git/ai/FINISH_REASON file
* @param finishReason The finish reason string, or null to clear
*/
async writeFinishReason(finishReason: string | null): Promise<void> {
const aiDir = join(this.workingDir, ".git", "ai");
const finishReasonPath = join(aiDir, "FINISH_REASON");
try {
// Ensure .git/ai directory exists
await this.fs.mkdir(aiDir, { recursive: true });
// Write the finish reason (empty string if null)
const content = finishReason ? finishReason.trim() + "\n" : "";
await this.fs.writeFile(finishReasonPath, content);
} catch (error) {
console.warn(`Failed to write .git/ai/FINISH_REASON file: ${error}`);
}
}
}
+27
View File
@@ -42,6 +42,7 @@ export interface SessionState {
totalCost?: number; // Total cost in USD for this session
lastInputTokens?: number; // Input tokens from the last AI request
imagesNotSupported?: boolean; // Track if this session's model doesn't support images
lastFinishReason?: string | null; // Last finish reason from AI generation
}
export interface SessionManagerEvents {
@@ -53,6 +54,7 @@ export interface SessionManagerEvents {
costUpdated: (projectId: string, totalCost: number) => void;
contextUsageUpdated: (projectId: string, inputTokens: number) => void;
fileChanged: (projectId: string, filePath: string) => void;
finishReasonChanged: (projectId: string, finishReason: string | null) => void;
}
/**
@@ -104,6 +106,7 @@ export class SessionManager {
): Promise<SessionState> {
let messages: OpenAI.Chat.Completions.ChatCompletionMessageParam[] = [];
let sessionName = DotAI.generateSessionName();
let lastFinishReason: string | null | undefined;
// Try to load existing history
try {
@@ -114,6 +117,9 @@ export class SessionManager {
messages = lastSession.messages;
sessionName = lastSession.sessionName;
}
// Load last finish reason
lastFinishReason = await dotAI.readFinishReason();
} catch (error) {
console.warn('Failed to load session history:', error);
}
@@ -127,6 +133,7 @@ export class SessionManager {
lastActivity: new Date(),
totalCost: 0,
lastInputTokens: 0,
lastFinishReason,
...this.sessions.get(projectId),
messages,
sessionName,
@@ -629,6 +636,11 @@ export class SessionManager {
}
}
// Update and save the finish reason
session.lastFinishReason = finishReason;
this.emit('finishReasonChanged', projectId, finishReason);
await this.saveFinishReason(projectId, finishReason);
// Check if we should stop
if (finishReason === 'stop') {
break;
@@ -685,9 +697,11 @@ export class SessionManager {
session.lastActivity = new Date();
session.totalCost = 0;
session.lastInputTokens = 0;
session.lastFinishReason = null;
this.emit('costUpdated', projectId, 0);
this.emit('contextUsageUpdated', projectId, 0);
this.emit('finishReasonChanged', projectId, null);
}
/**
@@ -774,6 +788,19 @@ export class SessionManager {
}
}
/**
* Save finish reason to file
*/
private async saveFinishReason(projectId: string, finishReason: string | null): Promise<void> {
try {
const config = this.getConfig();
const dotAI = new DotAI(this.fs, `${config.fsPathProjects}/${projectId}`);
await dotAI.writeFinishReason(finishReason);
} catch (error) {
console.warn('Failed to save finish reason:', error);
}
}
/**
* Accumulate request cost to the project's total cost file
*/