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

Upload AUTO_CONTINUE_FEATURE.md

Browse files
Files changed (1) hide show
  1. AUTO_CONTINUE_FEATURE.md +183 -0
AUTO_CONTINUE_FEATURE.md ADDED
@@ -0,0 +1,183 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Auto-Continue Feature for Free Models
2
+
3
+ ## Tổng quan
4
+ Khi model miễn phí dùng OpenRouter phản hồi bị dừng nhưng nhiệm vụ chưa hoàn thành, hệ thống sẽ:
5
+ 1. Phát hiện task incomplete dựa vào trạng thái plan
6
+ 2. Hiển thị 2 nút: "Tự động tiếp tục (10s)" và "Tạm dừng"
7
+
8
+ ## Các file cần sửa
9
+
10
+ ### 1. frontend/src/hooks/useAgentChat.ts
11
+
12
+ ```typescript
13
+ // Thêm vào import
14
+ import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
15
+
16
+ // Trong useAgentChat function, sau callbacksRef
17
+ const autoContinueTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
18
+ const [showAutoContinue, setShowAutoContinue] = useState(false);
19
+ const [taskIncompleteInfo, setTaskIncompleteInfo] = useState<{
20
+ incompletePlan: Array<{ id: string; content: string; status: string }>;
21
+ } | null>(null);
22
+
23
+ // Thêm vào SideChannelCallbacks interface
24
+ export interface SideChannelCallbacks {
25
+ // ... existing callbacks ...
26
+ onTaskIncomplete: (incompletePlan: Array<{ id: string; content: string; status: string }>) => void;
27
+ }
28
+
29
+ // Thêm vào sideChannel trong useMemo
30
+ onTaskIncomplete: (incompletePlan) => {
31
+ setTaskIncompleteInfo({ incompletePlan });
32
+ setShowAutoContinue(true);
33
+ if (autoContinueTimerRef.current) {
34
+ clearTimeout(autoContinueTimerRef.current);
35
+ }
36
+ autoContinueTimerRef.current = setTimeout(() => {
37
+ startAutoContinue();
38
+ }, 10000);
39
+ },
40
+
41
+ // Thêm hàm mới
42
+ const startAutoContinue = useCallback(() => {
43
+ setShowAutoContinue(false);
44
+ setTaskIncompleteInfo(null);
45
+ if (autoContinueTimerRef.current) {
46
+ clearTimeout(autoContinueTimerRef.current);
47
+ autoContinueTimerRef.current = null;
48
+ }
49
+ const incompleteItems = taskIncompleteInfo?.incompletePlan || [];
50
+ const continuationText = incompleteItems.length > 0
51
+ ? `Tiếp tục từ các task chưa hoàn thành: ${incompleteItems.map(i => `- ${i.content}`).join('\n')}`
52
+ : 'Tiếp tục nhiệm vụ.';
53
+ chat.sendMessage({
54
+ text: `[TỰ ĐỘNG TIẾP TỤC] ${continuationText}`,
55
+ metadata: { createdAt: new Date().toISOString() }
56
+ });
57
+ }, [taskIncompleteInfo, chat]);
58
+
59
+ const cancelAutoContinue = useCallback(() => {
60
+ setShowAutoContinue(false);
61
+ setTaskIncompleteInfo(null);
62
+ if (autoContinueTimerRef.current) {
63
+ clearTimeout(autoContinueTimerRef.current);
64
+ autoContinueTimerRef.current = null;
65
+ }
66
+ }, []);
67
+
68
+ // Thêm vào return
69
+ return {
70
+ messages: chat.messages,
71
+ sendMessage: chat.sendMessage,
72
+ stop,
73
+ status: chat.status,
74
+ undoLastTurn,
75
+ editAndRegenerate,
76
+ approveTools,
77
+ refreshMessages,
78
+ showAutoContinue,
79
+ taskIncompleteInfo,
80
+ startAutoContinue,
81
+ cancelAutoContinue,
82
+ };
83
+ ```
84
+
85
+ ### 2. frontend/src/components/Chat/ChatInput.tsx
86
+
87
+ ```typescript
88
+ // Thêm import
89
+ import { Box, IconButton, Stack, Tooltip, Button } from '@mui/material';
90
+
91
+ // Thêm props
92
+ interface ChatInputProps {
93
+ // ... existing props
94
+ showAutoContinue?: boolean;
95
+ taskIncompleteInfo?: { incompletePlan: Array<{ id: string; content: string; status: string }> } | null;
96
+ startAutoContinue?: () => void;
97
+ cancelAutoContinue?: () => void;
98
+ }
99
+
100
+ // Thêm UI buttons (trước hoặc sau input area)
101
+ {showAutoContinue && taskIncompleteInfo && (
102
+ <Stack direction="row" spacing={1} sx={{ mb: 1, px: 2 }}>
103
+ <Button
104
+ variant="contained"
105
+ color="primary"
106
+ size="small"
107
+ onClick={startAutoContinue}
108
+ sx={{ textTransform: 'none' }}
109
+ >
110
+ Tự động tiếp tục (10s)
111
+ </Button>
112
+ <Button
113
+ variant="outlined"
114
+ color="secondary"
115
+ size="small"
116
+ onClick={cancelAutoContinue}
117
+ sx={{ textTransform: 'none' }}
118
+ >
119
+ Tạm dừng
120
+ </Button>
121
+ </Stack>
122
+ )}
123
+ ```
124
+
125
+ ### 3. frontend/src/lib/sse-chat-transport.ts
126
+
127
+ ```typescript
128
+ // Thêm vào case trong createEventToChunkStream
129
+ case 'task_incomplete':
130
+ sideChannel.onTaskIncomplete(
131
+ (event.data?.incomplete_plan as Array<{ id: string; content: string; status: string }>) || [],
132
+ );
133
+ break;
134
+ ```
135
+
136
+ ### 4. agent/core/agent_loop.py
137
+
138
+ ```python
139
+ # Trong run_agent, sau khi xử lý LLM response
140
+ def _check_task_incomplete(session: Session, llm_result: LLMResult) -> bool:
141
+ """Kiểm tra nếu model dừng nhưng task chưa hoàn thành."""
142
+ if llm_result.tool_calls_acc:
143
+ return False # Có tool calls thì chưa incomplete
144
+
145
+ plan = getattr(session, "current_plan", None) or []
146
+ unfinished = [item for item in plan if item.get("status") in ("pending", "in_progress")]
147
+
148
+ return len(unfinished) > 0
149
+
150
+ # Gọi sau khi nhận kết quả LLM:
151
+ if _check_task_incomplete(session, llm_result):
152
+ unfinished = [item for item in plan if item.get("status") in ("pending", "in_progress")]
153
+ await session.send_event(
154
+ Event(
155
+ event_type="task_incomplete",
156
+ data={
157
+ "incomplete_plan": unfinished,
158
+ "message": "Model stopped streaming but task is incomplete."
159
+ }
160
+ )
161
+ )
162
+ ```
163
+
164
+ ## Logic hoạt động
165
+
166
+ 1. Khi agent loop phát hiện model trả lời không có tool calls nhưng plan còn incomplete
167
+ 2. Backend gửi event `task_incomplete` với danh sách task chưa hoàn thành
168
+ 3. Frontend nhận event qua `onTaskIncomplete` callback
169
+ 4. Hiển thị 2 nút:
170
+ - **Tự động tiếp tục (10s)**: Sau 10 giây sẽ tự động gửi tin nhắn tiếp tục
171
+ - **Tạm dừng**: Hủy timer, cho phép user nhập nội dung mới
172
+
173
+ ## Áp dụng patch
174
+
175
+ ```bash
176
+ # Tải source
177
+ git clone https://huggingface.co/spaces/smolagents/ml-intern /tmp/source
178
+
179
+ # Sửa các file trên
180
+ # Sau đó rebuild frontend
181
+ cd /tmp/source/frontend
182
+ npm install && npm run build
183
+ ```