dsh-model-fleet 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +166 -2
- package/cordis.patch.yml +23 -0
- package/icon.svg +8 -0
- package/lib/client.js +685 -0
- package/lib/index.js +806 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +53 -4
package/lib/client.js
ADDED
|
@@ -0,0 +1,685 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dsh-model-fleet — Client 侧(浏览器)。
|
|
3
|
+
*
|
|
4
|
+
* 这个文件是**手写的** `window.__ModuleLoader__.load` 工厂,不经过任何构建:
|
|
5
|
+
* 宿主只读 `lib/client.js`,所以发布包里它就是最终产物。`id` 必须等于包名全称,
|
|
6
|
+
* 否则宿主加载不到(不报错,只是没有面板)。
|
|
7
|
+
*
|
|
8
|
+
* 只 require('react')——它来自浏览器的模块表。不要 require 任何 harness 客户端包:
|
|
9
|
+
* 它们会变,而纯 JS 没有类型检查,组件一抛异常整个 slot 条目就白屏。
|
|
10
|
+
*/
|
|
11
|
+
window.__ModuleLoader__.load({
|
|
12
|
+
id: 'dsh-model-fleet',
|
|
13
|
+
factory(require) {
|
|
14
|
+
const React = require('react')
|
|
15
|
+
const h = React.createElement
|
|
16
|
+
|
|
17
|
+
const NS = 'modelFleet'
|
|
18
|
+
const PANEL_ID = 'model-fleet'
|
|
19
|
+
const API = '/dsh-model-fleet/api/members'
|
|
20
|
+
|
|
21
|
+
const zh = {
|
|
22
|
+
panel: '模型编队',
|
|
23
|
+
title: '模型编队',
|
|
24
|
+
intro: '你只跟主模型说话。主模型办不到的事,才交给副模型;结果由主模型汇报。',
|
|
25
|
+
mainLabel: '主模型',
|
|
26
|
+
mainUnknown: '未读取到',
|
|
27
|
+
membersTitle: '副模型',
|
|
28
|
+
membersEmpty: '还没有配置副模型。',
|
|
29
|
+
membersEmptyHint: '点「添加成员」加一个,或者直接改 profile 的 cordis.patch.yml。',
|
|
30
|
+
callsTitle: '最近委派',
|
|
31
|
+
callsEmpty: '本进程还没有发生过委派。',
|
|
32
|
+
loading: '正在读取…',
|
|
33
|
+
error: '读取失败',
|
|
34
|
+
retry: '重试',
|
|
35
|
+
refresh: '刷新',
|
|
36
|
+
ok: '成功',
|
|
37
|
+
failed: '失败',
|
|
38
|
+
tokens: 'tokens',
|
|
39
|
+
add: '添加成员',
|
|
40
|
+
addTitle: '新成员',
|
|
41
|
+
edit: '编辑',
|
|
42
|
+
remove: '删除',
|
|
43
|
+
save: '保存',
|
|
44
|
+
saving: '保存中…',
|
|
45
|
+
cancel: '取消',
|
|
46
|
+
confirm: '确认删除',
|
|
47
|
+
confirmPrompt: '删掉这个成员?',
|
|
48
|
+
saveFailed: '保存失败',
|
|
49
|
+
fId: 'id',
|
|
50
|
+
fLabel: '名称',
|
|
51
|
+
fProvider: 'provider',
|
|
52
|
+
fModel: 'model',
|
|
53
|
+
fNote: '说明',
|
|
54
|
+
fIdHint: '主模型调用时用的名字,留空自动生成',
|
|
55
|
+
fNoteHint: '一行能力说明,会进系统提示词里的名册',
|
|
56
|
+
needProviderModel: 'provider 和 model 必填',
|
|
57
|
+
idAuto: 'id 留空就用「{derived}」——它只是主模型调用时怎么称呼这个成员,之后改 label 不影响引用。',
|
|
58
|
+
idInvalid: 'id 只能用英文字母、数字、点、下划线、短横线,最多 40 个字符。',
|
|
59
|
+
providerUnknown: '这个 provider 没有注册,调用会失败',
|
|
60
|
+
keyHint: 'api key 不在成员表里:它属于 provider(一个 endpoint 一把钥匙),在「设置 → 模型」里配一次,同一 provider 下的所有模型共用。这里只填 provider 的名字。',
|
|
61
|
+
providerOptions: '已注册的 provider',
|
|
62
|
+
modelOptions: '这个 provider 下已知的模型',
|
|
63
|
+
hint: '这里改的就是 profile 的 Cordis patch:写盘后 loader 会重新组合这个条目,工具和名册一起换新。provider 必须在「设置 → 模型」里先配好。',
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const en = {
|
|
67
|
+
panel: 'Model fleet',
|
|
68
|
+
title: 'Model fleet',
|
|
69
|
+
intro: 'You only talk to the main model. What it cannot do, it hands to a member, and it reports the result.',
|
|
70
|
+
mainLabel: 'Main model',
|
|
71
|
+
mainUnknown: 'not detected',
|
|
72
|
+
membersTitle: 'Members',
|
|
73
|
+
membersEmpty: 'No members configured yet.',
|
|
74
|
+
membersEmptyHint: 'Hit "Add member", or edit config.members in the profile cordis.patch.yml.',
|
|
75
|
+
callsTitle: 'Recent delegations',
|
|
76
|
+
callsEmpty: 'No delegation has happened in this process yet.',
|
|
77
|
+
loading: 'Reading…',
|
|
78
|
+
error: 'Could not read',
|
|
79
|
+
retry: 'Retry',
|
|
80
|
+
refresh: 'Refresh',
|
|
81
|
+
ok: 'ok',
|
|
82
|
+
failed: 'failed',
|
|
83
|
+
tokens: 'tokens',
|
|
84
|
+
add: 'Add member',
|
|
85
|
+
addTitle: 'New member',
|
|
86
|
+
edit: 'Edit',
|
|
87
|
+
remove: 'Remove',
|
|
88
|
+
save: 'Save',
|
|
89
|
+
saving: 'Saving…',
|
|
90
|
+
cancel: 'Cancel',
|
|
91
|
+
confirm: 'Delete it',
|
|
92
|
+
confirmPrompt: 'Delete this member?',
|
|
93
|
+
saveFailed: 'Could not save',
|
|
94
|
+
fId: 'id',
|
|
95
|
+
fLabel: 'Label',
|
|
96
|
+
fProvider: 'provider',
|
|
97
|
+
fModel: 'model',
|
|
98
|
+
fNote: 'Note',
|
|
99
|
+
fIdHint: 'Name the main model calls it by; auto-generated when empty',
|
|
100
|
+
fNoteHint: 'One line about what it is good at; goes into the roster in the system prompt',
|
|
101
|
+
needProviderModel: 'provider and model are required',
|
|
102
|
+
idAuto: 'Leave the id empty to use "{derived}" — it is only how the main model names this member, so renaming the label later does not break references.',
|
|
103
|
+
idInvalid: 'The id may only contain letters, digits, dot, underscore, and dash, up to 40 characters.',
|
|
104
|
+
providerUnknown: 'This provider is not registered; calls will fail',
|
|
105
|
+
keyHint: 'The api key is not part of a member: it belongs to the provider (one key per endpoint). Set it once under Settings → Models; every model on that provider shares it. Fill in the provider name here.',
|
|
106
|
+
providerOptions: 'Registered providers',
|
|
107
|
+
modelOptions: 'Known models on this provider',
|
|
108
|
+
hint: 'Edits land in the profile Cordis patch: the loader recomposes this entry, so the tool and the roster change with it. The provider must already be configured under Settings → Models.',
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// ── 样式(只用主题 token,带兜底)────────────────────────────────────
|
|
112
|
+
const BORDER = '1px solid var(--dsw-alias-border-light, rgba(0,0,0,0.10))'
|
|
113
|
+
const SECONDARY = 'var(--dsw-alias-label-secondary, #6b6b6b)'
|
|
114
|
+
const MONO = 'var(--dsh-font-family-mono, ui-monospace, SFMono-Regular, Menlo, monospace)'
|
|
115
|
+
|
|
116
|
+
const S = {
|
|
117
|
+
page: {
|
|
118
|
+
boxSizing: 'border-box',
|
|
119
|
+
height: '100%',
|
|
120
|
+
overflowY: 'auto',
|
|
121
|
+
padding: 'calc(var(--dsh-frame-top-clearance, 0px) + 28px) 32px 48px',
|
|
122
|
+
color: 'var(--dsw-alias-label-primary, #1a1a1a)',
|
|
123
|
+
background: 'var(--dsw-alias-bg-base, transparent)',
|
|
124
|
+
},
|
|
125
|
+
inner: { maxWidth: 760, margin: '0 auto' },
|
|
126
|
+
head: { display: 'flex', alignItems: 'flex-start', gap: 12, marginBottom: 4 },
|
|
127
|
+
title: { fontSize: 20, lineHeight: '28px', fontWeight: 600, margin: 0, flex: '1 1 auto' },
|
|
128
|
+
intro: { fontSize: 13, lineHeight: '20px', margin: '0 0 28px', color: SECONDARY },
|
|
129
|
+
sectionHead: { display: 'flex', alignItems: 'center', gap: 8, margin: '0 0 10px' },
|
|
130
|
+
sectionTitle: {
|
|
131
|
+
fontSize: 12,
|
|
132
|
+
lineHeight: '18px',
|
|
133
|
+
fontWeight: 600,
|
|
134
|
+
letterSpacing: '0.02em',
|
|
135
|
+
textTransform: 'uppercase',
|
|
136
|
+
margin: 0,
|
|
137
|
+
color: SECONDARY,
|
|
138
|
+
flex: '1 1 auto',
|
|
139
|
+
},
|
|
140
|
+
card: {
|
|
141
|
+
border: BORDER,
|
|
142
|
+
borderRadius: 10,
|
|
143
|
+
padding: '12px 14px',
|
|
144
|
+
marginBottom: 8,
|
|
145
|
+
background: 'var(--dsw-alias-bg-layer-1, transparent)',
|
|
146
|
+
},
|
|
147
|
+
cardEditing: { background: 'var(--dsw-alias-bg-layer-2, rgba(0,0,0,0.03))' },
|
|
148
|
+
rowMain: { display: 'flex', alignItems: 'baseline', gap: 8, flexWrap: 'wrap' },
|
|
149
|
+
name: { fontSize: 14, lineHeight: '22px', fontWeight: 600 },
|
|
150
|
+
mono: { fontFamily: MONO, fontSize: 12, lineHeight: '18px', color: SECONDARY },
|
|
151
|
+
note: { fontSize: 12, lineHeight: '18px', marginTop: 4, color: SECONDARY },
|
|
152
|
+
dot: {
|
|
153
|
+
width: 6,
|
|
154
|
+
height: 6,
|
|
155
|
+
borderRadius: 3,
|
|
156
|
+
display: 'inline-block',
|
|
157
|
+
background: 'var(--dsw-alias-state-success-primary, #2ea043)',
|
|
158
|
+
flex: '0 0 auto',
|
|
159
|
+
},
|
|
160
|
+
dotBad: { background: 'var(--dsw-alias-state-error-primary, #d1242f)' },
|
|
161
|
+
button: {
|
|
162
|
+
appearance: 'none',
|
|
163
|
+
border: BORDER,
|
|
164
|
+
background: 'transparent',
|
|
165
|
+
color: 'inherit',
|
|
166
|
+
borderRadius: 8,
|
|
167
|
+
padding: '4px 10px',
|
|
168
|
+
fontSize: 12,
|
|
169
|
+
lineHeight: '18px',
|
|
170
|
+
cursor: 'pointer',
|
|
171
|
+
flex: '0 0 auto',
|
|
172
|
+
font: 'inherit',
|
|
173
|
+
},
|
|
174
|
+
buttonPrimary: {
|
|
175
|
+
appearance: 'none',
|
|
176
|
+
border: '1px solid transparent',
|
|
177
|
+
background: 'var(--dsw-alias-brand-primary, #247bbf)',
|
|
178
|
+
color: '#fff',
|
|
179
|
+
borderRadius: 8,
|
|
180
|
+
padding: '4px 12px',
|
|
181
|
+
fontSize: 12,
|
|
182
|
+
lineHeight: '18px',
|
|
183
|
+
cursor: 'pointer',
|
|
184
|
+
flex: '0 0 auto',
|
|
185
|
+
font: 'inherit',
|
|
186
|
+
},
|
|
187
|
+
buttonDisabled: { opacity: 0.5, cursor: 'default' },
|
|
188
|
+
actions: { display: 'flex', gap: 6, marginTop: 10, flexWrap: 'wrap' },
|
|
189
|
+
field: {
|
|
190
|
+
display: 'grid',
|
|
191
|
+
gridTemplateColumns: '92px 1fr',
|
|
192
|
+
gap: 8,
|
|
193
|
+
alignItems: 'center',
|
|
194
|
+
marginBottom: 6,
|
|
195
|
+
},
|
|
196
|
+
fieldLabel: { fontSize: 12, lineHeight: '18px', color: SECONDARY },
|
|
197
|
+
input: {
|
|
198
|
+
boxSizing: 'border-box',
|
|
199
|
+
width: '100%',
|
|
200
|
+
padding: '5px 8px',
|
|
201
|
+
fontSize: 12,
|
|
202
|
+
lineHeight: '18px',
|
|
203
|
+
borderRadius: 6,
|
|
204
|
+
border: BORDER,
|
|
205
|
+
background: 'var(--dsw-alias-bg-base, transparent)',
|
|
206
|
+
color: 'inherit',
|
|
207
|
+
font: 'inherit',
|
|
208
|
+
},
|
|
209
|
+
muted: { fontSize: 13, lineHeight: '20px', color: SECONDARY, margin: 0 },
|
|
210
|
+
hint: { fontSize: 12, lineHeight: '18px', marginTop: 24, color: SECONDARY },
|
|
211
|
+
callRow: {
|
|
212
|
+
display: 'flex',
|
|
213
|
+
alignItems: 'center',
|
|
214
|
+
gap: 8,
|
|
215
|
+
padding: '5px 0',
|
|
216
|
+
fontSize: 12,
|
|
217
|
+
lineHeight: '18px',
|
|
218
|
+
},
|
|
219
|
+
errorLine: {
|
|
220
|
+
fontSize: 12,
|
|
221
|
+
lineHeight: '18px',
|
|
222
|
+
marginTop: 8,
|
|
223
|
+
color: 'var(--dsw-alias-state-error-primary, #d1242f)',
|
|
224
|
+
},
|
|
225
|
+
warnLine: {
|
|
226
|
+
fontSize: 12,
|
|
227
|
+
lineHeight: '18px',
|
|
228
|
+
marginTop: 4,
|
|
229
|
+
color: 'var(--dsw-alias-state-warn-primary, #9a6700)',
|
|
230
|
+
},
|
|
231
|
+
hintLine: {
|
|
232
|
+
fontSize: 12,
|
|
233
|
+
lineHeight: '18px',
|
|
234
|
+
marginTop: 6,
|
|
235
|
+
color: SECONDARY,
|
|
236
|
+
},
|
|
237
|
+
emptyDraft: {
|
|
238
|
+
fontSize: 12,
|
|
239
|
+
lineHeight: '18px',
|
|
240
|
+
marginTop: 2,
|
|
241
|
+
color: SECONDARY,
|
|
242
|
+
},
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
function FleetIcon({ size }) {
|
|
246
|
+
const s = size || 16
|
|
247
|
+
return h(
|
|
248
|
+
'svg',
|
|
249
|
+
{
|
|
250
|
+
viewBox: '0 0 24 24',
|
|
251
|
+
width: s,
|
|
252
|
+
height: s,
|
|
253
|
+
fill: 'none',
|
|
254
|
+
stroke: 'currentColor',
|
|
255
|
+
strokeWidth: 1.6,
|
|
256
|
+
'aria-hidden': true,
|
|
257
|
+
style: { display: 'block' },
|
|
258
|
+
},
|
|
259
|
+
h('circle', { cx: 12, cy: 12, r: 3.2 }),
|
|
260
|
+
h('circle', { cx: 5, cy: 6, r: 2 }),
|
|
261
|
+
h('circle', { cx: 19, cy: 6, r: 2 }),
|
|
262
|
+
h('circle', { cx: 5, cy: 18, r: 2 }),
|
|
263
|
+
h('circle', { cx: 19, cy: 18, r: 2 }),
|
|
264
|
+
h('path', { d: 'M6.7 7.3 9.6 10.2M17.3 7.3 14.4 10.2M6.7 16.7 9.6 13.8M17.3 16.7 14.4 13.8' }),
|
|
265
|
+
)
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
// ── 编辑表单 ─────────────────────────────────────────────────────────
|
|
269
|
+
// 和宿主那边同一套规则:由 provider/model 推一个合法 id,抢先把占位符显示出来。
|
|
270
|
+
function slugId(source) {
|
|
271
|
+
const slug = String(source == null ? '' : source)
|
|
272
|
+
.replace(/[^a-zA-Z0-9._-]/g, '-')
|
|
273
|
+
.replace(/-{2,}/g, '-')
|
|
274
|
+
.replace(/^[-.]+|[-.]+$/g, '')
|
|
275
|
+
.slice(0, 40)
|
|
276
|
+
return /[a-z0-9]/i.test(slug) ? slug : 'member'
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
// 顺序按「人怎么想」来:先起名、再挑 provider/model、最后才轮到 id。
|
|
280
|
+
// 把 id 放第一格是有代价的——用户会把想好的名字填进第一个框,然后被 id 规则拒绝。
|
|
281
|
+
const FIELDS = [
|
|
282
|
+
{ key: 'label', label: 'fLabel' },
|
|
283
|
+
{ key: 'provider', label: 'fProvider', required: true },
|
|
284
|
+
{ key: 'model', label: 'fModel', required: true },
|
|
285
|
+
{ key: 'note', label: 'fNote', hint: 'fNoteHint' },
|
|
286
|
+
{ key: 'id', label: 'fId', hint: 'fIdHint' },
|
|
287
|
+
]
|
|
288
|
+
|
|
289
|
+
function MemberForm({ t, draft, providers, onChange, onSave, onCancel, saving, error }) {
|
|
290
|
+
const idRaw = String(draft.id || '').trim()
|
|
291
|
+
const derivedId = slugId(`${draft.provider}-${draft.model}`)
|
|
292
|
+
const idBad = Boolean(idRaw) && !/^[a-zA-Z0-9._-]{1,40}$/.test(idRaw)
|
|
293
|
+
const ready = Boolean(draft.provider.trim() && draft.model.trim()) && !idBad
|
|
294
|
+
const known = providers.map((provider) => provider.id)
|
|
295
|
+
const chosen = providers.find((provider) => provider.id === draft.provider)
|
|
296
|
+
const models = chosen ? chosen.models || [] : []
|
|
297
|
+
|
|
298
|
+
return h(
|
|
299
|
+
'div',
|
|
300
|
+
{ style: { ...S.card, ...S.cardEditing } },
|
|
301
|
+
...FIELDS.map((field) =>
|
|
302
|
+
h(
|
|
303
|
+
'label',
|
|
304
|
+
{ key: field.key, style: S.field },
|
|
305
|
+
h('span', { style: S.fieldLabel }, t(field.label) + (field.required ? ' *' : '')),
|
|
306
|
+
h('input', {
|
|
307
|
+
style: S.input,
|
|
308
|
+
name: field.key,
|
|
309
|
+
value: draft[field.key],
|
|
310
|
+
spellCheck: false,
|
|
311
|
+
// id 的占位符直接用「留空时会被采用的 id」,比一句说明有用。
|
|
312
|
+
placeholder: field.key === 'id' ? derivedId : field.hint ? t(field.hint) : '',
|
|
313
|
+
// provider / model 用 datalist:既能从已知项里挑,也不挡手填(新 provider 还没列进来时)。
|
|
314
|
+
...(field.key === 'provider' && known.length ? { list: 'fleet-providers', autoComplete: 'off' } : {}),
|
|
315
|
+
...(field.key === 'model' && models.length ? { list: 'fleet-models', autoComplete: 'off' } : {}),
|
|
316
|
+
onChange: (event) => onChange(field.key, event.target.value),
|
|
317
|
+
}),
|
|
318
|
+
),
|
|
319
|
+
),
|
|
320
|
+
known.length
|
|
321
|
+
? h('datalist', { id: 'fleet-providers' }, ...providers.map((provider) => h('option', { key: provider.id, value: provider.id })))
|
|
322
|
+
: null,
|
|
323
|
+
models.length
|
|
324
|
+
? h('datalist', { id: 'fleet-models' }, ...models.map((model) => h('option', { key: model, value: model })))
|
|
325
|
+
: null,
|
|
326
|
+
idBad ? h('div', { style: S.errorLine }, t('idInvalid')) : h('div', { style: S.hintLine }, t('idAuto').replace('{derived}', derivedId)),
|
|
327
|
+
!ready && !idBad ? h('div', { style: S.emptyDraft }, t('needProviderModel')) : null,
|
|
328
|
+
h('div', { style: S.hintLine }, t('keyHint')),
|
|
329
|
+
error ? h('div', { style: S.errorLine }, `${t('saveFailed')}: ${error}`) : null,
|
|
330
|
+
h(
|
|
331
|
+
'div',
|
|
332
|
+
{ style: S.actions },
|
|
333
|
+
h(
|
|
334
|
+
'button',
|
|
335
|
+
{
|
|
336
|
+
type: 'button',
|
|
337
|
+
style: { ...S.buttonPrimary, ...(ready && !saving ? {} : S.buttonDisabled) },
|
|
338
|
+
disabled: !ready || saving,
|
|
339
|
+
onClick: onSave,
|
|
340
|
+
},
|
|
341
|
+
saving ? t('saving') : t('save'),
|
|
342
|
+
),
|
|
343
|
+
h('button', { type: 'button', style: S.button, onClick: onCancel }, t('cancel')),
|
|
344
|
+
),
|
|
345
|
+
)
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
// ── 页面 ─────────────────────────────────────────────────────────────
|
|
349
|
+
function FleetPage({ t }) {
|
|
350
|
+
const [state, setState] = React.useState({ status: 'loading', data: null, error: '' })
|
|
351
|
+
const [nonce, setNonce] = React.useState(0)
|
|
352
|
+
const [editing, setEditing] = React.useState(null)
|
|
353
|
+
const [draft, setDraft] = React.useState({ id: '', label: '', provider: '', model: '', note: '' })
|
|
354
|
+
const [saving, setSaving] = React.useState(false)
|
|
355
|
+
const [saveError, setSaveError] = React.useState('')
|
|
356
|
+
const [confirmId, setConfirmId] = React.useState(null)
|
|
357
|
+
|
|
358
|
+
React.useEffect(() => {
|
|
359
|
+
let alive = true
|
|
360
|
+
const controller = typeof AbortController === 'function' ? new AbortController() : null
|
|
361
|
+
setState((prev) => (prev.status === 'loading' ? prev : { status: 'loading', data: prev.data, error: '' }))
|
|
362
|
+
fetch(API, {
|
|
363
|
+
method: 'GET',
|
|
364
|
+
credentials: 'same-origin',
|
|
365
|
+
headers: { accept: 'application/json' },
|
|
366
|
+
...(controller ? { signal: controller.signal } : {}),
|
|
367
|
+
})
|
|
368
|
+
.then(async (response) => {
|
|
369
|
+
if (!response.ok) throw new Error(`HTTP ${response.status}`)
|
|
370
|
+
return response.json()
|
|
371
|
+
})
|
|
372
|
+
.then((data) => {
|
|
373
|
+
if (alive) setState({ status: 'ready', data, error: '' })
|
|
374
|
+
})
|
|
375
|
+
.catch((error) => {
|
|
376
|
+
if (!alive || (error && error.name === 'AbortError')) return
|
|
377
|
+
setState({ status: 'error', data: null, error: String((error && error.message) || error) })
|
|
378
|
+
})
|
|
379
|
+
return () => {
|
|
380
|
+
alive = false
|
|
381
|
+
if (controller) controller.abort()
|
|
382
|
+
}
|
|
383
|
+
}, [nonce])
|
|
384
|
+
|
|
385
|
+
const data = state.data || { members: [], calls: [], main: null }
|
|
386
|
+
const members = Array.isArray(data.members) ? data.members : []
|
|
387
|
+
const calls = Array.isArray(data.calls) ? data.calls : []
|
|
388
|
+
const main = data.main || null
|
|
389
|
+
const providers = Array.isArray(data.providers) ? data.providers : []
|
|
390
|
+
const knownProviders = new Set(providers.map((provider) => provider.id))
|
|
391
|
+
// 目录读不到时不要乱扣帽子:空目录只是「不知道」,不是「没注册」。
|
|
392
|
+
const canJudge = providers.length > 0
|
|
393
|
+
|
|
394
|
+
// 保存:整表 POST。成功后就地更新,不等重挂——写盘会触发 loader 重新组合,
|
|
395
|
+
// 这时候再发一个 GET 有可能撞上重挂的窗口期。
|
|
396
|
+
const save = async (next) => {
|
|
397
|
+
setSaving(true)
|
|
398
|
+
setSaveError('')
|
|
399
|
+
try {
|
|
400
|
+
const response = await fetch(API, {
|
|
401
|
+
method: 'POST',
|
|
402
|
+
credentials: 'same-origin',
|
|
403
|
+
headers: { 'content-type': 'application/json', accept: 'application/json' },
|
|
404
|
+
body: JSON.stringify({ members: next }),
|
|
405
|
+
})
|
|
406
|
+
const payload = await response.json().catch(() => ({}))
|
|
407
|
+
if (!response.ok || payload.ok === false) {
|
|
408
|
+
throw new Error(payload.error || `HTTP ${response.status}`)
|
|
409
|
+
}
|
|
410
|
+
setState((prev) => ({
|
|
411
|
+
status: 'ready',
|
|
412
|
+
data: { ...(prev.data || {}), members: payload.members || [] },
|
|
413
|
+
error: '',
|
|
414
|
+
}))
|
|
415
|
+
setEditing(null)
|
|
416
|
+
setConfirmId(null)
|
|
417
|
+
setDraft({ id: '', label: '', provider: '', model: '', note: '' })
|
|
418
|
+
} catch (error) {
|
|
419
|
+
setSaveError(String((error && error.message) || error))
|
|
420
|
+
} finally {
|
|
421
|
+
setSaving(false)
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
const startNew = () => {
|
|
426
|
+
setSaveError('')
|
|
427
|
+
setConfirmId(null)
|
|
428
|
+
setEditing('__new__')
|
|
429
|
+
setDraft({ id: '', label: '', provider: '', model: '', note: '' })
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
const startEdit = (member) => {
|
|
433
|
+
setSaveError('')
|
|
434
|
+
setConfirmId(null)
|
|
435
|
+
setEditing(member.id)
|
|
436
|
+
setDraft({
|
|
437
|
+
id: member.id || '',
|
|
438
|
+
label: member.label || '',
|
|
439
|
+
provider: member.provider || '',
|
|
440
|
+
model: member.model || '',
|
|
441
|
+
note: member.note || '',
|
|
442
|
+
})
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
const commitDraft = () => {
|
|
446
|
+
if (editing === '__new__') {
|
|
447
|
+
save([...members, draft])
|
|
448
|
+
return
|
|
449
|
+
}
|
|
450
|
+
save(members.map((member) => (member.id === editing ? { ...member, ...draft } : member)))
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
const header = h(
|
|
454
|
+
'div',
|
|
455
|
+
{ style: S.head },
|
|
456
|
+
h('h1', { style: S.title }, t('title')),
|
|
457
|
+
h(
|
|
458
|
+
'button',
|
|
459
|
+
{
|
|
460
|
+
type: 'button',
|
|
461
|
+
style: S.button,
|
|
462
|
+
onClick: () => setNonce((n) => n + 1),
|
|
463
|
+
disabled: state.status === 'loading',
|
|
464
|
+
},
|
|
465
|
+
t('refresh'),
|
|
466
|
+
),
|
|
467
|
+
)
|
|
468
|
+
|
|
469
|
+
if (state.status === 'loading' && !state.data) {
|
|
470
|
+
return h('div', { style: S.page }, h('div', { style: S.inner }, header, h('p', { style: S.muted }, t('loading'))))
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
if (state.status === 'error') {
|
|
474
|
+
return h(
|
|
475
|
+
'div',
|
|
476
|
+
{ style: S.page },
|
|
477
|
+
h(
|
|
478
|
+
'div',
|
|
479
|
+
{ style: S.inner },
|
|
480
|
+
header,
|
|
481
|
+
h('p', { style: S.muted }, `${t('error')}: ${state.error}`),
|
|
482
|
+
h('div', { style: { marginTop: 12 } },
|
|
483
|
+
h('button', { type: 'button', style: S.button, onClick: () => setNonce((n) => n + 1) }, t('retry'))),
|
|
484
|
+
),
|
|
485
|
+
)
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
const mainCard = h(
|
|
489
|
+
'div',
|
|
490
|
+
{ style: S.card },
|
|
491
|
+
h('div', { style: S.sectionTitle }, t('mainLabel')),
|
|
492
|
+
h('div', { style: S.rowMain }, h('span', { style: S.name }, main ? `${main.provider} / ${main.model}` : t('mainUnknown'))),
|
|
493
|
+
)
|
|
494
|
+
|
|
495
|
+
const cards = []
|
|
496
|
+
for (const member of members) {
|
|
497
|
+
if (editing === member.id) {
|
|
498
|
+
cards.push(
|
|
499
|
+
h(MemberForm, {
|
|
500
|
+
key: member.id,
|
|
501
|
+
t,
|
|
502
|
+
draft,
|
|
503
|
+
providers,
|
|
504
|
+
saving,
|
|
505
|
+
error: saveError,
|
|
506
|
+
onChange: (key, value) => setDraft((prev) => ({ ...prev, [key]: value })),
|
|
507
|
+
onSave: commitDraft,
|
|
508
|
+
onCancel: () => {
|
|
509
|
+
setEditing(null)
|
|
510
|
+
setSaveError('')
|
|
511
|
+
},
|
|
512
|
+
}),
|
|
513
|
+
)
|
|
514
|
+
continue
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
const actions = []
|
|
518
|
+
if (confirmId === member.id) {
|
|
519
|
+
actions.push(h('span', { key: 'q', style: S.emptyDraft }, t('confirmPrompt')))
|
|
520
|
+
actions.push(
|
|
521
|
+
h(
|
|
522
|
+
'button',
|
|
523
|
+
{
|
|
524
|
+
key: 'y',
|
|
525
|
+
type: 'button',
|
|
526
|
+
style: { ...S.button, color: 'var(--dsw-alias-state-error-primary, #d1242f)' },
|
|
527
|
+
onClick: () => save(members.filter((row) => row.id !== member.id)),
|
|
528
|
+
},
|
|
529
|
+
t('confirm'),
|
|
530
|
+
),
|
|
531
|
+
)
|
|
532
|
+
actions.push(
|
|
533
|
+
h('button', { key: 'n', type: 'button', style: S.button, onClick: () => setConfirmId(null) }, t('cancel')),
|
|
534
|
+
)
|
|
535
|
+
} else {
|
|
536
|
+
actions.push(
|
|
537
|
+
h('button', { key: 'e', type: 'button', style: S.button, onClick: () => startEdit(member) }, t('edit')),
|
|
538
|
+
)
|
|
539
|
+
actions.push(
|
|
540
|
+
h('button', { key: 'r', type: 'button', style: S.button, onClick: () => setConfirmId(member.id) }, t('remove')),
|
|
541
|
+
)
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
const known = knownProviders.has(member.provider)
|
|
545
|
+
cards.push(
|
|
546
|
+
h(
|
|
547
|
+
'div',
|
|
548
|
+
{ key: member.id, style: S.card },
|
|
549
|
+
h(
|
|
550
|
+
'div',
|
|
551
|
+
{ style: S.rowMain },
|
|
552
|
+
h('span', { style: { ...S.dot, ...(canJudge && !known ? S.dotBad : {}) } }),
|
|
553
|
+
h('span', { style: S.name }, member.label),
|
|
554
|
+
h('span', { style: S.mono }, member.id),
|
|
555
|
+
h('span', { style: S.mono }, `${member.provider}/${member.model}`),
|
|
556
|
+
),
|
|
557
|
+
member.note ? h('div', { style: S.note }, member.note) : null,
|
|
558
|
+
canJudge && !known ? h('div', { style: S.warnLine }, t('providerUnknown')) : null,
|
|
559
|
+
h('div', { style: S.actions }, ...actions),
|
|
560
|
+
),
|
|
561
|
+
)
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
if (editing === '__new__') {
|
|
565
|
+
cards.push(
|
|
566
|
+
h(MemberForm, {
|
|
567
|
+
key: '__new__',
|
|
568
|
+
t,
|
|
569
|
+
draft,
|
|
570
|
+
providers,
|
|
571
|
+
saving,
|
|
572
|
+
error: saveError,
|
|
573
|
+
onChange: (key, value) => setDraft((prev) => ({ ...prev, [key]: value })),
|
|
574
|
+
onSave: commitDraft,
|
|
575
|
+
onCancel: () => {
|
|
576
|
+
setEditing(null)
|
|
577
|
+
setSaveError('')
|
|
578
|
+
},
|
|
579
|
+
}),
|
|
580
|
+
)
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
if (members.length === 0 && editing !== '__new__') {
|
|
584
|
+
cards.push(
|
|
585
|
+
h(
|
|
586
|
+
'div',
|
|
587
|
+
{ key: '__empty__', style: S.card },
|
|
588
|
+
h('p', { style: { ...S.muted, fontWeight: 600 } }, t('membersEmpty')),
|
|
589
|
+
h('p', { style: S.note }, t('membersEmptyHint')),
|
|
590
|
+
),
|
|
591
|
+
)
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
const memberSection = h(
|
|
595
|
+
'div',
|
|
596
|
+
{ style: { marginBottom: 28 } },
|
|
597
|
+
h(
|
|
598
|
+
'div',
|
|
599
|
+
{ style: S.sectionHead },
|
|
600
|
+
h('div', { style: S.sectionTitle }, t('membersTitle')),
|
|
601
|
+
editing === '__new__'
|
|
602
|
+
? null
|
|
603
|
+
: h('button', { type: 'button', style: S.button, onClick: startNew }, t('add')),
|
|
604
|
+
),
|
|
605
|
+
...cards,
|
|
606
|
+
)
|
|
607
|
+
|
|
608
|
+
const callList = calls.length
|
|
609
|
+
? calls
|
|
610
|
+
.slice()
|
|
611
|
+
.reverse()
|
|
612
|
+
.map((call) =>
|
|
613
|
+
h(
|
|
614
|
+
'div',
|
|
615
|
+
{ key: call.seq, style: S.callRow },
|
|
616
|
+
h('span', { style: { ...S.dot, ...(call.ok ? {} : S.dotBad) } }),
|
|
617
|
+
h('span', { style: S.name }, call.label || call.member),
|
|
618
|
+
h(
|
|
619
|
+
'span',
|
|
620
|
+
{ style: S.mono },
|
|
621
|
+
[
|
|
622
|
+
call.ok ? t('ok') : t('failed'),
|
|
623
|
+
call.inputTokens != null || call.outputTokens != null
|
|
624
|
+
? `${call.inputTokens ?? 0}→${call.outputTokens ?? 0} ${t('tokens')}`
|
|
625
|
+
: '',
|
|
626
|
+
`${call.elapsedMs ?? 0}ms`,
|
|
627
|
+
]
|
|
628
|
+
.filter(Boolean)
|
|
629
|
+
.join(' · '),
|
|
630
|
+
),
|
|
631
|
+
),
|
|
632
|
+
)
|
|
633
|
+
: h('p', { style: S.muted }, t('callsEmpty'))
|
|
634
|
+
|
|
635
|
+
return h(
|
|
636
|
+
'div',
|
|
637
|
+
{ style: S.page },
|
|
638
|
+
h(
|
|
639
|
+
'div',
|
|
640
|
+
{ style: S.inner },
|
|
641
|
+
header,
|
|
642
|
+
h('p', { style: S.intro }, t('intro')),
|
|
643
|
+
h('div', { style: { marginBottom: 24 } }, mainCard),
|
|
644
|
+
memberSection,
|
|
645
|
+
h('div', { style: S.sectionTitle }, t('callsTitle')),
|
|
646
|
+
h('div', { style: { marginBottom: 8 } }, callList),
|
|
647
|
+
h('p', { style: S.hint }, t('hint')),
|
|
648
|
+
),
|
|
649
|
+
)
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
return {
|
|
653
|
+
inject: ['slots', 'locale'],
|
|
654
|
+
apply(ctx) {
|
|
655
|
+
ctx.effect(() => ctx.locale.register(NS, { zh, en }), 'dsh-model-fleet: dictionaries')
|
|
656
|
+
const t = ctx.locale.bind(NS)
|
|
657
|
+
const Page = () => h(FleetPage, { t })
|
|
658
|
+
|
|
659
|
+
ctx.slots.inject('sidebar.panellist', () =>
|
|
660
|
+
ctx.slots.register(
|
|
661
|
+
{
|
|
662
|
+
name: 'sidebar.panellist',
|
|
663
|
+
id: PANEL_ID,
|
|
664
|
+
order: 40,
|
|
665
|
+
label: () => t('panel'),
|
|
666
|
+
locale: NS,
|
|
667
|
+
},
|
|
668
|
+
FleetIcon,
|
|
669
|
+
),
|
|
670
|
+
)
|
|
671
|
+
|
|
672
|
+
ctx.slots.inject('main', () =>
|
|
673
|
+
ctx.slots.register(
|
|
674
|
+
{
|
|
675
|
+
name: 'main',
|
|
676
|
+
key: PANEL_ID,
|
|
677
|
+
locale: NS,
|
|
678
|
+
},
|
|
679
|
+
Page,
|
|
680
|
+
),
|
|
681
|
+
)
|
|
682
|
+
},
|
|
683
|
+
}
|
|
684
|
+
},
|
|
685
|
+
})
|