dsh-mobile-plugin 0.5.31 → 0.5.32

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.
@@ -1,217 +0,0 @@
1
- 'use strict';
2
-
3
- /**
4
- * CLI 对话的适配器白名单 —— `/api/mobile/v1/cli/*` 能起什么进程的**全部**依据。
5
- *
6
- * 与远程终端那份白名单(`mobile/terminal.js` 的 `AGENTS`)是同一档边界,只是走的口子不同:
7
- * 终端把 CLI 的 TUI 原样搬给手机,这里让 CLI 以 **ACP(Agent Client Protocol)** 的方式跑,
8
- * 手机收到的是结构化的「消息 / 工具调用 / 审批请求」,于是对话页能像正常对话一样渲染。
9
- *
10
- * 四种 CLI 起法有两种:
11
- *
12
- * - `gemini --acp`、`opencode acp`:CLI **自己**就实现了 ACP 服务端,直接起。
13
- * - codex / claude:CLI 本身没有 ACP,靠官方 npm 桥(`@agentclientprotocol/*`)。桥包自带
14
- * 对应的 CLI 依赖、并复用本机登录态,所以本机装没装 `codex` / `claude` 不影响。
15
- *
16
- * **这张表是兜底,不是清单本身**:真正「能起哪些」来自官方 ACP 注册表(见 `cli/registry.js`),
17
- * 注册表里走 npx 分发的都能起,新 CLI 上线不必我们发版。下面这四条是它的降级档 —— 离线、
18
- * 注册表拉不到、盘上也没有缓存时,仍然有四家用,而不是一个空清单。
19
- *
20
- * 内置这四条**永远出现在清单里**(它们是本仓维护的那一档,桥包由 npx 按需拉);注册表补进来
21
- * 的那些则要求「这台电脑上真的装了」——见 `installedPackages`。
22
- *
23
- * `cursor-agent` / `aider` 没有 ACP 实现,所以在这一侧根本不存在 —— 它们仍然只能从终端页进
24
- * (那一份表在 `terminal.js`,两边各管各的能力面)。
25
- */
26
-
27
- const fs = require('node:fs');
28
- const os = require('node:os');
29
- const path = require('node:path');
30
-
31
- const { findExecutable } = require('../terminal.js');
32
-
33
- /**
34
- * `transport` 只描述**怎么起**,不代表能力差别:
35
- * - `native`:CLI 自带的 ACP 子命令,参数写在 `args`。
36
- * - `npx` :官方桥包。本机已经全局装了同名可执行就直接用,否则 `npx -y <包名>`。
37
- *
38
- * `package` 是 npx 分发那一档的包名(注册表里可能带版本,原样收着:版本由注册表钉住)。
39
- */
40
- const AGENTS = [
41
- {
42
- id: 'codex',
43
- label: 'Codex',
44
- transport: 'npx',
45
- package: '@agentclientprotocol/codex-acp',
46
- bin: 'codex-acp',
47
- },
48
- {
49
- id: 'claude',
50
- label: 'Claude Code',
51
- transport: 'npx',
52
- package: '@agentclientprotocol/claude-agent-acp',
53
- bin: 'claude-agent-acp',
54
- },
55
- {
56
- id: 'gemini',
57
- label: 'Gemini CLI',
58
- transport: 'native',
59
- bin: 'gemini',
60
- args: ['--acp'],
61
- },
62
- {
63
- id: 'opencode',
64
- label: 'OpenCode',
65
- transport: 'native',
66
- bin: 'opencode',
67
- args: ['acp'],
68
- },
69
- ];
70
-
71
- function findAgent(id) {
72
- return AGENTS.find((agent) => agent.id === id) || null;
73
- }
74
-
75
- /**
76
- * 子进程的 PATH。
77
- *
78
- * 宿主可能是从 Finder / 应用图标起的,那种进程的 PATH 常常只有 `/usr/bin:/bin`,`npx` 的
79
- * shebang 是 `#!/usr/bin/env node`,找不着 node 就直接退出。所以把「当前跑宿主的那个 node」
80
- * 和「适配器所在目录」垫到最前面 —— 只补这两处,不去猜用户 shell 的 PATH。
81
- */
82
- function childEnv(command, env = process.env) {
83
- const key = env.PATH !== undefined || env.Path === undefined ? 'PATH' : 'Path';
84
- const base = env[key] || '';
85
- const seen = new Set();
86
- const merged = [path.dirname(process.execPath), path.dirname(command), ...base.split(path.delimiter)]
87
- .filter((dir) => {
88
- if (!dir || seen.has(dir)) return false;
89
- seen.add(dir);
90
- return true;
91
- });
92
- return { ...env, [key]: merged.join(path.delimiter) };
93
- }
94
-
95
- /** 去掉 `@scope/name@1.2.3` 尾部的版本,得到 npm 认的包名。 */
96
- function barePackage(pkg) {
97
- const name = String(pkg || '').trim();
98
- if (!name) return '';
99
- // `@scope/name@1.2.3` / `name@1.2.3`:最后一个 `@` 在首位之后才是版本分隔。
100
- const at = name.lastIndexOf('@');
101
- return at > 0 ? name.slice(0, at) : name;
102
- }
103
-
104
- /** 一个 `node_modules` 目录下有没有这个包(两段式名字按第一段进一层)。 */
105
- function hasPackageIn(dir, name) {
106
- try {
107
- const stat = fs.statSync(path.join(dir, name));
108
- return stat.isDirectory();
109
- } catch {
110
- try {
111
- return fs.statSync(path.join(dir, name, 'package.json')).isFile();
112
- } catch {
113
- return false;
114
- }
115
- }
116
- }
117
-
118
- /**
119
- * 这台电脑上已经装了的 npm 包(只认名字,一份 Set)。
120
- *
121
- * 为什么要它:注册表里有二十来家能 npx 拉起的 ACP 适配器,全列出来等于给用户看一屏他从没
122
- * 装过的东西 —— 点下去要么下载几十秒,要么卡在登录上。手机上要的是「这台电脑现在能聊哪些」。
123
- *
124
- * 三个来源,全是读目录(不 spawn npm,列一次清单不该等一个子进程):
125
- *
126
- * 1. 跑宿主的那个 node 旁边的 `lib/node_modules`(Homebrew / 官方安装包的路子);
127
- * 2. `NPM_CONFIG_PREFIX` 指的全局根(用户自己配过的);
128
- * 3. `~/.npm/_npx` 下每一份 `node_modules`(npx 拉过一次就留在那儿,桥包通常就在这一档)。
129
- *
130
- * 探不到就返回空集 —— 那种情况下注册表那一条当「没装」处理,内置四条照旧在。
131
- */
132
- function installedPackages(env = process.env) {
133
- const roots = [];
134
- if (env.NPM_CONFIG_PREFIX) roots.push(path.join(env.NPM_CONFIG_PREFIX, 'lib', 'node_modules'));
135
- if (env.NPM_GLOBAL_ROOT) roots.push(env.NPM_GLOBAL_ROOT);
136
- roots.push(path.join(path.dirname(process.execPath), '..', 'lib', 'node_modules'));
137
- roots.push(path.join(os.homedir(), '.npm-global', 'lib', 'node_modules'));
138
- const npxCache = path.join(os.homedir(), '.npm', '_npx');
139
- try {
140
- for (const entry of fs.readdirSync(npxCache)) {
141
- roots.push(path.join(npxCache, entry, 'node_modules'));
142
- }
143
- } catch {
144
- // 没有 npx 缓存目录就是没走过 npx:不影响别的来源。
145
- }
146
-
147
- const names = new Set();
148
- for (const dir of roots) {
149
- let entries = [];
150
- try { entries = fs.readdirSync(dir); } catch { continue; }
151
- for (const entry of entries) {
152
- if (entry.startsWith('.')) continue;
153
- if (entry.startsWith('@')) {
154
- let scoped = [];
155
- try { scoped = fs.readdirSync(path.join(dir, entry)); } catch { continue; }
156
- for (const child of scoped) names.add(`${entry}/${child}`);
157
- continue;
158
- }
159
- names.add(entry);
160
- }
161
- }
162
- return names;
163
- }
164
-
165
- /** 这个包在这台电脑上装了没有(`bin` 在 PATH 里也算装了 —— 那是同一件事的另一半)。 */
166
- function isInstalled(agent, installed, env = process.env) {
167
- if (!agent) return false;
168
- if (agent.bin && findExecutable(agent.bin, env)) return true;
169
- const name = barePackage(agent.package);
170
- return Boolean(name && installed && installed.has(name));
171
- }
172
-
173
- /**
174
- * 这台电脑上能不能起这个 CLI。
175
- *
176
- * 回 `{ ok: true, command, args, via }` 或 `{ ok: false, reason }`;`reason` 直接给界面用,
177
- * 所以是一句人话,不是错误码。
178
- *
179
- * `ok` 说的是「现在点得下去、起得来」:`npx` 那一档只要本机有 npx 就成立(首次会下载桥包,
180
- * 之后走缓存)。要不要把它列进选择卡是另一件事,由 `isInstalled` 判(见 `service.listAgents`)。
181
- *
182
- * `npx` 那一档的参数要跟着一起给:注册表里不少包本身不是 ACP 服务端,得靠自己的子命令进
183
- * ACP 模式(`cline --acp`、`bob acp`、`droid exec --output-format acp-daemon`)。只给包名
184
- * 的话进程起来就是一个普通 CLI,握手永远等不到 —— 表现是「一直在启动」。
185
- */
186
- function resolveAgent(agent, env = process.env) {
187
- if (!agent) return { ok: false, reason: '不认识这个 CLI' };
188
- try {
189
- const extra = Array.isArray(agent.args) ? agent.args.filter((arg) => typeof arg === 'string') : [];
190
- if (agent.transport === 'native') {
191
- const bin = findExecutable(agent.bin, env);
192
- if (!bin) return { ok: false, reason: `这台电脑上没有装 ${agent.label}`, via: 'native' };
193
- return { ok: true, command: bin, args: extra, via: 'native' };
194
- }
195
- // 桥包已经全局装过就省掉一次 npx 解析;没有就交给 npx 拉(首次会下载,之后走缓存)。
196
- const installedBin = agent.bin ? findExecutable(agent.bin, env) : null;
197
- if (installedBin) return { ok: true, command: installedBin, args: extra, via: 'installed' };
198
- const npx = findExecutable('npx', env) || findExecutable('npm', env);
199
- if (!npx) return { ok: false, reason: `这台电脑上没有 npx,起不了 ${agent.label}` };
200
- const args = path.basename(npx) === 'npx'
201
- ? ['-y', agent.package, ...extra]
202
- : ['exec', '-y', '--', agent.package, ...extra];
203
- return { ok: true, command: npx, args, via: 'npx' };
204
- } catch {
205
- return { ok: false, reason: `探测 ${agent.label} 的时候出错了` };
206
- }
207
- }
208
-
209
- module.exports = {
210
- AGENTS,
211
- findAgent,
212
- resolveAgent,
213
- childEnv,
214
- installedPackages,
215
- isInstalled,
216
- barePackage,
217
- };
@@ -1,228 +0,0 @@
1
- 'use strict';
2
-
3
- /**
4
- * ACP 的 `session/update` 事件 → 手机对话页认识的块(`CliBlock`)。
5
- *
6
- * 这一层就是整个「对话式 CLI」的翻译器,它之所以能这么薄,是因为 ACP 本身就是照着
7
- * 「编辑器里的一个对话」设计的:正文、思考、工具调用、计划、审批各是独立事件,不需要
8
- * 去猜终端屏幕。
9
- *
10
- * 两条只在这里成立的口径:
11
- *
12
- * - **流式正文按 `messageId` 合并成一条块**。ACP 的 `agent_message_chunk` 是逐段推的,
13
- * 同一个 `messageId` 的若干 chunk 属于同一条消息;换成新 `messageId`(或没有 id)才
14
- * 另起一条。块的 `id` 直接由它派生成 `a:<messageId>`,所以重连重放与实时增量落到同一处。
15
- * - **不加 `user_message_chunk`**。宿主自己记得用户发过什么(`pushUser`),ACP 那边回显
16
- * 的那一份要是也画出来,手机上每条提问都会出现两遍。
17
- *
18
- * 块只增不改顺序:工具调用按 `callId` 原地更新,计划整表替换(ACP 就是这么定的)。
19
- */
20
-
21
- let autoSeq = 0;
22
-
23
- function nextId(prefix) {
24
- autoSeq += 1;
25
- return `${prefix}:${Date.now().toString(36)}-${autoSeq.toString(36)}`;
26
- }
27
-
28
- /** ACP 的工具状态四档 → 手机的三档(pending / in_progress 都还是「在跑」)。 */
29
- function toolState(status) {
30
- if (status === 'completed') return 'ok';
31
- if (status === 'failed') return 'error';
32
- return 'running';
33
- }
34
-
35
- /** 工具内容的正文:ACP 允许文本 / diff / 终端引用三种,手机上只画文本那一种。 */
36
- function textOfToolContent(content) {
37
- if (!Array.isArray(content)) return '';
38
- const parts = [];
39
- for (const item of content) {
40
- if (!item || typeof item !== 'object') continue;
41
- if (item.type === 'content' && item.content && item.content.type === 'text' && typeof item.content.text === 'string') {
42
- parts.push(item.content.text);
43
- } else if (item.type === 'diff' && typeof item.path === 'string') {
44
- parts.push(item.path);
45
- }
46
- }
47
- return parts.join('\n');
48
- }
49
-
50
- /**
51
- * 工具调用的正文。
52
- *
53
- * 优先取结构化的 `content`(ACP 里是文本 / diff / 终端引用),没有就退到 `rawOutput`:
54
- * codex 那边执行结果只出现在 `rawOutput` 里,`content` 一直是空的 —— 只认前者的话手机上
55
- * 每一条工具卡都不带结果。
56
- */
57
- function outputOf(update) {
58
- const fromContent = textOfToolContent(update.content);
59
- if (fromContent) return fromContent;
60
- const raw = update.rawOutput;
61
- if (typeof raw === 'string') return clip(raw, 4000);
62
- if (raw && typeof raw === 'object') return clip(JSON.stringify(raw), 4000);
63
- return '';
64
- }
65
-
66
- function textOfBlock(block) {
67
- if (!block || typeof block !== 'object') return '';
68
- if (block.type === 'text' && typeof block.text === 'string') return block.text;
69
- if (block.type === 'resource_link' && typeof block.uri === 'string') return block.uri;
70
- return '';
71
- }
72
-
73
- function clip(value, max) {
74
- return value.length > max ? `${value.slice(0, max)}…` : value;
75
- }
76
-
77
- /**
78
- * 一条会话的块表。
79
- *
80
- * `apply` 只改表并把「变了的块」交出来,广播由服务层做 —— 它才知道该不该攒批。
81
- */
82
- function createBlockLog() {
83
- /** 有序块;`index` 存同一批对象的引用,改属性即改表里那一份。 */
84
- const blocks = [];
85
- const index = new Map();
86
-
87
- function put(block) {
88
- blocks.push(block);
89
- index.set(block.id, block);
90
- return block;
91
- }
92
-
93
- function appendText(id, kind, text, at) {
94
- const existing = index.get(id);
95
- if (!existing) return put({ id, kind, text, at, streaming: true });
96
- existing.text += text;
97
- existing.streaming = true;
98
- return existing;
99
- }
100
-
101
- return {
102
- /** 只读快照(`hello` 帧用),复制一层免得调用方改了表里的对象。 */
103
- list() {
104
- return blocks.map((block) => ({ ...block }));
105
- },
106
-
107
- size() {
108
- return blocks.length;
109
- },
110
-
111
- /** 用户发出去的这一条(我们自己的记录,见文件头)。`images` 是已存好的图片元数据。 */
112
- pushUser(text, at, images) {
113
- return put({
114
- id: nextId('u'),
115
- kind: 'user',
116
- text,
117
- at,
118
- ...(Array.isArray(images) && images.length ? { images } : {}),
119
- });
120
- },
121
-
122
- notice(level, text, at) {
123
- return put({ id: nextId('n'), kind: 'notice', level, text, at });
124
- },
125
-
126
- /** 一轮结束了:把还在流式的正文收口(否则那一行永远带着光标)。 */
127
- settle(at) {
128
- const changed = [];
129
- for (const block of blocks) {
130
- if (!block.streaming) continue;
131
- block.streaming = false;
132
- block.at = at || block.at;
133
- changed.push(block);
134
- }
135
- return changed;
136
- },
137
-
138
- /**
139
- * 收下一条 `session/update`。
140
- *
141
- * 返回 `{ block }`(有块变了,广播它)、`{ title }`(只改了标题)、或 `null`(不关心)。
142
- */
143
- apply(update, at) {
144
- if (!update || typeof update !== 'object') return null;
145
- const kind = update.sessionUpdate;
146
-
147
- if (kind === 'agent_message_chunk' || kind === 'agent_thought_chunk') {
148
- const text = textOfBlock(update.content);
149
- if (!text) return null;
150
- const isText = kind === 'agent_message_chunk';
151
- const id = `${isText ? 't' : 'r'}:${update.messageId || 'main'}`;
152
- return { block: appendText(id, isText ? 'text' : 'reasoning', text, at) };
153
- }
154
-
155
- if (kind === 'tool_call') {
156
- const id = `c:${update.toolCallId}`;
157
- const block = {
158
- id,
159
- kind: 'tool',
160
- callId: update.toolCallId,
161
- name: typeof update.name === 'string' ? update.name : '',
162
- title: typeof update.title === 'string' ? update.title : '',
163
- state: toolState(update.status),
164
- argsRaw: update.rawInput === undefined ? '' : clip(JSON.stringify(update.rawInput), 2000),
165
- output: outputOf(update),
166
- at,
167
- };
168
- const existing = index.get(id);
169
- if (!existing) return { block: put(block) };
170
- Object.assign(existing, block, { at: existing.at });
171
- return { block: existing };
172
- }
173
-
174
- if (kind === 'tool_call_update') {
175
- const id = `c:${update.toolCallId}`;
176
- const existing = index.get(id);
177
- if (!existing) return null;
178
- if (typeof update.title === 'string' && update.title) existing.title = update.title;
179
- if (typeof update.name === 'string' && update.name) existing.name = update.name;
180
- if (update.status) existing.state = toolState(update.status);
181
- if (update.rawInput !== undefined) existing.argsRaw = clip(JSON.stringify(update.rawInput), 2000);
182
- const output = outputOf(update);
183
- if (output) existing.output = output;
184
- return { block: existing };
185
- }
186
-
187
- if (kind === 'plan') {
188
- const entries = Array.isArray(update.entries) ? update.entries : [];
189
- const steps = entries.map((entry) => ({
190
- text: typeof entry.content === 'string' ? entry.content : '',
191
- status: typeof entry.status === 'string' ? entry.status : 'pending',
192
- }));
193
- const existing = index.get('plan');
194
- if (existing) {
195
- existing.steps = steps;
196
- existing.at = at;
197
- return { block: existing };
198
- }
199
- return { block: put({ id: 'plan', kind: 'plan', steps, at }) };
200
- }
201
-
202
- if (kind === 'session_info_update') {
203
- const title = typeof update.title === 'string' ? update.title.trim() : '';
204
- return title ? { title } : null;
205
- }
206
-
207
- if (kind === 'notice') {
208
- const title = typeof update.title === 'string' ? update.title : '';
209
- const detail = typeof update.description === 'string' ? update.description : '';
210
- if (!title && !detail) return null;
211
- return {
212
- block: put({
213
- id: nextId('n'),
214
- kind: 'notice',
215
- level: update.severity === 'error' ? 'error' : 'info',
216
- text: detail ? `${title}:${detail}` : title,
217
- at,
218
- }),
219
- };
220
- }
221
-
222
- // `usage_update` / `available_commands_update` / 压缩那几档这一版不画。
223
- return null;
224
- },
225
- };
226
- }
227
-
228
- module.exports = { createBlockLog, toolState };
@@ -1,155 +0,0 @@
1
- 'use strict';
2
-
3
- /**
4
- * 「这个 CLI 该按哪一套配置跑」—— 一台电脑每个 CLI 记一份。
5
- *
6
- * ACP 的配置是**会话级**的:`session/new` 之后才有 `configOptions`,`set_config_option` 只对那一条
7
- * 会话生效,每开一条新对话都回到 CLI 默认档。所以这里把用户定的那套按 `agentId` 存下来,新会话
8
- * 握手完、第一次发消息之前逐项套过去。只记用户显式选过的项、没选过的不猜;选项表缓存一份供没开
9
- * 会话时画,但写入仍按新会话校验;写不进去不算错。文件:`$DSH_HOME/storages/mobile-plugin/cli-config.json`。
10
- */
11
-
12
- const fs = require('node:fs');
13
- const path = require('node:path');
14
-
15
- const paths = require('../../shared/paths');
16
-
17
- function storeFile() {
18
- return path.join(paths.dshHome(), 'storages', 'mobile-plugin', 'cli-config.json');
19
- }
20
-
21
- function text(value) {
22
- return typeof value === 'string' ? value.trim() : '';
23
- }
24
-
25
- function load() {
26
- try {
27
- const raw = JSON.parse(fs.readFileSync(storeFile(), 'utf8'));
28
- const agents = raw && typeof raw === 'object' ? raw.agents : null;
29
- return agents && typeof agents === 'object' ? agents : {};
30
- } catch {
31
- return {};
32
- }
33
- }
34
-
35
- function save(agents) {
36
- try {
37
- fs.mkdirSync(path.dirname(storeFile()), { recursive: true });
38
- fs.writeFileSync(storeFile(), JSON.stringify({ agents }, null, 0), 'utf8');
39
- } catch {
40
- // 写不进去只是下次开局回到 CLI 自己的默认,不影响这一条会话。
41
- }
42
- }
43
-
44
- /**
45
- * 把 ACP 的一项配置压成「够画一个下拉」的形状。
46
- *
47
- * ACP 的 `options` 有两种:扁平的一项一条,或者按组(`{group, name, options}`)。手机那边
48
- * 只认平铺的一行选项,所以这里摊平并保留 `description`(档位说明对「这一档到底放没放开」很
49
- * 关键,用户要靠它决定选哪个)。
50
- */
51
- function choicesOf(option) {
52
- const raw = Array.isArray(option && option.options) ? option.options : [];
53
- const out = [];
54
- for (const item of raw) {
55
- if (!item || typeof item !== 'object') continue;
56
- if (Array.isArray(item.options)) {
57
- for (const child of item.options) {
58
- if (child && child.value !== undefined) {
59
- out.push({
60
- value: String(child.value),
61
- name: text(child.name) || String(child.value),
62
- ...(text(child.description) ? { description: text(child.description) } : {}),
63
- ...(text(item.name) ? { group: text(item.name) } : {}),
64
- });
65
- }
66
- }
67
- continue;
68
- }
69
- if (item.value !== undefined) {
70
- out.push({
71
- value: String(item.value),
72
- name: text(item.name) || String(item.value),
73
- ...(text(item.description) ? { description: text(item.description) } : {}),
74
- });
75
- }
76
- }
77
- return out;
78
- }
79
-
80
- /**
81
- * 会话的整份配置项 → 配置页能直接画的那张表。
82
- *
83
- * `category` 原样带出去:手机靠它分组(权限档 / 模型 / 思考档位 / 其它),而它是 ACP 定的
84
- * 语义分类,不是我们猜的。
85
- */
86
- function describe(configOptions) {
87
- const list = Array.isArray(configOptions) ? configOptions : [];
88
- const out = [];
89
- for (const option of list) {
90
- if (!option || typeof option.id !== 'string' || !option.id) continue;
91
- const choices = choicesOf(option);
92
- if (choices.length === 0) continue;
93
- out.push({
94
- id: option.id,
95
- name: text(option.name) || option.id,
96
- ...(text(option.category) ? { category: text(option.category) } : {}),
97
- ...(text(option.description) ? { description: text(option.description) } : {}),
98
- ...(option.currentValue !== undefined ? { currentValue: String(option.currentValue) } : {}),
99
- choices,
100
- });
101
- }
102
- return out;
103
- }
104
-
105
- /** 这个 CLI 记着的那一套:`{ values: {<configId>: <value>}, options: [...] }`。 */
106
- function prefsOf(agentId) {
107
- const entry = load()[text(agentId)];
108
- const values = entry && entry.values && typeof entry.values === 'object' ? entry.values : {};
109
- const clean = {};
110
- for (const [key, value] of Object.entries(values)) {
111
- if (key && typeof value === 'string' && value) clean[key] = value;
112
- }
113
- return { values: clean, options: Array.isArray(entry && entry.options) ? entry.options : [] };
114
- }
115
-
116
- /** 记下「这个 CLI 的某一项该用哪个值」(值空 = 撤掉这一项,回到 CLI 自己的默认)。 */
117
- function setValue(agentId, configId, value) {
118
- const agent = text(agentId);
119
- const id = text(configId);
120
- if (!agent || !id) return;
121
- const agents = load();
122
- const entry = agents[agent] && typeof agents[agent] === 'object' ? { ...agents[agent] } : {};
123
- const values = entry.values && typeof entry.values === 'object' ? { ...entry.values } : {};
124
- const clean = text(value);
125
- if (clean) values[id] = clean;
126
- else delete values[id];
127
- if (Object.keys(values).length) entry.values = values;
128
- else delete entry.values;
129
- if (entry.values || (Array.isArray(entry.options) && entry.options.length)) agents[agent] = entry;
130
- else delete agents[agent];
131
- save(agents);
132
- }
133
-
134
- /** 记下这个 CLI 的选项表(配置页在没开会话时也要能画出来)。 */
135
- function rememberOptions(agentId, configOptions) {
136
- const agent = text(agentId);
137
- if (!agent) return;
138
- const options = describe(configOptions);
139
- const agents = load();
140
- if (options.length === 0) {
141
- // 一家 CLI 本来就不给配置项:把旧表清掉,别让配置页停在上一版 CLI 的样子。
142
- const entry = agents[agent];
143
- if (!entry || !Array.isArray(entry.options) || entry.options.length === 0) return;
144
- const next = { ...entry };
145
- delete next.options;
146
- if (next.values) agents[agent] = next;
147
- else delete agents[agent];
148
- } else {
149
- const entry = agents[agent] && typeof agents[agent] === 'object' ? { ...agents[agent] } : {};
150
- agents[agent] = { ...entry, options };
151
- }
152
- save(agents);
153
- }
154
-
155
- module.exports = { storeFile, describe, prefsOf, setValue, rememberOptions };