dsh-lcx-codex 0.7.0-pre.1 → 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.
@@ -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 (model.compat?.supportsMidConvoEffort !== true || reasoningEffort === undefined || reasoningEffort === "off")
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.provider === "openai" && model.baseUrl === "https://api.openai.com/v1" && !(await apiKey()).startsWith("sk-"))
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
- result.reasoning = { effort: configurationUpdateEffort === undefined ? wire : CONFIGURATION_UPDATE_REQUEST_EFFORT, summary: "auto" };
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
- result.temperature = Number(temperature);
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 0.86.0 Responses semantics.
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 cacheKey = retention === "none"
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 ?? (compat.supportsLongCacheRetention === true ? "24h" : undefined)
129
+ const longRetention = !suppressChatGptUnsupported && retention === "long" && compat.supportsExplicitPromptCacheMode !== true
130
+ ? promptCacheRetention ?? (supportsLongCache ? "24h" : undefined)
104
131
  : undefined;
105
- // Pi 0.86.0 uses explicit 30m only for long retention; short is implicit.
106
- const currentCache = retention === "long" && compat.supportsLongCacheRetention === true && compat.supportsExplicitPromptCacheMode === true;
107
- const explicitCache = retention === "none" && compat.supportsExplicitPromptCacheMode === true;
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
  };
@@ -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 { createGrokNativeReplayEnvelope, grokPendingSourcesStart, sanitizeGrokVisibleText, } from "./grok-native-search.js";
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. */
@@ -1119,7 +1120,7 @@ function rawArguments(value) {
1119
1120
  return "{}";
1120
1121
  }
1121
1122
  }
1122
- function replayState(message, nativeOutput, nativeReplayRoute, nativeServerSearchEchoCallIds = []) {
1123
+ function replayState(message, nativeOutput, nativeReplayRoute, nativeServerSearchEchoCallIds = [], baseURL, sessionId, headerAuthorityFingerprint) {
1123
1124
  if (!isRecord(message))
1124
1125
  return undefined;
1125
1126
  const content = Array.isArray(message.content)
@@ -1131,6 +1132,24 @@ function replayState(message, nativeOutput, nativeReplayRoute, nativeServerSearc
1131
1132
  if (!provider || !model || !api)
1132
1133
  return undefined;
1133
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;
1134
1153
  return {
1135
1154
  response: {
1136
1155
  kind: "pi-ai",
@@ -1177,6 +1196,7 @@ function replayState(message, nativeOutput, nativeReplayRoute, nativeServerSearc
1177
1196
  : {}),
1178
1197
  };
1179
1198
  }),
1199
+ ...(lcxReplay === undefined ? {} : { lcxReplay }),
1180
1200
  ...(grokNative === undefined ? {} : { grokNative }),
1181
1201
  };
1182
1202
  }
@@ -1204,7 +1224,7 @@ function successfulFinish(message) {
1204
1224
  /**
1205
1225
  * Pi event vocabulary -> DSH StreamChunk. This remains thin and provider-neutral.
1206
1226
  */
1207
- async function* toDshChunks(events, signal, wireMeta, nativeReplayRoute) {
1227
+ async function* toDshChunks(events, signal, wireMeta, nativeReplayRoute, baseURL, sessionId, headerAuthorityFingerprint) {
1208
1228
  const toolIds = new Map();
1209
1229
  let argumentsError = false;
1210
1230
  for await (const event of events) {
@@ -1315,7 +1335,7 @@ async function* toDshChunks(events, signal, wireMeta, nativeReplayRoute) {
1315
1335
  const reason = successfulFinish(message);
1316
1336
  const replay = reason.kind === "error"
1317
1337
  ? undefined
1318
- : replayState(message, wireMeta?.nativeOutput, nativeReplayRoute, wireMeta?.nativeServerSearchEchoCallIds);
1338
+ : replayState(message, wireMeta?.nativeOutput, nativeReplayRoute, wireMeta?.nativeServerSearchEchoCallIds, baseURL, sessionId, headerAuthorityFingerprint);
1319
1339
  yield {
1320
1340
  type: "finish",
1321
1341
  reason,
@@ -1365,7 +1385,7 @@ async function* toDshChunks(events, signal, wireMeta, nativeReplayRoute) {
1365
1385
  * Send one LCX-owned OpenAI Responses request. Ordinary and replay use one provider attempt;
1366
1386
  * the DSH agent recovery layer remains the visible retry owner.
1367
1387
  */
1368
- 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, }) {
1369
1389
  let contentEmitted = false;
1370
1390
  let failureReported = false;
1371
1391
  /** One redacted report per request; user aborts are not failures. */
@@ -1449,7 +1469,7 @@ export async function* streamResponsesRequest({ baseURL, provider, model, piMode
1449
1469
  piEvents.end();
1450
1470
  }
1451
1471
  })();
1452
- const chunks = toDshChunks(piEvents, signal, wireMeta, nativeReplayRoute)[Symbol.asyncIterator]();
1472
+ const chunks = toDshChunks(piEvents, signal, wireMeta, nativeReplayRoute, baseURL, sessionId, headerAuthorityFingerprint)[Symbol.asyncIterator]();
1453
1473
  let exhausted = false;
1454
1474
  try {
1455
1475
  while (true) {