@fanglinwei/agent-fit 0.0.0-stage → 0.7.1

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.
Files changed (32) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +157 -2
  3. package/VERSION +1 -0
  4. package/adapters/codex/modes.json +360 -0
  5. package/adapters/codex/skills/agent-fit/SKILL.md +118 -0
  6. package/adapters/codex/skills/agent-fit/agents/openai.yaml +6 -0
  7. package/adapters/codex/skills/agent-fit/assets/AGENTS.fragment.md +13 -0
  8. package/adapters/codex/skills/agent-fit/assets/codex/agents/task-delegate.toml +18 -0
  9. package/adapters/codex/skills/agent-fit/assets/codex/agents/task-execute.toml +20 -0
  10. package/adapters/codex/skills/agent-fit/assets/codex/agents/task-review.toml +20 -0
  11. package/adapters/codex/skills/agent-fit/assets/codex/agents/task-scan.toml +20 -0
  12. package/adapters/codex/skills/agent-fit/assets/codex/config.toml +17 -0
  13. package/adapters/codex/skills/agent-fit/references/calibration.md +66 -0
  14. package/adapters/codex/skills/agent-fit/references/configuration.md +38 -0
  15. package/adapters/codex/skills/agent-fit/references/failure-policy.md +26 -0
  16. package/adapters/codex/skills/agent-fit/references/task-routing.md +36 -0
  17. package/bin/agent-fit.mjs +246 -0
  18. package/docs/README.md +15 -0
  19. package/docs/implementation-review.md +119 -0
  20. package/docs/usage.md +458 -0
  21. package/lib/accounts.mjs +129 -0
  22. package/lib/codex.mjs +215 -0
  23. package/lib/install.mjs +733 -0
  24. package/lib/model-history.mjs +93 -0
  25. package/lib/models.mjs +376 -0
  26. package/lib/probe.mjs +186 -0
  27. package/lib/scopes.mjs +113 -0
  28. package/lib/server.mjs +200 -0
  29. package/package.json +15 -4
  30. package/web/app.js +744 -0
  31. package/web/index.html +109 -0
  32. package/web/styles.css +407 -0
package/web/app.js ADDED
@@ -0,0 +1,744 @@
1
+ 'use strict';
2
+
3
+ const LEVELS = ['L1', 'L2', 'L3', 'L4'];
4
+ const LEVEL_LABELS = ['明确执行', '有边界的推理', '复杂推理', '高不确定性推理'];
5
+ const LEVEL_EXAMPLES = ['方法与验收已知,含大型确定性处理', '局部分析、有限方案选择', '多约束、竞争假设与矛盾证据', '检验关键假设、处理强耦合开放问题'];
6
+ const ROUTE_EXAMPLES = ['按明确格式取证、按已批准规则批量替换', '实现边界清楚的功能、修复可复现的局部问题', '跨模块根因分析、状态与生命周期问题', '多机制交互的疑难问题、约束冲突的架构取舍'];
7
+ const TASK_LABELS = { mechanical: '确定性机械任务', bounded: '普通有界任务', scan: '大型有界扫描或审查', deep: '大型确定性深度任务', complex: '有界复杂任务', uncertain: '高歧义、高耦合或高后果', reasoning: '复杂推理或质量验证失败', latency: '明确追求低延迟' };
8
+ const TASK_LEVELS = { mechanical: ['L1'], bounded: ['L2'], scan: ['L1', 'L2'], deep: ['L1', 'L2'], complex: ['L2', 'L3'], uncertain: ['L3', 'L4'], reasoning: ['L3', 'L4'], latency: LEVELS };
9
+ const ROLE_LABELS = { task_scan: '扫描取证', task_execute: '范围内产出', task_review: '独立复核', task_delegate: '明确委派' };
10
+ const CONTRACT_LABELS = { readonly_evidence: '只读取证,保留证据与局部阻断', scoped_output: '只在明确范围内产出,独占写入', readonly_review: '只读独立复核,保留原验收要求', bounded_delegate: '遵守本次明确职责与授权范围' };
11
+ const $ = id => document.getElementById(id);
12
+ const clone = value => JSON.parse(JSON.stringify(value));
13
+ const same = (a, b) => JSON.stringify(a) === JSON.stringify(b);
14
+ const state = { scope: 'global', bootstrap: null, status: null, modes: [], saved: null, draft: null, selected: null, revision: null, busy: false, stale: false, connected: false, result: null, preview: null, restorePreview: null, readTime: null, tab: 'modes', account: null, modelCheck: null, rulesMode: '', rulesRoute: null };
15
+ const SCOPE_LABELS = { global: '全局', project: '当前项目', session: '当前会话' };
16
+ let loginTimer;
17
+ function node(tag, attributes = {}, ...children) {
18
+ const element = document.createElement(tag);
19
+ for (const [name, value] of Object.entries(attributes)) {
20
+ if (name === 'class') element.className = value;
21
+ else if (name === 'text') element.textContent = value;
22
+ else if (name === 'on') for (const [event, handler] of Object.entries(value)) element.addEventListener(event, handler);
23
+ else if (name === 'list') element.setAttribute(name, value);
24
+ else if (name in element && !name.startsWith('aria-')) element[name] = value;
25
+ else element.setAttribute(name, String(value));
26
+ }
27
+ for (const child of children.flat()) if (child !== null && child !== undefined) element.append(typeof child === 'string' ? document.createTextNode(child) : child);
28
+ return element;
29
+ }
30
+ function replace(id, ...children) { $(id).replaceChildren(...children.flat()); }
31
+ function installed() { return state.status?.installation; }
32
+ function activeMode() { return state.status?.mode ?? null; }
33
+ function dirty() { return Boolean(state.saved && state.draft && !same(state.saved.levels, state.draft.levels)); }
34
+ function current() { return state.selected === activeMode()?.id; }
35
+ function custom() { return state.saved?.kind === 'custom'; }
36
+ function now() { return new Date().toLocaleTimeString('zh-CN', { hour: '2-digit', minute: '2-digit', second: '2-digit' }); }
37
+ function pairText(pair) { return pair ? `${pair.model} / ${pair.effort ?? '供应商默认参数'}` : '无备用'; }
38
+ function routePair(pair) {
39
+ const names = { 'gpt-6-luna': 'Luna', 'gpt-6.1-sol': 'Sol 6.1', 'gpt-6-sol': 'Sol 6', 'gpt-6-astra': 'Astra', 'gpt-5.6-sol': 'Sol 5.6' };
40
+ return pair ? `${names[pair.model] ?? pair.model} / ${pair.effort ?? '供应商默认参数'}` : '未配置';
41
+ }
42
+ function primaryPairs(definition) {
43
+ return [...new Map(Object.values(definition.levels).flatMap(level => [level, ...Object.values(level.tasks ?? {})]).map(({ model, effort }) => [JSON.stringify([model, effort]), { model, effort }])).values()];
44
+ }
45
+ function validText(value, maximum) { return typeof value === 'string' && value.length > 0 && value.trim() === value && [...value].length <= maximum && !/[\p{Cc}\p{Cf}\p{Cs}]/u.test(value); }
46
+ function displayValue(value) { return value === null || value === undefined ? '未确认' : typeof value === 'object' ? JSON.stringify(value, null, 2) : String(value); }
47
+
48
+ class ActionError extends Error {
49
+ constructor(code, message, response = null) { super(message); this.code = code; this.response = response; }
50
+ }
51
+ async function request(action, params = {}, allowPartial = false) {
52
+ if (!['account', 'setup', 'restore'].includes(action)) params = { ...params, scope: state.scope };
53
+ let response, payload;
54
+ try {
55
+ response = await fetch('/api/actions', { method: 'POST', credentials: 'same-origin', headers: { 'Content-Type': 'application/json', 'X-Delegation-Token': state.bootstrap.csrf_token }, body: JSON.stringify({ action, params }) });
56
+ payload = await response.json();
57
+ } catch { state.connected = false; throw new ActionError('RESULT_UNKNOWN', '结果暂无法确认。请检查终端中的本地服务并刷新状态;已提交的操作不会自动重发。'); }
58
+ if (!payload || !Number.isInteger(payload.exit_code) || payload.report?.report_schema_version !== 1 || !Array.isArray(payload.report.errors) || payload.report.data !== null && (typeof payload.report.data !== 'object' || Array.isArray(payload.report.data))) {
59
+ const errors = payload?.errors ?? payload?.report?.errors;
60
+ if (Array.isArray(errors) && errors[0]?.code) throw new ActionError(errors[0].code, errors[0].message || '本地服务拒绝了此次操作。');
61
+ if (payload?.error?.code) throw new ActionError(payload.error.code, payload.error.message || '本地服务拒绝了此次操作。');
62
+ throw new ActionError('COMMAND_RESULT_INVALID', '结果暂无法确认:本地服务返回了无法识别的报告。请刷新状态核对。');
63
+ }
64
+ if (!response.ok || payload.exit_code !== 0 || payload.report.errors.length) {
65
+ if (allowPartial && (typeof payload.report.data?.configuration_revision === 'string' || ['applied', 'unchanged'].includes(payload.report.data?.result))) return payload;
66
+ const error = payload.report.errors[0] ?? { code: 'COMMAND_FAILED', message: '操作未完整完成,请查看本次结果。' };
67
+ if (['CSRF_REJECTED', 'SERVICE_CLOSING'].includes(error.code)) state.connected = false;
68
+ throw new ActionError(error.code, error.message, payload);
69
+ }
70
+ return payload;
71
+ }
72
+ function showError(error) {
73
+ const message = error.message || '操作未完成,请刷新状态核对。';
74
+ replace('error-content', node('p', {}, message), node('p', { class: 'muted' }, `诊断:${error.code || 'UNKNOWN'}`));
75
+ $('compare').hidden = !state.stale;
76
+ $('error').hidden = false;
77
+ $('error').focus();
78
+ }
79
+ function clearError() { $('error').hidden = true; $('compare').hidden = true; }
80
+ function busy(value, text = '') {
81
+ state.busy = value;
82
+ $('progress').hidden = !value;
83
+ $('progress').textContent = text;
84
+ renderControls();
85
+ for (const button of $('dialog-actions').querySelectorAll('button')) button.disabled = value;
86
+ }
87
+ function writeBlocked() {
88
+ const integrity = state.status?.integrity;
89
+ return !state.connected || !state.revision || state.stale || integrity === 'drifted' || installed() === 'invalid' || (state.status?.diagnostics ?? []).some(item => item.status === 'fail' || ['RULES_SHADOWED', 'ADAPTER_UNSUPPORTED'].includes(item.code));
90
+ }
91
+
92
+ async function readBatch(preferred) {
93
+ for (let attempt = 0; attempt < 2; attempt++) {
94
+ try {
95
+ const status = (await request('status', { details: true }, true)).report.data;
96
+ const listing = (await request('list', {}, true)).report.data;
97
+ if (!status || !Array.isArray(listing?.modes)) throw new ActionError('COMMAND_RESULT_INVALID', '配置查询缺少状态或档位列表。');
98
+ const active = status.mode?.id;
99
+ const selected = listing.modes.some(mode => mode.id === preferred) ? preferred : active ?? listing.modes.find(mode => mode.id === 'official')?.id ?? listing.modes[0]?.id;
100
+ const shown = selected ? (await request('show', { mode: selected }, true)).report.data : null;
101
+ const revision = status.configuration_revision;
102
+ if (revision && revision === listing.configuration_revision && (!shown || revision === shown.configuration_revision)) {
103
+ if (shown && (!shown.definition?.levels || LEVELS.some(level => !shown.definition.levels[level]))) throw new ActionError('COMMAND_RESULT_INVALID', '档位报告缺少完整 L1–L4 定义。');
104
+ return { status, modes: listing.modes, selected, saved: shown?.definition ?? null, revision };
105
+ }
106
+ } catch (error) { if (error.code !== 'BUSY' || attempt === 1) throw error; }
107
+ }
108
+ throw new ActionError('BUSY', '配置在查询期间发生变化。已保留上次结果与草稿,请稍后刷新。');
109
+ }
110
+ async function refresh({ selected = state.selected, compare = false, preserve = true } = {}) {
111
+ if (state.busy || !state.connected) return false;
112
+ const hasDraft = preserve && dirty();
113
+ const oldRevision = state.revision;
114
+ const focused = document.activeElement;
115
+ const focusedId = focused?.id;
116
+ const selection = focused instanceof HTMLInputElement || focused instanceof HTMLTextAreaElement ? [focused.selectionStart, focused.selectionEnd] : null;
117
+ busy(true, '正在按序读取状态、档位列表与完整定义…');
118
+ try {
119
+ const batch = await readBatch(selected);
120
+ state.status = batch.status; state.modes = batch.modes;
121
+ state.readTime = now();
122
+ if (hasDraft && batch.revision !== oldRevision && !compare) {
123
+ state.stale = true;
124
+ showError(new ActionError('CONFIG_CHANGED', '配置已被其他操作更新。草稿已保留;点击“刷新后比较”重新查看差异,再决定是否保存。'));
125
+ } else if (hasDraft && batch.selected !== state.selected) {
126
+ state.stale = true;
127
+ showError(new ActionError('CONFIG_CHANGED', '正在编辑的自定义档位已不存在。草稿仍保留,但无法保存;取消修改后重新选择档位。'));
128
+ } else {
129
+ state.selected = batch.selected;
130
+ state.saved = batch.saved;
131
+ if (!hasDraft) state.draft = batch.saved ? clone(batch.saved) : null;
132
+ state.revision = batch.revision;
133
+ state.stale = false;
134
+ clearError();
135
+ }
136
+ if (oldRevision !== batch.revision) { state.restorePreview = null; state.preview = null; }
137
+ render();
138
+ return true;
139
+ } catch (error) { showError(error); return false; }
140
+ finally {
141
+ busy(false);
142
+ if ($('error').hidden && focusedId && $(focusedId)) {
143
+ $(focusedId).focus();
144
+ if (selection && ($(focusedId) instanceof HTMLInputElement || $(focusedId) instanceof HTMLTextAreaElement) && selection[0] !== null) $(focusedId).setSelectionRange(...selection);
145
+ }
146
+ }
147
+ }
148
+
149
+ function commandArguments(action, params) {
150
+ const args = [];
151
+ if (['set', 'create', 'configure'].includes(action)) args.push('mode', action);
152
+ else args.push(action);
153
+ args.push(`--codex-home=${state.bootstrap?.home ?? ''}`, '--json');
154
+ if (!['setup', 'restore'].includes(action)) {
155
+ args.push(`--scope=${state.scope}`, `--project=${state.bootstrap?.project ?? ''}`);
156
+ if (state.bootstrap?.thread) args.push(`--thread=${state.bootstrap.thread}`);
157
+ }
158
+ if (params.expectRevision) args.push(`--expect-revision=${params.expectRevision}`);
159
+ if (params.dryRun) args.push('--dry-run');
160
+ if (action === 'setup' && params.mode) args.push(`--mode=${params.mode}`);
161
+ if (action === 'configure') args.push('--definition-stdin');
162
+ if (action === 'create') args.push(`--from=${params.from}`);
163
+ if (action === 'explain') args.push(`--mode=${params.mode}`, `--level=${params.level}`, `--role=${params.role}`);
164
+ if (action === 'explain' && params.task) args.push(`--task=${params.task}`);
165
+ if (['set', 'configure', 'show'].includes(action)) args.push('--', params.mode);
166
+ if (action === 'create') args.push('--', params.name);
167
+ return args;
168
+ }
169
+ function commandText(argv) { return ['agent-fit', ...argv.map(value => "'" + String(value).replaceAll("'", "'\\''") + "'")].join(' '); }
170
+ function nextWrite() {
171
+ if (!state.saved) return null;
172
+ const params = { mode: state.selected, expectRevision: state.revision };
173
+ if (installed() === 'not_installed') return { action: 'setup', params };
174
+ if (dirty()) return { action: 'configure', params: { ...params, definition: { levels: clone(state.draft.levels) } } };
175
+ return { action: 'set', params };
176
+ }
177
+ function validateDraft() {
178
+ const errors = [];
179
+ if (!state.draft) return false;
180
+ for (const level of LEVELS) {
181
+ const pair = state.draft.levels[level];
182
+ let message = '';
183
+ let target = `${level}-model`;
184
+ if (!validText(pair.model, 200)) message = '主模型必填,须为 1–200 个字符,不能含控制字符或首尾空白。';
185
+ else if (!validText(pair.effort, 40)) message = '推理强度必填,须为 1–40 个字符,不能含控制字符或首尾空白。';
186
+ else if (pair.fallback && (!validText(pair.fallback.model, 200) || !validText(pair.fallback.effort, 40))) message = '备用模型与推理强度都必须完整填写,或关闭备用。';
187
+ else if (pair.fallback && pair.fallback.model === pair.model && pair.fallback.effort === pair.effort) message = '备用组合须与主模型组合不同。';
188
+ if (!message && pair.tasks !== undefined) {
189
+ if (!pair.tasks || typeof pair.tasks !== 'object' || Array.isArray(pair.tasks) || Object.entries(pair.tasks).some(([task, candidate]) => !Object.hasOwn(TASK_LEVELS, task) || !TASK_LEVELS[task].includes(level) || !candidate || typeof candidate !== 'object' || Array.isArray(candidate) || Object.keys(candidate).length !== 3 || !['model', 'effort', 'fallback'].every(key => Object.hasOwn(candidate, key)) || !validText(candidate.model, 200) || !validText(candidate.effort, 40) || candidate.fallback !== null && (!candidate.fallback || Object.keys(candidate.fallback).length !== 2 || !validText(candidate.fallback.model, 200) || !validText(candidate.fallback.effort, 40) || candidate.fallback.model === candidate.model && candidate.fallback.effort === candidate.effort))) message = '任务候选须为有效 JSON 对象,类型须适用本等级,每项提供 model、effort 和 fallback(可为 null),备用须不同。';
190
+ if (message) target = `${level}-tasks`;
191
+ }
192
+ const messageNode = $(`${level}-error`);
193
+ if (messageNode) { messageNode.textContent = message; messageNode.hidden = !message; }
194
+ for (const field of document.querySelectorAll(`[data-level="${level}"]`)) field.setAttribute('aria-invalid', String(Boolean(message)));
195
+ if (message) errors.push({ level, message, target });
196
+ }
197
+ if (errors.length) {
198
+ replace('error-content', node('p', {}, '请先修正以下等级,输入内容已保留。'), node('ul', {}, errors.map(({ level, message, target }) => node('li', {}, node('a', { href: `#${target}`, on: { click: event => { event.preventDefault(); $(`level-${level}`).open = true; const field = $(target); const details = field.closest('.task-candidates'); if (details) details.open = true; field.focus(); } } }, `${level}:${message}`)))));
199
+ $('error').hidden = false; $('error').focus(); return false;
200
+ }
201
+ return true;
202
+ }
203
+ function resultTitle(data) {
204
+ if (!data) return '本次操作未完成。';
205
+ if (data.result === 'preview') return '预览完成,尚未写入配置。';
206
+ if (data.result === 'unchanged') return '无配置变化。';
207
+ if (data.configuration_status === 'rolled_back') return '配置未应用,变更已回退。';
208
+ if (data.configuration_status === 'partial') return '操作未完整完成,仍有残留需要核对。';
209
+ if (data.result === 'unknown' || data.configuration_status === 'unknown') return '结果暂无法确认,请刷新状态核对。';
210
+ if (data.configuration_status === 'not_written') return '操作未应用,配置未写入。';
211
+ if (data.configuration_status === 'written') {
212
+ if (data.action === 'restore') return '原配置已恢复。';
213
+ if (data.action === 'create') return '档位已创建,尚未设为当前。';
214
+ if (data.restart?.reason === 'definition_only') return '档位定义已保存,尚未应用。';
215
+ return '配置已写入。';
216
+ }
217
+ return '操作结果已返回。';
218
+ }
219
+ function renderResult() {
220
+ if (!state.result) return;
221
+ const response = state.result;
222
+ const data = response.report.data;
223
+ const contents = [node('p', {}, resultTitle(data))];
224
+ if (data?.restart?.message) contents.push(node('p', {}, data.restart.message));
225
+ for (const warning of data?.warnings ?? []) contents.push(node('p', { class: 'muted' }, typeof warning === 'string' ? warning : warning.message ?? warning.code));
226
+ contents.push(node('details', {}, node('summary', {}, '本次执行结果与命令'), node('p', { class: 'muted' }, `退出码:${response.exit_code};以下命令按 POSIX 终端引用参数。`), node('pre', {}, commandText(response.command?.argv ?? [])), node('details', {}, node('summary', {}, '完整参数数组'), node('pre', {}, JSON.stringify(response.command?.argv ?? [], null, 2))), node('pre', {}, JSON.stringify(data, null, 2))));
227
+ replace('result', contents); $('result').hidden = false;
228
+ }
229
+ async function performWrite(action, params, { preview = false } = {}) {
230
+ if (state.busy || (action === 'restore' ? !state.connected || !state.revision || state.stale : writeBlocked())) return false;
231
+ busy(true, preview ? '正在执行 CLI 预检与文件变更预览…' : action === 'restore' ? '正在恢复原配置,请等待安全完成…' : '正在保存配置,请等待…');
232
+ clearError();
233
+ try {
234
+ const response = await request(action, { ...params, ...(preview ? { dryRun: true } : {}) }, !preview);
235
+ if (preview) {
236
+ if (action === 'restore') { state.restorePreview = { response, revision: state.revision }; renderRestore(); }
237
+ else { state.preview = { response, definition: clone(state.draft), revision: state.revision }; renderPreview(); }
238
+ return true;
239
+ }
240
+ state.result = response; renderResult();
241
+ const data = response.report.data;
242
+ if (!['applied', 'unchanged'].includes(data?.result)) return false;
243
+ state.preview = null; state.restorePreview = null;
244
+ if (data.created_mode) state.selected = data.created_mode.id;
245
+ if (['restore', 'clear', 'delete'].includes(action)) state.selected = null;
246
+ if (action === 'restore') state.scope = 'global';
247
+ state.draft = state.saved ? clone(state.saved) : null;
248
+ busy(false);
249
+ await refresh({ preserve: false });
250
+ return true;
251
+ } catch (error) {
252
+ if (error.response) { state.result = error.response; renderResult(); }
253
+ if (error.code === 'CONFIG_CHANGED') state.stale = true;
254
+ if (['RESULT_UNKNOWN', 'COMMAND_RESULT_INVALID'].includes(error.code)) state.stale = true;
255
+ showError(error);
256
+ return false;
257
+ } finally { busy(false); }
258
+ }
259
+ async function apply(preview = false) {
260
+ const operation = nextWrite();
261
+ if (!operation || dirty() && !validateDraft()) return false;
262
+ return performWrite(operation.action, operation.params, { preview });
263
+ }
264
+
265
+ function dialog({ title, description, content = [], choices }) {
266
+ const previous = document.activeElement;
267
+ $('dialog-title').textContent = title; $('dialog-description').textContent = description;
268
+ replace('dialog-content', content);
269
+ return new Promise(resolve => {
270
+ replace('dialog-actions', choices.map(choice => node('button', { type: 'button', class: choice.class || 'secondary', on: { click: async () => { if (state.busy) return; if (choice.validate && !choice.validate()) return; if (choice.run) { await choice.run(); return; } finish(choice.value); } } }, choice.label)));
271
+ const finish = value => { $('confirm-dialog').removeEventListener('cancel', cancel); $('confirm-dialog').close(); if (previous?.isConnected) previous.focus(); resolve(value); };
272
+ const cancel = event => { event.preventDefault(); if (!state.busy) finish(null); };
273
+ $('confirm-dialog').addEventListener('cancel', cancel);
274
+ $('confirm-dialog').showModal();
275
+ $('dialog-actions').querySelector('button')?.focus();
276
+ });
277
+ }
278
+ async function leaveDraft(callback) {
279
+ if (!dirty()) { await callback(); return; }
280
+ const answer = await dialog({ title: '保留还是放弃本次修改?', description: '草稿尚未保存。放弃只清除本页输入,不会删除已创建的档位。', choices: [{ label: '继续编辑', value: null }, { label: '放弃修改并离开', value: 'discard' }, { label: current() ? '保存并应用后离开' : '保存档位后离开', value: 'save', class: 'primary' }] });
281
+ if (answer === 'discard') { state.draft = clone(state.saved); state.preview = null; if (state.stale) await refresh({ preserve: false }); else render(); await callback(); }
282
+ if (answer === 'save' && await apply()) await callback();
283
+ }
284
+ async function selectMode(id) {
285
+ if (state.busy || id === state.selected) return;
286
+ await leaveDraft(() => refresh({ selected: id, preserve: false }));
287
+ renderModePicker();
288
+ }
289
+ function showTab(tab) {
290
+ state.tab = ['modes', 'rules', 'accounts', 'restore'].includes(tab) ? tab : 'modes';
291
+ for (const item of ['modes', 'rules', 'accounts', 'restore']) $(item).hidden = item !== state.tab;
292
+ for (const link of document.querySelectorAll('.tabs a')) {
293
+ if (link.hash === `#${state.tab}`) link.setAttribute('aria-current', 'page'); else link.removeAttribute('aria-current');
294
+ }
295
+ history.replaceState(null, '', `#${state.tab}`);
296
+ if (state.tab === 'accounts' && state.connected && !state.busy) void accountAction({ operation: 'status' });
297
+ }
298
+ function renderModePicker() {
299
+ const focusedMode = $('mode-radios').contains(document.activeElement) ? document.activeElement.id : null;
300
+ const isCustom = state.modes.find(mode => mode.id === state.selected)?.kind === 'custom';
301
+ const descriptions = { cut: '节省优先,按任务选同级候选', official: '四级基线与任务候选', high: '选档即授权最高强度,含 Astra/ultra', fire: '选档即授权 Astra/ultra,仍须先验证', custom: '自行设置模型、候选与备用' };
302
+ const radios = state.modes.filter(mode => mode.kind !== 'custom').map(mode => ({ value: mode.id, label: `${mode.name} · ${mode.id}` }));
303
+ radios.push({ value: 'custom', label: '自定义' });
304
+ replace('mode-radios', radios.map(option => node('label', {}, node('input', { id: `mode-${option.value}`, type: 'radio', name: 'mode', value: option.value, 'aria-label': option.label, 'aria-describedby': `mode-${option.value}-note`, checked: option.value === 'custom' ? isCustom : state.selected === option.value, disabled: state.busy || !state.connected, on: { change: () => { if (option.value !== 'custom') selectMode(option.value); else { const target = state.modes.find(mode => mode.kind === 'custom'); if (target) selectMode(target.id); else { renderModePicker(); createMode(); } } } } }), node('span', { class: 'mode-name' }, option.label), node('span', { id: `mode-${option.value}-note`, class: 'mode-description' }, descriptions[option.value] ?? '查看模型分配'))));
305
+ $('custom-picker').hidden = !isCustom;
306
+ replace('custom-mode', state.modes.filter(mode => mode.kind === 'custom').map(mode => node('option', { value: mode.id, selected: mode.id === state.selected }, mode.name)));
307
+ if (focusedMode) $(focusedMode)?.focus();
308
+ }
309
+ function field(level, name, value, fallback = false) {
310
+ const id = `${level}-${fallback ? 'fallback-' : ''}${name}`;
311
+ return node('div', {}, node('label', { htmlFor: id }, `${fallback ? '备用' : '主'}${name === 'model' ? '模型' : '推理强度'}`), node('input', { id, value, ...(name === 'effort' ? { list: 'effort-suggestions' } : {}), maxLength: name === 'model' ? 400 : 80, autocomplete: 'off', 'data-level': level, 'aria-describedby': `${level}-error`, on: { input: event => {
312
+ const pair = fallback ? state.draft.levels[level].fallback : state.draft.levels[level];
313
+ pair[name] = event.target.value;
314
+ state.preview = null;
315
+ updateLevelSummary(level); renderChanges(); renderControls(); renderPreview();
316
+ $(`${level}-error`).hidden = true; event.target.removeAttribute('aria-invalid');
317
+ } } }));
318
+ }
319
+ const desktop = matchMedia('(min-width: 768px)');
320
+ function renderLevels() {
321
+ replace('levels');
322
+ if (!state.draft) return;
323
+ LEVELS.forEach((level, index) => {
324
+ const pair = state.draft.levels[level];
325
+ const fallbackText = !custom() && state.saved.id === 'fire' ? '用户指定,否则继承父代理' : pairText(pair.fallback);
326
+ const summary = node('summary', { on: { click: event => { if (desktop.matches) event.preventDefault(); } } }, node('span', { class: 'level-code' }, level), node('span', { class: 'level-name' }, LEVEL_LABELS[index]), node('span', { id: `${level}-changed`, class: 'tag', hidden: true }, '已修改'), node('span', { class: 'level-example' }, LEVEL_EXAMPLES[index]), node('span', { id: `${level}-summary`, class: 'level-summary' }, pairText(pair)));
327
+ const fields = node('div', { class: 'level-fields' });
328
+ if (custom()) fields.append(node('div', { class: 'field-grid' }, field(level, 'model', pair.model), field(level, 'effort', pair.effort)));
329
+ else fields.append(node('div', { class: 'field-grid' }, node('div', {}, node('p', { class: 'muted' }, '主模型'), node('p', { class: 'readonly-value model-value' }, pair.model)), node('div', {}, node('p', { class: 'muted' }, '推理强度'), node('p', { class: 'readonly-value effort-value' }, pair.effort))));
330
+ fields.append(node('p', { id: `${level}-error`, class: 'field-error', hidden: true }));
331
+ const fallbackFields = node('div', { id: `${level}-fallback-fields`, class: 'field-grid', hidden: !pair.fallback });
332
+ const fallbackDetails = node('details', { class: 'fallback' }, node('summary', { id: `${level}-fallback-summary` }, `备用:${fallbackText}`));
333
+ if (custom()) {
334
+ if (pair.fallback) fallbackFields.append(field(level, 'model', pair.fallback.model, true), field(level, 'effort', pair.fallback.effort, true));
335
+ fallbackDetails.append(node('label', { class: 'enable' }, node('input', { type: 'checkbox', checked: Boolean(pair.fallback), on: { change: event => {
336
+ state.draft.levels[level].fallback = event.target.checked ? { model: '', effort: '' } : null;
337
+ fallbackFields.hidden = !event.target.checked;
338
+ fallbackFields.replaceChildren();
339
+ if (event.target.checked) fallbackFields.append(field(level, 'model', '', true), field(level, 'effort', '', true));
340
+ state.preview = null; updateLevelSummary(level); renderChanges(); renderControls(); renderPreview();
341
+ } } }), '启用一个备用组合'), fallbackFields);
342
+ } else fallbackDetails.append(node('p', { class: 'muted' }, fallbackText));
343
+ fields.append(node('div', { class: 'level-meta' }, fallbackDetails));
344
+ const tasks = pair.tasks;
345
+ if (custom() || tasks && typeof tasks === 'object' && Object.keys(tasks).length) {
346
+ const candidates = node('details', { class: 'technical task-candidates' }, node('summary', {}, '同级任务候选'), node('p', {}, '先选复杂度与保障下限,再按任务特征选择;未指定任务类型时使用上方基线。'));
347
+ if (custom()) {
348
+ const id = `${level}-tasks`;
349
+ candidates.append(node('label', { htmlFor: id }, '任务候选 JSON'), node('p', { class: 'muted' }, `本等级支持:${Object.entries(TASK_LEVELS).filter(([, levels]) => levels.includes(level)).map(([task]) => task).join('、')}。每项包含 model、effort、fallback;{} 清空候选。明确保存并选择的组合即为授权,含高强度 Astra。`), node('textarea', { id, value: typeof tasks === 'string' ? tasks : JSON.stringify(tasks ?? {}, null, 2), rows: 8, maxLength: 65536, spellcheck: false, 'data-level': level, 'aria-describedby': `${level}-error`, on: { input: event => {
350
+ try { const parsed = JSON.parse(event.target.value); state.draft.levels[level].tasks = parsed && typeof parsed === 'object' && !Array.isArray(parsed) ? parsed : event.target.value; } catch { state.draft.levels[level].tasks = event.target.value; }
351
+ state.preview = null; updateLevelSummary(level); renderChanges(); renderControls(); renderPreview();
352
+ $(`${level}-error`).hidden = true; event.target.removeAttribute('aria-invalid');
353
+ } } }));
354
+ } else candidates.append(node('ul', {}, Object.entries(tasks).map(([task, candidate]) => node('li', {}, `${TASK_LABELS[task] ?? task}:${pairText(candidate)};备用:${pairText(candidate.fallback)}`))));
355
+ fields.append(candidates);
356
+ }
357
+ $('levels').append(node('details', { id: `level-${level}`, class: `level${custom() ? '' : ' readonly'}`, open: desktop.matches || index === 0 }, summary, fields));
358
+ updateLevelSummary(level);
359
+ });
360
+ }
361
+ function updateLevelSummary(level) {
362
+ const pair = state.draft?.levels[level];
363
+ if (!pair || !$(`${level}-summary`)) return;
364
+ const changed = !same(pair, state.saved.levels[level]);
365
+ $(`level-${level}`).classList.toggle('changed', changed);
366
+ $(`${level}-changed`).hidden = !changed;
367
+ $(`${level}-summary`).textContent = pairText(pair);
368
+ $(`${level}-fallback-summary`).textContent = `备用:${!custom() && state.saved.id === 'fire' ? '用户指定,否则继承父代理' : pairText(pair.fallback)}`;
369
+ }
370
+ function renderChanges() {
371
+ if (!state.draft) { $('changes-count').textContent = '暂无可编辑的定义'; replace('change-list'); return; }
372
+ const setup = installed() === 'not_installed';
373
+ const old = dirty() || current() ? state.saved : activeMode();
374
+ const changes = LEVELS.filter(level => setup || !same(old?.levels?.[level], state.draft.levels[level]));
375
+ $('changes-count').textContent = changes.length ? `${setup ? '将配置' : '变化涉及'} ${changes.length} 个任务等级` : current() ? '尚未修改' : '模型组合不变,将切换档位';
376
+ replace('change-list', changes.map(level => {
377
+ const before = old?.levels?.[level]; const after = state.draft.levels[level];
378
+ return node('div', { class: 'change-item' }, node('h4', {}, `${level} · ${LEVEL_LABELS[LEVELS.indexOf(level)]}`), before ? node('p', { class: 'change-before' }, `已保存:${pairText(before)}`) : null, node('p', { class: 'change-after' }, `将使用:${pairText(after)}`), !same(before?.fallback, after.fallback) || setup ? node('p', {}, before ? `备用:${pairText(before.fallback)} → ${pairText(after.fallback)}` : `备用:${pairText(after.fallback)}`) : null, !same(before?.tasks, after.tasks) ? node('details', {}, node('summary', {}, '任务候选变化'), node('pre', {}, `原候选:${JSON.stringify(before?.tasks ?? {}, null, 2)}\n新候选:${JSON.stringify(after.tasks ?? {}, null, 2)}`)) : null);
379
+ }));
380
+ if (!changes.length) replace('change-list', node('div', { class: 'empty-changes' }, node('span', { 'aria-hidden': true }, '✓'), node('p', {}, current() ? '没有待应用的变更' : `将切换至「${state.saved.name}」`), node('p', {}, current() ? '可重新设置当前档位,也可选择其他档位。' : '模型组合与当前档位相同。')));
381
+ $('save-note').textContent = state.stale ? '草稿来自旧版本,请刷新后比较。' : dirty() && !current() && !setup ? `保存到${SCOPE_LABELS[state.scope]},随后可单独设为当前档位。` : dirty() && current() ? `完整四级保存到${SCOPE_LABELS[state.scope]};编辑上层定义时先生成局部副本。` : setup ? '点击配置后才写入绑定的 Codex home。' : `仅设置${SCOPE_LABELS[state.scope]}的档位选择。`;
382
+ const operation = nextWrite();
383
+ $('command').textContent = operation ? commandText(commandArguments(operation.action, operation.params)) : '尚未选择配置动作。';
384
+ $('copy-definition').hidden = !custom();
385
+ }
386
+ function renderControls() {
387
+ renderPrimaryCheck();
388
+ const pendingLogin = state.account?.login?.status === 'pending';
389
+ const blocked = state.busy || writeBlocked() || pendingLogin;
390
+ const operation = nextWrite();
391
+ $('draft-label').hidden = !operation;
392
+ $('apply').textContent = state.busy ? '请等待操作完成…' : installed() === 'not_installed' ? '配置 Codex' : dirty() ? current() ? '保存并应用' : '保存档位' : !current() && state.saved ? '设为当前档位' : '重新设置当前档位';
393
+ $('apply').disabled = blocked || !operation;
394
+ $('preview').disabled = blocked || !operation;
395
+ $('check-primary').disabled = blocked || !activeMode() || !current() || dirty();
396
+ $('new-mode').disabled = blocked || installed() !== 'installed';
397
+ $('copy-mode').disabled = blocked || !state.saved || installed() !== 'installed';
398
+ $('delete-mode').disabled = blocked || !custom() || state.saved.definition_scope !== state.scope || dirty();
399
+ $('clear-scope').disabled = blocked || installed() !== 'installed' || !state.status?.selection || dirty();
400
+ $('scope').disabled = state.busy || !state.connected;
401
+ for (const option of $('scope').options) option.disabled = option.value !== 'global' && (installed() !== 'installed' || option.value === 'session' && !state.bootstrap?.thread);
402
+ $('cancel-draft').hidden = !dirty(); $('cancel-draft').disabled = state.busy;
403
+ $('cancel-selection').hidden = !activeMode() || current() || dirty(); $('cancel-selection').disabled = state.busy;
404
+ $('refresh').disabled = state.busy;
405
+ for (const id of ['custom-mode', 'rules-mode', 'edit-routes', 'close-preview', 'compare']) $(id).disabled = state.busy || !state.connected;
406
+ $('explain-role').disabled = state.busy || !state.connected || !state.rulesRoute;
407
+ for (const button of document.querySelectorAll('[data-route], #route-retry')) button.disabled = state.busy || !state.connected || state.stale;
408
+ for (const input of document.querySelectorAll('#levels input, #levels textarea, #mode-radios input')) input.disabled = state.busy || !state.connected;
409
+ for (const input of document.querySelectorAll('#account-refresh, #cancel-login')) input.disabled = state.busy || !state.connected;
410
+ for (const id of ['login-browser', 'login-device', 'login-key']) $(id).disabled = state.busy || !state.connected || pendingLogin;
411
+ $('account-logout').disabled = state.busy || !state.connected || pendingLogin;
412
+ for (const link of document.querySelectorAll('.tabs a')) link.setAttribute('aria-disabled', String(state.busy));
413
+ const restoration = state.status?.restoration?.status;
414
+ $('restore-preview').disabled = state.busy || !state.connected || !state.revision || state.stale || !['available'].includes(restoration);
415
+ $('restore-confirm').disabled = state.busy || !state.restorePreview || state.restorePreview.revision !== state.revision;
416
+ }
417
+ function renderAccount() {
418
+ const data = state.account;
419
+ if (!data) return;
420
+ const labels = { authenticated: '已登录', authentication_required: '需要登录', unknown: '授权状态未确认' };
421
+ const types = { chatgpt: 'ChatGPT', apiKey: 'API Key', amazonBedrock: 'Amazon Bedrock' };
422
+ $('account-status').textContent = `${labels[data.account?.status] ?? '授权状态未确认'}${types[data.account?.type] ? ` · ${types[data.account.type]}` : ''}`;
423
+ const source = { codex_login: '复用 Codex 已保存的登录', provider_environment: '已发现供应商环境变量凭据', provider_config: '已发现供应商配置凭据', not_confirmed: '尚未确认凭据来源' };
424
+ $('account-context').textContent = `配置目录:${state.bootstrap.home};供应商:${data.provider ?? '未确认'}。${source[data.credential_source] ?? '尚未确认凭据来源'}。`;
425
+ $('account-logout').hidden = data.account?.status !== 'authenticated';
426
+ const login = data.login;
427
+ const contents = [];
428
+ if (login?.status === 'pending') {
429
+ contents.push(node('p', {}, '等待官方授权完成。完成后本页会自动更新;也可点击“检查已有授权”。'));
430
+ if (login.user_code) contents.push(node('p', {}, '在官方页面输入设备码:', node('code', { class: 'device-code' }, login.user_code)));
431
+ if (/^https:\/\/(?:auth\.openai\.com|auth0\.openai\.com|chatgpt\.com)\//.test(login.url ?? '')) contents.push(node('a', { href: login.url, target: '_blank', rel: 'noopener noreferrer', class: 'auth-link' }, '打开官方授权页面'));
432
+ contents.push(node('button', { id: 'cancel-login', type: 'button', class: 'secondary', on: { click: () => accountAction({ operation: 'cancel' }) } }, '取消本次授权'), node('p', { class: 'muted' }, `本页等待至 ${new Date(login.expires_at).toLocaleTimeString('zh-CN')}。设备码不可用时,请先在 ChatGPT 安全设置中启用设备码登录。`));
433
+ } else if (login) {
434
+ contents.push(node('p', {}, { succeeded: '登录已完成。', cancelled: '本次授权已取消。', expired: '授权等待已超时,请重新发起登录。', failed: '登录未完成,请重试;设备码登录需在 ChatGPT 安全设置中启用。' }[login.status] ?? '登录状态未确认。'));
435
+ }
436
+ replace('account-login', contents);
437
+ clearTimeout(loginTimer);
438
+ if (login?.status === 'pending') loginTimer = setTimeout(() => {
439
+ if (state.busy || !state.connected) { renderAccount(); return; }
440
+ void accountAction({ operation: 'status' }, true);
441
+ }, 2000);
442
+ }
443
+ async function accountAction(params, polling = false) {
444
+ if (state.busy) return;
445
+ busy(true, polling ? '正在确认授权是否完成…' : '正在处理当前配置目录的账号授权…');
446
+ if (!polling) clearError();
447
+ try {
448
+ const previous = state.account;
449
+ state.account = (await request('account', params)).report.data;
450
+ if (params.operation !== 'status' || previous?.login?.status === 'pending' || !same(previous?.account, state.account.account) || previous?.credential_source !== state.account.credential_source) state.modelCheck = null;
451
+ renderAccount();
452
+ } catch (error) {
453
+ if (polling) {
454
+ if (state.account?.login) state.account.login = { ...state.account.login, status: 'failed', url: null, user_code: null };
455
+ renderAccount(); $('account-status').textContent = error.message;
456
+ }
457
+ else showError(error);
458
+ } finally { busy(false); }
459
+ }
460
+ async function login(method) {
461
+ if (state.busy) return;
462
+ const key = method === 'apiKey' ? node('input', { id: 'login-api-key', type: 'password', autocomplete: 'off', maxLength: 4096 }) : null;
463
+ const error = node('p', { class: 'dialog-error', hidden: true });
464
+ const answer = await dialog({ title: method === 'apiKey' ? '使用 API Key 登录' : '授权当前 Codex 配置目录', description: `凭据由 Codex 官方客户端保存到 ${state.bootstrap.home} 对应的认证存储。此操作可能替换该目录的既有登录,其他使用此目录的 Codex 客户端也会受影响。`, content: key ? [node('label', { htmlFor: 'login-api-key' }, 'OpenAI API Key'), key, error, node('p', { class: 'muted' }, '密钥只传给本地 Codex,不写入页面存储或检查记录。')] : node('p', {}, '发起后在官方页面完成授权,本页会等待并更新登录状态。'), choices: [{ label: '取消', value: null }, { label: key ? '登录' : '发起授权', value: true, class: 'primary', validate: () => {
465
+ if (key && !validText(key.value, 4096)) { error.textContent = '请输入有效 API Key,不含首尾空白或控制字符。'; error.hidden = false; key.focus(); return false; } return true;
466
+ } }] });
467
+ if (answer) {
468
+ const params = { operation: 'login', method };
469
+ if (key) { params.apiKey = key.value; key.value = ''; }
470
+ await accountAction(params);
471
+ delete params.apiKey;
472
+ } else if (key) key.value = '';
473
+ }
474
+ function renderPreview() {
475
+ $('preview-area').hidden = !state.preview;
476
+ if (!state.preview) return;
477
+ const data = state.preview.response.report.data;
478
+ $('preview-note').textContent = data?.restart?.message ?? '预览未写入;正式应用仍重新预检和核对配置版本。';
479
+ renderFiles('preview-files', data);
480
+ $('preview-files').append(node('details', { class: 'technical' }, node('summary', {}, '本次预览的终端命令'), node('pre', {}, commandText(state.preview.response.command?.argv ?? []))));
481
+ }
482
+ function renderFiles(id, data) {
483
+ const files = Array.isArray(data?.changes) ? data.changes : Array.isArray(data?.changed_files) ? data.changed_files : [];
484
+ if (!files.length) { replace(id, node('p', { class: 'muted' }, '未列出文件变化;请核对 CLI 返回的计划。')); return; }
485
+ const verbs = { create: '新增', update: '更新', delete: '清除', remove: '清除', restore: '恢复', replace: data?.action === 'restore' ? '恢复' : '更新', write: '写入' };
486
+ replace(id, node('ul', { class: 'file-list' }, files.map(file => typeof file === 'string' ? node('li', {}, file) : node('li', {}, node('strong', {}, verbs[file.action ?? file.operation] ?? file.action ?? file.operation ?? '变更'), file.path ?? file.relative ?? displayValue(file)))));
487
+ }
488
+ function rulesDefinition() { return state.modes.find(mode => mode.id === state.rulesMode) ?? activeMode() ?? state.modes.find(mode => mode.id === 'official'); }
489
+ function maximumMode(definition) { return definition?.kind === 'builtin' && ['high', 'fire'].includes(definition.id); }
490
+ function taskPair(definition, level, task) {
491
+ return task && !(maximumMode(definition) && task !== 'latency') ? definition.levels[level].tasks?.[task] : definition.levels[level];
492
+ }
493
+ const wideRoutes = matchMedia('(min-width: 1200px)');
494
+ function placeRouteDetail() {
495
+ const parent = !wideRoutes.matches && state.rulesRoute ? $(`routes-${state.rulesRoute.level}`) : $('routing-workspace');
496
+ if (parent && $('route-detail').parentElement !== parent) parent.append($('route-detail'));
497
+ }
498
+ function renderRules() {
499
+ state.rulesRoute = null;
500
+ if (!state.modes.some(mode => mode.id === state.rulesMode) || state.rulesMode === activeMode()?.id) state.rulesMode = '';
501
+ placeRouteDetail();
502
+ $('route-detail-title').textContent = '查看一条路由';
503
+ replace('route-selection', node('p', { class: 'muted' }, '点击等级或任务,查看模型、选择依据与备用条件。'));
504
+ replace('explanation'); replace('route-contract');
505
+ $('route-permissions').hidden = true;
506
+ $('explain-role').value = 'task_delegate';
507
+ const active = activeMode(), definition = rulesDefinition();
508
+ $('rules-context').textContent = `当前作用域:${SCOPE_LABELS[state.scope]};生效档位:${active?.name ?? '尚未配置'};来源:${SCOPE_LABELS[state.status?.effective_scope] ?? '未配置'}${state.status?.inherited ? '(继承)' : ''}`;
509
+ $('rules-bound-context').textContent = $('scope-context').textContent;
510
+ replace('rules-mode', node('option', { value: '' }, active ? `当前生效:${active.name} · ${active.id}` : '尚未配置 · 预览官方档位'), state.modes.filter(mode => mode.id !== active?.id).map(mode => node('option', { value: mode.id }, `预览:${mode.name} · ${mode.id}`)));
511
+ $('rules-mode').value = state.rulesMode;
512
+ const preview = !active || definition?.id !== active.id;
513
+ $('rules-view-note').textContent = preview ? `只读预览「${definition?.name ?? '未知'}」,尚未生效。查看不会切换档位。` : `正在展示「${active.name}」的已保存生效配置;未保存草稿不参与路由。`;
514
+ $('rules-view-note').dataset.preview = String(preview);
515
+ $('rules-maximum').hidden = !maximumMode(definition);
516
+ replace('rules-maximum', maximumMode(definition) ? [node('p', {}, preview ? '最高强度预览:查看不构成授权,明确选择此档位后才授权所示最高强度。' : '已选择最高强度档位:明确选档即授权所示组合,包含 Astra / ultra。'), node('p', {}, '普通任务沿用本级最高组合;低延迟仍须显式配置候选。'), definition.id === 'fire' ? node('p', {}, 'fire 每个会话首次使用前实际验证;模型、强度、供应商或账号变化后重新验证。失败时使用用户指定模型与强度,否则继承父代理。') : null] : []);
517
+ replace('route-overview', definition ? LEVELS.map((level, index) => node('button', { type: 'button', class: 'route-overview-item', 'data-route': '', 'data-level': level, 'data-task': '', 'aria-controls': 'route-detail', 'aria-pressed': 'false', on: { click: () => explain(level) } }, node('span', { class: 'route-overview-heading' }, node('strong', {}, level), LEVEL_LABELS[index]), node('strong', { class: 'route-overview-model', title: pairText(definition.levels[level]) }, routePair(definition.levels[level])), node('span', { class: 'muted' }, '等级默认'))) : []);
518
+ replace('route-levels', definition ? LEVELS.map((level, index) => {
519
+ const tasks = ['', ...Object.keys(TASK_LEVELS).filter(task => TASK_LEVELS[task].includes(level))];
520
+ return node('section', { id: `routes-${level}`, class: 'surface route-level', 'aria-labelledby': `routes-${level}-title` },
521
+ node('div', { class: 'route-level-heading' }, node('span', { class: 'level-code' }, level), node('div', {}, node('h3', { id: `routes-${level}-title` }, LEVEL_LABELS[index]), node('p', { class: 'muted' }, LEVEL_EXAMPLES[index]))),
522
+ node('p', { class: 'route-example muted' }, `例如:${ROUTE_EXAMPLES[index]}`),
523
+ node('div', { class: 'route-columns muted', 'aria-hidden': 'true' }, node('span', {}, '任务'), node('span', {}, '模型 / 推理强度')),
524
+ tasks.map(task => {
525
+ const pair = taskPair(definition, level, task), label = task ? TASK_LABELS[task] : '等级默认';
526
+ return node(pair ? 'button' : 'div', { class: `route-row${task ? '' : ' route-baseline'}`, ...(pair ? { type: 'button', 'data-route': '', 'data-level': level, 'data-task': task, 'aria-controls': 'route-detail', 'aria-pressed': 'false', on: { click: () => explain(level, task) } } : {}) }, node('span', { class: 'route-task' }, label, !pair ? node('small', {}, '需在档位配置中添加候选') : null), node('strong', { class: 'route-model', title: pair ? pairText(pair) : '此任务候选未配置,不猜测路由' }, routePair(pair)));
527
+ }));
528
+ }) : [node('p', { class: 'surface muted' }, '暂无可展示的档位。请刷新状态或前往档位配置。')]);
529
+ replace('role-list', (state.status?.roles ?? []).map(role => node('div', { class: 'role' }, node('h4', {}, `${ROLE_LABELS[role.name] ?? role.name} · ${role.name}`), node('p', {}, role.name === 'task_scan' || role.name === 'task_review' ? '只读职责;不获得项目写入权限。' : role.name === 'task_execute' ? '仅在本次明确范围内产出,写入范围须独占。' : '按照本次明确授权与原职责执行。'), node('details', { class: 'technical' }, node('summary', {}, '全局固定角色配置'), node('p', {}, `${role.configured_model ?? role.model ?? '模型未固定'} / ${role.configured_effort ?? role.effort ?? '推理强度未固定'}`), role.source ? node('p', { class: 'path' }, `来源:${role.source}`) : null))));
530
+ if (!state.status?.roles?.length) replace('role-list', node('p', { class: 'muted' }, '未安装本工具的角色。可在档位页先预览并配置。'));
531
+ const summary = state.status?.rules?.summary ?? ['用户与宿主授权优先', '禁止递归委派', '明确独占写入', '外部写入交回主代理', '局部阻断与最小充分验证', '保留专业规则、预算和高影响裁决边界'];
532
+ replace('rule-summary', node('ul', {}, summary.map(item => node('li', {}, typeof item === 'string' ? item : displayValue(item)))));
533
+ replace('sources', node('p', { class: 'muted' }, `项目查询范围:${state.bootstrap?.project ?? '服务启动时的工作目录'}`), node('pre', {}, JSON.stringify({ rules: state.status?.rules, related_config: state.status?.related_config, inspection: state.status?.inspection, diagnostics: state.status?.diagnostics }, null, 2)));
534
+ }
535
+ function renderRestore() {
536
+ const restoration = state.status?.restoration?.status;
537
+ const messages = { available: '安装前恢复记录可用。仍需先预览并通过完整预检。', not_installed: '未安装本工具,无需恢复。', invalid: '恢复记录无效。自动恢复已阻止,请核对诊断与原备份。' };
538
+ $('restore-capability').textContent = messages[restoration] ?? '恢复能力未确认,请查看状态诊断。';
539
+ $('restore-target').textContent = `绑定 home:${state.bootstrap?.home ?? ''}`;
540
+ const customs = state.restorePreview?.response.report.data?.custom_modes_removed;
541
+ replace('restore-customs', node('p', { class: 'muted' }, customs ? customs.length ? `将清理全部作用域的 ${customs.length} 个自定义档位:${customs.join('、')}` : '没有需清理的自定义档位。' : '恢复会清理此 home 全部作用域的自定义档位;请先预览完整清单。'));
542
+ $('restore-plan').hidden = !state.restorePreview;
543
+ if (state.restorePreview) {
544
+ renderFiles('restore-files', state.restorePreview.response.report.data);
545
+ $('restore-files').append(node('details', { class: 'technical' }, node('summary', {}, '本次恢复预览的终端命令'), node('pre', {}, commandText(state.restorePreview.response.command?.argv ?? []))));
546
+ $('restore-plan-note').textContent = state.restorePreview.response.report.data?.restart?.message ?? '预览未写入;原有配置和无关文件保留。';
547
+ }
548
+ renderControls();
549
+ }
550
+ function renderPrimaryCheck() {
551
+ const active = activeMode();
552
+ $('primary-check-note').textContent = !active ? '先配置档位,再检测主模型。' : !current() ? '先将所查看档位设为当前,再检测其主模型。' : dirty() ? '先保存并应用修改,再检测主模型。' : `检测已配置的「${active.name}」L1–L4 基线及同级任务候选;相同组合只请求一次,不检测备用。仅手动触发,实际调用可能消耗额度或产生费用。`;
553
+ const check = state.modelCheck;
554
+ if (!check) { replace('primary-check-result'); return; }
555
+ const data = check.response.report.data;
556
+ const stale = data.source_revision !== state.revision || data.configuration_changed !== false || state.stale;
557
+ const note = data.authorization_changed === true ? '授权已变化,本轮结果已失效,请重新检测。' : stale ? '配置已变化或来源版本未确认,请重新检测。' : '仅代表本次请求,不代替 fire 的会话验证。';
558
+ const access = { available: '可调用', unavailable: '不可用', denied: '访问被拒绝', unknown: '未确认' };
559
+ const identity = { matched: '模型与强度匹配', mismatch: '模型或强度不匹配', unverified: '身份未确认' };
560
+ replace('primary-check-result',
561
+ node('p', { class: 'muted' }, `检测档位:${check.mode};检测时间:${check.time};${note}`),
562
+ node('ul', {}, data.checked_pairs.map(pair => node('li', {}, `${pairText(pair)}:${pair.identity_status === 'mismatch' ? '未确认' : access[pair.access] ?? '未确认'};${identity[pair.identity_status] ?? '身份未确认'};${pair.reason_code}${pair.stopped_by ? `(停止原因:${pair.stopped_by})` : ''}${pair.http_status ? ` · HTTP ${pair.http_status}` : ''}`))),
563
+ data.diagnostics?.length ? node('p', { class: 'muted' }, `诊断:${[...new Set(data.diagnostics.map(item => item.message ? `${item.message}(${item.code})` : item.code))].join(';')}`) : null,
564
+ data.history?.status === 'failed' ? node('p', {}, '检测结果未能保存,请检查记录目录权限。') : null);
565
+ }
566
+ async function checkPrimary() {
567
+ if ($('check-primary').disabled) return;
568
+ const revision = state.revision;
569
+ const active = activeMode();
570
+ const pairs = primaryPairs(active);
571
+ const answer = await dialog({ title: '检测当前档位主模型?', description: `使用 ${state.bootstrap.home} 的当前账号与供应商配置,对已配置的「${active.name}」检测以下 ${pairs.length} 个主模型组合,最多 ${pairs.length} 次实际调用,可能消耗额度或产生费用。总期限 90 秒,不自动重试,不检测备用。`, content: node('ul', {}, pairs.map(pair => node('li', {}, pairText(pair)))), choices: [{ label: '取消', value: null }, { label: '确认并检测', value: true, class: 'primary' }] });
572
+ if (!answer || state.busy) return;
573
+ busy(true, '正在检测当前档位主模型,请等待本次请求完成…'); clearError();
574
+ state.modelCheck = null; renderPrimaryCheck();
575
+ try {
576
+ const response = await request('verify', { expectRevision: revision }, true);
577
+ if (!Array.isArray(response.report.data?.checked_pairs)) throw new ActionError('COMMAND_RESULT_INVALID', '检测结果不完整,请刷新状态后重试。');
578
+ state.modelCheck = { response, time: now(), mode: active.name };
579
+ renderPrimaryCheck();
580
+ } catch (error) { showError(error); }
581
+ finally { busy(false); }
582
+ }
583
+ function render() {
584
+ const active = activeMode();
585
+ $('configured').dataset.state = installed();
586
+ $('versions').textContent = `工具 ${state.bootstrap?.cli_version ?? '未知'} · 已安装配置 ${state.status?.configuration_version ?? '未安装'}`;
587
+ $('configured').textContent = active ? `已配置:${active.name}` : '尚未配置本工具';
588
+ $('scope').value = state.scope;
589
+ $('scope-context').textContent = state.scope === 'global' ? `绑定 home:${state.bootstrap?.home ?? ''}` : state.scope === 'project' ? `项目:${state.bootstrap?.project ?? ''}` : `聊天:${state.bootstrap?.thread ?? '未绑定'};项目:${state.bootstrap?.project ?? ''}`;
590
+ $('scope-source').textContent = `编辑范围:${SCOPE_LABELS[state.scope]};有效来源:${SCOPE_LABELS[state.status?.effective_scope] ?? '未配置'}${state.status?.inherited ? '(继承)' : ''}。${!state.bootstrap?.thread ? '此服务未绑定聊天,当前会话选项不可用。' : ''}`;
591
+ $('clear-scope').textContent = state.scope === 'global' ? '恢复全局默认档位' : '清除本层选择,恢复继承';
592
+ $('runtime').hidden = installed() !== 'installed';
593
+ $('runtime').textContent = '日常全局、项目和会话切档无需重启,后续新任务读取新配置,全局角色在下一轮加载;已开始任务沿用原配置。首次安装、基础规则更新或恢复后请重启 Codex。运行模型身份仍需核实。';
594
+ $('editing-title').textContent = state.saved ? `${custom() ? '正在编辑' : '正在查看'}:${state.saved.name}` : '四级模型预设';
595
+ $('editing-note').textContent = custom() ? state.saved.definition_scope !== state.scope ? '这是上层共享档位;保存时先复制到当前作用域,再修改完整四级。' : '编辑形成草稿,完整四级一次保存。' : '内置定义只读。复制为自定义后可调整四级与备用。';
596
+ const diagnostics = (state.status?.diagnostics ?? []).filter(item => item.status === 'fail' || item.status === 'warn');
597
+ $('diagnostics').hidden = !diagnostics.length;
598
+ replace('diagnostics', diagnostics.map(item => node('p', {}, `${item.code}:${item.message ?? item.status}`, item.source ? node('span', { class: 'path muted' }, `(${item.source})`) : null)));
599
+ $('last-read').textContent = state.readTime ? `最近状态读取:${state.readTime};配置与运行状态分别核对。` : '';
600
+ renderModePicker(); renderLevels(); renderChanges(); renderRules(); renderRestore(); renderPreview(); renderControls();
601
+ }
602
+
603
+ async function createMode() {
604
+ if (state.busy || writeBlocked()) return;
605
+ await leaveDraft(async () => {
606
+ const name = node('input', { id: 'new-name', autocomplete: 'off', maxLength: 160 });
607
+ const source = node('select', { id: 'new-from' }, state.modes.map(mode => node('option', { value: mode.id, selected: mode.id === state.selected }, mode.name)));
608
+ const error = node('p', { class: 'dialog-error', hidden: true, role: 'status' });
609
+ const preview = node('div', { id: 'create-preview', hidden: true });
610
+ name.addEventListener('input', () => { preview.hidden = true; });
611
+ source.addEventListener('change', () => { preview.hidden = true; });
612
+ const valid = () => {
613
+ const normalized = name.value.trim().normalize('NFC');
614
+ if (!validText(normalized, 80) || state.modes.some(mode => mode.id === normalized || mode.name === normalized)) {
615
+ error.textContent = '名称须为唯一的 1–80 个字符,不能与内置命令同名或包含控制字符。'; error.hidden = false; name.focus(); return false;
616
+ }
617
+ return true;
618
+ };
619
+ const answer = await dialog({ title: '新建自定义档位', description: '复制完整四级配置。创建后尚未设为当前,取消后续修改不会删除这个档位。', content: [node('label', { htmlFor: 'new-name' }, '档位名称'), name, node('label', { htmlFor: 'new-from' }, '从哪个档位复制'), source, error, preview], choices: [{ label: '关闭', value: null }, { label: '预览创建', validate: valid, run: async () => {
620
+ preview.hidden = false; preview.replaceChildren(node('p', { class: 'muted', role: 'status' }, '正在执行创建预览…'));
621
+ if (await performWrite('create', { name: name.value.trim().normalize('NFC'), from: source.value, expectRevision: state.revision }, { preview: true })) {
622
+ preview.hidden = false;
623
+ preview.replaceChildren(node('p', { class: 'muted', role: 'status' }, '预览完成,尚未创建档位。'), node('pre', {}, commandText(state.preview.response.command?.argv ?? [])));
624
+ }
625
+ } }, { label: '创建档位', value: 'create', class: 'primary', validate: valid }] });
626
+ if (answer === 'create') await performWrite('create', { name: name.value.trim().normalize('NFC'), from: source.value, expectRevision: state.revision });
627
+ });
628
+ }
629
+ async function explain(level, task = '') {
630
+ if (state.busy || !state.connected || state.stale) return;
631
+ const focused = document.activeElement;
632
+ const definition = rulesDefinition(), pair = taskPair(definition, level, task);
633
+ if (!pair) return;
634
+ state.rulesRoute = { level, task };
635
+ const label = task ? TASK_LABELS[task] : '等级默认';
636
+ $('route-detail-title').textContent = `${level} · ${label}`;
637
+ replace('route-selection', node('p', { class: 'route-selected-model', title: pairText(pair) }, routePair(pair)), node('p', { class: 'muted' }, `${definition.name} · ${definition.id === activeMode()?.id ? '当前生效' : '只读预览,尚未生效'}`));
638
+ replace('explanation', node('p', { class: 'muted' }, '正在读取本条路由的选择依据…'));
639
+ replace('route-contract');
640
+ $('route-permissions').hidden = true;
641
+ $('explanation').setAttribute('aria-busy', 'true');
642
+ for (const button of document.querySelectorAll('[data-route]')) button.setAttribute('aria-pressed', String(button.dataset.level === level && button.dataset.task === task));
643
+ placeRouteDetail();
644
+ if (!wideRoutes.matches && focused?.matches('[data-route]')) { $('route-detail-title').focus(); $('route-detail').scrollIntoView({ block: 'nearest' }); }
645
+ busy(true, '正在读取所选路由的解释…'); clearError();
646
+ try {
647
+ const response = await request('explain', { mode: definition.id, level, role: $('explain-role').value, ...(task ? { task } : {}) });
648
+ const data = response.report.data;
649
+ if (data?.configuration_revision !== state.revision) { state.stale = true; throw new ActionError('CONFIG_CHANGED', '配置版本已变化,请刷新状态后重新选择路由。'); }
650
+ if (data.mode_id !== definition.id || data.level !== level || data.task !== (task || null) || data.scope !== state.scope || data.primary?.model !== pair.model || data.primary?.effort !== pair.effort) throw new ActionError('COMMAND_RESULT_INVALID', '解释结果与所选路由不一致,请刷新后重新选择。');
651
+ const reason = task ? maximumMode(definition) && task !== 'latency' ? '此最高强度档位对普通任务使用本级最高组合。' : '命中此等级已配置的任务候选,替代等级默认组合;备用也使用此候选自己的配置。' : '未指定任务候选时,使用此等级的默认组合。';
652
+ const fields = [['等级依据', LEVEL_EXAMPLES[LEVELS.indexOf(level)]], ['选择依据', reason], ['备用模型 / 强度', data.verification?.required ? '用户指定,否则继承父代理' : pairText(data.fallback)]];
653
+ if (data.fallback) fields.push(['备用触发条件', '模型不可用、强度不支持,或临时故障有限重试后仍失败时,才在剩余次数与预算内接续;认证、权限、额度或原因不明时停止。仍须满足宿主、工具与原验收要求,写入者须先停止。']);
654
+ if (data.verification?.required) fields.push(['使用前检查', '每个会话首次使用前实际验证;模型、强度、供应商或账号变化后重新验证。'], ['检查失败', '使用 task_delegate,采用用户指定模型与强度,否则继承父代理当前组合。']);
655
+ replace('explanation', node('dl', {}, fields.flatMap(([label, value]) => [node('dt', {}, label), node('dd', {}, displayValue(value))])), node('p', { class: 'muted' }, '这是配置路由,未验证账号可用性或实际运行模型。'), node('details', { class: 'technical' }, node('summary', {}, '完整模型 ID、来源与诊断'), node('pre', {}, JSON.stringify({ primary: data.primary, scope: data.scope, effective_scope: data.effective_scope, reason_codes: data.reason_codes, fallback_conditions: data.fallback_conditions, source: data.source, configuration_revision: data.configuration_revision }, null, 2)), node('p', { class: 'muted' }, '本次解释的终端命令'), node('pre', {}, commandText(response.command?.argv ?? []))));
656
+ replace('route-contract', node('dl', {}, node('dt', {}, '请求职责'), node('dd', {}, ROLE_LABELS[data.requested_role]), node('dt', {}, '应使用角色'), node('dd', {}, data.dispatch_role), node('dt', {}, '权限契约'), node('dd', {}, CONTRACT_LABELS[data.permission_contract] ?? displayValue(data.permission_contract))));
657
+ $('route-permissions').hidden = false;
658
+ } catch (error) {
659
+ replace('explanation', node('p', {}, `未能读取本条路由:${error.message}`), node('button', { id: 'route-retry', type: 'button', on: { click: () => explain(level, task) } }, '重试读取'));
660
+ showError(error);
661
+ }
662
+ finally {
663
+ $('explanation').setAttribute('aria-busy', 'false'); busy(false);
664
+ if ($('error').hidden && document.activeElement === document.body) (focused?.isConnected ? focused : $('route-detail-title')).focus();
665
+ }
666
+ }
667
+ async function restore() {
668
+ if (!state.restorePreview || state.busy) return;
669
+ if (state.restorePreview.revision !== state.revision) { showError(new ActionError('CONFIG_CHANGED', '配置版本已改变,请重新预览恢复。')); return; }
670
+ const customs = state.restorePreview.response.report.data.custom_modes_removed;
671
+ const answer = await dialog({ title: '恢复原配置?', description: '按本次预览恢复首次配置前的记录。将移除本工具规则与角色,清理全部自定义档位。恢复后请重启 Codex。', content: [node('p', { class: 'path' }, state.bootstrap.home), node('p', {}, customs.length ? `清理全部作用域的自定义档位:${customs.join('、')}` : '没有需清理的自定义档位。')], choices: [{ label: '保留配置', value: null }, { label: '恢复原配置', value: true, class: 'danger' }] });
672
+ if (answer) await performWrite('restore', { expectRevision: state.restorePreview.revision });
673
+ }
674
+
675
+ for (const form of document.querySelectorAll('form')) form.addEventListener('submit', event => event.preventDefault());
676
+ for (const link of document.querySelectorAll('.tabs a')) link.addEventListener('click', event => { event.preventDefault(); if (!state.busy) leaveDraft(() => showTab(link.hash.slice(1))); });
677
+ $('refresh').addEventListener('click', () => state.connected ? refresh() : start());
678
+ $('compare').addEventListener('click', () => refresh({ compare: true }));
679
+ $('custom-mode').addEventListener('change', event => selectMode(event.target.value));
680
+ $('new-mode').addEventListener('click', createMode);
681
+ $('copy-mode').addEventListener('click', createMode);
682
+ $('apply').addEventListener('click', () => apply());
683
+ $('check-primary').addEventListener('click', checkPrimary);
684
+ $('scope').addEventListener('change', async event => {
685
+ const selected = event.target.value;
686
+ $('scope').value = state.scope;
687
+ await leaveDraft(async () => {
688
+ const previous = state.scope;
689
+ state.scope = selected;
690
+ if (await refresh({ selected: null, preserve: false })) { state.modelCheck = null; renderPrimaryCheck(); }
691
+ else { state.scope = previous; render(); }
692
+ });
693
+ });
694
+ for (const [id, action] of [['clear-scope', 'clear'], ['delete-mode', 'delete']]) $(id).addEventListener('click', async () => {
695
+ const selected = state.selected, revision = state.revision;
696
+ const answer = await dialog({ title: action === 'clear' ? '清除当前层的选择?' : '删除自定义档位?', description: action === 'clear' ? `仅修改${SCOPE_LABELS[state.scope]}的选择,保留自定义定义和其他层;全局清除恢复 official。` : `删除${SCOPE_LABELS[state.scope]}中的「${state.saved.name}」。仍被任何作用域引用时会阻止删除。`, choices: [{ label: '取消', value: null }, { label: '确认', value: true, class: 'primary' }] });
697
+ if (answer) await performWrite(action, { expectRevision: revision, ...(action === 'delete' ? { mode: selected } : {}) });
698
+ });
699
+ $('preview').addEventListener('click', () => apply(true));
700
+ $('cancel-draft').addEventListener('click', async () => { state.draft = clone(state.saved); state.preview = null; if (state.stale) await refresh({ preserve: false }); else render(); });
701
+ $('cancel-selection').addEventListener('click', () => selectMode(activeMode().id));
702
+ $('close-preview').addEventListener('click', () => { state.preview = null; renderPreview(); $('preview').focus(); });
703
+ $('copy-definition').addEventListener('click', async () => {
704
+ try { await navigator.clipboard.writeText(JSON.stringify({ levels: state.draft.levels }, null, 2)); $('copy-status').textContent = '完整四级 JSON 已复制。'; }
705
+ catch { $('copy-status').textContent = '浏览器未允许复制,请展开以下 JSON 手动复制。'; $('command').textContent += `\n\n${JSON.stringify({ levels: state.draft.levels }, null, 2)}`; }
706
+ });
707
+ $('rules-mode').addEventListener('change', () => { state.rulesMode = $('rules-mode').value; renderRules(); renderControls(); });
708
+ $('explain-role').addEventListener('change', () => { if (state.rulesRoute) explain(state.rulesRoute.level, state.rulesRoute.task); });
709
+ $('edit-routes').addEventListener('click', () => {
710
+ const mode = rulesDefinition()?.id;
711
+ if (!state.busy) leaveDraft(async () => { if (mode && !await refresh({ selected: mode, preserve: false })) return; showTab('modes'); $('editing-title').tabIndex = -1; $('editing-title').focus(); });
712
+ });
713
+ wideRoutes.addEventListener('change', placeRouteDetail);
714
+ $('account-refresh').addEventListener('click', () => accountAction({ operation: 'status' }));
715
+ $('login-browser').addEventListener('click', () => login('chatgpt'));
716
+ $('login-device').addEventListener('click', () => login('chatgptDeviceCode'));
717
+ $('login-key').addEventListener('click', () => login('apiKey'));
718
+ $('account-logout').addEventListener('click', async () => {
719
+ const answer = await dialog({ title: '退出当前 Codex 登录?', description: `将退出 ${state.bootstrap.home} 对应的官方登录。其他使用此配置目录的客户端也会受影响。供应商环境变量和配置凭据会保留。`, choices: [{ label: '保留登录', value: null }, { label: '退出登录', value: true, class: 'danger' }] });
720
+ if (answer) await accountAction({ operation: 'logout' });
721
+ });
722
+ $('restore-preview').addEventListener('click', () => performWrite('restore', { expectRevision: state.revision }, { preview: true }));
723
+ $('restore-confirm').addEventListener('click', restore);
724
+ desktop.addEventListener('change', event => { for (const level of LEVELS) if ($(`level-${level}`)) $(`level-${level}`).open = event.matches || level === 'L1'; });
725
+ window.addEventListener('beforeunload', event => { if (dirty() || state.busy) { event.preventDefault(); event.returnValue = ''; } });
726
+ window.addEventListener('focus', () => { if (state.connected && !state.busy && !$('confirm-dialog').open) refresh(); });
727
+
728
+ async function start() {
729
+ showTab(location.hash.slice(1));
730
+ busy(true, '正在连接本地配置服务…');
731
+ try {
732
+ const response = await fetch('/api/bootstrap', { credentials: 'same-origin' });
733
+ const bootstrap = await response.json();
734
+ if (!response.ok || typeof bootstrap.home !== 'string' || typeof bootstrap.csrf_token !== 'string' || typeof bootstrap.cli_version !== 'string') throw new ActionError('SERVICE_UNAVAILABLE', '本地服务无法连接,请在终端重新启动 serve 后刷新页面。');
735
+ state.bootstrap = bootstrap; state.connected = true;
736
+ $('home').textContent = bootstrap.home;
737
+ if (!state.result && bootstrap.recent_result?.report?.data?.result) { state.result = bootstrap.recent_result; renderResult(); }
738
+ busy(false);
739
+ await refresh();
740
+ } catch (error) { state.connected = false; showError(error instanceof ActionError ? error : new ActionError('SERVICE_UNAVAILABLE', '本地服务无法连接,请在终端重新启动 serve 后刷新页面。')); }
741
+ finally { busy(false); }
742
+ if (state.tab === 'accounts' && state.connected) await accountAction({ operation: 'status' });
743
+ }
744
+ start();