dsh-router-core 0.4.1 → 0.4.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.
- package/README.md +18 -15
- package/cordis.patch.yml +10 -0
- package/docs/ext.md +65 -3
- package/lib/client-registry.js +633 -50
- package/lib/client.js +633 -50
- package/lib/contract-D4BWZ7Xe.js +13 -0
- package/lib/ext-prompt/index.js +527 -0
- package/lib/ext-prompt/locale/en.json +6 -0
- package/lib/ext-prompt/locale/zh.json +6 -0
- package/lib/ext-prompt/package.json +5 -0
- package/lib/ext-test/index.js +1 -12
- package/lib/index.js +298 -119
- package/lib/types/ext/contract.d.ts +113 -0
- package/lib/types/ext-prompt/content.d.ts +57 -0
- package/lib/types/ext-prompt/index.d.ts +53 -0
- package/lib/types/ext-prompt/logo.d.ts +17 -0
- package/lib/types/ext-prompt/mount.d.ts +51 -0
- package/lib/types/ext-prompt/plugin.d.ts +32 -0
- package/lib/types/ext-prompt/render.d.ts +105 -0
- package/lib/types/host-base-url.d.ts +31 -0
- package/lib/types/host-version.d.ts +7 -8
- package/lib/types/index.d.ts +1 -1
- package/lib/types/llm/adapter.d.ts +6 -1
- package/lib/types/shared.d.ts +33 -0
- package/package.json +17 -7
- package/src/client/EndpointTab.tsx +15 -18
- package/src/client/ExtDetail.tsx +552 -9
- package/src/client/index.tsx +7 -28
- package/src/client/router.css +132 -62
- package/src/ext/contract.ts +111 -0
- package/src/ext-prompt/assembly.test.ts +79 -0
- package/src/ext-prompt/content.ts +174 -0
- package/src/ext-prompt/ext-prompt.test.ts +432 -0
- package/src/ext-prompt/index.ts +104 -0
- package/src/ext-prompt/locale/en.json +6 -0
- package/src/ext-prompt/locale/zh.json +6 -0
- package/src/ext-prompt/logo.svg +14 -0
- package/src/ext-prompt/logo.ts +18 -0
- package/src/ext-prompt/mount.test.ts +95 -0
- package/src/ext-prompt/mount.ts +79 -0
- package/src/ext-prompt/plugin.ts +223 -0
- package/src/ext-prompt/render.test.ts +256 -0
- package/src/ext-prompt/render.ts +216 -0
- package/src/ext-prompt/row.json +5 -0
- package/src/ext-route.test.ts +78 -0
- package/src/host-base-url.test.ts +129 -0
- package/src/host-base-url.ts +43 -0
- package/src/host-version.test.ts +21 -28
- package/src/host-version.ts +39 -39
- package/src/index.ts +230 -96
- package/src/llm/adapter.test.ts +19 -42
- package/src/llm/adapter.ts +32 -50
- package/src/shared.ts +34 -0
package/lib/index.js
CHANGED
|
@@ -2687,11 +2687,10 @@ function projectImageDimensions(width, height, maxPixels) {
|
|
|
2687
2687
|
* base64)。图片字节经 `resolveAttachments` 读取;读不到时回退为稳定的占位文本,
|
|
2688
2688
|
* **绝不静默丢图**(丢图 = 模型看到一条没有图的消息却毫无提示)。
|
|
2689
2689
|
*
|
|
2690
|
-
*
|
|
2691
|
-
*
|
|
2692
|
-
* 两者在 wire 上统一序列化为 role:'tool',产物一致。见主循环里的 role==='tool' 分支。
|
|
2690
|
+
* 工具结果只有一种形态:独立 `role:'tool'` 消息、toolCallId 挂消息级,wire 上原样
|
|
2691
|
+
* 发 `role:'tool'`。见主循环里的 `role === 'tool'` 分支。
|
|
2693
2692
|
*/
|
|
2694
|
-
/** tool
|
|
2693
|
+
/** tool 消息里的图片挂到 user 消息时用的说明文字(与官方适配器一致)。 */
|
|
2695
2694
|
const TOOL_RESULT_IMAGE_TEXT = "Tool result images";
|
|
2696
2695
|
/**
|
|
2697
2696
|
* 流空闲超时:上游连上后这么久没吐**任何**字节就判失败(抛 TIMEOUT,可重试)。
|
|
@@ -2706,7 +2705,7 @@ const STREAM_IDLE_TIMEOUT_MS = 3e5;
|
|
|
2706
2705
|
async function wireMessages(options, system, attachments, log) {
|
|
2707
2706
|
const out = [];
|
|
2708
2707
|
/**
|
|
2709
|
-
* 攒着「tool
|
|
2708
|
+
* 攒着「tool 消息里带出来的图片」,等这一串 tool 消息**发完**再合并成一条
|
|
2710
2709
|
* user 消息补在后面。
|
|
2711
2710
|
*
|
|
2712
2711
|
* 为什么必须跨消息攒、而不是各消息各发一条:harness 把**每个 tool 结果放
|
|
@@ -2774,26 +2773,12 @@ async function wireMessages(options, system, attachments, log) {
|
|
|
2774
2773
|
pendingToolImages.push(...imageParts);
|
|
2775
2774
|
continue;
|
|
2776
2775
|
}
|
|
2777
|
-
const
|
|
2778
|
-
|
|
2779
|
-
|
|
2780
|
-
|
|
2781
|
-
|
|
2782
|
-
|
|
2783
|
-
content: compactUserContent(ownParts)
|
|
2784
|
-
});
|
|
2785
|
-
}
|
|
2786
|
-
for (const result of toolResults) {
|
|
2787
|
-
const parts = await contentParts(result.content, attachments, options.signal, log);
|
|
2788
|
-
const imageParts = parts.filter((p) => p.type !== "text");
|
|
2789
|
-
const text = parts.filter((p) => p.type === "text").map((p) => p.text ?? "").join("");
|
|
2790
|
-
out.push({
|
|
2791
|
-
role: "tool",
|
|
2792
|
-
tool_call_id: result.toolCallId,
|
|
2793
|
-
content: text
|
|
2794
|
-
});
|
|
2795
|
-
pendingToolImages.push(...imageParts);
|
|
2796
|
-
}
|
|
2776
|
+
const userParts = await contentParts(message.content, attachments, options.signal, log);
|
|
2777
|
+
flushToolImages();
|
|
2778
|
+
out.push({
|
|
2779
|
+
role: "user",
|
|
2780
|
+
content: compactUserContent(userParts)
|
|
2781
|
+
});
|
|
2797
2782
|
}
|
|
2798
2783
|
flushToolImages();
|
|
2799
2784
|
return out;
|
|
@@ -2811,7 +2796,7 @@ function compactUserContent(parts) {
|
|
|
2811
2796
|
return texts.join("");
|
|
2812
2797
|
}
|
|
2813
2798
|
/**
|
|
2814
|
-
*
|
|
2799
|
+
* 一条 DSH 消息的内容块序列化成 OpenAI content parts(text + image_url)。
|
|
2815
2800
|
* 纯文本时返回空数组(上层走紧凑字符串路径)。图片转 base64 data URI;
|
|
2816
2801
|
* attachments 缺失或读取失败时回退稳定占位文本(绝不静默丢图)。
|
|
2817
2802
|
*/
|
|
@@ -2831,7 +2816,6 @@ async function contentParts(blocks, attachments, signal, log) {
|
|
|
2831
2816
|
parts.push(...await imageParts(ref, attachments, signal, log));
|
|
2832
2817
|
continue;
|
|
2833
2818
|
}
|
|
2834
|
-
if (block.type === "tool-result") parts.push(...await contentParts(block.content, attachments, signal, log));
|
|
2835
2819
|
}
|
|
2836
2820
|
return parts;
|
|
2837
2821
|
}
|
|
@@ -3160,6 +3144,11 @@ const ROUTER_REASONING_EFFORTS = [
|
|
|
3160
3144
|
}
|
|
3161
3145
|
];
|
|
3162
3146
|
var RouterAdapter = class RouterAdapter extends LlmAdapter {
|
|
3147
|
+
/**
|
|
3148
|
+
* 网关端点。**函数形态 = 每次请求现算**:端口要等宿主 listen 完才有值的场景
|
|
3149
|
+
* 必须传函数(写死 3080 在桌面端等非 3080 宿主上全线 `fetch failed`,
|
|
3150
|
+
* issue #9),见 `host-base-url.ts`。
|
|
3151
|
+
*/
|
|
3163
3152
|
baseURL;
|
|
3164
3153
|
source;
|
|
3165
3154
|
resolveAttachments;
|
|
@@ -3232,7 +3221,8 @@ var RouterAdapter = class RouterAdapter extends LlmAdapter {
|
|
|
3232
3221
|
options.signal?.addEventListener("abort", onAbort, { once: true });
|
|
3233
3222
|
let resp;
|
|
3234
3223
|
try {
|
|
3235
|
-
|
|
3224
|
+
const base = typeof this.baseURL === "function" ? this.baseURL() : this.baseURL;
|
|
3225
|
+
resp = await fetch(`${base}/chat/completions`, {
|
|
3236
3226
|
method: "POST",
|
|
3237
3227
|
headers: {
|
|
3238
3228
|
...attributionHeaders(),
|
|
@@ -4565,39 +4555,89 @@ function dshHome() {
|
|
|
4565
4555
|
return resolve(value);
|
|
4566
4556
|
}
|
|
4567
4557
|
//#endregion
|
|
4558
|
+
//#region src/host-base-url.ts
|
|
4559
|
+
/**
|
|
4560
|
+
* 网关端点地址(`http://127.0.0.1:<宿主端口>/v1`)的解析。
|
|
4561
|
+
*
|
|
4562
|
+
* ## 为什么端口不能写死
|
|
4563
|
+
*
|
|
4564
|
+
* dsh-router **自己不开端口**:`/v1/*` 是注册在**宿主** web server 上的路由
|
|
4565
|
+
* (见 `index.ts` 的 `ctx.webServer.register`)。所以端点地址里的端口由启动通道
|
|
4566
|
+
* 决定,而不是插件决定 —— `dsh web` 默认 3080,桌面端官方通道、以及配置
|
|
4567
|
+
* `port: 0`(让 OS 分配)时都**不是** 3080。之前 adapter 把
|
|
4568
|
+
* `http://localhost:3080/v1` 写死在构造参数里,于是非 3080 的宿主上 Router
|
|
4569
|
+
* 的每一轮对话都是 `dsh-router upstream call failed: fetch failed`(issue #9)。
|
|
4570
|
+
*
|
|
4571
|
+
* ## 为什么返回函数而不是字符串
|
|
4572
|
+
*
|
|
4573
|
+
* `webServer.port` 是**监听之后**才有值的 getter(listen 前是 `undefined`),
|
|
4574
|
+
* 且宿主重启/换端口后它会变。所以这里返回函数、**每次请求现算**,不在注册时
|
|
4575
|
+
* 算一次 —— 注册发生在启动早期,那时算只会把「拿不到端口」固化成常量。
|
|
4576
|
+
*
|
|
4577
|
+
* ## 拿不到端口时为什么抛,而不是回退 3080
|
|
4578
|
+
*
|
|
4579
|
+
* 回退等于把同一个 bug 换种写法:端口读不到时宿主的路由也没在服务,回退到 3080
|
|
4580
|
+
* 只会重新得到一句无从查证的 `fetch failed`。这里抛明确的话,让它出现在
|
|
4581
|
+
* `LlmError` 消息里(adapter 在 fetch 同一个 try 里调它),报错自带原因。
|
|
4582
|
+
*/
|
|
4583
|
+
/** 回环地址:宿主即使监听 `0.0.0.0`(LAN 模式),走 127.0.0.1 也通。 */
|
|
4584
|
+
const LOOPBACK_HOST = "127.0.0.1";
|
|
4585
|
+
/**
|
|
4586
|
+
* 造一个「每次调用现读宿主端口」的端点解析器。
|
|
4587
|
+
*
|
|
4588
|
+
* @param readPort 读宿主监听端口(`webServer.port`)。取不到时抛错而不是猜。
|
|
4589
|
+
* @param path 端点路径前缀,默认 `/v1`。
|
|
4590
|
+
*/
|
|
4591
|
+
function loopbackBaseURL(readPort, path = "/v1") {
|
|
4592
|
+
return () => {
|
|
4593
|
+
const port = readPort();
|
|
4594
|
+
if (port === void 0) throw new Error("dsh-router: 宿主 webServer 端口未就绪(webServer.port 读不到),无法定位 /v1 端点");
|
|
4595
|
+
return `http://${LOOPBACK_HOST}:${String(port)}${path}`;
|
|
4596
|
+
};
|
|
4597
|
+
}
|
|
4598
|
+
//#endregion
|
|
4568
4599
|
//#region src/host-version.ts
|
|
4569
4600
|
/**
|
|
4570
|
-
* 宿主 DSH 版本探测 ——
|
|
4601
|
+
* 宿主 DSH 版本探测 —— 现在只剩两件事:**报版本**(日志 + `/router/api/health`)与
|
|
4602
|
+
* **守版本地板**。
|
|
4603
|
+
*
|
|
4604
|
+
* ## 为什么不再有兼容分叉
|
|
4605
|
+
*
|
|
4606
|
+
* 原来这里有三处 `>= 0.1.7` 的分叉(设置卡片注册路径、「最近命中」徽章座位、adapter
|
|
4607
|
+
* 的工具结果消息形态),彼此独立判断、还容易误判。本插件现已按 **0.2.0 单面**编写,
|
|
4608
|
+
* 三处的旧分支全部删除:
|
|
4571
4609
|
*
|
|
4572
|
-
*
|
|
4573
|
-
*
|
|
4574
|
-
*
|
|
4575
|
-
*
|
|
4610
|
+
* 1. 设置卡片:0.1.5 走 `settings.installSection`,0.1.7+ 走导出的 volatile Config。
|
|
4611
|
+
* 0.2.0 只有后者 → installSection 分支删除。
|
|
4612
|
+
* 2. 「最近命中」徽章:0.1.5 渲染在 InputBar 之后的独立块(位置不对),0.1.7+ 才在
|
|
4613
|
+
* composer 底部操作行。0.2.0 起无条件挂载,不再问支持位。
|
|
4614
|
+
* 3. adapter 工具结果形态:旧的「user 内嵌 tool-result 块」0.1.7 起退役;0.2.0 的
|
|
4615
|
+
* v3→v4 会话迁移还主动拒绝该包装,所以它到不了 adapter → 分支删除。
|
|
4576
4616
|
*
|
|
4577
|
-
*
|
|
4578
|
-
*
|
|
4579
|
-
*
|
|
4580
|
-
*
|
|
4581
|
-
*
|
|
4617
|
+
* ## 为什么仍要读版本
|
|
4618
|
+
*
|
|
4619
|
+
* 因为**跑在老宿主上必须出声**。删掉分叉不等于「老宿主也能跑」:0.1.x 上本插件会
|
|
4620
|
+
* 静默走错路径(卡片不出、徽章错位、工具结果丢失),而用户看到的现象与「插件坏了」
|
|
4621
|
+
* 无法区分。所以这里只保留一个判据 `isHostSupported`(>= 0.2.0),仅用于 apply 时
|
|
4622
|
+
* 打一条醒目的警告 —— 不是兼容,是可诊断。
|
|
4582
4623
|
*
|
|
4583
4624
|
* 读法:宿主把 profile 目录挂在 `ctx.baseUrl`(见 data-dir.ts)。本插件跑在宿主
|
|
4584
4625
|
* 进程内,`require.resolve('@deepseek-ai/dsh/package.json', { paths: [profile] })`
|
|
4585
|
-
* 会沿 profile 的 node_modules
|
|
4586
|
-
*
|
|
4626
|
+
* 会沿 profile 的 node_modules 向上解析到全局安装根。拿不到就回落直接拼路径,再不行
|
|
4627
|
+
* 返回 undefined(按「无法确认」处理,不抛)。
|
|
4587
4628
|
*
|
|
4588
|
-
* 注意:**只能在 `apply(ctx)` 里用**(那时才有 baseUrl
|
|
4589
|
-
*
|
|
4629
|
+
* 注意:**只能在 `apply(ctx)` 里用**(那时才有 baseUrl)。模块顶层解析到的是插件自己
|
|
4630
|
+
* 的依赖,不能代表宿主。
|
|
4590
4631
|
*/
|
|
4591
|
-
/**
|
|
4592
|
-
|
|
4593
|
-
const V0_1_7 = [
|
|
4632
|
+
/** 版本地板:低于此版本的宿主本插件不再兼容(0.1.5/0.1.6/0.1.7 全部落此下)。 */
|
|
4633
|
+
const V0_2_0 = [
|
|
4594
4634
|
0,
|
|
4595
|
-
|
|
4596
|
-
|
|
4635
|
+
2,
|
|
4636
|
+
0
|
|
4597
4637
|
];
|
|
4598
4638
|
/**
|
|
4599
4639
|
* 解析并比较 semver(只取前导数字段,预发布标识忽略)。
|
|
4600
|
-
* `0.
|
|
4640
|
+
* `0.2.0-rc.1` → [0,2,0],`0.1.7-alpha.2` → [0,1,7]。
|
|
4601
4641
|
* 返回负数/0/正数,语义同 `<`/`==`/`>`。
|
|
4602
4642
|
*/
|
|
4603
4643
|
function compareVersion(a, b) {
|
|
@@ -4608,7 +4648,7 @@ function compareVersion(a, b) {
|
|
|
4608
4648
|
}
|
|
4609
4649
|
return 0;
|
|
4610
4650
|
}
|
|
4611
|
-
/** `0.
|
|
4651
|
+
/** `0.2.0-rc.1` → [0,2,0];解析失败返回 null。 */
|
|
4612
4652
|
function parseVersion(raw) {
|
|
4613
4653
|
const m = /^(\d+)(?:\.(\d+))?(?:\.(\d+))?/.exec(raw.trim());
|
|
4614
4654
|
if (m === null) return null;
|
|
@@ -4621,7 +4661,7 @@ function parseVersion(raw) {
|
|
|
4621
4661
|
/**
|
|
4622
4662
|
* 探测 profile 里安装的 DSH 宿主版本。
|
|
4623
4663
|
* @param baseUrl 宿主注入的 profile 锚点(`ctx.baseUrl`)
|
|
4624
|
-
* @returns 形如 `0.
|
|
4664
|
+
* @returns 形如 `0.2.0-rc.1`;读不到返回 undefined
|
|
4625
4665
|
*/
|
|
4626
4666
|
function detectHostVersion(baseUrl) {
|
|
4627
4667
|
const profile = profileDirOf(baseUrl);
|
|
@@ -4635,26 +4675,19 @@ function detectHostVersion(baseUrl) {
|
|
|
4635
4675
|
if (typeof parsed.version === "string" && parsed.version !== "") return parsed.version;
|
|
4636
4676
|
} catch {}
|
|
4637
4677
|
}
|
|
4638
|
-
/**
|
|
4639
|
-
|
|
4678
|
+
/**
|
|
4679
|
+
* 宿主是否在本插件支持的地板之上(>= 0.2.0)。
|
|
4680
|
+
*
|
|
4681
|
+
* **只用于报警,不用于分流** —— 低于地板时插件照常加载,但要在日志里说清「这台宿主
|
|
4682
|
+
* 不再兼容、会出现什么症状」,把静默的错变成可查的错。读不到版本 → false(当作
|
|
4683
|
+
* 「无法确认」,同样值得说一句)。
|
|
4684
|
+
*/
|
|
4685
|
+
function isHostSupported(baseUrl) {
|
|
4640
4686
|
const v = detectHostVersion(baseUrl);
|
|
4641
4687
|
if (v === void 0) return false;
|
|
4642
4688
|
const parsed = parseVersion(v);
|
|
4643
4689
|
if (parsed === null) return false;
|
|
4644
|
-
return compareVersion(parsed,
|
|
4645
|
-
}
|
|
4646
|
-
/**
|
|
4647
|
-
* 宿主是否 >= 0.1.7。三处兼容分叉共用这一个判据:
|
|
4648
|
-
* - 「最近命中」徽章(composer.dock 位置正确)
|
|
4649
|
-
* - 设置命名空间(Config 驱动而非 installSection)
|
|
4650
|
-
* - adapter 的独立 role:'tool' 消息形态
|
|
4651
|
-
*/
|
|
4652
|
-
function isHost017Plus(baseUrl) {
|
|
4653
|
-
return atLeast(baseUrl, V0_1_7);
|
|
4654
|
-
}
|
|
4655
|
-
/** 「最近命中」徽章是否可用(= 宿主 >= 0.1.7)。 */
|
|
4656
|
-
function supportsLastHitDock(baseUrl) {
|
|
4657
|
-
return isHost017Plus(baseUrl);
|
|
4690
|
+
return compareVersion(parsed, V0_2_0) >= 0;
|
|
4658
4691
|
}
|
|
4659
4692
|
//#endregion
|
|
4660
4693
|
//#region src/ext/store.ts
|
|
@@ -4776,26 +4809,23 @@ const name = "dsh-router-core";
|
|
|
4776
4809
|
/** Services required before mounting: the webserver (routes) + llm (设置-模型). */
|
|
4777
4810
|
const inject = ["webServer", "llm"];
|
|
4778
4811
|
/**
|
|
4779
|
-
* `Config` 导出:让 Router
|
|
4812
|
+
* `Config` 导出:让 Router 卡片进设置-模型的镜像(0.2.0 的收录链是「插件导出的
|
|
4813
|
+
* Config + 配置树行 id」)。
|
|
4780
4814
|
*
|
|
4781
|
-
*
|
|
4782
|
-
*
|
|
4783
|
-
*
|
|
4784
|
-
*
|
|
4785
|
-
*
|
|
4786
|
-
* 拿不到宿主版本,就只剩「这份 Schema 实例到底有没有 `.volatile`」这个真问题 ——
|
|
4787
|
-
* 能力检测恰好就是**对**的判据。
|
|
4815
|
+
* **这处保留能力检测,不看宿主版本号** —— 因为它是**模块顶层导出**,loader 一 import
|
|
4816
|
+
* 就求值,那时还没有 `ctx.baseUrl`;而 schemastery 是本插件的**普通依赖**,解析到的
|
|
4817
|
+
* 是插件自己 node_modules 里那一份,跟宿主实际加载的版本无关。既然这里拿不到宿主
|
|
4818
|
+
* 版本,就只剩「这份 Schema 实例到底有没有 `.volatile`」这个真问题 —— 能力检测
|
|
4819
|
+
* 恰好就是**对**的判据。
|
|
4788
4820
|
*
|
|
4789
|
-
*
|
|
4790
|
-
*
|
|
4791
|
-
*
|
|
4792
|
-
*
|
|
4793
|
-
* - 无 volatile:回落普通空 schema,不抛;此时宿主必是 0.1.5,本就靠
|
|
4794
|
-
* installSection 出卡片(见下方按版本分流的注册),这个 Config 无副作用。
|
|
4821
|
+
* 为什么要 `.volatile()`:空 object 会被 volatileForm 过滤掉,顶层标 volatile 才能让
|
|
4822
|
+
* 卡片进镜像。`.volatile()` 是 schemastery 3.18.4 才有的原型方法,直写
|
|
4823
|
+
* `Schema.object({}).volatile()` 在没有它的版本上会抛 TypeError → **整个插件加载
|
|
4824
|
+
* 失败**。所以:无 volatile 就回落普通空 schema(不抛,代价是卡片不出现,并有日志)。
|
|
4795
4825
|
*/
|
|
4796
4826
|
const emptySchema = Schema.object({});
|
|
4797
4827
|
/**
|
|
4798
|
-
*
|
|
4828
|
+
* 设置镜像只认「插件导出的 Config + 配置树行 id」这条收录链,且空 object
|
|
4799
4829
|
* 会被 volatileForm 过滤掉 —— 顶层 `.volatile()` 才能让 Router 卡片出现在设置-模型。
|
|
4800
4830
|
* Router 的真配置在自己的 state.json(路由系统面板),这里故意不暴露任何字段。
|
|
4801
4831
|
*/
|
|
@@ -4833,9 +4863,9 @@ function apply(rawContext) {
|
|
|
4833
4863
|
const stateFile = join(dataDir, "state.json");
|
|
4834
4864
|
log(`data dir: ${dataDir}`);
|
|
4835
4865
|
const hostVersion = detectHostVersion(ctx.baseUrl);
|
|
4836
|
-
const
|
|
4837
|
-
|
|
4838
|
-
|
|
4866
|
+
const hostSupported = isHostSupported(ctx.baseUrl);
|
|
4867
|
+
log(`host version: ${hostVersion ?? "未知"} (>=0.2.0: ${hostSupported ? "yes" : "no"})`);
|
|
4868
|
+
if (!hostSupported) ctx.logger.warn(`[dsh-router] 宿主版本 ${hostVersion ?? "未知"} 低于本插件支持的 0.2.0 —— 插件仍会加载,但设置卡片、「最近命中」徽章与工具结果转发会走错路径。请把宿主升到 0.2.0 及以上。`);
|
|
4839
4869
|
const store = new SupplierConfigStore(stateFile);
|
|
4840
4870
|
const credentials = new CredentialStore(join(dataDir, "auths"));
|
|
4841
4871
|
const router = new Router(stateFile, store, log);
|
|
@@ -5003,7 +5033,7 @@ function apply(rawContext) {
|
|
|
5003
5033
|
writeJson(res, 200, {
|
|
5004
5034
|
ok: true,
|
|
5005
5035
|
hostVersion: hostVersion ?? "",
|
|
5006
|
-
lastHitDock,
|
|
5036
|
+
lastHitDock: true,
|
|
5007
5037
|
suppliers: suppliers.map((s) => {
|
|
5008
5038
|
const loaded = loadedSuppliers.find((l) => l.supplier.id === s.id);
|
|
5009
5039
|
return {
|
|
@@ -5217,6 +5247,39 @@ function apply(rawContext) {
|
|
|
5217
5247
|
requireApiKey: keys.requireApiKey
|
|
5218
5248
|
});
|
|
5219
5249
|
});
|
|
5250
|
+
/**
|
|
5251
|
+
* 把 `router.ext` 表映射成面板要的 `ExtInfo[]`。**GET 与 PATCH 共用**。
|
|
5252
|
+
*
|
|
5253
|
+
* ⚠️ 2026-09-30:op 成功时我复制了一份这段映射来就地回表 —— 两份必然漂移
|
|
5254
|
+
* (改一处只生效一半,本仓反复栽的坑)⇒ 抽成函数,一处定义两处调用。
|
|
5255
|
+
*/
|
|
5256
|
+
function extInfos(table, isEnabled) {
|
|
5257
|
+
return Object.values(table).map((raw) => {
|
|
5258
|
+
const e = raw;
|
|
5259
|
+
if (!e || typeof e.id !== "string" || e.id === "") return void 0;
|
|
5260
|
+
const st = typeof e.getState === "function" ? e.getState() : { ready: false };
|
|
5261
|
+
return {
|
|
5262
|
+
id: e.id,
|
|
5263
|
+
name: e.name ?? e.id,
|
|
5264
|
+
...e.description !== void 0 ? { description: e.description } : {},
|
|
5265
|
+
...e.icon !== void 0 ? { icon: e.icon } : {},
|
|
5266
|
+
enabled: isEnabled(e.id),
|
|
5267
|
+
ready: st?.ready === true,
|
|
5268
|
+
...raw.source === "builtin" ? { source: "builtin" } : {},
|
|
5269
|
+
...Array.isArray(e.controls) && e.controls.length > 0 ? { controls: e.controls.filter((c) => !!c && typeof c.id === "string" && c.id !== "" && typeof c.title === "string").map((c) => ({
|
|
5270
|
+
id: c.id,
|
|
5271
|
+
title: c.title,
|
|
5272
|
+
on: c.on === true,
|
|
5273
|
+
...c.detail !== void 0 ? { detail: c.detail } : {},
|
|
5274
|
+
...typeof c.body === "string" ? { body: c.body } : {},
|
|
5275
|
+
...c.editable === true ? { editable: true } : {},
|
|
5276
|
+
custom: c.custom === true,
|
|
5277
|
+
overridden: c.overridden === true
|
|
5278
|
+
})) } : {},
|
|
5279
|
+
...st?.detail !== void 0 ? { detail: st.detail } : {}
|
|
5280
|
+
};
|
|
5281
|
+
}).filter((e) => e !== void 0);
|
|
5282
|
+
}
|
|
5220
5283
|
route(`${ROUTER_API_BASE}/ext`, async (req, res) => {
|
|
5221
5284
|
if (req.method === "PATCH") {
|
|
5222
5285
|
let body;
|
|
@@ -5229,6 +5292,7 @@ function apply(rawContext) {
|
|
|
5229
5292
|
});
|
|
5230
5293
|
return;
|
|
5231
5294
|
}
|
|
5295
|
+
let addedId = null;
|
|
5232
5296
|
const ext = typeof body.id === "string" ? exts[body.id] : void 0;
|
|
5233
5297
|
if (!ext) {
|
|
5234
5298
|
writeJson(res, 404, {
|
|
@@ -5237,6 +5301,152 @@ function apply(rawContext) {
|
|
|
5237
5301
|
});
|
|
5238
5302
|
return;
|
|
5239
5303
|
}
|
|
5304
|
+
if (body.op !== void 0) {
|
|
5305
|
+
const op = body.op;
|
|
5306
|
+
if (op === "reorder") {
|
|
5307
|
+
if (!Array.isArray(body.ids)) {
|
|
5308
|
+
writeJson(res, 400, {
|
|
5309
|
+
ok: false,
|
|
5310
|
+
error: "ids must be an array"
|
|
5311
|
+
});
|
|
5312
|
+
return;
|
|
5313
|
+
}
|
|
5314
|
+
if (typeof ext.setControlOrder !== "function" || !ext.setControlOrder(body.ids)) {
|
|
5315
|
+
writeJson(res, 400, {
|
|
5316
|
+
ok: false,
|
|
5317
|
+
error: "排序保存失败"
|
|
5318
|
+
});
|
|
5319
|
+
return;
|
|
5320
|
+
}
|
|
5321
|
+
} else if (op === "remove" || op === "reset") {
|
|
5322
|
+
if (typeof body.controlId !== "string" || body.controlId === "") {
|
|
5323
|
+
writeJson(res, 400, {
|
|
5324
|
+
ok: false,
|
|
5325
|
+
error: "controlId must be a non-empty string"
|
|
5326
|
+
});
|
|
5327
|
+
return;
|
|
5328
|
+
}
|
|
5329
|
+
const fn = op === "remove" ? ext.removeCustomControl : ext.resetControlText;
|
|
5330
|
+
if (typeof fn !== "function" || !fn.call(ext, body.controlId)) {
|
|
5331
|
+
writeJson(res, 400, {
|
|
5332
|
+
ok: false,
|
|
5333
|
+
error: op === "remove" ? "只能删除自定义准则(内置条目请用开关关掉)" : "只能还原内置准则,且它得先被改过"
|
|
5334
|
+
});
|
|
5335
|
+
return;
|
|
5336
|
+
}
|
|
5337
|
+
} else if (op === "add") {
|
|
5338
|
+
if (typeof ext.addCustomControl !== "function") {
|
|
5339
|
+
writeJson(res, 400, {
|
|
5340
|
+
ok: false,
|
|
5341
|
+
error: "extension does not support custom controls"
|
|
5342
|
+
});
|
|
5343
|
+
return;
|
|
5344
|
+
}
|
|
5345
|
+
addedId = ext.addCustomControl(typeof body.title === "string" ? body.title : "", typeof body.body === "string" ? body.body : "");
|
|
5346
|
+
if (addedId === null) {
|
|
5347
|
+
writeJson(res, 400, {
|
|
5348
|
+
ok: false,
|
|
5349
|
+
error: "标题与内容都不能为空"
|
|
5350
|
+
});
|
|
5351
|
+
return;
|
|
5352
|
+
}
|
|
5353
|
+
} else {
|
|
5354
|
+
writeJson(res, 400, {
|
|
5355
|
+
ok: false,
|
|
5356
|
+
error: `unknown op: ${String(op)}`
|
|
5357
|
+
});
|
|
5358
|
+
return;
|
|
5359
|
+
}
|
|
5360
|
+
writeJson(res, 200, addedId === null ? {
|
|
5361
|
+
ok: true,
|
|
5362
|
+
enhancers: extInfos(exts, (id) => extStore.isEnabled(id))
|
|
5363
|
+
} : {
|
|
5364
|
+
ok: true,
|
|
5365
|
+
id: addedId,
|
|
5366
|
+
enhancers: extInfos(exts, (id) => extStore.isEnabled(id))
|
|
5367
|
+
});
|
|
5368
|
+
return;
|
|
5369
|
+
}
|
|
5370
|
+
if (body.title !== void 0 || body.body !== void 0) {
|
|
5371
|
+
if (typeof body.controlId !== "string" || body.controlId === "") {
|
|
5372
|
+
writeJson(res, 400, {
|
|
5373
|
+
ok: false,
|
|
5374
|
+
error: "controlId must be a non-empty string"
|
|
5375
|
+
});
|
|
5376
|
+
return;
|
|
5377
|
+
}
|
|
5378
|
+
if (typeof body.title !== "undefined" && typeof body.title !== "string") {
|
|
5379
|
+
writeJson(res, 400, {
|
|
5380
|
+
ok: false,
|
|
5381
|
+
error: "title must be a string"
|
|
5382
|
+
});
|
|
5383
|
+
return;
|
|
5384
|
+
}
|
|
5385
|
+
if (typeof body.body !== "undefined" && typeof body.body !== "string") {
|
|
5386
|
+
writeJson(res, 400, {
|
|
5387
|
+
ok: false,
|
|
5388
|
+
error: "body must be a string"
|
|
5389
|
+
});
|
|
5390
|
+
return;
|
|
5391
|
+
}
|
|
5392
|
+
if (typeof ext.setControlText !== "function") {
|
|
5393
|
+
writeJson(res, 400, {
|
|
5394
|
+
ok: false,
|
|
5395
|
+
error: "extension controls are not editable"
|
|
5396
|
+
});
|
|
5397
|
+
return;
|
|
5398
|
+
}
|
|
5399
|
+
const patch = {};
|
|
5400
|
+
if (typeof body.title === "string") patch.title = body.title;
|
|
5401
|
+
if (typeof body.body === "string") patch.body = body.body;
|
|
5402
|
+
if (!ext.setControlText(body.controlId, patch)) {
|
|
5403
|
+
writeJson(res, 400, {
|
|
5404
|
+
ok: false,
|
|
5405
|
+
error: "unknown controlId"
|
|
5406
|
+
});
|
|
5407
|
+
return;
|
|
5408
|
+
}
|
|
5409
|
+
writeJson(res, 200, {
|
|
5410
|
+
ok: true,
|
|
5411
|
+
enhancers: extInfos(exts, (id) => extStore.isEnabled(id))
|
|
5412
|
+
});
|
|
5413
|
+
return;
|
|
5414
|
+
}
|
|
5415
|
+
if (body.controlId !== void 0) {
|
|
5416
|
+
if (typeof body.controlId !== "string" || body.controlId === "") {
|
|
5417
|
+
writeJson(res, 400, {
|
|
5418
|
+
ok: false,
|
|
5419
|
+
error: "controlId must be a non-empty string"
|
|
5420
|
+
});
|
|
5421
|
+
return;
|
|
5422
|
+
}
|
|
5423
|
+
if (typeof body.on !== "boolean") {
|
|
5424
|
+
writeJson(res, 400, {
|
|
5425
|
+
ok: false,
|
|
5426
|
+
error: "on must be a boolean"
|
|
5427
|
+
});
|
|
5428
|
+
return;
|
|
5429
|
+
}
|
|
5430
|
+
if (typeof ext.setControl !== "function") {
|
|
5431
|
+
writeJson(res, 400, {
|
|
5432
|
+
ok: false,
|
|
5433
|
+
error: "extension has no controls"
|
|
5434
|
+
});
|
|
5435
|
+
return;
|
|
5436
|
+
}
|
|
5437
|
+
if (!ext.setControl(body.controlId, body.on)) {
|
|
5438
|
+
writeJson(res, 400, {
|
|
5439
|
+
ok: false,
|
|
5440
|
+
error: "unknown controlId"
|
|
5441
|
+
});
|
|
5442
|
+
return;
|
|
5443
|
+
}
|
|
5444
|
+
writeJson(res, 200, {
|
|
5445
|
+
ok: true,
|
|
5446
|
+
enhancers: extInfos(exts, (id) => extStore.isEnabled(id))
|
|
5447
|
+
});
|
|
5448
|
+
return;
|
|
5449
|
+
}
|
|
5240
5450
|
if (typeof body.enabled !== "boolean") {
|
|
5241
5451
|
writeJson(res, 400, {
|
|
5242
5452
|
ok: false,
|
|
@@ -5258,21 +5468,7 @@ function apply(rawContext) {
|
|
|
5258
5468
|
}
|
|
5259
5469
|
writeJson(res, 200, {
|
|
5260
5470
|
ok: true,
|
|
5261
|
-
enhancers:
|
|
5262
|
-
const e = raw;
|
|
5263
|
-
if (!e || typeof e.id !== "string" || e.id === "") return void 0;
|
|
5264
|
-
const st = typeof e.getState === "function" ? e.getState() : { ready: false };
|
|
5265
|
-
return {
|
|
5266
|
-
id: e.id,
|
|
5267
|
-
name: e.name ?? e.id,
|
|
5268
|
-
...e.description !== void 0 ? { description: e.description } : {},
|
|
5269
|
-
...e.icon !== void 0 ? { icon: e.icon } : {},
|
|
5270
|
-
enabled: extStore.isEnabled(e.id),
|
|
5271
|
-
ready: st?.ready === true,
|
|
5272
|
-
...raw.source === "builtin" ? { source: "builtin" } : {},
|
|
5273
|
-
...st?.detail !== void 0 ? { detail: st.detail } : {}
|
|
5274
|
-
};
|
|
5275
|
-
}).filter((e) => e !== void 0)
|
|
5471
|
+
enhancers: extInfos(exts, (id) => extStore.isEnabled(id))
|
|
5276
5472
|
});
|
|
5277
5473
|
});
|
|
5278
5474
|
/** 解析并校验周期参数(不接受任意字符串)。 */
|
|
@@ -5335,25 +5531,8 @@ function apply(rawContext) {
|
|
|
5335
5531
|
disposers.push(ctx.llm.registerModelDiscovery(settingsNs, async () => {
|
|
5336
5532
|
return (await router.combos()).map((c) => ({ id: c.name }));
|
|
5337
5533
|
}));
|
|
5338
|
-
|
|
5339
|
-
|
|
5340
|
-
else ctx.inject(["settings"], (sctx) => {
|
|
5341
|
-
const settings = sctx.settings;
|
|
5342
|
-
if (settings === void 0) {
|
|
5343
|
-
log("settings service absent — skip Router settings-section registration");
|
|
5344
|
-
return;
|
|
5345
|
-
}
|
|
5346
|
-
if (settings.installSection === void 0) {
|
|
5347
|
-
log(`settings.installSection absent despite 0.1.5 host (${settingsNs})`);
|
|
5348
|
-
return;
|
|
5349
|
-
}
|
|
5350
|
-
settings.installSection(rawContext, settingsNs, routerSettingsSchema, {}, {
|
|
5351
|
-
setSource: () => {},
|
|
5352
|
-
onChange: () => {}
|
|
5353
|
-
});
|
|
5354
|
-
log(`Router settings section registered (${settingsNs})`);
|
|
5355
|
-
});
|
|
5356
|
-
disposers.push(ctx.llm.registerAdapter(["router"], new RouterAdapter("http://localhost:3080/v1", { comboModels: async () => (await router.combos()).map((c) => {
|
|
5534
|
+
log(`settings section driven by exported Config (${settingsNs})`);
|
|
5535
|
+
disposers.push(ctx.llm.registerAdapter(["router"], new RouterAdapter(loopbackBaseURL(() => ctx.webServer?.port), { comboModels: async () => (await router.combos()).map((c) => {
|
|
5357
5536
|
const w = router.comboContextWindow(c);
|
|
5358
5537
|
return {
|
|
5359
5538
|
id: c.name,
|