dsh-gateway-provider 1.0.1 → 1.0.2

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
@@ -50,14 +50,13 @@ re-deploy.
50
50
  1. Install the plugin into your profile (published on npm):
51
51
 
52
52
  ```bash
53
- dsh plugin --profile web add dsh-gateway-provider
53
+ dsh plugin --profile web add dsh-gateway-provider # latest
54
+ dsh plugin --profile web add dsh-gateway-provider@1.0.2 # pinned
54
55
  ```
55
56
 
56
- `dsh plugin add` forwards to `pnpm add` in the profile directory, so any
57
- pnpm spec works — e.g. pin a GitHub release with
58
- `git+https://github.com/Luck9Star/dsh-gateway-provider.git#v1.0.1` (omit
59
- the suffix to track main). The bundle patch (`cordis.patch.yml`) then
60
- mounts the `llm-newapi` loader row automatically — no manual patch editing.
57
+ `dsh plugin add` forwards to `pnpm add` in the profile directory.
58
+ The bundle patch (`cordis.patch.yml`) then mounts the `llm-newapi`
59
+ loader row automatically — no manual patch editing.
61
60
 
62
61
  2. Store your gateway key in `$DSH_HOME/.credentials.yaml` (mode 0600,
63
62
  hot-reloaded):
@@ -0,0 +1,217 @@
1
+ # dsh-gateway-provider
2
+
3
+ [![gitleaks](https://github.com/Luck9Star/dsh-gateway-provider/actions/workflows/gitleaks.yml/badge.svg)](https://github.com/Luck9Star/dsh-gateway-provider/actions/workflows/gitleaks.yml)
4
+
5
+ > English: [README.md](../README.md)
6
+
7
+ 把任意 LLM 网关 —— **newapi、LiteLLM、Higress 或任何 OpenAI 兼容端点** —— 接入
8
+ DeepSeek Harness。插件自动发现网关的模型列表,用 [models.dev](https://models.dev)
9
+ 的真实参数补齐每个模型,并通过 pi-ai SDK 以模型原生协议(OpenAI / Anthropic /
10
+ Gemini)分发请求。
11
+
12
+ ## 为什么需要它
13
+
14
+ DeepSeek Harness 自带的适配器(`llm-deepseek`、`llm-pi-ai`)各自只服务一个官方
15
+ provider。如果你的模型都在网关后面,就得手工维护一份静态模型清单,上下文窗口、
16
+ 输出上限全靠猜。
17
+
18
+ 本插件直接把网关挂进来:**N 个网关 = N 个 provider 路由,零静态清单。** 每个模型
19
+ 带着真实参数,网关侧加模型即可生效——无需重新部署。
20
+
21
+ ## 你能得到什么
22
+
23
+ - **多网关** — 默认 `newapi` 路由 + 每个额外网关一条 `gateway:<id>` 路由,
24
+ 各自独立 catalog 缓存。
25
+ - **自动发现** — 首选 `GET {base}/v1/models`(含每个模型支持的请求格式),
26
+ 管理 API 兜底。
27
+ - **真实参数** — models.dev 补齐上下文窗口、输出上限、推理等级、发布日期;
28
+ 配置默认值只是兜底。
29
+ - **全协议覆盖** — 每个模型路由到自己的协议(`openai-completions` /
30
+ `openai-responses` / `anthropic-messages` / `google-generative-ai`),
31
+ 无手写 SSE、无手写请求序列化。
32
+ - **图形设置页** — **设置 → 网关模型**:按模板添加/编辑网关、测试连接、
33
+ 隐藏/覆写/自定义添加模型——不用碰 YAML。
34
+ - **干净的选择器** — 按发布日期新到旧排序、只保留对话模型、正则剔除。
35
+
36
+ ## 环境要求
37
+
38
+ - 装有 profile 的 DeepSeek Harness(`dsh`)
39
+ - 一个可达网关:newapi / LiteLLM / Higress / 任意 OpenAI 兼容端点
40
+ - 该网关的 API Key
41
+
42
+ ## 快速开始
43
+
44
+ 1. 把插件装进 profile(已发布到 npm):
45
+
46
+ ```bash
47
+ dsh plugin --profile web add dsh-gateway-provider # 最新版
48
+ dsh plugin --profile web add dsh-gateway-provider@1.0.2 # 钉住版本
49
+ ```
50
+
51
+ `dsh plugin add` 会在 profile 目录里转发执行 `pnpm add`。bundle 补丁
52
+ (`cordis.patch.yml`)随后自动挂载 `llm-newapi` 加载行——无需手工改 patch。
53
+
54
+ 2. 把网关 key 写入 `$DSH_HOME/.credentials.yaml`(权限 0600,热加载):
55
+
56
+ ```yaml
57
+ NEWAPI_API_KEY: sk-REPLACE_WITH_YOUR_KEY
58
+ ```
59
+
60
+ 或在启动环境中导出 `NEWAPI_API_KEY`。
61
+
62
+ 3. 重启 profile,打开 **设置 → 网关模型**。
63
+
64
+ **成功的样子:** 网关出现并带"已同步 N 个模型"徽标,全部对话模型可选、
65
+ 上下文窗口是真实值。基础地址解析顺序:`llm-newapi.baseURL` 设置 →
66
+ `NEWAPI_BASE_URL` / `NEWAPI_API_URL` 环境变量 → 公共默认
67
+ `https://api.newapi.ai`。
68
+
69
+ ## 添加更多网关
70
+
71
+ 额外网关写在 `gateways` 数组——每个成为独立的 `gateway:<id>` provider 路由:
72
+
73
+ ```yaml
74
+ llm-newapi:
75
+ baseURL: https://your-newapi-instance.com
76
+ gateways:
77
+ - id: litellm-prod
78
+ label: LiteLLM 生产
79
+ baseURL: https://litellm.example.com
80
+ apiKeyEnv: LITELLM_API_KEY
81
+ flavor: litellm # 表单模板:newapi / litellm / higress / openai-compatible / custom
82
+
83
+ # 完全自定义网关:各协议写完整端点地址,不共享基础地址;
84
+ # 留空的协议不启用。
85
+ - id: edge
86
+ label: 边缘网关
87
+ flavor: custom
88
+ openaiURL: https://edge.example.com/openai/v1/chat/completions
89
+ responsesURL: https://edge.example.com/openai/v1/responses
90
+ anthropicURL: https://edge.example.com/anthropic/v1/messages
91
+ apiKeyEnv: EDGE_API_KEY
92
+ ```
93
+
94
+ 也可以在设置页里用同样的模板添加网关,不必编辑 YAML。
95
+
96
+ ## 按网关控制模型清单
97
+
98
+ 隐藏模型、修正错误元数据、添加网关未列出的模型:
99
+
100
+ ```yaml
101
+ llm-newapi:
102
+ models:
103
+ - id: glm-5.2
104
+ disabled: true # 从选择器隐藏
105
+ - id: glm-5.2-highspeed
106
+ contextWindow: 1000000 # 覆盖发现值
107
+ protocol: openai # 强制协议(openai/anthropic/gemini/openai-response)
108
+ - id: my-internal-model # 自定义模型(网关未列出)
109
+ name: 我的内测模型
110
+ contextWindow: 200000
111
+ ```
112
+
113
+ **网关模型**设置页提供同样的操作:搜索、带计数的已隐藏/自定义筛选、逐模型覆写
114
+ 编辑器(placeholder 显示发现值)、连接测试、保存/取消语义。
115
+
116
+ ## 配置参考
117
+
118
+ `$DSH_HOME/settings.yaml` 的 `llm-newapi:` 节。扁平字段(`baseURL` /
119
+ `apiKeyEnv` 等)构建默认 `newapi` 路由;`gateways` 数组里的每个网关支持
120
+ 大部分同名字段(`label` / `apiKeyEnv` / `flavor` / `catalogMode` /
121
+ `endpointPriority` / …)。
122
+
123
+ | 字段 | 默认 | 说明 |
124
+ |------|------|------|
125
+ | `label` | `NewAPI` | 默认网关路由的显示名 |
126
+ | `apiKeyEnv` | `NEWAPI_API_KEY` | 凭据引用(环境变量名) |
127
+ | `baseURL` | env `NEWAPI_BASE_URL` / `NEWAPI_API_URL` → `https://api.newapi.ai` | 网关地址(配置协议 URL 后不再使用) |
128
+ | `flavor` | `newapi` | 网关模板:`newapi` / `litellm` / `higress` / `openai-compatible` / `custom` |
129
+ | `openaiURL` | – | OpenAI 兼容完整端点地址(自定义模板;留空 = 不启用) |
130
+ | `responsesURL` | – | Responses 完整端点地址(自定义模板;留空 = 不启用) |
131
+ | `anthropicURL` | – | Anthropic messages 完整端点地址(自定义模板;留空 = 不启用) |
132
+ | `modelsUrl` | `https://models.dev/models.json` | models.dev 数据源(`file:` URL 可离线) |
133
+ | `useModelsDev` | `true` | 是否用 models.dev 参数增强模型 |
134
+ | `extendedReasoningLevels` | `false` | 未知模型推理等级兜底放宽到 off~max(默认仅 off/low/medium/high) |
135
+ | `sortModelsByRelease` | `true` | 选择器按发布日期新到旧排列(未知日期排最前) |
136
+ | `catalogMode` | `auto` | 模型列表来源:`auto` / `v1` / `management` |
137
+ | `catalogTtlMs` | `1800000` | 模型列表缓存时长 |
138
+ | `includeChatOnly` | `true` | 仅把对话模型放进选择器 |
139
+ | `excludePatterns` | image/speech/embed/… | 选择器剔除的正则列表 |
140
+ | `endpointPriority` | `["openai-response","anthropic","openai","gemini"]` | wire 格式优先级(每模型取首个匹配) |
141
+ | `userId` | `1` | 管理 API 的 `New-Api-User` 头 |
142
+ | `maxTokens` | `32768` | 无 models.dev 数据时的输出上限兜底 |
143
+ | `defaultContextWindow` | `128000` | 无 models.dev 数据时的上下文兜底 |
144
+ | `streamIdleTimeoutMs` | `300000` | 流空闲看门狗 |
145
+ | `retryPolicy` | 标准重试 | 与 `llm-deepseek` 同构 |
146
+
147
+ ## 工作原理
148
+
149
+ 所有 wire 格式细节都委托给
150
+ [`@earendil-works/pi-ai`](https://www.npmjs.com/package/@earendil-works/pi-ai)
151
+ —— 官方 `dsh-llm-pi-ai` 适配器用的同一 SDK。每次请求:
152
+
153
+ ```
154
+ harness GenerateOptions
155
+ → toPiContext() lib/pi-bridge.js harness 消息 → pi-ai Context
156
+ → models.streamSimple() pi-ai SDK 按每个模型的 `api` 分发协议
157
+ → toStreamChunks() lib/pi-bridge.js pi-ai 事件 → harness chunk
158
+ ```
159
+
160
+ 每个发现的模型带一个 `api` 字段,由网关广告的 `supported_endpoint_types`
161
+ 映射而来,遵循 `endpointPriority`;pi-ai provider 收到的是 api *映射表*,
162
+ 所以每个模型路由到自己的协议实现。`sdkBaseURL()` 为 OpenAI 协议模型补
163
+ `/v1`,Anthropic/Google 的基础地址保持不变。
164
+
165
+ ```
166
+ dsh-gateway-provider/
167
+ ├── index.js # 插件入口:Config 校验、provider 注册、设置/凭据接线
168
+ ├── cordis.patch.yml # dsh.bundle 补丁(经 `dsh plugin add` 自动挂载)
169
+ ├── lib/ # adapter、pi-provider、pi-bridge、catalog、modelsdev、thinking、client
170
+ ├── test/ # 冒烟(直连网关)+ 离线单元 + 设置页渲染
171
+ └── scripts/link.sh # 本地检出时链接 profile 的 node_modules
172
+ ```
173
+
174
+ ## 从本地检出开发
175
+
176
+ 1. `bash scripts/link.sh` — 把本包 `node_modules` 软链到 profile 的,
177
+ 保证 `@deepseek-ai/*` 裸导入解析到 harness 进程使用的同一份模块实例
178
+ (单实例 `instanceof` 安全)。
179
+ 2. 把检出注册为 profile 的 link 依赖并安装:
180
+
181
+ ```bash
182
+ cd "$DSH_HOME/profiles/web"
183
+ # package.json dependencies 追加:"dsh-gateway-provider": "link:/本包绝对路径/dsh-gateway-provider"
184
+ # 再把 "dsh-gateway-provider" 加进同一文件的 bundles 列表
185
+ pnpm install
186
+ ```
187
+
188
+ 3. 重启 profile。
189
+
190
+ > ⚠️ **不要**再往 profile 自己的 `cordis.patch.yml` 里插入 `id: llm-newapi`
191
+ > ——bundle 层已挂载,重复会导致启动时报 `duplicate loader entry id`。
192
+
193
+ 客户端 bundle 改动刷新浏览器即生效;宿主侧改动需要重启 profile。
194
+
195
+ ### 测试
196
+
197
+ ```bash
198
+ node test/smoke.mjs # 直连网关:catalog / openai × 2 / 工具调用 / anthropic / gemini / custom-urls
199
+ node test/smoke.mjs --only custom-urls
200
+ node test/protocol-urls.mjs # 离线:URL 派生 + 网关解析单元测试
201
+ node test/client-render.mjs # 离线:设置页渲染树(中/英)
202
+ ```
203
+
204
+ 冒烟测试凭据解析顺序:进程环境变量 → 插件目录 `.env` →
205
+ `$NEWAPI_ENV_FILE`(另有一个作者本地遗留兜底路径)。
206
+
207
+ ## 安全说明
208
+
209
+ - API Key 只经 `$DSH_HOME/.credentials.yaml`(0600)或环境变量解析,
210
+ 绝不写入日志、配置或对话;凭据字段在 Web UI 中以掩码编辑。
211
+ - 无运行时 `dependencies`;`peerDependencies` 复用 harness 已装的
212
+ `@deepseek-ai/*` 与 `@earendil-works/pi-ai`。提交在本地(pre-commit)
213
+ 与 CI 双层接受 gitleaks 扫描把关。
214
+
215
+ ## License
216
+
217
+ MIT
package/package.json CHANGED
@@ -1,7 +1,15 @@
1
1
  {
2
2
  "name": "dsh-gateway-provider",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "description": "Generic LLM gateway model provider plugin for DeepSeek Harness: mounts newapi / LiteLLM / Higress / any OpenAI-compatible gateway as provider routes, auto-discovers the model list, enriches every model with models.dev parameters, and dispatches each model over its native wire protocol (OpenAI / Anthropic / Gemini) via the pi-ai SDK",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "git+https://github.com/Luck9Star/dsh-gateway-provider.git"
8
+ },
9
+ "bugs": {
10
+ "url": "https://github.com/Luck9Star/dsh-gateway-provider/issues"
11
+ },
12
+ "homepage": "https://github.com/Luck9Star/dsh-gateway-provider#readme",
5
13
  "type": "module",
6
14
  "main": "index.js",
7
15
  "exports": {
@@ -12,7 +20,8 @@
12
20
  "files": [
13
21
  "index.js",
14
22
  "lib",
15
- "cordis.patch.yml"
23
+ "cordis.patch.yml",
24
+ "docs"
16
25
  ],
17
26
  "scripts": {
18
27
  "link": "bash scripts/link.sh",