mslxdff 0.1.199 → 0.1.203
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 +3 -3
- package/bin/mslxdff.js +1 -1
- package/package.json +38 -38
- package/plugins/README.md +14 -14
- package/plugins/prefer-model.mjs.example +14 -14
- package/src/anthropic/count-tokens.js +61 -61
- package/src/anthropic/request-body.js +126 -126
- package/src/anthropic/shared.js +18 -18
- package/src/anthropic/sse-aggregate.js +56 -56
- package/src/anthropic/translate.js +287 -287
- package/src/anthropic/web-search.js +361 -361
- package/src/api-kind.js +31 -31
- package/src/autostart.js +288 -288
- package/src/bench/probe.js +1 -1
- package/src/bench/runner.js +1 -1
- package/src/bench/via-probe.js +1 -1
- package/src/bench/via.js +144 -144
- package/src/bench/workbuddy-bench.js +1 -1
- package/src/chat/config.js +11 -11
- package/src/chat/cooling.js +81 -81
- package/src/chat/direct.js +58 -58
- package/src/chat/index.js +6 -6
- package/src/chat/spinner.js +38 -38
- package/src/chat/sse.js +69 -69
- package/src/chat/stats.js +247 -247
- package/src/chat/store.js +48 -48
- package/src/chat/tool-handlers.js +67 -67
- package/src/chat/tools.js +1 -1
- package/src/chat/upstream.js +59 -59
- package/src/chat-pipeline/auto-race.js +158 -158
- package/src/chat-pipeline/empty-turn.js +147 -147
- package/src/chat-pipeline/engine.js +17 -17
- package/src/chat-pipeline/serial-trial.js +283 -283
- package/src/chat-pipeline/terminal-verdict.js +147 -147
- package/src/chooser.js +54 -54
- package/src/claude-desktop/apply.js +101 -101
- package/src/claude-desktop/context1m.js +38 -38
- package/src/claude-desktop/doctor.js +146 -146
- package/src/claude-desktop/slots.js +152 -152
- package/src/cli/bootstrap.js +1 -1
- package/src/cli/commands/claude-desktop.js +284 -284
- package/src/cli/commands/daemon.js +231 -231
- package/src/cli/commands/model/list-live.js +22 -22
- package/src/cli/commands/model/list-providers.js +76 -76
- package/src/cli/commands/model/list-render.js +77 -77
- package/src/cli/commands/model/list-sort.js +52 -52
- package/src/cli/commands/model/list-tty.js +76 -76
- package/src/cli/commands/model/list.js +166 -166
- package/src/cli/commands/model/live-models.js +24 -24
- package/src/cli/commands/model/stats.js +43 -43
- package/src/cli/commands/model/status.js +47 -47
- package/src/cli/commands/model.js +51 -51
- package/src/cli/commands/provider/add.js +108 -108
- package/src/cli/commands/provider/allowlist.js +99 -99
- package/src/cli/commands/provider/cline-free.js +99 -99
- package/src/cli/commands/provider/cline-login.js +218 -218
- package/src/cli/commands/provider/cline-quota.js +65 -65
- package/src/cli/commands/provider/codearts-login.js +45 -45
- package/src/cli/commands/provider/config.js +119 -119
- package/src/cli/commands/provider/globalqwenwork-login.js +126 -126
- package/src/cli/commands/provider/keys-guard.js +50 -50
- package/src/cli/commands/provider/keys.js +161 -161
- package/src/cli/commands/provider/qoder-checkin.js +81 -81
- package/src/cli/commands/provider/qoder-login.js +102 -102
- package/src/cli/commands/provider/raccoon-login.js +180 -180
- package/src/cli/commands/provider/raccoon-password.js +162 -162
- package/src/cli/commands/provider/raccoon-quota.js +109 -109
- package/src/cli/commands/provider/traework-checkin.js +144 -144
- package/src/cli/commands/provider/traework-login.js +164 -164
- package/src/cli/commands/provider/workbuddy-login.js +153 -153
- package/src/cli/commands/provider/zcode-login.js +77 -77
- package/src/cli/commands/provider/zcode-quota.js +55 -55
- package/src/cli/commands/provider.js +1 -1
- package/src/cli/commands/stats.js +149 -149
- package/src/cli/commands/system.js +261 -261
- package/src/cli/commands/timezone.js +47 -47
- package/src/cli/commands/use-group.js +60 -60
- package/src/cli/commands/workbuddy.js +189 -189
- package/src/cli/format.js +141 -141
- package/src/cli/group-helpers.js +70 -70
- package/src/cli/help.js +80 -80
- package/src/cli/index.js +70 -70
- package/src/cli/interactive.js +121 -121
- package/src/cli/join-core.js +99 -99
- package/src/cli/join-wizard.js +175 -175
- package/src/cli/policy.js +136 -136
- package/src/cli/provider-row.js +126 -126
- package/src/cli/util.js +24 -24
- package/src/compat.js +53 -53
- package/src/events.js +38 -38
- package/src/free-lane.js +131 -131
- package/src/free-watcher.js +63 -63
- package/src/groups.js +262 -262
- package/src/logs.js +195 -195
- package/src/metrics.js +62 -62
- package/src/model-capabilities/context-lookup.js +65 -65
- package/src/model-capabilities/merge.js +142 -142
- package/src/model-trace.js +187 -187
- package/src/models.js +1 -1
- package/src/opencode-identity.js +72 -72
- package/src/peers.js +254 -251
- package/src/plugins.js +107 -107
- package/src/providers/base.js +164 -164
- package/src/providers/classify.js +22 -22
- package/src/providers/cline/allowlist-sync.js +93 -93
- package/src/providers/cline/free-catalog.js +71 -71
- package/src/providers/cline/headers.js +37 -37
- package/src/providers/cline/index.js +130 -130
- package/src/providers/cline/models.js +150 -150
- package/src/providers/cline/usage.js +274 -274
- package/src/providers/codearts/auth-pool.js +148 -148
- package/src/providers/codearts/concurrency.js +34 -34
- package/src/providers/codearts/const.js +65 -65
- package/src/providers/codearts/dpop.js +95 -95
- package/src/providers/codearts/index.js +175 -175
- package/src/providers/codearts/login.js +214 -214
- package/src/providers/codearts/models.js +182 -182
- package/src/providers/codearts/sign.js +96 -96
- package/src/providers/codearts/sse.js +204 -204
- package/src/providers/codearts/stream.js +209 -209
- package/src/providers/codearts/sts.js +124 -124
- package/src/providers/codearts.js +1 -1
- package/src/providers/dispatcher.js +146 -146
- package/src/providers/generic.js +156 -156
- package/src/providers/globalqwenwork/account-store.js +141 -141
- package/src/providers/globalqwenwork/constants.js +73 -73
- package/src/providers/globalqwenwork/crypto.js +2 -2
- package/src/providers/globalqwenwork/http.js +2 -2
- package/src/providers/globalqwenwork/index.js +331 -331
- package/src/providers/globalqwenwork/payload.js +147 -147
- package/src/providers/globalqwenwork/rsa.js +2 -2
- package/src/providers/globalqwenwork/sse.js +2 -2
- package/src/providers/globalqwenwork/stream.js +2 -2
- package/src/providers/globalqwenwork.js +1 -1
- package/src/providers/globalworkbuddy/account-store.js +198 -198
- package/src/providers/globalworkbuddy/auth.js +97 -97
- package/src/providers/globalworkbuddy/catalog.js +138 -138
- package/src/providers/globalworkbuddy/constants.js +73 -73
- package/src/providers/globalworkbuddy/device-auth.js +120 -120
- package/src/providers/globalworkbuddy/errors.js +98 -98
- package/src/providers/globalworkbuddy/headers.js +89 -89
- package/src/providers/globalworkbuddy/models.js +96 -96
- package/src/providers/globalworkbuddy/payload.js +80 -80
- package/src/providers/index.js +5 -5
- package/src/providers/model-id.js +172 -172
- package/src/providers/opencode.js +19 -19
- package/src/providers/openrouter.js +31 -31
- package/src/providers/qoder/account-store.js +106 -106
- package/src/providers/qoder/aggregate.js +60 -60
- package/src/providers/qoder/baseprompt.json +591 -591
- package/src/providers/qoder/checkin.js +160 -160
- package/src/providers/qoder/constants.js +44 -44
- package/src/providers/qoder/encode.js +41 -41
- package/src/providers/qoder/fingerprint.js +38 -38
- package/src/providers/qoder/models.js +77 -77
- package/src/providers/qoder/oauth.js +84 -84
- package/src/providers/qoder/payload.js +162 -162
- package/src/providers/qoder/request.js +27 -27
- package/src/providers/qoder/session.js +134 -134
- package/src/providers/qoder/sticky.js +38 -38
- package/src/providers/qoder/stream.js +116 -116
- package/src/providers/qoder.js +1 -1
- package/src/providers/raccoon/account-store.js +216 -216
- package/src/providers/raccoon/auth.js +67 -67
- package/src/providers/raccoon/chat.js +228 -228
- package/src/providers/raccoon/const.js +180 -165
- package/src/providers/raccoon/credits.js +185 -175
- package/src/providers/raccoon/crypto.js +43 -0
- package/src/providers/raccoon/envelope.js +31 -31
- package/src/providers/raccoon/headers.js +45 -45
- package/src/providers/raccoon/index.js +216 -216
- package/src/providers/raccoon/login.js +279 -279
- package/src/providers/raccoon/models.js +160 -160
- package/src/providers/raccoon/password-login.js +102 -87
- package/src/providers/raccoon/qr-codewords.js +115 -115
- package/src/providers/raccoon/qr.js +236 -236
- package/src/providers/raccoon/sse.js +135 -135
- package/src/providers/responses-channel.js +120 -120
- package/src/providers/share-keys.js +53 -53
- package/src/providers/traework/account-store.js +234 -234
- package/src/providers/traework/chat.js +217 -217
- package/src/providers/traework/checkin.js +77 -77
- package/src/providers/traework/constants.js +41 -41
- package/src/providers/traework/errors.js +52 -52
- package/src/providers/traework/headers.js +79 -79
- package/src/providers/traework/index.js +132 -132
- package/src/providers/traework/market-identity.js +116 -116
- package/src/providers/traework/models.js +98 -98
- package/src/providers/traework/payload.js +86 -86
- package/src/providers/traework/sse.js +184 -184
- package/src/providers/traework/token.js +52 -52
- package/src/providers/traework.js +2 -2
- package/src/providers/workbuddy/account-store.js +186 -186
- package/src/providers/workbuddy/auth.js +158 -158
- package/src/providers/workbuddy/balance.js +86 -86
- package/src/providers/workbuddy/cat-travel.js +125 -125
- package/src/providers/workbuddy/checkin.js +75 -75
- package/src/providers/workbuddy/desktop-info.js +78 -78
- package/src/providers/workbuddy/growth-api.js +67 -67
- package/src/providers/workbuddy/growth-plans.js +63 -63
- package/src/providers/workbuddy/growth.js +181 -181
- package/src/providers/workbuddy/index.js +217 -217
- package/src/providers/workbuddy/models.js +126 -126
- package/src/providers/workbuddy/payload.js +141 -141
- package/src/providers/workbuddy/rotation-log.js +83 -83
- package/src/providers/workbuddy/sanitize-tools.js +61 -61
- package/src/providers/workbuddy/sdk-chat.js +26 -26
- package/src/providers/workbuddy-balance.js +78 -78
- package/src/providers/zcode/account-store.js +129 -129
- package/src/providers/zcode/auth.js +28 -28
- package/src/providers/zcode/chat.js +179 -179
- package/src/providers/zcode/const.js +54 -54
- package/src/providers/zcode/context-shape.js +162 -162
- package/src/providers/zcode/headers.js +93 -93
- package/src/providers/zcode/index.js +124 -124
- package/src/providers/zcode/models.js +65 -65
- package/src/providers/zcode/oauth.js +120 -120
- package/src/providers/zcode/quota.js +176 -176
- package/src/providers/zcode/sse.js +196 -196
- package/src/readline-compat.js +25 -25
- package/src/responses/translate.js +311 -311
- package/src/routes/chat/broadband-handler.js +72 -72
- package/src/routes/chat/empty-turn-report.js +31 -31
- package/src/routes/chat/exhausted-handler.js +87 -87
- package/src/routes/chat/hedge-handler.js +130 -130
- package/src/routes/chat/index.js +1 -1
- package/src/routes/chat/local-handler.js +63 -63
- package/src/routes/chat/peer-handler.js +81 -81
- package/src/routes/chat.js +3 -3
- package/src/routes/fallback.js +88 -88
- package/src/routes/groups-relay.js +214 -214
- package/src/routes/groups.js +122 -122
- package/src/routes/helpers.js +83 -83
- package/src/routes/index.js +147 -147
- package/src/routes/messages-route.js +365 -365
- package/src/routes/peers.js +23 -1
- package/src/routes/relay-queue.js +181 -181
- package/src/routes/relay.js +60 -60
- package/src/routes/responses-route.js +213 -213
- package/src/routes/stream-body.js +84 -84
- package/src/routes/stream-hold.js +337 -337
- package/src/routes/stream-scan.js +366 -366
- package/src/routes.js +3 -3
- package/src/runtime/auto-update.js +113 -113
- package/src/runtime/bootstrap.js +8 -0
- package/src/runtime/broadband-stream.js +79 -79
- package/src/runtime/broadband.js +103 -103
- package/src/runtime/group-sync.js +81 -81
- package/src/runtime/keep-warm.js +120 -0
- package/src/runtime/lifecycle-forensics.js +116 -116
- package/src/runtime/lifecycle-log.js +87 -87
- package/src/runtime/log-stamp.js +73 -73
- package/src/runtime/proc-info.js +137 -137
- package/src/runtime/qoder-checkin.js +93 -93
- package/src/runtime/raccoon-checkin.js +126 -126
- package/src/runtime/server-lifecycle.js +189 -189
- package/src/runtime/traework-checkin.js +118 -118
- package/src/runtime/workbuddy-checkin.js +120 -120
- package/src/runtime/workbuddy-growth.js +133 -133
- package/src/server.js +7 -0
- package/src/state/facade.js +58 -58
- package/src/state/memory.js +162 -162
- package/src/state/merge.js +25 -25
- package/src/state/migrations/cline-unify.js +94 -94
- package/src/state/migrations.js +33 -33
- package/src/state/persist.js +42 -42
- package/src/state/provider-config.js +142 -142
- package/src/state/schemas/group.js +31 -31
- package/src/state/schemas/model.js +94 -94
- package/src/state/schemas/peer.js +31 -31
- package/src/state/schemas/port.js +10 -10
- package/src/state/schemas/provider.js +202 -202
- package/src/state/schemas/timezone.js +70 -70
- package/src/state/schemas/token.js +48 -48
- package/src/state/schemas/use-group.js +102 -102
- package/src/state/store.js +176 -176
- package/src/state.js +1 -1
- package/src/sync-claude-desktop.js +234 -234
- package/src/sync-claude.js +152 -152
- package/src/sync-codex.js +116 -116
- package/src/sync-workbuddy.js +158 -158
- package/src/talk-full-redact.js +129 -129
- package/src/talk-full.js +131 -131
- package/src/talk-log.js +257 -257
- package/src/text-tokens.js +52 -52
- package/src/thinking-effort.js +67 -67
- package/src/time.js +106 -106
- package/src/timeline.js +26 -26
- package/src/transport/index.js +1 -1
- package/src/upstream-engine/index.js +74 -72
- package/src/upstream-engine/mode.js +15 -15
- package/src/upstream-engine/sdk/attempt.js +221 -221
- package/src/upstream-engine/sdk/chat.js +28 -28
- package/src/upstream-engine/sdk/convert.js +192 -192
- package/src/upstream-engine/sdk/diagnose.js +62 -62
- package/src/upstream-engine/sdk/dispatch.js +55 -55
- package/src/upstream-engine/sdk/responses.js +176 -176
- package/src/upstream-engine/sdk/sse.js +166 -166
- package/src/upstream-responses.js +305 -305
- package/src/upstream.js +53 -1
- package/src/usage/record.js +106 -106
- package/src/usage/report.js +195 -195
- package/src/v6pool/claim.js +157 -157
- package/src/v6pool/config.js +65 -65
- package/src/v6pool/detect.js +130 -130
- package/src/v6pool/index.js +152 -152
- package/src/v6pool/prefix.js +140 -140
- package/src/v6pool/probe.js +40 -40
- package/src/v6pool/report.js +104 -104
- package/src/v6pool/rotate.js +263 -239
- package/src/warp-pool/config.js +74 -74
- package/src/warp-pool/health.js +106 -106
- package/src/warp-pool/identity.js +141 -141
- package/src/warp-pool/index.js +358 -358
- package/src/warp-pool/keys.js +29 -29
- package/src/warp-pool/nodes.js +95 -95
- package/src/warp-pool/ports.js +160 -160
- package/src/warp-pool/register.js +113 -113
- package/src/warp-pool/report.js +232 -232
- package/src/warp-pool/revive.js +168 -168
- package/src/warp-pool/rotate.js +214 -214
- package/src/warp-pool/strategy.js +43 -43
- package/src/warp-pool/verdict.js +69 -69
- package/src/warp-pool/wireproxy.js +162 -162
- package/docs/cli_help_mini.md +0 -146
- package/src/providers/AGENTS.md +0 -49
- package/src/routes/AGENTS.md +0 -37
|
@@ -1,361 +1,361 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 网关代跑 Anthropic 的 `web_search` 服务端工具(ADR-0049)。
|
|
3
|
-
*
|
|
4
|
-
* 为什么需要它:Claude Code 的内置 WebSearch 不是客户端搜索——它单发一发 `/v1/messages`
|
|
5
|
-
* 旁路请求,把 `{type:"web_search_20250305",name:"web_search",max_uses:8}`(**没有** input_schema)
|
|
6
|
-
* 交给推理服务端,由服务端执行搜索、以 `server_tool_use` + `web_search_tool_result` 块回结果。
|
|
7
|
-
* 本网关的上游是 OpenAI 兼容端点,没人执行搜索,于是那次旁路只能拿到模型瞎猜的假工具调用
|
|
8
|
-
* (实测:CLI 判 0 次搜索并连开 5 轮空转)。这里由**网关自己**跑搜索并把合规块还回去。
|
|
9
|
-
*
|
|
10
|
-
* 纯逻辑 + 可注入 fetch:判据与形状不碰网络,便于单测;出网统一走 compatFetch。
|
|
11
|
-
*/
|
|
12
|
-
import { compatFetch, timeoutSignal } from "../compat.js";
|
|
13
|
-
|
|
14
|
-
const PARALLEL_URL = "https://search.parallel.ai/mcp";
|
|
15
|
-
const EXA_URL = "https://mcp.exa.ai/mcp";
|
|
16
|
-
const TAVILY_URL = "https://api.tavily.com/search";
|
|
17
|
-
// 客户端旁路请求里那句固定 prompt(CLI 内部常量,版本可能变:认不出来就退回既有链路)
|
|
18
|
-
export const SEARCH_PROMPT_PREFIX = "Perform a web search for the query: ";
|
|
19
|
-
// 旁路 prompt 是 CLI 内部常量,理论上一条查询词很短;截一刀防畸形请求把整段上下文当查询送出去网
|
|
20
|
-
const MAX_QUERY_CHARS = 400;
|
|
21
|
-
// 搜索端点回包的读取上限(字符):边读边判,到顶即 cancel,不让 `*_URL` 指向的端点把内存吃穿
|
|
22
|
-
const MAX_RESPONSE_CHARS = 2_000_000;
|
|
23
|
-
// 结果条目 type 的唯一开关:真 Anthropic 用 `web_search_result`,若 CLI 侧口径不同只改这一行
|
|
24
|
-
const RESULT_ITEM_TYPE = "web_search_result";
|
|
25
|
-
|
|
26
|
-
let seq = 0;
|
|
27
|
-
export function newSearchMessageId() {
|
|
28
|
-
return `msg_${Date.now().toString(36)}${(seq++).toString(36)}`;
|
|
29
|
-
}
|
|
30
|
-
export function newServerToolUseId() {
|
|
31
|
-
return `srvtoolu_${Date.now().toString(36)}${(seq++).toString(36)}`;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
// ---------- 判据 ----------
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* server tool:Anthropic 的 `tools[]` 条目里,用户态工具要么带 `input_schema`,要么是 OpenAI 风格
|
|
38
|
-
* `type:"function"`,要么是自由文本的 `type:"custom"`(按 Anthropic 形状它**不带** `input_schema`,
|
|
39
|
-
* 只有 `format`,剥掉就把用户自己的工具吞了)。除此之外带字符串 `type` 的就是服务端工具——
|
|
40
|
-
* **反向判据**(而非枚举前缀白名单)才不会被 Anthropic 新发的带日期类型绕过(ADR-0049)。
|
|
41
|
-
*/
|
|
42
|
-
export function isServerTool(t) {
|
|
43
|
-
if (!t || typeof t !== "object") return false;
|
|
44
|
-
if (t.input_schema) return false;
|
|
45
|
-
const type = t.type;
|
|
46
|
-
return typeof type === "string" && type !== "function" && type !== "custom";
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/** 这一发是不是 web_search 的 server tool 条目。 */
|
|
50
|
-
export function isSearchTool(t) {
|
|
51
|
-
return !!t && typeof t === "object" && t.name === "web_search" && /^web_search(_\d{8})?$/.test(String(t.type || ""));
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
function messageText(m) {
|
|
55
|
-
if (!m || typeof m !== "object") return "";
|
|
56
|
-
const c = m.content;
|
|
57
|
-
if (typeof c === "string") return c;
|
|
58
|
-
if (!Array.isArray(c)) return "";
|
|
59
|
-
let t = "";
|
|
60
|
-
for (const b of c) if (b && typeof b === "object" && typeof b.text === "string") t += (t ? "\n" : "") + b.text;
|
|
61
|
-
return t;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* 命中判据(ADR-0049 D2,五条件与):
|
|
66
|
-
* 1. `tools[]` 里存在 web_search 的 server tool 条目(type + name 双对,见 `isSearchTool`)
|
|
67
|
-
* 2. **同一份 `tools[]` 里没有任何用户态工具**(带 `input_schema`、`type:"function"` 或 `type:"custom"`)
|
|
68
|
-
* ——主循环请求永远全套都带着,这一条把「用户正文恰好以那句套话开头」的劫持口堵死
|
|
69
|
-
* 3. 某条非 system 消息的文本**以** CLI 固定套话开头(`startsWith`,不是「正文里包含」)
|
|
70
|
-
* 4. 剥掉套话后**不含换行**(旁路的查询词是一行;粘贴套话再续写指令的多半是主循环)
|
|
71
|
-
* 5. 剥出的查询词截到 `MAX_QUERY_CHARS`
|
|
72
|
-
* `hit` 为真时 `query` 仍可能是空串(调用方据此不打后端)。
|
|
73
|
-
*/
|
|
74
|
-
export function detectSearchRequest(req = {}) {
|
|
75
|
-
const tools = Array.isArray(req.tools) ? req.tools : [];
|
|
76
|
-
const tool = tools.find(isSearchTool);
|
|
77
|
-
if (!tool) return { hit: false, query: "" };
|
|
78
|
-
if (tools.some((t) => !isServerTool(t) && !isSearchTool(t))) return { hit: false, query: "" };
|
|
79
|
-
for (const m of Array.isArray(req.messages) ? req.messages : []) {
|
|
80
|
-
if (m && m.role === "system") continue;
|
|
81
|
-
const text = messageText(m).trimStart(); // 只去前导空白:整段 trim 会吃掉前缀结尾那个空格,导致「空前缀」判不命中
|
|
82
|
-
if (!text.startsWith(SEARCH_PROMPT_PREFIX)) continue;
|
|
83
|
-
const query = text.slice(SEARCH_PROMPT_PREFIX.length).trim();
|
|
84
|
-
if (query.includes("\n")) return { hit: false, query: "" };
|
|
85
|
-
return { hit: true, query: query.slice(0, MAX_QUERY_CHARS) };
|
|
86
|
-
}
|
|
87
|
-
return { hit: false, query: "" };
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
// ---------- 配置 ----------
|
|
91
|
-
|
|
92
|
-
/** env 面:`MSLXDFF_WEB_SEARCH` = 后端顺序(逗号分隔)或 `off`;非法列表值退缺省。 */
|
|
93
|
-
export function searchConfig(env = process.env) {
|
|
94
|
-
const raw = String(env.MSLXDFF_WEB_SEARCH ?? "").trim().toLowerCase();
|
|
95
|
-
const known = ["tavily", "exa", "parallel"]; // 缺省顺序=首腿最稳的那个(grill Q13)
|
|
96
|
-
let backends;
|
|
97
|
-
if (raw === "off" || raw === "none" || raw === "0") backends = [];
|
|
98
|
-
else if (!raw) backends = [...known];
|
|
99
|
-
else {
|
|
100
|
-
backends = raw.split(",").map((s) => s.trim()).filter((b) => known.includes(b));
|
|
101
|
-
if (!backends.length) backends = [...known]; // 写错了别静默变成「关」
|
|
102
|
-
}
|
|
103
|
-
const int = (v, d) => {
|
|
104
|
-
const n = Number(v);
|
|
105
|
-
return Number.isFinite(n) && n > 0 ? Math.floor(n) : d;
|
|
106
|
-
};
|
|
107
|
-
const str = (v) => String(v || "").trim();
|
|
108
|
-
return {
|
|
109
|
-
enabled: backends.length > 0,
|
|
110
|
-
backends,
|
|
111
|
-
timeoutMs: int(env.MSLXDFF_WEB_SEARCH_TIMEOUT_MS, 15000),
|
|
112
|
-
maxResults: int(env.MSLXDFF_WEB_SEARCH_MAX_RESULTS, 8),
|
|
113
|
-
maxChars: int(env.MSLXDFF_WEB_SEARCH_MAX_CHARS, 700),
|
|
114
|
-
tavilyKey: str(env.MSLXDFF_WEB_SEARCH_TAVILY_KEY),
|
|
115
|
-
parallelKey: str(env.MSLXDFF_WEB_SEARCH_PARALLEL_KEY),
|
|
116
|
-
exaKey: str(env.MSLXDFF_WEB_SEARCH_EXA_KEY),
|
|
117
|
-
// `_URL` 三个覆盖点:自建/换端点用,测试也走它(不 mock 模块,少一层假象)
|
|
118
|
-
parallelUrl: str(env.MSLXDFF_WEB_SEARCH_PARALLEL_URL) || PARALLEL_URL,
|
|
119
|
-
exaUrl: str(env.MSLXDFF_WEB_SEARCH_EXA_URL) || EXA_URL,
|
|
120
|
-
tavilyUrl: str(env.MSLXDFF_WEB_SEARCH_TAVILY_URL) || TAVILY_URL,
|
|
121
|
-
};
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
// ---------- 后端(免 key 可用的公开端点:Tavily 走 REST,Exa/Parallel 走 MCP) ----------
|
|
125
|
-
|
|
126
|
-
function rpcBody(name, args) {
|
|
127
|
-
return JSON.stringify({ jsonrpc: "2.0", id: 1, method: "tools/call", params: { name, arguments: args } });
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
/** MCP 端点可能回整包 JSON,也可能回 SSE 帧;两种都认。RPC error 一律抛(200 也可能是错)。 */
|
|
131
|
-
export function unwrapRpcText(bodyText) {
|
|
132
|
-
const trimmed = String(bodyText || "").trim();
|
|
133
|
-
if (!trimmed) return "";
|
|
134
|
-
const candidates = [];
|
|
135
|
-
if (trimmed.startsWith("{")) candidates.push(trimmed);
|
|
136
|
-
for (const line of trimmed.split("\n")) {
|
|
137
|
-
const row = line.trim();
|
|
138
|
-
if (row.startsWith("data:")) candidates.push(row.slice(5).trim());
|
|
139
|
-
}
|
|
140
|
-
for (const c of candidates) {
|
|
141
|
-
let parsed;
|
|
142
|
-
try { parsed = JSON.parse(c); } catch { continue; }
|
|
143
|
-
if (parsed && parsed.error) {
|
|
144
|
-
const msg = typeof parsed.error.message === "string" ? parsed.error.message : "rpc error";
|
|
145
|
-
throw new Error(`search endpoint rejected the request: ${msg}`.slice(0, 200));
|
|
146
|
-
}
|
|
147
|
-
const content = parsed && parsed.result && parsed.result.content;
|
|
148
|
-
if (Array.isArray(content)) {
|
|
149
|
-
const hit = content.find((i) => i && typeof i.text === "string" && i.text.trim());
|
|
150
|
-
if (hit) return hit.text;
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
return "";
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
/**
|
|
157
|
-
* 有上限地读响应体:`await res.text()` 会把整包先物化进内存(`_URL` 可指向任意端点,
|
|
158
|
-
* 恶意/异常的大回包会按并发数放大),故能拿 reader 就边读边判、到顶即 cancel。
|
|
159
|
-
* 拿不到 reader(假 fetch / 已消费)才退回一次性 text() 后再截。
|
|
160
|
-
*/
|
|
161
|
-
async function readCapped(res, cap) {
|
|
162
|
-
const body = res && res.body;
|
|
163
|
-
if (!body || typeof body.getReader !== "function") {
|
|
164
|
-
return String((await res.text?.()) ?? "").slice(0, cap);
|
|
165
|
-
}
|
|
166
|
-
const reader = body.getReader();
|
|
167
|
-
const decoder = new TextDecoder("utf-8");
|
|
168
|
-
let out = "";
|
|
169
|
-
try {
|
|
170
|
-
for (;;) {
|
|
171
|
-
const { done, value } = await reader.read();
|
|
172
|
-
if (done) break;
|
|
173
|
-
if (!value) continue;
|
|
174
|
-
out += decoder.decode(value, { stream: true });
|
|
175
|
-
if (out.length >= cap) {
|
|
176
|
-
try { await reader.cancel(); } catch { /* 已尽 */ }
|
|
177
|
-
break;
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
out += decoder.decode();
|
|
181
|
-
} finally {
|
|
182
|
-
try { reader.releaseLock(); } catch { /* ignore */ }
|
|
183
|
-
}
|
|
184
|
-
return out.slice(0, cap);
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
async function callMcp(url, tool, args, { timeoutMs, bearer, fetchImpl }) {
|
|
188
|
-
const headers = { "Content-Type": "application/json", Accept: "application/json, text/event-stream" };
|
|
189
|
-
if (bearer) headers.Authorization = `Bearer ${bearer}`;
|
|
190
|
-
const res = await (fetchImpl || compatFetch)(url, {
|
|
191
|
-
method: "POST",
|
|
192
|
-
headers,
|
|
193
|
-
body: rpcBody(tool, args),
|
|
194
|
-
signal: timeoutSignal(timeoutMs),
|
|
195
|
-
});
|
|
196
|
-
const text = await readCapped(res, MAX_RESPONSE_CHARS);
|
|
197
|
-
if (Number(res.status) >= 400) throw new Error(`HTTP ${res.status}`);
|
|
198
|
-
return unwrapRpcText(text);
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
/** Parallel 的回包是 JSON 字符串:`results[].{url,title,excerpts[]}`。 */
|
|
202
|
-
export function parseParallelItems(text) {
|
|
203
|
-
let parsed;
|
|
204
|
-
try { parsed = JSON.parse(text); } catch { return []; }
|
|
205
|
-
const out = [];
|
|
206
|
-
for (const r of Array.isArray(parsed?.results) ? parsed.results : []) {
|
|
207
|
-
const url = typeof r?.url === "string" ? r.url.trim() : "";
|
|
208
|
-
if (!url) continue;
|
|
209
|
-
out.push({ title: typeof r.title === "string" && r.title.trim() ? r.title.trim() : url, url, snippet: Array.isArray(r.excerpts) ? r.excerpts.join("\n") : "" });
|
|
210
|
-
}
|
|
211
|
-
return out;
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
/** Exa 的回包是预排好的文本块:`Title:` / `URL:` / `Highlights:`,块间 `---`。 */
|
|
215
|
-
export function parseExaItems(text) {
|
|
216
|
-
const out = [];
|
|
217
|
-
for (const block of String(text || "").split(/\n-{3,}\n/)) {
|
|
218
|
-
const url = /^URL:\s*(.+)$/m.exec(block);
|
|
219
|
-
if (!url) continue;
|
|
220
|
-
const title = /^Title:\s*(.+)$/m.exec(block);
|
|
221
|
-
const at = block.search(/^Highlights:/m);
|
|
222
|
-
out.push({ title: (title && title[1].trim()) || url[1].trim(), url: url[1].trim(), snippet: at === -1 ? "" : block.slice(at).replace(/^Highlights:\s*/, "").trim() });
|
|
223
|
-
}
|
|
224
|
-
return out;
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
/** Tavily 的回包是普通 REST JSON:`results[].{title,url,content}`;免 key 档限流回 4xx(≥400 已在调用处抛)。 */
|
|
228
|
-
export function parseTavilyItems(text) {
|
|
229
|
-
let parsed;
|
|
230
|
-
try { parsed = JSON.parse(text); } catch { return []; }
|
|
231
|
-
const out = [];
|
|
232
|
-
for (const r of Array.isArray(parsed?.results) ? parsed.results : []) {
|
|
233
|
-
const url = typeof r?.url === "string" ? r.url.trim() : "";
|
|
234
|
-
if (!url) continue;
|
|
235
|
-
out.push({ title: typeof r.title === "string" && r.title.trim() ? r.title.trim() : url, url, snippet: typeof r.content === "string" ? r.content : "" });
|
|
236
|
-
}
|
|
237
|
-
return out;
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
const BACKENDS = {
|
|
241
|
-
async parallel(query, cfg, fetchImpl) {
|
|
242
|
-
const text = await callMcp(cfg.parallelUrl, "web_search", { objective: query, search_queries: [query] }, { timeoutMs: cfg.timeoutMs, bearer: cfg.parallelKey, fetchImpl });
|
|
243
|
-
return parseParallelItems(text);
|
|
244
|
-
},
|
|
245
|
-
async exa(query, cfg, fetchImpl) {
|
|
246
|
-
const url = cfg.exaKey ? `${cfg.exaUrl}?exaApiKey=${encodeURIComponent(cfg.exaKey)}` : cfg.exaUrl;
|
|
247
|
-
const text = await callMcp(url, "web_search_exa", { query, type: "auto", numResults: cfg.maxResults, livecrawl: "fallback" }, { timeoutMs: cfg.timeoutMs, fetchImpl });
|
|
248
|
-
return parseExaItems(text);
|
|
249
|
-
},
|
|
250
|
-
// 首腿:免注册的 keyless REST(2026-10-09 压测 10 路并发 10/10;exa/parallel 免 key 档当刻 429/空回)
|
|
251
|
-
async tavily(query, cfg, fetchImpl) {
|
|
252
|
-
const headers = { "Content-Type": "application/json" };
|
|
253
|
-
if (cfg.tavilyKey) headers.Authorization = `Bearer ${cfg.tavilyKey}`;
|
|
254
|
-
else headers["X-Tavily-Access-Mode"] = "keyless";
|
|
255
|
-
const res = await (fetchImpl || compatFetch)(cfg.tavilyUrl, {
|
|
256
|
-
method: "POST",
|
|
257
|
-
headers,
|
|
258
|
-
body: JSON.stringify({ query, max_results: cfg.maxResults, search_depth: "basic" }),
|
|
259
|
-
signal: timeoutSignal(cfg.timeoutMs),
|
|
260
|
-
});
|
|
261
|
-
const text = await readCapped(res, MAX_RESPONSE_CHARS);
|
|
262
|
-
if (Number(res.status) >= 400) throw new Error(`HTTP ${res.status}`);
|
|
263
|
-
return parseTavilyItems(text);
|
|
264
|
-
},
|
|
265
|
-
};
|
|
266
|
-
|
|
267
|
-
/**
|
|
268
|
-
* 按 cfg.backends 顺序试;**只有拿到 ≥1 条带 url 的结果才算成功**
|
|
269
|
-
* (免 key 档的限流文案也是 HTTP 200,见 ADR-0049 已知边界)。
|
|
270
|
-
*/
|
|
271
|
-
export async function runWebSearch(query, cfg, { fetchImpl } = {}) {
|
|
272
|
-
const q = String(query || "").trim();
|
|
273
|
-
const t0 = Date.now();
|
|
274
|
-
if (!q) return { ok: false, provider: "", query: "", results: [], error: "empty query", ms: 0, chain: [] };
|
|
275
|
-
const errs = [];
|
|
276
|
-
// 一条后端链共享一个 deadline(不是每发一个超时):各后端串行最坏也只等 cfg.timeoutMs
|
|
277
|
-
const deadline = t0 + cfg.timeoutMs;
|
|
278
|
-
const chain = []; // 逐后端耗时与失败原因:只有总账看不出是谁花的(grill Q15)
|
|
279
|
-
for (const name of cfg.backends) {
|
|
280
|
-
const run = BACKENDS[name];
|
|
281
|
-
if (!run) continue;
|
|
282
|
-
const left = deadline - Date.now();
|
|
283
|
-
const legT0 = Date.now();
|
|
284
|
-
if (left < 250) {
|
|
285
|
-
errs.push(`${name}: skipped (search budget ${cfg.timeoutMs}ms exhausted)`);
|
|
286
|
-
chain.push({ name, ms: 0, error: `skipped (search budget ${cfg.timeoutMs}ms exhausted)` });
|
|
287
|
-
break;
|
|
288
|
-
}
|
|
289
|
-
try {
|
|
290
|
-
const items = await run(q, { ...cfg, timeoutMs: left }, fetchImpl);
|
|
291
|
-
const results = items.slice(0, cfg.maxResults).map((r) => ({
|
|
292
|
-
title: String(r.title || r.url).slice(0, 200),
|
|
293
|
-
url: String(r.url).slice(0, 500),
|
|
294
|
-
snippet: String(r.snippet || "").replace(/[ \t]+\n/g, "\n").trim().slice(0, cfg.maxChars),
|
|
295
|
-
}));
|
|
296
|
-
if (results.length) return { ok: true, provider: name, query: q, results, ms: Date.now() - t0, chain: chain.concat({ name, ms: Date.now() - legT0 }) };
|
|
297
|
-
errs.push(`${name}: no usable results`);
|
|
298
|
-
chain.push({ name, ms: Date.now() - legT0, error: "no usable results" });
|
|
299
|
-
} catch (e) {
|
|
300
|
-
errs.push(`${name}: ${String(e?.message || e).slice(0, 200)}`);
|
|
301
|
-
chain.push({ name, ms: Date.now() - legT0, error: String(e?.message || e).slice(0, 200) });
|
|
302
|
-
}
|
|
303
|
-
}
|
|
304
|
-
return { ok: false, provider: "", query: q, results: [], error: errs.join("; ") || "all search backends failed", ms: Date.now() - t0, chain };
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
// ---------- 自造响应(块定义只此一份,流式与非流式共用) ----------
|
|
308
|
-
|
|
309
|
-
export function buildSearchBlocks({ query, results }) {
|
|
310
|
-
const id = newServerToolUseId();
|
|
311
|
-
const content = (Array.isArray(results) ? results : []).map((r) => {
|
|
312
|
-
const item = { type: RESULT_ITEM_TYPE, title: String(r.title || r.url), url: String(r.url) };
|
|
313
|
-
if (r.snippet) item.snippet = String(r.snippet);
|
|
314
|
-
return item;
|
|
315
|
-
});
|
|
316
|
-
return [
|
|
317
|
-
{ type: "server_tool_use", id, name: "web_search", input: { query: String(query || "") } },
|
|
318
|
-
{ type: "web_search_tool_result", tool_use_id: id, content },
|
|
319
|
-
];
|
|
320
|
-
}
|
|
321
|
-
|
|
322
|
-
export function buildSearchMessage({ model = "", query, results }) {
|
|
323
|
-
return {
|
|
324
|
-
id: newSearchMessageId(),
|
|
325
|
-
type: "message",
|
|
326
|
-
role: "assistant",
|
|
327
|
-
model,
|
|
328
|
-
content: buildSearchBlocks({ query, results }),
|
|
329
|
-
stop_reason: "end_turn",
|
|
330
|
-
stop_sequence: null,
|
|
331
|
-
usage: {
|
|
332
|
-
input_tokens: 0,
|
|
333
|
-
output_tokens: 0,
|
|
334
|
-
cache_read_input_tokens: 0,
|
|
335
|
-
cache_creation_input_tokens: 0,
|
|
336
|
-
// 没打模型,但按 Anthropic 口径报「跑了 N 次搜索」:客户端上报的正是这个字段
|
|
337
|
-
server_tool_use: { web_search_requests: 1, web_fetch_requests: 0 },
|
|
338
|
-
},
|
|
339
|
-
};
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
/** 由同一份块数组生成 Anthropic 具名事件序列(无 `[DONE]`;见 ADR-0047 收场口径)。 */
|
|
343
|
-
export function searchEvents(message) {
|
|
344
|
-
const ev = (type, extra) => ({ type, ...extra });
|
|
345
|
-
const out = [
|
|
346
|
-
ev("message_start", { message: { id: message.id, type: message.type, role: message.role, model: message.model, content: [], stop_reason: null, stop_sequence: null, usage: message.usage } }),
|
|
347
|
-
];
|
|
348
|
-
message.content.forEach((block, index) => {
|
|
349
|
-
if (block.type === "server_tool_use") {
|
|
350
|
-
out.push(ev("content_block_start", { index, content_block: { type: block.type, id: block.id, name: block.name, input: {} } }));
|
|
351
|
-
out.push(ev("content_block_delta", { index, delta: { type: "input_json_delta", partial_json: JSON.stringify(block.input || {}) } }));
|
|
352
|
-
} else {
|
|
353
|
-
// 结果块一次性给全(真 API 也不逐字滴结果)
|
|
354
|
-
out.push(ev("content_block_start", { index, content_block: block }));
|
|
355
|
-
}
|
|
356
|
-
out.push(ev("content_block_stop", { index }));
|
|
357
|
-
});
|
|
358
|
-
out.push(ev("message_delta", { delta: { stop_reason: message.stop_reason, stop_sequence: null }, usage: message.usage }));
|
|
359
|
-
out.push(ev("message_stop"));
|
|
360
|
-
return out;
|
|
361
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* 网关代跑 Anthropic 的 `web_search` 服务端工具(ADR-0049)。
|
|
3
|
+
*
|
|
4
|
+
* 为什么需要它:Claude Code 的内置 WebSearch 不是客户端搜索——它单发一发 `/v1/messages`
|
|
5
|
+
* 旁路请求,把 `{type:"web_search_20250305",name:"web_search",max_uses:8}`(**没有** input_schema)
|
|
6
|
+
* 交给推理服务端,由服务端执行搜索、以 `server_tool_use` + `web_search_tool_result` 块回结果。
|
|
7
|
+
* 本网关的上游是 OpenAI 兼容端点,没人执行搜索,于是那次旁路只能拿到模型瞎猜的假工具调用
|
|
8
|
+
* (实测:CLI 判 0 次搜索并连开 5 轮空转)。这里由**网关自己**跑搜索并把合规块还回去。
|
|
9
|
+
*
|
|
10
|
+
* 纯逻辑 + 可注入 fetch:判据与形状不碰网络,便于单测;出网统一走 compatFetch。
|
|
11
|
+
*/
|
|
12
|
+
import { compatFetch, timeoutSignal } from "../compat.js";
|
|
13
|
+
|
|
14
|
+
const PARALLEL_URL = "https://search.parallel.ai/mcp";
|
|
15
|
+
const EXA_URL = "https://mcp.exa.ai/mcp";
|
|
16
|
+
const TAVILY_URL = "https://api.tavily.com/search";
|
|
17
|
+
// 客户端旁路请求里那句固定 prompt(CLI 内部常量,版本可能变:认不出来就退回既有链路)
|
|
18
|
+
export const SEARCH_PROMPT_PREFIX = "Perform a web search for the query: ";
|
|
19
|
+
// 旁路 prompt 是 CLI 内部常量,理论上一条查询词很短;截一刀防畸形请求把整段上下文当查询送出去网
|
|
20
|
+
const MAX_QUERY_CHARS = 400;
|
|
21
|
+
// 搜索端点回包的读取上限(字符):边读边判,到顶即 cancel,不让 `*_URL` 指向的端点把内存吃穿
|
|
22
|
+
const MAX_RESPONSE_CHARS = 2_000_000;
|
|
23
|
+
// 结果条目 type 的唯一开关:真 Anthropic 用 `web_search_result`,若 CLI 侧口径不同只改这一行
|
|
24
|
+
const RESULT_ITEM_TYPE = "web_search_result";
|
|
25
|
+
|
|
26
|
+
let seq = 0;
|
|
27
|
+
export function newSearchMessageId() {
|
|
28
|
+
return `msg_${Date.now().toString(36)}${(seq++).toString(36)}`;
|
|
29
|
+
}
|
|
30
|
+
export function newServerToolUseId() {
|
|
31
|
+
return `srvtoolu_${Date.now().toString(36)}${(seq++).toString(36)}`;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// ---------- 判据 ----------
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* server tool:Anthropic 的 `tools[]` 条目里,用户态工具要么带 `input_schema`,要么是 OpenAI 风格
|
|
38
|
+
* `type:"function"`,要么是自由文本的 `type:"custom"`(按 Anthropic 形状它**不带** `input_schema`,
|
|
39
|
+
* 只有 `format`,剥掉就把用户自己的工具吞了)。除此之外带字符串 `type` 的就是服务端工具——
|
|
40
|
+
* **反向判据**(而非枚举前缀白名单)才不会被 Anthropic 新发的带日期类型绕过(ADR-0049)。
|
|
41
|
+
*/
|
|
42
|
+
export function isServerTool(t) {
|
|
43
|
+
if (!t || typeof t !== "object") return false;
|
|
44
|
+
if (t.input_schema) return false;
|
|
45
|
+
const type = t.type;
|
|
46
|
+
return typeof type === "string" && type !== "function" && type !== "custom";
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** 这一发是不是 web_search 的 server tool 条目。 */
|
|
50
|
+
export function isSearchTool(t) {
|
|
51
|
+
return !!t && typeof t === "object" && t.name === "web_search" && /^web_search(_\d{8})?$/.test(String(t.type || ""));
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function messageText(m) {
|
|
55
|
+
if (!m || typeof m !== "object") return "";
|
|
56
|
+
const c = m.content;
|
|
57
|
+
if (typeof c === "string") return c;
|
|
58
|
+
if (!Array.isArray(c)) return "";
|
|
59
|
+
let t = "";
|
|
60
|
+
for (const b of c) if (b && typeof b === "object" && typeof b.text === "string") t += (t ? "\n" : "") + b.text;
|
|
61
|
+
return t;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* 命中判据(ADR-0049 D2,五条件与):
|
|
66
|
+
* 1. `tools[]` 里存在 web_search 的 server tool 条目(type + name 双对,见 `isSearchTool`)
|
|
67
|
+
* 2. **同一份 `tools[]` 里没有任何用户态工具**(带 `input_schema`、`type:"function"` 或 `type:"custom"`)
|
|
68
|
+
* ——主循环请求永远全套都带着,这一条把「用户正文恰好以那句套话开头」的劫持口堵死
|
|
69
|
+
* 3. 某条非 system 消息的文本**以** CLI 固定套话开头(`startsWith`,不是「正文里包含」)
|
|
70
|
+
* 4. 剥掉套话后**不含换行**(旁路的查询词是一行;粘贴套话再续写指令的多半是主循环)
|
|
71
|
+
* 5. 剥出的查询词截到 `MAX_QUERY_CHARS`
|
|
72
|
+
* `hit` 为真时 `query` 仍可能是空串(调用方据此不打后端)。
|
|
73
|
+
*/
|
|
74
|
+
export function detectSearchRequest(req = {}) {
|
|
75
|
+
const tools = Array.isArray(req.tools) ? req.tools : [];
|
|
76
|
+
const tool = tools.find(isSearchTool);
|
|
77
|
+
if (!tool) return { hit: false, query: "" };
|
|
78
|
+
if (tools.some((t) => !isServerTool(t) && !isSearchTool(t))) return { hit: false, query: "" };
|
|
79
|
+
for (const m of Array.isArray(req.messages) ? req.messages : []) {
|
|
80
|
+
if (m && m.role === "system") continue;
|
|
81
|
+
const text = messageText(m).trimStart(); // 只去前导空白:整段 trim 会吃掉前缀结尾那个空格,导致「空前缀」判不命中
|
|
82
|
+
if (!text.startsWith(SEARCH_PROMPT_PREFIX)) continue;
|
|
83
|
+
const query = text.slice(SEARCH_PROMPT_PREFIX.length).trim();
|
|
84
|
+
if (query.includes("\n")) return { hit: false, query: "" };
|
|
85
|
+
return { hit: true, query: query.slice(0, MAX_QUERY_CHARS) };
|
|
86
|
+
}
|
|
87
|
+
return { hit: false, query: "" };
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// ---------- 配置 ----------
|
|
91
|
+
|
|
92
|
+
/** env 面:`MSLXDFF_WEB_SEARCH` = 后端顺序(逗号分隔)或 `off`;非法列表值退缺省。 */
|
|
93
|
+
export function searchConfig(env = process.env) {
|
|
94
|
+
const raw = String(env.MSLXDFF_WEB_SEARCH ?? "").trim().toLowerCase();
|
|
95
|
+
const known = ["tavily", "exa", "parallel"]; // 缺省顺序=首腿最稳的那个(grill Q13)
|
|
96
|
+
let backends;
|
|
97
|
+
if (raw === "off" || raw === "none" || raw === "0") backends = [];
|
|
98
|
+
else if (!raw) backends = [...known];
|
|
99
|
+
else {
|
|
100
|
+
backends = raw.split(",").map((s) => s.trim()).filter((b) => known.includes(b));
|
|
101
|
+
if (!backends.length) backends = [...known]; // 写错了别静默变成「关」
|
|
102
|
+
}
|
|
103
|
+
const int = (v, d) => {
|
|
104
|
+
const n = Number(v);
|
|
105
|
+
return Number.isFinite(n) && n > 0 ? Math.floor(n) : d;
|
|
106
|
+
};
|
|
107
|
+
const str = (v) => String(v || "").trim();
|
|
108
|
+
return {
|
|
109
|
+
enabled: backends.length > 0,
|
|
110
|
+
backends,
|
|
111
|
+
timeoutMs: int(env.MSLXDFF_WEB_SEARCH_TIMEOUT_MS, 15000),
|
|
112
|
+
maxResults: int(env.MSLXDFF_WEB_SEARCH_MAX_RESULTS, 8),
|
|
113
|
+
maxChars: int(env.MSLXDFF_WEB_SEARCH_MAX_CHARS, 700),
|
|
114
|
+
tavilyKey: str(env.MSLXDFF_WEB_SEARCH_TAVILY_KEY),
|
|
115
|
+
parallelKey: str(env.MSLXDFF_WEB_SEARCH_PARALLEL_KEY),
|
|
116
|
+
exaKey: str(env.MSLXDFF_WEB_SEARCH_EXA_KEY),
|
|
117
|
+
// `_URL` 三个覆盖点:自建/换端点用,测试也走它(不 mock 模块,少一层假象)
|
|
118
|
+
parallelUrl: str(env.MSLXDFF_WEB_SEARCH_PARALLEL_URL) || PARALLEL_URL,
|
|
119
|
+
exaUrl: str(env.MSLXDFF_WEB_SEARCH_EXA_URL) || EXA_URL,
|
|
120
|
+
tavilyUrl: str(env.MSLXDFF_WEB_SEARCH_TAVILY_URL) || TAVILY_URL,
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// ---------- 后端(免 key 可用的公开端点:Tavily 走 REST,Exa/Parallel 走 MCP) ----------
|
|
125
|
+
|
|
126
|
+
function rpcBody(name, args) {
|
|
127
|
+
return JSON.stringify({ jsonrpc: "2.0", id: 1, method: "tools/call", params: { name, arguments: args } });
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** MCP 端点可能回整包 JSON,也可能回 SSE 帧;两种都认。RPC error 一律抛(200 也可能是错)。 */
|
|
131
|
+
export function unwrapRpcText(bodyText) {
|
|
132
|
+
const trimmed = String(bodyText || "").trim();
|
|
133
|
+
if (!trimmed) return "";
|
|
134
|
+
const candidates = [];
|
|
135
|
+
if (trimmed.startsWith("{")) candidates.push(trimmed);
|
|
136
|
+
for (const line of trimmed.split("\n")) {
|
|
137
|
+
const row = line.trim();
|
|
138
|
+
if (row.startsWith("data:")) candidates.push(row.slice(5).trim());
|
|
139
|
+
}
|
|
140
|
+
for (const c of candidates) {
|
|
141
|
+
let parsed;
|
|
142
|
+
try { parsed = JSON.parse(c); } catch { continue; }
|
|
143
|
+
if (parsed && parsed.error) {
|
|
144
|
+
const msg = typeof parsed.error.message === "string" ? parsed.error.message : "rpc error";
|
|
145
|
+
throw new Error(`search endpoint rejected the request: ${msg}`.slice(0, 200));
|
|
146
|
+
}
|
|
147
|
+
const content = parsed && parsed.result && parsed.result.content;
|
|
148
|
+
if (Array.isArray(content)) {
|
|
149
|
+
const hit = content.find((i) => i && typeof i.text === "string" && i.text.trim());
|
|
150
|
+
if (hit) return hit.text;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
return "";
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* 有上限地读响应体:`await res.text()` 会把整包先物化进内存(`_URL` 可指向任意端点,
|
|
158
|
+
* 恶意/异常的大回包会按并发数放大),故能拿 reader 就边读边判、到顶即 cancel。
|
|
159
|
+
* 拿不到 reader(假 fetch / 已消费)才退回一次性 text() 后再截。
|
|
160
|
+
*/
|
|
161
|
+
async function readCapped(res, cap) {
|
|
162
|
+
const body = res && res.body;
|
|
163
|
+
if (!body || typeof body.getReader !== "function") {
|
|
164
|
+
return String((await res.text?.()) ?? "").slice(0, cap);
|
|
165
|
+
}
|
|
166
|
+
const reader = body.getReader();
|
|
167
|
+
const decoder = new TextDecoder("utf-8");
|
|
168
|
+
let out = "";
|
|
169
|
+
try {
|
|
170
|
+
for (;;) {
|
|
171
|
+
const { done, value } = await reader.read();
|
|
172
|
+
if (done) break;
|
|
173
|
+
if (!value) continue;
|
|
174
|
+
out += decoder.decode(value, { stream: true });
|
|
175
|
+
if (out.length >= cap) {
|
|
176
|
+
try { await reader.cancel(); } catch { /* 已尽 */ }
|
|
177
|
+
break;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
out += decoder.decode();
|
|
181
|
+
} finally {
|
|
182
|
+
try { reader.releaseLock(); } catch { /* ignore */ }
|
|
183
|
+
}
|
|
184
|
+
return out.slice(0, cap);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
async function callMcp(url, tool, args, { timeoutMs, bearer, fetchImpl }) {
|
|
188
|
+
const headers = { "Content-Type": "application/json", Accept: "application/json, text/event-stream" };
|
|
189
|
+
if (bearer) headers.Authorization = `Bearer ${bearer}`;
|
|
190
|
+
const res = await (fetchImpl || compatFetch)(url, {
|
|
191
|
+
method: "POST",
|
|
192
|
+
headers,
|
|
193
|
+
body: rpcBody(tool, args),
|
|
194
|
+
signal: timeoutSignal(timeoutMs),
|
|
195
|
+
});
|
|
196
|
+
const text = await readCapped(res, MAX_RESPONSE_CHARS);
|
|
197
|
+
if (Number(res.status) >= 400) throw new Error(`HTTP ${res.status}`);
|
|
198
|
+
return unwrapRpcText(text);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/** Parallel 的回包是 JSON 字符串:`results[].{url,title,excerpts[]}`。 */
|
|
202
|
+
export function parseParallelItems(text) {
|
|
203
|
+
let parsed;
|
|
204
|
+
try { parsed = JSON.parse(text); } catch { return []; }
|
|
205
|
+
const out = [];
|
|
206
|
+
for (const r of Array.isArray(parsed?.results) ? parsed.results : []) {
|
|
207
|
+
const url = typeof r?.url === "string" ? r.url.trim() : "";
|
|
208
|
+
if (!url) continue;
|
|
209
|
+
out.push({ title: typeof r.title === "string" && r.title.trim() ? r.title.trim() : url, url, snippet: Array.isArray(r.excerpts) ? r.excerpts.join("\n") : "" });
|
|
210
|
+
}
|
|
211
|
+
return out;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/** Exa 的回包是预排好的文本块:`Title:` / `URL:` / `Highlights:`,块间 `---`。 */
|
|
215
|
+
export function parseExaItems(text) {
|
|
216
|
+
const out = [];
|
|
217
|
+
for (const block of String(text || "").split(/\n-{3,}\n/)) {
|
|
218
|
+
const url = /^URL:\s*(.+)$/m.exec(block);
|
|
219
|
+
if (!url) continue;
|
|
220
|
+
const title = /^Title:\s*(.+)$/m.exec(block);
|
|
221
|
+
const at = block.search(/^Highlights:/m);
|
|
222
|
+
out.push({ title: (title && title[1].trim()) || url[1].trim(), url: url[1].trim(), snippet: at === -1 ? "" : block.slice(at).replace(/^Highlights:\s*/, "").trim() });
|
|
223
|
+
}
|
|
224
|
+
return out;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/** Tavily 的回包是普通 REST JSON:`results[].{title,url,content}`;免 key 档限流回 4xx(≥400 已在调用处抛)。 */
|
|
228
|
+
export function parseTavilyItems(text) {
|
|
229
|
+
let parsed;
|
|
230
|
+
try { parsed = JSON.parse(text); } catch { return []; }
|
|
231
|
+
const out = [];
|
|
232
|
+
for (const r of Array.isArray(parsed?.results) ? parsed.results : []) {
|
|
233
|
+
const url = typeof r?.url === "string" ? r.url.trim() : "";
|
|
234
|
+
if (!url) continue;
|
|
235
|
+
out.push({ title: typeof r.title === "string" && r.title.trim() ? r.title.trim() : url, url, snippet: typeof r.content === "string" ? r.content : "" });
|
|
236
|
+
}
|
|
237
|
+
return out;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
const BACKENDS = {
|
|
241
|
+
async parallel(query, cfg, fetchImpl) {
|
|
242
|
+
const text = await callMcp(cfg.parallelUrl, "web_search", { objective: query, search_queries: [query] }, { timeoutMs: cfg.timeoutMs, bearer: cfg.parallelKey, fetchImpl });
|
|
243
|
+
return parseParallelItems(text);
|
|
244
|
+
},
|
|
245
|
+
async exa(query, cfg, fetchImpl) {
|
|
246
|
+
const url = cfg.exaKey ? `${cfg.exaUrl}?exaApiKey=${encodeURIComponent(cfg.exaKey)}` : cfg.exaUrl;
|
|
247
|
+
const text = await callMcp(url, "web_search_exa", { query, type: "auto", numResults: cfg.maxResults, livecrawl: "fallback" }, { timeoutMs: cfg.timeoutMs, fetchImpl });
|
|
248
|
+
return parseExaItems(text);
|
|
249
|
+
},
|
|
250
|
+
// 首腿:免注册的 keyless REST(2026-10-09 压测 10 路并发 10/10;exa/parallel 免 key 档当刻 429/空回)
|
|
251
|
+
async tavily(query, cfg, fetchImpl) {
|
|
252
|
+
const headers = { "Content-Type": "application/json" };
|
|
253
|
+
if (cfg.tavilyKey) headers.Authorization = `Bearer ${cfg.tavilyKey}`;
|
|
254
|
+
else headers["X-Tavily-Access-Mode"] = "keyless";
|
|
255
|
+
const res = await (fetchImpl || compatFetch)(cfg.tavilyUrl, {
|
|
256
|
+
method: "POST",
|
|
257
|
+
headers,
|
|
258
|
+
body: JSON.stringify({ query, max_results: cfg.maxResults, search_depth: "basic" }),
|
|
259
|
+
signal: timeoutSignal(cfg.timeoutMs),
|
|
260
|
+
});
|
|
261
|
+
const text = await readCapped(res, MAX_RESPONSE_CHARS);
|
|
262
|
+
if (Number(res.status) >= 400) throw new Error(`HTTP ${res.status}`);
|
|
263
|
+
return parseTavilyItems(text);
|
|
264
|
+
},
|
|
265
|
+
};
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* 按 cfg.backends 顺序试;**只有拿到 ≥1 条带 url 的结果才算成功**
|
|
269
|
+
* (免 key 档的限流文案也是 HTTP 200,见 ADR-0049 已知边界)。
|
|
270
|
+
*/
|
|
271
|
+
export async function runWebSearch(query, cfg, { fetchImpl } = {}) {
|
|
272
|
+
const q = String(query || "").trim();
|
|
273
|
+
const t0 = Date.now();
|
|
274
|
+
if (!q) return { ok: false, provider: "", query: "", results: [], error: "empty query", ms: 0, chain: [] };
|
|
275
|
+
const errs = [];
|
|
276
|
+
// 一条后端链共享一个 deadline(不是每发一个超时):各后端串行最坏也只等 cfg.timeoutMs
|
|
277
|
+
const deadline = t0 + cfg.timeoutMs;
|
|
278
|
+
const chain = []; // 逐后端耗时与失败原因:只有总账看不出是谁花的(grill Q15)
|
|
279
|
+
for (const name of cfg.backends) {
|
|
280
|
+
const run = BACKENDS[name];
|
|
281
|
+
if (!run) continue;
|
|
282
|
+
const left = deadline - Date.now();
|
|
283
|
+
const legT0 = Date.now();
|
|
284
|
+
if (left < 250) {
|
|
285
|
+
errs.push(`${name}: skipped (search budget ${cfg.timeoutMs}ms exhausted)`);
|
|
286
|
+
chain.push({ name, ms: 0, error: `skipped (search budget ${cfg.timeoutMs}ms exhausted)` });
|
|
287
|
+
break;
|
|
288
|
+
}
|
|
289
|
+
try {
|
|
290
|
+
const items = await run(q, { ...cfg, timeoutMs: left }, fetchImpl);
|
|
291
|
+
const results = items.slice(0, cfg.maxResults).map((r) => ({
|
|
292
|
+
title: String(r.title || r.url).slice(0, 200),
|
|
293
|
+
url: String(r.url).slice(0, 500),
|
|
294
|
+
snippet: String(r.snippet || "").replace(/[ \t]+\n/g, "\n").trim().slice(0, cfg.maxChars),
|
|
295
|
+
}));
|
|
296
|
+
if (results.length) return { ok: true, provider: name, query: q, results, ms: Date.now() - t0, chain: chain.concat({ name, ms: Date.now() - legT0 }) };
|
|
297
|
+
errs.push(`${name}: no usable results`);
|
|
298
|
+
chain.push({ name, ms: Date.now() - legT0, error: "no usable results" });
|
|
299
|
+
} catch (e) {
|
|
300
|
+
errs.push(`${name}: ${String(e?.message || e).slice(0, 200)}`);
|
|
301
|
+
chain.push({ name, ms: Date.now() - legT0, error: String(e?.message || e).slice(0, 200) });
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
return { ok: false, provider: "", query: q, results: [], error: errs.join("; ") || "all search backends failed", ms: Date.now() - t0, chain };
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
// ---------- 自造响应(块定义只此一份,流式与非流式共用) ----------
|
|
308
|
+
|
|
309
|
+
export function buildSearchBlocks({ query, results }) {
|
|
310
|
+
const id = newServerToolUseId();
|
|
311
|
+
const content = (Array.isArray(results) ? results : []).map((r) => {
|
|
312
|
+
const item = { type: RESULT_ITEM_TYPE, title: String(r.title || r.url), url: String(r.url) };
|
|
313
|
+
if (r.snippet) item.snippet = String(r.snippet);
|
|
314
|
+
return item;
|
|
315
|
+
});
|
|
316
|
+
return [
|
|
317
|
+
{ type: "server_tool_use", id, name: "web_search", input: { query: String(query || "") } },
|
|
318
|
+
{ type: "web_search_tool_result", tool_use_id: id, content },
|
|
319
|
+
];
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
export function buildSearchMessage({ model = "", query, results }) {
|
|
323
|
+
return {
|
|
324
|
+
id: newSearchMessageId(),
|
|
325
|
+
type: "message",
|
|
326
|
+
role: "assistant",
|
|
327
|
+
model,
|
|
328
|
+
content: buildSearchBlocks({ query, results }),
|
|
329
|
+
stop_reason: "end_turn",
|
|
330
|
+
stop_sequence: null,
|
|
331
|
+
usage: {
|
|
332
|
+
input_tokens: 0,
|
|
333
|
+
output_tokens: 0,
|
|
334
|
+
cache_read_input_tokens: 0,
|
|
335
|
+
cache_creation_input_tokens: 0,
|
|
336
|
+
// 没打模型,但按 Anthropic 口径报「跑了 N 次搜索」:客户端上报的正是这个字段
|
|
337
|
+
server_tool_use: { web_search_requests: 1, web_fetch_requests: 0 },
|
|
338
|
+
},
|
|
339
|
+
};
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/** 由同一份块数组生成 Anthropic 具名事件序列(无 `[DONE]`;见 ADR-0047 收场口径)。 */
|
|
343
|
+
export function searchEvents(message) {
|
|
344
|
+
const ev = (type, extra) => ({ type, ...extra });
|
|
345
|
+
const out = [
|
|
346
|
+
ev("message_start", { message: { id: message.id, type: message.type, role: message.role, model: message.model, content: [], stop_reason: null, stop_sequence: null, usage: message.usage } }),
|
|
347
|
+
];
|
|
348
|
+
message.content.forEach((block, index) => {
|
|
349
|
+
if (block.type === "server_tool_use") {
|
|
350
|
+
out.push(ev("content_block_start", { index, content_block: { type: block.type, id: block.id, name: block.name, input: {} } }));
|
|
351
|
+
out.push(ev("content_block_delta", { index, delta: { type: "input_json_delta", partial_json: JSON.stringify(block.input || {}) } }));
|
|
352
|
+
} else {
|
|
353
|
+
// 结果块一次性给全(真 API 也不逐字滴结果)
|
|
354
|
+
out.push(ev("content_block_start", { index, content_block: block }));
|
|
355
|
+
}
|
|
356
|
+
out.push(ev("content_block_stop", { index }));
|
|
357
|
+
});
|
|
358
|
+
out.push(ev("message_delta", { delta: { stop_reason: message.stop_reason, stop_sequence: null }, usage: message.usage }));
|
|
359
|
+
out.push(ev("message_stop"));
|
|
360
|
+
return out;
|
|
361
|
+
}
|