Open History to see the last commit
1import { randomUUID } from 'node:crypto';
2
3export type ProgressStage = 'thinking' | 'working' | 'checking';
4export function publicProgressText(message: unknown): string | null {
5 const input = message as { role?: string; content?: Array<{ type: string; text?: string; name?: string }> };
6 if (input?.role !== 'assistant' || !Array.isArray(input.content)) return null;
7 if (!input.content.some(part => part.type === 'toolCall') || input.content.some(part => part.type === 'toolCall' && part.name === 'ask_user')) return null;
8 const text = input.content.filter(part => part.type === 'text').map(part => part.text ?? '').join('\n').trim();
9 return text && !text.includes('```') ? text.slice(0, 1200) : null;
10}
11
12// The reply shown when a change finishes: the latest real message, not a stub. Models sometimes end
13// a turn with a fragment such as "I" after their tool calls, which used to become the whole reply.
14export function finalReply(messages: readonly unknown[]): string | undefined {
15 const texts = messages.flatMap(message => {
16 const input = message as { role?: string; content?: Array<{ type: string; text?: string }> };
17 if (input?.role !== 'assistant' || !Array.isArray(input.content)) return [];
18 const text = input.content.filter(part => part.type === 'text').map(part => part.text ?? '').join('').trim();
19 return text ? [text] : [];
20 });
21 return texts.findLast(text => text.length >= 24 && /\s/.test(text)) ?? texts.findLast(text => text.length >= 8);
22}
23
24export function createProgressReporter(post: (input: { eventId: string; content: string; stage: ProgressStage }) => Promise<unknown>, invalidated: (error: Error) => void) {
25 let queue = Promise.resolve(), count = 0, previous = '', previousTool = '', stopped = false;
26 let stage: ProgressStage = 'thinking';
27 function report(content: string, nextStage: ProgressStage = 'working') {
28 const text = content.trim().slice(0, 1200);
29 if (stopped || !text || text === previous || count >= 24) return;
30 previous = text; stage = nextStage; count++;
31 const eventId = randomUUID();
32 queue = queue.then(async () => {
33 if (stopped) return;
34 try { await post({ eventId, content: text, stage: nextStage }); }
35 catch (error) {
36 if ([401, 403, 409].includes((error as { status?: number }).status ?? 0)) { stopped = true; invalidated(error as Error); }
37 }
38 });
39 }
40 return {
41 report,
42 tool(name: string) {
43 const activity: Record<string, [string, ProgressStage]> = { read: ['Reading through your app.', 'thinking'], list_files: ['Looking through your app.', 'thinking'], inspect_capabilities: ['Checking what the app needs.', 'thinking'], write: ['Building your app.', 'working'], edit: ['Making changes to your app.', 'working'], build_candidate: ['Checking the app.', 'checking'], look_at_app: ['Looking at the app on a phone.', 'checking'] };
44 const next = activity[name];
45 if (!next || name === previousTool) return;
46 previousTool = name; report(...next);
47 },
48 get phase() { return { thinking: 'Thinking', working: 'Making changes', checking: 'Checking the app' }[stage]; },
49 flush: () => queue,
50 close() { stopped = true; },
51 };
52}