@bitkyc08/opencodex 2.100.0 → 2.100.2
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/gui/dist/assets/App-BpJOzw4F.js +59 -0
- package/gui/dist/assets/Tray-BcaI-VCr.js +1 -0
- package/gui/dist/assets/brand-mark-DU-0Vdkr.js +1 -0
- package/gui/dist/assets/index-Bbor_y80.js +94 -0
- package/gui/dist/assets/index-DJEkIDAi.css +2 -0
- package/gui/dist/assets/{segment-thumb-features-CcxZBnxv.js → segment-thumb-features-e9A_FogJ.js} +1 -1
- package/gui/dist/classic/assets/{App-aYgIQMxI.js → App-BrIlUiLc.js} +18 -18
- package/gui/dist/classic/assets/{Tray-D8jfUXpQ.js → Tray-DT146AhS.js} +1 -1
- package/gui/dist/classic/assets/index-DyJHagvT.js +94 -0
- package/gui/dist/classic/assets/quota-summary-CakPe9Ni.js +1 -0
- package/gui/dist/classic/index.html +1 -1
- package/gui/dist/index.html +2 -2
- package/package.json +2 -2
- package/src/adapters/anthropic-image-normalize.ts +1 -1
- package/src/adapters/client-fingerprint.ts +6 -4
- package/src/adapters/cursor/native-exec.ts +3 -2
- package/src/adapters/cursor/tool-guidance.ts +3 -3
- package/src/adapters/cursor/tool-wording.ts +10 -1
- package/src/adapters/devin/cloud-direct/tool-schema.ts +20 -2
- package/src/adapters/google-antigravity-wire.ts +13 -6
- package/src/adapters/google-truncation.ts +4 -2
- package/src/adapters/google.ts +8 -2
- package/src/adapters/openai-chat/mistral-image-limit.ts +28 -15
- package/src/adapters/openai-chat/tool-schema.ts +9 -0
- package/src/adapters/openai-chat-images.ts +100 -0
- package/src/adapters/openai-chat.ts +6 -9
- package/src/adapters/openai-responses/tool-schema.ts +18 -4
- package/src/adapters/run-turn-queue.ts +139 -5
- package/src/adapters/upstream-http-error.ts +4 -2
- package/src/bridge/sse.ts +8 -1
- package/src/claude/chat-tool-dispatch.ts +233 -0
- package/src/claude/inbound-tool-loading.ts +42 -17
- package/src/cli/account-api.ts +6 -0
- package/src/cli/account-extended.ts +60 -6
- package/src/cli/account-key-quota.ts +22 -2
- package/src/cli/account.ts +1 -1
- package/src/cli/capabilities-access-remote.ts +10 -0
- package/src/cli/capabilities-agents-routing.ts +2 -2
- package/src/cli/capabilities-base.ts +2 -2
- package/src/cli/claude.ts +19 -14
- package/src/cli/config-command.ts +24 -1
- package/src/cli/connect.ts +36 -3
- package/src/cli/gui-pair-client.ts +23 -2
- package/src/cli/help.ts +1 -1
- package/src/cli/hub.ts +18 -1
- package/src/cli/link-child-ops.ts +136 -0
- package/src/cli/link-status-child.ts +94 -0
- package/src/cli/link.ts +22 -9
- package/src/cli/registry.ts +3 -2
- package/src/cli/sync-command.ts +3 -1
- package/src/cli/sync-fanout.ts +8 -81
- package/src/client/link-readiness.ts +16 -6
- package/src/client/link-tunnel-status.ts +76 -0
- package/src/clients/config-export.ts +14 -1
- package/src/codex/account-resume-health.ts +41 -0
- package/src/codex/auth-api/login-flow.ts +6 -0
- package/src/codex/auth-api/routes.ts +35 -2
- package/src/codex/catalog/gather-capture.ts +2 -0
- package/src/codex/catalog/provider-models.ts +13 -8
- package/src/codex/convergence-types.ts +2 -0
- package/src/codex/quota.ts +111 -6
- package/src/combos/cooldown-store.ts +208 -0
- package/src/combos/failover.ts +432 -35
- package/src/config/salvage.ts +1 -0
- package/src/config/schema/config-schema.ts +6 -3
- package/src/config/schema/leaf-validators.ts +34 -12
- package/src/generated/compatibility-version.json +227 -135
- package/src/lib/catalog-etag.ts +10 -0
- package/src/lib/client-error-body.ts +15 -0
- package/src/lib/config-ownership.ts +9 -2
- package/src/lib/context-overflow-verdict.ts +266 -9
- package/src/lib/failure-detail.ts +72 -3
- package/src/lib/hard-refusal.ts +259 -0
- package/src/link/child-activity.ts +57 -0
- package/src/link/child-catalog-freshness.ts +149 -0
- package/src/link/child-gated-ocx.ts +117 -0
- package/src/link/child-observations.ts +205 -0
- package/src/link/child-observe.ts +121 -0
- package/src/link/child-ops-engine.ts +162 -0
- package/src/link/child-update-contract.ts +165 -0
- package/src/link/paths.ts +5 -0
- package/src/link/remote-windows.ts +42 -6
- package/src/link/ssh-argv.ts +126 -32
- package/src/link/status-projection.ts +73 -0
- package/src/link/sync-fanout-classify.ts +98 -0
- package/src/link/tunnel-state.ts +10 -4
- package/src/messaging/mcp-queue-rpc.ts +2 -1
- package/src/messaging/mcp-queue-server.ts +5 -0
- package/src/messaging/mcp-reply-tools.ts +24 -9
- package/src/messaging/mcp-reply-wait.ts +70 -0
- package/src/oauth/account-resume-health.ts +60 -0
- package/src/oauth/anthropic-model-quota.ts +12 -0
- package/src/oauth/anthropic-routing.ts +11 -4
- package/src/oauth/index.ts +1 -0
- package/src/oauth/kiro-iam-identity-center.ts +4 -1
- package/src/oauth/store.ts +44 -1
- package/src/providers/api-key-pause-state.ts +23 -0
- package/src/providers/api-key-pause.ts +68 -10
- package/src/providers/api-key-selection-capture.ts +3 -1
- package/src/providers/api-key-selection.ts +3 -2
- package/src/providers/api-keys.ts +18 -5
- package/src/providers/claude-cli-identity.ts +9 -3
- package/src/providers/claude-cli-usage-state.ts +11 -6
- package/src/providers/claude-cli-usage.ts +27 -8
- package/src/providers/codex-capacity.ts +11 -2
- package/src/providers/key-failover.ts +13 -6
- package/src/providers/model-discovery-config.ts +31 -0
- package/src/providers/model-discovery-diagnostic.ts +34 -3
- package/src/providers/model-discovery.ts +43 -7
- package/src/providers/model-rename-fields.ts +2 -0
- package/src/providers/openai-sidecar.ts +15 -2
- package/src/providers/provider-id-rewrite.ts +3 -0
- package/src/providers/quota/account-cache.ts +52 -34
- package/src/providers/quota/anthropic-family-headers.ts +8 -1
- package/src/providers/quota/anthropic-usage-windows.ts +66 -9
- package/src/providers/quota/report-cache.ts +53 -5
- package/src/providers/quota/vendor-probes-key.ts +73 -0
- package/src/providers/quota/vendor-probes-oauth.ts +57 -3
- package/src/providers/quota-key-accounts.ts +12 -5
- package/src/providers/quota-mistral-budget.ts +18 -0
- package/src/providers/quota-types.ts +32 -0
- package/src/providers/quota.ts +22 -22
- package/src/providers/reasoning-metadata.ts +1051 -12
- package/src/providers/registry/entries-extended.ts +3 -1
- package/src/providers/registry/model-ids.ts +1 -0
- package/src/providers/registry/types.ts +6 -0
- package/src/quota/reset-observer.ts +4 -2
- package/src/quota/reset-seen-store.ts +9 -0
- package/src/quota/window-mapping.ts +1 -0
- package/src/quota-pool/assemble.ts +4 -1
- package/src/responses/hosted-tool-policy.ts +56 -12
- package/src/responses/retained-compaction.ts +31 -5
- package/src/router.ts +2 -0
- package/src/server/auth-cors.ts +11 -8
- package/src/server/catalog-download.ts +3 -5
- package/src/server/chat-native.ts +11 -2
- package/src/server/claude-messages.ts +2 -1
- package/src/server/images.ts +41 -13
- package/src/server/index/link-listener.ts +16 -2
- package/src/server/index/link-relay-sessions.ts +43 -2
- package/src/server/index/optional-listeners.ts +18 -0
- package/src/server/management/config-routes.ts +7 -1
- package/src/server/management/context.ts +1 -1
- package/src/server/management/link-child-ops-routes.ts +116 -0
- package/src/server/management/link-route-state.ts +10 -0
- package/src/server/management/link-routes.ts +10 -0
- package/src/server/management/oauth-account-routes.ts +33 -4
- package/src/server/management/provider-overwrite-carry.ts +28 -3
- package/src/server/management/provider-routes.ts +16 -11
- package/src/server/management/route-registry.ts +3 -0
- package/src/server/messages-native.ts +5 -0
- package/src/server/request-log-failure-detail.ts +59 -1
- package/src/server/responses/adapter-dispatch.ts +35 -44
- package/src/server/responses/combo-stream-preflight.ts +146 -29
- package/src/server/responses/compact.ts +27 -4
- package/src/server/responses/context-overflow.ts +58 -2
- package/src/server/responses/core-combo-failure.ts +29 -4
- package/src/server/responses/core-combo-native.ts +3 -1
- package/src/server/responses/core-combo.ts +30 -3
- package/src/server/responses/core-errors.ts +3 -1
- package/src/server/responses/core-opaque-recovery.ts +45 -2
- package/src/server/responses/core-options.ts +12 -0
- package/src/server/responses/passthrough-dispatch.ts +30 -38
- package/src/server/responses/request-transport.ts +43 -14
- package/src/server/responses/reset-replay.ts +58 -2
- package/src/server/responses/run-turn-execution.ts +56 -30
- package/src/types/claude-tool-loading.ts +48 -0
- package/src/types/config.ts +5 -35
- package/src/types/provider.ts +21 -0
- package/src/types.ts +1 -0
- package/src/web-search/parse.ts +44 -4
- package/gui/dist/assets/App-CitvEefy.js +0 -59
- package/gui/dist/assets/Tray-CeKVWQyB.js +0 -1
- package/gui/dist/assets/brand-mark-M92UnxX2.js +0 -1
- package/gui/dist/assets/index-BP47do6-.js +0 -94
- package/gui/dist/assets/index-DmDooSAG.css +0 -2
- package/gui/dist/classic/assets/index-CfGqHooN.js +0 -94
- package/gui/dist/classic/assets/quota-summary-R-ih7OlS.js +0 -1
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { applyChatDestinationPolicy } from "./openai-chat/destination-policy";
|
|
2
2
|
import { withRequiredChatToolResult } from "./openai-chat/required-tool-result";
|
|
3
|
-
import {
|
|
3
|
+
import { prepareOpenAIChatImages } from "./openai-chat-images";
|
|
4
|
+
import { registryEntryForProviderDestination } from "../providers/registry";
|
|
4
5
|
import { protectGlmSummaryBudget, resolveMaxTokens } from "./openai-chat/summary-budget";
|
|
5
6
|
import { chatParallelToolCallsWireValue } from "./openai-chat/parallel-tool-calls";
|
|
6
7
|
import { applyExplicitChatReasoningWirePolicy, declaresIndependentChatReasoningEffort } from "./openai-chat/reasoning-wire";
|
|
@@ -93,6 +94,7 @@ function canSerializeOpenAIChatServiceTier(
|
|
|
93
94
|
export function createOpenAIChatAdapter(provider: OcxProviderConfig): ProviderAdapter {
|
|
94
95
|
let lastRequestedModelId: string | undefined;
|
|
95
96
|
let freeformTools = new Map<string, FreeformToolIdentity>();
|
|
97
|
+
const minImageEdge = registryEntryForProviderDestination(provider)?.minImageEdge;
|
|
96
98
|
return withRequiredChatToolResult(withOpenAIChatToolNames(toolNames => ({
|
|
97
99
|
name: "openai-chat",
|
|
98
100
|
|
|
@@ -266,14 +268,9 @@ export function createOpenAIChatAdapter(provider: OcxProviderConfig): ProviderAd
|
|
|
266
268
|
...(tierLog ? { tierLog } : {}),
|
|
267
269
|
};
|
|
268
270
|
};
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
if (incoming?.abortSignal?.aborted) throw error;
|
|
273
|
-
return finish();
|
|
274
|
-
});
|
|
275
|
-
}
|
|
276
|
-
return finish();
|
|
271
|
+
const imageOptions = { tierBias: incoming?.imageTierBias, abortSignal: incoming?.abortSignal };
|
|
272
|
+
const prepared = prepareOpenAIChatImages(messages, minImageEdge, imageOptions);
|
|
273
|
+
return prepared ? prepared.then(finish) : finish();
|
|
277
274
|
},
|
|
278
275
|
|
|
279
276
|
async *parseStream(
|
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
import { namespacedToolName, type AdapterEvent, type OcxParsedRequest, type OcxProviderConfig, type OcxUsage, type TierDecision } from "../../types";
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
declaredUnsupportedHostedTools,
|
|
4
|
+
isHostedToolUnsupportedForModel,
|
|
5
|
+
responsesBodyDeclaresClientTools,
|
|
6
|
+
} from "../../responses/hosted-tool-policy";
|
|
3
7
|
import { debugProviderDiagnostic } from "../../lib/debug";
|
|
4
8
|
import { stripUnicodePropertyPatterns } from "../responses-tool-schema";
|
|
5
9
|
import {
|
|
@@ -249,18 +253,28 @@ export function promoteClientLoadedTools(body: unknown, replayPrefixLength?: num
|
|
|
249
253
|
* Responses gateway with a narrower capability set be described in config instead of
|
|
250
254
|
* requiring a hard-coded destination rule per vendor (#5002).
|
|
251
255
|
*
|
|
256
|
+
* `unsupportedHostedToolsWithClientTools` joins that declaration only when this request also
|
|
257
|
+
* declares client tools (#8204), for a gateway that serves a hosted tool alone but rejects it
|
|
258
|
+
* next to function calling. It feeds the same set, so every filter and selector
|
|
259
|
+
* reconciliation below treats a conditional denial exactly like an unconditional one.
|
|
260
|
+
*
|
|
252
261
|
* No-op (returns the original reference) when nothing matches, keeping the common path
|
|
253
262
|
* allocation-free.
|
|
254
263
|
*/
|
|
255
264
|
export function stripUnsupportedHostedTools(
|
|
256
265
|
body: unknown,
|
|
257
|
-
provider: Pick<OcxProviderConfig, "baseUrl" | "unsupportedHostedTools">,
|
|
266
|
+
provider: Pick<OcxProviderConfig, "baseUrl" | "unsupportedHostedTools" | "unsupportedHostedToolsWithClientTools">,
|
|
258
267
|
): unknown {
|
|
259
268
|
if (!isPlainObject(body)) return body;
|
|
260
269
|
const model = typeof body.model === "string" ? body.model : "";
|
|
261
270
|
// Expanded once per request rather than per tool: the alias walk is the only
|
|
262
|
-
// non-lookup work in this filter.
|
|
263
|
-
|
|
271
|
+
// non-lookup work in this filter. The client-tool scan runs only for a provider that
|
|
272
|
+
// declared a conditional denial, so every other provider skips it.
|
|
273
|
+
const conditional = provider.unsupportedHostedToolsWithClientTools;
|
|
274
|
+
const declaredUnsupported = declaredUnsupportedHostedTools(
|
|
275
|
+
provider,
|
|
276
|
+
Array.isArray(conditional) && conditional.length > 0 && responsesBodyDeclaresClientTools(body),
|
|
277
|
+
);
|
|
264
278
|
const filterTools = (tools: unknown[]): unknown[] => {
|
|
265
279
|
const filtered = tools.filter(t => {
|
|
266
280
|
const type = isPlainObject(t) && typeof t.type === "string" ? t.type : undefined;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { AdapterEvent } from "../types";
|
|
2
|
+
import { truncationReasonFor } from "../responses/truncated-stop-reason";
|
|
2
3
|
|
|
3
4
|
type QueueReader = (result: IteratorResult<AdapterEvent>) => void;
|
|
4
5
|
|
|
@@ -158,29 +159,107 @@ async function* replay(
|
|
|
158
159
|
}
|
|
159
160
|
}
|
|
160
161
|
|
|
162
|
+
export const DEFAULT_MAX_REASONING_EVENTS = 65_536;
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Required-tool reasoning and zero-output token-limit preflight may hold buffered reasoning,
|
|
166
|
+
* but never for an unbounded/trickling prelude. Bound the reasoning hold to commit and replay.
|
|
167
|
+
*/
|
|
168
|
+
export const DEFAULT_MAX_REASONING_HOLD_MS = 1_000;
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Classifies an adapter terminal event that reached an output token limit before producing output.
|
|
172
|
+
*
|
|
173
|
+
* When an upstream candidate exhausts its max output token budget without emitting any text or tool
|
|
174
|
+
* calls, this classifies the event as an HTTP 502 `zero_output_token_limit` error so combo failover
|
|
175
|
+
* can attempt a backup target. Candidate cooling is request-local (`comboFailureCooldownScope` returns
|
|
176
|
+
* "none") because the failure is specific to the requested token limit rather than provider unhealthiness.
|
|
177
|
+
*/
|
|
178
|
+
export function classifyZeroOutputTokenLimit(
|
|
179
|
+
event: AdapterEvent,
|
|
180
|
+
options?: { allowCompactionWithContent?: boolean },
|
|
181
|
+
): Extract<AdapterEvent, { type: "error" }> | undefined {
|
|
182
|
+
const zeroOutputLimit = event.type === "incomplete"
|
|
183
|
+
? event.reason === "max_output_tokens" && event.retryable !== false && event.endTurn !== false && !event.providerState
|
|
184
|
+
: event.type === "done"
|
|
185
|
+
&& event.stopReason?.trim().toLowerCase() !== "pause_turn"
|
|
186
|
+
&& (["max_tokens", "length", "max_output_tokens"].includes(event.stopReason ?? "") || truncationReasonFor(event.stopReason) === "max_output_tokens")
|
|
187
|
+
&& event.endTurn !== false && !event.providerState && (options?.allowCompactionWithContent || !event.compactionEncryptedContent);
|
|
188
|
+
if (!zeroOutputLimit) return undefined;
|
|
189
|
+
// With bufferCompaction, a compaction summary may have been produced in-memory without streaming.
|
|
190
|
+
// When that run terminates on max_tokens, classify it under the same code so failover can advance,
|
|
191
|
+
// since zero output was committed/streamed to the client.
|
|
192
|
+
const message = options?.allowCompactionWithContent
|
|
193
|
+
? "Remote compaction reached output token limit before completing"
|
|
194
|
+
: "Provider reached its output token limit before producing output";
|
|
195
|
+
return {
|
|
196
|
+
type: "error", status: 502, errorType: "upstream_error", code: "zero_output_token_limit",
|
|
197
|
+
message,
|
|
198
|
+
...((event.type === "done" || event.type === "incomplete") && event.usage ? { usage: event.usage } : {}),
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
export function isReasoningEvent(event: AdapterEvent): boolean {
|
|
203
|
+
return event.type === "thinking_delta"
|
|
204
|
+
|| event.type === "thinking_signature"
|
|
205
|
+
|| event.type === "redacted_thinking"
|
|
206
|
+
|| event.type === "kiro_redacted_reasoning"
|
|
207
|
+
|| event.type === "reasoning_raw_delta";
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
export function isContentEvent(event: AdapterEvent): boolean {
|
|
211
|
+
switch (event.type) {
|
|
212
|
+
case "text_delta":
|
|
213
|
+
return event.text.length > 0;
|
|
214
|
+
case "tool_call_start":
|
|
215
|
+
case "tool_call_delta":
|
|
216
|
+
case "tool_call_end":
|
|
217
|
+
case "web_search_call_begin":
|
|
218
|
+
case "web_search_call_end":
|
|
219
|
+
return true;
|
|
220
|
+
default:
|
|
221
|
+
return false;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
161
225
|
export async function preflightAdapterEvents(
|
|
162
226
|
source: AsyncIterable<AdapterEvent>,
|
|
163
227
|
classifyFirstEvent?: (event: AdapterEvent) => Extract<AdapterEvent, { type: "error" }> | undefined,
|
|
164
|
-
options?: {
|
|
228
|
+
options?: {
|
|
229
|
+
maxWaitMs?: number;
|
|
230
|
+
honorReady?: boolean;
|
|
231
|
+
bufferCompaction?: boolean;
|
|
232
|
+
maxReasoningHoldMs?: number;
|
|
233
|
+
},
|
|
165
234
|
): Promise<AdapterEventPreflight> {
|
|
166
235
|
const iterator = source[Symbol.asyncIterator]();
|
|
167
236
|
const buffered: AdapterEvent[] = [];
|
|
168
237
|
let replayUnsafe = false;
|
|
238
|
+
let bufferedReasoningCodeUnits = 0;
|
|
169
239
|
const maxWaitMs = options?.maxWaitMs;
|
|
170
240
|
if (maxWaitMs !== undefined && maxWaitMs <= 0) {
|
|
171
241
|
return { stream: replay(buffered, iterator), empty: false, replayUnsafe, timedOut: true };
|
|
172
242
|
}
|
|
173
243
|
let timeout: ReturnType<typeof setTimeout> | undefined;
|
|
174
244
|
let timeoutPromise: Promise<"timeout"> | undefined;
|
|
245
|
+
let retainedHeartbeats = 0;
|
|
175
246
|
if (maxWaitMs !== undefined) {
|
|
176
247
|
timeoutPromise = new Promise(resolve => {
|
|
177
248
|
timeout = setTimeout(() => resolve("timeout"), maxWaitMs);
|
|
178
249
|
});
|
|
179
250
|
}
|
|
251
|
+
const maxReasoningHoldMs = options?.maxReasoningHoldMs !== undefined
|
|
252
|
+
? options.maxReasoningHoldMs
|
|
253
|
+
: DEFAULT_MAX_REASONING_HOLD_MS;
|
|
254
|
+
let reasoningTimeout: ReturnType<typeof setTimeout> | undefined;
|
|
255
|
+
let reasoningPromise: Promise<"reasoning-hold-expired"> | undefined;
|
|
180
256
|
try {
|
|
181
257
|
while (true) {
|
|
182
258
|
const pendingNext = iterator.next();
|
|
183
|
-
const
|
|
259
|
+
const raceCandidates: Promise<IteratorResult<AdapterEvent> | "timeout" | "reasoning-hold-expired">[] = [pendingNext];
|
|
260
|
+
if (timeoutPromise) raceCandidates.push(timeoutPromise);
|
|
261
|
+
if (reasoningPromise) raceCandidates.push(reasoningPromise);
|
|
262
|
+
const raced = await Promise.race(raceCandidates);
|
|
184
263
|
if (raced === "timeout") {
|
|
185
264
|
return {
|
|
186
265
|
stream: replay(buffered, iterator, pendingNext),
|
|
@@ -189,19 +268,73 @@ export async function preflightAdapterEvents(
|
|
|
189
268
|
timedOut: true,
|
|
190
269
|
};
|
|
191
270
|
}
|
|
271
|
+
if (raced === "reasoning-hold-expired") {
|
|
272
|
+
return {
|
|
273
|
+
stream: replay(buffered, iterator, pendingNext),
|
|
274
|
+
empty: false,
|
|
275
|
+
replayUnsafe,
|
|
276
|
+
};
|
|
277
|
+
}
|
|
192
278
|
const next = raced;
|
|
193
|
-
if (next.done)
|
|
279
|
+
if (next.done) {
|
|
280
|
+
const hasMeaningful = buffered.some(event => event.type !== "heartbeat");
|
|
281
|
+
return { stream: replay(buffered, iterator), empty: !hasMeaningful, replayUnsafe };
|
|
282
|
+
}
|
|
194
283
|
if (next.value.type === "heartbeat") {
|
|
284
|
+
const wasUnsafe = replayUnsafe;
|
|
195
285
|
replayUnsafe ||= next.value.replayUnsafe === true;
|
|
196
286
|
// Preserve the latch in replay even after the original unsafe heartbeat is evicted.
|
|
197
287
|
buffered.push(replayUnsafe ? { ...next.value, replayUnsafe: true } : next.value);
|
|
288
|
+
retainedHeartbeats += 1;
|
|
289
|
+
if (replayUnsafe && !wasUnsafe) {
|
|
290
|
+
for (let i = 0; i < buffered.length; i++) {
|
|
291
|
+
const ev = buffered[i];
|
|
292
|
+
if (ev.type === "heartbeat" && !ev.replayUnsafe) {
|
|
293
|
+
buffered[i] = { ...ev, replayUnsafe: true };
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
}
|
|
198
297
|
if (next.value.preflightReady === true && options?.honorReady !== false) {
|
|
199
298
|
return { stream: replay(buffered, iterator), empty: false, replayUnsafe, ready: true };
|
|
200
299
|
}
|
|
201
|
-
if (
|
|
300
|
+
if (retainedHeartbeats > PREFLIGHT_HEARTBEAT_RETAIN_LIMIT) {
|
|
301
|
+
const firstHeartbeatIndex = buffered.findIndex(e => e.type === "heartbeat");
|
|
302
|
+
if (firstHeartbeatIndex !== -1) {
|
|
303
|
+
buffered.splice(firstHeartbeatIndex, 1);
|
|
304
|
+
retainedHeartbeats -= 1;
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
continue;
|
|
308
|
+
}
|
|
309
|
+
const shouldBuffer = classifyFirstEvent !== undefined || options?.bufferCompaction === true;
|
|
310
|
+
const emptyDelta = next.value.type === "text_delta" && next.value.text.length === 0;
|
|
311
|
+
const isBufferingEvent = isReasoningEvent(next.value)
|
|
312
|
+
|| emptyDelta
|
|
313
|
+
|| (options?.bufferCompaction === true && next.value.type === "text_delta");
|
|
314
|
+
|
|
315
|
+
if (isBufferingEvent) {
|
|
316
|
+
if (!shouldBuffer || maxReasoningHoldMs <= 0) {
|
|
317
|
+
buffered.push(next.value);
|
|
318
|
+
return { stream: replay(buffered, iterator), empty: false, replayUnsafe };
|
|
319
|
+
}
|
|
320
|
+
if (!reasoningPromise && maxReasoningHoldMs > 0) {
|
|
321
|
+
reasoningPromise = new Promise(resolve => {
|
|
322
|
+
reasoningTimeout = setTimeout(() => resolve("reasoning-hold-expired"), maxReasoningHoldMs);
|
|
323
|
+
});
|
|
324
|
+
}
|
|
325
|
+
buffered.push(next.value);
|
|
326
|
+
bufferedReasoningCodeUnits += retainedEventCodeUnits(next.value);
|
|
327
|
+
if (buffered.length > DEFAULT_MAX_REASONING_EVENTS || bufferedReasoningCodeUnits > DEFAULT_MAX_EVENT_CODE_UNITS) {
|
|
328
|
+
return { stream: replay(buffered, iterator), empty: false, replayUnsafe };
|
|
329
|
+
}
|
|
202
330
|
continue;
|
|
203
331
|
}
|
|
204
|
-
const
|
|
332
|
+
const hasPriorMeaningful = buffered.some(e => e.type !== "heartbeat");
|
|
333
|
+
const classifiedError = replayUnsafe
|
|
334
|
+
? undefined
|
|
335
|
+
: hasPriorMeaningful
|
|
336
|
+
? (next.value.type === "done" || next.value.type === "incomplete" ? classifyFirstEvent?.(next.value) : undefined)
|
|
337
|
+
: classifyFirstEvent?.(next.value);
|
|
205
338
|
if (classifiedError) {
|
|
206
339
|
buffered.push(classifiedError);
|
|
207
340
|
await iterator.return?.();
|
|
@@ -216,6 +349,7 @@ export async function preflightAdapterEvents(
|
|
|
216
349
|
}
|
|
217
350
|
} finally {
|
|
218
351
|
if (timeout !== undefined) clearTimeout(timeout);
|
|
352
|
+
if (reasoningTimeout !== undefined) clearTimeout(reasoningTimeout);
|
|
219
353
|
}
|
|
220
354
|
}
|
|
221
355
|
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { inheritAccountAutoPauseResponse } from "../lib/account-auto-pause-response";
|
|
2
2
|
import { readBoundedResponseBody } from "../lib/bounded-body";
|
|
3
|
+
import { providerErrorTypeOf, withProviderFailureFacts } from "../lib/failure-detail";
|
|
3
4
|
import { redactSecretString } from "../lib/redact";
|
|
4
5
|
|
|
5
6
|
const ABSOLUTE_PATH_PATTERN = /(?:\/Users\/[^ "';,]+|\/home\/[^ "';,]+|\/root\/[^ "';,]*|[A-Za-z]:\\Users\\[^ "';,]+)/g;
|
|
@@ -41,9 +42,10 @@ export async function normalizeUpstreamHttpErrorResponse(
|
|
|
41
42
|
const headers = new Headers(res.headers);
|
|
42
43
|
headers.delete("content-encoding");
|
|
43
44
|
headers.delete("content-length");
|
|
44
|
-
|
|
45
|
+
// The rewritten body is display text; keep the provider's closed error class for the request log (#8446).
|
|
46
|
+
return withProviderFailureFacts(inheritAccountAutoPauseResponse(res, new Response(await opts.formatMessage(payloadText), {
|
|
45
47
|
status: res.status,
|
|
46
48
|
statusText: res.statusText,
|
|
47
49
|
headers,
|
|
48
|
-
}));
|
|
50
|
+
})), { type: providerErrorTypeOf(parseUpstreamJsonPayload(payloadText)) });
|
|
49
51
|
}
|
package/src/bridge/sse.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { UNDECLARED_TOOL_CALL_MESSAGE, isProxyError, rememberProxyIncomplete, carryProxyIncompleteContext } from "../lib/client-error-body";
|
|
1
|
+
import { UNDECLARED_TOOL_CALL_MESSAGE, isProxyError, rememberProxyIncomplete, rememberPausedTurn, carryProxyIncompleteContext } from "../lib/client-error-body";
|
|
2
2
|
import { anthropicProgressItems } from "../responses/anthropic-progress";
|
|
3
3
|
import type {
|
|
4
4
|
AdapterEvent,
|
|
@@ -1386,6 +1386,10 @@ export function bridgeToResponsesSSE(
|
|
|
1386
1386
|
if (truncationReasonFor(event.stopReason) && !maxOutputCompletion.completed) {
|
|
1387
1387
|
// Upstream stopped before a normal completion. Surface as incomplete so the
|
|
1388
1388
|
// client can distinguish a truncated/filtered turn from a finished one.
|
|
1389
|
+
// `pause_turn` also maps to max_output_tokens for the client, but the turn is
|
|
1390
|
+
// paused and expected to continue: record the request-scoped fact so combo
|
|
1391
|
+
// preflight does not read it as an exhausted output budget and replay it.
|
|
1392
|
+
if (event.stopReason?.trim().toLowerCase() === "pause_turn") rememberPausedTurn(proxyIncompleteContext);
|
|
1389
1393
|
// #1926 gap 2: bound the window in which a handed-out thought signature is
|
|
1390
1394
|
// not yet durable before the turn becomes externally terminal.
|
|
1391
1395
|
await awaitThoughtSignatureDurability();
|
|
@@ -1418,6 +1422,9 @@ export function bridgeToResponsesSSE(
|
|
|
1418
1422
|
}
|
|
1419
1423
|
case "incomplete": {
|
|
1420
1424
|
if (isProxyError(event) && event.reason === "adapter_eof") rememberProxyIncomplete(proxyIncompleteContext, "adapter_eof");
|
|
1425
|
+
// Same request-scoped distinction as the `done` case below: the client still sees
|
|
1426
|
+
// max_output_tokens, while combo preflight can tell a paused turn from a real limit.
|
|
1427
|
+
if (event.reason?.trim().toLowerCase() === "pause_turn") rememberPausedTurn(proxyIncompleteContext);
|
|
1421
1428
|
if (currentMsg) closeCurrentMessage();
|
|
1422
1429
|
if (currentReasoning) closeCurrentReasoning();
|
|
1423
1430
|
if (currentRawReasoning) closeCurrentRawReasoning();
|
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
import type { ProviderAdapter } from "../adapters/base";
|
|
2
|
+
import { isAllowedToolChoice, type AdapterEvent, type OcxAssistantContentPart, type OcxContentPart, type OcxMessage, type OcxParsedRequest, type OcxTool } from "../types";
|
|
3
|
+
import { isRec, type Rec } from "./inbound-records";
|
|
4
|
+
import { readClientToolDiscovery } from "./inbound-tool-loading";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Chat-wire counterpart of the Responses `additional_tools` projection (#7452/#7453).
|
|
8
|
+
*
|
|
9
|
+
* A Chat Completions upstream renders the tool catalog ahead of every message, so a definition
|
|
10
|
+
* that ToolSearch loads mid-conversation rewrites the prompt prefix and the provider re-reads
|
|
11
|
+
* the whole history. The Chat wire has no chronological tool carrier, so on opted-in Chat routes
|
|
12
|
+
* the catalog instead carries one stable dispatcher from the first request on. A loaded deferred
|
|
13
|
+
* tool's definition is appended as text to the ToolSearch result that loaded it (the transcript
|
|
14
|
+
* tail at the time), the model calls it through the dispatcher, and the response is rewritten
|
|
15
|
+
* back to the client tool before the client sees it. History is re-encoded the same way on every
|
|
16
|
+
* request, so earlier bytes never change.
|
|
17
|
+
*/
|
|
18
|
+
export const CHAT_TOOL_DISPATCH_NAME = "use_loaded_tool";
|
|
19
|
+
|
|
20
|
+
const DISPATCH_TOOL: OcxTool = {
|
|
21
|
+
name: CHAT_TOOL_DISPATCH_NAME,
|
|
22
|
+
description: "Call a tool that was loaded during this conversation with ToolSearch. Loaded tools and their "
|
|
23
|
+
+ "parameters are listed in the ToolSearch result that loaded them. Pass the loaded tool's exact name and "
|
|
24
|
+
+ "an arguments object that matches its parameters.",
|
|
25
|
+
parameters: {
|
|
26
|
+
type: "object",
|
|
27
|
+
properties: {
|
|
28
|
+
name: { type: "string", description: "Exact name of a tool loaded with ToolSearch." },
|
|
29
|
+
arguments: { type: "object", description: "Arguments for that tool, matching its parameters.", additionalProperties: true },
|
|
30
|
+
},
|
|
31
|
+
required: ["name", "arguments"],
|
|
32
|
+
additionalProperties: false,
|
|
33
|
+
},
|
|
34
|
+
strict: false,
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
export interface ChatToolDispatchProjection {
|
|
38
|
+
parsed: OcxParsedRequest;
|
|
39
|
+
/** Client tools the dispatcher currently stands in for. */
|
|
40
|
+
loaded: ReadonlySet<string>;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** Loaded definitions as transcript text; deterministic so the same history re-encodes identically. */
|
|
44
|
+
function loadedToolsText(tools: readonly OcxTool[]): string {
|
|
45
|
+
const blocks = tools.map(tool => `<tool name=${JSON.stringify(tool.name)}>\n`
|
|
46
|
+
+ `Description: ${tool.description}\n`
|
|
47
|
+
+ `Parameters (JSON Schema): ${JSON.stringify(tool.parameters)}\n</tool>`);
|
|
48
|
+
return `\n[These tools are now loaded. Call them through \`${CHAT_TOOL_DISPATCH_NAME}\` with `
|
|
49
|
+
+ `{"name": "<tool name>", "arguments": {...}}.]\n${blocks.join("\n")}\n`;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function appendText(content: string | OcxContentPart[], text: string): string | OcxContentPart[] {
|
|
53
|
+
if (typeof content === "string") return content + text;
|
|
54
|
+
return [...content, { type: "text", text }];
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Project an opted-in Chat request onto a stable catalog, or return null to send it unchanged.
|
|
59
|
+
* Same discovery evidence as appendLoadedToolDefinitions: a successful structured ToolSearch
|
|
60
|
+
* result naming a deferred tool that this request still declares.
|
|
61
|
+
*/
|
|
62
|
+
export function projectChatToolDispatch(parsed: OcxParsedRequest, raw: Rec): ChatToolDispatchProjection | null {
|
|
63
|
+
const tools = parsed.context.tools;
|
|
64
|
+
if (!Array.isArray(tools)) return null;
|
|
65
|
+
|
|
66
|
+
const declared = new Map<string, OcxTool>();
|
|
67
|
+
for (const tool of tools) {
|
|
68
|
+
if (declared.has(tool.name)) return null;
|
|
69
|
+
declared.set(tool.name, tool);
|
|
70
|
+
}
|
|
71
|
+
// A client tool that already uses the dispatcher name would make the mapping ambiguous.
|
|
72
|
+
if (declared.has(CHAT_TOOL_DISPATCH_NAME)) return null;
|
|
73
|
+
const discovery = readClientToolDiscovery(raw, name => declared.has(name));
|
|
74
|
+
if (!discovery) return null;
|
|
75
|
+
const { deferred, used, loadAt } = discovery;
|
|
76
|
+
// The dispatcher exists only where discovery exists, and from that session's first request.
|
|
77
|
+
if (!declared.has("ToolSearch") || deferred.has("ToolSearch")) return null;
|
|
78
|
+
|
|
79
|
+
// Deferred tools stay out of the catalog unless they were already called without a discovery
|
|
80
|
+
// result (eager history, or a load the client compacted away): those keep their own slot.
|
|
81
|
+
const nativeUsed = new Set([...used].filter(name => !loadAt.has(name)));
|
|
82
|
+
const hidden = new Set([...deferred].filter(name => declared.has(name) && !nativeUsed.has(name)));
|
|
83
|
+
// Explicit forced-tool intent on a hidden tool cannot be expressed through the dispatcher.
|
|
84
|
+
const choice = parsed.options.toolChoice;
|
|
85
|
+
if (isRec(choice) && typeof (choice as Rec).name === "string" && hidden.has((choice as Rec).name as string)) return null;
|
|
86
|
+
// An allowed-tools restriction names tools the projection renames or hides; keep it exact.
|
|
87
|
+
if (isAllowedToolChoice(choice)) return null;
|
|
88
|
+
|
|
89
|
+
const additions = new Map<string, OcxTool[]>();
|
|
90
|
+
for (const [name, id] of loadAt) {
|
|
91
|
+
const list = additions.get(id) ?? [];
|
|
92
|
+
list.push(declared.get(name)!);
|
|
93
|
+
additions.set(id, list);
|
|
94
|
+
}
|
|
95
|
+
// Every discovery result must be present exactly once to carry its definitions.
|
|
96
|
+
const resultCounts = new Map<string, number>();
|
|
97
|
+
for (const message of parsed.context.messages) {
|
|
98
|
+
if (message.role === "toolResult") resultCounts.set(message.toolCallId, (resultCounts.get(message.toolCallId) ?? 0) + 1);
|
|
99
|
+
}
|
|
100
|
+
for (const id of additions.keys()) if (resultCounts.get(id) !== 1) return null;
|
|
101
|
+
|
|
102
|
+
const loaded = new Set(loadAt.keys());
|
|
103
|
+
const dispatchedCalls = new Set<string>();
|
|
104
|
+
const messages: OcxMessage[] = parsed.context.messages.map(message => {
|
|
105
|
+
if (message.role === "assistant") {
|
|
106
|
+
let changed = false;
|
|
107
|
+
const content = message.content.map((part): OcxAssistantContentPart => {
|
|
108
|
+
if (part.type !== "toolCall" || !loaded.has(part.name)) return part;
|
|
109
|
+
changed = true;
|
|
110
|
+
dispatchedCalls.add(part.id);
|
|
111
|
+
return { ...part, name: CHAT_TOOL_DISPATCH_NAME, arguments: { name: part.name, arguments: part.arguments } };
|
|
112
|
+
});
|
|
113
|
+
return changed ? { ...message, content } : message;
|
|
114
|
+
}
|
|
115
|
+
if (message.role === "toolResult") {
|
|
116
|
+
const added = additions.get(message.toolCallId);
|
|
117
|
+
const renamed = dispatchedCalls.has(message.toolCallId);
|
|
118
|
+
if (!added && !renamed) return message;
|
|
119
|
+
return {
|
|
120
|
+
...message,
|
|
121
|
+
...(renamed ? { toolName: CHAT_TOOL_DISPATCH_NAME } : {}),
|
|
122
|
+
...(added ? { content: appendText(message.content, loadedToolsText(added)) } : {}),
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
return message;
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
const visible = tools.filter(tool => !hidden.has(tool.name));
|
|
129
|
+
return {
|
|
130
|
+
parsed: { ...parsed, context: { ...parsed.context, messages, tools: [...visible, DISPATCH_TOOL] } },
|
|
131
|
+
loaded,
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Read a dispatcher call's arguments as exactly `{name, arguments?}`. `arguments` may be an object
|
|
137
|
+
* or a JSON string holding one (a common double-encoding). Anything else, including extra
|
|
138
|
+
* top-level keys that would otherwise be dropped silently, an empty name, or the dispatcher's own
|
|
139
|
+
* name, is not a dispatch.
|
|
140
|
+
*/
|
|
141
|
+
function dispatchTarget(text: string): { name: string; args: string } | null {
|
|
142
|
+
let value: unknown;
|
|
143
|
+
try { value = JSON.parse(text); } catch { return null; }
|
|
144
|
+
if (!isRec(value) || typeof value.name !== "string" || value.name === "" || value.name === CHAT_TOOL_DISPATCH_NAME) return null;
|
|
145
|
+
if (Object.keys(value).some(key => key !== "name" && key !== "arguments")) return null;
|
|
146
|
+
let args: unknown = value.arguments ?? {};
|
|
147
|
+
if (typeof args === "string") {
|
|
148
|
+
try { args = JSON.parse(args); } catch { return null; }
|
|
149
|
+
}
|
|
150
|
+
return isRec(args) ? { name: value.name, args: JSON.stringify(args) } : null;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Rewrites dispatcher calls back to the client tool they name, one adapter event at a time.
|
|
155
|
+
* Every well-formed dispatch becomes a call to the tool it names, whether or not that tool was
|
|
156
|
+
* loaded: the client then runs a declared tool exactly as a direct call, or reports an unknown
|
|
157
|
+
* tool by its real name. Claude inbound never enforces declared tool names, so this grants
|
|
158
|
+
* nothing a direct call could not reach. A malformed, extra-key or incomplete call passes through
|
|
159
|
+
* unchanged instead of the proxy guessing.
|
|
160
|
+
*/
|
|
161
|
+
function chatToolDispatchRewriter() {
|
|
162
|
+
let pending: { start: Extract<AdapterEvent, { type: "tool_call_start" }>; args: string } | null = null;
|
|
163
|
+
const release = (): AdapterEvent[] => {
|
|
164
|
+
if (!pending) return [];
|
|
165
|
+
const out: AdapterEvent[] = [pending.start];
|
|
166
|
+
if (pending.args.length > 0) out.push({ type: "tool_call_delta", arguments: pending.args });
|
|
167
|
+
pending = null;
|
|
168
|
+
return out;
|
|
169
|
+
};
|
|
170
|
+
const push = (event: AdapterEvent): AdapterEvent[] => {
|
|
171
|
+
if (pending) {
|
|
172
|
+
if (event.type === "tool_call_delta") { pending.args += event.arguments; return []; }
|
|
173
|
+
if (event.type === "tool_call_end") {
|
|
174
|
+
const target = event.incomplete ? null : dispatchTarget(pending.args);
|
|
175
|
+
if (!target) return [...release(), event];
|
|
176
|
+
const start = { ...pending.start, name: target.name };
|
|
177
|
+
pending = null;
|
|
178
|
+
return [start, { type: "tool_call_delta", arguments: target.args }, event];
|
|
179
|
+
}
|
|
180
|
+
// Any other event closes buffering without a terminal; surface what was buffered.
|
|
181
|
+
const out = release();
|
|
182
|
+
return [...out, ...push(event)];
|
|
183
|
+
}
|
|
184
|
+
if (event.type === "tool_call_start" && event.name === CHAT_TOOL_DISPATCH_NAME) {
|
|
185
|
+
pending = { start: event, args: "" };
|
|
186
|
+
return [];
|
|
187
|
+
}
|
|
188
|
+
return [event];
|
|
189
|
+
};
|
|
190
|
+
return { push, flush: release };
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/** Streaming form: rewrite dispatcher calls in an adapter stream (see chatToolDispatchRewriter). */
|
|
194
|
+
export async function* rewriteChatToolDispatchEvents(
|
|
195
|
+
events: AsyncIterable<AdapterEvent>,
|
|
196
|
+
): AsyncGenerator<AdapterEvent> {
|
|
197
|
+
const rewriter = chatToolDispatchRewriter();
|
|
198
|
+
for await (const event of events) yield* rewriter.push(event);
|
|
199
|
+
yield* rewriter.flush();
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/** Non-streaming form of rewriteChatToolDispatchEvents for adapter `parseResponse` results. */
|
|
203
|
+
export function rewriteChatToolDispatchEventList(events: readonly AdapterEvent[]): AdapterEvent[] {
|
|
204
|
+
const rewriter = chatToolDispatchRewriter();
|
|
205
|
+
return [...events.flatMap(event => rewriter.push(event)), ...rewriter.flush()];
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Wrap one physical openai-chat adapter: project each built request and un-project both parse
|
|
210
|
+
* paths. Each build decides afresh, so a request that bails out (forced hidden tool, allowed-tools
|
|
211
|
+
* restriction, duplicate call id) is sent unchanged and its response is not rewritten.
|
|
212
|
+
*/
|
|
213
|
+
export function applyChatToolDispatch(adapter: ProviderAdapter, discovery: Rec): void {
|
|
214
|
+
const build = adapter.buildRequest.bind(adapter);
|
|
215
|
+
const parseStream = adapter.parseStream.bind(adapter);
|
|
216
|
+
const parseResponse = adapter.parseResponse?.bind(adapter);
|
|
217
|
+
let projected = false;
|
|
218
|
+
adapter.buildRequest = (parsed, incoming) => {
|
|
219
|
+
const projection = projectChatToolDispatch(parsed, discovery);
|
|
220
|
+
projected = projection !== null;
|
|
221
|
+
return build(projection ? projection.parsed : parsed, incoming);
|
|
222
|
+
};
|
|
223
|
+
adapter.parseStream = (...args) => {
|
|
224
|
+
const events = parseStream(...args);
|
|
225
|
+
return projected ? rewriteChatToolDispatchEvents(events) : events;
|
|
226
|
+
};
|
|
227
|
+
if (parseResponse) {
|
|
228
|
+
adapter.parseResponse = async (...args) => {
|
|
229
|
+
const events = await parseResponse(...args);
|
|
230
|
+
return projected ? rewriteChatToolDispatchEventList(events) : events;
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
}
|
|
@@ -1,32 +1,41 @@
|
|
|
1
1
|
import { isRec, type Rec } from "./inbound-records";
|
|
2
2
|
|
|
3
|
-
/**
|
|
4
|
-
|
|
3
|
+
/**
|
|
4
|
+
* Selector list shared by appendOnlyToolLoadingModels and chatToolDispatchModels: provider-qualified
|
|
5
|
+
* routes, optionally ending in one prefix wildcard; never a global wildcard.
|
|
6
|
+
*/
|
|
7
|
+
export function isToolLoadingModelSelectors(value: unknown): value is string[] {
|
|
5
8
|
return Array.isArray(value) && value.length <= 128 && value.every(entry =>
|
|
6
9
|
typeof entry === "string" && entry.length <= 512 && /^[^\s/*]+\/[^\s*]+\*?$/.test(entry));
|
|
7
10
|
}
|
|
8
11
|
|
|
9
|
-
|
|
10
|
-
|
|
12
|
+
/** Whether a resolved provider/model route matches a valid tool-loading selector list. */
|
|
13
|
+
export function toolLoadingModelMatches(value: unknown, model: string): boolean {
|
|
14
|
+
return isToolLoadingModelSelectors(value) && value.some(pattern =>
|
|
11
15
|
pattern.endsWith("*") ? model.startsWith(pattern.slice(0, -1)) : model === pattern);
|
|
12
16
|
}
|
|
13
17
|
|
|
14
|
-
/**
|
|
15
|
-
export
|
|
16
|
-
|
|
17
|
-
|
|
18
|
+
/** Client discovery evidence in the original Messages body (shared by the Responses and Chat projections). */
|
|
19
|
+
export interface ClientToolDiscovery {
|
|
20
|
+
/** Tools the client declared with `defer_loading: true`. */
|
|
21
|
+
deferred: Set<string>;
|
|
22
|
+
/** Every tool name the assistant has called. */
|
|
23
|
+
used: Set<string>;
|
|
24
|
+
/** Deferred tool -> the ToolSearch call id whose successful result first loaded it. */
|
|
25
|
+
loadAt: Map<string, string>;
|
|
26
|
+
}
|
|
18
27
|
|
|
19
|
-
|
|
28
|
+
/**
|
|
29
|
+
* Read discovery evidence: a successful structured ToolSearch `tool_reference` for a deferred tool
|
|
30
|
+
* this request still declares and has not called before that result. Returns null when the
|
|
31
|
+
* client body is not the expected shape or reuses a tool_use id, so callers send it unchanged.
|
|
32
|
+
*/
|
|
33
|
+
export function readClientToolDiscovery(raw: Rec, isDeclared: (name: string) => boolean): ClientToolDiscovery | null {
|
|
34
|
+
if (!Array.isArray(raw.tools) || !Array.isArray(raw.messages)) return null;
|
|
20
35
|
const deferred = new Set<string>();
|
|
21
|
-
for (const tool of body.tools) {
|
|
22
|
-
if (!isRec(tool) || tool.type !== "function" || typeof tool.name !== "string") continue;
|
|
23
|
-
if (declared.has(tool.name)) return body;
|
|
24
|
-
declared.set(tool.name, tool);
|
|
25
|
-
}
|
|
26
36
|
for (const tool of raw.tools) {
|
|
27
37
|
if (isRec(tool) && tool.defer_loading === true && typeof tool.name === "string") deferred.add(tool.name);
|
|
28
38
|
}
|
|
29
|
-
|
|
30
39
|
const calls = new Map<string, string>();
|
|
31
40
|
const used = new Set<string>();
|
|
32
41
|
const loadAt = new Map<string, string>();
|
|
@@ -36,7 +45,7 @@ export function appendLoadedToolDefinitions(body: Rec, raw: Rec): Rec {
|
|
|
36
45
|
if (!isRec(block)) continue;
|
|
37
46
|
if (message.role === "assistant" && block.type === "tool_use"
|
|
38
47
|
&& typeof block.id === "string" && typeof block.name === "string") {
|
|
39
|
-
if (calls.has(block.id)) return
|
|
48
|
+
if (calls.has(block.id)) return null;
|
|
40
49
|
calls.set(block.id, block.name);
|
|
41
50
|
used.add(block.name);
|
|
42
51
|
}
|
|
@@ -46,12 +55,28 @@ export function appendLoadedToolDefinitions(body: Rec, raw: Rec): Rec {
|
|
|
46
55
|
for (const reference of block.content) {
|
|
47
56
|
if (!isRec(reference) || reference.type !== "tool_reference" || typeof reference.tool_name !== "string") continue;
|
|
48
57
|
const name = reference.tool_name;
|
|
49
|
-
if (
|
|
58
|
+
if (isDeclared(name) && deferred.has(name) && !used.has(name) && !loadAt.has(name)) {
|
|
50
59
|
loadAt.set(name, block.tool_use_id);
|
|
51
60
|
}
|
|
52
61
|
}
|
|
53
62
|
}
|
|
54
63
|
}
|
|
64
|
+
return { deferred, used, loadAt };
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** Replay client discovery in place, using only definitions still declared by this request. */
|
|
68
|
+
export function appendLoadedToolDefinitions(body: Rec, raw: Rec): Rec {
|
|
69
|
+
if (!Array.isArray(body.tools) || !Array.isArray(body.input)) return body;
|
|
70
|
+
|
|
71
|
+
const declared = new Map<string, Rec>();
|
|
72
|
+
for (const tool of body.tools) {
|
|
73
|
+
if (!isRec(tool) || tool.type !== "function" || typeof tool.name !== "string") continue;
|
|
74
|
+
if (declared.has(tool.name)) return body;
|
|
75
|
+
declared.set(tool.name, tool);
|
|
76
|
+
}
|
|
77
|
+
const discovery = readClientToolDiscovery(raw, name => declared.has(name));
|
|
78
|
+
if (!discovery) return body;
|
|
79
|
+
const { deferred, used, loadAt } = discovery;
|
|
55
80
|
// Discovery must remain callable; this optimization does not create a search capability.
|
|
56
81
|
if (!declared.has("ToolSearch") || deferred.has("ToolSearch")) return body;
|
|
57
82
|
|