ai_co_discuss 0.4.49 → 0.4.51
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 +187 -185
- package/package.json +107 -107
- package/runner/admin_cli.mjs +347 -0
- package/runner/codiscuss.mjs +135 -133
- package/web/modern.js +1 -1
package/package.json
CHANGED
|
@@ -1,107 +1,107 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "ai_co_discuss",
|
|
3
|
-
"version": "0.4.
|
|
4
|
-
"bin": {
|
|
5
|
-
"codiscuss": "runner/codiscuss.mjs"
|
|
6
|
-
},
|
|
7
|
-
"scripts": {
|
|
8
|
-
"dev": "npm run build && wrangler pages dev --ip 127.0.0.1 --port 8787",
|
|
9
|
-
"db:local": "wrangler d1 execute DB --local --file backend/schema.sql",
|
|
10
|
-
"db:remote": "wrangler d1 execute DB --remote --file backend/schema.sql",
|
|
11
|
-
"check": "node --check backend/worker.mjs && node --check web/app.js && node --check runner/account_space.mjs && node --check runner/admin_account.mjs && node --check runner/admin_panel.mjs && node --check runner/os_users.mjs && node --check runner/tray.mjs && node --check runner/launcher.mjs && node --check runner/codiscuss.mjs && node --check runner/discussion_api.mjs && node --check runner/discussion_launch.mjs && node --check runner/discussion_local.mjs && node --check runner/discussion_runner.mjs && node --check runner/discussion_service.mjs && node --check runner/discussion_setup.mjs && node --check runner/discussion_sidecar.mjs && node --check runner/discussion_skills.mjs && node --check runner/discussion_tui.mjs && node --check runner/execution_progress.mjs && node --check runner/expert_prompts.mjs && node --check runner/files.mjs && node --check runner/free_runner.mjs && node --check runner/remote_launch.mjs && node --check runner/remote_policy.mjs && node --check runner/device_auth.mjs && node --check runner/webauthn.mjs && node --check runner/scheduler.mjs && node --check runner/session_driver.mjs && node --check runner/workflows.mjs && node --check runner/workflow_flow.mjs && node --check runner/weixin.mjs && node --check runner/adapters/antigravity_app.mjs && node --check runner/adapters/antigravity_cli.mjs && node --check runner/adapters/antigravity_sidecar.mjs && node --check runner/adapters/api.mjs && node --check runner/adapters/claude.mjs && node --check runner/adapters/cli.mjs && node --check runner/adapters/codex_app.mjs && node --check runner/adapters/codex_app_client.mjs && node --check runner/adapters/codex_cli.mjs && node --check runner/adapters/common.mjs && node --check runner/adapters/index.mjs && node --check runner/adapters/profiles.mjs",
|
|
12
|
-
"runner": "node runner/discussion_runner.mjs",
|
|
13
|
-
"service": "node runner/discussion_service.mjs",
|
|
14
|
-
"build:ui": "vite build --config frontend/vite.config.mjs --mode markdown && vite build --config frontend/vite.config.mjs --mode backup && vite build --config frontend/vite.config.mjs --mode office && vite build --config frontend/vite.config.mjs --mode admin && vite build --config frontend/vite.config.mjs",
|
|
15
|
-
"test:markdown": "node --test tests/test_markdown.mjs",
|
|
16
|
-
"build": "npm run check && npm run build:ui && node scripts/build_pages.mjs",
|
|
17
|
-
"test:pages": "node --test tests/test_pages.mjs",
|
|
18
|
-
"deploy": "npm run build && wrangler pages deploy --branch main --commit-dirty=true",
|
|
19
|
-
"deploy:pages": "npm run deploy",
|
|
20
|
-
"test": "npm run test:python && npm run test:js",
|
|
21
|
-
"test:python": "python -m unittest discover -s tests -p test_*.py",
|
|
22
|
-
"test:js": "node --test tests/test_*.mjs",
|
|
23
|
-
"test:install": "node --test tests/test_setup.mjs tests/test_local.mjs tests/test_package.mjs"
|
|
24
|
-
},
|
|
25
|
-
"devDependencies": {
|
|
26
|
-
"@earendil-works/pi-coding-agent": "1.5.2",
|
|
27
|
-
"@iconify-json/lucide": "1.2.144",
|
|
28
|
-
"@mdit/plugin-katex": "1.1.3",
|
|
29
|
-
"@nuxt/ui": "4.11.3",
|
|
30
|
-
"@silurus/ooxml": "0.89.9",
|
|
31
|
-
"@tailwindcss/vite": "4.3.3",
|
|
32
|
-
"@videojs/core": "10.3.0",
|
|
33
|
-
"@videojs/html": "10.3.0",
|
|
34
|
-
"@vitejs/plugin-vue": "6.0.10",
|
|
35
|
-
"@vue-flow/background": "1.3.2",
|
|
36
|
-
"@vue-flow/controls": "1.1.3",
|
|
37
|
-
"@vue-flow/core": "1.48.2",
|
|
38
|
-
"@vue-flow/minimap": "1.5.4",
|
|
39
|
-
"docx-preview": "0.4.1",
|
|
40
|
-
"fflate": "0.8.3",
|
|
41
|
-
"jsqr": "1.4.0",
|
|
42
|
-
"katex": "0.18.10",
|
|
43
|
-
"markdown-it": "15.0.3",
|
|
44
|
-
"markdown-it-cjk-friendly": "3.0.0",
|
|
45
|
-
"motion-v": "2.7.0",
|
|
46
|
-
"papaparse": "5.7.0",
|
|
47
|
-
"pdfjs-dist": "6.4.299",
|
|
48
|
-
"uqr": "0.1.2",
|
|
49
|
-
"viewerjs": "1.15.2",
|
|
50
|
-
"vite": "8.3.4",
|
|
51
|
-
"vue": "3.5.44",
|
|
52
|
-
"wrangler": "4.151.0",
|
|
53
|
-
"xlsx": "https://cdn.sheetjs.com/xlsx-0.20.3/xlsx-0.20.3.tgz"
|
|
54
|
-
},
|
|
55
|
-
"description": "让不同 AI 在各自软件中协作的共享讨论空间",
|
|
56
|
-
"keywords": [
|
|
57
|
-
"ai",
|
|
58
|
-
"multi-agent",
|
|
59
|
-
"discussion",
|
|
60
|
-
"cli",
|
|
61
|
-
"codex",
|
|
62
|
-
"claude",
|
|
63
|
-
"gemini"
|
|
64
|
-
],
|
|
65
|
-
"homepage": "https://github.com/huyan1458/ai_co_discuss#readme",
|
|
66
|
-
"repository": {
|
|
67
|
-
"type": "git",
|
|
68
|
-
"url": "git+https://github.com/huyan1458/ai_co_discuss.git"
|
|
69
|
-
},
|
|
70
|
-
"bugs": {
|
|
71
|
-
"url": "https://github.com/huyan1458/ai_co_discuss/issues"
|
|
72
|
-
},
|
|
73
|
-
"author": "huyan1458",
|
|
74
|
-
"license": "MIT",
|
|
75
|
-
"engines": {
|
|
76
|
-
"node": ">=24"
|
|
77
|
-
},
|
|
78
|
-
"overrides": {
|
|
79
|
-
"fontless": {
|
|
80
|
-
"esbuild": "0.28.2"
|
|
81
|
-
}
|
|
82
|
-
},
|
|
83
|
-
"files": [
|
|
84
|
-
"runner/*.mjs",
|
|
85
|
-
"gateway/*.mjs",
|
|
86
|
-
"runner/adapters/*.mjs",
|
|
87
|
-
"runner/*.py",
|
|
88
|
-
"backend/*.py",
|
|
89
|
-
"backend/schema.sql",
|
|
90
|
-
"backend/migrations/*.sql",
|
|
91
|
-
"server.py",
|
|
92
|
-
"web/**",
|
|
93
|
-
".agents/skills/discuss-host/**",
|
|
94
|
-
".agents/skills/discuss-participant/**",
|
|
95
|
-
".agents/skills/discuss-goal/**",
|
|
96
|
-
".agents/skills/discuss-session/**",
|
|
97
|
-
".agents/skills/discuss-report/**",
|
|
98
|
-
".agents/skills/workflow-creator/**",
|
|
99
|
-
".agents/skills/local-tts-setup/**",
|
|
100
|
-
".agents/skills/local-stt-setup/**",
|
|
101
|
-
"workflows/**",
|
|
102
|
-
".github/SECURITY.md",
|
|
103
|
-
"docs/**",
|
|
104
|
-
"!**/*.log",
|
|
105
|
-
"LICENSE"
|
|
106
|
-
]
|
|
107
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "ai_co_discuss",
|
|
3
|
+
"version": "0.4.51",
|
|
4
|
+
"bin": {
|
|
5
|
+
"codiscuss": "runner/codiscuss.mjs"
|
|
6
|
+
},
|
|
7
|
+
"scripts": {
|
|
8
|
+
"dev": "npm run build && wrangler pages dev --ip 127.0.0.1 --port 8787",
|
|
9
|
+
"db:local": "wrangler d1 execute DB --local --file backend/schema.sql",
|
|
10
|
+
"db:remote": "wrangler d1 execute DB --remote --file backend/schema.sql",
|
|
11
|
+
"check": "node --check backend/worker.mjs && node --check web/app.js && node --check runner/account_space.mjs && node --check runner/admin_account.mjs && node --check runner/admin_panel.mjs && node --check runner/admin_cli.mjs && node --check runner/os_users.mjs && node --check runner/tray.mjs && node --check runner/launcher.mjs && node --check runner/codiscuss.mjs && node --check runner/discussion_api.mjs && node --check runner/discussion_launch.mjs && node --check runner/discussion_local.mjs && node --check runner/discussion_runner.mjs && node --check runner/discussion_service.mjs && node --check runner/discussion_setup.mjs && node --check runner/discussion_sidecar.mjs && node --check runner/discussion_skills.mjs && node --check runner/discussion_tui.mjs && node --check runner/execution_progress.mjs && node --check runner/expert_prompts.mjs && node --check runner/files.mjs && node --check runner/free_runner.mjs && node --check runner/remote_launch.mjs && node --check runner/remote_policy.mjs && node --check runner/device_auth.mjs && node --check runner/webauthn.mjs && node --check runner/scheduler.mjs && node --check runner/session_driver.mjs && node --check runner/workflows.mjs && node --check runner/workflow_flow.mjs && node --check runner/weixin.mjs && node --check runner/adapters/antigravity_app.mjs && node --check runner/adapters/antigravity_cli.mjs && node --check runner/adapters/antigravity_sidecar.mjs && node --check runner/adapters/api.mjs && node --check runner/adapters/claude.mjs && node --check runner/adapters/cli.mjs && node --check runner/adapters/codex_app.mjs && node --check runner/adapters/codex_app_client.mjs && node --check runner/adapters/codex_cli.mjs && node --check runner/adapters/common.mjs && node --check runner/adapters/index.mjs && node --check runner/adapters/profiles.mjs",
|
|
12
|
+
"runner": "node runner/discussion_runner.mjs",
|
|
13
|
+
"service": "node runner/discussion_service.mjs",
|
|
14
|
+
"build:ui": "vite build --config frontend/vite.config.mjs --mode markdown && vite build --config frontend/vite.config.mjs --mode backup && vite build --config frontend/vite.config.mjs --mode office && vite build --config frontend/vite.config.mjs --mode admin && vite build --config frontend/vite.config.mjs",
|
|
15
|
+
"test:markdown": "node --test tests/test_markdown.mjs",
|
|
16
|
+
"build": "npm run check && npm run build:ui && node scripts/build_pages.mjs",
|
|
17
|
+
"test:pages": "node --test tests/test_pages.mjs",
|
|
18
|
+
"deploy": "npm run build && wrangler pages deploy --branch main --commit-dirty=true",
|
|
19
|
+
"deploy:pages": "npm run deploy",
|
|
20
|
+
"test": "npm run test:python && npm run test:js",
|
|
21
|
+
"test:python": "python -m unittest discover -s tests -p test_*.py",
|
|
22
|
+
"test:js": "node --test tests/test_*.mjs",
|
|
23
|
+
"test:install": "node --test tests/test_setup.mjs tests/test_local.mjs tests/test_package.mjs"
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"@earendil-works/pi-coding-agent": "1.5.2",
|
|
27
|
+
"@iconify-json/lucide": "1.2.144",
|
|
28
|
+
"@mdit/plugin-katex": "1.1.3",
|
|
29
|
+
"@nuxt/ui": "4.11.3",
|
|
30
|
+
"@silurus/ooxml": "0.89.9",
|
|
31
|
+
"@tailwindcss/vite": "4.3.3",
|
|
32
|
+
"@videojs/core": "10.3.0",
|
|
33
|
+
"@videojs/html": "10.3.0",
|
|
34
|
+
"@vitejs/plugin-vue": "6.0.10",
|
|
35
|
+
"@vue-flow/background": "1.3.2",
|
|
36
|
+
"@vue-flow/controls": "1.1.3",
|
|
37
|
+
"@vue-flow/core": "1.48.2",
|
|
38
|
+
"@vue-flow/minimap": "1.5.4",
|
|
39
|
+
"docx-preview": "0.4.1",
|
|
40
|
+
"fflate": "0.8.3",
|
|
41
|
+
"jsqr": "1.4.0",
|
|
42
|
+
"katex": "0.18.10",
|
|
43
|
+
"markdown-it": "15.0.3",
|
|
44
|
+
"markdown-it-cjk-friendly": "3.0.0",
|
|
45
|
+
"motion-v": "2.7.0",
|
|
46
|
+
"papaparse": "5.7.0",
|
|
47
|
+
"pdfjs-dist": "6.4.299",
|
|
48
|
+
"uqr": "0.1.2",
|
|
49
|
+
"viewerjs": "1.15.2",
|
|
50
|
+
"vite": "8.3.4",
|
|
51
|
+
"vue": "3.5.44",
|
|
52
|
+
"wrangler": "4.151.0",
|
|
53
|
+
"xlsx": "https://cdn.sheetjs.com/xlsx-0.20.3/xlsx-0.20.3.tgz"
|
|
54
|
+
},
|
|
55
|
+
"description": "让不同 AI 在各自软件中协作的共享讨论空间",
|
|
56
|
+
"keywords": [
|
|
57
|
+
"ai",
|
|
58
|
+
"multi-agent",
|
|
59
|
+
"discussion",
|
|
60
|
+
"cli",
|
|
61
|
+
"codex",
|
|
62
|
+
"claude",
|
|
63
|
+
"gemini"
|
|
64
|
+
],
|
|
65
|
+
"homepage": "https://github.com/huyan1458/ai_co_discuss#readme",
|
|
66
|
+
"repository": {
|
|
67
|
+
"type": "git",
|
|
68
|
+
"url": "git+https://github.com/huyan1458/ai_co_discuss.git"
|
|
69
|
+
},
|
|
70
|
+
"bugs": {
|
|
71
|
+
"url": "https://github.com/huyan1458/ai_co_discuss/issues"
|
|
72
|
+
},
|
|
73
|
+
"author": "huyan1458",
|
|
74
|
+
"license": "MIT",
|
|
75
|
+
"engines": {
|
|
76
|
+
"node": ">=24"
|
|
77
|
+
},
|
|
78
|
+
"overrides": {
|
|
79
|
+
"fontless": {
|
|
80
|
+
"esbuild": "0.28.2"
|
|
81
|
+
}
|
|
82
|
+
},
|
|
83
|
+
"files": [
|
|
84
|
+
"runner/*.mjs",
|
|
85
|
+
"gateway/*.mjs",
|
|
86
|
+
"runner/adapters/*.mjs",
|
|
87
|
+
"runner/*.py",
|
|
88
|
+
"backend/*.py",
|
|
89
|
+
"backend/schema.sql",
|
|
90
|
+
"backend/migrations/*.sql",
|
|
91
|
+
"server.py",
|
|
92
|
+
"web/**",
|
|
93
|
+
".agents/skills/discuss-host/**",
|
|
94
|
+
".agents/skills/discuss-participant/**",
|
|
95
|
+
".agents/skills/discuss-goal/**",
|
|
96
|
+
".agents/skills/discuss-session/**",
|
|
97
|
+
".agents/skills/discuss-report/**",
|
|
98
|
+
".agents/skills/workflow-creator/**",
|
|
99
|
+
".agents/skills/local-tts-setup/**",
|
|
100
|
+
".agents/skills/local-stt-setup/**",
|
|
101
|
+
"workflows/**",
|
|
102
|
+
".github/SECURITY.md",
|
|
103
|
+
"docs/**",
|
|
104
|
+
"!**/*.log",
|
|
105
|
+
"LICENSE"
|
|
106
|
+
]
|
|
107
|
+
}
|
|
@@ -0,0 +1,347 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `codiscuss admin`: every operation of the management panel from a command line (for instance over SSH), through the
|
|
3
|
+
* running panel's own interface, so a command does exactly what the same click does.
|
|
4
|
+
*
|
|
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.
|
|
8
|
+
*
|
|
9
|
+
* Starting, stopping, restarting and updating work without the panel running: `start` launches everything in the owner's
|
|
10
|
+
* desktop session (from an SSH session, through a one-time scheduled task that runs there), so the taskbar icon appears
|
|
11
|
+
* and nothing ends when the SSH connection closes; `stop` ends the panel with its icon, the entry, the tunnel and the
|
|
12
|
+
* owner's service, keeping every setting, so the next `start` brings back what was running.
|
|
13
|
+
*/
|
|
14
|
+
import { spawn, spawnSync } from 'node:child_process';
|
|
15
|
+
import { existsSync, readdirSync, readFileSync, rmSync, statSync } from 'node:fs';
|
|
16
|
+
import { basename, dirname, join, relative } from 'node:path';
|
|
17
|
+
import { fileURLToPath } from 'node:url';
|
|
18
|
+
import { parseArgs } from 'node:util';
|
|
19
|
+
import { data_home, entry_settings, entry_status, stop_entry } from '../gateway/gateway.mjs';
|
|
20
|
+
import { local_status } from './discussion_local.mjs';
|
|
21
|
+
import { stop_tunnel } from './hosting_tunnel.mjs';
|
|
22
|
+
import { launcher_path } from './launcher.mjs';
|
|
23
|
+
|
|
24
|
+
const STATE_FILE = 'admin-panel.json';
|
|
25
|
+
const OWNER = 'owner';
|
|
26
|
+
const LOGIN_WAIT_MS = 60_000, UPDATE_WAIT_MS = 10 * 60_000, START_WAIT_MS = 90_000, POLL_MS = 1500;
|
|
27
|
+
const RUNNER = dirname(fileURLToPath(import.meta.url));
|
|
28
|
+
const DESKTOP_TASK = 'CoDiscuss start (one time)';
|
|
29
|
+
|
|
30
|
+
export const HELP = `用法:codiscuss admin <命令> [参数](与管理面板的操作一一对应,需管理面板正在运行)
|
|
31
|
+
|
|
32
|
+
启动与退出(不需要面板在运行)
|
|
33
|
+
start 启动共议:服务、管理面板与任务栏图标、登录入口与隧道(在桌面会话里启动)
|
|
34
|
+
stop 彻底退出:管理面板与图标、登录入口、隧道、共议服务;托管账号的实例照常运行
|
|
35
|
+
restart stop 后 start
|
|
36
|
+
update [check] 检查更新;不带 check 时安装最新版(面板未运行时先启动)并等待每一步结果
|
|
37
|
+
|
|
38
|
+
本机
|
|
39
|
+
status 概览:服务、版本、开机自启、登录入口、托管账号
|
|
40
|
+
service start|stop|restart 只启停共议服务(网站与讨论服务)
|
|
41
|
+
autostart on|off 登录 Windows 时自动启动
|
|
42
|
+
|
|
43
|
+
登录入口与外网访问(需打开托管管理)
|
|
44
|
+
entry start|stop 启动或停止登录入口
|
|
45
|
+
entry mode local|relay|tunnel 外网访问方式
|
|
46
|
+
entry join <接入码> 用接入码接入线上站点并切换到线上中转
|
|
47
|
+
entry connect <站点> <绑定令牌> 只绑定线上站点(管理员远程用),不改外网访问方式
|
|
48
|
+
runtime sync 同步隔离账号的程序副本
|
|
49
|
+
|
|
50
|
+
托管账号
|
|
51
|
+
account list
|
|
52
|
+
account add <名> [--no-isolate] [--no-start] 创建、隔离、初始化 AI 环境、开启入口保护、启动;输出一次性密码
|
|
53
|
+
account start|stop|disable|enable|remove|password <名>
|
|
54
|
+
account isolate|unisolate|protect|unprotect <名>
|
|
55
|
+
|
|
56
|
+
命令行登录(目标 owner 为管理员自己,否则为托管账号名)
|
|
57
|
+
login status <目标>
|
|
58
|
+
login set <目标> <claude|codex> <方式> --set 字段=值 ... 如 login set alice claude setup_token --set token=sk-ant-oat01-…
|
|
59
|
+
login start <目标> <程序> <方式> 浏览器登录:输出链接,之后用 login submit 提交授权码或回调地址
|
|
60
|
+
login submit <作业> <文本> / login progress <作业> / login cancel <作业>
|
|
61
|
+
logout <目标> <程序>
|
|
62
|
+
|
|
63
|
+
API 密钥、技能、工作流、系统提示词(目标同上)
|
|
64
|
+
key list <目标> / key add <目标> --base-url <地址> --key <密钥> / key remove <目标> <编号>
|
|
65
|
+
skill library / skill import <文件夹或.zip> / skill delete <名>
|
|
66
|
+
skill list <目标> / skill install <目标> <名>... / skill remove <目标> <名>...
|
|
67
|
+
workflow list <目标> / workflow install <目标>
|
|
68
|
+
prompt get <目标> / prompt set <目标> --file <文件> [--group <键>] 管理员的提示词按组保存(见 prompt get)
|
|
69
|
+
prompt default get / prompt default set --file <文件> 新托管账号的默认系统提示词
|
|
70
|
+
|
|
71
|
+
结果以 JSON 输出。--data 指定管理员服务目录(默认 ~/.codiscuss)。
|
|
72
|
+
`;
|
|
73
|
+
|
|
74
|
+
/** A client of the running panel: enters with the owner key and keeps the session for the requests that follow. */
|
|
75
|
+
export async function panel_client(data) {
|
|
76
|
+
let state;
|
|
77
|
+
try { state = JSON.parse(readFileSync(join(data, STATE_FILE), 'utf8')); } catch { state = null; }
|
|
78
|
+
if (!state?.port || !state?.key) throw new Error('管理面板没有在运行:在这台电脑的桌面上运行 codiscuss panel open,或从任务栏图标打开');
|
|
79
|
+
const origin = `http://127.0.0.1:${state.port}`;
|
|
80
|
+
let entered;
|
|
81
|
+
try { entered = await fetch(`${origin}/enter?key=${state.key}`, { redirect: 'manual', signal: AbortSignal.timeout(10_000) }); }
|
|
82
|
+
catch { throw new Error('管理面板没有响应:在这台电脑的桌面上运行 codiscuss panel open'); }
|
|
83
|
+
const cookie = (entered.headers.get('set-cookie') || '').split(';')[0];
|
|
84
|
+
if (entered.status !== 302 || !cookie) throw new Error('管理面板拒绝了钥匙(面板可能刚重启):稍后重试');
|
|
85
|
+
return async function call(path, body) {
|
|
86
|
+
const init = { headers: { cookie }, signal: AbortSignal.timeout(15 * 60_000) };
|
|
87
|
+
if (body !== undefined) Object.assign(init, { method: 'POST', body: JSON.stringify(body),
|
|
88
|
+
headers: { cookie, origin, 'x-codiscuss-panel': '1', 'content-type': 'application/json' } });
|
|
89
|
+
const response = await fetch(`${origin}/api/${path}`, init);
|
|
90
|
+
const result = await response.json().catch(() => ({}));
|
|
91
|
+
if (!response.ok) throw new Error(result.error || `管理面板返回 ${response.status}`);
|
|
92
|
+
return result;
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const wait = ms => new Promise(done => setTimeout(done, ms));
|
|
97
|
+
const need = (value, usage) => { if (!value) throw new Error(`缺少参数:${usage}`); return value; };
|
|
98
|
+
const is_owner = target => target === OWNER;
|
|
99
|
+
|
|
100
|
+
/** Create an account the way the panel's wizard does, step by step; the password is reported even if a later step fails. */
|
|
101
|
+
async function add_account(call, name, { isolate, start }) {
|
|
102
|
+
const steps = [];
|
|
103
|
+
const created = await call('accounts', { name });
|
|
104
|
+
steps.push('已创建账号与文件夹');
|
|
105
|
+
try {
|
|
106
|
+
if (isolate) {
|
|
107
|
+
await call(`accounts/${name}/os-user`, {});
|
|
108
|
+
steps.push('已创建独立的 Windows 用户(电脑上的管理员确认已通过)');
|
|
109
|
+
const state = await call('state');
|
|
110
|
+
if (!state.clis?.app || !state.clis?.python) { await call('clis/install', {}); steps.push('已安装隔离账号的程序副本'); }
|
|
111
|
+
}
|
|
112
|
+
await call(`accounts/${name}/env/init`, {});
|
|
113
|
+
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 };
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const ACCOUNT_ACTIONS = { start: 'start', stop: 'stop', disable: 'disable', enable: 'enable', remove: 'remove', password: 'password',
|
|
121
|
+
isolate: 'os-user', unisolate: 'os-user/remove' };
|
|
122
|
+
|
|
123
|
+
async function account_command(call, [action, name], values) {
|
|
124
|
+
if (action === 'list') return (await call('state')).accounts;
|
|
125
|
+
need(name, `account ${action} <名>`);
|
|
126
|
+
if (action === 'add') return add_account(call, name, { isolate: !values['no-isolate'], start: !values['no-start'] });
|
|
127
|
+
if (action === 'protect' || action === 'unprotect') return call(`accounts/${name}/protect`, { on: action === 'protect' });
|
|
128
|
+
if (!ACCOUNT_ACTIONS[action]) throw new Error(HELP);
|
|
129
|
+
return call(`accounts/${name}/${ACCOUNT_ACTIONS[action]}`, {});
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** Start a browser sign-in and wait until its link (or its end) is there. */
|
|
133
|
+
async function start_login(call, target, cli, method) {
|
|
134
|
+
const { id } = await call(`logins/${target}/${cli}/start`, { method });
|
|
135
|
+
for (let waited = 0; waited < LOGIN_WAIT_MS; waited += POLL_MS) {
|
|
136
|
+
await wait(POLL_MS);
|
|
137
|
+
const progress = await call(`login-jobs/${id}`);
|
|
138
|
+
if (progress.link || progress.ended) return { job: id, ...progress };
|
|
139
|
+
}
|
|
140
|
+
return { job: id, link: '', note: '一分钟内没有出现登录链接,可用 login progress 继续查看' };
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
async function login_command(call, [action, ...rest], values) {
|
|
144
|
+
const target = (name) => (is_owner(name) ? '_' : need(name, '目标'));
|
|
145
|
+
if (action === 'status') return call(`logins/${target(rest[0])}`);
|
|
146
|
+
if (action === 'set') {
|
|
147
|
+
const [who, cli, method] = rest;
|
|
148
|
+
const fields = Object.fromEntries((values.set || []).map(pair => { const at = pair.indexOf('='); return [pair.slice(0, at), pair.slice(at + 1)]; }));
|
|
149
|
+
return call(`logins/${target(who)}/${need(cli, '程序')}/apply`, { method: need(method, '方式'), ...fields });
|
|
150
|
+
}
|
|
151
|
+
if (action === 'start') return start_login(call, target(rest[0]), need(rest[1], '程序'), need(rest[2], '方式'));
|
|
152
|
+
if (action === 'submit') return call(`login-jobs/${need(rest[0], '作业')}/submit`, { text: need(rest.slice(1).join(' '), '文本') });
|
|
153
|
+
if (action === 'progress') return call(`login-jobs/${need(rest[0], '作业')}`);
|
|
154
|
+
if (action === 'cancel') return call(`login-jobs/${need(rest[0], '作业')}/cancel`, {});
|
|
155
|
+
throw new Error(HELP);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
async function key_command(call, [action, who, ref], values) {
|
|
159
|
+
const base = is_owner(need(who, '目标')) ? 'owner/keys' : `accounts/${who}/keys`;
|
|
160
|
+
if (action === 'list') return call(base);
|
|
161
|
+
if (action === 'add') return call(base, { base_url: need(values['base-url'], '--base-url'), api_key: need(values.key, '--key') });
|
|
162
|
+
if (action === 'remove') return call(`${base}/remove`, { ref: need(ref, '编号') });
|
|
163
|
+
throw new Error(HELP);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** A skill folder as the panel's drop zone sends it: every file with its path and base64 content. */
|
|
167
|
+
function folder_files(folder) {
|
|
168
|
+
const files = [];
|
|
169
|
+
const walk = (current) => {
|
|
170
|
+
for (const entry of readdirSync(current, { withFileTypes: true })) {
|
|
171
|
+
const path = join(current, entry.name);
|
|
172
|
+
if (entry.isDirectory()) walk(path);
|
|
173
|
+
else files.push({ path: relative(folder, path).replaceAll('\\', '/'), data: readFileSync(path).toString('base64') });
|
|
174
|
+
}
|
|
175
|
+
};
|
|
176
|
+
walk(folder);
|
|
177
|
+
return files;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
async function skill_command(call, [action, who, ...names]) {
|
|
181
|
+
if (action === 'library') return call('skill-library');
|
|
182
|
+
if (action === 'import') {
|
|
183
|
+
const path = need(who, '文件夹或 .zip');
|
|
184
|
+
const body = statSync(path).isDirectory() ? { name: basename(path), files: folder_files(path) } : { zip: readFileSync(path).toString('base64') };
|
|
185
|
+
return call('skill-library', body);
|
|
186
|
+
}
|
|
187
|
+
if (action === 'delete') return call(`skill-library/${need(who, '名')}/remove`, {});
|
|
188
|
+
const base = is_owner(need(who, '目标')) ? 'owner/env' : `accounts/${who}/env`;
|
|
189
|
+
if (action === 'list') return (await call(base)).skills;
|
|
190
|
+
if (action === 'install' || action === 'remove') return call(`${base}/skills`, { [action]: names });
|
|
191
|
+
throw new Error(HELP);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
async function workflow_command(call, [action, who]) {
|
|
195
|
+
const base = is_owner(need(who, '目标')) ? 'owner/workflows' : `accounts/${who}/workflows`;
|
|
196
|
+
if (action === 'list') return call(base);
|
|
197
|
+
if (action === 'install') return call(`${base}/install`, {});
|
|
198
|
+
throw new Error(HELP);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
async function prompt_command(call, [action, who], values) {
|
|
202
|
+
if (action === 'default') {
|
|
203
|
+
if (who === 'get') return call('hosted-prompt');
|
|
204
|
+
if (who === 'set') return call('hosted-prompt', { text: readFileSync(need(values.file, '--file'), 'utf8') });
|
|
205
|
+
throw new Error(HELP);
|
|
206
|
+
}
|
|
207
|
+
need(who, '目标');
|
|
208
|
+
if (!is_owner(who)) {
|
|
209
|
+
if (action === 'get') return { prompt: (await call(`accounts/${who}/env`)).prompt };
|
|
210
|
+
if (action === 'set') return call(`accounts/${who}/env/prompt`, { text: readFileSync(need(values.file, '--file'), 'utf8') });
|
|
211
|
+
throw new Error(HELP);
|
|
212
|
+
}
|
|
213
|
+
const groups = await call('owner/prompts');
|
|
214
|
+
if (action === 'get') return groups;
|
|
215
|
+
if (action !== 'set') throw new Error(HELP);
|
|
216
|
+
// The owner's prompt files are kept as groups of hard links; the one to write is named, or the only one there is.
|
|
217
|
+
const group = values.group ? groups.find(item => item.key === values.group) : groups.length === 1 ? groups[0] : null;
|
|
218
|
+
if (!group) throw new Error(`请用 --group 指定要写入的组:${groups.map(item => item.key).join('、')}`);
|
|
219
|
+
return call('owner/prompts', { key: group.key, text: readFileSync(need(values.file, '--file'), 'utf8'), original: group.text });
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
// ---- Starting and quitting, without the panel ----
|
|
223
|
+
|
|
224
|
+
function panel_state(data) {
|
|
225
|
+
try { return JSON.parse(readFileSync(join(data, STATE_FILE), 'utf8')); } catch { return null; }
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
function alive(pid) {
|
|
229
|
+
if (!(pid > 0)) return false;
|
|
230
|
+
try { process.kill(pid, 0); return true; } catch { return false; }
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Run a program in the owner's desktop session. From an SSH session (no desktop) a one-time scheduled task of the owner,
|
|
235
|
+
* interactive only, runs it there and is removed again; elsewhere it is started directly.
|
|
236
|
+
*/
|
|
237
|
+
function start_in_desktop(program, args) {
|
|
238
|
+
if (!(process.platform === 'win32' && process.env.SSH_CONNECTION)) {
|
|
239
|
+
spawn(program, args, { stdio: 'ignore', windowsHide: true }).unref();
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
const quote = text => `'${String(text).replaceAll("'", "''")}'`;
|
|
243
|
+
const script = `$ErrorActionPreference = 'Stop'
|
|
244
|
+
$action = New-ScheduledTaskAction -Execute ${quote(program)} -Argument ${quote(args.map(arg => `"${arg}"`).join(' '))}
|
|
245
|
+
$principal = New-ScheduledTaskPrincipal -UserId ([Security.Principal.WindowsIdentity]::GetCurrent().Name) -LogonType Interactive -RunLevel Limited
|
|
246
|
+
Register-ScheduledTask -TaskName ${quote(DESKTOP_TASK)} -Action $action -Principal $principal -Force | Out-Null
|
|
247
|
+
Start-ScheduledTask -TaskName ${quote(DESKTOP_TASK)}
|
|
248
|
+
Start-Sleep -Seconds 3
|
|
249
|
+
Unregister-ScheduledTask -TaskName ${quote(DESKTOP_TASK)} -Confirm:$false`;
|
|
250
|
+
const result = spawnSync('powershell.exe', ['-NoProfile', '-NonInteractive', '-Command', script], { encoding: 'utf8', windowsHide: true, timeout: 60_000 });
|
|
251
|
+
if (result.status !== 0) {
|
|
252
|
+
throw new Error(`没能在桌面会话里启动(需要这台电脑上有人登录着 Windows):${(result.stderr || result.error?.message || '').trim().split(/\r?\n/)[0]}`);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/** Start everything as the 共议 launcher does at login, and wait until the service and the panel answer. */
|
|
257
|
+
async function start_all(data) {
|
|
258
|
+
if (local_status(data).running && panel_state(data)?.pid && alive(panel_state(data).pid)) return { started: false, note: '已在运行' };
|
|
259
|
+
const launcher = launcher_path(data);
|
|
260
|
+
if (existsSync(launcher)) start_in_desktop(launcher, ['--start']);
|
|
261
|
+
else start_in_desktop(process.execPath, [join(RUNNER, 'discussion_local.mjs'), 'start']);
|
|
262
|
+
for (let waited = 0; waited < START_WAIT_MS; waited += POLL_MS) {
|
|
263
|
+
await wait(POLL_MS);
|
|
264
|
+
const state = panel_state(data);
|
|
265
|
+
if (local_status(data).running && state?.pid && alive(state.pid)) return { started: true, service: local_status(data).url, panel: state.port };
|
|
266
|
+
}
|
|
267
|
+
throw new Error(`启动超过 ${START_WAIT_MS / 1000} 秒没有完成:查看 ${join(data, 'local', 'supervisor.log')} 与 admin-panel.log`);
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/** Quit everything but the hosted accounts' instances, keeping every setting (what ran comes back with the next start). */
|
|
271
|
+
async function stop_all(data) {
|
|
272
|
+
const steps = [];
|
|
273
|
+
const panel = panel_state(data);
|
|
274
|
+
// The panel first: it would start the entry and the tunnel again.
|
|
275
|
+
if (panel?.pid && alive(panel.pid)) { process.kill(panel.pid); steps.push('已关闭管理面板与任务栏图标'); }
|
|
276
|
+
rmSync(join(data, STATE_FILE), { force: true });
|
|
277
|
+
if ((await entry_status(data)).running) { await stop_entry(data); steps.push('已停止登录入口'); }
|
|
278
|
+
if (entry_settings(data).tunnel) { stop_tunnel(data); steps.push('已停止隧道'); }
|
|
279
|
+
if (local_status(data).pid || local_status(data).service_pid) {
|
|
280
|
+
const result = spawnSync(process.execPath, [join(RUNNER, 'discussion_local.mjs'), 'stop'], { encoding: 'utf8', windowsHide: true,
|
|
281
|
+
env: { ...process.env, DISCUSS_SERVICE_HOME: data }, timeout: 120_000 });
|
|
282
|
+
if (result.status !== 0) throw new Error(`共议服务没有停下:${(result.stderr || '').trim()}`);
|
|
283
|
+
steps.push('已停止共议服务');
|
|
284
|
+
}
|
|
285
|
+
return { stopped: true, steps: steps.length ? steps : ['原本就没有在运行'] };
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/** Install the latest version and wait for the panel, which restarts on it, to report every step. */
|
|
289
|
+
async function update(call, data) {
|
|
290
|
+
await call('update', {});
|
|
291
|
+
for (let waited = 0; waited < UPDATE_WAIT_MS; waited += POLL_MS) {
|
|
292
|
+
await wait(POLL_MS);
|
|
293
|
+
try { const job = await (await panel_client(data))('update/progress'); if (job.status === 'done') return job; } catch { /* restarting */ }
|
|
294
|
+
}
|
|
295
|
+
throw new Error('更新超过 10 分钟没有完成:之后用 codiscuss admin status 查看版本');
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
async function entry_command(call, [action, first, second]) {
|
|
299
|
+
if (action === 'start' || action === 'stop') return call(`entry/${action}`, {});
|
|
300
|
+
if (action === 'mode') return call('entry/mode', { mode: need(first, 'local|relay|tunnel') });
|
|
301
|
+
if (action === 'join') return call('relay/join', { code: need(first, '接入码') });
|
|
302
|
+
if (action === 'connect') return call('site/connect', { site: need(first, '站点'), setup_key: need(second, '绑定令牌') });
|
|
303
|
+
throw new Error(HELP);
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/** Run one `codiscuss admin` command and print its result as JSON. */
|
|
307
|
+
export async function admin_cli(args, { client = panel_client, print = text => process.stdout.write(text) } = {}) {
|
|
308
|
+
const { positionals, values } = parseArgs({ args, allowPositionals: true, options: {
|
|
309
|
+
data: { type: 'string' }, help: { type: 'boolean', short: 'h' }, 'no-isolate': { type: 'boolean' }, 'no-start': { type: 'boolean' },
|
|
310
|
+
set: { type: 'string', multiple: true }, 'base-url': { type: 'string' }, key: { type: 'string' }, file: { type: 'string' }, group: { type: 'string' },
|
|
311
|
+
} });
|
|
312
|
+
const [command, ...rest] = positionals;
|
|
313
|
+
if (!command || values.help || command === 'help') return print(HELP);
|
|
314
|
+
const data = data_home(values.data);
|
|
315
|
+
// These work without the panel; update starts it first when it is not running.
|
|
316
|
+
const lifecycle = {
|
|
317
|
+
start: () => start_all(data),
|
|
318
|
+
stop: () => stop_all(data),
|
|
319
|
+
restart: async () => ({ stop: await stop_all(data), start: await start_all(data) }),
|
|
320
|
+
};
|
|
321
|
+
if (command === 'update' && rest[0] !== 'check' && !alive(panel_state(data)?.pid || 0)) await start_all(data);
|
|
322
|
+
if (lifecycle[command]) {
|
|
323
|
+
const result = await lifecycle[command]();
|
|
324
|
+
print(`${JSON.stringify(result, null, 2)}\n`);
|
|
325
|
+
return result;
|
|
326
|
+
}
|
|
327
|
+
const call = await client(data);
|
|
328
|
+
const commands = {
|
|
329
|
+
status: async () => { const { login_methods: _methods, providers: _providers, ...state } = await call('state'); return state; },
|
|
330
|
+
service: () => call(`service/${need(rest[0], 'start|stop|restart')}`, {}),
|
|
331
|
+
autostart: () => call('autostart', { on: need(rest[0], 'on|off') === 'on' }),
|
|
332
|
+
update: () => (rest[0] === 'check' ? call('update/check') : update(call, data)),
|
|
333
|
+
entry: () => entry_command(call, rest),
|
|
334
|
+
runtime: () => (rest[0] === 'sync' ? call('clis/install', {}) : Promise.reject(new Error(HELP))),
|
|
335
|
+
account: () => account_command(call, rest, values),
|
|
336
|
+
login: () => login_command(call, rest, values),
|
|
337
|
+
logout: () => call(`logins/${is_owner(need(rest[0], '目标')) ? '_' : rest[0]}/${need(rest[1], '程序')}/logout`, {}),
|
|
338
|
+
key: () => key_command(call, rest, values),
|
|
339
|
+
skill: () => skill_command(call, rest),
|
|
340
|
+
workflow: () => workflow_command(call, rest),
|
|
341
|
+
prompt: () => prompt_command(call, rest, values),
|
|
342
|
+
};
|
|
343
|
+
if (!commands[command]) throw new Error(HELP);
|
|
344
|
+
const result = await commands[command]();
|
|
345
|
+
print(`${JSON.stringify(result, null, 2)}\n`);
|
|
346
|
+
return result;
|
|
347
|
+
}
|