dsh-zcode-startplan 0.4.5 → 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.
Files changed (58) hide show
  1. package/CHANGELOG.md +1963 -405
  2. package/README.md +88 -405
  3. package/bin/cli.mjs +509 -450
  4. package/client.js +2459 -716
  5. package/cordis.patch.yml +4 -2
  6. package/index.js +144 -14
  7. package/lib/adapter.js +476 -422
  8. package/lib/anthropic-body.js +513 -469
  9. package/lib/assemble.js +808 -420
  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 -489
  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 -570
  23. package/lib/upstream.js +294 -134
  24. package/lib/version-probe.js +110 -0
  25. package/package.json +84 -80
  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 -76
  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/anthropic-body.test.mjs +305 -0
  35. package/tests/assembler-contract.test.mjs +256 -0
  36. package/tests/auto-claim.test.mjs +318 -0
  37. package/tests/claim-routes.test.mjs +438 -0
  38. package/tests/client-panel.test.mjs +1141 -0
  39. package/tests/distribution-keys.test.mjs +123 -0
  40. package/tests/e2e.test.mjs +388 -0
  41. package/tests/fallback-scrub.test.mjs +105 -0
  42. package/tests/fixtures/quota-200.json +1 -0
  43. package/tests/launch-command.test.mjs +68 -0
  44. package/tests/login-channel.test.mjs +102 -0
  45. package/tests/openai-gateway.test.mjs +454 -0
  46. package/tests/pinned-models.test.mjs +505 -0
  47. package/tests/plan-client.test.mjs +582 -0
  48. package/tests/pool.test.mjs +319 -0
  49. package/tests/quota-blackhole.test.mjs +409 -0
  50. package/tests/restart-script.test.mjs +53 -0
  51. package/tests/routing-quota.test.mjs +251 -0
  52. package/tests/sse.test.mjs +86 -0
  53. package/tests/stream-translator.test.mjs +137 -0
  54. package/tests/support/hanging-stream.mjs +48 -0
  55. package/tests/support/resolve-profile-deps.mjs +52 -0
  56. package/tests/upstream-error-200.test.mjs +164 -0
  57. package/tests/upstream.test.mjs +330 -0
  58. package/tests/version-probe.test.mjs +172 -0
@@ -0,0 +1,57 @@
1
+ /**
2
+ * 模型 id 的**准入校验**(网关与 CLI 共用的唯一判据)。
3
+ *
4
+ * ## 为什么必须有
5
+ *
6
+ * `plan-client.call` 按 `options.model` 选号并直发上游。上游对一个**不存在**的模型
7
+ * 会返回 HTTP 200 + 零内容帧 —— 而 `plan-client` 对「200 但零内容」的处置是把该
8
+ * 账号×模型标记限流到**当日结束**(`markModelRateLimited`)。于是:
9
+ *
10
+ * ```
11
+ * POST /v1/chat/completions {"model": "随便写的"}
12
+ * → 全部启用账号被标记限流到当日 → 整个池子当天不可用
13
+ * ```
14
+ *
15
+ * 一个拼错的模型名、或一次坏客户端重放,就能做掉整个池子。故模型 id 必须**先**在
16
+ * 入口按目录校验,绝不把未识别的 id 送进选号链路。
17
+ *
18
+ * ## 允许的形态
19
+ *
20
+ * - 目录内 id(大小写不敏感 —— 上游目录是唯一的真相);
21
+ * - 钉定后缀形态 `<base>@acct:<accountId>` 中的 `<base>`(0.3.0 兼容);
22
+ * - 目录为空时**放行**(上游不可达且无兜底表:宁可让请求按原样继续,也不能因为
23
+ * 「拿不到目录」把全部请求拒掉)。
24
+ */
25
+
26
+ /** 把模型 id 归一成「要拿去比对的基础 id」(剥离 `@acct:` 钉定后缀)。 */
27
+ export function baseModelId(model) {
28
+ if (typeof model !== 'string') return ''
29
+ const at = model.indexOf('@acct:')
30
+ return (at === -1 ? model : model.slice(0, at)).trim()
31
+ }
32
+
33
+ /**
34
+ * 校验模型 id。
35
+ *
36
+ * @param {string} model 客户端请求的模型 id
37
+ * @param {Array<{ id?: string }>} catalog 当前可用模型目录
38
+ * @returns {{ ok: true, id: string } | { ok: false, reason: string, available: string[] }}
39
+ */
40
+ export function checkModelId(model, catalog) {
41
+ const ids = (Array.isArray(catalog) ? catalog : [])
42
+ .map((entry) => (typeof entry?.id === 'string' ? entry.id : undefined))
43
+ .filter((id) => typeof id === 'string' && id.length > 0)
44
+ const wanted = baseModelId(model)
45
+ if (wanted.length === 0) {
46
+ return { ok: false, reason: '缺少 model 字段(发请求必须指名模型)', available: ids }
47
+ }
48
+ if (ids.length === 0) return { ok: true, id: wanted }
49
+ const lower = wanted.toLowerCase()
50
+ const hit = ids.find((id) => id.toLowerCase() === lower)
51
+ if (hit !== undefined) return { ok: true, id: hit }
52
+ return {
53
+ ok: false,
54
+ reason: `未知模型「${wanted}」:为避免把全部账号误标限流到当日,只接受目录内的模型`,
55
+ available: ids,
56
+ }
57
+ }