diff --git a/src/components/Shakespeare/ChatPane.tsx b/src/components/Shakespeare/ChatPane.tsx index 039ea930..423ff94e 100644 --- a/src/components/Shakespeare/ChatPane.tsx +++ b/src/components/Shakespeare/ChatPane.tsx @@ -4,7 +4,7 @@ import { useTranslation } from 'react-i18next'; import { Button } from '@/components/ui/button'; import { Skeleton } from '@/components/ui/skeleton'; import { OnboardingDialog } from '@/components/OnboardingDialog'; -import { ChevronDown } from 'lucide-react'; +import { ChevronDown, Play } from 'lucide-react'; import { useAISettings } from '@/hooks/useAISettings'; import { useFS } from '@/hooks/useFS'; import { useFSPaths } from '@/hooks/useFSPaths'; @@ -291,6 +291,7 @@ export const ChatPane = forwardRef(({ isLoadingHistory, totalCost, lastInputTokens, + lastFinishReason, addMessage, sendMessage, startGeneration, @@ -797,6 +798,22 @@ export const ChatPane = forwardRef(({ {!isLoadingHistory && renderStreamingMessage()} + {/* Resume button - shown when not loading and finish reason is not "stop" or "length" */} + {!isLoadingHistory && !isLoading && lastFinishReason && lastFinishReason !== 'stop' && lastFinishReason !== 'length' && messages.length > 0 && ( +
+ +
+ )} + {/* Error Alert (Console or AI) - Don't show while assistant is loading */} {displayError && !isLoading && ( (false); const [totalCost, setTotalCost] = useState(0); const [lastInputTokens, setLastInputTokens] = useState(0); + const [lastFinishReason, setLastFinishReason] = useState(null); const [isLoadingHistory, setIsLoadingHistory] = useState(true); const initSession = useCallback(async () => { @@ -56,6 +57,7 @@ export function useAIChat({ setIsLoading(session.isLoading); setTotalCost(session.totalCost || 0); setLastInputTokens(session.lastInputTokens || 0); + setLastFinishReason(session.lastFinishReason ?? null); } finally { setIsLoadingHistory(false); } @@ -115,6 +117,12 @@ export function useAIChat({ } }, [projectId]); + useSessionSubscription('finishReasonChanged', (updatedProjectId: string, finishReason: string | null) => { + if (updatedProjectId === projectId) { + setLastFinishReason(finishReason); + } + }, [projectId]); + // Actions const sendMessage = useCallback(async ( content: string | Array, @@ -161,6 +169,7 @@ export function useAIChat({ setStreamingMessage(undefined); setTotalCost(0); setLastInputTokens(0); + setLastFinishReason(null); }, [projectId, sessionManager]); const clearMessages = useCallback(async () => { @@ -175,6 +184,7 @@ export function useAIChat({ isLoadingHistory, totalCost, lastInputTokens, + lastFinishReason, sendMessage, startGeneration, stopGeneration, diff --git a/src/lib/DotAI.ts b/src/lib/DotAI.ts index 8ad0a1a6..cecd7ce9 100644 --- a/src/lib/DotAI.ts +++ b/src/lib/DotAI.ts @@ -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 { + 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 { + 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}`); + } + } } diff --git a/src/lib/SessionManager.ts b/src/lib/SessionManager.ts index 0968fb20..b4023974 100644 --- a/src/lib/SessionManager.ts +++ b/src/lib/SessionManager.ts @@ -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 { 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 { + 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 */ diff --git a/src/locales/en.json b/src/locales/en.json index 39311360..8f0531b9 100644 --- a/src/locales/en.json +++ b/src/locales/en.json @@ -572,5 +572,6 @@ "projectDuplicatedDescription": "Project has been duplicated successfully.", "failedToDuplicateProject": "Failed to duplicate project", "newProjectName": "New Project Name", - "duplicateProjectDescription": "Create a copy of this project with a new name" + "duplicateProjectDescription": "Create a copy of this project with a new name", + "resume": "Resume" } diff --git a/src/locales/ha.json b/src/locales/ha.json index e4c1cee5..51d17991 100644 --- a/src/locales/ha.json +++ b/src/locales/ha.json @@ -475,5 +475,6 @@ "projectDuplicatedDescription": "An kwafi aikin cikin nasara.", "failedToDuplicateProject": "An kasa kwafin aikin", "newProjectName": "Sunan Sabon Aikin", - "duplicateProjectDescription": "Ƙirƙiri kwafin wannan aikin da sabon suna" + "duplicateProjectDescription": "Ƙirƙiri kwafin wannan aikin da sabon suna", + "resume": "Ci gaba" } diff --git a/src/locales/ig.json b/src/locales/ig.json index 162146bf..779892b7 100644 --- a/src/locales/ig.json +++ b/src/locales/ig.json @@ -472,5 +472,6 @@ "projectDuplicatedDescription": "Emepụtara ọrụ nke ọma.", "failedToDuplicateProject": "Ọ dara ada imepụta ọrụ", "newProjectName": "Aha Ọrụ Ọhụrụ", - "duplicateProjectDescription": "Mepụta oyiri nke ọrụ a na aha ọhụrụ" + "duplicateProjectDescription": "Mepụta oyiri nke ọrụ a na aha ọhụrụ", + "resume": "Gaa n'ihu" } diff --git a/src/locales/pt.json b/src/locales/pt.json index e2cf2581..b005f512 100644 --- a/src/locales/pt.json +++ b/src/locales/pt.json @@ -484,5 +484,6 @@ "projectDuplicatedDescription": "O projeto foi duplicado com sucesso.", "failedToDuplicateProject": "Falha ao duplicar projeto", "newProjectName": "Nome do Novo Projeto", - "duplicateProjectDescription": "Criar uma cópia deste projeto com um novo nome" + "duplicateProjectDescription": "Criar uma cópia deste projeto com um novo nome", + "resume": "Retomar" } diff --git a/src/locales/yo.json b/src/locales/yo.json index a7252ad2..e3062139 100644 --- a/src/locales/yo.json +++ b/src/locales/yo.json @@ -472,5 +472,6 @@ "projectDuplicatedDescription": "Iṣẹ akanṣe ti ṣe ẹda ni aṣeyọri.", "failedToDuplicateProject": "O kuna lati ṣe ẹda iṣẹ akanṣe", "newProjectName": "Orukọ Iṣẹ Akanṣe Tuntun", - "duplicateProjectDescription": "Ṣẹda ẹda ti iṣẹ akanṣe yii pẹlu orukọ tuntun" + "duplicateProjectDescription": "Ṣẹda ẹda ti iṣẹ akanṣe yii pẹlu orukọ tuntun", + "resume": "Tẹsiwaju" } diff --git a/src/locales/zh.json b/src/locales/zh.json index b195f355..a94f665c 100644 --- a/src/locales/zh.json +++ b/src/locales/zh.json @@ -484,5 +484,6 @@ "projectDuplicatedDescription": "项目已成功复制。", "failedToDuplicateProject": "复制项目失败", "newProjectName": "新项目名称", - "duplicateProjectDescription": "使用新名称创建此项目的副本" + "duplicateProjectDescription": "使用新名称创建此项目的副本", + "resume": "继续" }