Spaces:
Running
Running
V16: Match exact two-line return pattern - THE final fix
Browse files- patch_auto_continue_v16.py +142 -0
patch_auto_continue_v16.py
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""V16: Auto-continue - match exact 'return {\n messages: chat.messages,' (only one in file)"""
|
| 2 |
+
|
| 3 |
+
import os, sys
|
| 4 |
+
|
| 5 |
+
F = lambda n: "/source/frontend/src/" + n
|
| 6 |
+
EVENTS = F("types/events.ts")
|
| 7 |
+
SSE = F("lib/sse-chat-transport.ts")
|
| 8 |
+
HOOK = F("hooks/useAgentChat.ts")
|
| 9 |
+
INPUT = F("components/Chat/ChatInput.tsx")
|
| 10 |
+
SESS = F("components/SessionChat.tsx")
|
| 11 |
+
AGENT = "/app/agent/core/agent_loop.py"
|
| 12 |
+
|
| 13 |
+
def ok(f): print(f"OK: {f}")
|
| 14 |
+
|
| 15 |
+
# --- events.ts ---
|
| 16 |
+
with open(EVENTS) as f: c = f.read()
|
| 17 |
+
if "'task_incomplete'" not in c:
|
| 18 |
+
c = c.replace(" | 'interrupted'", " | 'interrupted'\n | 'task_incomplete'")
|
| 19 |
+
open(EVENTS, 'w').write(c)
|
| 20 |
+
ok("events.ts")
|
| 21 |
+
|
| 22 |
+
# --- sse.ts ---
|
| 23 |
+
with open(SSE) as f: c = f.read()
|
| 24 |
+
if "case 'task_incomplete'" not in c:
|
| 25 |
+
c = c.replace("\n default:", "\n case 'task_incomplete':\n (sideChannel as any).onTaskIncomplete(\n (event.data?.incomplete_plan as Array<{ id: string; content: string; status: string }>) || [],\n );\n break;\n\n default:")
|
| 26 |
+
open(SSE, 'w').write(c)
|
| 27 |
+
ok("sse.ts")
|
| 28 |
+
|
| 29 |
+
# --- useAgentChat.ts ---
|
| 30 |
+
with open(HOOK) as f: content = f.read()
|
| 31 |
+
|
| 32 |
+
# useState import
|
| 33 |
+
if "useState" not in content:
|
| 34 |
+
content = content.replace("useRef }", "useRef, useState }")
|
| 35 |
+
|
| 36 |
+
# State after callbacksRef (insert once)
|
| 37 |
+
if "_showAc" not in content:
|
| 38 |
+
s = (" // Auto-continue state\n"
|
| 39 |
+
" const [_showAc, _setShowAc] = useState(false);\n"
|
| 40 |
+
" const _acRef = useRef<Array<{ id: string; content: string; status: string }>>([]);\n")
|
| 41 |
+
content = content.replace(
|
| 42 |
+
"callbacksRef.current = { onReady, onError, onSessionDead };",
|
| 43 |
+
"callbacksRef.current = { onReady, onError, onSessionDead };\n" + s
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
# Handler in onSessionUpdate
|
| 47 |
+
if "ac_plan" not in content:
|
| 48 |
+
h = (" // Auto-continue check\n"
|
| 49 |
+
" if ((data as any).ac_plan) {\n"
|
| 50 |
+
" _acRef.current = (data as any).ac_plan;\n"
|
| 51 |
+
" _setShowAc(true);\n"
|
| 52 |
+
" return;\n"
|
| 53 |
+
" }\n")
|
| 54 |
+
content = content.replace("onSessionUpdate: (data) => {", "onSessionUpdate: (data) => {\n" + h)
|
| 55 |
+
|
| 56 |
+
# CRITICAL: Only match the exact two-line return that has messages: chat.messages
|
| 57 |
+
# Only ONE place in the file has this exact pattern
|
| 58 |
+
marker = " return {\n messages: chat.messages,"
|
| 59 |
+
if marker in content and "_acCancel" not in content:
|
| 60 |
+
cancel_effect = (
|
| 61 |
+
" const _acCancel = useCallback(() => { _setShowAc(false); }, []);\n"
|
| 62 |
+
" useEffect(() => {\n"
|
| 63 |
+
" if (!_showAc) return;\n"
|
| 64 |
+
" const plan = _acRef.current;\n"
|
| 65 |
+
" const timer = setTimeout(() => {\n"
|
| 66 |
+
" const s = chatActionsRef.current.setMessages;\n"
|
| 67 |
+
" const m = chatActionsRef.current.messages;\n"
|
| 68 |
+
" if (s && plan.length > 0) {\n"
|
| 69 |
+
" const t = plan.map((i: { content: string }) => '- ' + i.content).join('\\\\n');\n"
|
| 70 |
+
" 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"
|
| 71 |
+
" s([...m, nu]);\n"
|
| 72 |
+
" }\n"
|
| 73 |
+
" _setShowAc(false);\n"
|
| 74 |
+
" }, 10000);\n"
|
| 75 |
+
" return () => clearTimeout(timer);\n"
|
| 76 |
+
" }, [_showAc]);\n\n"
|
| 77 |
+
)
|
| 78 |
+
content = content.replace(marker, cancel_effect + marker)
|
| 79 |
+
|
| 80 |
+
# Add to return values
|
| 81 |
+
content = content.replace(
|
| 82 |
+
"refreshMessages,\n };",
|
| 83 |
+
"refreshMessages,\n _showAc,\n _acCancel,\n };"
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
with open(HOOK, 'w').write(content)
|
| 87 |
+
ok("useAgentChat.ts")
|
| 88 |
+
|
| 89 |
+
# --- ChatInput.tsx ---
|
| 90 |
+
with open(INPUT) as f: content = f.read()
|
| 91 |
+
|
| 92 |
+
# Button: check for standalone ", Button" or " Button" NOT "IconButton"
|
| 93 |
+
if ", Button,\n" not in content and ", Button\n" not in content:
|
| 94 |
+
content = content.replace(
|
| 95 |
+
" Tooltip,",
|
| 96 |
+
" Tooltip,\n Button,"
|
| 97 |
+
)
|
| 98 |
+
|
| 99 |
+
if "_showAc" not in content:
|
| 100 |
+
content = content.replace("placeholder?: string;", "placeholder?: string;\n _showAc?: boolean;\n _acCancel?: () => void;")
|
| 101 |
+
content = content.replace("placeholder = 'Ask anything...' }: ChatInputProps) {",
|
| 102 |
+
"placeholder = 'Ask anything...', _showAc = false, _acCancel }: ChatInputProps) {")
|
| 103 |
+
btn = (" {_showAc && _acCancel && (\n"
|
| 104 |
+
" <Box sx={{ display: 'flex', justifyContent: 'center', mt: 1 }}>\n"
|
| 105 |
+
" <Button variant=\"outlined\" size=\"small\" onClick={_acCancel}\n"
|
| 106 |
+
" sx={{ textTransform: 'none', fontSize: '0.7rem', opacity: 0.7, '&:hover': { opacity: 1 } }}>\n"
|
| 107 |
+
" TAM_DUNG_TU_DONG_TIEP_TUC (10s)\n"
|
| 108 |
+
" </Button>\n"
|
| 109 |
+
" </Box>\n"
|
| 110 |
+
" )}\n")
|
| 111 |
+
content = content.replace("<JobsUpgradeDialog", btn + " <JobsUpgradeDialog")
|
| 112 |
+
|
| 113 |
+
open(INPUT, 'w').write((content).read() if hasattr(content, 'read') else content)
|
| 114 |
+
ok("ChatInput.tsx")
|
| 115 |
+
|
| 116 |
+
# --- SessionChat.tsx ---
|
| 117 |
+
if os.path.exists(SESS):
|
| 118 |
+
with open(SESS) as f: c = f.read()
|
| 119 |
+
if "_showAc" not in c:
|
| 120 |
+
c = c.replace("<ChatInput ", "<ChatInput _showAc={_showAc} _acCancel={_acCancel} ")
|
| 121 |
+
open(SESS, 'w').write(c)
|
| 122 |
+
ok("SessionChat.tsx")
|
| 123 |
+
|
| 124 |
+
# --- agent_loop.py ---
|
| 125 |
+
if os.path.exists(AGENT):
|
| 126 |
+
with open(AGENT) as f: c = f.read()
|
| 127 |
+
if "_unfinished_plan" not in c:
|
| 128 |
+
c = c.replace("class Handlers:", "\n\ndef _unfinished_plan(s):\n p = getattr(s, 'current_plan', None) or []\n return [it for it in p if it.get('status') in ('pending', 'in_progress')]\n\n\nclass Handlers:")
|
| 129 |
+
if "ac_plan" not in c:
|
| 130 |
+
check = ("\n if not llm_result.tool_calls_acc and llm_result.content:\n"
|
| 131 |
+
" unfinished = _unfinished_plan(session)\n"
|
| 132 |
+
" if unfinished:\n"
|
| 133 |
+
" await session.send_event(Event(event_type='session_update', data={\n"
|
| 134 |
+
" 'ac_plan': unfinished,\n"
|
| 135 |
+
" }))\n")
|
| 136 |
+
c = c.replace(" # -- End of turn --", check + " # -- End of turn --")
|
| 137 |
+
open(AGENT, 'w').write(c)
|
| 138 |
+
ok("agent_loop.py")
|
| 139 |
+
else:
|
| 140 |
+
print("SKIP: agent_loop.py")
|
| 141 |
+
|
| 142 |
+
print("\nDONE: V16")
|