@draht/ai 2026.11.19 → 2026.11.21
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/dist/api/bedrock-converse-stream.d.ts.map +1 -1
- package/dist/api/bedrock-converse-stream.js +9 -1
- package/dist/api/bedrock-converse-stream.js.map +1 -1
- package/dist/api/classifier-shared.d.ts +2 -0
- package/dist/api/classifier-shared.d.ts.map +1 -1
- package/dist/api/classifier-shared.js +10 -5
- package/dist/api/classifier-shared.js.map +1 -1
- package/dist/api/google-generative-ai.js +1 -1
- package/dist/api/google-generative-ai.js.map +1 -1
- package/dist/api/google-shared.d.ts +5 -2
- package/dist/api/google-shared.d.ts.map +1 -1
- package/dist/api/google-shared.js +10 -6
- package/dist/api/google-shared.js.map +1 -1
- package/dist/api/google-vertex.js +1 -1
- package/dist/api/google-vertex.js.map +1 -1
- package/dist/api/mistral-conversations.js +4 -3
- package/dist/api/mistral-conversations.js.map +1 -1
- package/dist/api/openai-codex-responses.d.ts.map +1 -1
- package/dist/api/openai-codex-responses.js +39 -20
- package/dist/api/openai-codex-responses.js.map +1 -1
- package/dist/api/openai-completions.js +4 -2
- package/dist/api/openai-completions.js.map +1 -1
- package/dist/api/openai-responses-shared.d.ts +0 -1
- package/dist/api/openai-responses-shared.d.ts.map +1 -1
- package/dist/api/openai-responses-shared.js +43 -17
- package/dist/api/openai-responses-shared.js.map +1 -1
- package/dist/models.d.ts.map +1 -1
- package/dist/models.js +25 -14
- package/dist/models.js.map +1 -1
- package/dist/providers/compat-schema.d.ts +14 -0
- package/dist/providers/compat-schema.d.ts.map +1 -1
- package/dist/providers/compat-schema.js +19 -3
- package/dist/providers/compat-schema.js.map +1 -1
- package/dist/providers/data/.manifest.json +1 -1
- package/dist/providers/data/amazon-bedrock.json +1 -1
- package/dist/providers/data/ant-ling.json +1 -1
- package/dist/providers/data/azure.json +1 -1
- package/dist/providers/data/baseten.json +1 -1
- package/dist/providers/data/cerebras.json +1 -1
- package/dist/providers/data/cloudflare-ai-gateway.json +1 -1
- package/dist/providers/data/cloudflare-workers-ai.json +1 -1
- package/dist/providers/data/deepseek.json +1 -1
- package/dist/providers/data/fireworks.json +1 -1
- package/dist/providers/data/github-copilot.json +1 -1
- package/dist/providers/data/google-vertex.json +1 -1
- package/dist/providers/data/google.json +1 -1
- package/dist/providers/data/huggingface.json +1 -1
- package/dist/providers/data/kimi-coding.json +1 -1
- package/dist/providers/data/mistral.json +1 -1
- package/dist/providers/data/moonshotai-cn.json +1 -1
- package/dist/providers/data/moonshotai.json +1 -1
- package/dist/providers/data/nvidia.json +1 -1
- package/dist/providers/data/openai-codex.json +1 -1
- package/dist/providers/data/openai.json +1 -1
- package/dist/providers/data/opencode-go.json +1 -1
- package/dist/providers/data/opencode.json +1 -1
- package/dist/providers/data/openrouter.json +1 -1
- package/dist/providers/data/qwen-token-plan-cn.json +1 -1
- package/dist/providers/data/qwen-token-plan-individual-cn.json +1 -1
- package/dist/providers/data/qwen-token-plan-individual.json +1 -1
- package/dist/providers/data/qwen-token-plan.json +1 -1
- package/dist/providers/data/radius.json +1 -1
- package/dist/providers/data/together.json +1 -1
- package/dist/providers/data/vercel-ai-gateway.json +1 -1
- package/dist/providers/data/zai-coding-cn.json +1 -1
- package/dist/providers/data/zai.json +1 -1
- package/dist/types.d.ts +3 -3
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/utils/retry.d.ts.map +1 -1
- package/dist/utils/retry.js +39 -6
- package/dist/utils/retry.js.map +1 -1
- package/dist/utils/version.d.ts +1 -1
- package/dist/utils/version.js +1 -1
- package/dist/utils/version.js.map +1 -1
- package/package.json +5 -4
|
@@ -7,8 +7,7 @@ import { AssistantMessageEventStream } from "../utils/event-stream.js";
|
|
|
7
7
|
import { headersToRecord } from "../utils/headers.js";
|
|
8
8
|
import { getIdentityHeaders } from "../utils/identity.js";
|
|
9
9
|
import { resolveHttpProxyUrlForTarget } from "../utils/node-http-proxy.js";
|
|
10
|
-
import {
|
|
11
|
-
import { getDeclaredTools, getInitialSystemMessage, normalizeContext, resolveTranscript, resolveTranscriptTools, } from "../utils/transcript.js";
|
|
10
|
+
import { getDeclaredTools, normalizeContext, resolveTranscript, resolveTranscriptTools } from "../utils/transcript.js";
|
|
12
11
|
import { uuidv7 } from "../utils/uuid.js";
|
|
13
12
|
import { createGrammarToolInputProperties } from "./constrained-sampling.js";
|
|
14
13
|
import { clampOpenAIPromptCacheKey } from "./openai-prompt-cache.js";
|
|
@@ -30,6 +29,10 @@ const CODEX_TOOL_CALL_PROVIDERS = new Set(["openai", "openai-codex", "opencode"]
|
|
|
30
29
|
const WEBSOCKET_MESSAGE_TOO_BIG_CLOSE_CODE = 1009;
|
|
31
30
|
const WEBSOCKET_CONNECTION_LIMIT_REACHED_CODE = "websocket_connection_limit_reached";
|
|
32
31
|
const PREVIOUS_RESPONSE_NOT_FOUND_CODE = "previous_response_not_found";
|
|
32
|
+
// The Responses API rejects max_output_tokens below 16: https://github.com/earendil-works/pi/issues/6265
|
|
33
|
+
const MIN_OUTPUT_TOKENS = 16;
|
|
34
|
+
// Fixed reasoning.effort while configuration_update items carry the thinking level.
|
|
35
|
+
const CONFIGURATION_UPDATE_REQUEST_EFFORT = "medium";
|
|
33
36
|
const CODEX_RESPONSE_STATUSES = new Set([
|
|
34
37
|
"completed",
|
|
35
38
|
"incomplete",
|
|
@@ -152,12 +155,14 @@ export const stream = (model, context, options) => {
|
|
|
152
155
|
const stream = new AssistantMessageEventStream();
|
|
153
156
|
const normalizedContext = resolveTranscript(context, model.compat?.supportsMidConvoSystemMessages);
|
|
154
157
|
(async () => {
|
|
158
|
+
const configurationUpdateEffort = getConfigurationUpdateEffort(model, options);
|
|
155
159
|
const output = {
|
|
156
160
|
role: "assistant",
|
|
157
161
|
content: [],
|
|
158
162
|
api: "openai-codex-responses",
|
|
159
163
|
provider: model.provider,
|
|
160
164
|
model: model.id,
|
|
165
|
+
...(configurationUpdateEffort === undefined ? {} : { providerThinkingLevel: configurationUpdateEffort }),
|
|
161
166
|
usage: {
|
|
162
167
|
input: 0,
|
|
163
168
|
output: 0,
|
|
@@ -178,7 +183,7 @@ export const stream = (model, context, options) => {
|
|
|
178
183
|
const grammarToolInputProperties = createGrammarToolInputProperties(getDeclaredTools(normalizedContext.messages), model.compat?.supportsOpenAIGrammarTools ?? false);
|
|
179
184
|
const cacheSessionId = options?.cacheRetention === "none" ? undefined : options?.sessionId;
|
|
180
185
|
const codexSessionId = clampOpenAIPromptCacheKey(cacheSessionId);
|
|
181
|
-
let body = buildRequestBody(model, normalizedContext, options, codexSessionId, grammarToolInputProperties);
|
|
186
|
+
let body = buildRequestBody(model, normalizedContext, options, codexSessionId, grammarToolInputProperties, configurationUpdateEffort);
|
|
182
187
|
const nextBody = await options?.onPayload?.(body, model);
|
|
183
188
|
if (nextBody !== undefined) {
|
|
184
189
|
body = nextBody;
|
|
@@ -365,8 +370,9 @@ export const streamSimple = (model, context, options) => {
|
|
|
365
370
|
...buildBaseOptions(model, context, options, apiKey),
|
|
366
371
|
toolChoice: options?.toolChoice,
|
|
367
372
|
};
|
|
368
|
-
const clampedReasoning = options?.reasoning ? clampThinkingLevel(model, options.reasoning) :
|
|
369
|
-
|
|
373
|
+
const clampedReasoning = options?.reasoning ? clampThinkingLevel(model, options.reasoning) : "off";
|
|
374
|
+
// Off is sent as effort none, so with configuration_update items switching to it keeps the cache.
|
|
375
|
+
const reasoningEffort = clampedReasoning !== "off" ? clampedReasoning : model.reasoning ? "none" : undefined;
|
|
370
376
|
return stream(model, context, {
|
|
371
377
|
...base,
|
|
372
378
|
reasoningEffort,
|
|
@@ -375,7 +381,20 @@ export const streamSimple = (model, context, options) => {
|
|
|
375
381
|
// ============================================================================
|
|
376
382
|
// Request Building
|
|
377
383
|
// ============================================================================
|
|
378
|
-
|
|
384
|
+
/** Effort to send in configuration_update items, or undefined to send it as reasoning.effort. */
|
|
385
|
+
function getConfigurationUpdateEffort(model, options) {
|
|
386
|
+
if (!model.compat?.supportsMidConvoEffort || options?.reasoningEffort === undefined)
|
|
387
|
+
return undefined;
|
|
388
|
+
return resolveReasoningEffort(model, options.reasoningEffort) ?? undefined;
|
|
389
|
+
}
|
|
390
|
+
/** Provider-native effort for a requested level; null when the model takes no reasoning field for it. */
|
|
391
|
+
function resolveReasoningEffort(model, reasoningEffort) {
|
|
392
|
+
if (reasoningEffort === "none") {
|
|
393
|
+
return model.thinkingLevelMap?.off === undefined ? "none" : model.thinkingLevelMap.off;
|
|
394
|
+
}
|
|
395
|
+
return model.thinkingLevelMap?.[reasoningEffort] ?? reasoningEffort;
|
|
396
|
+
}
|
|
397
|
+
function buildRequestBody(model, context, options, cacheSessionId, grammarToolInputProperties, configurationUpdateEffort) {
|
|
379
398
|
const supportsStrictMode = model.compat?.supportsStrictMode ?? true;
|
|
380
399
|
const supportsOpenAIGrammarTools = model.compat?.supportsOpenAIGrammarTools ?? false;
|
|
381
400
|
const supportsAdditionalTools = model.compat?.supportsAdditionalTools ?? false;
|
|
@@ -385,21 +404,21 @@ function buildRequestBody(model, context, options, cacheSessionId, grammarToolIn
|
|
|
385
404
|
supportsAdditionalTools,
|
|
386
405
|
supportsToolSearch,
|
|
387
406
|
}));
|
|
407
|
+
// The system prompt goes into a leading developer item, not top-level `instructions`, as in Codex.
|
|
408
|
+
// From GPT-6.2 on, the backend appends `instructions` after `input`, so every new turn changed the
|
|
409
|
+
// prompt before the system prompt and it was never served from the prompt cache (#12273).
|
|
388
410
|
const messages = convertResponsesMessages(model, context, CODEX_TOOL_CALL_PROVIDERS, {
|
|
389
|
-
includeSystemPrompt: false,
|
|
390
411
|
grammarToolInputProperties,
|
|
391
412
|
supportsMidConvoSystemMessages: model.compat?.supportsMidConvoSystemMessages ?? false,
|
|
392
413
|
supportsAdditionalTools,
|
|
393
414
|
supportsToolSearch,
|
|
394
415
|
toolOptions: { strict: null, supportsStrictMode, supportsOpenAIGrammarTools },
|
|
416
|
+
configurationUpdateEffort,
|
|
395
417
|
});
|
|
396
|
-
const initialSystemMessage = getInitialSystemMessage(context.messages);
|
|
397
|
-
const instructions = initialSystemMessage ? getSystemMessageText(initialSystemMessage) : "";
|
|
398
418
|
const body = {
|
|
399
419
|
model: model.id,
|
|
400
420
|
store: false,
|
|
401
421
|
stream: true,
|
|
402
|
-
instructions: instructions || "You are a helpful assistant.",
|
|
403
422
|
input: messages,
|
|
404
423
|
text: { verbosity: options?.textVerbosity || "low" },
|
|
405
424
|
include: ["reasoning.encrypted_content"],
|
|
@@ -407,6 +426,9 @@ function buildRequestBody(model, context, options, cacheSessionId, grammarToolIn
|
|
|
407
426
|
tool_choice: options?.toolChoice ?? "auto",
|
|
408
427
|
parallel_tool_calls: true,
|
|
409
428
|
};
|
|
429
|
+
if (options?.maxTokens && (model.compat?.supportsMaxOutputTokens ?? true)) {
|
|
430
|
+
body.max_output_tokens = Math.max(options.maxTokens, MIN_OUTPUT_TOKENS);
|
|
431
|
+
}
|
|
410
432
|
if (options?.temperature !== undefined) {
|
|
411
433
|
body.temperature = options.temperature;
|
|
412
434
|
}
|
|
@@ -421,11 +443,9 @@ function buildRequestBody(model, context, options, cacheSessionId, grammarToolIn
|
|
|
421
443
|
});
|
|
422
444
|
}
|
|
423
445
|
if (options?.reasoningEffort !== undefined) {
|
|
424
|
-
const effort =
|
|
425
|
-
? model.
|
|
426
|
-
|
|
427
|
-
: model.thinkingLevelMap.off
|
|
428
|
-
: (model.thinkingLevelMap?.[options.reasoningEffort] ?? options.reasoningEffort);
|
|
446
|
+
const effort = configurationUpdateEffort === undefined
|
|
447
|
+
? resolveReasoningEffort(model, options.reasoningEffort)
|
|
448
|
+
: CONFIGURATION_UPDATE_REQUEST_EFFORT;
|
|
429
449
|
if (effort !== null) {
|
|
430
450
|
body.reasoning = {
|
|
431
451
|
effort,
|
|
@@ -656,7 +676,9 @@ async function* parseSSE(response, signal) {
|
|
|
656
676
|
// ============================================================================
|
|
657
677
|
const OPENAI_BETA_RESPONSES_WEBSOCKETS = "responses_websockets=2026-02-06";
|
|
658
678
|
const SESSION_WEBSOCKET_CACHE_TTL_MS = 5 * 60 * 1000;
|
|
659
|
-
|
|
679
|
+
// The server closes a socket after 60 minutes, even mid-response, and the in-flight response is lost.
|
|
680
|
+
// New requests open a fresh socket after 45 minutes, leaving 15 minutes for long responses.
|
|
681
|
+
const SESSION_WEBSOCKET_MAX_AGE_MS = 45 * 60 * 1000;
|
|
660
682
|
const websocketSessionCache = new Map();
|
|
661
683
|
const websocketDebugStats = new Map();
|
|
662
684
|
const websocketSseFallbackSessions = new Set();
|
|
@@ -1221,10 +1243,7 @@ async function processWebSocketStream(url, body, headers, output, stream, model,
|
|
|
1221
1243
|
keepConnection = false;
|
|
1222
1244
|
}
|
|
1223
1245
|
else if (useCachedContext && entry && output.responseId) {
|
|
1224
|
-
const responseItems = convertResponsesMessages(model, normalizeContext({ messages: [output] }), CODEX_TOOL_CALL_PROVIDERS, {
|
|
1225
|
-
includeSystemPrompt: false,
|
|
1226
|
-
grammarToolInputProperties,
|
|
1227
|
-
}).filter((item) => item.type !== "function_call_output" && item.type !== "custom_tool_call_output");
|
|
1246
|
+
const responseItems = convertResponsesMessages(model, normalizeContext({ messages: [output] }), CODEX_TOOL_CALL_PROVIDERS, { grammarToolInputProperties }).filter((item) => item.type !== "function_call_output" && item.type !== "custom_tool_call_output");
|
|
1228
1247
|
entry.continuation = {
|
|
1229
1248
|
lastRequestBody: fullBody,
|
|
1230
1249
|
lastResponseId: output.responseId,
|