agent-bridge-service 0.2.2 → 1.0.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/CHANGELOG.md +112 -140
- package/README.md +508 -35
- package/README.zh-CN.md +443 -33
- package/assets/agent-icons/agy.svg +27 -0
- package/dist/adapters/acp-auth.js +51 -0
- package/dist/adapters/acp-codebuddy.js +507 -0
- package/dist/adapters/agent-adapters.js +466 -46
- package/dist/adapters/claude-stream.js +159 -0
- package/dist/adapters/codex-app-server.js +310 -0
- package/dist/adapters/host-env.js +48 -0
- package/dist/adapters/mcp-elicitation.js +148 -0
- package/dist/adapters/mcp-host.js +70 -0
- package/dist/adapters/step-parsers.js +583 -0
- package/dist/app/bootstrap.js +163 -74
- package/dist/app/claude-resident.js +204 -0
- package/dist/app/conversation.js +639 -0
- package/dist/app/service-gate.js +1 -1
- package/dist/app/session.js +425 -14
- package/dist/app/task-dispatch.js +883 -7
- package/dist/app/task-registry.js +168 -0
- package/dist/cli/dispatch.js +4 -0
- package/dist/cli/flags.js +11 -0
- package/dist/cli/help.js +25 -7
- package/dist/cli/panel.js +334 -0
- package/dist/cli/render.js +2 -0
- package/dist/commands/approvals.js +80 -0
- package/dist/commands/connect.js +122 -4
- package/dist/commands/daemon-service.js +81 -0
- package/dist/commands/daemon.js +269 -6
- package/dist/commands/logout.js +13 -10
- package/dist/commands/serve.js +98 -12
- package/dist/commands/sessions.js +106 -0
- package/dist/core/ansi.js +5 -0
- package/dist/core/artifact-extract.js +158 -0
- package/dist/core/artifact-transfer.js +583 -0
- package/dist/core/device-identity.js +22 -0
- package/dist/core/emitter.js +50 -8
- package/dist/core/errors.js +43 -0
- package/dist/core/form-schema.js +72 -0
- package/dist/core/interaction-broker.js +440 -0
- package/dist/core/interaction-url.js +63 -0
- package/dist/core/partial-messages.js +99 -0
- package/dist/core/permission-ledger.js +146 -0
- package/dist/core/permission-policy.js +88 -0
- package/dist/core/port-fallback.js +22 -0
- package/dist/core/redact.js +13 -0
- package/dist/core/scan.js +12 -7
- package/dist/core/session-store.js +1632 -0
- package/dist/core/task-supervisor.js +955 -30
- package/dist/infra/atomic-file.js +111 -0
- package/dist/infra/audit.js +7 -0
- package/dist/infra/auth.js +167 -42
- package/dist/infra/config.js +511 -72
- package/dist/infra/device-identity-store.js +87 -0
- package/dist/infra/jwt.js +149 -0
- package/dist/infra/profile-lock.js +295 -0
- package/dist/infra/proxy-env.js +60 -0
- package/dist/infra/service-token.js +25 -4
- package/dist/protocol.js +298 -2
- package/dist/transport/a2a-mapping.js +206 -0
- package/dist/transport/acp-gateway-mapping.js +186 -0
- package/dist/transport/bridge-client.js +31 -22
- package/dist/transport/control-plane-page.js +820 -0
- package/dist/transport/control-plane-sessions-page.js +90 -0
- package/dist/transport/control-plane.js +923 -71
- package/dist/transport/gateway.js +1005 -0
- package/dist/transport/office-preview.js +154 -0
- package/package.json +6 -3
- package/assets/agent-icons/gemini.svg +0 -1
package/dist/infra/config.js
CHANGED
|
@@ -1,41 +1,159 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* 本地配置:`~/.agent-bridge/config.toml`(Windows 为 `%APPDATA
|
|
2
|
+
* 本地配置:`~/.agent-bridge/config.toml`(Windows 为 `%APPDATA%\.agent-bridge\config.toml`)
|
|
3
3
|
*
|
|
4
4
|
* 安全:文件权限 0600(仅当前用户可读写);终端输出**永不打印** device_token 明文,
|
|
5
5
|
* 只显示尾号与设备 id。
|
|
6
6
|
*/
|
|
7
|
-
import {
|
|
7
|
+
import { readFileSync } from 'node:fs';
|
|
8
8
|
import { homedir } from 'node:os';
|
|
9
9
|
import { dirname, join, resolve } from 'node:path';
|
|
10
10
|
import { parse as parseToml, stringify as stringifyToml } from 'smol-toml';
|
|
11
|
+
import { assertDeviceId } from '../core/device-identity.js';
|
|
12
|
+
import { DEFAULT_APPROVAL_TTL_MS } from '../core/permission-ledger.js';
|
|
13
|
+
import { writeAtomicFile } from './atomic-file.js';
|
|
11
14
|
/**
|
|
12
|
-
*
|
|
13
|
-
*
|
|
15
|
+
* 本机权限策略(1.0)。
|
|
16
|
+
*
|
|
17
|
+
* ⚠️ 这是 **argv 确定性**策略,不是 OS 提权、配置隔离或环境沙箱 —— 它只决定 bridge 用哪套
|
|
18
|
+
* 参数构造命令,不承诺 CLI 插件/配置文件/继承环境造成的等效自动审批能力被阻断。
|
|
19
|
+
*/
|
|
20
|
+
export const PERMISSION_POLICIES = ['default', 'allow-full', 'full'];
|
|
21
|
+
/**
|
|
22
|
+
* 并发上限解析:启动参数 `--max-tasks` > `AGENT_BRIDGE_MAX_TASKS`(临时/CI 覆盖)
|
|
23
|
+
* > config.toml 的 `max_tasks` > 默认 30。
|
|
24
|
+
* 夹在 1~128:0 或负数会让 bridge 一个任务都跑不了;128 是防手滑的保险丝,
|
|
25
|
+
* 批量调度场景的默认值是 30(2026-10-24 调整,原为 3),真需要更高请先评估本机资源。
|
|
14
26
|
*/
|
|
15
|
-
export const DEFAULT_MAX_TASKS =
|
|
16
|
-
const MAX_TASKS_CEILING =
|
|
17
|
-
export function resolveMaxTasks(fromConfig) {
|
|
27
|
+
export const DEFAULT_MAX_TASKS = 30;
|
|
28
|
+
const MAX_TASKS_CEILING = 128;
|
|
29
|
+
export function resolveMaxTasks(fromConfig, override) {
|
|
18
30
|
const envRaw = Number(process.env['AGENT_BRIDGE_MAX_TASKS'] ?? '');
|
|
19
|
-
const chosen = Number.isFinite(
|
|
31
|
+
const chosen = override !== undefined && Number.isFinite(override) && override > 0
|
|
32
|
+
? override
|
|
33
|
+
: Number.isFinite(envRaw) && envRaw > 0
|
|
34
|
+
? envRaw
|
|
35
|
+
: fromConfig;
|
|
20
36
|
if (!Number.isFinite(chosen ?? NaN) || (chosen ?? 0) <= 0)
|
|
21
37
|
return DEFAULT_MAX_TASKS;
|
|
22
38
|
return Math.min(MAX_TASKS_CEILING, Math.max(1, Math.floor(chosen)));
|
|
23
39
|
}
|
|
24
|
-
/**
|
|
25
|
-
|
|
40
|
+
/**
|
|
41
|
+
* waiting_user 空闲关闭:默认 0(不自动关闭,保持旧行为)。
|
|
42
|
+
* 环境变量 `AGENT_BRIDGE_SESSION_IDLE_MS` > config.toml `session_idle_ms`。
|
|
43
|
+
* 上限 24h,防止手滑写成 `99999999999` 把会话永久占住。
|
|
44
|
+
*/
|
|
45
|
+
export function resolveSessionIdleMs(fromConfig) {
|
|
46
|
+
const raw = process.env['AGENT_BRIDGE_SESSION_IDLE_MS'];
|
|
47
|
+
const envRaw = raw === undefined || raw.trim() === '' ? undefined : Number(raw);
|
|
48
|
+
const chosen = envRaw !== undefined && Number.isFinite(envRaw) && envRaw >= 0 ? envRaw : fromConfig;
|
|
49
|
+
if (!Number.isFinite(chosen ?? NaN) || (chosen ?? 0) <= 0)
|
|
50
|
+
return 0;
|
|
51
|
+
return Math.min(24 * 60 * 60 * 1000, Math.floor(chosen));
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* 队列上限:默认 60,夹在 0~1024(0 = 不允许排队,超限直接失败)。
|
|
55
|
+
* 优先级:启动参数 `--max-queue` > `AGENT_BRIDGE_MAX_QUEUE` > config.toml 的 `max_queue` > 默认。
|
|
56
|
+
*/
|
|
57
|
+
export const DEFAULT_MAX_QUEUED = 60;
|
|
26
58
|
const MAX_QUEUED_CEILING = 1024;
|
|
27
|
-
export function resolveMaxQueued(fromConfig) {
|
|
59
|
+
export function resolveMaxQueued(fromConfig, override) {
|
|
28
60
|
const rawEnv = process.env['AGENT_BRIDGE_MAX_QUEUE'];
|
|
29
61
|
const envValue = rawEnv === undefined || rawEnv.trim() === '' ? undefined : Number(rawEnv);
|
|
30
|
-
const chosen =
|
|
62
|
+
const chosen = override !== undefined && Number.isFinite(override) && override >= 0
|
|
63
|
+
? override
|
|
64
|
+
: envValue !== undefined && Number.isFinite(envValue) && envValue >= 0
|
|
65
|
+
? envValue
|
|
66
|
+
: fromConfig;
|
|
31
67
|
if (chosen === undefined || !Number.isFinite(chosen) || chosen < 0)
|
|
32
68
|
return DEFAULT_MAX_QUEUED;
|
|
33
69
|
return Math.min(MAX_QUEUED_CEILING, Math.floor(chosen));
|
|
34
70
|
}
|
|
71
|
+
export const DEFAULT_PERMISSION_POLICY = 'default';
|
|
72
|
+
/**
|
|
73
|
+
* 权限策略解析:启动参数 `--permission-policy` > config.toml 的 `permission_policy` > 默认 `default`。
|
|
74
|
+
*
|
|
75
|
+
* ⚠️ 非法值**回落到 default**(失败关闭)。绝不因为写错一个字符串就静默放开 full。
|
|
76
|
+
*/
|
|
77
|
+
export function resolvePermissionPolicy(fromConfig, override) {
|
|
78
|
+
const pick = override ?? fromConfig;
|
|
79
|
+
return PERMISSION_POLICIES.includes(pick ?? '')
|
|
80
|
+
? pick
|
|
81
|
+
: DEFAULT_PERMISSION_POLICY;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* 外部沙箱声明:只由本机配置/启动参数开启(`--external-sandbox`),平台无权设置。
|
|
85
|
+
* 仅影响 CodeBuddy 的 full 准入。
|
|
86
|
+
*/
|
|
87
|
+
export function resolveExternalSandbox(fromConfig, override) {
|
|
88
|
+
return override === true || fromConfig === true;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* 交互开关(1.0):缺省 **关闭** —— 关闭时不向上游发任何 task_interaction_request,
|
|
92
|
+
* 平台不该等一个永远不会来的问题。开启才允许 permission/form 往返。
|
|
93
|
+
*/
|
|
94
|
+
export function resolveInteractionEnabled(fromConfig, override) {
|
|
95
|
+
return override === true || fromConfig === true;
|
|
96
|
+
}
|
|
97
|
+
/** 单次交互等待上限:默认 15 分钟,夹 10 秒 ~ 2 小时(比任务总超时短才有意义) */
|
|
98
|
+
export const DEFAULT_INTERACTION_TIMEOUT_MS = 15 * 60 * 1000;
|
|
99
|
+
const INTERACTION_TIMEOUT_FLOOR = 10_000;
|
|
100
|
+
const INTERACTION_TIMEOUT_CEILING = 2 * 60 * 60 * 1000;
|
|
101
|
+
export function resolveInteractionTimeoutMs(fromConfig, override) {
|
|
102
|
+
const raw = process.env['AGENT_BRIDGE_INTERACTION_TIMEOUT_MS'];
|
|
103
|
+
const env = raw === undefined || raw.trim() === '' ? undefined : Number(raw);
|
|
104
|
+
const pick = [override, env, fromConfig].find((v) => typeof v === 'number' && Number.isFinite(v));
|
|
105
|
+
if (pick === undefined)
|
|
106
|
+
return DEFAULT_INTERACTION_TIMEOUT_MS;
|
|
107
|
+
return Math.min(INTERACTION_TIMEOUT_CEILING, Math.max(INTERACTION_TIMEOUT_FLOOR, Math.floor(pick)));
|
|
108
|
+
}
|
|
109
|
+
/** 单任务交互次数上限:默认 16,夹 1~256(防上游刷屏把平台打爆) */
|
|
110
|
+
export const DEFAULT_MAX_INTERACTIONS_PER_TASK = 16;
|
|
111
|
+
const MAX_INTERACTIONS_CEILING = 256;
|
|
112
|
+
export function resolveMaxInteractionsPerTask(fromConfig, override) {
|
|
113
|
+
const raw = process.env['AGENT_BRIDGE_MAX_INTERACTIONS_PER_TASK'];
|
|
114
|
+
const env = raw === undefined || raw.trim() === '' ? undefined : Number(raw);
|
|
115
|
+
const pick = [override, env, fromConfig].find((v) => typeof v === 'number' && Number.isFinite(v));
|
|
116
|
+
if (pick === undefined)
|
|
117
|
+
return DEFAULT_MAX_INTERACTIONS_PER_TASK;
|
|
118
|
+
return Math.min(MAX_INTERACTIONS_CEILING, Math.max(1, Math.floor(pick)));
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* 单会话事件保留上限:默认 5000 条,0 = 不裁剪,夹在 0 ~ 100 万。
|
|
122
|
+
*
|
|
123
|
+
* 为什么按条数而不是按天:裁剪必须可预测、无需定时任务,也不能让"这条会话才开了 10 分钟
|
|
124
|
+
* 但 agent 刷了 2 万条日志"把 SQLite 撑爆。按条数能直接约束库大小,语义也更好解释。
|
|
125
|
+
*/
|
|
126
|
+
export const DEFAULT_EVENT_RETENTION_MAX_EVENTS = 5000;
|
|
127
|
+
const EVENT_RETENTION_CEILING = 1_000_000;
|
|
128
|
+
export function resolveEventRetentionMaxEvents(fromConfig, override) {
|
|
129
|
+
const raw = process.env['AGENT_BRIDGE_EVENT_RETENTION_MAX_EVENTS'];
|
|
130
|
+
const env = raw === undefined || raw.trim() === '' ? undefined : Number(raw);
|
|
131
|
+
const pick = [override, env, fromConfig].find((v) => typeof v === 'number' && Number.isFinite(v));
|
|
132
|
+
if (pick === undefined)
|
|
133
|
+
return DEFAULT_EVENT_RETENTION_MAX_EVENTS;
|
|
134
|
+
return Math.min(EVENT_RETENTION_CEILING, Math.max(0, Math.floor(pick)));
|
|
135
|
+
}
|
|
35
136
|
/** diff 上限:默认 256 KB,夹在 1 KB ~ 8 MB(人工审核看不了几 MB,也别让报文失控) */
|
|
36
137
|
export const DEFAULT_DIFF_MAX_BYTES = 256 * 1024;
|
|
37
138
|
const DIFF_MIN_BYTES = 1024;
|
|
38
139
|
const DIFF_MAX_CEILING = 8 * 1024 * 1024;
|
|
140
|
+
/**
|
|
141
|
+
* 持久授权账本开关(1.0):缺省 **关闭**。
|
|
142
|
+
*
|
|
143
|
+
* 理由和交互开关一致 —— 账本会替用户在未来任务里自动点「允许」,
|
|
144
|
+
* 这种能力必须由本机显式打开,不能被平台或用例顺手开启。
|
|
145
|
+
*/
|
|
146
|
+
export function resolveApprovalLedgerEnabled(fromConfig, override) {
|
|
147
|
+
return override === true || fromConfig === true;
|
|
148
|
+
}
|
|
149
|
+
/** 持久授权有效期:默认 7 天,夹 1 分钟 ~ 90 天(永不过期的授权等于没有撤销入口) */
|
|
150
|
+
export function resolveApprovalTtlMs(fromConfig, override) {
|
|
151
|
+
const raw = process.env['AGENT_BRIDGE_APPROVAL_TTL_MS'];
|
|
152
|
+
const env = raw === undefined || raw.trim() === '' ? undefined : Number(raw);
|
|
153
|
+
const pick = [override, env, fromConfig].find((v) => typeof v === 'number' && Number.isFinite(v));
|
|
154
|
+
const ttl = pick === undefined ? DEFAULT_APPROVAL_TTL_MS : pick;
|
|
155
|
+
return Math.min(90 * 24 * 60 * 60 * 1000, Math.max(60_000, Math.floor(ttl)));
|
|
156
|
+
}
|
|
39
157
|
/** 解析顺序:`AGENT_BRIDGE_DIFF_MAX_BYTES` > config.toml 的 `diff_max_bytes` > 默认 256 KB */
|
|
40
158
|
export function resolveDiffMaxBytes(fromConfig) {
|
|
41
159
|
const rawEnv = process.env['AGENT_BRIDGE_DIFF_MAX_BYTES'];
|
|
@@ -75,6 +193,48 @@ export function resolveControlPlane(fromConfig) {
|
|
|
75
193
|
requireToken: fromConfig?.requireToken !== false,
|
|
76
194
|
};
|
|
77
195
|
}
|
|
196
|
+
/**
|
|
197
|
+
* 网关设置解析:`AGENT_BRIDGE_GATEWAY`(1/0,CI 临时开)> config.toml 的 `[gateway]` > 默认(**关闭**)。
|
|
198
|
+
* 默认关闭与控制面同一条红线:不开就多一个字节都不监听。
|
|
199
|
+
*/
|
|
200
|
+
export function resolveGateway(fromConfig) {
|
|
201
|
+
const envOn = process.env['AGENT_BRIDGE_GATEWAY'];
|
|
202
|
+
const envEnabled = envOn === '1' || envOn === 'true' ? true : envOn === '0' || envOn === 'false' ? false : undefined;
|
|
203
|
+
return {
|
|
204
|
+
enabled: envEnabled ?? fromConfig?.enabled === true,
|
|
205
|
+
a2a: fromConfig?.a2a !== false,
|
|
206
|
+
acp: fromConfig?.acp !== false,
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
/** 有界 feed 的默认预算(spec「会话快照、预算与终态收尾」:默认配置,非产品硬上限) */
|
|
210
|
+
export const DEFAULT_FEED_BUDGETS = {
|
|
211
|
+
maxMessages: 200,
|
|
212
|
+
maxSteps: 200,
|
|
213
|
+
maxArtifacts: 50,
|
|
214
|
+
taskMaxBytes: 4 * 1024 * 1024,
|
|
215
|
+
globalMaxBytes: 64 * 1024 * 1024,
|
|
216
|
+
};
|
|
217
|
+
/**
|
|
218
|
+
* 有界 feed 预算解析:未写 `[feed]` 段 = 全默认(生产默认启用);
|
|
219
|
+
* 写了则**逐维覆盖** —— 只调 maxMessages 不会把别的维关掉;单维显式 0 = 关闭该维;
|
|
220
|
+
* 全 0 = 整套关闭。非法值按 0 处理(失败关闭,不误淘汰)。
|
|
221
|
+
*/
|
|
222
|
+
export function resolveFeedBudgets(fromConfig) {
|
|
223
|
+
if (!fromConfig)
|
|
224
|
+
return { ...DEFAULT_FEED_BUDGETS };
|
|
225
|
+
const pick = (value, fallback) => value === undefined
|
|
226
|
+
? fallback
|
|
227
|
+
: Number.isFinite(value) && value >= 0
|
|
228
|
+
? Math.floor(value)
|
|
229
|
+
: 0;
|
|
230
|
+
return {
|
|
231
|
+
maxMessages: pick(fromConfig.maxMessages, DEFAULT_FEED_BUDGETS.maxMessages),
|
|
232
|
+
maxSteps: pick(fromConfig.maxSteps, DEFAULT_FEED_BUDGETS.maxSteps),
|
|
233
|
+
maxArtifacts: pick(fromConfig.maxArtifacts, DEFAULT_FEED_BUDGETS.maxArtifacts),
|
|
234
|
+
taskMaxBytes: pick(fromConfig.taskMaxBytes, DEFAULT_FEED_BUDGETS.taskMaxBytes),
|
|
235
|
+
globalMaxBytes: pick(fromConfig.globalMaxBytes, DEFAULT_FEED_BUDGETS.globalMaxBytes),
|
|
236
|
+
};
|
|
237
|
+
}
|
|
78
238
|
/** 白名单条目规范化:`resolve()` 成绝对路径、丢弃空项、去重;空数组等价于「未配置」。
|
|
79
239
|
* 配置里写相对路径(如 `"./"`)时以启动 bridge 时的 cwd 为基准 —— 规范化一次,避免每次任务都算。
|
|
80
240
|
*/
|
|
@@ -90,6 +250,14 @@ export function normalizeAllowedCwd(value) {
|
|
|
90
250
|
export function stateDir() {
|
|
91
251
|
return dirname(configPath());
|
|
92
252
|
}
|
|
253
|
+
/**
|
|
254
|
+
* Session/Turn/Event SQLite 路径。
|
|
255
|
+
* 默认与配置同目录;`AGENT_BRIDGE_STATE_DB` 仅供测试/运维显式覆盖。
|
|
256
|
+
*/
|
|
257
|
+
export function stateDbPath() {
|
|
258
|
+
const override = process.env['AGENT_BRIDGE_STATE_DB']?.trim();
|
|
259
|
+
return override ? override : join(stateDir(), 'state.db');
|
|
260
|
+
}
|
|
93
261
|
export function configPath() {
|
|
94
262
|
if (process.env.AGENT_BRIDGE_CONFIG)
|
|
95
263
|
return process.env.AGENT_BRIDGE_CONFIG;
|
|
@@ -99,90 +267,361 @@ export function configPath() {
|
|
|
99
267
|
: homedir();
|
|
100
268
|
return join(base, '.agent-bridge', 'config.toml');
|
|
101
269
|
}
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
const controlPlane = {
|
|
124
|
-
enabled: cpEnabled,
|
|
125
|
-
host: typeof cpRaw['host'] === 'string' ? cpRaw['host'] : undefined,
|
|
126
|
-
port: Number.isFinite(Number(cpRaw['port'])) ? Math.floor(Number(cpRaw['port'])) : undefined,
|
|
127
|
-
requireToken: cpRaw['require_token'] !== false,
|
|
128
|
-
};
|
|
129
|
-
return {
|
|
130
|
-
...raw,
|
|
131
|
-
allowedCwd,
|
|
132
|
-
maxTasks,
|
|
133
|
-
maxQueued,
|
|
134
|
-
diffMaxBytes,
|
|
135
|
-
diffDefault,
|
|
136
|
-
requireServiceToken,
|
|
137
|
-
controlPlane,
|
|
138
|
-
};
|
|
139
|
-
}
|
|
140
|
-
catch {
|
|
141
|
-
return null;
|
|
270
|
+
function errorCode(error) {
|
|
271
|
+
return typeof error === 'object' && error !== null && 'code' in error && typeof error.code === 'string'
|
|
272
|
+
? error.code
|
|
273
|
+
: undefined;
|
|
274
|
+
}
|
|
275
|
+
function invalidConfig(file, detail) {
|
|
276
|
+
return new Error(`配置无效:${file}。${detail};请修复该文件后重试。`);
|
|
277
|
+
}
|
|
278
|
+
function optionalString(raw, key, file) {
|
|
279
|
+
const value = raw[key];
|
|
280
|
+
if (value === undefined)
|
|
281
|
+
return undefined;
|
|
282
|
+
if (typeof value !== 'string' || value.trim() === '')
|
|
283
|
+
throw invalidConfig(file, `${key} 必须是非空字符串`);
|
|
284
|
+
return value;
|
|
285
|
+
}
|
|
286
|
+
function optionalNumber(value, name, file, minimum) {
|
|
287
|
+
if (value === undefined)
|
|
288
|
+
return undefined;
|
|
289
|
+
if (typeof value !== 'number' || !Number.isFinite(value) || value < minimum) {
|
|
290
|
+
throw invalidConfig(file, `${name} 必须是不小于 ${minimum} 的有限数字`);
|
|
142
291
|
}
|
|
292
|
+
return Math.floor(value);
|
|
143
293
|
}
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
294
|
+
function optionalBoolean(value, name, file) {
|
|
295
|
+
if (value === undefined)
|
|
296
|
+
return undefined;
|
|
297
|
+
if (typeof value !== 'boolean')
|
|
298
|
+
throw invalidConfig(file, `${name} 必须是布尔值`);
|
|
299
|
+
return value;
|
|
300
|
+
}
|
|
301
|
+
function serializeConfig(cfg) {
|
|
148
302
|
const out = {
|
|
149
303
|
platformUrl: cfg.platformUrl,
|
|
150
304
|
deviceId: cfg.deviceId,
|
|
151
|
-
deviceToken: cfg.deviceToken,
|
|
152
305
|
};
|
|
153
|
-
if (cfg.
|
|
306
|
+
if (cfg.deviceToken !== undefined)
|
|
307
|
+
out['deviceToken'] = cfg.deviceToken;
|
|
308
|
+
if (cfg.refreshToken !== undefined)
|
|
154
309
|
out['refreshToken'] = cfg.refreshToken;
|
|
155
310
|
if (cfg.allowedCwd?.length)
|
|
156
311
|
out['allowed_cwd'] = cfg.allowedCwd;
|
|
157
|
-
if (cfg.maxTasks)
|
|
312
|
+
if (cfg.maxTasks !== undefined)
|
|
158
313
|
out['max_tasks'] = cfg.maxTasks;
|
|
159
314
|
if (cfg.maxQueued !== undefined)
|
|
160
315
|
out['max_queue'] = cfg.maxQueued;
|
|
161
|
-
if (cfg.diffMaxBytes)
|
|
316
|
+
if (cfg.diffMaxBytes !== undefined)
|
|
162
317
|
out['diff_max_bytes'] = cfg.diffMaxBytes;
|
|
163
|
-
if (cfg.diffDefault)
|
|
164
|
-
out['diff_default'] =
|
|
165
|
-
if (cfg.requireServiceToken)
|
|
166
|
-
out['require_service_token'] =
|
|
167
|
-
if (cfg.
|
|
318
|
+
if (cfg.diffDefault !== undefined)
|
|
319
|
+
out['diff_default'] = cfg.diffDefault;
|
|
320
|
+
if (cfg.requireServiceToken !== undefined)
|
|
321
|
+
out['require_service_token'] = cfg.requireServiceToken;
|
|
322
|
+
if (cfg.sessionIdleMs !== undefined)
|
|
323
|
+
out['session_idle_ms'] = cfg.sessionIdleMs;
|
|
324
|
+
if (cfg.permissionPolicy !== undefined)
|
|
325
|
+
out['permission_policy'] = cfg.permissionPolicy;
|
|
326
|
+
if (cfg.externalSandbox !== undefined)
|
|
327
|
+
out['external_sandbox'] = cfg.externalSandbox;
|
|
328
|
+
if (cfg.interactionEnabled !== undefined)
|
|
329
|
+
out['interaction_enabled'] = cfg.interactionEnabled;
|
|
330
|
+
if (cfg.interactionTimeoutMs !== undefined)
|
|
331
|
+
out['interaction_timeout_ms'] = cfg.interactionTimeoutMs;
|
|
332
|
+
if (cfg.maxInteractionsPerTask !== undefined)
|
|
333
|
+
out['max_interactions_per_task'] = cfg.maxInteractionsPerTask;
|
|
334
|
+
if (cfg.eventRetentionMaxEvents !== undefined)
|
|
335
|
+
out['event_retention_max_events'] = cfg.eventRetentionMaxEvents;
|
|
336
|
+
if (cfg.approvalLedgerEnabled !== undefined)
|
|
337
|
+
out['approval_ledger_enabled'] = cfg.approvalLedgerEnabled;
|
|
338
|
+
if (cfg.approvalTtlMs !== undefined)
|
|
339
|
+
out['approval_ttl_ms'] = cfg.approvalTtlMs;
|
|
340
|
+
if (cfg.mcp !== undefined) {
|
|
341
|
+
const servers = {};
|
|
342
|
+
for (const [name, server] of Object.entries(cfg.mcp.servers)) {
|
|
343
|
+
servers[name] = {
|
|
344
|
+
command: server.command,
|
|
345
|
+
...(server.args !== undefined ? { args: server.args } : {}),
|
|
346
|
+
...(server.cwd !== undefined ? { cwd: server.cwd } : {}),
|
|
347
|
+
...(server.env !== undefined ? { env: server.env } : {}),
|
|
348
|
+
};
|
|
349
|
+
}
|
|
350
|
+
out['mcp'] = { servers };
|
|
351
|
+
}
|
|
352
|
+
if (cfg.jwt !== undefined) {
|
|
353
|
+
out['jwt'] = {
|
|
354
|
+
jwks_url: cfg.jwt.jwksUrl,
|
|
355
|
+
...(cfg.jwt.issuer !== undefined ? { issuer: cfg.jwt.issuer } : {}),
|
|
356
|
+
...(cfg.jwt.audience !== undefined ? { audience: cfg.jwt.audience } : {}),
|
|
357
|
+
...(cfg.jwt.tenantClaim !== undefined ? { tenant_claim: cfg.jwt.tenantClaim } : {}),
|
|
358
|
+
...(cfg.jwt.cacheMs !== undefined ? { cache_ms: cfg.jwt.cacheMs } : {}),
|
|
359
|
+
};
|
|
360
|
+
}
|
|
361
|
+
if (cfg.controlPlane !== undefined) {
|
|
362
|
+
const controlPlane = {};
|
|
363
|
+
if (cfg.controlPlane.enabled !== undefined)
|
|
364
|
+
controlPlane['enabled'] = cfg.controlPlane.enabled;
|
|
365
|
+
if (cfg.controlPlane.host !== undefined)
|
|
366
|
+
controlPlane['host'] = cfg.controlPlane.host;
|
|
367
|
+
if (cfg.controlPlane.port !== undefined)
|
|
368
|
+
controlPlane['port'] = cfg.controlPlane.port;
|
|
369
|
+
if (cfg.controlPlane.requireToken !== undefined)
|
|
370
|
+
controlPlane['require_token'] = cfg.controlPlane.requireToken;
|
|
371
|
+
out['control_plane'] = controlPlane;
|
|
372
|
+
}
|
|
373
|
+
if (cfg.gateway !== undefined) {
|
|
374
|
+
const gateway = {};
|
|
375
|
+
if (cfg.gateway.enabled !== undefined)
|
|
376
|
+
gateway['enabled'] = cfg.gateway.enabled;
|
|
377
|
+
if (cfg.gateway.a2a !== undefined)
|
|
378
|
+
gateway['a2a'] = cfg.gateway.a2a;
|
|
379
|
+
if (cfg.gateway.acp !== undefined)
|
|
380
|
+
gateway['acp'] = cfg.gateway.acp;
|
|
381
|
+
out['gateway'] = gateway;
|
|
382
|
+
}
|
|
383
|
+
if (cfg.feed !== undefined) {
|
|
384
|
+
const feed = {};
|
|
385
|
+
if (cfg.feed.maxMessages !== undefined)
|
|
386
|
+
feed['max_messages'] = cfg.feed.maxMessages;
|
|
387
|
+
if (cfg.feed.maxSteps !== undefined)
|
|
388
|
+
feed['max_steps'] = cfg.feed.maxSteps;
|
|
389
|
+
if (cfg.feed.maxArtifacts !== undefined)
|
|
390
|
+
feed['max_artifacts'] = cfg.feed.maxArtifacts;
|
|
391
|
+
if (cfg.feed.taskMaxBytes !== undefined)
|
|
392
|
+
feed['task_max_bytes'] = cfg.feed.taskMaxBytes;
|
|
393
|
+
if (cfg.feed.globalMaxBytes !== undefined)
|
|
394
|
+
feed['global_max_bytes'] = cfg.feed.globalMaxBytes;
|
|
395
|
+
if (Object.keys(feed).length > 0)
|
|
396
|
+
out['feed'] = feed;
|
|
397
|
+
}
|
|
398
|
+
if (cfg.label !== undefined)
|
|
168
399
|
out['label'] = cfg.label;
|
|
169
|
-
if (cfg.boundAt)
|
|
400
|
+
if (cfg.boundAt !== undefined)
|
|
170
401
|
out['boundAt'] = cfg.boundAt;
|
|
171
402
|
const body = stringifyToml(out);
|
|
172
|
-
|
|
403
|
+
return body.endsWith('\n') ? body : `${body}\n`;
|
|
404
|
+
}
|
|
405
|
+
export function loadConfig() {
|
|
406
|
+
const file = configPath();
|
|
407
|
+
let text;
|
|
173
408
|
try {
|
|
174
|
-
|
|
409
|
+
text = readFileSync(file, 'utf8');
|
|
410
|
+
}
|
|
411
|
+
catch (error) {
|
|
412
|
+
if (errorCode(error) === 'ENOENT')
|
|
413
|
+
return null;
|
|
414
|
+
throw new Error(`读取配置失败(${errorCode(error) ?? 'UNKNOWN'}):${file};请检查文件权限和路径后重试。`);
|
|
415
|
+
}
|
|
416
|
+
let raw;
|
|
417
|
+
try {
|
|
418
|
+
raw = parseToml(text);
|
|
419
|
+
}
|
|
420
|
+
catch {
|
|
421
|
+
throw new Error(`解析配置失败:${file};请修复 TOML 语法后重试。`);
|
|
422
|
+
}
|
|
423
|
+
const platformUrl = optionalString(raw, 'platformUrl', file);
|
|
424
|
+
if (!platformUrl)
|
|
425
|
+
throw invalidConfig(file, 'platformUrl 必须是非空字符串');
|
|
426
|
+
const currentId = optionalString(raw, 'deviceId', file);
|
|
427
|
+
const legacyId = optionalString(raw, 'device_id', file);
|
|
428
|
+
if (currentId !== undefined && legacyId !== undefined && currentId !== legacyId) {
|
|
429
|
+
throw invalidConfig(file, 'deviceId 与 legacy device_id 镜像冲突');
|
|
430
|
+
}
|
|
431
|
+
const deviceId = currentId ?? legacyId;
|
|
432
|
+
if (deviceId === undefined)
|
|
433
|
+
throw invalidConfig(file, '缺少 deviceId');
|
|
434
|
+
try {
|
|
435
|
+
assertDeviceId(deviceId);
|
|
175
436
|
}
|
|
176
437
|
catch {
|
|
177
|
-
|
|
438
|
+
throw invalidConfig(file, 'deviceId 格式非法');
|
|
439
|
+
}
|
|
440
|
+
const deviceToken = optionalString(raw, 'deviceToken', file);
|
|
441
|
+
const refreshToken = optionalString(raw, 'refreshToken', file);
|
|
442
|
+
const boundAt = optionalString(raw, 'boundAt', file);
|
|
443
|
+
if (deviceToken === undefined && (refreshToken !== undefined || boundAt !== undefined)) {
|
|
444
|
+
throw invalidConfig(file, 'refreshToken/boundAt 只能与 deviceToken 同时存在');
|
|
445
|
+
}
|
|
446
|
+
const allowedRaw = raw['allowed_cwd'] ?? raw['allowedCwd'];
|
|
447
|
+
if (allowedRaw !== undefined && (!Array.isArray(allowedRaw) || allowedRaw.some((value) => typeof value !== 'string' || value.trim() === ''))) {
|
|
448
|
+
throw invalidConfig(file, 'allowed_cwd 必须是非空字符串数组');
|
|
449
|
+
}
|
|
450
|
+
const allowedCwd = normalizeAllowedCwd(allowedRaw);
|
|
451
|
+
const sessionIdleMs = optionalNumber(raw['session_idle_ms'] ?? raw['sessionIdleMs'], 'session_idle_ms', file, 0);
|
|
452
|
+
const maxTasks = optionalNumber(raw['max_tasks'] ?? raw['maxTasks'], 'max_tasks', file, 1);
|
|
453
|
+
const maxQueued = optionalNumber(raw['max_queue'] ?? raw['maxQueued'], 'max_queue', file, 0);
|
|
454
|
+
const diffMaxBytes = optionalNumber(raw['diff_max_bytes'] ?? raw['diffMaxBytes'], 'diff_max_bytes', file, 1);
|
|
455
|
+
const diffDefault = optionalBoolean(raw['diff_default'] ?? raw['diffDefault'], 'diff_default', file);
|
|
456
|
+
const requireServiceToken = optionalBoolean(raw['require_service_token'] ?? raw['requireServiceToken'], 'require_service_token', file);
|
|
457
|
+
// 权限策略:非法枚举值直接报错,而不是悄悄回落(配置写错应当立刻可见)
|
|
458
|
+
const policyRaw = optionalString(raw, 'permission_policy', file);
|
|
459
|
+
if (policyRaw !== undefined && !PERMISSION_POLICIES.includes(policyRaw)) {
|
|
460
|
+
throw invalidConfig(file, `permission_policy 必须是 ${PERMISSION_POLICIES.join(' | ')},实际为 ${policyRaw}`);
|
|
461
|
+
}
|
|
462
|
+
const permissionPolicy = policyRaw;
|
|
463
|
+
const externalSandbox = optionalBoolean(raw['external_sandbox'] ?? raw['externalSandbox'], 'external_sandbox', file);
|
|
464
|
+
const interactionEnabled = optionalBoolean(raw['interaction_enabled'] ?? raw['interactionEnabled'], 'interaction_enabled', file);
|
|
465
|
+
const interactionTimeoutMs = optionalNumber(raw['interaction_timeout_ms'] ?? raw['interactionTimeoutMs'], 'interaction_timeout_ms', file, 1);
|
|
466
|
+
const maxInteractionsPerTask = optionalNumber(raw['max_interactions_per_task'] ?? raw['maxInteractionsPerTask'], 'max_interactions_per_task', file, 1);
|
|
467
|
+
const eventRetentionMaxEvents = optionalNumber(raw['event_retention_max_events'] ?? raw['eventRetentionMaxEvents'], 'event_retention_max_events', file, 0);
|
|
468
|
+
const approvalLedgerEnabled = optionalBoolean(raw['approval_ledger_enabled'] ?? raw['approvalLedgerEnabled'], 'approval_ledger_enabled', file);
|
|
469
|
+
const approvalTtlMs = optionalNumber(raw['approval_ttl_ms'] ?? raw['approvalTtlMs'], 'approval_ttl_ms', file, 1);
|
|
470
|
+
const label = optionalString(raw, 'label', file);
|
|
471
|
+
const cpValue = raw['control_plane'];
|
|
472
|
+
if (cpValue !== undefined && (typeof cpValue !== 'object' || cpValue === null || Array.isArray(cpValue))) {
|
|
473
|
+
throw invalidConfig(file, 'control_plane 必须是 TOML 表');
|
|
474
|
+
}
|
|
475
|
+
const cpRaw = (cpValue ?? {});
|
|
476
|
+
const flatEnabled = optionalBoolean(raw['control_plane_enabled'], 'control_plane_enabled', file);
|
|
477
|
+
const controlPlane = cpValue !== undefined || flatEnabled !== undefined
|
|
478
|
+
? {
|
|
479
|
+
enabled: optionalBoolean(cpRaw['enabled'], 'control_plane.enabled', file) ?? flatEnabled,
|
|
480
|
+
host: optionalString(cpRaw, 'host', file),
|
|
481
|
+
port: optionalNumber(cpRaw['port'], 'control_plane.port', file, 0),
|
|
482
|
+
requireToken: optionalBoolean(cpRaw['require_token'], 'control_plane.require_token', file),
|
|
483
|
+
}
|
|
484
|
+
: undefined;
|
|
485
|
+
const gwValue = raw['gateway'];
|
|
486
|
+
if (gwValue !== undefined && (typeof gwValue !== 'object' || gwValue === null || Array.isArray(gwValue))) {
|
|
487
|
+
throw invalidConfig(file, 'gateway 必须是 TOML 表');
|
|
178
488
|
}
|
|
489
|
+
const gwRaw = (gwValue ?? {});
|
|
490
|
+
const gateway = gwValue !== undefined
|
|
491
|
+
? {
|
|
492
|
+
enabled: optionalBoolean(gwRaw['enabled'], 'gateway.enabled', file),
|
|
493
|
+
a2a: optionalBoolean(gwRaw['a2a'], 'gateway.a2a', file),
|
|
494
|
+
acp: optionalBoolean(gwRaw['acp'], 'gateway.acp', file),
|
|
495
|
+
}
|
|
496
|
+
: undefined;
|
|
497
|
+
const feedValue = raw['feed'];
|
|
498
|
+
if (feedValue !== undefined && (typeof feedValue !== 'object' || feedValue === null || Array.isArray(feedValue))) {
|
|
499
|
+
throw invalidConfig(file, 'feed 必须是 TOML 表');
|
|
500
|
+
}
|
|
501
|
+
let feed;
|
|
502
|
+
if (feedValue !== undefined) {
|
|
503
|
+
const feedRaw = feedValue;
|
|
504
|
+
const num = (key) => optionalNumber(feedRaw[key], `feed.${key}`, file, 0);
|
|
505
|
+
const anySet = [
|
|
506
|
+
'max_messages',
|
|
507
|
+
'max_steps',
|
|
508
|
+
'max_artifacts',
|
|
509
|
+
'task_max_bytes',
|
|
510
|
+
'global_max_bytes',
|
|
511
|
+
].some((key) => feedRaw[key] !== undefined);
|
|
512
|
+
if (anySet) {
|
|
513
|
+
feed = {
|
|
514
|
+
maxMessages: num('max_messages'),
|
|
515
|
+
maxSteps: num('max_steps'),
|
|
516
|
+
maxArtifacts: num('max_artifacts'),
|
|
517
|
+
taskMaxBytes: num('task_max_bytes'),
|
|
518
|
+
globalMaxBytes: num('global_max_bytes'),
|
|
519
|
+
};
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
const mcpValue = raw['mcp'];
|
|
523
|
+
if (mcpValue !== undefined && (typeof mcpValue !== 'object' || mcpValue === null || Array.isArray(mcpValue))) {
|
|
524
|
+
throw invalidConfig(file, 'mcp 必须是 TOML 表');
|
|
525
|
+
}
|
|
526
|
+
let mcp;
|
|
527
|
+
if (mcpValue !== undefined) {
|
|
528
|
+
const serversValue = mcpValue['servers'];
|
|
529
|
+
if (serversValue !== undefined && (typeof serversValue !== 'object' || serversValue === null || Array.isArray(serversValue))) {
|
|
530
|
+
throw invalidConfig(file, 'mcp.servers 必须是 TOML 表');
|
|
531
|
+
}
|
|
532
|
+
const servers = {};
|
|
533
|
+
for (const [name, value] of Object.entries((serversValue ?? {}))) {
|
|
534
|
+
if (typeof value !== 'object' || value === null || Array.isArray(value)) {
|
|
535
|
+
throw invalidConfig(file, `mcp.servers.${name} 必须是 TOML 表`);
|
|
536
|
+
}
|
|
537
|
+
const server = value;
|
|
538
|
+
const command = optionalString(server, 'command', file);
|
|
539
|
+
if (!command)
|
|
540
|
+
throw invalidConfig(file, `mcp.servers.${name}.command 必须是非空字符串`);
|
|
541
|
+
const argsRaw = server['args'];
|
|
542
|
+
if (argsRaw !== undefined && (!Array.isArray(argsRaw) || argsRaw.some((arg) => typeof arg !== 'string'))) {
|
|
543
|
+
throw invalidConfig(file, `mcp.servers.${name}.args 必须是字符串数组`);
|
|
544
|
+
}
|
|
545
|
+
const envRaw = server['env'];
|
|
546
|
+
if (envRaw !== undefined && (typeof envRaw !== 'object' || envRaw === null || Array.isArray(envRaw))) {
|
|
547
|
+
throw invalidConfig(file, `mcp.servers.${name}.env 必须是 TOML 表`);
|
|
548
|
+
}
|
|
549
|
+
const env = {};
|
|
550
|
+
for (const [key, val] of Object.entries((envRaw ?? {}))) {
|
|
551
|
+
if (typeof val !== 'string')
|
|
552
|
+
throw invalidConfig(file, `mcp.servers.${name}.env.${key} 必须是字符串`);
|
|
553
|
+
env[key] = val;
|
|
554
|
+
}
|
|
555
|
+
servers[name] = {
|
|
556
|
+
command,
|
|
557
|
+
...(Array.isArray(argsRaw) ? { args: argsRaw } : {}),
|
|
558
|
+
...(optionalString(server, 'cwd', file) ? { cwd: optionalString(server, 'cwd', file) } : {}),
|
|
559
|
+
...(Object.keys(env).length > 0 ? { env } : {}),
|
|
560
|
+
};
|
|
561
|
+
}
|
|
562
|
+
mcp = { servers };
|
|
563
|
+
}
|
|
564
|
+
const jwtValue = raw['jwt'];
|
|
565
|
+
if (jwtValue !== undefined && (typeof jwtValue !== 'object' || jwtValue === null || Array.isArray(jwtValue))) {
|
|
566
|
+
throw invalidConfig(file, 'jwt 必须是 TOML 表');
|
|
567
|
+
}
|
|
568
|
+
let jwt;
|
|
569
|
+
if (jwtValue !== undefined) {
|
|
570
|
+
const jwtRaw = jwtValue;
|
|
571
|
+
const jwksUrl = optionalString(jwtRaw, 'jwks_url', file);
|
|
572
|
+
if (!jwksUrl)
|
|
573
|
+
throw invalidConfig(file, 'jwt.jwks_url 必须是非空字符串');
|
|
574
|
+
jwt = {
|
|
575
|
+
jwksUrl,
|
|
576
|
+
issuer: optionalString(jwtRaw, 'issuer', file),
|
|
577
|
+
audience: optionalString(jwtRaw, 'audience', file),
|
|
578
|
+
tenantClaim: optionalString(jwtRaw, 'tenant_claim', file),
|
|
579
|
+
cacheMs: optionalNumber(jwtRaw['cache_ms'], 'jwt.cache_ms', file, 0),
|
|
580
|
+
};
|
|
581
|
+
}
|
|
582
|
+
return {
|
|
583
|
+
platformUrl,
|
|
584
|
+
deviceId,
|
|
585
|
+
...(deviceToken !== undefined ? { deviceToken } : {}),
|
|
586
|
+
...(refreshToken !== undefined ? { refreshToken } : {}),
|
|
587
|
+
...(allowedCwd !== undefined ? { allowedCwd } : {}),
|
|
588
|
+
...(maxTasks !== undefined ? { maxTasks } : {}),
|
|
589
|
+
...(maxQueued !== undefined ? { maxQueued } : {}),
|
|
590
|
+
...(diffMaxBytes !== undefined ? { diffMaxBytes } : {}),
|
|
591
|
+
...(diffDefault !== undefined ? { diffDefault } : {}),
|
|
592
|
+
...(requireServiceToken !== undefined ? { requireServiceToken } : {}),
|
|
593
|
+
...(sessionIdleMs !== undefined ? { sessionIdleMs } : {}),
|
|
594
|
+
...(permissionPolicy !== undefined ? { permissionPolicy } : {}),
|
|
595
|
+
...(externalSandbox !== undefined ? { externalSandbox } : {}),
|
|
596
|
+
...(interactionEnabled !== undefined ? { interactionEnabled } : {}),
|
|
597
|
+
...(interactionTimeoutMs !== undefined ? { interactionTimeoutMs } : {}),
|
|
598
|
+
...(maxInteractionsPerTask !== undefined ? { maxInteractionsPerTask } : {}),
|
|
599
|
+
...(eventRetentionMaxEvents !== undefined ? { eventRetentionMaxEvents } : {}),
|
|
600
|
+
...(approvalLedgerEnabled !== undefined ? { approvalLedgerEnabled } : {}),
|
|
601
|
+
...(approvalTtlMs !== undefined ? { approvalTtlMs } : {}),
|
|
602
|
+
...(mcp !== undefined ? { mcp } : {}),
|
|
603
|
+
...(jwt !== undefined ? { jwt } : {}),
|
|
604
|
+
...(controlPlane !== undefined ? { controlPlane } : {}),
|
|
605
|
+
...(gateway !== undefined ? { gateway } : {}),
|
|
606
|
+
...(feed !== undefined ? { feed } : {}),
|
|
607
|
+
...(label !== undefined ? { label } : {}),
|
|
608
|
+
...(boundAt !== undefined ? { boundAt } : {}),
|
|
609
|
+
};
|
|
610
|
+
}
|
|
611
|
+
export async function saveConfig(cfg) {
|
|
612
|
+
const file = configPath();
|
|
613
|
+
await writeAtomicFile(file, serializeConfig(cfg));
|
|
179
614
|
return file;
|
|
180
615
|
}
|
|
181
|
-
export function clearConfig() {
|
|
616
|
+
export async function clearConfig() {
|
|
182
617
|
const file = configPath();
|
|
183
|
-
|
|
618
|
+
const cfg = loadConfig();
|
|
619
|
+
if (!cfg)
|
|
184
620
|
return false;
|
|
185
|
-
|
|
621
|
+
delete cfg.deviceToken;
|
|
622
|
+
delete cfg.refreshToken;
|
|
623
|
+
delete cfg.boundAt;
|
|
624
|
+
await writeAtomicFile(file, serializeConfig(cfg));
|
|
186
625
|
return true;
|
|
187
626
|
}
|
|
188
627
|
/** 只回尾号,用于终端展示 */
|