dsh-magpie-route 1.1.0 → 1.3.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
@@ -22,7 +22,7 @@ DSH's model picker only knows the routing-group alias you configured (for exampl
22
22
  ## Requirements
23
23
 
24
24
  - DeepSeek Harness with the composer dock slot (0.2.x).
25
- - A running [magpie](https://usemagpie.ai) gateway on the default `http://127.0.0.1:3425`.
25
+ - A running [magpie](https://usemagpie.ai) gateway. The plugin follows the endpoint DSH already uses for the `magpie` provider, so it tracks whatever host/port your DSH profile configures (default `http://127.0.0.1:3425`).
26
26
 
27
27
  ## Install
28
28
 
@@ -32,13 +32,14 @@ For a local checkout instead, install the bundle directory with `plugin_manager`
32
32
 
33
33
  ## Configuration
34
34
 
35
- No configuration is required: the plugin talks to the default local gateway at `http://127.0.0.1:3425` and filters the `dsh` agent. Only if your gateway runs elsewhere, override `baseURL` (and optionally `agent`/`limit`) in your profile's `cordis.patch.yml`.
35
+ No configuration is required. The Host half discovers the gateway from DSH itself: every magpie inference reports its resolved endpoint through `llm-pi-ai/request-headers`, and before the first inference it reads `llm-pi-ai`'s `providers.magpie.baseURL` (then `magpie-anthropic`, then `http://127.0.0.1:3425`). An explicit `baseURL` in the plugin row's `config` wins, so you only set it to point at an address DSH does not know about.
36
36
 
37
37
  ## How it works
38
38
 
39
- - The Host half contributes `X-Magpie-Session: <dsh session id>` through DSH's `llm-pi-ai/request-headers` waterfall (a header only; no request-body, model, or tool change), then reads `GET {baseURL}/v1/magpie/route?session=<dsh session id>` for this session's current/last route.
39
+ - The Host half derives the gateway root from the `baseURL` that DSH reports for each magpie inference through the `llm-pi-ai/request-headers` waterfall (no config-file parsing), contributes `X-Magpie-Session: <dsh session id>` (a header only; no request-body, model, or tool change), then reads `GET {baseURL}/v1/magpie/route?session=<dsh session id>` for this session's current/last route.
40
+ - Before the first inference it reads `llm-pi-ai`'s configured `providers.magpie.baseURL` from the live Loader config (falling back to `http://127.0.0.1:3425`), so the first paint already uses the right port.
40
41
  - `GET {baseURL}/v1/magpie/trace` is the fallback for the first paint, before any inference has happened in this Host process.
41
- - The client half renders the composer-dock line by polling the package's same-origin route `/plugins/magpie-route/route`, because the magpie gateway sends no CORS headers.
42
+ - The client half renders the composer-dock line by polling the package's same-origin route `/plugins/magpie-route/route`, because the magpie gateway sends no CORS headers. The route checks DSH's `connection.requestRejection` (Host/Origin trust and browser authentication) before serving data, in addition to rejecting a mismatched `Origin` header.
42
43
 
43
44
  ## Limitations
44
45
 
@@ -47,6 +48,7 @@ No configuration is required: the plugin talks to the default local gateway at `
47
48
  - `trace` keeps a recent window (about 60 rows), not full history. For full history use `magpie usage --csv`.
48
49
  - `trace` reports `provider/model`; the final `served_model` (for example `hy4-free`) appears in `magpie usage --csv`.
49
50
  - Routing groups use session affinity and rules, so the "current real model" can change per turn.
51
+ - The gateway address mirrors whatever DSH has configured for the `magpie` provider. If that DSH value is stale (for example after moving magpie without reconnecting it), the dock shows the same stale endpoint; set `config.baseURL` to correct it.
50
52
 
51
53
  ## Uninstall
52
54
 
package/README.zh.md CHANGED
@@ -22,7 +22,7 @@ DSH 的模型选择器只知道你配置的路由组别名(例如 `magpie/grou
22
22
  ## 要求
23
23
 
24
24
  - 带 composer dock slot 的 DeepSeek Harness(0.2.x)。
25
- - 正在运行的 [magpie](https://usemagpie.ai) 网关,默认地址 `http://127.0.0.1:3425`。
25
+ - 正在运行的 [magpie](https://usemagpie.ai) 网关。插件会跟随 DSH 里 `magpie` provider 已经使用的地址,所以 DSH profile 配的是哪个主机/端口就跟哪个(默认 `http://127.0.0.1:3425`)。
26
26
 
27
27
  ## 安装
28
28
 
@@ -32,13 +32,14 @@ DSH 的模型选择器只知道你配置的路由组别名(例如 `magpie/grou
32
32
 
33
33
  ## 配置
34
34
 
35
- 默认无需配置:插件自动连接本地网关 `http://127.0.0.1:3425`,并按 `dsh` agent 过滤。只有网关不在默认地址时,才需要在 profile 的 `cordis.patch.yml` 里覆盖 `baseURL`(以及可选的 `agent`/`limit`)。
35
+ 默认无需配置。Host 半从 DSH 自身取网关地址:每次 magpie 推理都会通过 `llm-pi-ai/request-headers` 报告解析后的 endpoint;首次绘制之前则读 `llm-pi-ai` 的 `providers.magpie.baseURL`(再退 `magpie-anthropic`,最后兜底 `http://127.0.0.1:3425`)。插件行 `config` 里显式设置的 `baseURL` 优先级最高,只有要指向 DSH 不知道的地址时才需要写。
36
36
 
37
37
  ## 工作原理
38
38
 
39
- - Host 半通过 DSH 的 `llm-pi-ai/request-headers` waterfall 给每次 magpie 推理加上 `X-Magpie-Session: <dsh session id>`(只加请求头,不改请求体、模型或工具集),然后读取 `GET {baseURL}/v1/magpie/route?session=<dsh session id>` 得到本会话当前/最近的路由。
39
+ - Host 半从 DSH 为每次 magpie 推理报告的 `baseURL`(`llm-pi-ai/request-headers` waterfall)推导出网关根地址,不解析任何配置文件;同时加上 `X-Magpie-Session: <dsh session id>`(只加请求头,不改请求体、模型或工具集),然后读取 `GET {baseURL}/v1/magpie/route?session=<dsh session id>` 得到本会话当前/最近的路由。
40
+ - 首次推理之前,它从运行中的 Loader 配置读取 `llm-pi-ai` 的 `providers.magpie.baseURL`(兜底 `http://127.0.0.1:3425`),所以首帧就用对端口。
40
41
  - `GET {baseURL}/v1/magpie/trace` 只作为首次绘制的兜底(Host 进程里还没有发生过推理时)。
41
- - Client 半轮询本包 Host 的同源路由 `/plugins/magpie-route/route`,渲染输入框下方那一行,因为 magpie 网关没有 CORS 头。
42
+ - Client 半轮询本包 Host 的同源路由 `/plugins/magpie-route/route`,渲染输入框下方那一行,因为 magpie 网关没有 CORS 头。路由入口先检查 DSH 的 `connection.requestRejection`(Host/Origin 信任与浏览器认证),并额外拒绝 `Origin` 不匹配的请求。
42
43
 
43
44
  ## 限制
44
45
 
@@ -47,6 +48,7 @@ DSH 的模型选择器只知道你配置的路由组别名(例如 `magpie/grou
47
48
  - `trace` 只保留最近窗口(约 60 条),不是完整历史;完整历史用 `magpie usage --csv`。
48
49
  - `trace` 显示 `provider/model`;最终 `served_model`(如 `hy4-free`)只在 `magpie usage --csv` 里出现。
49
50
  - 路由组有会话亲和与规则路由,“当前真实模型”可能逐轮变化。
51
+ - 网关地址跟 DSH 给 `magpie` provider 配的值一致。如果 DSH 里那个值本身过期了(比如 magpie 换了端口但没重新接入),dock 会显示同一个过期地址;此时用 `config.baseURL` 覆盖即可。
50
52
 
51
53
  ## 卸载
52
54
 
package/index.js CHANGED
@@ -4,13 +4,11 @@ import { collectRoutes, describeError } from './route-query.js'
4
4
  export const name = 'magpie-route'
5
5
 
6
6
  /** Only the same-origin route remains: the plugin has no command and no agent tool. */
7
- export const inject = ['webServer']
7
+ export const inject = ['webServer', 'connection']
8
8
 
9
- const DEFAULTS = {
10
- baseURL: 'http://127.0.0.1:3425',
11
- agent: 'dsh',
12
- limit: 5,
13
- }
9
+ /** Last resort only. The real endpoint comes from the DSH profile's magpie provider. */
10
+ const FALLBACK_BASE_URL = 'http://127.0.0.1:3425'
11
+ const DEFAULT_AGENT = 'dsh'
14
12
 
15
13
  /** Same-origin endpoint for the composer dock; browsers cannot call the gateway directly (no CORS). */
16
14
  const CLIENT_ROUTE = '/plugins/magpie-route/route'
@@ -22,9 +20,90 @@ function jsonResponse(value, status = 200) {
22
20
  })
23
21
  }
24
22
 
23
+ /** Both the OpenAI-compatible and Anthropic routes go through the same magpie gateway. */
24
+ function isMagpieProvider(provider) {
25
+ return typeof provider === 'string' && /^magpie(?:-|$)/u.test(provider)
26
+ }
27
+
28
+ /**
29
+ * Reduce a pi-ai provider endpoint to the magpie gateway root, where the
30
+ * `/v1/magpie/*` read endpoints live.
31
+ *
32
+ * `http://127.0.0.1:3425/v1` -> `http://127.0.0.1:3425`
33
+ * `https://host/prefix/v1` -> `https://host/prefix`
34
+ * `http://127.0.0.1:3425` -> `http://127.0.0.1:3425`
35
+ */
36
+ function gatewayOriginOf(baseURL) {
37
+ if (typeof baseURL !== 'string' || baseURL.trim().length === 0) return undefined
38
+ try {
39
+ const url = new URL(baseURL.trim())
40
+ const path = url.pathname.replace(/\/v1\/?$/u, '').replace(/\/+$/u, '')
41
+ return `${url.origin}${path}`
42
+ } catch {
43
+ return undefined
44
+ }
45
+ }
46
+
47
+ /** Pull `providers.magpie.baseURL` out of a raw `llm-pi-ai` config object. */
48
+ function magpieBaseURLFromConfig(config) {
49
+ if (!config || typeof config !== 'object') return undefined
50
+ const providers = config.providers && typeof config.providers === 'object' ? config.providers : undefined
51
+ if (providers === undefined) return undefined
52
+ const route = providers.magpie ?? providers['magpie-anthropic']
53
+ return route && typeof route === 'object' && typeof route.baseURL === 'string'
54
+ ? route.baseURL
55
+ : undefined
56
+ }
57
+
58
+ /**
59
+ * Read the endpoint from the live Loader entry without touching the profile
60
+ * patch file. `fiber.effectiveConfig` is the config DSH actually activated.
61
+ */
62
+ function readFromLoader(ctx) {
63
+ try {
64
+ const loader = ctx.get('loader')
65
+ if (loader === undefined || typeof loader.entries !== 'function') return undefined
66
+ for (const entry of loader.entries()) {
67
+ if (entry?.options?.id !== 'llm-pi-ai') continue
68
+ const effective = entry.fiber?.effectiveConfig
69
+ if (!effective) continue
70
+ // `providers` is a volatile reference on the resolved Config.
71
+ const providers = effective.providers
72
+ const raw = providers && typeof providers.get === 'function' ? providers.get() : providers
73
+ const found = magpieBaseURLFromConfig({ providers: raw })
74
+ if (found !== undefined) return found
75
+ }
76
+ } catch {
77
+ /* fall through to configEditor */
78
+ }
79
+ return undefined
80
+ }
81
+
82
+ /**
83
+ * Compose the profile the same way the config editor does and read the
84
+ * `llm-pi-ai` row. Used for the first paint, before any inference has run.
85
+ */
86
+ function readFromConfigEditor(ctx) {
87
+ try {
88
+ const editor = ctx.get('configEditor')
89
+ if (editor === undefined || typeof editor.configuration !== 'function') return undefined
90
+ const row = editor.configuration().find(candidate => candidate?.entry?.options?.id === 'llm-pi-ai')
91
+ if (row === undefined) return undefined
92
+ return magpieBaseURLFromConfig(row.override) ?? magpieBaseURLFromConfig(row.inherited)
93
+ } catch {
94
+ return undefined
95
+ }
96
+ }
97
+
25
98
  /**
26
99
  * Serve the same-origin read route that the client dock polls.
27
100
  *
101
+ * The gateway address is discovered from DSH instead of being configured here:
102
+ * every magpie inference reports its resolved endpoint through the
103
+ * `llm-pi-ai/request-headers` waterfall, and before the first inference the
104
+ * plugin reads the `llm-pi-ai` row's `providers.magpie.baseURL`. An explicit
105
+ * `config.baseURL` still wins, and `http://127.0.0.1:3425` is the last resort.
106
+ *
28
107
  * The Host half also tags every magpie inference with `X-Magpie-Session`, so
29
108
  * the gateway can answer `/v1/magpie/route?session=...` for *this* DSH session
30
109
  * (including an in-flight request's already-picked target). The trace endpoint
@@ -32,16 +111,39 @@ function jsonResponse(value, status = 200) {
32
111
  * has happened yet. The plugin adds no command and no agent tool.
33
112
  */
34
113
  export function apply(ctx, config = {}) {
35
- const settings = { ...DEFAULTS, ...(config && typeof config === 'object' ? config : {}) }
114
+ const raw = config && typeof config === 'object' ? config : {}
115
+ const configuredBaseURL = typeof raw.baseURL === 'string' && raw.baseURL.trim().length > 0
116
+ ? (gatewayOriginOf(raw.baseURL) ?? raw.baseURL.trim())
117
+ : undefined
118
+ const settings = {
119
+ agent: typeof raw.agent === 'string' && raw.agent.trim().length > 0 ? raw.agent.trim() : DEFAULT_AGENT,
120
+ limit: raw.limit,
121
+ }
122
+
123
+ const connection = ctx.connection
36
124
  let lastSession
125
+ let eventBaseURL
126
+ let storedBaseURL
127
+
128
+ /** First-paint lookup from DSH config; cached once a value is found. */
129
+ const profileBaseURL = () => {
130
+ if (storedBaseURL !== undefined) return storedBaseURL
131
+ const found = readFromLoader(ctx) ?? readFromConfigEditor(ctx)
132
+ storedBaseURL = gatewayOriginOf(found)
133
+ return storedBaseURL
134
+ }
135
+
136
+ const currentBaseURL = () => configuredBaseURL ?? eventBaseURL ?? profileBaseURL() ?? FALLBACK_BASE_URL
37
137
 
38
138
  // Only a transport header is added; the request body, model, and tool set are untouched.
39
139
  ctx.effect(() => ctx.on('llm-pi-ai/request-headers', (request, headers, next) => {
40
- if (request.provider === 'magpie'
41
- && typeof request.sessionId === 'string'
42
- && request.sessionId.length > 0) {
43
- lastSession = request.sessionId
44
- headers.set('x-magpie-session', request.sessionId)
140
+ if (isMagpieProvider(request.provider)) {
141
+ const gateway = gatewayOriginOf(request.baseURL)
142
+ if (gateway !== undefined) eventBaseURL = gateway
143
+ if (typeof request.sessionId === 'string' && request.sessionId.length > 0) {
144
+ lastSession = request.sessionId
145
+ headers.set('x-magpie-session', request.sessionId)
146
+ }
45
147
  }
46
148
  return next()
47
149
  }))
@@ -50,24 +152,28 @@ export function apply(ctx, config = {}) {
50
152
  kind: 'exact',
51
153
  path: CLIENT_ROUTE,
52
154
  resource: async (request) => {
155
+ const rejection = connection.requestRejection(request)
156
+ if (rejection !== undefined) return new Response(null, { status: rejection })
157
+
53
158
  const url = new URL(request.url)
54
159
  const origin = request.headers.get('origin')
55
160
  if (origin !== null && origin !== url.origin) {
56
161
  return jsonResponse({ error: 'forbidden origin' }, 403)
57
162
  }
163
+ const baseURL = currentBaseURL()
58
164
  try {
59
165
  let result
60
166
  if (lastSession) {
61
- result = await collectRoutes({ ...settings, session: lastSession, signal: request.signal })
167
+ result = await collectRoutes({ ...settings, baseURL, session: lastSession, signal: request.signal })
62
168
  if (result.kind === 'session-empty') {
63
- result = await collectRoutes({ ...settings, limit: 1, signal: request.signal })
169
+ result = await collectRoutes({ ...settings, baseURL, limit: 1, signal: request.signal })
64
170
  }
65
171
  } else {
66
- result = await collectRoutes({ ...settings, limit: 1, signal: request.signal })
172
+ result = await collectRoutes({ ...settings, baseURL, limit: 1, signal: request.signal })
67
173
  }
68
- return jsonResponse(result)
174
+ return jsonResponse({ ...result, baseURL })
69
175
  } catch (error) {
70
- return jsonResponse({ error: describeError(error, settings.baseURL) }, 502)
176
+ return jsonResponse({ error: describeError(error, baseURL), baseURL }, 502)
71
177
  }
72
178
  },
73
179
  }))
package/locale/en.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "meta": {
3
3
  "title": "Magpie real route",
4
- "description": "Show the real upstream model behind a magpie routing group directly below the composer (the text follows the DSH UI language: \"magpie real route\" in English, \"magpie 真实路由\" in Chinese). The Host half tags each magpie inference with X-Magpie-Session and reads /v1/magpie/route for this session's current/last route, including an in-flight request's already-picked target; trace is the first-paint fallback. The dock line refreshes every 15 seconds and on click; it reads the package Host half's same-origin route /plugins/magpie-route/route, so the browser never calls the gateway directly. Read-only: it sends no model request and does not change routing; the magpie gateway must be running. No configuration is required: it uses the default gateway http://127.0.0.1:3425; only if your gateway runs elsewhere, override baseURL in cordis.patch.yml. This plugin adds no command and no agent tool."
4
+ "description": "Show the real upstream model behind a magpie routing group directly below the composer (the text follows the DSH UI language: \"magpie real route\" in English, \"magpie 真实路由\" in Chinese). The Host half tags each magpie inference with X-Magpie-Session and reads /v1/magpie/route for this session's current/last route, including an in-flight request's already-picked target; trace is the first-paint fallback. The dock line refreshes every 15 seconds and on click; it reads the package Host half's same-origin route /plugins/magpie-route/route, so the browser never calls the gateway directly; the route checks DSH's connection.requestRejection (Host/Origin trust and browser authentication) and rejects a mismatched Origin. Read-only: it sends no model request and does not change routing; the magpie gateway must be running. No configuration is required: the Host half discovers the gateway from DSH's active magpie provider (each inference's request.baseURL, with llm-pi-ai's providers.magpie.baseURL for the first paint, and http://127.0.0.1:3425 as the last resort); only override config.baseURL when you need an address DSH does not know about. This plugin adds no command and no agent tool."
5
5
  }
6
6
  }
package/locale/zh.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "meta": {
3
3
  "title": "Magpie 真实路由",
4
- "description": "把 magpie 路由组别名背后的真实上游模型直接显示在输入框下方(文本跟随 DSH 界面语言:中文“magpie 真实路由”,英文“magpie real route”)。Host 半给每次 magpie 推理加上 X-Magpie-Session,并读取 /v1/magpie/route 得到本会话当前/最近的路由(进行中请求显示已选定目标);trace 仅作为首次绘制兜底。界面行每 15 秒自动刷新、点击刷新,读取 Host 半的同源路由 /plugins/magpie-route/route(浏览器无需直连网关)。只读,不发模型请求、不改路由;需要 magpie 网关正在运行。默认无需配置,自动连接 http://127.0.0.1:3425;仅当网关不在默认地址时,才需要在 cordis.patch.yml 中覆盖 baseURL。本插件不提供命令,也不提供 agent 工具。"
4
+ "description": "把 magpie 路由组别名背后的真实上游模型直接显示在输入框下方(文本跟随 DSH 界面语言:中文“magpie 真实路由”,英文“magpie real route”)。Host 半给每次 magpie 推理加上 X-Magpie-Session,并读取 /v1/magpie/route 得到本会话当前/最近的路由(进行中请求显示已选定目标);trace 仅作为首次绘制兜底。界面行每 15 秒自动刷新、点击刷新,读取 Host 半的同源路由 /plugins/magpie-route/route(浏览器无需直连网关);路由入口先经过 DSH connection.requestRejection(Host/Origin 信任与浏览器认证)检查,并拒绝 Origin 不匹配的请求。只读,不发模型请求、不改路由;需要 magpie 网关正在运行。默认无需配置:Host 半会从 DSH 当前生效的 magpie provider 配置里自动取得网关地址——每次推理的 request.baseURL,首帧读 llm-pi-ai 的 providers.magpie.baseURL,最后才兜底 http://127.0.0.1:3425;只有要指向 DSH 之外的地址时才需在插件 config.baseURL 里覆盖。本插件不提供命令,也不提供 agent 工具。"
5
5
  }
6
6
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-magpie-route",
3
- "version": "1.1.0",
3
+ "version": "1.3.0",
4
4
  "description": "DeepSeek Harness plugin: show the real upstream model behind a magpie routing group directly below the composer.",
5
5
  "type": "module",
6
6
  "license": "MIT",
package/route-query.js CHANGED
@@ -150,5 +150,5 @@ export function formatRoutes(result) {
150
150
 
151
151
  export function describeError(error, baseURL) {
152
152
  const reason = error instanceof Error ? error.message : String(error)
153
- return `查询 magpie 网关失败:${reason}\n请确认 magpie 正在运行并监听 ${normalizeBaseURL(baseURL)}(/v1/magpie/trace、/v1/magpie/route)。`
153
+ return `查询 magpie 网关失败:${reason}\n当前使用的网关地址是 ${normalizeBaseURL(baseURL)}(自动取自 DSH 里 llm-pi-ai 的 magpie baseURL);请确认 magpie 正在运行并监听该地址(/v1/magpie/trace、/v1/magpie/route)。如 DSH 里的地址与实际不符,可在插件 config.baseURL 里覆盖。`
154
154
  }