@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.
- package/ARCHITECTURE.md +43 -0
- package/LICENSE +202 -0
- package/README.md +142 -2
- package/assets/openllm-light.svg +18 -0
- package/assets/openllm.svg +18 -0
- package/client/chat.ts +140 -0
- package/client/devices.ts +269 -0
- package/client/http.ts +72 -0
- package/client/index.ts +129 -0
- package/client/relay-mux.ts +547 -0
- package/client/types.ts +51 -0
- package/client/vault.ts +239 -0
- package/connect/chat.ts +58 -0
- package/connect/client.ts +240 -0
- package/connect/config.ts +47 -0
- package/connect/errors.ts +121 -0
- package/connect/index.ts +23 -0
- package/connect/oauth.ts +265 -0
- package/connect/routes.ts +234 -0
- package/connect/store.ts +107 -0
- package/mcp/handle.ts +24 -0
- package/mcp/index.ts +76 -0
- package/mcp/server.ts +319 -0
- package/mcp/tools/marketplace.ts +439 -0
- package/mcp/tools/memory-client.ts +297 -0
- package/mcp/tools/memory-config.ts +32 -0
- package/mcp/tools/memory.ts +369 -0
- package/mcp/tools/model-priority.ts +88 -0
- package/mcp/tools/openllm.ts +883 -0
- package/mcp/types.ts +28 -0
- package/openapi/call.ts +90 -0
- package/openapi/index.ts +8 -0
- package/openapi/openapi.json +80213 -0
- package/openapi/operations.ts +2075 -0
- package/package.json +39 -4
- package/protocol/CONTRIBUTING.md +21 -0
- package/protocol/README.md +134 -0
- package/protocol/access.ts +186 -0
- package/protocol/agent-prompt-text.ts +32 -0
- package/protocol/agent.ts +991 -0
- package/protocol/api-key-syntax.ts +105 -0
- package/protocol/assets/openllm-light.svg +18 -0
- package/protocol/assets/openllm.svg +18 -0
- package/protocol/audio.ts +29 -0
- package/protocol/auth.ts +142 -0
- package/protocol/changelog.ts +155 -0
- package/protocol/chat.ts +458 -0
- package/protocol/client-registry.ts +356 -0
- package/protocol/config.ts +345 -0
- package/protocol/control-channel.ts +115 -0
- package/protocol/cooldown-reason.ts +78 -0
- package/protocol/cost.ts +10 -0
- package/protocol/daemon-port.ts +31 -0
- package/protocol/daemon.ts +1524 -0
- package/protocol/default-chains.ts +66 -0
- package/protocol/device-state.ts +85 -0
- package/protocol/doctor-report-local.ts +443 -0
- package/protocol/doctor-report-policy.ts +119 -0
- package/protocol/doctor-report.ts +947 -0
- package/protocol/errors.ts +14 -0
- package/protocol/execution-selection.ts +141 -0
- package/protocol/hop-grant.ts +33 -0
- package/protocol/image-edit-parse.ts +263 -0
- package/protocol/images.ts +298 -0
- package/protocol/index.ts +74 -0
- package/protocol/inference-headers.ts +3 -0
- package/protocol/keepalive.ts +69 -0
- package/protocol/local-auth.ts +300 -0
- package/protocol/local-device-control.ts +133 -0
- package/protocol/local-provider.ts +288 -0
- package/protocol/local-session.ts +118 -0
- package/protocol/marketplace.ts +187 -0
- package/protocol/media-attempt.ts +81 -0
- package/protocol/media-bytes.ts +28 -0
- package/protocol/media-default.ts +357 -0
- package/protocol/media-dimensions.ts +132 -0
- package/protocol/media-input.ts +61 -0
- package/protocol/media-model.ts +142 -0
- package/protocol/media-option-issue.ts +90 -0
- package/protocol/media-types.ts +70 -0
- package/protocol/model-match.ts +1051 -0
- package/protocol/model-settings.ts +327 -0
- package/protocol/models.ts +767 -0
- package/protocol/mux.ts +722 -0
- package/protocol/plan-surface.ts +59 -0
- package/protocol/provider-models.ts +16 -0
- package/protocol/provider-status.ts +206 -0
- package/protocol/provider-tier.ts +67 -0
- package/protocol/provider-usage.ts +263 -0
- package/protocol/providers.ts +901 -0
- package/protocol/public-usage-index.ts +354 -0
- package/protocol/realtime-generated.ts +31 -0
- package/protocol/realtime.ts +324 -0
- package/protocol/registry-pointer.ts +118 -0
- package/protocol/relay.ts +702 -0
- package/protocol/replay-session.ts +11 -0
- package/protocol/responses.ts +280 -0
- package/protocol/semver.ts +182 -0
- package/protocol/setup-mode.ts +22 -0
- package/protocol/sha256-hex.ts +98 -0
- package/protocol/stats.ts +406 -0
- package/protocol/streaming.ts +19 -0
- package/protocol/subscription-daemon-floors.ts +33 -0
- package/protocol/subscription-provider-compat.ts +104 -0
- package/protocol/subscription-provider.ts +32 -0
- package/protocol/team-grant.ts +17 -0
- package/protocol/team-shareable.ts +61 -0
- package/protocol/transcription-encoding.ts +76 -0
- package/protocol/url-locality-dns.ts +37 -0
- package/protocol/url-locality.ts +288 -0
- package/protocol/usage-snapshots.ts +9 -0
- package/protocol/vendor-cli-install.ts +29 -0
- package/protocol/video-job-plan.ts +50 -0
- package/protocol/videos.ts +227 -0
- package/tsconfig.json +14 -0
- package/tunnel/ARCHITECTURE.md +111 -0
- package/tunnel/LICENSE +33 -0
- package/tunnel/README.md +45 -0
- package/tunnel/assets/openllm-light.svg +18 -0
- package/tunnel/assets/openllm.svg +18 -0
- package/tunnel/channel-envelope.ts +139 -0
- package/tunnel/codec.ts +191 -0
- package/tunnel/device-anchor.ts +229 -0
- package/tunnel/device-control.ts +118 -0
- package/tunnel/device-grant.ts +173 -0
- package/tunnel/fleet.ts +77 -0
- package/tunnel/index.ts +22 -0
- package/tunnel/mux.ts +580 -0
- package/tunnel/peer-link.ts +390 -0
- package/tunnel/rtc-auth.ts +415 -0
- package/tunnel/rtc-duplex.ts +187 -0
- package/tunnel/rtc-ice.ts +136 -0
- package/tunnel/secure.ts +1298 -0
- package/tunnel/stream-reset-error.ts +14 -0
- package/tunnel/streams.ts +1430 -0
- package/tunnel/team-grant.ts +302 -0
- package/tunnel/wire-bytes.ts +102 -0
- package/vault/ARCHITECTURE.md +470 -0
- package/vault/LICENSE +33 -0
- package/vault/api-key.ts +190 -0
- package/vault/argon2.ts +106 -0
- package/vault/cli-auth-seal.ts +157 -0
- package/vault/cred-seal.ts +146 -0
- package/vault/credential.ts +55 -0
- package/vault/crypto.ts +210 -0
- package/vault/daemon-attest.ts +229 -0
- package/vault/derive.ts +113 -0
- package/vault/device-access.ts +87 -0
- package/vault/device-anchor.ts +185 -0
- package/vault/device-control.ts +174 -0
- package/vault/handoff.ts +396 -0
- package/vault/index.ts +21 -0
- package/vault/passkey.ts +243 -0
- package/vault/phrase.ts +65 -0
- package/vault/pin.ts +153 -0
- package/vault/public-key-seal.ts +131 -0
- package/vault/seed.ts +272 -0
- package/vault/team.ts +355 -0
- package/vault/vault-item.ts +131 -0
- package/vault/vault-migrate-digest.ts +29 -0
- package/vault/wrapped.ts +77 -0
- package/wire/ARCHITECTURE.md +90 -0
- package/wire/CONTRIBUTING.md +19 -0
- package/wire/LICENSE +33 -0
- package/wire/README.md +51 -0
- package/wire/adapters/messages/anthropic-map.ts +37 -0
- package/wire/adapters/messages/index.ts +7 -0
- package/wire/adapters/messages/reasoning-from-items.ts +38 -0
- package/wire/adapters/messages/reasoning-signature.ts +243 -0
- package/wire/adapters/messages/request.ts +574 -0
- package/wire/adapters/messages/response.ts +215 -0
- package/wire/adapters/messages/streaming.ts +1027 -0
- package/wire/adapters/messages/usage.ts +43 -0
- package/wire/adapters/responses/index.ts +3 -0
- package/wire/adapters/responses/request.ts +395 -0
- package/wire/adapters/responses/response.ts +88 -0
- package/wire/adapters/responses/streaming.ts +344 -0
- package/wire/assets/openllm-light.svg +18 -0
- package/wire/assets/openllm.svg +18 -0
- package/wire/features/account-rank.ts +166 -0
- package/wire/features/compaction/compact-request.ts +862 -0
- package/wire/features/compaction/compaction-text.ts +43 -0
- package/wire/features/context-demote.ts +47 -0
- package/wire/features/context-overflow-strategy.ts +26 -0
- package/wire/features/context-skip.ts +170 -0
- package/wire/features/conversation-affinity.ts +144 -0
- package/wire/features/max-tokens-backfill.ts +141 -0
- package/wire/features/model-caps.ts +53 -0
- package/wire/features/model-settings.ts +24 -0
- package/wire/features/output-limit-clamp.ts +64 -0
- package/wire/features/quota-gate.ts +85 -0
- package/wire/index.ts +44 -0
- package/wire/lib/attempt-outcome.ts +43 -0
- package/wire/lib/canonical/content-part.ts +38 -0
- package/wire/lib/canonical/count-tokens-capability.ts +138 -0
- package/wire/lib/canonical/encoding-select.ts +82 -0
- package/wire/lib/canonical/image-signature.ts +202 -0
- package/wire/lib/canonical/image-sse.ts +216 -0
- package/wire/lib/canonical/message.ts +43 -0
- package/wire/lib/canonical/token-estimate.ts +266 -0
- package/wire/lib/encrypted-content.ts +66 -0
- package/wire/lib/error-class.ts +154 -0
- package/wire/lib/error-envelope.ts +56 -0
- package/wire/lib/forwarded-headers.ts +180 -0
- package/wire/lib/provider-error-rules.ts +48 -0
- package/wire/lib/refusal.ts +38 -0
- package/wire/lib/responses-apply-patch.ts +104 -0
- package/wire/lib/responses-namespace.ts +53 -0
- package/wire/lib/secret-scrub.ts +101 -0
- package/wire/lib/streaming/accumulate.ts +221 -0
- package/wire/lib/streaming/finish-reason.ts +33 -0
- package/wire/lib/streaming/peek.ts +154 -0
- package/wire/lib/streaming/provider-decode.ts +259 -0
- package/wire/lib/streaming/response-stream.ts +71 -0
- package/wire/lib/streaming/sse.ts +699 -0
- package/wire/lib/streaming/strip-tool-calls.ts +95 -0
- package/wire/lib/streaming/upstream-error.ts +58 -0
- package/wire/lib/strip-subagent-isolation.ts +284 -0
- package/wire/lib/thinking-signature.ts +156 -0
- package/wire/lib/token-ruler/bpe-core.ts +426 -0
- package/wire/lib/token-ruler/index.ts +2 -0
- package/wire/lib/token-ruler/ruler.ts +79 -0
- package/wire/lib/token-ruler/vocab/claude-ranks.ts +16 -0
- package/wire/lib/token-ruler/vocab/claude.ts +22 -0
- package/wire/lib/token-ruler/vocab/o200k-ranks.ts +18 -0
- package/wire/lib/token-ruler/vocab/o200k.ts +19 -0
- package/wire/lib/tool-name-map.ts +271 -0
- package/wire/lib/tool-schema.ts +353 -0
- package/wire/lib/usage-observer.ts +125 -0
- package/wire/providers/anthropic/adaptive-thinking.ts +291 -0
- package/wire/providers/anthropic/beta-headers.ts +165 -0
- package/wire/providers/anthropic/index.ts +10 -0
- package/wire/providers/anthropic/request.ts +607 -0
- package/wire/providers/anthropic/response.ts +155 -0
- package/wire/providers/anthropic/streaming.ts +315 -0
- package/wire/providers/anthropic/web-search-domains.ts +49 -0
- package/wire/providers/chatgpt/common.ts +136 -0
- package/wire/providers/chatgpt/index.ts +9 -0
- package/wire/providers/chatgpt/request.ts +959 -0
- package/wire/providers/chatgpt/streaming.ts +1007 -0
- package/wire/providers/chatgpt/web-search.ts +63 -0
- package/wire/providers/grok/web-search.ts +87 -0
- package/wire/providers/kimi/web-search.ts +145 -0
- package/wire/providers/upstream-deny.ts +355 -0
- package/wire/providers/upstream-request.ts +619 -0
|
@@ -0,0 +1,619 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
TAnthropicRequest,
|
|
3
|
+
TChatCompletionRequest,
|
|
4
|
+
TModelCaps,
|
|
5
|
+
TModelSettings,
|
|
6
|
+
TResponsesRequest,
|
|
7
|
+
} from "@openllmsh/sdk/protocol";
|
|
8
|
+
import { fromAnthropicMessagesRequest } from "../adapters/messages/request";
|
|
9
|
+
import { fromResponsesRequest } from "../adapters/responses";
|
|
10
|
+
import { applyModelSettings } from "../features/model-settings";
|
|
11
|
+
import { requestHasImageContent } from "../lib/canonical/content-part";
|
|
12
|
+
import { normaliseAdaptiveThinking } from "./anthropic/adaptive-thinking";
|
|
13
|
+
import {
|
|
14
|
+
ANTHROPIC_FILES_API_BETA,
|
|
15
|
+
ANTHROPIC_OAUTH_BETA,
|
|
16
|
+
deriveAnthropicBetaHeader,
|
|
17
|
+
} from "./anthropic/beta-headers";
|
|
18
|
+
import { toAnthropicRequest } from "./anthropic/request";
|
|
19
|
+
import { normaliseAnthropicNativeTools } from "./anthropic/web-search-domains";
|
|
20
|
+
import { deriveChatGptSessionId, toChatGptRequest } from "./chatgpt/request";
|
|
21
|
+
import { finalizeUpstreamBody } from "./upstream-deny";
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* The SINGLE recipe for preparing an upstream provider request from an inbound
|
|
25
|
+
* one — body + wire-derived headers — for every `(clientWire × upstreamWire)`
|
|
26
|
+
* pairing. The cloud runner (`@openllm/core`) and the coreless daemon walker
|
|
27
|
+
* (`@openllmsh/daemon`) BOTH call this; neither re-derives the recipe.
|
|
28
|
+
*
|
|
29
|
+
* This exists because the pure transforms were single-sourced in `@openllmsh/sdk/wire`
|
|
30
|
+
* but their COMPOSITION was open-coded in core's runner AND the daemon's
|
|
31
|
+
* walker — which can't share (the daemon is core-free) — and drifted, dropping
|
|
32
|
+
* the client's `anthropic-beta` and skipping `normaliseAdaptiveThinking`. See
|
|
33
|
+
* `docs/proposals/unified-upstream-request-builder.md`.
|
|
34
|
+
*
|
|
35
|
+
* Response decode (upstream → canonical) + encode (canonical → client) are
|
|
36
|
+
* already shared wire concerns; this module is the REQUEST side.
|
|
37
|
+
*/
|
|
38
|
+
|
|
39
|
+
export type TClientSurface = "messages" | "chat_completions" | "responses";
|
|
40
|
+
export type TUpstreamWire = "anthropic" | "chatgpt" | "openai";
|
|
41
|
+
|
|
42
|
+
const DEFAULT_ANTHROPIC_VERSION = "2023-06-01";
|
|
43
|
+
|
|
44
|
+
/** The client's upstream wire, derived from the surface it hit. `responses`
|
|
45
|
+
* rides the OpenAI family for the passthrough decision (it never passes
|
|
46
|
+
* through — its body is Responses-shaped — so it always transforms). */
|
|
47
|
+
export const clientWireOf = (surface: TClientSurface): TUpstreamWire =>
|
|
48
|
+
surface === "messages" ? "anthropic" : "openai";
|
|
49
|
+
|
|
50
|
+
/** Inbound (client-shaped) body → canonical ChatCompletion, per surface. */
|
|
51
|
+
export const canonicalFromInbound = (
|
|
52
|
+
surface: TClientSurface,
|
|
53
|
+
rawBody: unknown,
|
|
54
|
+
): TChatCompletionRequest =>
|
|
55
|
+
surface === "messages"
|
|
56
|
+
? fromAnthropicMessagesRequest(rawBody as TAnthropicRequest)
|
|
57
|
+
: surface === "responses"
|
|
58
|
+
? fromResponsesRequest(rawBody as TResponsesRequest)
|
|
59
|
+
: (rawBody as TChatCompletionRequest);
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Canonical request → an UPSTREAM body, per upstream wire. The cross-wire half
|
|
63
|
+
* of the recipe: `toChatGptRequest` / `toAnthropicRequest` / OpenAI-identity.
|
|
64
|
+
* Exported because the daemon's web_search agentic loop rebuilds the upstream
|
|
65
|
+
* body each round from an ALREADY-canonical request (not the inbound body).
|
|
66
|
+
*/
|
|
67
|
+
const inlineAnthropicSystemContent = (content: unknown): unknown[] =>
|
|
68
|
+
typeof content === "string"
|
|
69
|
+
? [{ type: "text", text: content }]
|
|
70
|
+
: Array.isArray(content)
|
|
71
|
+
? content
|
|
72
|
+
: [];
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Mid-conversation inline `role: "system"` messages are only present in bodies
|
|
76
|
+
* coming from Claude Code and are not universally accepted by Anthropic models.
|
|
77
|
+
* The rewrite is therefore triggered by BODY SHAPE (presence of inline system
|
|
78
|
+
* messages) and never by model id.
|
|
79
|
+
*
|
|
80
|
+
* Commit e6ac9b93 rewrote inline system messages unconditionally and destroyed
|
|
81
|
+
* cache behavior (constant `cache_read`, 250k-285k `cache_creation`/request).
|
|
82
|
+
* Commit 2f7357be then added a model gate, but this was still wrong because
|
|
83
|
+
* acceptance is not monotonic by family/version.
|
|
84
|
+
*
|
|
85
|
+
* Verified live ground truth (2026-07-26, handrolled claude_code hop, verbatim
|
|
86
|
+
* Claude Code preamble, hoist bypassed so this reads RAW model capability —
|
|
87
|
+
* re-measure with `tests/server/inline-system-capability.e2e.test.ts`):
|
|
88
|
+
* - accept: `claude-opus-5`, `claude-sonnet-5`, `claude-opus-4-8`,
|
|
89
|
+
* `claude-fable-5`
|
|
90
|
+
* - reject: `claude-opus-4-7`, `claude-opus-4-1`, `claude-sonnet-4-6`,
|
|
91
|
+
* `claude-sonnet-4-5`, `claude-haiku-4-5`
|
|
92
|
+
*
|
|
93
|
+
* `claude-opus-4-1` rejects while `claude-opus-4-8` accepts — NON-MONOTONIC
|
|
94
|
+
* within one family, which is exactly why no regex or list can work. The
|
|
95
|
+
* `anthropic-beta: mid-conversation-system-2026-04-07` header makes no
|
|
96
|
+
* difference either way.
|
|
97
|
+
*
|
|
98
|
+
* Prompt-cache rules:
|
|
99
|
+
* 1. No inline system message → the exact same object is returned (no
|
|
100
|
+
* rewrite, no re-serialisation, no key reordering).
|
|
101
|
+
* 2. A model whose card says `caps.inlineSystemMessages: true` also gets the
|
|
102
|
+
* exact same object back: the inline turns stay where the client put
|
|
103
|
+
* them. This is the ONLY cache-safe path when the client keeps appending
|
|
104
|
+
* new inline turns. The cache prefix is `tools → system → messages`, so a
|
|
105
|
+
* NEW block appended to top-level `system` changes the prefix BEFORE every
|
|
106
|
+
* message and invalidates the whole cached conversation — only tools + the
|
|
107
|
+
* original system prompt survive. (A stable hoisted block is fine on the
|
|
108
|
+
* next turn, which is all a 3-turn probe with one fixed reminder sees; a
|
|
109
|
+
* reminder every few turns is a full re-write every few turns. Same
|
|
110
|
+
* incident and same fix as LiteLLM v1.91.2.)
|
|
111
|
+
* 3. When the hoist runs (models that reject inline system, or no card),
|
|
112
|
+
* moved content is APPENDED AFTER the existing top-level `system` blocks
|
|
113
|
+
* so the client's `cache_control` breakpoint keeps its index — tools +
|
|
114
|
+
* original system stay cached, the messages after them do not.
|
|
115
|
+
* Locked in by `tests/transport/cache-prefix-stability.test.ts`.
|
|
116
|
+
*/
|
|
117
|
+
/**
|
|
118
|
+
* Claude Code can emit mid-conversation inline system messages. Anthropic models
|
|
119
|
+
* accept equivalent top-level system prompts, so move inline system content into
|
|
120
|
+
* `system` when present and remove inline system messages from `messages`. Keep
|
|
121
|
+
* all moved blocks verbatim except for stripping inline `cache_control` fields.
|
|
122
|
+
*/
|
|
123
|
+
const stripInlineSystemCacheMarker = (block: unknown): unknown => {
|
|
124
|
+
if (block === null || typeof block !== "object" || Array.isArray(block)) {
|
|
125
|
+
return block;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const message = block as Record<string, unknown>;
|
|
129
|
+
if (!Object.hasOwn(message, "cache_control")) {
|
|
130
|
+
return block;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const { cache_control: _cacheControl, ...rest } = message;
|
|
134
|
+
return rest;
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
const isInlineSystemMessage = (message: unknown): boolean =>
|
|
138
|
+
typeof message === "object" &&
|
|
139
|
+
message !== null &&
|
|
140
|
+
(message as { readonly role?: unknown }).role === "system";
|
|
141
|
+
|
|
142
|
+
const isToolAdditionBlock = (block: unknown): boolean =>
|
|
143
|
+
typeof block === "object" &&
|
|
144
|
+
block !== null &&
|
|
145
|
+
(block as { readonly type?: unknown }).type === "tool_addition";
|
|
146
|
+
|
|
147
|
+
/** The tool a `tool_addition` block carries (`{type:"tool_definition",
|
|
148
|
+
* definition}`), or null for any other shape. */
|
|
149
|
+
const toolAdditionDefinition = (
|
|
150
|
+
block: unknown,
|
|
151
|
+
): Record<string, unknown> | null => {
|
|
152
|
+
const tool = (block as { readonly tool?: unknown }).tool;
|
|
153
|
+
if (typeof tool !== "object" || tool === null) return null;
|
|
154
|
+
const definition = (tool as { readonly definition?: unknown }).definition;
|
|
155
|
+
return typeof definition === "object" && definition !== null
|
|
156
|
+
? (definition as Record<string, unknown>)
|
|
157
|
+
: null;
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
const toolNameOf = (tool: unknown): unknown =>
|
|
161
|
+
typeof tool === "object" && tool !== null
|
|
162
|
+
? (tool as { readonly name?: unknown }).name
|
|
163
|
+
: undefined;
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* A `tool_addition` block (`inline-tools-2026-09-15`) is valid only inside an
|
|
167
|
+
* inline system turn — hoisted into top-level `system` it 400s every later
|
|
168
|
+
* request in the session (`system.N.type: Input should be 'text'`). On a model
|
|
169
|
+
* that accepts it (`caps.inlineToolAdditions` not false) it stays in place as a
|
|
170
|
+
* system turn of just those blocks. On one that doesn't, its definition joins
|
|
171
|
+
* top-level `tools` (appended, deduped by name) — the shape a client without
|
|
172
|
+
* the beta would send. Everything else is hoisted.
|
|
173
|
+
*/
|
|
174
|
+
const hoistInlineAnthropicSystemMessages = (
|
|
175
|
+
body: Record<string, unknown>,
|
|
176
|
+
caps: TModelCaps | undefined,
|
|
177
|
+
): Record<string, unknown> => {
|
|
178
|
+
if (!Array.isArray(body.messages)) return body;
|
|
179
|
+
if (!body.messages.some(isInlineSystemMessage)) return body;
|
|
180
|
+
// The model takes inline system turns (and any `tool_addition` in them) as
|
|
181
|
+
// sent: forward the exact object. Hoisting here would change top-level
|
|
182
|
+
// `system` every time the client appends a new inline turn, and because the
|
|
183
|
+
// cache prefix is `tools → system → messages` that re-writes the whole
|
|
184
|
+
// cached conversation (observed: cache_read pinned at tools+system,
|
|
185
|
+
// ~150–220k cache_creation per new reminder on claude-opus-5-5).
|
|
186
|
+
if (caps?.inlineSystemMessages === true && caps.inlineToolAdditions !== false)
|
|
187
|
+
return body;
|
|
188
|
+
|
|
189
|
+
const existingSystem =
|
|
190
|
+
typeof body.system === "string"
|
|
191
|
+
? [{ type: "text", text: body.system }]
|
|
192
|
+
: Array.isArray(body.system)
|
|
193
|
+
? body.system
|
|
194
|
+
: [];
|
|
195
|
+
const keepInline = caps?.inlineToolAdditions !== false;
|
|
196
|
+
const hoistedSystem: unknown[] = [];
|
|
197
|
+
const addedTools: unknown[] = [];
|
|
198
|
+
const messages: unknown[] = [];
|
|
199
|
+
for (const message of body.messages) {
|
|
200
|
+
if (!isInlineSystemMessage(message)) {
|
|
201
|
+
messages.push(message);
|
|
202
|
+
continue;
|
|
203
|
+
}
|
|
204
|
+
const blocks = inlineAnthropicSystemContent(
|
|
205
|
+
(message as { readonly content?: unknown }).content,
|
|
206
|
+
);
|
|
207
|
+
for (const block of blocks)
|
|
208
|
+
if (!isToolAdditionBlock(block))
|
|
209
|
+
hoistedSystem.push(stripInlineSystemCacheMarker(block));
|
|
210
|
+
const additions = blocks.filter(isToolAdditionBlock);
|
|
211
|
+
const unconvertible = keepInline
|
|
212
|
+
? additions
|
|
213
|
+
: additions.filter((block) => toolAdditionDefinition(block) === null);
|
|
214
|
+
if (!keepInline)
|
|
215
|
+
for (const block of additions) {
|
|
216
|
+
const definition = toolAdditionDefinition(block);
|
|
217
|
+
if (definition !== null) addedTools.push(definition);
|
|
218
|
+
}
|
|
219
|
+
if (unconvertible.length > 0)
|
|
220
|
+
messages.push({
|
|
221
|
+
...(message as object),
|
|
222
|
+
content: unconvertible.map(stripInlineSystemCacheMarker),
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
const existingTools = Array.isArray(body.tools) ? body.tools : [];
|
|
227
|
+
const existingNames = new Set(existingTools.map(toolNameOf));
|
|
228
|
+
const newTools = addedTools.filter((tool) => {
|
|
229
|
+
const name = toolNameOf(tool);
|
|
230
|
+
if (existingNames.has(name)) return false;
|
|
231
|
+
existingNames.add(name);
|
|
232
|
+
return true;
|
|
233
|
+
});
|
|
234
|
+
return {
|
|
235
|
+
...body,
|
|
236
|
+
system: [...existingSystem, ...hoistedSystem],
|
|
237
|
+
messages,
|
|
238
|
+
...(newTools.length > 0 ? { tools: [...existingTools, ...newTools] } : {}),
|
|
239
|
+
};
|
|
240
|
+
};
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* Remove opaque Responses-only carriers before sending a canonical request to
|
|
244
|
+
* an OpenAI-compatible upstream. Responses/Codex re-emits these fields, but
|
|
245
|
+
* ordinary OpenAI-compatible APIs reject all three as unknown arguments.
|
|
246
|
+
*/
|
|
247
|
+
export const stripResponsesCarriers = (
|
|
248
|
+
request: TChatCompletionRequest,
|
|
249
|
+
): TChatCompletionRequest => {
|
|
250
|
+
const {
|
|
251
|
+
responses_tools: _responsesTools,
|
|
252
|
+
responses_additional_tools: _responsesAdditionalTools,
|
|
253
|
+
responses_client_metadata: _responsesClientMetadata,
|
|
254
|
+
...withoutCarriers
|
|
255
|
+
} = request;
|
|
256
|
+
return withoutCarriers;
|
|
257
|
+
};
|
|
258
|
+
|
|
259
|
+
export const canonicalToUpstreamBody = (
|
|
260
|
+
upstreamWire: TUpstreamWire,
|
|
261
|
+
canonical: TChatCompletionRequest,
|
|
262
|
+
// Kept for callers of this public encoder; final policy runs in buildUpstreamBody.
|
|
263
|
+
_provider: string,
|
|
264
|
+
providerModelId: string,
|
|
265
|
+
stream: boolean,
|
|
266
|
+
// Whether the chatgpt (Responses) encode injects the Codex preamble. Undefined
|
|
267
|
+
// → inject (Codex default); `false` suppresses it for non-Codex Responses-wire
|
|
268
|
+
// providers (xAI Grok). No effect for the anthropic / openai wires.
|
|
269
|
+
codexInstructions?: boolean,
|
|
270
|
+
// Resolved model card caps. Only the anthropic encoder reads them today
|
|
271
|
+
// (`thinkingModes` decides which thinking mode an inferred
|
|
272
|
+
// `reasoning_effort` maps to). Absent = unknown = permissive.
|
|
273
|
+
caps?: TModelCaps,
|
|
274
|
+
): Record<string, unknown> => {
|
|
275
|
+
const options = { providerModelId, ...(caps !== undefined ? { caps } : {}) };
|
|
276
|
+
if (upstreamWire === "chatgpt") {
|
|
277
|
+
// Caps reach this encoder too: `reasoningContexts` is how it learns a
|
|
278
|
+
// model rejects `reasoning.context: "all_turns"` (the Codex spark family),
|
|
279
|
+
// a fact it used to read off the model id.
|
|
280
|
+
return toChatGptRequest(canonical, { ...options, codexInstructions });
|
|
281
|
+
}
|
|
282
|
+
if (upstreamWire === "anthropic") {
|
|
283
|
+
return { ...toAnthropicRequest(canonical, options), stream };
|
|
284
|
+
}
|
|
285
|
+
// OpenAI-identity passthrough drops `responses_tools`,
|
|
286
|
+
// `responses_additional_tools`, and `responses_client_metadata`: ChatGPT
|
|
287
|
+
// re-emits them, while OpenAI-compatible upstreams reject all three.
|
|
288
|
+
const openai = stripResponsesCarriers(canonical);
|
|
289
|
+
const streamOptions =
|
|
290
|
+
stream === true
|
|
291
|
+
? { ...openai.stream_options, include_usage: true }
|
|
292
|
+
: openai.stream_options;
|
|
293
|
+
return {
|
|
294
|
+
...openai,
|
|
295
|
+
...(streamOptions !== undefined ? { stream_options: streamOptions } : {}),
|
|
296
|
+
model: providerModelId,
|
|
297
|
+
stream,
|
|
298
|
+
};
|
|
299
|
+
};
|
|
300
|
+
|
|
301
|
+
/** Inbound (client-shaped) body → an upstream body, per `(surface,
|
|
302
|
+
* upstreamWire)`. Passthrough (+ adaptive-thinking normalise for anthropic)
|
|
303
|
+
* or cross-wire via canonical. Exported so the daemon's single-shot serve and
|
|
304
|
+
* the cloud runner share the exact body recipe. */
|
|
305
|
+
export const buildUpstreamBody = (
|
|
306
|
+
surface: TClientSurface,
|
|
307
|
+
upstreamWire: TUpstreamWire,
|
|
308
|
+
rawBody: unknown,
|
|
309
|
+
provider: string,
|
|
310
|
+
providerModelId: string,
|
|
311
|
+
// `undefined` → PRESERVE the body's own stream flag (the cloud passthrough
|
|
312
|
+
// forwards verbatim); a boolean pins it (the daemon, off the 307's intent).
|
|
313
|
+
stream: boolean | undefined,
|
|
314
|
+
// Codex-preamble injection for the chatgpt wire (see canonicalToUpstreamBody).
|
|
315
|
+
codexInstructions?: boolean,
|
|
316
|
+
caps?: TModelCaps,
|
|
317
|
+
modelSettings?: TModelSettings,
|
|
318
|
+
): Record<string, unknown> => {
|
|
319
|
+
// No Claude launch-overlay prefix is injected.
|
|
320
|
+
// Passthrough: same wire in + out (NEVER for `responses` — its body is
|
|
321
|
+
// Responses-shaped). Only the model id + stream flag are pinned. The
|
|
322
|
+
// Anthropic passthrough additionally normalises adaptive-thinking knobs
|
|
323
|
+
// (`thinking:adaptive` / `output_config.effort` / top-level `effort`) for
|
|
324
|
+
// the RESOLVED model, which rejects them unless its card declares
|
|
325
|
+
// `adaptive` in `caps.thinkingModes`. No card = no rewrite.
|
|
326
|
+
if (upstreamWire === clientWireOf(surface) && surface !== "responses") {
|
|
327
|
+
// Pin the concrete model id + stream flag. The daemon always supplies a
|
|
328
|
+
// resolved `providerModelId` (off the 307); the cloud passthrough (whose
|
|
329
|
+
// body already carries the right model) passes the body's own model — and
|
|
330
|
+
// an empty id means "preserve the body's model", so it stays a true
|
|
331
|
+
// passthrough.
|
|
332
|
+
const raw =
|
|
333
|
+
upstreamWire === "openai"
|
|
334
|
+
? applyModelSettings(rawBody as TChatCompletionRequest, modelSettings)
|
|
335
|
+
: (rawBody as Record<string, unknown>);
|
|
336
|
+
const effectiveStream = stream ?? raw.stream === true;
|
|
337
|
+
const pinned = {
|
|
338
|
+
...raw,
|
|
339
|
+
...(upstreamWire === "openai" && effectiveStream
|
|
340
|
+
? {
|
|
341
|
+
stream_options: {
|
|
342
|
+
...(raw.stream_options as Record<string, unknown> | undefined),
|
|
343
|
+
include_usage: true,
|
|
344
|
+
},
|
|
345
|
+
}
|
|
346
|
+
: {}),
|
|
347
|
+
...(providerModelId.length > 0 ? { model: providerModelId } : {}),
|
|
348
|
+
...(stream !== undefined ? { stream } : {}),
|
|
349
|
+
};
|
|
350
|
+
const result =
|
|
351
|
+
upstreamWire === "anthropic"
|
|
352
|
+
? normaliseAdaptiveThinking(
|
|
353
|
+
hoistInlineAnthropicSystemMessages(
|
|
354
|
+
normaliseAnthropicNativeTools(pinned),
|
|
355
|
+
caps,
|
|
356
|
+
),
|
|
357
|
+
caps,
|
|
358
|
+
)
|
|
359
|
+
: pinned;
|
|
360
|
+
return finalizeUpstreamBody(
|
|
361
|
+
result as Record<string, unknown>,
|
|
362
|
+
provider,
|
|
363
|
+
caps,
|
|
364
|
+
upstreamWire,
|
|
365
|
+
);
|
|
366
|
+
}
|
|
367
|
+
// Cross-wire: route through canonical, then encode to the upstream's wire.
|
|
368
|
+
return finalizeUpstreamBody(
|
|
369
|
+
canonicalToUpstreamBody(
|
|
370
|
+
upstreamWire,
|
|
371
|
+
applyModelSettings(canonicalFromInbound(surface, rawBody), modelSettings),
|
|
372
|
+
provider,
|
|
373
|
+
providerModelId,
|
|
374
|
+
stream ?? false,
|
|
375
|
+
codexInstructions,
|
|
376
|
+
caps,
|
|
377
|
+
),
|
|
378
|
+
provider,
|
|
379
|
+
caps,
|
|
380
|
+
upstreamWire,
|
|
381
|
+
);
|
|
382
|
+
};
|
|
383
|
+
|
|
384
|
+
/**
|
|
385
|
+
* True when a cross-wire (canonical-shaped) body carries a `file` part
|
|
386
|
+
* referencing an uploaded file — `toAnthropicRequest` encodes that to a
|
|
387
|
+
* Files-API document source, which Anthropic only honours under the
|
|
388
|
+
* Files API beta.
|
|
389
|
+
*/
|
|
390
|
+
const canonicalUsesFileIds = (canonical: TChatCompletionRequest): boolean =>
|
|
391
|
+
canonical.messages.some(
|
|
392
|
+
(m) =>
|
|
393
|
+
typeof m.content !== "string" &&
|
|
394
|
+
m.content != null &&
|
|
395
|
+
m.content.some((p) => p.type === "file" && p.file.file_id !== undefined),
|
|
396
|
+
);
|
|
397
|
+
|
|
398
|
+
/** Wire-derived headers (layered OVER the caller's auth/identity). Only the
|
|
399
|
+
* Anthropic upstream contributes here: version + the merged `anthropic-beta`
|
|
400
|
+
* (OAuth beta + the client's inbound betas + body-derived betas). */
|
|
401
|
+
const wireHeaders = (
|
|
402
|
+
surface: TClientSurface,
|
|
403
|
+
upstreamWire: TUpstreamWire,
|
|
404
|
+
rawBody: unknown,
|
|
405
|
+
inboundBeta: string | null,
|
|
406
|
+
isOAuth: boolean,
|
|
407
|
+
apiVersion: string | undefined,
|
|
408
|
+
): Record<string, string> => {
|
|
409
|
+
if (upstreamWire !== "anthropic")
|
|
410
|
+
return { "content-type": "application/json" };
|
|
411
|
+
// Body-derived betas (web_search/web_fetch/files-api) only read the
|
|
412
|
+
// Anthropic-shaped request; for a non-messages (cross-wire) request there's
|
|
413
|
+
// no Anthropic-shaped request, so an empty stand-in yields no false derived
|
|
414
|
+
// betas — the Files-API beta is instead derived from the canonical body's
|
|
415
|
+
// `file_id` parts (which `toAnthropicRequest` encodes to file sources).
|
|
416
|
+
const request =
|
|
417
|
+
surface === "messages"
|
|
418
|
+
? (rawBody as TAnthropicRequest)
|
|
419
|
+
: ({ model: "", messages: [], max_tokens: 0 } as TAnthropicRequest);
|
|
420
|
+
const extraBetas =
|
|
421
|
+
surface !== "messages" &&
|
|
422
|
+
canonicalUsesFileIds(canonicalFromInbound(surface, rawBody))
|
|
423
|
+
? [ANTHROPIC_FILES_API_BETA]
|
|
424
|
+
: [];
|
|
425
|
+
const beta = deriveAnthropicBetaHeader({
|
|
426
|
+
inboundBeta,
|
|
427
|
+
request,
|
|
428
|
+
isOAuth,
|
|
429
|
+
extraBetas,
|
|
430
|
+
});
|
|
431
|
+
return {
|
|
432
|
+
"anthropic-version": apiVersion ?? DEFAULT_ANTHROPIC_VERSION,
|
|
433
|
+
"content-type": "application/json",
|
|
434
|
+
...(beta !== undefined
|
|
435
|
+
? { "anthropic-beta": beta }
|
|
436
|
+
: isOAuth
|
|
437
|
+
? { "anthropic-beta": ANTHROPIC_OAUTH_BETA }
|
|
438
|
+
: {}),
|
|
439
|
+
};
|
|
440
|
+
};
|
|
441
|
+
|
|
442
|
+
export type TBuildUpstreamRequestInput = {
|
|
443
|
+
readonly surface: TClientSurface;
|
|
444
|
+
readonly upstreamWire: TUpstreamWire;
|
|
445
|
+
/** The inbound body in the CLIENT's wire shape. */
|
|
446
|
+
readonly rawBody: unknown;
|
|
447
|
+
/** Provider registry slug used to select final outbound policy. */
|
|
448
|
+
readonly provider: string;
|
|
449
|
+
/** Concrete upstream model id to pin. */
|
|
450
|
+
readonly providerModelId: string;
|
|
451
|
+
/**
|
|
452
|
+
* The client's stream intent. Undefined preserves the inbound body's stream
|
|
453
|
+
* flag for same-wire passthrough requests.
|
|
454
|
+
*/
|
|
455
|
+
readonly stream: boolean | undefined;
|
|
456
|
+
/**
|
|
457
|
+
* Auth + identity the CALLER owns — BYOK `x-api-key` / OAuth `authorization`
|
|
458
|
+
* (cloud), or the local CLI's `authorization` + vendor identity headers
|
|
459
|
+
* (daemon). Wire-derived headers (anthropic-version / anthropic-beta /
|
|
460
|
+
* content-type) are layered ON TOP so they win on collision.
|
|
461
|
+
*/
|
|
462
|
+
readonly baseHeaders: Record<string, string>;
|
|
463
|
+
/** The client's inbound `anthropic-beta` header (or null). */
|
|
464
|
+
readonly inboundBeta?: string | null;
|
|
465
|
+
/** True when the Anthropic auth is a subscription OAuth token. */
|
|
466
|
+
readonly isOAuth?: boolean;
|
|
467
|
+
/** Pinned `anthropic-version` (else the ground-floor default). */
|
|
468
|
+
readonly apiVersion?: string;
|
|
469
|
+
/**
|
|
470
|
+
* chatgpt-wire only: inject the Codex preamble. Undefined → inject (Codex
|
|
471
|
+
* default); `false` suppresses it for a non-Codex Responses-wire provider
|
|
472
|
+
* (xAI Grok). See {@link canonicalToUpstreamBody}.
|
|
473
|
+
*/
|
|
474
|
+
readonly codexInstructions?: boolean;
|
|
475
|
+
/** Catalog-declared final outbound-body constraints for the resolved hop. */
|
|
476
|
+
readonly caps?: TModelCaps;
|
|
477
|
+
readonly modelSettings?: TModelSettings;
|
|
478
|
+
/**
|
|
479
|
+
* Catalog capabilities for the resolved hop. Empty / missing = unknown
|
|
480
|
+
* (custom / passthrough) — never treated as non-vision. The vision
|
|
481
|
+
* gate rejects only when this is NON-empty and lacks `"vision"`.
|
|
482
|
+
*/
|
|
483
|
+
readonly capabilities?: ReadonlyArray<string>;
|
|
484
|
+
};
|
|
485
|
+
|
|
486
|
+
/**
|
|
487
|
+
* The caller's auth/identity (`baseHeaders`) with the wire-derived headers
|
|
488
|
+
* (anthropic-version / merged anthropic-beta / content-type) layered ON TOP so
|
|
489
|
+
* they win on collision. Exported so the daemon's web_search loop (which
|
|
490
|
+
* rebuilds the body per round but keeps one header set) can compute headers
|
|
491
|
+
* once, independently of the body.
|
|
492
|
+
*/
|
|
493
|
+
export const buildUpstreamHeaders = (
|
|
494
|
+
i: TBuildUpstreamRequestInput,
|
|
495
|
+
): Record<string, string> => {
|
|
496
|
+
const headers: Record<string, string> = {
|
|
497
|
+
...i.baseHeaders,
|
|
498
|
+
...wireHeaders(
|
|
499
|
+
i.surface,
|
|
500
|
+
i.upstreamWire,
|
|
501
|
+
i.rawBody,
|
|
502
|
+
i.inboundBeta ?? null,
|
|
503
|
+
i.isOAuth ?? false,
|
|
504
|
+
i.apiVersion,
|
|
505
|
+
),
|
|
506
|
+
};
|
|
507
|
+
if (i.upstreamWire === "chatgpt") {
|
|
508
|
+
ensureChatGptSessionAffinity(headers, i.surface, i.rawBody);
|
|
509
|
+
}
|
|
510
|
+
return headers;
|
|
511
|
+
};
|
|
512
|
+
|
|
513
|
+
/**
|
|
514
|
+
* Guarantee a STABLE `session_id` on the ChatGPT Codex wire (chatgpt + grok).
|
|
515
|
+
* The backend routes prompt-cache affinity by `session_id`; with none, every
|
|
516
|
+
* request lands on a cold machine and caches nothing (`cached=0`), re-billing
|
|
517
|
+
* the full conversation input every turn — a subscription drain for agentic
|
|
518
|
+
* coding (audit 2026-07-14-codex-handrolled-quota-drain). We PRESERVE the
|
|
519
|
+
* client's own session (a real Codex CLI already sends one, in either
|
|
520
|
+
* `session_id` or codex-rs `session-id` form) and only SYNTHESIZE a stable
|
|
521
|
+
* per-conversation one — derived from the immutable conversation prefix — when
|
|
522
|
+
* the client sent none, so bare clients (chat-completions, custom agents) also
|
|
523
|
+
* get cache affinity. `x-client-request-id` rides along for vendor-client
|
|
524
|
+
* parity (caching keys on `session_id`, not this).
|
|
525
|
+
*/
|
|
526
|
+
const ensureChatGptSessionAffinity = (
|
|
527
|
+
headers: Record<string, string>,
|
|
528
|
+
surface: TClientSurface,
|
|
529
|
+
rawBody: unknown,
|
|
530
|
+
): void => {
|
|
531
|
+
const hasSession = Object.keys(headers).some((k) => {
|
|
532
|
+
const lk = k.toLowerCase();
|
|
533
|
+
return lk === "session_id" || lk === "session-id";
|
|
534
|
+
});
|
|
535
|
+
if (hasSession) return;
|
|
536
|
+
const sessionId = deriveChatGptSessionId(
|
|
537
|
+
canonicalFromInbound(surface, rawBody),
|
|
538
|
+
);
|
|
539
|
+
headers.session_id = sessionId;
|
|
540
|
+
headers["x-client-request-id"] = sessionId;
|
|
541
|
+
};
|
|
542
|
+
|
|
543
|
+
/**
|
|
544
|
+
* Known-non-vision: catalog hit with a NON-empty capability set that
|
|
545
|
+
* does not include `"vision"`. Empty / missing capabilities are UNKNOWN
|
|
546
|
+
* (custom / passthrough) and must never be blocked.
|
|
547
|
+
*/
|
|
548
|
+
const isKnownNonVision = (
|
|
549
|
+
capabilities: ReadonlyArray<string> | undefined,
|
|
550
|
+
): boolean =>
|
|
551
|
+
capabilities !== undefined &&
|
|
552
|
+
capabilities.length > 0 &&
|
|
553
|
+
!capabilities.includes("vision");
|
|
554
|
+
|
|
555
|
+
const inboundHasImageContent = (
|
|
556
|
+
surface: TClientSurface,
|
|
557
|
+
rawBody: unknown,
|
|
558
|
+
): boolean => {
|
|
559
|
+
if (surface === "chat_completions") {
|
|
560
|
+
if (
|
|
561
|
+
typeof rawBody !== "object" ||
|
|
562
|
+
rawBody === null ||
|
|
563
|
+
!("messages" in rawBody) ||
|
|
564
|
+
!Array.isArray((rawBody as { messages: unknown }).messages)
|
|
565
|
+
) {
|
|
566
|
+
return false;
|
|
567
|
+
}
|
|
568
|
+
return requestHasImageContent(rawBody as TChatCompletionRequest);
|
|
569
|
+
}
|
|
570
|
+
try {
|
|
571
|
+
return requestHasImageContent(canonicalFromInbound(surface, rawBody));
|
|
572
|
+
} catch {
|
|
573
|
+
return false;
|
|
574
|
+
}
|
|
575
|
+
};
|
|
576
|
+
|
|
577
|
+
/**
|
|
578
|
+
* Thrown when a known-non-vision hop would otherwise leak `image_url`
|
|
579
|
+
* to a text-only OpenAI-compat backend. `type` + `name` are both the
|
|
580
|
+
* `unsupported_content` tag the tests (and callers) match.
|
|
581
|
+
*/
|
|
582
|
+
export class UnsupportedContentError extends Error {
|
|
583
|
+
readonly type = "unsupported_content";
|
|
584
|
+
constructor(providerModelId: string) {
|
|
585
|
+
super(
|
|
586
|
+
`unsupported_content: model "${providerModelId}" does not support image content`,
|
|
587
|
+
);
|
|
588
|
+
this.name = "unsupported_content";
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
/**
|
|
593
|
+
* Prepare the `{ body, headers }` for ONE upstream call. The only place the
|
|
594
|
+
* `(clientWire × upstreamWire)` request recipe lives.
|
|
595
|
+
*/
|
|
596
|
+
export const buildUpstreamRequest = (
|
|
597
|
+
i: TBuildUpstreamRequestInput,
|
|
598
|
+
): { readonly body: unknown; readonly headers: Record<string, string> } => {
|
|
599
|
+
if (
|
|
600
|
+
isKnownNonVision(i.capabilities) &&
|
|
601
|
+
inboundHasImageContent(i.surface, i.rawBody)
|
|
602
|
+
) {
|
|
603
|
+
throw new UnsupportedContentError(i.providerModelId);
|
|
604
|
+
}
|
|
605
|
+
return {
|
|
606
|
+
body: buildUpstreamBody(
|
|
607
|
+
i.surface,
|
|
608
|
+
i.upstreamWire,
|
|
609
|
+
i.rawBody,
|
|
610
|
+
i.provider,
|
|
611
|
+
i.providerModelId,
|
|
612
|
+
i.stream,
|
|
613
|
+
i.codexInstructions,
|
|
614
|
+
i.caps,
|
|
615
|
+
i.modelSettings,
|
|
616
|
+
),
|
|
617
|
+
headers: buildUpstreamHeaders(i),
|
|
618
|
+
};
|
|
619
|
+
};
|