dsh-kylin-media 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -55,8 +55,11 @@ npm publish # prepack 会自动跑 smoke
55
55
  | 单次最多等待 | 工具调用内等待任务的秒数(10-600);超时不影响网关侧任务,可继续用 `media_job` |
56
56
  | 产物目录 | 留空 = `<会话目录>/media-output/<任务 id>/`;也可填绝对路径 |
57
57
  | 自动下载 | 关闭后只返回远端 URL,用 `media_job action=download` 取回 |
58
+ | 默认模型 | 「可用能力」卡片里逐类型列出网关实时目录中的模型,点选即设为该类型默认模型并立即保存;`media_*` 未传 `model` 时用它,没选则用该类型第一个可用模型 |
58
59
 
59
- 页面上的「测试连接」会读取 `/api/health` 与 `/api/models`,显示网关版本、鉴权模式、ComfyUI 与双卡队列、各类型可用模型数。连接失败时会给出排查提示。
60
+ 页面上的「测试连接」会读取 `/api/health` 与 `/api/models`,显示网关版本、鉴权模式、ComfyUI 与双卡队列,并按能力类型列出具体模型(含可用性与权重缺失),可直接点选默认模型。连接失败时会给出排查提示。
61
+
62
+ > 常见坑:网关默认监听明文 HTTP(`API_PORT=8288`)。frp / 反代把 8288 映射出去后仍然是 HTTP,地址要写 `http://<对外地址>:<端口>`;写成 `https://` 会在 TLS 握手阶段被断开,错误信息里会提示「这个端口没有完成 TLS 握手…把地址改成 http:// 再试」。反过来,如果前面套了 nginx / Caddy 的 HTTPS,则必须写 `https://`(自签证书还需要让运行宿主信任该 CA)。
60
63
 
61
64
  ## 工具
62
65
 
@@ -105,10 +108,12 @@ media_audio action=speech text="你好,这是本地语音合成。" format=mp3
105
108
 
106
109
  ```bash
107
110
  cd dsh-kylin-media
108
- npm test # smoke(包契约 + 客户端自注册 + locale/icon/技能) + selftest(mock ctx + mock 网关)
111
+ npm test # smoke(包契约 + 客户端自注册 + locale/icon/技能)+ selftest(mock ctx + mock 网关)+ client-test(渲染自测)
109
112
  ```
110
113
 
111
- `selftest` 会用临时 HOME 与 `127.0.0.1` 上的 mock 网关跑完整链路:设置持久化、403 / 400 / 404 / 405 围栏、模型选择、提交轮询、产物下载、鉴权头、注册与卸载。
114
+ `selftest` 会用临时 HOME 与 `127.0.0.1` 上的 mock 网关跑完整链路:设置持久化、403 / 400 / 404 / 405 围栏、默认模型选择与回退、提交轮询、产物下载、鉴权头、注册与卸载。
115
+
116
+ `client-test` 用最小 React 替身渲染设置页的「可用能力」卡片:类型卡片列出具体模型、默认模型高亮、点选后 POST `settings.update`、缺权重模型不可选,且渲染树不含明文 Key。
112
117
 
113
118
  ## 目录
114
119
 
@@ -126,7 +131,7 @@ dsh-kylin-media/
126
131
  │ └── routes.js # fenced JSON API
127
132
  ├── skills/kylin-media/ # 随包技能
128
133
  ├── locale/ # 插件管理器展示元数据
129
- └── scripts/ # smoke / selftest
134
+ └── scripts/ # smoke / selftest / client-test
130
135
  ```
131
136
 
132
137
  ## License
package/client.js CHANGED
@@ -60,7 +60,13 @@ window.__ModuleLoader__.load({
60
60
  noKey: '未配置访问 Key',
61
61
  keyConfigured: '访问 Key 已配置',
62
62
  capabilities: '可用能力',
63
- capabilitiesSub: '来自网关 /api/models 的实时目录。灰色表示模型文件缺失,需要服务方补齐权重。',
63
+ capabilitiesSub: '来自网关 /api/models 的实时目录。点选一个模型即设为该能力的默认模型,media_* 工具没传 model 时用它;灰色表示权重缺失。',
64
+ modelsAuto: '自动(第一个可用模型)',
65
+ modelsStale: '已选:',
66
+ modelsEmpty: '没有注册模型',
67
+ missingWeights: '缺权重',
68
+ defaultSaved: '默认模型已更新',
69
+ defaultFailed: '默认模型保存失败:',
64
70
  behavior: '任务与产物',
65
71
  behaviorSub: '这些默认值对所有 media_* 工具生效。',
66
72
  waitSeconds: '单次最多等待(秒)',
@@ -76,7 +82,7 @@ window.__ModuleLoader__.load({
76
82
  available: '可用',
77
83
  unprofiled: '未注册权重',
78
84
  noCatalog: '尚未读取到能力目录,点「测试连接」重试。',
79
- unreachableHint: '检查网关是否已启动、地址是否正确;若网关是 always 模式,还需填写正确的访问 Key。',
85
+ unreachableHint: '检查网关是否已启动、地址是否正确;若网关是 always 模式,还需填写正确的访问 Key。https:// 与 http:// 写反也会连不上(错误信息里会提示)。',
80
86
  testFailed: '连接测试没有通过。',
81
87
  keyRequired: '请先填写访问 Key(网关可能要求鉴权)。',
82
88
  type_text2image: '文生图',
@@ -131,7 +137,13 @@ window.__ModuleLoader__.load({
131
137
  noKey: 'No access key',
132
138
  keyConfigured: 'Access key configured',
133
139
  capabilities: 'Capabilities',
134
- capabilitiesSub: 'Live catalogue from the gateway /api/models. Grey means model files are missing and the operator must add the weights.',
140
+ capabilitiesSub: 'Live catalogue from the gateway /api/models. Pick a model to make it the default for that capability; media_* tools use it when model is omitted. Grey means the weights are missing.',
141
+ modelsAuto: 'Auto (first available model)',
142
+ modelsStale: 'Selected: ',
143
+ modelsEmpty: 'No registered models',
144
+ missingWeights: 'weights missing',
145
+ defaultSaved: 'Default model updated',
146
+ defaultFailed: 'Failed to save the default model: ',
135
147
  behavior: 'Jobs and artifacts',
136
148
  behaviorSub: 'These defaults apply to every media_* tool.',
137
149
  waitSeconds: 'Max wait per call (s)',
@@ -147,7 +159,7 @@ window.__ModuleLoader__.load({
147
159
  available: 'available',
148
160
  unprofiled: 'unprofiled weights',
149
161
  noCatalog: 'No catalogue yet — run "Test connection" again.',
150
- unreachableHint: 'Check that the gateway is running and the address is right; if it uses always mode, also check the access key.',
162
+ unreachableHint: 'Check that the gateway is running and the address is right; if it uses always mode, also check the access key. A mismatched https:// or http:// scheme also fails to connect (the error says which).',
151
163
  testFailed: 'Connection test did not pass.',
152
164
  keyRequired: 'Enter the access key first (the gateway may require auth).',
153
165
  type_text2image: 'Text to image',
@@ -216,11 +228,25 @@ window.__ModuleLoader__.load({
216
228
  '.km-state{margin-top:14px;font-size:12.5px;line-height:1.75;color:var(--dsw-alias-label-secondary,#5c6470);padding:10px 12px;border:1px solid var(--dsw-alias-border-l1,rgba(127,127,127,.12));border-left:3px solid var(--dsw-alias-border-l3,rgba(127,127,127,.3));border-radius:var(--dsw-radius-sm,8px);background:var(--dsw-alias-bg-layer-1,transparent)}',
217
229
  '.km-state[data-kind=ok]{border-left-color:var(--dsw-alias-state-success-primary,#2f8f5b)}',
218
230
  '.km-state[data-kind=error]{color:var(--dsw-alias-state-error-primary,#d9534f);border-left-color:var(--dsw-alias-state-error-primary,#d9534f)}',
219
- '.km-tiles{display:grid;grid-template-columns:repeat(auto-fill,minmax(150px,1fr));gap:10px}',
231
+ '.km-tiles{display:grid;grid-template-columns:repeat(auto-fill,minmax(210px,1fr));gap:10px}',
220
232
  '.km-tile{border:1px solid var(--dsw-alias-border-l2,rgba(127,127,127,.22));border-radius:var(--dsw-radius-sm,10px);padding:11px 12px;background:var(--dsw-alias-bg-layer-1,transparent);display:flex;flex-direction:column;gap:5px;min-width:0}',
221
233
  '.km-tile strong{font-size:12.5px;font-weight:600;color:var(--dsw-alias-label-primary,#1f2329)}',
222
- '.km-tile span{font-size:11.5px;color:var(--dsw-alias-label-tertiary,#8a919c)}',
234
+ '.km-tile>span,.km-tile-head span{font-size:11.5px;color:var(--dsw-alias-label-tertiary,#8a919c)}',
223
235
  '.km-tile[data-state=missing]{opacity:.55}',
236
+ '.km-tile-head{display:flex;align-items:baseline;justify-content:space-between;gap:8px}',
237
+ '.km-tile-models{gap:8px}',
238
+ '.km-models{display:flex;flex-direction:column;gap:1px}',
239
+ '.km-model{appearance:none;display:flex;align-items:center;gap:7px;width:100%;box-sizing:border-box;background:transparent;border:1px solid transparent;border-radius:var(--dsw-radius-sm,8px);padding:4px 6px;font:inherit;font-size:11.5px;line-height:1.45;color:var(--dsw-alias-label-secondary,#5c6470);text-align:left;cursor:pointer;transition:background-color .14s ease,border-color .14s ease,color .14s ease}',
240
+ '.km-model:hover{background:var(--dsw-alias-interactive-bg-hover,rgba(127,127,127,.07))}',
241
+ '.km-model:focus-visible{outline:2px solid var(--dsw-alias-brand-primary,#4c7dff);outline-offset:1px}',
242
+ '.km-model[data-selected=true]{border-color:var(--dsw-alias-brand-primary,#4c7dff);color:var(--dsw-alias-label-primary,#1f2329);background:color-mix(in srgb, var(--dsw-alias-brand-primary,#4c7dff) 12%, transparent)}',
243
+ '.km-model[data-disabled=true]{opacity:.5;cursor:not-allowed}',
244
+ '.km-model-radio{width:11px;height:11px;flex:none;border-radius:50%;border:1px solid var(--dsw-alias-border-l3,rgba(127,127,127,.45));position:relative}',
245
+ '.km-model[data-selected=true] .km-model-radio{border-color:var(--dsw-alias-brand-primary,#4c7dff)}',
246
+ '.km-model[data-selected=true] .km-model-radio::after{content:"";position:absolute;inset:2px;border-radius:50%;background:var(--dsw-alias-brand-primary,#4c7dff)}',
247
+ '.km-model-name{flex:1;min-width:0;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;font-size:11.5px;color:inherit}',
248
+ '.km-model-badge{flex:none;font-size:10px;line-height:1;padding:2px 5px;border-radius:999px;border:1px solid var(--dsw-alias-border-l2,rgba(127,127,127,.3));color:var(--dsw-alias-label-tertiary,#8a919c)}',
249
+ '.km-model-empty,.km-model-stale{font-size:11px;line-height:1.5;color:var(--dsw-alias-label-tertiary,#8a919c)}',
224
250
  '.km-check{display:flex;align-items:flex-start;gap:8px;font-size:12px;line-height:1.6;color:var(--dsw-alias-label-secondary,#5c6470);cursor:pointer}',
225
251
  '.km-check input{width:15px;height:15px;margin:1px 0 0;accent-color:var(--dsw-alias-brand-primary,#4c7dff);flex:none;cursor:pointer}',
226
252
  '.km-foot{margin:0;color:var(--dsw-alias-label-tertiary,#8a919c);font-size:11.5px;line-height:1.7}',
@@ -233,14 +259,24 @@ window.__ModuleLoader__.load({
233
259
  return function () { if (style.parentNode) style.parentNode.removeChild(style); };
234
260
  }
235
261
 
262
+ /* 写操作一律 POST:路由只放行 GET 的 state,其余动作 GET 会被 405 拒绝,
263
+ 所以这里绝不按「有没有 body」决定方法。 */
236
264
  function request(action, body) {
237
- var options = { headers: { accept: 'application/json' } };
238
- if (body !== undefined) {
239
- options.method = 'POST';
240
- options.headers['content-type'] = 'application/json';
241
- options.body = JSON.stringify(body);
242
- }
243
- return fetch(API + '/' + action, options).then(function (response) {
265
+ var payload = body === undefined ? {} : body;
266
+ return fetch(API + '/' + action, {
267
+ method: 'POST',
268
+ headers: { accept: 'application/json', 'content-type': 'application/json' },
269
+ body: JSON.stringify(payload)
270
+ }).then(function (response) {
271
+ return response.json().catch(function () {
272
+ return { ok: false, error: { code: 'http-' + response.status, message: 'HTTP ' + response.status } };
273
+ });
274
+ });
275
+ }
276
+
277
+ /* 只读查询(目前只有 state 允许 GET)。 */
278
+ function query(action) {
279
+ return fetch(API + '/' + action, { headers: { accept: 'application/json' } }).then(function (response) {
244
280
  return response.json().catch(function () {
245
281
  return { ok: false, error: { code: 'http-' + response.status, message: 'HTTP ' + response.status } };
246
282
  });
@@ -282,10 +318,39 @@ window.__ModuleLoader__.load({
282
318
  var busyHook = React.useState(false);
283
319
  var busy = busyHook[0];
284
320
  var setBusy = busyHook[1];
321
+ var defsHook = React.useState({});
322
+ var defaults = defsHook[0];
323
+ var setDefaults = defsHook[1];
324
+ var modelNoticeHook = React.useState(null);
325
+ var modelNotice = modelNoticeHook[0];
326
+ var setModelNotice = modelNoticeHook[1];
327
+
328
+ /* 点选即保存:{ type: modelId },空串表示回到「自动」。 */
329
+ function selectModel(type, modelId) {
330
+ var previous = defaults;
331
+ var next = Object.assign({}, defaults);
332
+ if (modelId === '') delete next[type];
333
+ else next[type] = modelId;
334
+ setDefaults(next);
335
+ setModelNotice(null);
336
+ request('settings.update', { patch: { defaultModels: next } }).then(function (payload) {
337
+ if (!payload.ok) throw new Error(payloadError(payload));
338
+ setSnapshot(function (current) {
339
+ if (current === null || current.data === null || current.data === undefined) return current;
340
+ return Object.assign({}, current, {
341
+ data: Object.assign({}, current.data, { defaultModels: next })
342
+ });
343
+ });
344
+ setModelNotice({ kind: 'ok', text: t('defaultSaved') });
345
+ }).catch(function (error) {
346
+ setDefaults(previous);
347
+ setModelNotice({ kind: 'error', text: t('defaultFailed') + (error && error.message ? error.message : String(error)) });
348
+ });
349
+ }
285
350
 
286
351
  function runTest() {
287
352
  setTest({ status: 'loading', data: null, error: null });
288
- request('test').then(function (payload) {
353
+ request('test', {}).then(function (payload) {
289
354
  if (!payload.ok) throw new Error(payloadError(payload));
290
355
  setTest({ status: 'ready', data: payload.value, error: null });
291
356
  }).catch(function (error) {
@@ -295,9 +360,10 @@ window.__ModuleLoader__.load({
295
360
 
296
361
  function load() {
297
362
  setSnapshot({ status: 'loading', data: null, error: null });
298
- request('state').then(function (payload) {
363
+ query('state').then(function (payload) {
299
364
  if (!payload.ok) throw new Error(payloadError(payload));
300
365
  setSnapshot({ status: 'ready', data: payload.value, error: null });
366
+ setDefaults(payload.value.defaultModels || {});
301
367
  setDraft({
302
368
  baseUrl: payload.value.baseUrl,
303
369
  waitSeconds: String(payload.value.waitSeconds),
@@ -342,10 +408,11 @@ window.__ModuleLoader__.load({
342
408
  }).then(function (payload) {
343
409
  if (payload !== null && !payload.ok) throw new Error(payloadError(payload));
344
410
  setKeyInput('');
345
- return request('state');
411
+ return query('state');
346
412
  }).then(function (payload) {
347
413
  if (!payload.ok) throw new Error(payloadError(payload));
348
414
  setSnapshot({ status: 'ready', data: payload.value, error: null });
415
+ setDefaults(payload.value.defaultModels || {});
349
416
  setNotice({ kind: 'ok', text: t('saved') });
350
417
  runTest();
351
418
  }).catch(function (error) {
@@ -359,7 +426,7 @@ window.__ModuleLoader__.load({
359
426
  if (busy) return;
360
427
  setBusy(true);
361
428
  setNotice(null);
362
- request('key.clear').then(function (payload) {
429
+ request('key.clear', {}).then(function (payload) {
363
430
  if (!payload.ok) throw new Error(payloadError(payload));
364
431
  setKeyInput('');
365
432
  setSnapshot({ status: 'ready', data: payload.value, error: null });
@@ -471,17 +538,54 @@ window.__ModuleLoader__.load({
471
538
  var capabilitiesCard = React.createElement('section', { className: 'km-card' },
472
539
  React.createElement('h3', null, t('capabilities')),
473
540
  React.createElement('p', { className: 'km-card-sub' }, t('capabilitiesSub')),
541
+ modelNotice !== null ? React.createElement('p', { className: 'km-notice', 'data-kind': modelNotice.kind, style: { margin: '0 0 10px' } }, modelNotice.text) : null,
474
542
  React.createElement('div', { className: 'km-tiles' },
475
543
  TYPE_ORDER.map(function (type) {
476
544
  var entry = catalogByType[type];
545
+ var hasModelList = entry !== undefined && entry !== null && Array.isArray(entry.models);
546
+ var models = hasModelList ? entry.models : [];
547
+ var chosen = defaults[type] === undefined ? '' : defaults[type];
477
548
  var count = entry ? String(entry.available) + '/' + String(entry.count) + ' ' + t('available') : (catalog ? '—' : t('idle'));
549
+ var chosenKnown = false;
550
+ for (var probe = 0; probe < models.length; probe += 1) if (models[probe].id === chosen) chosenKnown = true;
478
551
  return React.createElement('div', {
479
552
  key: type,
480
- className: 'km-tile',
553
+ className: 'km-tile' + (models.length > 0 ? ' km-tile-models' : ''),
481
554
  'data-state': entry && entry.available === 0 ? 'missing' : 'ready'
482
555
  },
483
- React.createElement('strong', null, t('type_' + type)),
484
- React.createElement('span', null, count));
556
+ React.createElement('div', { className: 'km-tile-head' },
557
+ React.createElement('strong', null, t('type_' + type)),
558
+ React.createElement('span', null, count)),
559
+ models.length === 0
560
+ ? (hasModelList ? React.createElement('span', { className: 'km-model-empty' }, t('modelsEmpty')) : null)
561
+ : React.createElement('div', { className: 'km-models' },
562
+ React.createElement('button', {
563
+ type: 'button',
564
+ className: 'km-model',
565
+ 'data-selected': chosen === '' ? 'true' : 'false',
566
+ onClick: function () { selectModel(type, ''); }
567
+ },
568
+ React.createElement('span', { className: 'km-model-radio' }),
569
+ React.createElement('span', { className: 'km-model-name' }, t('modelsAuto'))),
570
+ chosen !== '' && !chosenKnown ? React.createElement('span', { className: 'km-model-stale' }, t('modelsStale') + chosen) : null,
571
+ models.map(function (model) {
572
+ var unavailable = model.available !== true;
573
+ return React.createElement('button', {
574
+ key: model.id,
575
+ type: 'button',
576
+ className: 'km-model',
577
+ 'data-selected': chosen === model.id ? 'true' : 'false',
578
+ 'data-disabled': unavailable ? 'true' : 'false',
579
+ disabled: unavailable,
580
+ title: model.id,
581
+ onClick: function () { selectModel(type, model.id); }
582
+ },
583
+ React.createElement('span', { className: 'km-model-radio' }),
584
+ React.createElement('span', { className: 'km-model-name' }, model.name),
585
+ unavailable ? React.createElement('span', { className: 'km-model-badge' }, t('missingWeights')) : null);
586
+ })
587
+ )
588
+ );
485
589
  })),
486
590
  catalog === null ? React.createElement('p', { className: 'km-hint', style: { margin: '12px 0 0' } }, t('noCatalog')) : null,
487
591
  catalog && catalog.unprofiled > 0 ? React.createElement('p', { className: 'km-hint', style: { margin: '8px 0 0' } }, String(catalog.unprofiled) + ' ' + t('unprofiled')) : null
package/entry.js CHANGED
@@ -17,11 +17,11 @@ import { createTools } from './lib/tools.js'
17
17
 
18
18
  export const name = 'dsh-kylin-media'
19
19
  export const inject = ['tools', 'webServer']
20
- export const VERSION = '0.1.0'
20
+ export const VERSION = '0.1.1'
21
21
 
22
22
  const ROOT = dirname(fileURLToPath(import.meta.url))
23
23
  const SKILLS_DIR = join(ROOT, 'skills')
24
- const CONFIG_FIELDS = ['baseUrl', 'waitSeconds', 'pollIntervalMs', 'requestTimeoutMs', 'autoDownload', 'outputDir']
24
+ const CONFIG_FIELDS = ['baseUrl', 'waitSeconds', 'pollIntervalMs', 'requestTimeoutMs', 'autoDownload', 'outputDir', 'defaultModels']
25
25
 
26
26
  function pickConfig(raw) {
27
27
  if (typeof raw !== 'object' || raw === null || Array.isArray(raw)) return {}
package/lib/gateway.js CHANGED
@@ -69,6 +69,40 @@ function codeOf(payload, status) {
69
69
  return 'http-' + String(status)
70
70
  }
71
71
 
72
+ /** fetch 只会抛 "fetch failed",真正原因躺在 cause 链里(EPROTO / ECONNREFUSED / 证书…)。 */
73
+ function describeCause(error) {
74
+ const parts = []
75
+ let cursor = error
76
+ let guard = 0
77
+ while (cursor !== undefined && cursor !== null && guard < 6) {
78
+ guard += 1
79
+ if (typeof cursor !== 'object') {
80
+ parts.push(String(cursor))
81
+ break
82
+ }
83
+ const message = typeof cursor.message === 'string' ? cursor.message : ''
84
+ const code = typeof cursor.code === 'string' ? cursor.code : ''
85
+ const text = code !== '' && message !== '' ? code + ' ' + message : (message !== '' ? message : code)
86
+ if (text !== '' && parts.indexOf(text) < 0) parts.push(text)
87
+ cursor = cursor.cause
88
+ }
89
+ const detail = parts.join(':')
90
+ return detail.length > 240 ? detail.slice(0, 240) + '…' : detail
91
+ }
92
+
93
+ /** 明文端口回 TLS 客户端时 Node 报 EPROTO / wrong version number;TLS 端口收明文则是 socket hang up。 */
94
+ function looksLikeSchemeMismatch(detail) {
95
+ return /EPROTO|ERR_SSL|wrong version number|unexpected eof|SSL routines|ssl3_|packet length too long|record layer|secure TLS connection|TLS connection/i.test(detail)
96
+ }
97
+
98
+ /** 把 https/http 写反是最常见的「连不上」:TLS 握手失败时直接给出改法。 */
99
+ function schemeHint(base, detail) {
100
+ if (!looksLikeSchemeMismatch(detail)) return ''
101
+ if (base.slice(0, 8) === 'https://') return '。这个端口没有完成 TLS 握手,很可能不是 HTTPS(frp / 反代映射常是明文 HTTP):确认后把地址改成 http:// 再试'
102
+ if (base.slice(0, 7) === 'http://') return '。这个端口看起来要求 HTTPS:把地址改成 https:// 再试'
103
+ return ''
104
+ }
105
+
72
106
  function failForStatus(status, payload, text) {
73
107
  if (status === 401 || status === 403) {
74
108
  return new GatewayError('unauthorized', '访问 Key 缺失或错误(HTTP ' + String(status) + ')。请在「设置 → 多媒体网关」里填写网关的 API Key。', status, isRecord(payload) ? payload['details'] : null)
@@ -118,8 +152,8 @@ export function createGateway(settings) {
118
152
  if (combined.timedOut()) {
119
153
  throw new GatewayError('timeout', '请求网关超时(' + String(timeoutMs) + 'ms):' + base, 0, null)
120
154
  }
121
- const detail = error instanceof Error ? error.message : String(error)
122
- throw new GatewayError('gateway-unreachable', '无法连接网关 ' + base + ':' + detail, 0, null)
155
+ const detail = describeCause(error)
156
+ throw new GatewayError('gateway-unreachable', '无法连接网关 ' + base + ':' + detail + schemeHint(base, detail), 0, null)
123
157
  } finally {
124
158
  combined.close()
125
159
  }
package/lib/routes.js CHANGED
@@ -73,7 +73,15 @@ async function testConnection(store, signal) {
73
73
  },
74
74
  catalog: catalog === null ? null : {
75
75
  types: catalog.types.map(function (entry) {
76
- return { type: entry.type, label: entry.label, count: entry.count, available: entry.available }
76
+ return {
77
+ type: entry.type,
78
+ label: entry.label,
79
+ count: entry.count,
80
+ available: entry.available,
81
+ models: entry.models.map(function (model) {
82
+ return { id: model.id, name: model.name, engine: model.engine, available: model.available }
83
+ })
84
+ }
77
85
  }),
78
86
  unprofiled: catalog.unprofiled
79
87
  },
@@ -105,7 +113,7 @@ export async function handleRequest(store, request, body) {
105
113
  if (!pathname.startsWith(API_PREFIX)) return undefined
106
114
  const action = pathname.slice(API_PREFIX.length).replace(/^\/+/, '')
107
115
  if (method === 'GET' && action === 'state') {
108
- return { status: 200, payload: { ok: true, value: { ...store.publicState(), name: PLUGIN_NAME, version: '0.1.0' } } }
116
+ return { status: 200, payload: { ok: true, value: { ...store.publicState(), name: PLUGIN_NAME, version: '0.1.1' } } }
109
117
  }
110
118
  if (method !== 'POST') {
111
119
  return { status: 405, payload: errorPayload('method-not-allowed', '写操作必须使用 POST') }
package/lib/store.js CHANGED
@@ -21,9 +21,15 @@ export const DEFAULT_SETTINGS = {
21
21
  pollIntervalMs: 1500,
22
22
  requestTimeoutMs: 30000,
23
23
  autoDownload: true,
24
- outputDir: ''
24
+ outputDir: '',
25
+ defaultModels: {}
25
26
  }
26
27
 
28
+ /** 能力类型键:与网关能力目录的 type 同形,避免把任意字段写进设置。 */
29
+ const TYPE_KEY = /^[a-z][a-z0-9_]{0,31}$/
30
+ const MAX_MODEL_ID_LENGTH = 200
31
+ const MAX_DEFAULT_MODELS = 32
32
+
27
33
  function isRecord(value) {
28
34
  return typeof value === 'object' && value !== null && !Array.isArray(value)
29
35
  }
@@ -57,6 +63,24 @@ function readString(value, fallback, maxLength) {
57
63
  return trimmed.slice(0, maxLength)
58
64
  }
59
65
 
66
+ /** 只保留 { 已知类型: 模型 id },空值删除;坏键坏值直接丢弃。 */
67
+ export function normalizeDefaultModels(input) {
68
+ const raw = isRecord(input) ? input : {}
69
+ const map = {}
70
+ let count = 0
71
+ for (const key of Object.keys(raw)) {
72
+ if (count >= MAX_DEFAULT_MODELS) break
73
+ if (!TYPE_KEY.test(key)) continue
74
+ const value = raw[key]
75
+ if (typeof value !== 'string') continue
76
+ const model = value.trim()
77
+ if (model === '' || model.length > MAX_MODEL_ID_LENGTH) continue
78
+ map[key] = model
79
+ count += 1
80
+ }
81
+ return map
82
+ }
83
+
60
84
  function clampInt(value, min, max, fallback) {
61
85
  const parsed = typeof value === 'number' ? value : Number(value)
62
86
  if (!Number.isFinite(parsed)) return fallback
@@ -78,7 +102,8 @@ export function normalizeSettings(input) {
78
102
  pollIntervalMs: clampInt(raw['pollIntervalMs'], 500, 10000, DEFAULT_SETTINGS.pollIntervalMs),
79
103
  requestTimeoutMs: clampInt(raw['requestTimeoutMs'], 5000, 120000, DEFAULT_SETTINGS.requestTimeoutMs),
80
104
  autoDownload: raw['autoDownload'] === false ? false : DEFAULT_SETTINGS.autoDownload,
81
- outputDir: readString(raw['outputDir'], '', 500).replace(/\u0000/g, '')
105
+ outputDir: readString(raw['outputDir'], '', 500).replace(/\u0000/g, ''),
106
+ defaultModels: normalizeDefaultModels(raw['defaultModels'])
82
107
  }
83
108
  }
84
109
 
@@ -110,6 +135,38 @@ export function validatePatch(input) {
110
135
  if (typeof input['autoDownload'] !== 'boolean') errors.push('autoDownload 必须是布尔值')
111
136
  else patch.autoDownload = input['autoDownload']
112
137
  }
138
+ if (input['defaultModels'] !== undefined) {
139
+ if (!isRecord(input['defaultModels'])) {
140
+ errors.push('defaultModels 必须是对象')
141
+ } else {
142
+ const raw = input['defaultModels']
143
+ const keys = Object.keys(raw)
144
+ if (keys.length > MAX_DEFAULT_MODELS) errors.push('defaultModels 最多 ' + String(MAX_DEFAULT_MODELS) + ' 项')
145
+ else {
146
+ const map = {}
147
+ for (const key of keys) {
148
+ if (!TYPE_KEY.test(key)) {
149
+ errors.push('未知能力类型:' + key)
150
+ continue
151
+ }
152
+ const value = raw[key]
153
+ if (value === undefined || value === null || value === '') continue
154
+ if (typeof value !== 'string') {
155
+ errors.push(key + ' 的默认模型必须是字符串')
156
+ continue
157
+ }
158
+ const model = value.trim()
159
+ if (model === '') continue
160
+ if (model.length > MAX_MODEL_ID_LENGTH) {
161
+ errors.push(key + ' 的默认模型 id 过长(上限 ' + String(MAX_MODEL_ID_LENGTH) + ' 字符)')
162
+ continue
163
+ }
164
+ map[key] = model
165
+ }
166
+ patch.defaultModels = map
167
+ }
168
+ }
169
+ }
113
170
  if (input['outputDir'] !== undefined) {
114
171
  if (typeof input['outputDir'] !== 'string') {
115
172
  errors.push('outputDir 必须是字符串')
@@ -165,7 +222,7 @@ export class MediaStore {
165
222
 
166
223
  /** 完整设置(含明文 Key):只允许宿主内部使用。 */
167
224
  settings() {
168
- return { ...this.data }
225
+ return { ...this.data, defaultModels: { ...this.data.defaultModels } }
169
226
  }
170
227
 
171
228
  /** 供客户端 / 路由使用:永远不带明文 Key。 */
@@ -178,7 +235,8 @@ export class MediaStore {
178
235
  pollIntervalMs: this.data.pollIntervalMs,
179
236
  requestTimeoutMs: this.data.requestTimeoutMs,
180
237
  autoDownload: this.data.autoDownload,
181
- outputDir: this.data.outputDir
238
+ outputDir: this.data.outputDir,
239
+ defaultModels: { ...this.data.defaultModels }
182
240
  }
183
241
  }
184
242
 
package/lib/tools.js CHANGED
@@ -23,6 +23,8 @@ import {
23
23
  typeLabel
24
24
  } from './catalog.js'
25
25
 
26
+ const AUDIO_TYPE_BY_ACTION = { transcribe: 'asr', speech: 'tts', separate: 'separate' }
27
+
26
28
  function stringOutput(title) {
27
29
  return {
28
30
  schema: { type: 'string' },
@@ -94,8 +96,10 @@ function missingText(missing) {
94
96
  }).join(', ')
95
97
  }
96
98
 
97
- async function pickModel(gateway, type, requested, signal) {
99
+ async function pickModel(gateway, type, requested, signal, preferred) {
98
100
  if (typeof requested === 'string' && requested.trim() !== '') return requested.trim()
101
+ // 设置里选过的默认模型直接提交(少一次目录往返);模型失效时由网关报错,用户可在设置里改选。
102
+ if (typeof preferred === 'string' && preferred.trim() !== '') return preferred.trim()
99
103
  const payload = await gateway.models(false, signal)
100
104
  const types = payload !== null && typeof payload === 'object' && Array.isArray(payload['types']) ? payload['types'] : []
101
105
  const entry = types.find(function (item) { return item.type === type })
@@ -183,6 +187,11 @@ export function createTools(context) {
183
187
  }
184
188
  const jobs = health['jobs'] !== undefined && health['jobs'] !== null ? health['jobs'] : {}
185
189
  lines.push('任务:' + String(jobs['active'] || 0) + ' 执行中 / 累计 ' + String(jobs['total'] || 0))
190
+ const configuredModels = current.defaultModels
191
+ const chosenTypes = Object.keys(configuredModels)
192
+ lines.push('默认模型:' + (chosenTypes.length === 0
193
+ ? '未设置(各类型取第一个可用模型,可在「设置 → 多媒体网关」里选)'
194
+ : chosenTypes.map(function (type) { return type + '=' + configuredModels[type] }).join(' · ')))
186
195
  if (args['action'] === 'test') {
187
196
  lines.push('')
188
197
  try {
@@ -218,7 +227,9 @@ export function createTools(context) {
218
227
  },
219
228
  timeoutMs: 45000,
220
229
  execute: async function (args, exec) {
221
- const gateway = createGateway(settings())
230
+ const current = settings()
231
+ const gateway = createGateway(current)
232
+ const preferred = current.defaultModels
222
233
  try {
223
234
  if (str(args['model']) !== undefined) {
224
235
  const payload = await gateway.model(str(args['model']), signalOf(exec))
@@ -242,6 +253,8 @@ export function createTools(context) {
242
253
  if (types.length === 0) return '没有类型 ' + String(wanted) + '。可用:' + GENERATE_TYPES.concat(AUDIO_TYPES).join(', ')
243
254
  const lines = []
244
255
  lines.push('ComfyUI:' + (summary.comfy !== null && summary.comfy.reachable === true ? '在线' : '离线或未知'))
256
+ const chosen = Object.keys(preferred)
257
+ lines.push('默认模型:' + (chosen.length === 0 ? '未设置(各类型取第一个可用模型)' : chosen.map(function (type) { return type + '=' + preferred[type] }).join(' · ')))
245
258
  for (const entry of types) {
246
259
  lines.push('')
247
260
  lines.push('## ' + entry.label + '(' + entry.type + ')' + String(entry.available) + '/' + String(entry.count) + ' 可用')
@@ -251,7 +264,8 @@ export function createTools(context) {
251
264
  }
252
265
  for (const model of entry.models) {
253
266
  if (args['onlyAvailable'] === true && model.available !== true) continue
254
- lines.push('- ' + model.id + ' | ' + model.name + ' | ' + (model.engine || '-') + ' | ' + (model.available ? '可用' : '不可用'))
267
+ const mark = preferred[entry.type] === model.id ? ' | 默认' : ''
268
+ lines.push('- ' + model.id + ' | ' + model.name + ' | ' + (model.engine || '-') + ' | ' + (model.available ? '可用' : '不可用') + mark)
255
269
  }
256
270
  }
257
271
  if (summary.unprofiled > 0) lines.push('' + '\n未注册权重:' + String(summary.unprofiled) + ' 个')
@@ -274,7 +288,7 @@ export function createTools(context) {
274
288
  type: 'object',
275
289
  properties: {
276
290
  type: { type: 'string', enum: GENERATE_TYPES, description: '生成类型' },
277
- model: { type: 'string', description: '模型 id;缺省选该类型第一个可用模型' },
291
+ model: { type: 'string', description: '模型 id;缺省用「设置 → 多媒体网关」里该类型的默认模型,未设置则取第一个可用模型' },
278
292
  params: { type: 'object', description: '参数表,字段来自该模型的 paramSchema,如 {prompt, width, height, seed, image, audio}' },
279
293
  wait: { type: 'boolean', description: '是否在本调用内等待完成(默认 true)' },
280
294
  waitSeconds: { type: 'number', description: '本次最多等多少秒(10-600,缺省 60)' },
@@ -297,7 +311,7 @@ export function createTools(context) {
297
311
  const seconds = num(args['waitSeconds'])
298
312
  const waitSeconds = Math.max(10, Math.min(600, seconds === undefined ? current.waitSeconds : seconds))
299
313
  try {
300
- const model = await pickModel(gateway, type, str(args['model']), signalOf(exec))
314
+ const model = await pickModel(gateway, type, str(args['model']), signalOf(exec), current.defaultModels[type])
301
315
  if (model === null) {
302
316
  return typeLabel(type) + ' 没有可用模型。用 media_models type=' + type + ' 查看缺失文件,或先在网关侧补齐权重。'
303
317
  }
@@ -355,6 +369,10 @@ export function createTools(context) {
355
369
  const gateway = createGateway(current)
356
370
  const seconds = num(args['waitSeconds'])
357
371
  const waitSeconds = Math.max(10, Math.min(600, seconds === undefined ? current.waitSeconds : seconds))
372
+ // 语音三类动作各自的默认模型:transcribe→asr / speech→tts / separate→separate
373
+ const model = str(args['model']) === undefined
374
+ ? current.defaultModels[AUDIO_TYPE_BY_ACTION[action]]
375
+ : str(args['model'])
358
376
  const file = str(args['file'])
359
377
  if ((action === 'transcribe' || action === 'separate') && file === undefined) return 'action=' + action + ' 需要 file(本地音频文件路径)。'
360
378
  if (file !== undefined && !existsSync(file)) return '找不到文件:' + file
@@ -362,7 +380,7 @@ export function createTools(context) {
362
380
  let submit
363
381
  if (action === 'transcribe') {
364
382
  const fields = {
365
- model: str(args['model']),
383
+ model,
366
384
  language: str(args['language']),
367
385
  srt: bool(args['srt'], true),
368
386
  vad: bool(args['vad'], false)
@@ -370,7 +388,7 @@ export function createTools(context) {
370
388
  submit = function (client) { return client.transcribe(file, fields, { signal: signalOf(exec) }) }
371
389
  } else if (action === 'separate') {
372
390
  const fields = {
373
- model: str(args['model']),
391
+ model,
374
392
  shifts: num(args['shifts']),
375
393
  overlap: num(args['overlap']),
376
394
  device: str(args['device'])
@@ -382,7 +400,7 @@ export function createTools(context) {
382
400
  const voice = str(args['voice'])
383
401
  const body = {
384
402
  text,
385
- model: str(args['model']),
403
+ model,
386
404
  voice: voice === undefined ? undefined : (/^\d+$/.test(voice) ? Number(voice) : voice),
387
405
  speed: num(args['speed']),
388
406
  format: str(args['format']),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-kylin-media",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "麒麟(QiLin)/ DSH 多媒体网关插件:连接本地 comfyui-gateway,在会话里出图、出视频、出音乐、对口型,并做语音转写、合成与分离。",
5
5
  "keywords": [
6
6
  "deepseek-harness",
@@ -46,7 +46,8 @@
46
46
  "scripts": {
47
47
  "smoke": "node scripts/smoke.mjs",
48
48
  "selftest": "node scripts/selftest.mjs",
49
- "test": "node scripts/smoke.mjs && node scripts/selftest.mjs",
49
+ "client-test": "node scripts/client-test.mjs",
50
+ "test": "node scripts/smoke.mjs && node scripts/selftest.mjs && node scripts/client-test.mjs",
50
51
  "prepack": "node scripts/smoke.mjs"
51
52
  },
52
53
  "dsh": {
@@ -0,0 +1,243 @@
1
+ /**
2
+ * 客户端渲染自测:不装浏览器,用最小 React 替身渲染设置页的「可用能力」卡片。
3
+ *
4
+ * 覆盖:类型卡片列出具体模型、默认模型高亮、点选后 POST settings.update、
5
+ * 缺权重模型不可选、界面永不出现明文 Key。用法:node scripts/client-test.mjs
6
+ */
7
+ let failures = 0
8
+ function check(condition, message) {
9
+ if (condition) return
10
+ failures += 1
11
+ console.error('FAIL ' + message)
12
+ }
13
+
14
+ /* ---------- 最小 React 替身(只实现 createElement / useState / useEffect) ---------- */
15
+ const hookState = []
16
+ let hookCursor = 0
17
+ let effectsRan = false
18
+ let dirty = false
19
+
20
+ function createElement(type, props) {
21
+ const children = []
22
+ for (let index = 2; index < arguments.length; index += 1) {
23
+ const child = arguments[index]
24
+ if (Array.isArray(child)) children.push(...child)
25
+ else children.push(child)
26
+ }
27
+ return {
28
+ type,
29
+ props: props === null || props === undefined ? {} : props,
30
+ children: children.filter(function (child) { return child !== null && child !== undefined && child !== false })
31
+ }
32
+ }
33
+
34
+ function useState(initial) {
35
+ const index = hookCursor
36
+ hookCursor += 1
37
+ if (!(index in hookState)) hookState[index] = typeof initial === 'function' ? initial() : initial
38
+ function set(next) {
39
+ const value = typeof next === 'function' ? next(hookState[index]) : next
40
+ if (Object.is(value, hookState[index])) return
41
+ hookState[index] = value
42
+ dirty = true
43
+ }
44
+ return [hookState[index], set]
45
+ }
46
+
47
+ const pendingEffects = []
48
+ function useEffect(fn) {
49
+ hookCursor += 1
50
+ if (!effectsRan) pendingEffects.push(fn)
51
+ }
52
+
53
+ const ReactStub = { createElement, useState, useEffect, Fragment: Symbol('fragment') }
54
+
55
+ /* ---------- 假的 fenced JSON API ---------- */
56
+ const calls = []
57
+ const STATE = {
58
+ baseUrl: 'http://127.0.0.1:8288',
59
+ hasKey: true,
60
+ keyMask: 'Awx••••hto',
61
+ waitSeconds: 60,
62
+ pollIntervalMs: 1500,
63
+ requestTimeoutMs: 30000,
64
+ autoDownload: true,
65
+ outputDir: '',
66
+ defaultModels: { text2image: 'qwen-image-2.1-lite' },
67
+ name: 'dsh-kylin-media',
68
+ version: '0.1.1'
69
+ }
70
+ const CATALOG = {
71
+ types: [
72
+ {
73
+ type: 'text2image',
74
+ label: '文生图',
75
+ count: 3,
76
+ available: 3,
77
+ models: [
78
+ { id: 'qwen-image-2.1', name: 'Qwen-Image 2.1', engine: 'comfy', available: true },
79
+ { id: 'qwen-image-2.1-lite', name: 'Qwen-Image 2.1 Lite', engine: 'comfy', available: true },
80
+ { id: 'zimage-turbo', name: 'Z-Image Turbo', engine: 'comfy', available: true }
81
+ ]
82
+ },
83
+ {
84
+ type: 'upscale',
85
+ label: '图片超分',
86
+ count: 2,
87
+ available: 1,
88
+ models: [
89
+ { id: 'upscale-ultrasharp', name: 'UltraSharp', engine: 'comfy', available: true },
90
+ { id: 'upscale-missing', name: 'Missing Weights', engine: 'comfy', available: false }
91
+ ]
92
+ }
93
+ ],
94
+ unprofiled: 0
95
+ }
96
+ const TEST_PAYLOAD = {
97
+ reachable: true,
98
+ latencyMs: 12,
99
+ gateway: { version: '0.3.0', uptimeSec: 10, authMode: 'always', jobs: { active: 0, total: 9 } },
100
+ comfy: { reachable: true, upstreams: [] },
101
+ catalog: CATALOG,
102
+ modelsError: null
103
+ }
104
+
105
+ function respond(action, options) {
106
+ calls.push({ action, method: options === undefined || options.method === undefined ? 'GET' : options.method, body: options === undefined || options.body === undefined ? null : JSON.parse(options.body) })
107
+ let payload
108
+ if (action === 'state') payload = { ok: true, value: STATE }
109
+ else if (action === 'test') payload = { ok: true, value: TEST_PAYLOAD }
110
+ else if (action === 'settings.update') payload = { ok: true, value: STATE }
111
+ else if (action === 'key.clear') payload = { ok: true, value: STATE }
112
+ else payload = { ok: false, error: { code: 'not-found', message: 'unexpected ' + action } }
113
+ return Promise.resolve({ status: 200, json: function () { return Promise.resolve(payload) } })
114
+ }
115
+
116
+ globalThis.fetch = function (url, options) {
117
+ const action = String(url).slice(String(url).lastIndexOf('/') + 1)
118
+ return respond(action, options)
119
+ }
120
+
121
+ /* ---------- 加载客户端 bundle(自注册 + 工厂) ---------- */
122
+ let clientExports = null
123
+ globalThis.window = {
124
+ __ModuleLoader__: {
125
+ load: function (spec) {
126
+ clientExports = spec.factory(function (name) {
127
+ if (name === 'react') return ReactStub
128
+ throw new Error('unexpected require: ' + name)
129
+ })
130
+ }
131
+ }
132
+ }
133
+
134
+ await import(new URL('../client.js', import.meta.url))
135
+
136
+ /* ---------- 用 mock ctx 拿到设置页组件 ---------- */
137
+ let Section = null
138
+ const ctx = {
139
+ locale: {
140
+ register: function () { return function () {} },
141
+ bind: function () { return function (key) { return key } }
142
+ },
143
+ effect: function (fn) { fn(); return function () {} },
144
+ slots: {
145
+ inject: function (_name, register) { register() },
146
+ register: function (_entry, component) { Section = component; return function () {} }
147
+ }
148
+ }
149
+ clientExports.apply(ctx)
150
+ check(typeof Section === 'function', 'settings section component is registered')
151
+
152
+ /* ---------- 渲染循环 ---------- */
153
+ function render() {
154
+ hookCursor = 0
155
+ dirty = false
156
+ return Section()
157
+ }
158
+ async function settle() {
159
+ for (let round = 0; round < 40; round += 1) {
160
+ render()
161
+ if (!effectsRan) {
162
+ effectsRan = true
163
+ for (const effect of pendingEffects) effect()
164
+ }
165
+ await new Promise(function (resolve) { setTimeout(resolve, 0) })
166
+ if (!dirty) {
167
+ render()
168
+ return
169
+ }
170
+ }
171
+ throw new Error('render loop did not settle')
172
+ }
173
+
174
+ function textOf(node) {
175
+ if (node === null || node === undefined || node === false) return ''
176
+ if (typeof node === 'string' || typeof node === 'number') return String(node)
177
+ if (Array.isArray(node)) return node.map(textOf).join('')
178
+ let text = textOf(node.children)
179
+ return text
180
+ }
181
+
182
+ function walk(node, visit) {
183
+ if (node === null || node === undefined || typeof node !== 'object' || Array.isArray(node)) return
184
+ visit(node)
185
+ for (const child of node.children ?? []) walk(child, visit)
186
+ }
187
+
188
+ function findAll(tree, predicate) {
189
+ const found = []
190
+ walk(tree, function (node) { if (predicate(node)) found.push(node) })
191
+ return found
192
+ }
193
+
194
+ function tileByLabel(tree, typeKey) {
195
+ return findAll(tree, function (node) {
196
+ return node.props !== undefined && typeof node.props.className === 'string' && /(^|\s)km-tile(\s|$)/.test(node.props.className)
197
+ }).find(function (tile) { return textOf(tile).includes(typeKey) })
198
+ }
199
+
200
+ const tree = await (async function () { await settle(); return render() })()
201
+ const tiles = findAll(tree, function (node) {
202
+ return typeof node.props.className === 'string' && /(^|\s)km-tile(\s|$)/.test(node.props.className)
203
+ })
204
+ check(tiles.length === 10, 'ten capability tiles are rendered')
205
+ check(textOf(tree).includes('Awx••••hto') || !textOf(tree).includes('AwxCVbDBhUIbRt6KW_oxCkpfZMvH3hto'), 'rendered tree never shows the plaintext key')
206
+
207
+ const imageTile = tileByLabel(tree, 'type_text2image')
208
+ check(imageTile !== undefined, 'text2image tile is rendered')
209
+ const imageButtons = findAll(imageTile, function (node) { return node.type === 'button' })
210
+ check(imageButtons.length === 4, 'text2image tile lists auto + three models (got ' + String(imageButtons.length) + ')')
211
+ check(findAll(imageTile, function (node) { return node.props['data-selected'] === 'true' }).length === 1, 'exactly one row is selected')
212
+ check(findAll(imageTile, function (node) { return node.props['data-selected'] === 'true' })[0].props.className.indexOf('km-model') >= 0, 'selected row is a model row')
213
+ check(textOf(findAll(imageTile, function (node) { return node.props['data-selected'] === 'true' })[0]).includes('Qwen-Image 2.1 Lite'), 'the stored default model is preselected')
214
+
215
+ const upscaleTile = tileByLabel(tree, 'type_upscale')
216
+ const disabled = findAll(upscaleTile, function (node) { return node.props['data-disabled'] === 'true' })
217
+ check(disabled.length === 1 && disabled[0].props.disabled === true, 'an unavailable model is rendered disabled')
218
+ check(textOf(disabled[0]).includes('missingWeights'), 'an unavailable model carries the missing-weights badge')
219
+
220
+ const target = findAll(imageTile, function (node) { return textOf(node).includes('Z-Image Turbo') && node.type === 'button' })[0]
221
+ check(target !== undefined, 'model row for zimage-turbo exists')
222
+ target.props.onClick()
223
+ await settle()
224
+ const update = calls.filter(function (call) { return call.action === 'settings.update' }).pop()
225
+ check(update !== undefined && update.method === 'POST', 'choosing a model posts settings.update')
226
+ check(update !== undefined && update.body.patch.defaultModels.text2image === 'zimage-turbo', 'the chosen model is sent as the type default')
227
+ check(JSON.stringify(update === undefined ? {} : update.body).includes(STATE.keyMask) === false, 'settings.update never carries the key')
228
+ const selectedAfter = tileByLabel(render(), 'type_text2image')
229
+ check(findAll(selectedAfter, function (node) { return node.props['data-selected'] === 'true' }).length === 1, 'selection stays single after choosing')
230
+
231
+ const autoRow = findAll(tileByLabel(render(), 'type_text2image'), function (node) { return textOf(node).includes('modelsAuto') && node.type === 'button' })[0]
232
+ check(autoRow !== undefined, 'auto row exists')
233
+ autoRow.props.onClick()
234
+ await settle()
235
+ const clearedUpdate = calls.filter(function (call) { return call.action === 'settings.update' }).pop()
236
+ check(clearedUpdate !== undefined && clearedUpdate.body.patch.defaultModels !== undefined, 'auto row posts a defaultModels patch')
237
+ check(clearedUpdate !== undefined && clearedUpdate.body.patch.defaultModels.text2image === undefined, 'auto row clears the type default')
238
+
239
+ if (failures > 0) {
240
+ console.error('client-test: ' + String(failures) + ' failure(s)')
241
+ process.exit(1)
242
+ }
243
+ console.log('client-test: OK')
@@ -33,6 +33,8 @@ function assertIncludes(text, part, message) {
33
33
  const KEY = 'test-secret-key-123'
34
34
  const OUTPUT = join(TMP, 'output')
35
35
  const requests = []
36
+ let lastGenerateModel = null
37
+ let lastSpeechModel = null
36
38
 
37
39
  function json(res, status, payload) {
38
40
  res.writeHead(status, { 'content-type': 'application/json; charset=utf-8' })
@@ -72,8 +74,14 @@ const server = createServer(async function (req, res) {
72
74
  ok: true,
73
75
  comfy: { reachable: true, source: 'mock' },
74
76
  types: [
75
- { type: 'text2image', label: '文生图', models: [{ id: 'mock-image', type: 'text2image', name: 'Mock Image', engine: 'comfy', available: true, params: { prompt: 'x' }, paramSchema: { prompt: { type: 'string', required: true } } }] },
76
- { type: 'tts', label: '语音合成', models: [{ id: 'mock-tts', type: 'tts', name: 'Mock TTS', engine: 'tts', available: true }] }
77
+ { type: 'text2image', label: '文生图', models: [
78
+ { id: 'mock-image', type: 'text2image', name: 'Mock Image', engine: 'comfy', available: true, params: { prompt: 'x' }, paramSchema: { prompt: { type: 'string', required: true } } },
79
+ { id: 'mock-image-b', type: 'text2image', name: 'Mock Image B', engine: 'comfy', available: true, params: { prompt: 'x' }, paramSchema: { prompt: { type: 'string', required: true } } }
80
+ ] },
81
+ { type: 'tts', label: '语音合成', models: [
82
+ { id: 'mock-tts', type: 'tts', name: 'Mock TTS', engine: 'tts', available: true },
83
+ { id: 'mock-tts-alt', type: 'tts', name: 'Mock TTS Alt', engine: 'tts', available: true }
84
+ ] }
77
85
  ],
78
86
  unprofiled: []
79
87
  })
@@ -82,11 +90,13 @@ const server = createServer(async function (req, res) {
82
90
  return json(res, 200, { ok: true, model: { id: 'mock-image', name: 'Mock Image', type: 'text2image', engine: 'comfy', output: 'image', available: true, missing: [], params: { prompt: 'x' }, paramSchema: { prompt: { type: 'string', required: true }, width: { type: 'number' } } } })
83
91
  }
84
92
  if (url.pathname === '/api/generate/text2image' && req.method === 'POST') {
85
- await readReq(req)
93
+ const raw = await readReq(req)
94
+ try { lastGenerateModel = JSON.parse(raw.toString()).model } catch { lastGenerateModel = null }
86
95
  return json(res, 202, { ok: true, job: { id: 'job-1', status: 'queued', type: 'text2image', model_id: 'mock-image', engine: 'comfy', outputs: [] } })
87
96
  }
88
97
  if ((url.pathname === '/api/audio/speech' || url.pathname === '/api/audio/transcribe' || url.pathname === '/api/audio/separate') && req.method === 'POST') {
89
- await readReq(req)
98
+ const raw = await readReq(req)
99
+ try { lastSpeechModel = JSON.parse(raw.toString()).model } catch { lastSpeechModel = null }
90
100
  return json(res, 202, { ok: true, job: { id: 'job-2', status: 'queued', type: url.pathname.endsWith('speech') ? 'tts' : 'asr', model_id: 'mock-tts' } })
91
101
  }
92
102
  if (url.pathname === '/api/upload' && req.method === 'POST') {
@@ -179,9 +189,12 @@ const unknown = await callRoute({ method: 'POST', url: '/dsh-kylin-media/api/nop
179
189
  check(unknown.status === 404, 'unknown action is 404')
180
190
  const methodNotAllowed = await callRoute({ url: '/dsh-kylin-media/api/settings.update' })
181
191
  check(methodNotAllowed.status === 405, 'GET on a write action is 405')
192
+ const testViaGet = await callRoute({ url: '/dsh-kylin-media/api/test' })
193
+ check(testViaGet.status === 405, 'GET on the test action is 405 (the client must POST it)')
182
194
  const testResponse = await callRoute({ method: 'POST', url: '/dsh-kylin-media/api/test', body: {} })
183
195
  check(testResponse.status === 200 && testResponse.body.value.reachable === true, 'test action reaches the mock gateway')
184
196
  check(testResponse.body.value.catalog.types.length >= 1, 'test action returns a catalogue')
197
+ check(testResponse.body.value.catalog.types[0].models[0].id === 'mock-image', 'test payload lists concrete models')
185
198
 
186
199
  const tools = createTools({ store: restarted, workingDirectory: { get: function () { return OUTPUT } } })
187
200
  const byName = {}
@@ -217,6 +230,36 @@ const speechText = await byName.media_audio.execute({ action: 'speech', text: '
217
230
  assertIncludes(speechText, 'job-2', 'media_audio speech submits a job')
218
231
  check(existsSync(join(OUTPUT, 'job-2', 'speech.mp3')), 'media_audio downloads the speech artifact')
219
232
 
233
+ // 设置里选定的默认模型:优先于「第一个可用模型」,跨重启保存,清空即回到自动
234
+ const defaultResult = restarted.update({ defaultModels: { text2image: 'mock-image-b', tts: 'mock-tts-alt' } })
235
+ check(defaultResult.ok, 'store.update accepts defaultModels')
236
+ check(restarted.publicState().defaultModels.text2image === 'mock-image-b', 'publicState exposes defaultModels')
237
+ const reloadedDefaults = new MediaStore(HOME)
238
+ check(reloadedDefaults.publicState().defaultModels.tts === 'mock-tts-alt', 'defaultModels survive a restart')
239
+ check(JSON.stringify(reloadedDefaults.publicState()).includes(KEY) === false, 'defaultModels state still hides the key')
240
+
241
+ const defaultGenerate = await byName.media_generate.execute({ type: 'text2image', params: { prompt: 'default model' }, wait: true, waitSeconds: 10, outputDir: OUTPUT }, exec)
242
+ assertIncludes(defaultGenerate, 'job-1', 'media_generate still submits with a configured default model')
243
+ check(lastGenerateModel === 'mock-image-b', 'media_generate uses the configured default model')
244
+
245
+ const defaultSpeech = await byName.media_audio.execute({ action: 'speech', text: '默认模型', wait: true, waitSeconds: 10, outputDir: OUTPUT }, exec)
246
+ assertIncludes(defaultSpeech, 'job-2', 'media_audio speech still submits with a default model')
247
+ check(lastSpeechModel === 'mock-tts-alt', 'media_audio speech uses the configured default model')
248
+
249
+ const clearDefault = restarted.update({ defaultModels: { text2image: '', tts: '' } })
250
+ check(clearDefault.ok && Object.keys(restarted.publicState().defaultModels).length === 0, 'empty model id clears the default')
251
+ const fallbackGenerate = await byName.media_generate.execute({ type: 'text2image', params: { prompt: 'fallback' }, wait: true, waitSeconds: 10, outputDir: OUTPUT }, exec)
252
+ assertIncludes(fallbackGenerate, 'job-1', 'media_generate falls back without a default')
253
+ check(lastGenerateModel === 'mock-image', 'media_generate falls back to the first available model')
254
+
255
+ const badDefaultType = restarted.update({ defaultModels: { 'Bad-Type': 'x' } })
256
+ check(badDefaultType.ok === false, 'invalid default model type is rejected')
257
+ const badDefaultValue = restarted.update({ defaultModels: { text2image: 42 } })
258
+ check(badDefaultValue.ok === false, 'non-string default model is rejected')
259
+
260
+ const modelsWithDefaults = await byName.media_models.execute({ type: 'text2image' }, exec)
261
+ check(modelsWithDefaults.includes('默认模型'), 'media_models reports the configured defaults')
262
+
220
263
  const uploadText = await byName.media_upload.execute({ file: join(process.cwd(), 'package.json') }, exec)
221
264
  assertIncludes(uploadText, 'ref.png', 'media_upload returns the uploaded name')
222
265
 
package/scripts/smoke.mjs CHANGED
@@ -57,6 +57,10 @@ check(!client.trimStart().startsWith('export '), 'client has no bare ESM exports
57
57
  check(client.includes("ctx.slots.inject('settings.section'"), 'client registers settings.section only')
58
58
  check(!client.includes('plugins.bundle.config'), 'client does not add a second config seat')
59
59
  check(!client.includes('dsh-kylin-images'), 'client has no legacy plugin name')
60
+ check(!/request\('[^']+'\)/.test(client), 'client always POSTs write actions (no body-less request())')
61
+ check(client.includes("query('state')"), 'client reads state through the GET query helper')
62
+ check(client.includes('defaultModels'), 'client persists per-type default models')
63
+ check(client.includes('km-model-radio'), 'client renders selectable model rows')
60
64
 
61
65
  const entryModule = await import(new URL('../entry.js', import.meta.url))
62
66
  check(entryModule.name === PACKAGE, 'entry name matches package')
@@ -27,7 +27,7 @@ description: 调用本地 comfyui-gateway 的多媒体能力(文生图/改图/
27
27
  ## 工作流
28
28
 
29
29
  1. 需要参考图/音频/视频时,先 media_upload 上传本地文件,拿到 name(可直接作为 params.image / params.audio / params.video)。
30
- 2. media_generate 提交:model 可省略,插件会选该类型第一个可用模型;params 严格按 media_models 返回的 paramSchema。
30
+ 2. media_generate 提交:model 可省略——先用用户在「设置 → 多媒体网关 → 可用能力」里点选的该类型默认模型,未选则取该类型第一个可用模型;params 严格按 media_models 返回的 paramSchema。
31
31
  3. 默认最多等 60 秒;返回 pending 时用 media_job action=wait(id + waitSeconds)继续等,直到 completed。
32
32
  4. 完成后工具自动下载并返回绝对路径;图片用 read_image 查看,视频/音频可用 present 交付。
33
33