ai_co_discuss 0.4.52 → 0.4.53

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/docs/runner.md CHANGED
@@ -43,7 +43,7 @@ codiscuss setup
43
43
 
44
44
  Windows 上在终端运行 `codiscuss setup` 时,“安装配置”一步有“桌面快捷方式”:还没有时默认勾选“创建‘共议’快捷方式”,已有时显示为“重新生成”、默认不勾(非交互运行不创建);也可随时运行 `codiscuss launcher` 生成或重新生成。它会生成“共议”程序,桌面和开始菜单各有一个快捷方式,双击即可启动服务并打开网站。服务运行时任务栏右下角有共议图标:左键打开**管理面板**(也可运行 `codiscuss panel`),右键可打开共议、重启或关闭服务。管理面板分两页:“概览”显示本机服务状态(可启动、重启、停止)、检查并安装新版本(同时更新你的房间技能)、开关“登录 Windows 时自动启动”;“账号”页左侧选账号,“管理员”里管理你自己的 Claude Code / Codex / Antigravity 登录(可直接登录、切换方式、退出;Claude Code 推荐 Setup Token)、API 密钥和已安装的技能(可从技能库添加);面板只能在这台电脑上打开。第一次需要在“设置 → 个性化 → 任务栏 → 其他系统托盘图标”里打开“共议”,图标才会常驻任务栏。
45
45
 
46
- 管理面板的每项操作也能用命令行完成:`codiscuss admin`(`codiscuss admin --help` 列出全部命令),适合经 SSH 远程管理这台电脑。`start`、`stop`(彻底退出:面板与图标、登录入口、隧道、共议服务;托管账号的实例照常运行)、`restart`、`update` 不需要面板在运行;`start` 在已登录的桌面会话里启动,任务栏图标照常出现。其余命令经运行中的面板执行,与点击界面效果相同,只有这台电脑的管理员能用;隔离账号时的管理员确认仍弹在这台电脑的桌面上。
46
+ 管理面板的每项操作也能用命令行完成:`codiscuss admin`(`codiscuss admin --help` 列出全部命令),适合经 SSH 远程管理这台电脑。`start`、`stop`(彻底退出:面板与图标、登录入口、隧道、共议服务;托管账号的实例照常运行)、`restart`、`update` 不需要面板在运行;`start` 在已登录的桌面会话里启动,任务栏图标照常出现。其余命令经运行中的面板执行,与点击界面效果相同,只有这台电脑的管理员能用。建或删隔离账号的 Windows 用户需要管理员权限:在管理员终端或管理员的 SSH 会话里直接执行,否则在这台电脑的桌面上弹出确认。`account add` 中途失败时,解决后重新执行同一命令,会从缺少的步骤接着做。
47
47
 
48
48
  给别人开账号用这台电脑上的 AI 时,在 setup 的“安装配置”里勾选“托管管理”(或加 `--hosting`,`--no-hosting` 关闭;选择会记住),面板会多出“托管账号”页:
49
49
  - 点“新建账号”,填账号名,默认“隔离”(给它一个独立的 Windows 用户,电脑会弹出一次管理员确认);向导会一次装好它要用的程序、房间技能、系统提示词和随包工作流,并启动它的网站,最后显示一次密码。
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ai_co_discuss",
3
- "version": "0.4.52",
3
+ "version": "0.4.53",
4
4
  "bin": {
5
5
  "codiscuss": "runner/codiscuss.mjs"
6
6
  },
@@ -3,8 +3,10 @@
3
3
  * running panel's own interface, so a command does exactly what the same click does.
4
4
  *
5
5
  * It opens the panel the way the taskbar icon does, with the key in the owner's service directory
6
- * (`admin-panel.json`, readable by the owner only), so only the computer's owner can use it. Elevation prompts and
7
- * browser windows the panel opens appear in the owner's desktop session, not in this terminal.
6
+ * (`admin-panel.json`, readable by the owner only), so only the computer's owner can use it. Browser windows the panel
7
+ * opens appear in the owner's desktop session, not in this terminal. Steps that need administrator rights run in this
8
+ * command when it already has them (an elevated terminal, an administrator's SSH session), through the panel's own
9
+ * operations; otherwise the panel asks on the desktop.
8
10
  *
9
11
  * Starting, stopping, restarting and updating work without the panel running: `start` launches everything in the owner's
10
12
  * desktop session (from an SSH session, through a one-time scheduled task that runs there), so the taskbar icon appears
@@ -20,14 +22,18 @@ import { data_home, entry_settings, entry_status, stop_entry } from '../gateway/
20
22
  import { local_status } from './discussion_local.mjs';
21
23
  import { stop_tunnel } from './hosting_tunnel.mjs';
22
24
  import { launcher_path } from './launcher.mjs';
25
+ import { running_elevated } from './os_users.mjs';
23
26
 
24
27
  const STATE_FILE = 'admin-panel.json';
25
28
  const OWNER = 'owner';
29
+ // What needs administrator rights (creating or removing an account's Windows user, which registers its instance task).
30
+ const ELEVATED_PATHS = /^accounts\/[^/]+\/os-user(\/remove)?$/;
26
31
  const LOGIN_WAIT_MS = 60_000, UPDATE_WAIT_MS = 10 * 60_000, START_WAIT_MS = 90_000, POLL_MS = 1500;
27
32
  const RUNNER = dirname(fileURLToPath(import.meta.url));
28
33
  const DESKTOP_TASK = 'CoDiscuss start (one time)';
29
34
 
30
- export const HELP = `用法:codiscuss admin <命令> [参数](与管理面板的操作一一对应,需管理面板正在运行)
35
+ export const HELP = `用法:codiscuss admin <命令> [参数](与管理面板的操作一一对应;除启动与退出外,需管理面板正在运行)
36
+ 需要管理员权限的步骤(建或删 Windows 用户),在管理员终端或管理员的 SSH 会话里直接执行;否则在电脑桌面上弹出确认。
31
37
 
32
38
  启动与退出(不需要面板在运行)
33
39
  start 启动共议:服务、管理面板与任务栏图标、登录入口与隧道(在桌面会话里启动)
@@ -36,7 +42,7 @@ export const HELP = `用法:codiscuss admin <命令> [参数](与管理面
36
42
  update [check] 检查更新;不带 check 时安装最新版(面板未运行时先启动)并等待每一步结果
37
43
 
38
44
  本机
39
- status 概览:服务、版本、开机自启、登录入口、托管账号
45
+ status [--full] 概览:服务、版本、开机自启、登录入口、托管账号;--full 输出面板的完整状态
40
46
  service start|stop|restart 只启停共议服务(网站与讨论服务)
41
47
  autostart on|off 登录 Windows 时自动启动
42
48
 
@@ -45,16 +51,20 @@ export const HELP = `用法:codiscuss admin <命令> [参数](与管理面
45
51
  entry mode local|relay|tunnel 访问方式:仅这台电脑 | 线上中转 | 自有域名
46
52
  entry join <接入码> 用接入码接入线上站点并切换到线上中转
47
53
  entry connect <站点> <绑定令牌> 只绑定线上站点(管理员远程用),不改访问方式
54
+ entry code [--config <wrangler.jsonc>] [--site <地址>] 在部署线上站点的电脑上生成接入码(不填用上次的)
55
+ entry tunnel <域名> [隧道名] 设置自有域名(Cloudflare 隧道),输出授权链接;域名须已托管在 Cloudflare
56
+ entry tunnel status 查看自有域名设置的进度
48
57
  runtime sync 同步隔离账号的程序副本
49
58
 
50
59
  托管账号
51
60
  account list
52
- account add <名> [--no-isolate] [--no-start] 创建、隔离、初始化 AI 环境、开启入口保护、启动;输出一次性密码
61
+ account add <名> [--no-isolate] [--no-start] 创建、隔离、初始化 AI 环境、开启入口保护、启动;输出一次性密码。
62
+ 中途失败时解决后重新执行同一命令,从缺少的步骤接着做
53
63
  account start|stop|disable|enable|remove|password <名>
54
64
  account isolate|unisolate|protect|unprotect <名>
55
65
 
56
66
  命令行登录(目标 owner 为管理员自己,否则为托管账号名)
57
- login status <目标>
67
+ login status <目标> 各程序的登录状态,以及每个程序可用的登录方式
58
68
  login set <目标> <claude|codex> <方式> --set 字段=值 ... 如 login set alice claude setup_token --set token=sk-ant-oat01-…
59
69
  login start <目标> <程序> <方式> 浏览器登录:输出链接,之后用 login submit 提交授权码或回调地址
60
70
  login submit <作业> <文本> / login progress <作业> / login cancel <作业>
@@ -97,24 +107,35 @@ const wait = ms => new Promise(done => setTimeout(done, ms));
97
107
  const need = (value, usage) => { if (!value) throw new Error(`缺少参数:${usage}`); return value; };
98
108
  const is_owner = target => target === OWNER;
99
109
 
100
- /** Create an account the way the panel's wizard does, step by step; the password is reported even if a later step fails. */
110
+ /**
111
+ * Create an account the way the panel's wizard does, step by step. Run again for an account that exists, it goes on
112
+ * from the first step missing; the password is reported once created, even if a later step fails.
113
+ */
101
114
  async function add_account(call, name, { isolate, start }) {
102
115
  const steps = [];
103
- const created = await call('accounts', { name });
104
- steps.push('已创建账号与文件夹');
116
+ const existing = (await call('state')).accounts?.find(item => item.name === name);
117
+ let password = '';
118
+ if (existing) steps.push('账号已存在,从缺少的步骤接着做(密码不变;需要新密码用 account password)');
119
+ else { password = (await call('accounts', { name })).password; steps.push('已创建账号与文件夹'); }
120
+ const report = extra => ({ name, ...(password ? { password } : {}), steps, ...extra });
105
121
  try {
106
- if (isolate) {
122
+ if (isolate && !existing?.os_user) {
107
123
  await call(`accounts/${name}/os-user`, {});
108
- steps.push('已创建独立的 Windows 用户(电脑上的管理员确认已通过)');
124
+ steps.push(running_elevated() ? '已创建独立的 Windows 用户' : '已创建独立的 Windows 用户(电脑上的管理员确认已通过)');
125
+ }
126
+ if (isolate) {
109
127
  const state = await call('state');
110
128
  if (!state.clis?.app || !state.clis?.python) { await call('clis/install', {}); steps.push('已安装隔离账号的程序副本'); }
111
129
  }
112
130
  await call(`accounts/${name}/env/init`, {});
113
131
  steps.push(isolate ? '已初始化 AI 环境' : '已安装随包工作流');
114
- if (process.platform === 'win32') { await call(`accounts/${name}/protect`, { on: true }); steps.push('已开启入口保护'); }
115
- if (start) { await call(`accounts/${name}/start`, {}); steps.push('已启动实例'); }
116
- } catch (error) { return { name, password: created.password, steps, failed: error.message }; }
117
- return { name, password: created.password, steps };
132
+ if (process.platform === 'win32' && !existing?.protected) { await call(`accounts/${name}/protect`, { on: true }); steps.push('已开启入口保护'); }
133
+ if (start && !existing?.running) { await call(`accounts/${name}/start`, {}); steps.push('已启动实例'); }
134
+ } catch (error) {
135
+ const rights = /管理员权限/.test(error.message) ? '在管理员终端或管理员的 SSH 会话里执行,或在电脑桌面上确认提权,' : '';
136
+ return report({ failed: error.message, next: `${rights}解决后重新执行 codiscuss admin account add ${name},会从这一步接着做` });
137
+ }
138
+ return report({});
118
139
  }
119
140
 
120
141
  const ACCOUNT_ACTIONS = { start: 'start', stop: 'stop', disable: 'disable', enable: 'enable', remove: 'remove', password: 'password',
@@ -142,7 +163,10 @@ async function start_login(call, target, cli, method) {
142
163
 
143
164
  async function login_command(call, [action, ...rest], values) {
144
165
  const target = (name) => (is_owner(name) ? '_' : need(name, '目标'));
145
- if (action === 'status') return call(`logins/${target(rest[0])}`);
166
+ if (action === 'status') {
167
+ const [status, state] = await Promise.all([call(`logins/${target(rest[0])}`), call('state')]);
168
+ return { status, methods: login_methods(state.login_methods) };
169
+ }
146
170
  if (action === 'set') {
147
171
  const [who, cli, method] = rest;
148
172
  const fields = Object.fromEntries((values.set || []).map(pair => { const at = pair.indexOf('='); return [pair.slice(0, at), pair.slice(at + 1)]; }));
@@ -155,6 +179,13 @@ async function login_command(call, [action, ...rest], values) {
155
179
  throw new Error(HELP);
156
180
  }
157
181
 
182
+ /** The ways each program signs in, as `login set` (filled-in fields) or `login start` (in a browser) takes them. */
183
+ function login_methods(methods = {}) {
184
+ return Object.fromEntries(Object.entries(methods).map(([cli, list]) => [cli, list.map(item => (item.interactive
185
+ ? `${item.key}:${item.label}(login start <目标> ${cli} ${item.key})`
186
+ : `${item.key}:${item.label}(login set <目标> ${cli} ${item.key} ${(item.fields || []).map(field => `--set ${field}=…`).join(' ')})`))]));
187
+ }
188
+
158
189
  async function key_command(call, [action, who, ref], values) {
159
190
  const base = is_owner(need(who, '目标')) ? 'owner/keys' : `accounts/${who}/keys`;
160
191
  if (action === 'list') return call(base);
@@ -295,19 +326,58 @@ async function update(call, data) {
295
326
  throw new Error('更新超过 10 分钟没有完成:之后用 codiscuss admin status 查看版本');
296
327
  }
297
328
 
298
- async function entry_command(call, [action, first, second]) {
329
+ async function entry_command(call, [action, first, second], values) {
299
330
  if (action === 'start' || action === 'stop') return call(`entry/${action}`, {});
300
331
  if (action === 'mode') return call('entry/mode', { mode: need(first, 'local|relay|tunnel') });
301
332
  if (action === 'join') return call('relay/join', { code: need(first, '接入码') });
302
333
  if (action === 'connect') return call('site/connect', { site: need(first, '站点'), setup_key: need(second, '绑定令牌') });
334
+ if (action === 'code') return call('site/code', { config: values.config || '', site: values.site || '' });
335
+ if (action === 'tunnel') return first === 'status' ? call('tunnel/setup') : setup_tunnel(call, need(first, '域名'), second || '');
303
336
  throw new Error(HELP);
304
337
  }
305
338
 
339
+ /** Start setting up the owner's domain and wait for the Cloudflare authorization link (or the end). */
340
+ async function setup_tunnel(call, host, name) {
341
+ await call('tunnel/setup', { host, name });
342
+ for (let waited = 0; waited < LOGIN_WAIT_MS; waited += POLL_MS) {
343
+ await wait(POLL_MS);
344
+ const job = await call('tunnel/setup');
345
+ if (job.url) return { ...job, note: '用任一浏览器打开 url,登录 Cloudflare 并选择这个域名授权;之后用 entry tunnel status 查看进度' };
346
+ if (job.status !== 'running') return job;
347
+ }
348
+ return { ...(await call('tunnel/setup')), note: '仍在进行,用 entry tunnel status 查看进度' };
349
+ }
350
+
351
+ /** The overview at a glance: what runs, where users sign in, and each account in one line. */
352
+ function summary(state) {
353
+ const { entry, service } = state;
354
+ const tunnel = entry?.tunnel ? (entry.tunnel.connected ? '已连接' : entry.tunnel.running ? '连接中' : '未运行') : undefined;
355
+ return { hosting: state.hosting, version: state.version, autostart: state.autostart,
356
+ service: { running: service.running, url: service.url, version: service.version, error: service.error || undefined },
357
+ ...(state.hosting ? {
358
+ entry: entry && { running: entry.running, mode: entry.mode, address: entry.address || `http://127.0.0.1:${entry.port}`, tunnel, problem: entry.relay?.problem || undefined },
359
+ runtime: state.clis,
360
+ accounts: state.accounts.map(item => ({ name: item.name, isolated: Boolean(item.os_user), protected: item.protected, running: item.running, disabled: item.disabled })),
361
+ } : {}) };
362
+ }
363
+
364
+ /**
365
+ * The panel's client, with the steps that need administrator rights done in this command when it already has them: an
366
+ * administrator's SSH session has no desktop to answer the panel's prompt.
367
+ */
368
+ async function with_elevation(call, data) {
369
+ if (!running_elevated()) return call;
370
+ const { create_panel } = await import('./admin_panel.mjs');
371
+ const { api } = create_panel({ data, key: '', port: 0 });
372
+ return (path, body) => (body !== undefined && ELEVATED_PATHS.test(path) ? api('POST', `/api/${path}`, body) : call(path, body));
373
+ }
374
+
306
375
  /** Run one `codiscuss admin` command and print its result as JSON. */
307
376
  export async function admin_cli(args, { client = panel_client, print = text => process.stdout.write(text) } = {}) {
308
377
  const { positionals, values } = parseArgs({ args, allowPositionals: true, options: {
309
378
  data: { type: 'string' }, help: { type: 'boolean', short: 'h' }, 'no-isolate': { type: 'boolean' }, 'no-start': { type: 'boolean' },
310
379
  set: { type: 'string', multiple: true }, 'base-url': { type: 'string' }, key: { type: 'string' }, file: { type: 'string' }, group: { type: 'string' },
380
+ full: { type: 'boolean' }, config: { type: 'string' }, site: { type: 'string' },
311
381
  } });
312
382
  const [command, ...rest] = positionals;
313
383
  if (!command || values.help || command === 'help') return print(HELP);
@@ -324,13 +394,16 @@ export async function admin_cli(args, { client = panel_client, print = text => p
324
394
  print(`${JSON.stringify(result, null, 2)}\n`);
325
395
  return result;
326
396
  }
327
- const call = await client(data);
397
+ const call = await with_elevation(await client(data), data);
328
398
  const commands = {
329
- status: async () => { const { login_methods: _methods, providers: _providers, ...state } = await call('state'); return state; },
399
+ status: async () => {
400
+ const { login_methods: _methods, providers: _providers, ...state } = await call('state');
401
+ return values.full ? state : summary(state);
402
+ },
330
403
  service: () => call(`service/${need(rest[0], 'start|stop|restart')}`, {}),
331
404
  autostart: () => call('autostart', { on: need(rest[0], 'on|off') === 'on' }),
332
405
  update: () => (rest[0] === 'check' ? call('update/check') : update(call, data)),
333
- entry: () => entry_command(call, rest),
406
+ entry: () => entry_command(call, rest, values),
334
407
  runtime: () => (rest[0] === 'sync' ? call('clis/install', {}) : Promise.reject(new Error(HELP))),
335
408
  account: () => account_command(call, rest, values),
336
409
  login: () => login_command(call, rest, values),
@@ -343,5 +416,7 @@ export async function admin_cli(args, { client = panel_client, print = text => p
343
416
  if (!commands[command]) throw new Error(HELP);
344
417
  const result = await commands[command]();
345
418
  print(`${JSON.stringify(result, null, 2)}\n`);
419
+ // A step that failed part way (account add) is reported in the result, and in the exit code for scripts.
420
+ if (result?.failed) process.exitCode = 1;
346
421
  return result;
347
422
  }
@@ -44,7 +44,7 @@ const VERSION = JSON.parse(readFileSync(join(ROOT, 'package.json'), 'utf8')).ver
44
44
  const NPM_PACKAGE = 'ai_co_discuss';
45
45
  const LATEST_URL = `https://registry.npmjs.org/${NPM_PACKAGE}/latest`;
46
46
  const LATEST_TTL_MS = 60 * 60 * 1000;
47
- const OUTSIDE_TIMEOUT_MS = 15_000, OUTSIDE_ATTEMPTS = 2;
47
+ const OUTSIDE_TIMEOUT_MS = 8000, OUTSIDE_ATTEMPTS = 2;
48
48
  // Node 24 (OpenSSL 3.5) offers a post-quantum key share first, which makes its TLS greeting span several packets; behind
49
49
  // a Clash (Mihomo) TUN such connections were reset more than half the time, while curl and browsers got through (seen
50
50
  // 2026-11-14). What the panel fetches outside is public (the latest version, a site's entry address, the relay's state),
@@ -102,8 +102,9 @@ function service_overview(data) {
102
102
  let latest = { at: 0, version: '' };
103
103
 
104
104
  /**
105
- * Fetch an outside address, trying once more when the connection is dropped; a request that stalls to its timeout is not
106
- * tried again (that would keep the owner waiting). Failing is reported naming the host, not as a problem of the site.
105
+ * Fetch an outside address, trying once more when it fails: now and then a request to Cloudflare stalls for many seconds
106
+ * while the next one answers in under two (seen 2026-11-14). Failing is reported naming the host, not as a problem of
107
+ * the site.
107
108
  */
108
109
  async function fetch_outside(url, timeout_ms) {
109
110
  for (let attempt = 1; ; attempt += 1) {
@@ -111,7 +112,7 @@ async function fetch_outside(url, timeout_ms) {
111
112
  return await fetch(url, { signal: AbortSignal.timeout(timeout_ms) });
112
113
  } catch (error) {
113
114
  const timed_out = error.name === 'TimeoutError';
114
- if (!timed_out && attempt < OUTSIDE_ATTEMPTS) continue;
115
+ if (attempt < OUTSIDE_ATTEMPTS) continue;
115
116
  const why = timed_out ? `${timeout_ms / 1000} 秒内没有响应` : `连接被断开(${error.cause?.code || error.message})`;
116
117
  throw new Error(`连不上 ${new URL(url).host}:${why},请检查这台电脑的网络或代理后重试`);
117
118
  }
@@ -741,7 +742,7 @@ export function create_panel({ data, key, port, ops = {}, sessions = new Set() }
741
742
  throw new Error('接口不存在');
742
743
  }
743
744
 
744
- return http.createServer(async (request, response) => {
745
+ const server = http.createServer(async (request, response) => {
745
746
  try {
746
747
  const url = new URL(request.url, 'http://127.0.0.1');
747
748
  // Only a page this browser opened from this address; anything else (another site, a renamed host) is refused.
@@ -780,6 +781,9 @@ export function create_panel({ data, key, port, ops = {}, sessions = new Set() }
780
781
  else send_json(response, error.status || 400, { error: error.message });
781
782
  }
782
783
  });
784
+ // The same operations without HTTP, for `codiscuss admin` run elevated (the steps that need administrator rights).
785
+ server.api = api;
786
+ return server;
783
787
  }
784
788
 
785
789
  /** The account entry as the hosting page shows it: whether it runs, its loopback port and the address customers open. */
@@ -113,15 +113,28 @@ function write_os_users(data, users) {
113
113
 
114
114
  const quote = text => `'${String(text).replaceAll("'", "''")}'`;
115
115
 
116
+ let elevated_already = null;
117
+
118
+ /**
119
+ * Whether this process already runs with administrator rights (an elevated terminal, or an administrator's SSH session,
120
+ * which Windows gives the full token): an elevated script then runs directly, with no prompt nobody could answer.
121
+ */
122
+ export function running_elevated() {
123
+ if (elevated_already === null) elevated_already = process.platform === 'win32' && spawnSync('net', ['session'], { stdio: 'ignore', windowsHide: true }).status === 0;
124
+ return elevated_already;
125
+ }
126
+
116
127
  /**
117
128
  * Run a PowerShell script and return what it printed.
118
129
  *
119
130
  * Args:
120
131
  * script: The script text; written to a file with a byte-order mark, since Windows PowerShell 5.1 reads one without it
121
132
  * in the ANSI code page.
122
- * elevated: Run through a UAC prompt; output then comes back through a file.
133
+ * elevated: Run through a UAC prompt (directly when this process is already elevated); output then comes back through
134
+ * a file.
123
135
  */
124
136
  export function run_powershell(script, { elevated = false, timeout = DEFAULT_TIMEOUT_MS } = {}) {
137
+ if (elevated && running_elevated()) elevated = false;
125
138
  mkdirSync(OWNER_JOB_ROOT, { recursive: true });
126
139
  const job = join(OWNER_JOB_ROOT, `owner-${process.pid}-${randomBytes(6).toString('hex')}`);
127
140
  mkdirSync(job);