@aivin-labs/cli 1.0.4 → 1.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +263 -216
- package/bin/cli.mjs +1780 -1361
- package/bin/lib/browserAutomation.mjs +655 -648
- package/bin/lib/browserTui.mjs +276 -245
- package/bin/lib/cancellation.mjs +61 -0
- package/bin/lib/codegen.mjs +61 -5
- package/bin/lib/connectors.mjs +55 -21
- package/bin/lib/deploy.mjs +25 -2
- package/bin/lib/mcpProxy.mjs +869 -773
- package/bin/lib/mission.mjs +13 -1
- package/bin/lib/missionStream.mjs +696 -660
- package/bin/lib/pluginAuthResolve.mjs +127 -0
- package/bin/lib/pluginTrigger.mjs +207 -90
- package/bin/lib/releaseKeys.mjs +9 -0
- package/bin/lib/remote/activityLog.mjs +68 -0
- package/bin/lib/remote/autoStart.mjs +457 -0
- package/bin/lib/remote/autoUpdate.mjs +129 -0
- package/bin/lib/remote/brand.mjs +60 -0
- package/bin/lib/remote/browserTunnel.mjs +292 -0
- package/bin/lib/remote/callLedger.mjs +134 -0
- package/bin/lib/remote/cdpPolicy.mjs +66 -0
- package/bin/lib/remote/client.mjs +726 -0
- package/bin/lib/remote/constants.mjs +173 -0
- package/bin/lib/remote/credentials.mjs +208 -0
- package/bin/lib/remote/daemonLock.mjs +86 -0
- package/bin/lib/remote/dailyBrowser.mjs +371 -0
- package/bin/lib/remote/deviceMemory.mjs +246 -0
- package/bin/lib/remote/deviceProfile.mjs +142 -0
- package/bin/lib/remote/deviceWatches.mjs +138 -0
- package/bin/lib/remote/docText.mjs +117 -0
- package/bin/lib/remote/fileIndexer.mjs +399 -0
- package/bin/lib/remote/gui/index.mjs +245 -0
- package/bin/lib/remote/gui/officeHostSource.mjs +393 -0
- package/bin/lib/remote/gui/windowsHost.mjs +109 -0
- package/bin/lib/remote/gui/windowsHostSource.mjs +904 -0
- package/bin/lib/remote/httpTransfer.mjs +89 -0
- package/bin/lib/remote/idempotency.mjs +135 -0
- package/bin/lib/remote/keepAwake.mjs +100 -0
- package/bin/lib/remote/localConfirm.mjs +268 -0
- package/bin/lib/remote/localPolicy.mjs +249 -0
- package/bin/lib/remote/mascot/assets.mjs +218 -0
- package/bin/lib/remote/mascot/hostSource.mjs +362 -0
- package/bin/lib/remote/mascot/index.mjs +322 -0
- package/bin/lib/remote/mascot/labels.mjs +83 -0
- package/bin/lib/remote/mascot/strings.mjs +189 -0
- package/bin/lib/remote/memberFsPool.mjs +134 -0
- package/bin/lib/remote/memberFsWorker.mjs +60 -0
- package/bin/lib/remote/mission.mjs +156 -0
- package/bin/lib/remote/missionQuestion.mjs +63 -0
- package/bin/lib/remote/multiUser.mjs +344 -0
- package/bin/lib/remote/netProxy.mjs +145 -0
- package/bin/lib/remote/notify.mjs +58 -0
- package/bin/lib/remote/office/index.mjs +90 -0
- package/bin/lib/remote/policyCommand.mjs +93 -0
- package/bin/lib/remote/processes.mjs +217 -0
- package/bin/lib/remote/sandbox.mjs +221 -0
- package/bin/lib/remote/schedule.mjs +213 -0
- package/bin/lib/remote/scheduler.mjs +124 -0
- package/bin/lib/remote/textIndex.mjs +131 -0
- package/bin/lib/remote/tokenStore.mjs +77 -0
- package/bin/lib/remote/toolHandlers.mjs +726 -0
- package/bin/lib/remote/undoStore.mjs +242 -0
- package/bin/lib/remoteControl.mjs +26 -0
- package/bin/lib/scaffold.mjs +654 -653
- package/bin/lib/selfUpdate.mjs +71 -0
- package/bin/lib/store.mjs +16 -2
- package/bin/lib/tasks.mjs +52 -8
- package/bin/lib/update/binarySwap.mjs +63 -0
- package/bin/lib/update/constants.mjs +56 -0
- package/bin/lib/update/download.mjs +145 -0
- package/bin/lib/update/signature.mjs +42 -0
- package/bin/lib/update/state.mjs +205 -0
- package/bin/lib/update/versions.mjs +60 -0
- package/bin/lib/util.mjs +8 -0
- package/docs/CLI.md +1353 -1274
- package/package.json +71 -62
package/bin/lib/browserTui.mjs
CHANGED
|
@@ -1,245 +1,276 @@
|
|
|
1
|
-
import React, { useState, useRef, useEffect } from 'react';
|
|
2
|
-
import { render, Box, Text, Static } from 'ink';
|
|
3
|
-
import TextInput from 'ink-text-input';
|
|
4
|
-
import { io } from 'socket.io-client';
|
|
5
|
-
import axios from 'axios';
|
|
6
|
-
import {
|
|
7
|
-
MISSION_LOG_EVENTS,
|
|
8
|
-
MISSION_DONE_EVENT_KEYS,
|
|
9
|
-
openBrowserViewer,
|
|
10
|
-
sendOSNotification,
|
|
11
|
-
streamBrowserMissionLog,
|
|
12
|
-
} from './missionStream.mjs';
|
|
13
|
-
|
|
14
|
-
// ── Claude-Code-style split view for `aivin browser`: log cuộn phía trên (Ink <Static>, append-only,
|
|
15
|
-
// không bao giờ re-render 1 dòng đã in — đúng thứ cần cho log liên tục), 1 dòng input cố định dưới
|
|
16
|
-
// cùng, luôn nhận gõ. Chỉ dùng khi có TTY thật (guard ở call site, browserAutomation.mjs) - Ink cần
|
|
17
|
-
// raw-mode stdin, không hoạt động khi output bị pipe/redirect. Không có bundler/JSX transform trong
|
|
18
|
-
// CLI này (chỉ publish .mjs thô) nên toàn bộ component viết bằng React.createElement thuần, không JSX.
|
|
19
|
-
const h = React.createElement;
|
|
20
|
-
|
|
21
|
-
const STEP_ICON = { planning: '📋', perceiving: '👁', executing: '▶', blocked: '🛡', recovering: '↺', auditing: '🔎', done: '✅', failed: '❌' };
|
|
22
|
-
|
|
23
|
-
let nextLineId = 0;
|
|
24
|
-
function line(text, color, dim) {
|
|
25
|
-
return { id: nextLineId++, text, color, dim };
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
function LogLine({ item }) {
|
|
29
|
-
return h(Text, { color: item.color, dimColor: item.dim }, item.text);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
function MissionTuiApp({ serverUrl, apiKey, threadId, tenantClient, idleTimeoutMs, openViewerOnHIL, onSettle }) {
|
|
33
|
-
const [lines, setLines] = useState([]);
|
|
34
|
-
const [inputValue, setInputValue] = useState('');
|
|
35
|
-
// Khác `paused=false` (mission đang tự chạy, Enter -> steer) - khi có giá trị, Enter gửi
|
|
36
|
-
// browser:resolve nhắm đúng session HIL đang treo thay vì đi qua kênh steer.
|
|
37
|
-
const [pausedSessionKey, setPausedSessionKey] = useState(null);
|
|
38
|
-
const
|
|
39
|
-
const [
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
const
|
|
43
|
-
const
|
|
44
|
-
const
|
|
45
|
-
const
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
//
|
|
55
|
-
//
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
settledRef.current
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
idleTimerRef.current
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
//
|
|
80
|
-
//
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
socket.
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
//
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
)
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
*
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
1
|
+
import React, { useState, useRef, useEffect } from 'react';
|
|
2
|
+
import { render, Box, Text, Static } from 'ink';
|
|
3
|
+
import TextInput from 'ink-text-input';
|
|
4
|
+
import { io } from 'socket.io-client';
|
|
5
|
+
import axios from 'axios';
|
|
6
|
+
import {
|
|
7
|
+
MISSION_LOG_EVENTS,
|
|
8
|
+
MISSION_DONE_EVENT_KEYS,
|
|
9
|
+
openBrowserViewer,
|
|
10
|
+
sendOSNotification,
|
|
11
|
+
streamBrowserMissionLog,
|
|
12
|
+
} from './missionStream.mjs';
|
|
13
|
+
|
|
14
|
+
// ── Claude-Code-style split view for `aivin browser`: log cuộn phía trên (Ink <Static>, append-only,
|
|
15
|
+
// không bao giờ re-render 1 dòng đã in — đúng thứ cần cho log liên tục), 1 dòng input cố định dưới
|
|
16
|
+
// cùng, luôn nhận gõ. Chỉ dùng khi có TTY thật (guard ở call site, browserAutomation.mjs) - Ink cần
|
|
17
|
+
// raw-mode stdin, không hoạt động khi output bị pipe/redirect. Không có bundler/JSX transform trong
|
|
18
|
+
// CLI này (chỉ publish .mjs thô) nên toàn bộ component viết bằng React.createElement thuần, không JSX.
|
|
19
|
+
const h = React.createElement;
|
|
20
|
+
|
|
21
|
+
const STEP_ICON = { planning: '📋', perceiving: '👁', executing: '▶', blocked: '🛡', recovering: '↺', auditing: '🔎', done: '✅', failed: '❌' };
|
|
22
|
+
|
|
23
|
+
let nextLineId = 0;
|
|
24
|
+
function line(text, color, dim) {
|
|
25
|
+
return { id: nextLineId++, text, color, dim };
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function LogLine({ item }) {
|
|
29
|
+
return h(Text, { color: item.color, dimColor: item.dim }, item.text);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function MissionTuiApp({ serverUrl, apiKey, threadId, tenantClient, idleTimeoutMs, openViewerOnHIL, onSettle, sessionId }) {
|
|
33
|
+
const [lines, setLines] = useState([]);
|
|
34
|
+
const [inputValue, setInputValue] = useState('');
|
|
35
|
+
// Khác `paused=false` (mission đang tự chạy, Enter -> steer) - khi có giá trị, Enter gửi
|
|
36
|
+
// browser:resolve nhắm đúng session HIL đang treo thay vì đi qua kênh steer.
|
|
37
|
+
const [pausedSessionKey, setPausedSessionKey] = useState(null);
|
|
38
|
+
const castTokenRef = useRef(undefined);
|
|
39
|
+
const [sending, setSending] = useState(false);
|
|
40
|
+
const [done, setDone] = useState(false);
|
|
41
|
+
|
|
42
|
+
const socketRef = useRef(null);
|
|
43
|
+
const settledRef = useRef(false);
|
|
44
|
+
const viewerServerRef = useRef(null);
|
|
45
|
+
const idleTimerRef = useRef(null);
|
|
46
|
+
const viewerOpenedRef = useRef(false);
|
|
47
|
+
|
|
48
|
+
const appendLine = (text, color, dim) => setLines((prev) => [...prev, line(text, color, dim)]);
|
|
49
|
+
|
|
50
|
+
useEffect(() => {
|
|
51
|
+
const socket = io(serverUrl, { auth: { token: apiKey || 'dev-token' }, transports: ['websocket'], reconnection: true });
|
|
52
|
+
socketRef.current = socket;
|
|
53
|
+
|
|
54
|
+
// `reason` - cùng lý do với streamBrowserMissionLog's stop(reason): phân biệt kết quả mission
|
|
55
|
+
// thật ('success'/'failed') với "chỉ đơn giản là ngừng theo dõi" ('idle'/'connect_error') - CLI
|
|
56
|
+
// dùng để quyết định có nên retry tunnel hay không (chỉ retry ở nhóm sau).
|
|
57
|
+
const settle = (reason) => {
|
|
58
|
+
if (settledRef.current) return;
|
|
59
|
+
settledRef.current = true;
|
|
60
|
+
clearTimeout(idleTimerRef.current);
|
|
61
|
+
socket.disconnect();
|
|
62
|
+
viewerServerRef.current?.close();
|
|
63
|
+
setDone(true);
|
|
64
|
+
onSettle(reason);
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
const bumpIdle = () => {
|
|
68
|
+
clearTimeout(idleTimerRef.current);
|
|
69
|
+
idleTimerRef.current = setTimeout(() => {
|
|
70
|
+
appendLine(`\n(không có hoạt động ${Math.round(idleTimeoutMs / 1000)}s - dừng theo dõi; mission có thể vẫn đang chạy nền)`, 'gray');
|
|
71
|
+
settle('idle');
|
|
72
|
+
}, idleTimeoutMs);
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
socket.on('connect_error', (error) => {
|
|
76
|
+
appendLine(`⚠️ Không theo dõi được tiến độ trực tiếp (${error.message}) - mission vẫn tiếp tục.`, 'yellow');
|
|
77
|
+
settle('connect_error');
|
|
78
|
+
});
|
|
79
|
+
// ✅ FIX: chưa có listener 'error' nào ở đây - socket.io lỗi phía server (khác connect_error)
|
|
80
|
+
// trước đây sẽ throw và crash cả tiến trình CLI thay vì chỉ dừng theo dõi như connect_error.
|
|
81
|
+
socket.on('error', (error) => {
|
|
82
|
+
appendLine(`⚠️ Lỗi luồng tiến độ (${error?.message || error}) - mission vẫn tiếp tục.`, 'yellow');
|
|
83
|
+
settle('connect_error');
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
// Học đúng sessionKey (clientId) của phiên HIL hiện tại - browser:cast-start bắn vào room
|
|
87
|
+
// `client:<tenant>` (auto-join, cùng room browser:agent-step dùng - xem comment tại nơi emit
|
|
88
|
+
// trong AIBrowserService.ts), nhưng browser:resolved/cast-error lại bắn vào room
|
|
89
|
+
// `aibrowser:<sessionKey>` (chỉ join khi biết sessionKey, KHÔNG auto-join) - phải tự emit
|
|
90
|
+
// join-room ngay khi biết sessionKey, đúng như browserViewerHtml's script đã làm, nếu không 2
|
|
91
|
+
// event này không bao giờ tới được đây.
|
|
92
|
+
socket.on('browser:cast-start', (payload) => {
|
|
93
|
+
if (!payload?.clientId) return;
|
|
94
|
+
setPausedSessionKey(payload.clientId);
|
|
95
|
+
castTokenRef.current = payload.castToken;
|
|
96
|
+
socket.emit('join-room', { name: 'aivin-cli-tui', rooms: [`aibrowser:${payload.clientId}`] });
|
|
97
|
+
});
|
|
98
|
+
socket.on('browser:resolved', () => {
|
|
99
|
+
setPausedSessionKey(null);
|
|
100
|
+
castTokenRef.current = undefined;
|
|
101
|
+
});
|
|
102
|
+
socket.on('browser:cast-error', (payload) => {
|
|
103
|
+
appendLine(`⚠️ ${payload?.reason || 'Mất kết nối phiên trình duyệt'}`, 'yellow');
|
|
104
|
+
setPausedSessionKey(null);
|
|
105
|
+
castTokenRef.current = undefined;
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
for (const eventName of MISSION_LOG_EVENTS) {
|
|
109
|
+
socket.on(eventName, (payload) => {
|
|
110
|
+
if (!payload || payload.thread_id !== threadId) return;
|
|
111
|
+
bumpIdle();
|
|
112
|
+
const time = new Date(payload.timestamp || Date.now()).toLocaleTimeString();
|
|
113
|
+
const icon = payload.status === 'error' ? '✗' : payload.status === 'success' ? '✓' : payload.status === 'warning' ? '!' : '·';
|
|
114
|
+
const iconColor = payload.status === 'error' ? 'red' : payload.status === 'success' ? 'green' : payload.status === 'warning' ? 'yellow' : 'gray';
|
|
115
|
+
const label = payload.event_key || eventName;
|
|
116
|
+
appendLine(`[${time}] ${icon} ${label} ${payload.message || ''}`.trimEnd(), iconColor);
|
|
117
|
+
|
|
118
|
+
if (payload.event_key === 'ai_browser.hil_required' && !viewerOpenedRef.current) {
|
|
119
|
+
viewerOpenedRef.current = true;
|
|
120
|
+
sendOSNotification('AI Browser cần bạn hỗ trợ', payload.message || 'Mission đang chờ bạn tương tác');
|
|
121
|
+
if (openViewerOnHIL) {
|
|
122
|
+
appendLine('\n👤 Mission cần bạn tương tác trực tiếp (captcha/đăng nhập/xác nhận) - đang mở trình duyệt... (hoặc gõ "done" bên dưới sau khi xong)', 'magenta');
|
|
123
|
+
openBrowserViewer(serverUrl, apiKey, tenantClient)
|
|
124
|
+
.then((server) => {
|
|
125
|
+
if (settledRef.current) server.close();
|
|
126
|
+
else viewerServerRef.current = server;
|
|
127
|
+
})
|
|
128
|
+
.catch((e) => appendLine(`⚠️ Không mở được viewer tự động: ${e.message}`, 'yellow'));
|
|
129
|
+
} else {
|
|
130
|
+
appendLine(`\n👤 ${payload.message || 'Mission cần bạn hỗ trợ'} - kiểm tra cửa sổ trình duyệt (banner xanh ở đầu trang), hoặc gõ "done" bên dưới khi xong.`, 'magenta');
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
if (payload.event_key === 'ai_browser.success' || payload.event_key === 'ai_browser.failed' || MISSION_DONE_EVENT_KEYS.has(payload.event_key)) {
|
|
134
|
+
const ok = payload.event_key !== 'ai_browser.failed' && payload.status !== 'error';
|
|
135
|
+
sendOSNotification(ok ? '✅ AI Browser hoàn tất' : '❌ AI Browser thất bại', payload.message || '');
|
|
136
|
+
settle(ok ? 'success' : 'failed');
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
socket.on('browser:agent-step', (payload) => {
|
|
142
|
+
if (!payload) return;
|
|
143
|
+
bumpIdle();
|
|
144
|
+
const time = new Date().toLocaleTimeString();
|
|
145
|
+
const icon = STEP_ICON[payload.type] || '·';
|
|
146
|
+
appendLine(`[${time}] ${icon} step ${payload.step} ${payload.type} ${payload.summary || ''}`.trimEnd(), 'cyan');
|
|
147
|
+
if (payload.url) appendLine(` url: ${payload.url}`, 'gray');
|
|
148
|
+
const detail = payload.detail;
|
|
149
|
+
if (Array.isArray(detail)) {
|
|
150
|
+
for (const a of detail) {
|
|
151
|
+
appendLine(` - ${a.type}${a.detail ? ` (${a.detail})` : ''}`, 'gray');
|
|
152
|
+
if (a.reason) appendLine(` reason: ${a.reason}`, 'gray', true);
|
|
153
|
+
}
|
|
154
|
+
} else if (detail && typeof detail === 'object') {
|
|
155
|
+
if (detail.reason) appendLine(` reason: ${detail.reason}`, 'gray', true);
|
|
156
|
+
for (const a of detail.revisedActions || []) {
|
|
157
|
+
appendLine(` - ${a.type}${a.detail ? ` (${a.detail})` : ''}`, 'gray');
|
|
158
|
+
if (a.reason) appendLine(` reason: ${a.reason}`, 'gray', true);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
appendLine(`📡 Đang theo dõi tiến độ cho ${threadId}... (gõ bên dưới để phản hồi/hướng dẫn, Ctrl+C để dừng theo dõi)\n`, 'gray');
|
|
164
|
+
bumpIdle();
|
|
165
|
+
|
|
166
|
+
return () => {
|
|
167
|
+
clearTimeout(idleTimerRef.current);
|
|
168
|
+
socket.disconnect();
|
|
169
|
+
};
|
|
170
|
+
// Chỉ setup socket 1 lần lúc mount ([] deps có chủ đích) - props (serverUrl/apiKey/threadId/...)
|
|
171
|
+
// không đổi trong suốt đời sống component này (1 lần chạy mission = 1 lần mount).
|
|
172
|
+
}, []);
|
|
173
|
+
|
|
174
|
+
const submit = async (value) => {
|
|
175
|
+
const message = value.trim();
|
|
176
|
+
setInputValue('');
|
|
177
|
+
if (!message || sending) return;
|
|
178
|
+
setSending(true);
|
|
179
|
+
try {
|
|
180
|
+
if (pausedSessionKey) {
|
|
181
|
+
// Đang pause chờ HIL (loại SCREENCAST - captcha/login/2FA, xem escalateToHIL) - kênh này CHỈ
|
|
182
|
+
// mang tín hiệu boolean "xong hay chưa" (resolveSession/escalateToHIL chỉ check
|
|
183
|
+
// hilResult !== 'cancel', KHÔNG đọc nội dung text) - đúng bản chất "user tự thao tác thẳng
|
|
184
|
+
// trên trang thật rồi", không phải "gõ câu trả lời ở đây thay việc thao tác". Enter ở đây
|
|
185
|
+
// tương đương bấm nút "✅ Xong" trên viewer/banner cũ; gõ đúng "cancel" mới có nghĩa khác
|
|
186
|
+
// (huỷ HIL). Bất kỳ text nào khác chỉ có tác dụng như 1 cú Enter trống.
|
|
187
|
+
socketRef.current?.emit('browser:resolve', {
|
|
188
|
+
clientId: pausedSessionKey,
|
|
189
|
+
option: message,
|
|
190
|
+
castToken: castTokenRef.current,
|
|
191
|
+
});
|
|
192
|
+
appendLine(`> ${message} (đã gửi tín hiệu hoàn tất HIL - gõ "cancel" nếu muốn huỷ thay vì tiếp tục)`, 'blue');
|
|
193
|
+
// Tự xoá ngay, không đợi round-trip browser:resolved - phòng trường hợp join-room (join lúc
|
|
194
|
+
// cast-start) chưa kịp có hiệu lực phía server khi broadcast resolved bắn ra, khiến state kẹt
|
|
195
|
+
// ở "vẫn đang pause" và mọi input sau đó bị hiểu nhầm là resolve cho session đã đóng.
|
|
196
|
+
setPausedSessionKey(null);
|
|
197
|
+
castTokenRef.current = undefined;
|
|
198
|
+
} else {
|
|
199
|
+
// Mission đang tự chạy - gửi làm hướng dẫn real-time cho vòng plan kế tiếp, kèm session_id/thread_id
|
|
200
|
+
await axios.post(
|
|
201
|
+
`${serverUrl}/plugins/ai-browser/steer`,
|
|
202
|
+
{
|
|
203
|
+
message,
|
|
204
|
+
session_id: sessionId || threadId,
|
|
205
|
+
thread_id: threadId,
|
|
206
|
+
},
|
|
207
|
+
{ headers: { Authorization: `Bearer ${apiKey || 'dev-token'}` } },
|
|
208
|
+
);
|
|
209
|
+
appendLine(`> ${message} (đã gửi làm hướng dẫn - áp dụng ở bước kế tiếp)`, 'blue');
|
|
210
|
+
}
|
|
211
|
+
} catch (e) {
|
|
212
|
+
appendLine(`⚠️ Gửi phản hồi thất bại: ${e.response?.data?.message || e.message}`, 'yellow');
|
|
213
|
+
} finally {
|
|
214
|
+
setSending(false);
|
|
215
|
+
}
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
return h(
|
|
219
|
+
Box,
|
|
220
|
+
{ flexDirection: 'column' },
|
|
221
|
+
h(Static, { items: lines }, (item) => h(LogLine, { key: item.id, item })),
|
|
222
|
+
!done &&
|
|
223
|
+
h(
|
|
224
|
+
Box,
|
|
225
|
+
{ borderStyle: 'round', borderColor: pausedSessionKey ? 'magenta' : 'blue', paddingX: 1, marginTop: 1 },
|
|
226
|
+
h(Text, { color: pausedSessionKey ? 'magenta' : 'blue' }, pausedSessionKey ? '👤 HIL > ' : '💬 > '),
|
|
227
|
+
h(TextInput, { value: inputValue, onChange: setInputValue, onSubmit: submit }),
|
|
228
|
+
),
|
|
229
|
+
);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Thay thế `streamBrowserMissionLog` (missionStream.mjs) khi có TTY thật - cùng semantics resolve
|
|
234
|
+
* (event terminal / idle timeout / Ctrl+C), cùng logic parse event (log/HIL/notification), chỉ khác
|
|
235
|
+
* cách hiển thị (Ink split-view thay vì console.log tuần tự) và thêm được ô input nhận feedback.
|
|
236
|
+
*/
|
|
237
|
+
export function runBrowserMissionTui(serverUrl, apiKey, threadId, tenantClient, { idleTimeoutMs = 120_000, openViewerOnHIL = true, sessionId } = {}) {
|
|
238
|
+
return new Promise((resolve) => {
|
|
239
|
+
let settled = false;
|
|
240
|
+
const onSettle = (reason) => {
|
|
241
|
+
if (settled) return;
|
|
242
|
+
settled = true;
|
|
243
|
+
process.off('SIGINT', onSigint);
|
|
244
|
+
unmount();
|
|
245
|
+
resolve(reason);
|
|
246
|
+
};
|
|
247
|
+
const onSigint = () => onSettle('sigint');
|
|
248
|
+
process.on('SIGINT', onSigint);
|
|
249
|
+
|
|
250
|
+
const { unmount } = render(
|
|
251
|
+
h(MissionTuiApp, {
|
|
252
|
+
serverUrl,
|
|
253
|
+
apiKey,
|
|
254
|
+
threadId,
|
|
255
|
+
tenantClient,
|
|
256
|
+
idleTimeoutMs,
|
|
257
|
+
openViewerOnHIL,
|
|
258
|
+
onSettle,
|
|
259
|
+
sessionId: sessionId || threadId,
|
|
260
|
+
}),
|
|
261
|
+
);
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* Chọn đúng cách theo dõi mission: TUI Ink (log cuộn + input cố định) khi có TTY thật cả 2 chiều
|
|
267
|
+
* (stdout để vẽ, stdin để nhận raw-mode input); fallback về `streamBrowserMissionLog` (console.log
|
|
268
|
+
* tuần tự, không nhận input) cho môi trường không có TTY thật (CI, output bị pipe/redirect) - Ink
|
|
269
|
+
* throw ngay khi cố bật raw mode trên stdin không phải TTY.
|
|
270
|
+
*/
|
|
271
|
+
export function watchBrowserMission(serverUrl, apiKey, threadId, tenantClient, opts = {}) {
|
|
272
|
+
if (process.stdout.isTTY && process.stdin.isTTY) {
|
|
273
|
+
return runBrowserMissionTui(serverUrl, apiKey, threadId, tenantClient, opts);
|
|
274
|
+
}
|
|
275
|
+
return streamBrowserMissionLog(serverUrl, apiKey, threadId, tenantClient, opts);
|
|
276
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import axios from 'axios';
|
|
2
|
+
import chalk from 'chalk';
|
|
3
|
+
|
|
4
|
+
// Ctrl+C used to just kill the local CLI process while whatever HTTP request was in flight (MCP
|
|
5
|
+
// scan, plugin trigger, deploy, login, ...) kept running server-side with nothing telling it the
|
|
6
|
+
// caller gave up - most commands had zero SIGINT handling at all. Rather than threading an
|
|
7
|
+
// AbortController through every individual axios call site across the CLI, this hooks the ONE
|
|
8
|
+
// shared `axios` module instance every lib file already imports: a single request interceptor
|
|
9
|
+
// attaches the currently-live controller's signal to every request that doesn't already set its
|
|
10
|
+
// own, so aborting that one controller cancels every in-flight request CLI-wide.
|
|
11
|
+
//
|
|
12
|
+
// A handful of commands (browser missions, log watchers) already have their own more specific
|
|
13
|
+
// SIGINT handler that does real cooperative cancellation (e.g. POST /plugins/ai-browser/cancel)
|
|
14
|
+
// before letting the process wind down naturally - this module doesn't touch those. It only adds a
|
|
15
|
+
// baseline "abort in-flight requests, then force an exit if nothing else already caused one" for
|
|
16
|
+
// everything else, which is the vast majority of commands.
|
|
17
|
+
let controller = new AbortController();
|
|
18
|
+
|
|
19
|
+
axios.interceptors.request.use((config) => {
|
|
20
|
+
if (config.signal === undefined) config.signal = controller.signal;
|
|
21
|
+
return config;
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
// Rewritten so a command's own `` `X failed: ${error.message}` `` reads as a clean "Cancelled"
|
|
25
|
+
// instead of axios' raw "canceled" - every command already has this wrapping, so fixing the message
|
|
26
|
+
// here (once) covers all of them instead of editing each catch block individually.
|
|
27
|
+
axios.interceptors.response.use(
|
|
28
|
+
(response) => response,
|
|
29
|
+
(error) => {
|
|
30
|
+
if (axios.isCancel(error) || error.code === 'ERR_CANCELED') {
|
|
31
|
+
error.message = 'Cancelled';
|
|
32
|
+
}
|
|
33
|
+
return Promise.reject(error);
|
|
34
|
+
},
|
|
35
|
+
);
|
|
36
|
+
|
|
37
|
+
const FORCE_EXIT_GRACE_MS = 4000;
|
|
38
|
+
let sigintCount = 0;
|
|
39
|
+
|
|
40
|
+
process.on('SIGINT', () => {
|
|
41
|
+
sigintCount++;
|
|
42
|
+
if (sigintCount > 1) {
|
|
43
|
+
// Second Ctrl+C - stop waiting on whatever cooperative cleanup another handler is mid-way
|
|
44
|
+
// through and just leave now.
|
|
45
|
+
process.exit(130);
|
|
46
|
+
}
|
|
47
|
+
console.log(chalk.yellow('\n⏹ Cancelling (Ctrl+C again to force quit)...'));
|
|
48
|
+
|
|
49
|
+
// Abort every in-flight request, then swap in a fresh controller immediately - a *different*
|
|
50
|
+
// SIGINT handler elsewhere (e.g. the browser-mission cooperative cancel) may still need to send
|
|
51
|
+
// its own request right after this (a "please stop" POST) and shouldn't have it born
|
|
52
|
+
// already-aborted just because it shares the same axios instance.
|
|
53
|
+
controller.abort();
|
|
54
|
+
controller = new AbortController();
|
|
55
|
+
|
|
56
|
+
// Safety net, not the primary exit path: a command whose own request just got aborted rejects
|
|
57
|
+
// almost immediately and exits on its own via its normal catch block. This only matters for the
|
|
58
|
+
// cases nothing else handles - stuck on a prompt, a socket still reconnecting, etc.
|
|
59
|
+
const forceExit = setTimeout(() => process.exit(130), FORCE_EXIT_GRACE_MS);
|
|
60
|
+
forceExit.unref?.();
|
|
61
|
+
});
|