@dmall-inc/wd-cli 0.2.0 → 0.4.0
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/README.md +88 -0
- package/docs/design-references.md +2 -0
- package/lib/args.js +2 -1
- package/lib/auth.js +1 -1
- package/lib/commands/daemon.js +411 -0
- package/lib/commands/device.js +92 -0
- package/lib/commands/diwork.js +544 -0
- package/lib/commands/index.js +5 -0
- package/lib/commands/open.js +89 -0
- package/lib/commands/tool.js +327 -0
- package/lib/daemon/agui.js +63 -0
- package/lib/daemon/channel.js +183 -0
- package/lib/daemon/enroll.js +78 -0
- package/lib/daemon/executors/acp.js +480 -0
- package/lib/daemon/executors/pi-approval-extension.ts +57 -0
- package/lib/daemon/executors/pi.js +360 -0
- package/lib/daemon/index.js +146 -0
- package/lib/daemon/jsonrpc.js +127 -0
- package/lib/daemon/permission.js +96 -0
- package/lib/daemon/roots.js +161 -0
- package/lib/daemon/runtimes.js +238 -0
- package/lib/daemon/service.js +135 -0
- package/lib/daemon/state.js +188 -0
- package/lib/daemon/supervisor.js +244 -0
- package/package.json +1 -1
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* daemon 本机状态(零依赖):
|
|
5
|
+
* ~/.config/wd-cli/daemon.json daemon 配置:allowedRoots / projects(wd-cli open 打开过的目录)/ runtimes 命令覆盖 / autoApprove / bridgeUrl
|
|
6
|
+
* ~/.config/wd-cli/device.json 本机设备标识(首次启动生成,跨重登记保持不变)
|
|
7
|
+
* ~/.config/wd-cli/daemon.pid 后台进程 PID
|
|
8
|
+
* ~/.config/wd-cli/daemon.log 后台日志
|
|
9
|
+
* ~/.config/wd-cli/daemon.status 运行态快照(连接状态 / 运行时清单),供 daemon status 读取
|
|
10
|
+
* ~/.config/wd-cli/executors/ 执行器包安装目录(npm --prefix)
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
const crypto = require('node:crypto');
|
|
14
|
+
const fs = require('node:fs');
|
|
15
|
+
const os = require('node:os');
|
|
16
|
+
const path = require('node:path');
|
|
17
|
+
|
|
18
|
+
const config = require('../config');
|
|
19
|
+
const keyring = require('../keyring');
|
|
20
|
+
|
|
21
|
+
function paths(env = process.env) {
|
|
22
|
+
const dir = config.configDir(env);
|
|
23
|
+
return {
|
|
24
|
+
dir,
|
|
25
|
+
daemonConfig: path.join(dir, 'daemon.json'),
|
|
26
|
+
device: path.join(dir, 'device.json'),
|
|
27
|
+
pid: path.join(dir, 'daemon.pid'),
|
|
28
|
+
log: path.join(dir, 'daemon.log'),
|
|
29
|
+
status: path.join(dir, 'daemon.status'),
|
|
30
|
+
executors: path.join(dir, 'executors'),
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function readJson(file, fallback) {
|
|
35
|
+
try {
|
|
36
|
+
return JSON.parse(fs.readFileSync(file, 'utf8'));
|
|
37
|
+
} catch {
|
|
38
|
+
return fallback;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function writeJson(file, data, mode = 0o600) {
|
|
43
|
+
fs.mkdirSync(path.dirname(file), { recursive: true, mode: 0o700 });
|
|
44
|
+
fs.writeFileSync(file, `${JSON.stringify(data, null, 2)}\n`, { mode });
|
|
45
|
+
try {
|
|
46
|
+
fs.chmodSync(file, mode);
|
|
47
|
+
} catch {
|
|
48
|
+
// Windows:忽略权限位
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** daemon.json:缺省值在此补齐,调用方不必判空。 */
|
|
53
|
+
function loadDaemonConfig(env = process.env) {
|
|
54
|
+
const raw = readJson(paths(env).daemonConfig, {});
|
|
55
|
+
return {
|
|
56
|
+
allowedRoots: Array.isArray(raw.allowedRoots) ? raw.allowedRoots.filter((r) => typeof r === 'string') : [],
|
|
57
|
+
runtimes: raw.runtimes && typeof raw.runtimes === 'object' ? raw.runtimes : {},
|
|
58
|
+
autoApprove: Array.isArray(raw.autoApprove) ? raw.autoApprove.filter((r) => typeof r === 'string') : [],
|
|
59
|
+
bridgeUrl: typeof raw.bridgeUrl === 'string' ? raw.bridgeUrl : '',
|
|
60
|
+
deviceName: typeof raw.deviceName === 'string' ? raw.deviceName : '',
|
|
61
|
+
projects: Array.isArray(raw.projects)
|
|
62
|
+
? raw.projects.filter((p) => p && typeof p.path === 'string').map((p) => ({ path: p.path, openedAt: typeof p.openedAt === 'string' ? p.openedAt : null }))
|
|
63
|
+
: [],
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** 本机最多保留的项目打开记录(add-local-agent-projects)。 */
|
|
68
|
+
const MAX_PROJECTS = 100;
|
|
69
|
+
|
|
70
|
+
/** 记录一次打开(按路径去重,最新的排最前,保留最近 100 条),返回新列表。 */
|
|
71
|
+
function addProject(projects, dir, openedAt = new Date().toISOString()) {
|
|
72
|
+
const rest = (projects || []).filter((p) => p.path !== dir);
|
|
73
|
+
return [{ path: dir, openedAt }, ...rest].slice(0, MAX_PROJECTS);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function saveDaemonConfig(next, env = process.env) {
|
|
77
|
+
writeJson(paths(env).daemonConfig, next, 0o600);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** 本机设备标识:首次生成后固定,重登记沿用同一个,服务端据此轮换令牌而不是新建设备。 */
|
|
81
|
+
function deviceId(env = process.env) {
|
|
82
|
+
const file = paths(env).device;
|
|
83
|
+
const existing = readJson(file, null);
|
|
84
|
+
if (existing && typeof existing.deviceId === 'string' && /^[A-Za-z0-9_-]{8,64}$/.test(existing.deviceId)) {
|
|
85
|
+
return existing.deviceId;
|
|
86
|
+
}
|
|
87
|
+
const id = `dev_${crypto.randomBytes(12).toString('hex')}`;
|
|
88
|
+
writeJson(file, { deviceId: id, createdAt: new Date().toISOString() }, 0o600);
|
|
89
|
+
return id;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function hostInfo() {
|
|
93
|
+
return { hostname: os.hostname(), os: process.platform, arch: process.arch };
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// ---------------------------------------------------------------- 设备令牌
|
|
97
|
+
|
|
98
|
+
function deviceAccount(profileName) {
|
|
99
|
+
return `device:${profileName}`;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** 取设备令牌:钥匙串优先,回落 hosts.json 的 deviceToken 字段。 */
|
|
103
|
+
function loadDeviceToken(profileName, env = process.env) {
|
|
104
|
+
const hosts = config.loadHosts(env);
|
|
105
|
+
const entry = hosts[profileName] || {};
|
|
106
|
+
if (entry.deviceToken && typeof entry.deviceToken === 'object' && entry.deviceToken.__keyring) {
|
|
107
|
+
return keyring.get(deviceAccount(profileName));
|
|
108
|
+
}
|
|
109
|
+
return typeof entry.deviceToken === 'string' ? entry.deviceToken : null;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function saveDeviceToken(profileName, token, env = process.env) {
|
|
113
|
+
if (!token) {
|
|
114
|
+
if (keyring.available(env)) keyring.remove(deviceAccount(profileName));
|
|
115
|
+
config.updateHostEntry(profileName, { deviceToken: null }, env);
|
|
116
|
+
return 'removed';
|
|
117
|
+
}
|
|
118
|
+
if (keyring.available(env) && keyring.set(deviceAccount(profileName), token)) {
|
|
119
|
+
config.updateHostEntry(profileName, { deviceToken: { __keyring: true } }, env);
|
|
120
|
+
return keyring.describe(env);
|
|
121
|
+
}
|
|
122
|
+
config.updateHostEntry(profileName, { deviceToken: token }, env);
|
|
123
|
+
return '配置文件';
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// ---------------------------------------------------------------- 进程与状态快照
|
|
127
|
+
|
|
128
|
+
function readPid(env = process.env) {
|
|
129
|
+
try {
|
|
130
|
+
const pid = Number(fs.readFileSync(paths(env).pid, 'utf8').trim());
|
|
131
|
+
return Number.isInteger(pid) && pid > 0 ? pid : null;
|
|
132
|
+
} catch {
|
|
133
|
+
return null;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function isAlive(pid) {
|
|
138
|
+
if (!pid) return false;
|
|
139
|
+
try {
|
|
140
|
+
process.kill(pid, 0);
|
|
141
|
+
return true;
|
|
142
|
+
} catch (error) {
|
|
143
|
+
return error.code === 'EPERM';
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function writePid(pid, env = process.env) {
|
|
148
|
+
writeJson(paths(env).pid, pid, 0o600);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function clearPid(env = process.env) {
|
|
152
|
+
try {
|
|
153
|
+
fs.unlinkSync(paths(env).pid);
|
|
154
|
+
} catch {
|
|
155
|
+
// 已不存在
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function writeStatus(snapshot, env = process.env) {
|
|
160
|
+
try {
|
|
161
|
+
writeJson(paths(env).status, { ...snapshot, updatedAt: new Date().toISOString() }, 0o600);
|
|
162
|
+
} catch {
|
|
163
|
+
// 状态快照只是给 status 命令看的,写失败不影响运行
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function readStatus(env = process.env) {
|
|
168
|
+
return readJson(paths(env).status, null);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
module.exports = {
|
|
172
|
+
paths,
|
|
173
|
+
readJson,
|
|
174
|
+
writeJson,
|
|
175
|
+
loadDaemonConfig,
|
|
176
|
+
saveDaemonConfig,
|
|
177
|
+
addProject,
|
|
178
|
+
deviceId,
|
|
179
|
+
hostInfo,
|
|
180
|
+
loadDeviceToken,
|
|
181
|
+
saveDeviceToken,
|
|
182
|
+
readPid,
|
|
183
|
+
isAlive,
|
|
184
|
+
writePid,
|
|
185
|
+
clearPid,
|
|
186
|
+
writeStatus,
|
|
187
|
+
readStatus,
|
|
188
|
+
};
|
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 命令分派与会话表(design D4):把 bridge 下行命令落到执行器,把执行器事件包成上行帧。
|
|
5
|
+
* 与网络无关——构造时注入 send(frame),便于单测。
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
const { agui } = require('./agui');
|
|
9
|
+
const { PermissionBroker } = require('./permission');
|
|
10
|
+
const { resolveAllowedCwd, listDirs } = require('./roots');
|
|
11
|
+
const runtimesLib = require('./runtimes');
|
|
12
|
+
|
|
13
|
+
/** 这些错误说明运行时本身可能出了问题,触发一次复检 */
|
|
14
|
+
const RECHECK_CODES = new Set(['RUNTIME_UNAVAILABLE', 'RUNTIME_START_FAILED', 'RUNTIME_EXITED']);
|
|
15
|
+
|
|
16
|
+
class Supervisor {
|
|
17
|
+
/**
|
|
18
|
+
* @param opts.send (frame) => void
|
|
19
|
+
* @param opts.getRuntimes () => runtimes(detectAll 的结果)
|
|
20
|
+
* @param opts.getDaemonConfig () => daemon.json(每次取最新,roots 可在运行中修改)
|
|
21
|
+
* @param opts.host { hostname, os, arch }
|
|
22
|
+
* @param opts.cliVersion
|
|
23
|
+
* @param opts.log
|
|
24
|
+
* @param opts.refreshRuntimes 可选 () => Promise<void>:重新探测运行时(自动恢复:运行时不可用 / 启动失败时触发)
|
|
25
|
+
*/
|
|
26
|
+
constructor(opts) {
|
|
27
|
+
this.opts = opts;
|
|
28
|
+
this.log = opts.log || (() => {});
|
|
29
|
+
this.sessions = new Map();
|
|
30
|
+
/** 各运行时最近一次上报的可用斜杠命令:runtimeId → [{name, description, hint}] */
|
|
31
|
+
this.commands = {};
|
|
32
|
+
this.probed = false;
|
|
33
|
+
/** 已探测过命令的运行时;运行时掉线后移出,恢复可用时重新探测 */
|
|
34
|
+
this.probeAttempted = new Set();
|
|
35
|
+
this.permission = new PermissionBroker({
|
|
36
|
+
emit: (ctx, event) => this.emitEvent(ctx, event),
|
|
37
|
+
autoApprove: (opts.getDaemonConfig().autoApprove || []),
|
|
38
|
+
log: this.log,
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
emitEvent(ctx, event) {
|
|
43
|
+
this.opts.send({ type: 'agui', sessionId: ctx.sessionId, runId: ctx.runId, event });
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
inventory() {
|
|
47
|
+
const runtimes = this.opts.getRuntimes();
|
|
48
|
+
const daemonConfig = this.opts.getDaemonConfig();
|
|
49
|
+
return {
|
|
50
|
+
type: 'inventory',
|
|
51
|
+
runtimes: runtimesLib.toInventory(runtimes).map((r) => (this.commands[r.id] ? { ...r, commands: this.commands[r.id] } : r)),
|
|
52
|
+
roots: daemonConfig.allowedRoots,
|
|
53
|
+
// add-local-agent-projects:wd-cli open 打开过的目录,平台据此登记项目
|
|
54
|
+
projects: daemonConfig.projects || [],
|
|
55
|
+
cliVersion: this.opts.cliVersion,
|
|
56
|
+
executorVersions: runtimesLib.executorVersions(runtimes),
|
|
57
|
+
...(this.opts.host || {}),
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
handle(cmd) {
|
|
62
|
+
if (!cmd || typeof cmd !== 'object') return;
|
|
63
|
+
switch (cmd.type) {
|
|
64
|
+
case 'hello':
|
|
65
|
+
if (cmd.config && cmd.config.permissionTimeoutMs) this.permission.setTimeout(Number(cmd.config.permissionTimeoutMs));
|
|
66
|
+
this.opts.send(this.inventory());
|
|
67
|
+
this.probeCommands().catch((error) => this.log(`探测可用命令失败:${error.message}`));
|
|
68
|
+
return;
|
|
69
|
+
case 'prompt':
|
|
70
|
+
this.handlePrompt(cmd).catch((error) => this.log(`执行失败:${error.stack || error.message}`));
|
|
71
|
+
return;
|
|
72
|
+
case 'cancel': {
|
|
73
|
+
const entry = this.sessions.get(cmd.sessionId);
|
|
74
|
+
if (entry && entry.session) entry.session.cancel();
|
|
75
|
+
this.ack(cmd, Boolean(entry));
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
case 'permission_decision': {
|
|
79
|
+
const ok = this.permission.decide(cmd.requestId, { optionId: cmd.optionId, approved: cmd.approved });
|
|
80
|
+
this.ack(cmd, ok, ok ? undefined : '审批请求已结束或不存在');
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
case 'list_dirs': {
|
|
84
|
+
// add-local-agent-projects:页面浏览白名单内的子目录选项目;结果随 ack 回给 bridge
|
|
85
|
+
try {
|
|
86
|
+
const result = listDirs(cmd.path || '', this.opts.getDaemonConfig().allowedRoots, { showHidden: Boolean(cmd.showHidden) });
|
|
87
|
+
this.ack(cmd, true, undefined, result);
|
|
88
|
+
} catch (error) {
|
|
89
|
+
this.ack(cmd, false, error.message);
|
|
90
|
+
}
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
case 'close_session': {
|
|
94
|
+
const entry = this.sessions.get(cmd.sessionId);
|
|
95
|
+
if (entry && entry.session) entry.session.close();
|
|
96
|
+
this.sessions.delete(cmd.sessionId);
|
|
97
|
+
this.ack(cmd, true);
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
default:
|
|
101
|
+
this.ack(cmd, false, `未知命令:${cmd.type}`);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** 记录运行时的可用命令;有变化才重新上报清单。 */
|
|
106
|
+
setCommands(runtimeId, commands) {
|
|
107
|
+
if (!Array.isArray(commands)) return;
|
|
108
|
+
if (JSON.stringify(this.commands[runtimeId] || null) === JSON.stringify(commands)) return;
|
|
109
|
+
this.commands[runtimeId] = commands;
|
|
110
|
+
this.opts.send(this.inventory());
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* 在第一个白名单目录里探测各 ACP 运行时的命令(每个运行时只探测一次,串行,失败忽略)。
|
|
115
|
+
* 首次 hello 后触发;运行时从不可用恢复时由 runtimesChanged 再触发一次。
|
|
116
|
+
*/
|
|
117
|
+
async probeCommands() {
|
|
118
|
+
this.probed = true;
|
|
119
|
+
const roots = this.opts.getDaemonConfig().allowedRoots || [];
|
|
120
|
+
if (!roots.length) return;
|
|
121
|
+
for (const runtime of this.opts.getRuntimes()) {
|
|
122
|
+
if (!runtime.available || !runtime.executor || typeof runtime.executor.probeCommands !== 'function') continue;
|
|
123
|
+
if (this.commands[runtime.id] || this.probeAttempted.has(runtime.id)) continue;
|
|
124
|
+
this.probeAttempted.add(runtime.id);
|
|
125
|
+
const commands = await runtime.executor.probeCommands({ cwd: roots[0] });
|
|
126
|
+
if (commands) this.setCommands(runtime.id, commands);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* 运行时清单变化(周期复检 / 失败触发的复检发现可用性变化)后调用:
|
|
132
|
+
* 掉线的运行时清掉命令缓存,重报清单;已连上过平台的,补探测新恢复运行时的命令。
|
|
133
|
+
*/
|
|
134
|
+
runtimesChanged() {
|
|
135
|
+
for (const runtime of this.opts.getRuntimes()) {
|
|
136
|
+
if (!runtime.available) {
|
|
137
|
+
delete this.commands[runtime.id];
|
|
138
|
+
this.probeAttempted.delete(runtime.id);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
this.opts.send(this.inventory());
|
|
142
|
+
if (this.probed) this.probeCommands().catch((error) => this.log(`探测可用命令失败:${error.message}`));
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** 触发一次运行时复检(失败只记日志)。 */
|
|
146
|
+
async refreshRuntimes(reason) {
|
|
147
|
+
if (typeof this.opts.refreshRuntimes !== 'function') return;
|
|
148
|
+
try {
|
|
149
|
+
await this.opts.refreshRuntimes(reason);
|
|
150
|
+
} catch (error) {
|
|
151
|
+
this.log(`复检运行时失败:${error.message}`);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
ack(cmd, ok, error, result) {
|
|
156
|
+
if (!cmd || !cmd.id) return;
|
|
157
|
+
const frame = { type: 'ack', commandId: cmd.id, ok, error };
|
|
158
|
+
if (result !== undefined) frame.result = result;
|
|
159
|
+
this.opts.send(frame);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
async handlePrompt(cmd) {
|
|
163
|
+
const ctx = { sessionId: cmd.sessionId, threadId: cmd.threadId || cmd.sessionId, runId: cmd.runId };
|
|
164
|
+
const fail = (code, message) => {
|
|
165
|
+
this.emitEvent(ctx, agui.runStarted(ctx));
|
|
166
|
+
this.emitEvent(ctx, agui.runError(ctx, code, message));
|
|
167
|
+
};
|
|
168
|
+
if (!cmd.sessionId || !cmd.runId) return;
|
|
169
|
+
const findRuntime = () => this.opts.getRuntimes().find((r) => r.id === cmd.runtime);
|
|
170
|
+
let runtime = findRuntime();
|
|
171
|
+
if (!runtime || !runtime.available || !runtime.executor) {
|
|
172
|
+
// 自动恢复:启动时没探测到(PATH 未就绪、--version 超时、后来才装)不等于现在不可用,先现场复检一次
|
|
173
|
+
await this.refreshRuntimes(`收到 ${cmd.runtime} 的执行请求`);
|
|
174
|
+
runtime = findRuntime();
|
|
175
|
+
}
|
|
176
|
+
if (!runtime || !runtime.available || !runtime.executor) {
|
|
177
|
+
fail('RUNTIME_UNAVAILABLE', `本机运行时不可用:${cmd.runtime}${runtime && runtime.reason ? `(${runtime.reason})` : ''}`);
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
const daemonConfig = this.opts.getDaemonConfig();
|
|
181
|
+
const cwd = resolveAllowedCwd(cmd.cwd, daemonConfig.allowedRoots);
|
|
182
|
+
if (!cwd) {
|
|
183
|
+
fail('CWD_NOT_ALLOWED', `目录不在本机白名单内:${cmd.cwd}(用 wd-cli daemon roots add 添加)`);
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
let entry = this.sessions.get(cmd.sessionId);
|
|
187
|
+
if (entry && (entry.runtimeId !== runtime.id || entry.cwd !== cwd)) {
|
|
188
|
+
entry.session.close();
|
|
189
|
+
entry = null;
|
|
190
|
+
}
|
|
191
|
+
if (entry && entry.busy) {
|
|
192
|
+
fail('RUN_IN_PROGRESS', '该会话已有一轮在执行');
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
if (!entry) {
|
|
196
|
+
const session = runtime.executor.createSession({
|
|
197
|
+
platformSessionId: cmd.sessionId,
|
|
198
|
+
runtimeSessionId: cmd.runtimeSessionId || null,
|
|
199
|
+
cwd,
|
|
200
|
+
permission: this.permission,
|
|
201
|
+
log: (m) => this.log(`[${cmd.sessionId}] ${m}`),
|
|
202
|
+
emit: (c, event) => {
|
|
203
|
+
// 运行时起不来 / 中途退出:后台复检,可用性有变化会重报清单(页面据此提示)
|
|
204
|
+
if (event && event.type === 'RUN_ERROR' && RECHECK_CODES.has(event.code)) {
|
|
205
|
+
this.refreshRuntimes(`${runtime.id} ${event.code}`);
|
|
206
|
+
}
|
|
207
|
+
this.emitEvent(c, event);
|
|
208
|
+
},
|
|
209
|
+
onCommands: (commands) => this.setCommands(runtime.id, commands),
|
|
210
|
+
onRuntimeSession: (runtimeSessionId) =>
|
|
211
|
+
this.opts.send({ type: 'session', sessionId: cmd.sessionId, runtimeSessionId }),
|
|
212
|
+
});
|
|
213
|
+
entry = { runtimeId: runtime.id, cwd, session, busy: false };
|
|
214
|
+
this.sessions.set(cmd.sessionId, entry);
|
|
215
|
+
}
|
|
216
|
+
entry.busy = true;
|
|
217
|
+
try {
|
|
218
|
+
const images = Array.isArray(cmd.images)
|
|
219
|
+
? cmd.images.filter((i) => i && typeof i.data === 'string' && typeof i.mimeType === 'string' && i.mimeType.startsWith('image/'))
|
|
220
|
+
: [];
|
|
221
|
+
await entry.session.prompt(ctx, String(cmd.text || ''), images);
|
|
222
|
+
} finally {
|
|
223
|
+
entry.busy = false;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
activeRuns() {
|
|
228
|
+
return [...this.sessions.values()].filter((e) => e.busy).length;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
shutdown() {
|
|
232
|
+
this.permission.cancelAll();
|
|
233
|
+
for (const entry of this.sessions.values()) {
|
|
234
|
+
try {
|
|
235
|
+
entry.session.close();
|
|
236
|
+
} catch {
|
|
237
|
+
// 忽略
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
this.sessions.clear();
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
module.exports = { Supervisor };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dmall-inc/wd-cli",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "Waterdrop platform CLI: sign in with your unified identity, pick a workspace, and use the open platform resources from your terminal",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "commonjs",
|