dsh-lcx-codex 0.6.0 → 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-compat.js +5 -5
- package/lib/dsh-responses.js +268 -44
- package/lib/grok-native-search.js +18 -1
- package/lib/index.js +704 -73
- 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 +40 -7
- package/lib/route.js +358 -38
- package/lib/search-usage.js +7 -5
- package/lib/tool-surface.js +41 -8
- package/lib/types/index.d.ts +8 -0
- package/package.json +40 -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"];
|
|
@@ -37,6 +37,8 @@ const ADVANCED_HOSTED_TOOL = "websearch_gpt_advanced";
|
|
|
37
37
|
const ALPHA_TOOL = "websearch_alpha";
|
|
38
38
|
const hostedSearchRouteContext = new AsyncLocalStorage();
|
|
39
39
|
const WEB_SEARCH_TIMEOUT_MS = 240_000;
|
|
40
|
+
// DSH 0.2.2 defaults to 800; its root export does not expose this constant.
|
|
41
|
+
const WEB_SEARCH_SUMMARY_MAX_CHARS = 800;
|
|
40
42
|
const AUTO_COMPACTION_THRESHOLD_PERCENT = 90;
|
|
41
43
|
const EMERGENCY_PRUNE_THRESHOLD_PERCENT = 95;
|
|
42
44
|
function positiveInteger(value, fallback, maximum) {
|
|
@@ -55,12 +57,16 @@ function routeWithPolicies(route, config) {
|
|
|
55
57
|
alphaMaxOutputTokens: config.alphaMaxOutputTokens,
|
|
56
58
|
assistantRetentionPerMessageTokenCap: config.assistantRetentionPerMessageTokenCap,
|
|
57
59
|
assistantRetentionTokenReserve: config.assistantRetentionTokenReserve,
|
|
60
|
+
gatewayCompatibilityFallback: config.gatewayCompatibilityFallback,
|
|
58
61
|
maxRequestImageBytes: route.maxRequestImageBytes ?? config.maxRequestImageBytes,
|
|
59
62
|
maxResponseBytes: config.maxResponseBytes,
|
|
60
63
|
nativeRetentionTokenBudget: config.nativeRetentionTokenBudget,
|
|
61
64
|
portableReplayMaxChars: config.portableReplayMaxChars,
|
|
62
65
|
requestImageMaxBytes: route.requestImageMaxBytes ?? config.requestImageMaxBytes,
|
|
63
66
|
requestImagePixelBudget: route.requestImagePixelBudget ?? config.requestImagePixelBudget,
|
|
67
|
+
responsesCompat: config.promptCacheKeyMode === "disabled" || config.promptCacheKeyMode === "enabled"
|
|
68
|
+
? { ...(route.responsesCompat ?? {}), promptCacheKeyMode: config.promptCacheKeyMode }
|
|
69
|
+
: route.responsesCompat,
|
|
64
70
|
toolUpdates: config.toolUpdates,
|
|
65
71
|
webMaxResults: config.webMaxResults,
|
|
66
72
|
};
|
|
@@ -103,8 +109,10 @@ export const Config = z.object({
|
|
|
103
109
|
grokNativeXSearch: z.boolean().default(false).volatile(),
|
|
104
110
|
searchMediaPreview: z.boolean().default(false).volatile(),
|
|
105
111
|
incrementalToolUpdates: z.boolean().default(true).volatile(),
|
|
112
|
+
gatewayCompatibilityFallback: z.boolean().default(DEFAULTS.gatewayCompatibilityFallback).volatile(),
|
|
106
113
|
supportsLongCacheRetention: z.boolean().default(false),
|
|
107
114
|
supportsExplicitPromptCacheMode: z.boolean().default(false),
|
|
115
|
+
promptCacheKeyMode: z.union(["auto", "disabled", "enabled"]).default("auto"),
|
|
108
116
|
alphaCapabilityPath: z.string().default(""),
|
|
109
117
|
alphaRefPath: z.string().default(""),
|
|
110
118
|
alphaProfile: z.string().default(""),
|
|
@@ -129,6 +137,12 @@ function normalizeConfig(input = {}) {
|
|
|
129
137
|
return {
|
|
130
138
|
supportsLongCacheRetention: input.supportsLongCacheRetention === true,
|
|
131
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),
|
|
132
146
|
alphaCapabilityPath: stringValue(input.alphaCapabilityPath, defaultAlphaCapabilityPath()),
|
|
133
147
|
alphaRefPath: stringValue(input.alphaRefPath, defaultAlphaRefPath()),
|
|
134
148
|
alphaProfile: String(input.alphaProfile ?? ""),
|
|
@@ -188,6 +202,8 @@ function requestImageOptions(routeConfig, imageSupport, signal, imageMap, extra
|
|
|
188
202
|
signal,
|
|
189
203
|
imageMap,
|
|
190
204
|
maxRequestImageBytes: routeConfig.maxRequestImageBytes,
|
|
205
|
+
maxRequestImages: routeConfig.maxRequestImages,
|
|
206
|
+
imageOffloadCountQuantum: routeConfig.imageOffloadCountQuantum,
|
|
191
207
|
requestImagePixelBudget: routeConfig.requestImagePixelBudget,
|
|
192
208
|
requestImageMaxBytes: routeConfig.requestImageMaxBytes,
|
|
193
209
|
...extra,
|
|
@@ -197,9 +213,11 @@ async function executeHostedSearch(ctx, routeConfig, args, signal, sessionId = "
|
|
|
197
213
|
const normalized = normalizeHostedSearchArgs(args);
|
|
198
214
|
const requestId = randomUUID();
|
|
199
215
|
const route = currentRoute({ provider: routeConfig.provider, model: routeConfig.model, sessionId }, routeConfig);
|
|
200
|
-
const
|
|
201
|
-
const
|
|
202
|
-
|
|
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)}`)
|
|
203
221
|
: undefined;
|
|
204
222
|
const body = buildHostedSearchBody(normalized, routeConfig.model, {
|
|
205
223
|
promptCacheKey: searchCacheKey,
|
|
@@ -266,7 +284,7 @@ function createScopedGptWebSearchTool(ctx, state, getConfig, provider, fetchEnab
|
|
|
266
284
|
section: () => () => { },
|
|
267
285
|
},
|
|
268
286
|
};
|
|
269
|
-
applyWebSearchTool(composition, getConfig().webMaxResults, WEB_SEARCH_MAX_QUERIES, WEB_SEARCH_TIMEOUT_MS, fetchEnabled);
|
|
287
|
+
applyWebSearchTool(composition, getConfig().webMaxResults, WEB_SEARCH_MAX_QUERIES, WEB_SEARCH_SUMMARY_MAX_CHARS, WEB_SEARCH_TIMEOUT_MS, fetchEnabled);
|
|
270
288
|
if (!definition)
|
|
271
289
|
throw new Error("DSH web_search composition did not register a tool");
|
|
272
290
|
const execute = definition.execute.bind(definition);
|
|
@@ -577,14 +595,134 @@ function locateSupersededItems(original, restored, positions) {
|
|
|
577
595
|
}
|
|
578
596
|
return located;
|
|
579
597
|
}
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
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)
|
|
586
705
|
return;
|
|
587
|
-
|
|
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
|
+
}
|
|
588
726
|
}
|
|
589
727
|
const reportedToolSurfaceFallbacks = new Set();
|
|
590
728
|
/**
|
|
@@ -623,12 +761,56 @@ function reportCatalogGap(ctx, route) {
|
|
|
623
761
|
reportedCatalogGaps.add(key);
|
|
624
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`);
|
|
625
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
|
+
}
|
|
626
808
|
async function serializeNativeAware(rawMessages, route, routeConfig, ctx, options) {
|
|
627
809
|
const session = sessionFor(ctx, route.sessionId);
|
|
628
810
|
// One adapter-owned lookup supplies image support and the route's system-prompt update mode. On an
|
|
629
811
|
// "in-history" route DSH appends a changed prompt as a later full system message; it must reach the
|
|
630
812
|
// model in place with instruction authority instead of being demoted to user context.
|
|
631
|
-
const modelFacts = await resolveRouteModelFacts(ctx, route, options.signal);
|
|
813
|
+
const modelFacts = options.prefetchedModelFacts ?? (await resolveRouteModelFacts(ctx, route, options.signal));
|
|
632
814
|
if (options.grokNativeReplayRoute === undefined)
|
|
633
815
|
reportCatalogGap(ctx, route);
|
|
634
816
|
const { imageSupport, systemPromptUpdate, resolved } = modelFacts;
|
|
@@ -636,9 +818,12 @@ async function serializeNativeAware(rawMessages, route, routeConfig, ctx, option
|
|
|
636
818
|
let messages = rawMessages;
|
|
637
819
|
let tools = options.tools;
|
|
638
820
|
let anchoredTools = false;
|
|
821
|
+
const gatewayCompatibilityFallback = routeConfig.gatewayCompatibilityFallback === true;
|
|
639
822
|
if (options.toolSurface !== undefined) {
|
|
640
823
|
const surface = planToolSurface({
|
|
641
|
-
mode:
|
|
824
|
+
mode: gatewayCompatibilityFallback
|
|
825
|
+
? "full"
|
|
826
|
+
: (routeConfig.toolUpdates ?? DEFAULT_TOOL_UPDATES),
|
|
642
827
|
messages: rawMessages,
|
|
643
828
|
tools: options.tools,
|
|
644
829
|
toolHistory: options.toolSurface.history,
|
|
@@ -663,18 +848,52 @@ async function serializeNativeAware(rawMessages, route, routeConfig, ctx, option
|
|
|
663
848
|
let grammarToolInputProperties;
|
|
664
849
|
/** Absolute input positions rendered from prompt snapshots superseded by a checkpoint. */
|
|
665
850
|
const supersededInstructionItems = [];
|
|
666
|
-
const
|
|
667
|
-
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,
|
|
668
860
|
responsesCompat: routeConfig.responsesCompat,
|
|
669
861
|
systemPromptUpdate,
|
|
670
862
|
...(anchoredTools ? { toolUpdate: "addition-only" } : {}),
|
|
863
|
+
...(gatewayCompatibilityFallback ? { gatewayCompatibilityFallback: true } : {}),
|
|
671
864
|
...(modelOverride === undefined ? {} : { model: modelOverride }),
|
|
672
865
|
...extra,
|
|
673
866
|
});
|
|
674
867
|
const { systemPrompt, messages: surfaceMessages } = splitSystemPrompt(options.system, messages);
|
|
675
868
|
// Resolve the descriptor before selecting effort. This conversion has no tool-bearing transcript.
|
|
676
|
-
const descriptor = await serializeDshMessages([], ctx,
|
|
677
|
-
|
|
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
|
+
});
|
|
678
897
|
const nonce = `lcx-request-boundary:${globalThis.crypto.randomUUID()}:`;
|
|
679
898
|
const marker = (id) => ({
|
|
680
899
|
role: "user", content: [{ type: "text", text: nonce + id }],
|
|
@@ -683,7 +902,7 @@ async function serializeNativeAware(rawMessages, route, routeConfig, ctx, option
|
|
|
683
902
|
// the entire tool-bearing surface; Pi alone owns declarations, removals and incremental notices.
|
|
684
903
|
const transcript = [];
|
|
685
904
|
const replacements = new Map();
|
|
686
|
-
|
|
905
|
+
const replacementContexts = new Map();
|
|
687
906
|
for (const message of surfaceMessages) {
|
|
688
907
|
assertSupportedCheckpointMessage(message);
|
|
689
908
|
const checkpointId = session && isPersistedMessage(message) ? compactCheckpointId(message) : undefined;
|
|
@@ -693,10 +912,11 @@ async function serializeNativeAware(rawMessages, route, routeConfig, ctx, option
|
|
|
693
912
|
continue;
|
|
694
913
|
}
|
|
695
914
|
let items;
|
|
696
|
-
if (stateRouteCompatible(state,
|
|
915
|
+
if (stateRouteCompatible(state, effectiveRoute, ctx)) {
|
|
697
916
|
const hydratedMap = new Map();
|
|
698
917
|
items = responseInputItems(await hydrateNativeImageReferences(state.nativeOutput, ctx, requestImageOptions(routeConfig, imageSupport, options.signal, hydratedMap)));
|
|
699
918
|
mergeMap(imageMap, hydratedMap);
|
|
919
|
+
replacementContexts.set(String(replacements.size), estimateItemsToPiMessages(items));
|
|
700
920
|
}
|
|
701
921
|
else {
|
|
702
922
|
const portable = await portableMessagesForCheckpoint(ctx, session, checkpointId, {
|
|
@@ -706,18 +926,26 @@ async function serializeNativeAware(rawMessages, route, routeConfig, ctx, option
|
|
|
706
926
|
// the current series' surface or regain instruction authority on a different route.
|
|
707
927
|
const serialized = await serializeDshMessages(portable, ctx, serializeOptions({
|
|
708
928
|
systemPromptUpdate: undefined, toolUpdate: undefined, tools: undefined,
|
|
929
|
+
assistantReplayScope: { disallowAllNativeReplay: true },
|
|
930
|
+
// Fixed replay images cannot be offloaded by the surface owner; check the assembled request below.
|
|
931
|
+
maxRequestImageBytes: Number.MAX_SAFE_INTEGER, maxRequestImages: Number.MAX_SAFE_INTEGER,
|
|
709
932
|
}));
|
|
710
933
|
items = serialized.input;
|
|
711
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);
|
|
712
942
|
}
|
|
713
|
-
for (const bytes of inputImageBase64Lengths(items))
|
|
714
|
-
fixedImageBytes += bytes;
|
|
715
943
|
const id = String(replacements.size);
|
|
716
944
|
replacements.set(id, items);
|
|
717
945
|
transcript.push(marker(id));
|
|
718
946
|
}
|
|
719
947
|
const serialized = await serializeDshMessages(transcript, ctx, serializeOptions({
|
|
720
|
-
systemPrompt,
|
|
948
|
+
systemPrompt, tracePrelude: true,
|
|
721
949
|
...(configurationUpdateEffort === undefined ? {} : { configurationUpdateEffort }),
|
|
722
950
|
}));
|
|
723
951
|
const surfaceImageBytes = inputImageBase64Lengths(serialized.input);
|
|
@@ -725,7 +953,71 @@ async function serializeNativeAware(rawMessages, route, routeConfig, ctx, option
|
|
|
725
953
|
nativeModel = serialized.model;
|
|
726
954
|
grammarToolInputProperties = serialized.grammarToolInputProperties;
|
|
727
955
|
mergeMap(imageMap, serialized.imageMap);
|
|
728
|
-
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
|
+
};
|
|
729
1021
|
const snapshotPositions = new Set(locateSupersededItems(serialized.input, restored, serialized.supersededInstructionItems));
|
|
730
1022
|
const ephemeralPreludeItemCount = serialized.preludeItemCount;
|
|
731
1023
|
const seen = new Set();
|
|
@@ -746,11 +1038,15 @@ async function serializeNativeAware(rawMessages, route, routeConfig, ctx, option
|
|
|
746
1038
|
}
|
|
747
1039
|
if (ephemeralPreludeItemCount < 0 || seen.size !== replacements.size || JSON.stringify(input).includes(nonce))
|
|
748
1040
|
throw Object.assign(new Error("LCX could not remove request boundary markers"), { code: "LCX_RESPONSES_INVALID_INPUT" });
|
|
1041
|
+
const assembledImages = inputImageBase64Lengths(input);
|
|
749
1042
|
assertAggregateImageBudget({
|
|
750
1043
|
maxBytes: Number.isSafeInteger(routeConfig.maxRequestImageBytes) && Number(routeConfig.maxRequestImageBytes) > 0
|
|
751
1044
|
? Number(routeConfig.maxRequestImageBytes)
|
|
752
1045
|
: DEFAULT_MAX_REQUEST_IMAGE_BYTES,
|
|
753
|
-
fixedBytes:
|
|
1046
|
+
fixedBytes: assembledImages.reduce((sum, bytes) => sum + bytes, 0) - surfaceImageBytes.reduce((sum, bytes) => sum + bytes, 0),
|
|
1047
|
+
fixedCount: assembledImages.length - surfaceImageBytes.length,
|
|
1048
|
+
maxImages: routeConfig.maxRequestImages,
|
|
1049
|
+
countQuantum: routeConfig.imageOffloadCountQuantum,
|
|
754
1050
|
surfaceBytes: surfaceImageBytes,
|
|
755
1051
|
});
|
|
756
1052
|
if (!nativeModel)
|
|
@@ -761,14 +1057,17 @@ async function serializeNativeAware(rawMessages, route, routeConfig, ctx, option
|
|
|
761
1057
|
return {
|
|
762
1058
|
input,
|
|
763
1059
|
configurationUpdateEffort,
|
|
1060
|
+
providerThinkingLevel,
|
|
764
1061
|
ephemeralPreludeItemCount,
|
|
765
1062
|
supersededInstructionItems,
|
|
766
1063
|
imageMap,
|
|
767
1064
|
imageSupport,
|
|
768
1065
|
tools: nativeTools,
|
|
769
1066
|
model: nativeModel,
|
|
1067
|
+
piContext,
|
|
770
1068
|
grammarToolInputProperties,
|
|
771
1069
|
modelFacts,
|
|
1070
|
+
headerAuthorityFingerprint,
|
|
772
1071
|
};
|
|
773
1072
|
}
|
|
774
1073
|
function fallbackEligible(error, signal) {
|
|
@@ -781,19 +1080,57 @@ function fallbackEligible(error, signal) {
|
|
|
781
1080
|
return true;
|
|
782
1081
|
return ["LCX_HTTP_RETRYABLE", "LCX_RETRY_EXHAUSTED"].includes(String(details.code ?? ""));
|
|
783
1082
|
}
|
|
784
|
-
async function* remoteCompactionStream(options, routeConfig, ctx, next) {
|
|
1083
|
+
async function* remoteCompactionStream(options, routeConfig, ctx, next, prefetchedModelFacts) {
|
|
785
1084
|
const history = stripCompactionDirective(options.messages);
|
|
786
1085
|
const route = currentRoute(options, routeConfig);
|
|
787
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;
|
|
788
1112
|
const prepared = await serializeNativeAware(history, route, routeConfig, ctx, {
|
|
789
1113
|
signal: options.signal,
|
|
790
1114
|
system: options.system,
|
|
791
1115
|
tools: options.tools && [...options.tools],
|
|
792
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,
|
|
793
1126
|
});
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
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();
|
|
797
1134
|
const result = await requestNativeCompaction({
|
|
798
1135
|
baseURL: routeConfig.baseURL,
|
|
799
1136
|
model: route.model,
|
|
@@ -803,9 +1140,11 @@ async function* remoteCompactionStream(options, routeConfig, ctx, next) {
|
|
|
803
1140
|
promptCacheKey: promptCacheKey(route, routeConfig, ctx),
|
|
804
1141
|
promptCacheRetention: promptCacheRetention(routeConfig),
|
|
805
1142
|
cacheRetention: routeConfig.cacheRetention,
|
|
806
|
-
reasoningEffort:
|
|
807
|
-
temperature:
|
|
808
|
-
maxTokens:
|
|
1143
|
+
reasoningEffort: effectiveCompactionReasoning,
|
|
1144
|
+
temperature: effectiveCompactionTemperature,
|
|
1145
|
+
maxTokens: effectiveCompactionMaxTokens,
|
|
1146
|
+
gatewayCompatibilityFallback: routeConfig.gatewayCompatibilityFallback === true,
|
|
1147
|
+
omitUnsupportedFields: isChatGPTSignInCredential(prepared.model, apiKey),
|
|
809
1148
|
idempotencyKey: randomUUID(),
|
|
810
1149
|
headers,
|
|
811
1150
|
signal: options.signal,
|
|
@@ -819,7 +1158,9 @@ async function* remoteCompactionStream(options, routeConfig, ctx, next) {
|
|
|
819
1158
|
const block = await createNativeCheckpointBlock({
|
|
820
1159
|
ctx,
|
|
821
1160
|
session,
|
|
822
|
-
route
|
|
1161
|
+
route: prepared.headerAuthorityFingerprint === undefined
|
|
1162
|
+
? route
|
|
1163
|
+
: { ...route, headerAuthorityFingerprint: prepared.headerAuthorityFingerprint },
|
|
823
1164
|
result,
|
|
824
1165
|
input: prepared.input,
|
|
825
1166
|
ephemeralPreludeItemCount: prepared.ephemeralPreludeItemCount,
|
|
@@ -1083,21 +1424,63 @@ function logStreamFailure(ctx, route, info) {
|
|
|
1083
1424
|
].filter(Boolean);
|
|
1084
1425
|
ctx.logger?.warn?.(`[lcx-codex] responses stream failed: ${parts.join(" ")}`);
|
|
1085
1426
|
}
|
|
1086
|
-
async function* managedResponsesStream(options, routeConfig, ctx) {
|
|
1427
|
+
async function* managedResponsesStream(options, routeConfig, ctx, prefetchedModelFacts) {
|
|
1087
1428
|
const route = currentRoute(options, routeConfig);
|
|
1088
1429
|
try {
|
|
1089
1430
|
if (options.stop !== undefined)
|
|
1090
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
|
+
})());
|
|
1091
1456
|
const prepared = await serializeNativeAware(options.messages, route, routeConfig, ctx, {
|
|
1092
1457
|
signal: options.signal,
|
|
1093
1458
|
system: options.system,
|
|
1094
1459
|
tools: options.tools && [...options.tools],
|
|
1095
1460
|
toolSurface: { history: options.toolHistory },
|
|
1096
|
-
|
|
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
|
+
},
|
|
1097
1479
|
});
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
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();
|
|
1101
1484
|
const body = buildResponsesBody({
|
|
1102
1485
|
model: prepared.model,
|
|
1103
1486
|
input: prepared.input,
|
|
@@ -1106,9 +1489,11 @@ async function* managedResponsesStream(options, routeConfig, ctx) {
|
|
|
1106
1489
|
promptCacheKey: promptCacheKey(route, routeConfig, ctx),
|
|
1107
1490
|
promptCacheRetention: promptCacheRetention(routeConfig),
|
|
1108
1491
|
cacheRetention: routeConfig.cacheRetention,
|
|
1109
|
-
reasoningEffort:
|
|
1492
|
+
reasoningEffort: effectiveReasoningEffort,
|
|
1110
1493
|
temperature: options.temperature,
|
|
1111
|
-
maxTokens:
|
|
1494
|
+
maxTokens: effectiveMaxTokens,
|
|
1495
|
+
gatewayCompatibilityFallback: routeConfig.gatewayCompatibilityFallback === true,
|
|
1496
|
+
omitUnsupportedFields: isChatGPTSignInCredential(prepared.model, apiKey),
|
|
1112
1497
|
...(prepared.configurationUpdateEffort === undefined ? {} : { configurationUpdateEffort: prepared.configurationUpdateEffort }),
|
|
1113
1498
|
});
|
|
1114
1499
|
const nativeReplay = inputHasNativeState(prepared.input);
|
|
@@ -1120,6 +1505,8 @@ async function* managedResponsesStream(options, routeConfig, ctx) {
|
|
|
1120
1505
|
baseURL: routeConfig.baseURL,
|
|
1121
1506
|
provider: route.provider,
|
|
1122
1507
|
model: route.model,
|
|
1508
|
+
sessionId: route.sessionId || undefined,
|
|
1509
|
+
headerAuthorityFingerprint,
|
|
1123
1510
|
piModel: prepared.model,
|
|
1124
1511
|
body,
|
|
1125
1512
|
grammarToolInputProperties: prepared.grammarToolInputProperties,
|
|
@@ -1128,7 +1515,7 @@ async function* managedResponsesStream(options, routeConfig, ctx) {
|
|
|
1128
1515
|
timeoutMs: routeConfig.timeoutMs,
|
|
1129
1516
|
maxAttempts: 1,
|
|
1130
1517
|
maxResponseBytes: routeConfig.maxResponseBytes,
|
|
1131
|
-
...(prepared.
|
|
1518
|
+
...(prepared.providerThinkingLevel === undefined ? {} : { providerThinkingLevel: prepared.providerThinkingLevel }),
|
|
1132
1519
|
onFailure: (info) => logStreamFailure(ctx, "gpt", info),
|
|
1133
1520
|
});
|
|
1134
1521
|
}
|
|
@@ -1136,37 +1523,248 @@ async function* managedResponsesStream(options, routeConfig, ctx) {
|
|
|
1136
1523
|
yield managedFailureChunk(error, options.signal);
|
|
1137
1524
|
}
|
|
1138
1525
|
}
|
|
1139
|
-
|
|
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) {
|
|
1140
1706
|
const route = currentRoute(options, routeConfig);
|
|
1141
|
-
const nativeReplayRoute = grokReplayRoute(route, routeConfig);
|
|
1142
1707
|
try {
|
|
1143
1708
|
if (options.stop !== undefined)
|
|
1144
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);
|
|
1145
1712
|
const projected = projectToolUpdates(options.messages, options.tools, undefined, options.toolHistory);
|
|
1146
1713
|
const visibleTools = grokVisibleFunctionTools(projected.tools, nativeSearch);
|
|
1147
1714
|
const declaredToolNames = new Set(visibleTools?.map((tool) => tool.name) ?? []);
|
|
1148
|
-
const
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
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 }),
|
|
1153
1730
|
});
|
|
1154
|
-
const cacheSessionId = grokPromptCacheSessionId(route, routeConfig);
|
|
1155
|
-
const headers = await authenticatedGrokHeaders(ctx, routeConfig, cacheSessionId, providerDynamicHeaders(routeConfig.provider, options.messages), route.sessionId || undefined);
|
|
1156
1731
|
const model = {
|
|
1157
|
-
...
|
|
1732
|
+
...preflightBaseModel,
|
|
1158
1733
|
...routeConfig.modelDefaults,
|
|
1159
1734
|
...routeConfig.modelControls,
|
|
1735
|
+
...(routeConfig.responsesCompat === undefined
|
|
1736
|
+
? {}
|
|
1737
|
+
: {
|
|
1738
|
+
compat: {
|
|
1739
|
+
...(isRecord(preflightBaseModel.compat) ? preflightBaseModel.compat : {}),
|
|
1740
|
+
...routeConfig.responsesCompat,
|
|
1741
|
+
},
|
|
1742
|
+
}),
|
|
1160
1743
|
id: route.model,
|
|
1161
1744
|
provider: route.provider,
|
|
1162
|
-
baseUrl: routeConfig.baseURL,
|
|
1745
|
+
baseUrl: routeConfig.rawBaseURL ?? routeConfig.baseURL,
|
|
1163
1746
|
api: "openai-responses",
|
|
1164
1747
|
};
|
|
1165
|
-
const effectiveReasoning = options.reasoningEffort
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
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);
|
|
1170
1768
|
const body = buildResponsesBody({
|
|
1171
1769
|
model,
|
|
1172
1770
|
input: prepared.input,
|
|
@@ -1176,7 +1774,9 @@ async function* managedGrokNativeSearchStream(options, routeConfig, nativeSearch
|
|
|
1176
1774
|
cacheRetention: routeConfig.cacheRetention,
|
|
1177
1775
|
reasoningEffort: effectiveReasoning,
|
|
1178
1776
|
temperature: options.temperature,
|
|
1179
|
-
maxTokens:
|
|
1777
|
+
maxTokens: effectiveMaxTokens,
|
|
1778
|
+
gatewayCompatibilityFallback: routeConfig.gatewayCompatibilityFallback === true,
|
|
1779
|
+
omitUnsupportedFields: isChatGPTSignInCredential(model, apiKey),
|
|
1180
1780
|
});
|
|
1181
1781
|
if (nativeSearch.web) {
|
|
1182
1782
|
const include = new Set(Array.isArray(body.include) ? body.include : []);
|
|
@@ -1200,6 +1800,8 @@ async function* managedGrokNativeSearchStream(options, routeConfig, nativeSearch
|
|
|
1200
1800
|
serverToolTypes: GROK_NATIVE_SERVER_TOOL_TYPES,
|
|
1201
1801
|
isServerToolItem: (item) => isGrokNativeServerToolItem(item, nativeSearch, declaredToolNames),
|
|
1202
1802
|
declaredToolNames,
|
|
1803
|
+
sessionId: route.sessionId || undefined,
|
|
1804
|
+
headerAuthorityFingerprint: grokHeaderAuthorityFingerprint,
|
|
1203
1805
|
nativeReplayRoute,
|
|
1204
1806
|
onFailure: (info) => logStreamFailure(ctx, "grok-native", info),
|
|
1205
1807
|
onServerToolUsage(usage) {
|
|
@@ -1237,8 +1839,8 @@ async function* hostRecognizedRouteStream(options, ctx, policy, next) {
|
|
|
1237
1839
|
return;
|
|
1238
1840
|
}
|
|
1239
1841
|
yield* options.purpose === "compaction"
|
|
1240
|
-
? remoteCompactionStream(options, routeWithPolicies(routeConfig, policy()), ctx, next)
|
|
1241
|
-
: managedResponsesStream(options, routeWithPolicies(routeConfig, policy()), ctx);
|
|
1842
|
+
? remoteCompactionStream(options, routeWithPolicies(routeConfig, policy()), ctx, next, facts)
|
|
1843
|
+
: managedResponsesStream(options, routeWithPolicies(routeConfig, policy()), ctx, facts);
|
|
1242
1844
|
}
|
|
1243
1845
|
/** The Grok counterpart of `hostRecognizedRouteStream`: DSH not resolving the model is DSH's to report. */
|
|
1244
1846
|
async function* hostRecognizedGrokStream(options, ctx, policy, nativeSearch, next) {
|
|
@@ -1246,9 +1848,12 @@ async function* hostRecognizedGrokStream(options, ctx, policy, nativeSearch, nex
|
|
|
1246
1848
|
if (facts.resolved)
|
|
1247
1849
|
markHostRecognizedModel(ctx, options.provider, options.model);
|
|
1248
1850
|
const grokRoute = facts.resolved ? resolveGrokResponsesRouteConfig(ctx, options, policy()) : undefined;
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
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);
|
|
1252
1857
|
}
|
|
1253
1858
|
function installInjected(ctx, configInput) {
|
|
1254
1859
|
installSearchUsage(ctx);
|
|
@@ -1270,6 +1875,10 @@ function installInjected(ctx, configInput) {
|
|
|
1270
1875
|
const alphaToolRegistrations = new Map();
|
|
1271
1876
|
const compactionPatchRecords = new Map();
|
|
1272
1877
|
const syncAgentTools = (agent) => {
|
|
1878
|
+
try {
|
|
1879
|
+
reportGrokBudgetDiagnosis(ctx, selectedAgentRoute(agent));
|
|
1880
|
+
}
|
|
1881
|
+
catch { }
|
|
1273
1882
|
syncGptToolsForAgent(ctx, agent, state, () => runtimeConfig, provider, gptToolRegistrations);
|
|
1274
1883
|
syncAlphaToolForAgent(ctx, agent, state, () => runtimeConfig, capabilityStore, refStore, alphaToolRegistrations);
|
|
1275
1884
|
};
|
|
@@ -1303,10 +1912,15 @@ function installInjected(ctx, configInput) {
|
|
|
1303
1912
|
state.grokNativeWebSearch = configInput.grokNativeWebSearch.get();
|
|
1304
1913
|
state.grokNativeXSearch = configInput.grokNativeXSearch.get();
|
|
1305
1914
|
// Absent (older config shapes) means the default: addition-only. Only an explicit false selects the full list.
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
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
|
+
};
|
|
1310
1924
|
refreshTools();
|
|
1311
1925
|
};
|
|
1312
1926
|
/**
|
|
@@ -1380,12 +1994,29 @@ function installInjected(ctx, configInput) {
|
|
|
1380
1994
|
web: state.grokNativeWebSearch,
|
|
1381
1995
|
x: state.grokNativeXSearch,
|
|
1382
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
|
+
}
|
|
1383
2015
|
if (options.purpose === undefined && grokNativeSearchEnabled(grokNativeSearch)) {
|
|
1384
|
-
const grokRoute = resolveGrokResponsesRouteConfig(ctx, options, runtimeConfig);
|
|
1385
2016
|
if (grokRoute)
|
|
1386
2017
|
return managedGrokNativeSearchStream(options, routeWithPolicies(grokRoute, runtimeConfig), grokNativeSearch, ctx);
|
|
1387
2018
|
// A Grok model only DSH's own catalog may know: ask DSH now, so the first request is not left to a race.
|
|
1388
|
-
if (
|
|
2019
|
+
if (hostGrokPending)
|
|
1389
2020
|
return hostRecognizedGrokStream(options, ctx, () => runtimeConfig, grokNativeSearch, next);
|
|
1390
2021
|
}
|
|
1391
2022
|
if (!state.enabled || !isGptLifecycleTarget(options))
|