dsh-connect-workbuddy-gateway 1.3.0 → 1.3.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
@@ -434,7 +434,7 @@ hy4flash-free@#9 ← 昵称含中文/emoji,退化成序号
434
434
  node tests/harness.mjs
435
435
  ```
436
436
 
437
- 覆盖 210 项:首次建 key 与惰性补选、鉴权、模型勾选与清单投影、重名后缀(区域 / 昵称 /
437
+ 覆盖 223 项:首次建 key 与惰性补选、鉴权、模型勾选与清单投影、重名后缀(区域 / 昵称 /
438
438
  非 ASCII 序号)、跨区同名、按名字路由、推理档透传、**固定监听端口与占用提示**、**局域网分享与它的边界**、
439
439
  并行工具调用按 index 分组、
440
440
  Responses 流的规范校验(`sequence_number`、part 一致性、terminal 的 output 顺序)、
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-connect-workbuddy-gateway",
3
- "version": "1.3.0",
3
+ "version": "1.3.1",
4
4
  "description": "把 dsh-connect-workbuddy 的 WorkBuddy 模型开放成本机 API:OpenAI Chat / Responses、Anthropic Messages、Gemini 四协议共用一个 base URL,模型按 API key 逐个勾选。",
5
5
  "keywords": [
6
6
  "dsh-plugin",
package/src/models.js CHANGED
@@ -65,6 +65,15 @@ export async function directModelsByRegion(env) {
65
65
  export async function catalogForSettings(env, accounts, usableAccounts) {
66
66
  const entries = await collectModels(env)
67
67
  const pool = entries.length > 0 ? entries : await directModelsByRegion(env)
68
+
69
+ /*
70
+ * 把这份目录记到 env 上,供 {@link keyCatalog} 判断「落盘的勾选是否还有效」。
71
+ *
72
+ * 只在目录**非空**时记:一次读失败(上游暂时不可达)不该被当成「上游没有任何
73
+ * 模型」,否则下一次请求会把用户的清单整个清空。
74
+ */
75
+ if (pool.length > 0) env.liveModels = liveModelKeys(pool)
76
+
68
77
  const usableIds = usableAccounts === undefined
69
78
  ? undefined
70
79
  : new Set(usableAccounts.map((account) => account.id))
@@ -95,7 +104,52 @@ export async function catalogForSettings(env, accounts, usableAccounts) {
95
104
  return out
96
105
  }
97
106
 
98
- /** 推理档列表:adapter 报了就用它的,否则给一组保守的常用档位。 */
107
+ /**
108
+ * 上游此刻真正提供的模型键集合:`region \0 modelId`。
109
+ *
110
+ * 用来判断一条落盘的勾选是否**仍然指向一个存在的模型**。
111
+ *
112
+ * @param pool {@link collectModels} 或 {@link directModelsByRegion} 的结果
113
+ */
114
+ export function liveModelKeys(pool) {
115
+ const keys = new Set()
116
+ for (const entry of Array.isArray(pool) ? pool : []) {
117
+ keys.add(`${entry.region ?? ''}\u0000${entry.id}`)
118
+ }
119
+ return keys
120
+ }
121
+
122
+ /**
123
+ * 把勾选清单收窄到「上游此刻仍然提供」的模型。
124
+ *
125
+ * ## 为什么必须收窄
126
+ *
127
+ * 勾选是**落盘**的,而上游的模型目录是**活的**:用户可能在上游卡片里取消勾选某个
128
+ * 模型,或者在那边换了套餐、模型下架、区域关掉。那些勾选会一直留在 keys.json 里,
129
+ * 于是网关继续对外宣称提供这些模型 —— 客户端能选到,一调就失败。
130
+ *
131
+ * 用户报的就是这个现象:「勾选项没有和上游插件的选中模型同步」。
132
+ *
133
+ * ## 为什么按 (region, modelId) 而不是按名字
134
+ *
135
+ * 客户端看到的名字带账号后缀(`x@pony`),那是由勾选**推导**出来的;判存活要看
136
+ * 上游真实 id 与区域,两者不能混。
137
+ *
138
+ * @param selections key 的勾选
139
+ * @param live {@link liveModelKeys} 的结果
140
+ * @returns `{ kept, dropped }`,`dropped` 供设置页提示用户
141
+ */
142
+ export function pruneSelections(selections, live) {
143
+ const kept = []
144
+ const dropped = []
145
+ for (const selection of Array.isArray(selections) ? selections : []) {
146
+ const region = selection.region ?? ''
147
+ const key = `${region}\u0000${selection.modelId}`
148
+ if (live.has(key)) kept.push(selection)
149
+ else dropped.push(selection)
150
+ }
151
+ return { kept, dropped }
152
+ }
99
153
  export function effortsOf(reasoning) {
100
154
  const declared = Array.isArray(reasoning?.efforts)
101
155
  ? reasoning.efforts.map((effort) => (typeof effort === 'string' ? effort : effort?.id)).filter((id) => typeof id === 'string' && id !== '')
package/src/resolve.js CHANGED
@@ -16,10 +16,25 @@
16
16
  import { buildCatalog, lookupModel } from './catalog.js'
17
17
 
18
18
  /** provider 路由 → 区域。 */
19
+ /**
20
+ * provider 路由 → 区域。
21
+ *
22
+ * **只认 dsh-connect-workbuddy 自己注册的那两个路由**,不做前缀匹配。
23
+ *
24
+ * 早期这里是 `provider.startsWith('workbuddy')`,结果把 `workbuddy-remote`
25
+ * (另一个插件的路由,模型 id 形如 `cn:deepseek-v4.1-flash`)也当成了本插件的
26
+ * cn 通道 —— 模型清单里因此混进六条带 `cn:` 前缀的假条目,和真正的
27
+ * `deepseek-v4.1-flash` 并列,用户在勾选界面无法分辨。
28
+ *
29
+ * 判据应该是「这个 provider 是不是上游插件注册的」,而不是「名字像不像」。
30
+ *
31
+ * @param provider provider id
32
+ * @returns `cn` | `global` | undefined
33
+ */
19
34
  export function regionOfProvider(provider) {
20
35
  if (typeof provider !== 'string') return undefined
21
- if (provider === 'workbuddy-global' || provider.startsWith('workbuddy-global')) return 'global'
22
- if (provider === 'workbuddy' || provider.startsWith('workbuddy')) return 'cn'
36
+ if (provider === 'workbuddy-global') return 'global'
37
+ if (provider === 'workbuddy') return 'cn'
23
38
  return undefined
24
39
  }
25
40
 
package/src/routes.js CHANGED
@@ -19,7 +19,7 @@ import { handleChat } from './openai-chat.js'
19
19
  import { handleResponses } from './openai-responses.js'
20
20
  import { handleMessages } from './anthropic.js'
21
21
  import { handleGenerate, parseGeminiPath, geminiCatalog } from './gemini.js'
22
- import { collectModels, directModelsByRegion, openAiCatalog, anthropicCatalog, catalogForSettings } from './models.js'
22
+ import { collectModels, directModelsByRegion, openAiCatalog, anthropicCatalog, catalogForSettings, liveModelKeys, pruneSelections } from './models.js'
23
23
  import {
24
24
  readConfig,
25
25
  writeConfig,
@@ -155,13 +155,16 @@ async function dispatchApi(req, res, ctx, env) {
155
155
  }
156
156
  if (method === 'GET' && route === '/models') {
157
157
  /*
158
- * 只返回这把 key 勾选的模型 —— 用户的勾选就是唯一的事实来源。
159
- * 重名项的名字已经带上账号后缀(见 src/catalog.js)。
160
- *
161
158
  * 先补一次选:客户端常常比设置页更早被打开,那时默认 key 还没勾过任何
162
159
  * 模型,列表会是空的 —— 对用户来说就是「网关没起来」。
163
160
  */
164
161
  await env.ensureSeeded?.()
162
+ /*
163
+ * **再刷一次上游目录。** 客户端不一定打开过设置页,而清单必须反映上游此刻
164
+ * 提供的模型 —— 否则上游下架/取消勾选的模型会一直留在这把 key 的清单里。
165
+ * 刷新失败时 `liveModels` 保持上一次的值,不会把清单清空。
166
+ */
167
+ await refreshLiveModels(env)
165
168
  const fresh = authenticate(req.headers, url) ?? keyEntry
166
169
  const catalog = keyCatalog(fresh, env)
167
170
  sendJson(res, 200, wantsAnthropicShape ? anthropicCatalog(catalog) : openAiCatalog(catalog))
@@ -230,6 +233,11 @@ async function dispatchAdmin(req, res, ctx, env) {
230
233
  if (method === 'GET' && path === '/state') {
231
234
  /* 设置页打开是最可靠的「provider 肯定就绪」时机:顺手补选。 */
232
235
  await env.ensureSeeded?.()
236
+ /*
237
+ * 刷新目录并把失效的勾选清掉 —— 设置页读的是落盘配置,只忽略不清理的话,
238
+ * 画面上仍会列出上游已经没有的模型。
239
+ */
240
+ if (await refreshLiveModels(env)) pruneStoredSelections(env)
233
241
  sendJson(res, 200, await adminState(ctx, env))
234
242
  return
235
243
  }
@@ -656,8 +664,64 @@ function isLoopbackAddress(value) {
656
664
  /* ── 杂项 ─────────────────────────────────────────────────────────────── */
657
665
 
658
666
  /** 某个 key 的模型清单(勾选 → 客户端看到的模型)。 */
667
+ /**
668
+ * 刷新 `env.liveModels`(上游此刻提供的模型键集合)。
669
+ *
670
+ * 失败时**不动**上一次的值:一次上游不可达不等于「上游没有模型」,把它当成后者
671
+ * 会让所有 key 的清单瞬间变空,比显示一份稍旧的清单糟得多。
672
+ *
673
+ * @param env 插件环境
674
+ * @returns 刷新成功时返回 `true`
675
+ */
676
+ async function refreshLiveModels(env) {
677
+ try {
678
+ const entries = await collectModels(env)
679
+ const pool = entries.length > 0 ? entries : await directModelsByRegion(env)
680
+ if (pool.length === 0) return false
681
+ env.liveModels = liveModelKeys(pool)
682
+ return true
683
+ } catch {
684
+ /* 保持上一次的值 */
685
+ return false
686
+ }
687
+ }
688
+
689
+ /**
690
+ * 把各 key 上「上游已经没有」的勾选从配置里清掉。
691
+ *
692
+ * 光在投影时忽略不够:设置页读的是落盘配置,脏条目会一直显示在勾选界面里,
693
+ * 用户以为它还生效。这里在确认目录已刷新之后才动手,并且只写回真正变了的情况
694
+ * —— 否则每次读 state 都会重写一次文件。
695
+ *
696
+ * @param env 插件环境
697
+ */
698
+ function pruneStoredSelections(env) {
699
+ const live = env.liveModels
700
+ if (live === undefined) return
701
+ const config = readConfig()
702
+ let changed = false
703
+ for (const entry of config.keys) {
704
+ const { kept, dropped } = pruneSelections(entry.selections ?? [], live)
705
+ if (dropped.length === 0) continue
706
+ entry.selections = kept
707
+ changed = true
708
+ }
709
+ if (changed) writeConfig(config)
710
+ }
711
+
712
+ /**
713
+ * 某把 key 对外提供的模型清单。
714
+ *
715
+ * `env.liveModels` 是上一次读到的上游目录(`region\0modelId` 集合)。有它时先把
716
+ * 勾选收窄到「上游仍然提供」的那些——上游取消勾选/下架之后,本插件不该继续对外
717
+ * 宣称提供那个模型。没有它(还没读过目录)时按原样投影,不能因为一次读失败就把
718
+ * 用户的清单清空。
719
+ */
659
720
  function keyCatalog(keyEntry, env) {
660
- return buildCatalog(keyEntry.selections ?? [], publicAccounts(env))
721
+ const selections = keyEntry.selections ?? []
722
+ const live = env.liveModels
723
+ const usable = live === undefined ? selections : pruneSelections(selections, live).kept
724
+ return buildCatalog(usable, publicAccounts(env))
661
725
  }
662
726
 
663
727
  /** 本机账号表(不含 token),供清单解析与设置页共用。 */