dsh-lcx-codex 0.7.0-pre.1 → 0.7.0
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/README.md +62 -165
- package/THIRD_PARTY_NOTICES.md +5 -2
- package/lib/client.js +75 -3
- package/lib/compact-v2.js +8 -2
- package/lib/defaults.js +1 -0
- package/lib/dsh-responses.js +250 -35
- package/lib/grok-native-search.js +18 -1
- package/lib/index.js +692 -68
- package/lib/model-catalog.js +16 -3
- package/lib/native-checkpoint.js +13 -1
- package/lib/pi-responses-runtime.js +151 -21
- package/lib/responses-request.js +49 -15
- package/lib/responses-stream.js +26 -6
- package/lib/route.js +325 -38
- package/lib/tool-surface.js +41 -8
- package/lib/types/index.d.ts +8 -0
- package/package.json +36 -36
package/lib/index.js
CHANGED
|
@@ -10,17 +10,17 @@ import { randomUUID } from "node:crypto";
|
|
|
10
10
|
import { homedir } from "node:os";
|
|
11
11
|
import { join } from "node:path";
|
|
12
12
|
import { AsyncLocalStorage } from "node:async_hooks";
|
|
13
|
-
import { getSupportedThinkingLevels } from "./pi-responses-runtime.js";
|
|
13
|
+
import { clampMaxTokensToContext, clampOpenAIPromptCacheKey, getSupportedThinkingLevels, normalizeContext, } from "./pi-responses-runtime.js";
|
|
14
14
|
import { fetchJsonWithRetry } from "./transport.js";
|
|
15
15
|
import { DEFAULT_TOOL_UPDATES, planToolSurface } from "./tool-surface.js";
|
|
16
16
|
import { agentSessionId, agentUsesSession, compactionConfigState, compactionPatchCandidate, installCompactionPatch, patchCompactionConfig, patchToolResultPruner, readAgentRouteState, readSelectedAgentRouteState, recordAgentRouteEvent, resolveAgentService, resolveContextService, resolveScopedService, restoreCompactionConfig, restoreCompactionPatches, restoreToolResultPruner, sessionFor, sessionFromAgent, scopedToolRuntime, onScopedToolExecute, tokenMeterTotal, toolResultPrunerState, } from "./dsh-compat.js";
|
|
17
17
|
import { InvocationPolicyScope } from "./invocation-policy-scope.js";
|
|
18
18
|
import { providerDynamicHeaders } from "./provider-headers.js";
|
|
19
19
|
import { resolveCatalogModel } from "./model-catalog.js";
|
|
20
|
-
import {
|
|
21
|
-
import { DEFAULT_MAX_REQUEST_IMAGE_BYTES, DEFAULT_REQUEST_IMAGE_PIXEL_BUDGET, DEFAULT_REQUEST_IMAGE_MAX_BYTES, assertAggregateImageBudget, hydrateNativeImageReferences, inputImageBase64Lengths, resolveRouteModelFacts, routeModelOverride, hasMidHistoryPromptSnapshot, responseInputItems, serializeDshMessages, splitSystemPrompt, } from "./dsh-responses.js";
|
|
20
|
+
import { authenticatedGrokRequestHeaders, authenticatedHeaders, authenticatedRequestHeaders, baseURLFingerprint, currentRoute, diagnoseGrokContextBudget, diagnoseGrokContextBudgetAsync, grokBudgetInputSignature, grokRouteMayNeedHostBudgetLookup, generationControlsFromSession, grokPromptCacheSessionId, isGrokModelId, promptCacheKey, promptCacheRetention, promptCacheSessionId, resolveGrokResponsesRouteConfig, resolveRouteHeaderAuthorityFingerprint, diagnoseResponsesRoute, markHostRecognizedModel, warmHostRecognizedModels, resolveApiKey, resolveResponsesRouteConfig, routeFingerprint, } from "./route.js";
|
|
21
|
+
import { DEFAULT_MAX_REQUEST_IMAGE_BYTES, DEFAULT_REQUEST_IMAGE_PIXEL_BUDGET, DEFAULT_REQUEST_IMAGE_MAX_BYTES, assertAggregateImageBudget, hydrateNativeImageReferences, inputImageBase64Lengths, resolveRouteModelFacts, resolvePiResponsesModel, routeModelOverride, hasMidHistoryPromptSnapshot, responseInputItems, serializeDshMessages, splitSystemPrompt, } from "./dsh-responses.js";
|
|
22
22
|
import { mergeFeatureHeader, requestNativeCompaction } from "./compact-v2.js";
|
|
23
|
-
import { buildResponsesBody, configurationUpdateEffort } from "./responses-request.js";
|
|
23
|
+
import { buildResponsesBody, configurationUpdateEffort, isChatGPTSignInCredential } from "./responses-request.js";
|
|
24
24
|
import { managedFailureChunk, streamResponsesRequest, } from "./responses-stream.js";
|
|
25
25
|
import { assertSupportedCheckpointMessage, checkpointStateForMessage, compactCheckpointId, createNativeCheckpointBlock, nativeCheckpointChunks, portableMessagesForCheckpoint, stateRouteCompatible, } from "./native-checkpoint.js";
|
|
26
26
|
import { HOSTED_SEARCH_OUTPUT, HOSTED_SEARCH_PARAMETERS, buildHostedSearchBody, hostedMediaPresentationMeta, normalizeHostedSearchArgs, parseHostedSearchResponse, renderHostedSearchResult, } from "./web-search-hosted.js";
|
|
@@ -29,7 +29,7 @@ import { recentAlphaInput } from "./web-search-alpha-history.js";
|
|
|
29
29
|
import { AlphaCapabilityStore, alphaCapabilityFingerprint, alphaCapabilityUsable, alphaSearchParametersFor, assertAlphaActionAllowed, } from "./web-search-capability.js";
|
|
30
30
|
import { AlphaRefStore } from "./web-search-ref-store.js";
|
|
31
31
|
import { ServiceMutex } from "./service-mutex.js";
|
|
32
|
-
import { GROK_NATIVE_SERVER_TOOL_TYPES, grokNativeSearchEnabled, grokVisibleFunctionTools, isGrokNativeServerToolItem, grokWireTools, restoreGrokNativeReplay, } from "./grok-native-search.js";
|
|
32
|
+
import { GROK_NATIVE_SERVER_TOOL_TYPES, grokNativeReplayRouteFingerprint, grokNativeSearchEnabled, grokVisibleFunctionTools, isGrokNativeServerToolItem, grokWireTools, restoreGrokNativeReplay, } from "./grok-native-search.js";
|
|
33
33
|
import { DEFAULTS } from "./defaults.js";
|
|
34
34
|
export const name = "lcx-codex";
|
|
35
35
|
export const inject = ["llm", "web", "sessions", "tools", "settings", "credentials", "attachments", "fs"];
|
|
@@ -57,12 +57,16 @@ function routeWithPolicies(route, config) {
|
|
|
57
57
|
alphaMaxOutputTokens: config.alphaMaxOutputTokens,
|
|
58
58
|
assistantRetentionPerMessageTokenCap: config.assistantRetentionPerMessageTokenCap,
|
|
59
59
|
assistantRetentionTokenReserve: config.assistantRetentionTokenReserve,
|
|
60
|
+
gatewayCompatibilityFallback: config.gatewayCompatibilityFallback,
|
|
60
61
|
maxRequestImageBytes: route.maxRequestImageBytes ?? config.maxRequestImageBytes,
|
|
61
62
|
maxResponseBytes: config.maxResponseBytes,
|
|
62
63
|
nativeRetentionTokenBudget: config.nativeRetentionTokenBudget,
|
|
63
64
|
portableReplayMaxChars: config.portableReplayMaxChars,
|
|
64
65
|
requestImageMaxBytes: route.requestImageMaxBytes ?? config.requestImageMaxBytes,
|
|
65
66
|
requestImagePixelBudget: route.requestImagePixelBudget ?? config.requestImagePixelBudget,
|
|
67
|
+
responsesCompat: config.promptCacheKeyMode === "disabled" || config.promptCacheKeyMode === "enabled"
|
|
68
|
+
? { ...(route.responsesCompat ?? {}), promptCacheKeyMode: config.promptCacheKeyMode }
|
|
69
|
+
: route.responsesCompat,
|
|
66
70
|
toolUpdates: config.toolUpdates,
|
|
67
71
|
webMaxResults: config.webMaxResults,
|
|
68
72
|
};
|
|
@@ -105,8 +109,10 @@ export const Config = z.object({
|
|
|
105
109
|
grokNativeXSearch: z.boolean().default(false).volatile(),
|
|
106
110
|
searchMediaPreview: z.boolean().default(false).volatile(),
|
|
107
111
|
incrementalToolUpdates: z.boolean().default(true).volatile(),
|
|
112
|
+
gatewayCompatibilityFallback: z.boolean().default(DEFAULTS.gatewayCompatibilityFallback).volatile(),
|
|
108
113
|
supportsLongCacheRetention: z.boolean().default(false),
|
|
109
114
|
supportsExplicitPromptCacheMode: z.boolean().default(false),
|
|
115
|
+
promptCacheKeyMode: z.union(["auto", "disabled", "enabled"]).default("auto"),
|
|
110
116
|
alphaCapabilityPath: z.string().default(""),
|
|
111
117
|
alphaRefPath: z.string().default(""),
|
|
112
118
|
alphaProfile: z.string().default(""),
|
|
@@ -131,6 +137,12 @@ function normalizeConfig(input = {}) {
|
|
|
131
137
|
return {
|
|
132
138
|
supportsLongCacheRetention: input.supportsLongCacheRetention === true,
|
|
133
139
|
supportsExplicitPromptCacheMode: input.supportsExplicitPromptCacheMode === true,
|
|
140
|
+
promptCacheKeyMode: input.promptCacheKeyMode === "disabled" || input.promptCacheKeyMode === "enabled"
|
|
141
|
+
? input.promptCacheKeyMode
|
|
142
|
+
: "auto",
|
|
143
|
+
gatewayCompatibilityFallback: input.gatewayCompatibilityFallback === true ||
|
|
144
|
+
(input.gatewayCompatibilityFallback === undefined &&
|
|
145
|
+
DEFAULTS.gatewayCompatibilityFallback),
|
|
134
146
|
alphaCapabilityPath: stringValue(input.alphaCapabilityPath, defaultAlphaCapabilityPath()),
|
|
135
147
|
alphaRefPath: stringValue(input.alphaRefPath, defaultAlphaRefPath()),
|
|
136
148
|
alphaProfile: String(input.alphaProfile ?? ""),
|
|
@@ -201,9 +213,11 @@ async function executeHostedSearch(ctx, routeConfig, args, signal, sessionId = "
|
|
|
201
213
|
const normalized = normalizeHostedSearchArgs(args);
|
|
202
214
|
const requestId = randomUUID();
|
|
203
215
|
const route = currentRoute({ provider: routeConfig.provider, model: routeConfig.model, sessionId }, routeConfig);
|
|
204
|
-
const
|
|
205
|
-
const
|
|
206
|
-
|
|
216
|
+
const cacheSessionId = promptCacheSessionId(route, routeConfig, ctx);
|
|
217
|
+
const headers = await authenticatedHeaders(ctx, routeConfig, cacheSessionId, requestId, {}, sessionId || undefined);
|
|
218
|
+
const searchCacheKey = cacheSessionId &&
|
|
219
|
+
routeConfig.responsesCompat?.promptCacheKeyMode !== "disabled"
|
|
220
|
+
? clampOpenAIPromptCacheKey(`dsh-lcx-search:${routeFingerprint(route)}`)
|
|
207
221
|
: undefined;
|
|
208
222
|
const body = buildHostedSearchBody(normalized, routeConfig.model, {
|
|
209
223
|
promptCacheKey: searchCacheKey,
|
|
@@ -581,14 +595,134 @@ function locateSupersededItems(original, restored, positions) {
|
|
|
581
595
|
}
|
|
582
596
|
return located;
|
|
583
597
|
}
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
598
|
+
function resolveEffectiveReasoningEffort(requestedEffort, facts, routeConfig) {
|
|
599
|
+
return (requestedEffort ??
|
|
600
|
+
routeConfig.profileReasoning ??
|
|
601
|
+
(facts.resolved ? facts.reasoning?.defaultEffort : undefined));
|
|
602
|
+
}
|
|
603
|
+
function assertValidPositiveMaxTokens(value) {
|
|
604
|
+
if (value !== undefined && (!Number.isSafeInteger(value) || Number(value) <= 0)) {
|
|
605
|
+
throw Object.assign(new Error("Responses maxTokens must be a positive safe integer"), { code: "LCX_RESPONSES_INVALID_INPUT" });
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
function assertValidTemperature(value) {
|
|
609
|
+
if (value !== undefined && (typeof value !== "number" || !Number.isFinite(value))) {
|
|
610
|
+
throw Object.assign(new Error("Responses temperature must be finite"), { code: "LCX_RESPONSES_INVALID_INPUT" });
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
/** Resolve request maxTokens (caller -> DSH defaultMaxTokens -> Pi model.maxTokens) clamped to context room. */
|
|
614
|
+
function resolveEffectiveMaxTokens(requestedMaxTokens, facts, model, piContext) {
|
|
615
|
+
assertValidPositiveMaxTokens(requestedMaxTokens);
|
|
616
|
+
const candidate = requestedMaxTokens ??
|
|
617
|
+
(facts.resolved ? facts.defaultMaxTokens : undefined) ??
|
|
618
|
+
(typeof model.maxTokens === "number" && Number.isFinite(model.maxTokens) && model.maxTokens > 0
|
|
619
|
+
? model.maxTokens
|
|
620
|
+
: undefined);
|
|
621
|
+
if (candidate === undefined)
|
|
622
|
+
return undefined;
|
|
623
|
+
return clampMaxTokensToContext(model, normalizeContext(piContext), candidate);
|
|
624
|
+
}
|
|
625
|
+
function estimateContentArrayToPiParts(parts) {
|
|
626
|
+
const result = [];
|
|
627
|
+
for (const part of parts) {
|
|
628
|
+
if (!isRecord(part)) {
|
|
629
|
+
const text = JSON.stringify(part);
|
|
630
|
+
if (text)
|
|
631
|
+
result.push({ type: "text", text });
|
|
632
|
+
continue;
|
|
633
|
+
}
|
|
634
|
+
if (part.type === "input_image") {
|
|
635
|
+
result.push({ type: "image", data: "", mimeType: "image/png" });
|
|
636
|
+
continue;
|
|
637
|
+
}
|
|
638
|
+
if ((part.type === "input_text" || part.type === "output_text" || part.type === "text") &&
|
|
639
|
+
typeof part.text === "string") {
|
|
640
|
+
if (part.text)
|
|
641
|
+
result.push({ type: "text", text: part.text });
|
|
642
|
+
continue;
|
|
643
|
+
}
|
|
644
|
+
const text = JSON.stringify(part);
|
|
645
|
+
if (text)
|
|
646
|
+
result.push({ type: "text", text });
|
|
647
|
+
}
|
|
648
|
+
return result;
|
|
649
|
+
}
|
|
650
|
+
function extractVisibleReasoningText(parts) {
|
|
651
|
+
if (!Array.isArray(parts))
|
|
652
|
+
return [];
|
|
653
|
+
const texts = [];
|
|
654
|
+
for (const part of parts) {
|
|
655
|
+
if (typeof part === "string" && part)
|
|
656
|
+
texts.push(part);
|
|
657
|
+
else if (isRecord(part) && typeof part.text === "string" && part.text)
|
|
658
|
+
texts.push(part.text);
|
|
659
|
+
}
|
|
660
|
+
return texts;
|
|
661
|
+
}
|
|
662
|
+
function estimateItemsToPiMessages(items) {
|
|
663
|
+
const projected = [];
|
|
664
|
+
for (const item of items) {
|
|
665
|
+
if (!isRecord(item) || item.type === "compaction")
|
|
666
|
+
continue;
|
|
667
|
+
if (item.type === "reasoning") {
|
|
668
|
+
// Never count opaque encrypted_content ciphertext toward visible prompt-token budgets.
|
|
669
|
+
const visibleText = [
|
|
670
|
+
...(typeof item.text === "string" && item.text ? [item.text] : []),
|
|
671
|
+
...extractVisibleReasoningText(item.summary),
|
|
672
|
+
...extractVisibleReasoningText(item.content),
|
|
673
|
+
].join("\n\n");
|
|
674
|
+
if (visibleText)
|
|
675
|
+
projected.push({ role: "user", content: [{ type: "text", text: visibleText }], timestamp: 0 });
|
|
676
|
+
continue;
|
|
677
|
+
}
|
|
678
|
+
if (Array.isArray(item.content)) {
|
|
679
|
+
const content = estimateContentArrayToPiParts(item.content);
|
|
680
|
+
if (content.length > 0)
|
|
681
|
+
projected.push({ role: "user", content, timestamp: 0 });
|
|
682
|
+
continue;
|
|
683
|
+
}
|
|
684
|
+
if (Array.isArray(item.output)) {
|
|
685
|
+
const content = estimateContentArrayToPiParts(item.output);
|
|
686
|
+
if (content.length > 0)
|
|
687
|
+
projected.push({ role: "user", content, timestamp: 0 });
|
|
688
|
+
continue;
|
|
689
|
+
}
|
|
690
|
+
const text = typeof item.content === "string"
|
|
691
|
+
? item.content
|
|
692
|
+
: typeof item.arguments === "string"
|
|
693
|
+
? `${String(item.name ?? "")}${item.arguments}`
|
|
694
|
+
: typeof item.output === "string"
|
|
695
|
+
? item.output
|
|
696
|
+
: JSON.stringify(item);
|
|
697
|
+
if (text)
|
|
698
|
+
projected.push({ role: "user", content: [{ type: "text", text }], timestamp: 0 });
|
|
699
|
+
}
|
|
700
|
+
return projected;
|
|
701
|
+
}
|
|
702
|
+
/** Refuse an effort unsupported by either DSH facts or the resolved Pi model descriptor. */
|
|
703
|
+
function assertSupportedReasoningEffort(effort, facts, model, routeConfig) {
|
|
704
|
+
if (effort === undefined)
|
|
590
705
|
return;
|
|
591
|
-
|
|
706
|
+
const requested = String(effort);
|
|
707
|
+
let dshAllowsOff = false;
|
|
708
|
+
if (facts.resolved) {
|
|
709
|
+
const supported = facts.reasoning === undefined ? ["off"] : facts.reasoning.efforts;
|
|
710
|
+
dshAllowsOff = supported.includes("off");
|
|
711
|
+
if (!supported.includes(requested)) {
|
|
712
|
+
throw Object.assign(new Error(`Responses route does not support reasoning effort "${requested}"`), { code: "LCX_RESPONSES_UNSUPPORTED_OPTION" });
|
|
713
|
+
}
|
|
714
|
+
}
|
|
715
|
+
if (model !== undefined) {
|
|
716
|
+
const omitOffExemption = requested === "off" &&
|
|
717
|
+
dshAllowsOff &&
|
|
718
|
+
routeConfig?.modelControls?.offOmitsReasoning === true &&
|
|
719
|
+
model.thinkingLevelMap?.off === null;
|
|
720
|
+
const explicitNullMapping = model.thinkingLevelMap?.[requested] === null;
|
|
721
|
+
if ((explicitNullMapping && !omitOffExemption) ||
|
|
722
|
+
(!omitOffExemption && !getSupportedThinkingLevels(model).some((level) => level === requested))) {
|
|
723
|
+
throw Object.assign(new Error(`Responses route does not support reasoning effort "${requested}"`), { code: "LCX_RESPONSES_UNSUPPORTED_OPTION" });
|
|
724
|
+
}
|
|
725
|
+
}
|
|
592
726
|
}
|
|
593
727
|
const reportedToolSurfaceFallbacks = new Set();
|
|
594
728
|
/**
|
|
@@ -627,12 +761,56 @@ function reportCatalogGap(ctx, route) {
|
|
|
627
761
|
reportedCatalogGaps.add(key);
|
|
628
762
|
ctx.logger?.debug?.(`[lcx-codex] ${key} is not described by LCX's bundled model catalog; its wire capabilities follow the profile and what DSH declares for the model`);
|
|
629
763
|
}
|
|
764
|
+
async function resolveAssistantReplayScope(ctx, session, route, grokNativeReplayRoute, headerAuthorityFingerprint) {
|
|
765
|
+
const inheritedMessageIds = new Set();
|
|
766
|
+
let disallowUnstampedReplay = false;
|
|
767
|
+
if (session && (session.header?.isSeeded === true || session.header?.parentSession !== undefined)) {
|
|
768
|
+
const query = resolveContextService(ctx, "sessionQuery");
|
|
769
|
+
if (typeof query?.observeSession !== "function" || typeof session.isOwnSeq !== "function") {
|
|
770
|
+
disallowUnstampedReplay = true;
|
|
771
|
+
}
|
|
772
|
+
else {
|
|
773
|
+
let lease;
|
|
774
|
+
try {
|
|
775
|
+
lease = await query.observeSession(session.id, { projectionMode: "none" });
|
|
776
|
+
for (const event of lease.events) {
|
|
777
|
+
if (event.type === "assistant/message" && !session.isOwnSeq(event.seq)) {
|
|
778
|
+
const msgId = event.data?.message?.id;
|
|
779
|
+
if (typeof msgId === "string" && msgId.length > 0)
|
|
780
|
+
inheritedMessageIds.add(msgId);
|
|
781
|
+
}
|
|
782
|
+
}
|
|
783
|
+
}
|
|
784
|
+
catch {
|
|
785
|
+
disallowUnstampedReplay = true;
|
|
786
|
+
}
|
|
787
|
+
finally {
|
|
788
|
+
lease?.[Symbol.dispose]?.();
|
|
789
|
+
}
|
|
790
|
+
}
|
|
791
|
+
}
|
|
792
|
+
const effectiveHeaderAuthorityFingerprint = headerAuthorityFingerprint ?? route.headerAuthorityFingerprint;
|
|
793
|
+
return {
|
|
794
|
+
...(route.provider ? { provider: route.provider } : {}),
|
|
795
|
+
...(route.model ? { model: route.model } : {}),
|
|
796
|
+
...(route.sessionId ? { sessionId: route.sessionId } : {}),
|
|
797
|
+
...(route.baseURL ? { baseURLFingerprint: baseURLFingerprint(route.baseURL) } : {}),
|
|
798
|
+
...(effectiveHeaderAuthorityFingerprint
|
|
799
|
+
? { headerAuthorityFingerprint: effectiveHeaderAuthorityFingerprint }
|
|
800
|
+
: {}),
|
|
801
|
+
...(grokNativeReplayRoute
|
|
802
|
+
? { grokRouteAuthorityFingerprint: grokNativeReplayRouteFingerprint(grokNativeReplayRoute) }
|
|
803
|
+
: {}),
|
|
804
|
+
...(inheritedMessageIds.size > 0 ? { inheritedMessageIds } : {}),
|
|
805
|
+
...(disallowUnstampedReplay ? { disallowUnstampedReplay: true } : {}),
|
|
806
|
+
};
|
|
807
|
+
}
|
|
630
808
|
async function serializeNativeAware(rawMessages, route, routeConfig, ctx, options) {
|
|
631
809
|
const session = sessionFor(ctx, route.sessionId);
|
|
632
810
|
// One adapter-owned lookup supplies image support and the route's system-prompt update mode. On an
|
|
633
811
|
// "in-history" route DSH appends a changed prompt as a later full system message; it must reach the
|
|
634
812
|
// model in place with instruction authority instead of being demoted to user context.
|
|
635
|
-
const modelFacts = await resolveRouteModelFacts(ctx, route, options.signal);
|
|
813
|
+
const modelFacts = options.prefetchedModelFacts ?? (await resolveRouteModelFacts(ctx, route, options.signal));
|
|
636
814
|
if (options.grokNativeReplayRoute === undefined)
|
|
637
815
|
reportCatalogGap(ctx, route);
|
|
638
816
|
const { imageSupport, systemPromptUpdate, resolved } = modelFacts;
|
|
@@ -640,9 +818,12 @@ async function serializeNativeAware(rawMessages, route, routeConfig, ctx, option
|
|
|
640
818
|
let messages = rawMessages;
|
|
641
819
|
let tools = options.tools;
|
|
642
820
|
let anchoredTools = false;
|
|
821
|
+
const gatewayCompatibilityFallback = routeConfig.gatewayCompatibilityFallback === true;
|
|
643
822
|
if (options.toolSurface !== undefined) {
|
|
644
823
|
const surface = planToolSurface({
|
|
645
|
-
mode:
|
|
824
|
+
mode: gatewayCompatibilityFallback
|
|
825
|
+
? "full"
|
|
826
|
+
: (routeConfig.toolUpdates ?? DEFAULT_TOOL_UPDATES),
|
|
646
827
|
messages: rawMessages,
|
|
647
828
|
tools: options.tools,
|
|
648
829
|
toolHistory: options.toolSurface.history,
|
|
@@ -667,18 +848,52 @@ async function serializeNativeAware(rawMessages, route, routeConfig, ctx, option
|
|
|
667
848
|
let grammarToolInputProperties;
|
|
668
849
|
/** Absolute input positions rendered from prompt snapshots superseded by a checkpoint. */
|
|
669
850
|
const supersededInstructionItems = [];
|
|
670
|
-
const
|
|
671
|
-
route
|
|
851
|
+
const baseSerializeOptions = (extra = {}) => requestImageOptions(routeConfig, imageSupport, options.signal, undefined, {
|
|
852
|
+
route: routeConfig.profileDefaultMaxTokens === undefined && routeConfig.catalogMaxTokens === undefined
|
|
853
|
+
? route
|
|
854
|
+
: {
|
|
855
|
+
...route,
|
|
856
|
+
...(routeConfig.catalogMaxTokens === undefined ? {} : { catalogMaxTokens: routeConfig.catalogMaxTokens }),
|
|
857
|
+
...(routeConfig.profileDefaultMaxTokens === undefined ? {} : { profileDefaultMaxTokens: routeConfig.profileDefaultMaxTokens }),
|
|
858
|
+
},
|
|
859
|
+
tools,
|
|
672
860
|
responsesCompat: routeConfig.responsesCompat,
|
|
673
861
|
systemPromptUpdate,
|
|
674
862
|
...(anchoredTools ? { toolUpdate: "addition-only" } : {}),
|
|
863
|
+
...(gatewayCompatibilityFallback ? { gatewayCompatibilityFallback: true } : {}),
|
|
675
864
|
...(modelOverride === undefined ? {} : { model: modelOverride }),
|
|
676
865
|
...extra,
|
|
677
866
|
});
|
|
678
867
|
const { systemPrompt, messages: surfaceMessages } = splitSystemPrompt(options.system, messages);
|
|
679
868
|
// Resolve the descriptor before selecting effort. This conversion has no tool-bearing transcript.
|
|
680
|
-
const descriptor = await serializeDshMessages([], ctx,
|
|
681
|
-
|
|
869
|
+
const descriptor = await serializeDshMessages([], ctx, baseSerializeOptions({ tools: undefined }));
|
|
870
|
+
await options.validateBeforeAuth?.(descriptor.model, modelFacts);
|
|
871
|
+
const configurationUpdateEffort = gatewayCompatibilityFallback
|
|
872
|
+
? undefined
|
|
873
|
+
: await options.configurationUpdate?.(descriptor.model, modelFacts);
|
|
874
|
+
const providerThinkingLevel = configurationUpdateEffort ??
|
|
875
|
+
(gatewayCompatibilityFallback ? await options.persistedThinkingLevel?.(descriptor.model, modelFacts) : undefined);
|
|
876
|
+
const headerAuthorityFingerprint = options.resolveHeaderAuthorityFingerprint !== undefined
|
|
877
|
+
? await options.resolveHeaderAuthorityFingerprint()
|
|
878
|
+
: route.headerAuthorityFingerprint ??
|
|
879
|
+
(await resolveRouteHeaderAuthorityFingerprint(ctx, route, routeConfig, {
|
|
880
|
+
sessionId: route.sessionId || undefined,
|
|
881
|
+
}));
|
|
882
|
+
const effectiveRoute = headerAuthorityFingerprint === undefined
|
|
883
|
+
? route
|
|
884
|
+
: { ...route, headerAuthorityFingerprint };
|
|
885
|
+
const assistantReplayScope = await resolveAssistantReplayScope(ctx, session, effectiveRoute, options.grokNativeReplayRoute, headerAuthorityFingerprint);
|
|
886
|
+
const serializeOptions = (extra = {}) => baseSerializeOptions({
|
|
887
|
+
route: routeConfig.profileDefaultMaxTokens === undefined && routeConfig.catalogMaxTokens === undefined
|
|
888
|
+
? effectiveRoute
|
|
889
|
+
: {
|
|
890
|
+
...effectiveRoute,
|
|
891
|
+
...(routeConfig.catalogMaxTokens === undefined ? {} : { catalogMaxTokens: routeConfig.catalogMaxTokens }),
|
|
892
|
+
...(routeConfig.profileDefaultMaxTokens === undefined ? {} : { profileDefaultMaxTokens: routeConfig.profileDefaultMaxTokens }),
|
|
893
|
+
},
|
|
894
|
+
assistantReplayScope,
|
|
895
|
+
...extra,
|
|
896
|
+
});
|
|
682
897
|
const nonce = `lcx-request-boundary:${globalThis.crypto.randomUUID()}:`;
|
|
683
898
|
const marker = (id) => ({
|
|
684
899
|
role: "user", content: [{ type: "text", text: nonce + id }],
|
|
@@ -687,6 +902,7 @@ async function serializeNativeAware(rawMessages, route, routeConfig, ctx, option
|
|
|
687
902
|
// the entire tool-bearing surface; Pi alone owns declarations, removals and incremental notices.
|
|
688
903
|
const transcript = [];
|
|
689
904
|
const replacements = new Map();
|
|
905
|
+
const replacementContexts = new Map();
|
|
690
906
|
for (const message of surfaceMessages) {
|
|
691
907
|
assertSupportedCheckpointMessage(message);
|
|
692
908
|
const checkpointId = session && isPersistedMessage(message) ? compactCheckpointId(message) : undefined;
|
|
@@ -696,10 +912,11 @@ async function serializeNativeAware(rawMessages, route, routeConfig, ctx, option
|
|
|
696
912
|
continue;
|
|
697
913
|
}
|
|
698
914
|
let items;
|
|
699
|
-
if (stateRouteCompatible(state,
|
|
915
|
+
if (stateRouteCompatible(state, effectiveRoute, ctx)) {
|
|
700
916
|
const hydratedMap = new Map();
|
|
701
917
|
items = responseInputItems(await hydrateNativeImageReferences(state.nativeOutput, ctx, requestImageOptions(routeConfig, imageSupport, options.signal, hydratedMap)));
|
|
702
918
|
mergeMap(imageMap, hydratedMap);
|
|
919
|
+
replacementContexts.set(String(replacements.size), estimateItemsToPiMessages(items));
|
|
703
920
|
}
|
|
704
921
|
else {
|
|
705
922
|
const portable = await portableMessagesForCheckpoint(ctx, session, checkpointId, {
|
|
@@ -709,18 +926,26 @@ async function serializeNativeAware(rawMessages, route, routeConfig, ctx, option
|
|
|
709
926
|
// the current series' surface or regain instruction authority on a different route.
|
|
710
927
|
const serialized = await serializeDshMessages(portable, ctx, serializeOptions({
|
|
711
928
|
systemPromptUpdate: undefined, toolUpdate: undefined, tools: undefined,
|
|
929
|
+
assistantReplayScope: { disallowAllNativeReplay: true },
|
|
712
930
|
// Fixed replay images cannot be offloaded by the surface owner; check the assembled request below.
|
|
713
931
|
maxRequestImageBytes: Number.MAX_SAFE_INTEGER, maxRequestImages: Number.MAX_SAFE_INTEGER,
|
|
714
932
|
}));
|
|
715
933
|
items = serialized.input;
|
|
716
934
|
mergeMap(imageMap, serialized.imageMap);
|
|
935
|
+
const portableMessages = serialized.piContext.systemPrompt !== undefined && serialized.piContext.systemPrompt.length > 0
|
|
936
|
+
? [
|
|
937
|
+
{ role: "system", content: serialized.piContext.systemPrompt, timestamp: 0 },
|
|
938
|
+
...serialized.piContext.messages,
|
|
939
|
+
]
|
|
940
|
+
: serialized.piContext.messages;
|
|
941
|
+
replacementContexts.set(String(replacements.size), portableMessages);
|
|
717
942
|
}
|
|
718
943
|
const id = String(replacements.size);
|
|
719
944
|
replacements.set(id, items);
|
|
720
945
|
transcript.push(marker(id));
|
|
721
946
|
}
|
|
722
947
|
const serialized = await serializeDshMessages(transcript, ctx, serializeOptions({
|
|
723
|
-
systemPrompt,
|
|
948
|
+
systemPrompt, tracePrelude: true,
|
|
724
949
|
...(configurationUpdateEffort === undefined ? {} : { configurationUpdateEffort }),
|
|
725
950
|
}));
|
|
726
951
|
const surfaceImageBytes = inputImageBase64Lengths(serialized.input);
|
|
@@ -728,7 +953,71 @@ async function serializeNativeAware(rawMessages, route, routeConfig, ctx, option
|
|
|
728
953
|
nativeModel = serialized.model;
|
|
729
954
|
grammarToolInputProperties = serialized.grammarToolInputProperties;
|
|
730
955
|
mergeMap(imageMap, serialized.imageMap);
|
|
731
|
-
const
|
|
956
|
+
const grokRestored = new Map();
|
|
957
|
+
const expectedGrokRouteFingerprint = options.grokNativeReplayRoute === undefined
|
|
958
|
+
? undefined
|
|
959
|
+
: grokNativeReplayRouteFingerprint(options.grokNativeReplayRoute);
|
|
960
|
+
const grokNativeEligibleMessages = surfaceMessages
|
|
961
|
+
.filter(isPersistedMessage)
|
|
962
|
+
.filter((message) => {
|
|
963
|
+
if (message.role !== "assistant" || message.source.kind !== "model")
|
|
964
|
+
return false;
|
|
965
|
+
const lcxReplay = isRecord(message.source.replayState)
|
|
966
|
+
? message.source.replayState.lcxReplay
|
|
967
|
+
: undefined;
|
|
968
|
+
return (isRecord(lcxReplay) &&
|
|
969
|
+
lcxReplay.version === 1 &&
|
|
970
|
+
typeof lcxReplay.sourceSessionId === "string" &&
|
|
971
|
+
lcxReplay.sourceSessionId.length > 0 &&
|
|
972
|
+
lcxReplay.sourceSessionId === effectiveRoute.sessionId &&
|
|
973
|
+
typeof lcxReplay.headerAuthorityFingerprint === "string" &&
|
|
974
|
+
lcxReplay.headerAuthorityFingerprint.length > 0 &&
|
|
975
|
+
typeof headerAuthorityFingerprint === "string" &&
|
|
976
|
+
headerAuthorityFingerprint.length > 0 &&
|
|
977
|
+
lcxReplay.headerAuthorityFingerprint === headerAuthorityFingerprint &&
|
|
978
|
+
expectedGrokRouteFingerprint !== undefined &&
|
|
979
|
+
typeof lcxReplay.grokRouteAuthorityFingerprint === "string" &&
|
|
980
|
+
lcxReplay.grokRouteAuthorityFingerprint.length > 0 &&
|
|
981
|
+
lcxReplay.grokRouteAuthorityFingerprint === expectedGrokRouteFingerprint);
|
|
982
|
+
});
|
|
983
|
+
const effectiveGrokNativeReplayRoute = options.grokNativeReplayRoute === undefined
|
|
984
|
+
? undefined
|
|
985
|
+
: {
|
|
986
|
+
...options.grokNativeReplayRoute,
|
|
987
|
+
...(headerAuthorityFingerprint ? { headerAuthorityFingerprint } : {}),
|
|
988
|
+
};
|
|
989
|
+
const restored = restoreGrokNativeReplay(serialized.input, grokNativeEligibleMessages, effectiveGrokNativeReplayRoute, (message, output) => {
|
|
990
|
+
grokRestored.set(message, output);
|
|
991
|
+
});
|
|
992
|
+
const surfaceAssistants = surfaceMessages.filter((message) => message.role === "assistant");
|
|
993
|
+
let assistantIndex = 0;
|
|
994
|
+
const piContext = replacementContexts.size === 0 && grokRestored.size === 0
|
|
995
|
+
? serialized.piContext
|
|
996
|
+
: {
|
|
997
|
+
...serialized.piContext,
|
|
998
|
+
messages: serialized.piContext.messages.flatMap((msg) => {
|
|
999
|
+
if (msg.role === "assistant") {
|
|
1000
|
+
const surfaceAssistant = surfaceAssistants[assistantIndex++];
|
|
1001
|
+
const restoredOutput = surfaceAssistant !== undefined && isPersistedMessage(surfaceAssistant)
|
|
1002
|
+
? grokRestored.get(surfaceAssistant)
|
|
1003
|
+
: undefined;
|
|
1004
|
+
if (restoredOutput !== undefined) {
|
|
1005
|
+
return estimateItemsToPiMessages(restoredOutput);
|
|
1006
|
+
}
|
|
1007
|
+
return [msg];
|
|
1008
|
+
}
|
|
1009
|
+
if (msg.role === "user" &&
|
|
1010
|
+
Array.isArray(msg.content) &&
|
|
1011
|
+
msg.content.length === 1 &&
|
|
1012
|
+
msg.content[0]?.type === "text" &&
|
|
1013
|
+
typeof msg.content[0].text === "string" &&
|
|
1014
|
+
msg.content[0].text.startsWith(nonce)) {
|
|
1015
|
+
const id = msg.content[0].text.slice(nonce.length);
|
|
1016
|
+
return replacementContexts.get(id) ?? [msg];
|
|
1017
|
+
}
|
|
1018
|
+
return [msg];
|
|
1019
|
+
}),
|
|
1020
|
+
};
|
|
732
1021
|
const snapshotPositions = new Set(locateSupersededItems(serialized.input, restored, serialized.supersededInstructionItems));
|
|
733
1022
|
const ephemeralPreludeItemCount = serialized.preludeItemCount;
|
|
734
1023
|
const seen = new Set();
|
|
@@ -768,14 +1057,17 @@ async function serializeNativeAware(rawMessages, route, routeConfig, ctx, option
|
|
|
768
1057
|
return {
|
|
769
1058
|
input,
|
|
770
1059
|
configurationUpdateEffort,
|
|
1060
|
+
providerThinkingLevel,
|
|
771
1061
|
ephemeralPreludeItemCount,
|
|
772
1062
|
supersededInstructionItems,
|
|
773
1063
|
imageMap,
|
|
774
1064
|
imageSupport,
|
|
775
1065
|
tools: nativeTools,
|
|
776
1066
|
model: nativeModel,
|
|
1067
|
+
piContext,
|
|
777
1068
|
grammarToolInputProperties,
|
|
778
1069
|
modelFacts,
|
|
1070
|
+
headerAuthorityFingerprint,
|
|
779
1071
|
};
|
|
780
1072
|
}
|
|
781
1073
|
function fallbackEligible(error, signal) {
|
|
@@ -788,19 +1080,57 @@ function fallbackEligible(error, signal) {
|
|
|
788
1080
|
return true;
|
|
789
1081
|
return ["LCX_HTTP_RETRYABLE", "LCX_RETRY_EXHAUSTED"].includes(String(details.code ?? ""));
|
|
790
1082
|
}
|
|
791
|
-
async function* remoteCompactionStream(options, routeConfig, ctx, next) {
|
|
1083
|
+
async function* remoteCompactionStream(options, routeConfig, ctx, next, prefetchedModelFacts) {
|
|
792
1084
|
const history = stripCompactionDirective(options.messages);
|
|
793
1085
|
const route = currentRoute(options, routeConfig);
|
|
794
1086
|
try {
|
|
1087
|
+
assertValidTemperature(options.temperature);
|
|
1088
|
+
assertValidPositiveMaxTokens(options.maxTokens);
|
|
1089
|
+
const generation = generationControlsFromSession(sessionFor(ctx, route.sessionId), route);
|
|
1090
|
+
const cacheSessionId = promptCacheSessionId(route, routeConfig, ctx);
|
|
1091
|
+
let cachedAuthHeaders;
|
|
1092
|
+
const getAuthHeaders = () => (cachedAuthHeaders ??= (async () => {
|
|
1093
|
+
const apiKey = await resolveApiKey(ctx, routeConfig);
|
|
1094
|
+
const headerRouteConfig = routeConfig.apiKeyEnv
|
|
1095
|
+
? routeConfig
|
|
1096
|
+
: {
|
|
1097
|
+
...routeConfig,
|
|
1098
|
+
provider: routeConfig.provider ?? route.provider,
|
|
1099
|
+
model: routeConfig.model ?? route.model,
|
|
1100
|
+
apiKeyEnv: resolveResponsesRouteConfig(ctx, { provider: route.provider, model: route.model }, {})
|
|
1101
|
+
?.apiKeyEnv ?? "",
|
|
1102
|
+
};
|
|
1103
|
+
const { headers, headerAuthorityFingerprint } = await authenticatedRequestHeaders(ctx, headerRouteConfig, cacheSessionId, cacheSessionId === undefined ? null : route.sessionId, providerDynamicHeaders(routeConfig.provider, options.messages), route.sessionId || undefined, options, apiKey);
|
|
1104
|
+
return {
|
|
1105
|
+
apiKey,
|
|
1106
|
+
headers,
|
|
1107
|
+
headerAuthorityFingerprint,
|
|
1108
|
+
};
|
|
1109
|
+
})());
|
|
1110
|
+
let effectiveCompactionReasoning;
|
|
1111
|
+
let effectiveCompactionTemperature;
|
|
795
1112
|
const prepared = await serializeNativeAware(history, route, routeConfig, ctx, {
|
|
796
1113
|
signal: options.signal,
|
|
797
1114
|
system: options.system,
|
|
798
1115
|
tools: options.tools && [...options.tools],
|
|
799
1116
|
toolSurface: { history: options.toolHistory },
|
|
1117
|
+
...(prefetchedModelFacts === undefined ? {} : { prefetchedModelFacts }),
|
|
1118
|
+
validateBeforeAuth: (model, modelFacts) => {
|
|
1119
|
+
effectiveCompactionReasoning = resolveEffectiveReasoningEffort(options.reasoningEffort ?? generation.reasoningEffort, modelFacts, routeConfig);
|
|
1120
|
+
assertSupportedReasoningEffort(effectiveCompactionReasoning, modelFacts, model, routeConfig);
|
|
1121
|
+
effectiveCompactionTemperature =
|
|
1122
|
+
options.temperature ?? generation.temperature;
|
|
1123
|
+
assertValidTemperature(effectiveCompactionTemperature);
|
|
1124
|
+
},
|
|
1125
|
+
resolveHeaderAuthorityFingerprint: async () => (await getAuthHeaders()).headerAuthorityFingerprint,
|
|
800
1126
|
});
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
1127
|
+
effectiveCompactionReasoning ??= resolveEffectiveReasoningEffort(options.reasoningEffort ?? generation.reasoningEffort, prepared.modelFacts, routeConfig);
|
|
1128
|
+
assertSupportedReasoningEffort(effectiveCompactionReasoning, prepared.modelFacts, prepared.model, routeConfig);
|
|
1129
|
+
effectiveCompactionTemperature ??=
|
|
1130
|
+
options.temperature ?? generation.temperature;
|
|
1131
|
+
assertValidTemperature(effectiveCompactionTemperature);
|
|
1132
|
+
const effectiveCompactionMaxTokens = resolveEffectiveMaxTokens(options.maxTokens ?? generation.maxTokens, prepared.modelFacts, prepared.model, prepared.piContext);
|
|
1133
|
+
const { apiKey, headers } = await getAuthHeaders();
|
|
804
1134
|
const result = await requestNativeCompaction({
|
|
805
1135
|
baseURL: routeConfig.baseURL,
|
|
806
1136
|
model: route.model,
|
|
@@ -810,9 +1140,11 @@ async function* remoteCompactionStream(options, routeConfig, ctx, next) {
|
|
|
810
1140
|
promptCacheKey: promptCacheKey(route, routeConfig, ctx),
|
|
811
1141
|
promptCacheRetention: promptCacheRetention(routeConfig),
|
|
812
1142
|
cacheRetention: routeConfig.cacheRetention,
|
|
813
|
-
reasoningEffort:
|
|
814
|
-
temperature:
|
|
815
|
-
maxTokens:
|
|
1143
|
+
reasoningEffort: effectiveCompactionReasoning,
|
|
1144
|
+
temperature: effectiveCompactionTemperature,
|
|
1145
|
+
maxTokens: effectiveCompactionMaxTokens,
|
|
1146
|
+
gatewayCompatibilityFallback: routeConfig.gatewayCompatibilityFallback === true,
|
|
1147
|
+
omitUnsupportedFields: isChatGPTSignInCredential(prepared.model, apiKey),
|
|
816
1148
|
idempotencyKey: randomUUID(),
|
|
817
1149
|
headers,
|
|
818
1150
|
signal: options.signal,
|
|
@@ -826,7 +1158,9 @@ async function* remoteCompactionStream(options, routeConfig, ctx, next) {
|
|
|
826
1158
|
const block = await createNativeCheckpointBlock({
|
|
827
1159
|
ctx,
|
|
828
1160
|
session,
|
|
829
|
-
route
|
|
1161
|
+
route: prepared.headerAuthorityFingerprint === undefined
|
|
1162
|
+
? route
|
|
1163
|
+
: { ...route, headerAuthorityFingerprint: prepared.headerAuthorityFingerprint },
|
|
830
1164
|
result,
|
|
831
1165
|
input: prepared.input,
|
|
832
1166
|
ephemeralPreludeItemCount: prepared.ephemeralPreludeItemCount,
|
|
@@ -1090,21 +1424,63 @@ function logStreamFailure(ctx, route, info) {
|
|
|
1090
1424
|
].filter(Boolean);
|
|
1091
1425
|
ctx.logger?.warn?.(`[lcx-codex] responses stream failed: ${parts.join(" ")}`);
|
|
1092
1426
|
}
|
|
1093
|
-
async function* managedResponsesStream(options, routeConfig, ctx) {
|
|
1427
|
+
async function* managedResponsesStream(options, routeConfig, ctx, prefetchedModelFacts) {
|
|
1094
1428
|
const route = currentRoute(options, routeConfig);
|
|
1095
1429
|
try {
|
|
1096
1430
|
if (options.stop !== undefined)
|
|
1097
1431
|
throw Object.assign(new Error("LCX Responses does not support GenerateOptions.stop"), { code: "LCX_RESPONSES_UNSUPPORTED_OPTION" });
|
|
1432
|
+
assertValidTemperature(options.temperature);
|
|
1433
|
+
assertValidPositiveMaxTokens(options.maxTokens);
|
|
1434
|
+
let cachedApiKey;
|
|
1435
|
+
const getApiKey = () => (cachedApiKey ??= resolveApiKey(ctx, routeConfig));
|
|
1436
|
+
const cacheSessionId = promptCacheSessionId(route, routeConfig, ctx);
|
|
1437
|
+
let cachedAuthHeaders;
|
|
1438
|
+
const getAuthHeaders = () => (cachedAuthHeaders ??= (async () => {
|
|
1439
|
+
const apiKey = await getApiKey();
|
|
1440
|
+
const headerRouteConfig = routeConfig.apiKeyEnv
|
|
1441
|
+
? routeConfig
|
|
1442
|
+
: {
|
|
1443
|
+
...routeConfig,
|
|
1444
|
+
provider: routeConfig.provider ?? route.provider,
|
|
1445
|
+
model: routeConfig.model ?? route.model,
|
|
1446
|
+
apiKeyEnv: resolveResponsesRouteConfig(ctx, { provider: route.provider, model: route.model }, {})
|
|
1447
|
+
?.apiKeyEnv ?? "",
|
|
1448
|
+
};
|
|
1449
|
+
const { headers, headerAuthorityFingerprint } = await authenticatedRequestHeaders(ctx, headerRouteConfig, cacheSessionId, cacheSessionId === undefined ? null : route.sessionId, providerDynamicHeaders(routeConfig.provider, options.messages), route.sessionId || undefined, options, apiKey);
|
|
1450
|
+
return {
|
|
1451
|
+
apiKey,
|
|
1452
|
+
headers,
|
|
1453
|
+
headerAuthorityFingerprint,
|
|
1454
|
+
};
|
|
1455
|
+
})());
|
|
1098
1456
|
const prepared = await serializeNativeAware(options.messages, route, routeConfig, ctx, {
|
|
1099
1457
|
signal: options.signal,
|
|
1100
1458
|
system: options.system,
|
|
1101
1459
|
tools: options.tools && [...options.tools],
|
|
1102
1460
|
toolSurface: { history: options.toolHistory },
|
|
1103
|
-
|
|
1461
|
+
...(prefetchedModelFacts === undefined ? {} : { prefetchedModelFacts }),
|
|
1462
|
+
validateBeforeAuth: (model, modelFacts) => {
|
|
1463
|
+
const effectiveEffort = resolveEffectiveReasoningEffort(options.reasoningEffort, modelFacts, routeConfig);
|
|
1464
|
+
assertSupportedReasoningEffort(effectiveEffort, modelFacts, model, routeConfig);
|
|
1465
|
+
},
|
|
1466
|
+
resolveHeaderAuthorityFingerprint: async () => (await getAuthHeaders()).headerAuthorityFingerprint,
|
|
1467
|
+
configurationUpdate: (model, modelFacts) => {
|
|
1468
|
+
const effectiveEffort = resolveEffectiveReasoningEffort(options.reasoningEffort, modelFacts, routeConfig);
|
|
1469
|
+
assertSupportedReasoningEffort(effectiveEffort, modelFacts, model, routeConfig);
|
|
1470
|
+
return configurationUpdateEffort(model, effectiveEffort, getApiKey, {
|
|
1471
|
+
gatewayCompatibilityFallback: routeConfig.gatewayCompatibilityFallback === true,
|
|
1472
|
+
});
|
|
1473
|
+
},
|
|
1474
|
+
persistedThinkingLevel: (model, modelFacts) => {
|
|
1475
|
+
const effectiveEffort = resolveEffectiveReasoningEffort(options.reasoningEffort, modelFacts, routeConfig);
|
|
1476
|
+
assertSupportedReasoningEffort(effectiveEffort, modelFacts, model, routeConfig);
|
|
1477
|
+
return configurationUpdateEffort(model, effectiveEffort, getApiKey, { gatewayCompatibilityFallback: false });
|
|
1478
|
+
},
|
|
1104
1479
|
});
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
const
|
|
1480
|
+
const effectiveReasoningEffort = resolveEffectiveReasoningEffort(options.reasoningEffort, prepared.modelFacts, routeConfig);
|
|
1481
|
+
assertSupportedReasoningEffort(effectiveReasoningEffort, prepared.modelFacts, prepared.model, routeConfig);
|
|
1482
|
+
const effectiveMaxTokens = resolveEffectiveMaxTokens(options.maxTokens, prepared.modelFacts, prepared.model, prepared.piContext);
|
|
1483
|
+
const { apiKey, headers, headerAuthorityFingerprint } = await getAuthHeaders();
|
|
1108
1484
|
const body = buildResponsesBody({
|
|
1109
1485
|
model: prepared.model,
|
|
1110
1486
|
input: prepared.input,
|
|
@@ -1113,9 +1489,11 @@ async function* managedResponsesStream(options, routeConfig, ctx) {
|
|
|
1113
1489
|
promptCacheKey: promptCacheKey(route, routeConfig, ctx),
|
|
1114
1490
|
promptCacheRetention: promptCacheRetention(routeConfig),
|
|
1115
1491
|
cacheRetention: routeConfig.cacheRetention,
|
|
1116
|
-
reasoningEffort:
|
|
1492
|
+
reasoningEffort: effectiveReasoningEffort,
|
|
1117
1493
|
temperature: options.temperature,
|
|
1118
|
-
maxTokens:
|
|
1494
|
+
maxTokens: effectiveMaxTokens,
|
|
1495
|
+
gatewayCompatibilityFallback: routeConfig.gatewayCompatibilityFallback === true,
|
|
1496
|
+
omitUnsupportedFields: isChatGPTSignInCredential(prepared.model, apiKey),
|
|
1119
1497
|
...(prepared.configurationUpdateEffort === undefined ? {} : { configurationUpdateEffort: prepared.configurationUpdateEffort }),
|
|
1120
1498
|
});
|
|
1121
1499
|
const nativeReplay = inputHasNativeState(prepared.input);
|
|
@@ -1127,6 +1505,8 @@ async function* managedResponsesStream(options, routeConfig, ctx) {
|
|
|
1127
1505
|
baseURL: routeConfig.baseURL,
|
|
1128
1506
|
provider: route.provider,
|
|
1129
1507
|
model: route.model,
|
|
1508
|
+
sessionId: route.sessionId || undefined,
|
|
1509
|
+
headerAuthorityFingerprint,
|
|
1130
1510
|
piModel: prepared.model,
|
|
1131
1511
|
body,
|
|
1132
1512
|
grammarToolInputProperties: prepared.grammarToolInputProperties,
|
|
@@ -1135,7 +1515,7 @@ async function* managedResponsesStream(options, routeConfig, ctx) {
|
|
|
1135
1515
|
timeoutMs: routeConfig.timeoutMs,
|
|
1136
1516
|
maxAttempts: 1,
|
|
1137
1517
|
maxResponseBytes: routeConfig.maxResponseBytes,
|
|
1138
|
-
...(prepared.
|
|
1518
|
+
...(prepared.providerThinkingLevel === undefined ? {} : { providerThinkingLevel: prepared.providerThinkingLevel }),
|
|
1139
1519
|
onFailure: (info) => logStreamFailure(ctx, "gpt", info),
|
|
1140
1520
|
});
|
|
1141
1521
|
}
|
|
@@ -1143,37 +1523,248 @@ async function* managedResponsesStream(options, routeConfig, ctx) {
|
|
|
1143
1523
|
yield managedFailureChunk(error, options.signal);
|
|
1144
1524
|
}
|
|
1145
1525
|
}
|
|
1146
|
-
|
|
1526
|
+
function createGrokBudgetScopeStores() {
|
|
1527
|
+
return {
|
|
1528
|
+
warnings: new WeakMap(),
|
|
1529
|
+
generations: new WeakMap(),
|
|
1530
|
+
pending: new WeakMap(),
|
|
1531
|
+
};
|
|
1532
|
+
}
|
|
1533
|
+
const grokBudgetProfileStores = createGrokBudgetScopeStores();
|
|
1534
|
+
const grokBudgetRequestStores = createGrokBudgetScopeStores();
|
|
1535
|
+
const grokBudgetInFlightCounts = new WeakMap();
|
|
1536
|
+
let grokBudgetNextToken = 0;
|
|
1537
|
+
function grokBudgetScope(requestMaxTokens) {
|
|
1538
|
+
return requestMaxTokens === undefined ? "profile" : "request";
|
|
1539
|
+
}
|
|
1540
|
+
function grokBudgetStoresForScope(scope) {
|
|
1541
|
+
return scope === "profile" ? grokBudgetProfileStores : grokBudgetRequestStores;
|
|
1542
|
+
}
|
|
1543
|
+
function contextMap(store, ctx) {
|
|
1544
|
+
if (ctx === null || typeof ctx !== "object")
|
|
1545
|
+
return undefined;
|
|
1546
|
+
let existing = store.get(ctx);
|
|
1547
|
+
if (!existing) {
|
|
1548
|
+
existing = new Map();
|
|
1549
|
+
store.set(ctx, existing);
|
|
1550
|
+
}
|
|
1551
|
+
return existing;
|
|
1552
|
+
}
|
|
1553
|
+
function grokBudgetKey(route) {
|
|
1554
|
+
return `${route.provider}\u001f${route.model}`;
|
|
1555
|
+
}
|
|
1556
|
+
const MAX_PENDING_GROK_LOOKUPS = 4096;
|
|
1557
|
+
function evictOldestNonPending(cache, pending, maxSize = 256) {
|
|
1558
|
+
if (cache.size < maxSize)
|
|
1559
|
+
return;
|
|
1560
|
+
for (const existingKey of cache.keys()) {
|
|
1561
|
+
if (!pending?.has(existingKey)) {
|
|
1562
|
+
cache.delete(existingKey);
|
|
1563
|
+
if (cache.size < maxSize)
|
|
1564
|
+
return;
|
|
1565
|
+
}
|
|
1566
|
+
}
|
|
1567
|
+
}
|
|
1568
|
+
function nextGrokBudgetGeneration(ctx, scope, key) {
|
|
1569
|
+
const stores = grokBudgetStoresForScope(scope);
|
|
1570
|
+
const generations = contextMap(stores.generations, ctx);
|
|
1571
|
+
if (!generations)
|
|
1572
|
+
return 0;
|
|
1573
|
+
evictOldestNonPending(generations, contextMap(stores.pending, ctx));
|
|
1574
|
+
grokBudgetNextToken += 1;
|
|
1575
|
+
generations.set(key, grokBudgetNextToken);
|
|
1576
|
+
return grokBudgetNextToken;
|
|
1577
|
+
}
|
|
1578
|
+
function recordGrokBudgetWarning(ctx, route, warning, options) {
|
|
1579
|
+
const scope = options?.scope ?? "profile";
|
|
1580
|
+
const key = options?.key ?? grokBudgetKey(route);
|
|
1581
|
+
const stores = grokBudgetStoresForScope(scope);
|
|
1582
|
+
if (options?.expectedGeneration !== undefined) {
|
|
1583
|
+
const generations = contextMap(stores.generations, ctx);
|
|
1584
|
+
const currentGeneration = generations?.get(key);
|
|
1585
|
+
const pendingGeneration = contextMap(stores.pending, ctx)?.get(key)?.generation;
|
|
1586
|
+
if (currentGeneration !== undefined) {
|
|
1587
|
+
if (currentGeneration !== options.expectedGeneration)
|
|
1588
|
+
return;
|
|
1589
|
+
}
|
|
1590
|
+
else if (pendingGeneration !== options.expectedGeneration) {
|
|
1591
|
+
return;
|
|
1592
|
+
}
|
|
1593
|
+
}
|
|
1594
|
+
const targetCache = contextMap(stores.warnings, ctx);
|
|
1595
|
+
if (warning === undefined) {
|
|
1596
|
+
targetCache?.delete(key);
|
|
1597
|
+
return;
|
|
1598
|
+
}
|
|
1599
|
+
if (targetCache?.get(key) === warning)
|
|
1600
|
+
return;
|
|
1601
|
+
if (targetCache) {
|
|
1602
|
+
evictOldestNonPending(targetCache, contextMap(stores.pending, ctx));
|
|
1603
|
+
targetCache.set(key, warning);
|
|
1604
|
+
}
|
|
1605
|
+
try {
|
|
1606
|
+
ctx.logger?.warn?.(warning);
|
|
1607
|
+
}
|
|
1608
|
+
catch {
|
|
1609
|
+
// Diagnostic logging failures must never affect caller lifecycle or stream outcome.
|
|
1610
|
+
}
|
|
1611
|
+
}
|
|
1612
|
+
function recordGrokBudgetFromModelFacts(ctx, route, facts, requestMaxTokens) {
|
|
1613
|
+
const scope = grokBudgetScope(requestMaxTokens);
|
|
1614
|
+
const key = grokBudgetKey(route);
|
|
1615
|
+
const generation = nextGrokBudgetGeneration(ctx, scope, key);
|
|
1616
|
+
contextMap(grokBudgetStoresForScope(scope).pending, ctx)?.delete(key);
|
|
1617
|
+
recordGrokBudgetWarning(ctx, route, diagnoseGrokContextBudget(ctx, route, {
|
|
1618
|
+
requestMaxTokens,
|
|
1619
|
+
resolvedModelInfo: facts.resolved
|
|
1620
|
+
? {
|
|
1621
|
+
contextWindow: facts.contextWindow,
|
|
1622
|
+
defaultMaxTokens: facts.defaultMaxTokens,
|
|
1623
|
+
}
|
|
1624
|
+
: undefined,
|
|
1625
|
+
}), { scope, key, expectedGeneration: generation });
|
|
1626
|
+
}
|
|
1627
|
+
function reportGrokBudgetDiagnosis(ctx, route, options) {
|
|
1628
|
+
if (!route.provider || !route.model || !isGrokModelId(route.model))
|
|
1629
|
+
return;
|
|
1630
|
+
const scope = grokBudgetScope(options?.requestMaxTokens);
|
|
1631
|
+
const key = grokBudgetKey(route);
|
|
1632
|
+
const stores = grokBudgetStoresForScope(scope);
|
|
1633
|
+
const canLookup = !options?.skipAsyncLookup &&
|
|
1634
|
+
typeof ctx?.llm?.resolveModelInfo === "function" &&
|
|
1635
|
+
grokRouteMayNeedHostBudgetLookup(ctx, route, options?.requestMaxTokens);
|
|
1636
|
+
if (canLookup) {
|
|
1637
|
+
const signature = grokBudgetInputSignature(ctx, route, options?.requestMaxTokens);
|
|
1638
|
+
const pending = contextMap(stores.pending, ctx);
|
|
1639
|
+
const existing = pending?.get(key);
|
|
1640
|
+
if (existing && existing.signature === signature)
|
|
1641
|
+
return;
|
|
1642
|
+
const owner = ctx;
|
|
1643
|
+
const activeInFlight = owner !== null && typeof owner === "object"
|
|
1644
|
+
? (grokBudgetInFlightCounts.get(owner) ?? 0)
|
|
1645
|
+
: 0;
|
|
1646
|
+
if (activeInFlight >= MAX_PENDING_GROK_LOOKUPS ||
|
|
1647
|
+
(pending && !existing && pending.size >= MAX_PENDING_GROK_LOOKUPS)) {
|
|
1648
|
+
return;
|
|
1649
|
+
}
|
|
1650
|
+
if (owner !== null && typeof owner === "object") {
|
|
1651
|
+
grokBudgetInFlightCounts.set(owner, activeInFlight + 1);
|
|
1652
|
+
}
|
|
1653
|
+
const generation = nextGrokBudgetGeneration(ctx, scope, key);
|
|
1654
|
+
let promise;
|
|
1655
|
+
try {
|
|
1656
|
+
promise = diagnoseGrokContextBudgetAsync(ctx, route, {
|
|
1657
|
+
requestMaxTokens: options?.requestMaxTokens,
|
|
1658
|
+
})
|
|
1659
|
+
.then((warning) => {
|
|
1660
|
+
recordGrokBudgetWarning(ctx, route, warning, { scope, key, expectedGeneration: generation });
|
|
1661
|
+
})
|
|
1662
|
+
.catch(() => {
|
|
1663
|
+
// Pre-flight diagnostic lookup must never affect caller lifecycle.
|
|
1664
|
+
})
|
|
1665
|
+
.finally(() => {
|
|
1666
|
+
if (owner !== null && typeof owner === "object") {
|
|
1667
|
+
const current = grokBudgetInFlightCounts.get(owner) ?? 0;
|
|
1668
|
+
if (current <= 1)
|
|
1669
|
+
grokBudgetInFlightCounts.delete(owner);
|
|
1670
|
+
else
|
|
1671
|
+
grokBudgetInFlightCounts.set(owner, current - 1);
|
|
1672
|
+
}
|
|
1673
|
+
if (pending?.get(key)?.generation === generation)
|
|
1674
|
+
pending.delete(key);
|
|
1675
|
+
});
|
|
1676
|
+
}
|
|
1677
|
+
catch (error) {
|
|
1678
|
+
if (owner !== null && typeof owner === "object") {
|
|
1679
|
+
const current = grokBudgetInFlightCounts.get(owner) ?? 0;
|
|
1680
|
+
if (current <= 1)
|
|
1681
|
+
grokBudgetInFlightCounts.delete(owner);
|
|
1682
|
+
else
|
|
1683
|
+
grokBudgetInFlightCounts.set(owner, current - 1);
|
|
1684
|
+
}
|
|
1685
|
+
throw error;
|
|
1686
|
+
}
|
|
1687
|
+
if (pending) {
|
|
1688
|
+
pending.set(key, { signature, generation, promise });
|
|
1689
|
+
}
|
|
1690
|
+
return;
|
|
1691
|
+
}
|
|
1692
|
+
const generation = nextGrokBudgetGeneration(ctx, scope, key);
|
|
1693
|
+
contextMap(stores.pending, ctx)?.delete(key);
|
|
1694
|
+
let warning;
|
|
1695
|
+
try {
|
|
1696
|
+
warning = diagnoseGrokContextBudget(ctx, route, {
|
|
1697
|
+
requestMaxTokens: options?.requestMaxTokens,
|
|
1698
|
+
});
|
|
1699
|
+
}
|
|
1700
|
+
catch {
|
|
1701
|
+
return;
|
|
1702
|
+
}
|
|
1703
|
+
recordGrokBudgetWarning(ctx, route, warning, { scope, key, expectedGeneration: generation });
|
|
1704
|
+
}
|
|
1705
|
+
async function* managedGrokNativeSearchStream(options, routeConfig, nativeSearch, ctx, prefetchedModelFacts) {
|
|
1147
1706
|
const route = currentRoute(options, routeConfig);
|
|
1148
|
-
const nativeReplayRoute = grokReplayRoute(route, routeConfig);
|
|
1149
1707
|
try {
|
|
1150
1708
|
if (options.stop !== undefined)
|
|
1151
1709
|
throw Object.assign(new Error("LCX Responses does not support GenerateOptions.stop"), { code: "LCX_RESPONSES_UNSUPPORTED_OPTION" });
|
|
1710
|
+
assertValidTemperature(options.temperature);
|
|
1711
|
+
assertValidPositiveMaxTokens(options.maxTokens);
|
|
1152
1712
|
const projected = projectToolUpdates(options.messages, options.tools, undefined, options.toolHistory);
|
|
1153
1713
|
const visibleTools = grokVisibleFunctionTools(projected.tools, nativeSearch);
|
|
1154
1714
|
const declaredToolNames = new Set(visibleTools?.map((tool) => tool.name) ?? []);
|
|
1155
|
-
const
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1715
|
+
const modelFacts = prefetchedModelFacts ?? (await resolveRouteModelFacts(ctx, route, options.signal));
|
|
1716
|
+
if (!modelFacts.resolved && hasMidHistoryPromptSnapshot(projected.messages))
|
|
1717
|
+
throw Object.assign(new Error("LCX could not read the route's model information, so in-history system prompt updates cannot be sent safely; retry once the route is available"), { code: "LCX_SYSTEM_PROMPT_MODE_UNKNOWN" });
|
|
1718
|
+
const modelOverride = routeModelOverride(routeConfig, modelFacts, true);
|
|
1719
|
+
const preflightBaseModel = resolvePiResponsesModel({
|
|
1720
|
+
imageSupport: modelFacts.imageSupport,
|
|
1721
|
+
route: routeConfig.profileDefaultMaxTokens === undefined && routeConfig.catalogMaxTokens === undefined
|
|
1722
|
+
? route
|
|
1723
|
+
: {
|
|
1724
|
+
...route,
|
|
1725
|
+
...(routeConfig.catalogMaxTokens === undefined ? {} : { catalogMaxTokens: routeConfig.catalogMaxTokens }),
|
|
1726
|
+
...(routeConfig.profileDefaultMaxTokens === undefined ? {} : { profileDefaultMaxTokens: routeConfig.profileDefaultMaxTokens }),
|
|
1727
|
+
},
|
|
1728
|
+
responsesCompat: routeConfig.responsesCompat,
|
|
1729
|
+
...(modelOverride === undefined ? {} : { model: modelOverride }),
|
|
1160
1730
|
});
|
|
1161
|
-
const cacheSessionId = grokPromptCacheSessionId(route, routeConfig);
|
|
1162
|
-
const headers = await authenticatedGrokHeaders(ctx, routeConfig, cacheSessionId, providerDynamicHeaders(routeConfig.provider, options.messages), route.sessionId || undefined);
|
|
1163
1731
|
const model = {
|
|
1164
|
-
...
|
|
1732
|
+
...preflightBaseModel,
|
|
1165
1733
|
...routeConfig.modelDefaults,
|
|
1166
1734
|
...routeConfig.modelControls,
|
|
1735
|
+
...(routeConfig.responsesCompat === undefined
|
|
1736
|
+
? {}
|
|
1737
|
+
: {
|
|
1738
|
+
compat: {
|
|
1739
|
+
...(isRecord(preflightBaseModel.compat) ? preflightBaseModel.compat : {}),
|
|
1740
|
+
...routeConfig.responsesCompat,
|
|
1741
|
+
},
|
|
1742
|
+
}),
|
|
1167
1743
|
id: route.model,
|
|
1168
1744
|
provider: route.provider,
|
|
1169
|
-
baseUrl: routeConfig.baseURL,
|
|
1745
|
+
baseUrl: routeConfig.rawBaseURL ?? routeConfig.baseURL,
|
|
1170
1746
|
api: "openai-responses",
|
|
1171
1747
|
};
|
|
1172
|
-
const effectiveReasoning = options.reasoningEffort
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1748
|
+
const effectiveReasoning = resolveEffectiveReasoningEffort(options.reasoningEffort, modelFacts, routeConfig);
|
|
1749
|
+
assertSupportedReasoningEffort(effectiveReasoning, modelFacts, model, routeConfig);
|
|
1750
|
+
// Reuse the single `resolveModelInfo` answer instead of querying DSH twice.
|
|
1751
|
+
recordGrokBudgetFromModelFacts(ctx, route, modelFacts, options.maxTokens);
|
|
1752
|
+
const cacheSessionId = grokPromptCacheSessionId(route, routeConfig);
|
|
1753
|
+
const apiKey = await resolveApiKey(ctx, routeConfig);
|
|
1754
|
+
const { headers, replayAuthorityHeaders, headerAuthorityFingerprint: grokHeaderAuthorityFingerprint, } = await authenticatedGrokRequestHeaders(ctx, routeConfig, cacheSessionId, providerDynamicHeaders(routeConfig.provider, options.messages), route.sessionId || undefined, options, apiKey);
|
|
1755
|
+
const nativeReplayRoute = grokReplayRoute(route, {
|
|
1756
|
+
apiKeyEnv: routeConfig.apiKeyEnv,
|
|
1757
|
+
headers: replayAuthorityHeaders,
|
|
1758
|
+
});
|
|
1759
|
+
const prepared = await serializeNativeAware(projected.messages, route, routeConfig, ctx, {
|
|
1760
|
+
signal: options.signal,
|
|
1761
|
+
system: options.system,
|
|
1762
|
+
tools: visibleTools,
|
|
1763
|
+
grokNativeReplayRoute: nativeReplayRoute,
|
|
1764
|
+
resolveHeaderAuthorityFingerprint: async () => grokHeaderAuthorityFingerprint,
|
|
1765
|
+
prefetchedModelFacts: modelFacts,
|
|
1766
|
+
});
|
|
1767
|
+
const effectiveMaxTokens = resolveEffectiveMaxTokens(options.maxTokens, prepared.modelFacts, model, prepared.piContext);
|
|
1177
1768
|
const body = buildResponsesBody({
|
|
1178
1769
|
model,
|
|
1179
1770
|
input: prepared.input,
|
|
@@ -1183,7 +1774,9 @@ async function* managedGrokNativeSearchStream(options, routeConfig, nativeSearch
|
|
|
1183
1774
|
cacheRetention: routeConfig.cacheRetention,
|
|
1184
1775
|
reasoningEffort: effectiveReasoning,
|
|
1185
1776
|
temperature: options.temperature,
|
|
1186
|
-
maxTokens:
|
|
1777
|
+
maxTokens: effectiveMaxTokens,
|
|
1778
|
+
gatewayCompatibilityFallback: routeConfig.gatewayCompatibilityFallback === true,
|
|
1779
|
+
omitUnsupportedFields: isChatGPTSignInCredential(model, apiKey),
|
|
1187
1780
|
});
|
|
1188
1781
|
if (nativeSearch.web) {
|
|
1189
1782
|
const include = new Set(Array.isArray(body.include) ? body.include : []);
|
|
@@ -1207,6 +1800,8 @@ async function* managedGrokNativeSearchStream(options, routeConfig, nativeSearch
|
|
|
1207
1800
|
serverToolTypes: GROK_NATIVE_SERVER_TOOL_TYPES,
|
|
1208
1801
|
isServerToolItem: (item) => isGrokNativeServerToolItem(item, nativeSearch, declaredToolNames),
|
|
1209
1802
|
declaredToolNames,
|
|
1803
|
+
sessionId: route.sessionId || undefined,
|
|
1804
|
+
headerAuthorityFingerprint: grokHeaderAuthorityFingerprint,
|
|
1210
1805
|
nativeReplayRoute,
|
|
1211
1806
|
onFailure: (info) => logStreamFailure(ctx, "grok-native", info),
|
|
1212
1807
|
onServerToolUsage(usage) {
|
|
@@ -1244,8 +1839,8 @@ async function* hostRecognizedRouteStream(options, ctx, policy, next) {
|
|
|
1244
1839
|
return;
|
|
1245
1840
|
}
|
|
1246
1841
|
yield* options.purpose === "compaction"
|
|
1247
|
-
? remoteCompactionStream(options, routeWithPolicies(routeConfig, policy()), ctx, next)
|
|
1248
|
-
: managedResponsesStream(options, routeWithPolicies(routeConfig, policy()), ctx);
|
|
1842
|
+
? remoteCompactionStream(options, routeWithPolicies(routeConfig, policy()), ctx, next, facts)
|
|
1843
|
+
: managedResponsesStream(options, routeWithPolicies(routeConfig, policy()), ctx, facts);
|
|
1249
1844
|
}
|
|
1250
1845
|
/** The Grok counterpart of `hostRecognizedRouteStream`: DSH not resolving the model is DSH's to report. */
|
|
1251
1846
|
async function* hostRecognizedGrokStream(options, ctx, policy, nativeSearch, next) {
|
|
@@ -1253,9 +1848,12 @@ async function* hostRecognizedGrokStream(options, ctx, policy, nativeSearch, nex
|
|
|
1253
1848
|
if (facts.resolved)
|
|
1254
1849
|
markHostRecognizedModel(ctx, options.provider, options.model);
|
|
1255
1850
|
const grokRoute = facts.resolved ? resolveGrokResponsesRouteConfig(ctx, options, policy()) : undefined;
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1851
|
+
if (!grokRoute) {
|
|
1852
|
+
recordGrokBudgetFromModelFacts(ctx, { provider: String(options.provider ?? ""), model: String(options.model ?? "") }, facts, options.maxTokens);
|
|
1853
|
+
yield* next();
|
|
1854
|
+
return;
|
|
1855
|
+
}
|
|
1856
|
+
yield* managedGrokNativeSearchStream(options, routeWithPolicies(grokRoute, policy()), nativeSearch, ctx, facts);
|
|
1259
1857
|
}
|
|
1260
1858
|
function installInjected(ctx, configInput) {
|
|
1261
1859
|
installSearchUsage(ctx);
|
|
@@ -1277,6 +1875,10 @@ function installInjected(ctx, configInput) {
|
|
|
1277
1875
|
const alphaToolRegistrations = new Map();
|
|
1278
1876
|
const compactionPatchRecords = new Map();
|
|
1279
1877
|
const syncAgentTools = (agent) => {
|
|
1878
|
+
try {
|
|
1879
|
+
reportGrokBudgetDiagnosis(ctx, selectedAgentRoute(agent));
|
|
1880
|
+
}
|
|
1881
|
+
catch { }
|
|
1280
1882
|
syncGptToolsForAgent(ctx, agent, state, () => runtimeConfig, provider, gptToolRegistrations);
|
|
1281
1883
|
syncAlphaToolForAgent(ctx, agent, state, () => runtimeConfig, capabilityStore, refStore, alphaToolRegistrations);
|
|
1282
1884
|
};
|
|
@@ -1310,10 +1912,15 @@ function installInjected(ctx, configInput) {
|
|
|
1310
1912
|
state.grokNativeWebSearch = configInput.grokNativeWebSearch.get();
|
|
1311
1913
|
state.grokNativeXSearch = configInput.grokNativeXSearch.get();
|
|
1312
1914
|
// Absent (older config shapes) means the default: addition-only. Only an explicit false selects the full list.
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1915
|
+
const toolUpdates = configInput.incrementalToolUpdates?.get() === false
|
|
1916
|
+
? "full"
|
|
1917
|
+
: DEFAULT_TOOL_UPDATES;
|
|
1918
|
+
const gatewayCompatibilityFallback = configInput.gatewayCompatibilityFallback?.get() === true;
|
|
1919
|
+
runtimeConfig = {
|
|
1920
|
+
...baseConfig,
|
|
1921
|
+
toolUpdates,
|
|
1922
|
+
gatewayCompatibilityFallback,
|
|
1923
|
+
};
|
|
1317
1924
|
refreshTools();
|
|
1318
1925
|
};
|
|
1319
1926
|
/**
|
|
@@ -1387,12 +1994,29 @@ function installInjected(ctx, configInput) {
|
|
|
1387
1994
|
web: state.grokNativeWebSearch,
|
|
1388
1995
|
x: state.grokNativeXSearch,
|
|
1389
1996
|
};
|
|
1997
|
+
const grokRoute = options.purpose === undefined && grokNativeSearchEnabled(grokNativeSearch)
|
|
1998
|
+
? resolveGrokResponsesRouteConfig(ctx, options, runtimeConfig)
|
|
1999
|
+
: undefined;
|
|
2000
|
+
const hostGrokPending = options.purpose === undefined &&
|
|
2001
|
+
grokNativeSearchEnabled(grokNativeSearch) &&
|
|
2002
|
+
!grokRoute &&
|
|
2003
|
+
isGrokModelId(options.model) &&
|
|
2004
|
+
diagnoseResponsesRoute(ctx, options) === "api-unresolved";
|
|
2005
|
+
const streamHandlesGrokFacts = Boolean(grokRoute) || hostGrokPending;
|
|
2006
|
+
if (!(streamHandlesGrokFacts && grokRouteMayNeedHostBudgetLookup(ctx, options, options.maxTokens))) {
|
|
2007
|
+
reportGrokBudgetDiagnosis(ctx, {
|
|
2008
|
+
provider: String(options.provider ?? ""),
|
|
2009
|
+
model: String(options.model ?? ""),
|
|
2010
|
+
}, {
|
|
2011
|
+
requestMaxTokens: options.maxTokens,
|
|
2012
|
+
skipAsyncLookup: streamHandlesGrokFacts,
|
|
2013
|
+
});
|
|
2014
|
+
}
|
|
1390
2015
|
if (options.purpose === undefined && grokNativeSearchEnabled(grokNativeSearch)) {
|
|
1391
|
-
const grokRoute = resolveGrokResponsesRouteConfig(ctx, options, runtimeConfig);
|
|
1392
2016
|
if (grokRoute)
|
|
1393
2017
|
return managedGrokNativeSearchStream(options, routeWithPolicies(grokRoute, runtimeConfig), grokNativeSearch, ctx);
|
|
1394
2018
|
// A Grok model only DSH's own catalog may know: ask DSH now, so the first request is not left to a race.
|
|
1395
|
-
if (
|
|
2019
|
+
if (hostGrokPending)
|
|
1396
2020
|
return hostRecognizedGrokStream(options, ctx, () => runtimeConfig, grokNativeSearch, next);
|
|
1397
2021
|
}
|
|
1398
2022
|
if (!state.enabled || !isGptLifecycleTarget(options))
|