dsh-lcx-codex 0.6.0 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +62 -165
- package/THIRD_PARTY_NOTICES.md +5 -2
- package/lib/client.js +75 -3
- package/lib/compact-v2.js +8 -2
- package/lib/defaults.js +1 -0
- package/lib/dsh-compat.js +5 -5
- package/lib/dsh-responses.js +268 -44
- package/lib/grok-native-search.js +18 -1
- package/lib/index.js +704 -73
- package/lib/model-catalog.js +16 -3
- package/lib/native-checkpoint.js +13 -1
- package/lib/pi-responses-runtime.js +151 -21
- package/lib/responses-request.js +49 -15
- package/lib/responses-stream.js +40 -7
- package/lib/route.js +358 -38
- package/lib/search-usage.js +7 -5
- package/lib/tool-surface.js +41 -8
- package/lib/types/index.d.ts +8 -0
- package/package.json +40 -36
package/lib/responses-request.js
CHANGED
|
@@ -22,29 +22,48 @@ function normalizeCacheRetention(value) {
|
|
|
22
22
|
? normalized
|
|
23
23
|
: "short";
|
|
24
24
|
}
|
|
25
|
+
function isOpenAIDirectSignInTarget(model) {
|
|
26
|
+
return (isRecord(model) &&
|
|
27
|
+
model.provider === "openai" &&
|
|
28
|
+
model.baseUrl === "https://api.openai.com/v1");
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Matches Pi's `isChatGPTSignIn`: direct `openai` against `https://api.openai.com/v1` with a non-`sk-` token.
|
|
32
|
+
*/
|
|
33
|
+
export function isChatGPTSignInCredential(model, apiKey) {
|
|
34
|
+
if (!isRecord(model))
|
|
35
|
+
return false;
|
|
36
|
+
return (isOpenAIDirectSignInTarget(model) &&
|
|
37
|
+
apiKey !== undefined &&
|
|
38
|
+
!apiKey.startsWith("sk-"));
|
|
39
|
+
}
|
|
25
40
|
/**
|
|
26
41
|
* The effort Pi 1.3.0 sends as `configuration_update` input items instead of `reasoning.effort`, or undefined
|
|
27
42
|
* to send it as `reasoning.effort` (Pi's `getConfigurationUpdateEffort`): only for a model whose compat states
|
|
28
43
|
* `supportsMidConvoEffort`, only with a requested level, and never for a Sign in with ChatGPT key, which rejects
|
|
29
44
|
* those items. The key is read only when that last rule can apply.
|
|
30
45
|
*/
|
|
31
|
-
export async function configurationUpdateEffort(model, reasoningEffort, apiKey) {
|
|
32
|
-
if (
|
|
46
|
+
export async function configurationUpdateEffort(model, reasoningEffort, apiKey, options) {
|
|
47
|
+
if (options?.gatewayCompatibilityFallback === true ||
|
|
48
|
+
model.compat?.supportsMidConvoEffort !== true ||
|
|
49
|
+
reasoningEffort === undefined ||
|
|
50
|
+
reasoningEffort === "off")
|
|
33
51
|
return undefined;
|
|
34
52
|
const requested = String(reasoningEffort);
|
|
35
53
|
const mapped = model.thinkingLevelMap?.[requested];
|
|
36
54
|
// A level the model maps to null is unsupported; DSH refuses it before dispatch, so it never travels here.
|
|
37
55
|
if (mapped === null)
|
|
38
56
|
return undefined;
|
|
39
|
-
if (model
|
|
57
|
+
if (isOpenAIDirectSignInTarget(model) && isChatGPTSignInCredential(model, await apiKey()))
|
|
40
58
|
return undefined;
|
|
41
59
|
return mapped ?? requested;
|
|
42
60
|
}
|
|
43
61
|
/**
|
|
44
62
|
* Pi-parity generation controls for OpenAI Responses.
|
|
45
63
|
*/
|
|
46
|
-
export function responsesGenerationEnvelope({ model = "unknown", reasoningEffort, temperature, maxTokens, configurationUpdateEffort, includeDefaultReasoning = false, } = {}) {
|
|
64
|
+
export function responsesGenerationEnvelope({ model = "unknown", reasoningEffort, temperature, maxTokens, configurationUpdateEffort, gatewayCompatibilityFallback = false, omitUnsupportedFields = false, apiKey, includeDefaultReasoning = false, } = {}) {
|
|
47
65
|
const descriptor = modelRecord(model);
|
|
66
|
+
const suppressChatGptUnsupported = omitUnsupportedFields === true || isChatGPTSignInCredential(descriptor, apiKey);
|
|
48
67
|
const result = {};
|
|
49
68
|
if (descriptor.reasoning !== false) {
|
|
50
69
|
if (descriptor.provider === "xai" ||
|
|
@@ -59,7 +78,11 @@ export function responsesGenerationEnvelope({ model = "unknown", reasoningEffort
|
|
|
59
78
|
if (wire !== null) {
|
|
60
79
|
// With mid-conversation effort the request carries a fixed effort, so an effort change keeps the
|
|
61
80
|
// cached prefix; the level itself is the last `configuration_update` input item.
|
|
62
|
-
|
|
81
|
+
const useConfigurationUpdate = !gatewayCompatibilityFallback && configurationUpdateEffort !== undefined;
|
|
82
|
+
result.reasoning = {
|
|
83
|
+
effort: useConfigurationUpdate ? CONFIGURATION_UPDATE_REQUEST_EFFORT : wire,
|
|
84
|
+
summary: "auto",
|
|
85
|
+
};
|
|
63
86
|
result.include = ["reasoning.encrypted_content"];
|
|
64
87
|
}
|
|
65
88
|
}
|
|
@@ -74,37 +97,46 @@ export function responsesGenerationEnvelope({ model = "unknown", reasoningEffort
|
|
|
74
97
|
throw Object.assign(new Error("Responses temperature must be finite"), {
|
|
75
98
|
code: "LCX_RESPONSES_INVALID_INPUT",
|
|
76
99
|
});
|
|
77
|
-
|
|
100
|
+
if (!suppressChatGptUnsupported)
|
|
101
|
+
result.temperature = Number(temperature);
|
|
78
102
|
}
|
|
79
103
|
if (maxTokens !== undefined) {
|
|
80
104
|
if (!Number.isSafeInteger(maxTokens) || Number(maxTokens) <= 0)
|
|
81
105
|
throw Object.assign(new Error("Responses maxTokens must be a positive safe integer"), { code: "LCX_RESPONSES_INVALID_INPUT" });
|
|
82
|
-
if (descriptor.compat?.supportsMaxOutputTokens !== false)
|
|
106
|
+
if (!suppressChatGptUnsupported && descriptor.compat?.supportsMaxOutputTokens !== false)
|
|
83
107
|
result.max_output_tokens = Math.max(OPENAI_RESPONSES_MIN_OUTPUT_TOKENS, Number(maxTokens));
|
|
84
108
|
}
|
|
85
109
|
return result;
|
|
86
110
|
}
|
|
87
111
|
/**
|
|
88
|
-
* Build the shared LCX-owned request envelope while retaining Pi
|
|
112
|
+
* Build the shared LCX-owned request envelope while retaining Pi Responses semantics.
|
|
89
113
|
* Production LCX ordinary/compact/replay construction places the DSH system prompt in canonical input.
|
|
90
114
|
*/
|
|
91
|
-
export function buildResponsesBody({ model, input, tools, sessionId, promptCacheKey, promptCacheRetention, cacheRetention, reasoningEffort, temperature, maxTokens, configurationUpdateEffort, samplingParams, }) {
|
|
115
|
+
export function buildResponsesBody({ model, input, tools, sessionId, promptCacheKey, promptCacheRetention, cacheRetention, reasoningEffort, temperature, maxTokens, configurationUpdateEffort, gatewayCompatibilityFallback, omitUnsupportedFields, apiKey, samplingParams, }) {
|
|
92
116
|
if (!Array.isArray(input))
|
|
93
117
|
throw Object.assign(new Error("Responses input must be an array"), {
|
|
94
118
|
code: "LCX_RESPONSES_INVALID_INPUT",
|
|
95
119
|
});
|
|
96
120
|
const descriptor = modelRecord(model);
|
|
121
|
+
const suppressChatGptUnsupported = omitUnsupportedFields === true || isChatGPTSignInCredential(descriptor, apiKey);
|
|
97
122
|
const retention = normalizeCacheRetention(cacheRetention);
|
|
98
123
|
const compat = descriptor.compat ?? {};
|
|
99
|
-
const
|
|
124
|
+
const supportsLongCache = compat.supportsLongCacheRetention !== false;
|
|
125
|
+
const sendPromptCacheKey = retention !== "none" && compat.promptCacheKeyMode !== "disabled";
|
|
126
|
+
const cacheKey = !sendPromptCacheKey
|
|
100
127
|
? undefined
|
|
101
128
|
: (promptCacheKey ?? clampOpenAIPromptCacheKey(sessionId));
|
|
102
|
-
const longRetention = retention === "long" && compat.supportsExplicitPromptCacheMode !== true
|
|
103
|
-
? promptCacheRetention ?? (
|
|
129
|
+
const longRetention = !suppressChatGptUnsupported && retention === "long" && compat.supportsExplicitPromptCacheMode !== true
|
|
130
|
+
? promptCacheRetention ?? (supportsLongCache ? "24h" : undefined)
|
|
104
131
|
: undefined;
|
|
105
|
-
// Pi
|
|
106
|
-
const currentCache =
|
|
107
|
-
|
|
132
|
+
// Pi uses explicit 30m only for long retention; short is implicit.
|
|
133
|
+
const currentCache = !suppressChatGptUnsupported &&
|
|
134
|
+
retention === "long" &&
|
|
135
|
+
supportsLongCache &&
|
|
136
|
+
compat.supportsExplicitPromptCacheMode === true;
|
|
137
|
+
const explicitCache = !suppressChatGptUnsupported &&
|
|
138
|
+
retention === "none" &&
|
|
139
|
+
compat.supportsExplicitPromptCacheMode === true;
|
|
108
140
|
const promptCacheOptions = currentCache
|
|
109
141
|
? { ttl: "30m" }
|
|
110
142
|
: explicitCache
|
|
@@ -128,6 +160,8 @@ export function buildResponsesBody({ model, input, tools, sessionId, promptCache
|
|
|
128
160
|
temperature,
|
|
129
161
|
maxTokens,
|
|
130
162
|
configurationUpdateEffort,
|
|
163
|
+
gatewayCompatibilityFallback,
|
|
164
|
+
omitUnsupportedFields: suppressChatGptUnsupported,
|
|
131
165
|
includeDefaultReasoning: true,
|
|
132
166
|
}),
|
|
133
167
|
};
|
package/lib/responses-stream.js
CHANGED
|
@@ -3,7 +3,8 @@ import { abortError } from "./abort-error.js";
|
|
|
3
3
|
import { IMAGE_OFFLOAD_REQUIRED_CODE, ToolCallId, isContextWindowExceededError } from "@deepseek-ai/dsh-llm";
|
|
4
4
|
import { fetchSseWithRetry, readSseEvents } from "./transport.js";
|
|
5
5
|
import { sanitizeContextDetails } from "./search-accounting.js";
|
|
6
|
-
import {
|
|
6
|
+
import { baseURLFingerprint } from "./route.js";
|
|
7
|
+
import { createGrokNativeReplayEnvelope, grokNativeReplayRouteFingerprint, grokPendingSourcesStart, sanitizeGrokVisibleText, } from "./grok-native-search.js";
|
|
7
8
|
import { codedError, isPositiveFinite, isRecord } from "./util.js";
|
|
8
9
|
import { DEFAULTS } from "./defaults.js";
|
|
9
10
|
/** Durable LCX-only pressure evidence; canonical usage stays on Pi/DSH billing. */
|
|
@@ -183,7 +184,11 @@ export function managedFailure(error, signal, upstream) {
|
|
|
183
184
|
/time(?:d)?\s*out|timeout/iu.test(String(signalReason?.message ?? "")));
|
|
184
185
|
const upstreamClass = signal?.aborted ? undefined : classifyUpstreamFailure(upstream);
|
|
185
186
|
let code = "RESPONSES_ERROR";
|
|
186
|
-
if (
|
|
187
|
+
if (sourceCodes.has("REQUEST_HEADERS"))
|
|
188
|
+
code = "REQUEST_HEADERS";
|
|
189
|
+
else if (sourceCodes.has("MALFORMED_RESPONSE"))
|
|
190
|
+
code = "MALFORMED_RESPONSE";
|
|
191
|
+
else if (upstreamClass?.definite === true)
|
|
187
192
|
code = upstreamClass.code;
|
|
188
193
|
else if (signalTimedOut ||
|
|
189
194
|
facts.status === 408 ||
|
|
@@ -246,6 +251,8 @@ export function managedFailure(error, signal, upstream) {
|
|
|
246
251
|
if (code === "RESPONSES_ERROR" && upstreamClass !== undefined)
|
|
247
252
|
code = upstreamClass.code;
|
|
248
253
|
const messages = {
|
|
254
|
+
MALFORMED_RESPONSE: "Responses provider completed a tool call with malformed arguments",
|
|
255
|
+
REQUEST_HEADERS: error instanceof Error ? error.message : "Request header preparation failed",
|
|
249
256
|
ABORTED: "Responses request was aborted",
|
|
250
257
|
AUTH: "Responses request was rejected by authentication",
|
|
251
258
|
INSUFFICIENT_QUOTA: "服务商额度不足或未满足请求预留额度,请检查中转额度(并非 API 密钥无效) / Provider quota is insufficient for this request",
|
|
@@ -1113,7 +1120,7 @@ function rawArguments(value) {
|
|
|
1113
1120
|
return "{}";
|
|
1114
1121
|
}
|
|
1115
1122
|
}
|
|
1116
|
-
function replayState(message, nativeOutput, nativeReplayRoute, nativeServerSearchEchoCallIds = []) {
|
|
1123
|
+
function replayState(message, nativeOutput, nativeReplayRoute, nativeServerSearchEchoCallIds = [], baseURL, sessionId, headerAuthorityFingerprint) {
|
|
1117
1124
|
if (!isRecord(message))
|
|
1118
1125
|
return undefined;
|
|
1119
1126
|
const content = Array.isArray(message.content)
|
|
@@ -1125,6 +1132,24 @@ function replayState(message, nativeOutput, nativeReplayRoute, nativeServerSearc
|
|
|
1125
1132
|
if (!provider || !model || !api)
|
|
1126
1133
|
return undefined;
|
|
1127
1134
|
const grokNative = createGrokNativeReplayEnvelope(nativeOutput, nativeReplayRoute, nativeServerSearchEchoCallIds);
|
|
1135
|
+
const normalizedBaseUrl = typeof baseURL === "string" ? baseURL.trim() : "";
|
|
1136
|
+
const normalizedSessionId = typeof sessionId === "string" ? sessionId : "";
|
|
1137
|
+
const lcxReplay = normalizedBaseUrl &&
|
|
1138
|
+
normalizedSessionId &&
|
|
1139
|
+
typeof headerAuthorityFingerprint === "string" &&
|
|
1140
|
+
headerAuthorityFingerprint.length > 0
|
|
1141
|
+
? {
|
|
1142
|
+
version: 1,
|
|
1143
|
+
baseURLFingerprint: baseURLFingerprint(normalizedBaseUrl),
|
|
1144
|
+
sourceSessionId: normalizedSessionId,
|
|
1145
|
+
headerAuthorityFingerprint,
|
|
1146
|
+
...(nativeReplayRoute
|
|
1147
|
+
? {
|
|
1148
|
+
grokRouteAuthorityFingerprint: grokNativeReplayRouteFingerprint(nativeReplayRoute),
|
|
1149
|
+
}
|
|
1150
|
+
: {}),
|
|
1151
|
+
}
|
|
1152
|
+
: undefined;
|
|
1128
1153
|
return {
|
|
1129
1154
|
response: {
|
|
1130
1155
|
kind: "pi-ai",
|
|
@@ -1171,6 +1196,7 @@ function replayState(message, nativeOutput, nativeReplayRoute, nativeServerSearc
|
|
|
1171
1196
|
: {}),
|
|
1172
1197
|
};
|
|
1173
1198
|
}),
|
|
1199
|
+
...(lcxReplay === undefined ? {} : { lcxReplay }),
|
|
1174
1200
|
...(grokNative === undefined ? {} : { grokNative }),
|
|
1175
1201
|
};
|
|
1176
1202
|
}
|
|
@@ -1198,8 +1224,9 @@ function successfulFinish(message) {
|
|
|
1198
1224
|
/**
|
|
1199
1225
|
* Pi event vocabulary -> DSH StreamChunk. This remains thin and provider-neutral.
|
|
1200
1226
|
*/
|
|
1201
|
-
async function* toDshChunks(events, signal, wireMeta, nativeReplayRoute) {
|
|
1227
|
+
async function* toDshChunks(events, signal, wireMeta, nativeReplayRoute, baseURL, sessionId, headerAuthorityFingerprint) {
|
|
1202
1228
|
const toolIds = new Map();
|
|
1229
|
+
let argumentsError = false;
|
|
1203
1230
|
for await (const event of events) {
|
|
1204
1231
|
switch (event.type) {
|
|
1205
1232
|
case "start":
|
|
@@ -1282,6 +1309,10 @@ async function* toDshChunks(events, signal, wireMeta, nativeReplayRoute) {
|
|
|
1282
1309
|
const call = isRecord(event.toolCall)
|
|
1283
1310
|
? event.toolCall
|
|
1284
1311
|
: {};
|
|
1312
|
+
if (call.argumentsError !== undefined) {
|
|
1313
|
+
argumentsError = true;
|
|
1314
|
+
break;
|
|
1315
|
+
}
|
|
1285
1316
|
yield {
|
|
1286
1317
|
type: "block-end",
|
|
1287
1318
|
index: Number(event.contentIndex),
|
|
@@ -1299,10 +1330,12 @@ async function* toDshChunks(events, signal, wireMeta, nativeReplayRoute) {
|
|
|
1299
1330
|
? event.message
|
|
1300
1331
|
: {};
|
|
1301
1332
|
yield { type: "usage", usage: dshUsage(message.usage) };
|
|
1333
|
+
if (argumentsError && message.stopReason !== "length")
|
|
1334
|
+
throw codedError("Responses provider completed a tool call with malformed arguments", "MALFORMED_RESPONSE");
|
|
1302
1335
|
const reason = successfulFinish(message);
|
|
1303
1336
|
const replay = reason.kind === "error"
|
|
1304
1337
|
? undefined
|
|
1305
|
-
: replayState(message, wireMeta?.nativeOutput, nativeReplayRoute, wireMeta?.nativeServerSearchEchoCallIds);
|
|
1338
|
+
: replayState(message, wireMeta?.nativeOutput, nativeReplayRoute, wireMeta?.nativeServerSearchEchoCallIds, baseURL, sessionId, headerAuthorityFingerprint);
|
|
1306
1339
|
yield {
|
|
1307
1340
|
type: "finish",
|
|
1308
1341
|
reason,
|
|
@@ -1352,7 +1385,7 @@ async function* toDshChunks(events, signal, wireMeta, nativeReplayRoute) {
|
|
|
1352
1385
|
* Send one LCX-owned OpenAI Responses request. Ordinary and replay use one provider attempt;
|
|
1353
1386
|
* the DSH agent recovery layer remains the visible retry owner.
|
|
1354
1387
|
*/
|
|
1355
|
-
export async function* streamResponsesRequest({ baseURL, provider, model, piModel, body, grammarToolInputProperties, headers, signal, timeoutMs, applyDefaultTimeout = true, streamIdleTimeoutMs, maxAttempts = 1, maxResponseBytes, serverToolTypes, isServerToolItem, declaredToolNames, nativeReplayRoute, providerThinkingLevel, onServerToolUsage, onFailure, }) {
|
|
1388
|
+
export async function* streamResponsesRequest({ baseURL, provider, model, piModel, body, grammarToolInputProperties, headers, signal, timeoutMs, applyDefaultTimeout = true, streamIdleTimeoutMs, maxAttempts = 1, maxResponseBytes, serverToolTypes, isServerToolItem, declaredToolNames, sessionId, headerAuthorityFingerprint, nativeReplayRoute, providerThinkingLevel, onServerToolUsage, onFailure, }) {
|
|
1356
1389
|
let contentEmitted = false;
|
|
1357
1390
|
let failureReported = false;
|
|
1358
1391
|
/** One redacted report per request; user aborts are not failures. */
|
|
@@ -1436,7 +1469,7 @@ export async function* streamResponsesRequest({ baseURL, provider, model, piMode
|
|
|
1436
1469
|
piEvents.end();
|
|
1437
1470
|
}
|
|
1438
1471
|
})();
|
|
1439
|
-
const chunks = toDshChunks(piEvents, signal, wireMeta, nativeReplayRoute)[Symbol.asyncIterator]();
|
|
1472
|
+
const chunks = toDshChunks(piEvents, signal, wireMeta, nativeReplayRoute, baseURL, sessionId, headerAuthorityFingerprint)[Symbol.asyncIterator]();
|
|
1440
1473
|
let exhausted = false;
|
|
1441
1474
|
try {
|
|
1442
1475
|
while (true) {
|