dsh-model-headers 0.2.0 → 0.2.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/lib/client.js CHANGED
@@ -17,13 +17,13 @@ window.__ModuleLoader__.load({
17
17
 
18
18
  const INJECT = ["slots", "connection"];
19
19
 
20
- /** connection RPC 通道(与 host 半一致)。 */
21
- const RPC_CHANNEL = "/model-headers";
20
+ /** dsh v0.1.5:浏览器侧统一走 /api 通道,endpoint 带插件前缀。 */
21
+ const RPC_PREFIX = "model-headers";
22
22
 
23
23
  let hostCtx = null;
24
24
 
25
25
  async function callApi(endpoint, body = {}) {
26
- const result = await hostCtx.connection.rpc.call(RPC_CHANNEL, endpoint, body);
26
+ const result = await hostCtx.connection.rpc.call("/api", `${RPC_PREFIX}/${endpoint}`, body);
27
27
  if (result.ok) return result.value;
28
28
  throw new Error(result.error?.message || `rpc ${endpoint} failed`);
29
29
  }
package/lib/index.js CHANGED
@@ -27,6 +27,8 @@ import path from 'node:path'
27
27
  import { randomUUID } from 'node:crypto'
28
28
 
29
29
  export const name = 'dsh-model-headers'
30
+ // 保持空 inject:静态声明只影响自身 fiber 启动门槛,headless 无 webServer 会让
31
+ // 插件永不装配(自愈/热加载失效)。RPC 的服务等待改用 apply 内 ctx.inject 回调。
30
32
  export const inject = []
31
33
  export { ensureCacheRetention, listConfiguredModels }
32
34
 
@@ -415,35 +417,41 @@ export function apply(ctx) {
415
417
  await loadRules()
416
418
  await ensureCacheRetention()
417
419
  await watchConfig()
418
- console.log(`[dsh-model-headers] host ready: ${rules.length} rule(s), rpc '${RPC_CHANNEL}'`)
420
+ console.log(`[dsh-model-headers] host ready: ${rules.length} rule(s)`)
419
421
  })()
420
422
 
421
- // connection 是 web profile 的服务,headless 没有 → 可选获取,取不到就跳过 RPC
422
- //(fetch 钩子与配置文件热加载不依赖它)。ctx.get 绕过 inject 要求直读服务店。
423
- const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms))
424
- void (async () => {
425
- let connection
426
- for (let attempt = 0; attempt < 10; attempt++) {
427
- connection = ctx.get?.('connection')
428
- if (connection?.rpc?.handle) break
429
- connection = undefined
430
- await sleep(1000)
431
- }
432
- if (!connection?.rpc?.handle) {
433
- console.log('[dsh-model-headers] 无 connection 服务(headless):RPC 关闭,注入钩子照常工作')
434
- return
423
+ // ---- RPC(dsh v0.1.5 新传输模型)----
424
+ // 浏览器侧固定 POST /api/<endpoint>;/api 前缀路由由 core 的 connection
425
+ // 宿主插件用 webServer 挂载。插件通过 connection.fetch.register 注册精确
426
+ // POST 路由(fetchRoutes 优先于网关 interceptor,且注册过程不碰 webServer,
427
+ // 只需 inject connection)。信封:client-request {rpcId,method,payload} →
428
+ // server-response {rpcId,result:{ok,value|error}}。
429
+ const okResponse = (rpcId, result) => new Response(
430
+ JSON.stringify({ type: 'server-response', rpcId, result }),
431
+ { status: 200, headers: { 'content-type': 'application/json' } },
432
+ )
433
+ ctx.inject(['connection'], (svc) => {
434
+ for (const [name, action] of Object.entries(endpoints)) {
435
+ const path = `/api/${RPC_PREFIX}/${name}`
436
+ svc.connection.fetch.register({
437
+ path,
438
+ methods: ['POST'],
439
+ requestBody: 'buffered',
440
+ fetch: async (request) => {
441
+ let rpcId = ''
442
+ try {
443
+ const msg = await request.json()
444
+ rpcId = typeof msg?.rpcId === 'string' ? msg.rpcId : ''
445
+ const value = await action.run(msg?.payload ?? {})
446
+ return okResponse(rpcId, rpcOk(value))
447
+ } catch (e) {
448
+ return okResponse(rpcId, rpcFail(e2str(e)))
449
+ }
450
+ },
451
+ })
435
452
  }
436
- connection.rpc.handle(RPC_CHANNEL, async (endpoint, payload) => {
437
- try {
438
- const action = endpoints[endpoint]
439
- if (action === undefined) return rpcFail(`unknown endpoint "${endpoint}"`)
440
- return rpcOk(await action.run(payload ?? {}))
441
- } catch (e) {
442
- return rpcFail(e2str(e))
443
- }
444
- }, { authority: 'loopback' })
445
- console.log(`[dsh-model-headers] rpc '${RPC_CHANNEL}' ready`)
446
- })()
453
+ console.log(`[dsh-model-headers] rpc routes ready under /api/${RPC_PREFIX} (${Object.keys(endpoints).length} endpoints)`)
454
+ })
447
455
 
448
456
  // 文件监听随插件卸载关闭;fetch 钩子恢复原状(配合热重载)。
449
457
  ctx.effect(() => () => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-model-headers",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "description": "Per-model custom request headers: inject headers into LLM requests matched by model-name wildcard; values support fixed strings and the ${sessionId} variable. Managed from a settings-page UI.",
5
5
  "license": "BSD-3-Clause",
6
6
  "homepage": "https://github.com/LvanLa/deepseek-harness-ex/tree/master/plugins/dsh-model-headers",