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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""V17: Auto-continue - perfect Python, exact marker matching"""
|
| 2 |
+
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
EVENTS = "/source/frontend/src/types/events.ts"
|
| 6 |
+
SSE = "/source/frontend/src/lib/sse-chat-transport.ts"
|
| 7 |
+
HOOK = "/source/frontend/src/hooks/useAgentChat.ts"
|
| 8 |
+
INPUT = "/source/frontend/src/components/Chat/ChatInput.tsx"
|
| 9 |
+
SESS = "/source/frontend/src/components/SessionChat.tsx"
|
| 10 |
+
AGENT = "/app/agent/core/agent_loop.py"
|
| 11 |
+
|
| 12 |
+
print("Patching auto-continue V17...")
|
| 13 |
+
|
| 14 |
+
# --- events.ts ---
|
| 15 |
+
with open(EVENTS) as f:
|
| 16 |
+
c = f.read()
|
| 17 |
+
if "'task_incomplete'" not in c:
|
| 18 |
+
c = c.replace(" | 'interrupted'", " | 'interrupted'\n | 'task_incomplete'")
|
| 19 |
+
with open(EVENTS, 'w') as f:
|
| 20 |
+
f.write(c)
|
| 21 |
+
print("OK: events.ts")
|
| 22 |
+
|
| 23 |
+
# --- sse.ts ---
|
| 24 |
+
with open(SSE) as f:
|
| 25 |
+
c = f.read()
|
| 26 |
+
if "case 'task_incomplete'" not in c:
|
| 27 |
+
block = (
|
| 28 |
+
" case 'task_incomplete':\n"
|
| 29 |
+
" (sideChannel as any).onTaskIncomplete(\n"
|
| 30 |
+
" (event.data?.incomplete_plan as Array<{ id: string; content: string; status: string }>) || [],\n"
|
| 31 |
+
" );\n"
|
| 32 |
+
" break;\n\n"
|
| 33 |
+
" default:"
|
| 34 |
+
)
|
| 35 |
+
c = c.replace("\n default:", "\n" + block)
|
| 36 |
+
with open(SSE, 'w') as f:
|
| 37 |
+
f.write(c)
|
| 38 |
+
print("OK: sse.ts")
|
| 39 |
+
|
| 40 |
+
# --- useAgentChat.ts ---
|
| 41 |
+
with open(HOOK) as f:
|
| 42 |
+
content = f.read()
|
| 43 |
+
|
| 44 |
+
# Add useState import
|
| 45 |
+
if "useState" not in content:
|
| 46 |
+
content = content.replace(
|
| 47 |
+
"import { useCallback, useEffect, useMemo, useRef } from 'react';",
|
| 48 |
+
"import { useCallback, useEffect, useMemo, useRef, useState } from 'react';"
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
# Add state after callbacksRef
|
| 52 |
+
if "_showAc" not in content:
|
| 53 |
+
state_code = (
|
| 54 |
+
" // Auto-continue state\n"
|
| 55 |
+
" const [_showAc, _setShowAc] = useState(false);\n"
|
| 56 |
+
" const _acRef = useRef<Array<{ id: string; content: string; status: string }>>([]);\n"
|
| 57 |
+
)
|
| 58 |
+
content = content.replace(
|
| 59 |
+
"callbacksRef.current = { onReady, onError, onSessionDead };",
|
| 60 |
+
"callbacksRef.current = { onReady, onError, onSessionDead };\n" + state_code
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
# Add handler in onSessionUpdate
|
| 64 |
+
if "ac_plan" not in content:
|
| 65 |
+
h = (
|
| 66 |
+
" // Auto-continue check\n"
|
| 67 |
+
" if ((data as any).ac_plan) {\n"
|
| 68 |
+
" _acRef.current = (data as any).ac_plan;\n"
|
| 69 |
+
" _setShowAc(true);\n"
|
| 70 |
+
" return;\n"
|
| 71 |
+
" }\n"
|
| 72 |
+
)
|
| 73 |
+
content = content.replace("onSessionUpdate: (data) => {", "onSessionUpdate: (data) => {\n" + h)
|
| 74 |
+
|
| 75 |
+
# ONLY match the unique two-line pattern: " return {\n messages: chat.messages,"
|
| 76 |
+
UNIQUE_MARKER = " return {\n messages: chat.messages,"
|
| 77 |
+
if UNIQUE_MARKER in content and "_acCancel" not in content:
|
| 78 |
+
insert = (
|
| 79 |
+
" const _acCancel = useCallback(() => { _setShowAc(false); }, []);\n"
|
| 80 |
+
" useEffect(() => {\n"
|
| 81 |
+
" if (!_showAc) return;\n"
|
| 82 |
+
" const plan = _acRef.current;\n"
|
| 83 |
+
" const timer = setTimeout(() => {\n"
|
| 84 |
+
" const s = chatActionsRef.current.setMessages;\n"
|
| 85 |
+
" const m = chatActionsRef.current.messages;\n"
|
| 86 |
+
" if (s && plan.length > 0) {\n"
|
| 87 |
+
" const t = plan.map((i: { content: string }) => '- ' + i.content).join('\\\\n');\n"
|
| 88 |
+
" 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"
|
| 89 |
+
" s([...m, nu]);\n"
|
| 90 |
+
" }\n"
|
| 91 |
+
" _setShowAc(false);\n"
|
| 92 |
+
" }, 10000);\n"
|
| 93 |
+
" return () => clearTimeout(timer);\n"
|
| 94 |
+
" }, [_showAc]);\n\n"
|
| 95 |
+
)
|
| 96 |
+
content = content.replace(UNIQUE_MARKER, insert + UNIQUE_MARKER)
|
| 97 |
+
|
| 98 |
+
# Add to return
|
| 99 |
+
content = content.replace(
|
| 100 |
+
"refreshMessages,\n };",
|
| 101 |
+
"refreshMessages,\n _showAc,\n _acCancel,\n };"
|
| 102 |
+
)
|
| 103 |
+
|
| 104 |
+
with open(HOOK, 'w') as f:
|
| 105 |
+
f.write(content)
|
| 106 |
+
print("OK: useAgentChat.ts")
|
| 107 |
+
|
| 108 |
+
# --- ChatInput.tsx ---
|
| 109 |
+
with open(INPUT) as f:
|
| 110 |
+
content = f.read()
|
| 111 |
+
|
| 112 |
+
# Add Button (check exact substring, not "IconButton")
|
| 113 |
+
if " Button,\n" not in content and " Button\n" not in content:
|
| 114 |
+
content = content.replace(
|
| 115 |
+
" Tooltip,",
|
| 116 |
+
" Tooltip,\n Button,"
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
if "_showAc" not in content:
|
| 120 |
+
content = content.replace(
|
| 121 |
+
"placeholder?: string;",
|
| 122 |
+
"placeholder?: string;\n _showAc?: boolean;\n _acCancel?: () => void;"
|
| 123 |
+
)
|
| 124 |
+
content = content.replace(
|
| 125 |
+
"placeholder = 'Ask anything...' }: ChatInputProps) {",
|
| 126 |
+
"placeholder = 'Ask anything...', _showAc = false, _acCancel }: ChatInputProps) {"
|
| 127 |
+
)
|
| 128 |
+
btn = (
|
| 129 |
+
" {_showAc && _acCancel && (\n"
|
| 130 |
+
" <Box sx={{ display: 'flex', justifyContent: 'center', mt: 1 }}>\n"
|
| 131 |
+
" <Button variant=\"outlined\" size=\"small\" onClick={_acCancel}\n"
|
| 132 |
+
" sx={{ textTransform: 'none', fontSize: '0.7rem', opacity: 0.7, '&:hover': { opacity: 1 } }}>\n"
|
| 133 |
+
" TAM_DUNG_TU_DONG_TIEP_TUC (10s)\n"
|
| 134 |
+
" </Button>\n"
|
| 135 |
+
" </Box>\n"
|
| 136 |
+
" )}\n"
|
| 137 |
+
)
|
| 138 |
+
content = content.replace("<JobsUpgradeDialog", btn + " <JobsUpgradeDialog")
|
| 139 |
+
|
| 140 |
+
with open(INPUT, 'w') as f:
|
| 141 |
+
f.write(content)
|
| 142 |
+
print("OK: ChatInput.tsx")
|
| 143 |
+
|
| 144 |
+
# --- SessionChat.tsx ---
|
| 145 |
+
if os.path.exists(SESS):
|
| 146 |
+
with open(SESS) as f:
|
| 147 |
+
c = f.read()
|
| 148 |
+
if "_showAc" not in c:
|
| 149 |
+
c = c.replace(
|
| 150 |
+
"<ChatInput ",
|
| 151 |
+
"<ChatInput _showAc={_showAc} _acCancel={_acCancel} "
|
| 152 |
+
)
|
| 153 |
+
with open(SESS, 'w') as f:
|
| 154 |
+
f.write(c)
|
| 155 |
+
print("OK: SessionChat.tsx")
|
| 156 |
+
|
| 157 |
+
# --- agent_loop.py ---
|
| 158 |
+
if os.path.exists(AGENT):
|
| 159 |
+
with open(AGENT) as f:
|
| 160 |
+
c = f.read()
|
| 161 |
+
|
| 162 |
+
if "_unfinished_plan" not in c:
|
| 163 |
+
fn = (
|
| 164 |
+
"\n\ndef _unfinished_plan(s):\n"
|
| 165 |
+
" p = getattr(s, 'current_plan', None) or []\n"
|
| 166 |
+
" return [it for it in p if it.get('status') in ('pending', 'in_progress')]\n\n"
|
| 167 |
+
)
|
| 168 |
+
c = c.replace("class Handlers:", fn + "class Handlers:")
|
| 169 |
+
|
| 170 |
+
if "ac_plan" not in c:
|
| 171 |
+
check = (
|
| 172 |
+
"\n if not llm_result.tool_calls_acc and llm_result.content:\n"
|
| 173 |
+
" unfinished = _unfinished_plan(session)\n"
|
| 174 |
+
" if unfinished:\n"
|
| 175 |
+
" await session.send_event(Event(event_type='session_update', data={\n"
|
| 176 |
+
" 'ac_plan': unfinished,\n"
|
| 177 |
+
" }))\n"
|
| 178 |
+
)
|
| 179 |
+
c = c.replace(" # -- End of turn --", check + " # -- End of turn --")
|
| 180 |
+
|
| 181 |
+
with open(AGENT, 'w') as f:
|
| 182 |
+
f.write(c)
|
| 183 |
+
print("OK: agent_loop.py")
|
| 184 |
+
else:
|
| 185 |
+
print("SKIP: agent_loop.py")
|
| 186 |
+
|
| 187 |
+
print("DONE: V17")
|