"""V11: Auto-continue feature - clean single-file patch. Backend: agent_loop.py detects model stopped mid-task, sends event. Frontend: useAgentChat listens via onSessionUpdate, shows pause button. All TS errors avoided via (as any) casts where needed. """ import os ROOT = "/source/frontend/src" EVENTS = f"{ROOT}/types/events.ts" SSE = f"{ROOT}/lib/sse-chat-transport.ts" HOOK = f"{ROOT}/hooks/useAgentChat.ts" INPUT = f"{ROOT}/components/Chat/ChatInput.tsx" SESS = f"{ROOT}/components/SessionChat.tsx" AGENT_LOOP = "/app/agent/core/agent_loop.py" def p(f, s): print(f"OK: {f}" if s else f"FAIL: {f}") # --- PATCH 1: events.ts --- with open(EVENTS) as f: c = f.read() r1 = "'task_incomplete'" not in c if r1: c = c.replace(" | 'interrupted'", " | 'interrupted'\n | 'task_incomplete'") with open(EVENTS, 'w') as f: f.write(c) p("events.ts", r1) # --- PATCH 2: sse-chat-transport.ts --- with open(SSE) as f: c = f.read() r2 = "case 'task_incomplete'" not in c if r2: block = """ case 'task_incomplete': (sideChannel as any).onTaskIncomplete( (event.data?.incomplete_plan as Array<{ id: string; content: string; status: string }>) || [], ); break; default:""" c = c.replace("\n default:", "\n" + block) with open(SSE, 'w') as f: f.write(c) p("sse.ts", r2) # --- PATCH 3: useAgentChat.ts --- with open(HOOK) as f: lines = f.readlines() r3 = True new_lines = [] state_done = False handler_done = False efx_done = False ret_done = False for line in lines: if "import { useCallback, useEffect, useMemo, useRef } from 'react';" in line: line = line.replace("useRef }", "useRef, useState }") if "callbacksRef.current = { onReady, onError, onSessionDead };" in line and not state_done: new_lines.append(line) new_lines.append(" // Auto-continue state\n") new_lines.append(" const [_showAc, _setShowAc] = useState(false);\n") new_lines.append(" const _acRef = useRef>([]);\n") state_done = True continue if "onSessionUpdate: (data) => {" in line and not handler_done: new_lines.append(line) new_lines.append(" // Auto-continue check\n") new_lines.append(" if ((data as any).ac_plan) {\n") new_lines.append(" _acRef.current = (data as any).ac_plan;\n") new_lines.append(" _setShowAc(true);\n") new_lines.append(" return;\n") new_lines.append(" }\n") handler_done = True continue stripped = line.strip() if stripped == "return {" and not efx_done: new_lines.append(" const _acCancel = useCallback(() => { _setShowAc(false); }, []);\n") new_lines.append(" useEffect(() => {\n") new_lines.append(" if (!_showAc) return;\n") new_lines.append(" const plan = _acRef.current;\n") new_lines.append(" const timer = setTimeout(() => {\n") new_lines.append(" const s = chatActionsRef.current.setMessages;\n") new_lines.append(" const m = chatActionsRef.current.messages;\n") new_lines.append(" if (s && plan.length > 0) {\n") new_lines.append(" const t = plan.map(i => `- ${i.content}`).join('\\n');\n") new_lines.append(" const nu = { id: 'ac-'+Date.now(), role: 'user' as const, parts: [{ type: 'text' as const, text: `[TIEP TUC] Task chua hoan thanh:\\n${t}` }], content: '' };\n") new_lines.append(" s([...m, nu]);\n") new_lines.append(" }\n") new_lines.append(" _setShowAc(false);\n") new_lines.append(" }, 10000);\n") new_lines.append(" return () => clearTimeout(timer);\n") new_lines.append(" }, [_showAc]);\n") efx_done = True if "refreshMessages," in line and not ret_done: new_lines.append(line) new_lines.append(" _showAc,\n") new_lines.append(" _acCancel,\n") ret_done = True continue new_lines.append(line) if not all([state_done, handler_done, efx_done, ret_done]): r3 = False print(f" MISSING: state={state_done} handler={handler_done} effect={efx_done} ret={ret_done}") else: with open(HOOK, 'w') as f: f.writelines(new_lines) p("useAgentChat.ts", r3) # --- PATCH 4: ChatInput.tsx --- with open(INPUT) as f: c = f.read() r4 = True if "Button" not in c: c = c.replace(" Tooltip,", " Tooltip,\n Button,") if "_showAc" not in c: old = "placeholder?: string;" new = "placeholder?: string;\n _showAc?: boolean;\n _acCancel?: () => void;" c = c.replace(old, new) old2 = "placeholder = 'Ask anything...' }: ChatInputProps) {" new2 = "placeholder = 'Ask anything...', _showAc = false, _acCancel }: ChatInputProps) {" c = c.replace(old2, new2) btn = """ {_showAc && _acCancel && ( )} """ c = c.replace("