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
package/lib/dsh-compat.js
CHANGED
|
@@ -56,8 +56,8 @@ export function readSelectedAgentRouteState(agent) {
|
|
|
56
56
|
return route;
|
|
57
57
|
// Cold/resumed Agents recover pending selection from DSH's public projection.
|
|
58
58
|
const projections = resolveScopedService(agent, "sessionProjections");
|
|
59
|
-
if (
|
|
60
|
-
const selection =
|
|
59
|
+
if (typeof projections?.stateOf === "function") {
|
|
60
|
+
const selection = projections.stateOf(session, "modelSelection");
|
|
61
61
|
if (mutableService(selection))
|
|
62
62
|
return { ...route, requestConfig: selection.pending ?? route.requestConfig };
|
|
63
63
|
}
|
|
@@ -66,13 +66,11 @@ export function readSelectedAgentRouteState(agent) {
|
|
|
66
66
|
}
|
|
67
67
|
export function scopedToolRuntime(agent) {
|
|
68
68
|
const tools = resolveScopedService(agent, "tools");
|
|
69
|
-
if (
|
|
69
|
+
if (typeof tools?.register !== "function")
|
|
70
70
|
return undefined;
|
|
71
71
|
return {
|
|
72
72
|
register: tools.register.bind(tools),
|
|
73
|
-
...(typeof tools.get === "function"
|
|
74
|
-
? { get: tools.get.bind(tools) }
|
|
75
|
-
: {}),
|
|
73
|
+
...(typeof tools.get === "function" ? { get: tools.get.bind(tools) } : {}),
|
|
76
74
|
};
|
|
77
75
|
}
|
|
78
76
|
/**
|
|
@@ -82,31 +80,32 @@ export function scopedToolRuntime(agent) {
|
|
|
82
80
|
*/
|
|
83
81
|
export function onScopedToolExecute(agent, listener) {
|
|
84
82
|
const ctx = agentLike(agent)?.ctx;
|
|
85
|
-
if (
|
|
83
|
+
if (typeof ctx?.on !== "function")
|
|
86
84
|
return undefined;
|
|
87
85
|
try {
|
|
88
|
-
const dispose =
|
|
89
|
-
return typeof dispose === "function" ? () => void
|
|
86
|
+
const dispose = ctx.on("tools/execute", listener);
|
|
87
|
+
return typeof dispose === "function" ? () => void dispose() : undefined;
|
|
90
88
|
}
|
|
91
89
|
catch {
|
|
92
90
|
return undefined;
|
|
93
91
|
}
|
|
94
92
|
}
|
|
95
93
|
export function tokenMeterTotal(value, session) {
|
|
96
|
-
|
|
94
|
+
const meter = value;
|
|
95
|
+
if (typeof meter?.measure !== "function")
|
|
97
96
|
return undefined;
|
|
98
|
-
const measurement =
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
97
|
+
const measurement = meter.measure(session);
|
|
98
|
+
return typeof measurement?.totalTokens === "number" && Number.isFinite(measurement.totalTokens)
|
|
99
|
+
? measurement.totalTokens
|
|
100
|
+
: undefined;
|
|
102
101
|
}
|
|
103
102
|
export function sessionsService(ctx) {
|
|
104
103
|
const service = resolveContextService(ctx, "sessions");
|
|
105
|
-
if (
|
|
104
|
+
if (typeof service?.get !== "function")
|
|
106
105
|
return undefined;
|
|
107
106
|
return {
|
|
108
107
|
get(id) {
|
|
109
|
-
const value =
|
|
108
|
+
const value = service.get?.(id);
|
|
110
109
|
return value instanceof Session ? value : undefined;
|
|
111
110
|
},
|
|
112
111
|
};
|
|
@@ -116,30 +115,6 @@ export function sessionFor(ctx, sessionId) {
|
|
|
116
115
|
? sessionsService(ctx)?.get(SessionId(sessionId))
|
|
117
116
|
: undefined;
|
|
118
117
|
}
|
|
119
|
-
export function readWebSearchProvider(ctx) {
|
|
120
|
-
const web = resolveContextService(ctx, "web");
|
|
121
|
-
if (!mutableService(web))
|
|
122
|
-
return undefined;
|
|
123
|
-
try {
|
|
124
|
-
return Reflect.get(web, "searchProviderId");
|
|
125
|
-
}
|
|
126
|
-
catch {
|
|
127
|
-
return undefined;
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
export function writeWebSearchProvider(ctx, providerId) {
|
|
131
|
-
const web = resolveContextService(ctx, "web");
|
|
132
|
-
if (!mutableService(web))
|
|
133
|
-
return false;
|
|
134
|
-
try {
|
|
135
|
-
if (!Reflect.set(web, "searchProviderId", providerId))
|
|
136
|
-
return false;
|
|
137
|
-
return Reflect.get(web, "searchProviderId") === providerId;
|
|
138
|
-
}
|
|
139
|
-
catch {
|
|
140
|
-
return false;
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
118
|
export function contextService(ctx, name) {
|
|
144
119
|
if (!mutableService(ctx))
|
|
145
120
|
return undefined;
|
|
@@ -334,39 +309,3 @@ export function restoreCompactionConfig(record) {
|
|
|
334
309
|
catch { }
|
|
335
310
|
}
|
|
336
311
|
}
|
|
337
|
-
export function patchVisibleWebSearchTimeout(agent, getTimeoutMs, patchedDefinitions) {
|
|
338
|
-
const tools = resolveScopedService(agent, "tools");
|
|
339
|
-
if (!mutableService(tools) || typeof tools.get !== "function")
|
|
340
|
-
return;
|
|
341
|
-
const definition = Reflect.apply(tools.get, tools, ["web_search", agent]);
|
|
342
|
-
if (!mutableService(definition))
|
|
343
|
-
return;
|
|
344
|
-
if (!patchedDefinitions.has(definition))
|
|
345
|
-
patchedDefinitions.set(definition, definition.timeoutMs);
|
|
346
|
-
const timeoutMs = getTimeoutMs();
|
|
347
|
-
const target = timeoutMs === undefined ? patchedDefinitions.get(definition) : timeoutMs;
|
|
348
|
-
try {
|
|
349
|
-
definition.timeoutMs = target;
|
|
350
|
-
}
|
|
351
|
-
catch { }
|
|
352
|
-
}
|
|
353
|
-
export function refreshVisibleWebSearchTimeouts(patchedDefinitions, timeoutMs) {
|
|
354
|
-
for (const [definition, original] of patchedDefinitions) {
|
|
355
|
-
try {
|
|
356
|
-
definition.timeoutMs = timeoutMs === undefined ? original : timeoutMs;
|
|
357
|
-
}
|
|
358
|
-
catch { }
|
|
359
|
-
}
|
|
360
|
-
}
|
|
361
|
-
export function restoreVisibleWebSearchTimeouts(patchedDefinitions) {
|
|
362
|
-
for (const [definition, original] of patchedDefinitions) {
|
|
363
|
-
try {
|
|
364
|
-
if (original === undefined)
|
|
365
|
-
delete definition.timeoutMs;
|
|
366
|
-
else
|
|
367
|
-
definition.timeoutMs = original;
|
|
368
|
-
}
|
|
369
|
-
catch { }
|
|
370
|
-
}
|
|
371
|
-
patchedDefinitions.clear();
|
|
372
|
-
}
|
package/lib/dsh-responses.js
CHANGED
|
@@ -2,15 +2,14 @@ import { IMAGE_OFFLOAD_REQUIRED_CODE, LlmError, offloadedImageText, projectOfflo
|
|
|
2
2
|
import { requestImageDimensions } from "@deepseek-ai/dsh-attachment";
|
|
3
3
|
import { convertResponsesMessages, convertResponsesTools, createGrammarToolInputProperties, normalizeContext, } from "./pi-responses-runtime.js";
|
|
4
4
|
import { resolveCatalogModel } from "./model-catalog.js";
|
|
5
|
+
import { withoutDeferLoading } from "./tool-surface.js";
|
|
6
|
+
import { isRecord, codedError, positiveSafeInteger } from "./util.js";
|
|
5
7
|
const DEFAULT_MAX_REQUEST_IMAGE_BYTES = 20 * 1024 * 1024;
|
|
6
8
|
const DEFAULT_REQUEST_IMAGE_PIXEL_BUDGET = 2048 * 2048;
|
|
7
9
|
const DEFAULT_REQUEST_IMAGE_MAX_BYTES = 1024 * 1024;
|
|
8
10
|
export { DEFAULT_MAX_REQUEST_IMAGE_BYTES, DEFAULT_REQUEST_IMAGE_PIXEL_BUDGET, DEFAULT_REQUEST_IMAGE_MAX_BYTES, };
|
|
9
|
-
function isObject(value) {
|
|
10
|
-
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
11
|
-
}
|
|
12
11
|
function isResponseInputItem(value) {
|
|
13
|
-
if (!
|
|
12
|
+
if (!isRecord(value))
|
|
14
13
|
return false;
|
|
15
14
|
if (value.type === "compaction")
|
|
16
15
|
return typeof value.encrypted_content === "string";
|
|
@@ -30,7 +29,7 @@ function isResponseInputItem(value) {
|
|
|
30
29
|
}
|
|
31
30
|
export function responseInputItems(value) {
|
|
32
31
|
if (!Array.isArray(value) || !value.every(isResponseInputItem))
|
|
33
|
-
throw
|
|
32
|
+
throw codedError("native checkpoint contains an unsupported Responses input item", "LCX_COMPACT_INVALID_RESPONSE");
|
|
34
33
|
return value;
|
|
35
34
|
}
|
|
36
35
|
function asArray(value) {
|
|
@@ -39,18 +38,8 @@ function asArray(value) {
|
|
|
39
38
|
function stringValue(value, fallback = "") {
|
|
40
39
|
return typeof value === "string" ? value : fallback;
|
|
41
40
|
}
|
|
42
|
-
function positiveSafeInteger(value, fallback) {
|
|
43
|
-
return Number.isSafeInteger(value) && value > 0
|
|
44
|
-
? value
|
|
45
|
-
: fallback;
|
|
46
|
-
}
|
|
47
|
-
function error(message, code) {
|
|
48
|
-
const value = new Error(message);
|
|
49
|
-
value.code = code;
|
|
50
|
-
return value;
|
|
51
|
-
}
|
|
52
41
|
function errorCode(value) {
|
|
53
|
-
return
|
|
42
|
+
return isRecord(value) && typeof value.code === "string" ? value.code : undefined;
|
|
54
43
|
}
|
|
55
44
|
function errorMessage(value) {
|
|
56
45
|
return value instanceof Error ? value.message : String(value);
|
|
@@ -64,7 +53,7 @@ function attachmentServices(store) {
|
|
|
64
53
|
};
|
|
65
54
|
}
|
|
66
55
|
function hasImageAttachment(value) {
|
|
67
|
-
return
|
|
56
|
+
return isRecord(value) && typeof value.attachmentId === "string" && typeof value.bytes === "number";
|
|
68
57
|
}
|
|
69
58
|
function imageAccess(ctx, attachments, ref) {
|
|
70
59
|
const mapper = ctx?.fs
|
|
@@ -72,11 +61,15 @@ function imageAccess(ctx, attachments, ref) {
|
|
|
72
61
|
: () => undefined;
|
|
73
62
|
return attachments.resolveAccess(mapper, ref);
|
|
74
63
|
}
|
|
75
|
-
/**
|
|
64
|
+
/**
|
|
65
|
+
* Facts DSH resolves for the exact routed model through `llm.resolveModelInfo`, the same
|
|
66
|
+
* adapter-owned metadata the agent loop uses to choose how a system prompt change is recorded.
|
|
67
|
+
* Resolution failure reports `resolved: false` with conservative defaults (unknown image support, no in-history mode).
|
|
68
|
+
*/
|
|
76
69
|
export async function resolveRouteModelFacts(ctx, route, signal) {
|
|
77
70
|
const llm = ctx?.llm;
|
|
78
71
|
if (llm === undefined)
|
|
79
|
-
return { imageSupport: "unknown", reasoning: undefined, contextWindow: undefined, resolved: false };
|
|
72
|
+
return { imageSupport: "unknown", systemPromptUpdate: undefined, toolUpdate: undefined, reasoning: undefined, contextWindow: undefined, resolved: false };
|
|
80
73
|
try {
|
|
81
74
|
const info = await llm.resolveModelInfo(stringValue(route.provider), stringValue(route.model), signal);
|
|
82
75
|
const modalities = info.inputModalities;
|
|
@@ -86,6 +79,8 @@ export async function resolveRouteModelFacts(ctx, route, signal) {
|
|
|
86
79
|
: modalities !== undefined
|
|
87
80
|
? "unsupported"
|
|
88
81
|
: "unknown",
|
|
82
|
+
systemPromptUpdate: info.systemPromptUpdate === "in-history" ? "in-history" : undefined,
|
|
83
|
+
toolUpdate: info.toolUpdate === "addition-only" || info.toolUpdate === "in-history" ? info.toolUpdate : undefined,
|
|
89
84
|
reasoning: info.reasoning === undefined
|
|
90
85
|
? undefined
|
|
91
86
|
: { efforts: info.reasoning.efforts.map((effort) => String(effort.id)) },
|
|
@@ -94,28 +89,43 @@ export async function resolveRouteModelFacts(ctx, route, signal) {
|
|
|
94
89
|
};
|
|
95
90
|
}
|
|
96
91
|
catch {
|
|
97
|
-
|
|
92
|
+
// `resolved: false` tells callers the mode is unknown rather than known-absent, so they can refuse
|
|
93
|
+
// to downgrade history that only an in-history route may legitimately contain.
|
|
94
|
+
return { imageSupport: "unknown", systemPromptUpdate: undefined, toolUpdate: undefined, reasoning: undefined, contextWindow: undefined, resolved: false };
|
|
98
95
|
}
|
|
99
96
|
}
|
|
100
|
-
|
|
101
|
-
|
|
97
|
+
/**
|
|
98
|
+
* The model facts a route serializes and sends with, laid over the bundled catalog descriptor. What DSH
|
|
99
|
+
* reported for the exact routed model (context window; on a GPT route also the reasoning capability) wins
|
|
100
|
+
* over the catalog entry, and on a GPT route a profile's declared `reasoningEfforts` (wire mapping, or `false`
|
|
101
|
+
* for a non-reasoning model) wins over both. A GPT model with no catalog entry follows what DSH itself
|
|
102
|
+
* resolved for it instead of being assumed to reason. A Grok route carries its reasoning projection
|
|
103
|
+
* separately (`modelDefaults` / `modelControls`), so only the context window is applied there.
|
|
104
|
+
*/
|
|
105
|
+
export function routeModelOverride(routeConfig, facts, grok) {
|
|
106
|
+
const contextWindow = facts.resolved && facts.contextWindow !== undefined ? { contextWindow: facts.contextWindow } : {};
|
|
107
|
+
if (grok)
|
|
108
|
+
return Object.keys(contextWindow).length > 0 ? contextWindow : undefined;
|
|
109
|
+
if (!facts.resolved)
|
|
110
|
+
return routeConfig.modelControls === undefined ? undefined : { ...routeConfig.modelControls };
|
|
111
|
+
return { reasoning: facts.reasoning !== undefined, ...contextWindow, ...routeConfig.modelControls };
|
|
102
112
|
}
|
|
103
113
|
function attachmentResolver(ctx, options, requestImages) {
|
|
104
114
|
const attachments = attachmentServices(ctx?.attachments);
|
|
105
115
|
return async (block, signal) => {
|
|
106
116
|
if (attachments === undefined)
|
|
107
|
-
throw
|
|
117
|
+
throw codedError("LCX requires the DSH request-image attachment API", "LCX_COMPACT_IMAGE_API_UNAVAILABLE");
|
|
108
118
|
const request = requestImages === undefined
|
|
109
119
|
? await attachments.store.readImageRequest(block.attachment, { ...requestImageDimensions(block.attachment.width, block.attachment.height, options.requestImagePixelBudget), maxBytes: options.requestImageMaxBytes }, signal)
|
|
110
120
|
: requestImages.get(String(block.attachment.attachmentId));
|
|
111
121
|
if (request === undefined)
|
|
112
|
-
throw
|
|
122
|
+
throw codedError("DSH attachment returned no request-image bytes", "LCX_COMPACT_IMAGE_UNAVAILABLE");
|
|
113
123
|
const data = request.data;
|
|
114
124
|
const mediaType = request.mediaType ?? block.attachment.mediaType;
|
|
115
125
|
if (!(data instanceof Uint8Array) && !Buffer.isBuffer(data))
|
|
116
|
-
throw
|
|
126
|
+
throw codedError("DSH attachment returned no request-image bytes", "LCX_COMPACT_IMAGE_UNAVAILABLE");
|
|
117
127
|
if (typeof mediaType !== "string" || !mediaType.startsWith("image/"))
|
|
118
|
-
throw
|
|
128
|
+
throw codedError("DSH attachment returned an invalid request-image media type", "LCX_COMPACT_IMAGE_UNAVAILABLE");
|
|
119
129
|
return {
|
|
120
130
|
data: Buffer.from(data),
|
|
121
131
|
mediaType,
|
|
@@ -130,7 +140,7 @@ async function imagePart(block, ctx, options, imageMap, requestImages) {
|
|
|
130
140
|
return { type: "input_text", text: "[image omitted because the target model does not support image input]" };
|
|
131
141
|
const image = await attachmentResolver(ctx, options, requestImages)(block, options.signal);
|
|
132
142
|
if (Math.ceil(image.data.byteLength / 3) * 4 > options.maxRequestImageBytes)
|
|
133
|
-
throw
|
|
143
|
+
throw codedError("one image exceeds the configured LCX request image bound", "LCX_COMPACT_IMAGE_TOO_LARGE");
|
|
134
144
|
const imageUrl = `data:${image.mediaType};base64,${image.data.toString("base64")}`;
|
|
135
145
|
imageMap.set(imageUrl, structuredClone(image.ref));
|
|
136
146
|
return { type: "input_image", detail: "auto", image_url: imageUrl };
|
|
@@ -140,7 +150,7 @@ async function piImageParts(block, ctx, options, imageMap, requestImages) {
|
|
|
140
150
|
return [{ type: "text", text: "[image omitted because the target model does not support image input]" }];
|
|
141
151
|
const image = await attachmentResolver(ctx, options, requestImages)(block, options.signal);
|
|
142
152
|
if (Math.ceil(image.data.byteLength / 3) * 4 > options.maxRequestImageBytes)
|
|
143
|
-
throw
|
|
153
|
+
throw codedError("one image exceeds the configured LCX request image bound", "LCX_COMPACT_IMAGE_TOO_LARGE");
|
|
144
154
|
const data = image.data.toString("base64");
|
|
145
155
|
imageMap.set(`data:${image.mediaType};base64,${data}`, structuredClone(image.ref));
|
|
146
156
|
return [
|
|
@@ -149,13 +159,13 @@ async function piImageParts(block, ctx, options, imageMap, requestImages) {
|
|
|
149
159
|
];
|
|
150
160
|
}
|
|
151
161
|
function parseArguments(value) {
|
|
152
|
-
if (
|
|
162
|
+
if (isRecord(value))
|
|
153
163
|
return structuredClone(value);
|
|
154
164
|
if (typeof value !== "string")
|
|
155
165
|
return {};
|
|
156
166
|
try {
|
|
157
167
|
const parsed = JSON.parse(value);
|
|
158
|
-
return
|
|
168
|
+
return isRecord(parsed) ? parsed : {};
|
|
159
169
|
}
|
|
160
170
|
catch {
|
|
161
171
|
return {};
|
|
@@ -165,10 +175,10 @@ function emptyUsage() {
|
|
|
165
175
|
return { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, totalTokens: 0, cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 } };
|
|
166
176
|
}
|
|
167
177
|
function invalidReplay(message) {
|
|
168
|
-
return
|
|
178
|
+
return codedError(`invalid pi-ai replay state: ${message}`, "LCX_COMPACT_INVALID_REPLAY_STATE");
|
|
169
179
|
}
|
|
170
180
|
function unsupportedContent(type) {
|
|
171
|
-
return
|
|
181
|
+
return codedError(`LCX Compact cannot safely serialize DSH message content type: ${String(type)}`, "LCX_CHECKPOINT_PORTABLE_UNSUPPORTED_CONTENT");
|
|
172
182
|
}
|
|
173
183
|
function replayBlockType(type) {
|
|
174
184
|
return type === "text" || type === "reasoning" || type === "tool-call" ? type : undefined;
|
|
@@ -178,7 +188,7 @@ function replayTextPhase(signature) {
|
|
|
178
188
|
return undefined;
|
|
179
189
|
try {
|
|
180
190
|
const parsed = JSON.parse(signature);
|
|
181
|
-
if (!
|
|
191
|
+
if (!isRecord(parsed) || parsed.v !== 1 || typeof parsed.id !== "string")
|
|
182
192
|
return undefined;
|
|
183
193
|
return parsed.phase === "commentary" || parsed.phase === "final_answer"
|
|
184
194
|
? parsed.phase
|
|
@@ -210,14 +220,14 @@ function restorePortableAssistantPhases(input, messages, target) {
|
|
|
210
220
|
const output = structuredClone(input);
|
|
211
221
|
let slotIndex = 0;
|
|
212
222
|
for (const [index, candidate] of output.entries()) {
|
|
213
|
-
if (!
|
|
223
|
+
if (!isRecord(candidate) || candidate.type !== "message" || candidate.role !== "assistant" ||
|
|
214
224
|
!Array.isArray(candidate.content))
|
|
215
225
|
continue;
|
|
216
226
|
const slot = slots[slotIndex++];
|
|
217
227
|
if (slot?.phase === undefined)
|
|
218
228
|
continue;
|
|
219
229
|
const text = candidate.content
|
|
220
|
-
.map((part) =>
|
|
230
|
+
.map((part) => isRecord(part) && part.type === "output_text" ? String(part.text ?? "") : "")
|
|
221
231
|
.join("");
|
|
222
232
|
if (text === slot.text)
|
|
223
233
|
output[index] = { ...candidate, phase: slot.phase };
|
|
@@ -225,10 +235,10 @@ function restorePortableAssistantPhases(input, messages, target) {
|
|
|
225
235
|
return output;
|
|
226
236
|
}
|
|
227
237
|
export function readDshPiReplayState(value) {
|
|
228
|
-
if (!
|
|
238
|
+
if (!isRecord(value))
|
|
229
239
|
throw invalidReplay("expected a replay envelope");
|
|
230
240
|
const responseValue = value.response;
|
|
231
|
-
if (!
|
|
241
|
+
if (!isRecord(responseValue) || responseValue.kind !== "pi-ai" || responseValue.version !== 2)
|
|
232
242
|
throw invalidReplay("expected a pi-ai version 2 response object");
|
|
233
243
|
const required = ["api", "provider", "model"];
|
|
234
244
|
for (const key of required)
|
|
@@ -243,7 +253,7 @@ export function readDshPiReplayState(value) {
|
|
|
243
253
|
if (!Array.isArray(value.blocks))
|
|
244
254
|
throw invalidReplay("blocks must be an array");
|
|
245
255
|
const blocks = value.blocks.map((block, index) => {
|
|
246
|
-
if (!
|
|
256
|
+
if (!isRecord(block) || replayBlockType(block.type) === undefined)
|
|
247
257
|
throw invalidReplay(`block ${index} has an unknown type`);
|
|
248
258
|
for (const signature of ["textSignature", "thinkingSignature", "thoughtSignature", "namespace"])
|
|
249
259
|
if (block[signature] !== undefined && typeof block[signature] !== "string")
|
|
@@ -367,6 +377,15 @@ async function prepareRequestImageVersions(messages, ctx, options) {
|
|
|
367
377
|
}));
|
|
368
378
|
return new Map(versions);
|
|
369
379
|
}
|
|
380
|
+
/** A full system prompt snapshot written by DSH's own system-prompt projection. */
|
|
381
|
+
export function isPromptSnapshotMessage(message) {
|
|
382
|
+
return message.role === "system" && message.source?.kind === "system-prompt";
|
|
383
|
+
}
|
|
384
|
+
/** Whether history after the leading head holds a non-empty DSH prompt snapshot (an in-history update). */
|
|
385
|
+
export function hasMidHistoryPromptSnapshot(messages) {
|
|
386
|
+
return messages.slice(1).some((message) => isPromptSnapshotMessage(message) &&
|
|
387
|
+
message.content.some((block) => block.type === "text" && block.text.length > 0));
|
|
388
|
+
}
|
|
370
389
|
function flattenMessageText(message) {
|
|
371
390
|
return message.content.map((block) => {
|
|
372
391
|
if (block.type !== "text")
|
|
@@ -379,11 +398,41 @@ async function dshToPiMessages(messages, ctx, options, imageMap, requestImages)
|
|
|
379
398
|
const toolNames = new Map();
|
|
380
399
|
for (const message of messages) {
|
|
381
400
|
if (message.role === "system") {
|
|
401
|
+
if (options.inHistorySystem === true) {
|
|
402
|
+
// In-history routes record a changed system prompt as a full later `system` message that keeps
|
|
403
|
+
// instruction authority. Pi renders a non-leading system message with the model's instruction
|
|
404
|
+
// role (developer for reasoning models, system otherwise) at this exact position.
|
|
405
|
+
const text = flattenMessageText(message);
|
|
406
|
+
if (text.length > 0) {
|
|
407
|
+
// A throwaway marker message directly before a DSH prompt snapshot lets the serializer trace the
|
|
408
|
+
// rendered item back to its source message without inspecting text; it is removed afterwards.
|
|
409
|
+
if (options.snapshotMarker !== undefined && isPromptSnapshotMessage(message))
|
|
410
|
+
result.push({ role: "user", content: options.snapshotMarker, timestamp: 0 });
|
|
411
|
+
result.push({ role: "system", content: text, timestamp: 0 });
|
|
412
|
+
}
|
|
413
|
+
continue;
|
|
414
|
+
}
|
|
382
415
|
result.push({ role: "user", content: flattenMessageText(message), timestamp: 0 });
|
|
383
416
|
continue;
|
|
384
417
|
}
|
|
385
|
-
if (message.role === "developer")
|
|
386
|
-
|
|
418
|
+
if (message.role === "developer") {
|
|
419
|
+
// Mirrors the host adapter's `appendDeveloper`: one tool-registry event becomes one system update that
|
|
420
|
+
// carries the added declarations; Pi renders it as `additional_tools` at this exact position.
|
|
421
|
+
if (options.anchoredTools === undefined)
|
|
422
|
+
throw unsupportedContent("developer message");
|
|
423
|
+
const toolsAdded = [];
|
|
424
|
+
for (const block of message.content) {
|
|
425
|
+
if (block.type !== "tool-addition")
|
|
426
|
+
throw unsupportedContent(block.type);
|
|
427
|
+
const tool = options.anchoredTools.find((candidate) => candidate.name === block.toolName);
|
|
428
|
+
if (tool === undefined)
|
|
429
|
+
throw codedError(`LCX tool update references undeclared tool "${block.toolName}"`, "LCX_RESPONSES_INVALID_INPUT");
|
|
430
|
+
toolsAdded.push(tool);
|
|
431
|
+
}
|
|
432
|
+
if (toolsAdded.length > 0)
|
|
433
|
+
result.push({ role: "system", content: [], toolsAdded, timestamp: 0 });
|
|
434
|
+
continue;
|
|
435
|
+
}
|
|
387
436
|
if (message.role === "assistant") {
|
|
388
437
|
const assistant = toPiAssistant(message, options.onReplayDegrade);
|
|
389
438
|
for (const block of assistant.content)
|
|
@@ -425,38 +474,52 @@ function builtinResponsesModel(provider, modelId) {
|
|
|
425
474
|
return resolveCatalogModel(provider, modelId).descriptor;
|
|
426
475
|
}
|
|
427
476
|
export function resolvePiResponsesModel(options) {
|
|
428
|
-
const route =
|
|
429
|
-
const explicit =
|
|
477
|
+
const route = isRecord(options.route) ? options.route : {};
|
|
478
|
+
const explicit = isRecord(options.model) ? options.model : {};
|
|
430
479
|
const provider = stringValue(explicit.provider, stringValue(route.provider, "dsh-lcx-codex"));
|
|
431
480
|
const id = stringValue(explicit.id, stringValue(route.model, "unknown"));
|
|
432
481
|
const builtin = builtinResponsesModel(provider, id);
|
|
433
|
-
const builtinRecord =
|
|
434
|
-
const compat = { ...(
|
|
482
|
+
const builtinRecord = isRecord(builtin) ? builtin : {};
|
|
483
|
+
const compat = { ...(isRecord(builtinRecord.compat) ? builtinRecord.compat : {}), ...(isRecord(options.responsesCompat) ? options.responsesCompat : {}), ...(isRecord(explicit.compat) ? explicit.compat : {}) };
|
|
435
484
|
const imageSupport = options.imageSupport === "supported" || options.imageSupport === "unsupported" ? options.imageSupport : "unknown";
|
|
436
485
|
const input = imageSupport === "supported" ? ["text", "image"] : imageSupport === "unsupported" ? ["text"] : Array.isArray(explicit.input) ? explicit.input : Array.isArray(builtinRecord.input) ? builtinRecord.input : ["text"];
|
|
437
486
|
return { ...builtinRecord, ...explicit, id, name: stringValue(explicit.name, stringValue(builtinRecord.name, id)), api: "openai-responses", provider, baseUrl: stringValue(explicit.baseUrl, stringValue(route.baseURL, stringValue(builtinRecord.baseUrl))), reasoning: typeof explicit.reasoning === "boolean" ? explicit.reasoning : typeof builtinRecord.reasoning === "boolean" ? builtinRecord.reasoning : true, input, cost: explicit.cost ?? builtinRecord.cost ?? { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 }, contextWindow: positiveSafeInteger(explicit.contextWindow ?? builtinRecord.contextWindow, 262144), maxTokens: positiveSafeInteger(explicit.maxTokens ?? builtinRecord.maxTokens, 32768), ...(Object.keys(compat).length > 0 ? { compat } : {}) };
|
|
438
487
|
}
|
|
439
488
|
export async function serializeDshMessages(messages, ctx, options = {}) {
|
|
489
|
+
const anchored = options.toolUpdate === "addition-only";
|
|
490
|
+
if (anchored && options.systemPromptUpdate !== "in-history")
|
|
491
|
+
throw codedError("LCX addition-only tool updates require an in-history route", "LCX_RESPONSES_INVALID_INPUT");
|
|
440
492
|
for (const message of messages) {
|
|
441
|
-
if (message.role === "developer")
|
|
493
|
+
if (message.role === "developer" && !anchored)
|
|
442
494
|
throw unsupportedContent("developer message");
|
|
443
495
|
for (const block of message.content)
|
|
444
|
-
if (block.type === "tool-addition" || block.type === "tool-removal")
|
|
445
|
-
|
|
496
|
+
if (block.type === "tool-addition" || block.type === "tool-removal") {
|
|
497
|
+
// Removals never reach a serializer: the host projection drops them on an addition-only route.
|
|
498
|
+
if (!anchored || message.role !== "developer" || block.type === "tool-removal")
|
|
499
|
+
throw unsupportedContent(block.type);
|
|
500
|
+
}
|
|
446
501
|
}
|
|
447
502
|
for (const tool of options.tools ?? [])
|
|
448
|
-
if ("deferLoading" in tool && tool.deferLoading === true)
|
|
503
|
+
if (!anchored && "deferLoading" in tool && tool.deferLoading === true)
|
|
449
504
|
throw unsupportedContent("deferLoading tool declaration");
|
|
505
|
+
// Declared = every tool the history may reference; immediate = the subset that stays in the request `tools`.
|
|
506
|
+
const declaredTools = (Array.isArray(options.tools) ? options.tools : []).map((tool) => withoutDeferLoading(tool));
|
|
507
|
+
const immediateDeclared = (Array.isArray(options.tools) ? options.tools : [])
|
|
508
|
+
.filter((tool) => !("deferLoading" in tool && tool.deferLoading === true))
|
|
509
|
+
.map((tool) => withoutDeferLoading(tool));
|
|
450
510
|
const normalized = {
|
|
451
511
|
imageSupport: options.imageSupport === "supported" || options.imageSupport === "unsupported" ? options.imageSupport : "unknown",
|
|
452
512
|
signal: options.signal instanceof AbortSignal ? options.signal : undefined,
|
|
453
513
|
route: options.route ?? {}, model: options.model, responsesCompat: options.responsesCompat,
|
|
454
514
|
systemPrompt: typeof options.systemPrompt === "string" ? options.systemPrompt : undefined,
|
|
455
515
|
includeSystemPrompt: options.includeSystemPrompt === true, onReplayDegrade: options.onReplayDegrade,
|
|
516
|
+
inHistorySystem: options.systemPromptUpdate === "in-history",
|
|
517
|
+
snapshotMarker: options.systemPromptUpdate === "in-history" ? `\u0000lcx-prompt-snapshot:${globalThis.crypto.randomUUID()}` : undefined,
|
|
456
518
|
maxRequestImageBytes: positiveSafeInteger(options.maxRequestImageBytes, DEFAULT_MAX_REQUEST_IMAGE_BYTES),
|
|
457
519
|
requestImagePixelBudget: positiveSafeInteger(options.requestImagePixelBudget, DEFAULT_REQUEST_IMAGE_PIXEL_BUDGET),
|
|
458
520
|
requestImageMaxBytes: positiveSafeInteger(options.requestImageMaxBytes, DEFAULT_REQUEST_IMAGE_MAX_BYTES),
|
|
459
|
-
tools:
|
|
521
|
+
tools: immediateDeclared,
|
|
522
|
+
...(anchored ? { anchoredTools: declaredTools } : {}),
|
|
460
523
|
};
|
|
461
524
|
const imageMap = new Map();
|
|
462
525
|
const fileProjected = projectFiles(messages, ctx);
|
|
@@ -478,44 +541,82 @@ export async function serializeDshMessages(messages, ctx, options = {}) {
|
|
|
478
541
|
const projected = projectOffloadedImages(fileProjected, (ref) => offloadedImageText(ref, attachments === undefined ? undefined : imageAccess(ctx, attachments, ref)));
|
|
479
542
|
const model = resolvePiResponsesModel(normalized);
|
|
480
543
|
const dshContext = { systemPrompt: normalized.systemPrompt, messages: await dshToPiMessages(projected, ctx, normalized, imageMap, requestImages), tools: [...normalized.tools] };
|
|
481
|
-
const compat =
|
|
544
|
+
const compat = isRecord(model.compat) ? model.compat : {};
|
|
482
545
|
const supportsStrictMode = compat.supportsStrictMode === true;
|
|
483
546
|
const supportsOpenAIGrammarTools = compat.supportsOpenAIGrammarTools === true;
|
|
484
547
|
const supportsAdditionalTools = compat.supportsAdditionalTools === true;
|
|
485
548
|
const supportsToolSearch = compat.supportsToolSearch === true;
|
|
486
549
|
const deferredToolsMode = supportsAdditionalTools ? "additional-tools" : supportsToolSearch ? "tool-search" : undefined;
|
|
487
|
-
const grammarToolInputProperties = createGrammarToolInputProperties(dshContext.tools, supportsOpenAIGrammarTools);
|
|
488
|
-
//
|
|
550
|
+
const grammarToolInputProperties = createGrammarToolInputProperties([...(normalized.anchoredTools ?? dshContext.tools)], supportsOpenAIGrammarTools);
|
|
551
|
+
// Immediate declarations only: on an anchored surface deferred tools are declared by their history additions.
|
|
489
552
|
const immediateTools = new Map();
|
|
490
553
|
for (const tool of dshContext.tools)
|
|
491
554
|
if (tool.name)
|
|
492
555
|
immediateTools.set(tool.name, tool);
|
|
493
556
|
const toolOptions = { supportsStrictMode, supportsOpenAIGrammarTools, toolSearchResult: false };
|
|
494
|
-
const
|
|
557
|
+
const normalizedContext = normalizeContext(dshContext);
|
|
558
|
+
// Pi treats whichever system message is first in the transcript as the leading prompt: it skips it when
|
|
559
|
+
// includeSystemPrompt is false and renders it without the update framing. A serialization batch carrying
|
|
560
|
+
// no caller-owned head (no systemPrompt/tools) must never promote a history update to that slot, so an
|
|
561
|
+
// empty placeholder holds the leading position and renders nothing.
|
|
562
|
+
const piContext = normalized.inHistorySystem && normalizedContext.messages[0]?.role === "system" && normalizedContext.messages === dshContext.messages
|
|
563
|
+
? { ...normalizedContext, messages: [{ role: "system", content: "", timestamp: 0 }, ...normalizedContext.messages] }
|
|
564
|
+
: normalizedContext;
|
|
495
565
|
const piModel = model;
|
|
496
|
-
|
|
497
|
-
|
|
566
|
+
// Pi collapses every system message into the leading one unless the transport accepts mid-conversation
|
|
567
|
+
// system messages, which would drop or reorder in-history updates in batched serialization.
|
|
568
|
+
const convertedInput = convertResponsesMessages(piModel, piContext, new Set(["openai", "openai-codex", "opencode"]), { includeSystemPrompt: normalized.includeSystemPrompt, grammarToolInputProperties, supportsAdditionalTools, supportsToolSearch, supportsMidConvoSystemMessages: normalized.inHistorySystem, toolOptions });
|
|
569
|
+
const supersededInstructionItems = [];
|
|
570
|
+
const unmarkedInput = [];
|
|
571
|
+
let markersFound = 0;
|
|
572
|
+
for (let position = 0; position < convertedInput.length; position += 1) {
|
|
573
|
+
const item = convertedInput[position];
|
|
574
|
+
if (normalized.snapshotMarker !== undefined && isMarkerItem(item, normalized.snapshotMarker)) {
|
|
575
|
+
markersFound += 1;
|
|
576
|
+
const rendered = convertedInput[position + 1];
|
|
577
|
+
if (!isRecord(rendered) || (rendered.role !== "developer" && rendered.role !== "system"))
|
|
578
|
+
throw codedError("LCX could not trace a rendered system prompt snapshot", "LCX_RESPONSES_INVALID_INPUT");
|
|
579
|
+
supersededInstructionItems.push(unmarkedInput.length);
|
|
580
|
+
continue;
|
|
581
|
+
}
|
|
582
|
+
unmarkedInput.push(item);
|
|
583
|
+
}
|
|
584
|
+
if (normalized.snapshotMarker !== undefined) {
|
|
585
|
+
// Markers are inserted only before non-empty snapshots (empty ones render nothing), so each inserted
|
|
586
|
+
// marker must be found exactly once and none may survive in the output, even if a future Pi merged
|
|
587
|
+
// adjacent user messages.
|
|
588
|
+
const inserted = dshContext.messages.filter((message) => message.role === "user" && message.content === normalized.snapshotMarker).length;
|
|
589
|
+
const nonce = normalized.snapshotMarker.slice(normalized.snapshotMarker.lastIndexOf(":") + 1);
|
|
590
|
+
if (markersFound !== inserted || JSON.stringify(unmarkedInput).includes(nonce))
|
|
591
|
+
throw codedError("LCX could not remove a system prompt snapshot marker", "LCX_RESPONSES_INVALID_INPUT");
|
|
592
|
+
}
|
|
593
|
+
const input = restorePortableAssistantPhases(unmarkedInput, dshContext.messages, { provider: model.provider, id: model.id, api: model.api });
|
|
498
594
|
const tools = options.tools === undefined ? undefined : convertResponsesTools([...immediateTools.values()], toolOptions);
|
|
499
|
-
return { input, imageMap, tools, model: piModel, grammarToolInputProperties, deferredToolsMode };
|
|
595
|
+
return { input, supersededInstructionItems, imageMap, tools, model: piModel, grammarToolInputProperties, deferredToolsMode };
|
|
596
|
+
}
|
|
597
|
+
function isMarkerItem(item, marker) {
|
|
598
|
+
if (!isRecord(item) || item.role !== "user" || !Array.isArray(item.content))
|
|
599
|
+
return false;
|
|
600
|
+
return item.content.length === 1 && isRecord(item.content[0]) && item.content[0].text === marker;
|
|
500
601
|
}
|
|
501
602
|
export function responsesTools(tools) {
|
|
502
603
|
if (tools === undefined)
|
|
503
604
|
return undefined;
|
|
504
605
|
if (!Array.isArray(tools))
|
|
505
|
-
throw
|
|
506
|
-
if (tools.every((tool) =>
|
|
606
|
+
throw codedError("LCX Responses tools must be an array", "LCX_COMPACT_INVALID_TOOLS");
|
|
607
|
+
if (tools.every((tool) => isRecord(tool) && typeof tool.type === "string"))
|
|
507
608
|
return structuredClone(tools);
|
|
508
609
|
for (const tool of tools)
|
|
509
|
-
if (!
|
|
510
|
-
throw
|
|
610
|
+
if (!isRecord(tool) || typeof tool.name !== "string" || !tool.name)
|
|
611
|
+
throw codedError("LCX Responses tool has no name", "LCX_COMPACT_INVALID_TOOLS");
|
|
511
612
|
return convertResponsesTools(tools, { supportsStrictMode: false, supportsOpenAIGrammarTools: false });
|
|
512
613
|
}
|
|
513
614
|
function isResponsesMessageItem(item) {
|
|
514
|
-
return
|
|
615
|
+
return isRecord(item) && typeof item.role === "string" && (item.type === undefined || item.type === "message");
|
|
515
616
|
}
|
|
516
617
|
function mapImageParts(value, mapper) {
|
|
517
618
|
return value.map((part) => {
|
|
518
|
-
if (!
|
|
619
|
+
if (!isRecord(part))
|
|
519
620
|
return structuredClone(part);
|
|
520
621
|
if (part.type === "input_image" || part.type === "dsh_image_attachment")
|
|
521
622
|
return mapper(part);
|
|
@@ -526,17 +627,17 @@ function mapImageParts(value, mapper) {
|
|
|
526
627
|
}
|
|
527
628
|
export function persistNativeImageReferences(output, imageMap) {
|
|
528
629
|
const persist = (part) => {
|
|
529
|
-
if (part.type === "dsh_image_attachment" &&
|
|
630
|
+
if (part.type === "dsh_image_attachment" && isRecord(part.attachment))
|
|
530
631
|
return structuredClone(part);
|
|
531
632
|
if (part.type !== "input_image" || typeof part.image_url !== "string")
|
|
532
|
-
throw
|
|
633
|
+
throw codedError("native compact returned an invalid image item", "LCX_COMPACT_INVALID_RESPONSE");
|
|
533
634
|
const ref = imageMap.get(part.image_url);
|
|
534
635
|
if (ref === undefined)
|
|
535
|
-
throw
|
|
636
|
+
throw codedError("native compact returned an untracked image item", "LCX_COMPACT_INVALID_RESPONSE");
|
|
536
637
|
return { type: "dsh_image_attachment", attachment: structuredClone(ref) };
|
|
537
638
|
};
|
|
538
639
|
return output.map((item) => {
|
|
539
|
-
if (!
|
|
640
|
+
if (!isRecord(item))
|
|
540
641
|
return structuredClone(item);
|
|
541
642
|
if (isResponsesMessageItem(item) && Array.isArray(item.content))
|
|
542
643
|
return { ...structuredClone(item), content: mapImageParts(item.content, persist) };
|
|
@@ -546,11 +647,11 @@ export function persistNativeImageReferences(output, imageMap) {
|
|
|
546
647
|
});
|
|
547
648
|
}
|
|
548
649
|
async function hydratePart(part, ctx, options) {
|
|
549
|
-
if (!
|
|
650
|
+
if (!isRecord(part) || part.type !== "dsh_image_attachment")
|
|
550
651
|
return structuredClone(part);
|
|
551
652
|
const attachment = part.attachment;
|
|
552
653
|
if (!hasImageAttachment(attachment))
|
|
553
|
-
throw
|
|
654
|
+
throw codedError("native compact returned an invalid image attachment", "LCX_COMPACT_INVALID_RESPONSE");
|
|
554
655
|
if (options.imageSupport === "unsupported")
|
|
555
656
|
return { type: "input_text", text: "[image omitted because the target model does not support image input]" };
|
|
556
657
|
return imagePart({ attachment }, ctx, options, options.imageMap);
|
|
@@ -564,7 +665,7 @@ export function inputImageBase64Lengths(input) {
|
|
|
564
665
|
visit(child);
|
|
565
666
|
return;
|
|
566
667
|
}
|
|
567
|
-
if (!
|
|
668
|
+
if (!isRecord(value))
|
|
568
669
|
return;
|
|
569
670
|
if (value.type === "input_image" && typeof value.image_url === "string") {
|
|
570
671
|
const url = value.image_url;
|
|
@@ -604,7 +705,7 @@ export function assertAggregateImageBudget(options) {
|
|
|
604
705
|
if (removed >= excess) {
|
|
605
706
|
throw new LlmError(`LCX request images exceed the configured base64 bound once checkpoint images are counted; ${count} more oldest occurrence(s) must be offloaded.`, IMAGE_OFFLOAD_REQUIRED_CODE, { offloadImages: count });
|
|
606
707
|
}
|
|
607
|
-
throw
|
|
708
|
+
throw codedError(`Images retained in the compaction checkpoint (${options.fixedBytes} base64 bytes) leave no room within the ${options.maxBytes}-byte request image bound, and offloading new images cannot recover it; start a new session or raise maxRequestImageBytes`, "LCX_CHECKPOINT_IMAGE_BUDGET");
|
|
608
709
|
}
|
|
609
710
|
export async function hydrateNativeImageReferences(output, ctx, options = {}) {
|
|
610
711
|
const normalized = {
|
|
@@ -617,7 +718,7 @@ export async function hydrateNativeImageReferences(output, ctx, options = {}) {
|
|
|
617
718
|
};
|
|
618
719
|
const result = [];
|
|
619
720
|
for (const item of asArray(output)) {
|
|
620
|
-
if (!
|
|
721
|
+
if (!isRecord(item)) {
|
|
621
722
|
result.push(structuredClone(item));
|
|
622
723
|
continue;
|
|
623
724
|
}
|