@aivin-labs/cli 1.0.66 → 1.0.68
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/bin/lib/remote/agentContract.mjs +10 -1
- package/bin/lib/remote/agentEvidence.mjs +97 -2
- package/bin/lib/remote/client.mjs +14 -3
- package/bin/lib/remote/codingAgents.mjs +171 -8
- package/bin/lib/remote/desktop/agentOverlay.mjs +382 -0
- package/bin/lib/remote/desktop/desktopCastSource.mjs +56 -17
- package/bin/lib/remote/gui/index.mjs +52 -1
- package/bin/lib/remote/localLlm.mjs +59 -16
- package/bin/lib/remote/localLlmAccounts.mjs +57 -0
- package/bin/lib/remote/mascot/hostSource.mjs +1 -1
- package/bin/lib/tasks.mjs +391 -389
- package/package.json +1 -1
|
@@ -36,6 +36,10 @@ const MAX_WARM_KEYS = 4;
|
|
|
36
36
|
* nguội (~1 s). Tổng có trần (`MAX_WARM_PROCS`, mỗi tiến trình Claude Code ~200 MB). Đổi bằng `AIVIN_LLM_WARM_PER_KEY`. */
|
|
37
37
|
const WARM_PER_KEY = Math.min(4, Math.max(1, Number(process.env.AIVIN_LLM_WARM_PER_KEY) || 2));
|
|
38
38
|
const MAX_WARM_PROCS = 6;
|
|
39
|
+
/** Antigravity (ACP): tiến trình phục vụ xong một lời gọi thì mở phiên mới trên CHÍNH nó và quay lại pool — lời gọi sau khỏi khởi động agy
|
|
40
|
+
* (initialize + session/new + set_model, 2–4 s). Mỗi tiến trình vẫn một lời gọi một lúc; đủ số lượt này thì thay tiến trình mới (agy giữ
|
|
41
|
+
* các phiên đã mở trong bộ nhớ). */
|
|
42
|
+
const ACP_MAX_USES = 40;
|
|
39
43
|
/** Mã lỗi riêng: agent định dùng công cụ — câu trả lời bị bỏ. */
|
|
40
44
|
export const TOOL_USE_ERROR = 'LOCAL_LLM_TOOL_USE';
|
|
41
45
|
export const QUOTA_ERROR = 'LOCAL_LLM_QUOTA';
|
|
@@ -277,15 +281,9 @@ export function createLocalLlm({
|
|
|
277
281
|
if (adapter.kind === 'acp') {
|
|
278
282
|
proc.child = launch(adapter.args());
|
|
279
283
|
proc.rpc = acpClient(proc.child);
|
|
280
|
-
proc.
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
const modelId = pickAgyModel(sess?.models?.availableModels, model, effort);
|
|
284
|
-
if (model && !modelId) throw failed(`${adapter.label} has no model "${model}" here`, DeviceErrorCode.NOT_FOUND);
|
|
285
|
-
if (modelId && modelId !== sess?.models?.currentModelId) await proc.rpc.call('session/set_model', { sessionId: sess.sessionId, modelId });
|
|
286
|
-
return { sessionId: sess.sessionId, modelId: modelId || sess?.models?.currentModelId };
|
|
287
|
-
})();
|
|
288
|
-
proc.ready.catch(() => {});
|
|
284
|
+
proc.initialized = proc.rpc.call('initialize', { protocolVersion: 1, clientCapabilities: { fs: { readTextFile: false, writeTextFile: false }, terminal: false } });
|
|
285
|
+
proc.initialized.catch(() => {});
|
|
286
|
+
openAcpSession(proc);
|
|
289
287
|
return Promise.resolve(proc);
|
|
290
288
|
}
|
|
291
289
|
return probeOf(adapter).then((probe) => {
|
|
@@ -294,14 +292,31 @@ export function createLocalLlm({
|
|
|
294
292
|
});
|
|
295
293
|
}
|
|
296
294
|
|
|
295
|
+
/** Mở một phiên ACP mới (đã initialize một lần cho cả tiến trình) với đúng model / effort của tiến trình → `proc.ready`. */
|
|
296
|
+
function openAcpSession(proc) {
|
|
297
|
+
const { adapter, model, effort } = proc;
|
|
298
|
+
proc.sessionId = null;
|
|
299
|
+
proc.modelId = null;
|
|
300
|
+
proc.ready = (async () => {
|
|
301
|
+
await proc.initialized;
|
|
302
|
+
const sess = await proc.rpc.call('session/new', { cwd: dir, mcpServers: [] });
|
|
303
|
+
const modelId = pickAgyModel(sess?.models?.availableModels, model, effort);
|
|
304
|
+
if (model && !modelId) throw failed(`${adapter.label} has no model "${model}" here`, DeviceErrorCode.NOT_FOUND);
|
|
305
|
+
if (modelId && modelId !== sess?.models?.currentModelId) await proc.rpc.call('session/set_model', { sessionId: sess.sessionId, modelId });
|
|
306
|
+
return { sessionId: sess.sessionId, modelId: modelId || sess?.models?.currentModelId };
|
|
307
|
+
})();
|
|
308
|
+
proc.ready.catch(() => {});
|
|
309
|
+
}
|
|
310
|
+
|
|
297
311
|
function closeProc(proc) {
|
|
298
312
|
if (!proc) return;
|
|
299
313
|
try { proc.child?.stdin?.end(); } catch { /* đã đóng */ }
|
|
300
314
|
try { proc.child?.kill(); } catch { /* đã thoát */ }
|
|
301
315
|
}
|
|
302
316
|
|
|
303
|
-
|
|
304
|
-
|
|
317
|
+
const keyOf = (adapter, opts) => `${adapter.id}|${opts.model || ''}|${opts.effort || ''}`;
|
|
318
|
+
|
|
319
|
+
function entryFor(key) {
|
|
305
320
|
let entry = warm.get(key);
|
|
306
321
|
if (!entry) {
|
|
307
322
|
while (warm.size >= MAX_WARM_KEYS) {
|
|
@@ -312,6 +327,23 @@ export function createLocalLlm({
|
|
|
312
327
|
warm.set(key, entry);
|
|
313
328
|
}
|
|
314
329
|
entry.lastUsed = now();
|
|
330
|
+
return entry;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/** ACP xong một lời gọi thành công → phiên mới trên chính tiến trình đó, đưa lên đầu pool (nóng hơn tiến trình mới dựng). */
|
|
334
|
+
function recycleAcp(proc) {
|
|
335
|
+
proc.uses = (proc.uses || 0) + 1;
|
|
336
|
+
const entry = entryFor(keyOf(proc.adapter, proc));
|
|
337
|
+
if (proc.exited || proc.uses >= ACP_MAX_USES || entry.procs.length >= warmPerKey || warmCount() >= maxWarmProcs) return closeProc(proc);
|
|
338
|
+
proc.rpc.onNotify = null;
|
|
339
|
+
proc.rpc.onRequest = null;
|
|
340
|
+
openAcpSession(proc);
|
|
341
|
+
entry.procs.unshift(Promise.resolve(proc));
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
function keepWarm(adapter, opts) {
|
|
345
|
+
const key = keyOf(adapter, opts);
|
|
346
|
+
const entry = entryFor(key);
|
|
315
347
|
// Đủ số cho loại này, tổng còn chỗ thì mới dựng thêm; loại khác ít dùng nhất nhường chỗ khi tổng đầy.
|
|
316
348
|
while (entry.procs.length < warmPerKey) {
|
|
317
349
|
if (warmCount() >= maxWarmProcs) {
|
|
@@ -336,13 +368,14 @@ export function createLocalLlm({
|
|
|
336
368
|
}
|
|
337
369
|
|
|
338
370
|
async function take(adapter, opts) {
|
|
339
|
-
const key =
|
|
371
|
+
const key = keyOf(adapter, opts);
|
|
340
372
|
const w = warm.get(key);
|
|
341
373
|
const pending = w?.procs.shift();
|
|
342
374
|
let proc = pending ? await pending.catch(() => null) : null;
|
|
343
375
|
const warmStart = !!proc && !proc.exited;
|
|
344
376
|
if (!warmStart) { closeProc(proc); proc = await startProc(adapter, opts); }
|
|
345
|
-
|
|
377
|
+
// ACP: tiến trình này quay lại pool khi xong (recycleAcp) — bù chỗ lúc đó, không dựng thêm ngay (đầy pool thì nó không quay lại được).
|
|
378
|
+
if (adapter.kind !== 'acp') setImmediate(() => keepWarm(adapter, opts));
|
|
346
379
|
return { proc, warmStart };
|
|
347
380
|
}
|
|
348
381
|
|
|
@@ -370,12 +403,18 @@ export function createLocalLlm({
|
|
|
370
403
|
let settled = false;
|
|
371
404
|
let ttft = null;
|
|
372
405
|
const meta = {};
|
|
406
|
+
let onChildError = null;
|
|
407
|
+
let onChildExit = null;
|
|
373
408
|
const finish = (err, value) => {
|
|
374
409
|
if (settled) return;
|
|
375
410
|
settled = true;
|
|
376
411
|
clearTimeout(timer);
|
|
377
412
|
ctx.signal?.removeEventListener?.('abort', onAbort);
|
|
378
|
-
|
|
413
|
+
if (onChildError) proc.child.off?.('error', onChildError);
|
|
414
|
+
if (onChildExit) proc.child.off?.('exit', onChildExit);
|
|
415
|
+
// ACP thành công → dùng lại tiến trình (phiên mới); lỗi / huỷ / hết giờ / định dùng công cụ → đóng (phiên có thể đang dở).
|
|
416
|
+
if (!err && adapter.kind === 'acp') recycleAcp(proc); else closeProc(proc);
|
|
417
|
+
if (adapter.kind === 'acp') setImmediate(() => keepWarm(adapter, { model, effort }));
|
|
379
418
|
if (err) reject(err); else resolve(value);
|
|
380
419
|
};
|
|
381
420
|
const st = {
|
|
@@ -402,12 +441,16 @@ export function createLocalLlm({
|
|
|
402
441
|
const onAbort = () => { if (proc.rpc && proc.sessionId) proc.rpc.notify('session/cancel', { sessionId: proc.sessionId }); finish(failed('Cancelled', DeviceErrorCode.CANCELLED)); };
|
|
403
442
|
const timer = setTimeout(() => finish(failed(`Model did not finish in ${timeoutMs} ms`, DeviceErrorCode.CALL_TIMEOUT)), timeoutMs);
|
|
404
443
|
ctx.signal?.addEventListener?.('abort', onAbort, { once: true });
|
|
405
|
-
|
|
406
|
-
|
|
444
|
+
onChildError = (e) => finish(failed(`Could not start ${adapter.label}: ${e.message}`));
|
|
445
|
+
onChildExit = (code) => finish(failed(`${adapter.label} exited (${code}) before answering${proc.stderr ? `: ${proc.stderr.trim().slice(-400)}` : ''}`));
|
|
446
|
+
proc.child.once('error', onChildError);
|
|
447
|
+
proc.child.once('exit', onChildExit);
|
|
407
448
|
|
|
408
449
|
if (adapter.kind === 'acp') {
|
|
409
450
|
proc.rpc.onNotify = (method, params) => {
|
|
410
451
|
if (method !== 'session/update') return;
|
|
452
|
+
// Tiến trình dùng lại: thông báo trễ của phiên trước không được lẫn vào lời gọi này.
|
|
453
|
+
if (params?.sessionId && proc.sessionId && params.sessionId !== proc.sessionId) return;
|
|
411
454
|
const u = params?.update || {};
|
|
412
455
|
if (u.sessionUpdate === 'agent_message_chunk' && u.content?.type === 'text') st.delta(u.content.text);
|
|
413
456
|
if (u.sessionUpdate === 'tool_call' || u.sessionUpdate === 'tool_call_update') st.tool(u.name || u.title || u.kind || 'tool');
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
// Dấu vân tay tài khoản của từng agent làm mô hình trên máy (17/11/2026) — BE tính hạn mức theo TÀI KHOẢN (hai máy cùng một tài khoản
|
|
2
|
+
// Claude Code / Codex dùng chung hạn mức; xem BE DeviceLocalQuotaHelper). Chỉ gửi băm (không gửi email / mã tài khoản), đọc từ tệp đăng nhập
|
|
3
|
+
// sẵn có của agent; đọc không được → bỏ agent đó (BE tính theo máy như trước).
|
|
4
|
+
//
|
|
5
|
+
// - Claude Code: `<CLAUDE_CONFIG_DIR | ~>/.claude.json` → `oauthAccount.accountUuid` (thiếu thì `emailAddress`).
|
|
6
|
+
// - Codex: `<CODEX_HOME | ~/.codex>/auth.json` → `tokens.account_id`.
|
|
7
|
+
// - Antigravity: không lộ tài khoản (kể cả qua ACP) → `installation_id` của bản cài (`~/.gemini/antigravity-cli` hoặc `~/.gemini/antigravity`).
|
|
8
|
+
import { createHash } from 'crypto';
|
|
9
|
+
import fs from 'fs';
|
|
10
|
+
import os from 'os';
|
|
11
|
+
import path from 'path';
|
|
12
|
+
|
|
13
|
+
const readJson = (file) => {
|
|
14
|
+
try { return JSON.parse(fs.readFileSync(file, 'utf8')); } catch { return null; }
|
|
15
|
+
};
|
|
16
|
+
const readText = (file) => {
|
|
17
|
+
try { return fs.readFileSync(file, 'utf8').trim(); } catch { return ''; }
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export const ACCOUNT_READERS = Object.freeze({
|
|
21
|
+
claude: ({ env, home }) => {
|
|
22
|
+
const dirs = [env.CLAUDE_CONFIG_DIR, home].filter(Boolean);
|
|
23
|
+
for (const dir of dirs) {
|
|
24
|
+
const acct = readJson(path.join(dir, '.claude.json'))?.oauthAccount;
|
|
25
|
+
const id = acct?.accountUuid || acct?.emailAddress;
|
|
26
|
+
if (id) return String(id);
|
|
27
|
+
}
|
|
28
|
+
return null;
|
|
29
|
+
},
|
|
30
|
+
codex: ({ env, home }) => {
|
|
31
|
+
const id = readJson(path.join(env.CODEX_HOME || path.join(home, '.codex'), 'auth.json'))?.tokens?.account_id;
|
|
32
|
+
return id ? String(id) : null;
|
|
33
|
+
},
|
|
34
|
+
antigravity: ({ home }) => {
|
|
35
|
+
for (const sub of ['antigravity-cli', 'antigravity']) {
|
|
36
|
+
const id = readText(path.join(home, '.gemini', sub, 'installation_id'));
|
|
37
|
+
if (id) return `install:${id}`;
|
|
38
|
+
}
|
|
39
|
+
return null;
|
|
40
|
+
},
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
/** Băm ổn định, không đảo ngược được: 16 ký tự hex của sha256("aivin-local-llm:<agent>:<id>"). */
|
|
44
|
+
export function accountFingerprint(agent, id) {
|
|
45
|
+
return createHash('sha256').update(`aivin-local-llm:${agent}:${id}`).digest('hex').slice(0, 16);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** `{ claude: '<16 hex>', … }` cho các agent đọc được tài khoản; rỗng → undefined. */
|
|
49
|
+
export function localLlmAccounts(agents, { env = process.env, home = os.homedir(), readers = ACCOUNT_READERS } = {}) {
|
|
50
|
+
const out = {};
|
|
51
|
+
for (const agent of agents || []) {
|
|
52
|
+
let id = null;
|
|
53
|
+
try { id = readers[agent]?.({ env, home }) || null; } catch { id = null; }
|
|
54
|
+
if (id) out[agent] = accountFingerprint(agent, id);
|
|
55
|
+
}
|
|
56
|
+
return Object.keys(out).length ? out : undefined;
|
|
57
|
+
}
|
|
@@ -298,7 +298,7 @@ ${[0, 1, 2].map((i) => ` <Border x:Name="NotifyStep${i}" Tag="${i}" Backgro
|
|
|
298
298
|
<TextBlock x:Name="AskStatus" Visibility="Collapsed" FontSize="13" Foreground="${B.muted}" Margin="0,2,4,1" TextTrimming="CharacterEllipsis"/>
|
|
299
299
|
</StackPanel>
|
|
300
300
|
<!-- Dau x mo o goc phai tren: cat o hoi di ngay, ke ca khi Aivin dang cho tra loi (cau hoi van con, mo lai bang cach re vao bot). -->
|
|
301
|
-
<Button x:Name="AskClose" Visibility="Collapsed" Style="{StaticResource IconBtn}" Width="18" Height="18" Padding="0" Margin="0,
|
|
301
|
+
<Button x:Name="AskClose" Visibility="Collapsed" Style="{StaticResource IconBtn}" Width="18" Height="18" Padding="0" Margin="0,1,1,0" Opacity="0.3"
|
|
302
302
|
HorizontalAlignment="Right" VerticalAlignment="Top" Background="#E8FFFFFF" BorderThickness="0" Tag="#F2F3F5">
|
|
303
303
|
<Path Data="${ICON_CLOSE_X}" Stroke="${B.muted}" StrokeThickness="1.5" StrokeStartLineCap="Round" StrokeEndLineCap="Round"
|
|
304
304
|
Width="9" Height="9" Stretch="Uniform"/>
|