dsh-connect-workbuddy-gateway 1.4.3 → 1.5.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/README.md CHANGED
@@ -558,7 +558,7 @@ hy4flash-free@#9 ← 昵称含中文/emoji,退化成序号
558
558
  node tests/harness.mjs
559
559
  ```
560
560
 
561
- 覆盖 292 项:首次建 key 与惰性补选、鉴权、模型勾选与清单投影、重名后缀(区域 / 昵称 /
561
+ 覆盖 327 项:首次建 key 与惰性补选、鉴权、模型勾选与清单投影、重名后缀(区域 / 昵称 /
562
562
  非 ASCII 序号)、跨区同名、按名字路由、推理档透传、**固定监听端口与占用提示**、**局域网分享与接入**、**两种角色模式**、
563
563
  并行工具调用按 index 分组、
564
564
  Responses 流的规范校验(`sequence_number`、part 一致性、terminal 的 output 顺序)、
package/client.js CHANGED
@@ -124,6 +124,19 @@ window.__ModuleLoader__.load({
124
124
  pairingShareName: '本机名称',
125
125
  pairingShareNameHint: '对方在列表里会看到这个名字,留空则只显示 IP。',
126
126
  pairingSaveName: '保存名称',
127
+ /* 已分享的凭据 */
128
+ shares: '已分享出去的凭据',
129
+ sharesEmpty: '还没有分享给任何设备',
130
+ shareDevice: '设备',
131
+ shareAddress: '地址',
132
+ shareWhen: '分享于',
133
+ shareRevoke: '撤销分享',
134
+ shareRevoking: '撤销中…',
135
+ shareKeyNote: '撤销会**同时删除**那把专用 key(它唯一的用途就是给这台设备)。',
136
+ shareAndKey: '同时删除 key',
137
+ unknownDevice: '未知设备',
138
+ pairingRetry: '这个申请已失效,请重新申请',
139
+ peersRetry: '重新申请',
127
140
  /* 运行模式 */
128
141
  mode: '运行模式',
129
142
  modeHint: '两种角色做的事完全不同,配置项也不重叠。切换后插件会重启。',
@@ -282,6 +295,19 @@ window.__ModuleLoader__.load({
282
295
  pairingShareName: 'Machine name',
283
296
  pairingShareNameHint: 'Other devices see this in their list; leave empty to show only the IP.',
284
297
  pairingSaveName: 'Save name',
298
+ /* Issued credentials */
299
+ shares: 'Shared credentials',
300
+ sharesEmpty: 'Nothing has been shared yet',
301
+ shareDevice: 'Device',
302
+ shareAddress: 'Address',
303
+ shareWhen: 'Shared at',
304
+ shareRevoke: 'Revoke',
305
+ shareRevoking: 'Revoking...',
306
+ shareKeyNote: 'Revoking also deletes that dedicated key, whose only purpose was this device.',
307
+ shareAndKey: 'and delete the key',
308
+ unknownDevice: 'unknown device',
309
+ pairingRetry: 'This request expired; start a new one',
310
+ peersRetry: 'Request again',
285
311
  /* Run mode */
286
312
  mode: 'Run mode',
287
313
  modeHint: 'The two roles do different things, and their settings do not overlap. Switching reloads the plugin.',
@@ -1220,9 +1246,10 @@ window.__ModuleLoader__.load({
1220
1246
  * 这一屏是「谁可以拿走我的模型额度」的唯一决策点,所以它把三件事摆明:
1221
1247
  * 谁在申请、要分享哪些模型、会新建一把专用 key。
1222
1248
  */
1223
- function PairingCard({ state, t, onApprove, onDeny, onSaveShareName }) {
1249
+ function PairingCard({ state, t, onApprove, onDeny, onSaveShareName, onRevoke }) {
1224
1250
  const pairing = state?.pairing ?? {}
1225
1251
  const pending = Array.isArray(pairing.pending) ? pairing.pending : []
1252
+ const shares = Array.isArray(state?.shares) ? state.shares : []
1226
1253
  const keys = Array.isArray(state?.keys) ? state.keys : []
1227
1254
 
1228
1255
  /* 正在批准哪一个(展开模型勾选)。 */
@@ -1233,6 +1260,8 @@ window.__ModuleLoader__.load({
1233
1260
  const [error, setError] = useState(undefined)
1234
1261
  const [done, setDone] = useState(undefined)
1235
1262
  const [shareName, setShareName] = useState(pairing.shareName ?? '')
1263
+ /* 正在撤销哪一个分享(要二次确认:它会连带删掉那把 key)。 */
1264
+ const [revoking, setRevoking] = useState(undefined)
1236
1265
 
1237
1266
  useEffect(() => {
1238
1267
  if (busy) return
@@ -1271,6 +1300,25 @@ window.__ModuleLoader__.load({
1271
1300
  }
1272
1301
  }
1273
1302
 
1303
+ /*
1304
+ * 撤销分享。
1305
+ *
1306
+ * 两步:先点「撤销分享」展开确认(因为会连带删掉那把 key),再点确认才真删。
1307
+ * 不做二次确认的话,一次误点就把别人正在用的访问断了。
1308
+ */
1309
+ const revoke = async (keyId2) => {
1310
+ setBusy(true)
1311
+ setError(undefined)
1312
+ try {
1313
+ await onRevoke(keyId2)
1314
+ setRevoking(undefined)
1315
+ } catch (caught) {
1316
+ setError(String(caught.message ?? caught))
1317
+ } finally {
1318
+ setBusy(false)
1319
+ }
1320
+ }
1321
+
1274
1322
  return h('div', { className: 'wbg-card wbg-card-soft' },
1275
1323
  h('div', { className: 'wbg-row' },
1276
1324
  h('strong', { className: 'wbg-sec-title' }, t.pairing),
@@ -1299,7 +1347,11 @@ window.__ModuleLoader__.load({
1299
1347
  style: { borderTop: '1px solid var(--dsw-alias-border-l1, #eee)', paddingTop: 10 },
1300
1348
  },
1301
1349
  h('div', { className: 'wbg-row' },
1302
- h('span', null, `${t.pairingFrom}「${entry.deviceName}」`),
1350
+ h('span', null, `${t.pairingFrom}「${entry.deviceName || t.unknownDevice}」`),
1351
+ /* 地址是我们从 socket 看到的,设备名可能是空的 —— 它才是可靠标识。 */
1352
+ entry.address === undefined || entry.address === ''
1353
+ ? null
1354
+ : h('span', { className: 'wbg-caption wbg-mono' }, entry.address),
1303
1355
  h('span', { className: 'wbg-spacer' }),
1304
1356
  h('span', { className: 'wbg-caption' },
1305
1357
  `${t.pairingExpires} ${new Date(entry.expiresAt).toLocaleTimeString()}`),
@@ -1372,6 +1424,55 @@ window.__ModuleLoader__.load({
1372
1424
  h('span', { className: 'wbg-alert-icon', 'aria-hidden': true }, '!'),
1373
1425
  h('span', { className: 'wbg-alert-body' }, error)),
1374
1426
 
1427
+ /* 已分享出去的凭据:能看到给了谁,并且能撤销。 */
1428
+ h('div', { className: 'wbg-field', style: { marginTop: 12 } },
1429
+ h('span', null, t.shares),
1430
+ shares.length === 0
1431
+ ? h('p', { className: 'wbg-caption' }, t.sharesEmpty)
1432
+ : h('div', null, shares.map((share) => h('div', {
1433
+ key: share.keyId,
1434
+ style: { borderTop: '1px solid var(--dsw-alias-border-l1, #eee)', paddingTop: 8, marginTop: 8 },
1435
+ },
1436
+ h('div', { className: 'wbg-row' },
1437
+ h('span', null, share.deviceName === '' ? t.unknownDevice : share.deviceName),
1438
+ h('span', { className: 'wbg-caption wbg-mono' }, share.address),
1439
+ h('span', { className: 'wbg-spacer' }),
1440
+ h('span', { className: 'wbg-caption' },
1441
+ `${t.shareWhen} ${new Date(share.sharedAt).toLocaleString()}`),
1442
+ ),
1443
+ h('div', { className: 'wbg-chips', style: { marginTop: 4 } },
1444
+ (share.models ?? []).map((name) => h('span', { key: name, className: 'wbg-chip wbg-mono' }, name)),
1445
+ ),
1446
+
1447
+ revoking === share.keyId
1448
+ ? h('div', { className: 'wbg-row', style: { marginTop: 6 } },
1449
+ h('span', { className: 'wbg-caption' }, t.shareKeyNote),
1450
+ h('span', { className: 'wbg-spacer' }),
1451
+ h('button', {
1452
+ type: 'button',
1453
+ className: 'wbg-btn wbg-btn-sm',
1454
+ onClick: () => setRevoking(undefined),
1455
+ disabled: busy,
1456
+ }, t.pairingCancel),
1457
+ h('button', {
1458
+ type: 'button',
1459
+ className: 'wbg-btn wbg-btn-sm wbg-btn-danger',
1460
+ onClick: () => revoke(share.keyId),
1461
+ disabled: busy,
1462
+ }, busy ? t.shareRevoking : t.shareRevoke),
1463
+ )
1464
+ : h('div', { className: 'wbg-row', style: { marginTop: 6 } },
1465
+ h('span', { className: 'wbg-spacer' }),
1466
+ h('button', {
1467
+ type: 'button',
1468
+ className: 'wbg-btn wbg-btn-sm',
1469
+ onClick: () => setRevoking(share.keyId),
1470
+ disabled: busy,
1471
+ }, t.shareRevoke),
1472
+ ),
1473
+ ))),
1474
+ ),
1475
+
1375
1476
  /* 本机名称:对方在发现列表里靠它认出该连哪台。 */
1376
1477
  pairing.available === true
1377
1478
  ? h('div', { className: 'wbg-field', style: { marginTop: 10 } },
@@ -1463,7 +1564,7 @@ window.__ModuleLoader__.load({
1463
1564
  * 这里**没有**任何对外分享的控件 —— client 模式下本机不监听、不发现、不接受
1464
1565
  * 配对请求,那些配置项连同界面一起消失(见 src/routes.js 的 upstreamMode)。
1465
1566
  */
1466
- function PeersCard({ state, t, onScan, onRequest, onClaim, onRemove, onRefresh, onDiagnose }) {
1567
+ function PeersCard({ state, t, onScan, onRequest, onClaim, onRemove, onRefresh, onDiagnose, onReset }) {
1467
1568
  const peers = Array.isArray(state?.peers) ? state.peers : []
1468
1569
  const pending = state?.pendingPairing
1469
1570
 
@@ -1495,7 +1596,8 @@ window.__ModuleLoader__.load({
1495
1596
  setScanning(true)
1496
1597
  setError(undefined)
1497
1598
  try {
1498
- setDiag(await onDiagnose())
1599
+ /* 把手填的地址一起送去 —— 它能顺带测 TCP,指出「广播被拦但地址通」。 */
1600
+ setDiag(await onDiagnose(manual.trim()))
1499
1601
  } catch (caught) {
1500
1602
  setError(String(caught.message ?? caught))
1501
1603
  } finally {
@@ -1557,6 +1659,17 @@ window.__ModuleLoader__.load({
1557
1659
  onClick: claim,
1558
1660
  disabled: busy,
1559
1661
  }, busy ? t.peersClaiming : t.peersClaim),
1662
+ /*
1663
+ * 「重新申请」:申请过期或已被领过时,用户不该只能看着一条报错。
1664
+ * 清掉本机的等待态,回到可以重新申请的界面。
1665
+ */
1666
+ h('button', {
1667
+ type: 'button',
1668
+ className: 'wbg-btn wbg-btn-sm',
1669
+ onClick: async () => { await onReset(); setNote(undefined); setError(undefined) },
1670
+ disabled: busy,
1671
+ style: { marginLeft: 6 },
1672
+ }, t.peersRetry),
1560
1673
  ),
1561
1674
  ),
1562
1675
 
@@ -1801,6 +1914,17 @@ window.__ModuleLoader__.load({
1801
1914
  await reload()
1802
1915
  }, [reload])
1803
1916
 
1917
+ /*
1918
+ * 撤销一次分享。
1919
+ *
1920
+ * 后端会**连带删掉那把专用 key** —— 它存在的唯一目的就是给那台设备用,
1921
+ * 留着等于留一把没人管的凭据。
1922
+ */
1923
+ const revokeShare = useCallback(async (keyId) => {
1924
+ await adminPost('/share/revoke', { keyId })
1925
+ await reload()
1926
+ }, [reload])
1927
+
1804
1928
  /* ── 模式切换与接收方动作 ─────────────────────────────────────── */
1805
1929
 
1806
1930
  const switchMode = useCallback(async (mode) => {
@@ -1813,7 +1937,7 @@ window.__ModuleLoader__.load({
1813
1937
  return body?.peers ?? []
1814
1938
  }, [])
1815
1939
 
1816
- const diagnosePeers = useCallback(async () => adminPost('/peers/diagnose', {}), [])
1940
+ const diagnosePeers = useCallback(async (baseUrl) => adminPost('/peers/diagnose', { baseUrl: baseUrl ?? '' }), [])
1817
1941
 
1818
1942
  const requestPeer = useCallback(async (baseUrl, deviceName) => {
1819
1943
  await adminPost('/peers/request', { baseUrl, deviceName })
@@ -1833,6 +1957,17 @@ window.__ModuleLoader__.load({
1833
1957
  await reload()
1834
1958
  }, [reload])
1835
1959
 
1960
+ /*
1961
+ * 放弃当前这次等待,重新开始。
1962
+ *
1963
+ * 用于「申请已失效」的情形:过了 5 分钟、或被本机取消过。重开之后用户
1964
+ * 会回到「找网关 / 填地址」那一步,而不是卡在一句报错上。
1965
+ */
1966
+ const resetPairing = useCallback(async () => {
1967
+ await adminPost('/peers/reset', {})
1968
+ await reload()
1969
+ }, [reload])
1970
+
1836
1971
  const removePeer = useCallback(async (id) => {
1837
1972
  await adminPost('/peers/remove', { id })
1838
1973
  await reload()
@@ -1981,6 +2116,7 @@ window.__ModuleLoader__.load({
1981
2116
  onRemove: removePeer,
1982
2117
  onRefresh: refreshPeer,
1983
2118
  onDiagnose: diagnosePeers,
2119
+ onReset: resetPairing,
1984
2120
  })
1985
2121
  : h('div', { style: { display: 'flex', flexDirection: 'column', gap: 12 } },
1986
2122
  h(PortCard, { state, t, onSave: savePort }),
@@ -1991,6 +2127,7 @@ window.__ModuleLoader__.load({
1991
2127
  onApprove: approvePairing,
1992
2128
  onDeny: denyPairing,
1993
2129
  onSaveShareName: saveShareName,
2130
+ onRevoke: revokeShare,
1994
2131
  }),
1995
2132
  h(OriginCard, { state, t, onSave: saveOriginPolicy }),
1996
2133
  ),
package/index.js CHANGED
@@ -22,6 +22,8 @@ import { workbuddyProviders } from './src/backend.js'
22
22
  import { closeDirect } from './src/direct.js'
23
23
  import { readConfig, writeConfig, makeKey, normalizeSelections, configPath, useDataDir } from './src/config.js'
24
24
  import { catalogForSettings } from './src/models.js'
25
+ import { LAN_PROVIDER, createLanAdapter } from './src/lan-adapter.js'
26
+ import { readPeers } from './src/peers.js'
25
27
 
26
28
  export const name = 'workbuddy-gateway'
27
29
 
@@ -200,6 +202,47 @@ export function apply(ctx, config) {
200
202
  void closeDirect()
201
203
  }
202
204
  })
205
+
206
+ /*
207
+ * **client 模式:把已接入的网关注册成 DSH 的模型来源。**
208
+ *
209
+ * 这是「装上插件就能在会话里用那台机器的模型」的落点。只存 peers 是不够的 ——
210
+ * 宿主要先认识那个 provider,模型选择器里才会出现它。
211
+ *
212
+ * 用的是鸭子类型的 adapter(见 src/lan-adapter.js):不继承 `LlmAdapter`,
213
+ * 因为那个类所在的包插件解析不到。注册表只调那几个方法,这样就够了。
214
+ */
215
+ if (settings.mode === 'client') {
216
+ ctx.effect(() => {
217
+ const llm = ctx.get('llm')
218
+ if (llm === undefined) {
219
+ ctx.logger.warn('workbuddy-gateway: no llm service, so shared models cannot be offered')
220
+ return () => {}
221
+ }
222
+ let handle
223
+ try {
224
+ handle = llm.registerAdapter([LAN_PROVIDER], createLanAdapter({
225
+ getPeers: readPeers,
226
+ logger: ctx.logger,
227
+ }))
228
+ } catch (error) {
229
+ /*
230
+ * 重复注册(例如插件被热重载两次)不该把整个插件搞崩 —— 那只意味着
231
+ * 这个 provider 已经有人服务了。
232
+ */
233
+ ctx.logger.warn(`workbuddy-gateway: could not register the LAN provider: ${error instanceof Error ? error.message : String(error)}`)
234
+ return () => {}
235
+ }
236
+ ctx.logger.info(`workbuddy-gateway: registered provider "${LAN_PROVIDER}" for shared models`)
237
+ return () => {
238
+ try {
239
+ handle()
240
+ } catch {
241
+ /* 已经卸载了 */
242
+ }
243
+ }
244
+ })
245
+ }
203
246
  }
204
247
 
205
248
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-connect-workbuddy-gateway",
3
- "version": "1.4.3",
3
+ "version": "1.5.0",
4
4
  "description": "把 dsh-connect-workbuddy 的 WorkBuddy 模型开放成 HTTP API:OpenAI Chat / Responses、Anthropic Messages、Gemini 四协议共用一个固定 base URL,模型按 API key 逐个勾选。也可切换成接收方,接入局域网内另一台设备分享的模型。",
5
5
  "keywords": [
6
6
  "dsh-plugin",
package/src/config.js CHANGED
@@ -160,7 +160,15 @@ export function normalizeConfig(value) {
160
160
  return { version: CONFIG_VERSION, keys }
161
161
  }
162
162
 
163
- /** 规整一把 key。`secret` 缺失的条目直接丢弃(不能用的 key 不该留在表里)。 */
163
+ /**
164
+ * 规整一把 key。`secret` 缺失的条目直接丢弃(不能用的 key 不该留在表里)。
165
+ *
166
+ * `sharedWith` 是**这把 key 是分享给谁用的**。只有通过局域网配对发出去的 key 才有
167
+ * 这个字段;自己手动建的 key 没有。
168
+ *
169
+ * 为什么必须记:不记的话,分享出去之后就看不出「哪把 key 给了哪台设备」,
170
+ * 于是既不能撤销某一次分享(不知道该删哪个),也不能在撤销时连带删掉那把 key。
171
+ */
164
172
  function normalizeKey(entry) {
165
173
  if (entry === null || typeof entry !== 'object') return undefined
166
174
  const secret = typeof entry.secret === 'string' ? entry.secret.trim() : ''
@@ -174,10 +182,28 @@ function normalizeKey(entry) {
174
182
  ...(typeof entry.lastUsedAt === 'number' ? { lastUsedAt: entry.lastUsedAt } : {}),
175
183
  /* `seeded` 记录「已经自动补过模型勾选」,见 index.js 的 ensureSeeded。 */
176
184
  ...(entry.seeded === true ? { seeded: true } : {}),
185
+ ...(normalizeSharedWith(entry.sharedWith) === undefined ? {} : { sharedWith: normalizeSharedWith(entry.sharedWith) }),
177
186
  selections: normalizeSelections(entry.selections),
178
187
  }
179
188
  }
180
189
 
190
+ /**
191
+ * 规整「分享给谁」。
192
+ *
193
+ * `deviceName` 可以为空(对方没填),`address` 是服务端从 socket 看到的对端 IP ——
194
+ * 它才是可靠的那个标识,所以至少要有它。
195
+ */
196
+ function normalizeSharedWith(value) {
197
+ if (value === null || typeof value !== 'object') return undefined
198
+ const address = typeof value.address === 'string' ? value.address.trim() : ''
199
+ if (address === '') return undefined
200
+ return {
201
+ deviceName: typeof value.deviceName === 'string' ? value.deviceName.trim().slice(0, 64) : '',
202
+ address,
203
+ sharedAt: typeof value.sharedAt === 'number' ? value.sharedAt : Date.now(),
204
+ }
205
+ }
206
+
181
207
  /**
182
208
  * 规整勾选清单。
183
209
  *