@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,959 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
TChatCompletionRequest,
|
|
3
|
+
TChatGptProviderOptions,
|
|
4
|
+
TChatMessage,
|
|
5
|
+
TModelCaps,
|
|
6
|
+
} from "@openllmsh/sdk/protocol";
|
|
7
|
+
import { codexServiceTier } from "@openllmsh/sdk/protocol";
|
|
8
|
+
import type { TReasoningResponsesInput } from "../../adapters/messages/reasoning-signature";
|
|
9
|
+
import {
|
|
10
|
+
reasoningItemsFromUnknown,
|
|
11
|
+
reasoningItemToResponsesInput,
|
|
12
|
+
} from "../../adapters/messages/reasoning-signature";
|
|
13
|
+
import { extractMessageText } from "../../lib/canonical/message";
|
|
14
|
+
import { splitNamespacedToolName } from "../../lib/responses-namespace";
|
|
15
|
+
import { effectiveDeny, responsesWirePolicy } from "../upstream-deny";
|
|
16
|
+
|
|
17
|
+
const CHATGPT_NAME_RE = /^[a-zA-Z0-9_-]+$/;
|
|
18
|
+
const CHATGPT_NAME_SUB_RE = /[^a-zA-Z0-9_-]/g;
|
|
19
|
+
const COLLAPSE_UNDERSCORE_RE = /_+/g;
|
|
20
|
+
const CODEX_IDENTIFIER_MAX_LENGTH = 64;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* A stable, deterministic 64-bit hash (two FNV-1a passes with distinct offset
|
|
24
|
+
* bases → 16 hex chars). Non-cryptographic — a prompt-cache key is a routing
|
|
25
|
+
* hint, not a security boundary — but sync + env-agnostic (no `node:crypto` /
|
|
26
|
+
* async WebCrypto), which the pure wire layer requires.
|
|
27
|
+
*/
|
|
28
|
+
const stableHash = (s: string): string => {
|
|
29
|
+
let h1 = 0x811c9dc5; // FNV offset basis
|
|
30
|
+
let h2 = 0x1000193; // FNV prime, reused as a second, independent seed
|
|
31
|
+
for (let i = 0; i < s.length; i++) {
|
|
32
|
+
const c = s.charCodeAt(i);
|
|
33
|
+
h1 = Math.imul(h1 ^ c, 0x01000193) >>> 0;
|
|
34
|
+
h2 = Math.imul(h2 ^ c, 0x85ebca77) >>> 0;
|
|
35
|
+
}
|
|
36
|
+
return h1.toString(16).padStart(8, "0") + h2.toString(16).padStart(8, "0");
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Hash the IMMUTABLE conversation prefix — the first user turn's COMPLETE
|
|
41
|
+
* content plus canonical system text (`instructions`) — into a stable 16-hex
|
|
42
|
+
* digest. Shared
|
|
43
|
+
* by {@link derivePromptCacheKey} and {@link deriveChatGptSessionId}: both key
|
|
44
|
+
* off this prefix (it never changes as the conversation grows), so both are
|
|
45
|
+
* stable across turns and distinct across conversations. JSON (not a delimited
|
|
46
|
+
* concat) keeps conversations that differ only by non-text parts (images/files)
|
|
47
|
+
* distinct and removes delimiter ambiguity.
|
|
48
|
+
*/
|
|
49
|
+
const conversationPrefixHash = (
|
|
50
|
+
conversation: ReadonlyArray<TChatMessage>,
|
|
51
|
+
instructions: string,
|
|
52
|
+
): string => {
|
|
53
|
+
const firstUser = conversation.find((m) => m.role === "user");
|
|
54
|
+
const firstUserContent = firstUser !== undefined ? firstUser.content : null;
|
|
55
|
+
return stableHash(JSON.stringify({ firstUserContent, instructions }));
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Derive a prompt-cache key that is STABLE across every turn of one
|
|
60
|
+
* conversation and DISTINCT across conversations. Codex uses its own
|
|
61
|
+
* `thread_id`; the stateless gateway has none, so we key off the immutable
|
|
62
|
+
* conversation prefix — the canonical system text plus the first user turn —
|
|
63
|
+
* which never changes as the conversation grows. Without this, OpenAI's
|
|
64
|
+
* automatic prefix-hash routing can collide distinct conversations onto the
|
|
65
|
+
* same cache lane, causing them to evict each other and cache-hit rate to
|
|
66
|
+
* collapse — burning subscription quota. See the reference
|
|
67
|
+
* `codex-rs/core/src/client.rs::prompt_cache_key`.
|
|
68
|
+
*/
|
|
69
|
+
const derivePromptCacheKey = (
|
|
70
|
+
instructions: string,
|
|
71
|
+
conversation: ReadonlyArray<TChatMessage>,
|
|
72
|
+
): string => `openllm-${conversationPrefixHash(conversation, instructions)}`;
|
|
73
|
+
|
|
74
|
+
/** Max `prompt_cache_key` length the OpenAI Responses backend accepts. A
|
|
75
|
+
* longer key 400s (`prompt_cache_key` too long); the partner client clamps to
|
|
76
|
+
* the same bound. Our synthesized key is 24 chars — this only bites a
|
|
77
|
+
* client's forwarded key. */
|
|
78
|
+
const PROMPT_CACHE_KEY_MAX = 64;
|
|
79
|
+
|
|
80
|
+
/** Clamp by Unicode code point (not UTF-16 unit) so a multi-byte key isn't
|
|
81
|
+
* split mid-character. */
|
|
82
|
+
const clampPromptCacheKey = (key: string): string => {
|
|
83
|
+
const cps = Array.from(key);
|
|
84
|
+
return cps.length <= PROMPT_CACHE_KEY_MAX
|
|
85
|
+
? key
|
|
86
|
+
: cps.slice(0, PROMPT_CACHE_KEY_MAX).join("");
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Clamp a Codex tool identifier to the Responses backend's 64-character
|
|
91
|
+
* bound. Long values retain a 47-character prefix and gain a `_` plus the
|
|
92
|
+
* stable 16-hex digest of the complete original value, making the result
|
|
93
|
+
* deterministic and collision-resistant without changing short identifiers.
|
|
94
|
+
*/
|
|
95
|
+
const clampCodexIdentifier = (value: string): string => {
|
|
96
|
+
if (value.length <= CODEX_IDENTIFIER_MAX_LENGTH) return value;
|
|
97
|
+
const suffix = `_${stableHash(value)}`;
|
|
98
|
+
return `${value.slice(0, CODEX_IDENTIFIER_MAX_LENGTH - suffix.length)}${suffix}`;
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Coerce a `name` field to match `^[a-zA-Z0-9_-]+$`, then clamp it to the
|
|
103
|
+
* Codex Responses backend's 64-character limit. ChatGPT 400s on any other
|
|
104
|
+
* character with a `pattern` error which triggers a retry spiral. Mirrors
|
|
105
|
+
* `chat/transformation.py:64-79`.
|
|
106
|
+
*
|
|
107
|
+
* A request-scoped reverse map built by {@link buildChatGptToolNameMap} restores
|
|
108
|
+
* any lossy rewrite before the streaming decoder emits a client tool call.
|
|
109
|
+
*/
|
|
110
|
+
const sanitizeName = (name: string): string => {
|
|
111
|
+
if (name === "" || CHATGPT_NAME_RE.test(name)) {
|
|
112
|
+
return clampCodexIdentifier(name);
|
|
113
|
+
}
|
|
114
|
+
const cleaned = name
|
|
115
|
+
.replace(CHATGPT_NAME_SUB_RE, "_")
|
|
116
|
+
.replace(COLLAPSE_UNDERSCORE_RE, "_")
|
|
117
|
+
.replace(/^_+|_+$/g, "");
|
|
118
|
+
return clampCodexIdentifier(cleaned.length > 0 ? cleaned : "tool");
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
/** Call IDs do not share tool-name charset constraints, only the 64-character
|
|
122
|
+
* Responses API length limit. Applying the same pure clamp on both the prior
|
|
123
|
+
* function call and its tool result preserves their pairing. */
|
|
124
|
+
const clampCodexCallId = (callId: string): string =>
|
|
125
|
+
clampCodexIdentifier(callId);
|
|
126
|
+
|
|
127
|
+
// runtime-only: a single Responses API content part. The input/output
|
|
128
|
+
// distinction matters — the chatgpt.com endpoint rejects an
|
|
129
|
+
// `output_text` part on a `user` message and vice versa.
|
|
130
|
+
type TResponsesContentPart =
|
|
131
|
+
| { readonly type: "input_text"; readonly text: string }
|
|
132
|
+
| { readonly type: "output_text"; readonly text: string }
|
|
133
|
+
| {
|
|
134
|
+
readonly type: "input_image";
|
|
135
|
+
readonly image_url: string;
|
|
136
|
+
readonly detail?: "auto" | "low" | "high";
|
|
137
|
+
}
|
|
138
|
+
| {
|
|
139
|
+
readonly type: "input_file";
|
|
140
|
+
readonly filename?: string;
|
|
141
|
+
readonly file_data?: string;
|
|
142
|
+
readonly file_id?: string;
|
|
143
|
+
readonly file_url?: string;
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
// runtime-only: a single item in the Responses API `input` array.
|
|
147
|
+
// Mirrors the union from openai-python's `ResponseInputItem`.
|
|
148
|
+
type TResponsesInputItem =
|
|
149
|
+
| {
|
|
150
|
+
readonly type: "message";
|
|
151
|
+
readonly role: "user" | "assistant" | "system" | "developer";
|
|
152
|
+
readonly content: ReadonlyArray<TResponsesContentPart>;
|
|
153
|
+
}
|
|
154
|
+
| {
|
|
155
|
+
readonly type: "function_call";
|
|
156
|
+
readonly call_id: string;
|
|
157
|
+
readonly name: string;
|
|
158
|
+
readonly namespace?: string;
|
|
159
|
+
readonly arguments: string;
|
|
160
|
+
}
|
|
161
|
+
| {
|
|
162
|
+
readonly type: "function_call_output";
|
|
163
|
+
readonly call_id: string;
|
|
164
|
+
readonly output: string;
|
|
165
|
+
}
|
|
166
|
+
| {
|
|
167
|
+
/** Opaque Codex harness declarations, preserved from Responses input. */
|
|
168
|
+
readonly type: "additional_tools";
|
|
169
|
+
readonly role: "developer";
|
|
170
|
+
readonly tools: ReadonlyArray<unknown>;
|
|
171
|
+
readonly [key: string]: unknown;
|
|
172
|
+
}
|
|
173
|
+
| TReasoningResponsesInput;
|
|
174
|
+
|
|
175
|
+
const contentToInputParts = (
|
|
176
|
+
content: TChatMessage["content"] | null | undefined,
|
|
177
|
+
): TResponsesContentPart[] => {
|
|
178
|
+
if (content == null) return [{ type: "input_text", text: "" }];
|
|
179
|
+
if (typeof content === "string") {
|
|
180
|
+
return [{ type: "input_text", text: content }];
|
|
181
|
+
}
|
|
182
|
+
const parts: TResponsesContentPart[] = [];
|
|
183
|
+
for (const block of content) {
|
|
184
|
+
if (block.type === "text") {
|
|
185
|
+
parts.push({ type: "input_text", text: block.text });
|
|
186
|
+
} else if (block.type === "image_url") {
|
|
187
|
+
parts.push({
|
|
188
|
+
type: "input_image",
|
|
189
|
+
image_url: block.image_url.url,
|
|
190
|
+
...(block.image_url.detail !== undefined
|
|
191
|
+
? { detail: block.image_url.detail }
|
|
192
|
+
: {}),
|
|
193
|
+
});
|
|
194
|
+
} else if (block.type === "file") {
|
|
195
|
+
// Responses `input_file` takes exactly ONE file carrier. Pick a single
|
|
196
|
+
// one by precedence — a public `url` (clean-context, no bytes) wins,
|
|
197
|
+
// then a provider `file_id`, then an inline `file_data` data URL — so a
|
|
198
|
+
// caller that sets more than one can't emit an ambiguous part.
|
|
199
|
+
const fileCarrier =
|
|
200
|
+
block.file.url !== undefined
|
|
201
|
+
? { file_url: block.file.url }
|
|
202
|
+
: block.file.file_id !== undefined
|
|
203
|
+
? { file_id: block.file.file_id }
|
|
204
|
+
: block.file.file_data !== undefined
|
|
205
|
+
? { file_data: block.file.file_data }
|
|
206
|
+
: {};
|
|
207
|
+
parts.push({
|
|
208
|
+
type: "input_file",
|
|
209
|
+
...(block.file.filename !== undefined
|
|
210
|
+
? { filename: block.file.filename }
|
|
211
|
+
: {}),
|
|
212
|
+
...fileCarrier,
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
if (parts.length === 0) parts.push({ type: "input_text", text: "" });
|
|
217
|
+
return parts;
|
|
218
|
+
};
|
|
219
|
+
|
|
220
|
+
const contentToOutputParts = (
|
|
221
|
+
content: TChatMessage["content"] | null | undefined,
|
|
222
|
+
): TResponsesContentPart[] => {
|
|
223
|
+
if (content == null) return [{ type: "output_text", text: "" }];
|
|
224
|
+
if (typeof content === "string") {
|
|
225
|
+
return [{ type: "output_text", text: content }];
|
|
226
|
+
}
|
|
227
|
+
const parts: TResponsesContentPart[] = [];
|
|
228
|
+
for (const block of content) {
|
|
229
|
+
if (block.type === "text") {
|
|
230
|
+
parts.push({ type: "output_text", text: block.text });
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
if (parts.length === 0) parts.push({ type: "output_text", text: "" });
|
|
234
|
+
return parts;
|
|
235
|
+
};
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* Pull every `role: "system"` message out of the array, returning both the
|
|
239
|
+
* non-system conversation and concatenated system text. This helper exists
|
|
240
|
+
* only for stable prompt-cache and session hashing: outbound system turns are
|
|
241
|
+
* emitted as `developer` input items, while the top-level `instructions` field
|
|
242
|
+
* is always empty.
|
|
243
|
+
*
|
|
244
|
+
* Mirrors the immutable-prefix portion of
|
|
245
|
+
* `_merge_system_and_developer_into_instruction_text` from
|
|
246
|
+
* `chat/transformation.py:41-61`.
|
|
247
|
+
*/
|
|
248
|
+
const extractSystemInstructions = (
|
|
249
|
+
messages: ReadonlyArray<TChatMessage>,
|
|
250
|
+
): { conversation: TChatMessage[]; instructions: string } => {
|
|
251
|
+
const parts: string[] = [];
|
|
252
|
+
const conversation: TChatMessage[] = [];
|
|
253
|
+
for (const msg of messages) {
|
|
254
|
+
if (msg.role === "system") {
|
|
255
|
+
const text = extractMessageText(msg.content);
|
|
256
|
+
if (text.trim().length > 0) parts.push(text);
|
|
257
|
+
continue;
|
|
258
|
+
}
|
|
259
|
+
conversation.push(msg);
|
|
260
|
+
}
|
|
261
|
+
return {
|
|
262
|
+
conversation,
|
|
263
|
+
instructions: parts.filter((p) => p.trim().length > 0).join("\n\n"),
|
|
264
|
+
};
|
|
265
|
+
};
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* Derive a STABLE per-conversation `session_id` for the ChatGPT Codex backend's
|
|
269
|
+
* prompt-cache affinity. Without a stable `session_id` the backend routes each
|
|
270
|
+
* request to a cold machine and caches NOTHING — every turn re-bills the full
|
|
271
|
+
* conversation input at full rate (verified live: `cached=0` without it vs
|
|
272
|
+
* ~90% with it; audit 2026-07-14-codex-handrolled-quota-drain). Keyed on the
|
|
273
|
+
* immutable conversation prefix (first user turn + system instructions),
|
|
274
|
+
* mirroring {@link derivePromptCacheKey}: stable across every turn of one
|
|
275
|
+
* conversation, distinct across conversations (so traffic doesn't hot-spot one
|
|
276
|
+
* machine). The hash retains canonical system text even though the outbound
|
|
277
|
+
* `instructions` field is always empty.
|
|
278
|
+
*/
|
|
279
|
+
export const deriveChatGptSessionId = (req: TChatCompletionRequest): string => {
|
|
280
|
+
const { conversation, instructions } = extractSystemInstructions(
|
|
281
|
+
req.messages,
|
|
282
|
+
);
|
|
283
|
+
return `openllm-sess-${conversationPrefixHash(conversation, instructions)}`;
|
|
284
|
+
};
|
|
285
|
+
|
|
286
|
+
const TOOL_RESULT_IMAGE_REPLAY_TEXT = "Attached image(s) from tool result:";
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* Convert one canonical tool-result message into Responses input items.
|
|
290
|
+
* `function_call_output.output` is a STRING — the shape Codex itself and
|
|
291
|
+
* litellm's reference transformation send, and the one field xAI's partner
|
|
292
|
+
* client (openclaw) force-coerces for Grok before every request (see
|
|
293
|
+
* docs/audit/2026-07-14-grok-upstream-wire-openclaw-comparison.md §F1).
|
|
294
|
+
* Non-text parts can't ride in the string: images are replayed as an
|
|
295
|
+
* IMMEDIATELY-FOLLOWING `user` message (adjacent, not end-of-input, so the
|
|
296
|
+
* conversation prefix stays byte-stable across turns for prompt caching);
|
|
297
|
+
* media without a text sibling leaves a placeholder so the model knows the
|
|
298
|
+
* tool returned something.
|
|
299
|
+
*/
|
|
300
|
+
const toolResultToItems = (
|
|
301
|
+
msg: Extract<TChatMessage, { readonly role: "tool" }>,
|
|
302
|
+
): TResponsesInputItem[] => {
|
|
303
|
+
const content = msg.content;
|
|
304
|
+
const text = extractMessageText(content);
|
|
305
|
+
const images: TResponsesContentPart[] = [];
|
|
306
|
+
let hasOtherMedia = false;
|
|
307
|
+
if (content != null && typeof content !== "string") {
|
|
308
|
+
for (const block of content) {
|
|
309
|
+
if (block.type === "image_url") {
|
|
310
|
+
images.push({
|
|
311
|
+
type: "input_image",
|
|
312
|
+
image_url: block.image_url.url,
|
|
313
|
+
...(block.image_url.detail !== undefined
|
|
314
|
+
? { detail: block.image_url.detail }
|
|
315
|
+
: {}),
|
|
316
|
+
});
|
|
317
|
+
} else if (block.type !== "text") {
|
|
318
|
+
hasOtherMedia = true;
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
const output =
|
|
323
|
+
text.trim().length > 0
|
|
324
|
+
? text
|
|
325
|
+
: hasOtherMedia
|
|
326
|
+
? "(see attached media)"
|
|
327
|
+
: images.length > 0
|
|
328
|
+
? "(see attached image)"
|
|
329
|
+
: "";
|
|
330
|
+
const items: TResponsesInputItem[] = [
|
|
331
|
+
{
|
|
332
|
+
type: "function_call_output",
|
|
333
|
+
call_id: clampCodexCallId(msg.tool_call_id),
|
|
334
|
+
output,
|
|
335
|
+
},
|
|
336
|
+
];
|
|
337
|
+
if (images.length > 0) {
|
|
338
|
+
items.push({
|
|
339
|
+
type: "message",
|
|
340
|
+
role: "user",
|
|
341
|
+
content: [
|
|
342
|
+
{ type: "input_text", text: TOOL_RESULT_IMAGE_REPLAY_TEXT },
|
|
343
|
+
...images,
|
|
344
|
+
],
|
|
345
|
+
});
|
|
346
|
+
}
|
|
347
|
+
return items;
|
|
348
|
+
};
|
|
349
|
+
|
|
350
|
+
/**
|
|
351
|
+
* Convert the canonical OpenAI ChatCompletion message array into
|
|
352
|
+
* Responses API input items. Mirrors
|
|
353
|
+
* `convert_chat_completion_messages_to_responses_api` from
|
|
354
|
+
* `completion_extras/litellm_responses_transformation/transformation.py:203-289`.
|
|
355
|
+
*
|
|
356
|
+
* - `system` content -> `input_text` parts on a `developer` message.
|
|
357
|
+
* - `user` content -> `input_text` parts on a `user` message.
|
|
358
|
+
* - `assistant` text content -> `output_text` parts.
|
|
359
|
+
* - `assistant.tool_calls` -> one `function_call` item per call.
|
|
360
|
+
* - `tool` (tool result) -> `function_call_output` (string) via
|
|
361
|
+
* {@link toolResultToItems}, plus an image-replay user message.
|
|
362
|
+
*/
|
|
363
|
+
const messagesToInputItems = (
|
|
364
|
+
messages: ReadonlyArray<TChatMessage>,
|
|
365
|
+
toolNames: ReadonlyMap<string, string>,
|
|
366
|
+
): TResponsesInputItem[] => {
|
|
367
|
+
const items: TResponsesInputItem[] = [];
|
|
368
|
+
for (const msg of messages) {
|
|
369
|
+
if (msg.role === "system") {
|
|
370
|
+
const text = extractMessageText(msg.content);
|
|
371
|
+
if (text.trim().length === 0) continue;
|
|
372
|
+
items.push({
|
|
373
|
+
type: "message",
|
|
374
|
+
role: "developer",
|
|
375
|
+
content: contentToInputParts(msg.content),
|
|
376
|
+
});
|
|
377
|
+
continue;
|
|
378
|
+
}
|
|
379
|
+
if (msg.role === "user") {
|
|
380
|
+
items.push({
|
|
381
|
+
type: "message",
|
|
382
|
+
role: "user",
|
|
383
|
+
content: contentToInputParts(msg.content),
|
|
384
|
+
});
|
|
385
|
+
continue;
|
|
386
|
+
}
|
|
387
|
+
if (msg.role === "tool") {
|
|
388
|
+
items.push(...toolResultToItems(msg));
|
|
389
|
+
continue;
|
|
390
|
+
}
|
|
391
|
+
if (msg.role === "assistant") {
|
|
392
|
+
// Echo prior `reasoning` item(s) back, in order, immediately
|
|
393
|
+
// before the assistant's tool calls / content. The Responses API
|
|
394
|
+
// requires this for reasoning models (`store: false`); dropping
|
|
395
|
+
// it makes the model restart reasoning and loop. Mirrors litellm
|
|
396
|
+
// `transformation.py:261-262, 279-280`.
|
|
397
|
+
const reasoningItems = reasoningItemsFromUnknown(msg.reasoning_items);
|
|
398
|
+
for (const r of reasoningItems) {
|
|
399
|
+
items.push(reasoningItemToResponsesInput(r));
|
|
400
|
+
}
|
|
401
|
+
const toolCalls = msg.tool_calls;
|
|
402
|
+
if (toolCalls !== undefined && toolCalls.length > 0) {
|
|
403
|
+
for (const call of toolCalls) {
|
|
404
|
+
// A namespace member (canonical `<ns>--<name>`) goes back as the
|
|
405
|
+
// Responses pair Codex declared it with, verbatim in responses_tools.
|
|
406
|
+
const split = splitNamespacedToolName(call.function.name);
|
|
407
|
+
items.push({
|
|
408
|
+
type: "function_call",
|
|
409
|
+
call_id: clampCodexCallId(call.id),
|
|
410
|
+
...(split.namespace === undefined
|
|
411
|
+
? { name: outboundToolName(call.function.name, toolNames) }
|
|
412
|
+
: { namespace: split.namespace, name: split.name }),
|
|
413
|
+
arguments: call.function.arguments,
|
|
414
|
+
});
|
|
415
|
+
}
|
|
416
|
+
// Assistant text alongside tool_calls is rare, but allowed —
|
|
417
|
+
// emit a separate message item if present.
|
|
418
|
+
if (msg.content != null) {
|
|
419
|
+
const text = extractMessageText(msg.content);
|
|
420
|
+
if (text.trim().length > 0) {
|
|
421
|
+
items.push({
|
|
422
|
+
type: "message",
|
|
423
|
+
role: "assistant",
|
|
424
|
+
content: contentToOutputParts(msg.content),
|
|
425
|
+
});
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
continue;
|
|
429
|
+
}
|
|
430
|
+
if (msg.content != null) {
|
|
431
|
+
items.push({
|
|
432
|
+
type: "message",
|
|
433
|
+
role: "assistant",
|
|
434
|
+
content: contentToOutputParts(msg.content),
|
|
435
|
+
});
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
return items;
|
|
440
|
+
};
|
|
441
|
+
|
|
442
|
+
/**
|
|
443
|
+
* Recover the opaque leading `additional_tools` input items carried through the
|
|
444
|
+
* canonical request. They originate at the validated Responses boundary; the
|
|
445
|
+
* narrow runtime check keeps an arbitrary canonical caller from injecting a
|
|
446
|
+
* malformed input item while preserving accepted payloads byte-for-byte.
|
|
447
|
+
*/
|
|
448
|
+
/**
|
|
449
|
+
* The vendor's hosted `image_generation` tool is never forwarded. Its result
|
|
450
|
+
* is an inline base64 image (`image_generation_call.result`) that the client
|
|
451
|
+
* replays in history on every later turn — multi-MB request bodies that trip
|
|
452
|
+
* size limits — and the canonical pipeline has no carrier for it, so the
|
|
453
|
+
* image would be dropped anyway. Image generation goes through the OpenLLM
|
|
454
|
+
* MCP image tool, which returns a durable URL.
|
|
455
|
+
*/
|
|
456
|
+
const isHostedImageGenerationTool = (tool: unknown): boolean =>
|
|
457
|
+
tool !== null &&
|
|
458
|
+
typeof tool === "object" &&
|
|
459
|
+
(tool as { readonly type?: unknown }).type === "image_generation";
|
|
460
|
+
|
|
461
|
+
const additionalToolsToInputItems = (
|
|
462
|
+
items: ReadonlyArray<unknown> | undefined,
|
|
463
|
+
): TResponsesInputItem[] => {
|
|
464
|
+
if (items === undefined) return [];
|
|
465
|
+
const result: TResponsesInputItem[] = [];
|
|
466
|
+
for (const item of items) {
|
|
467
|
+
if (
|
|
468
|
+
item !== null &&
|
|
469
|
+
typeof item === "object" &&
|
|
470
|
+
!Array.isArray(item) &&
|
|
471
|
+
(item as { readonly type?: unknown }).type === "additional_tools" &&
|
|
472
|
+
(item as { readonly role?: unknown }).role === "developer" &&
|
|
473
|
+
Array.isArray((item as { readonly tools?: unknown }).tools)
|
|
474
|
+
) {
|
|
475
|
+
const tools = (item as { readonly tools: ReadonlyArray<unknown> }).tools;
|
|
476
|
+
result.push({
|
|
477
|
+
...(item as TResponsesInputItem & object),
|
|
478
|
+
tools: tools.filter((tool) => !isHostedImageGenerationTool(tool)),
|
|
479
|
+
} as TResponsesInputItem);
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
return result;
|
|
483
|
+
};
|
|
484
|
+
|
|
485
|
+
const LOOKAROUND_RE = /\(\?[=!]|\(\?<[=!]/;
|
|
486
|
+
|
|
487
|
+
/**
|
|
488
|
+
* Deep-walk a JSON-schema value and drop any `pattern` key whose string value
|
|
489
|
+
* contains a regex lookaround construct (`(?=`, `(?!`, `(?<=`, `(?<!`). The
|
|
490
|
+
* codex upstream 400s with `Invalid JSON schema: regex lookaround is not
|
|
491
|
+
* supported. Found at $.properties.contact.properties.email.pattern.` on any
|
|
492
|
+
* such schema, so tool params carrying one (e.g. an MCP tool validating email
|
|
493
|
+
* format) must be stripped before forwarding. Pure — never mutates `params`;
|
|
494
|
+
* a `pattern` key whose value isn't a string (e.g. a user data property
|
|
495
|
+
* literally named "pattern") is left untouched.
|
|
496
|
+
*/
|
|
497
|
+
export const sanitizeToolParameters = (params: unknown): unknown => {
|
|
498
|
+
if (Array.isArray(params)) {
|
|
499
|
+
return params.map((item) => sanitizeToolParameters(item));
|
|
500
|
+
}
|
|
501
|
+
if (params === null || typeof params !== "object") return params;
|
|
502
|
+
// Build via `fromEntries`, not `result[key] = ...` — a schema property
|
|
503
|
+
// literally named `__proto__` would otherwise hit the inherited setter
|
|
504
|
+
// and rewrite the object's prototype instead of becoming an own property.
|
|
505
|
+
return Object.fromEntries(
|
|
506
|
+
Object.entries(params)
|
|
507
|
+
.filter(
|
|
508
|
+
([key, value]) =>
|
|
509
|
+
!(
|
|
510
|
+
key === "pattern" &&
|
|
511
|
+
typeof value === "string" &&
|
|
512
|
+
LOOKAROUND_RE.test(value)
|
|
513
|
+
),
|
|
514
|
+
)
|
|
515
|
+
.map(([key, value]) => [key, sanitizeToolParameters(value)]),
|
|
516
|
+
);
|
|
517
|
+
};
|
|
518
|
+
|
|
519
|
+
// runtime-only: a single tool definition in the Responses API. Note
|
|
520
|
+
// the FLAT shape — the chat-completions tool wrapper
|
|
521
|
+
// (`{type:"function", function:{name,...}}`) is not accepted here.
|
|
522
|
+
type TResponsesToolDef = {
|
|
523
|
+
readonly type: "function";
|
|
524
|
+
readonly name: string;
|
|
525
|
+
readonly description?: string;
|
|
526
|
+
readonly parameters?: unknown;
|
|
527
|
+
readonly strict?: boolean;
|
|
528
|
+
};
|
|
529
|
+
|
|
530
|
+
// runtime-only: a Codex built-in / non-function tool carried verbatim from the
|
|
531
|
+
// inbound Responses request (`custom` apply_patch, `web_search`,
|
|
532
|
+
// `image_generation`, `tool_search`). Opaque — re-emitted as-is to the chatgpt
|
|
533
|
+
// upstream, which is the same endpoint Codex sends them to natively.
|
|
534
|
+
type TResponsesPassthroughToolDef = {
|
|
535
|
+
readonly type: string;
|
|
536
|
+
readonly [key: string]: unknown;
|
|
537
|
+
};
|
|
538
|
+
|
|
539
|
+
const EMPTY_TOOL_NAME_MAP: ReadonlyMap<string, string> = new Map();
|
|
540
|
+
|
|
541
|
+
type TChatGptToolNameMaps = {
|
|
542
|
+
/** Original client name → final unique Responses name. */
|
|
543
|
+
readonly outbound: ReadonlyMap<string, string>;
|
|
544
|
+
/** Final unique Responses name → original client name. */
|
|
545
|
+
readonly inbound: ReadonlyMap<string, string>;
|
|
546
|
+
};
|
|
547
|
+
|
|
548
|
+
const EMPTY_TOOL_NAME_MAPS: TChatGptToolNameMaps = {
|
|
549
|
+
outbound: EMPTY_TOOL_NAME_MAP,
|
|
550
|
+
inbound: EMPTY_TOOL_NAME_MAP,
|
|
551
|
+
};
|
|
552
|
+
|
|
553
|
+
const functionResponsesToolName = (tool: unknown): string | undefined => {
|
|
554
|
+
if (tool === null || typeof tool !== "object" || Array.isArray(tool)) {
|
|
555
|
+
return undefined;
|
|
556
|
+
}
|
|
557
|
+
const record = tool as Record<string, unknown>;
|
|
558
|
+
return record.type === "function" && typeof record.name === "string"
|
|
559
|
+
? record.name
|
|
560
|
+
: undefined;
|
|
561
|
+
};
|
|
562
|
+
|
|
563
|
+
/**
|
|
564
|
+
* Visit declared function names in their request order. Stable declaration
|
|
565
|
+
* ordering makes suffix assignment reproducible across the request encoder and
|
|
566
|
+
* stream decoder setup.
|
|
567
|
+
*/
|
|
568
|
+
const forEachFunctionToolName = (
|
|
569
|
+
req: Pick<TChatCompletionRequest, "tools" | "responses_tools">,
|
|
570
|
+
visit: (name: string) => void,
|
|
571
|
+
): void => {
|
|
572
|
+
for (const tool of req.tools ?? []) visit(tool.function.name);
|
|
573
|
+
for (const tool of req.responses_tools ?? []) {
|
|
574
|
+
const name = functionResponsesToolName(tool);
|
|
575
|
+
if (name !== undefined) visit(name);
|
|
576
|
+
}
|
|
577
|
+
};
|
|
578
|
+
|
|
579
|
+
/** Append a numeric suffix while retaining Codex's 64-character bound. */
|
|
580
|
+
const makeUniqueToolName = (
|
|
581
|
+
base: string,
|
|
582
|
+
used: ReadonlySet<string>,
|
|
583
|
+
): string => {
|
|
584
|
+
if (!used.has(base)) return base;
|
|
585
|
+
for (let suffixNumber = 1; ; suffixNumber++) {
|
|
586
|
+
const suffix = `_${suffixNumber}`;
|
|
587
|
+
const prefixLength = Math.max(
|
|
588
|
+
0,
|
|
589
|
+
CODEX_IDENTIFIER_MAX_LENGTH - suffix.length,
|
|
590
|
+
);
|
|
591
|
+
const candidate = `${base.slice(0, prefixLength)}${suffix}`;
|
|
592
|
+
if (!used.has(candidate)) return candidate;
|
|
593
|
+
}
|
|
594
|
+
};
|
|
595
|
+
|
|
596
|
+
/**
|
|
597
|
+
* Build the paired request-scoped maps for Codex tool identifiers. Distinct
|
|
598
|
+
* originals that normalize to the same Responses name are assigned `_1`, `_2`,
|
|
599
|
+
* … in declaration order rather than rejecting the whole request. Repeated
|
|
600
|
+
* declarations of one original reuse its first assignment.
|
|
601
|
+
*/
|
|
602
|
+
const buildChatGptToolNameMaps = (
|
|
603
|
+
req: Pick<TChatCompletionRequest, "tools" | "responses_tools">,
|
|
604
|
+
): TChatGptToolNameMaps => {
|
|
605
|
+
let hasLossyRewrite = false;
|
|
606
|
+
forEachFunctionToolName(req, (name) => {
|
|
607
|
+
if (sanitizeName(name) !== name) hasLossyRewrite = true;
|
|
608
|
+
});
|
|
609
|
+
if (!hasLossyRewrite) return EMPTY_TOOL_NAME_MAPS;
|
|
610
|
+
|
|
611
|
+
const outbound = new Map<string, string>();
|
|
612
|
+
const inbound = new Map<string, string>();
|
|
613
|
+
const used = new Set<string>();
|
|
614
|
+
forEachFunctionToolName(req, (originalName) => {
|
|
615
|
+
if (outbound.has(originalName)) return;
|
|
616
|
+
const assignedName = makeUniqueToolName(sanitizeName(originalName), used);
|
|
617
|
+
outbound.set(originalName, assignedName);
|
|
618
|
+
used.add(assignedName);
|
|
619
|
+
if (assignedName !== originalName) {
|
|
620
|
+
inbound.set(assignedName, originalName);
|
|
621
|
+
}
|
|
622
|
+
});
|
|
623
|
+
return { outbound, inbound };
|
|
624
|
+
};
|
|
625
|
+
|
|
626
|
+
/**
|
|
627
|
+
* Build the request-scoped reverse mapping the Responses stream needs to put
|
|
628
|
+
* every client tool name back exactly as it was sent to OpenLLM.
|
|
629
|
+
*/
|
|
630
|
+
export const buildChatGptToolNameMap = (
|
|
631
|
+
req: Pick<TChatCompletionRequest, "tools" | "responses_tools">,
|
|
632
|
+
): ReadonlyMap<string, string> => buildChatGptToolNameMaps(req).inbound;
|
|
633
|
+
|
|
634
|
+
const outboundToolName = (
|
|
635
|
+
originalName: string,
|
|
636
|
+
names: ReadonlyMap<string, string>,
|
|
637
|
+
): string => names.get(originalName) ?? sanitizeName(originalName);
|
|
638
|
+
|
|
639
|
+
const toolsToResponses = (
|
|
640
|
+
tools: NonNullable<TChatCompletionRequest["tools"]>,
|
|
641
|
+
toolNames: ReadonlyMap<string, string>,
|
|
642
|
+
): TResponsesToolDef[] =>
|
|
643
|
+
tools.map((tool) => ({
|
|
644
|
+
type: "function",
|
|
645
|
+
name: outboundToolName(tool.function.name, toolNames),
|
|
646
|
+
...(tool.function.description !== undefined
|
|
647
|
+
? { description: tool.function.description }
|
|
648
|
+
: {}),
|
|
649
|
+
...(tool.function.parameters !== undefined
|
|
650
|
+
? { parameters: sanitizeToolParameters(tool.function.parameters) }
|
|
651
|
+
: {}),
|
|
652
|
+
...(tool.function.strict !== undefined
|
|
653
|
+
? { strict: tool.function.strict }
|
|
654
|
+
: {}),
|
|
655
|
+
}));
|
|
656
|
+
|
|
657
|
+
/**
|
|
658
|
+
* Sanitize the verbatim `responses_tools` passthrough (Codex's original tool
|
|
659
|
+
* set). Function tools carry a JSON-schema `parameters` and so are subject to
|
|
660
|
+
* the same lookaround-`pattern` 400 as {@link toolsToResponses} — this is
|
|
661
|
+
* actually the path real Codex traffic takes. Non-function tools
|
|
662
|
+
* (`web_search`, `apply_patch`, `image_generation`, `tool_search`, …) must
|
|
663
|
+
* round-trip byte-identical, since they're opaque and re-emitted as-is.
|
|
664
|
+
*/
|
|
665
|
+
const sanitizeResponsesTools = (
|
|
666
|
+
tools: ReadonlyArray<TResponsesPassthroughToolDef>,
|
|
667
|
+
toolNames: ReadonlyMap<string, string>,
|
|
668
|
+
): ReadonlyArray<TResponsesPassthroughToolDef> =>
|
|
669
|
+
tools
|
|
670
|
+
.filter((tool) => !isHostedImageGenerationTool(tool))
|
|
671
|
+
.map((tool) => {
|
|
672
|
+
if (tool.type !== "function") return tool;
|
|
673
|
+
return {
|
|
674
|
+
...tool,
|
|
675
|
+
...(typeof tool.name === "string"
|
|
676
|
+
? { name: outboundToolName(tool.name, toolNames) }
|
|
677
|
+
: {}),
|
|
678
|
+
...("parameters" in tool
|
|
679
|
+
? { parameters: sanitizeToolParameters(tool.parameters) }
|
|
680
|
+
: {}),
|
|
681
|
+
};
|
|
682
|
+
});
|
|
683
|
+
|
|
684
|
+
// runtime-only: tool_choice in the Responses API. Mirrors the tools
|
|
685
|
+
// shape — FLAT `{type:"function", name}`, NOT the chat-completions
|
|
686
|
+
// `{type:"function", function:{name}}` wrapper. Forwarding the chat
|
|
687
|
+
// shape verbatim 400s with `Unknown parameter: 'tool_choice.function'.`
|
|
688
|
+
type TResponsesToolChoice =
|
|
689
|
+
| "auto"
|
|
690
|
+
| "none"
|
|
691
|
+
| "required"
|
|
692
|
+
| { readonly type: "function"; readonly name: string };
|
|
693
|
+
|
|
694
|
+
const toResponsesToolChoice = (
|
|
695
|
+
choice: NonNullable<TChatCompletionRequest["tool_choice"]>,
|
|
696
|
+
toolNames: ReadonlyMap<string, string>,
|
|
697
|
+
): TResponsesToolChoice =>
|
|
698
|
+
choice === "auto" || choice === "none" || choice === "required"
|
|
699
|
+
? choice
|
|
700
|
+
: {
|
|
701
|
+
type: "function",
|
|
702
|
+
name: outboundToolName(choice.function.name, toolNames),
|
|
703
|
+
};
|
|
704
|
+
|
|
705
|
+
/**
|
|
706
|
+
* The Codex client provides the only reliable Responses-Lite signal: it mirrors
|
|
707
|
+
* the internal websocket header into `client_metadata`. Model ids and account
|
|
708
|
+
* tiers are deliberately NOT used — both Lite and full Responses can serve the
|
|
709
|
+
* same catalog ids.
|
|
710
|
+
*/
|
|
711
|
+
/**
|
|
712
|
+
* Whether this hop accepts `reasoning.context: "all_turns"`.
|
|
713
|
+
*
|
|
714
|
+
* The fact is catalog-owned (`caps.reasoningContexts`), NOT derived from the
|
|
715
|
+
* model id. It used to be `modelId.toLowerCase().endsWith("spark")`, which
|
|
716
|
+
* made the encoder guess a model fact from a name: a renamed or newly shipped
|
|
717
|
+
* spark model silently changed behaviour, and a non-spark model that happened
|
|
718
|
+
* to end in those five letters was mis-served. The Codex `spark` family 400s
|
|
719
|
+
* on `all_turns` and accepts only `auto` / `current_turn`; every other Codex
|
|
720
|
+
* model requires `all_turns`.
|
|
721
|
+
*
|
|
722
|
+
* Absent caps = unknown = PERMISSIVE (emit `all_turns`), which is what every
|
|
723
|
+
* non-spark Codex model needs. Un-catalogued spark ids are covered by the
|
|
724
|
+
* catalog's own default rules, not by this adapter.
|
|
725
|
+
*/
|
|
726
|
+
const acceptsAllTurnsContext = (caps: TModelCaps | undefined): boolean =>
|
|
727
|
+
caps?.reasoningContexts === undefined ||
|
|
728
|
+
caps.reasoningContexts.includes("all_turns");
|
|
729
|
+
|
|
730
|
+
const isCodexResponsesLite = (clientMetadata: unknown): boolean => {
|
|
731
|
+
if (
|
|
732
|
+
clientMetadata === null ||
|
|
733
|
+
typeof clientMetadata !== "object" ||
|
|
734
|
+
Array.isArray(clientMetadata)
|
|
735
|
+
) {
|
|
736
|
+
return false;
|
|
737
|
+
}
|
|
738
|
+
const marker = (clientMetadata as Record<string, unknown>)
|
|
739
|
+
.ws_request_header_x_openai_internal_codex_responses_lite;
|
|
740
|
+
return (
|
|
741
|
+
marker === true ||
|
|
742
|
+
(typeof marker === "string" && marker.trim().toLowerCase() === "true")
|
|
743
|
+
);
|
|
744
|
+
};
|
|
745
|
+
|
|
746
|
+
// runtime-only: payload sent to `/backend-api/codex/responses`. Strictly
|
|
747
|
+
// the keys allowed by `ChatGPTResponsesAPIConfig.transform_responses_api_request`
|
|
748
|
+
// (`responses/transformation.py:215-227`), plus `max_output_tokens`. Anything
|
|
749
|
+
// outside this list is dropped to avoid `Unsupported parameter` 400s.
|
|
750
|
+
//
|
|
751
|
+
// Notably ABSENT: `temperature`, `top_p`, `frequency_penalty`,
|
|
752
|
+
// `presence_penalty`, `seed`, `response_format`, `metadata`, `user`.
|
|
753
|
+
// `temperature`/`top_p` are ACCEPTED by the Grok chat proxy (verified live
|
|
754
|
+
// 2026-07-14) but stay off this wire pending a chatgpt.com probe — the Codex
|
|
755
|
+
// allowed-list doesn't include them. `response_format`/`text.format` is
|
|
756
|
+
// deliberately dropped: the Grok proxy accepts the field but does NOT
|
|
757
|
+
// enforce it and derails into a runaway generation (a 16k-token garbage
|
|
758
|
+
// completion, verified live — audit 2026-07-14 §5).
|
|
759
|
+
export type TChatGptRequestBody = {
|
|
760
|
+
readonly model: string;
|
|
761
|
+
readonly input: ReadonlyArray<TResponsesInputItem>;
|
|
762
|
+
// Always empty: canonical system messages are emitted as `developer` input.
|
|
763
|
+
readonly instructions: string;
|
|
764
|
+
readonly stream: true;
|
|
765
|
+
readonly store: false;
|
|
766
|
+
readonly service_tier?: string;
|
|
767
|
+
readonly include: ReadonlyArray<string>;
|
|
768
|
+
// Responses-Lite requires false; full Codex Responses requires true. The
|
|
769
|
+
// authoritative Lite marker lives in `client_metadata`, never a model id.
|
|
770
|
+
readonly parallel_tool_calls?: boolean;
|
|
771
|
+
/** Opaque Codex request metadata, including the Responses-Lite marker. */
|
|
772
|
+
readonly client_metadata?: unknown;
|
|
773
|
+
// The client's token cap — emitted ONLY on the non-Codex Responses
|
|
774
|
+
// variant (`codexInstructions: false`, i.e. grok), where the chat proxy
|
|
775
|
+
// honors it as a hard cap (verified live 2026-07-14). The chatgpt.com
|
|
776
|
+
// Codex endpoint rejects it (`Unsupported parameter: max_output_tokens`),
|
|
777
|
+
// so Codex hops keep dropping it. Grok also allow-backs temperature/top_p
|
|
778
|
+
// (same live verify); both stay off the Codex body via WIRE_DENY.chatgpt.
|
|
779
|
+
readonly max_output_tokens?: number;
|
|
780
|
+
readonly temperature?: number;
|
|
781
|
+
readonly top_p?: number;
|
|
782
|
+
readonly tools?: ReadonlyArray<
|
|
783
|
+
TResponsesToolDef | TResponsesPassthroughToolDef
|
|
784
|
+
>;
|
|
785
|
+
readonly tool_choice?: TResponsesToolChoice;
|
|
786
|
+
// `summary: "auto"` rides with every effort — Codex itself sends it, the
|
|
787
|
+
// Grok proxy accepts it (verified live), and the stream decoder already
|
|
788
|
+
// maps `response.reasoning_summary_text.delta` → `reasoning_content`.
|
|
789
|
+
// `context: "all_turns"` is REQUIRED by the ChatGPT Codex "Responses-Lite"
|
|
790
|
+
// backend (gpt-5.6-terra/luna) and sent natively by codex v0.147. Codex path
|
|
791
|
+
// only — the Grok chat proxy rejects it, so it's omitted there. A model whose
|
|
792
|
+
// card says it does not accept `all_turns` (the Codex `spark` family) also
|
|
793
|
+
// omits it — see `acceptsAllTurnsContext`.
|
|
794
|
+
readonly reasoning?: {
|
|
795
|
+
readonly effort: "low" | "medium" | "high";
|
|
796
|
+
readonly summary: "auto";
|
|
797
|
+
readonly context?: "all_turns";
|
|
798
|
+
};
|
|
799
|
+
readonly previous_response_id?: string;
|
|
800
|
+
readonly truncation?: "auto" | "disabled";
|
|
801
|
+
// Stable per-conversation prompt-cache routing hint (preserved from the
|
|
802
|
+
// inbound Codex request or synthesized from the prefix). Codex ALWAYS sends
|
|
803
|
+
// this; omitting it collapses cache-hit rate. See `derivePromptCacheKey`.
|
|
804
|
+
readonly prompt_cache_key?: string;
|
|
805
|
+
};
|
|
806
|
+
|
|
807
|
+
/**
|
|
808
|
+
* Convert canonical OpenAI ChatCompletion → ChatGPT/Codex Responses API body.
|
|
809
|
+
*
|
|
810
|
+
* 1. Preserve system messages as `developer` items inside `input`.
|
|
811
|
+
* 2. Keep top-level `instructions` as an empty string.
|
|
812
|
+
* 3. Convert `messages` -> `input` items.
|
|
813
|
+
* 4. Sanitize every tool name + assistant tool_call name.
|
|
814
|
+
* 5. Force `stream: true`, `store: false`,
|
|
815
|
+
* `include: ["reasoning.encrypted_content"]`.
|
|
816
|
+
* 6. Map `max_tokens` / `max_completion_tokens` -> `max_output_tokens` —
|
|
817
|
+
* non-Codex upstreams only (`codexInstructions: false`).
|
|
818
|
+
* 7. Map `reasoning_effort` -> `reasoning.effort` (+ `summary: "auto"`).
|
|
819
|
+
* 8. DROP every other key — only the allowed-list is forwarded.
|
|
820
|
+
*
|
|
821
|
+
* System text still feeds prompt-cache/session hashing, but never the body
|
|
822
|
+
* `instructions` field.
|
|
823
|
+
*
|
|
824
|
+
* Mirrors `transform_request` in `chat/transformation.py:212-248` plus
|
|
825
|
+
* the Responses-API allowed-list filter in
|
|
826
|
+
* `responses/transformation.py:215-229`.
|
|
827
|
+
*/
|
|
828
|
+
export const toChatGptRequest = (
|
|
829
|
+
req: TChatCompletionRequest,
|
|
830
|
+
options: TChatGptProviderOptions,
|
|
831
|
+
): TChatGptRequestBody => {
|
|
832
|
+
const { conversation, instructions: fromSystem } = extractSystemInstructions(
|
|
833
|
+
req.messages,
|
|
834
|
+
);
|
|
835
|
+
const isCodex = options.codexInstructions !== false;
|
|
836
|
+
const serviceTier = isCodex ? codexServiceTier(req.service_tier) : null;
|
|
837
|
+
// Assign names once so function definitions, tool choices, and replayed
|
|
838
|
+
// assistant calls all use the same final identifier.
|
|
839
|
+
const toolNameMaps = buildChatGptToolNameMaps(req);
|
|
840
|
+
// Codex requires harness declarations before every conversational turn.
|
|
841
|
+
// They are not part of Grok's Responses contract.
|
|
842
|
+
const input = [
|
|
843
|
+
...(isCodex
|
|
844
|
+
? additionalToolsToInputItems(req.responses_additional_tools)
|
|
845
|
+
: []),
|
|
846
|
+
...messagesToInputItems(req.messages, toolNameMaps.outbound),
|
|
847
|
+
];
|
|
848
|
+
// Prefer the verbatim `responses_tools` passthrough (Codex's full original
|
|
849
|
+
// tool set, function + non-function) — it round-trips apply_patch /
|
|
850
|
+
// web_search / tool_search intact (hosted image_generation is dropped —
|
|
851
|
+
// see isHostedImageGenerationTool) to the same endpoint
|
|
852
|
+
// Codex speaks to natively. Fall back to the function-only canonical tools
|
|
853
|
+
// (a cross-wire client, or a non-Codex caller that set `tools`).
|
|
854
|
+
const responsesTools:
|
|
855
|
+
| ReadonlyArray<TResponsesToolDef | TResponsesPassthroughToolDef>
|
|
856
|
+
| undefined =
|
|
857
|
+
req.responses_tools !== undefined && req.responses_tools.length > 0
|
|
858
|
+
? sanitizeResponsesTools(
|
|
859
|
+
req.responses_tools as ReadonlyArray<TResponsesPassthroughToolDef>,
|
|
860
|
+
toolNameMaps.outbound,
|
|
861
|
+
)
|
|
862
|
+
: req.tools !== undefined && req.tools.length > 0
|
|
863
|
+
? toolsToResponses(req.tools, toolNameMaps.outbound)
|
|
864
|
+
: undefined;
|
|
865
|
+
|
|
866
|
+
return {
|
|
867
|
+
model: options.providerModelId,
|
|
868
|
+
input,
|
|
869
|
+
instructions: "",
|
|
870
|
+
stream: true,
|
|
871
|
+
store: false,
|
|
872
|
+
...(serviceTier !== null ? { service_tier: serviceTier } : {}),
|
|
873
|
+
include: ["reasoning.encrypted_content"],
|
|
874
|
+
// Lite always disables parallel calls. Full Codex honors an explicit client
|
|
875
|
+
// value and otherwise preserves Codex's default of parallel calls enabled.
|
|
876
|
+
...(isCodex
|
|
877
|
+
? {
|
|
878
|
+
parallel_tool_calls: isCodexResponsesLite(
|
|
879
|
+
req.responses_client_metadata,
|
|
880
|
+
)
|
|
881
|
+
? false
|
|
882
|
+
: (req.parallel_tool_calls ?? true),
|
|
883
|
+
}
|
|
884
|
+
: {}),
|
|
885
|
+
...(isCodex && req.responses_client_metadata !== undefined
|
|
886
|
+
? { client_metadata: req.responses_client_metadata }
|
|
887
|
+
: {}),
|
|
888
|
+
// Preserve the caller's key when present (a genuine Codex request already
|
|
889
|
+
// carries a stable per-thread one); otherwise synthesize one off the
|
|
890
|
+
// conversation prefix so turns of the same conversation share a cache lane.
|
|
891
|
+
prompt_cache_key: clampPromptCacheKey(
|
|
892
|
+
req.prompt_cache_key !== undefined && req.prompt_cache_key.length > 0
|
|
893
|
+
? req.prompt_cache_key
|
|
894
|
+
: derivePromptCacheKey(fromSystem, conversation),
|
|
895
|
+
),
|
|
896
|
+
...(responsesTools !== undefined ? { tools: responsesTools } : {}),
|
|
897
|
+
...(req.tool_choice !== undefined
|
|
898
|
+
? {
|
|
899
|
+
tool_choice: toResponsesToolChoice(
|
|
900
|
+
req.tool_choice,
|
|
901
|
+
toolNameMaps.outbound,
|
|
902
|
+
),
|
|
903
|
+
}
|
|
904
|
+
: {}),
|
|
905
|
+
...(() => {
|
|
906
|
+
// Axis 1+2: Codex keeps the full WIRE_DENY.chatgpt drop set (body
|
|
907
|
+
// byte-identical). Grok (`codexInstructions: false`) allow-backs
|
|
908
|
+
// temperature / top_p / max_output_tokens. Map the OpenAI cap aliases
|
|
909
|
+
// onto max_output_tokens; keep response_format / stop / seed / top_k
|
|
910
|
+
// denied.
|
|
911
|
+
const deny = effectiveDeny(
|
|
912
|
+
"chatgpt",
|
|
913
|
+
responsesWirePolicy(options.codexInstructions),
|
|
914
|
+
);
|
|
915
|
+
const extra: {
|
|
916
|
+
temperature?: number;
|
|
917
|
+
top_p?: number;
|
|
918
|
+
max_output_tokens?: number;
|
|
919
|
+
} = {};
|
|
920
|
+
if (!deny.has("temperature") && req.temperature !== undefined) {
|
|
921
|
+
extra.temperature = req.temperature;
|
|
922
|
+
}
|
|
923
|
+
if (!deny.has("top_p") && req.top_p !== undefined) {
|
|
924
|
+
extra.top_p = req.top_p;
|
|
925
|
+
}
|
|
926
|
+
if (!deny.has("max_output_tokens")) {
|
|
927
|
+
const cap = req.max_completion_tokens ?? req.max_tokens;
|
|
928
|
+
if (cap !== undefined) extra.max_output_tokens = cap;
|
|
929
|
+
}
|
|
930
|
+
return extra;
|
|
931
|
+
})(),
|
|
932
|
+
...(() => {
|
|
933
|
+
// ChatGPT's Responses API only accepts `low | medium | high`.
|
|
934
|
+
// Map the wider canonical enum (`minimal/xhigh/max/none`) down to
|
|
935
|
+
// the closest supported neighbour: `minimal` → low, `xhigh`/`max`
|
|
936
|
+
// → high, `none` → reasoning omitted entirely.
|
|
937
|
+
const e = req.reasoning_effort;
|
|
938
|
+
if (e === undefined || e === "none") return {};
|
|
939
|
+
const effort: "low" | "medium" | "high" =
|
|
940
|
+
e === "minimal" || e === "low"
|
|
941
|
+
? "low"
|
|
942
|
+
: e === "medium"
|
|
943
|
+
? "medium"
|
|
944
|
+
: "high";
|
|
945
|
+
return {
|
|
946
|
+
reasoning: {
|
|
947
|
+
effort,
|
|
948
|
+
summary: "auto" as const,
|
|
949
|
+
// Codex path requires `context: "all_turns"`, except a model whose
|
|
950
|
+
// card declares it unaccepted (the spark family 400s on it).
|
|
951
|
+
...(options.codexInstructions !== false &&
|
|
952
|
+
acceptsAllTurnsContext(options.caps)
|
|
953
|
+
? { context: "all_turns" as const }
|
|
954
|
+
: {}),
|
|
955
|
+
},
|
|
956
|
+
};
|
|
957
|
+
})(),
|
|
958
|
+
};
|
|
959
|
+
};
|