dsh-opencode-routing 0.0.0-stage → 2.1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 锈者之家
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,123 @@
1
- # Temporary Holding Version
1
+ # dsh-opencode-routing
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ 一个 [DeepSeek Harness](https://github.com/deepseek-ai/deepseek-harness)(`dsh`)插件,把原先分开的两个插件合成一个:
4
+ **OpenCode 账户池 + 额度路由** 和 **用量看板** 现在装在同一个包里,设置里也只有一个页面
5
+ 「OpenCode 账户与用量」。
6
+
7
+ - **客户端**:`settings.section` 槽位,60 秒自动刷新;账户池可增删改、模型表带能力标签、两张表都能按住左右拖动;界面支持 10 种语言(跟随浏览器语言)。
8
+ - **Host**:两个只读/管理接口 —— `GET /opencode-usage.json`(用量,`?refresh=1` 强制刷新)与 `GET|POST /opencode-quota-router.json`(账户池与路由,动作见下)。
9
+
10
+ ## 安装
11
+
12
+ ```bash
13
+ npm i dsh-opencode-routing
14
+ ```
15
+
16
+ 包自带 `cordis.patch.yml`:里面同时声明 `opencode-go` / `opencode-free` 两个 provider 和**一条** insert
17
+ (`id: opencode-router`)。`usage:` 与 `router:` 两个子对象分别对应看板与路由各自的配置项(两边都有 `path`,
18
+ 所以必须分开写)。
19
+
20
+ ```yaml
21
+ - insert:
22
+ - id: opencode-router
23
+ name: 'dsh-opencode-routing'
24
+ config:
25
+ usageUrl: https://opencode.ai/inference/go/v1/usage
26
+ apiKeyRef: OPENCODE_API_KEY
27
+ usage:
28
+ path: /opencode-usage
29
+ plan: go
30
+ router:
31
+ path: /opencode-quota-router
32
+ routes:
33
+ - primary: { provider: opencode-go, model: deepseek-v4.1-flash }
34
+ fallback: { provider: deepseek-account, model: deepseek-flash }
35
+ ```
36
+
37
+ 顶层键对两半都生效(`usageUrl`、`apiKeyRef`、`probeIntervalMs` 之类),子对象里的键只覆盖自己那一半。
38
+
39
+ ## 账户池与轮替
40
+
41
+ 账户顺序就是使用顺序。任何时候只有一个「当前账户」,它的密钥会被镜像到 provider 读取的那个凭据引用
42
+ (默认 `OPENCODE_API_KEY`),因此切换账户不需要改任何 provider 配置:`resolveApiKey` 是在
43
+ `agent/request` 钩子之后、每次流式请求时现取的。
44
+
45
+ - **A 用完自动用 B**:出现 `QUOTA` / `ACCOUNT_QUOTA` / 429 且官方额度确认耗尽,或出现鉴权类错误
46
+ (`AUTH`、`INVALID_CREDENTIAL`、`MISSING_CREDENTIAL`、`ACCOUNT_SIGN_IN_REQUIRED`、`CLIENT_UPGRADE_REQUIRED`)时,
47
+ 当前账户被标记为不可用(额度用尽到窗口重置,鉴权失败 30 分钟),路由切到下一个启用账户并在**同一步重试**,
48
+ 不会把整轮对话降级。
49
+ - **单模型隔离**:只有一个模型报耗尽、池里还有额度时,只隔离这个模型(默认 30 分钟),其它模型继续走套餐。
50
+ - **整池耗尽**:才把这条路整体回退到 `fallback`(默认 DeepSeek 账户),并每 30 分钟(或 rolling 窗口重置时刻)
51
+ 用零 token 的 `GET /inference/go/v1/usage` 探测,恢复后自动回到套餐。
52
+ - **重置**:设置页的「重置封禁标记」清空所有「额度用尽 / 鉴权失败」标记并立刻回到账户池,不动账户与密钥。
53
+
54
+ ## 用量看板
55
+
56
+ 官方 `/inference/go/v1/usage` 只给一组四舍五入的 rolling / weekly / monthly 百分比,**没有按模型明细**;
57
+ 而套餐是按模型各自计额度(5 小时 = 月额度 20%、周 = 50%、月 = 100%)。所以页面有两套数:
58
+
59
+ - **官方百分比**(每个账户一行):来自接口,是账户真实用量,别的客户端、别的机器用了也算进去。
60
+ - **按模型金额**(模型表):本机估算 —— 回扫 `$DSH_HOME/sessions` 近 40 天的会话日志,只统计 provider 以
61
+ `opencode` 开头的 `assistant/message`,按公开单价(含 DeepSeek 峰谷 2×)折算。别的工具用掉的额度没有本地记录。
62
+
63
+ 「查看用量」按钮按账户弹窗展示三个窗口的百分比、重置时间与探测结果(只探测该账户,不切换模型)。
64
+
65
+ 模型表的「能力」列来自同一个模型目录接口(`/inference/go/v1/models`)的
66
+ `modalities.input` 与 `capabilities`,标出 文本 / 图像 / 视频 / 音频 / PDF、工具调用、推理,以及上下文长度;
67
+ 价格为 0 的模型显示「无限」。
68
+
69
+ ### 峰谷与节假日
70
+
71
+ 官方 API 的空闲时段是北京周一至周五 09:00–12:00、14:00–18:00 之外(含午休、夜间、周末、**法定节假日**),
72
+ 价格正好是峰价一半。OpenCode 只照 UTC 周一至周五 01–04、06–10 两个窗口算峰,**不认中国法定节假日**
73
+ (anomalyco/opencode#53071 维护者原话:*"Go pricing follows our own schedule, not DeepSeek's holiday calendar."*)。
74
+
75
+ 插件里带 2026–2031 的中国法定节假日表,只有把 `holidayAware: true` 写进配置(`usage.holidayAware`)才会计入
76
+ 官方侧折算;默认关闭,因为 OpenCode 侧照旧按峰计费。表在 `usage.js` 的 `HOLIDAYS`,`isChinaHoliday()`
77
+ 可单独调用。
78
+
79
+ ## 设置接口
80
+
81
+ `GET /opencode-quota-router.json` 返回 `{mode, reason, active, accounts[], quarantine[], routes[], lastFailure}`,
82
+ 账户里的密钥永远是掩码(`hint`)。`POST` 同一路径,body `{action, ...}`:
83
+
84
+ | action | 参数 | 作用 |
85
+ | --- | --- | --- |
86
+ | `add` | `label?`, `key` | 新建账户(密钥≥8 字符),写入凭据库并探测 |
87
+ | `update` | `id`, `label?` / `enabled?` / `key?` | 改名、停用/启用、换密钥 |
88
+ | `remove` | `id` | 删除账户并清掉它的密钥(至少留一个) |
89
+ | `activate` | `id` | 立刻切到该账户 |
90
+ | `move` | `id`, `direction: up\|down` | 调整顺序 |
91
+ | `probe` | `id?` | 探测全部账户;带 `id` 只探测这一个 |
92
+ | `reset` | — | 清空封禁标记,回到账户池 |
93
+
94
+ `GET /opencode-usage.json` 返回 `{plan, configuredPlan, detectedPlan, holidayAware, peak, usage, accounts[],
95
+ activeAccount, models[], totals}`;`?refresh=1` 先强制刷新官方快照再返回。
96
+
97
+ ## 多语言
98
+
99
+ 界面文案在 `client.js` 的 `DICT` 里:`zh`(源)、`zh-tw`、`en`,以及 `es` / `fr` / `de` / `pt` / `ja` / `ko` / `ru`
100
+ (由 `_merge_i18n.mjs` 从翻译文件内联,缺键回退到 `en`,再回退到键名)。`localeOf()` 按浏览器语言前缀匹配,
101
+ 中文区分简繁。
102
+
103
+ ## 测试
104
+
105
+ ```bash
106
+ node test.mjs
107
+ ```
108
+
109
+ 不需要任何模型额度:假 context、假凭据库、假 webServer,本地起一个 usage 端点按 API key 返回不同百分比,
110
+ 覆盖组合插件挂载、账户收养、单模型隔离、A→B 切换与密钥镜像、按账户探测、整池耗尽回退、重置、设置动作校验、
111
+ 用量视图(能力标签、无限额度、峰谷换算)与单账户退化。
112
+
113
+ ## 与旧包的关系
114
+
115
+ `dsh-opencode-quota-router` 与 `dsh-opencode-usage-settings` 是它的前身,功能已全部并入本包;
116
+ 旧包在 npm 上标记为 deprecated,指向这里。仓库分别是
117
+ [`2869949486/dsh-opencode-routing`](https://github.com/2869949486/dsh-opencode-routing)(当前)、
118
+ [`dsh-opencode-quota-router`](https://github.com/2869949486/dsh-opencode-quota-router)、
119
+ [`dsh-opencode-usage`](https://github.com/2869949486/dsh-opencode-usage)。
120
+
121
+ ## License
122
+
123
+ MIT