mslxdff 0.1.200 → 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 -74
- 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/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 -263
- 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
package/README.md
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
# mslxdff
|
|
2
|
-
|
|
3
|
-
测试项目,请勿使用
|
|
1
|
+
# mslxdff
|
|
2
|
+
|
|
3
|
+
测试项目,请勿使用
|
package/bin/mslxdff.js
CHANGED
package/package.json
CHANGED
|
@@ -1,38 +1,38 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "mslxdff",
|
|
3
|
-
"version": "0.1.
|
|
4
|
-
"description": "测试项目,请勿使用。",
|
|
5
|
-
"type": "module",
|
|
6
|
-
"bin": {
|
|
7
|
-
"mslxdff": "bin/mslxdff.js"
|
|
8
|
-
},
|
|
9
|
-
"scripts": {
|
|
10
|
-
"start": "node bin/mslxdff.js",
|
|
11
|
-
"test": "node --test --test-concurrency=1 test/*.test.js",
|
|
12
|
-
"docs:check": "node scripts/docs-check.js"
|
|
13
|
-
},
|
|
14
|
-
"engines": {
|
|
15
|
-
"node": ">=18"
|
|
16
|
-
},
|
|
17
|
-
"files": [
|
|
18
|
-
"bin/",
|
|
19
|
-
"src/",
|
|
20
|
-
"plugins/",
|
|
21
|
-
"README.md",
|
|
22
|
-
"docs/cli_help_mini.md"
|
|
23
|
-
],
|
|
24
|
-
"keywords": [
|
|
25
|
-
"test",
|
|
26
|
-
"demo",
|
|
27
|
-
"placeholder"
|
|
28
|
-
],
|
|
29
|
-
"license": "MIT",
|
|
30
|
-
"dependencies": {
|
|
31
|
-
"undici": "^5.28.4"
|
|
32
|
-
},
|
|
33
|
-
"optionalDependencies": {
|
|
34
|
-
"@ai-sdk/openai-compatible": "2.0.41",
|
|
35
|
-
"@ai-sdk/openai": "3.0.84",
|
|
36
|
-
"zod": "^3.25.76"
|
|
37
|
-
}
|
|
38
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "mslxdff",
|
|
3
|
+
"version": "0.1.203",
|
|
4
|
+
"description": "测试项目,请勿使用。",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"bin": {
|
|
7
|
+
"mslxdff": "bin/mslxdff.js"
|
|
8
|
+
},
|
|
9
|
+
"scripts": {
|
|
10
|
+
"start": "node bin/mslxdff.js",
|
|
11
|
+
"test": "node --test --test-concurrency=1 test/*.test.js",
|
|
12
|
+
"docs:check": "node scripts/docs-check.js"
|
|
13
|
+
},
|
|
14
|
+
"engines": {
|
|
15
|
+
"node": ">=18"
|
|
16
|
+
},
|
|
17
|
+
"files": [
|
|
18
|
+
"bin/",
|
|
19
|
+
"src/",
|
|
20
|
+
"plugins/",
|
|
21
|
+
"README.md",
|
|
22
|
+
"docs/cli_help_mini.md"
|
|
23
|
+
],
|
|
24
|
+
"keywords": [
|
|
25
|
+
"test",
|
|
26
|
+
"demo",
|
|
27
|
+
"placeholder"
|
|
28
|
+
],
|
|
29
|
+
"license": "MIT",
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"undici": "^5.28.4"
|
|
32
|
+
},
|
|
33
|
+
"optionalDependencies": {
|
|
34
|
+
"@ai-sdk/openai-compatible": "2.0.41",
|
|
35
|
+
"@ai-sdk/openai": "3.0.84",
|
|
36
|
+
"zod": "^3.25.76"
|
|
37
|
+
}
|
|
38
|
+
}
|
package/plugins/README.md
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
# mslxdff 官方插件目录
|
|
2
|
-
|
|
3
|
-
本目录随 mslxdff 包分发(`*.mjs` 会被自动加载)。**注意:auto-update 重装包时本目录会被重置**——你自己的正式插件请放 `~/.config/mslxdff/plugins/`(升级永不丢)。
|
|
4
|
-
|
|
5
|
-
优先级:同名文件时用户目录(`~/.config/mslxdff/plugins/`)覆盖本目录。
|
|
6
|
-
|
|
7
|
-
## 目录约定
|
|
8
|
-
|
|
9
|
-
- `*.mjs` / `*.js` — 插件模块(default export `{ name, version, hooks, onEvent?, createUpstream? }`)
|
|
10
|
-
- `*.example` — 示例模板,不会被加载;复制成 `.mjs` 并去掉后缀即可启用
|
|
11
|
-
|
|
12
|
-
## Hook 全表
|
|
13
|
-
|
|
14
|
-
见 `docs/plugins.md`。
|
|
1
|
+
# mslxdff 官方插件目录
|
|
2
|
+
|
|
3
|
+
本目录随 mslxdff 包分发(`*.mjs` 会被自动加载)。**注意:auto-update 重装包时本目录会被重置**——你自己的正式插件请放 `~/.config/mslxdff/plugins/`(升级永不丢)。
|
|
4
|
+
|
|
5
|
+
优先级:同名文件时用户目录(`~/.config/mslxdff/plugins/`)覆盖本目录。
|
|
6
|
+
|
|
7
|
+
## 目录约定
|
|
8
|
+
|
|
9
|
+
- `*.mjs` / `*.js` — 插件模块(default export `{ name, version, hooks, onEvent?, createUpstream? }`)
|
|
10
|
+
- `*.example` — 示例模板,不会被加载;复制成 `.mjs` 并去掉后缀即可启用
|
|
11
|
+
|
|
12
|
+
## Hook 全表
|
|
13
|
+
|
|
14
|
+
见 `docs/plugins.md`。
|
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
// 示例:把首选模型排到最前(复制为 prefer-model.mjs 即生效;或放 ~/.config/mslxdff/plugins/ 永久生效)
|
|
2
|
-
const PREFER = "big-pickle";
|
|
3
|
-
|
|
4
|
-
export default {
|
|
5
|
-
name: "prefer-model",
|
|
6
|
-
version: "1.0.0",
|
|
7
|
-
description: `auto 时优先使用 ${PREFER}`,
|
|
8
|
-
hooks: {
|
|
9
|
-
"model:select": (ctx) => {
|
|
10
|
-
if (!ctx.order.includes(PREFER)) return;
|
|
11
|
-
return [PREFER, ...ctx.order.filter((m) => m !== PREFER)];
|
|
12
|
-
},
|
|
13
|
-
},
|
|
14
|
-
};
|
|
1
|
+
// 示例:把首选模型排到最前(复制为 prefer-model.mjs 即生效;或放 ~/.config/mslxdff/plugins/ 永久生效)
|
|
2
|
+
const PREFER = "big-pickle";
|
|
3
|
+
|
|
4
|
+
export default {
|
|
5
|
+
name: "prefer-model",
|
|
6
|
+
version: "1.0.0",
|
|
7
|
+
description: `auto 时优先使用 ${PREFER}`,
|
|
8
|
+
hooks: {
|
|
9
|
+
"model:select": (ctx) => {
|
|
10
|
+
if (!ctx.order.includes(PREFER)) return;
|
|
11
|
+
return [PREFER, ...ctx.order.filter((m) => m !== PREFER)];
|
|
12
|
+
},
|
|
13
|
+
},
|
|
14
|
+
};
|
|
@@ -1,61 +1,61 @@
|
|
|
1
|
-
// /v1/messages/count_tokens 的 token 估算(从 translate.js 拆出,控单文件体积 ≤20KB 硬门)。
|
|
2
|
-
// 精度非目标:Anthropic 官方说明该端点缺失时客户端本来就退回字符估算。
|
|
3
|
-
// 权重口径的单一来源在 ../text-tokens.js(ADR-0055):旧版整段 `chars/4` 把中文低报约 4 倍,而客户端
|
|
4
|
-
// 正是拿这个读数决定何时压实上下文 —— 读数偏低就一路不压实,撞上游「上下文超限」才炸。
|
|
5
|
-
import { toolResultTextOf } from "./translate.js";
|
|
6
|
-
import { estimateTextTokens, createTokenCounter } from "../text-tokens.js";
|
|
7
|
-
|
|
8
|
-
// 图片不吃字符但占真实上下文,按固定面值计;总长设硬上限防极端请求把估算推到天文数字
|
|
9
|
-
const IMAGE_TOKENS = 2000;
|
|
10
|
-
const CHAR_CAP = 32_000_000;
|
|
11
|
-
|
|
12
|
-
// 请求侧按「一条消息」聚成一段再套一次公式:粒度对齐参照实现 wb_proxy.py:14267-14278
|
|
13
|
-
// (每条消息 content 整段一次 + 每个 tool_call 一次)。逐块各套会把「非空至少 1」下限乘上块数
|
|
14
|
-
// —— 几十个 1 字符 text 块能被高报几十倍(评审 P3:方向虽偏高安全,但与生成侧口径分叉就是第二个真相源)。
|
|
15
|
-
function add(acc, text) {
|
|
16
|
-
// 超限后停止累计(chars 与 tokens 一起停):只护 chars 会让 tokens 无界增长,
|
|
17
|
-
// 客户端拿这个读数决定何时压实上下文 ⇒ 超长请求触发一次毫无必要的激进压缩。
|
|
18
|
-
if (acc.chars > CHAR_CAP) return;
|
|
19
|
-
const s = text == null ? "" : String(text);
|
|
20
|
-
const c = createTokenCounter();
|
|
21
|
-
c.add(s);
|
|
22
|
-
acc.chars += s.length;
|
|
23
|
-
acc.tokens += c.tokens();
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
function countMessage(m, acc) {
|
|
27
|
-
if (!m || typeof m !== "object") return;
|
|
28
|
-
const blocks = typeof m.content === "string" ? [{ type: "text", text: m.content }] : Array.isArray(m.content) ? m.content : null;
|
|
29
|
-
if (!blocks) { add(acc, JSON.stringify(m.content ?? "")); return; }
|
|
30
|
-
const c = createTokenCounter();
|
|
31
|
-
let chars = 0;
|
|
32
|
-
const put = (s) => { c.add(s); chars += s.length; };
|
|
33
|
-
for (const b of blocks) {
|
|
34
|
-
if (!b || typeof b !== "object") continue;
|
|
35
|
-
if (typeof b.text === "string") put(b.text);
|
|
36
|
-
else if (b.type === "tool_use") put(String(b.name || "") + JSON.stringify(b.input ?? {}));
|
|
37
|
-
else if (b.type === "tool_result") put(toolResultTextOf(b.content)); // 工具结果是大头,必须算
|
|
38
|
-
else if (b.type === "thinking") put(String(b.thinking || ""));
|
|
39
|
-
else if (b.type === "image") acc.images += 1;
|
|
40
|
-
}
|
|
41
|
-
// tokens 与 chars 必须**同受 CHAR_CAP 约束**:只护 chars 不护 tokens 时,超长请求(readBody 无大小上限,
|
|
42
|
-
// 实测 10 条 4000 万字符消息 → 1.11 亿 tokens)会把读数推到天文数字,而客户端正是拿它决定何时压实上下文
|
|
43
|
-
// ⇒ 一次毫无必要的激进压缩。超限后不再累加,保留已计部分。
|
|
44
|
-
if (acc.chars <= CHAR_CAP) {
|
|
45
|
-
acc.chars += chars;
|
|
46
|
-
acc.tokens += c.tokens();
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/** 字符估算版 token 计数(Anthropic 官方:端点缺失时客户端本来就退回字符估算,精度非目标)。 */
|
|
51
|
-
export function estimateTokens(req = {}) {
|
|
52
|
-
const acc = { chars: 0, tokens: 0, images: 0 };
|
|
53
|
-
const sys = typeof req.system === "string" ? [{ type: "text", text: req.system }] : Array.isArray(req.system) ? req.system : [];
|
|
54
|
-
for (const b of sys) add(acc, b && typeof b.text === "string" ? b.text : "");
|
|
55
|
-
for (const m of Array.isArray(req.messages) ? req.messages : []) countMessage(m, acc);
|
|
56
|
-
if (Array.isArray(req.tools)) add(acc, JSON.stringify(req.tools));
|
|
57
|
-
// 字段名以 Anthropic 线上为准:`POST /v1/messages/count_tokens` 回 `{"input_tokens": N}`;
|
|
58
|
-
// 额外带一份 count_tokens 仅作人读兼容(Claude Code 读的是 input_tokens)。
|
|
59
|
-
const n = Math.max(1, acc.tokens) + acc.images * IMAGE_TOKENS;
|
|
60
|
-
return { input_tokens: n, count_tokens: n };
|
|
61
|
-
}
|
|
1
|
+
// /v1/messages/count_tokens 的 token 估算(从 translate.js 拆出,控单文件体积 ≤20KB 硬门)。
|
|
2
|
+
// 精度非目标:Anthropic 官方说明该端点缺失时客户端本来就退回字符估算。
|
|
3
|
+
// 权重口径的单一来源在 ../text-tokens.js(ADR-0055):旧版整段 `chars/4` 把中文低报约 4 倍,而客户端
|
|
4
|
+
// 正是拿这个读数决定何时压实上下文 —— 读数偏低就一路不压实,撞上游「上下文超限」才炸。
|
|
5
|
+
import { toolResultTextOf } from "./translate.js";
|
|
6
|
+
import { estimateTextTokens, createTokenCounter } from "../text-tokens.js";
|
|
7
|
+
|
|
8
|
+
// 图片不吃字符但占真实上下文,按固定面值计;总长设硬上限防极端请求把估算推到天文数字
|
|
9
|
+
const IMAGE_TOKENS = 2000;
|
|
10
|
+
const CHAR_CAP = 32_000_000;
|
|
11
|
+
|
|
12
|
+
// 请求侧按「一条消息」聚成一段再套一次公式:粒度对齐参照实现 wb_proxy.py:14267-14278
|
|
13
|
+
// (每条消息 content 整段一次 + 每个 tool_call 一次)。逐块各套会把「非空至少 1」下限乘上块数
|
|
14
|
+
// —— 几十个 1 字符 text 块能被高报几十倍(评审 P3:方向虽偏高安全,但与生成侧口径分叉就是第二个真相源)。
|
|
15
|
+
function add(acc, text) {
|
|
16
|
+
// 超限后停止累计(chars 与 tokens 一起停):只护 chars 会让 tokens 无界增长,
|
|
17
|
+
// 客户端拿这个读数决定何时压实上下文 ⇒ 超长请求触发一次毫无必要的激进压缩。
|
|
18
|
+
if (acc.chars > CHAR_CAP) return;
|
|
19
|
+
const s = text == null ? "" : String(text);
|
|
20
|
+
const c = createTokenCounter();
|
|
21
|
+
c.add(s);
|
|
22
|
+
acc.chars += s.length;
|
|
23
|
+
acc.tokens += c.tokens();
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function countMessage(m, acc) {
|
|
27
|
+
if (!m || typeof m !== "object") return;
|
|
28
|
+
const blocks = typeof m.content === "string" ? [{ type: "text", text: m.content }] : Array.isArray(m.content) ? m.content : null;
|
|
29
|
+
if (!blocks) { add(acc, JSON.stringify(m.content ?? "")); return; }
|
|
30
|
+
const c = createTokenCounter();
|
|
31
|
+
let chars = 0;
|
|
32
|
+
const put = (s) => { c.add(s); chars += s.length; };
|
|
33
|
+
for (const b of blocks) {
|
|
34
|
+
if (!b || typeof b !== "object") continue;
|
|
35
|
+
if (typeof b.text === "string") put(b.text);
|
|
36
|
+
else if (b.type === "tool_use") put(String(b.name || "") + JSON.stringify(b.input ?? {}));
|
|
37
|
+
else if (b.type === "tool_result") put(toolResultTextOf(b.content)); // 工具结果是大头,必须算
|
|
38
|
+
else if (b.type === "thinking") put(String(b.thinking || ""));
|
|
39
|
+
else if (b.type === "image") acc.images += 1;
|
|
40
|
+
}
|
|
41
|
+
// tokens 与 chars 必须**同受 CHAR_CAP 约束**:只护 chars 不护 tokens 时,超长请求(readBody 无大小上限,
|
|
42
|
+
// 实测 10 条 4000 万字符消息 → 1.11 亿 tokens)会把读数推到天文数字,而客户端正是拿它决定何时压实上下文
|
|
43
|
+
// ⇒ 一次毫无必要的激进压缩。超限后不再累加,保留已计部分。
|
|
44
|
+
if (acc.chars <= CHAR_CAP) {
|
|
45
|
+
acc.chars += chars;
|
|
46
|
+
acc.tokens += c.tokens();
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** 字符估算版 token 计数(Anthropic 官方:端点缺失时客户端本来就退回字符估算,精度非目标)。 */
|
|
51
|
+
export function estimateTokens(req = {}) {
|
|
52
|
+
const acc = { chars: 0, tokens: 0, images: 0 };
|
|
53
|
+
const sys = typeof req.system === "string" ? [{ type: "text", text: req.system }] : Array.isArray(req.system) ? req.system : [];
|
|
54
|
+
for (const b of sys) add(acc, b && typeof b.text === "string" ? b.text : "");
|
|
55
|
+
for (const m of Array.isArray(req.messages) ? req.messages : []) countMessage(m, acc);
|
|
56
|
+
if (Array.isArray(req.tools)) add(acc, JSON.stringify(req.tools));
|
|
57
|
+
// 字段名以 Anthropic 线上为准:`POST /v1/messages/count_tokens` 回 `{"input_tokens": N}`;
|
|
58
|
+
// 额外带一份 count_tokens 仅作人读兼容(Claude Code 读的是 input_tokens)。
|
|
59
|
+
const n = Math.max(1, acc.tokens) + acc.images * IMAGE_TOKENS;
|
|
60
|
+
return { input_tokens: n, count_tokens: n };
|
|
61
|
+
}
|
|
@@ -1,126 +1,126 @@
|
|
|
1
|
-
// 请求侧翻译:Anthropic Messages 请求体 → OpenAI chat 请求体(/v1/messages 外壳,ADR-0047)。
|
|
2
|
-
// 从 translate.js 拆出(ADR-0055):translate.js 贴 20KB 体积硬门,且「入站请求形状」与
|
|
3
|
-
// 「出站响应/usage 翻译」本就是两向关注点,拆后 translate.js 单向 import 本文件,不回环。
|
|
4
|
-
// 对外接口不变:调用方与测试仍从 anthropic/translate.js import(那边 re-export)。
|
|
5
|
-
//
|
|
6
|
-
// 纪律(别改回去):
|
|
7
|
-
// * 思考档位**原样透传**(thinking/effort/reasoning_effort/reasoningEffort/extra_body),语义归一交给
|
|
8
|
-
// 各 provider 与 src/thinking-effort.js —— 旧版在此丢掉,下游被迫补默认值(workbuddy 恒 high,ADR-0053);
|
|
9
|
-
// * 但**必须主动丢** output_config 原始对象 / context_management / 消息块内 thinking:Claude Code 对
|
|
10
|
-
// 不认识的 model ID 照发全套字段,块内 thinking 无法签发 signature,回传必被上游拒(ADR-0047)。
|
|
11
|
-
import { isServerTool } from "./web-search.js";
|
|
12
|
-
import { newToolUseId } from "./shared.js";
|
|
13
|
-
|
|
14
|
-
// system 允许字符串或 text 块数组;块间空行连接(缓存语义不保留,上游是 OpenAI)
|
|
15
|
-
function systemTextOf(system) {
|
|
16
|
-
if (typeof system === "string") return system;
|
|
17
|
-
if (!Array.isArray(system)) return "";
|
|
18
|
-
return system.map((b) => (b && typeof b.text === "string" ? b.text : "")).filter(Boolean).join("\n\n");
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
function imagePartOf(block) {
|
|
22
|
-
const src = block?.source || {};
|
|
23
|
-
if (src.type === "base64" && src.data) {
|
|
24
|
-
return { type: "image_url", image_url: { url: `data:${src.media_type || "image/png"};base64,${src.data}` } };
|
|
25
|
-
}
|
|
26
|
-
if (src.type === "url" && src.url) return { type: "image_url", image_url: { url: String(src.url) } };
|
|
27
|
-
return null;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export function toolResultTextOf(content) {
|
|
31
|
-
if (typeof content === "string") return content;
|
|
32
|
-
if (Array.isArray(content)) {
|
|
33
|
-
return content
|
|
34
|
-
.map((p) => (typeof p === "string" ? p : p && typeof p.text === "string" ? p.text : ""))
|
|
35
|
-
.filter(Boolean)
|
|
36
|
-
.join("\n");
|
|
37
|
-
}
|
|
38
|
-
return content == null ? "" : String(content);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/** Anthropic Messages 请求体 → OpenAI chat 请求体(直接喂既有 ChatPipeline)。 */
|
|
42
|
-
export function messagesToChatBody(req = {}) {
|
|
43
|
-
const model = String(req.model || "").trim();
|
|
44
|
-
if (!model) throw new Error("messages: 缺少 model");
|
|
45
|
-
const messages = [];
|
|
46
|
-
const sys = systemTextOf(req.system);
|
|
47
|
-
if (sys) messages.push({ role: "system", content: sys });
|
|
48
|
-
|
|
49
|
-
// 重复 tool_use_id 保首个、丢后续:客户端重试/重放历史会把同一工具结果存两份,
|
|
50
|
-
// 部分上游按 call_id 查重直接 400(同 responses/translate.js:57-59 的实测教训)。
|
|
51
|
-
const seenToolResult = new Set();
|
|
52
|
-
|
|
53
|
-
for (const m of Array.isArray(req.messages) ? req.messages : []) {
|
|
54
|
-
if (!m || typeof m !== "object") continue;
|
|
55
|
-
// role=system/developer 必须保留:Claude Code ≥2.1.286 的 mid-conversation system beta 就往 messages[] 里塞系统指令,
|
|
56
|
-
const role = m.role === "assistant" ? "assistant" : m.role === "system" || m.role === "developer" ? "system" : "user";
|
|
57
|
-
const blocks = typeof m.content === "string" ? [{ type: "text", text: m.content }] : Array.isArray(m.content) ? m.content : [];
|
|
58
|
-
let text = "";
|
|
59
|
-
const imgs = [];
|
|
60
|
-
const toolCalls = [];
|
|
61
|
-
for (const b of blocks) {
|
|
62
|
-
if (!b || typeof b !== "object") continue;
|
|
63
|
-
if (b.type === "text") text += typeof b.text === "string" ? b.text : "";
|
|
64
|
-
else if (b.type === "image") { const p = imagePartOf(b); if (p) imgs.push(p); }
|
|
65
|
-
else if (b.type === "tool_use") {
|
|
66
|
-
let args = "{}";
|
|
67
|
-
try { args = JSON.stringify(b.input ?? {}); } catch { args = "{}"; }
|
|
68
|
-
toolCalls.push({ id: String(b.id || newToolUseId()), type: "function", function: { name: String(b.name || ""), arguments: args } });
|
|
69
|
-
} else if (b.type === "tool_result") {
|
|
70
|
-
// tool 消息必须先于同轮 user 文本进数组:OpenAI 侧要求 role:tool 紧跟其 assistant
|
|
71
|
-
const key = String(b.tool_use_id || "");
|
|
72
|
-
if (key) { if (seenToolResult.has(key)) continue; seenToolResult.add(key); }
|
|
73
|
-
const body = toolResultTextOf(b.content);
|
|
74
|
-
messages.push({ role: "tool", tool_call_id: key, content: b.is_error ? `[tool_error] ${body}` : body });
|
|
75
|
-
}
|
|
76
|
-
// thinking / redacted_thinking 忽略:我们无法签发 signature,回传必被上游拒(ADR-0047)
|
|
77
|
-
}
|
|
78
|
-
if (!text && !imgs.length && !toolCalls.length) continue;
|
|
79
|
-
const msg = { role, content: imgs.length ? [...(text ? [{ type: "text", text }] : []), ...imgs] : text };
|
|
80
|
-
if (toolCalls.length) msg.tool_calls = toolCalls;
|
|
81
|
-
messages.push(msg);
|
|
82
|
-
}
|
|
83
|
-
// 空会话直接 400:绝不伪造一句 "hi" 打上游(那会把假提问写进用户上下文并白烧一发额度)
|
|
84
|
-
if (!messages.some((x) => x.role !== "system")) throw new Error("messages: 缺少用户消息");
|
|
85
|
-
|
|
86
|
-
const body = { model, messages, stream: Boolean(req.stream) };
|
|
87
|
-
// 流式必须让上游把 usage 放进收尾帧,否则 message_delta.usage 恒 0(OpenAI 规范字段)。
|
|
88
|
-
// 个别上游不认 stream_options 会 400 → `MSLXDFF_ANTHROPIC_STREAM_USAGE=0` 关掉。
|
|
89
|
-
if (body.stream && process.env.MSLXDFF_ANTHROPIC_STREAM_USAGE !== "0") body.stream_options = { include_usage: true };
|
|
90
|
-
// Anthropic 的 server tool(web_search_*/web_fetch_*/…)没有 input_schema,折成假 function 会
|
|
91
|
-
// 让上游收到「空参数空描述」的工具(严格端点直接 422),本网关不实现的发前一律剥除(ADR-0049)。
|
|
92
|
-
const fnTools = (Array.isArray(req.tools) ? req.tools : []).filter((t) => !isServerTool(t));
|
|
93
|
-
if (fnTools.length) {
|
|
94
|
-
body.tools = fnTools.map((t) => ({
|
|
95
|
-
type: "function",
|
|
96
|
-
function: { name: String(t?.name || ""), description: String(t?.description || ""), parameters: t?.input_schema || {} },
|
|
97
|
-
}));
|
|
98
|
-
}
|
|
99
|
-
const keptNames = new Set(fnTools.map((t) => String(t?.name || "")));
|
|
100
|
-
const tc = req.tool_choice;
|
|
101
|
-
if (tc === "auto" || (tc && typeof tc === "object" && tc.type === "auto")) body.tool_choice = "auto";
|
|
102
|
-
else if (tc && typeof tc === "object" && tc.type === "any") body.tool_choice = "required";
|
|
103
|
-
// 点名一个已被剥掉的工具时降为 auto:上游会按不存在的函数强制点名,那样一定拿不到有用回答
|
|
104
|
-
else if (tc && typeof tc === "object" && tc.type === "tool") body.tool_choice = keptNames.has(String(tc.name || "")) ? { type: "function", function: { name: String(tc.name || "") } } : "auto";
|
|
105
|
-
else if (tc && typeof tc === "object" && tc.type === "none") body.tool_choice = "none";
|
|
106
|
-
if (req.max_tokens != null && Number.isFinite(Number(req.max_tokens))) body.max_tokens = Number(req.max_tokens);
|
|
107
|
-
if (req.temperature != null) body.temperature = req.temperature;
|
|
108
|
-
if (req.top_p != null) body.top_p = req.top_p;
|
|
109
|
-
if (Array.isArray(req.stop_sequences) && req.stop_sequences.length) body.stop = req.stop_sequences;
|
|
110
|
-
// 思考档位原样透传(不解释语义,语义归一交给各 provider / src/thinking-effort.js):
|
|
111
|
-
// 客户端显式声明必须一路带到上游,旧版在此丢掉 → 下游被迫补默认值(workbuddy 恒 high)。
|
|
112
|
-
if (req.thinking != null) body.thinking = req.thinking;
|
|
113
|
-
// 档位载体只收「非空 trimmed 字符串」:空串声明不算声明,不该发出去顶掉下游默认值。
|
|
114
|
-
for (const k of ["effort", "reasoning_effort", "reasoningEffort"]) {
|
|
115
|
-
const v = typeof req[k] === "string" ? req[k].trim() : "";
|
|
116
|
-
if (v) body[k] = v;
|
|
117
|
-
}
|
|
118
|
-
if (req.extra_body != null) body.extra_body = req.extra_body;
|
|
119
|
-
// output_config.effort 是 Claude Code 的另一档位载体(beta),原始对象上游不认故不外发,
|
|
120
|
-
// 但档位本身不能丢 → 归一到 reasoning_effort(客户端已显式声明时以声明为准)。
|
|
121
|
-
const ocEffort = req.output_config && typeof req.output_config === "object" ? req.output_config.effort : undefined;
|
|
122
|
-
if (ocEffort != null && body.reasoning_effort == null && body.reasoningEffort == null) {
|
|
123
|
-
body.reasoning_effort = ocEffort;
|
|
124
|
-
}
|
|
125
|
-
return body;
|
|
126
|
-
}
|
|
1
|
+
// 请求侧翻译:Anthropic Messages 请求体 → OpenAI chat 请求体(/v1/messages 外壳,ADR-0047)。
|
|
2
|
+
// 从 translate.js 拆出(ADR-0055):translate.js 贴 20KB 体积硬门,且「入站请求形状」与
|
|
3
|
+
// 「出站响应/usage 翻译」本就是两向关注点,拆后 translate.js 单向 import 本文件,不回环。
|
|
4
|
+
// 对外接口不变:调用方与测试仍从 anthropic/translate.js import(那边 re-export)。
|
|
5
|
+
//
|
|
6
|
+
// 纪律(别改回去):
|
|
7
|
+
// * 思考档位**原样透传**(thinking/effort/reasoning_effort/reasoningEffort/extra_body),语义归一交给
|
|
8
|
+
// 各 provider 与 src/thinking-effort.js —— 旧版在此丢掉,下游被迫补默认值(workbuddy 恒 high,ADR-0053);
|
|
9
|
+
// * 但**必须主动丢** output_config 原始对象 / context_management / 消息块内 thinking:Claude Code 对
|
|
10
|
+
// 不认识的 model ID 照发全套字段,块内 thinking 无法签发 signature,回传必被上游拒(ADR-0047)。
|
|
11
|
+
import { isServerTool } from "./web-search.js";
|
|
12
|
+
import { newToolUseId } from "./shared.js";
|
|
13
|
+
|
|
14
|
+
// system 允许字符串或 text 块数组;块间空行连接(缓存语义不保留,上游是 OpenAI)
|
|
15
|
+
function systemTextOf(system) {
|
|
16
|
+
if (typeof system === "string") return system;
|
|
17
|
+
if (!Array.isArray(system)) return "";
|
|
18
|
+
return system.map((b) => (b && typeof b.text === "string" ? b.text : "")).filter(Boolean).join("\n\n");
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function imagePartOf(block) {
|
|
22
|
+
const src = block?.source || {};
|
|
23
|
+
if (src.type === "base64" && src.data) {
|
|
24
|
+
return { type: "image_url", image_url: { url: `data:${src.media_type || "image/png"};base64,${src.data}` } };
|
|
25
|
+
}
|
|
26
|
+
if (src.type === "url" && src.url) return { type: "image_url", image_url: { url: String(src.url) } };
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function toolResultTextOf(content) {
|
|
31
|
+
if (typeof content === "string") return content;
|
|
32
|
+
if (Array.isArray(content)) {
|
|
33
|
+
return content
|
|
34
|
+
.map((p) => (typeof p === "string" ? p : p && typeof p.text === "string" ? p.text : ""))
|
|
35
|
+
.filter(Boolean)
|
|
36
|
+
.join("\n");
|
|
37
|
+
}
|
|
38
|
+
return content == null ? "" : String(content);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Anthropic Messages 请求体 → OpenAI chat 请求体(直接喂既有 ChatPipeline)。 */
|
|
42
|
+
export function messagesToChatBody(req = {}) {
|
|
43
|
+
const model = String(req.model || "").trim();
|
|
44
|
+
if (!model) throw new Error("messages: 缺少 model");
|
|
45
|
+
const messages = [];
|
|
46
|
+
const sys = systemTextOf(req.system);
|
|
47
|
+
if (sys) messages.push({ role: "system", content: sys });
|
|
48
|
+
|
|
49
|
+
// 重复 tool_use_id 保首个、丢后续:客户端重试/重放历史会把同一工具结果存两份,
|
|
50
|
+
// 部分上游按 call_id 查重直接 400(同 responses/translate.js:57-59 的实测教训)。
|
|
51
|
+
const seenToolResult = new Set();
|
|
52
|
+
|
|
53
|
+
for (const m of Array.isArray(req.messages) ? req.messages : []) {
|
|
54
|
+
if (!m || typeof m !== "object") continue;
|
|
55
|
+
// role=system/developer 必须保留:Claude Code ≥2.1.286 的 mid-conversation system beta 就往 messages[] 里塞系统指令,
|
|
56
|
+
const role = m.role === "assistant" ? "assistant" : m.role === "system" || m.role === "developer" ? "system" : "user";
|
|
57
|
+
const blocks = typeof m.content === "string" ? [{ type: "text", text: m.content }] : Array.isArray(m.content) ? m.content : [];
|
|
58
|
+
let text = "";
|
|
59
|
+
const imgs = [];
|
|
60
|
+
const toolCalls = [];
|
|
61
|
+
for (const b of blocks) {
|
|
62
|
+
if (!b || typeof b !== "object") continue;
|
|
63
|
+
if (b.type === "text") text += typeof b.text === "string" ? b.text : "";
|
|
64
|
+
else if (b.type === "image") { const p = imagePartOf(b); if (p) imgs.push(p); }
|
|
65
|
+
else if (b.type === "tool_use") {
|
|
66
|
+
let args = "{}";
|
|
67
|
+
try { args = JSON.stringify(b.input ?? {}); } catch { args = "{}"; }
|
|
68
|
+
toolCalls.push({ id: String(b.id || newToolUseId()), type: "function", function: { name: String(b.name || ""), arguments: args } });
|
|
69
|
+
} else if (b.type === "tool_result") {
|
|
70
|
+
// tool 消息必须先于同轮 user 文本进数组:OpenAI 侧要求 role:tool 紧跟其 assistant
|
|
71
|
+
const key = String(b.tool_use_id || "");
|
|
72
|
+
if (key) { if (seenToolResult.has(key)) continue; seenToolResult.add(key); }
|
|
73
|
+
const body = toolResultTextOf(b.content);
|
|
74
|
+
messages.push({ role: "tool", tool_call_id: key, content: b.is_error ? `[tool_error] ${body}` : body });
|
|
75
|
+
}
|
|
76
|
+
// thinking / redacted_thinking 忽略:我们无法签发 signature,回传必被上游拒(ADR-0047)
|
|
77
|
+
}
|
|
78
|
+
if (!text && !imgs.length && !toolCalls.length) continue;
|
|
79
|
+
const msg = { role, content: imgs.length ? [...(text ? [{ type: "text", text }] : []), ...imgs] : text };
|
|
80
|
+
if (toolCalls.length) msg.tool_calls = toolCalls;
|
|
81
|
+
messages.push(msg);
|
|
82
|
+
}
|
|
83
|
+
// 空会话直接 400:绝不伪造一句 "hi" 打上游(那会把假提问写进用户上下文并白烧一发额度)
|
|
84
|
+
if (!messages.some((x) => x.role !== "system")) throw new Error("messages: 缺少用户消息");
|
|
85
|
+
|
|
86
|
+
const body = { model, messages, stream: Boolean(req.stream) };
|
|
87
|
+
// 流式必须让上游把 usage 放进收尾帧,否则 message_delta.usage 恒 0(OpenAI 规范字段)。
|
|
88
|
+
// 个别上游不认 stream_options 会 400 → `MSLXDFF_ANTHROPIC_STREAM_USAGE=0` 关掉。
|
|
89
|
+
if (body.stream && process.env.MSLXDFF_ANTHROPIC_STREAM_USAGE !== "0") body.stream_options = { include_usage: true };
|
|
90
|
+
// Anthropic 的 server tool(web_search_*/web_fetch_*/…)没有 input_schema,折成假 function 会
|
|
91
|
+
// 让上游收到「空参数空描述」的工具(严格端点直接 422),本网关不实现的发前一律剥除(ADR-0049)。
|
|
92
|
+
const fnTools = (Array.isArray(req.tools) ? req.tools : []).filter((t) => !isServerTool(t));
|
|
93
|
+
if (fnTools.length) {
|
|
94
|
+
body.tools = fnTools.map((t) => ({
|
|
95
|
+
type: "function",
|
|
96
|
+
function: { name: String(t?.name || ""), description: String(t?.description || ""), parameters: t?.input_schema || {} },
|
|
97
|
+
}));
|
|
98
|
+
}
|
|
99
|
+
const keptNames = new Set(fnTools.map((t) => String(t?.name || "")));
|
|
100
|
+
const tc = req.tool_choice;
|
|
101
|
+
if (tc === "auto" || (tc && typeof tc === "object" && tc.type === "auto")) body.tool_choice = "auto";
|
|
102
|
+
else if (tc && typeof tc === "object" && tc.type === "any") body.tool_choice = "required";
|
|
103
|
+
// 点名一个已被剥掉的工具时降为 auto:上游会按不存在的函数强制点名,那样一定拿不到有用回答
|
|
104
|
+
else if (tc && typeof tc === "object" && tc.type === "tool") body.tool_choice = keptNames.has(String(tc.name || "")) ? { type: "function", function: { name: String(tc.name || "") } } : "auto";
|
|
105
|
+
else if (tc && typeof tc === "object" && tc.type === "none") body.tool_choice = "none";
|
|
106
|
+
if (req.max_tokens != null && Number.isFinite(Number(req.max_tokens))) body.max_tokens = Number(req.max_tokens);
|
|
107
|
+
if (req.temperature != null) body.temperature = req.temperature;
|
|
108
|
+
if (req.top_p != null) body.top_p = req.top_p;
|
|
109
|
+
if (Array.isArray(req.stop_sequences) && req.stop_sequences.length) body.stop = req.stop_sequences;
|
|
110
|
+
// 思考档位原样透传(不解释语义,语义归一交给各 provider / src/thinking-effort.js):
|
|
111
|
+
// 客户端显式声明必须一路带到上游,旧版在此丢掉 → 下游被迫补默认值(workbuddy 恒 high)。
|
|
112
|
+
if (req.thinking != null) body.thinking = req.thinking;
|
|
113
|
+
// 档位载体只收「非空 trimmed 字符串」:空串声明不算声明,不该发出去顶掉下游默认值。
|
|
114
|
+
for (const k of ["effort", "reasoning_effort", "reasoningEffort"]) {
|
|
115
|
+
const v = typeof req[k] === "string" ? req[k].trim() : "";
|
|
116
|
+
if (v) body[k] = v;
|
|
117
|
+
}
|
|
118
|
+
if (req.extra_body != null) body.extra_body = req.extra_body;
|
|
119
|
+
// output_config.effort 是 Claude Code 的另一档位载体(beta),原始对象上游不认故不外发,
|
|
120
|
+
// 但档位本身不能丢 → 归一到 reasoning_effort(客户端已显式声明时以声明为准)。
|
|
121
|
+
const ocEffort = req.output_config && typeof req.output_config === "object" ? req.output_config.effort : undefined;
|
|
122
|
+
if (ocEffort != null && body.reasoning_effort == null && body.reasoningEffort == null) {
|
|
123
|
+
body.reasoning_effort = ocEffort;
|
|
124
|
+
}
|
|
125
|
+
return body;
|
|
126
|
+
}
|
package/src/anthropic/shared.js
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
// /v1/messages 外壳的公共原语:消息/工具 id 与 Anthropic 协议错误体。
|
|
2
|
-
// ADR-0055 把请求侧拆去 request-body.js 时独立成档,让 request-body.js 与 translate.js 单向依赖它,
|
|
3
|
-
// 避免两文件互相 import 成环(函数提升虽能跑,顶层常量会 TDZ,属定时炸弹)。
|
|
4
|
-
// 共用面:src/anthropic/request-body.js(newToolUseId)、src/anthropic/translate.js(newMessageId/
|
|
5
|
-
// newToolUseId,并对外 re-export 三个符号,调用方与测试的 import 路径不变)。
|
|
6
|
-
|
|
7
|
-
let seq = 0;
|
|
8
|
-
export function newMessageId() {
|
|
9
|
-
return `msg_${Date.now().toString(36)}${(seq++).toString(36)}`;
|
|
10
|
-
}
|
|
11
|
-
export function newToolUseId() {
|
|
12
|
-
return `toolu_${Date.now().toString(36)}${(seq++).toString(36)}`;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
/** Anthropic 协议错误体形状(非流式 JSON 与流式 in-band error 帧共用)。 */
|
|
16
|
-
export function anthropicError(type, message) {
|
|
17
|
-
return { type: "error", error: { type: String(type || "api_error"), message: String(message || "error").slice(0, 500) } };
|
|
18
|
-
}
|
|
1
|
+
// /v1/messages 外壳的公共原语:消息/工具 id 与 Anthropic 协议错误体。
|
|
2
|
+
// ADR-0055 把请求侧拆去 request-body.js 时独立成档,让 request-body.js 与 translate.js 单向依赖它,
|
|
3
|
+
// 避免两文件互相 import 成环(函数提升虽能跑,顶层常量会 TDZ,属定时炸弹)。
|
|
4
|
+
// 共用面:src/anthropic/request-body.js(newToolUseId)、src/anthropic/translate.js(newMessageId/
|
|
5
|
+
// newToolUseId,并对外 re-export 三个符号,调用方与测试的 import 路径不变)。
|
|
6
|
+
|
|
7
|
+
let seq = 0;
|
|
8
|
+
export function newMessageId() {
|
|
9
|
+
return `msg_${Date.now().toString(36)}${(seq++).toString(36)}`;
|
|
10
|
+
}
|
|
11
|
+
export function newToolUseId() {
|
|
12
|
+
return `toolu_${Date.now().toString(36)}${(seq++).toString(36)}`;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** Anthropic 协议错误体形状(非流式 JSON 与流式 in-band error 帧共用)。 */
|
|
16
|
+
export function anthropicError(type, message) {
|
|
17
|
+
return { type: "error", error: { type: String(type || "api_error"), message: String(message || "error").slice(0, 500) } };
|
|
18
|
+
}
|