mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-05 16:23:36 +00:00
Save last finish_reason, display a "Resume" button
This commit is contained in:
10 files changed
+102
-7
No files matched your search
@@ -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<ChatPaneRef, ChatPaneProps>(({
|
||||
isLoadingHistory,
|
||||
totalCost,
|
||||
lastInputTokens,
|
||||
lastFinishReason,
|
||||
addMessage,
|
||||
sendMessage,
|
||||
startGeneration,
|
||||
@@ -797,6 +798,22 @@ export const ChatPane = forwardRef<ChatPaneRef, ChatPaneProps>(({
|
||||
|
||||
{!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 && (
|
||||
<div className="flex justify-center py-4">
|
||||
<Button
|
||||
onClick={() => startGeneration(providerModel)}
|
||||
variant="outline"
|
||||
size="sm"
|
||||
disabled={!providerModel.trim() || !isConfigured}
|
||||
className="gap-2"
|
||||
>
|
||||
<Play className="h-4 w-4" />
|
||||
{t('resume')}
|
||||
</Button>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Error Alert (Console or AI) - Don't show while assistant is loading */}
|
||||
{displayError && !isLoading && (
|
||||
<Quilly
|
||||
|
||||
@@ -43,6 +43,7 @@ export function useAIChat({
|
||||
const [isLoading, setIsLoading] = useState<boolean>(false);
|
||||
const [totalCost, setTotalCost] = useState<number>(0);
|
||||
const [lastInputTokens, setLastInputTokens] = useState<number>(0);
|
||||
const [lastFinishReason, setLastFinishReason] = useState<string | null>(null);
|
||||
const [isLoadingHistory, setIsLoadingHistory] = useState<boolean>(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<OpenAI.Chat.Completions.ChatCompletionContentPartText | OpenAI.Chat.Completions.ChatCompletionContentPartImage>,
|
||||
@@ -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,
|
||||
|
||||
@@ -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}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
*/
|
||||
|
||||
+2
-1
@@ -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"
|
||||
}
|
||||
+2
-1
@@ -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"
|
||||
}
|
||||
+2
-1
@@ -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"
|
||||
}
|
||||
+2
-1
@@ -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"
|
||||
}
|
||||
+2
-1
@@ -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"
|
||||
}
|
||||
+2
-1
@@ -484,5 +484,6 @@
|
||||
"projectDuplicatedDescription": "项目已成功复制。",
|
||||
"failedToDuplicateProject": "复制项目失败",
|
||||
"newProjectName": "新项目名称",
|
||||
"duplicateProjectDescription": "使用新名称创建此项目的副本"
|
||||
"duplicateProjectDescription": "使用新名称创建此项目的副本",
|
||||
"resume": "继续"
|
||||
}
|
||||
Reference in new issue
Block a user