@bitkyc08/opencodex 2.98.0-preview.20261107 → 2.99.0-preview.20261108
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/bin/ocx.mjs +6 -19
- package/gui/dist/assets/{App-6TFtZsUF.css → App-C1hGwaN1.css} +1 -1
- package/gui/dist/assets/App-CWz0areF.js +59 -0
- package/gui/dist/assets/Tray-BosDoskm.js +1 -0
- package/gui/dist/assets/{brand-mark-VPhzz-WE.js → brand-mark-DpiBmFGI.js} +1 -1
- package/gui/dist/assets/{index-BWKBeK77.js → index-CAxjGWkm.js} +13 -13
- package/gui/dist/assets/index-bnUWypyz.css +2 -0
- package/gui/dist/assets/{segment-thumb-features-BQYzrze9.js → segment-thumb-features-JcszQ18T.js} +1 -1
- package/gui/dist/classic/assets/App-BOEsFmiN.js +58 -0
- package/gui/dist/classic/assets/Tray-5v86r9Az.js +1 -0
- package/gui/dist/classic/assets/{index-Dp2j8bSc.js → index-ClZ_zZ77.js} +13 -13
- package/gui/dist/classic/assets/{quota-summary-CvamtHjB.js → quota-summary-xwvDtvu7.js} +1 -1
- package/gui/dist/classic/index.html +1 -1
- package/gui/dist/index.html +2 -2
- package/package.json +1 -1
- package/src/adapters/coding-agent/protocol.ts +8 -3
- package/src/adapters/coding-agent/turn.ts +6 -2
- package/src/adapters/command-code.ts +8 -5
- package/src/adapters/devin.ts +11 -1
- package/src/adapters/kiro/stream.ts +5 -4
- package/src/adapters/openai-chat/destination-policy.ts +25 -0
- package/src/adapters/openai-chat/native-content.ts +32 -0
- package/src/adapters/openai-chat/passthrough.ts +14 -1
- package/src/adapters/openai-chat/reasoning-wire.ts +42 -1
- package/src/adapters/openai-chat.ts +2 -2
- package/src/adapters/qoder/adapter.ts +9 -0
- package/src/bridge/response-json.ts +13 -1
- package/src/bridge/sse.ts +112 -22
- package/src/cli/account-affinity-reset.ts +42 -0
- package/src/cli/account.ts +5 -0
- package/src/cli/capabilities-accounts.ts +8 -0
- package/src/cli/capabilities-agents-routing.ts +5 -4
- package/src/cli/message-command.ts +1 -1
- package/src/cli/message-mcp-command.ts +8 -5
- package/src/client/orphan-token-recovery.ts +1 -1
- package/src/codex/autostart-health.ts +4 -2
- package/src/generated/compatibility-version.json +128 -76
- package/src/lib/desktop-cli-handoff.mjs +5 -8
- package/src/lib/failure-detail.ts +5 -2
- package/src/lib/launcher-signal-forwarding.d.mts +8 -0
- package/src/lib/launcher-signal-forwarding.mjs +36 -0
- package/src/lib/privacy.ts +6 -0
- package/src/lib/request-execution-budget.ts +188 -33
- package/src/lib/upstream-validation-detail.ts +47 -0
- package/src/lib/workflow-budget.ts +35 -16
- package/src/messaging/mcp-queue-rpc.ts +7 -3
- package/src/messaging/mcp-queue-server.ts +6 -0
- package/src/messaging/mcp-reply-cursor.ts +34 -0
- package/src/messaging/mcp-reply-tools.ts +93 -0
- package/src/oauth/anthropic-routing.ts +46 -3
- package/src/oauth/anthropic-session-affinity-store.ts +23 -10
- package/src/protocols/codecs/ollama.ts +22 -12
- package/src/providers/registry/entries-extended.ts +14 -0
- package/src/remote/hub-usage.ts +7 -0
- package/src/responses/parser.ts +27 -0
- package/src/responses/reasoning-envelope.ts +4 -0
- package/src/responses/schema.ts +1 -0
- package/src/server/chat-completions.ts +3 -0
- package/src/server/chat-native-sse.ts +14 -10
- package/src/server/chat-native.ts +24 -11
- package/src/server/index/websocket-handler.ts +12 -1
- package/src/server/management/oauth-account-routes.ts +65 -13
- package/src/server/management/route-registry.ts +1 -0
- package/src/server/messages-native.ts +21 -5
- package/src/server/request-log-attempt-recovery.ts +17 -0
- package/src/server/request-log-failure-detail.ts +18 -1
- package/src/server/request-log.ts +5 -18
- package/src/server/responses/account-auto-pause-recovery.ts +29 -1
- package/src/server/responses/adapter-continuation.ts +12 -5
- package/src/server/responses/adapter-dispatch.ts +50 -51
- package/src/server/responses/antigravity-model-denial-recovery.ts +4 -2
- package/src/server/responses/codex-ws-exchange.ts +13 -4
- package/src/server/responses/combo-required-tool-contract.ts +22 -3
- package/src/server/responses/core-codex-account.ts +92 -86
- package/src/server/responses/core-combo-failure.ts +7 -0
- package/src/server/responses/core-combo.ts +11 -4
- package/src/server/responses/core-lifetime.ts +3 -0
- package/src/server/responses/deferred-send-reporter.ts +18 -1
- package/src/server/responses/fetch-helpers.ts +9 -3
- package/src/server/responses/native-continuation-refusal.ts +8 -0
- package/src/server/responses/native-continuation-send.ts +34 -0
- package/src/server/responses/passthrough-delivery.ts +6 -2
- package/src/server/responses/passthrough-dispatch.ts +48 -33
- package/src/server/responses/passthrough-execution.ts +1 -0
- package/src/server/responses/policy-fallback.ts +32 -5
- package/src/server/responses/policy-request-scope.ts +26 -0
- package/src/server/responses/request-prepare.ts +11 -2
- package/src/server/responses/request-send-budget.ts +96 -14
- package/src/server/responses/request-spend-target.ts +19 -0
- package/src/server/responses/request-spend.ts +80 -28
- package/src/server/responses/request-transport.ts +106 -53
- package/src/server/responses/run-turn-execution.ts +3 -0
- package/src/server/responses/sidecar-execution.ts +2 -1
- package/src/server/responses/ws-upstream.ts +2 -1
- package/src/server/workflow-refusal.ts +38 -4
- package/src/server/ws-bridge.ts +10 -3
- package/src/service/desktop-startup.ts +76 -13
- package/src/types/request.ts +2 -1
- package/src/usage/app-breakdown.ts +23 -0
- package/src/usage/summary.ts +34 -1
- package/src/web-search/anthropic-executor.ts +25 -9
- package/src/web-search/loop.ts +3 -1
- package/src/web-search/passthrough-bridge.ts +3 -0
- package/src/web-search/run-turn-loop.ts +3 -1
- package/gui/dist/assets/App-B8_sOxoz.js +0 -59
- package/gui/dist/assets/Tray-DpWEp6ee.js +0 -1
- package/gui/dist/assets/index-5REODpG3.css +0 -2
- package/gui/dist/classic/assets/App-Bz65x1J6.js +0 -58
- package/gui/dist/classic/assets/Tray-kk7mdO6I.js +0 -1
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { carryProxyError, proxyErrorPresentation, isProxyError, rememberProxyErrorMessage } from "../../lib/client-error-body";
|
|
2
2
|
import { bindAnthropicRejectionResponse } from "../../adapters/anthropic-rejection-memo";
|
|
3
|
+
import { deferPhysicalSendReporter } from "./deferred-send-reporter";
|
|
4
|
+
import { responsesSpendTarget } from "./request-spend-target";
|
|
3
5
|
import { rotateAnthropicAccountOnResponseForInstance } from "../../oauth/anthropic-account-refusal";
|
|
4
6
|
import type { ResponsesRequestContext } from "./core-options";
|
|
5
7
|
import type { PreparedResponsesRequest } from "./request-prepare";
|
|
@@ -32,7 +34,7 @@ import {
|
|
|
32
34
|
applyUpstreamRecoveryInit,
|
|
33
35
|
isNonReplayableResponse, isSameTargetRetryHold,
|
|
34
36
|
prepareSameTarget429Wait,
|
|
35
|
-
SendBudgetExhaustedError,
|
|
37
|
+
SendBudgetExhaustedError, errorDestinationLabel,
|
|
36
38
|
} from "../../lib/upstream-retry";
|
|
37
39
|
import { unwrapUpstreamRetryEvidenceError } from "../../codex/auth-context";
|
|
38
40
|
import { AdmissionModelDeniedError, MODEL_NOT_ALLOWED_FOR_KEY } from "../admission-model-scope";
|
|
@@ -275,11 +277,13 @@ export function createAdapterContinuations(
|
|
|
275
277
|
const fetchContinuationWithRetryPolicy = (route.provider.adapter === "google" || continuationTransientPolicy)
|
|
276
278
|
? fetchWithTransientRetry
|
|
277
279
|
: fetchWithResetRetry;
|
|
280
|
+
const continuationReporter = transientSendReporter();
|
|
281
|
+
const deferredContinuation = (continuationTransientPolicy || sendBudgetState.adapterSendBudget?.spendEnforced)
|
|
282
|
+
&& route.providerName === "github-copilot" && route.provider.authMode !== "forward"
|
|
283
|
+
? deferPhysicalSendReporter(continuationReporter) : undefined;
|
|
278
284
|
return await fetchContinuationWithRetryPolicy(
|
|
279
285
|
recovery => {
|
|
280
|
-
sendBudgetState.adapterSendBudget?.startRequest?.(
|
|
281
|
-
poolId: logCtx.spendPoolId ?? route.providerName, identityId: logCtx.accountLogLabel,
|
|
282
|
-
});
|
|
286
|
+
sendBudgetState.adapterSendBudget?.startRequest?.(responsesSpendTarget(logCtx, route.providerName));
|
|
283
287
|
transportState.noteRoutedAttemptSend(continuationEstimate, recovery ?? replayKind);
|
|
284
288
|
return fetchWithHeaderTimeout(
|
|
285
289
|
builtContinuationRequest.url,
|
|
@@ -293,6 +297,9 @@ export function createAdapterContinuations(
|
|
|
293
297
|
nextParsed.stream,
|
|
294
298
|
providerFetch(route.provider, options.codexWsRuntimeIdentity, {
|
|
295
299
|
dispatchOverride: oauthDispatch(builtContinuationRequest, nextParsed),
|
|
300
|
+
onPhysicalDispatch: deferredContinuation ? () => {
|
|
301
|
+
if (!deferredContinuation.beforeDispatch()) throw new SendBudgetExhaustedError(errorDestinationLabel(builtContinuationRequest.url));
|
|
302
|
+
} : undefined,
|
|
296
303
|
providerName: route.providerName,
|
|
297
304
|
modelId: nextParsed.modelId,
|
|
298
305
|
}),
|
|
@@ -307,7 +314,7 @@ export function createAdapterContinuations(
|
|
|
307
314
|
...(continuationTransientPolicy || sendBudgetState.adapterSendBudget?.spendEnforced
|
|
308
315
|
? {
|
|
309
316
|
attempts: remainingTransientSendBudget(continuationTransientPolicy?.attempts ?? 1),
|
|
310
|
-
onSendsConsumed:
|
|
317
|
+
onSendsConsumed: deferredContinuation?.report ?? continuationReporter,
|
|
311
318
|
}
|
|
312
319
|
: {}),
|
|
313
320
|
},
|
|
@@ -1,8 +1,10 @@
|
|
|
1
|
+
import { recordUpstreamValidationDetail } from "../request-log-failure-detail";
|
|
1
2
|
import { clientFacingModelText } from "../../lib/error-destination";
|
|
3
|
+
import { responsesSpendTarget } from "./request-spend-target";
|
|
2
4
|
import { projectLocalModelRefusalLog } from "../request-log-failure-detail";
|
|
3
5
|
import { markProxyError, carryProxyError } from "../../lib/client-error-body";
|
|
4
6
|
import { codexAdapterBuildRefusal } from "./passthrough-build-error";
|
|
5
|
-
import { recoverAutoPausedAccount } from "./account-auto-pause-recovery";
|
|
7
|
+
import { createAutoPauseRefreshErrorMapper, recoverAutoPausedAccount } from "./account-auto-pause-recovery";
|
|
6
8
|
import { wasAccountAutoPaused, wasPermanentAccountRefusal } from "../../oauth/account-auto-pause";
|
|
7
9
|
import { bindAnthropicRejectionResponse } from "../../adapters/anthropic-rejection-memo";
|
|
8
10
|
import { deferPhysicalSendReporter } from "./deferred-send-reporter";
|
|
@@ -37,6 +39,7 @@ import { anthropicPoolCooldownResponse, clientCancelledResponse, readDisplaySafe
|
|
|
37
39
|
import { isTranslatorBudgetExceededError } from "../../lib/translator-budget";
|
|
38
40
|
import { redactSecretString } from "../../lib/redact";
|
|
39
41
|
import { rewriteUpstreamPolicyRefusal } from "./policy-refusal";
|
|
42
|
+
import { carryPolicyCandidateRefusal } from "./policy-request-scope";
|
|
40
43
|
import { withProviderRequestSlot } from "../../providers/request-pacing";
|
|
41
44
|
import { providerFetch, fetchWithHeaderTimeout, safeHostLabel } from "./fetch-helpers";
|
|
42
45
|
import { isAnthropicBillingKeyPool, rotateAnthropicBillingKeyOnResponse } from "../../providers/anthropic-billing-recovery";
|
|
@@ -120,9 +123,10 @@ import { createCodexAuthDispatchGuard, releaseCodexAuthContextProbeLease, unwrap
|
|
|
120
123
|
import { isCanonicalOpenAiForwardProvider } from "../../providers/openai-tiers";
|
|
121
124
|
import { mapCodexAuthContextErrorToResponse } from "./codex-auth-error";
|
|
122
125
|
import { chargeWorkflowSends } from "../../lib/workflow-budget";
|
|
126
|
+
import { hasWorkflowSendObserver } from "../../lib/request-execution-budget";
|
|
123
127
|
import { isAntigravityValidationRefusal } from "./antigravity-validation-refusal";
|
|
124
128
|
import { createAntigravityModelDenialRecovery, recoverAntigravityAccount } from "./antigravity-model-denial-recovery";
|
|
125
|
-
import { unboundPoolSpendRefusalResponse } from "../workflow-refusal";
|
|
129
|
+
import { unboundPoolSpendRefusalResponse, workflowSendCeilingRefusalResponse } from "../workflow-refusal";
|
|
126
130
|
import { AdmissionModelDeniedError, admissionModelDeniedResponse, projectAdmissionModelDeniedLog } from "../admission-model-scope";
|
|
127
131
|
/** One responsibility of the Responses request pipeline; state owners are explicit. */
|
|
128
132
|
export async function prepareAdapterExchange(
|
|
@@ -171,7 +175,7 @@ export async function prepareAdapterExchange(
|
|
|
171
175
|
| "genericFailovers"
|
|
172
176
|
| "genericFailoverLimit"
|
|
173
177
|
| "applyFailoverSnapshot"
|
|
174
|
-
| "noteRoutedAttemptSend"
|
|
178
|
+
| "noteRoutedAttemptSend" | "attemptSendDeferred" | "deferPhysicalAttribution"
|
|
175
179
|
>,
|
|
176
180
|
responseEffects: Pick<ResponsesEffects, "cancelResponseCompletion" | "notifyResponseComplete" | "refreshRequestToolAliases">,
|
|
177
181
|
sendBudgetState: Pick<
|
|
@@ -246,12 +250,11 @@ export async function prepareAdapterExchange(
|
|
|
246
250
|
"pre-header", "connection-reset",
|
|
247
251
|
ambiguousResendAllowanceFor(route.provider, requestIsSelfContained, claimAmbiguousResend),
|
|
248
252
|
).allowed;
|
|
249
|
-
|
|
250
253
|
const upstream = new AbortController();
|
|
251
254
|
const cleanupUpstreamAbort = linkAbortSignal(upstream, options.abortSignal);
|
|
252
255
|
/** Contain replacement negotiation before adapter admission and release its upstream abort link. */
|
|
253
|
-
const resolveRotatedCopilotSelection = async (): Promise<Response | undefined> => {
|
|
254
|
-
try { await resolveCopilotSelection(parsed); }
|
|
256
|
+
const resolveRotatedCopilotSelection = async (provider = route.provider): Promise<Response | undefined> => {
|
|
257
|
+
try { await resolveCopilotSelection(parsed, false, provider); }
|
|
255
258
|
catch (error) {
|
|
256
259
|
cleanupUpstreamAbort();
|
|
257
260
|
upstream.abort();
|
|
@@ -270,7 +273,6 @@ export async function prepareAdapterExchange(
|
|
|
270
273
|
const antigravityPoolActivated = route.providerName === "google-antigravity"
|
|
271
274
|
&& isGenericOAuthFailoverEnabled(config, route.providerName);
|
|
272
275
|
const antigravityDenial = createAntigravityModelDenialRecovery();
|
|
273
|
-
|
|
274
276
|
// One immutable, body-safe outbound request per same-target sequence (URL, serialized body,
|
|
275
277
|
// auth headers, generated compat headers). Same-target 429 replays reuse it verbatim; the
|
|
276
278
|
// builder runs again only after a key/account/adapter rotation, an oauth refresh, or an
|
|
@@ -403,7 +405,7 @@ export async function prepareAdapterExchange(
|
|
|
403
405
|
const autoPauseEnabled = config.providers[route.providerName]?.accountAutoPause?.enabled === true;
|
|
404
406
|
const billingPoolRequest = isAnthropicBillingKeyPool(route.provider);
|
|
405
407
|
let billingRecoveryActive = false;
|
|
406
|
-
const genericRefetchCountsSends = () => options.comboAttempt === true || sendBudgetState.adapterSendBudget?.spendEnforced === true
|
|
408
|
+
const genericRefetchCountsSends = () => !!sendBudgetState.workflowRootId || options.comboAttempt === true || sendBudgetState.adapterSendBudget?.spendEnforced === true
|
|
407
409
|
|| transientRetryPolicyFor(route.provider) !== null || resetReplayPolicyFor(route.provider) !== null
|
|
408
410
|
|| billingPoolRequest || billingRecoveryActive || autoPauseEnabled;
|
|
409
411
|
let handoffPermit: ResponsesSendBudget["pendingHopPermit"];
|
|
@@ -479,14 +481,12 @@ export async function prepareAdapterExchange(
|
|
|
479
481
|
? fetchWithTransientRetry
|
|
480
482
|
: fetchWithResetRetry;
|
|
481
483
|
const initialReporter = transientSendReporter(compactPrepaid ?? options.comboDispatchPermit);
|
|
482
|
-
const deferredInitial = billingPoolRequest && !receiptMode ? deferPhysicalSendReporter(initialReporter) : undefined;
|
|
484
|
+
const deferredInitial = (billingPoolRequest || route.providerName === "github-copilot" && route.provider.authMode !== "forward") && !receiptMode ? deferPhysicalSendReporter(initialReporter) : undefined;
|
|
483
485
|
try { upstreamResponse = await fetchWithRetryPolicy(
|
|
484
486
|
recovery => {
|
|
485
487
|
// Unconfigured generic sends may omit the counting reporter. Capture policy
|
|
486
488
|
// independently so a later recovery still belongs to this request's start.
|
|
487
|
-
sendBudgetState.adapterSendBudget?.startRequest?.(
|
|
488
|
-
poolId: logCtx.spendPoolId ?? route.providerName, identityId: logCtx.accountLogLabel,
|
|
489
|
-
});
|
|
489
|
+
sendBudgetState.adapterSendBudget?.startRequest?.(responsesSpendTarget(logCtx, route.providerName));
|
|
490
490
|
// Receipt mode books the physical send at executor admission instead of here.
|
|
491
491
|
if (!deferredInitial && !receiptMode && compactPrepaid && !compactPrepaidUsed) {
|
|
492
492
|
if (!compactPrepaid.use()) throw new SendBudgetExhaustedError(errorDestinationLabel(builtInitialRequest.url));
|
|
@@ -526,7 +526,7 @@ export async function prepareAdapterExchange(
|
|
|
526
526
|
abortSignal: upstream.signal,
|
|
527
527
|
label: safeHostLabel(builtInitialRequest.url), destination: errorDestinationLabel(builtInitialRequest.url),
|
|
528
528
|
claimAmbiguousResend: claimPreHeaderResend,
|
|
529
|
-
...(sendBudgetState.adapterSendBudget?.spendEnforced || options.comboDispatchPermit || transientPolicy || resetPolicy || compactPrepaid || options.comboAttempt
|
|
529
|
+
...(sendBudgetState.workflowRootId || sendBudgetState.adapterSendBudget?.spendEnforced || options.comboDispatchPermit || transientPolicy || resetPolicy || compactPrepaid || options.comboAttempt
|
|
530
530
|
|| billingPoolRequest || autoPauseEnabled
|
|
531
531
|
? {
|
|
532
532
|
// A pending compaction permit already booked this leg's first physical send.
|
|
@@ -582,7 +582,7 @@ export async function prepareAdapterExchange(
|
|
|
582
582
|
// blaming the provider makes the caller send the whole turn again -- the amplification this
|
|
583
583
|
// budget exists to stop. The passthrough path has answered 429 here since #4546.
|
|
584
584
|
if (err instanceof SendBudgetExhaustedError) {
|
|
585
|
-
return unboundPoolSpendRefusalResponse(logCtx)
|
|
585
|
+
return unboundPoolSpendRefusalResponse(logCtx) ?? workflowSendCeilingRefusalResponse(logCtx, sendBudgetState.workflowRootId)
|
|
586
586
|
?? markProxyError(formatErrorResponse(429, SEND_BUDGET_EXHAUSTED_CODE, err.message));
|
|
587
587
|
}
|
|
588
588
|
const msg = describeUpstreamConnectFailure(err, connectMs);
|
|
@@ -639,15 +639,14 @@ export async function prepareAdapterExchange(
|
|
|
639
639
|
onDispatch?: () => void,
|
|
640
640
|
preserveFailureResponse?: Response,
|
|
641
641
|
): Promise<Response | { failed: Response }> => {
|
|
642
|
-
let replacementAdmitted = false;
|
|
643
|
-
//
|
|
644
|
-
// that send to the root workflow. The other two dispatch paths confirm it here, at
|
|
645
|
-
// their physical-send boundary, without booking the request ledger again.
|
|
642
|
+
let replacementAdmitted = false, dispatchConfirmed = false;
|
|
643
|
+
// Only budgets without a physical observer need this legacy workflow report.
|
|
646
644
|
let fastDowngradeWorkflowCharged = false;
|
|
647
645
|
const chargeFastDowngradeWorkflowSend = (): void => {
|
|
648
646
|
if (recovery !== "anthropic-fast-downgrade" || fastDowngradeWorkflowCharged) return;
|
|
649
647
|
fastDowngradeWorkflowCharged = true;
|
|
650
|
-
|
|
648
|
+
if (!sendBudgetState.adapterSendBudget || !hasWorkflowSendObserver(sendBudgetState.adapterSendBudget))
|
|
649
|
+
chargeWorkflowSends(sendBudgetState.workflowRootId, 1);
|
|
651
650
|
};
|
|
652
651
|
let retryRequest: AdapterRequest;
|
|
653
652
|
if (transportState.sameTargetRequest !== undefined && transportState.sameTargetParsed === parsed && transportState.sameTargetToken === transportState.transportToken) {
|
|
@@ -699,8 +698,10 @@ export async function prepareAdapterExchange(
|
|
|
699
698
|
? retryRequest.usageLog.inputTokens
|
|
700
699
|
: undefined;
|
|
701
700
|
if (retryEstimate !== undefined) logCtx.usageLogInputTokens = retryEstimate;
|
|
702
|
-
|
|
703
|
-
|
|
701
|
+
if (!transportState.deferPhysicalAttribution) {
|
|
702
|
+
logCtx.providerAdapter = transportState.activeAdapter.name;
|
|
703
|
+
sealRequestAttemptIdentity(logCtx.activeAttempt, logCtx.provider, transportState.activeAdapter.name, logCtx.accountLogLabel);
|
|
704
|
+
}
|
|
704
705
|
recordAttemptCredentialSource(logCtx.activeAttempt, route.providerName, route.provider, transportState.activeAdapter.name);
|
|
705
706
|
try {
|
|
706
707
|
try {
|
|
@@ -713,7 +714,7 @@ export async function prepareAdapterExchange(
|
|
|
713
714
|
// an abort, a saturated queue, an expired slot or a removed provider, and none of
|
|
714
715
|
// those reach the wire. Confirming earlier would hold the charge for a send that the
|
|
715
716
|
// pacer refused.
|
|
716
|
-
onDispatch?.();
|
|
717
|
+
if (!dispatchConfirmed) { onDispatch?.(); dispatchConfirmed = true; }
|
|
717
718
|
return transportState.activeAdapter.fetchResponse!(retryRequest, {
|
|
718
719
|
kiroPreferAccountFailover: route.providerName === "kiro" && isGenericOAuthFailoverEnabled(config, "kiro"),
|
|
719
720
|
abortSignal: upstream.signal,
|
|
@@ -767,27 +768,25 @@ export async function prepareAdapterExchange(
|
|
|
767
768
|
: undefined;
|
|
768
769
|
let refetchPermitUsed = false;
|
|
769
770
|
const refetchReporter = refetchAllowance ? transientSendReporter(refetchAllowance.permit) : undefined;
|
|
770
|
-
const deferredRefetch = (billingPoolRequest || options.comboAttempt) && refetchReporter
|
|
771
|
+
const deferredRefetch = (billingPoolRequest || options.comboAttempt || route.providerName === "github-copilot" && route.provider.authMode !== "forward") && refetchReporter
|
|
771
772
|
? deferPhysicalSendReporter(refetchReporter) : undefined;
|
|
772
|
-
const pendingKeyMetadata = deferredRefetch &&
|
|
773
|
+
const pendingKeyMetadata = deferredRefetch && transportState.attemptSendDeferred();
|
|
773
774
|
try {
|
|
774
775
|
return await refetchWithPolicy(
|
|
775
776
|
recoveryKind => {
|
|
776
|
-
sendBudgetState.adapterSendBudget?.startRequest?.(
|
|
777
|
-
poolId: logCtx.spendPoolId ?? route.providerName, identityId: logCtx.accountLogLabel,
|
|
778
|
-
});
|
|
777
|
+
sendBudgetState.adapterSendBudget?.startRequest?.(responsesSpendTarget(logCtx, route.providerName));
|
|
779
778
|
// Prepare key metadata before the guarded transport commits its attempt;
|
|
780
779
|
// counters and permits still wait for the physical executor after pacing.
|
|
781
780
|
if (pendingKeyMetadata) transportState.noteRoutedAttemptSend(retryEstimate, recoveryKind ?? recovery);
|
|
782
781
|
const confirmDispatch = () => {
|
|
783
|
-
if (refetchAllowance?.permit && !refetchPermitUsed) {
|
|
782
|
+
if (refetchAllowance?.permit && !refetchPermitUsed && (!onDispatch || refetchAllowance.permit !== prepaid)) {
|
|
784
783
|
if (!refetchAllowance.permit.use()) throw new SendBudgetExhaustedError(errorDestinationLabel(retryRequest.url));
|
|
785
784
|
refetchPermitUsed = true;
|
|
786
785
|
}
|
|
787
786
|
if (!pendingKeyMetadata) transportState.noteRoutedAttemptSend(retryEstimate, recoveryKind ?? recovery);
|
|
788
787
|
// Counted Combo/pool sends confirm at the physical executor; legacy
|
|
789
788
|
// helper paths retain their existing thunk admission boundary.
|
|
790
|
-
onDispatch?.();
|
|
789
|
+
if (!dispatchConfirmed) { onDispatch?.(); dispatchConfirmed = true; }
|
|
791
790
|
if (preserveFailureResponse) replacementAdmitted = true;
|
|
792
791
|
if (!refetchAllowance) chargeFastDowngradeWorkflowSend();
|
|
793
792
|
};
|
|
@@ -872,7 +871,7 @@ export async function prepareAdapterExchange(
|
|
|
872
871
|
// Same rule on the recovery leg: the ladder refused to send again, so the answer names
|
|
873
872
|
// this proxy rather than the provider it never reached.
|
|
874
873
|
if (err instanceof SendBudgetExhaustedError) {
|
|
875
|
-
return { failed: unboundPoolSpendRefusalResponse(logCtx)
|
|
874
|
+
return { failed: unboundPoolSpendRefusalResponse(logCtx) ?? workflowSendCeilingRefusalResponse(logCtx, sendBudgetState.workflowRootId)
|
|
876
875
|
?? markProxyError(formatErrorResponse(429, SEND_BUDGET_EXHAUSTED_CODE, err.message)) };
|
|
877
876
|
}
|
|
878
877
|
const msg = describeUpstreamConnectFailure(err, connectMs);
|
|
@@ -932,6 +931,7 @@ export async function prepareAdapterExchange(
|
|
|
932
931
|
if (wasAccountAutoPaused(upstreamResponse) && route.provider.authMode !== "forward") {
|
|
933
932
|
const recovered = await recoverAutoPausedAccount({ requestKey: transportState, reserve: reserveCredentialHop,
|
|
934
933
|
remaining: transientSendCapFor(transientRetryPolicyFor(route.provider)?.attempts, sendBudgetState.targetSendsUsed),
|
|
934
|
+
mapRefreshError: createAutoPauseRefreshErrorMapper(upstreamResponse, upstream, cleanupUpstreamAbort, copilotRefusalResponse, () => (options.abortSignal ?? req.signal).aborted, clientCancelledResponse),
|
|
935
935
|
refresh: () => transportState.recoverAutoPausedSelection(parsed), refetch: () => rebuildAndRefetch("oauth-account-429"), setPermit: permit => { sendBudgetState.pendingHopPermit = permit; } });
|
|
936
936
|
if (!recovered) break;
|
|
937
937
|
if ("failed" in recovered) return recovered.failed;
|
|
@@ -1014,7 +1014,7 @@ export async function prepareAdapterExchange(
|
|
|
1014
1014
|
route.provider, transportState.activeAdapter.name);
|
|
1015
1015
|
sendBudgetState.pendingHopPermit = hop.permit;
|
|
1016
1016
|
const result = await rebuildAndRefetch("oauth-account-429", () => {
|
|
1017
|
-
if (!adapterOwnsDispatch
|
|
1017
|
+
if (!adapterOwnsDispatch && hop.permit?.use() === false) throw new SendBudgetExhaustedError();
|
|
1018
1018
|
});
|
|
1019
1019
|
if ("failed" in result) return result.failed;
|
|
1020
1020
|
upstreamResponse = result;
|
|
@@ -1035,11 +1035,6 @@ export async function prepareAdapterExchange(
|
|
|
1035
1035
|
cleanupUpstreamAbort();
|
|
1036
1036
|
return markProxyError(formatErrorResponse(401, "authentication_error", publicOAuthAuthenticationErrorMessage(new Error("Cloud Code Assist project is required"))));
|
|
1037
1037
|
}
|
|
1038
|
-
transportState.sentOAuthSnapshot = refreshed;
|
|
1039
|
-
transportState.replayOAuthCredentialSnapshot = {
|
|
1040
|
-
accountId: refreshed.accountId,
|
|
1041
|
-
generation: refreshed.generation,
|
|
1042
|
-
};
|
|
1043
1038
|
if (route.providerName === "kiro") {
|
|
1044
1039
|
parsed._kiroAuthContext = { ...(refreshed.kiro ?? {}) };
|
|
1045
1040
|
}
|
|
@@ -1055,10 +1050,13 @@ export async function prepareAdapterExchange(
|
|
|
1055
1050
|
? resolveCopilotApiBaseUrl(refreshed.apiBaseUrl)
|
|
1056
1051
|
: undefined,
|
|
1057
1052
|
);
|
|
1058
|
-
|
|
1059
|
-
const copilotRefusal = await resolveRotatedCopilotSelection();
|
|
1053
|
+
const copilotRefusal = await resolveRotatedCopilotSelection(refreshedProvider);
|
|
1060
1054
|
if (copilotRefusal) return copilotRefusal;
|
|
1061
1055
|
refreshedProvider = route.provider;
|
|
1056
|
+
if (route.providerName !== "github-copilot") {
|
|
1057
|
+
transportState.sentOAuthSnapshot = refreshed;
|
|
1058
|
+
transportState.replayOAuthCredentialSnapshot = { accountId: refreshed.accountId, generation: refreshed.generation };
|
|
1059
|
+
}
|
|
1062
1060
|
invalidateSameTargetRequest();
|
|
1063
1061
|
transportState.activeAdapter = resolveSelectionAdapter(
|
|
1064
1062
|
resolveWireProtocolOverride(route.providerName, route.modelId, refreshedProvider, inboundWire, route.staticPolicy),
|
|
@@ -1136,7 +1134,7 @@ export async function prepareAdapterExchange(
|
|
|
1136
1134
|
});
|
|
1137
1135
|
if (billingHop) sendBudgetState.pendingHopPermit = billingHop.permit;
|
|
1138
1136
|
const result = await rebuildAndRefetch("key-401",
|
|
1139
|
-
billingHop ? () => { billingHop.permit?.use(); } : undefined, failedResponse);
|
|
1137
|
+
billingHop ? () => { if (billingHop.permit?.use() === false) throw new SendBudgetExhaustedError(); } : undefined, failedResponse);
|
|
1140
1138
|
if ("failed" in result) {
|
|
1141
1139
|
if (result.failed !== failedResponse) return result.failed;
|
|
1142
1140
|
break;
|
|
@@ -1174,7 +1172,7 @@ export async function prepareAdapterExchange(
|
|
|
1174
1172
|
resolveWireProtocolOverride(route.providerName, route.modelId, route.provider, inboundWire, route.staticPolicy), config.cacheRetention);
|
|
1175
1173
|
bindRouteReasoningReplayScope({ parsed, providerName: route.providerName, provider: route.provider, adapterName: transportState.activeAdapter.name });
|
|
1176
1174
|
sendBudgetState.pendingHopPermit = hop.permit;
|
|
1177
|
-
const result = await rebuildAndRefetch("key-401", () => { hop.permit?.use(); }, failedResponse);
|
|
1175
|
+
const result = await rebuildAndRefetch("key-401", () => { if (hop.permit?.use() === false) throw new SendBudgetExhaustedError(); }, failedResponse);
|
|
1178
1176
|
if ("failed" in result) {
|
|
1179
1177
|
if (result.failed !== failedResponse) return result.failed;
|
|
1180
1178
|
break;
|
|
@@ -1217,7 +1215,7 @@ export async function prepareAdapterExchange(
|
|
|
1217
1215
|
let result: Response | { failed: Response };
|
|
1218
1216
|
try {
|
|
1219
1217
|
result = await rebuildAndRefetch("anthropic-fast-downgrade", () => {
|
|
1220
|
-
if (!adapterOwnsDispatch
|
|
1218
|
+
if (!adapterOwnsDispatch && hop.permit?.use() === false) throw new SendBudgetExhaustedError();
|
|
1221
1219
|
});
|
|
1222
1220
|
} finally {
|
|
1223
1221
|
sendBudgetState.pendingHopPermit = undefined;
|
|
@@ -1337,7 +1335,7 @@ export async function prepareAdapterExchange(
|
|
|
1337
1335
|
});
|
|
1338
1336
|
if (billingHop) sendBudgetState.pendingHopPermit = billingHop.permit;
|
|
1339
1337
|
const result = await rebuildAndRefetch("key-429",
|
|
1340
|
-
billingHop ? () => { billingHop.permit?.use(); } : undefined, failedResponse);
|
|
1338
|
+
billingHop ? () => { if (billingHop.permit?.use() === false) throw new SendBudgetExhaustedError(); } : undefined, failedResponse);
|
|
1341
1339
|
if ("failed" in result) {
|
|
1342
1340
|
if (result.failed !== failedResponse) return result.failed;
|
|
1343
1341
|
break;
|
|
@@ -1416,7 +1414,7 @@ export async function prepareAdapterExchange(
|
|
|
1416
1414
|
try {
|
|
1417
1415
|
result = await rebuildAndRefetch(upstreamResponse.status === 401 ? "oauth-401"
|
|
1418
1416
|
: upstreamResponse.status === 403 ? "oauth-account-403" : "anthropic-oauth-429",
|
|
1419
|
-
() => { if (!adapterOwnsDispatch
|
|
1417
|
+
() => { if (!adapterOwnsDispatch && hop.permit?.use() === false) throw new SendBudgetExhaustedError(); });
|
|
1420
1418
|
} finally {
|
|
1421
1419
|
sendBudgetState.pendingHopPermit = undefined;
|
|
1422
1420
|
}
|
|
@@ -1508,7 +1506,7 @@ export async function prepareAdapterExchange(
|
|
|
1508
1506
|
adapterName: transportState.activeAdapter.name,
|
|
1509
1507
|
oauthCredentialSnapshot: transportState.replayOAuthCredentialSnapshot,
|
|
1510
1508
|
});
|
|
1511
|
-
sealRequestAttemptIdentity(logCtx.activeAttempt, logCtx.provider, transportState.activeAdapter.name, logCtx.accountLogLabel);
|
|
1509
|
+
if (!transportState.deferPhysicalAttribution) sealRequestAttemptIdentity(logCtx.activeAttempt, logCtx.provider, transportState.activeAdapter.name, logCtx.accountLogLabel);
|
|
1512
1510
|
recordAttemptCredentialSource(logCtx.activeAttempt, route.providerName, route.provider, transportState.activeAdapter.name);
|
|
1513
1511
|
// The replay IS this hop's send, so hand the reservation down and let the layer that
|
|
1514
1512
|
// dispatches settle it: `adapterDispatchBudget` spends it on the adapter's first
|
|
@@ -1523,7 +1521,7 @@ export async function prepareAdapterExchange(
|
|
|
1523
1521
|
// and settling here first would hand it a dead permit, which it reads as an
|
|
1524
1522
|
// exhausted request and stops sending on.
|
|
1525
1523
|
result = await rebuildAndRefetch("oauth-account-429", () => {
|
|
1526
|
-
if (!adapterOwnsDispatch
|
|
1524
|
+
if (!adapterOwnsDispatch && hop.permit?.use() === false) throw new SendBudgetExhaustedError();
|
|
1527
1525
|
}, upstreamResponse);
|
|
1528
1526
|
} finally {
|
|
1529
1527
|
sendBudgetState.pendingHopPermit = undefined;
|
|
@@ -1613,7 +1611,7 @@ export async function prepareAdapterExchange(
|
|
|
1613
1611
|
adapterName: transportState.activeAdapter.name,
|
|
1614
1612
|
oauthCredentialSnapshot: transportState.replayOAuthCredentialSnapshot,
|
|
1615
1613
|
});
|
|
1616
|
-
sealRequestAttemptIdentity(logCtx.activeAttempt, logCtx.provider, transportState.activeAdapter.name, logCtx.accountLogLabel);
|
|
1614
|
+
if (!transportState.deferPhysicalAttribution) sealRequestAttemptIdentity(logCtx.activeAttempt, logCtx.provider, transportState.activeAdapter.name, logCtx.accountLogLabel);
|
|
1617
1615
|
recordAttemptCredentialSource(logCtx.activeAttempt, route.providerName, route.provider, transportState.activeAdapter.name);
|
|
1618
1616
|
// The replay IS this hop's send, so hand the reservation down and let the layer that
|
|
1619
1617
|
// dispatches settle it: `adapterDispatchBudget` spends it on the adapter's first
|
|
@@ -1628,7 +1626,7 @@ export async function prepareAdapterExchange(
|
|
|
1628
1626
|
// and settling here first would hand it a dead permit, which it reads as an
|
|
1629
1627
|
// exhausted request and stops sending on.
|
|
1630
1628
|
result = await rebuildAndRefetch("oauth-account-429", () => {
|
|
1631
|
-
if (!adapterOwnsDispatch
|
|
1629
|
+
if (!adapterOwnsDispatch && hop.permit?.use() === false) throw new SendBudgetExhaustedError();
|
|
1632
1630
|
});
|
|
1633
1631
|
} finally {
|
|
1634
1632
|
sendBudgetState.pendingHopPermit = undefined;
|
|
@@ -1740,7 +1738,7 @@ export async function prepareAdapterExchange(
|
|
|
1740
1738
|
let result: Response | { failed: Response };
|
|
1741
1739
|
try {
|
|
1742
1740
|
result = await rebuildAndRefetch("oauth-account-403", () => {
|
|
1743
|
-
if (!adapterOwnsDispatch
|
|
1741
|
+
if (!adapterOwnsDispatch && hop.permit?.use() === false) throw new SendBudgetExhaustedError();
|
|
1744
1742
|
}, failedResponse);
|
|
1745
1743
|
} finally {
|
|
1746
1744
|
sendBudgetState.pendingHopPermit = undefined;
|
|
@@ -1870,7 +1868,7 @@ export async function prepareAdapterExchange(
|
|
|
1870
1868
|
// `response.body` itself with the abort signal threaded through, and the combo
|
|
1871
1869
|
// contract is that this body's getter is touched exactly once. A guard here would be
|
|
1872
1870
|
// a second `.body` access for no gain, since the bounded reader owns settlement.
|
|
1873
|
-
const failure = await consumeComboFailure(upstreamResponse, options.abortSignal)
|
|
1871
|
+
const failure = await consumeComboFailure(upstreamResponse, options.abortSignal, Date.now(), logCtx)
|
|
1874
1872
|
.finally(cleanupUpstreamAbort);
|
|
1875
1873
|
options.onConsumedComboFailure?.(failure);
|
|
1876
1874
|
return failure.response;
|
|
@@ -1885,6 +1883,7 @@ export async function prepareAdapterExchange(
|
|
|
1885
1883
|
} finally {
|
|
1886
1884
|
cleanupUpstreamAbort();
|
|
1887
1885
|
}
|
|
1886
|
+
recordUpstreamValidationDetail(logCtx, upstreamResponse.status, errorText);
|
|
1888
1887
|
if (upstreamResponse.status === 413) {
|
|
1889
1888
|
if (options.clientModelLabel) projectLocalModelRefusalLog(logCtx, options.clientModelLabel);
|
|
1890
1889
|
return clientRequestedStream
|
|
@@ -1962,7 +1961,7 @@ export async function prepareAdapterExchange(
|
|
|
1962
1961
|
upstreamRetryAfter,
|
|
1963
1962
|
includeDefault: !(transportState.anthropicPoolAccountId && classifyAnthropic429(upstreamResponse.headers) === "request-scoped-unknown"),
|
|
1964
1963
|
});
|
|
1965
|
-
return formatUpstreamFailureResponse(upstreamResponse, normalized.code,
|
|
1964
|
+
return carryPolicyCandidateRefusal(upstreamResponse, formatUpstreamFailureResponse(upstreamResponse, normalized.code,
|
|
1966
1965
|
upstreamResponse.status,
|
|
1967
1966
|
normalized.cyberPolicy ? (normalized.type ?? CYBER_POLICY_ERROR_CODE) : "upstream_error",
|
|
1968
1967
|
message,
|
|
@@ -1970,7 +1969,7 @@ export async function prepareAdapterExchange(
|
|
|
1970
1969
|
...(normalized.cyberPolicy ? { code: CYBER_POLICY_ERROR_CODE } : {}),
|
|
1971
1970
|
...(retryAfter !== undefined ? { retryAfter } : {}),
|
|
1972
1971
|
},
|
|
1973
|
-
);
|
|
1972
|
+
));
|
|
1974
1973
|
}
|
|
1975
1974
|
}
|
|
1976
1975
|
|
|
@@ -2,7 +2,7 @@ import { getConfigDir } from "../../config/paths";
|
|
|
2
2
|
import type { OAuthAccessSnapshot } from "../../oauth";
|
|
3
3
|
import { failoverAccountSnapshot, rotateAntigravityAccountOnModelNotFound } from "../../oauth/generic-account-failover";
|
|
4
4
|
import { captureAntigravityModelObservation, recordAntigravityModelDenial } from "../../providers/antigravity-account-roster";
|
|
5
|
-
import { isNonReplayableResponse } from "../../lib/upstream-retry";
|
|
5
|
+
import { isNonReplayableResponse, SendBudgetExhaustedError } from "../../lib/upstream-retry";
|
|
6
6
|
import type { OcxConfig } from "../../types";
|
|
7
7
|
import type { ResponsesSendBudget } from "./request-send-budget";
|
|
8
8
|
import { isAntigravityModelNotFoundRefusal } from "./antigravity-validation-refusal";
|
|
@@ -25,7 +25,9 @@ export async function recoverAntigravityAccount(options: {
|
|
|
25
25
|
if ((await options.applyFailoverSnapshot(snapshot))?.accountId !== options.nextId) return null;
|
|
26
26
|
options.prepare();
|
|
27
27
|
options.setPermit(hop.permit);
|
|
28
|
-
const result = await options.refetch(() => {
|
|
28
|
+
const result = await options.refetch(() => {
|
|
29
|
+
if (!options.adapterOwnsDispatch && hop.permit?.use() === false) throw new SendBudgetExhaustedError();
|
|
30
|
+
});
|
|
29
31
|
if ("failed" in result) return result.failed === options.failedResponse ? null : result.failed;
|
|
30
32
|
options.complete();
|
|
31
33
|
try { void options.failedResponse.body?.cancel().catch(() => {}); } catch { /* already closed */ }
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { NativeSteeringError } from "./native-steering";
|
|
2
|
+
import { NativeContinuationRefusalError } from "./native-continuation-refusal";
|
|
2
3
|
import { mergeSteeringContinuation } from "./native-steering-settings";
|
|
3
4
|
import { markNativeControlResponse } from "./native-response-control";
|
|
4
5
|
import type { NativeResponseControl } from "./native-response-control";
|
|
@@ -27,6 +28,7 @@ interface ExchangeOptions {
|
|
|
27
28
|
onQuota?: CodexWsQuotaObserver;
|
|
28
29
|
beforeDispatch?: (headers: Headers) => void;
|
|
29
30
|
onPhysicalDispatch?: () => void;
|
|
31
|
+
onContinuationDispatch?: (frame: { body: Record<string, unknown>; text: string }) => void;
|
|
30
32
|
/** Bun version string the caller gated on; stamped onto the stage record. */
|
|
31
33
|
bunVersion?: string;
|
|
32
34
|
/**
|
|
@@ -527,21 +529,28 @@ export function codexWsExchange(options: ExchangeOptions): Promise<Response> {
|
|
|
527
529
|
throw new Error("Native steering connection is no longer available");
|
|
528
530
|
}
|
|
529
531
|
beforeDispatch?.(new Headers(headers));
|
|
530
|
-
if (frame.type === "response.create")
|
|
532
|
+
if (frame.type === "response.create") {
|
|
533
|
+
// The exact serialized successor and its body, so admission can price the
|
|
534
|
+
// reconstructed input and the successor's own output ceiling — never the
|
|
535
|
+
// opening request's stale estimate.
|
|
536
|
+
options.onContinuationDispatch?.({ body: prepared.outgoing, text: prepared.text });
|
|
537
|
+
continuationBase = prepared.outgoing;
|
|
538
|
+
}
|
|
531
539
|
try { ws.send(prepared.text); } catch {
|
|
532
540
|
// A send failure has unknown delivery. Never replay or fall back.
|
|
533
541
|
failStream("Native steering send failed; delivery is unknown");
|
|
534
542
|
throw new Error("Native steering send failed; delivery is unknown");
|
|
535
543
|
}
|
|
536
544
|
};
|
|
537
|
-
if (frame.type === "response.create"
|
|
545
|
+
if (frame.type === "response.create") {
|
|
538
546
|
// Explicit tool-result continuations are physical request starts;
|
|
539
547
|
// they keep provider pacing and revalidate auth AFTER the wait.
|
|
540
548
|
// A typed pre-send refusal (e.g. the configured upstream body limit) is a
|
|
541
549
|
// known non-delivery and keeps its own message; every other failure keeps
|
|
542
550
|
// the unknown-delivery wording.
|
|
543
|
-
|
|
544
|
-
|
|
551
|
+
// Keep known refusals on this stream even when no pacing hook is installed.
|
|
552
|
+
void Promise.resolve().then(beforeContinuation).then(sendControl).catch(error => failStream(
|
|
553
|
+
error instanceof NativeSteeringError || error instanceof NativeContinuationRefusalError ? error
|
|
545
554
|
: new Error("Native steering continuation could not be dispatched; do not automatically replay queued input"),
|
|
546
555
|
));
|
|
547
556
|
} else sendControl();
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { REQUIRED_TOOL_CONTRACT_CODE } from "../../lib/errors";
|
|
2
2
|
import { mapToolChoice } from "../../responses/parser-tools";
|
|
3
|
-
import {
|
|
3
|
+
import { extractHostedWebSearch } from "../../web-search/synthetic-tool";
|
|
4
|
+
import { modelInList, type OcxProviderConfig, type OcxTool } from "../../types";
|
|
4
5
|
import { hasResponseMediaType } from "../../lib/http-response-semantics";
|
|
5
6
|
import { applyReplayRefusalClientHeaders, isNonReplayableResponse, markResponseNonReplayable } from "../../lib/upstream-retry";
|
|
6
7
|
import { clientWireOf } from "../inference/client-wire";
|
|
@@ -42,10 +43,28 @@ export function hasMandatoryToolChoice(rawBody: unknown): boolean {
|
|
|
42
43
|
}
|
|
43
44
|
|
|
44
45
|
/** A translated Chat target must not turn a mandatory selector into auto or none. */
|
|
45
|
-
export function chatCanPreserveMandatoryToolChoice(rawBody: unknown, provider: OcxProviderConfig, modelId: string): boolean {
|
|
46
|
+
export function chatCanPreserveMandatoryToolChoice(rawBody: unknown, provider: OcxProviderConfig, modelId: string, effectiveTools?: readonly OcxTool[]): boolean {
|
|
46
47
|
if (!hasMandatoryToolChoice(rawBody)) return true;
|
|
47
48
|
if (modelInList(provider.autoToolChoiceOnlyModels, modelId)) return false;
|
|
48
|
-
const
|
|
49
|
+
const body = rawBody as Record<string, unknown>;
|
|
50
|
+
const choice = body.tool_choice;
|
|
51
|
+
const tools = body.tools;
|
|
52
|
+
const hasDeclaredTools = Array.isArray(tools) && tools.length > 0
|
|
53
|
+
|| Array.isArray(body.input) && body.input.some(item => item && typeof item === "object"
|
|
54
|
+
&& (item.type === "additional_tools" || item.type === "tool_search_output")
|
|
55
|
+
&& Array.isArray(item.tools) && item.tools.length > 0);
|
|
56
|
+
// The parsed catalog includes additional_tools and tool_search_output. A static
|
|
57
|
+
// candidate check without it defers this decision to final request preparation.
|
|
58
|
+
// Hosted search belongs to the later sidecar injection owner.
|
|
59
|
+
if (choice === "required" && hasDeclaredTools && effectiveTools !== undefined && effectiveTools.length === 0
|
|
60
|
+
&& !extractHostedWebSearch(Array.isArray(tools) ? tools : undefined)) return false;
|
|
61
|
+
if (choice && typeof choice === "object" && !Array.isArray(choice)) {
|
|
62
|
+
const selector = choice as Record<string, unknown>;
|
|
63
|
+
// Chat function names cannot preserve the selected MCP server's ownership.
|
|
64
|
+
if (selector.type === "allowed_tools" && Array.isArray(selector.tools)
|
|
65
|
+
&& selector.tools.some(tool => tool && typeof tool === "object" && tool.type === "mcp")) return false;
|
|
66
|
+
}
|
|
67
|
+
const mapped = mapToolChoice(choice);
|
|
49
68
|
return mapped === "required" || (!!mapped && typeof mapped === "object");
|
|
50
69
|
}
|
|
51
70
|
|