dsh-zcode-startplan 0.5.2 → 1.0.3

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.
Files changed (51) hide show
  1. package/CHANGELOG.md +1963 -672
  2. package/README.md +92 -472
  3. package/bin/cli.mjs +509 -450
  4. package/client.js +2459 -1002
  5. package/cordis.patch.yml +4 -2
  6. package/index.js +109 -11
  7. package/lib/adapter.js +476 -449
  8. package/lib/anthropic-body.js +513 -469
  9. package/lib/assemble.js +808 -542
  10. package/lib/auto-claim.js +229 -0
  11. package/lib/captcha/minter.js +61 -7
  12. package/lib/distribution-keys.js +202 -0
  13. package/lib/errors.js +281 -125
  14. package/lib/fallback.js +120 -0
  15. package/lib/local-endpoint.js +367 -352
  16. package/lib/manage-routes.js +1245 -652
  17. package/lib/model-catalog.js +57 -0
  18. package/lib/openai-gateway.js +776 -0
  19. package/lib/plan-client.js +942 -532
  20. package/lib/pool.js +771 -478
  21. package/lib/sse.js +429 -317
  22. package/lib/upstream-api.js +948 -611
  23. package/lib/upstream.js +294 -134
  24. package/lib/version-probe.js +110 -0
  25. package/package.json +84 -81
  26. package/scripts/analyze-grant-intervals.mjs +171 -0
  27. package/scripts/check-compat-rc3.mjs +21 -11
  28. package/scripts/debug-claimable.mjs +130 -0
  29. package/scripts/probe-marketing.mjs +203 -0
  30. package/scripts/restart-dsh.ps1 +119 -116
  31. package/scripts/test-claimed-pending.mjs +152 -0
  32. package/scripts/verify_compat.mjs +75 -64
  33. package/scripts/watch-marketing.mjs +155 -0
  34. package/tests/auto-claim.test.mjs +318 -0
  35. package/tests/claim-routes.test.mjs +438 -293
  36. package/tests/client-panel.test.mjs +1141 -356
  37. package/tests/distribution-keys.test.mjs +123 -0
  38. package/tests/e2e.test.mjs +388 -353
  39. package/tests/fallback-scrub.test.mjs +105 -0
  40. package/tests/launch-command.test.mjs +68 -0
  41. package/tests/openai-gateway.test.mjs +454 -0
  42. package/tests/pinned-models.test.mjs +505 -497
  43. package/tests/plan-client.test.mjs +582 -222
  44. package/tests/pool.test.mjs +319 -93
  45. package/tests/quota-blackhole.test.mjs +409 -0
  46. package/tests/restart-script.test.mjs +53 -0
  47. package/tests/routing-quota.test.mjs +251 -0
  48. package/tests/support/hanging-stream.mjs +48 -0
  49. package/tests/upstream-error-200.test.mjs +164 -125
  50. package/tests/upstream.test.mjs +330 -250
  51. package/tests/version-probe.test.mjs +172 -0
package/cordis.patch.yml CHANGED
@@ -5,8 +5,10 @@
5
5
  # provider / displayName DSH 里的路由名与展示名(与 llm-pi-ai 的路由不能重名)
6
6
  # models 暴露给 DSH 的模型 id(与上游目录求交集)
7
7
  # accountProviders true = 每个启用账号一个独立 provider 栏位(可在面板关统一栏)
8
- # captcha.debugPort 宿主 Electron 的 CDP 端口(DSH 桌面端需以
9
- # --remote-debugging-port=9229 启动;captcha 用它产出)
8
+ # captcha.debugPort 宿主 Electron 的 CDP 端口。宿主必须**先彻底退出**再用
9
+ # & "D:\software\dsh\DeepSeek Harness.exe" --remote-debugging-port=9229
10
+ # 重开(单实例锁:旧实例在时参数不生效);改端口要同步设
11
+ # ZCODE_START_PLAN_CDP_PORT,面板提示里也有这条命令
10
12
  # captcha.mode cdp = 自动产出;manual = 只认人工粘贴(见 CLI)
11
13
  # endpoint.enabled true 才启动本地 Anthropic 端点(仅 llm-pi-ai 回落路线需要)
12
14
  # statusTool 是否注册 zcode_start_plan_status 工具
package/index.js CHANGED
@@ -76,6 +76,31 @@ const DEFAULT_CONFIG = Object.freeze({
76
76
  * (0.3.0 的 `pinnedAccounts` 同义更名,旧值仍被接受。)
77
77
  */
78
78
  accountProviders: true,
79
+ /**
80
+ * 统一选号的取号顺序(0.7.0):
81
+ * - 'expiry'(默认):**快过期的先用** —— 先吃快过期账号的额度,减少到期作废的浪费;
82
+ * - 'manual':按面板「使用」置顶的顺序(0.6.x 的旧行为)。
83
+ * 两种策略下钉定栏位都固定吃指定账号,不受影响。
84
+ */
85
+ selectionOrder: 'expiry',
86
+ /**
87
+ * 上游**首内容超时**(毫秒,issue #4)。
88
+ *
89
+ * 请求发出后这么久还没见到第一个内容事件 → 判定「上游不再出流」(额度用尽后
90
+ * 本渠道的典型形态:HTTP 200 + 一个永不产出内容的 SSE 流),中止并按 `TIMEOUT`
91
+ * 分类,让宿主/回退链路立刻改道。
92
+ *
93
+ * ⚠ 必须显著小于宿主的单次超时(实测 ~300s),否则宿主的超时先落地,
94
+ * 分类变成笼统的 `PROVIDER_ERROR`,「额度用尽」这个真因被完全掩盖。
95
+ * 默认 90s;设 0 或负数则回落默认值。
96
+ */
97
+ headTimeoutMs: 90_000,
98
+ /**
99
+ * 流内读超时(毫秒):**已经有内容之后**,两个事件之间允许的最大静默。
100
+ *
101
+ * 长思考 / 长工具参数需要宽裕,所以它比 `headTimeoutMs` 大得多(默认 180s)。
102
+ */
103
+ readTimeoutMs: 180_000,
79
104
  /** 上游模型目录缓存时长(毫秒)。 */
80
105
  modelCacheMs: 10 * 60_000,
81
106
  /** 数据目录(空 = ~/.dsh/zcode-start-plan)。 */
@@ -93,7 +118,13 @@ const DEFAULT_CONFIG = Object.freeze({
93
118
  captcha: Object.freeze({
94
119
  /** 'cdp' = 用宿主 Electron 产出(默认);'manual' = 由人工粘贴 param(见 CLI)。 */
95
120
  mode: 'cdp',
96
- /** 宿主调试端口(DSH 桌面端需以 --remote-debugging-port 启动)。 */
121
+ /**
122
+ * 宿主调试端口(captcha 载体用)。
123
+ *
124
+ * 宿主必须显式以 `--remote-debugging-port=<同值>` 启动;DSH 桌面端有
125
+ * **单实例锁**,旧实例还在时再启动只会聚焦旧窗口、参数被丢弃。
126
+ * 改这个值时要同步设环境变量 ZCODE_START_PLAN_CDP_PORT。
127
+ */
97
128
  debugPort: 9229,
98
129
  /** captcha 页面 origin(必须是真实 https origin,否则二次 mint 必失败)。 */
99
130
  origin: 'https://zcode.z.ai/',
@@ -103,6 +134,34 @@ const DEFAULT_CONFIG = Object.freeze({
103
134
  enabled: false,
104
135
  port: 17_899,
105
136
  }),
137
+ /**
138
+ * 本地 **OpenAI 兼容端点**(0.9.0):`/v1/chat/completions` + `/v1/models`。
139
+ *
140
+ * 让任意 OpenAI 兼容客户端(Codex / Cline / Continue / 自写脚本)用
141
+ * `baseURL = http://127.0.0.1:<port>/v1` + 插件生成的 apiKey 消耗账号池额度。
142
+ * 与 `endpoint`(Anthropic 格式)是**并列的两个出口**,可同时开。
143
+ *
144
+ * ⚠ 默认可写:apiKey 持久化在 dataDir 的 `openai-api-key`(形态固定为
145
+ * `randomBytes(32).toString('base64url')`),文件损坏时**报错而不静默换 key**
146
+ * —— 静默换会让所有已配置的客户端突然 401 且用户无从判断。
147
+ */
148
+ openaiGateway: Object.freeze({
149
+ enabled: false,
150
+ port: 17_900,
151
+ }),
152
+ /**
153
+ * 自动领取营销活动(0.6.0)。
154
+ *
155
+ * 开启后定时扫 `/api/v1/marketing/touch`(官方活动发现端点,GET、零 captcha);
156
+ **发现新的 `plan_id` 才 mint captcha 领取**,`0`(成功)/`1003`(已领过)/
157
+ * `1005`(领完)都会记入已见集合并持久化,绝不重复烧 captcha。
158
+ */
159
+ autoClaim: Object.freeze({
160
+ /** 是否开启自动领取。 */
161
+ enabled: false,
162
+ /** 扫描间隔(毫秒);下限 5 分钟(营销投放官方约 10 分钟一刷)。 */
163
+ intervalMs: 30 * 60_000,
164
+ }),
106
165
  /** 是否注册 `zcode_start_plan_status` 工具。 */
107
166
  statusTool: true,
108
167
  })
@@ -119,6 +178,8 @@ export function resolveConfig(raw) {
119
178
  const out = { ...DEFAULT_CONFIG, ...src }
120
179
  out.captcha = { ...DEFAULT_CONFIG.captcha, ...(src.captcha ?? {}) }
121
180
  out.endpoint = { ...DEFAULT_CONFIG.endpoint, ...(src.endpoint ?? {}) }
181
+ out.openaiGateway = { ...DEFAULT_CONFIG.openaiGateway, ...(src.openaiGateway ?? {}) }
182
+ out.autoClaim = { ...DEFAULT_CONFIG.autoClaim, ...(src.autoClaim ?? {}) }
122
183
  if (src.models === undefined || src.models === null) out.models = [...DEFAULT_CONFIG.models]
123
184
  else if (Array.isArray(src.models)) out.models = src.models.length > 0 ? src.models.slice() : [...DEFAULT_CONFIG.models]
124
185
  /** 0.3.0 的 `pinnedAccounts` 同义更名:旧名仍被接受,新名优先。 */
@@ -208,9 +269,22 @@ export function validateConfig(value) {
208
269
  if (typeof out.accountProviders !== 'boolean') {
209
270
  throw new Error('config.accountProviders 必须是布尔值')
210
271
  }
272
+ if (out.selectionOrder !== 'expiry' && out.selectionOrder !== 'manual') {
273
+ throw new Error('config.selectionOrder 只能是 "expiry"(快过期先用)或 "manual"(手动置顶优先)')
274
+ }
211
275
  if (!Number.isInteger(out.modelCacheMs) || out.modelCacheMs < 0) {
212
276
  throw new Error('config.modelCacheMs 必须是非负整数')
213
277
  }
278
+ /**
279
+ * 超时配置(issue #4)。语义:正整数 = 生效;`0` / 负数 / 缺省 = 回落默认值
280
+ * (见 plan-client 的 `positiveOr`)。只拒绝「明显写错」的形态(字符串 / 小数)。
281
+ */
282
+ if (!Number.isInteger(out.headTimeoutMs)) {
283
+ throw new Error('config.headTimeoutMs 必须是整数(毫秒;≤0 表示使用默认 90000)')
284
+ }
285
+ if (!Number.isInteger(out.readTimeoutMs)) {
286
+ throw new Error('config.readTimeoutMs 必须是整数(毫秒;≤0 表示使用默认 180000)')
287
+ }
214
288
  if (!isChannelValue(out.channel)) {
215
289
  throw new Error('config.channel 只能是 "cn"(国内智谱)或 "intl"(国际 Z.AI)')
216
290
  }
@@ -231,22 +305,46 @@ export function validateConfig(value) {
231
305
  throw new Error('config.endpoint.port 必须是 0-65535 的整数')
232
306
  }
233
307
  if (typeof endpoint.enabled !== 'boolean') throw new Error('config.endpoint.enabled 必须是布尔值')
308
+ const openaiGateway = out.openaiGateway
309
+ if (typeof openaiGateway !== 'object' || openaiGateway === null) {
310
+ throw new Error('config.openaiGateway 必须是对象')
311
+ }
312
+ if (!Number.isInteger(openaiGateway.port) || openaiGateway.port < 0 || openaiGateway.port > 65_535) {
313
+ throw new Error('config.openaiGateway.port 必须是 0-65535 的整数')
314
+ }
315
+ if (typeof openaiGateway.enabled !== 'boolean') {
316
+ throw new Error('config.openaiGateway.enabled 必须是布尔值')
317
+ }
318
+ if (typeof out.autoClaim !== 'object' || out.autoClaim === null) throw new Error('config.autoClaim 必须是对象')
319
+ if (typeof out.autoClaim.enabled !== 'boolean') throw new Error('config.autoClaim.enabled 必须是布尔值')
320
+ if (!Number.isInteger(out.autoClaim.intervalMs) || out.autoClaim.intervalMs < 5 * 60_000) {
321
+ throw new Error('config.autoClaim.intervalMs 必须是 ≥ 300000(5 分钟)的整数')
322
+ }
234
323
  if (typeof out.statusTool !== 'boolean') throw new Error('config.statusTool 必须是布尔值')
235
324
 
236
325
  return out
237
326
  }
238
327
 
328
+ /**
329
+ * Config 契约的默认值投影:**直接引用 `DEFAULT_CONFIG`**,同一组默认值只写一份
330
+ * (此前两处手写字面量,改一处漏一处就会「schema 显示的默认」与「运行时默认」漂移)。
331
+ */
239
332
  export const Config = makeConfigSchema(validateConfig, {
240
- provider: PROVIDER,
241
- displayName: 'ZCode Start Plan(赠送免费 plan)',
242
- models: [...DEFAULT_MODELS],
243
- accountProviders: true,
244
- modelCacheMs: 10 * 60_000,
245
- dataDir: '',
246
- channel: 'cn',
247
- captcha: { mode: 'cdp', debugPort: 9229, origin: 'https://zcode.z.ai/' },
248
- endpoint: { enabled: false, port: 17_899 },
249
- statusTool: true,
333
+ provider: DEFAULT_CONFIG.provider,
334
+ displayName: DEFAULT_CONFIG.displayName,
335
+ models: [...DEFAULT_CONFIG.models],
336
+ accountProviders: DEFAULT_CONFIG.accountProviders,
337
+ selectionOrder: DEFAULT_CONFIG.selectionOrder,
338
+ headTimeoutMs: DEFAULT_CONFIG.headTimeoutMs,
339
+ readTimeoutMs: DEFAULT_CONFIG.readTimeoutMs,
340
+ modelCacheMs: DEFAULT_CONFIG.modelCacheMs,
341
+ dataDir: DEFAULT_CONFIG.dataDir,
342
+ channel: DEFAULT_CONFIG.channel,
343
+ captcha: { ...DEFAULT_CONFIG.captcha },
344
+ endpoint: { ...DEFAULT_CONFIG.endpoint },
345
+ openaiGateway: { ...DEFAULT_CONFIG.openaiGateway },
346
+ autoClaim: { ...DEFAULT_CONFIG.autoClaim },
347
+ statusTool: DEFAULT_CONFIG.statusTool,
250
348
  })
251
349
 
252
350
  /**