@bastani/pi-ai 0.9.71 → 0.9.72-alpha.1
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/CHANGELOG.md +56 -0
- package/README.md +78 -8
- package/dist/api/anthropic-messages.d.ts.map +1 -1
- package/dist/api/anthropic-messages.js +50 -10
- package/dist/api/anthropic-messages.js.map +1 -1
- package/dist/api/azure-openai-responses.d.ts.map +1 -1
- package/dist/api/azure-openai-responses.js +7 -2
- package/dist/api/azure-openai-responses.js.map +1 -1
- package/dist/api/bedrock-converse-stream.d.ts.map +1 -1
- package/dist/api/bedrock-converse-stream.js +28 -1
- package/dist/api/bedrock-converse-stream.js.map +1 -1
- package/dist/api/bedrock-converse-stream.lazy.d.ts +7 -0
- package/dist/api/bedrock-converse-stream.lazy.d.ts.map +1 -1
- package/dist/api/bedrock-converse-stream.lazy.js +18 -5
- package/dist/api/bedrock-converse-stream.lazy.js.map +1 -1
- package/dist/api/google-generative-ai.d.ts +3 -1
- package/dist/api/google-generative-ai.d.ts.map +1 -1
- package/dist/api/google-generative-ai.js +4 -2
- package/dist/api/google-generative-ai.js.map +1 -1
- package/dist/api/google-shared.d.ts +10 -2
- package/dist/api/google-shared.d.ts.map +1 -1
- package/dist/api/google-shared.js +31 -9
- package/dist/api/google-shared.js.map +1 -1
- package/dist/api/google-vertex.d.ts +3 -1
- package/dist/api/google-vertex.d.ts.map +1 -1
- package/dist/api/google-vertex.js +4 -2
- package/dist/api/google-vertex.js.map +1 -1
- package/dist/api/lazy.d.ts +2 -2
- package/dist/api/lazy.d.ts.map +1 -1
- package/dist/api/lazy.js +10 -9
- package/dist/api/lazy.js.map +1 -1
- package/dist/api/mistral-conversations.d.ts.map +1 -1
- package/dist/api/mistral-conversations.js +12 -1
- 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 +6 -1
- package/dist/api/openai-codex-responses.js.map +1 -1
- package/dist/api/openai-completions.d.ts +2 -1
- package/dist/api/openai-completions.d.ts.map +1 -1
- package/dist/api/openai-completions.js +112 -15
- package/dist/api/openai-completions.js.map +1 -1
- package/dist/api/openai-images-options.d.ts +107 -0
- package/dist/api/openai-images-options.d.ts.map +1 -0
- package/dist/api/openai-images-options.js +106 -0
- package/dist/api/openai-images-options.js.map +1 -0
- package/dist/api/openai-images.d.ts +3 -11
- package/dist/api/openai-images.d.ts.map +1 -1
- package/dist/api/openai-images.js +117 -16
- package/dist/api/openai-images.js.map +1 -1
- package/dist/api/openai-responses-shared.js +6 -2
- package/dist/api/openai-responses-shared.js.map +1 -1
- package/dist/api/openai-responses.d.ts.map +1 -1
- package/dist/api/openai-responses.js +29 -7
- package/dist/api/openai-responses.js.map +1 -1
- package/dist/auth/oauth/load.d.ts.map +1 -1
- package/dist/auth/oauth/load.js +5 -2
- package/dist/auth/oauth/load.js.map +1 -1
- package/dist/env-api-keys.d.ts.map +1 -1
- package/dist/env-api-keys.js +1 -0
- package/dist/env-api-keys.js.map +1 -1
- package/dist/image-generation-error.d.ts +203 -0
- package/dist/image-generation-error.d.ts.map +1 -0
- package/dist/image-generation-error.js +441 -0
- package/dist/image-generation-error.js.map +1 -0
- package/dist/index.d.ts +4 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -1
- package/dist/lazy-bedrock-provider.d.ts +10 -0
- package/dist/lazy-bedrock-provider.d.ts.map +1 -0
- package/dist/lazy-bedrock-provider.js +13 -0
- package/dist/lazy-bedrock-provider.js.map +1 -0
- package/dist/lazy-oauth.d.ts +10 -0
- package/dist/lazy-oauth.d.ts.map +1 -0
- package/dist/lazy-oauth.js +23 -0
- package/dist/lazy-oauth.js.map +1 -0
- package/dist/models.d.ts.map +1 -1
- package/dist/models.generated.d.ts +4 -0
- package/dist/models.generated.d.ts.map +1 -1
- package/dist/models.generated.js +4 -0
- package/dist/models.generated.js.map +1 -1
- package/dist/models.js +8 -8
- package/dist/models.js.map +1 -1
- package/dist/providers/all.d.ts.map +1 -1
- package/dist/providers/all.js +2 -0
- package/dist/providers/all.js.map +1 -1
- package/dist/providers/azure.d.ts.map +1 -1
- package/dist/providers/azure.js +2 -2
- package/dist/providers/azure.js.map +1 -1
- package/dist/providers/compat-schema.d.ts +15 -0
- package/dist/providers/compat-schema.d.ts.map +1 -1
- package/dist/providers/compat-schema.js +19 -1
- package/dist/providers/compat-schema.js.map +1 -1
- package/dist/providers/data/.manifest.json +1 -1
- package/dist/providers/data/anthropic.json +1 -1
- package/dist/providers/data/azure.json +1 -1
- package/dist/providers/data/baseten.json +1 -1
- package/dist/providers/data/cloudflare-ai-gateway.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/nvidia.json +1 -1
- package/dist/providers/data/openai-images.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-individual-cn.json +1 -0
- package/dist/providers/data/qwen-token-plan-individual.json +1 -1
- package/dist/providers/data/together.json +1 -1
- package/dist/providers/data/vercel-ai-gateway.json +1 -1
- package/dist/providers/github-copilot-capabilities.d.ts +67 -0
- package/dist/providers/github-copilot-capabilities.d.ts.map +1 -0
- package/dist/providers/github-copilot-capabilities.js +81 -0
- package/dist/providers/github-copilot-capabilities.js.map +1 -0
- package/dist/providers/github-copilot-errors.d.ts +243 -0
- package/dist/providers/github-copilot-errors.d.ts.map +1 -0
- package/dist/providers/github-copilot-errors.js +720 -0
- package/dist/providers/github-copilot-errors.js.map +1 -0
- package/dist/providers/github-copilot.d.ts +1 -2
- package/dist/providers/github-copilot.d.ts.map +1 -1
- package/dist/providers/github-copilot.js +29 -4
- package/dist/providers/github-copilot.js.map +1 -1
- package/dist/providers/openai-codex.js +1 -1
- package/dist/providers/openai-codex.js.map +1 -1
- package/dist/providers/qwen-token-plan-individual-cn.d.ts +3 -0
- package/dist/providers/qwen-token-plan-individual-cn.d.ts.map +1 -0
- package/dist/providers/qwen-token-plan-individual-cn.js +15 -0
- package/dist/providers/qwen-token-plan-individual-cn.js.map +1 -0
- package/dist/providers/qwen-token-plan-individual-cn.models.d.ts +5 -0
- package/dist/providers/qwen-token-plan-individual-cn.models.d.ts.map +1 -0
- package/dist/providers/qwen-token-plan-individual-cn.models.js +8 -0
- package/dist/providers/qwen-token-plan-individual-cn.models.js.map +1 -0
- package/dist/types.d.ts +29 -3
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/utils/error-body.d.ts +8 -0
- package/dist/utils/error-body.d.ts.map +1 -1
- package/dist/utils/error-body.js +28 -1
- package/dist/utils/error-body.js.map +1 -1
- package/dist/utils/model-operations.d.ts.map +1 -1
- package/dist/utils/model-operations.js +6 -0
- package/dist/utils/model-operations.js.map +1 -1
- package/dist/utils/overflow.d.ts +4 -0
- package/dist/utils/overflow.d.ts.map +1 -1
- package/dist/utils/overflow.js +16 -3
- package/dist/utils/overflow.js.map +1 -1
- package/dist/utils/provider-retry.d.ts +2 -0
- package/dist/utils/provider-retry.d.ts.map +1 -1
- package/dist/utils/provider-retry.js +2 -0
- package/dist/utils/provider-retry.js.map +1 -1
- package/dist/utils/retry.d.ts +3 -1
- package/dist/utils/retry.d.ts.map +1 -1
- package/dist/utils/retry.js +9 -0
- package/dist/utils/retry.js.map +1 -1
- package/dist/utils/thinking-off.d.ts +23 -0
- package/dist/utils/thinking-off.d.ts.map +1 -0
- package/dist/utils/thinking-off.js +51 -0
- package/dist/utils/thinking-off.js.map +1 -0
- package/dist/utils/version.d.ts +1 -1
- package/dist/utils/version.d.ts.map +1 -1
- package/dist/utils/version.js +1 -1
- package/dist/utils/version.js.map +1 -1
- package/package.json +9 -1
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { calculateCost, clampThinkingLevel } from "../models.js";
|
|
2
|
+
import { githubCopilotEmptyResponse, recordGitHubCopilotFailure } from "../providers/github-copilot-errors.js";
|
|
2
3
|
import { formatProviderError, normalizeProviderError } from "../utils/error-body.js";
|
|
3
4
|
import { AssistantMessageEventStream } from "../utils/event-stream.js";
|
|
4
5
|
import { shortHash } from "../utils/hash.js";
|
|
@@ -13,6 +14,7 @@ import { sanitizeSurrogates } from "../utils/sanitize-unicode.js";
|
|
|
13
14
|
import { createStreamDeadline, withStreamDeadline } from "../utils/stream-deadline.js";
|
|
14
15
|
import { ToolCallArgumentsStream } from "../utils/streaming-json.js";
|
|
15
16
|
import { getSystemMessageText, renderSystemMessageUpdate } from "../utils/text.js";
|
|
17
|
+
import { recordLowestThinkingOffFallback, recordThinkingOffFallback } from "../utils/thinking-off.js";
|
|
16
18
|
import { getDeclaredTools, resolveTranscript, resolveTranscriptTools, } from "../utils/transcript.js";
|
|
17
19
|
import { appendGrammarToolInputJsonDelta, closeGrammarToolInputJson, createGrammarToolInputProperties, getGrammarToolInput, getJsonSchemaToolParameters, resolveGrammarConstrainedSampling, resolveJsonSchemaStrictSampling, } from "./constrained-sampling.js";
|
|
18
20
|
import { buildCopilotDynamicHeaders, hasCopilotVisionInput, preserveCopilotIntegrationHeader, } from "./github-copilot-headers.js";
|
|
@@ -154,6 +156,20 @@ const OPENAI_COMPLETIONS_REASONING_FIELDS = ["reasoning", "reasoning_content", "
|
|
|
154
156
|
function isOpenAICompletionsReasoningField(field) {
|
|
155
157
|
return OPENAI_COMPLETIONS_REASONING_FIELDS.includes(field);
|
|
156
158
|
}
|
|
159
|
+
/**
|
|
160
|
+
* GitHub Copilot's Gemini models stream their reasoning as an opaque `reasoning_opaque` value that must be sent back
|
|
161
|
+
* unchanged on the same assistant message. Atomic keeps it as the signature of a redacted thinking block.
|
|
162
|
+
*/
|
|
163
|
+
const OPAQUE_REASONING_SIGNATURE_PREFIX = "reasoning_opaque:";
|
|
164
|
+
function encodeOpaqueReasoning(data) {
|
|
165
|
+
return `${OPAQUE_REASONING_SIGNATURE_PREFIX}${data}`;
|
|
166
|
+
}
|
|
167
|
+
function decodeOpaqueReasoning(block) {
|
|
168
|
+
const signature = block.thinkingSignature;
|
|
169
|
+
return signature?.startsWith(OPAQUE_REASONING_SIGNATURE_PREFIX)
|
|
170
|
+
? signature.slice(OPAQUE_REASONING_SIGNATURE_PREFIX.length)
|
|
171
|
+
: undefined;
|
|
172
|
+
}
|
|
157
173
|
function resolveCacheRetention(cacheRetention, env) {
|
|
158
174
|
if (cacheRetention) {
|
|
159
175
|
return cacheRetention;
|
|
@@ -186,13 +202,15 @@ export const stream = (model, context, options) => {
|
|
|
186
202
|
};
|
|
187
203
|
// `reasoning_details` are replay metadata, not user-visible stream deltas.
|
|
188
204
|
// Keep them in memory during streaming and serialize once when the block is finalized.
|
|
205
|
+
// An opaque reasoning block keeps its own signature.
|
|
189
206
|
let streamedReasoningDetails;
|
|
190
207
|
const applyStreamedReasoningDetails = (block) => {
|
|
191
|
-
if (streamedReasoningDetails !== undefined) {
|
|
208
|
+
if (streamedReasoningDetails !== undefined && block.redacted !== true) {
|
|
192
209
|
block.thinkingSignature = JSON.stringify(streamedReasoningDetails);
|
|
193
210
|
}
|
|
194
211
|
};
|
|
195
212
|
const streamDeadline = createStreamDeadline(options?.streamDeadlineMs, options?.signal);
|
|
213
|
+
let streamStarted = false;
|
|
196
214
|
try {
|
|
197
215
|
const apiKey = getClientApiKey(model.provider, options?.apiKey, options?.headers);
|
|
198
216
|
const compat = getCompat(model);
|
|
@@ -200,7 +218,11 @@ export const stream = (model, context, options) => {
|
|
|
200
218
|
const cacheRetention = resolveCacheRetention(options?.cacheRetention, options?.env);
|
|
201
219
|
const cacheSessionId = cacheRetention === "none" ? undefined : options?.sessionId;
|
|
202
220
|
const client = createClient(model, normalizedContext, apiKey, options?.headers, options?.identity, options?.fetch, cacheSessionId, compat, options?.apiKey);
|
|
203
|
-
|
|
221
|
+
const thinkingOffFallback = options?.reasoningEffort
|
|
222
|
+
? undefined
|
|
223
|
+
: recordCompletionsThinkingOffFallback(model, compat, output);
|
|
224
|
+
const reasoningOptions = thinkingOffFallback ? { ...options, reasoningEffort: thinkingOffFallback } : options;
|
|
225
|
+
let params = buildParams(model, normalizedContext, reasoningOptions, compat, cacheRetention, grammarToolInputProperties);
|
|
204
226
|
const nextParams = await options?.onPayload?.(params, model);
|
|
205
227
|
if (nextParams !== undefined) {
|
|
206
228
|
params = nextParams;
|
|
@@ -217,8 +239,10 @@ export const stream = (model, context, options) => {
|
|
|
217
239
|
});
|
|
218
240
|
await options?.onResponse?.({ status: response.status, headers: headersToRecord(response.headers) }, model);
|
|
219
241
|
stream.push({ type: "start", partial: output });
|
|
242
|
+
streamStarted = true;
|
|
220
243
|
let textBlock = null;
|
|
221
244
|
let thinkingBlock = null;
|
|
245
|
+
let opaqueReasoning = null;
|
|
222
246
|
let hasFinishReason = false;
|
|
223
247
|
const toolCallBlocksByIndex = new Map();
|
|
224
248
|
const toolCallBlocksById = new Map();
|
|
@@ -297,6 +321,19 @@ export const stream = (model, context, options) => {
|
|
|
297
321
|
}
|
|
298
322
|
return thinkingBlock;
|
|
299
323
|
};
|
|
324
|
+
const appendOpaqueReasoning = (delta) => {
|
|
325
|
+
if (!opaqueReasoning) {
|
|
326
|
+
opaqueReasoning = { block: { type: "thinking", thinking: "", redacted: true }, data: "" };
|
|
327
|
+
blocks.push(opaqueReasoning.block);
|
|
328
|
+
stream.push({
|
|
329
|
+
type: "thinking_start",
|
|
330
|
+
contentIndex: getContentIndex(opaqueReasoning.block),
|
|
331
|
+
partial: output,
|
|
332
|
+
});
|
|
333
|
+
}
|
|
334
|
+
opaqueReasoning.data += delta;
|
|
335
|
+
opaqueReasoning.block.thinkingSignature = encodeOpaqueReasoning(opaqueReasoning.data);
|
|
336
|
+
};
|
|
300
337
|
const ensureToolCallBlock = (toolCall) => {
|
|
301
338
|
const streamIndex = typeof toolCall.index === "number" ? toolCall.index : undefined;
|
|
302
339
|
const name = toolCall.function?.name ?? toolCall.custom?.name ?? "";
|
|
@@ -412,6 +449,10 @@ export const stream = (model, context, options) => {
|
|
|
412
449
|
});
|
|
413
450
|
}
|
|
414
451
|
}
|
|
452
|
+
const opaqueReasoningDelta = deltaFields.reasoning_opaque;
|
|
453
|
+
if (typeof opaqueReasoningDelta === "string" && opaqueReasoningDelta.length > 0) {
|
|
454
|
+
appendOpaqueReasoning(opaqueReasoningDelta);
|
|
455
|
+
}
|
|
415
456
|
const reasoningDetails = choice.delta.reasoning_details;
|
|
416
457
|
if (Array.isArray(reasoningDetails)) {
|
|
417
458
|
for (const detail of reasoningDetails) {
|
|
@@ -487,6 +528,9 @@ export const stream = (model, context, options) => {
|
|
|
487
528
|
if ((compat.supportsFinishReason && !hasFinishReason) || output.stopReason === "pending") {
|
|
488
529
|
throw new Error("Stream ended without finish_reason");
|
|
489
530
|
}
|
|
531
|
+
const emptyResponse = githubCopilotEmptyResponse(output);
|
|
532
|
+
if (emptyResponse)
|
|
533
|
+
throw emptyResponse;
|
|
490
534
|
stream.push({ type: "done", reason: output.stopReason, message: output });
|
|
491
535
|
stream.end();
|
|
492
536
|
}
|
|
@@ -507,6 +551,8 @@ export const stream = (model, context, options) => {
|
|
|
507
551
|
if (rawMetadata && !output.errorMessage.includes(String(rawMetadata))) {
|
|
508
552
|
output.errorMessage += `\n${rawMetadata}`;
|
|
509
553
|
}
|
|
554
|
+
if (model.provider === "github-copilot")
|
|
555
|
+
recordGitHubCopilotFailure(output, error, streamStarted);
|
|
510
556
|
stream.push({ type: "error", reason: output.stopReason, error: output });
|
|
511
557
|
stream.end();
|
|
512
558
|
}
|
|
@@ -516,6 +562,17 @@ export const stream = (model, context, options) => {
|
|
|
516
562
|
})();
|
|
517
563
|
return stream;
|
|
518
564
|
};
|
|
565
|
+
/**
|
|
566
|
+
* OpenRouter translates a gateway effort for the routed model, to its nearest supported effort or to a share
|
|
567
|
+
* of the output budget, so thinking `off` there runs at the lowest supported level even when the thinking map
|
|
568
|
+
* lists no effort for it: the same request as selecting that level. Other formats send the map's value to an
|
|
569
|
+
* endpoint that can reject an effort the model does not support, so they need it listed.
|
|
570
|
+
*/
|
|
571
|
+
function recordCompletionsThinkingOffFallback(model, compat, output) {
|
|
572
|
+
return compat.thinkingFormat === "openrouter"
|
|
573
|
+
? recordLowestThinkingOffFallback(model, output)
|
|
574
|
+
: recordThinkingOffFallback(model, output);
|
|
575
|
+
}
|
|
519
576
|
export const streamSimple = (model, context, options) => {
|
|
520
577
|
getClientApiKey(model.provider, options?.apiKey, options?.headers);
|
|
521
578
|
const base = {
|
|
@@ -575,12 +632,16 @@ function buildParams(model, context, options, compat = getCompat(model), cacheRe
|
|
|
575
632
|
grammarToolInputProperties,
|
|
576
633
|
});
|
|
577
634
|
const cacheControl = getCompatCacheControl(compat, cacheRetention);
|
|
635
|
+
const promptCacheKeyMode = compat.promptCacheKeyMode ?? "auto";
|
|
636
|
+
const supportsPromptCacheKey = promptCacheKeyMode === "enabled" ||
|
|
637
|
+
(promptCacheKeyMode === "auto" &&
|
|
638
|
+
(model.baseUrl.includes("api.openai.com") ||
|
|
639
|
+
(cacheRetention === "long" && compat.supportsLongCacheRetention)));
|
|
578
640
|
const params = {
|
|
579
641
|
model: model.id,
|
|
580
642
|
messages,
|
|
581
643
|
stream: true,
|
|
582
|
-
prompt_cache_key:
|
|
583
|
-
(cacheRetention === "long" && compat.supportsLongCacheRetention)
|
|
644
|
+
prompt_cache_key: cacheRetention !== "none" && supportsPromptCacheKey
|
|
584
645
|
? clampOpenAIPromptCacheKey(options?.sessionId)
|
|
585
646
|
: undefined,
|
|
586
647
|
prompt_cache_retention: cacheRetention === "long" && compat.supportsLongCacheRetention ? "24h" : undefined,
|
|
@@ -613,8 +674,7 @@ function buildParams(model, context, options, compat = getCompat(model), cacheRe
|
|
|
613
674
|
params.tool_stream = true;
|
|
614
675
|
}
|
|
615
676
|
}
|
|
616
|
-
else if (hasToolHistory(context.messages)) {
|
|
617
|
-
// Anthropic (via LiteLLM/proxy) requires tools param when conversation has tool_calls/tool_results
|
|
677
|
+
else if (compat.requiresToolsWithToolHistory && hasToolHistory(context.messages)) {
|
|
618
678
|
params.tools = [];
|
|
619
679
|
}
|
|
620
680
|
if (cacheControl) {
|
|
@@ -666,7 +726,10 @@ function buildParams(model, context, options, compat = getCompat(model), cacheRe
|
|
|
666
726
|
const thinkingBudget = resolveClampedThinkingBudget(model, options, params);
|
|
667
727
|
if (compat.thinkingFormat === "zai" && model.reasoning) {
|
|
668
728
|
const zaiParams = params;
|
|
669
|
-
|
|
729
|
+
const thinkingEnabled = resolveThinkingToggle(model, options);
|
|
730
|
+
if (thinkingEnabled !== undefined) {
|
|
731
|
+
zaiParams.thinking = thinkingEnabled ? { type: "enabled", clear_thinking: false } : { type: "disabled" };
|
|
732
|
+
}
|
|
670
733
|
if (options?.reasoningEffort && compat.supportsReasoningEffort) {
|
|
671
734
|
const mappedEffort = model.thinkingLevelMap?.[options.reasoningEffort];
|
|
672
735
|
const effort = mappedEffort === undefined ? options.reasoningEffort : mappedEffort;
|
|
@@ -676,7 +739,10 @@ function buildParams(model, context, options, compat = getCompat(model), cacheRe
|
|
|
676
739
|
}
|
|
677
740
|
}
|
|
678
741
|
else if (compat.thinkingFormat === "qwen" && model.reasoning) {
|
|
679
|
-
|
|
742
|
+
const thinkingEnabled = resolveThinkingToggle(model, options);
|
|
743
|
+
if (thinkingEnabled !== undefined) {
|
|
744
|
+
params.enable_thinking = thinkingEnabled;
|
|
745
|
+
}
|
|
680
746
|
if (options?.reasoningEffort && compat.supportsReasoningEffort) {
|
|
681
747
|
const effort = model.thinkingLevelMap?.[options.reasoningEffort] ?? options.reasoningEffort;
|
|
682
748
|
if (typeof effort === "string") {
|
|
@@ -685,8 +751,9 @@ function buildParams(model, context, options, compat = getCompat(model), cacheRe
|
|
|
685
751
|
}
|
|
686
752
|
}
|
|
687
753
|
else if (compat.thinkingFormat === "qwen-chat-template" && model.reasoning) {
|
|
754
|
+
const thinkingEnabled = resolveThinkingToggle(model, options);
|
|
688
755
|
params.chat_template_kwargs = {
|
|
689
|
-
enable_thinking:
|
|
756
|
+
...(thinkingEnabled === undefined ? {} : { enable_thinking: thinkingEnabled }),
|
|
690
757
|
preserve_thinking: true,
|
|
691
758
|
};
|
|
692
759
|
}
|
|
@@ -743,7 +810,10 @@ function buildParams(model, context, options, compat = getCompat(model), cacheRe
|
|
|
743
810
|
}
|
|
744
811
|
else if (compat.thinkingFormat === "together" && model.reasoning) {
|
|
745
812
|
const togetherParams = params;
|
|
746
|
-
|
|
813
|
+
const thinkingEnabled = resolveThinkingToggle(model, options);
|
|
814
|
+
if (thinkingEnabled !== undefined) {
|
|
815
|
+
togetherParams.reasoning = { enabled: thinkingEnabled };
|
|
816
|
+
}
|
|
747
817
|
if (options?.reasoningEffort && compat.supportsReasoningEffort) {
|
|
748
818
|
togetherParams.reasoning_effort = model.thinkingLevelMap?.[options.reasoningEffort] ?? options.reasoningEffort;
|
|
749
819
|
}
|
|
@@ -844,7 +914,7 @@ function resolveChatTemplateKwargValue(model, options, value, thinkingBudget) {
|
|
|
844
914
|
return undefined;
|
|
845
915
|
}
|
|
846
916
|
if (value.$var === "thinking.enabled") {
|
|
847
|
-
return
|
|
917
|
+
return resolveThinkingToggle(model, options);
|
|
848
918
|
}
|
|
849
919
|
if (value.$var === "thinking.budget") {
|
|
850
920
|
return thinkingBudget;
|
|
@@ -852,6 +922,15 @@ function resolveChatTemplateKwargValue(model, options, value, thinkingBudget) {
|
|
|
852
922
|
const mappedValue = reasoningEffort ? model.thinkingLevelMap?.[reasoningEffort] : model.thinkingLevelMap?.off;
|
|
853
923
|
return mappedValue === undefined ? reasoningEffort : typeof mappedValue === "string" ? mappedValue : undefined;
|
|
854
924
|
}
|
|
925
|
+
/**
|
|
926
|
+
* The value of a thinking on/off toggle. A model whose thinking map denies `off` is never sent a disabled
|
|
927
|
+
* toggle it does not support, so the toggle is left out and the provider keeps its default.
|
|
928
|
+
*/
|
|
929
|
+
function resolveThinkingToggle(model, options) {
|
|
930
|
+
if (options?.reasoningEffort)
|
|
931
|
+
return true;
|
|
932
|
+
return model.thinkingLevelMap?.off === null ? undefined : false;
|
|
933
|
+
}
|
|
855
934
|
function getCompatCacheControl(compat, cacheRetention) {
|
|
856
935
|
if (compat.cacheControlFormat !== "anthropic" || cacheRetention === "none") {
|
|
857
936
|
return undefined;
|
|
@@ -1024,7 +1103,7 @@ export function convertMessages(model, context, compat, options) {
|
|
|
1024
1103
|
// Some providers don't accept null content, use empty string instead
|
|
1025
1104
|
const assistantMsg = {
|
|
1026
1105
|
role: "assistant",
|
|
1027
|
-
content: compat.requiresAssistantAfterToolResult ? "" : null,
|
|
1106
|
+
content: compat.requiresAssistantAfterToolResult || compat.requiresNonNullAssistantContent ? "" : null,
|
|
1028
1107
|
};
|
|
1029
1108
|
const assistantTextParts = msg.content
|
|
1030
1109
|
.filter(isTextContentBlock)
|
|
@@ -1108,6 +1187,10 @@ export function convertMessages(model, context, compat, options) {
|
|
|
1108
1187
|
if (preservedReasoningDetails) {
|
|
1109
1188
|
assistantMsg.reasoning_details = preservedReasoningDetails;
|
|
1110
1189
|
}
|
|
1190
|
+
const opaqueReasoning = thinkingBlocks.map(decodeOpaqueReasoning).find((data) => data !== undefined);
|
|
1191
|
+
if (opaqueReasoning !== undefined) {
|
|
1192
|
+
assistantMsg.reasoning_opaque = opaqueReasoning;
|
|
1193
|
+
}
|
|
1111
1194
|
if (compat.requiresReasoningContentOnAssistantMessages &&
|
|
1112
1195
|
model.reasoning &&
|
|
1113
1196
|
assistantMsg.reasoning_content === undefined) {
|
|
@@ -1243,14 +1326,23 @@ rawUsage, model) {
|
|
|
1243
1326
|
// providers are under-reported. DS4 mirrors this contract too:
|
|
1244
1327
|
// https://github.com/antirez/ds4/pull/29
|
|
1245
1328
|
const input = Math.max(0, promptTokens - cacheReadTokens - cacheWriteTokens);
|
|
1246
|
-
// OpenAI completion_tokens already includes reasoning_tokens.
|
|
1247
|
-
|
|
1329
|
+
// OpenAI completion_tokens already includes reasoning_tokens. GitHub Copilot's Gemini models report reasoning
|
|
1330
|
+
// only as a top-level reasoning_tokens outside completion_tokens, so their total_tokens adds all three; those
|
|
1331
|
+
// tokens are counted as output here, which `Usage.reasoning` documents as a subset of it.
|
|
1332
|
+
const completionTokens = rawUsage.completion_tokens || 0;
|
|
1333
|
+
const nestedReasoningTokens = rawUsage.completion_tokens_details?.reasoning_tokens;
|
|
1334
|
+
const topLevelReasoningTokens = rawUsage.reasoning_tokens || 0;
|
|
1335
|
+
const separateReasoningTokens = nestedReasoningTokens === undefined &&
|
|
1336
|
+
(rawUsage.total_tokens ?? 0) >= promptTokens + completionTokens + topLevelReasoningTokens
|
|
1337
|
+
? topLevelReasoningTokens
|
|
1338
|
+
: 0;
|
|
1339
|
+
const outputTokens = completionTokens + separateReasoningTokens;
|
|
1248
1340
|
const usage = {
|
|
1249
1341
|
input,
|
|
1250
1342
|
output: outputTokens,
|
|
1251
1343
|
cacheRead: cacheReadTokens,
|
|
1252
1344
|
cacheWrite: cacheWriteTokens,
|
|
1253
|
-
reasoning:
|
|
1345
|
+
reasoning: nestedReasoningTokens || topLevelReasoningTokens,
|
|
1254
1346
|
totalTokens: input + outputTokens + cacheReadTokens + cacheWriteTokens,
|
|
1255
1347
|
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
|
|
1256
1348
|
};
|
|
@@ -1341,6 +1433,8 @@ function detectCompat(model) {
|
|
|
1341
1433
|
maxTokensField: useMaxTokens ? "max_tokens" : "max_completion_tokens",
|
|
1342
1434
|
requiresToolResultName: false,
|
|
1343
1435
|
requiresAssistantAfterToolResult: false,
|
|
1436
|
+
requiresNonNullAssistantContent: false,
|
|
1437
|
+
requiresToolsWithToolHistory: false,
|
|
1344
1438
|
requiresThinkingAsText: false,
|
|
1345
1439
|
requiresReasoningContentOnAssistantMessages: isDeepSeek,
|
|
1346
1440
|
thinkingFormat: isDeepSeek
|
|
@@ -1395,6 +1489,8 @@ function getCompat(model) {
|
|
|
1395
1489
|
maxTokensField: model.compat.maxTokensField ?? detected.maxTokensField,
|
|
1396
1490
|
requiresToolResultName: model.compat.requiresToolResultName ?? detected.requiresToolResultName,
|
|
1397
1491
|
requiresAssistantAfterToolResult: model.compat.requiresAssistantAfterToolResult ?? detected.requiresAssistantAfterToolResult,
|
|
1492
|
+
requiresNonNullAssistantContent: model.compat.requiresNonNullAssistantContent ?? detected.requiresNonNullAssistantContent,
|
|
1493
|
+
requiresToolsWithToolHistory: model.compat.requiresToolsWithToolHistory ?? detected.requiresToolsWithToolHistory,
|
|
1398
1494
|
requiresThinkingAsText: model.compat.requiresThinkingAsText ?? detected.requiresThinkingAsText,
|
|
1399
1495
|
requiresReasoningContentOnAssistantMessages: model.compat.requiresReasoningContentOnAssistantMessages ??
|
|
1400
1496
|
detected.requiresReasoningContentOnAssistantMessages,
|
|
@@ -1414,6 +1510,7 @@ function getCompat(model) {
|
|
|
1414
1510
|
sendSessionAffinityHeaders: model.compat.sendSessionAffinityHeaders ?? detected.sendSessionAffinityHeaders,
|
|
1415
1511
|
sessionAffinityFormat: model.compat.sessionAffinityFormat ?? detected.sessionAffinityFormat,
|
|
1416
1512
|
supportsLongCacheRetention: model.compat.supportsLongCacheRetention ?? detected.supportsLongCacheRetention,
|
|
1513
|
+
promptCacheKeyMode: model.compat.promptCacheKeyMode,
|
|
1417
1514
|
vllmPriority: model.compat.vllmPriority,
|
|
1418
1515
|
};
|
|
1419
1516
|
}
|