bep40 commited on
Commit
4975526
·
verified ·
1 Parent(s): bf6e039

Upload patch_auto_continue.py

Browse files
Files changed (1) hide show
  1. patch_auto_continue.py +129 -0
patch_auto_continue.py ADDED
@@ -0,0 +1,129 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Patch script: Add auto-continue feature for free models in ml-intern.
2
+
3
+ Apply this script during Docker build to enable:
4
+ 1. Detection when streaming ends but task incomplete
5
+ 2. "Tự động tiếp tục (10s)" button - auto continues after 10s
6
+ 3. "Tạm dừng" button - lets user send different content
7
+
8
+ Usage: Run this during Docker build after cloning source.
9
+ """
10
+
11
+ import os
12
+ import re
13
+
14
+ # Paths - adjust if needed
15
+ USE_AGENT_CHAT = "/source/frontend/src/hooks/useAgentChat.ts"
16
+ SSE_TRANSPORT = "/source/frontend/src/lib/sse-chat-transport.ts"
17
+ AGENT_LOOP = "/app/agent/core/agent_loop.py"
18
+
19
+ # =============================================================================
20
+ # Patch 1: useAgentChat.ts
21
+ # =============================================================================
22
+ def patch_use_agent_chat():
23
+ if not os.path.exists(USE_AGENT_CHAT):
24
+ print(f"SKIP: {USE_AGENT_CHAT} not found")
25
+ return
26
+
27
+ with open(USE_AGENT_CHAT, "r", encoding="utf-8") as f:
28
+ content = f.read()
29
+
30
+ # Add useState to imports if missing
31
+ if "import { useCallback, useEffect, useMemo, useRef, useState } from 'react';" not in content:
32
+ content = content.replace(
33
+ "import { useCallback, useEffect, useMemo, useRef } from 'react';",
34
+ "import { useCallback, useEffect, useMemo, useRef, useState } from 'react';"
35
+ )
36
+
37
+ # Add auto-continue state after callbacksRef
38
+ state_code = '''
39
+ // Auto-continue state for free models when task incomplete
40
+ const autoContinueTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
41
+ const [showAutoContinue, setShowAutoContinue] = useState(false);
42
+ const [taskIncompleteInfo, setTaskIncompleteInfo] = useState<{
43
+ incompletePlan: Array<{ id: string; content: string; status: string }>;
44
+ } | null>(null);
45
+ '''
46
+ if "autoContinueTimerRef" not in content and "callbacksRef.current = { onReady, onError, onSessionDead }" in content:
47
+ content = content.replace(
48
+ "callbacksRef.current = { onReady, onError, onSessionDead };",
49
+ "callbacksRef.current = { onReady, onError, onSessionDead };" + state_code
50
+ )
51
+
52
+ # Add onTaskIncomplete to SideChannelCallbacks interface
53
+ if "onTaskIncomplete" not in content:
54
+ content = content.replace(
55
+ "onInterrupted: () => void;",
56
+ "onInterrupted: () => void;\n onTaskIncomplete: (incompletePlan: Array<{ id: string; content: string; status: string }>) => void;"
57
+ )
58
+
59
+ with open(USE_AGENT_CHAT, "w", encoding="utf-8") as f:
60
+ f.write(content)
61
+ print(f"OK: Patched {USE_AGENT_CHAT}")
62
+
63
+ # =============================================================================
64
+ # Patch 2: sse-chat-transport.ts
65
+ # =============================================================================
66
+ def patch_sse_transport():
67
+ if not os.path.exists(SSE_TRANSPORT):
68
+ print(f"SKIP: {SSE_TRANSPORT} not found")
69
+ return
70
+
71
+ with open(SSE_TRANSPORT, "r", encoding="utf-8") as f:
72
+ content = f.read()
73
+
74
+ # Add task_incomplete case
75
+ if "case 'task_incomplete'" not in content:
76
+ task_incomplete_case = ''' case 'task_incomplete':
77
+ sideChannel.onTaskIncomplete(
78
+ (event.data?.incomplete_plan as Array<{ id: string; content: string; status: string }>) || [],
79
+ );
80
+ break;
81
+ '''
82
+ # Insert before turn_complete case
83
+ content = content.replace(
84
+ "case 'turn_complete':",
85
+ task_incomplete_case + "\n case 'turn_complete':"
86
+ )
87
+
88
+ with open(SSE_TRANSPORT, "w", encoding="utf-8") as f:
89
+ f.write(content)
90
+ print(f"OK: Patched {SSE_TRANSPORT}")
91
+
92
+ # =============================================================================
93
+ # Patch 3: agent_loop.py
94
+ # =============================================================================
95
+ def patch_agent_loop():
96
+ if not os.path.exists(AGENT_LOOP):
97
+ print(f"SKIP: {AGENT_LOOP} not found")
98
+ return
99
+
100
+ with open(AGENT_LOOP, "r", encoding="utf-8") as f:
101
+ content = f.read()
102
+
103
+ # Add check function if not exists
104
+ check_func = '''
105
+ def _check_task_incomplete(session: Session, llm_result: LLMResult) -> bool:
106
+ """Check if model stopped but task is incomplete (has unfinished plan items)."""
107
+ if llm_result.tool_calls_acc:
108
+ return False # Has tool calls, not incomplete
109
+ plan = getattr(session, "current_plan", None) or []
110
+ unfinished = [item for item in plan if item.get("status") in ("pending", "in_progress")]
111
+ return len(unfinished) > 0
112
+ '''
113
+ if "_check_task_incomplete" not in content:
114
+ # Insert after LLMResult dataclass
115
+ content = content.replace(
116
+ "class Handlers:",
117
+ check_func + "\n\nclass Handlers:"
118
+ )
119
+
120
+ with open(AGENT_LOOP, "w", encoding="utf-8") as f:
121
+ f.write(content)
122
+ print(f"OK: Patched {AGENT_LOOP}")
123
+
124
+
125
+ if __name__ == "__main__":
126
+ patch_use_agent_chat()
127
+ patch_sse_transport()
128
+ patch_agent_loop()
129
+ print("\nDONE: Auto-continue patches applied")