dsh-router-core 0.3.21 → 0.4.1
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 +238 -63
- package/cordis.patch.yml +22 -0
- package/docs/ext.md +28 -5
- package/docs/pool-sticky-block.md +158 -0
- package/docs/suppliers.md +28 -5
- package/lib/client-registry.js +1300 -549
- package/lib/client.js +1300 -549
- package/lib/ext-test/index.js +47 -0
- package/lib/ext-test/locale/en.json +6 -0
- package/lib/ext-test/locale/zh.json +6 -0
- package/lib/ext-test/package.json +5 -0
- package/lib/http-state-C1vb3gW2.js +48 -0
- package/lib/index.js +1169 -254
- package/lib/suppliers/{nvidia.js → nvidia/index.js} +14 -6
- package/lib/suppliers/nvidia/locale/en.json +6 -0
- package/lib/suppliers/nvidia/locale/zh.json +6 -0
- package/lib/suppliers/nvidia/package.json +5 -0
- package/lib/suppliers/{opencode.js → opencode/index.js} +14 -6
- package/lib/suppliers/opencode/locale/en.json +6 -0
- package/lib/suppliers/opencode/locale/zh.json +6 -0
- package/lib/suppliers/opencode/package.json +5 -0
- package/lib/suppliers/{openrouter.js → openrouter/index.js} +14 -6
- package/lib/suppliers/openrouter/locale/en.json +6 -0
- package/lib/suppliers/openrouter/locale/zh.json +6 -0
- package/lib/suppliers/openrouter/package.json +5 -0
- package/lib/types/ext/contract.d.ts +9 -7
- package/lib/types/ext-test/index.d.ts +21 -0
- package/lib/types/ext-test/plugin.d.ts +23 -0
- package/lib/types/host-version.d.ts +15 -0
- package/lib/types/index.d.ts +6 -0
- package/lib/types/llm/adapter.d.ts +35 -4
- package/lib/types/router/account-pool.d.ts +53 -18
- package/lib/types/router/index.d.ts +65 -15
- package/lib/types/router/prefix-affinity.d.ts +18 -1
- package/lib/types/router/types.d.ts +7 -4
- package/lib/types/router/usage-store.d.ts +12 -0
- package/lib/types/shared.d.ts +25 -21
- package/lib/types/supplier-config.d.ts +15 -3
- package/lib/types/suppliers/contract.d.ts +37 -4
- package/lib/types/suppliers/http-state.d.ts +23 -0
- package/lib/types/suppliers/loader.d.ts +6 -4
- package/lib/types/suppliers/nvidia/index.d.ts +18 -0
- package/lib/types/suppliers/nvidia/plugin.d.ts +13 -0
- package/lib/types/suppliers/opencode/index.d.ts +18 -0
- package/lib/types/suppliers/opencode/plugin.d.ts +7 -0
- package/lib/types/suppliers/openrouter/index.d.ts +18 -0
- package/lib/types/suppliers/openrouter/plugin.d.ts +11 -0
- package/lib/types/suppliers/probe.d.ts +160 -0
- package/lib/types/suppliers/registry.d.ts +3 -2
- package/package.json +50 -16
- package/src/client/CombosTab.tsx +8 -53
- package/src/client/ExtDetail.tsx +32 -196
- package/src/client/ExtTab.tsx +25 -42
- package/src/client/ExtTestPanel.tsx +392 -0
- package/src/client/LastHitDock.tsx +141 -0
- package/src/client/RouterComponentsSection.tsx +241 -0
- package/src/client/RouterView.tsx +12 -5
- package/src/client/StatsTab.tsx +8 -6
- package/src/client/SupplierDetail.tsx +2 -34
- package/src/client/comp-button-scope.test.ts +58 -0
- package/src/client/comp-panel-layout.test.ts +58 -0
- package/src/client/env.d.ts +38 -1
- package/src/client/ext-panels.test.ts +44 -0
- package/src/client/ext-panels.ts +49 -0
- package/src/client/index.tsx +86 -0
- package/src/client/last-hit-store.ts +89 -0
- package/src/client/model-choice.test.ts +41 -0
- package/src/client/model-choice.ts +37 -0
- package/src/client/model-hint-copy.test.ts +7 -2
- package/src/client/model-hint-copy.ts +2 -2
- package/src/client/router-components.test.ts +114 -0
- package/src/client/router-components.ts +104 -0
- package/src/client/router.css +2592 -1635
- package/src/client/supplier-visibility.test.ts +57 -0
- package/src/client/supplier-visibility.ts +63 -0
- package/src/ext/contract.ts +9 -7
- package/src/ext-test/ext-test.test.ts +60 -0
- package/src/ext-test/index.ts +35 -0
- package/src/ext-test/locale/en.json +6 -0
- package/src/ext-test/locale/zh.json +6 -0
- package/src/ext-test/plugin.ts +34 -0
- package/src/ext-test/row.json +5 -0
- package/src/host-version.test.ts +61 -0
- package/src/host-version.ts +101 -0
- package/src/index.ts +165 -68
- package/src/llm/adapter.test.ts +175 -4
- package/src/llm/adapter.ts +109 -16
- package/src/router/account-pool.test.ts +189 -83
- package/src/router/account-pool.ts +120 -31
- package/src/router/disconnect-leak.test.ts +7 -12
- package/src/router/first-byte-budget.test.ts +160 -0
- package/src/router/index.ts +276 -62
- package/src/router/models-cache.test.ts +54 -9
- package/src/router/precise-dispatch.test.ts +6 -6
- package/src/router/prefix-affinity.ts +22 -1
- package/src/router/supplier-switch.test.ts +220 -0
- package/src/router/types.ts +7 -5
- package/src/router/usage-store.ts +16 -0
- package/src/router/usage.test.ts +1 -1
- package/src/shared.ts +25 -24
- package/src/supplier-config.ts +33 -13
- package/src/suppliers/contract.ts +56 -4
- package/src/suppliers/loader.test.ts +38 -4
- package/src/suppliers/loader.ts +33 -18
- package/src/suppliers/nvidia/index.ts +27 -0
- package/src/suppliers/nvidia/locale/en.json +6 -0
- package/src/suppliers/nvidia/locale/zh.json +6 -0
- package/src/suppliers/nvidia/row.json +5 -0
- package/src/suppliers/opencode/index.ts +27 -0
- package/src/suppliers/opencode/locale/en.json +6 -0
- package/src/suppliers/opencode/locale/zh.json +6 -0
- package/src/suppliers/opencode/row.json +5 -0
- package/src/suppliers/openrouter/index.ts +27 -0
- package/src/suppliers/openrouter/locale/en.json +6 -0
- package/src/suppliers/openrouter/locale/zh.json +6 -0
- package/src/suppliers/openrouter/row.json +5 -0
- package/src/suppliers/probe.test.ts +266 -0
- package/src/suppliers/probe.ts +475 -0
- package/src/suppliers/registry.ts +103 -23
- package/src/types.d.ts +2 -1
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
//#region src/ext/contract.ts
|
|
2
|
+
/** 读取当前 router.ext 表(同一 live 对象,可追加)——扩展插件用它注册自己。 */
|
|
3
|
+
function currentExts(ctx) {
|
|
4
|
+
const c = ctx;
|
|
5
|
+
return c.get?.("router.ext") ?? c.router?.ext;
|
|
6
|
+
}
|
|
7
|
+
/** 读取扩展存储服务——扩展插件用它读开关、读写自己存的数据。 */
|
|
8
|
+
function currentExtStore(ctx) {
|
|
9
|
+
const c = ctx;
|
|
10
|
+
return c.get?.("router.extStore") ?? c.router?.extStore;
|
|
11
|
+
}
|
|
12
|
+
//#endregion
|
|
13
|
+
//#region src/ext-test/plugin.ts
|
|
14
|
+
/** 扩展开关表里的注册键(也是 URL 面板读它的 id)。 */
|
|
15
|
+
const EXT_TEST_ID = "test";
|
|
16
|
+
/**
|
|
17
|
+
* 构造这个扩展器。
|
|
18
|
+
*
|
|
19
|
+
* `getState()` 恒为就绪:自检能力不依赖外部二进制(不像 rtk 需要本机装 rtk),
|
|
20
|
+
* 「能不能测」取决于用户选的供应商/模型/连接,不该由扩展提前否决 —— 真跑一次
|
|
21
|
+
* 才知道,那时错误信息比一个「未就绪」有用得多。
|
|
22
|
+
*/
|
|
23
|
+
function createTestExt() {
|
|
24
|
+
return {
|
|
25
|
+
id: EXT_TEST_ID,
|
|
26
|
+
name: "插件自检",
|
|
27
|
+
description: "选供应商、模型与连接跑一次真实访问测试;或对外部供应商插件做契约体检,逐个成员报告实现与可用情况",
|
|
28
|
+
source: "builtin",
|
|
29
|
+
getState: () => ({ ready: true })
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
//#endregion
|
|
33
|
+
//#region src/ext-test/index.ts
|
|
34
|
+
/** cordis 插件身份(配置树里这一行的技术名;显示名走同目录的 locale)。 */
|
|
35
|
+
const name = "dsh-router-ext-test";
|
|
36
|
+
/** 登记本扩展。幂等:同一个 id 已在表里就直接返回,不顶掉别人的登记。 */
|
|
37
|
+
function apply(ctx) {
|
|
38
|
+
ctx.inject(["router.ext"], (sctx) => {
|
|
39
|
+
const table = currentExts(sctx);
|
|
40
|
+
if (table === void 0) return;
|
|
41
|
+
if (table["test"] !== void 0) return;
|
|
42
|
+
table[EXT_TEST_ID] = createTestExt();
|
|
43
|
+
currentExtStore(sctx)?.setEnabled(EXT_TEST_ID, true);
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
//#endregion
|
|
47
|
+
export { apply, name };
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
//#region src/suppliers/contract.ts
|
|
2
|
+
/**
|
|
3
|
+
* 读当前 `router.suppliers` 表(同一 live 对象,可追加)——供应商行用它登记自己。
|
|
4
|
+
*
|
|
5
|
+
* 读法与 `ext/contract.ts` 的 `currentExts` 同一套(cordis 的 ctx 是 Proxy,
|
|
6
|
+
* 未声明的服务直接 get 会抛,所以走 `get()` 兜底)。
|
|
7
|
+
*/
|
|
8
|
+
function currentSuppliers(ctx) {
|
|
9
|
+
const c = ctx;
|
|
10
|
+
return c.get?.("router.suppliers") ?? c.router?.suppliers;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* 把一个供应商工厂登记进 `router.suppliers`,并通知核心去装载它。
|
|
14
|
+
*
|
|
15
|
+
* 为什么自己 emit `internal/service`:核心靠这个事件发现「表里多了供应商」
|
|
16
|
+
* (同 `router.ext` 的做法)。**幂等** —— 同一个 id 已经在表里就直接返回,
|
|
17
|
+
* 不会把别人的登记顶掉(内置行与用户目录 js 同名时由核心按优先级裁决,
|
|
18
|
+
* 这里不越权覆盖)。
|
|
19
|
+
*/
|
|
20
|
+
function registerSupplierRow(ctx, id, factory) {
|
|
21
|
+
ctx.inject(["router.suppliers"], (sctx) => {
|
|
22
|
+
const table = currentSuppliers(sctx);
|
|
23
|
+
if (table === void 0) return;
|
|
24
|
+
if (table[id] !== void 0) return;
|
|
25
|
+
table[id] = factory;
|
|
26
|
+
ctx.emit("internal/service", "router.suppliers", table);
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
//#endregion
|
|
30
|
+
//#region src/suppliers/http-state.ts
|
|
31
|
+
/**
|
|
32
|
+
* 非 2xx 上游响应 → 语义状态。
|
|
33
|
+
*
|
|
34
|
+
* 4xx 里除了「限流/凭证/不存在」这三种明确归属账号的,剩下的都是**请求本身
|
|
35
|
+
* 有问题**(400 参数错、413 太大、422 语义错…)——同一个请求对池里每个号结果
|
|
36
|
+
* 都相同,归 `bad_request`(核心不惩罚账号,直接换下一个模型)。
|
|
37
|
+
* 5xx / 说不清的仍归 `unknown`(上游故障侧,瞬冷换号是对的)。
|
|
38
|
+
*/
|
|
39
|
+
function stateFromHttpStatus(status) {
|
|
40
|
+
if (status === 429) return "rate_limit";
|
|
41
|
+
if (status === 401 || status === 403) return "session_dead";
|
|
42
|
+
if (status === 404) return "unavailable";
|
|
43
|
+
if (status === 402) return "quota";
|
|
44
|
+
if (status >= 400 && status < 500) return "bad_request";
|
|
45
|
+
return "unknown";
|
|
46
|
+
}
|
|
47
|
+
//#endregion
|
|
48
|
+
export { registerSupplierRow as n, stateFromHttpStatus as t };
|