dsh-lcx-codex 0.5.1-pre.1 → 0.5.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 +11 -11
- package/THIRD_PARTY_NOTICES.md +2 -2
- package/lib/auxiliary-usage.js +45 -6
- package/lib/client.js +12 -12
- package/lib/dsh-compat.js +17 -0
- package/lib/dsh-responses.js +66 -88
- package/lib/index.js +121 -50
- package/lib/model-catalog.js +48 -0
- package/lib/native-checkpoint.js +18 -51
- package/lib/pi-responses-runtime.js +4 -0
- package/lib/responses-request.js +4 -1
- package/lib/responses-stream.js +162 -12
- package/lib/route.js +75 -33
- package/lib/search-usage.js +12 -5
- package/lib/transport.js +8 -0
- package/package.json +30 -30
package/lib/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import z from "@deepseek-ai/schemastery";
|
|
2
|
-
import { recordHostedUsage, withAuxiliaryUsage } from "./auxiliary-usage.js";
|
|
2
|
+
import { failedAuxiliaryUsageListener, recordHostedUsage, withAuxiliaryUsage } from "./auxiliary-usage.js";
|
|
3
3
|
import { installSearchUsage } from "./search-usage.js";
|
|
4
4
|
import { projectToolUpdates } from "@deepseek-ai/dsh-llm";
|
|
5
5
|
import { WEB_SEARCH_MAX_QUERIES, applyWebSearchTool, } from "@deepseek-ai/dsh-tool-web";
|
|
@@ -12,10 +12,11 @@ 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 { agentSessionId, agentUsesSession, compactionConfigState, compactionPatchCandidate, installCompactionPatch, patchCompactionConfig, patchToolResultPruner, readAgentRouteState, readSelectedAgentRouteState, recordAgentRouteEvent, resolveAgentService, resolveContextService, resolveScopedService, restoreCompactionConfig, restoreCompactionPatches, restoreToolResultPruner, sessionFor, sessionFromAgent, scopedToolRuntime, tokenMeterTotal, toolResultPrunerState, } from "./dsh-compat.js";
|
|
15
|
+
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
16
|
import { InvocationPolicyScope } from "./invocation-policy-scope.js";
|
|
17
|
-
import {
|
|
18
|
-
import {
|
|
17
|
+
import { resolveCatalogModel } from "./model-catalog.js";
|
|
18
|
+
import { authenticatedGrokHeaders, authenticatedHeaders, currentRoute, generationControlsFromSession, grokPromptCacheSessionId, promptCacheKey, promptCacheRetention, promptCacheSessionId, diagnoseResponsesRoute, resolveGrokResponsesRouteConfig, resolveResponsesRouteConfig, routeFingerprint, } from "./route.js";
|
|
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";
|
|
19
20
|
import { mergeFeatureHeader, requestNativeCompaction } from "./compact-v2.js";
|
|
20
21
|
import { buildResponsesBody } from "./responses-request.js";
|
|
21
22
|
import { managedFailureChunk, streamResponsesRequest, } from "./responses-stream.js";
|
|
@@ -346,6 +347,11 @@ function syncGptToolsForAgent(ctx, agent, state, getConfig, provider, registrati
|
|
|
346
347
|
}
|
|
347
348
|
if (advanced)
|
|
348
349
|
disposers.push(scopedTools.register(createAdvancedHostedTool(ctx, state, getConfig)));
|
|
350
|
+
// A search that was billed and then failed keeps its usage: only the Agent-scoped around-dispatch
|
|
351
|
+
// seam may attach `meta` to a failed tool result.
|
|
352
|
+
const disposeBilling = onScopedToolExecute(agent, failedAuxiliaryUsageListener);
|
|
353
|
+
if (disposeBilling)
|
|
354
|
+
disposers.push(disposeBilling);
|
|
349
355
|
registrations.set(agent, {
|
|
350
356
|
fingerprint,
|
|
351
357
|
dispose() {
|
|
@@ -545,50 +551,66 @@ function mergeMap(target, source) {
|
|
|
545
551
|
target.set(key, value);
|
|
546
552
|
}
|
|
547
553
|
/**
|
|
548
|
-
*
|
|
549
|
-
*
|
|
550
|
-
*
|
|
554
|
+
* The model facts a GPT route serializes and sends with. What DSH reported for the exact routed model
|
|
555
|
+
* (reasoning capability, context window) wins over the bundled catalog entry, and a profile's declared
|
|
556
|
+
* `reasoningEfforts` (wire mapping, or `false` for a non-reasoning model) wins over both. A model with no
|
|
557
|
+
* catalog entry follows what DSH itself resolved for it instead of being assumed to reason.
|
|
551
558
|
*/
|
|
552
|
-
function
|
|
553
|
-
if (
|
|
554
|
-
return
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
559
|
+
function gptModelOverride(routeConfig, facts) {
|
|
560
|
+
if (!facts.resolved)
|
|
561
|
+
return routeConfig.modelControls === undefined ? undefined : { ...routeConfig.modelControls };
|
|
562
|
+
return {
|
|
563
|
+
reasoning: facts.reasoning !== undefined,
|
|
564
|
+
...(facts.contextWindow === undefined ? {} : { contextWindow: facts.contextWindow }),
|
|
565
|
+
...routeConfig.modelControls,
|
|
566
|
+
};
|
|
567
|
+
}
|
|
568
|
+
/** Refuse an effort DSH does not offer for this model, the way the DSH adapter and the Grok route do. */
|
|
569
|
+
function assertSupportedReasoningEffort(effort, facts) {
|
|
570
|
+
if (effort === undefined || !facts.resolved)
|
|
571
|
+
return;
|
|
572
|
+
const supported = facts.reasoning === undefined ? ["off"] : facts.reasoning.efforts;
|
|
573
|
+
if (supported.includes(String(effort)))
|
|
574
|
+
return;
|
|
575
|
+
throw Object.assign(new Error(`Responses route does not support reasoning effort "${String(effort)}"`), { code: "LCX_RESPONSES_UNSUPPORTED_OPTION" });
|
|
576
|
+
}
|
|
577
|
+
const reportedCatalogGaps = new Set();
|
|
578
|
+
/**
|
|
579
|
+
* Say once per route model that LCX's bundled Pi catalog does not describe it, so its wire capabilities
|
|
580
|
+
* (strict tools, grammar tools, cache mode) come from the profile and from what DSH declares instead.
|
|
581
|
+
*/
|
|
582
|
+
function reportCatalogGap(ctx, route) {
|
|
583
|
+
const source = resolveCatalogModel(route.provider, route.model, true).source;
|
|
584
|
+
if (source !== "catalog-provider-default" && source !== "frozen-pi086")
|
|
585
|
+
return;
|
|
586
|
+
const key = `${route.provider}/${route.model}`;
|
|
587
|
+
if (reportedCatalogGaps.has(key))
|
|
588
|
+
return;
|
|
589
|
+
if (reportedCatalogGaps.size >= 256)
|
|
590
|
+
reportedCatalogGaps.clear();
|
|
591
|
+
reportedCatalogGaps.add(key);
|
|
592
|
+
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`);
|
|
567
593
|
}
|
|
568
594
|
async function serializeNativeAware(messages, route, routeConfig, ctx, options) {
|
|
569
595
|
const session = sessionFor(ctx, route.sessionId);
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
const { imageSupport
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
if (!resolved && hasMidHistoryPromptSnapshot(messages))
|
|
578
|
-
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" });
|
|
596
|
+
const modelFacts = await resolveRouteModelFacts(ctx, route, options.signal);
|
|
597
|
+
if (options.grokNativeReplayRoute === undefined)
|
|
598
|
+
reportCatalogGap(ctx, route);
|
|
599
|
+
const { imageSupport } = modelFacts;
|
|
600
|
+
const modelOverride = options.grokNativeReplayRoute === undefined
|
|
601
|
+
? gptModelOverride(routeConfig, modelFacts)
|
|
602
|
+
: undefined;
|
|
579
603
|
const input = [];
|
|
580
604
|
const imageMap = new Map();
|
|
581
605
|
let nativeTools;
|
|
582
606
|
let nativeModel;
|
|
583
607
|
let grammarToolInputProperties;
|
|
584
608
|
let normal = [];
|
|
585
|
-
/** Absolute `input` positions rendered from DSH prompt snapshots that a checkpoint supersedes. */
|
|
586
|
-
const supersededInstructionItems = [];
|
|
587
609
|
const serializeOptions = (imageMapOverride = undefined, extra = {}) => requestImageOptions(routeConfig, imageSupport, options.signal, imageMapOverride, {
|
|
588
610
|
route,
|
|
589
611
|
tools: options.tools,
|
|
590
612
|
responsesCompat: routeConfig.responsesCompat,
|
|
591
|
-
|
|
613
|
+
...(modelOverride === undefined ? {} : { model: modelOverride }),
|
|
592
614
|
...extra,
|
|
593
615
|
});
|
|
594
616
|
const firstMessage = messages[0];
|
|
@@ -607,20 +629,22 @@ async function serializeNativeAware(messages, route, routeConfig, ctx, options)
|
|
|
607
629
|
nativeTools = prelude.tools;
|
|
608
630
|
nativeModel = prelude.model;
|
|
609
631
|
grammarToolInputProperties = prelude.grammarToolInputProperties;
|
|
632
|
+
/** Base64 length of images DSH cannot offload (hydrated checkpoint, portable replay). */
|
|
633
|
+
let fixedImageBytes = 0;
|
|
634
|
+
/** Base64 length of each offloadable surface image, oldest first, across all batches. */
|
|
635
|
+
const surfaceImageBytes = [];
|
|
610
636
|
const flush = async () => {
|
|
611
637
|
if (!normal.length)
|
|
612
638
|
return;
|
|
613
639
|
const batch = normal;
|
|
614
640
|
normal = [];
|
|
615
641
|
const serialized = await serializeDshMessages(batch, ctx, serializeOptions());
|
|
642
|
+
surfaceImageBytes.push(...inputImageBase64Lengths(serialized.input));
|
|
616
643
|
nativeTools = serialized.tools ?? nativeTools;
|
|
617
644
|
nativeModel = serialized.model ?? nativeModel;
|
|
618
645
|
grammarToolInputProperties =
|
|
619
646
|
serialized.grammarToolInputProperties ?? grammarToolInputProperties;
|
|
620
|
-
|
|
621
|
-
const base = input.length;
|
|
622
|
-
supersededInstructionItems.push(...locateSupersededItems(serialized.input, restored, serialized.supersededInstructionItems).map((position) => base + position));
|
|
623
|
-
input.push(...restored);
|
|
647
|
+
input.push(...restoreGrokNativeReplay(serialized.input, batch.filter(isPersistedMessage), options.grokNativeReplayRoute));
|
|
624
648
|
mergeMap(imageMap, serialized.imageMap);
|
|
625
649
|
};
|
|
626
650
|
for (const message of surfaceMessages ?? []) {
|
|
@@ -639,15 +663,17 @@ async function serializeNativeAware(messages, route, routeConfig, ctx, options)
|
|
|
639
663
|
const nativeOutput = state.nativeOutput;
|
|
640
664
|
const hydratedMap = new Map();
|
|
641
665
|
const hydrated = await hydrateNativeImageReferences(nativeOutput, ctx, requestImageOptions(routeConfig, imageSupport, options.signal, hydratedMap));
|
|
642
|
-
|
|
666
|
+
const hydratedItems = responseInputItems(hydrated);
|
|
667
|
+
for (const bytes of inputImageBase64Lengths(hydratedItems))
|
|
668
|
+
fixedImageBytes += bytes;
|
|
669
|
+
input.push(...hydratedItems);
|
|
643
670
|
mergeMap(imageMap, hydratedMap);
|
|
644
671
|
}
|
|
645
672
|
else {
|
|
646
673
|
const portable = await portableMessagesForCheckpoint(ctx, session, checkpointId, { maxChars: routeConfig.portableReplayMaxChars });
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
const serialized = await serializeDshMessages(portable, ctx, serializeOptions(undefined, { systemPromptUpdate: undefined }));
|
|
674
|
+
const serialized = await serializeDshMessages(portable, ctx, serializeOptions());
|
|
675
|
+
for (const bytes of inputImageBase64Lengths(serialized.input))
|
|
676
|
+
fixedImageBytes += bytes;
|
|
651
677
|
input.push(...serialized.input);
|
|
652
678
|
mergeMap(imageMap, serialized.imageMap);
|
|
653
679
|
}
|
|
@@ -656,17 +682,24 @@ async function serializeNativeAware(messages, route, routeConfig, ctx, options)
|
|
|
656
682
|
normal.push(message);
|
|
657
683
|
}
|
|
658
684
|
await flush();
|
|
685
|
+
assertAggregateImageBudget({
|
|
686
|
+
maxBytes: Number.isSafeInteger(routeConfig.maxRequestImageBytes) && Number(routeConfig.maxRequestImageBytes) > 0
|
|
687
|
+
? Number(routeConfig.maxRequestImageBytes)
|
|
688
|
+
: DEFAULT_MAX_REQUEST_IMAGE_BYTES,
|
|
689
|
+
fixedBytes: fixedImageBytes,
|
|
690
|
+
surfaceBytes: surfaceImageBytes,
|
|
691
|
+
});
|
|
659
692
|
if (!nativeModel)
|
|
660
693
|
throw Object.assign(new Error("LCX could not resolve the Pi Responses model descriptor"), { code: "LCX_RESPONSES_MODEL_UNAVAILABLE" });
|
|
661
694
|
return {
|
|
662
695
|
input,
|
|
663
696
|
ephemeralPreludeItemCount,
|
|
664
|
-
supersededInstructionItems,
|
|
665
697
|
imageMap,
|
|
666
698
|
imageSupport,
|
|
667
699
|
tools: nativeTools,
|
|
668
700
|
model: nativeModel,
|
|
669
701
|
grammarToolInputProperties,
|
|
702
|
+
modelFacts,
|
|
670
703
|
};
|
|
671
704
|
}
|
|
672
705
|
function fallbackEligible(error, signal) {
|
|
@@ -717,7 +750,6 @@ async function* remoteCompactionStream(options, routeConfig, ctx, next) {
|
|
|
717
750
|
result,
|
|
718
751
|
input: prepared.input,
|
|
719
752
|
ephemeralPreludeItemCount: prepared.ephemeralPreludeItemCount,
|
|
720
|
-
supersededItemIndices: prepared.supersededInstructionItems,
|
|
721
753
|
imageMap: prepared.imageMap,
|
|
722
754
|
retentionOptions: {
|
|
723
755
|
tokenBudget: routeConfig.nativeRetentionTokenBudget,
|
|
@@ -955,6 +987,28 @@ async function messagesContainNativeCheckpoint(ctx, messages, session) {
|
|
|
955
987
|
function inputHasNativeState(input) {
|
|
956
988
|
return input.some((item) => isRecord(item) && item.type === "compaction");
|
|
957
989
|
}
|
|
990
|
+
/**
|
|
991
|
+
* One redacted line per failed provider request. Only the LCX class and machine-safe provider
|
|
992
|
+
* 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 plugin-logger exporter by default
|
|
994
|
+
* (see ERRORS.md), so the line is visible wherever the host attaches one.
|
|
995
|
+
*/
|
|
996
|
+
function logStreamFailure(ctx, route, info) {
|
|
997
|
+
const parts = [
|
|
998
|
+
`route=${route}`,
|
|
999
|
+
`code=${info.code}`,
|
|
1000
|
+
info.status === undefined ? "" : `status=${info.status}`,
|
|
1001
|
+
info.requestId === undefined ? "" : `requestId=${info.requestId}`,
|
|
1002
|
+
info.event === undefined ? "" : `event=${info.event}`,
|
|
1003
|
+
info.providerCode === undefined ? "" : `providerCode=${info.providerCode}`,
|
|
1004
|
+
info.providerType === undefined ? "" : `providerType=${info.providerType}`,
|
|
1005
|
+
info.incompleteReason === undefined
|
|
1006
|
+
? ""
|
|
1007
|
+
: `incompleteReason=${info.incompleteReason}`,
|
|
1008
|
+
`emitted=${info.contentEmitted}`,
|
|
1009
|
+
].filter(Boolean);
|
|
1010
|
+
ctx.logger?.warn?.(`[lcx-codex] responses stream failed: ${parts.join(" ")}`);
|
|
1011
|
+
}
|
|
958
1012
|
async function* managedResponsesStream(options, routeConfig, ctx) {
|
|
959
1013
|
const route = currentRoute(options, routeConfig);
|
|
960
1014
|
try {
|
|
@@ -962,6 +1016,7 @@ async function* managedResponsesStream(options, routeConfig, ctx) {
|
|
|
962
1016
|
throw Object.assign(new Error("LCX Responses does not support GenerateOptions.stop"), { code: "LCX_RESPONSES_UNSUPPORTED_OPTION" });
|
|
963
1017
|
const projected = projectToolUpdates(options.messages, options.tools, undefined, options.toolHistory);
|
|
964
1018
|
const prepared = await serializeNativeAware(projected.messages, route, routeConfig, ctx, { signal: options.signal, tools: projected.tools && [...projected.tools] });
|
|
1019
|
+
assertSupportedReasoningEffort(options.reasoningEffort, prepared.modelFacts);
|
|
965
1020
|
const cacheSessionId = promptCacheSessionId(route, routeConfig, ctx);
|
|
966
1021
|
const headers = await authenticatedHeaders(ctx, routeConfig, cacheSessionId, cacheSessionId === undefined ? null : route.sessionId);
|
|
967
1022
|
const body = buildResponsesBody({
|
|
@@ -993,6 +1048,7 @@ async function* managedResponsesStream(options, routeConfig, ctx) {
|
|
|
993
1048
|
timeoutMs: routeConfig.timeoutMs,
|
|
994
1049
|
maxAttempts: 1,
|
|
995
1050
|
maxResponseBytes: routeConfig.maxResponseBytes,
|
|
1051
|
+
onFailure: (info) => logStreamFailure(ctx, "gpt", info),
|
|
996
1052
|
});
|
|
997
1053
|
}
|
|
998
1054
|
catch (error) {
|
|
@@ -1026,7 +1082,8 @@ async function* managedGrokNativeSearchStream(options, routeConfig, nativeSearch
|
|
|
1026
1082
|
};
|
|
1027
1083
|
const effectiveReasoning = options.reasoningEffort ?? routeConfig.profileReasoning;
|
|
1028
1084
|
if (effectiveReasoning !== undefined &&
|
|
1029
|
-
!getSupportedThinkingLevels(model).some((level) => level === effectiveReasoning)
|
|
1085
|
+
!getSupportedThinkingLevels(model).some((level) => level === effectiveReasoning) &&
|
|
1086
|
+
!(effectiveReasoning === "off" && routeConfig.modelControls?.offOmitsReasoning === true))
|
|
1030
1087
|
throw Object.assign(new Error(`Grok route does not support reasoning effort "${String(effectiveReasoning)}"`), { code: "LCX_RESPONSES_UNSUPPORTED_OPTION" });
|
|
1031
1088
|
const body = buildResponsesBody({
|
|
1032
1089
|
model,
|
|
@@ -1062,6 +1119,7 @@ async function* managedGrokNativeSearchStream(options, routeConfig, nativeSearch
|
|
|
1062
1119
|
isServerToolItem: (item) => isGrokNativeServerToolItem(item, nativeSearch, declaredToolNames),
|
|
1063
1120
|
declaredToolNames,
|
|
1064
1121
|
nativeReplayRoute,
|
|
1122
|
+
onFailure: (info) => logStreamFailure(ctx, "grok-native", info),
|
|
1065
1123
|
onServerToolUsage(usage) {
|
|
1066
1124
|
ctx.logger?.info?.(`[lcx-codex] Grok native search used ${usage.total} server-side tool call(s) (web=${usage.webSearchCalls}, x=${usage.xSearchCalls})`);
|
|
1067
1125
|
},
|
|
@@ -1074,8 +1132,21 @@ async function* managedGrokNativeSearchStream(options, routeConfig, nativeSearch
|
|
|
1074
1132
|
function isGptLifecycleTarget(options) {
|
|
1075
1133
|
return /^gpt-/iu.test(String(options.model ?? ""));
|
|
1076
1134
|
}
|
|
1077
|
-
async function* unavailableManagedRouteStream(options) {
|
|
1078
|
-
|
|
1135
|
+
async function* unavailableManagedRouteStream(options, diagnosis = "incomplete") {
|
|
1136
|
+
// Say what to change: the profile's models list leaves the model out, or no protocol can be inferred for it.
|
|
1137
|
+
const notConfigured = diagnosis === "model-not-configured";
|
|
1138
|
+
const apiUnresolved = diagnosis === "api-unresolved";
|
|
1139
|
+
yield managedFailureChunk(Object.assign(new Error(notConfigured
|
|
1140
|
+
? "LCX is enabled but the selected model is not configured for this provider: the provider profile lists other models. Add the model to the profile's models list or select a listed model"
|
|
1141
|
+
: apiUnresolved
|
|
1142
|
+
? "LCX could not determine the wire protocol of the selected GPT route: the provider profile sets no api and the model is unknown to the catalog. Add api: openai-responses to the provider profile, or select a model DSH recognizes"
|
|
1143
|
+
: "LCX is enabled but the selected DSH route cannot be resolved as an authenticated OpenAI Responses wire route"), {
|
|
1144
|
+
code: notConfigured
|
|
1145
|
+
? "LCX_RESPONSES_MODEL_NOT_CONFIGURED"
|
|
1146
|
+
: apiUnresolved
|
|
1147
|
+
? "LCX_RESPONSES_API_UNRESOLVED"
|
|
1148
|
+
: "LCX_RESPONSES_ROUTE_UNAVAILABLE",
|
|
1149
|
+
}), options.signal);
|
|
1079
1150
|
}
|
|
1080
1151
|
function installInjected(ctx, configInput) {
|
|
1081
1152
|
installSearchUsage(ctx);
|
|
@@ -1191,11 +1262,11 @@ function installInjected(ctx, configInput) {
|
|
|
1191
1262
|
const routeConfig = resolveResponsesRouteConfig(ctx, options, runtimeConfig);
|
|
1192
1263
|
if (options.purpose === "compaction") {
|
|
1193
1264
|
if (!routeConfig)
|
|
1194
|
-
return unavailableManagedRouteStream(options);
|
|
1265
|
+
return unavailableManagedRouteStream(options, diagnoseResponsesRoute(ctx, options));
|
|
1195
1266
|
return remoteCompactionStream(options, routeWithPolicies(routeConfig, runtimeConfig), ctx, next);
|
|
1196
1267
|
}
|
|
1197
1268
|
if (!routeConfig)
|
|
1198
|
-
return unavailableManagedRouteStream(options);
|
|
1269
|
+
return unavailableManagedRouteStream(options, diagnoseResponsesRoute(ctx, options));
|
|
1199
1270
|
return managedResponsesStream(options, routeWithPolicies(routeConfig, runtimeConfig), ctx);
|
|
1200
1271
|
});
|
|
1201
1272
|
ctx.effect?.(() => async () => {
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { getCatalogModels } from "./pi-responses-runtime.js";
|
|
2
|
+
import { wasPi086ResponsesModel } from "./pi086-responses-allowlist.js";
|
|
3
|
+
const providerDefaultsCache = new Map();
|
|
4
|
+
function sharedValue(values) {
|
|
5
|
+
const distinct = new Set(values);
|
|
6
|
+
const [only] = distinct;
|
|
7
|
+
return distinct.size === 1 && typeof only === "string" && only.length > 0 ? only : undefined;
|
|
8
|
+
}
|
|
9
|
+
function providerDefaults(provider) {
|
|
10
|
+
const cached = providerDefaultsCache.get(provider);
|
|
11
|
+
if (cached !== undefined)
|
|
12
|
+
return cached;
|
|
13
|
+
const models = getCatalogModels(provider);
|
|
14
|
+
const defaults = {
|
|
15
|
+
api: sharedValue(models.map((model) => model.api)),
|
|
16
|
+
baseUrl: sharedValue(models.map((model) => model.baseUrl)),
|
|
17
|
+
};
|
|
18
|
+
providerDefaultsCache.set(provider, defaults);
|
|
19
|
+
return defaults;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Resolve a route model's protocol, endpoint and catalog descriptor from the bundled Pi catalog.
|
|
23
|
+
* @param declaredByProfile the provider profile lists this model in `models`; only then does DSH serve a model
|
|
24
|
+
* its catalog does not describe, so only then may the provider-wide default apply.
|
|
25
|
+
*/
|
|
26
|
+
export function resolveCatalogModel(provider, modelId, declaredByProfile = false) {
|
|
27
|
+
const providerName = String(provider ?? "");
|
|
28
|
+
const id = String(modelId ?? "");
|
|
29
|
+
const exact = getCatalogModels(providerName).find((model) => model.id === id);
|
|
30
|
+
if (exact !== undefined)
|
|
31
|
+
return {
|
|
32
|
+
source: "catalog-model",
|
|
33
|
+
api: exact.api,
|
|
34
|
+
baseUrl: exact.baseUrl,
|
|
35
|
+
descriptor: exact.api === "openai-responses" ? exact : undefined,
|
|
36
|
+
};
|
|
37
|
+
const defaults = providerDefaults(providerName);
|
|
38
|
+
if (declaredByProfile && defaults.api !== undefined)
|
|
39
|
+
return {
|
|
40
|
+
source: "catalog-provider-default",
|
|
41
|
+
api: defaults.api,
|
|
42
|
+
baseUrl: defaults.baseUrl,
|
|
43
|
+
descriptor: undefined,
|
|
44
|
+
};
|
|
45
|
+
if (wasPi086ResponsesModel(providerName, id))
|
|
46
|
+
return { source: "frozen-pi086", api: "openai-responses", baseUrl: undefined, descriptor: undefined };
|
|
47
|
+
return { source: "unknown", api: undefined, baseUrl: undefined, descriptor: undefined };
|
|
48
|
+
}
|
package/lib/native-checkpoint.js
CHANGED
|
@@ -321,32 +321,11 @@ export async function activeCompactionId(ctx, session) {
|
|
|
321
321
|
lease[Symbol.dispose]();
|
|
322
322
|
}
|
|
323
323
|
}
|
|
324
|
-
/**
|
|
325
|
-
* On "in-history" routes DSH records every system prompt change as a full later `system` message
|
|
326
|
-
* (source `system-prompt`). A checkpoint begins a new DSH series whose head is re-rendered with the
|
|
327
|
-
* CURRENT prompt, so those snapshots in the retained window are superseded by that head. Retaining
|
|
328
|
-
* one, as developer/system or even as user text, would keep a stale full prompt (for example plan-mode
|
|
329
|
-
* policy that has since been switched off) in front of the model after the compaction.
|
|
330
|
-
*
|
|
331
|
-
* Only items the serializer traced back to such snapshots (`supersededItemIndices`, absolute positions
|
|
332
|
-
* in `input`) are removed. Any other retained developer/system item is genuine client context and
|
|
333
|
-
* stays. The ephemeral prelude is removed by the caller's slice, and the retention budget is computed
|
|
334
|
-
* over what is actually kept.
|
|
335
|
-
*/
|
|
336
|
-
function dropSupersededItems(input, preludeCount, superseded) {
|
|
337
|
-
const drop = new Set();
|
|
338
|
-
for (const position of superseded) {
|
|
339
|
-
if (!Number.isSafeInteger(position) || position < preludeCount || position >= input.length)
|
|
340
|
-
throw Object.assign(new Error("Native compaction superseded-item provenance is invalid"), { code: "LCX_COMPACTION_PRELUDE_PROVENANCE_INVALID" });
|
|
341
|
-
drop.add(position);
|
|
342
|
-
}
|
|
343
|
-
return input.slice(preludeCount).filter((_, offset) => !drop.has(preludeCount + offset));
|
|
344
|
-
}
|
|
345
324
|
/**
|
|
346
325
|
* @param {CreateCheckpointOptions} options
|
|
347
326
|
* @returns {Promise<NativeCheckpointV5>}
|
|
348
327
|
*/
|
|
349
|
-
export async function createNativeCheckpointBlock({ ctx, session, route, result, input = [], ephemeralPreludeItemCount = 0,
|
|
328
|
+
export async function createNativeCheckpointBlock({ ctx, session, route, result, input = [], ephemeralPreludeItemCount = 0, imageMap, retentionOptions = {}, }) {
|
|
350
329
|
const compactionId = await activeCompactionId(ctx, session);
|
|
351
330
|
if (!compactionId) {
|
|
352
331
|
const error = new Error("Native compaction could not correlate the active DSH compaction transaction");
|
|
@@ -358,7 +337,7 @@ export async function createNativeCheckpointBlock({ ctx, session, route, result,
|
|
|
358
337
|
ephemeralPreludeItemCount > input.length) {
|
|
359
338
|
throw Object.assign(new Error("Native compaction prelude provenance is invalid"), { code: "LCX_COMPACTION_PRELUDE_PROVENANCE_INVALID" });
|
|
360
339
|
}
|
|
361
|
-
const retention = retainedConversationPlan(
|
|
340
|
+
const retention = retainedConversationPlan(input.slice(ephemeralPreludeItemCount), retentionOptions);
|
|
362
341
|
/** @type {NativeOutputItem[]} */
|
|
363
342
|
const nativeOutput = persistNativeImageReferences([...retention.items, structuredClone(result.compaction)], imageMap ?? new Map());
|
|
364
343
|
return {
|
|
@@ -686,46 +665,35 @@ function groupMessages(messages) {
|
|
|
686
665
|
.map((block) => String(block.id))
|
|
687
666
|
: [];
|
|
688
667
|
if (toolCalls.length === 0) {
|
|
668
|
+
// A tool result no call can claim (its call was not directly in front of it) cannot be replayed:
|
|
669
|
+
// upstream refuses a function_call_output without its call. Results that belong to a call are
|
|
670
|
+
// consumed with that call's group below and never reach this branch.
|
|
671
|
+
if (message.role === "tool")
|
|
672
|
+
continue;
|
|
689
673
|
groups.push([message]);
|
|
690
674
|
continue;
|
|
691
675
|
}
|
|
692
676
|
const group = [message];
|
|
693
677
|
const pending = new Set(toolCalls);
|
|
694
678
|
let cursor = index + 1;
|
|
695
|
-
|
|
679
|
+
// The run of tool messages right behind the call belongs to this group. Only the first result of
|
|
680
|
+
// each call it issued joins the group; a result for any other call, or a repeat, is not replayable
|
|
681
|
+
// (upstream refuses a function_call_output without its call) and is dropped. The whole run is
|
|
682
|
+
// consumed either way, so nothing in it is read again as a group of its own.
|
|
683
|
+
while (cursor < messages.length) {
|
|
696
684
|
const next = messages[cursor];
|
|
697
|
-
if (!next)
|
|
685
|
+
if (!next || next.role !== "tool")
|
|
698
686
|
break;
|
|
699
|
-
const
|
|
700
|
-
if (
|
|
701
|
-
|
|
702
|
-
group.push(next);
|
|
703
|
-
for (const id of results)
|
|
704
|
-
pending.delete(id);
|
|
687
|
+
const id = String(next.toolCallId);
|
|
688
|
+
if (pending.delete(id))
|
|
689
|
+
group.push(next);
|
|
705
690
|
cursor += 1;
|
|
706
691
|
}
|
|
707
|
-
|
|
708
|
-
index = cursor - 1;
|
|
692
|
+
index = cursor - 1;
|
|
709
693
|
groups.push(group);
|
|
710
694
|
}
|
|
711
695
|
return groups;
|
|
712
696
|
}
|
|
713
|
-
/**
|
|
714
|
-
* A shadowed `system/message` (source `system-prompt`) is a full system prompt snapshot DSH appended
|
|
715
|
-
* when the prompt changed on an "in-history" route. Portable replay is only used when the checkpoint
|
|
716
|
-
* cannot be replayed natively (fork, route or model migration), and the request that follows always
|
|
717
|
-
* carries a head rendered with the current prompt, so every snapshot in the shadowed range is
|
|
718
|
-
* superseded: replaying it, even as user text, would put stale instructions (for example plan-mode
|
|
719
|
-
* policy that was switched off) back in front of the model.
|
|
720
|
-
*
|
|
721
|
-
* Only messages DSH itself marks as system-prompt snapshots are dropped. The head is never inside a
|
|
722
|
-
* compaction range, and a Session V3 log (where later system messages were ordinary context) cannot
|
|
723
|
-
* reach this path: LCX refuses V3 checkpoints before transport. Other system-role messages without that
|
|
724
|
-
* source keep the previous ordinary-context mapping.
|
|
725
|
-
*/
|
|
726
|
-
export function withoutSupersededPromptSnapshots(messages) {
|
|
727
|
-
return messages.filter((message) => !(message.role === "system" && message.source?.kind === "system-prompt"));
|
|
728
|
-
}
|
|
729
697
|
/**
|
|
730
698
|
* @param {Session} session
|
|
731
699
|
* @param {string} compactionId
|
|
@@ -740,8 +708,7 @@ export async function portableMessagesForCheckpoint(ctx, session, compactionId,
|
|
|
740
708
|
? configuredMaxChars
|
|
741
709
|
: 80_000;
|
|
742
710
|
const maxTokens = portableTokenCeiling(maxChars) ?? 1;
|
|
743
|
-
|
|
744
|
-
const expanded = withoutSupersededPromptSnapshots(await shadowedMessagesForCheckpoint(ctx, session, compactionId));
|
|
711
|
+
const expanded = await shadowedMessagesForCheckpoint(ctx, session, compactionId);
|
|
745
712
|
if (expanded.length === 0)
|
|
746
713
|
return [];
|
|
747
714
|
const groups = groupMessages(expanded);
|
|
@@ -1705,6 +1705,9 @@ var models = {
|
|
|
1705
1705
|
"opencode-go": responsesOnly(RESPONSE_CATALOGS["opencode-go"]),
|
|
1706
1706
|
xai: responsesOnly(RESPONSE_CATALOGS.xai)
|
|
1707
1707
|
};
|
|
1708
|
+
function getCatalogModels(provider) {
|
|
1709
|
+
return Object.hasOwn(RESPONSE_CATALOGS, provider) ? Object.values(RESPONSE_CATALOGS[provider]) : [];
|
|
1710
|
+
}
|
|
1708
1711
|
function getBuiltinProviders() {
|
|
1709
1712
|
return providers;
|
|
1710
1713
|
}
|
|
@@ -1719,6 +1722,7 @@ export {
|
|
|
1719
1722
|
createGrammarToolInputProperties,
|
|
1720
1723
|
getBuiltinModels,
|
|
1721
1724
|
getBuiltinProviders,
|
|
1725
|
+
getCatalogModels,
|
|
1722
1726
|
getSupportedThinkingLevels,
|
|
1723
1727
|
normalizeContext,
|
|
1724
1728
|
processResponsesStream
|
package/lib/responses-request.js
CHANGED
|
@@ -38,7 +38,10 @@ export function responsesGenerationEnvelope({ model = "unknown", reasoningEffort
|
|
|
38
38
|
result.include = ["reasoning.encrypted_content"];
|
|
39
39
|
if (reasoningEffort !== undefined && reasoningEffort !== "off") {
|
|
40
40
|
const requested = String(reasoningEffort);
|
|
41
|
-
|
|
41
|
+
// Unmapped means "send the level as is"; an explicit null means the model declares the level
|
|
42
|
+
// unsupported, so nothing is sent for it (the DSH adapter refuses such a level before dispatch).
|
|
43
|
+
const mapped = descriptor.thinkingLevelMap?.[requested];
|
|
44
|
+
const wire = mapped === undefined ? requested : mapped;
|
|
42
45
|
if (wire !== null) {
|
|
43
46
|
result.reasoning = { effort: wire, summary: "auto" };
|
|
44
47
|
result.include = ["reasoning.encrypted_content"];
|