dsh-lcx-codex 0.5.2 → 0.6.0-pre.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +16 -9
- package/THIRD_PARTY_NOTICES.md +3 -2
- package/icon.svg +1 -0
- package/lib/abort-error.js +12 -1
- package/lib/client.js +137 -39
- package/lib/compact-v2.js +16 -56
- package/lib/defaults.js +15 -0
- package/lib/dsh-compat.js +15 -76
- package/lib/dsh-responses.js +172 -71
- package/lib/grok-native-search.js +1 -3
- package/lib/index.js +228 -78
- package/lib/model-catalog.js +5 -7
- package/lib/native-checkpoint.js +57 -182
- package/lib/pi-responses-runtime.js +140 -50
- package/lib/provider-headers.js +31 -0
- package/lib/responses-request.js +4 -12
- package/lib/responses-stream.js +116 -218
- package/lib/route.js +169 -75
- package/lib/search-accounting.js +19 -21
- package/lib/token-budget.js +6 -41
- package/lib/tool-surface.js +106 -0
- package/lib/transport.js +89 -107
- package/lib/types/client/search-media.d.ts +0 -2
- package/lib/types/index.d.ts +4 -0
- package/lib/util.js +21 -0
- package/lib/web-run-output.js +0 -3
- package/lib/web-search-alpha.js +22 -27
- package/lib/web-search-capability.js +1 -3
- package/lib/web-search-hosted.js +2 -2
- package/lib/web-search-ref-store.js +1 -3
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +37 -32
- package/lib/pi086-responses-allowlist.js +0 -131
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { createHash } from "node:crypto";
|
|
2
|
+
import { isRecord } from "./util.js";
|
|
2
3
|
export const GROK_NATIVE_SERVER_TOOL_TYPES = new Set([
|
|
3
4
|
"web_search_call",
|
|
4
5
|
"x_search_call",
|
|
@@ -28,9 +29,6 @@ const GROK_CITATION_ID_MARKER = /([ \t]?)\{render_inline_citation:citation_id=(\
|
|
|
28
29
|
const GROK_UI_CITATION_MARKER = /render_inline_citation[^\r\n]*[ \t]*/giu;
|
|
29
30
|
const GROK_PRIVATE_RENDER_MARKER = /[\uE000-\uF8FF]render_inline_citation[^\uE000-\uF8FF\r\n]*[\uE000-\uF8FF][ \t]*/giu;
|
|
30
31
|
const GROK_EOS_MARKER = /<\|eos\|>/giu;
|
|
31
|
-
function isRecord(value) {
|
|
32
|
-
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
33
|
-
}
|
|
34
32
|
function citationId(annotation) {
|
|
35
33
|
const value = annotation.citation_id ?? annotation.citationId ?? annotation.id;
|
|
36
34
|
return typeof value === "string" || typeof value === "number"
|
package/lib/index.js
CHANGED
|
@@ -12,11 +12,13 @@ import { join } from "node:path";
|
|
|
12
12
|
import { AsyncLocalStorage } from "node:async_hooks";
|
|
13
13
|
import { getSupportedThinkingLevels } from "./pi-responses-runtime.js";
|
|
14
14
|
import { fetchJsonWithRetry } from "./transport.js";
|
|
15
|
+
import { DEFAULT_TOOL_UPDATES, planToolSurface, withoutDeferLoading } from "./tool-surface.js";
|
|
15
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";
|
|
16
17
|
import { InvocationPolicyScope } from "./invocation-policy-scope.js";
|
|
18
|
+
import { providerDynamicHeaders } from "./provider-headers.js";
|
|
17
19
|
import { resolveCatalogModel } from "./model-catalog.js";
|
|
18
|
-
import { authenticatedGrokHeaders, authenticatedHeaders, currentRoute, generationControlsFromSession, grokPromptCacheSessionId, promptCacheKey, promptCacheRetention, promptCacheSessionId, diagnoseResponsesRoute,
|
|
19
|
-
import { DEFAULT_MAX_REQUEST_IMAGE_BYTES, DEFAULT_REQUEST_IMAGE_PIXEL_BUDGET, DEFAULT_REQUEST_IMAGE_MAX_BYTES, assertAggregateImageBudget, hydrateNativeImageReferences, inputImageBase64Lengths, resolveRouteModelFacts, responseInputItems, serializeDshMessages, } from "./dsh-responses.js";
|
|
20
|
+
import { authenticatedGrokHeaders, authenticatedHeaders, currentRoute, generationControlsFromSession, grokPromptCacheSessionId, promptCacheKey, promptCacheRetention, promptCacheSessionId, resolveGrokResponsesRouteConfig, diagnoseResponsesRoute, markHostRecognizedModel, warmHostRecognizedModels, 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, routeModelOverride, hasMidHistoryPromptSnapshot, responseInputItems, serializeDshMessages, } from "./dsh-responses.js";
|
|
20
22
|
import { mergeFeatureHeader, requestNativeCompaction } from "./compact-v2.js";
|
|
21
23
|
import { buildResponsesBody } from "./responses-request.js";
|
|
22
24
|
import { managedFailureChunk, streamResponsesRequest, } from "./responses-stream.js";
|
|
@@ -28,6 +30,7 @@ import { AlphaCapabilityStore, alphaCapabilityFingerprint, alphaCapabilityUsable
|
|
|
28
30
|
import { AlphaRefStore } from "./web-search-ref-store.js";
|
|
29
31
|
import { ServiceMutex } from "./service-mutex.js";
|
|
30
32
|
import { GROK_NATIVE_SERVER_TOOL_TYPES, grokNativeSearchEnabled, grokVisibleFunctionTools, isGrokNativeServerToolItem, grokWireTools, restoreGrokNativeReplay, } from "./grok-native-search.js";
|
|
33
|
+
import { DEFAULTS } from "./defaults.js";
|
|
31
34
|
export const name = "lcx-codex";
|
|
32
35
|
export const inject = ["llm", "web", "sessions", "tools", "settings", "credentials", "attachments", "fs"];
|
|
33
36
|
const ADVANCED_HOSTED_TOOL = "websearch_gpt_advanced";
|
|
@@ -58,6 +61,7 @@ function routeWithPolicies(route, config) {
|
|
|
58
61
|
portableReplayMaxChars: config.portableReplayMaxChars,
|
|
59
62
|
requestImageMaxBytes: route.requestImageMaxBytes ?? config.requestImageMaxBytes,
|
|
60
63
|
requestImagePixelBudget: route.requestImagePixelBudget ?? config.requestImagePixelBudget,
|
|
64
|
+
toolUpdates: config.toolUpdates,
|
|
61
65
|
webMaxResults: config.webMaxResults,
|
|
62
66
|
};
|
|
63
67
|
}
|
|
@@ -71,9 +75,6 @@ function grokReplayRoute(route, config) {
|
|
|
71
75
|
function isRecord(value) {
|
|
72
76
|
return value !== null && typeof value === "object";
|
|
73
77
|
}
|
|
74
|
-
function asAbortSignal(value) {
|
|
75
|
-
return value instanceof AbortSignal ? value : undefined;
|
|
76
|
-
}
|
|
77
78
|
function errorDetails(value) {
|
|
78
79
|
return isRecord(value)
|
|
79
80
|
? {
|
|
@@ -101,27 +102,28 @@ export const Config = z.object({
|
|
|
101
102
|
grokNativeWebSearch: z.boolean().default(false).volatile(),
|
|
102
103
|
grokNativeXSearch: z.boolean().default(false).volatile(),
|
|
103
104
|
searchMediaPreview: z.boolean().default(false).volatile(),
|
|
105
|
+
incrementalToolUpdates: z.boolean().default(true).volatile(),
|
|
104
106
|
supportsLongCacheRetention: z.boolean().default(false),
|
|
105
107
|
supportsExplicitPromptCacheMode: z.boolean().default(false),
|
|
106
108
|
alphaCapabilityPath: z.string().default(""),
|
|
107
109
|
alphaRefPath: z.string().default(""),
|
|
108
110
|
alphaProfile: z.string().default(""),
|
|
109
111
|
alphaGroup: z.string().default(""),
|
|
110
|
-
alphaMaxOutputTokens: z.number().default(
|
|
111
|
-
webSearchProvider: z.string().default(
|
|
112
|
-
webMaxResults: z.number().default(
|
|
113
|
-
timeoutMs: z.number().default(
|
|
114
|
-
maxResponseBytes: z.number().default(
|
|
115
|
-
maxAttempts: z.number().default(
|
|
112
|
+
alphaMaxOutputTokens: z.number().default(DEFAULTS.alphaMaxOutputTokens),
|
|
113
|
+
webSearchProvider: z.string().default(DEFAULTS.webSearchProvider),
|
|
114
|
+
webMaxResults: z.number().default(DEFAULTS.webMaxResults),
|
|
115
|
+
timeoutMs: z.number().default(DEFAULTS.requestTimeoutMs),
|
|
116
|
+
maxResponseBytes: z.number().default(DEFAULTS.maxResponseBytes),
|
|
117
|
+
maxAttempts: z.number().default(DEFAULTS.maxAttempts),
|
|
116
118
|
maxRequestImageBytes: z.number().default(DEFAULT_MAX_REQUEST_IMAGE_BYTES),
|
|
117
119
|
requestImagePixelBudget: z
|
|
118
120
|
.number()
|
|
119
121
|
.default(DEFAULT_REQUEST_IMAGE_PIXEL_BUDGET),
|
|
120
122
|
requestImageMaxBytes: z.number().default(DEFAULT_REQUEST_IMAGE_MAX_BYTES),
|
|
121
|
-
portableReplayMaxChars: z.number().default(
|
|
122
|
-
nativeRetentionTokenBudget: z.number().default(
|
|
123
|
-
assistantRetentionTokenReserve: z.number().default(
|
|
124
|
-
assistantRetentionPerMessageTokenCap: z.number().default(
|
|
123
|
+
portableReplayMaxChars: z.number().default(DEFAULTS.portableReplayMaxChars),
|
|
124
|
+
nativeRetentionTokenBudget: z.number().default(DEFAULTS.nativeRetentionTokenBudget),
|
|
125
|
+
assistantRetentionTokenReserve: z.number().default(DEFAULTS.assistantRetentionTokenReserve),
|
|
126
|
+
assistantRetentionPerMessageTokenCap: z.number().default(DEFAULTS.assistantRetentionPerMessageTokenCap),
|
|
125
127
|
});
|
|
126
128
|
function normalizeConfig(input = {}) {
|
|
127
129
|
return {
|
|
@@ -131,23 +133,24 @@ function normalizeConfig(input = {}) {
|
|
|
131
133
|
alphaRefPath: stringValue(input.alphaRefPath, defaultAlphaRefPath()),
|
|
132
134
|
alphaProfile: String(input.alphaProfile ?? ""),
|
|
133
135
|
alphaGroup: String(input.alphaGroup ?? ""),
|
|
134
|
-
alphaMaxOutputTokens: positiveInteger(input.alphaMaxOutputTokens,
|
|
135
|
-
webSearchProvider: stringValue(input.webSearchProvider,
|
|
136
|
-
webMaxResults: positiveInteger(input.webMaxResults,
|
|
137
|
-
timeoutMs: positiveInteger(input.timeoutMs,
|
|
138
|
-
maxResponseBytes: positiveInteger(input.maxResponseBytes,
|
|
139
|
-
maxAttempts: positiveInteger(input.maxAttempts,
|
|
136
|
+
alphaMaxOutputTokens: positiveInteger(input.alphaMaxOutputTokens, DEFAULTS.alphaMaxOutputTokens, DEFAULTS.alphaMaxOutputTokensCap),
|
|
137
|
+
webSearchProvider: stringValue(input.webSearchProvider, DEFAULTS.webSearchProvider),
|
|
138
|
+
webMaxResults: positiveInteger(input.webMaxResults, DEFAULTS.webMaxResults),
|
|
139
|
+
timeoutMs: positiveInteger(input.timeoutMs, DEFAULTS.requestTimeoutMs),
|
|
140
|
+
maxResponseBytes: positiveInteger(input.maxResponseBytes, DEFAULTS.maxResponseBytes),
|
|
141
|
+
maxAttempts: positiveInteger(input.maxAttempts, DEFAULTS.maxAttempts, DEFAULTS.maxAttemptsCap),
|
|
140
142
|
maxRequestImageBytes: positiveInteger(input.maxRequestImageBytes, DEFAULT_MAX_REQUEST_IMAGE_BYTES),
|
|
141
143
|
requestImagePixelBudget: positiveInteger(input.requestImagePixelBudget, DEFAULT_REQUEST_IMAGE_PIXEL_BUDGET),
|
|
142
144
|
requestImageMaxBytes: positiveInteger(input.requestImageMaxBytes, DEFAULT_REQUEST_IMAGE_MAX_BYTES),
|
|
143
|
-
portableReplayMaxChars: positiveInteger(input.portableReplayMaxChars,
|
|
144
|
-
nativeRetentionTokenBudget: positiveInteger(input.nativeRetentionTokenBudget,
|
|
145
|
+
portableReplayMaxChars: positiveInteger(input.portableReplayMaxChars, DEFAULTS.portableReplayMaxChars),
|
|
146
|
+
nativeRetentionTokenBudget: positiveInteger(input.nativeRetentionTokenBudget, DEFAULTS.nativeRetentionTokenBudget),
|
|
145
147
|
assistantRetentionTokenReserve: typeof input.assistantRetentionTokenReserve === "number" &&
|
|
146
148
|
Number.isSafeInteger(input.assistantRetentionTokenReserve) &&
|
|
147
149
|
input.assistantRetentionTokenReserve >= 0
|
|
148
150
|
? input.assistantRetentionTokenReserve
|
|
149
|
-
:
|
|
150
|
-
assistantRetentionPerMessageTokenCap: positiveInteger(input.assistantRetentionPerMessageTokenCap,
|
|
151
|
+
: DEFAULTS.assistantRetentionTokenReserve,
|
|
152
|
+
assistantRetentionPerMessageTokenCap: positiveInteger(input.assistantRetentionPerMessageTokenCap, DEFAULTS.assistantRetentionPerMessageTokenCap),
|
|
153
|
+
toolUpdates: DEFAULT_TOOL_UPDATES,
|
|
151
154
|
};
|
|
152
155
|
}
|
|
153
156
|
function webError(message, code, cause) {
|
|
@@ -224,7 +227,8 @@ class LcxResponsesSearchProvider {
|
|
|
224
227
|
if (!active)
|
|
225
228
|
return false;
|
|
226
229
|
const config = this.getConfig();
|
|
227
|
-
|
|
230
|
+
// Tool-assembly question, answered before DSH can be asked: see `assumeHostServes` in route.ts.
|
|
231
|
+
const route = resolveResponsesRouteConfig(this.ctx, active, config, true);
|
|
228
232
|
return this.enabled() && Boolean(route);
|
|
229
233
|
}
|
|
230
234
|
async search(request, signal) {
|
|
@@ -318,7 +322,9 @@ function disposeGptToolsForAgent(agent, registrations) {
|
|
|
318
322
|
function syncGptToolsForAgent(ctx, agent, state, getConfig, provider, registrations) {
|
|
319
323
|
const config = getConfig();
|
|
320
324
|
const active = selectedAgentRoute(agent);
|
|
321
|
-
|
|
325
|
+
// Registering the tools cannot wait for DSH's answer about a model only its own catalog knows: the optimistic
|
|
326
|
+
// answer is exact (see `assumeHostServes` in route.ts), and the stream awaits DSH before LCX takes the request.
|
|
327
|
+
const route = resolveResponsesRouteConfig(ctx, active, config, true);
|
|
322
328
|
const webSearch = state.enabled && state.webSearch && Boolean(route);
|
|
323
329
|
const advanced = webSearch && state.advancedHostedSearch;
|
|
324
330
|
if (!webSearch && !advanced) {
|
|
@@ -551,19 +557,25 @@ function mergeMap(target, source) {
|
|
|
551
557
|
target.set(key, value);
|
|
552
558
|
}
|
|
553
559
|
/**
|
|
554
|
-
*
|
|
555
|
-
*
|
|
556
|
-
*
|
|
557
|
-
* catalog entry follows what DSH itself resolved for it instead of being assumed to reason.
|
|
560
|
+
* Follow traced item positions through the Grok replay restoration, which may splice items. When the
|
|
561
|
+
* item count is unchanged the positions are exact; otherwise each traced item is found again by exact
|
|
562
|
+
* structural equality in order (restoration never alters instruction items).
|
|
558
563
|
*/
|
|
559
|
-
function
|
|
560
|
-
if (
|
|
561
|
-
return
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
564
|
+
function locateSupersededItems(original, restored, positions) {
|
|
565
|
+
if (positions.length === 0 || original.length === restored.length)
|
|
566
|
+
return [...positions];
|
|
567
|
+
const located = [];
|
|
568
|
+
let cursor = 0;
|
|
569
|
+
for (const position of positions) {
|
|
570
|
+
const wanted = JSON.stringify(original[position]);
|
|
571
|
+
while (cursor < restored.length && JSON.stringify(restored[cursor]) !== wanted)
|
|
572
|
+
cursor += 1;
|
|
573
|
+
if (cursor >= restored.length)
|
|
574
|
+
throw Object.assign(new Error("LCX lost track of a superseded system prompt item"), { code: "LCX_RESPONSES_INVALID_INPUT" });
|
|
575
|
+
located.push(cursor);
|
|
576
|
+
cursor += 1;
|
|
577
|
+
}
|
|
578
|
+
return located;
|
|
567
579
|
}
|
|
568
580
|
/** Refuse an effort DSH does not offer for this model, the way the DSH adapter and the Grok route do. */
|
|
569
581
|
function assertSupportedReasoningEffort(effort, facts) {
|
|
@@ -574,6 +586,26 @@ function assertSupportedReasoningEffort(effort, facts) {
|
|
|
574
586
|
return;
|
|
575
587
|
throw Object.assign(new Error(`Responses route does not support reasoning effort "${String(effort)}"`), { code: "LCX_RESPONSES_UNSUPPORTED_OPTION" });
|
|
576
588
|
}
|
|
589
|
+
const reportedToolSurfaceFallbacks = new Set();
|
|
590
|
+
/**
|
|
591
|
+
* Say why a request carries the complete tool list although the policy asked for addition-only updates.
|
|
592
|
+
* One line per session and reason: a warning when the host declared addition-only (LCX should have been able to
|
|
593
|
+
* follow), a debug line otherwise. The reasons are fixed phrases; no request content, key or header is logged.
|
|
594
|
+
*/
|
|
595
|
+
function reportToolSurfaceFallback(ctx, route, surface) {
|
|
596
|
+
const reason = surface.fallbackReason ?? "unknown";
|
|
597
|
+
const key = `${route.sessionId}\u001f${reason}`;
|
|
598
|
+
if (reportedToolSurfaceFallbacks.has(key))
|
|
599
|
+
return;
|
|
600
|
+
if (reportedToolSurfaceFallbacks.size >= 256)
|
|
601
|
+
reportedToolSurfaceFallbacks.clear();
|
|
602
|
+
reportedToolSurfaceFallbacks.add(key);
|
|
603
|
+
const message = `[lcx-codex] tool updates: sending the complete tool list on ${route.provider}/${route.model} (${reason})`;
|
|
604
|
+
if (surface.fallbackUnexpected === true)
|
|
605
|
+
ctx.logger?.warn?.(message);
|
|
606
|
+
else
|
|
607
|
+
ctx.logger?.debug?.(message);
|
|
608
|
+
}
|
|
577
609
|
const reportedCatalogGaps = new Set();
|
|
578
610
|
/**
|
|
579
611
|
* Say once per route model that LCX's bundled Pi catalog does not describe it, so its wire capabilities
|
|
@@ -581,7 +613,7 @@ const reportedCatalogGaps = new Set();
|
|
|
581
613
|
*/
|
|
582
614
|
function reportCatalogGap(ctx, route) {
|
|
583
615
|
const source = resolveCatalogModel(route.provider, route.model, true).source;
|
|
584
|
-
if (source !== "catalog-provider-default"
|
|
616
|
+
if (source !== "catalog-provider-default")
|
|
585
617
|
return;
|
|
586
618
|
const key = `${route.provider}/${route.model}`;
|
|
587
619
|
if (reportedCatalogGaps.has(key))
|
|
@@ -591,25 +623,57 @@ function reportCatalogGap(ctx, route) {
|
|
|
591
623
|
reportedCatalogGaps.add(key);
|
|
592
624
|
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`);
|
|
593
625
|
}
|
|
594
|
-
async function serializeNativeAware(
|
|
626
|
+
async function serializeNativeAware(rawMessages, route, routeConfig, ctx, options) {
|
|
595
627
|
const session = sessionFor(ctx, route.sessionId);
|
|
628
|
+
// One adapter-owned lookup supplies image support and the route's system-prompt update mode. On an
|
|
629
|
+
// "in-history" route DSH appends a changed prompt as a later full system message; it must reach the
|
|
630
|
+
// model in place with instruction authority instead of being demoted to user context.
|
|
596
631
|
const modelFacts = await resolveRouteModelFacts(ctx, route, options.signal);
|
|
597
632
|
if (options.grokNativeReplayRoute === undefined)
|
|
598
633
|
reportCatalogGap(ctx, route);
|
|
599
|
-
const { imageSupport } = modelFacts;
|
|
600
|
-
const modelOverride = options.grokNativeReplayRoute
|
|
601
|
-
|
|
602
|
-
|
|
634
|
+
const { imageSupport, systemPromptUpdate, resolved } = modelFacts;
|
|
635
|
+
const modelOverride = routeModelOverride(routeConfig, modelFacts, options.grokNativeReplayRoute !== undefined);
|
|
636
|
+
let messages = rawMessages;
|
|
637
|
+
let tools = options.tools;
|
|
638
|
+
let anchoredTools = false;
|
|
639
|
+
let toolSurfaceCompat;
|
|
640
|
+
if (options.toolSurface !== undefined) {
|
|
641
|
+
const surface = planToolSurface({
|
|
642
|
+
mode: routeConfig.toolUpdates ?? DEFAULT_TOOL_UPDATES,
|
|
643
|
+
messages: rawMessages,
|
|
644
|
+
tools: options.tools,
|
|
645
|
+
toolHistory: options.toolSurface.history,
|
|
646
|
+
facts: modelFacts,
|
|
647
|
+
compat: routeConfig.responsesCompat,
|
|
648
|
+
});
|
|
649
|
+
messages = surface.messages;
|
|
650
|
+
tools = surface.tools;
|
|
651
|
+
anchoredTools = surface.anchored;
|
|
652
|
+
toolSurfaceCompat = surface.compat;
|
|
653
|
+
if (surface.fallbackReason !== undefined)
|
|
654
|
+
reportToolSurfaceFallback(ctx, route, surface);
|
|
655
|
+
}
|
|
656
|
+
// An unreadable model mode must not demote instructions: history that holds a DSH prompt snapshot after
|
|
657
|
+
// the head can only come from an in-history route, and sending it as user text would silently lower its
|
|
658
|
+
// authority. Without such history the conservative default is harmless, so ordinary requests proceed.
|
|
659
|
+
if (!resolved && hasMidHistoryPromptSnapshot(messages))
|
|
660
|
+
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" });
|
|
603
661
|
const input = [];
|
|
604
662
|
const imageMap = new Map();
|
|
605
663
|
let nativeTools;
|
|
606
664
|
let nativeModel;
|
|
607
665
|
let grammarToolInputProperties;
|
|
608
666
|
let normal = [];
|
|
667
|
+
/** Absolute `input` positions rendered from DSH prompt snapshots that a checkpoint supersedes. */
|
|
668
|
+
const supersededInstructionItems = [];
|
|
609
669
|
const serializeOptions = (imageMapOverride = undefined, extra = {}) => requestImageOptions(routeConfig, imageSupport, options.signal, imageMapOverride, {
|
|
610
670
|
route,
|
|
611
|
-
tools
|
|
612
|
-
responsesCompat:
|
|
671
|
+
tools,
|
|
672
|
+
responsesCompat: toolSurfaceCompat === undefined
|
|
673
|
+
? routeConfig.responsesCompat
|
|
674
|
+
: { ...routeConfig.responsesCompat, ...toolSurfaceCompat },
|
|
675
|
+
systemPromptUpdate,
|
|
676
|
+
...(anchoredTools ? { toolUpdate: "addition-only" } : {}),
|
|
613
677
|
...(modelOverride === undefined ? {} : { model: modelOverride }),
|
|
614
678
|
...extra,
|
|
615
679
|
});
|
|
@@ -640,11 +704,18 @@ async function serializeNativeAware(messages, route, routeConfig, ctx, options)
|
|
|
640
704
|
normal = [];
|
|
641
705
|
const serialized = await serializeDshMessages(batch, ctx, serializeOptions());
|
|
642
706
|
surfaceImageBytes.push(...inputImageBase64Lengths(serialized.input));
|
|
707
|
+
// Every batch derives its request tools from the same immediate declarations; a difference would
|
|
708
|
+
// change the cached prefix between batches of one request.
|
|
709
|
+
if (anchoredTools && JSON.stringify(serialized.tools) !== JSON.stringify(nativeTools))
|
|
710
|
+
throw Object.assign(new Error("LCX anchored tool declarations diverged between serialization batches"), { code: "LCX_RESPONSES_INVALID_INPUT" });
|
|
643
711
|
nativeTools = serialized.tools ?? nativeTools;
|
|
644
712
|
nativeModel = serialized.model ?? nativeModel;
|
|
645
713
|
grammarToolInputProperties =
|
|
646
714
|
serialized.grammarToolInputProperties ?? grammarToolInputProperties;
|
|
647
|
-
|
|
715
|
+
const restored = restoreGrokNativeReplay(serialized.input, batch.filter(isPersistedMessage), options.grokNativeReplayRoute);
|
|
716
|
+
const base = input.length;
|
|
717
|
+
supersededInstructionItems.push(...locateSupersededItems(serialized.input, restored, serialized.supersededInstructionItems).map((position) => base + position));
|
|
718
|
+
input.push(...restored);
|
|
648
719
|
mergeMap(imageMap, serialized.imageMap);
|
|
649
720
|
};
|
|
650
721
|
for (const message of surfaceMessages ?? []) {
|
|
@@ -671,7 +742,17 @@ async function serializeNativeAware(messages, route, routeConfig, ctx, options)
|
|
|
671
742
|
}
|
|
672
743
|
else {
|
|
673
744
|
const portable = await portableMessagesForCheckpoint(ctx, session, checkpointId, { maxChars: routeConfig.portableReplayMaxChars });
|
|
674
|
-
|
|
745
|
+
// Shadowed history is superseded by the head DSH re-rendered when the compaction began its new
|
|
746
|
+
// series, so its old system messages stay ordinary context instead of regaining instruction
|
|
747
|
+
// authority behind the current prompt.
|
|
748
|
+
const serialized = await serializeDshMessages(portable, ctx,
|
|
749
|
+
// Portable history is plain context: it carries no developer updates, and deferred declarations are
|
|
750
|
+
// only meaningful to the anchored surface itself.
|
|
751
|
+
serializeOptions(undefined, {
|
|
752
|
+
systemPromptUpdate: undefined,
|
|
753
|
+
toolUpdate: undefined,
|
|
754
|
+
tools: tools?.map((tool) => withoutDeferLoading(tool)),
|
|
755
|
+
}));
|
|
675
756
|
for (const bytes of inputImageBase64Lengths(serialized.input))
|
|
676
757
|
fixedImageBytes += bytes;
|
|
677
758
|
input.push(...serialized.input);
|
|
@@ -694,6 +775,7 @@ async function serializeNativeAware(messages, route, routeConfig, ctx, options)
|
|
|
694
775
|
return {
|
|
695
776
|
input,
|
|
696
777
|
ephemeralPreludeItemCount,
|
|
778
|
+
supersededInstructionItems,
|
|
697
779
|
imageMap,
|
|
698
780
|
imageSupport,
|
|
699
781
|
tools: nativeTools,
|
|
@@ -713,20 +795,23 @@ function fallbackEligible(error, signal) {
|
|
|
713
795
|
return ["LCX_HTTP_RETRYABLE", "LCX_RETRY_EXHAUSTED"].includes(String(details.code ?? ""));
|
|
714
796
|
}
|
|
715
797
|
async function* remoteCompactionStream(options, routeConfig, ctx, next) {
|
|
716
|
-
const
|
|
717
|
-
const history = stripCompactionDirective(projected.messages);
|
|
798
|
+
const history = stripCompactionDirective(options.messages);
|
|
718
799
|
const route = currentRoute(options, routeConfig);
|
|
719
800
|
try {
|
|
720
|
-
const prepared = await serializeNativeAware(history, route, routeConfig, ctx, {
|
|
801
|
+
const prepared = await serializeNativeAware(history, route, routeConfig, ctx, {
|
|
802
|
+
signal: options.signal,
|
|
803
|
+
tools: options.tools && [...options.tools],
|
|
804
|
+
toolSurface: { history: options.toolHistory },
|
|
805
|
+
});
|
|
721
806
|
const cacheSessionId = promptCacheSessionId(route, routeConfig, ctx);
|
|
722
|
-
const headers = await authenticatedHeaders(ctx, routeConfig, cacheSessionId, cacheSessionId === undefined ? null : route.sessionId);
|
|
807
|
+
const headers = await authenticatedHeaders(ctx, routeConfig, cacheSessionId, cacheSessionId === undefined ? null : route.sessionId, providerDynamicHeaders(routeConfig.provider, options.messages), route.sessionId || undefined);
|
|
723
808
|
const generation = generationControlsFromSession(sessionFor(ctx, route.sessionId), route);
|
|
724
809
|
const result = await requestNativeCompaction({
|
|
725
810
|
baseURL: routeConfig.baseURL,
|
|
726
811
|
model: route.model,
|
|
727
812
|
modelDescriptor: prepared.model,
|
|
728
813
|
input: prepared.input,
|
|
729
|
-
tools: prepared.tools
|
|
814
|
+
tools: prepared.tools,
|
|
730
815
|
promptCacheKey: promptCacheKey(route, routeConfig, ctx),
|
|
731
816
|
promptCacheRetention: promptCacheRetention(routeConfig),
|
|
732
817
|
cacheRetention: routeConfig.cacheRetention,
|
|
@@ -750,6 +835,7 @@ async function* remoteCompactionStream(options, routeConfig, ctx, next) {
|
|
|
750
835
|
result,
|
|
751
836
|
input: prepared.input,
|
|
752
837
|
ephemeralPreludeItemCount: prepared.ephemeralPreludeItemCount,
|
|
838
|
+
supersededItemIndices: prepared.supersededInstructionItems,
|
|
753
839
|
imageMap: prepared.imageMap,
|
|
754
840
|
retentionOptions: {
|
|
755
841
|
tokenBudget: routeConfig.nativeRetentionTokenBudget,
|
|
@@ -990,7 +1076,7 @@ function inputHasNativeState(input) {
|
|
|
990
1076
|
/**
|
|
991
1077
|
* One redacted line per failed provider request. Only the LCX class and machine-safe provider
|
|
992
1078
|
* identifiers are logged: never the provider message, a payload, a header or a credential.
|
|
993
|
-
* `warn` matches the compaction failure line; DSH ships no persistent
|
|
1079
|
+
* `warn` matches the compaction failure line; DSH 0.2.1-alpha.2 ships no persistent logger exporter
|
|
994
1080
|
* (see ERRORS.md), so the line is visible wherever the host attaches one.
|
|
995
1081
|
*/
|
|
996
1082
|
function logStreamFailure(ctx, route, info) {
|
|
@@ -1014,15 +1100,18 @@ async function* managedResponsesStream(options, routeConfig, ctx) {
|
|
|
1014
1100
|
try {
|
|
1015
1101
|
if (options.stop !== undefined)
|
|
1016
1102
|
throw Object.assign(new Error("LCX Responses does not support GenerateOptions.stop"), { code: "LCX_RESPONSES_UNSUPPORTED_OPTION" });
|
|
1017
|
-
const
|
|
1018
|
-
|
|
1103
|
+
const prepared = await serializeNativeAware(options.messages, route, routeConfig, ctx, {
|
|
1104
|
+
signal: options.signal,
|
|
1105
|
+
tools: options.tools && [...options.tools],
|
|
1106
|
+
toolSurface: { history: options.toolHistory },
|
|
1107
|
+
});
|
|
1019
1108
|
assertSupportedReasoningEffort(options.reasoningEffort, prepared.modelFacts);
|
|
1020
1109
|
const cacheSessionId = promptCacheSessionId(route, routeConfig, ctx);
|
|
1021
|
-
const headers = await authenticatedHeaders(ctx, routeConfig, cacheSessionId, cacheSessionId === undefined ? null : route.sessionId);
|
|
1110
|
+
const headers = await authenticatedHeaders(ctx, routeConfig, cacheSessionId, cacheSessionId === undefined ? null : route.sessionId, providerDynamicHeaders(routeConfig.provider, options.messages), route.sessionId || undefined);
|
|
1022
1111
|
const body = buildResponsesBody({
|
|
1023
1112
|
model: prepared.model,
|
|
1024
1113
|
input: prepared.input,
|
|
1025
|
-
tools: prepared.tools
|
|
1114
|
+
tools: prepared.tools,
|
|
1026
1115
|
sessionId: cacheSessionId,
|
|
1027
1116
|
promptCacheKey: promptCacheKey(route, routeConfig, ctx),
|
|
1028
1117
|
promptCacheRetention: promptCacheRetention(routeConfig),
|
|
@@ -1070,7 +1159,7 @@ async function* managedGrokNativeSearchStream(options, routeConfig, nativeSearch
|
|
|
1070
1159
|
grokNativeReplayRoute: nativeReplayRoute,
|
|
1071
1160
|
});
|
|
1072
1161
|
const cacheSessionId = grokPromptCacheSessionId(route, routeConfig);
|
|
1073
|
-
const headers = await authenticatedGrokHeaders(ctx, routeConfig, cacheSessionId);
|
|
1162
|
+
const headers = await authenticatedGrokHeaders(ctx, routeConfig, cacheSessionId, providerDynamicHeaders(routeConfig.provider, options.messages), route.sessionId || undefined);
|
|
1074
1163
|
const model = {
|
|
1075
1164
|
...prepared.model,
|
|
1076
1165
|
...routeConfig.modelDefaults,
|
|
@@ -1133,20 +1222,40 @@ function isGptLifecycleTarget(options) {
|
|
|
1133
1222
|
return /^gpt-/iu.test(String(options.model ?? ""));
|
|
1134
1223
|
}
|
|
1135
1224
|
async function* unavailableManagedRouteStream(options, diagnosis = "incomplete") {
|
|
1136
|
-
//
|
|
1137
|
-
const notConfigured = diagnosis === "model-not-configured";
|
|
1225
|
+
// DSH names no protocol for a model its catalog lacks and the profile sets no `api`: say what to change.
|
|
1138
1226
|
const apiUnresolved = diagnosis === "api-unresolved";
|
|
1139
|
-
yield managedFailureChunk(Object.assign(new Error(
|
|
1140
|
-
? "LCX
|
|
1141
|
-
: apiUnresolved
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1227
|
+
yield managedFailureChunk(Object.assign(new Error(apiUnresolved
|
|
1228
|
+
? "LCX could not determine the wire protocol of the selected GPT route: the provider profile sets no api and LCX could not infer one for this model. Add api: openai-responses to the provider profile, or select another model"
|
|
1229
|
+
: "LCX is enabled but the selected DSH route cannot be resolved as an authenticated OpenAI Responses wire route"), { code: apiUnresolved ? "LCX_RESPONSES_API_UNRESOLVED" : "LCX_RESPONSES_ROUTE_UNAVAILABLE" }), options.signal);
|
|
1230
|
+
}
|
|
1231
|
+
/**
|
|
1232
|
+
* Re-resolve a route whose protocol LCX could not infer after asking DSH whether it resolves the exact model.
|
|
1233
|
+
* DSH resolving it counts as the profile declaring it: the provider-wide protocol then applies, as in DSH.
|
|
1234
|
+
*/
|
|
1235
|
+
async function* hostRecognizedRouteStream(options, ctx, policy, next) {
|
|
1236
|
+
const facts = await resolveRouteModelFacts(ctx, options, options.signal);
|
|
1237
|
+
if (facts.resolved)
|
|
1238
|
+
markHostRecognizedModel(ctx, options.provider, options.model);
|
|
1239
|
+
const routeConfig = facts.resolved
|
|
1240
|
+
? resolveResponsesRouteConfig(ctx, options, policy())
|
|
1241
|
+
: undefined;
|
|
1242
|
+
if (!routeConfig) {
|
|
1243
|
+
yield* unavailableManagedRouteStream(options, diagnoseResponsesRoute(ctx, options));
|
|
1244
|
+
return;
|
|
1245
|
+
}
|
|
1246
|
+
yield* options.purpose === "compaction"
|
|
1247
|
+
? remoteCompactionStream(options, routeWithPolicies(routeConfig, policy()), ctx, next)
|
|
1248
|
+
: managedResponsesStream(options, routeWithPolicies(routeConfig, policy()), ctx);
|
|
1249
|
+
}
|
|
1250
|
+
/** The Grok counterpart of `hostRecognizedRouteStream`: DSH not resolving the model is DSH's to report. */
|
|
1251
|
+
async function* hostRecognizedGrokStream(options, ctx, policy, nativeSearch, next) {
|
|
1252
|
+
const facts = await resolveRouteModelFacts(ctx, options, options.signal);
|
|
1253
|
+
if (facts.resolved)
|
|
1254
|
+
markHostRecognizedModel(ctx, options.provider, options.model);
|
|
1255
|
+
const grokRoute = facts.resolved ? resolveGrokResponsesRouteConfig(ctx, options, policy()) : undefined;
|
|
1256
|
+
yield* grokRoute
|
|
1257
|
+
? managedGrokNativeSearchStream(options, routeWithPolicies(grokRoute, policy()), nativeSearch, ctx)
|
|
1258
|
+
: next();
|
|
1150
1259
|
}
|
|
1151
1260
|
function installInjected(ctx, configInput) {
|
|
1152
1261
|
installSearchUsage(ctx);
|
|
@@ -1200,18 +1309,44 @@ function installInjected(ctx, configInput) {
|
|
|
1200
1309
|
state.alphaSearch = configInput.alphaSearch.get();
|
|
1201
1310
|
state.grokNativeWebSearch = configInput.grokNativeWebSearch.get();
|
|
1202
1311
|
state.grokNativeXSearch = configInput.grokNativeXSearch.get();
|
|
1203
|
-
|
|
1312
|
+
// Absent (older config shapes) means the default: addition-only. Only an explicit false selects the full list.
|
|
1313
|
+
runtimeConfig =
|
|
1314
|
+
configInput.incrementalToolUpdates?.get() === false
|
|
1315
|
+
? { ...baseConfig, toolUpdates: "full" }
|
|
1316
|
+
: baseConfig;
|
|
1204
1317
|
refreshTools();
|
|
1205
1318
|
};
|
|
1319
|
+
/**
|
|
1320
|
+
* Tool ownership is decided synchronously when an agent's tools are assembled, but whether DSH serves a model
|
|
1321
|
+
* that LCX's bundled catalog does not describe is only known by asking DSH. So ask ahead of time, at start and
|
|
1322
|
+
* whenever the provider configuration or the provider set changes, and resynchronise the tools when the
|
|
1323
|
+
* remembered answers change; a first request on such a model then already carries the LCX tools.
|
|
1324
|
+
*/
|
|
1325
|
+
const prewarmHostModels = () => {
|
|
1326
|
+
void warmHostRecognizedModels(ctx)
|
|
1327
|
+
.then((changed) => {
|
|
1328
|
+
if (changed)
|
|
1329
|
+
refreshTools();
|
|
1330
|
+
})
|
|
1331
|
+
.catch((error) => ctx.logger?.debug?.(`[lcx-codex] host model prewarm skipped: ${String(error)}`));
|
|
1332
|
+
};
|
|
1206
1333
|
syncSettings();
|
|
1207
|
-
|
|
1208
|
-
ctx.on("
|
|
1334
|
+
prewarmHostModels();
|
|
1335
|
+
ctx.on("loader/volatile-update", () => {
|
|
1336
|
+
syncSettings();
|
|
1337
|
+
prewarmHostModels();
|
|
1338
|
+
});
|
|
1339
|
+
ctx.on("settings/document-updated", () => {
|
|
1340
|
+
refreshTools();
|
|
1341
|
+
prewarmHostModels();
|
|
1342
|
+
});
|
|
1343
|
+
ctx.on("llm/adapters-updated", prewarmHostModels);
|
|
1209
1344
|
ctx.inject(["compaction"], (compactionCtx) => {
|
|
1210
1345
|
patchCompactionPressureService(resolveContextService(compactionCtx, "compaction"), state, () => runtimeConfig, ctx, compactionPatchRecords);
|
|
1211
1346
|
});
|
|
1212
1347
|
/**
|
|
1213
1348
|
* LCX instrumentation is optional capability, never a creation precondition.
|
|
1214
|
-
* DSH
|
|
1349
|
+
* DSH awaits `agent/created` serially, so an escaping failure would make
|
|
1215
1350
|
* an otherwise valid Agent uncreatable; `agent/status` stays a contained emit.
|
|
1216
1351
|
* Both edges therefore fail closed locally: the Agent runs uninstrumented and
|
|
1217
1352
|
* the reason is logged, rather than propagating into the host lifecycle.
|
|
@@ -1228,7 +1363,7 @@ function installInjected(ctx, configInput) {
|
|
|
1228
1363
|
ctx.logger?.warn?.(`[lcx-codex] agent instrumentation skipped on ${edge}: ${String(error)}`);
|
|
1229
1364
|
}
|
|
1230
1365
|
};
|
|
1231
|
-
/** DSH
|
|
1366
|
+
/** DSH lifecycle source, recorded for diagnostics only. */
|
|
1232
1367
|
const startSource = (value) => typeof value === "string" && /^[a-z]+$/u.test(value) ? value : "unknown";
|
|
1233
1368
|
ctx.on("agent/created", (payload) => {
|
|
1234
1369
|
const agent = payload?.agent;
|
|
@@ -1256,10 +1391,25 @@ function installInjected(ctx, configInput) {
|
|
|
1256
1391
|
const grokRoute = resolveGrokResponsesRouteConfig(ctx, options, runtimeConfig);
|
|
1257
1392
|
if (grokRoute)
|
|
1258
1393
|
return managedGrokNativeSearchStream(options, routeWithPolicies(grokRoute, runtimeConfig), grokNativeSearch, ctx);
|
|
1394
|
+
// 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 (/^grok/iu.test(String(options.model ?? "")) && diagnoseResponsesRoute(ctx, options) === "api-unresolved")
|
|
1396
|
+
return hostRecognizedGrokStream(options, ctx, () => runtimeConfig, grokNativeSearch, next);
|
|
1259
1397
|
}
|
|
1260
1398
|
if (!state.enabled || !isGptLifecycleTarget(options))
|
|
1261
1399
|
return next();
|
|
1262
1400
|
const routeConfig = resolveResponsesRouteConfig(ctx, options, runtimeConfig);
|
|
1401
|
+
if (!routeConfig) {
|
|
1402
|
+
const diagnosis = diagnoseResponsesRoute(ctx, options);
|
|
1403
|
+
// DSH owns these calls: a model the provider's models list leaves out (DSH serves only the listed
|
|
1404
|
+
// models and answers UNKNOWN_MODEL, nothing is sent) and a provider whose authentication only Pi's own
|
|
1405
|
+
// client can perform (see HOST_AUTH_PROVIDERS). LCX does not own a route it cannot serve like DSH.
|
|
1406
|
+
if (diagnosis === "model-not-configured" || diagnosis === "host-auth")
|
|
1407
|
+
return next();
|
|
1408
|
+
}
|
|
1409
|
+
// The profile names no api and LCX's catalog does not describe the model: DSH's own catalog may still
|
|
1410
|
+
// resolve it, which only a lookup tells (once per failing request; resolving routes never pay it).
|
|
1411
|
+
if (!routeConfig && diagnoseResponsesRoute(ctx, options) === "api-unresolved")
|
|
1412
|
+
return hostRecognizedRouteStream(options, ctx, () => runtimeConfig, next);
|
|
1263
1413
|
if (options.purpose === "compaction") {
|
|
1264
1414
|
if (!routeConfig)
|
|
1265
1415
|
return unavailableManagedRouteStream(options, diagnoseResponsesRoute(ctx, options));
|
package/lib/model-catalog.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { getCatalogModels } from "./pi-responses-runtime.js";
|
|
2
|
-
import { wasPi086ResponsesModel } from "./pi086-responses-allowlist.js";
|
|
3
2
|
const providerDefaultsCache = new Map();
|
|
4
3
|
function sharedValue(values) {
|
|
5
4
|
const distinct = new Set(values);
|
|
@@ -20,10 +19,11 @@ function providerDefaults(provider) {
|
|
|
20
19
|
}
|
|
21
20
|
/**
|
|
22
21
|
* Resolve a route model's protocol, endpoint and catalog descriptor from the bundled Pi catalog.
|
|
23
|
-
* @param
|
|
24
|
-
*
|
|
22
|
+
* @param dshServes DSH serves this model although the bundled catalog does not describe it: the provider profile
|
|
23
|
+
* lists it in `models`, or DSH's own catalog resolves it (`resolveModelInfo` succeeds). Only then may the
|
|
24
|
+
* provider-wide default apply.
|
|
25
25
|
*/
|
|
26
|
-
export function resolveCatalogModel(provider, modelId,
|
|
26
|
+
export function resolveCatalogModel(provider, modelId, dshServes = false) {
|
|
27
27
|
const providerName = String(provider ?? "");
|
|
28
28
|
const id = String(modelId ?? "");
|
|
29
29
|
const exact = getCatalogModels(providerName).find((model) => model.id === id);
|
|
@@ -35,14 +35,12 @@ export function resolveCatalogModel(provider, modelId, declaredByProfile = false
|
|
|
35
35
|
descriptor: exact.api === "openai-responses" ? exact : undefined,
|
|
36
36
|
};
|
|
37
37
|
const defaults = providerDefaults(providerName);
|
|
38
|
-
if (
|
|
38
|
+
if (dshServes && defaults.api !== undefined)
|
|
39
39
|
return {
|
|
40
40
|
source: "catalog-provider-default",
|
|
41
41
|
api: defaults.api,
|
|
42
42
|
baseUrl: defaults.baseUrl,
|
|
43
43
|
descriptor: undefined,
|
|
44
44
|
};
|
|
45
|
-
if (wasPi086ResponsesModel(providerName, id))
|
|
46
|
-
return { source: "frozen-pi086", api: "openai-responses", baseUrl: undefined, descriptor: undefined };
|
|
47
45
|
return { source: "unknown", api: undefined, baseUrl: undefined, descriptor: undefined };
|
|
48
46
|
}
|