ai_co_discuss 0.4.27 → 0.4.28
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 +1 -1
- package/gateway/gateway.mjs +324 -0
- package/package.json +2 -1
- package/runner/admin_panel.mjs +30 -10
- package/runner/discussion_local.mjs +4 -4
- package/runner/discussion_setup.mjs +589 -563
- package/runner/launcher.mjs +17 -12
- package/runner/tray.mjs +3 -2
- package/web/modern.js +1 -1
package/docs/runner.md
CHANGED
|
@@ -41,7 +41,7 @@ codiscuss setup
|
|
|
41
41
|
|
|
42
42
|
不选项目目录时,房间使用**草稿空间**:程序在草稿空间里为这个房间单独建一个文件夹(房间名加短编号)并作为它的项目目录,成员生成的文件存在这里,“文件”页可以浏览,成员也可以设为可写。草稿空间默认在服务目录旁(`~/.codiscuss-workspace`)。房间画的图除了作为附件随消息发出,成员对项目目录可写时、或房间在草稿空间时,还会在该目录的 `images/` 下留一份。
|
|
43
43
|
|
|
44
|
-
Windows 上在终端运行 `codiscuss setup` 时,“安装配置”一步有“桌面快捷方式”:还没有时默认勾选“创建‘共议’快捷方式”,已有时显示为“重新生成”、默认不勾(非交互运行不创建);也可随时运行 `codiscuss launcher`
|
|
44
|
+
Windows 上在终端运行 `codiscuss setup` 时,“安装配置”一步有“桌面快捷方式”:还没有时默认勾选“创建‘共议’快捷方式”,已有时显示为“重新生成”、默认不勾(非交互运行不创建);也可随时运行 `codiscuss launcher` 生成或重新生成。它会生成“共议”程序,桌面和开始菜单各有一个快捷方式,双击即可启动服务并打开网站。服务运行时任务栏右下角有共议图标:左键打开共议,右键可打开共议、重启或关闭服务。在 setup 的“安装配置”里勾选“托管管理(管理面板)”(或加 `--hosting`,`--no-hosting` 关闭;选择会记住)后,左键改为打开**管理面板**,菜单多一项“打开管理面板”(也可运行 `codiscuss panel`)。一般自用不需要打开。第一次需要在“设置 → 个性化 → 任务栏 → 其他系统托盘图标”里打开“共议”,图标才会常驻任务栏。面板只在这台电脑上打开,用来管理托管的用户、为每个用户建立独立的 Windows 用户、登录 Claude Code / Codex / Antigravity 命令行,以及给用户配置 API 密钥。
|
|
45
45
|
|
|
46
46
|
### 浏览项目文件
|
|
47
47
|
|
|
@@ -0,0 +1,324 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* Account entry for hosted instances.
|
|
4
|
+
*
|
|
5
|
+
* Each account owns one instance: a loopback-only local website with its own service directory and port. The entry signs
|
|
6
|
+
* a person in and forwards every request to that account's instance as a same-origin loopback request, which the instance
|
|
7
|
+
* treats as its own local page. The instance therefore trusts whatever reaches it from this computer; the entry is the only
|
|
8
|
+
* thing between the network and it.
|
|
9
|
+
*/
|
|
10
|
+
import { spawn } from 'node:child_process';
|
|
11
|
+
import { createHash, randomBytes, scryptSync, timingSafeEqual } from 'node:crypto';
|
|
12
|
+
import { existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from 'node:fs';
|
|
13
|
+
import http from 'node:http';
|
|
14
|
+
import { homedir } from 'node:os';
|
|
15
|
+
import { dirname, join, resolve } from 'node:path';
|
|
16
|
+
import { fileURLToPath, pathToFileURL } from 'node:url';
|
|
17
|
+
import { parseArgs } from 'node:util';
|
|
18
|
+
|
|
19
|
+
const SESSION_COOKIE = 'codiscuss_entry';
|
|
20
|
+
const SESSION_SECONDS = 30 * 24 * 3600;
|
|
21
|
+
const LOGIN_PATH = '/_entry/login';
|
|
22
|
+
const LOGOUT_PATH = '/_entry/logout';
|
|
23
|
+
const LOGIN_BODY_LIMIT = 4096;
|
|
24
|
+
const FIRST_INSTANCE_PORT = 18801;
|
|
25
|
+
const DEFAULT_ENTRY_PORT = 8780;
|
|
26
|
+
const MAX_FAILURES = 5;
|
|
27
|
+
const LOCK_MS = 10 * 60 * 1000;
|
|
28
|
+
const NAME_PATTERN = /^[a-z0-9][a-z0-9_-]{1,31}$/;
|
|
29
|
+
const ACCOUNTS_FILE = 'gateway-accounts.json';
|
|
30
|
+
// Not account names: the administrator is never an entry account, and its draft space is the folder ~/.codiscuss-workspace.
|
|
31
|
+
const RESERVED_NAMES = new Set(['admin', 'workspace']);
|
|
32
|
+
// What a new instance takes from the administrator's saved software settings: where each program is, nothing else.
|
|
33
|
+
const SOFTWARE_FIELDS = ['adapter', 'executable', 'ipc_directory'];
|
|
34
|
+
const LOCAL_SCRIPT = join(dirname(dirname(fileURLToPath(import.meta.url))), 'runner', 'discussion_local.mjs');
|
|
35
|
+
const HELP = `用法:
|
|
36
|
+
node gateway/gateway.mjs serve [--host 127.0.0.1] [--port ${DEFAULT_ENTRY_PORT}] [--data 管理员目录]
|
|
37
|
+
node gateway/gateway.mjs add 账号 [--port 实例端口] [--data 管理员目录] 新建账号与实例目录,输出一次性显示的密码
|
|
38
|
+
node gateway/gateway.mjs passwd 账号 [--data 管理员目录] 重设密码
|
|
39
|
+
node gateway/gateway.mjs remove 账号 [--data 管理员目录] 删除账号(保留实例目录)
|
|
40
|
+
node gateway/gateway.mjs list [--data 管理员目录]
|
|
41
|
+
node gateway/gateway.mjs start|stop 账号 [--data 管理员目录] 启动或停止这个账号的实例
|
|
42
|
+
`;
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* The administrator's service directory, which also holds the account list.
|
|
46
|
+
*
|
|
47
|
+
* The administrator is the computer's owner and keeps the default instance here. That instance is never reachable through
|
|
48
|
+
* the entry: it is used at the computer itself or from a paired device.
|
|
49
|
+
*/
|
|
50
|
+
export function data_home(option) {
|
|
51
|
+
return resolve(option || process.env.CODISCUSS_GATEWAY_HOME || join(homedir(), '.codiscuss'));
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** One account's folder: a sibling of the administrator's service directory, named after the account. */
|
|
55
|
+
export function account_home(data, name) {
|
|
56
|
+
return `${data}-${name}`;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** The service directory of one account's instance; its projects and draft space sit beside it, never inside. */
|
|
60
|
+
export function instance_home(data, name) {
|
|
61
|
+
return join(account_home(data, name), 'service');
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function read_json(path) {
|
|
65
|
+
return existsSync(path) ? JSON.parse(readFileSync(path, 'utf8')) : {};
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** Give a new instance its port, its folders, and the administrator's Python and program locations. */
|
|
69
|
+
function prepare_instance(data, name, port) {
|
|
70
|
+
const home = account_home(data, name), service = instance_home(data, name);
|
|
71
|
+
const folders = { workspace: join(home, 'workspace'), projects_root: join(home, 'projects') };
|
|
72
|
+
for (const folder of Object.values(folders)) mkdirSync(folder, { recursive: true });
|
|
73
|
+
const python = read_json(join(data, 'local-settings.json')).python;
|
|
74
|
+
write_json(join(service, 'local-settings.json'), { port, ...(python ? { python } : {}), ...folders });
|
|
75
|
+
const participants = Object.entries(read_json(join(data, 'launch-settings.json')).participants || {}).map(([member, profile]) =>
|
|
76
|
+
[member, Object.fromEntries(SOFTWARE_FIELDS.filter(field => profile?.[field]).map(field => [field, profile[field]]))]);
|
|
77
|
+
if (participants.length) write_json(join(service, 'launch-settings.json'), { participants: Object.fromEntries(participants) });
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function accounts_path(data) {
|
|
81
|
+
return join(data, ACCOUNTS_FILE);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export function read_accounts(data) {
|
|
85
|
+
const path = accounts_path(data);
|
|
86
|
+
return existsSync(path) ? JSON.parse(readFileSync(path, 'utf8')) : {};
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function write_json(path, value) {
|
|
90
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
91
|
+
const draft = `${path}.${process.pid}.tmp`;
|
|
92
|
+
writeFileSync(draft, `${JSON.stringify(value, null, 2)}\n`, { mode: 0o600 });
|
|
93
|
+
renameSync(draft, path);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function password_hash(password, salt) {
|
|
97
|
+
return scryptSync(password, Buffer.from(salt, 'hex'), 32);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function new_credentials() {
|
|
101
|
+
const password = randomBytes(9).toString('base64url');
|
|
102
|
+
const salt = randomBytes(16).toString('hex');
|
|
103
|
+
return { password, salt, hash: password_hash(password, salt).toString('hex') };
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Create an account with its folder: the instance's service directory, a projects folder and a draft space.
|
|
108
|
+
*
|
|
109
|
+
* Returns:
|
|
110
|
+
* { name, port, password }; the password is shown once and only its hash is stored.
|
|
111
|
+
*/
|
|
112
|
+
export function add_account(data, name, port) {
|
|
113
|
+
if (!NAME_PATTERN.test(name || '')) throw new Error('账号须为 2–32 位小写字母、数字、下划线或短横线,且以字母或数字开头');
|
|
114
|
+
if (RESERVED_NAMES.has(name)) throw new Error(`“${name}”不能作为入口账号:管理员使用这台电脑的默认实例,只能在本机或已配对的设备上访问`);
|
|
115
|
+
const accounts = read_accounts(data);
|
|
116
|
+
if (accounts[name]) throw new Error(`账号已存在:${name}`);
|
|
117
|
+
if (existsSync(account_home(data, name))) throw new Error(`目录已存在,请换一个账号名:${account_home(data, name)}`);
|
|
118
|
+
const used = Object.values(accounts).map(account => account.port);
|
|
119
|
+
const chosen = port === undefined ? Math.max(FIRST_INSTANCE_PORT - 1, ...used) + 1 : Number(port);
|
|
120
|
+
if (!Number.isInteger(chosen) || chosen < 1024 || chosen > 65535) throw new Error('实例端口须为 1024–65535 的整数');
|
|
121
|
+
if (used.includes(chosen)) throw new Error(`端口 ${chosen} 已分配给其他账号`);
|
|
122
|
+
const { password, salt, hash } = new_credentials();
|
|
123
|
+
prepare_instance(data, name, chosen);
|
|
124
|
+
write_json(accounts_path(data), { ...accounts, [name]: { salt, hash, port: chosen } });
|
|
125
|
+
return { name, port: chosen, password };
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** Replace an account's password and return the new one. */
|
|
129
|
+
export function reset_password(data, name) {
|
|
130
|
+
const accounts = read_accounts(data);
|
|
131
|
+
if (!accounts[name]) throw new Error(`账号不存在:${name}`);
|
|
132
|
+
const { password, salt, hash } = new_credentials();
|
|
133
|
+
write_json(accounts_path(data), { ...accounts, [name]: { ...accounts[name], salt, hash } });
|
|
134
|
+
return password;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** Delete the account; its instance directory stays on disk. */
|
|
138
|
+
export function remove_account(data, name) {
|
|
139
|
+
const { [name]: removed, ...rest } = read_accounts(data);
|
|
140
|
+
if (!removed) throw new Error(`账号不存在:${name}`);
|
|
141
|
+
write_json(accounts_path(data), rest);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** Whether the password is this account's; an unknown account costs the same time as a wrong password. */
|
|
145
|
+
function password_matches(account, password) {
|
|
146
|
+
const salt = account?.salt || '00'.repeat(16);
|
|
147
|
+
const expected = Buffer.from(account?.hash || '00'.repeat(32), 'hex');
|
|
148
|
+
const actual = password_hash(String(password || ''), salt);
|
|
149
|
+
return timingSafeEqual(actual, expected) && Boolean(account);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function token_key(token) {
|
|
153
|
+
return createHash('sha256').update(token).digest('hex');
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function request_cookies(request) {
|
|
157
|
+
return String(request.headers.cookie || '').split(';').map(part => part.trim()).filter(Boolean);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function session_token(request) {
|
|
161
|
+
const cookie = request_cookies(request).find(part => part.startsWith(`${SESSION_COOKIE}=`));
|
|
162
|
+
return cookie ? cookie.slice(SESSION_COOKIE.length + 1) : '';
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function cookie_header(request, token, seconds) {
|
|
166
|
+
const secure = request.headers['x-forwarded-proto'] === 'https' ? '; Secure' : '';
|
|
167
|
+
return `${SESSION_COOKIE}=${token}; Path=/; HttpOnly; SameSite=Lax; Max-Age=${seconds}${secure}`;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function send(response, status, headers, body = '') {
|
|
171
|
+
response.writeHead(status, { 'Cache-Control': 'no-store', ...headers });
|
|
172
|
+
response.end(body);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
function send_json(response, status, value) {
|
|
176
|
+
send(response, status, { 'Content-Type': 'application/json; charset=utf-8' }, JSON.stringify(value));
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function login_page(message = '') {
|
|
180
|
+
const notice = message ? `<p class="error">${message}</p>` : '';
|
|
181
|
+
return `<!doctype html><html lang="zh-CN"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
|
|
182
|
+
<title>登录 · AI CoDiscuss</title><style>
|
|
183
|
+
body{margin:0;min-height:100vh;display:grid;place-items:center;font:16px system-ui,sans-serif;background:#f5f5f4;color:#1c1917}
|
|
184
|
+
form{width:min(320px,calc(100vw - 48px));display:grid;gap:12px}h1{font-size:20px;margin:0 0 8px}
|
|
185
|
+
input,button{font:inherit;padding:10px 12px;border-radius:8px;border:1px solid #d6d3d1}button{background:#1c1917;color:#fff;border:0}
|
|
186
|
+
.error{color:#b91c1c;margin:0;font-size:14px}
|
|
187
|
+
@media (prefers-color-scheme:dark){body{background:#1c1917;color:#f5f5f4}input{background:#292524;color:inherit;border-color:#44403c}button{background:#f5f5f4;color:#1c1917}}
|
|
188
|
+
</style></head><body><form method="post" action="${LOGIN_PATH}"><h1>AI CoDiscuss</h1>${notice}
|
|
189
|
+
<input name="name" placeholder="账号" autocomplete="username" autocapitalize="none" required>
|
|
190
|
+
<input name="password" type="password" placeholder="密码" autocomplete="current-password" required>
|
|
191
|
+
<button>登录</button></form></body></html>`;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
async function read_form(request) {
|
|
195
|
+
let body = '';
|
|
196
|
+
for await (const chunk of request) {
|
|
197
|
+
body += chunk;
|
|
198
|
+
if (body.length > LOGIN_BODY_LIMIT) throw new Error('请求过大');
|
|
199
|
+
}
|
|
200
|
+
return new URLSearchParams(body);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/** A browser request from another site's page; requests without an Origin are not from a page script. */
|
|
204
|
+
function cross_origin(request) {
|
|
205
|
+
const origin = request.headers.origin;
|
|
206
|
+
if (!origin) return false;
|
|
207
|
+
try { return new URL(origin).host !== request.headers.host; } catch { return true; }
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/** Present the request to the instance as its own loopback page, without the entry's session cookie. */
|
|
211
|
+
function forward(request, response, port) {
|
|
212
|
+
const target = `127.0.0.1:${port}`;
|
|
213
|
+
const headers = { ...request.headers, host: target };
|
|
214
|
+
if (headers.origin) headers.origin = `http://${target}`;
|
|
215
|
+
delete headers.referer;
|
|
216
|
+
const cookies = request_cookies(request).filter(part => !part.startsWith(`${SESSION_COOKIE}=`));
|
|
217
|
+
if (cookies.length) headers.cookie = cookies.join('; ');
|
|
218
|
+
else delete headers.cookie;
|
|
219
|
+
const upstream = http.request({ host: '127.0.0.1', port, path: request.url, method: request.method, headers }, reply => {
|
|
220
|
+
response.writeHead(reply.statusCode, reply.headers);
|
|
221
|
+
reply.pipe(response);
|
|
222
|
+
});
|
|
223
|
+
upstream.on('error', () => {
|
|
224
|
+
if (response.headersSent) response.destroy();
|
|
225
|
+
else send_json(response, 502, { error: '这个账号的实例没有在运行' });
|
|
226
|
+
});
|
|
227
|
+
response.on('close', () => upstream.destroy());
|
|
228
|
+
request.pipe(upstream);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Build the entry server.
|
|
233
|
+
*
|
|
234
|
+
* Args:
|
|
235
|
+
* data: The administrator's service directory, which holds the account list; instances are its siblings.
|
|
236
|
+
* now: Clock in milliseconds, replaceable in tests.
|
|
237
|
+
*
|
|
238
|
+
* Returns:
|
|
239
|
+
* An http.Server that is not yet listening. Sessions live in memory, so restarting it signs everyone out.
|
|
240
|
+
*/
|
|
241
|
+
export function create_gateway({ data, now = Date.now }) {
|
|
242
|
+
const sessions = new Map();
|
|
243
|
+
const failures = new Map();
|
|
244
|
+
|
|
245
|
+
function session_of(request) {
|
|
246
|
+
const key = token_key(session_token(request));
|
|
247
|
+
const session = sessions.get(key);
|
|
248
|
+
if (session && session.expires > now()) return session;
|
|
249
|
+
sessions.delete(key);
|
|
250
|
+
return null;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
async function login(request, response) {
|
|
254
|
+
const form = await read_form(request);
|
|
255
|
+
const name = String(form.get('name') || '').trim().toLowerCase();
|
|
256
|
+
const failed = failures.get(name);
|
|
257
|
+
if (failed && failed.until > now()) return send(response, 429, { 'Content-Type': 'text/html; charset=utf-8' }, login_page('尝试次数过多,请 10 分钟后再试'));
|
|
258
|
+
if (!password_matches(NAME_PATTERN.test(name) ? read_accounts(data)[name] : null, form.get('password'))) {
|
|
259
|
+
const count = (failed?.count || 0) + 1;
|
|
260
|
+
if (NAME_PATTERN.test(name)) failures.set(name, { count, until: count >= MAX_FAILURES ? now() + LOCK_MS : 0 });
|
|
261
|
+
return send(response, 401, { 'Content-Type': 'text/html; charset=utf-8' }, login_page('账号或密码不正确'));
|
|
262
|
+
}
|
|
263
|
+
failures.delete(name);
|
|
264
|
+
const token = randomBytes(32).toString('hex');
|
|
265
|
+
sessions.set(token_key(token), { name, expires: now() + SESSION_SECONDS * 1000 });
|
|
266
|
+
send(response, 303, { Location: '/', 'Set-Cookie': cookie_header(request, token, SESSION_SECONDS) });
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
async function handle(request, response) {
|
|
270
|
+
const path = new URL(request.url, 'http://entry').pathname;
|
|
271
|
+
if (cross_origin(request)) return send_json(response, 403, { error: '不接受来自其他网站的请求' });
|
|
272
|
+
if (path === LOGIN_PATH && request.method === 'GET') return send(response, 200, { 'Content-Type': 'text/html; charset=utf-8' }, login_page());
|
|
273
|
+
if (path === LOGIN_PATH && request.method === 'POST') return login(request, response);
|
|
274
|
+
if (path === LOGOUT_PATH && request.method === 'POST') {
|
|
275
|
+
sessions.delete(token_key(session_token(request)));
|
|
276
|
+
return send(response, 303, { Location: LOGIN_PATH, 'Set-Cookie': cookie_header(request, '', 0) });
|
|
277
|
+
}
|
|
278
|
+
const account = read_accounts(data)[session_of(request)?.name];
|
|
279
|
+
if (account) return forward(request, response, account.port);
|
|
280
|
+
const page = request.method === 'GET' && String(request.headers.accept || '').includes('text/html');
|
|
281
|
+
return page ? send(response, 302, { Location: LOGIN_PATH }) : send_json(response, 401, { error: '请先登录' });
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
return http.createServer((request, response) => {
|
|
285
|
+
handle(request, response).catch(error => {
|
|
286
|
+
if (response.headersSent) response.destroy();
|
|
287
|
+
else send_json(response, 400, { error: error.message });
|
|
288
|
+
});
|
|
289
|
+
});
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/** Run `codiscuss local start|stop` against one account's service directory and wait for it. */
|
|
293
|
+
function run_instance(data, name, command) {
|
|
294
|
+
if (!read_accounts(data)[name]) throw new Error(`账号不存在:${name}`);
|
|
295
|
+
const child = spawn(process.execPath, [LOCAL_SCRIPT, command, '--json'], { stdio: 'inherit', windowsHide: true,
|
|
296
|
+
env: { ...process.env, DISCUSS_SERVICE_HOME: instance_home(data, name) } });
|
|
297
|
+
return new Promise((done, fail) => {
|
|
298
|
+
child.on('error', fail);
|
|
299
|
+
child.on('exit', code => code === 0 ? done() : fail(new Error(`实例${command === 'start' ? '启动' : '停止'}失败(退出码 ${code})`)));
|
|
300
|
+
});
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
async function main(args) {
|
|
304
|
+
const { positionals, values } = parseArgs({ args, allowPositionals: true, options: {
|
|
305
|
+
host: { type: 'string' }, port: { type: 'string' }, data: { type: 'string' }, help: { type: 'boolean', short: 'h' } } });
|
|
306
|
+
const [command, name] = positionals;
|
|
307
|
+
const data = data_home(values.data);
|
|
308
|
+
const print = value => process.stdout.write(`${typeof value === 'string' ? value : JSON.stringify(value, null, 2)}\n`);
|
|
309
|
+
if (values.help || !command) return print(HELP);
|
|
310
|
+
if (command === 'serve') {
|
|
311
|
+
const host = values.host || '127.0.0.1', port = Number(values.port || DEFAULT_ENTRY_PORT);
|
|
312
|
+
return create_gateway({ data }).listen(port, host, () => print(`入口已启动:http://${host}:${port}(管理员目录 ${data})`));
|
|
313
|
+
}
|
|
314
|
+
if (command === 'list') return print(Object.entries(read_accounts(data)).map(([account, { port }]) => ({ account, port, home: account_home(data, account) })));
|
|
315
|
+
if (command === 'add') return print(add_account(data, name, values.port));
|
|
316
|
+
if (command === 'passwd') return print({ name, password: reset_password(data, name) });
|
|
317
|
+
if (command === 'remove') return print((remove_account(data, name), { removed: name, kept: account_home(data, name) }));
|
|
318
|
+
if (command === 'start' || command === 'stop') return run_instance(data, name, command);
|
|
319
|
+
throw new Error(HELP);
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
if (process.argv[1] && import.meta.url === pathToFileURL(resolve(process.argv[1])).href) {
|
|
323
|
+
main(process.argv.slice(2)).catch(error => { process.stderr.write(`${error.message}\n`); process.exitCode = 1; });
|
|
324
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ai_co_discuss",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.28",
|
|
4
4
|
"bin": {
|
|
5
5
|
"codiscuss": "runner/codiscuss.mjs"
|
|
6
6
|
},
|
|
@@ -82,6 +82,7 @@
|
|
|
82
82
|
},
|
|
83
83
|
"files": [
|
|
84
84
|
"runner/*.mjs",
|
|
85
|
+
"gateway/*.mjs",
|
|
85
86
|
"runner/adapters/*.mjs",
|
|
86
87
|
"runner/*.py",
|
|
87
88
|
"backend/*.py",
|
package/runner/admin_panel.mjs
CHANGED
|
@@ -296,26 +296,39 @@ function run_owner_service(data, command) {
|
|
|
296
296
|
});
|
|
297
297
|
}
|
|
298
298
|
|
|
299
|
+
/** Whether the owner turned on hosting (setup's "托管管理"); only then does this process serve the panel. */
|
|
300
|
+
export function hosting_enabled(data) {
|
|
301
|
+
try { return JSON.parse(readFileSync(join(data, 'local-settings.json'), 'utf8')).hosting === true; } catch { return false; }
|
|
302
|
+
}
|
|
303
|
+
|
|
299
304
|
/** Run the server and the icon until the icon's "close service" entry or `panel stop`. */
|
|
300
305
|
function run_panel(data, port) {
|
|
306
|
+
const hosting = hosting_enabled(data);
|
|
301
307
|
const running = read_state(data);
|
|
302
308
|
if (running && running.pid !== process.pid && alive(running.pid)) throw new Error(`管理面板已在运行:http://127.0.0.1:${running.port}`);
|
|
303
|
-
const state = { pid: process.pid, port, key: randomBytes(24).toString('hex') };
|
|
304
|
-
const server = create_panel({ data, key: state.key, port });
|
|
305
|
-
|
|
309
|
+
const state = { pid: process.pid, port: hosting ? port : 0, key: hosting ? randomBytes(24).toString('hex') : '' };
|
|
310
|
+
const server = hosting ? create_panel({ data, key: state.key, port }) : null;
|
|
311
|
+
if (server) {
|
|
312
|
+
server.listen(port, '127.0.0.1', () => {
|
|
313
|
+
writeFileSync(state_path(data), JSON.stringify(state), { mode: 0o600 });
|
|
314
|
+
process.stdout.write(`管理面板已启动:http://127.0.0.1:${port}\n`);
|
|
315
|
+
});
|
|
316
|
+
} else {
|
|
306
317
|
writeFileSync(state_path(data), JSON.stringify(state), { mode: 0o600 });
|
|
307
|
-
process.stdout.write(
|
|
308
|
-
}
|
|
318
|
+
process.stdout.write('任务栏图标已启动(没有管理面板)\n');
|
|
319
|
+
}
|
|
309
320
|
const rendered = join(data, 'launcher', 'codiscuss.ico');
|
|
310
|
-
const tray = show_tray({ title: '共议 · 管理面板', icon_path: existsSync(rendered) ? rendered : '', window_title: WINDOW_TITLE
|
|
311
|
-
|
|
321
|
+
const tray = show_tray({ title: hosting ? '共议 · 管理面板' : '共议', icon_path: existsSync(rendered) ? rendered : '', window_title: hosting ? WINDOW_TITLE : '',
|
|
322
|
+
host: join(data, 'launcher', '共议.exe'), on_action: action => {
|
|
323
|
+
// A launcher built before the icon could run without a panel still offers "open panel"; it opens the site then.
|
|
324
|
+
if (action === 'open') { if (hosting) open_panel_window(data, panel_url(state), false); else open_browser(site_url(data)); }
|
|
312
325
|
if (action === 'site') open_browser(site_url(data));
|
|
313
326
|
if (action === 'restart') run_owner_service(data, 'stop').then(() => run_owner_service(data, 'start')).catch(() => {});
|
|
314
327
|
if (action === 'stop') run_owner_service(data, 'stop').catch(() => {}).finally(shutdown);
|
|
315
328
|
} });
|
|
316
329
|
function shutdown() {
|
|
317
330
|
tray?.close();
|
|
318
|
-
server
|
|
331
|
+
server?.close();
|
|
319
332
|
if (read_state(data)?.pid === process.pid) rmSync(state_path(data), { force: true });
|
|
320
333
|
setTimeout(() => process.exit(0), 300);
|
|
321
334
|
}
|
|
@@ -346,8 +359,15 @@ export async function panel_main(args) {
|
|
|
346
359
|
if (values.help) return process.stdout.write(HELP);
|
|
347
360
|
const data = data_home(values.data), port = Number(values.port || DEFAULT_PORT);
|
|
348
361
|
if (command === 'run') return run_panel(data, port);
|
|
349
|
-
if (command === 'start') { const state = await start_panel(data, port); return process.stdout.write(`管理面板:http://127.0.0.1:${state.port}\n`); }
|
|
350
|
-
if (command === 'open') {
|
|
362
|
+
if (command === 'start') { const state = await start_panel(data, port); return process.stdout.write(state.port ? `管理面板:http://127.0.0.1:${state.port}\n` : '任务栏图标已启动(没有管理面板)\n'); }
|
|
363
|
+
if (command === 'open') {
|
|
364
|
+
if (!hosting_enabled(data)) throw new Error('托管管理未打开:运行 codiscuss setup 勾选“托管管理(管理面板)”,或 codiscuss setup --hosting');
|
|
365
|
+
const state = await start_panel(data, port);
|
|
366
|
+
// An icon started before hosting was turned on has no panel; setup restarts it, this covers a hand-edited setting.
|
|
367
|
+
if (!state.port) throw new Error('任务栏图标是在打开托管管理之前启动的:运行 codiscuss panel stop 后再运行 codiscuss panel open');
|
|
368
|
+
open_panel_window(data, panel_url(state));
|
|
369
|
+
return;
|
|
370
|
+
}
|
|
351
371
|
if (command === 'stop') {
|
|
352
372
|
const state = read_state(data);
|
|
353
373
|
if (state && alive(state.pid)) process.kill(state.pid);
|
|
@@ -181,15 +181,15 @@ async function requested_service(home, status, owned_service) {
|
|
|
181
181
|
return owned_service;
|
|
182
182
|
}
|
|
183
183
|
|
|
184
|
-
// The computer owner's management panel
|
|
184
|
+
// The computer owner's taskbar icon, with the management panel where gateway/ is present; hosted accounts (with a
|
|
185
|
+
// projects root) have neither.
|
|
185
186
|
const PANEL_SCRIPT = join(dirname(fileURLToPath(import.meta.url)), 'admin_panel.mjs');
|
|
186
|
-
const ACCOUNT_MODULE = join(dirname(dirname(fileURLToPath(import.meta.url))), 'gateway', 'gateway.mjs');
|
|
187
187
|
|
|
188
|
-
/** Start the
|
|
188
|
+
/** Start the taskbar icon (and panel) in the background on the owner's Windows instance; it keeps running on its own. */
|
|
189
189
|
function start_panel_icon(home, settings) {
|
|
190
190
|
// Only the owner's default service directory: a test or a second copy elsewhere must not put up another icon.
|
|
191
191
|
if (process.platform !== 'win32' || settings.projects_root || resolve(home) !== resolve(homedir(), '.codiscuss')) return;
|
|
192
|
-
if (!existsSync(PANEL_SCRIPT)
|
|
192
|
+
if (!existsSync(PANEL_SCRIPT)) return;
|
|
193
193
|
const child = spawn(process.execPath, [PANEL_SCRIPT, 'start', '--data', home], { detached: true, stdio: 'ignore', windowsHide: true });
|
|
194
194
|
child.on('error', () => {});
|
|
195
195
|
child.unref();
|