@openllmsh/sdk 0.0.0-stage → 2.10.12-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (245) hide show
  1. package/ARCHITECTURE.md +43 -0
  2. package/LICENSE +202 -0
  3. package/README.md +142 -2
  4. package/assets/openllm-light.svg +18 -0
  5. package/assets/openllm.svg +18 -0
  6. package/client/chat.ts +140 -0
  7. package/client/devices.ts +269 -0
  8. package/client/http.ts +72 -0
  9. package/client/index.ts +129 -0
  10. package/client/relay-mux.ts +547 -0
  11. package/client/types.ts +51 -0
  12. package/client/vault.ts +239 -0
  13. package/connect/chat.ts +58 -0
  14. package/connect/client.ts +240 -0
  15. package/connect/config.ts +47 -0
  16. package/connect/errors.ts +121 -0
  17. package/connect/index.ts +23 -0
  18. package/connect/oauth.ts +265 -0
  19. package/connect/routes.ts +234 -0
  20. package/connect/store.ts +107 -0
  21. package/mcp/handle.ts +24 -0
  22. package/mcp/index.ts +76 -0
  23. package/mcp/server.ts +319 -0
  24. package/mcp/tools/marketplace.ts +439 -0
  25. package/mcp/tools/memory-client.ts +297 -0
  26. package/mcp/tools/memory-config.ts +32 -0
  27. package/mcp/tools/memory.ts +369 -0
  28. package/mcp/tools/model-priority.ts +88 -0
  29. package/mcp/tools/openllm.ts +883 -0
  30. package/mcp/types.ts +28 -0
  31. package/openapi/call.ts +90 -0
  32. package/openapi/index.ts +8 -0
  33. package/openapi/openapi.json +80213 -0
  34. package/openapi/operations.ts +2075 -0
  35. package/package.json +39 -4
  36. package/protocol/CONTRIBUTING.md +21 -0
  37. package/protocol/README.md +134 -0
  38. package/protocol/access.ts +186 -0
  39. package/protocol/agent-prompt-text.ts +32 -0
  40. package/protocol/agent.ts +991 -0
  41. package/protocol/api-key-syntax.ts +105 -0
  42. package/protocol/assets/openllm-light.svg +18 -0
  43. package/protocol/assets/openllm.svg +18 -0
  44. package/protocol/audio.ts +29 -0
  45. package/protocol/auth.ts +142 -0
  46. package/protocol/changelog.ts +155 -0
  47. package/protocol/chat.ts +458 -0
  48. package/protocol/client-registry.ts +356 -0
  49. package/protocol/config.ts +345 -0
  50. package/protocol/control-channel.ts +115 -0
  51. package/protocol/cooldown-reason.ts +78 -0
  52. package/protocol/cost.ts +10 -0
  53. package/protocol/daemon-port.ts +31 -0
  54. package/protocol/daemon.ts +1524 -0
  55. package/protocol/default-chains.ts +66 -0
  56. package/protocol/device-state.ts +85 -0
  57. package/protocol/doctor-report-local.ts +443 -0
  58. package/protocol/doctor-report-policy.ts +119 -0
  59. package/protocol/doctor-report.ts +947 -0
  60. package/protocol/errors.ts +14 -0
  61. package/protocol/execution-selection.ts +141 -0
  62. package/protocol/hop-grant.ts +33 -0
  63. package/protocol/image-edit-parse.ts +263 -0
  64. package/protocol/images.ts +298 -0
  65. package/protocol/index.ts +74 -0
  66. package/protocol/inference-headers.ts +3 -0
  67. package/protocol/keepalive.ts +69 -0
  68. package/protocol/local-auth.ts +300 -0
  69. package/protocol/local-device-control.ts +133 -0
  70. package/protocol/local-provider.ts +288 -0
  71. package/protocol/local-session.ts +118 -0
  72. package/protocol/marketplace.ts +187 -0
  73. package/protocol/media-attempt.ts +81 -0
  74. package/protocol/media-bytes.ts +28 -0
  75. package/protocol/media-default.ts +357 -0
  76. package/protocol/media-dimensions.ts +132 -0
  77. package/protocol/media-input.ts +61 -0
  78. package/protocol/media-model.ts +142 -0
  79. package/protocol/media-option-issue.ts +90 -0
  80. package/protocol/media-types.ts +70 -0
  81. package/protocol/model-match.ts +1051 -0
  82. package/protocol/model-settings.ts +327 -0
  83. package/protocol/models.ts +767 -0
  84. package/protocol/mux.ts +722 -0
  85. package/protocol/plan-surface.ts +59 -0
  86. package/protocol/provider-models.ts +16 -0
  87. package/protocol/provider-status.ts +206 -0
  88. package/protocol/provider-tier.ts +67 -0
  89. package/protocol/provider-usage.ts +263 -0
  90. package/protocol/providers.ts +901 -0
  91. package/protocol/public-usage-index.ts +354 -0
  92. package/protocol/realtime-generated.ts +31 -0
  93. package/protocol/realtime.ts +324 -0
  94. package/protocol/registry-pointer.ts +118 -0
  95. package/protocol/relay.ts +702 -0
  96. package/protocol/replay-session.ts +11 -0
  97. package/protocol/responses.ts +280 -0
  98. package/protocol/semver.ts +182 -0
  99. package/protocol/setup-mode.ts +22 -0
  100. package/protocol/sha256-hex.ts +98 -0
  101. package/protocol/stats.ts +406 -0
  102. package/protocol/streaming.ts +19 -0
  103. package/protocol/subscription-daemon-floors.ts +33 -0
  104. package/protocol/subscription-provider-compat.ts +104 -0
  105. package/protocol/subscription-provider.ts +32 -0
  106. package/protocol/team-grant.ts +17 -0
  107. package/protocol/team-shareable.ts +61 -0
  108. package/protocol/transcription-encoding.ts +76 -0
  109. package/protocol/url-locality-dns.ts +37 -0
  110. package/protocol/url-locality.ts +288 -0
  111. package/protocol/usage-snapshots.ts +9 -0
  112. package/protocol/vendor-cli-install.ts +29 -0
  113. package/protocol/video-job-plan.ts +50 -0
  114. package/protocol/videos.ts +227 -0
  115. package/tsconfig.json +14 -0
  116. package/tunnel/ARCHITECTURE.md +111 -0
  117. package/tunnel/LICENSE +33 -0
  118. package/tunnel/README.md +45 -0
  119. package/tunnel/assets/openllm-light.svg +18 -0
  120. package/tunnel/assets/openllm.svg +18 -0
  121. package/tunnel/channel-envelope.ts +139 -0
  122. package/tunnel/codec.ts +191 -0
  123. package/tunnel/device-anchor.ts +229 -0
  124. package/tunnel/device-control.ts +118 -0
  125. package/tunnel/device-grant.ts +173 -0
  126. package/tunnel/fleet.ts +77 -0
  127. package/tunnel/index.ts +22 -0
  128. package/tunnel/mux.ts +580 -0
  129. package/tunnel/peer-link.ts +390 -0
  130. package/tunnel/rtc-auth.ts +415 -0
  131. package/tunnel/rtc-duplex.ts +187 -0
  132. package/tunnel/rtc-ice.ts +136 -0
  133. package/tunnel/secure.ts +1298 -0
  134. package/tunnel/stream-reset-error.ts +14 -0
  135. package/tunnel/streams.ts +1430 -0
  136. package/tunnel/team-grant.ts +302 -0
  137. package/tunnel/wire-bytes.ts +102 -0
  138. package/vault/ARCHITECTURE.md +470 -0
  139. package/vault/LICENSE +33 -0
  140. package/vault/api-key.ts +190 -0
  141. package/vault/argon2.ts +106 -0
  142. package/vault/cli-auth-seal.ts +157 -0
  143. package/vault/cred-seal.ts +146 -0
  144. package/vault/credential.ts +55 -0
  145. package/vault/crypto.ts +210 -0
  146. package/vault/daemon-attest.ts +229 -0
  147. package/vault/derive.ts +113 -0
  148. package/vault/device-access.ts +87 -0
  149. package/vault/device-anchor.ts +185 -0
  150. package/vault/device-control.ts +174 -0
  151. package/vault/handoff.ts +396 -0
  152. package/vault/index.ts +21 -0
  153. package/vault/passkey.ts +243 -0
  154. package/vault/phrase.ts +65 -0
  155. package/vault/pin.ts +153 -0
  156. package/vault/public-key-seal.ts +131 -0
  157. package/vault/seed.ts +272 -0
  158. package/vault/team.ts +355 -0
  159. package/vault/vault-item.ts +131 -0
  160. package/vault/vault-migrate-digest.ts +29 -0
  161. package/vault/wrapped.ts +77 -0
  162. package/wire/ARCHITECTURE.md +90 -0
  163. package/wire/CONTRIBUTING.md +19 -0
  164. package/wire/LICENSE +33 -0
  165. package/wire/README.md +51 -0
  166. package/wire/adapters/messages/anthropic-map.ts +37 -0
  167. package/wire/adapters/messages/index.ts +7 -0
  168. package/wire/adapters/messages/reasoning-from-items.ts +38 -0
  169. package/wire/adapters/messages/reasoning-signature.ts +243 -0
  170. package/wire/adapters/messages/request.ts +574 -0
  171. package/wire/adapters/messages/response.ts +215 -0
  172. package/wire/adapters/messages/streaming.ts +1027 -0
  173. package/wire/adapters/messages/usage.ts +43 -0
  174. package/wire/adapters/responses/index.ts +3 -0
  175. package/wire/adapters/responses/request.ts +395 -0
  176. package/wire/adapters/responses/response.ts +88 -0
  177. package/wire/adapters/responses/streaming.ts +344 -0
  178. package/wire/assets/openllm-light.svg +18 -0
  179. package/wire/assets/openllm.svg +18 -0
  180. package/wire/features/account-rank.ts +166 -0
  181. package/wire/features/compaction/compact-request.ts +862 -0
  182. package/wire/features/compaction/compaction-text.ts +43 -0
  183. package/wire/features/context-demote.ts +47 -0
  184. package/wire/features/context-overflow-strategy.ts +26 -0
  185. package/wire/features/context-skip.ts +170 -0
  186. package/wire/features/conversation-affinity.ts +144 -0
  187. package/wire/features/max-tokens-backfill.ts +141 -0
  188. package/wire/features/model-caps.ts +53 -0
  189. package/wire/features/model-settings.ts +24 -0
  190. package/wire/features/output-limit-clamp.ts +64 -0
  191. package/wire/features/quota-gate.ts +85 -0
  192. package/wire/index.ts +44 -0
  193. package/wire/lib/attempt-outcome.ts +43 -0
  194. package/wire/lib/canonical/content-part.ts +38 -0
  195. package/wire/lib/canonical/count-tokens-capability.ts +138 -0
  196. package/wire/lib/canonical/encoding-select.ts +82 -0
  197. package/wire/lib/canonical/image-signature.ts +202 -0
  198. package/wire/lib/canonical/image-sse.ts +216 -0
  199. package/wire/lib/canonical/message.ts +43 -0
  200. package/wire/lib/canonical/token-estimate.ts +266 -0
  201. package/wire/lib/encrypted-content.ts +66 -0
  202. package/wire/lib/error-class.ts +154 -0
  203. package/wire/lib/error-envelope.ts +56 -0
  204. package/wire/lib/forwarded-headers.ts +180 -0
  205. package/wire/lib/provider-error-rules.ts +48 -0
  206. package/wire/lib/refusal.ts +38 -0
  207. package/wire/lib/responses-apply-patch.ts +104 -0
  208. package/wire/lib/responses-namespace.ts +53 -0
  209. package/wire/lib/secret-scrub.ts +101 -0
  210. package/wire/lib/streaming/accumulate.ts +221 -0
  211. package/wire/lib/streaming/finish-reason.ts +33 -0
  212. package/wire/lib/streaming/peek.ts +154 -0
  213. package/wire/lib/streaming/provider-decode.ts +259 -0
  214. package/wire/lib/streaming/response-stream.ts +71 -0
  215. package/wire/lib/streaming/sse.ts +699 -0
  216. package/wire/lib/streaming/strip-tool-calls.ts +95 -0
  217. package/wire/lib/streaming/upstream-error.ts +58 -0
  218. package/wire/lib/strip-subagent-isolation.ts +284 -0
  219. package/wire/lib/thinking-signature.ts +156 -0
  220. package/wire/lib/token-ruler/bpe-core.ts +426 -0
  221. package/wire/lib/token-ruler/index.ts +2 -0
  222. package/wire/lib/token-ruler/ruler.ts +79 -0
  223. package/wire/lib/token-ruler/vocab/claude-ranks.ts +16 -0
  224. package/wire/lib/token-ruler/vocab/claude.ts +22 -0
  225. package/wire/lib/token-ruler/vocab/o200k-ranks.ts +18 -0
  226. package/wire/lib/token-ruler/vocab/o200k.ts +19 -0
  227. package/wire/lib/tool-name-map.ts +271 -0
  228. package/wire/lib/tool-schema.ts +353 -0
  229. package/wire/lib/usage-observer.ts +125 -0
  230. package/wire/providers/anthropic/adaptive-thinking.ts +291 -0
  231. package/wire/providers/anthropic/beta-headers.ts +165 -0
  232. package/wire/providers/anthropic/index.ts +10 -0
  233. package/wire/providers/anthropic/request.ts +607 -0
  234. package/wire/providers/anthropic/response.ts +155 -0
  235. package/wire/providers/anthropic/streaming.ts +315 -0
  236. package/wire/providers/anthropic/web-search-domains.ts +49 -0
  237. package/wire/providers/chatgpt/common.ts +136 -0
  238. package/wire/providers/chatgpt/index.ts +9 -0
  239. package/wire/providers/chatgpt/request.ts +959 -0
  240. package/wire/providers/chatgpt/streaming.ts +1007 -0
  241. package/wire/providers/chatgpt/web-search.ts +63 -0
  242. package/wire/providers/grok/web-search.ts +87 -0
  243. package/wire/providers/kimi/web-search.ts +145 -0
  244. package/wire/providers/upstream-deny.ts +355 -0
  245. package/wire/providers/upstream-request.ts +619 -0
@@ -0,0 +1,574 @@
1
+ import type {
2
+ TAnthropicContentBlock,
3
+ TAnthropicMessage,
4
+ TAnthropicRequest,
5
+ TAnthropicTool,
6
+ TChatCompletionRequest,
7
+ TChatMessage,
8
+ TToolCall,
9
+ } from "@openllmsh/sdk/protocol";
10
+ import type { TCanonicalContentPart } from "../../lib/canonical/content-part";
11
+ import { budgetToReasoningEffort } from "../../providers/anthropic/adaptive-thinking";
12
+ import {
13
+ decodeNativeAssistantSignature,
14
+ decodeReasoningSignature,
15
+ } from "./reasoning-signature";
16
+
17
+ const reasonEffortFromThinking = (
18
+ req: TAnthropicRequest,
19
+ ): TChatCompletionRequest["reasoning_effort"] => {
20
+ if (req.output_config?.effort !== undefined) {
21
+ return req.output_config.effort;
22
+ }
23
+ if (req.thinking?.type === "enabled") {
24
+ return budgetToReasoningEffort(req.thinking.budget_tokens);
25
+ }
26
+ if (req.thinking?.type === "adaptive") {
27
+ return req.output_config?.effort ?? "medium";
28
+ }
29
+ return undefined;
30
+ };
31
+
32
+ const extractTextFromBlocks = (
33
+ blocks: ReadonlyArray<TAnthropicContentBlock>,
34
+ ): string =>
35
+ blocks
36
+ .filter(
37
+ (b): b is { type: "text"; text: string } & TAnthropicContentBlock =>
38
+ b.type === "text",
39
+ )
40
+ .map((b) => b.text)
41
+ .join("\n");
42
+
43
+ /** Readable text degradation for a citable search_result block. */
44
+ const renderSearchResult = (
45
+ block: Extract<TAnthropicContentBlock, { type: "search_result" }>,
46
+ ): string => {
47
+ const body = block.content.map((b) => b.text).join("\n");
48
+ return `[search result] ${block.title} (${block.source})\n${body}`;
49
+ };
50
+
51
+ /** Pull the text out of an opaque custom-content document source. */
52
+ const extractTextFromUnknownContent = (content: unknown): string => {
53
+ if (typeof content === "string") return content;
54
+ if (!Array.isArray(content)) return "";
55
+ return content
56
+ .filter(
57
+ (b): b is { type: "text"; text: string } =>
58
+ typeof b === "object" &&
59
+ b !== null &&
60
+ (b as { type?: unknown }).type === "text" &&
61
+ typeof (b as { text?: unknown }).text === "string",
62
+ )
63
+ .map((b) => b.text)
64
+ .join("\n");
65
+ };
66
+
67
+ const documentToCanonicalPart = (
68
+ block: Extract<TAnthropicContentBlock, { type: "document" }>,
69
+ ): TCanonicalContentPart => {
70
+ const src = block.source;
71
+ if (src.type === "base64") {
72
+ return {
73
+ type: "file",
74
+ file: {
75
+ file_data: `data:${src.media_type};base64,${src.data}`,
76
+ ...(block.title != null ? { filename: block.title } : {}),
77
+ },
78
+ };
79
+ }
80
+ if (src.type === "file") {
81
+ return {
82
+ type: "file",
83
+ file: {
84
+ file_id: src.file_id,
85
+ ...(block.title != null ? { filename: block.title } : {}),
86
+ },
87
+ };
88
+ }
89
+ if (src.type === "text") {
90
+ // Plain-text documents ARE text — carry the content directly.
91
+ return { type: "text", text: src.data };
92
+ }
93
+ if (src.type === "content") {
94
+ const text = extractTextFromUnknownContent(src.content);
95
+ return {
96
+ type: "text",
97
+ text:
98
+ text.length > 0
99
+ ? text
100
+ : "[document omitted — custom-content document had no extractable text]",
101
+ };
102
+ }
103
+ // url source — the wire layer is pure (no fetch) and OpenAI file parts
104
+ // have no URL form, so degrade to an annotation. Anthropic→Anthropic
105
+ // passthrough keeps URL documents working; only cross-provider hops
106
+ // land here.
107
+ return {
108
+ type: "text",
109
+ text: `[document omitted — URL documents are not supported by this provider: ${src.url}]`,
110
+ };
111
+ };
112
+
113
+ const blockToCanonicalPart = (
114
+ block: TAnthropicContentBlock,
115
+ ): TCanonicalContentPart | null => {
116
+ if (block.type === "text") {
117
+ return { type: "text", text: block.text };
118
+ }
119
+ if (block.type === "image") {
120
+ if (block.source.type === "url") {
121
+ return { type: "image_url", image_url: { url: block.source.url } };
122
+ }
123
+ if (block.source.type === "base64") {
124
+ return {
125
+ type: "image_url",
126
+ image_url: {
127
+ url: `data:${block.source.media_type};base64,${block.source.data}`,
128
+ },
129
+ };
130
+ }
131
+ // file_id image source — OpenAI image_url has no file-reference
132
+ // form, so annotate rather than silently dropping the image.
133
+ return {
134
+ type: "text",
135
+ text: "[image omitted — file_id image sources are not supported by this provider]",
136
+ };
137
+ }
138
+ if (block.type === "document") {
139
+ return documentToCanonicalPart(block);
140
+ }
141
+ if (block.type === "search_result") {
142
+ // No canonical carrier for citable search results — degrade to a
143
+ // readable text rendering (citations are dropped cross-provider;
144
+ // Anthropic→Anthropic passthrough keeps them intact).
145
+ return { type: "text", text: renderSearchResult(block) };
146
+ }
147
+ if (block.type === "container_upload") {
148
+ return {
149
+ type: "text",
150
+ text: "[container upload omitted — code-execution file references are not supported by this provider]",
151
+ };
152
+ }
153
+ if (CROSS_WIRE_UNSUPPORTED_BLOCK_TYPES.has(block.type)) {
154
+ // Server-tool blocks Anthropic RETURNS and the client replays as
155
+ // history. There is no canonical carrier for any of them, and
156
+ // inventing one would be a guess at semantics we cannot verify — so
157
+ // name the loss instead of vanishing the block. These block types were
158
+ // previously rejected by the request decoder; this annotation supports
159
+ // newly accepted history without implying their semantics are preserved.
160
+ return {
161
+ type: "text",
162
+ text: `[${block.type} omitted — provider-executed tool results are not supported by this provider]`,
163
+ };
164
+ }
165
+ return null;
166
+ };
167
+
168
+ /**
169
+ * Anthropic content blocks with no canonical (OpenAI-shaped) equivalent.
170
+ *
171
+ * These decode fine — they must, or the gateway 400s its own upstream's
172
+ * output — but they cannot cross to a non-Anthropic provider intact.
173
+ * `blockToCanonicalPart` degrades them to a NAMED annotation rather than
174
+ * dropping them silently.
175
+ */
176
+ const CROSS_WIRE_UNSUPPORTED_BLOCK_TYPES: ReadonlySet<string> = new Set([
177
+ "web_fetch_tool_result",
178
+ "code_execution_tool_result",
179
+ "bash_code_execution_tool_result",
180
+ "text_editor_code_execution_tool_result",
181
+ "tool_search_tool_result",
182
+ "mcp_tool_use",
183
+ "mcp_tool_result",
184
+ "tool_reference",
185
+ "browser_state",
186
+ "tool_addition",
187
+ ]);
188
+
189
+ const blocksToCanonicalContent = (
190
+ blocks: ReadonlyArray<TAnthropicContentBlock>,
191
+ ): string | ReadonlyArray<TCanonicalContentPart> => {
192
+ const parts: TCanonicalContentPart[] = [];
193
+ for (const b of blocks) {
194
+ // A document's `context` is retrieval metadata the model should
195
+ // still see — surface it as a preceding text part so it isn't lost
196
+ // on cross-provider hops.
197
+ if (b.type === "document" && b.context != null && b.context.length > 0) {
198
+ parts.push({ type: "text", text: b.context });
199
+ }
200
+ const p = blockToCanonicalPart(b);
201
+ if (p !== null) parts.push(p);
202
+ }
203
+ if (parts.length === 0) return "";
204
+ if (parts.length === 1 && parts[0]?.type === "text") return parts[0].text;
205
+ // If every part is text, collapse to a single string for simpler
206
+ // downstream handling.
207
+ if (parts.every((p) => p.type === "text")) {
208
+ return parts.map((p) => (p.type === "text" ? p.text : "")).join("\n");
209
+ }
210
+ return parts;
211
+ };
212
+
213
+ const extractSystemText = (
214
+ system: TAnthropicRequest["system"] | undefined,
215
+ ): string => {
216
+ if (system === undefined) return "";
217
+ if (typeof system === "string") return system;
218
+ return system.map((b) => b.text).join("\n\n");
219
+ };
220
+
221
+ const toolUseToOpenAIToolCall = (block: {
222
+ readonly id: string;
223
+ readonly name: string;
224
+ readonly input: unknown;
225
+ }): TToolCall => ({
226
+ id: block.id,
227
+ type: "function",
228
+ function: {
229
+ name: block.name,
230
+ arguments: JSON.stringify(block.input ?? {}),
231
+ },
232
+ });
233
+
234
+ const toolResultContentToString = (
235
+ content: Extract<TAnthropicContentBlock, { type: "tool_result" }>["content"],
236
+ ): string => {
237
+ if (typeof content === "string") return content;
238
+ // A tool_result may carry image / document / search_result blocks
239
+ // alongside text — the canonical OpenAI `role: "tool"` message is
240
+ // text-only, so keep the text and annotate the rest rather than
241
+ // leaking `undefined` into the join.
242
+ return content
243
+ .map((b) => {
244
+ if (b.type === "text") return b.text;
245
+ if (b.type === "document") {
246
+ if (b.source.type === "text") return b.source.data;
247
+ if (b.source.type === "content") {
248
+ const text = extractTextFromUnknownContent(b.source.content);
249
+ if (text.length > 0) return text;
250
+ }
251
+ return "[document content omitted from tool result]";
252
+ }
253
+ if (b.type === "search_result") return renderSearchResult(b);
254
+ return "[image content omitted from tool result]";
255
+ })
256
+ .join("\n");
257
+ };
258
+
259
+ /**
260
+ * Does an inbound Anthropic-wire body declare the `web_search_*` SERVER
261
+ * tool? The typed descriptor is the client's explicit request for
262
+ * platform-executed search (Claude Code's WebSearch). Consumers switch a
263
+ * capable non-Anthropic hop onto its provider-NATIVE search (grok
264
+ * web/x_search, kimi builtin `$web_search`) — never armed by a bare
265
+ * `web_search`-NAMED function tool, which stays an ordinary client tool.
266
+ */
267
+ export const declaresAnthropicServerSearchTool = (
268
+ rawBody: unknown,
269
+ ): boolean => {
270
+ if (rawBody === null || typeof rawBody !== "object") return false;
271
+ const tools = (rawBody as { readonly tools?: unknown }).tools;
272
+ return (
273
+ Array.isArray(tools) &&
274
+ tools.some(
275
+ (tool) =>
276
+ tool !== null &&
277
+ typeof tool === "object" &&
278
+ typeof (tool as { readonly type?: unknown }).type === "string" &&
279
+ (tool as { readonly type: string }).type.startsWith("web_search_"),
280
+ )
281
+ );
282
+ };
283
+
284
+ /**
285
+ * Default `parameters` schema for Anthropic native server tools that
286
+ * don't carry an `input_schema` of their own (`web_search_*`,
287
+ * `web_fetch_*`, …). Without this, a non-Anthropic provider sees a
288
+ * function tool with no arg shape and calls it with empty input — the
289
+ * proxy then can't pull a query out, search runs against `""`, and the
290
+ * whole WebSearch flow returns an empty result.
291
+ *
292
+ * Today we know only `web_search_*` needs this. Adding more native
293
+ * types is a one-line extension below.
294
+ */
295
+ const NATIVE_TOOL_DEFAULT_PARAMETERS = (
296
+ toolType: string | undefined,
297
+ ): unknown => {
298
+ if (typeof toolType !== "string") return undefined;
299
+ if (toolType.startsWith("web_search_")) {
300
+ return {
301
+ type: "object",
302
+ properties: {
303
+ query: {
304
+ type: "string",
305
+ description: "The web search query to run.",
306
+ },
307
+ },
308
+ required: ["query"],
309
+ additionalProperties: false,
310
+ };
311
+ }
312
+ return undefined;
313
+ };
314
+
315
+ const anthropicToolToOpenAI = (
316
+ tool: TAnthropicTool,
317
+ ): NonNullable<TChatCompletionRequest["tools"]>[number] => {
318
+ const parameters =
319
+ tool.input_schema ?? NATIVE_TOOL_DEFAULT_PARAMETERS(tool.type);
320
+ return {
321
+ type: "function",
322
+ function: {
323
+ name: tool.name,
324
+ ...(tool.description !== undefined
325
+ ? { description: tool.description }
326
+ : {}),
327
+ ...(parameters !== undefined ? { parameters } : {}),
328
+ },
329
+ };
330
+ };
331
+
332
+ const anthropicToolChoiceToOpenAI = (
333
+ choice: NonNullable<TAnthropicRequest["tool_choice"]>,
334
+ ): NonNullable<TChatCompletionRequest["tool_choice"]> | undefined => {
335
+ switch (choice.type) {
336
+ case "auto":
337
+ return "auto";
338
+ case "any":
339
+ return "required";
340
+ case "none":
341
+ return "none";
342
+ case "tool":
343
+ return { type: "function", function: { name: choice.name } };
344
+ }
345
+ };
346
+
347
+ /**
348
+ * Anthropic `tool_choice.disable_parallel_tool_use` → canonical
349
+ * `parallel_tool_calls`. The two express the same intent with opposite
350
+ * polarity, so this is a translation, not a drop — previously the flag was
351
+ * accepted and then discarded, silently re-enabling parallel tool calls on
352
+ * every cross-provider hop.
353
+ *
354
+ * Only `false` is emitted: `disable_parallel_tool_use: false` is already
355
+ * the provider default on both wires, so asserting `true` would add a
356
+ * field the client never sent.
357
+ */
358
+ const parallelToolCallsFrom = (
359
+ choice: TAnthropicRequest["tool_choice"],
360
+ ): false | undefined =>
361
+ choice !== undefined &&
362
+ choice.type !== "none" &&
363
+ choice.disable_parallel_tool_use === true
364
+ ? false
365
+ : undefined;
366
+
367
+ /**
368
+ * Walk an Anthropic-style message and emit the canonical OpenAI
369
+ * message(s). An assistant turn with mixed text + tool_use blocks
370
+ * yields ONE assistant message carrying both `content` (text) and
371
+ * `tool_calls`. A user turn with one or more tool_result blocks
372
+ * yields ONE OpenAI `role: "tool"` message PER block.
373
+ */
374
+ const splitAnthropicMessage = (m: TAnthropicMessage): TChatMessage[] => {
375
+ // A `role: "system"` turn inlined in `messages[]` (Claude Code
376
+ // v2.1.157+ under `mid-conversation-system-2026-04-07`) maps to the
377
+ // canonical `system` role. Handle it BEFORE the generic
378
+ // string-content branch below, which would otherwise mis-bucket a
379
+ // string-content system turn as an assistant message.
380
+ if (m.role === "system") {
381
+ const content =
382
+ typeof m.content === "string"
383
+ ? m.content
384
+ : extractTextFromBlocks(m.content);
385
+ return [{ role: "system", content }];
386
+ }
387
+
388
+ if (typeof m.content === "string") {
389
+ return m.role === "user"
390
+ ? [{ role: "user", content: m.content }]
391
+ : [{ role: "assistant", content: m.content }];
392
+ }
393
+
394
+ if (m.role === "user") {
395
+ if (m.content.some((b) => b.type === "tool_result")) {
396
+ // A tool_result turn may carry SIBLING blocks — Claude Code delivers
397
+ // loaded-skill instructions as a text block alongside the Skill
398
+ // tool_result. Emit a `tool` message per tool_result AND a `user`
399
+ // message per contiguous run of other blocks, preserving block order;
400
+ // returning only the tool messages silently severed that injected
401
+ // context from the conversation on every cross-provider hop.
402
+ const out: TChatMessage[] = [];
403
+ let siblings: TAnthropicContentBlock[] = [];
404
+ const flushSiblings = (): void => {
405
+ if (siblings.length === 0) return;
406
+ const content = blocksToCanonicalContent(siblings);
407
+ siblings = [];
408
+ if (typeof content === "string" && content.length === 0) return;
409
+ out.push({ role: "user", content });
410
+ };
411
+ for (const b of m.content) {
412
+ if (b.type === "tool_result") {
413
+ flushSiblings();
414
+ out.push({
415
+ role: "tool",
416
+ content: toolResultContentToString(b.content),
417
+ tool_call_id: b.tool_use_id,
418
+ });
419
+ } else {
420
+ siblings.push(b);
421
+ }
422
+ }
423
+ flushSiblings();
424
+ return out;
425
+ }
426
+ // Preserve image blocks so vision flows through to the chosen
427
+ // upstream provider (works regardless of which provider the user
428
+ // selected — OpenAI keeps image_url native, Anthropic re-emits via
429
+ // contentToBlocks).
430
+ const content = blocksToCanonicalContent(m.content);
431
+ return [{ role: "user", content }];
432
+ }
433
+
434
+ // assistant
435
+ // Assistant turns carry provider-executed tool blocks too (the client
436
+ // replays them verbatim as history). `extractTextFromBlocks` sees only
437
+ // `text`, so without this every one of them vanished from the
438
+ // conversation on a cross-provider hop — the model then had no record
439
+ // that a web fetch / code execution / MCP call ever happened.
440
+ const unsupportedNotes = m.content
441
+ .filter((b) => CROSS_WIRE_UNSUPPORTED_BLOCK_TYPES.has(b.type))
442
+ .map(
443
+ (b) =>
444
+ `[${b.type} omitted — provider-executed tool results are not supported by this provider]`,
445
+ );
446
+ const text = [extractTextFromBlocks(m.content), ...unsupportedNotes]
447
+ .filter((s) => s.length > 0)
448
+ .join("\n");
449
+ const toolCalls = m.content
450
+ .filter(
451
+ (b): b is Extract<TAnthropicContentBlock, { type: "tool_use" }> =>
452
+ b.type === "tool_use",
453
+ )
454
+ .map(toolUseToOpenAIToolCall);
455
+ // Carry prior extended-thinking forward as canonical
456
+ // `reasoning_content`. The Anthropic→Anthropic case is now handled by
457
+ // the native pass-through (which keeps `thinking` + `signature`
458
+ // verbatim), so this only fires for a cross-provider fallback — and
459
+ // there a non-Anthropic model can't use the opaque `signature`
460
+ // anyway, but DOES benefit from the reasoning text as context.
461
+ // Previously these blocks were silently dropped (request.ts only
462
+ // handled text + tool_use), truncating multi-turn reasoning history.
463
+ const thinkingBlocks = m.content.filter(
464
+ (b): b is Extract<TAnthropicContentBlock, { type: "thinking" }> =>
465
+ b.type === "thinking",
466
+ );
467
+ const thinking = thinkingBlocks
468
+ .map((b) => b.thinking)
469
+ .filter((t) => t.length > 0)
470
+ .join("\n\n");
471
+ // Recover the upstream `reasoning` item(s) we smuggled out through
472
+ // the thinking `signature`. Echoing them back lets a reasoning
473
+ // upstream (Codex/Responses) resume its chain-of-thought instead of
474
+ // restarting — and re-issuing the same tool call — every turn.
475
+ // Genuine Anthropic signatures decode to null and are ignored here
476
+ // (that path is the native pass-through, not this adapter).
477
+ // Native facade carriers are cumulative snapshots. The last signature
478
+ // supersedes earlier snapshots emitted as native assistant messages arrive.
479
+ // Responses reasoning items, by contrast, are independent and accumulate.
480
+ const nativeSnapshots = thinkingBlocks
481
+ .map((b) => decodeNativeAssistantSignature(b.signature))
482
+ .filter((items) => items !== null);
483
+ const reasoningItems =
484
+ nativeSnapshots.at(-1) ??
485
+ thinkingBlocks.flatMap((b) => decodeReasoningSignature(b.signature) ?? []);
486
+ const assistantMsg: Extract<TChatMessage, { role: "assistant" }> = {
487
+ role: "assistant",
488
+ content: text,
489
+ ...(toolCalls.length > 0 ? { tool_calls: toolCalls } : {}),
490
+ ...(thinking.length > 0
491
+ ? { reasoning_content: thinking }
492
+ : toolCalls.length > 0
493
+ ? { reasoning_content: "" }
494
+ : {}),
495
+ ...(reasoningItems.length > 0 ? { reasoning_items: reasoningItems } : {}),
496
+ };
497
+ return [assistantMsg];
498
+ };
499
+
500
+ /**
501
+ * Adapt an inbound `/v1/messages` (Anthropic-format) request to the
502
+ * internal canonical `TChatCompletionRequest` (OpenAI shape).
503
+ *
504
+ * Used for the genuine cross-provider path (Anthropic-format client →
505
+ * a non-Anthropic upstream). Anthropic→Anthropic no longer flows
506
+ * through here — it takes the native pass-through, which keeps
507
+ * `cache_control`, `thinking`+`signature`, `top_k` and structured
508
+ * `system` verbatim. Here we map text, images, tools/tool_choice,
509
+ * tool_use↔tool_calls, tool_result→tool messages, and prior
510
+ * `thinking`→`reasoning_content`. `cache_control` is intentionally
511
+ * not propagated: no non-Anthropic upstream honours it.
512
+ *
513
+ * Known follow-up: OpenAI rejects tool names >64 chars; LiteLLM
514
+ * truncates with a reverse map. Deferred — long tool names in practice
515
+ * flow Anthropic→Anthropic (pass-through, no limit).
516
+ */
517
+ export const fromAnthropicMessagesRequest = (
518
+ req: TAnthropicRequest,
519
+ ): TChatCompletionRequest => {
520
+ const messages: TChatMessage[] = [];
521
+
522
+ const systemText = extractSystemText(req.system);
523
+ if (systemText.length > 0) {
524
+ messages.push({ role: "system", content: systemText });
525
+ }
526
+
527
+ for (const m of req.messages) {
528
+ for (const expanded of splitAnthropicMessage(m)) {
529
+ // Drop empty user messages so a turn that was purely tool_result
530
+ // doesn't leak through as an empty text turn.
531
+ if (
532
+ expanded.role === "user" &&
533
+ typeof expanded.content === "string" &&
534
+ expanded.content.length === 0
535
+ ) {
536
+ continue;
537
+ }
538
+ messages.push(expanded);
539
+ }
540
+ }
541
+
542
+ const tools = req.tools?.map(anthropicToolToOpenAI);
543
+ const toolChoice =
544
+ req.tool_choice !== undefined
545
+ ? anthropicToolChoiceToOpenAI(req.tool_choice)
546
+ : undefined;
547
+
548
+ const parallelToolCalls = parallelToolCallsFrom(req.tool_choice);
549
+
550
+ const reasoningEffort = reasonEffortFromThinking(req);
551
+
552
+ return {
553
+ model: req.model,
554
+ messages,
555
+ max_completion_tokens: req.max_tokens,
556
+ ...(req.temperature !== undefined ? { temperature: req.temperature } : {}),
557
+ ...(req.top_p !== undefined ? { top_p: req.top_p } : {}),
558
+ ...(req.stop_sequences !== undefined
559
+ ? { stop: req.stop_sequences as readonly string[] as string[] }
560
+ : {}),
561
+ ...(req.stream !== undefined ? { stream: req.stream } : {}),
562
+ ...(tools !== undefined && tools.length > 0 ? { tools } : {}),
563
+ ...(toolChoice !== undefined ? { tool_choice: toolChoice } : {}),
564
+ ...(parallelToolCalls !== undefined
565
+ ? { parallel_tool_calls: parallelToolCalls }
566
+ : {}),
567
+ ...(reasoningEffort !== undefined
568
+ ? { reasoning_effort: reasoningEffort }
569
+ : {}),
570
+ ...(req.metadata?.user_id !== undefined
571
+ ? { user: req.metadata.user_id }
572
+ : {}),
573
+ };
574
+ };