@aivin-labs/cli 1.0.28 → 1.0.30

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.
@@ -5,7 +5,7 @@ import { spawn } from 'child_process';
5
5
  import { REMOTE_CONFIG_DIR } from '../constants.mjs';
6
6
  import { lockHolder } from '../daemonLock.mjs';
7
7
  import { disableAutoStart, uninstallDesktopApp } from '../autoStart.mjs';
8
- import { clearDeviceCredentials, loadDeviceCredentials, unpairDevice } from '../credentials.mjs';
8
+ import { clearAllDeviceCredentials, loadDeviceCredentials, unpairDevice } from '../credentials.mjs';
9
9
  import { isStandaloneBinary } from '../../update/versions.mjs';
10
10
  import { launcherExePath } from './index.mjs';
11
11
 
@@ -8,8 +8,8 @@ import { confirmTokenRotation, revertTokenRotation } from './credentials.mjs';
8
8
  */
9
9
 
10
10
  /** Chào thành công bằng `token` → token cũ (nếu còn giữ) hết cần. Không ném. */
11
- export function settleRotation(token) {
12
- try { return confirmTokenRotation(token); } catch { return false; }
11
+ export function settleRotation(token, serverUrl) {
12
+ try { return confirmTokenRotation(token, serverUrl); } catch { return false; }
13
13
  }
14
14
 
15
15
  /**
@@ -18,7 +18,7 @@ export function settleRotation(token) {
18
18
  */
19
19
  export function fallBackToPreviousToken({ creds, socket, isStopped = () => false, delayMs = 1000, revert = revertTokenRotation }) {
20
20
  let previous;
21
- try { previous = revert(creds.device_token); } catch { previous = null; }
21
+ try { previous = revert(creds.device_token, creds.server_url); } catch { previous = null; }
22
22
  if (!previous) return false;
23
23
  creds.device_token = previous;
24
24
  socket.auth = { ...socket.auth, device_token: previous };
@@ -62,8 +62,8 @@ export function openToken(record) {
62
62
  if (record.device_token) return record.device_token;
63
63
  try {
64
64
  if (record.token_store === TokenStoreKind.DPAPI && record.token_enc) return run('powershell.exe', [...PS, DPAPI_UNPROTECT], record.token_enc) || null;
65
- if (record.token_store === TokenStoreKind.KEYCHAIN) return run('/usr/bin/security', ['find-generic-password', '-a', record.device_id, '-s', SERVICE, '-w']) || null;
66
- if (record.token_store === TokenStoreKind.LIBSECRET) return run('secret-tool', ['lookup', 'service', SERVICE, 'account', record.device_id]) || null;
65
+ if (record.token_store === TokenStoreKind.KEYCHAIN) return run('/usr/bin/security', ['find-generic-password', '-a', record.token_account || record.device_id, '-s', SERVICE, '-w']) || null;
66
+ if (record.token_store === TokenStoreKind.LIBSECRET) return run('secret-tool', ['lookup', 'service', SERVICE, 'account', record.token_account || record.device_id]) || null;
67
67
  } catch { /* không giải được */ }
68
68
  return null;
69
69
  }
@@ -71,7 +71,7 @@ export function openToken(record) {
71
71
  /** Xoá token khỏi kho hệ điều hành (DPAPI / tệp thường: xoá tệp là đủ). */
72
72
  export function forgetToken(record) {
73
73
  try {
74
- if (record?.token_store === TokenStoreKind.KEYCHAIN) run('/usr/bin/security', ['delete-generic-password', '-a', record.device_id, '-s', SERVICE]);
75
- if (record?.token_store === TokenStoreKind.LIBSECRET) run('secret-tool', ['clear', 'service', SERVICE, 'account', record.device_id]);
74
+ if (record?.token_store === TokenStoreKind.KEYCHAIN) run('/usr/bin/security', ['delete-generic-password', '-a', record.token_account || record.device_id, '-s', SERVICE]);
75
+ if (record?.token_store === TokenStoreKind.LIBSECRET) run('secret-tool', ['clear', 'service', SERVICE, 'account', record.token_account || record.device_id]);
76
76
  } catch { /* không có thì thôi */ }
77
77
  }
@@ -0,0 +1,69 @@
1
+ import fs from 'fs';
2
+ import os from 'os';
3
+ import path from 'path';
4
+ import { createHash } from 'crypto';
5
+ import { writeFileAtomic } from './remote/atomicFile.mjs';
6
+
7
+ export const SERVER_CONFIG_FILE = path.join(process.env.AIVIN_REMOTE_HOME || path.join(os.homedir(), '.aivin'), 'server.json');
8
+ export const DEFAULT_SERVER_URL = 'https://api.aivin.cloud';
9
+
10
+ export function resolveServerUrl(input, { fallback = DEFAULT_SERVER_URL } = {}) {
11
+ const raw = String(input ?? '').trim();
12
+ if (!raw) return resolveServerUrl(fallback);
13
+ const alias = raw.toLowerCase();
14
+ if (alias === 'cloud') return 'https://api.aivin.cloud';
15
+ if (alias === 'vn') return 'https://api.aivin.vn';
16
+ const local = alias.match(/^local(?::(\d+))?$/);
17
+ if (local) {
18
+ const port = Number(local[1] || 3001);
19
+ if (port < 1 || port > 65535) throw new Error('Cổng local phải nằm trong khoảng 1–65535.');
20
+ return `http://localhost:${port}`;
21
+ }
22
+
23
+ const candidate = /^[a-z][a-z\d+.-]*:\/\//i.test(raw) ? raw : `https://${raw}`;
24
+ let url;
25
+ try { url = new URL(candidate); } catch { throw new Error(`Máy chủ không hợp lệ: ${raw}`); }
26
+ if (url.username || url.password) throw new Error('URL máy chủ không được chứa username hoặc password.');
27
+ const host = url.hostname.toLowerCase();
28
+ const loopback = host === 'localhost' || host === '127.0.0.1' || host === '::1' || host === '[::1]';
29
+ if (url.protocol !== 'https:' && !(url.protocol === 'http:' && loopback)) {
30
+ throw new Error('Chỉ localhost mới được dùng HTTP; máy chủ khác phải dùng HTTPS.');
31
+ }
32
+ for (const domain of ['aivin.cloud', 'aivin.vn']) {
33
+ if (host === domain) return `https://api.${domain}`;
34
+ if (host.endsWith(`.${domain}`)) {
35
+ const first = host.slice(0, -(domain.length + 1)).split('.').at(-1);
36
+ if (!/(^|-)api$/.test(first)) return `https://api.${domain}`;
37
+ }
38
+ }
39
+ url.hash = '';
40
+ url.search = '';
41
+ return url.toString().replace(/\/+$/, '');
42
+ }
43
+
44
+ export function serverKey(serverUrl) {
45
+ return createHash('sha256').update(resolveServerUrl(serverUrl)).digest('hex').slice(0, 24);
46
+ }
47
+
48
+ export function readActiveServer() {
49
+ try {
50
+ const value = JSON.parse(fs.readFileSync(SERVER_CONFIG_FILE, 'utf8'))?.server_url;
51
+ return value ? resolveServerUrl(value) : null;
52
+ } catch { return null; }
53
+ }
54
+
55
+ export function setActiveServer(serverUrl) {
56
+ const normalized = resolveServerUrl(serverUrl);
57
+ fs.mkdirSync(path.dirname(SERVER_CONFIG_FILE), { recursive: true });
58
+ writeFileAtomic(SERVER_CONFIG_FILE, `${JSON.stringify({ server_url: normalized }, null, 2)}\n`, { mode: 0o600 });
59
+ try { fs.chmodSync(SERVER_CONFIG_FILE, 0o600); } catch { /* Windows ACL follows the user profile */ }
60
+ return normalized;
61
+ }
62
+
63
+ export function serverLabel(serverUrl) {
64
+ const url = resolveServerUrl(serverUrl);
65
+ if (url === 'https://api.aivin.cloud') return 'Cloud';
66
+ if (url === 'https://api.aivin.vn') return 'VN';
67
+ if (url.startsWith('http://localhost:') || url.startsWith('http://127.0.0.1:')) return 'Local';
68
+ return new URL(url).host;
69
+ }
@@ -1,12 +1,12 @@
1
1
  # Aivin mascot
2
2
 
3
3
  <p align="center">
4
- <img src="media/aivin-mascot-live.gif" alt="The current live Aivin mascot showing chat, file drag and attached-file states" width="480" />
4
+ <img src="media/aivin-mascot-live.gif" alt="The live Aivin mascot ask box showing a conversation and the mascot" width="720" />
5
5
  </p>
6
6
 
7
7
  The Aivin mascot is the floating Windows interface for Aivin Remote. It shows what the agent is doing, accepts typed or spoken questions, and gives access to recent activity without opening the web app.
8
8
 
9
- The animation above is rendered from the current WPF visual tree. It demonstrates the real chat box, the file-drag target and the tray badge after two files have been attached.
9
+ The animation above is an enlarged capture of the real WPF ask box and mascot, cropped to remove excess space. It shows the live conversation and the mascot's file attachment states.
10
10
 
11
11
  ## How it connects to Aivin
12
12