@magic6/dsh-llm-resilience 0.0.0-stage → 0.5.3
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 +66 -0
- package/DESIGN.md +265 -0
- package/LICENSE +21 -0
- package/README.md +305 -2
- package/README.zh-CN.md +279 -0
- package/cordis.patch.yml +46 -0
- package/lib/adapter.js +1055 -0
- package/lib/client.js +1695 -0
- package/lib/config.js +135 -0
- package/lib/index.js +87 -0
- package/lib/policy.js +549 -0
- package/lib/session-log.js +42 -0
- package/lib/state-file.js +105 -0
- package/package.json +69 -4
package/lib/client.js
ADDED
|
@@ -0,0 +1,1695 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Browser half of dsh-llm-resilience: the fallback-chain card on the plugin's own page
|
|
3
|
+
* (Plugins → Installed → dsh-llm-resilience), registered into `plugins.bundle.config`.
|
|
4
|
+
*
|
|
5
|
+
* Served verbatim (no build step), so the few policy formulas this card needs —
|
|
6
|
+
* nominal backoff and jitter bounds — are inlined here. `test/client.test.js`
|
|
7
|
+
* pins them against `lib/policy.js`, so the preview cannot drift from what the
|
|
8
|
+
* adapter actually waits.
|
|
9
|
+
*
|
|
10
|
+
* Every field writes through `ctx.configForms.get('llm-fallback')`, the profile
|
|
11
|
+
* entry id whose `Config` owns this document. A non-loopback browser only gets a
|
|
12
|
+
* memory view; the card says so and renders no values rather than passing the
|
|
13
|
+
* plugin defaults off as the host's policy.
|
|
14
|
+
*
|
|
15
|
+
* @module dsh-llm-resilience/client
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
window.__ModuleLoader__.load({
|
|
19
|
+
id: 'dsh-llm-resilience',
|
|
20
|
+
factory: (require) => {
|
|
21
|
+
var module = { exports: {} };
|
|
22
|
+
var exports = module.exports;
|
|
23
|
+
|
|
24
|
+
const react = require('react');
|
|
25
|
+
const jsx = require('react/jsx-runtime');
|
|
26
|
+
|
|
27
|
+
const NS = 'llm-fallback';
|
|
28
|
+
/** npm 包名:`plugins.bundle.config` 用它与「已安装插件」里的行配对。 */
|
|
29
|
+
const PACKAGE_NAME = 'dsh-llm-resilience';
|
|
30
|
+
const DEFAULT_ROUTE = 'fallback';
|
|
31
|
+
|
|
32
|
+
const zh = {
|
|
33
|
+
title: '模型回退链',
|
|
34
|
+
hint: '这条合成 provider 按顺序尝试下面的目标。每个 target 有独立的重试预算和退避;连续失败会熔断,锁到期后先探活再恢复。',
|
|
35
|
+
enabled: '启用插件',
|
|
36
|
+
route: '合成 route',
|
|
37
|
+
routeHint: 'route 在插件加载时固定;改名需要修改配置并重启 dsh。',
|
|
38
|
+
targets: '目标链',
|
|
39
|
+
targetCount: '{total} 条目标 · {enabled} 条参与',
|
|
40
|
+
targetsEmpty: '链为空:合成 route 保持 dormant,不提供模型。至少添加一个目标后才会接管请求。',
|
|
41
|
+
addTarget: '添加目标',
|
|
42
|
+
enableTarget: '参与链',
|
|
43
|
+
labelPlaceholder: '显示名(可选)',
|
|
44
|
+
provider: 'provider',
|
|
45
|
+
model: 'model',
|
|
46
|
+
maxRetries: '重试次数',
|
|
47
|
+
initialDelay: '首次等待 (ms)',
|
|
48
|
+
multiplier: '倍率',
|
|
49
|
+
maxDelay: '等待上限 (ms)',
|
|
50
|
+
jitter: '抖动 (0–1)',
|
|
51
|
+
plan: '重试计划',
|
|
52
|
+
planTitle: '重试计划',
|
|
53
|
+
planSub: '首次请求失败后,最多再试 {n} 次:',
|
|
54
|
+
planNone: '重试次数为 0:首次失败后直接进入下一个目标。',
|
|
55
|
+
planRow: '第 {n} 次',
|
|
56
|
+
planRange: '{low} – {high}',
|
|
57
|
+
planJitterNote: '每行是四舍五入后的名义等待;实际值在抖动范围内随机。',
|
|
58
|
+
planTotal: '名义总等待 {total}',
|
|
59
|
+
planTruncated: '……共 {n} 次重试',
|
|
60
|
+
moveUp: '上移',
|
|
61
|
+
moveDown: '下移',
|
|
62
|
+
remove: '移除',
|
|
63
|
+
disable: '已禁用',
|
|
64
|
+
breaker: '熔断锁',
|
|
65
|
+
breakerHint: '按 provider + model 独立记账。连续失败达到阈值后锁定;锁到期后先发一个最小 chat 探活,成功才恢复使用,失败再锁一轮。',
|
|
66
|
+
failureThreshold: '失败阈值',
|
|
67
|
+
lockMinutes: '锁定时长(分钟)',
|
|
68
|
+
probe: '锁到期先探活',
|
|
69
|
+
probeBackground: '锁到期在后台探活',
|
|
70
|
+
probeBackgroundHint: '开启后,锁到期的目标在本次请求里直接跳过,探活放到后台跑;下一次请求使用探活结果,当前请求不会为了等探活而变慢。',
|
|
71
|
+
probeTimeout: '探活超时 (ms)',
|
|
72
|
+
probeMaxTokens: '探活 maxTokens',
|
|
73
|
+
probePrompt: '探活 prompt',
|
|
74
|
+
limits: '单次请求预算',
|
|
75
|
+
limitsHint: '跨 target、跨重试累计;重试等待也计入总时限。0 表示不限制。',
|
|
76
|
+
maxTotalMs: '总时限 (ms)',
|
|
77
|
+
maxAttempts: '最多请求数',
|
|
78
|
+
live: '运行状态',
|
|
79
|
+
liveHint: '每 4 秒从主机读取一次熔断状态;这里只读。',
|
|
80
|
+
liveEmpty: '还没有运行状态:插件在这个进程里尚未发起过请求,或链为空。',
|
|
81
|
+
liveDisabled: '插件已停用,运行状态可能来自停用之前。',
|
|
82
|
+
liveUpdated: '更新于 {age}前',
|
|
83
|
+
liveFailureCount: '连败 {n}',
|
|
84
|
+
liveLockCount: '累计锁 {n} 次',
|
|
85
|
+
liveRemaining: '剩余 {time}',
|
|
86
|
+
liveError: '读取运行状态失败:{message}',
|
|
87
|
+
liveStateClosed: '正常',
|
|
88
|
+
liveStateOpen: '已锁定',
|
|
89
|
+
liveStateHalfOpen: '待探活',
|
|
90
|
+
liveStateProbing: '探活中',
|
|
91
|
+
liveStateUnknown: '未知',
|
|
92
|
+
stateFile: '状态文件',
|
|
93
|
+
stateFileHint: '只存熔断计数与时间,不写密钥。',
|
|
94
|
+
toolCalls: '工具调用',
|
|
95
|
+
retryMalformed: '重试「模型输出损坏」的工具调用',
|
|
96
|
+
retryMalformedHint: '只覆盖工具参数 JSON 非法等 LLM 输出问题——重发整个请求是安全的。工具真正执行失败(文件不存在、命令非零退出)不会被重试,因为可能有副作用。',
|
|
97
|
+
save: '保存',
|
|
98
|
+
discard: '放弃改动',
|
|
99
|
+
unsaved: '有未保存的改动',
|
|
100
|
+
loading: '正在读取配置…',
|
|
101
|
+
catalogLoading: '正在读取会话模型目录…',
|
|
102
|
+
catalogFailed: '模型目录加载失败,provider / model 暂时无法选择;修好后才能保存。',
|
|
103
|
+
catalogRetry: '重新加载目录',
|
|
104
|
+
catalogBlocked: '模型目录未就绪,暂时不能保存。',
|
|
105
|
+
selectPlaceholder: '请选择',
|
|
106
|
+
notInCatalog: '(不在目录中)',
|
|
107
|
+
unavailable: '这个浏览器读不到主机上的 fallback 配置(dsh 只给非环回地址内存态视图)。为避免把默认值冒充成真值,这里不显示任何字段;请在本机用 127.0.0.1 打开设置页。',
|
|
108
|
+
readOnly: '主机配置对这台客户端只读:下面是真实生效值,但不能在这里修改。',
|
|
109
|
+
writeRefused: '写入未被主机接受:配置没有保存。',
|
|
110
|
+
invalid: '有字段不合法,修好后再保存:',
|
|
111
|
+
saved: '已保存',
|
|
112
|
+
errProvider: '第 {n} 条:provider 不能为空',
|
|
113
|
+
errModel: '第 {n} 条:model 不能为空',
|
|
114
|
+
errMaxRetries: '第 {n} 条:重试次数必须是 ≥0 的整数',
|
|
115
|
+
errInitialDelay: '第 {n} 条:首次等待必须 > 0',
|
|
116
|
+
errMultiplier: '第 {n} 条:倍率必须 > 1',
|
|
117
|
+
errMaxDelay: '第 {n} 条:等待上限必须 ≥ 首次等待',
|
|
118
|
+
errJitter: '第 {n} 条:抖动必须在 0–1 之间',
|
|
119
|
+
errSelf: '第 {n} 条:provider 不能等于合成 route',
|
|
120
|
+
errDuplicate: '第 {n} 条:与前面的目标重复',
|
|
121
|
+
errFailureThreshold: '失败阈值必须是 ≥1 的整数',
|
|
122
|
+
errLock: '锁定时长必须 > 0 分钟',
|
|
123
|
+
errProbeTimeout: '探活超时必须 > 0',
|
|
124
|
+
errProbeMaxTokens: '探活 maxTokens 必须是 ≥1 的整数',
|
|
125
|
+
errProbePrompt: '探活 prompt 不能为空',
|
|
126
|
+
errMaxTotalMs: '总时限必须是 ≥0 的毫秒数',
|
|
127
|
+
errMaxAttempts: '最多请求数必须是 ≥0 的整数',
|
|
128
|
+
errProviderUnknown: '第 {n} 条:provider 不在当前会话的模型目录中',
|
|
129
|
+
errModelUnknown: '第 {n} 条:model 不在该 provider 的目录中',
|
|
130
|
+
errStateFile: '状态文件路径不能为空',
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
const en = {
|
|
134
|
+
title: 'Model fallback chain',
|
|
135
|
+
hint: 'This synthetic provider walks the targets below in order. Each target keeps its own retry budget and backoff; repeated failures trip a circuit breaker, and a lock is re-probed before the target is used again.',
|
|
136
|
+
enabled: 'Enable plugin',
|
|
137
|
+
route: 'Synthetic route',
|
|
138
|
+
routeHint: 'The route is fixed when the plugin loads; renaming it requires a config edit and a dsh restart.',
|
|
139
|
+
targets: 'Target chain',
|
|
140
|
+
targetCount: '{total} target(s) · {enabled} enabled',
|
|
141
|
+
targetsEmpty: 'The chain is empty: the synthetic route stays dormant and serves no models. Add at least one target to take over requests.',
|
|
142
|
+
addTarget: 'Add target',
|
|
143
|
+
enableTarget: 'In chain',
|
|
144
|
+
labelPlaceholder: 'Display name (optional)',
|
|
145
|
+
provider: 'provider',
|
|
146
|
+
model: 'model',
|
|
147
|
+
maxRetries: 'Retries',
|
|
148
|
+
initialDelay: 'First wait (ms)',
|
|
149
|
+
multiplier: 'Multiplier',
|
|
150
|
+
maxDelay: 'Wait cap (ms)',
|
|
151
|
+
jitter: 'Jitter (0–1)',
|
|
152
|
+
plan: 'Retry plan',
|
|
153
|
+
planTitle: 'Retry plan',
|
|
154
|
+
planSub: 'After the first request fails, up to {n} more attempt(s):',
|
|
155
|
+
planNone: 'Zero retries: the first failure moves straight to the next target.',
|
|
156
|
+
planRow: 'Retry {n}',
|
|
157
|
+
planRange: '{low} – {high}',
|
|
158
|
+
planJitterNote: 'Each row is the rounded nominal wait; the actual wait is randomized inside the jitter range.',
|
|
159
|
+
planTotal: 'Nominal total {total}',
|
|
160
|
+
planTruncated: '…{n} retries in total',
|
|
161
|
+
moveUp: 'Move up',
|
|
162
|
+
moveDown: 'Move down',
|
|
163
|
+
remove: 'Remove',
|
|
164
|
+
disable: 'disabled',
|
|
165
|
+
breaker: 'Circuit breaker',
|
|
166
|
+
breakerHint: 'Tracked per provider + model. After enough consecutive failures the target is locked; when the lock expires a minimal chat probes it, and it only returns if the probe succeeds.',
|
|
167
|
+
failureThreshold: 'Failure threshold',
|
|
168
|
+
lockMinutes: 'Lock duration (minutes)',
|
|
169
|
+
probe: 'Probe before reuse',
|
|
170
|
+
probeBackground: 'Probe expired locks in the background',
|
|
171
|
+
probeBackgroundHint: 'When on, an expired target is skipped by the current request and probed in the background; the next request uses the probe result, so the current one never waits for it.',
|
|
172
|
+
probeTimeout: 'Probe timeout (ms)',
|
|
173
|
+
probeMaxTokens: 'Probe maxTokens',
|
|
174
|
+
probePrompt: 'Probe prompt',
|
|
175
|
+
limits: 'Per-request budget',
|
|
176
|
+
limitsHint: 'Accumulated across targets and retries; retry waits count toward the deadline. 0 means unlimited.',
|
|
177
|
+
maxTotalMs: 'Total deadline (ms)',
|
|
178
|
+
maxAttempts: 'Max upstream requests',
|
|
179
|
+
live: 'Live state',
|
|
180
|
+
liveHint: 'Reads breaker state from the host every 4 seconds; read-only.',
|
|
181
|
+
liveEmpty: 'No runtime state yet: the plugin has not served a request in this process, or the chain is empty.',
|
|
182
|
+
liveDisabled: 'The plugin is disabled; state may predate the switch.',
|
|
183
|
+
liveUpdated: 'Updated {age} ago',
|
|
184
|
+
liveFailureCount: '{n} consecutive',
|
|
185
|
+
liveLockCount: '{n} lock(s)',
|
|
186
|
+
liveRemaining: '{time} left',
|
|
187
|
+
liveError: 'Cannot read runtime state: {message}',
|
|
188
|
+
liveStateClosed: 'healthy',
|
|
189
|
+
liveStateOpen: 'locked',
|
|
190
|
+
liveStateHalfOpen: 'probe pending',
|
|
191
|
+
liveStateProbing: 'probing',
|
|
192
|
+
liveStateUnknown: 'unknown',
|
|
193
|
+
stateFile: 'State file',
|
|
194
|
+
stateFileHint: 'Stores breaker counters and timestamps only — never credentials.',
|
|
195
|
+
toolCalls: 'Tool calls',
|
|
196
|
+
retryMalformed: 'Retry tool calls with malformed model output',
|
|
197
|
+
retryMalformedHint: 'Covers LLM output problems such as invalid tool-argument JSON, where replaying the whole request is safe. Real tool-execution failures (missing file, non-zero command) are not retried because they may have side effects.',
|
|
198
|
+
save: 'Save',
|
|
199
|
+
discard: 'Discard',
|
|
200
|
+
unsaved: 'Unsaved changes',
|
|
201
|
+
loading: 'Reading configuration…',
|
|
202
|
+
catalogLoading: 'Reading the session model catalog…',
|
|
203
|
+
catalogFailed: 'The model catalog failed to load, so provider / model cannot be chosen; saving stays disabled until it recovers.',
|
|
204
|
+
catalogRetry: 'Reload catalog',
|
|
205
|
+
catalogBlocked: 'The model catalog is not ready; saving is disabled.',
|
|
206
|
+
selectPlaceholder: 'Select…',
|
|
207
|
+
notInCatalog: '(not in catalog)',
|
|
208
|
+
unavailable: 'This browser cannot read the host fallback configuration (dsh gives non-loopback clients a memory view only). No fields are shown, because plugin defaults are not the deployed truth; open Settings on 127.0.0.1 instead.',
|
|
209
|
+
readOnly: 'The host configuration is read-only for this client: the values below are real, but cannot be edited here.',
|
|
210
|
+
writeRefused: 'The host refused the write: nothing was saved.',
|
|
211
|
+
invalid: 'Some fields are invalid; fix them before saving:',
|
|
212
|
+
saved: 'Saved',
|
|
213
|
+
errProvider: 'Target {n}: provider must not be empty',
|
|
214
|
+
errModel: 'Target {n}: model must not be empty',
|
|
215
|
+
errMaxRetries: 'Target {n}: retries must be an integer ≥ 0',
|
|
216
|
+
errInitialDelay: 'Target {n}: first wait must be > 0',
|
|
217
|
+
errMultiplier: 'Target {n}: multiplier must be > 1',
|
|
218
|
+
errMaxDelay: 'Target {n}: wait cap must be ≥ the first wait',
|
|
219
|
+
errJitter: 'Target {n}: jitter must be between 0 and 1',
|
|
220
|
+
errSelf: 'Target {n}: provider must not equal the synthetic route',
|
|
221
|
+
errDuplicate: 'Target {n}: duplicates an earlier target',
|
|
222
|
+
errFailureThreshold: 'Failure threshold must be an integer ≥ 1',
|
|
223
|
+
errLock: 'Lock duration must be > 0 minutes',
|
|
224
|
+
errProbeTimeout: 'Probe timeout must be > 0',
|
|
225
|
+
errProbeMaxTokens: 'Probe maxTokens must be an integer ≥ 1',
|
|
226
|
+
errProbePrompt: 'Probe prompt must not be empty',
|
|
227
|
+
errMaxTotalMs: 'Total deadline must be a number of ms ≥ 0',
|
|
228
|
+
errMaxAttempts: 'Max upstream requests must be an integer ≥ 0',
|
|
229
|
+
errProviderUnknown: 'Target {n}: provider is not in this session model catalog',
|
|
230
|
+
errModelUnknown: 'Target {n}: model is not offered by that provider',
|
|
231
|
+
errStateFile: 'State file path must not be empty',
|
|
232
|
+
};
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* Shared config-card kit (`cc*` classes) — the plugin-form layout vocabulary
|
|
236
|
+
* for settings pages, expressed only in `--dsw-alias-*` theme tokens.
|
|
237
|
+
*
|
|
238
|
+
* Provenance: grown inside dsh-compaction-control's card, promoted to the
|
|
239
|
+
* shared standard (WorkSpaces/ui-unification-plan-2026-10-26.md). This exact
|
|
240
|
+
* text is vendored into every hand-written client that renders a config
|
|
241
|
+
* card; `plugin-devkit/ui-kit/check.mjs` fails when a copy drifts. The
|
|
242
|
+
* injector is idempotent on the shared `data-plugin-css="dsh-ui-kit"` id, so
|
|
243
|
+
* the first client to boot owns the single <style> tag and the rest no-op —
|
|
244
|
+
* no plugin may grow a runtime dependency on another plugin's stylesheet.
|
|
245
|
+
*/
|
|
246
|
+
const UI_KIT_CSS = [
|
|
247
|
+
'.ccWrap{display:flex;flex-direction:column;gap:8px;padding:12px 0}',
|
|
248
|
+
'.ccTitle{color:var(--dsw-alias-label-primary);font-size:13px;font-weight:500}',
|
|
249
|
+
'.ccHint{color:var(--dsw-alias-label-tertiary);margin:0;font-size:12px;line-height:1.5}',
|
|
250
|
+
'.ccRow{display:flex;align-items:center;gap:10px}',
|
|
251
|
+
'.ccLabel{color:var(--dsw-alias-label-secondary);flex:1;font-size:13px}',
|
|
252
|
+
'.ccInput{border:.5px solid var(--dsw-alias-border-l4);background:var(--dsw-alias-bg-layer-3);height:32px;font:inherit;color:var(--dsw-alias-label-primary);border-radius:8px;padding:0 10px;width:104px;font-size:13px;font-variant-numeric:tabular-nums}',
|
|
253
|
+
'.ccInput:focus-visible{border-color:var(--dsw-alias-brand-primary);outline:none}',
|
|
254
|
+
'.ccInputInvalid{border-color:var(--dsw-alias-label-error)}',
|
|
255
|
+
'.ccInputWide{width:170px}',
|
|
256
|
+
'.ccReset{font:inherit;color:var(--dsw-alias-label-secondary);cursor:pointer;background:0 0;border:none;padding:0;font-size:12px}',
|
|
257
|
+
'.ccReset:hover:not(:disabled){color:var(--dsw-alias-label-primary)}',
|
|
258
|
+
'.ccReset:disabled{cursor:default;opacity:.5}',
|
|
259
|
+
'.ccError{color:var(--dsw-alias-label-error);margin:0;font-size:12px}',
|
|
260
|
+
'.ccReadonly{color:var(--dsw-alias-label-secondary);font-size:13px}',
|
|
261
|
+
'.ccSelect{border:.5px solid var(--dsw-alias-border-l4);background:var(--dsw-alias-bg-layer-3);height:32px;font:inherit;color:var(--dsw-alias-label-primary);border-radius:8px;padding:0 8px;max-width:240px;font-size:13px;cursor:pointer}',
|
|
262
|
+
'.ccSelect:focus-visible{border-color:var(--dsw-alias-brand-primary);outline:none}',
|
|
263
|
+
'.ccSelect:disabled{cursor:default;opacity:.6}',
|
|
264
|
+
'.ccCheck{width:16px;height:16px;accent-color:var(--dsw-alias-brand-primary);cursor:pointer;flex:none;margin:0}',
|
|
265
|
+
'.ccSubHint{color:var(--dsw-alias-label-tertiary);margin:0;font-size:12px;line-height:1.5;padding-left:26px}',
|
|
266
|
+
'.ccList{margin:0;padding-left:1.2em;max-height:9em;overflow:auto;color:var(--dsw-alias-label-secondary);font-size:12px;line-height:1.5}',
|
|
267
|
+
// 回退链是一张有序表,缩进让它读起来像“主模型之下的第 1、2、3 选择”,
|
|
268
|
+
// 而不是一组与阈值平级的设置。
|
|
269
|
+
'.ccChain{display:flex;flex-direction:column;gap:6px;border-left:2px solid var(--dsw-alias-border-l4);padding-left:10px;margin:4px 0}',
|
|
270
|
+
'.ccChainRow{gap:8px}',
|
|
271
|
+
'.ccChainIndex{color:var(--dsw-alias-label-tertiary);font-size:12px;min-width:14px}',
|
|
272
|
+
'.ccChainValue{flex:1;font-size:13px;color:var(--dsw-alias-label-primary);overflow:hidden;text-overflow:ellipsis;white-space:nowrap}',
|
|
273
|
+
].join('');
|
|
274
|
+
|
|
275
|
+
if (
|
|
276
|
+
typeof document !== 'undefined' &&
|
|
277
|
+
document.querySelector('style[data-plugin-css="dsh-ui-kit"]') === null
|
|
278
|
+
) {
|
|
279
|
+
const uiKitTag = document.createElement('style');
|
|
280
|
+
uiKitTag.dataset.pluginCss = 'dsh-ui-kit';
|
|
281
|
+
uiKitTag.textContent = UI_KIT_CSS;
|
|
282
|
+
document.head.appendChild(uiKitTag);
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
const css = [
|
|
286
|
+
'.lfHead{display:flex;align-items:center;gap:8px;flex-wrap:wrap}',
|
|
287
|
+
'.lfRoute{color:var(--dsw-alias-label-secondary);background:var(--dsw-alias-bg-layer-3);border:.5px solid var(--dsw-alias-border-l4);border-radius:6px;padding:1px 6px;font-size:11px;font-variant-numeric:tabular-nums}',
|
|
288
|
+
'.lfSection{display:flex;flex-direction:column;gap:8px;border-top:.5px solid var(--dsw-alias-border-l4);padding-top:10px;margin-top:2px}',
|
|
289
|
+
'.lfSectionHead{display:flex;align-items:baseline;gap:8px;flex-wrap:wrap}',
|
|
290
|
+
'.lfCount{color:var(--dsw-alias-label-tertiary);font-size:11px;margin-left:auto}',
|
|
291
|
+
'.lfTarget{display:flex;flex-direction:column;gap:8px;padding:10px;border:.5px solid var(--dsw-alias-border-l4);border-radius:10px;background:var(--dsw-alias-bg-layer-2);position:relative}',
|
|
292
|
+
'.lfTargetOff{opacity:.6}',
|
|
293
|
+
'.lfTargetHead{display:flex;align-items:center;gap:8px}',
|
|
294
|
+
'.lfLabelInput{flex:1;width:auto;min-width:120px}',
|
|
295
|
+
'.lfFields{display:flex;flex-wrap:wrap;gap:10px}',
|
|
296
|
+
'.lfField{display:flex;flex-direction:column;gap:3px;font-size:11px;color:var(--dsw-alias-label-tertiary)}',
|
|
297
|
+
'.lfField .ccInput{width:96px}',
|
|
298
|
+
'.lfFieldWide .ccInput{width:160px}',
|
|
299
|
+
'.lfFieldWide .lfStateInput{width:300px}',
|
|
300
|
+
'.lfSelect{width:180px;max-width:180px;text-overflow:ellipsis}',
|
|
301
|
+
'.lfFoot{display:flex;align-items:center;gap:8px;flex-wrap:wrap}',
|
|
302
|
+
'.lfSummary{color:var(--dsw-alias-label-tertiary);font-size:11px;line-height:1.5}',
|
|
303
|
+
'.lfTipWrap{position:relative;display:inline-flex}',
|
|
304
|
+
'.lfTipPanel{position:fixed;z-index:200;box-sizing:border-box;width:260px;max-height:340px;overflow:auto;background:var(--dsw-alias-bg-layer-1);border:.5px solid var(--dsw-alias-border-l2);--dsw-elevation-stroke-color:var(--dsw-alias-border-l1);box-shadow:var(--dsw-elevation-prominent);border-radius:10px;padding:10px;font-size:12px;line-height:18px;color:var(--dsw-alias-label-secondary);text-align:left}',
|
|
305
|
+
'.lfTipTitle{margin:0 0 2px;color:var(--dsw-alias-label-primary);font-size:12px;font-weight:500}',
|
|
306
|
+
'.lfTipSub{margin:0 0 6px;color:var(--dsw-alias-label-tertiary);font-size:11px;line-height:1.5}',
|
|
307
|
+
'.lfTipRow{display:flex;align-items:baseline;justify-content:space-between;gap:16px;font-variant-numeric:tabular-nums}',
|
|
308
|
+
'.lfTipVal{color:var(--dsw-alias-label-primary)}',
|
|
309
|
+
'.lfTipFoot{margin:6px 0 0;color:var(--dsw-alias-label-tertiary);font-size:11px;line-height:1.5}',
|
|
310
|
+
'.lfActions{display:flex;align-items:center;gap:10px;flex-wrap:wrap}',
|
|
311
|
+
'.lfPrimary{font:inherit;font-size:12px;height:28px;padding:0 12px;border-radius:8px;border:none;background:var(--dsw-alias-brand-primary);color:var(--dsw-alias-label-primary-inverted);cursor:pointer}',
|
|
312
|
+
'.lfPrimary:hover:not(:disabled){background:var(--dsw-alias-button-primary-hover)}',
|
|
313
|
+
'.lfPrimary:disabled{cursor:default;opacity:.5}',
|
|
314
|
+
'.lfOffTag{color:var(--dsw-alias-label-tertiary);font-size:11px;border:.5px solid var(--dsw-alias-border-l4);border-radius:999px;padding:0 6px}',
|
|
315
|
+
'.lfLiveRows{display:flex;flex-direction:column;gap:6px}',
|
|
316
|
+
'.lfLiveRow{display:flex;align-items:baseline;gap:8px;font-size:12px;font-variant-numeric:tabular-nums}',
|
|
317
|
+
'.lfLiveDot{width:8px;height:8px;border-radius:999px;flex:none;align-self:center;background:var(--dsw-alias-label-tertiary)}',
|
|
318
|
+
'.lfLiveDotClosed{background:var(--dsw-alias-state-success-primary)}',
|
|
319
|
+
'.lfLiveDotHalfOpen,.lfLiveDotProbing{background:var(--dsw-alias-state-warn-primary)}',
|
|
320
|
+
'.lfLiveDotOpen{background:var(--dsw-alias-state-error-primary)}',
|
|
321
|
+
'.lfLiveName{color:var(--dsw-alias-label-primary);overflow:hidden;text-overflow:ellipsis;white-space:nowrap;max-width:40%}',
|
|
322
|
+
'.lfLiveState{color:var(--dsw-alias-label-secondary)}',
|
|
323
|
+
'.lfLiveDetail{color:var(--dsw-alias-label-secondary);margin-left:auto}',
|
|
324
|
+
'.lfLiveCounts{color:var(--dsw-alias-label-tertiary)}',
|
|
325
|
+
].join('');
|
|
326
|
+
|
|
327
|
+
if (
|
|
328
|
+
typeof document !== 'undefined' &&
|
|
329
|
+
document.querySelector('style[data-plugin-css="dsh-llm-resilience"]') === null
|
|
330
|
+
) {
|
|
331
|
+
const tag = document.createElement('style');
|
|
332
|
+
tag.dataset.plugin = 'dsh-llm-resilience';
|
|
333
|
+
tag.dataset.pluginCss = 'dsh-llm-resilience';
|
|
334
|
+
tag.textContent = css;
|
|
335
|
+
document.head.appendChild(tag);
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
// ── 纯函数:与 host 侧 lib/policy.js 同一套公式 ──────────────────────
|
|
339
|
+
// 这段文件被原样下发,不能 import;`test/client.test.js` 用 policy.js 的
|
|
340
|
+
// nominalSchedule 钉住这里的计算,任何一边改了公式都会失败。
|
|
341
|
+
|
|
342
|
+
const DEFAULTS = Object.freeze({
|
|
343
|
+
maxRetries: 5,
|
|
344
|
+
initialDelayMs: 1500,
|
|
345
|
+
multiplier: 2,
|
|
346
|
+
maxDelayMs: 20000,
|
|
347
|
+
jitterRatio: 0.3,
|
|
348
|
+
failureThreshold: 5,
|
|
349
|
+
lockMs: 3600000,
|
|
350
|
+
probeTimeoutMs: 10000,
|
|
351
|
+
probeMaxTokens: 1,
|
|
352
|
+
probePrompt: 'ping',
|
|
353
|
+
probeBackground: false,
|
|
354
|
+
maxTotalMs: 600000,
|
|
355
|
+
maxAttempts: 50,
|
|
356
|
+
stateFile: '$DSH_HOME/storages/llm-resilience/state.json',
|
|
357
|
+
});
|
|
358
|
+
|
|
359
|
+
/** 预览最多渲染多少行;超出部分只显示总数,避免一个巨大的数字卡住页面。 */
|
|
360
|
+
const PREVIEW_LIMIT = 50;
|
|
361
|
+
|
|
362
|
+
/** 把毫秒格式化成紧凑的可读时长;保留到 0.1s / 0.1min。 */
|
|
363
|
+
function formatMs(ms) {
|
|
364
|
+
const value = Number(ms);
|
|
365
|
+
if (!Number.isFinite(value) || value < 0) return '—';
|
|
366
|
+
if (value < 1000) return `${Math.round(value)}ms`;
|
|
367
|
+
if (value < 60000) {
|
|
368
|
+
const seconds = Math.round(value / 100) / 10;
|
|
369
|
+
return `${seconds}s`;
|
|
370
|
+
}
|
|
371
|
+
const minutes = Math.round(value / 6000) / 10;
|
|
372
|
+
return `${minutes}min`;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
/** 运行状态的“多久之前”;只用于粗粒度新鲜度,不做倒计时。 */
|
|
376
|
+
function formatAge(ms) {
|
|
377
|
+
const value = Number(ms);
|
|
378
|
+
if (!Number.isFinite(value) || value < 0) return '—';
|
|
379
|
+
if (value < 1000) return '<1s';
|
|
380
|
+
if (value < 60000) return `${Math.round(value / 1000)}s`;
|
|
381
|
+
return `${Math.round(value / 60000)}min`;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
const LIVE_STATE_KEYS = Object.freeze({
|
|
385
|
+
closed: 'liveStateClosed',
|
|
386
|
+
open: 'liveStateOpen',
|
|
387
|
+
'half-open': 'liveStateHalfOpen',
|
|
388
|
+
probing: 'liveStateProbing',
|
|
389
|
+
});
|
|
390
|
+
|
|
391
|
+
function liveDotClass(state) {
|
|
392
|
+
if (state === 'closed') return 'lfLiveDot lfLiveDotClosed';
|
|
393
|
+
if (state === 'open') return 'lfLiveDot lfLiveDotOpen';
|
|
394
|
+
if (state === 'half-open') return 'lfLiveDot lfLiveDotHalfOpen';
|
|
395
|
+
if (state === 'probing') return 'lfLiveDot lfLiveDotProbing';
|
|
396
|
+
return 'lfLiveDot';
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
function textOr(value, fallback) {
|
|
400
|
+
return value === undefined || value === null ? String(fallback) : String(value);
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
/** 解析草稿里的数字:空串/非法值回落到 fallback,供预览安全降级。 */
|
|
404
|
+
function numberOr(text, fallback) {
|
|
405
|
+
const value = Number(text);
|
|
406
|
+
return text === '' || Number.isFinite(value) ? (text === '' ? fallback : value) : fallback;
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
/**
|
|
410
|
+
* 一个 target 的重试计划(名义值 + 抖动区间)。
|
|
411
|
+
*
|
|
412
|
+
* 公式与 host 侧一致:先 `round(initial * multiplier^(n-1))`,再夹到
|
|
413
|
+
* maxDelayMs;抖动区间两端四舍五入,上端同样夹到 maxDelayMs。
|
|
414
|
+
*
|
|
415
|
+
* @param target - 草稿或配置里的 target(数字可以是字符串)。
|
|
416
|
+
* @returns `{ rows, totalMs, retries, truncated }`。
|
|
417
|
+
*/
|
|
418
|
+
function retryPlanRows(target) {
|
|
419
|
+
const retries = Math.max(
|
|
420
|
+
0,
|
|
421
|
+
Math.floor(numberOr(target?.maxRetries, DEFAULTS.maxRetries)),
|
|
422
|
+
);
|
|
423
|
+
const initial = Math.max(1, numberOr(target?.initialDelayMs, DEFAULTS.initialDelayMs));
|
|
424
|
+
const multiplier = Math.max(1.01, numberOr(target?.multiplier, DEFAULTS.multiplier));
|
|
425
|
+
const maxDelay = Math.max(initial, numberOr(target?.maxDelayMs, DEFAULTS.maxDelayMs));
|
|
426
|
+
const jitter = Math.min(1, Math.max(0, numberOr(target?.jitterRatio, DEFAULTS.jitterRatio)));
|
|
427
|
+
|
|
428
|
+
const rows = [];
|
|
429
|
+
let totalMs = 0;
|
|
430
|
+
const shown = Math.min(retries, PREVIEW_LIMIT);
|
|
431
|
+
for (let index = 0; index < shown; index += 1) {
|
|
432
|
+
const nominalMs = Math.min(Math.round(initial * multiplier ** index), maxDelay);
|
|
433
|
+
totalMs += nominalMs;
|
|
434
|
+
rows.push({
|
|
435
|
+
attempt: index + 1,
|
|
436
|
+
nominalMs,
|
|
437
|
+
lowMs: Math.round(nominalMs * (1 - jitter)),
|
|
438
|
+
highMs: Math.min(Math.round(nominalMs * (1 + jitter)), maxDelay),
|
|
439
|
+
});
|
|
440
|
+
}
|
|
441
|
+
return { rows, retries, totalMs, truncated: retries > shown };
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
/** 配置值 → 可编辑草稿(数字一律变成字符串,便于输入中间态)。 */
|
|
445
|
+
function configDraft(value) {
|
|
446
|
+
const resolved = value ?? {};
|
|
447
|
+
const breaker = resolved.breaker ?? {};
|
|
448
|
+
const probe = breaker.probe ?? {};
|
|
449
|
+
const lockMs =
|
|
450
|
+
Number.isFinite(Number(breaker.lockMs)) && Number(breaker.lockMs) > 0
|
|
451
|
+
? Number(breaker.lockMs)
|
|
452
|
+
: DEFAULTS.lockMs;
|
|
453
|
+
return {
|
|
454
|
+
enabled: resolved.enabled !== false,
|
|
455
|
+
targets: (Array.isArray(resolved.targets) ? resolved.targets : []).map((target) => ({
|
|
456
|
+
enabled: target?.enabled !== false,
|
|
457
|
+
provider: target?.provider ?? '',
|
|
458
|
+
model: target?.model ?? '',
|
|
459
|
+
label: target?.label ?? '',
|
|
460
|
+
maxRetries: textOr(target?.maxRetries, DEFAULTS.maxRetries),
|
|
461
|
+
initialDelayMs: textOr(target?.initialDelayMs, DEFAULTS.initialDelayMs),
|
|
462
|
+
multiplier: textOr(target?.multiplier, DEFAULTS.multiplier),
|
|
463
|
+
maxDelayMs: textOr(target?.maxDelayMs, DEFAULTS.maxDelayMs),
|
|
464
|
+
jitterRatio: textOr(target?.jitterRatio, DEFAULTS.jitterRatio),
|
|
465
|
+
})),
|
|
466
|
+
failureThreshold: textOr(breaker.failureThreshold, DEFAULTS.failureThreshold),
|
|
467
|
+
lockMinutes: textOr(Math.round(lockMs / 6000) / 10, DEFAULTS.lockMs / 60000),
|
|
468
|
+
probeEnabled: probe.enabled !== false,
|
|
469
|
+
probeBackground: probe.background === true,
|
|
470
|
+
probeTimeoutMs: textOr(probe.timeoutMs, DEFAULTS.probeTimeoutMs),
|
|
471
|
+
probeMaxTokens: textOr(probe.maxTokens, DEFAULTS.probeMaxTokens),
|
|
472
|
+
probePrompt: probe.prompt ?? DEFAULTS.probePrompt,
|
|
473
|
+
maxTotalMs: textOr(resolved.limits?.maxTotalMs, DEFAULTS.maxTotalMs),
|
|
474
|
+
maxAttempts: textOr(resolved.limits?.maxAttempts, DEFAULTS.maxAttempts),
|
|
475
|
+
stateFile: breaker.stateFile ?? DEFAULTS.stateFile,
|
|
476
|
+
retryMalformed: resolved.toolCalls?.retryMalformed !== false,
|
|
477
|
+
};
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
/** 草稿 → 写回配置的 target;数值已通过校验,这里直接 Number()。 */
|
|
481
|
+
function storedTarget(target) {
|
|
482
|
+
return {
|
|
483
|
+
enabled: target.enabled !== false,
|
|
484
|
+
provider: target.provider.trim(),
|
|
485
|
+
model: target.model.trim(),
|
|
486
|
+
label: target.label.trim(),
|
|
487
|
+
maxRetries: Number(target.maxRetries),
|
|
488
|
+
initialDelayMs: Number(target.initialDelayMs),
|
|
489
|
+
multiplier: Number(target.multiplier),
|
|
490
|
+
maxDelayMs: Number(target.maxDelayMs),
|
|
491
|
+
jitterRatio: Number(target.jitterRatio),
|
|
492
|
+
};
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
function defaultTarget() {
|
|
496
|
+
return {
|
|
497
|
+
enabled: true,
|
|
498
|
+
provider: '',
|
|
499
|
+
model: '',
|
|
500
|
+
label: '',
|
|
501
|
+
maxRetries: String(DEFAULTS.maxRetries),
|
|
502
|
+
initialDelayMs: String(DEFAULTS.initialDelayMs),
|
|
503
|
+
multiplier: String(DEFAULTS.multiplier),
|
|
504
|
+
maxDelayMs: String(DEFAULTS.maxDelayMs),
|
|
505
|
+
jitterRatio: String(DEFAULTS.jitterRatio),
|
|
506
|
+
};
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
/** 会话模型目录 → `Map<provider, Set<model>>`;目录没拿到时不返回索引。 */
|
|
510
|
+
function catalogIndex(catalog) {
|
|
511
|
+
const providers = new Map();
|
|
512
|
+
for (const group of Array.isArray(catalog?.groups) ? catalog.groups : []) {
|
|
513
|
+
if (typeof group?.id !== 'string' || group.id === '') continue;
|
|
514
|
+
providers.set(
|
|
515
|
+
group.id,
|
|
516
|
+
new Set(
|
|
517
|
+
(Array.isArray(group.models) ? group.models : [])
|
|
518
|
+
.map((model) => model?.id)
|
|
519
|
+
.filter((id) => typeof id === 'string' && id !== ''),
|
|
520
|
+
),
|
|
521
|
+
);
|
|
522
|
+
}
|
|
523
|
+
return providers;
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
/**
|
|
527
|
+
* 校验整张草稿。
|
|
528
|
+
*
|
|
529
|
+
* 只校验**启用**的 target 的 provider/model(禁用条目可以暂时留空,稍后再补);
|
|
530
|
+
* 数值字段对所有条目都校验,因为写回时它们必须是合法数字。返回翻译键 + 参数,
|
|
531
|
+
* 由卡片按当前语言渲染。传入 `catalog` 时,provider/model 必须真实存在于会话
|
|
532
|
+
* 模型目录中,防止手填出无法使用的路由。
|
|
533
|
+
*
|
|
534
|
+
* @returns `[{ key, params }]`。
|
|
535
|
+
*/
|
|
536
|
+
function validateDraft(draft, route, catalog) {
|
|
537
|
+
const errors = [];
|
|
538
|
+
const seen = new Set();
|
|
539
|
+
const known = catalog === undefined || catalog === null ? undefined : catalogIndex(catalog);
|
|
540
|
+
draft.targets.forEach((target, index) => {
|
|
541
|
+
const n = index + 1;
|
|
542
|
+
const provider = target.provider.trim();
|
|
543
|
+
const model = target.model.trim();
|
|
544
|
+
if (target.enabled !== false) {
|
|
545
|
+
if (provider === '') errors.push({ key: 'errProvider', params: { n } });
|
|
546
|
+
if (model === '') errors.push({ key: 'errModel', params: { n } });
|
|
547
|
+
if (provider === route) errors.push({ key: 'errSelf', params: { n } });
|
|
548
|
+
if (known !== undefined && provider !== '' && provider !== route && !known.has(provider)) {
|
|
549
|
+
errors.push({ key: 'errProviderUnknown', params: { n } });
|
|
550
|
+
}
|
|
551
|
+
if (
|
|
552
|
+
known !== undefined &&
|
|
553
|
+
provider !== '' &&
|
|
554
|
+
provider !== route &&
|
|
555
|
+
known.has(provider) &&
|
|
556
|
+
model !== '' &&
|
|
557
|
+
!known.get(provider).has(model)
|
|
558
|
+
) {
|
|
559
|
+
errors.push({ key: 'errModelUnknown', params: { n } });
|
|
560
|
+
}
|
|
561
|
+
const key = `${provider}\u0000${model}`;
|
|
562
|
+
if (seen.has(key)) errors.push({ key: 'errDuplicate', params: { n } });
|
|
563
|
+
seen.add(key);
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
const maxRetries = Number(target.maxRetries);
|
|
567
|
+
if (target.maxRetries === '' || !Number.isSafeInteger(maxRetries) || maxRetries < 0) {
|
|
568
|
+
errors.push({ key: 'errMaxRetries', params: { n } });
|
|
569
|
+
}
|
|
570
|
+
const initial = Number(target.initialDelayMs);
|
|
571
|
+
if (target.initialDelayMs === '' || !Number.isFinite(initial) || initial <= 0) {
|
|
572
|
+
errors.push({ key: 'errInitialDelay', params: { n } });
|
|
573
|
+
}
|
|
574
|
+
const multiplier = Number(target.multiplier);
|
|
575
|
+
if (target.multiplier === '' || !Number.isFinite(multiplier) || multiplier <= 1) {
|
|
576
|
+
errors.push({ key: 'errMultiplier', params: { n } });
|
|
577
|
+
}
|
|
578
|
+
const maxDelay = Number(target.maxDelayMs);
|
|
579
|
+
if (target.maxDelayMs === '' || !Number.isFinite(maxDelay) || maxDelay < initial) {
|
|
580
|
+
errors.push({ key: 'errMaxDelay', params: { n } });
|
|
581
|
+
}
|
|
582
|
+
const jitter = Number(target.jitterRatio);
|
|
583
|
+
if (target.jitterRatio === '' || !Number.isFinite(jitter) || jitter < 0 || jitter > 1) {
|
|
584
|
+
errors.push({ key: 'errJitter', params: { n } });
|
|
585
|
+
}
|
|
586
|
+
});
|
|
587
|
+
|
|
588
|
+
const threshold = Number(draft.failureThreshold);
|
|
589
|
+
if (draft.failureThreshold === '' || !Number.isSafeInteger(threshold) || threshold < 1) {
|
|
590
|
+
errors.push({ key: 'errFailureThreshold' });
|
|
591
|
+
}
|
|
592
|
+
const lock = Number(draft.lockMinutes);
|
|
593
|
+
if (draft.lockMinutes === '' || !Number.isFinite(lock) || lock <= 0) {
|
|
594
|
+
errors.push({ key: 'errLock' });
|
|
595
|
+
}
|
|
596
|
+
const probeTimeout = Number(draft.probeTimeoutMs);
|
|
597
|
+
if (draft.probeTimeoutMs === '' || !Number.isFinite(probeTimeout) || probeTimeout <= 0) {
|
|
598
|
+
errors.push({ key: 'errProbeTimeout' });
|
|
599
|
+
}
|
|
600
|
+
const probeMaxTokens = Number(draft.probeMaxTokens);
|
|
601
|
+
if (draft.probeMaxTokens === '' || !Number.isSafeInteger(probeMaxTokens) || probeMaxTokens < 1) {
|
|
602
|
+
errors.push({ key: 'errProbeMaxTokens' });
|
|
603
|
+
}
|
|
604
|
+
if (draft.probePrompt.trim() === '') errors.push({ key: 'errProbePrompt' });
|
|
605
|
+
const maxTotalMs = Number(draft.maxTotalMs);
|
|
606
|
+
if (draft.maxTotalMs === '' || !Number.isFinite(maxTotalMs) || maxTotalMs < 0) {
|
|
607
|
+
errors.push({ key: 'errMaxTotalMs' });
|
|
608
|
+
}
|
|
609
|
+
const maxAttempts = Number(draft.maxAttempts);
|
|
610
|
+
if (draft.maxAttempts === '' || !Number.isSafeInteger(maxAttempts) || maxAttempts < 0) {
|
|
611
|
+
errors.push({ key: 'errMaxAttempts' });
|
|
612
|
+
}
|
|
613
|
+
if (draft.stateFile.trim() === '') errors.push({ key: 'errStateFile' });
|
|
614
|
+
|
|
615
|
+
return errors;
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
/** 草稿 → 一次写入的全部 ops(一个 revision、一次原子提交)。 */
|
|
619
|
+
function buildOps(draft) {
|
|
620
|
+
return [
|
|
621
|
+
{ op: 'set', path: ['enabled'], value: draft.enabled },
|
|
622
|
+
{ op: 'set', path: ['targets'], value: draft.targets.map(storedTarget) },
|
|
623
|
+
{
|
|
624
|
+
op: 'set',
|
|
625
|
+
path: ['breaker'],
|
|
626
|
+
value: {
|
|
627
|
+
failureThreshold: Number(draft.failureThreshold),
|
|
628
|
+
lockMs: Math.round(Number(draft.lockMinutes) * 60000),
|
|
629
|
+
probe: {
|
|
630
|
+
enabled: draft.probeEnabled,
|
|
631
|
+
background: draft.probeBackground,
|
|
632
|
+
timeoutMs: Number(draft.probeTimeoutMs),
|
|
633
|
+
maxTokens: Number(draft.probeMaxTokens),
|
|
634
|
+
prompt: draft.probePrompt,
|
|
635
|
+
},
|
|
636
|
+
stateFile: draft.stateFile.trim(),
|
|
637
|
+
},
|
|
638
|
+
},
|
|
639
|
+
{
|
|
640
|
+
op: 'set',
|
|
641
|
+
path: ['limits'],
|
|
642
|
+
value: {
|
|
643
|
+
maxTotalMs: Number(draft.maxTotalMs),
|
|
644
|
+
maxAttempts: Number(draft.maxAttempts),
|
|
645
|
+
},
|
|
646
|
+
},
|
|
647
|
+
{
|
|
648
|
+
op: 'set',
|
|
649
|
+
path: ['toolCalls'],
|
|
650
|
+
value: { retryMalformed: draft.retryMalformed },
|
|
651
|
+
},
|
|
652
|
+
];
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
function moveIn(entries, index, delta) {
|
|
656
|
+
const next = [...entries];
|
|
657
|
+
const [moved] = next.splice(index, 1);
|
|
658
|
+
next.splice(index + delta, 0, moved);
|
|
659
|
+
return next;
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
// ── 组件 ──────────────────────────────────────────────────────────────
|
|
663
|
+
|
|
664
|
+
/** 一行一次重试;悬停/聚焦/点击展开。 */
|
|
665
|
+
function PlanTip(props) {
|
|
666
|
+
const { target, t } = props;
|
|
667
|
+
const [open, setOpen] = react.useState(false);
|
|
668
|
+
const [anchor, setAnchor] = react.useState(null);
|
|
669
|
+
const plan = retryPlanRows(target);
|
|
670
|
+
const range = plan.rows.length > 0 && Number(target.jitterRatio) > 0;
|
|
671
|
+
const panelWidth = 260;
|
|
672
|
+
|
|
673
|
+
/**
|
|
674
|
+
* 用视口坐标而不是父元素的 absolute 定位:设置弹窗会滚动、也会把
|
|
675
|
+
* 后一个 section 画在前面,absolute 面板会被裁掉或压在字下面。
|
|
676
|
+
* 下方空间不够时翻到按钮上方。
|
|
677
|
+
*/
|
|
678
|
+
const show = (element) => {
|
|
679
|
+
if (element === null || typeof element === 'undefined' || typeof window === 'undefined') {
|
|
680
|
+
setOpen(true);
|
|
681
|
+
return;
|
|
682
|
+
}
|
|
683
|
+
const rect = element.getBoundingClientRect();
|
|
684
|
+
const spaceBelow = window.innerHeight - rect.bottom - 12;
|
|
685
|
+
const spaceAbove = rect.top - 12;
|
|
686
|
+
const above = spaceBelow < 240 && spaceAbove > spaceBelow;
|
|
687
|
+
setAnchor({
|
|
688
|
+
left: Math.max(8, Math.min(rect.left, window.innerWidth - panelWidth - 8)),
|
|
689
|
+
top: above ? undefined : rect.bottom + 6,
|
|
690
|
+
bottom: above ? window.innerHeight - rect.top + 6 : undefined,
|
|
691
|
+
maxHeight: Math.max(120, Math.min(340, above ? spaceAbove : spaceBelow)),
|
|
692
|
+
});
|
|
693
|
+
setOpen(true);
|
|
694
|
+
};
|
|
695
|
+
const hide = () => {
|
|
696
|
+
setOpen(false);
|
|
697
|
+
setAnchor(null);
|
|
698
|
+
};
|
|
699
|
+
|
|
700
|
+
return jsx.jsxs('span', {
|
|
701
|
+
className: 'lfTipWrap',
|
|
702
|
+
onMouseEnter: (event) => show(event.currentTarget),
|
|
703
|
+
onMouseLeave: hide,
|
|
704
|
+
onFocus: (event) => show(event.currentTarget),
|
|
705
|
+
onBlur: (event) => {
|
|
706
|
+
if (!event.currentTarget.contains(event.relatedTarget)) hide();
|
|
707
|
+
},
|
|
708
|
+
children: [
|
|
709
|
+
jsx.jsx('button', {
|
|
710
|
+
type: 'button',
|
|
711
|
+
className: 'ccReset',
|
|
712
|
+
'aria-expanded': open,
|
|
713
|
+
onClick: (event) => (open ? hide() : show(event.currentTarget)),
|
|
714
|
+
children: t('plan'),
|
|
715
|
+
}),
|
|
716
|
+
open
|
|
717
|
+
? jsx.jsxs('div', {
|
|
718
|
+
className: 'lfTipPanel',
|
|
719
|
+
role: 'tooltip',
|
|
720
|
+
style:
|
|
721
|
+
anchor === null
|
|
722
|
+
? undefined
|
|
723
|
+
: {
|
|
724
|
+
left: anchor.left,
|
|
725
|
+
top: anchor.top,
|
|
726
|
+
bottom: anchor.bottom,
|
|
727
|
+
maxHeight: anchor.maxHeight,
|
|
728
|
+
},
|
|
729
|
+
children: [
|
|
730
|
+
jsx.jsx('p', { className: 'lfTipTitle', children: t('planTitle') }),
|
|
731
|
+
plan.rows.length === 0
|
|
732
|
+
? jsx.jsx('p', { className: 'lfTipSub', children: t('planNone') })
|
|
733
|
+
: jsx.jsxs('p', {
|
|
734
|
+
className: 'lfTipSub',
|
|
735
|
+
children: t('planSub', { n: plan.retries }),
|
|
736
|
+
}),
|
|
737
|
+
...plan.rows.map((row) =>
|
|
738
|
+
jsx.jsxs(
|
|
739
|
+
'div',
|
|
740
|
+
{
|
|
741
|
+
className: 'lfTipRow',
|
|
742
|
+
children: [
|
|
743
|
+
jsx.jsx('span', { children: t('planRow', { n: row.attempt }) }),
|
|
744
|
+
jsx.jsx('span', {
|
|
745
|
+
className: 'lfTipVal',
|
|
746
|
+
children: range
|
|
747
|
+
? t('planRange', {
|
|
748
|
+
low: formatMs(row.lowMs),
|
|
749
|
+
high: formatMs(row.highMs),
|
|
750
|
+
})
|
|
751
|
+
: formatMs(row.nominalMs),
|
|
752
|
+
}),
|
|
753
|
+
],
|
|
754
|
+
},
|
|
755
|
+
row.attempt,
|
|
756
|
+
),
|
|
757
|
+
),
|
|
758
|
+
plan.truncated
|
|
759
|
+
? jsx.jsx('p', {
|
|
760
|
+
className: 'lfTipFoot',
|
|
761
|
+
children: t('planTruncated', { n: plan.retries }),
|
|
762
|
+
})
|
|
763
|
+
: null,
|
|
764
|
+
plan.rows.length === 0
|
|
765
|
+
? null
|
|
766
|
+
: plan.truncated
|
|
767
|
+
? null
|
|
768
|
+
: jsx.jsx('p', {
|
|
769
|
+
className: 'lfTipFoot',
|
|
770
|
+
children: [
|
|
771
|
+
t('planTotal', { total: formatMs(plan.totalMs) }),
|
|
772
|
+
range ? ` · ${t('planJitterNote')}` : '',
|
|
773
|
+
].join(''),
|
|
774
|
+
}),
|
|
775
|
+
],
|
|
776
|
+
})
|
|
777
|
+
: null,
|
|
778
|
+
],
|
|
779
|
+
});
|
|
780
|
+
}
|
|
781
|
+
|
|
782
|
+
/** 一个数值字段:小标签 + 输入框。 */
|
|
783
|
+
function Field(props) {
|
|
784
|
+
const { label, value, invalid, disabled, wide, onChange } = props;
|
|
785
|
+
return jsx.jsxs('label', {
|
|
786
|
+
className: wide ? 'lfField lfFieldWide' : 'lfField',
|
|
787
|
+
children: [
|
|
788
|
+
jsx.jsx('span', { children: label }),
|
|
789
|
+
jsx.jsx('input', {
|
|
790
|
+
className: invalid ? 'ccInput ccInputInvalid' : 'ccInput',
|
|
791
|
+
inputMode: 'decimal',
|
|
792
|
+
value,
|
|
793
|
+
disabled,
|
|
794
|
+
'aria-label': label,
|
|
795
|
+
onChange: (event) => onChange(event.target.value),
|
|
796
|
+
}),
|
|
797
|
+
],
|
|
798
|
+
});
|
|
799
|
+
}
|
|
800
|
+
|
|
801
|
+
/** 一个 target 的编辑块。 */
|
|
802
|
+
function TargetRow(props) {
|
|
803
|
+
const {
|
|
804
|
+
target,
|
|
805
|
+
index,
|
|
806
|
+
total,
|
|
807
|
+
t,
|
|
808
|
+
disabled,
|
|
809
|
+
providerChoices,
|
|
810
|
+
modelChoicesOf,
|
|
811
|
+
choicesDisabled,
|
|
812
|
+
onEdit,
|
|
813
|
+
onMove,
|
|
814
|
+
onRemove,
|
|
815
|
+
} = props;
|
|
816
|
+
const modelChoices = modelChoicesOf(target.provider);
|
|
817
|
+
const knownModel = modelChoices.some((choice) => choice.id === target.model);
|
|
818
|
+
const knownProvider = providerChoices.some((choice) => choice.id === target.provider);
|
|
819
|
+
const plan = retryPlanRows(target);
|
|
820
|
+
const summary = plan.rows.slice(0, 4).map((row) => formatMs(row.nominalMs)).join(' → ');
|
|
821
|
+
const off = target.enabled === false;
|
|
822
|
+
|
|
823
|
+
return jsx.jsxs('div', {
|
|
824
|
+
className: off ? 'lfTarget lfTargetOff' : 'lfTarget',
|
|
825
|
+
children: [
|
|
826
|
+
jsx.jsxs('div', {
|
|
827
|
+
className: 'lfTargetHead',
|
|
828
|
+
children: [
|
|
829
|
+
jsx.jsx('span', { className: 'ccChainIndex', children: String(index + 1) }),
|
|
830
|
+
jsx.jsx('input', {
|
|
831
|
+
type: 'checkbox',
|
|
832
|
+
className: 'ccCheck',
|
|
833
|
+
checked: !off,
|
|
834
|
+
disabled,
|
|
835
|
+
'aria-label': t('enableTarget'),
|
|
836
|
+
onChange: (event) => onEdit({ enabled: event.target.checked }),
|
|
837
|
+
}),
|
|
838
|
+
jsx.jsx('input', {
|
|
839
|
+
className: 'ccInput lfLabelInput',
|
|
840
|
+
value: target.label,
|
|
841
|
+
disabled,
|
|
842
|
+
placeholder: t('labelPlaceholder'),
|
|
843
|
+
'aria-label': t('labelPlaceholder'),
|
|
844
|
+
onChange: (event) => onEdit({ label: event.target.value }),
|
|
845
|
+
}),
|
|
846
|
+
off ? jsx.jsx('span', { className: 'lfOffTag', children: t('disable') }) : null,
|
|
847
|
+
jsx.jsx('button', {
|
|
848
|
+
type: 'button',
|
|
849
|
+
className: 'ccReset',
|
|
850
|
+
disabled: disabled || index === 0,
|
|
851
|
+
'aria-label': t('moveUp'),
|
|
852
|
+
onClick: () => onMove(-1),
|
|
853
|
+
children: '↑',
|
|
854
|
+
}),
|
|
855
|
+
jsx.jsx('button', {
|
|
856
|
+
type: 'button',
|
|
857
|
+
className: 'ccReset',
|
|
858
|
+
disabled: disabled || index === total - 1,
|
|
859
|
+
'aria-label': t('moveDown'),
|
|
860
|
+
onClick: () => onMove(1),
|
|
861
|
+
children: '↓',
|
|
862
|
+
}),
|
|
863
|
+
jsx.jsx('button', {
|
|
864
|
+
type: 'button',
|
|
865
|
+
className: 'ccReset',
|
|
866
|
+
disabled,
|
|
867
|
+
onClick: onRemove,
|
|
868
|
+
children: t('remove'),
|
|
869
|
+
}),
|
|
870
|
+
],
|
|
871
|
+
}),
|
|
872
|
+
jsx.jsxs('div', {
|
|
873
|
+
className: 'lfFields',
|
|
874
|
+
children: [
|
|
875
|
+
jsx.jsxs('label', {
|
|
876
|
+
className: 'lfField lfFieldWide',
|
|
877
|
+
children: [
|
|
878
|
+
jsx.jsx('span', { children: t('provider') }),
|
|
879
|
+
jsx.jsxs('select', {
|
|
880
|
+
className:
|
|
881
|
+
knownProvider || target.provider === ''
|
|
882
|
+
? 'ccSelect lfSelect'
|
|
883
|
+
: 'ccSelect lfSelect ccInputInvalid',
|
|
884
|
+
value: target.provider,
|
|
885
|
+
disabled: disabled || choicesDisabled,
|
|
886
|
+
'aria-label': t('provider'),
|
|
887
|
+
onChange: (event) => onEdit({ provider: event.target.value }),
|
|
888
|
+
children: [
|
|
889
|
+
target.provider === ''
|
|
890
|
+
? jsx.jsx('option', {
|
|
891
|
+
value: '',
|
|
892
|
+
children: t('selectPlaceholder'),
|
|
893
|
+
})
|
|
894
|
+
: null,
|
|
895
|
+
knownProvider || target.provider === ''
|
|
896
|
+
? null
|
|
897
|
+
: jsx.jsx('option', {
|
|
898
|
+
value: target.provider,
|
|
899
|
+
children: `${target.provider} ${t('notInCatalog')}`,
|
|
900
|
+
}),
|
|
901
|
+
...providerChoices.map((choice) =>
|
|
902
|
+
jsx.jsx(
|
|
903
|
+
'option',
|
|
904
|
+
{ value: choice.id, children: choice.name },
|
|
905
|
+
choice.id,
|
|
906
|
+
),
|
|
907
|
+
),
|
|
908
|
+
],
|
|
909
|
+
}),
|
|
910
|
+
],
|
|
911
|
+
}),
|
|
912
|
+
jsx.jsxs('label', {
|
|
913
|
+
className: 'lfField lfFieldWide',
|
|
914
|
+
children: [
|
|
915
|
+
jsx.jsx('span', { children: t('model') }),
|
|
916
|
+
jsx.jsxs('select', {
|
|
917
|
+
className:
|
|
918
|
+
knownModel || target.model === ''
|
|
919
|
+
? 'ccSelect lfSelect'
|
|
920
|
+
: 'ccSelect lfSelect ccInputInvalid',
|
|
921
|
+
value: target.model,
|
|
922
|
+
disabled:
|
|
923
|
+
disabled ||
|
|
924
|
+
choicesDisabled ||
|
|
925
|
+
target.provider === '' ||
|
|
926
|
+
modelChoices.length === 0,
|
|
927
|
+
'aria-label': t('model'),
|
|
928
|
+
onChange: (event) => onEdit({ model: event.target.value }),
|
|
929
|
+
children: [
|
|
930
|
+
target.model === ''
|
|
931
|
+
? jsx.jsx('option', {
|
|
932
|
+
value: '',
|
|
933
|
+
children: t('selectPlaceholder'),
|
|
934
|
+
})
|
|
935
|
+
: null,
|
|
936
|
+
knownModel || target.model === ''
|
|
937
|
+
? null
|
|
938
|
+
: jsx.jsx('option', {
|
|
939
|
+
value: target.model,
|
|
940
|
+
children: `${target.model} ${t('notInCatalog')}`,
|
|
941
|
+
}),
|
|
942
|
+
...modelChoices.map((choice) =>
|
|
943
|
+
jsx.jsx(
|
|
944
|
+
'option',
|
|
945
|
+
{ value: choice.id, children: choice.name },
|
|
946
|
+
choice.id,
|
|
947
|
+
),
|
|
948
|
+
),
|
|
949
|
+
],
|
|
950
|
+
}),
|
|
951
|
+
],
|
|
952
|
+
}),
|
|
953
|
+
],
|
|
954
|
+
}),
|
|
955
|
+
jsx.jsxs('div', {
|
|
956
|
+
className: 'lfFields',
|
|
957
|
+
children: [
|
|
958
|
+
jsx.jsx(Field, {
|
|
959
|
+
label: t('maxRetries'),
|
|
960
|
+
value: target.maxRetries,
|
|
961
|
+
disabled,
|
|
962
|
+
invalid:
|
|
963
|
+
target.maxRetries === '' ||
|
|
964
|
+
!Number.isSafeInteger(Number(target.maxRetries)) ||
|
|
965
|
+
Number(target.maxRetries) < 0,
|
|
966
|
+
onChange: (value) => onEdit({ maxRetries: value }),
|
|
967
|
+
}),
|
|
968
|
+
jsx.jsx(Field, {
|
|
969
|
+
label: t('initialDelay'),
|
|
970
|
+
value: target.initialDelayMs,
|
|
971
|
+
disabled,
|
|
972
|
+
invalid: !(Number(target.initialDelayMs) > 0),
|
|
973
|
+
onChange: (value) => onEdit({ initialDelayMs: value }),
|
|
974
|
+
}),
|
|
975
|
+
jsx.jsx(Field, {
|
|
976
|
+
label: t('multiplier'),
|
|
977
|
+
value: target.multiplier,
|
|
978
|
+
disabled,
|
|
979
|
+
invalid: !(Number(target.multiplier) > 1),
|
|
980
|
+
onChange: (value) => onEdit({ multiplier: value }),
|
|
981
|
+
}),
|
|
982
|
+
jsx.jsx(Field, {
|
|
983
|
+
label: t('maxDelay'),
|
|
984
|
+
value: target.maxDelayMs,
|
|
985
|
+
disabled,
|
|
986
|
+
invalid: target.maxDelayMs === '' || !(Number(target.maxDelayMs) >= Number(target.initialDelayMs)),
|
|
987
|
+
onChange: (value) => onEdit({ maxDelayMs: value }),
|
|
988
|
+
}),
|
|
989
|
+
jsx.jsx(Field, {
|
|
990
|
+
label: t('jitter'),
|
|
991
|
+
value: target.jitterRatio,
|
|
992
|
+
disabled,
|
|
993
|
+
invalid:
|
|
994
|
+
target.jitterRatio === '' ||
|
|
995
|
+
!(Number(target.jitterRatio) >= 0 && Number(target.jitterRatio) <= 1),
|
|
996
|
+
onChange: (value) => onEdit({ jitterRatio: value }),
|
|
997
|
+
}),
|
|
998
|
+
],
|
|
999
|
+
}),
|
|
1000
|
+
jsx.jsxs('div', {
|
|
1001
|
+
className: 'lfFoot',
|
|
1002
|
+
children: [
|
|
1003
|
+
jsx.jsx(PlanTip, { target, t }),
|
|
1004
|
+
jsx.jsx('span', {
|
|
1005
|
+
className: 'lfSummary',
|
|
1006
|
+
children:
|
|
1007
|
+
plan.rows.length === 0
|
|
1008
|
+
? t('planNone')
|
|
1009
|
+
: `${summary}${plan.rows.length > 4 ? ' → …' : ''}`,
|
|
1010
|
+
}),
|
|
1011
|
+
],
|
|
1012
|
+
}),
|
|
1013
|
+
],
|
|
1014
|
+
});
|
|
1015
|
+
}
|
|
1016
|
+
|
|
1017
|
+
/**
|
|
1018
|
+
* 轮询主机的 settings.describe,取本插件命名空间的 runtime 字段。
|
|
1019
|
+
*
|
|
1020
|
+
* 熔断状态写在 volatile 单元格里,不产生 settings/document-updated 事件,
|
|
1021
|
+
* 所以不能等配置表单的推送,只能按固定间隔重新 describe。
|
|
1022
|
+
*/
|
|
1023
|
+
function useLiveState(readSettings) {
|
|
1024
|
+
const [state, setState] = react.useState({
|
|
1025
|
+
status: 'loading',
|
|
1026
|
+
runtime: undefined,
|
|
1027
|
+
error: null,
|
|
1028
|
+
fetchedAt: 0,
|
|
1029
|
+
});
|
|
1030
|
+
react.useEffect(() => {
|
|
1031
|
+
let alive = true;
|
|
1032
|
+
const read = async () => {
|
|
1033
|
+
try {
|
|
1034
|
+
const response = await readSettings();
|
|
1035
|
+
if (!alive) return;
|
|
1036
|
+
if (!response?.ok) {
|
|
1037
|
+
setState((current) => ({
|
|
1038
|
+
...current,
|
|
1039
|
+
status: 'error',
|
|
1040
|
+
error: response?.error?.message ?? 'settings.describe failed',
|
|
1041
|
+
}));
|
|
1042
|
+
return;
|
|
1043
|
+
}
|
|
1044
|
+
const row = (response.value?.namespaces ?? []).find((entry) => entry.ns === NS);
|
|
1045
|
+
setState({
|
|
1046
|
+
status: 'ready',
|
|
1047
|
+
runtime: row?.value?.runtime,
|
|
1048
|
+
error: null,
|
|
1049
|
+
fetchedAt: Date.now(),
|
|
1050
|
+
});
|
|
1051
|
+
} catch (failure) {
|
|
1052
|
+
if (!alive) return;
|
|
1053
|
+
setState((current) => ({
|
|
1054
|
+
...current,
|
|
1055
|
+
status: 'error',
|
|
1056
|
+
error: failure?.message ?? String(failure),
|
|
1057
|
+
}));
|
|
1058
|
+
}
|
|
1059
|
+
};
|
|
1060
|
+
void read();
|
|
1061
|
+
const timer = setInterval(() => void read(), 4000);
|
|
1062
|
+
return () => {
|
|
1063
|
+
alive = false;
|
|
1064
|
+
clearInterval(timer);
|
|
1065
|
+
};
|
|
1066
|
+
}, []);
|
|
1067
|
+
return state;
|
|
1068
|
+
}
|
|
1069
|
+
|
|
1070
|
+
/** 熔断状态的只读视图:一行一个 target。 */
|
|
1071
|
+
function LiveState(props) {
|
|
1072
|
+
const { live, enabled, t } = props;
|
|
1073
|
+
const now = Date.now();
|
|
1074
|
+
const targets = Array.isArray(live.runtime?.targets) ? live.runtime.targets : [];
|
|
1075
|
+
return jsx.jsxs('div', {
|
|
1076
|
+
className: 'lfSection',
|
|
1077
|
+
children: [
|
|
1078
|
+
jsx.jsxs('div', {
|
|
1079
|
+
className: 'lfSectionHead',
|
|
1080
|
+
children: [
|
|
1081
|
+
jsx.jsx('span', { className: 'ccTitle', children: t('live') }),
|
|
1082
|
+
live.fetchedAt > 0
|
|
1083
|
+
? jsx.jsx('span', {
|
|
1084
|
+
className: 'lfCount',
|
|
1085
|
+
children: t('liveUpdated', { age: formatAge(now - live.fetchedAt) }),
|
|
1086
|
+
})
|
|
1087
|
+
: null,
|
|
1088
|
+
],
|
|
1089
|
+
}),
|
|
1090
|
+
jsx.jsx('p', { className: 'ccHint', children: t('liveHint') }),
|
|
1091
|
+
live.error === null
|
|
1092
|
+
? null
|
|
1093
|
+
: jsx.jsx('p', { className: 'ccError', children: t('liveError', { message: live.error }) }),
|
|
1094
|
+
!enabled
|
|
1095
|
+
? jsx.jsx('p', { className: 'ccSubHint', children: t('liveDisabled') })
|
|
1096
|
+
: targets.length === 0
|
|
1097
|
+
? jsx.jsx('p', { className: 'ccSubHint', children: t('liveEmpty') })
|
|
1098
|
+
: jsx.jsxs('div', {
|
|
1099
|
+
className: 'lfLiveRows',
|
|
1100
|
+
children: targets.map((row) => {
|
|
1101
|
+
const lockedFor = Number(row.lockedUntil) - now;
|
|
1102
|
+
return jsx.jsxs(
|
|
1103
|
+
'div',
|
|
1104
|
+
{
|
|
1105
|
+
className: 'lfLiveRow',
|
|
1106
|
+
children: [
|
|
1107
|
+
jsx.jsx('span', { className: liveDotClass(row.state) }),
|
|
1108
|
+
jsx.jsx('span', {
|
|
1109
|
+
className: 'lfLiveName',
|
|
1110
|
+
children: row.label || row.key,
|
|
1111
|
+
}),
|
|
1112
|
+
jsx.jsx('span', {
|
|
1113
|
+
className: 'lfLiveState',
|
|
1114
|
+
children: t(LIVE_STATE_KEYS[row.state] ?? 'liveStateUnknown'),
|
|
1115
|
+
}),
|
|
1116
|
+
jsx.jsx('span', {
|
|
1117
|
+
className: 'lfLiveDetail',
|
|
1118
|
+
children:
|
|
1119
|
+
lockedFor > 0
|
|
1120
|
+
? t('liveRemaining', { time: formatMs(lockedFor) })
|
|
1121
|
+
: '',
|
|
1122
|
+
}),
|
|
1123
|
+
jsx.jsx('span', {
|
|
1124
|
+
className: 'lfLiveCounts',
|
|
1125
|
+
children: [
|
|
1126
|
+
t('liveFailureCount', { n: row.failures ?? 0 }),
|
|
1127
|
+
' · ',
|
|
1128
|
+
t('liveLockCount', { n: row.locks ?? 0 }),
|
|
1129
|
+
].join(''),
|
|
1130
|
+
}),
|
|
1131
|
+
],
|
|
1132
|
+
},
|
|
1133
|
+
row.key ?? row.label,
|
|
1134
|
+
);
|
|
1135
|
+
}),
|
|
1136
|
+
}),
|
|
1137
|
+
],
|
|
1138
|
+
});
|
|
1139
|
+
}
|
|
1140
|
+
|
|
1141
|
+
/** 插件详情页卡片。 */
|
|
1142
|
+
function FallbackCard(props) {
|
|
1143
|
+
const { t, useScope, useCatalog, reloadCatalog, useLive, mutate } = props;
|
|
1144
|
+
const snapshot = useScope((value) => value);
|
|
1145
|
+
const catalog = useCatalog();
|
|
1146
|
+
const catalogReady = catalog !== undefined && catalog !== null;
|
|
1147
|
+
const live = useLive();
|
|
1148
|
+
const [draft, setDraft] = react.useState(null);
|
|
1149
|
+
const [error, setError] = react.useState(null);
|
|
1150
|
+
const [saved, setSaved] = react.useState(false);
|
|
1151
|
+
|
|
1152
|
+
if (snapshot?.status === 'loading' || snapshot?.status === undefined) {
|
|
1153
|
+
return jsx.jsxs('div', {
|
|
1154
|
+
className: 'ccWrap',
|
|
1155
|
+
children: [
|
|
1156
|
+
jsx.jsx('span', { className: 'ccTitle', children: t('title') }),
|
|
1157
|
+
jsx.jsx('p', { className: 'ccHint', children: t('loading') }),
|
|
1158
|
+
],
|
|
1159
|
+
});
|
|
1160
|
+
}
|
|
1161
|
+
if (snapshot.status === 'unavailable' || snapshot.mode === 'memory') {
|
|
1162
|
+
return jsx.jsxs('div', {
|
|
1163
|
+
className: 'ccWrap',
|
|
1164
|
+
children: [
|
|
1165
|
+
jsx.jsx('span', { className: 'ccTitle', children: t('title') }),
|
|
1166
|
+
jsx.jsx('p', { className: 'ccError', children: t('unavailable') }),
|
|
1167
|
+
],
|
|
1168
|
+
});
|
|
1169
|
+
}
|
|
1170
|
+
|
|
1171
|
+
const value = snapshot.value;
|
|
1172
|
+
if (value === undefined) {
|
|
1173
|
+
return jsx.jsxs('div', {
|
|
1174
|
+
className: 'ccWrap',
|
|
1175
|
+
children: [
|
|
1176
|
+
jsx.jsx('span', { className: 'ccTitle', children: t('title') }),
|
|
1177
|
+
jsx.jsx('p', { className: 'ccError', children: t('unavailable') }),
|
|
1178
|
+
],
|
|
1179
|
+
});
|
|
1180
|
+
}
|
|
1181
|
+
|
|
1182
|
+
const readOnly = snapshot.writable === false;
|
|
1183
|
+
const shown = draft ?? configDraft(value);
|
|
1184
|
+
const route = typeof value.route === 'string' && value.route !== '' ? value.route : DEFAULT_ROUTE;
|
|
1185
|
+
const errors = validateDraft(shown, route, catalogReady ? catalog : undefined);
|
|
1186
|
+
const dirty = draft !== null;
|
|
1187
|
+
|
|
1188
|
+
const update = (patch) => {
|
|
1189
|
+
setSaved(false);
|
|
1190
|
+
setError(null);
|
|
1191
|
+
setDraft((current) => ({ ...(current ?? configDraft(value)), ...patch }));
|
|
1192
|
+
};
|
|
1193
|
+
const modelChoicesOf = (provider) => {
|
|
1194
|
+
const group = (catalog?.groups ?? []).find((entry) => entry.id === provider);
|
|
1195
|
+
return (group?.models ?? []).map((model) => ({
|
|
1196
|
+
id: model.id,
|
|
1197
|
+
name: model.name ?? model.id,
|
|
1198
|
+
}));
|
|
1199
|
+
};
|
|
1200
|
+
const editTarget = (index, patch) => {
|
|
1201
|
+
const base = draft ?? configDraft(value);
|
|
1202
|
+
update({
|
|
1203
|
+
targets: base.targets.map((target, position) => {
|
|
1204
|
+
if (position !== index) return target;
|
|
1205
|
+
const next = { ...target, ...patch };
|
|
1206
|
+
// provider 由目录下拉选择:换 provider 时把 model 收敛到新目录的第一项,
|
|
1207
|
+
// 不允许留下一个属于旧 provider 的 model id。
|
|
1208
|
+
if (patch.provider !== undefined && patch.provider !== target.provider) {
|
|
1209
|
+
next.model = modelChoicesOf(patch.provider)[0]?.id ?? '';
|
|
1210
|
+
}
|
|
1211
|
+
return next;
|
|
1212
|
+
}),
|
|
1213
|
+
});
|
|
1214
|
+
};
|
|
1215
|
+
const moveTarget = (index, delta) => {
|
|
1216
|
+
const base = draft ?? configDraft(value);
|
|
1217
|
+
update({ targets: moveIn(base.targets, index, delta) });
|
|
1218
|
+
};
|
|
1219
|
+
const removeTarget = (index) => {
|
|
1220
|
+
const base = draft ?? configDraft(value);
|
|
1221
|
+
update({ targets: base.targets.filter((_, position) => position !== index) });
|
|
1222
|
+
};
|
|
1223
|
+
const addTarget = () => {
|
|
1224
|
+
const base = draft ?? configDraft(value);
|
|
1225
|
+
update({ targets: [...base.targets, defaultTarget()] });
|
|
1226
|
+
};
|
|
1227
|
+
const discard = () => {
|
|
1228
|
+
setDraft(null);
|
|
1229
|
+
setError(null);
|
|
1230
|
+
setSaved(false);
|
|
1231
|
+
};
|
|
1232
|
+
const commit = async () => {
|
|
1233
|
+
if (errors.length > 0 || readOnly || !catalogReady) return;
|
|
1234
|
+
setError(null);
|
|
1235
|
+
setSaved(false);
|
|
1236
|
+
try {
|
|
1237
|
+
if (!(await mutate(buildOps(shown)))) throw new Error(t('writeRefused'));
|
|
1238
|
+
setDraft(null);
|
|
1239
|
+
setSaved(true);
|
|
1240
|
+
} catch (failure) {
|
|
1241
|
+
setError(failure?.message ?? String(failure));
|
|
1242
|
+
}
|
|
1243
|
+
};
|
|
1244
|
+
|
|
1245
|
+
const providerChoices = (catalog?.groups ?? [])
|
|
1246
|
+
.filter((group) => group.id !== route)
|
|
1247
|
+
.map((group) => ({
|
|
1248
|
+
id: group.id,
|
|
1249
|
+
name: group.name ?? group.id,
|
|
1250
|
+
}));
|
|
1251
|
+
const enabledCount = shown.targets.filter((target) => target.enabled !== false).length;
|
|
1252
|
+
|
|
1253
|
+
return jsx.jsxs('div', {
|
|
1254
|
+
className: 'ccWrap',
|
|
1255
|
+
children: [
|
|
1256
|
+
jsx.jsxs('div', {
|
|
1257
|
+
className: 'lfHead',
|
|
1258
|
+
children: [
|
|
1259
|
+
jsx.jsx('span', { className: 'ccTitle', children: t('title') }),
|
|
1260
|
+
jsx.jsxs('span', {
|
|
1261
|
+
className: 'lfRoute',
|
|
1262
|
+
children: ['route · ', route],
|
|
1263
|
+
}),
|
|
1264
|
+
],
|
|
1265
|
+
}),
|
|
1266
|
+
jsx.jsx('p', { className: 'ccHint', children: t('hint') }),
|
|
1267
|
+
readOnly ? jsx.jsx('p', { className: 'ccError', children: t('readOnly') }) : null,
|
|
1268
|
+
jsx.jsxs('div', {
|
|
1269
|
+
className: 'ccRow',
|
|
1270
|
+
children: [
|
|
1271
|
+
jsx.jsx('input', {
|
|
1272
|
+
type: 'checkbox',
|
|
1273
|
+
className: 'ccCheck',
|
|
1274
|
+
checked: shown.enabled,
|
|
1275
|
+
disabled: readOnly,
|
|
1276
|
+
'aria-label': t('enabled'),
|
|
1277
|
+
onChange: (event) => update({ enabled: event.target.checked }),
|
|
1278
|
+
}),
|
|
1279
|
+
jsx.jsx('span', { className: 'ccLabel', children: t('enabled') }),
|
|
1280
|
+
],
|
|
1281
|
+
}),
|
|
1282
|
+
jsx.jsxs('div', {
|
|
1283
|
+
className: 'ccRow',
|
|
1284
|
+
children: [
|
|
1285
|
+
jsx.jsx('span', { className: 'ccLabel', children: t('route') }),
|
|
1286
|
+
jsx.jsx('span', { className: 'lfRoute', children: route }),
|
|
1287
|
+
],
|
|
1288
|
+
}),
|
|
1289
|
+
jsx.jsx('p', { className: 'ccSubHint', children: t('routeHint') }),
|
|
1290
|
+
|
|
1291
|
+
jsx.jsxs('div', {
|
|
1292
|
+
className: 'lfSection',
|
|
1293
|
+
children: [
|
|
1294
|
+
jsx.jsxs('div', {
|
|
1295
|
+
className: 'lfSectionHead',
|
|
1296
|
+
children: [
|
|
1297
|
+
jsx.jsx('span', { className: 'ccTitle', children: t('targets') }),
|
|
1298
|
+
jsx.jsx('span', {
|
|
1299
|
+
className: 'lfCount',
|
|
1300
|
+
children: t('targetCount', {
|
|
1301
|
+
total: shown.targets.length,
|
|
1302
|
+
enabled: enabledCount,
|
|
1303
|
+
}),
|
|
1304
|
+
}),
|
|
1305
|
+
jsx.jsx('button', {
|
|
1306
|
+
type: 'button',
|
|
1307
|
+
className: 'ccReset',
|
|
1308
|
+
disabled: readOnly,
|
|
1309
|
+
onClick: addTarget,
|
|
1310
|
+
children: `+ ${t('addTarget')}`,
|
|
1311
|
+
}),
|
|
1312
|
+
],
|
|
1313
|
+
}),
|
|
1314
|
+
shown.targets.length === 0
|
|
1315
|
+
? jsx.jsx('p', { className: 'ccSubHint', children: t('targetsEmpty') })
|
|
1316
|
+
: null,
|
|
1317
|
+
catalog === undefined
|
|
1318
|
+
? jsx.jsx('p', { className: 'ccSubHint', children: t('catalogLoading') })
|
|
1319
|
+
: null,
|
|
1320
|
+
catalog === null
|
|
1321
|
+
? jsx.jsxs('div', {
|
|
1322
|
+
className: 'ccRow',
|
|
1323
|
+
children: [
|
|
1324
|
+
jsx.jsx('p', { className: 'ccError', children: t('catalogFailed') }),
|
|
1325
|
+
jsx.jsx('button', {
|
|
1326
|
+
type: 'button',
|
|
1327
|
+
className: 'ccReset',
|
|
1328
|
+
onClick: reloadCatalog,
|
|
1329
|
+
children: t('catalogRetry'),
|
|
1330
|
+
}),
|
|
1331
|
+
],
|
|
1332
|
+
})
|
|
1333
|
+
: null,
|
|
1334
|
+
...shown.targets.map((target, index) =>
|
|
1335
|
+
jsx.jsx(
|
|
1336
|
+
TargetRow,
|
|
1337
|
+
{
|
|
1338
|
+
target,
|
|
1339
|
+
index,
|
|
1340
|
+
total: shown.targets.length,
|
|
1341
|
+
t,
|
|
1342
|
+
disabled: readOnly,
|
|
1343
|
+
choicesDisabled: !catalogReady,
|
|
1344
|
+
providerChoices,
|
|
1345
|
+
modelChoicesOf,
|
|
1346
|
+
onEdit: (patch) => editTarget(index, patch),
|
|
1347
|
+
onMove: (delta) => moveTarget(index, delta),
|
|
1348
|
+
onRemove: () => removeTarget(index),
|
|
1349
|
+
},
|
|
1350
|
+
index,
|
|
1351
|
+
),
|
|
1352
|
+
),
|
|
1353
|
+
],
|
|
1354
|
+
}),
|
|
1355
|
+
|
|
1356
|
+
jsx.jsxs('div', {
|
|
1357
|
+
className: 'lfSection',
|
|
1358
|
+
children: [
|
|
1359
|
+
jsx.jsx('span', { className: 'ccTitle', children: t('limits') }),
|
|
1360
|
+
jsx.jsx('p', { className: 'ccHint', children: t('limitsHint') }),
|
|
1361
|
+
jsx.jsxs('div', {
|
|
1362
|
+
className: 'lfFields',
|
|
1363
|
+
children: [
|
|
1364
|
+
jsx.jsx(Field, {
|
|
1365
|
+
label: t('maxTotalMs'),
|
|
1366
|
+
value: shown.maxTotalMs,
|
|
1367
|
+
disabled: readOnly,
|
|
1368
|
+
invalid:
|
|
1369
|
+
shown.maxTotalMs === '' ||
|
|
1370
|
+
!Number.isFinite(Number(shown.maxTotalMs)) ||
|
|
1371
|
+
Number(shown.maxTotalMs) < 0,
|
|
1372
|
+
onChange: (value) => update({ maxTotalMs: value }),
|
|
1373
|
+
}),
|
|
1374
|
+
jsx.jsx(Field, {
|
|
1375
|
+
label: t('maxAttempts'),
|
|
1376
|
+
value: shown.maxAttempts,
|
|
1377
|
+
disabled: readOnly,
|
|
1378
|
+
invalid:
|
|
1379
|
+
shown.maxAttempts === '' ||
|
|
1380
|
+
!Number.isSafeInteger(Number(shown.maxAttempts)) ||
|
|
1381
|
+
Number(shown.maxAttempts) < 0,
|
|
1382
|
+
onChange: (value) => update({ maxAttempts: value }),
|
|
1383
|
+
}),
|
|
1384
|
+
],
|
|
1385
|
+
}),
|
|
1386
|
+
],
|
|
1387
|
+
}),
|
|
1388
|
+
|
|
1389
|
+
jsx.jsxs('div', {
|
|
1390
|
+
className: 'lfSection',
|
|
1391
|
+
children: [
|
|
1392
|
+
jsx.jsx('span', { className: 'ccTitle', children: t('breaker') }),
|
|
1393
|
+
jsx.jsx('p', { className: 'ccHint', children: t('breakerHint') }),
|
|
1394
|
+
jsx.jsxs('div', {
|
|
1395
|
+
className: 'lfFields',
|
|
1396
|
+
children: [
|
|
1397
|
+
jsx.jsx(Field, {
|
|
1398
|
+
label: t('failureThreshold'),
|
|
1399
|
+
value: shown.failureThreshold,
|
|
1400
|
+
disabled: readOnly,
|
|
1401
|
+
invalid:
|
|
1402
|
+
!Number.isSafeInteger(Number(shown.failureThreshold)) ||
|
|
1403
|
+
Number(shown.failureThreshold) < 1,
|
|
1404
|
+
onChange: (value) => update({ failureThreshold: value }),
|
|
1405
|
+
}),
|
|
1406
|
+
jsx.jsx(Field, {
|
|
1407
|
+
label: t('lockMinutes'),
|
|
1408
|
+
value: shown.lockMinutes,
|
|
1409
|
+
disabled: readOnly,
|
|
1410
|
+
invalid: !(Number(shown.lockMinutes) > 0),
|
|
1411
|
+
onChange: (value) => update({ lockMinutes: value }),
|
|
1412
|
+
}),
|
|
1413
|
+
],
|
|
1414
|
+
}),
|
|
1415
|
+
jsx.jsxs('div', {
|
|
1416
|
+
className: 'ccRow',
|
|
1417
|
+
children: [
|
|
1418
|
+
jsx.jsx('input', {
|
|
1419
|
+
type: 'checkbox',
|
|
1420
|
+
className: 'ccCheck',
|
|
1421
|
+
checked: shown.probeEnabled,
|
|
1422
|
+
disabled: readOnly,
|
|
1423
|
+
'aria-label': t('probe'),
|
|
1424
|
+
onChange: (event) => update({ probeEnabled: event.target.checked }),
|
|
1425
|
+
}),
|
|
1426
|
+
jsx.jsx('span', { className: 'ccLabel', children: t('probe') }),
|
|
1427
|
+
],
|
|
1428
|
+
}),
|
|
1429
|
+
shown.probeEnabled
|
|
1430
|
+
? jsx.jsxs('div', {
|
|
1431
|
+
className: 'lfFields',
|
|
1432
|
+
children: [
|
|
1433
|
+
jsx.jsx(Field, {
|
|
1434
|
+
label: t('probeTimeout'),
|
|
1435
|
+
value: shown.probeTimeoutMs,
|
|
1436
|
+
disabled: readOnly,
|
|
1437
|
+
invalid: !(Number(shown.probeTimeoutMs) > 0),
|
|
1438
|
+
onChange: (value) => update({ probeTimeoutMs: value }),
|
|
1439
|
+
}),
|
|
1440
|
+
jsx.jsx(Field, {
|
|
1441
|
+
label: t('probeMaxTokens'),
|
|
1442
|
+
value: shown.probeMaxTokens,
|
|
1443
|
+
disabled: readOnly,
|
|
1444
|
+
invalid:
|
|
1445
|
+
!Number.isSafeInteger(Number(shown.probeMaxTokens)) ||
|
|
1446
|
+
Number(shown.probeMaxTokens) < 1,
|
|
1447
|
+
onChange: (value) => update({ probeMaxTokens: value }),
|
|
1448
|
+
}),
|
|
1449
|
+
jsx.jsx(Field, {
|
|
1450
|
+
label: t('probePrompt'),
|
|
1451
|
+
value: shown.probePrompt,
|
|
1452
|
+
disabled: readOnly,
|
|
1453
|
+
invalid: shown.probePrompt.trim() === '',
|
|
1454
|
+
wide: true,
|
|
1455
|
+
onChange: (value) => update({ probePrompt: value }),
|
|
1456
|
+
}),
|
|
1457
|
+
],
|
|
1458
|
+
})
|
|
1459
|
+
: null,
|
|
1460
|
+
shown.probeEnabled
|
|
1461
|
+
? jsx.jsxs('div', {
|
|
1462
|
+
className: 'ccRow',
|
|
1463
|
+
children: [
|
|
1464
|
+
jsx.jsx('input', {
|
|
1465
|
+
type: 'checkbox',
|
|
1466
|
+
className: 'ccCheck',
|
|
1467
|
+
checked: shown.probeBackground,
|
|
1468
|
+
disabled: readOnly,
|
|
1469
|
+
'aria-label': t('probeBackground'),
|
|
1470
|
+
onChange: (event) =>
|
|
1471
|
+
update({ probeBackground: event.target.checked }),
|
|
1472
|
+
}),
|
|
1473
|
+
jsx.jsx('span', {
|
|
1474
|
+
className: 'ccLabel',
|
|
1475
|
+
children: t('probeBackground'),
|
|
1476
|
+
}),
|
|
1477
|
+
],
|
|
1478
|
+
})
|
|
1479
|
+
: null,
|
|
1480
|
+
shown.probeEnabled
|
|
1481
|
+
? jsx.jsx('p', { className: 'ccSubHint', children: t('probeBackgroundHint') })
|
|
1482
|
+
: null,
|
|
1483
|
+
jsx.jsxs('label', {
|
|
1484
|
+
className: 'lfField lfFieldWide',
|
|
1485
|
+
children: [
|
|
1486
|
+
jsx.jsx('span', { children: t('stateFile') }),
|
|
1487
|
+
jsx.jsx('input', {
|
|
1488
|
+
className:
|
|
1489
|
+
shown.stateFile.trim() === ''
|
|
1490
|
+
? 'ccInput lfStateInput ccInputInvalid'
|
|
1491
|
+
: 'ccInput lfStateInput',
|
|
1492
|
+
value: shown.stateFile,
|
|
1493
|
+
disabled: readOnly,
|
|
1494
|
+
onChange: (event) => update({ stateFile: event.target.value }),
|
|
1495
|
+
}),
|
|
1496
|
+
jsx.jsx('span', { className: 'lfSummary', children: t('stateFileHint') }),
|
|
1497
|
+
],
|
|
1498
|
+
}),
|
|
1499
|
+
],
|
|
1500
|
+
}),
|
|
1501
|
+
|
|
1502
|
+
jsx.jsxs('div', {
|
|
1503
|
+
className: 'lfSection',
|
|
1504
|
+
children: [
|
|
1505
|
+
jsx.jsx('span', { className: 'ccTitle', children: t('toolCalls') }),
|
|
1506
|
+
jsx.jsxs('div', {
|
|
1507
|
+
className: 'ccRow',
|
|
1508
|
+
children: [
|
|
1509
|
+
jsx.jsx('input', {
|
|
1510
|
+
type: 'checkbox',
|
|
1511
|
+
className: 'ccCheck',
|
|
1512
|
+
checked: shown.retryMalformed,
|
|
1513
|
+
disabled: readOnly,
|
|
1514
|
+
'aria-label': t('retryMalformed'),
|
|
1515
|
+
onChange: (event) => update({ retryMalformed: event.target.checked }),
|
|
1516
|
+
}),
|
|
1517
|
+
jsx.jsx('span', { className: 'ccLabel', children: t('retryMalformed') }),
|
|
1518
|
+
],
|
|
1519
|
+
}),
|
|
1520
|
+
jsx.jsx('p', { className: 'ccSubHint', children: t('retryMalformedHint') }),
|
|
1521
|
+
],
|
|
1522
|
+
}),
|
|
1523
|
+
|
|
1524
|
+
jsx.jsx(LiveState, { live, enabled: shown.enabled, t }),
|
|
1525
|
+
|
|
1526
|
+
errors.length > 0 && dirty
|
|
1527
|
+
? jsx.jsxs('div', {
|
|
1528
|
+
children: [
|
|
1529
|
+
jsx.jsx('p', { className: 'ccError', children: t('invalid') }),
|
|
1530
|
+
jsx.jsx('ul', {
|
|
1531
|
+
className: 'ccList',
|
|
1532
|
+
children: errors.slice(0, 8).map((entry, index) =>
|
|
1533
|
+
jsx.jsx(
|
|
1534
|
+
'li',
|
|
1535
|
+
{ children: t(entry.key, entry.params) },
|
|
1536
|
+
`${entry.key}-${index}`,
|
|
1537
|
+
),
|
|
1538
|
+
),
|
|
1539
|
+
}),
|
|
1540
|
+
],
|
|
1541
|
+
})
|
|
1542
|
+
: null,
|
|
1543
|
+
error === null ? null : jsx.jsx('p', { className: 'ccError', children: error }),
|
|
1544
|
+
dirty
|
|
1545
|
+
? jsx.jsxs('div', {
|
|
1546
|
+
className: 'lfActions',
|
|
1547
|
+
children: [
|
|
1548
|
+
jsx.jsx('button', {
|
|
1549
|
+
type: 'button',
|
|
1550
|
+
className: 'lfPrimary',
|
|
1551
|
+
disabled: readOnly || errors.length > 0 || !catalogReady,
|
|
1552
|
+
onClick: () => void commit(),
|
|
1553
|
+
children: t('save'),
|
|
1554
|
+
}),
|
|
1555
|
+
jsx.jsx('button', {
|
|
1556
|
+
type: 'button',
|
|
1557
|
+
className: 'ccReset',
|
|
1558
|
+
onClick: discard,
|
|
1559
|
+
children: t('discard'),
|
|
1560
|
+
}),
|
|
1561
|
+
jsx.jsx('span', {
|
|
1562
|
+
className: 'lfSummary',
|
|
1563
|
+
children: catalogReady ? t('unsaved') : t('catalogBlocked'),
|
|
1564
|
+
}),
|
|
1565
|
+
],
|
|
1566
|
+
})
|
|
1567
|
+
: saved
|
|
1568
|
+
? jsx.jsx('p', { className: 'lfSummary', children: t('saved') })
|
|
1569
|
+
: null,
|
|
1570
|
+
],
|
|
1571
|
+
});
|
|
1572
|
+
}
|
|
1573
|
+
|
|
1574
|
+
/**
|
|
1575
|
+
* 会话模型目录:懒加载;失败是事实而不是空选择器(可以用 reload 重试)。
|
|
1576
|
+
*
|
|
1577
|
+
* provider/model 只能从这里选,不再提供自由输入。
|
|
1578
|
+
*/
|
|
1579
|
+
function useCatalogService(ctx) {
|
|
1580
|
+
let state = { status: 'idle', value: undefined };
|
|
1581
|
+
const listeners = new Set();
|
|
1582
|
+
const notify = () => {
|
|
1583
|
+
for (const listener of listeners) listener();
|
|
1584
|
+
};
|
|
1585
|
+
const load = () => {
|
|
1586
|
+
if (state.status === 'loading') return;
|
|
1587
|
+
state = { status: 'loading', value: undefined };
|
|
1588
|
+
notify();
|
|
1589
|
+
Promise.resolve()
|
|
1590
|
+
.then(() => ctx.remote.session.modelCatalog())
|
|
1591
|
+
.then((response) => {
|
|
1592
|
+
if (!response?.ok) {
|
|
1593
|
+
throw new Error(
|
|
1594
|
+
response?.error === undefined
|
|
1595
|
+
? 'modelCatalog failed'
|
|
1596
|
+
: `${response.error.code}: ${response.error.message}`,
|
|
1597
|
+
);
|
|
1598
|
+
}
|
|
1599
|
+
state = { status: 'ready', value: response.value };
|
|
1600
|
+
})
|
|
1601
|
+
.catch(() => {
|
|
1602
|
+
state = { status: 'failed', value: null };
|
|
1603
|
+
})
|
|
1604
|
+
.finally(notify);
|
|
1605
|
+
};
|
|
1606
|
+
const useCatalog = () => {
|
|
1607
|
+
const [value, setValue] = react.useState(() => state.value);
|
|
1608
|
+
react.useEffect(() => {
|
|
1609
|
+
load();
|
|
1610
|
+
const listener = () => setValue(state.value);
|
|
1611
|
+
listeners.add(listener);
|
|
1612
|
+
listener();
|
|
1613
|
+
return () => {
|
|
1614
|
+
listeners.delete(listener);
|
|
1615
|
+
};
|
|
1616
|
+
}, []);
|
|
1617
|
+
return value;
|
|
1618
|
+
};
|
|
1619
|
+
return { useCatalog, reload: load };
|
|
1620
|
+
}
|
|
1621
|
+
|
|
1622
|
+
/** 注册卡片。 */
|
|
1623
|
+
function apply(ctx) {
|
|
1624
|
+
const t = ctx.locale.bind(NS);
|
|
1625
|
+
ctx.effect(() => ctx.locale.register(NS, { zh, en }), 'llm-resilience: card copy');
|
|
1626
|
+
|
|
1627
|
+
// 设置平面按 profile entry id 索引表单,不是插件自己的命名空间。
|
|
1628
|
+
// 这个字面量必须与 cordis.patch.yml 的 `- id` 一致。
|
|
1629
|
+
const form = ctx.configForms.get('llm-fallback');
|
|
1630
|
+
const scope = {
|
|
1631
|
+
getSnapshot: () => form.getSnapshot(),
|
|
1632
|
+
subscribe: (listener) => form.subscribe(listener),
|
|
1633
|
+
mutate: (ops) => form.mutate(ops),
|
|
1634
|
+
};
|
|
1635
|
+
const catalogService = useCatalogService(ctx);
|
|
1636
|
+
|
|
1637
|
+
// 放在「插件 → 已安装插件 → dsh-llm-resilience」详情页,而不是 Settings → Models:
|
|
1638
|
+
// `plugins.bundle.config` 是按**包名**索引的,与已安装列表里的行配对。
|
|
1639
|
+
// whileServed 会等宿主真正提供 `llm-fallback` 命名空间后再注册。
|
|
1640
|
+
ctx.effect(
|
|
1641
|
+
() =>
|
|
1642
|
+
ctx.configForms.whileServed([NS], () =>
|
|
1643
|
+
ctx.slots.inject('plugins.bundle.config', () =>
|
|
1644
|
+
ctx.slots.register(
|
|
1645
|
+
{
|
|
1646
|
+
name: 'plugins.bundle.config',
|
|
1647
|
+
key: PACKAGE_NAME,
|
|
1648
|
+
order: 30,
|
|
1649
|
+
locale: NS,
|
|
1650
|
+
inject: () => ({
|
|
1651
|
+
t,
|
|
1652
|
+
mutate: (ops) => scope.mutate(ops),
|
|
1653
|
+
useCatalog: catalogService.useCatalog,
|
|
1654
|
+
reloadCatalog: catalogService.reload,
|
|
1655
|
+
useLive: () => useLiveState(() => ctx.remote.settings.describe()),
|
|
1656
|
+
useScope: (select) => {
|
|
1657
|
+
const [snapshot, setSnapshot] = react.useState(() =>
|
|
1658
|
+
scope.getSnapshot(),
|
|
1659
|
+
);
|
|
1660
|
+
react.useEffect(
|
|
1661
|
+
() => scope.subscribe(() => setSnapshot(scope.getSnapshot())),
|
|
1662
|
+
[],
|
|
1663
|
+
);
|
|
1664
|
+
return select(snapshot);
|
|
1665
|
+
},
|
|
1666
|
+
}),
|
|
1667
|
+
},
|
|
1668
|
+
FallbackCard,
|
|
1669
|
+
),
|
|
1670
|
+
),
|
|
1671
|
+
),
|
|
1672
|
+
'llm-resilience: plugins page',
|
|
1673
|
+
);
|
|
1674
|
+
}
|
|
1675
|
+
|
|
1676
|
+
/** 必需客户端服务;名字写错 = entry 永远 pending。 */
|
|
1677
|
+
const inject = ['locale', 'remote', 'remote.session', 'remote.settings', 'configForms', 'slots'];
|
|
1678
|
+
|
|
1679
|
+
exports.apply = apply;
|
|
1680
|
+
exports.inject = inject;
|
|
1681
|
+
exports.__test = {
|
|
1682
|
+
DEFAULTS,
|
|
1683
|
+
buildOps,
|
|
1684
|
+
configDraft,
|
|
1685
|
+
defaultTarget,
|
|
1686
|
+
formatAge,
|
|
1687
|
+
formatMs,
|
|
1688
|
+
moveIn,
|
|
1689
|
+
retryPlanRows,
|
|
1690
|
+
storedTarget,
|
|
1691
|
+
validateDraft,
|
|
1692
|
+
};
|
|
1693
|
+
return module.exports;
|
|
1694
|
+
},
|
|
1695
|
+
});
|