@aivin-labs/cli 1.0.75 → 1.0.78

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.
@@ -0,0 +1,99 @@
1
+ import fs from 'fs';
2
+ import path from 'path';
3
+ import { writeFileAtomic } from './atomicFile.mjs';
4
+
5
+ /**
6
+ * Vé LightCode của Cloud Computer (24/11/2026). BE cấp vé loại managed (Ed25519, hạn tối đa 6 giờ, ràng machine-id của lần bật máy này):
7
+ * - Lúc tạo container: env AIVIN_LIGHTCODE_LICENSE_KEY — entrypoint.sh ghi vào tệp rồi xoá env (thành viên không đọc được).
8
+ * - Giữa chừng: sự kiện socket BE → CLI `org_computer.license_renew` { token, expiresAt? } — module này ghi đè tệp nguyên tử.
9
+ *
10
+ * Tệp vé thuộc tài khoản daemon LightCode (không phải root, không phải thành viên), thư mục chứa 0700 → LightCode đọc được, thành viên
11
+ * không. CLI chạy root (có DAC_OVERRIDE/CHOWN) nên ghi + đổi chủ được. Token KHÔNG bao giờ vào log / ack / thông báo lỗi.
12
+ */
13
+
14
+ export const LICENSE_RENEW_EVENT = 'org_computer.license_renew';
15
+ export const LOG_TAG = '[ORG_COMPUTER_LICENSE]';
16
+ /** Khớp LCAGENT_LICENSE_FILE mà entrypoint.sh đặt cho daemon LightCode. */
17
+ export const DEFAULT_LICENSE_FILE = '/var/lib/aivin-lc/lightcode-license.key';
18
+ /** LightCode (server/src/license/license.ts readManagedLicenseKey) chỉ nhận khoá bắt đầu `LCA1.` và dài ≤ 2000 ký tự. */
19
+ export const TICKET_PREFIX = 'LCA1.';
20
+ export const TICKET_MAX_LENGTH = 2000;
21
+ const TICKET_RE = /^LCA1\.[A-Za-z0-9._~+/=-]+$/;
22
+
23
+ export const RenewReason = Object.freeze({
24
+ BAD_PAYLOAD: 'bad_payload',
25
+ BAD_TOKEN: 'bad_token',
26
+ BAD_EXPIRES: 'bad_expires',
27
+ EXPIRED: 'expired',
28
+ NOT_ENABLED: 'not_enabled',
29
+ WRITE_FAILED: 'write_failed',
30
+ });
31
+
32
+ export function licenseFilePath(env = process.env) {
33
+ return String(env.AIVIN_LIGHTCODE_LICENSE_FILE || '').trim() || DEFAULT_LICENSE_FILE;
34
+ }
35
+
36
+ /** `expiresAt` (ISO hoặc mili-giây) → mili-giây, hoặc null nếu không phải thời điểm hợp lệ. */
37
+ function toMillis(value) {
38
+ if (typeof value === 'number') return Number.isFinite(value) && value > 0 ? value : null;
39
+ if (typeof value === 'string' && value.trim()) {
40
+ const ms = Date.parse(value);
41
+ return Number.isFinite(ms) ? ms : null;
42
+ }
43
+ return null;
44
+ }
45
+
46
+ /** Kiểm tra payload — trả { ok: true, token } hoặc { ok: false, reason }. Không ném lỗi, kết quả lỗi không chứa token. */
47
+ export function validateRenewPayload(payload, now = Date.now()) {
48
+ if (!payload || typeof payload !== 'object' || Array.isArray(payload)) return { ok: false, reason: RenewReason.BAD_PAYLOAD };
49
+ const token = typeof payload.token === 'string' ? payload.token.trim() : '';
50
+ if (!token || token.length > TICKET_MAX_LENGTH || !TICKET_RE.test(token)) return { ok: false, reason: RenewReason.BAD_TOKEN };
51
+ if (payload.expiresAt !== undefined && payload.expiresAt !== null) {
52
+ const exp = toMillis(payload.expiresAt);
53
+ if (exp === null) return { ok: false, reason: RenewReason.BAD_EXPIRES };
54
+ if (exp <= now) return { ok: false, reason: RenewReason.EXPIRED };
55
+ }
56
+ return { ok: true, token };
57
+ }
58
+
59
+ /** Chủ của thư mục chứa vé = tài khoản daemon LightCode; chưa có thư mục này = image không bật LightCode. */
60
+ function daemonOwner(dir) {
61
+ try {
62
+ const st = fs.statSync(dir);
63
+ return st.isDirectory() ? { uid: st.uid, gid: st.gid } : null;
64
+ } catch { return null; }
65
+ }
66
+
67
+ /**
68
+ * Ghi vé ra tệp: nguyên tử (tệp tạm cạnh bên + đổi tên đè), 0600, đổi chủ cho tài khoản daemon. Trả { ok } hoặc { ok:false, reason }.
69
+ * Tệp tạm cũng 0600 và nằm trong thư mục 0700 nên không có lúc nào thành viên đọc được. Nếu đổi chủ lỗi, tệp vẫn thuộc root (daemon
70
+ * không đọc được) → báo write_failed để BE thử lại, không im lặng.
71
+ */
72
+ export function writeLicenseFile(file, token, { ownerOf = daemonOwner, chown = fs.chownSync, writeAtomic = writeFileAtomic } = {}) {
73
+ const owner = ownerOf(path.dirname(file));
74
+ if (!owner) return { ok: false, reason: RenewReason.NOT_ENABLED };
75
+ try {
76
+ writeAtomic(file, `${token}\n`, { mode: 0o600 });
77
+ if (process.platform !== 'win32') chown(file, owner.uid, owner.gid);
78
+ return { ok: true };
79
+ } catch {
80
+ return { ok: false, reason: RenewReason.WRITE_FAILED };
81
+ }
82
+ }
83
+
84
+ /**
85
+ * Bộ xử lý sự kiện `org_computer.license_renew`: (payload, ack). Luôn ack — `{ ok: true }` hoặc `{ ok: false, reason }` (mã cố định,
86
+ * không có token / đường dẫn).
87
+ */
88
+ export function createLicenseRenewHandler({ file = licenseFilePath(), now = () => Date.now(), write = writeLicenseFile, log = () => {} } = {}) {
89
+ return (payload, ack) => {
90
+ const reply = typeof ack === 'function' ? ack : () => {};
91
+ const checked = validateRenewPayload(payload, now());
92
+ if (!checked.ok) { log(`${LOG_TAG} từ chối: ${checked.reason}`); reply({ ok: false, reason: checked.reason }); return checked; }
93
+ const res = write(file, checked.token);
94
+ // Chỉ ghi mã lý do cố định — không bao giờ token / đường dẫn.
95
+ log(res.ok ? `${LOG_TAG} đã gia hạn vé LightCode` : `${LOG_TAG} ghi vé thất bại: ${res.reason}`);
96
+ reply(res.ok ? { ok: true } : { ok: false, reason: res.reason });
97
+ return res;
98
+ };
99
+ }
@@ -21,8 +21,10 @@ import { buildOfficeTools } from './office/index.mjs';
21
21
  import { measurePath } from './undoStore.mjs';
22
22
  import { buildProcessTools, shellCommand } from './processes.mjs';
23
23
  import { buildAgentTools, createAgentRunner } from './codingAgents.mjs';
24
+ import { buildLightcodeTools } from './lightcodeAgent.mjs';
24
25
  import { buildLocalLlmTools, createLocalLlm } from './localLlm.mjs';
25
26
  import { buildLlmLoginTools, createLlmLogin } from './localLlmLogin.mjs';
27
+ import { buildCommandAuthEnv, redactAuthText } from './ephemeralAuth.mjs';
26
28
 
27
29
  /** Một bộ chạy agent lập trình cho cả tiến trình (việc đang chạy sống qua các lần dựng lại danh mục công cụ). */
28
30
  let sharedAgents = null;
@@ -690,7 +692,16 @@ async function shellExec(args, ctx, env) {
690
692
  assertCommandAllowed(command);
691
693
  const cwd = resolveInRoots(env.roots, args.cwd || '');
692
694
  const [cmd, argv] = shellCommand(command);
693
- const result = await runProcess(cmd, argv, { cwd, signal: ctx.signal, onOutput: ctx.onOutput, runAs: env.runAs });
695
+ const { env: authExtra, secrets } = buildCommandAuthEnv(env.authEnv, command);
696
+ const procEnv = secrets.length || Object.keys(authExtra).length ? { ...agentEnv(), ...authExtra } : undefined;
697
+ const onOutput = secrets.length && ctx.onOutput
698
+ ? (stream, chunk) => ctx.onOutput(stream, redactAuthText(chunk, secrets))
699
+ : ctx.onOutput;
700
+ const result = await runProcess(cmd, argv, { cwd, signal: ctx.signal, onOutput, runAs: env.runAs, ...(procEnv ? { env: procEnv } : {}) });
701
+ if (secrets.length) {
702
+ result.stdout = redactAuthText(result.stdout, secrets);
703
+ result.stderr = redactAuthText(result.stderr, secrets);
704
+ }
694
705
  return { command, cwd: displayPath(env.roots, cwd), ...result };
695
706
  }
696
707
 
@@ -852,7 +863,12 @@ export function buildTools(env) {
852
863
  // Giao việc cho agent lập trình trên máy (Claude Code…, chế độ không giao diện, có phiên nói tiếp) — máy cá nhân có cài ít nhất một agent.
853
864
  if (env.processes && env.schedules && !env.runAs) {
854
865
  sharedAgents ||= env.codingAgents || createAgentRunner();
855
- if (sharedAgents.installed().length) tools.push(...buildAgentTools(sharedAgents));
866
+ // LightCode (20/11/2026): xem / cài bên thực thi cho hợp đồng lập trình — máy Windows của một người (cài cần người đồng ý: sàn luật
867
+ // tại máy luôn hỏi lightcode.install, BE luôn hỏi duyệt trong chat). Có LightCode thì agent.run luôn có mặt: máy chưa có agent nào
868
+ // cài LightCode giữa chừng vẫn nhận được hợp đồng ngay (agent.run tự định vị lại LightCode lúc giao).
869
+ const lightcode = process.platform === 'win32' && !env.noLightcode;
870
+ if (sharedAgents.installed().length || lightcode) tools.push(...buildAgentTools(sharedAgents));
871
+ if (lightcode) tools.push(...buildLightcodeTools({ onInstalled: env.announceProfile, roots: env.roots }));
856
872
  } else if (env.processes && env.runAs && env.localLlm?.memberMode) {
857
873
  // Máy tính tổ chức (17/11/2026): thành viên giao việc lập trình chạy bằng agent họ đã đăng nhập (Claude Code / Codex cài sẵn trong image),
858
874
  // dưới tài khoản Linux của họ, trong thư mục của họ.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aivin-labs/cli",
3
- "version": "1.0.75",
3
+ "version": "1.0.78",
4
4
  "description": "aivin CLI - scaffold, run, and deploy Aivin plugins built on @aivin-labs/sdk",
5
5
  "type": "module",
6
6
  "bin": {