@openclaw/ai 2026.8.1-beta.3 → 2026.9.1-beta.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/dist/{anthropic-B8KwTtlb.mjs → anthropic-DI4PZKKB.mjs} +18 -16
- package/dist/{anthropic-compaction-replay-4Zx7-aCr.mjs → anthropic-compaction-replay-nhKaCXu6.mjs} +8 -12
- package/dist/{anthropic-payload-policy-CSx7MoKH.d.mts → anthropic-payload-policy-B_0n0enE.d.mts} +2 -1
- package/dist/{api-registry-DFxSnFqG.d.mts → api-registry-CTd8NzcD.d.mts} +2 -1
- package/dist/{azure-openai-responses-Aa4mVcYG.mjs → azure-openai-responses-D8dI0ekg.mjs} +3 -3
- package/dist/diagnostics.d.mts +0 -1
- package/dist/diagnostics.mjs +1 -1
- package/dist/event-stream-BKp4fOt_.d.mts +1 -0
- package/dist/{event-stream-D91-ohD_.d.mts → event-stream-LiXePESD.d.mts} +1 -2
- package/dist/event-stream.d.mts +2 -1
- package/dist/{google-yDRnipeq.mjs → google-Od-LLdVK.mjs} +3 -3
- package/dist/{google-shared-M1dl64nz.mjs → google-shared-Bgr9lFPw.mjs} +14 -7
- package/dist/{google-vertex-DCJHM_a5.mjs → google-vertex-aerRSXc4.mjs} +4 -4
- package/dist/{host-DF_yxOsA.mjs → host-D51fmkH6.mjs} +4 -8
- package/dist/{host-DJq_FGcz.d.mts → host-DsbPMFU6.d.mts} +3 -3
- package/dist/index-Qbej8io0.d.mts +3 -0
- package/dist/index.d.mts +6 -6
- package/dist/index.mjs +2 -3
- package/dist/internal/anthropic.d.mts +5 -4
- package/dist/internal/anthropic.mjs +4 -4
- package/dist/internal/openai-responses-payload-policy.d.mts +1 -1
- package/dist/internal/openai-responses-payload-policy.mjs +2 -2
- package/dist/internal/openai.d.mts +5 -5
- package/dist/internal/openai.mjs +8 -7
- package/dist/internal/runtime.d.mts +16 -5
- package/dist/internal/runtime.mjs +6 -6
- package/dist/internal/shared.d.mts +2 -2
- package/dist/internal/shared.mjs +3 -3
- package/dist/{json-parse-CDnesDM_.mjs → json-parse-BuAJEbdW.mjs} +18 -2
- package/dist/{mistral-D6hRXI7A.mjs → mistral-EiHdymeb.mjs} +17 -21
- package/dist/{openai-chatgpt-responses-C339VIfT.mjs → openai-chatgpt-responses-DHpF9o1e.mjs} +28 -11
- package/dist/openai-completions-EFPiUGfS.mjs +399 -0
- package/dist/{openai-completions-compat-B0KTleXp.d.mts → openai-completions-compat-BefmHT26.d.mts} +3 -3
- package/dist/openai-completions-stream-DUlGwRMz.mjs +1621 -0
- package/dist/{openai-responses-BXEcr-Tp.mjs → openai-responses-BogP8OjX.mjs} +6 -5
- package/dist/{openai-responses-contracts-CD75pQ0m.d.mts → openai-responses-contracts-Dvj_UtPy.d.mts} +3 -3
- package/dist/{openai-responses-contracts-DvILoqBk.mjs → openai-responses-contracts-QUY12X0B.mjs} +4 -18
- package/dist/{openai-responses-payload-policy-BDo8CQsx.mjs → openai-responses-payload-policy-D8EdimYe.mjs} +3 -7
- package/dist/{openai-responses-prompt-observer-internal-BG-1AsBI.mjs → openai-responses-prompt-observer-internal-Cd4hJG-S.mjs} +3 -2
- package/dist/{openai-responses-shared-Cvt5MgEm.mjs → openai-responses-shared-DJThc6jW.mjs} +150 -1362
- package/dist/openai-stop-reason-Drnn_6Qj.mjs +28 -0
- package/dist/openai-tool-schema-ho-hIkQf.mjs +1695 -0
- package/dist/{provider-error-C8mBRJDl.mjs → provider-error-DDOs_Bz0.mjs} +1 -3
- package/dist/{provider-options-Cc5ZQ6Aa.d.mts → provider-options-MdMBLzlU.d.mts} +4 -3
- package/dist/{provider-transcript-transform--gtf0ZbR.mjs → provider-transcript-transform-C01bQ5a3.mjs} +1 -1
- package/dist/provider-types.d.mts +5 -3
- package/dist/providers.d.mts +1 -2
- package/dist/providers.mjs +9 -9
- package/dist/{sanitize-unicode-BP7HLLZn.mjs → sanitize-unicode-DCkltN94.mjs} +1 -2
- package/dist/simple-options-CFj7x3J4.mjs +118 -0
- package/dist/{anthropic-BQXQudmA.mjs → src-Bu3FiFAG.mjs} +1 -0
- package/dist/{streaming-byte-guard-BrbkbwUu.mjs → streaming-byte-guard-CC-HMn_u.mjs} +5 -6
- package/dist/string-normalization--fwJ4S2q.mjs +16 -0
- package/dist/{tool-schema-json-projection-FEr-jOoZ.mjs → tool-schema-json-projection-qvTEs5Am.mjs} +2 -2
- package/dist/{simple-options-oqsXB6U3.mjs → transport-stream-shared-ljShVIZB.mjs} +10 -118
- package/dist/transport-stream-shared-rvaCyFYt.d.mts +138 -0
- package/dist/{transport-utils-Bv0FHkd0.mjs → transport-utils-LJ1_rbi-.mjs} +8 -5
- package/dist/transports.d.mts +23 -117
- package/dist/transports.mjs +43 -1038
- package/dist/{types-DTAP49HF.d.mts → types-DhLoJbFm.d.mts} +34 -17
- package/dist/types-GiAjXatj.d.mts +1 -0
- package/dist/types.d.mts +4 -3
- package/dist/types.mjs +1 -2
- package/dist/utf16-slice-qz3nsy87.mjs +84 -0
- package/dist/{validation-DYNNrU9u.d.mts → validation-Dk7q7NMN.d.mts} +1 -2
- package/dist/validation.d.mts +1 -1
- package/package.json +5 -5
- package/dist/event-stream-BVVgDSdq.d.mts +0 -1
- package/dist/headers-B_e4-1J0.mjs +0 -9
- package/dist/index-BVVgDSdq.d.mts +0 -1
- package/dist/openai-completions-D88orSJ9.mjs +0 -632
- package/dist/openai-stop-reason-9OONKyAm.mjs +0 -579
- package/dist/openai-tool-projection-CHGLt03m.mjs +0 -426
- package/dist/record-coerce-DdXsgUd_.mjs +0 -23
- package/dist/src-D2H6yKkH.mjs +0 -2
- package/dist/stream-first-event-timeout-DvDeSucC.d.mts +0 -29
- package/dist/string-coerce-fsri9iCu.mjs +0 -34
- package/dist/types-BVVgDSdq.d.mts +0 -1
- package/dist/utf16-slice-CvGodqok.mjs +0 -29
package/dist/internal/shared.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { t as sanitizeSurrogates } from "../sanitize-unicode-
|
|
2
|
-
import { a as describeToolResultMediaPlaceholder, c as extractToolResultText, d as isImageWithMediaPayload, i as transformMessages, l as formatToolResultText, o as describeUnsupportedToolResultMedia, s as extractToolResultBlockText, u as hasMediaPayload } from "../host-
|
|
3
|
-
import {
|
|
1
|
+
import { t as sanitizeSurrogates } from "../sanitize-unicode-DCkltN94.mjs";
|
|
2
|
+
import { a as describeToolResultMediaPlaceholder, c as extractToolResultText, d as isImageWithMediaPayload, i as transformMessages, l as formatToolResultText, o as describeUnsupportedToolResultMedia, s as extractToolResultBlockText, u as hasMediaPayload } from "../host-D51fmkH6.mjs";
|
|
3
|
+
import { a as SYSTEM_PROMPT_CACHE_BOUNDARY, c as splitSystemPromptCacheBoundary, d as normalizeStructuredPromptSection, f as sortPromptCacheToolsByName, i as clampReasoning, l as stripSystemPromptCacheBoundary, n as buildBaseOptions, o as ensureSystemPromptCacheBoundary, r as clampMaxTokensToModel, s as prependSystemPromptAdditionAfterCacheBoundary, t as adjustMaxTokensForThinking, u as normalizePromptCapabilityIds } from "../simple-options-CFj7x3J4.mjs";
|
|
4
4
|
import { t as inspectTlsCertificateError } from "../tls-certificate-errors-DXSpluKI.mjs";
|
|
5
5
|
export { SYSTEM_PROMPT_CACHE_BOUNDARY, adjustMaxTokensForThinking, buildBaseOptions, clampMaxTokensToModel, clampReasoning, describeToolResultMediaPlaceholder, describeUnsupportedToolResultMedia, ensureSystemPromptCacheBoundary, extractToolResultBlockText, extractToolResultText, formatToolResultText, hasMediaPayload, inspectTlsCertificateError, isImageWithMediaPayload, normalizePromptCapabilityIds, normalizeStructuredPromptSection, prependSystemPromptAdditionAfterCacheBoundary, sanitizeSurrogates, sortPromptCacheToolsByName, splitSystemPromptCacheBoundary, stripSystemPromptCacheBoundary, transformMessages };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { n as asNonArrayRecord } from "./utf16-slice-qz3nsy87.mjs";
|
|
2
2
|
import { parse } from "partial-json";
|
|
3
3
|
//#region packages/ai/src/utils/json-parse.ts
|
|
4
4
|
const VALID_JSON_ESCAPES = /* @__PURE__ */ new Set([
|
|
@@ -126,5 +126,21 @@ function parseStreamingJson(partialJson) {
|
|
|
126
126
|
}
|
|
127
127
|
}
|
|
128
128
|
}
|
|
129
|
+
const TOOL_ARGUMENT_PREVIEW_FIRST_CHECKPOINT_CHARS = 512;
|
|
130
|
+
/**
|
|
131
|
+
* Streamed tool-call arguments are preview-only; the terminal parse re-reads
|
|
132
|
+
* the full buffer authoritatively at content_block_stop. Reparsing every delta
|
|
133
|
+
* scans an ever-growing buffer and makes assembly quadratic in the argument
|
|
134
|
+
* size, so refresh previews on a geometric length schedule instead — bounded
|
|
135
|
+
* staleness, linear total parse work.
|
|
136
|
+
*/
|
|
137
|
+
function createToolArgumentPreviewSchedule() {
|
|
138
|
+
let nextCheckpointChars = TOOL_ARGUMENT_PREVIEW_FIRST_CHECKPOINT_CHARS;
|
|
139
|
+
return (accumulatedChars) => {
|
|
140
|
+
if (accumulatedChars < nextCheckpointChars) return false;
|
|
141
|
+
nextCheckpointChars = accumulatedChars * 2;
|
|
142
|
+
return true;
|
|
143
|
+
};
|
|
144
|
+
}
|
|
129
145
|
//#endregion
|
|
130
|
-
export {
|
|
146
|
+
export { repairJson as i, parseJsonWithRepair as n, parseStreamingJson as r, createToolArgumentPreviewSchedule as t };
|
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
import { n as getEnvApiKey } from "./env-api-keys-DrgeBuva.mjs";
|
|
2
2
|
import { t as AssistantMessageEventStream } from "./event-stream-uSMZJ3FA.mjs";
|
|
3
|
-
import { i as clampThinkingLevel, r as calculateCost, t as sanitizeSurrogates } from "./sanitize-unicode-
|
|
4
|
-
import { a as describeToolResultMediaPlaceholder, c as extractToolResultText, d as isImageWithMediaPayload, l as formatToolResultText, n as getAiTransportHost } from "./host-
|
|
5
|
-
import { n as projectProviderError } from "./provider-error-
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
3
|
+
import { i as clampThinkingLevel, r as calculateCost, t as sanitizeSurrogates } from "./sanitize-unicode-DCkltN94.mjs";
|
|
4
|
+
import { a as describeToolResultMediaPlaceholder, c as extractToolResultText, d as isImageWithMediaPayload, l as formatToolResultText, n as getAiTransportHost } from "./host-D51fmkH6.mjs";
|
|
5
|
+
import { n as projectProviderError } from "./provider-error-DDOs_Bz0.mjs";
|
|
6
|
+
import { f as sortPromptCacheToolsByName, l as stripSystemPromptCacheBoundary, n as buildBaseOptions, r as clampMaxTokensToModel } from "./simple-options-CFj7x3J4.mjs";
|
|
7
|
+
import { _ as transportAbortError, f as notifyProviderHttpResponse, s as finalizeTerminalToolCallArguments } from "./transport-stream-shared-ljShVIZB.mjs";
|
|
8
|
+
import { r as parseStreamingJson, t as createToolArgumentPreviewSchedule } from "./json-parse-BuAJEbdW.mjs";
|
|
8
9
|
import { t as shortHash } from "./hash-CHgqbJmD.mjs";
|
|
9
|
-
import { t as transformProviderMessages } from "./provider-transcript-transform
|
|
10
|
-
import { t as
|
|
10
|
+
import { t as transformProviderMessages } from "./provider-transcript-transform-C01bQ5a3.mjs";
|
|
11
|
+
import { t as mapOpenAIStopReason } from "./openai-stop-reason-Drnn_6Qj.mjs";
|
|
12
|
+
import { t as createSseByteGuard } from "./streaming-byte-guard-CC-HMn_u.mjs";
|
|
11
13
|
import { randomUUID } from "node:crypto";
|
|
12
14
|
import { HTTPClient, Mistral } from "@mistralai/mistralai";
|
|
13
15
|
//#region packages/ai/src/providers/mistral.ts
|
|
@@ -107,7 +109,7 @@ const streamMistral = (model, context, options) => {
|
|
|
107
109
|
});
|
|
108
110
|
await consumeChatStream(model, output, stream, mistralStream);
|
|
109
111
|
if (options?.signal?.aborted) throw transportAbortError(options.signal);
|
|
110
|
-
if (output.stopReason === "aborted" || output.stopReason === "error") throw new Error("An unknown error occurred");
|
|
112
|
+
if (output.stopReason === "aborted" || output.stopReason === "error") throw new Error(output.errorMessage ?? "An unknown error occurred");
|
|
111
113
|
stream.push({
|
|
112
114
|
type: "done",
|
|
113
115
|
reason: output.stopReason,
|
|
@@ -241,6 +243,7 @@ async function consumeChatStream(model, output, stream, mistralStream) {
|
|
|
241
243
|
const blocks = output.content;
|
|
242
244
|
const blockIndex = () => blocks.length - 1;
|
|
243
245
|
const toolBlockIdentities = /* @__PURE__ */ new Map();
|
|
246
|
+
const toolArgumentPreviewSchedules = /* @__PURE__ */ new WeakMap();
|
|
244
247
|
const normalizeMissingToolCallId = createMistralToolCallIdNormalizer();
|
|
245
248
|
const missingToolCallIdScope = randomUUID();
|
|
246
249
|
const createMissingToolCallId = (contentIndex) => normalizeMissingToolCallId(`${missingToolCallIdScope}:toolcall:${contentIndex}`);
|
|
@@ -320,6 +323,7 @@ async function consumeChatStream(model, output, stream, mistralStream) {
|
|
|
320
323
|
for await (const event of mistralStream) {
|
|
321
324
|
const chunk = event.data;
|
|
322
325
|
output.responseId ||= chunk.id;
|
|
326
|
+
if (typeof chunk.model === "string" && chunk.model.length > 0 && chunk.model !== model.id) output.responseModel ||= chunk.model;
|
|
323
327
|
if (chunk.usage) {
|
|
324
328
|
const promptTokens = chunk.usage.promptTokens || 0;
|
|
325
329
|
const cachedPromptTokens = readMistralCachedPromptTokens(chunk.usage, promptTokens);
|
|
@@ -334,7 +338,9 @@ async function consumeChatStream(model, output, stream, mistralStream) {
|
|
|
334
338
|
if (!choice) continue;
|
|
335
339
|
if (choice.finishReason) {
|
|
336
340
|
terminalFinishReason = choice.finishReason;
|
|
337
|
-
|
|
341
|
+
const { stopReason, errorMessage } = mapOpenAIStopReason(choice.finishReason === "model_length" ? "length" : choice.finishReason);
|
|
342
|
+
output.stopReason = stopReason;
|
|
343
|
+
if (errorMessage) output.errorMessage = errorMessage;
|
|
338
344
|
}
|
|
339
345
|
const delta = choice.delta;
|
|
340
346
|
if (delta.content !== null && delta.content !== void 0) {
|
|
@@ -445,6 +451,7 @@ async function consumeChatStream(model, output, stream, mistralStream) {
|
|
|
445
451
|
partialArgs: ""
|
|
446
452
|
};
|
|
447
453
|
output.content.push(block);
|
|
454
|
+
toolArgumentPreviewSchedules.set(block, createToolArgumentPreviewSchedule());
|
|
448
455
|
toolBlockIdentities.set(contentIndex, {
|
|
449
456
|
explicitIds: new Set(providedCallId ? [providedCallId] : []),
|
|
450
457
|
functionNames: new Set(functionName ? [functionName] : []),
|
|
@@ -472,7 +479,7 @@ async function consumeChatStream(model, output, stream, mistralStream) {
|
|
|
472
479
|
if (toolCallIndex !== void 0) identity.indexes.add(toolCallIndex);
|
|
473
480
|
const argsDelta = typeof toolCall.function.arguments === "string" ? toolCall.function.arguments : JSON.stringify(toolCall.function.arguments || {});
|
|
474
481
|
block.partialArgs = (block.partialArgs || "") + argsDelta;
|
|
475
|
-
block.arguments = parseStreamingJson(block.partialArgs);
|
|
482
|
+
if (toolArgumentPreviewSchedules.get(block)?.(block.partialArgs.length)) block.arguments = parseStreamingJson(block.partialArgs);
|
|
476
483
|
stream.push({
|
|
477
484
|
type: "toolcall_delta",
|
|
478
485
|
contentIndex,
|
|
@@ -661,16 +668,5 @@ function mapToolChoice(choice, convertedToolNames) {
|
|
|
661
668
|
function: { name: toolName }
|
|
662
669
|
};
|
|
663
670
|
}
|
|
664
|
-
function mapChatStopReason(reason) {
|
|
665
|
-
if (reason === null) return "stop";
|
|
666
|
-
switch (reason) {
|
|
667
|
-
case "stop": return "stop";
|
|
668
|
-
case "length":
|
|
669
|
-
case "model_length": return "length";
|
|
670
|
-
case "tool_calls": return "toolUse";
|
|
671
|
-
case "error": return "error";
|
|
672
|
-
default: return "stop";
|
|
673
|
-
}
|
|
674
|
-
}
|
|
675
671
|
//#endregion
|
|
676
672
|
export { createBoundedMistralFetcher, streamMistral, streamSimpleMistral };
|
package/dist/{openai-chatgpt-responses-C339VIfT.mjs → openai-chatgpt-responses-DHpF9o1e.mjs}
RENAMED
|
@@ -1,23 +1,23 @@
|
|
|
1
1
|
import { n as getEnvApiKey } from "./env-api-keys-DrgeBuva.mjs";
|
|
2
2
|
import { i as formatThrownValue, n as createAssistantMessageDiagnostic, t as appendAssistantMessageDiagnostic } from "./diagnostics-COpOtRwq.mjs";
|
|
3
3
|
import { t as AssistantMessageEventStream } from "./event-stream-uSMZJ3FA.mjs";
|
|
4
|
-
import { n as getAiTransportHost, r as resolveAiTransportHeaderSentinels } from "./host-
|
|
5
|
-
import { n as projectProviderError } from "./provider-error-
|
|
6
|
-
import { n as toErrorObject, t as createResponsesPromptEgressObserver } from "./openai-responses-prompt-observer-internal-
|
|
4
|
+
import { n as getAiTransportHost, r as resolveAiTransportHeaderSentinels } from "./host-D51fmkH6.mjs";
|
|
5
|
+
import { n as projectProviderError } from "./provider-error-DDOs_Bz0.mjs";
|
|
6
|
+
import { n as toErrorObject, t as createResponsesPromptEgressObserver } from "./openai-responses-prompt-observer-internal-Cd4hJG-S.mjs";
|
|
7
7
|
import { c as resolveTimerTimeoutMs, i as clampTimerTimeoutMs } from "./number-coercion-H9qHik3g.mjs";
|
|
8
|
-
import { S as supportsOpenAITemperature, m as responsesPromptObserver } from "./openai-responses-contracts-DvILoqBk.mjs";
|
|
9
8
|
import { n as clampOpenAIPromptCacheKey } from "./openai-prompt-cache-mZTCdRPo.mjs";
|
|
10
|
-
import {
|
|
11
|
-
import { t as headersToRecord } from "./headers-B_e4-1J0.mjs";
|
|
9
|
+
import { l as stripSystemPromptCacheBoundary, n as buildBaseOptions } from "./simple-options-CFj7x3J4.mjs";
|
|
12
10
|
import { parseRetryAfterHttpDateMs } from "./internal/retry-after.mjs";
|
|
13
|
-
import { t as MALFORMED_STREAMING_FRAGMENT_ERROR_MESSAGE } from "./transport-utils-
|
|
14
|
-
import {
|
|
15
|
-
import {
|
|
11
|
+
import { t as MALFORMED_STREAMING_FRAGMENT_ERROR_MESSAGE } from "./transport-utils-LJ1_rbi-.mjs";
|
|
12
|
+
import { C as headersToRecord, _ as transportAbortError, p as notifyProviderStreamOpened, y as withProviderResponseHook } from "./transport-stream-shared-ljShVIZB.mjs";
|
|
13
|
+
import { F as commitResponsesEncryptedContentAttempt, G as suppressOpenAIResponsesCompaction, L as isInvalidEncryptedContentError, U as buildOpenAIResponsesReasoningReplayMetadata, a as resolveResponsesReasoningEffort, c as processResponsesStream, i as createResponsesAssistantOutput, n as applyResponsesServiceTierPricing, p as ResponsesStreamFailure, r as convertResponsesMessages, s as convertResponsesToolPayload, z as resolveNextResponsesEncryptedContentAttempt } from "./openai-responses-shared-DJThc6jW.mjs";
|
|
14
|
+
import { C as createOpenAIProviderAcceptanceHook, w as createOpenAIResponseHook } from "./openai-tool-schema-ho-hIkQf.mjs";
|
|
16
15
|
import { a as withFirstStreamEventTimeout, i as getFirstStreamEventTimeoutMs, r as getFirstStreamEventTimeoutHandler, t as createFirstStreamEventAbortController } from "./stream-first-event-timeout-MK28puvq.mjs";
|
|
16
|
+
import { S as supportsOpenAITemperature, m as responsesPromptObserver } from "./openai-responses-contracts-QUY12X0B.mjs";
|
|
17
17
|
import { n as registerSessionResourceCleanup } from "./session-resources-CkR4WWy1.mjs";
|
|
18
18
|
import "./diagnostics.mjs";
|
|
19
19
|
import { n as resolveOpenAICodexAccountId } from "./openai-chatgpt-jwt-KWcgd0d_.mjs";
|
|
20
|
-
import { t as createSseByteGuard } from "./streaming-byte-guard-
|
|
20
|
+
import { t as createSseByteGuard } from "./streaming-byte-guard-CC-HMn_u.mjs";
|
|
21
21
|
import { t as inspectTlsCertificateError } from "./tls-certificate-errors-DXSpluKI.mjs";
|
|
22
22
|
//#region packages/ai/src/internal/retry-sleep.ts
|
|
23
23
|
function sleepWithAbort(ms, signal) {
|
|
@@ -154,6 +154,12 @@ function isRequestTimeoutError(error, callerSignal, requestSignal, timeoutMs) {
|
|
|
154
154
|
if (!(error instanceof Error)) return false;
|
|
155
155
|
return error.name === "AbortError" || error.name === "TimeoutError" || error.message === "Request was aborted";
|
|
156
156
|
}
|
|
157
|
+
/** Local-only failure category for transport diagnostics; never provider text. */
|
|
158
|
+
function classifyStreamFailure(error, signal, requestTimedOut) {
|
|
159
|
+
if (requestTimedOut) return "timeout";
|
|
160
|
+
if (signal?.aborted) return "caller-abort";
|
|
161
|
+
return error instanceof ResponsesStreamFailure || error instanceof CodexApiError ? "provider-failure" : "transport";
|
|
162
|
+
}
|
|
157
163
|
function formatRequestTimeoutError(timeoutMs, cause) {
|
|
158
164
|
return new Error(`Request timed out after ${timeoutMs}ms`, { cause: cause instanceof Error ? cause : void 0 });
|
|
159
165
|
}
|
|
@@ -171,6 +177,7 @@ function compressRequestBodyZstd(bodyJson) {
|
|
|
171
177
|
const streamOpenAICodexResponses = (model, context, options) => {
|
|
172
178
|
const stream = new AssistantMessageEventStream();
|
|
173
179
|
(async () => {
|
|
180
|
+
const startedAt = Date.now();
|
|
174
181
|
let requestTimeoutMs;
|
|
175
182
|
let requestTimeoutSignal;
|
|
176
183
|
let activeSignal;
|
|
@@ -364,9 +371,19 @@ const streamOpenAICodexResponses = (model, context, options) => {
|
|
|
364
371
|
});
|
|
365
372
|
stream.end();
|
|
366
373
|
} catch (error) {
|
|
367
|
-
const
|
|
374
|
+
const requestTimedOut = isRequestTimeoutError(error, options?.signal, requestTimeoutSignal, requestTimeoutMs) && requestTimeoutMs !== void 0;
|
|
375
|
+
const normalizedError = requestTimedOut && requestTimeoutMs !== void 0 ? formatRequestTimeoutError(requestTimeoutMs, error) : error;
|
|
368
376
|
for (const block of output.content) delete block.partialJson;
|
|
369
377
|
const terminal = projectProviderError(normalizedError, options?.signal);
|
|
378
|
+
getAiTransportHost().logWarn("openai-transport", "ChatGPT Responses stream terminated", {
|
|
379
|
+
provider: model.provider,
|
|
380
|
+
api: model.api,
|
|
381
|
+
model: model.id,
|
|
382
|
+
transport: options?.transport || "auto",
|
|
383
|
+
elapsedMs: Math.max(0, Date.now() - startedAt),
|
|
384
|
+
stopReason: terminal.stopReason,
|
|
385
|
+
failureKind: classifyStreamFailure(error, options?.signal, requestTimedOut)
|
|
386
|
+
});
|
|
370
387
|
Object.assign(output, terminal);
|
|
371
388
|
stream.push({
|
|
372
389
|
type: "error",
|
|
@@ -0,0 +1,399 @@
|
|
|
1
|
+
import { n as getEnvApiKey, r as __exportAll } from "./env-api-keys-DrgeBuva.mjs";
|
|
2
|
+
import { t as AssistantMessageEventStream } from "./event-stream-uSMZJ3FA.mjs";
|
|
3
|
+
import { i as clampThinkingLevel } from "./sanitize-unicode-DCkltN94.mjs";
|
|
4
|
+
import { n as getAiTransportHost } from "./host-D51fmkH6.mjs";
|
|
5
|
+
import { n as projectProviderError } from "./provider-error-DDOs_Bz0.mjs";
|
|
6
|
+
import { n as clampOpenAIPromptCacheKey } from "./openai-prompt-cache-mZTCdRPo.mjs";
|
|
7
|
+
import { t as resolveCacheRetention } from "./cache-retention-0x979a5V.mjs";
|
|
8
|
+
import { c as splitSystemPromptCacheBoundary, f as sortPromptCacheToolsByName, n as buildBaseOptions } from "./simple-options-CFj7x3J4.mjs";
|
|
9
|
+
import { _ as transportAbortError, y as withProviderResponseHook } from "./transport-stream-shared-ljShVIZB.mjs";
|
|
10
|
+
import { E as tagUnresolvedTextAsCommentary, S as shouldOmitOllamaCompatResponseFormat, _ as hasToolCallHistory, b as resolveOpenAICompletionsCompat, g as convertMessages, h as resolveOpenAIReasoningEffortMap, t as processCompletionsStream, v as finalizeOpenAICompletionsToolCalls, w as clearPendingCommentaryText, x as resolveOpenAICompletionsResponseFormat } from "./openai-completions-stream-DUlGwRMz.mjs";
|
|
11
|
+
import { C as createOpenAIProviderAcceptanceHook, T as isOpenAICompletionsThinkingEnabled, j as resolveOpenAIClientBaseUrl, v as projectOpenAITools, y as reconcileOpenAICompletionsToolChoice } from "./openai-tool-schema-ho-hIkQf.mjs";
|
|
12
|
+
import { i as getFirstStreamEventTimeoutMs, r as getFirstStreamEventTimeoutHandler, t as createFirstStreamEventAbortController } from "./stream-first-event-timeout-MK28puvq.mjs";
|
|
13
|
+
import { i as resolveCloudflareBaseUrl, n as hasCopilotVisionInput, r as isCloudflareProvider, t as buildCopilotDynamicHeaders } from "./github-copilot-headers-NCJtz9i0.mjs";
|
|
14
|
+
import OpenAI from "openai";
|
|
15
|
+
//#region packages/ai/src/providers/openai-completions.ts
|
|
16
|
+
var openai_completions_exports = /* @__PURE__ */ __exportAll({
|
|
17
|
+
convertMessages: () => convertMessages,
|
|
18
|
+
streamOpenAICompletions: () => streamOpenAICompletions,
|
|
19
|
+
streamSimpleOpenAICompletions: () => streamSimpleOpenAICompletions
|
|
20
|
+
});
|
|
21
|
+
const streamOpenAICompletions = (model, context, options) => {
|
|
22
|
+
const stream = new AssistantMessageEventStream();
|
|
23
|
+
(async () => {
|
|
24
|
+
const output = {
|
|
25
|
+
role: "assistant",
|
|
26
|
+
content: [],
|
|
27
|
+
api: model.api,
|
|
28
|
+
provider: model.provider,
|
|
29
|
+
model: model.id,
|
|
30
|
+
usage: {
|
|
31
|
+
input: 0,
|
|
32
|
+
output: 0,
|
|
33
|
+
cacheRead: 0,
|
|
34
|
+
cacheWrite: 0,
|
|
35
|
+
totalTokens: 0,
|
|
36
|
+
cost: {
|
|
37
|
+
input: 0,
|
|
38
|
+
output: 0,
|
|
39
|
+
cacheRead: 0,
|
|
40
|
+
cacheWrite: 0,
|
|
41
|
+
total: 0
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
stopReason: "stop",
|
|
45
|
+
timestamp: Date.now()
|
|
46
|
+
};
|
|
47
|
+
const provisionalCommentaryTags = /* @__PURE__ */ new Map();
|
|
48
|
+
let firstEventAbort;
|
|
49
|
+
try {
|
|
50
|
+
const apiKey = options?.apiKey || getEnvApiKey(model.provider) || "";
|
|
51
|
+
const compat = resolveOpenAICompletionsCompat(model);
|
|
52
|
+
const shouldEmitReasoning = Boolean(model.reasoning && options?.reasoningEffort && isOpenAICompletionsThinkingEnabled(options.reasoningEffort));
|
|
53
|
+
const cacheRetention = resolveCacheRetention(options?.cacheRetention);
|
|
54
|
+
const cacheSessionId = cacheRetention === "none" ? void 0 : options?.sessionId;
|
|
55
|
+
const client = createClient(model, context, apiKey, options?.headers, cacheSessionId, compat);
|
|
56
|
+
let params = buildParams(model, context, options, compat, cacheRetention);
|
|
57
|
+
const nextParams = await options?.onPayload?.(params, model);
|
|
58
|
+
if (nextParams !== void 0) params = nextParams;
|
|
59
|
+
firstEventAbort = createFirstStreamEventAbortController(options?.signal);
|
|
60
|
+
const requestOptions = {
|
|
61
|
+
signal: firstEventAbort.signal,
|
|
62
|
+
...options?.timeoutMs !== void 0 ? { timeout: options.timeoutMs } : {},
|
|
63
|
+
maxRetries: options?.maxRetries ?? 0
|
|
64
|
+
};
|
|
65
|
+
const { data: openaiStream, response } = await client.chat.completions.create(params, requestOptions).withResponse();
|
|
66
|
+
const hookedOpenAIStream = withProviderResponseHook({
|
|
67
|
+
stream: openaiStream,
|
|
68
|
+
signal: firstEventAbort.signal,
|
|
69
|
+
abort: firstEventAbort.abort,
|
|
70
|
+
hook: createOpenAIProviderAcceptanceHook(options, response, model),
|
|
71
|
+
onReady: () => stream.push({
|
|
72
|
+
type: "start",
|
|
73
|
+
partial: output
|
|
74
|
+
})
|
|
75
|
+
});
|
|
76
|
+
const finishedBlocks = /* @__PURE__ */ new Set();
|
|
77
|
+
const contentIndices = /* @__PURE__ */ new WeakMap();
|
|
78
|
+
let openTextBlock;
|
|
79
|
+
let openThinkingBlock;
|
|
80
|
+
const finishBlock = (block) => {
|
|
81
|
+
const contentIndex = contentIndices.get(block);
|
|
82
|
+
if (contentIndex === void 0 || finishedBlocks.has(block)) return;
|
|
83
|
+
finishedBlocks.add(block);
|
|
84
|
+
if (block.type === "text") {
|
|
85
|
+
openTextBlock = void 0;
|
|
86
|
+
stream.push({
|
|
87
|
+
type: "text_end",
|
|
88
|
+
contentIndex,
|
|
89
|
+
content: block.text,
|
|
90
|
+
partial: output
|
|
91
|
+
});
|
|
92
|
+
} else if (block.type === "thinking") {
|
|
93
|
+
openThinkingBlock = void 0;
|
|
94
|
+
stream.push({
|
|
95
|
+
type: "thinking_end",
|
|
96
|
+
contentIndex,
|
|
97
|
+
content: block.thinking,
|
|
98
|
+
partial: output
|
|
99
|
+
});
|
|
100
|
+
} else if (block.type === "toolCall") stream.push({
|
|
101
|
+
type: "toolcall_end",
|
|
102
|
+
contentIndex,
|
|
103
|
+
toolCall: block,
|
|
104
|
+
partial: output
|
|
105
|
+
});
|
|
106
|
+
};
|
|
107
|
+
const directEventStream = { push(event) {
|
|
108
|
+
if (event.type === "text_start" || event.type === "thinking_start" || event.type === "toolcall_start") {
|
|
109
|
+
const block = output.content[event.contentIndex];
|
|
110
|
+
if (block) {
|
|
111
|
+
contentIndices.set(block, event.contentIndex);
|
|
112
|
+
if (block.type === "text") openTextBlock = block;
|
|
113
|
+
else if (block.type === "thinking") openThinkingBlock = block;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
stream.push(event);
|
|
117
|
+
} };
|
|
118
|
+
try {
|
|
119
|
+
await processCompletionsStream(hookedOpenAIStream, output, model, directEventStream, {
|
|
120
|
+
mode: "direct",
|
|
121
|
+
beforeContentBlock(nextType) {
|
|
122
|
+
if (openThinkingBlock) finishBlock(openThinkingBlock);
|
|
123
|
+
if (openTextBlock && nextType !== "toolCall") finishBlock(openTextBlock);
|
|
124
|
+
},
|
|
125
|
+
provisionalCommentaryTags,
|
|
126
|
+
signal: options?.signal,
|
|
127
|
+
emitReasoning: shouldEmitReasoning,
|
|
128
|
+
firstEventTimeoutMs: getFirstStreamEventTimeoutMs(options),
|
|
129
|
+
abortFirstEventStream: firstEventAbort.abort,
|
|
130
|
+
onFirstEventTimeout: getFirstStreamEventTimeoutHandler(options)
|
|
131
|
+
});
|
|
132
|
+
if (options?.signal?.aborted) throw transportAbortError(options.signal);
|
|
133
|
+
if (output.stopReason === "aborted" || output.stopReason === "error") throw new Error(output.errorMessage || (output.stopReason === "aborted" ? "Request was aborted" : "Provider returned an invalid tool call"));
|
|
134
|
+
} catch (error) {
|
|
135
|
+
for (const block of output.content) if (block.type !== "toolCall") finishBlock(block);
|
|
136
|
+
throw error;
|
|
137
|
+
}
|
|
138
|
+
for (const block of output.content) if (block.type !== "toolCall" || output.stopReason === "toolUse") finishBlock(block);
|
|
139
|
+
stream.push({
|
|
140
|
+
type: "done",
|
|
141
|
+
reason: output.stopReason,
|
|
142
|
+
message: output
|
|
143
|
+
});
|
|
144
|
+
stream.end();
|
|
145
|
+
} catch (error) {
|
|
146
|
+
const terminal = projectProviderError(error, options?.signal);
|
|
147
|
+
Object.assign(output, terminal);
|
|
148
|
+
finalizeOpenAICompletionsToolCalls(output, { allowSilentToolCallPromotion: false });
|
|
149
|
+
clearPendingCommentaryText(provisionalCommentaryTags);
|
|
150
|
+
tagUnresolvedTextAsCommentary(output);
|
|
151
|
+
for (const block of output.content) {
|
|
152
|
+
delete block.index;
|
|
153
|
+
delete block.partialArgs;
|
|
154
|
+
delete block.streamIndex;
|
|
155
|
+
}
|
|
156
|
+
stream.push({
|
|
157
|
+
type: "error",
|
|
158
|
+
reason: terminal.stopReason,
|
|
159
|
+
error: output
|
|
160
|
+
});
|
|
161
|
+
stream.end();
|
|
162
|
+
} finally {
|
|
163
|
+
firstEventAbort?.dispose();
|
|
164
|
+
}
|
|
165
|
+
})();
|
|
166
|
+
return stream;
|
|
167
|
+
};
|
|
168
|
+
const streamSimpleOpenAICompletions = (model, context, options) => {
|
|
169
|
+
const apiKey = options?.apiKey || getEnvApiKey(model.provider);
|
|
170
|
+
if (!apiKey) throw new Error(`No API key for provider: ${model.provider}`);
|
|
171
|
+
const base = buildBaseOptions(model, options, apiKey);
|
|
172
|
+
const clampedReasoning = options?.reasoning ? clampThinkingLevel(model, options.reasoning) : void 0;
|
|
173
|
+
const reasoningEffort = clampedReasoning === "off" ? void 0 : clampedReasoning === "max" ? "xhigh" : clampedReasoning;
|
|
174
|
+
const toolChoice = options?.toolChoice;
|
|
175
|
+
return streamOpenAICompletions(model, context, {
|
|
176
|
+
...base,
|
|
177
|
+
reasoningEffort,
|
|
178
|
+
toolChoice
|
|
179
|
+
});
|
|
180
|
+
};
|
|
181
|
+
function createClient(model, context, apiKey, optionsHeaders, sessionId, compat = resolveOpenAICompletionsCompat(model)) {
|
|
182
|
+
if (!apiKey) throw new Error(`No API key for provider: ${model.provider}`);
|
|
183
|
+
const headers = { ...model.headers };
|
|
184
|
+
if (model.provider === "github-copilot") {
|
|
185
|
+
const hasImages = hasCopilotVisionInput(context.messages);
|
|
186
|
+
const copilotHeaders = buildCopilotDynamicHeaders({
|
|
187
|
+
messages: context.messages,
|
|
188
|
+
hasImages
|
|
189
|
+
});
|
|
190
|
+
Object.assign(headers, copilotHeaders);
|
|
191
|
+
}
|
|
192
|
+
if (sessionId && compat.sessionAffinity !== "none") if (compat.sessionAffinity === "openrouter") headers["x-session-id"] = sessionId;
|
|
193
|
+
else {
|
|
194
|
+
headers.session_id = sessionId;
|
|
195
|
+
headers["x-client-request-id"] = sessionId;
|
|
196
|
+
headers["x-session-affinity"] = sessionId;
|
|
197
|
+
}
|
|
198
|
+
if (optionsHeaders) Object.assign(headers, optionsHeaders);
|
|
199
|
+
const defaultHeaders = model.provider === "cloudflare-ai-gateway" ? {
|
|
200
|
+
...headers,
|
|
201
|
+
Authorization: headers.Authorization ?? null,
|
|
202
|
+
"cf-aig-authorization": `Bearer ${apiKey}`
|
|
203
|
+
} : headers;
|
|
204
|
+
return new OpenAI({
|
|
205
|
+
apiKey,
|
|
206
|
+
baseURL: resolveOpenAIClientBaseUrl(model, isCloudflareProvider(model.provider) ? resolveCloudflareBaseUrl(model) : model.baseUrl),
|
|
207
|
+
dangerouslyAllowBrowser: true,
|
|
208
|
+
defaultHeaders,
|
|
209
|
+
fetch: getAiTransportHost().buildModelFetch(model)
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
function buildParams(model, context, options, compat = resolveOpenAICompletionsCompat(model), cacheRetention = resolveCacheRetention(options?.cacheRetention)) {
|
|
213
|
+
const cacheControl = getCompatCacheControl(compat, cacheRetention);
|
|
214
|
+
const cacheOptOutIndexes = /* @__PURE__ */ new Set();
|
|
215
|
+
const messages = convertMessages(model, context, compat, {
|
|
216
|
+
cacheOptOutIndexes,
|
|
217
|
+
preserveSystemPromptCacheBoundary: cacheControl !== void 0
|
|
218
|
+
});
|
|
219
|
+
const supportsPromptCacheKey = model.baseUrl.includes("api.openai.com") || compat.supportsPromptCacheKey;
|
|
220
|
+
const promptCacheKey = supportsPromptCacheKey && cacheRetention !== "none" ? clampOpenAIPromptCacheKey(options?.promptCacheKey ?? options?.sessionId) : void 0;
|
|
221
|
+
const params = {
|
|
222
|
+
model: model.id,
|
|
223
|
+
messages,
|
|
224
|
+
stream: true,
|
|
225
|
+
prompt_cache_key: promptCacheKey,
|
|
226
|
+
prompt_cache_retention: supportsPromptCacheKey && cacheRetention === "long" && compat.supportsLongCacheRetention ? "24h" : void 0
|
|
227
|
+
};
|
|
228
|
+
if (compat.supportsUsageInStreaming) params.stream_options = { include_usage: true };
|
|
229
|
+
if (compat.supportsStore) params.store = false;
|
|
230
|
+
if (options?.maxTokens) {
|
|
231
|
+
const maxTokens = clampOpenAICompletionsMaxTokens(model, options.maxTokens);
|
|
232
|
+
if (compat.maxTokensField === "max_tokens") params.max_tokens = maxTokens;
|
|
233
|
+
else params.max_completion_tokens = maxTokens;
|
|
234
|
+
}
|
|
235
|
+
if (options?.temperature !== void 0) params.temperature = options.temperature;
|
|
236
|
+
if (options?.stop !== void 0 && options.stop.length > 0) params.stop = options.stop;
|
|
237
|
+
const requestedResponseFormat = options?.responseFormat;
|
|
238
|
+
const responseFormat = requestedResponseFormat === void 0 ? void 0 : resolveOpenAICompletionsResponseFormat(shouldOmitOllamaCompatResponseFormat({
|
|
239
|
+
provider: model.provider,
|
|
240
|
+
baseUrl: model.baseUrl,
|
|
241
|
+
hasTools: () => Boolean(context.tools?.length)
|
|
242
|
+
}) ? void 0 : requestedResponseFormat, compat.supportsJsonSchemaResponseFormat);
|
|
243
|
+
if (responseFormat !== void 0) params.response_format = responseFormat;
|
|
244
|
+
let toolProjection;
|
|
245
|
+
if (context.tools) {
|
|
246
|
+
const converted = convertTools(context.tools, compat);
|
|
247
|
+
toolProjection = converted.projection;
|
|
248
|
+
if (converted.tools.length > 0) params.tools = converted.tools;
|
|
249
|
+
else if (hasToolCallHistory(context.messages)) params.tools = [];
|
|
250
|
+
if (compat.zaiToolStream && converted.tools.length > 0) params.tool_stream = true;
|
|
251
|
+
} else if (hasToolCallHistory(context.messages)) params.tools = [];
|
|
252
|
+
if (cacheControl) applyAnthropicCacheControl(messages, params.tools, cacheControl, cacheOptOutIndexes);
|
|
253
|
+
if (options?.toolChoice) {
|
|
254
|
+
const toolChoice = reconcileOpenAICompletionsToolChoice(options.toolChoice, toolProjection ?? projectOpenAITools([]));
|
|
255
|
+
if (toolChoice !== void 0) params.tool_choice = toolChoice;
|
|
256
|
+
}
|
|
257
|
+
const reasoningEffortMap = resolveOpenAIReasoningEffortMap(model);
|
|
258
|
+
const thinkingLevelMap = model.thinkingLevelMap;
|
|
259
|
+
const reasoningEffort = options?.reasoningEffort === void 0 ? void 0 : reasoningEffortMap[options.reasoningEffort] ?? thinkingLevelMap?.[options.reasoningEffort] ?? options.reasoningEffort;
|
|
260
|
+
const reasoningEnabled = reasoningEffort !== void 0 && reasoningEffort !== "none";
|
|
261
|
+
const offReasoningEffort = reasoningEffortMap.off ?? model.thinkingLevelMap?.off;
|
|
262
|
+
if (compat.thinkingFormat === "zai" && model.reasoning) params.thinking = reasoningEnabled ? {
|
|
263
|
+
type: "enabled",
|
|
264
|
+
clear_thinking: false
|
|
265
|
+
} : { type: "disabled" };
|
|
266
|
+
else if (compat.thinkingFormat === "qwen" && model.reasoning) params.enable_thinking = reasoningEnabled;
|
|
267
|
+
else if (compat.thinkingFormat === "qwen-chat-template" && model.reasoning) params.chat_template_kwargs = {
|
|
268
|
+
enable_thinking: reasoningEnabled,
|
|
269
|
+
preserve_thinking: true
|
|
270
|
+
};
|
|
271
|
+
else if (compat.thinkingFormat === "deepseek" && model.reasoning) {
|
|
272
|
+
params.thinking = { type: reasoningEnabled ? "enabled" : "disabled" };
|
|
273
|
+
if (reasoningEnabled && compat.supportsReasoningEffort) params.reasoning_effort = reasoningEffort;
|
|
274
|
+
} else if (compat.thinkingFormat === "openrouter" && model.reasoning) {
|
|
275
|
+
const openRouterParams = params;
|
|
276
|
+
if (reasoningEnabled) openRouterParams.reasoning = { effort: reasoningEffort };
|
|
277
|
+
else if (offReasoningEffort !== null) openRouterParams.reasoning = { effort: offReasoningEffort ?? "none" };
|
|
278
|
+
} else if (compat.thinkingFormat === "together" && model.reasoning) {
|
|
279
|
+
const togetherParams = params;
|
|
280
|
+
togetherParams.reasoning = { enabled: reasoningEnabled };
|
|
281
|
+
if (reasoningEnabled && compat.supportsReasoningEffort) togetherParams.reasoning_effort = reasoningEffort;
|
|
282
|
+
} else if (reasoningEnabled && model.reasoning && compat.supportsReasoningEffort) params.reasoning_effort = reasoningEffort;
|
|
283
|
+
else if (model.reasoning && compat.supportsReasoningEffort) {
|
|
284
|
+
if (typeof offReasoningEffort === "string") params.reasoning_effort = offReasoningEffort;
|
|
285
|
+
}
|
|
286
|
+
if (compat.openRouterRouting) params.provider = compat.openRouterRouting;
|
|
287
|
+
if (model.baseUrl.includes("ai-gateway.vercel.sh") && model.compat?.vercelGatewayRouting) {
|
|
288
|
+
const routing = model.compat.vercelGatewayRouting;
|
|
289
|
+
if (routing.only || routing.order) {
|
|
290
|
+
const gatewayOptions = {};
|
|
291
|
+
if (routing.only) gatewayOptions.only = routing.only;
|
|
292
|
+
if (routing.order) gatewayOptions.order = routing.order;
|
|
293
|
+
params.providerOptions = { gateway: gatewayOptions };
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
return params;
|
|
297
|
+
}
|
|
298
|
+
function clampOpenAICompletionsMaxTokens(model, requestedMaxTokens) {
|
|
299
|
+
const modelMaxTokens = typeof model.maxTokens === "number" && Number.isFinite(model.maxTokens) && model.maxTokens > 0 ? Math.floor(model.maxTokens) : void 0;
|
|
300
|
+
return modelMaxTokens === void 0 || requestedMaxTokens <= modelMaxTokens ? requestedMaxTokens : modelMaxTokens;
|
|
301
|
+
}
|
|
302
|
+
function getCompatCacheControl(compat, cacheRetention) {
|
|
303
|
+
if (compat.cacheControlFormat !== "anthropic" || cacheRetention === "none") return;
|
|
304
|
+
const ttl = cacheRetention === "long" && compat.supportsLongCacheRetention ? "1h" : void 0;
|
|
305
|
+
return {
|
|
306
|
+
type: "ephemeral",
|
|
307
|
+
...ttl ? { ttl } : {}
|
|
308
|
+
};
|
|
309
|
+
}
|
|
310
|
+
function applyAnthropicCacheControl(messages, tools, cacheControl, cacheOptOutIndexes) {
|
|
311
|
+
addCacheControlToSystemPrompt(messages, cacheControl);
|
|
312
|
+
addCacheControlToLastTool(tools, cacheControl);
|
|
313
|
+
addCacheControlToLastConversationMessage(messages, cacheControl, cacheOptOutIndexes);
|
|
314
|
+
}
|
|
315
|
+
function addCacheControlToSystemPrompt(messages, cacheControl) {
|
|
316
|
+
for (const message of messages) if (message.role === "system" || message.role === "developer") {
|
|
317
|
+
addCacheControlToInstructionMessage(message, cacheControl);
|
|
318
|
+
return;
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
function addCacheControlToLastConversationMessage(messages, cacheControl, cacheOptOutIndexes) {
|
|
322
|
+
for (let i = messages.length - 1; i >= 0; i--) {
|
|
323
|
+
const message = messages[i];
|
|
324
|
+
if (!message || cacheOptOutIndexes.has(i)) continue;
|
|
325
|
+
if (message.role === "user" || message.role === "assistant") {
|
|
326
|
+
if (addCacheControlToMessage(message, cacheControl)) return;
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
function addCacheControlToLastTool(tools, cacheControl) {
|
|
331
|
+
if (!tools || tools.length === 0) return;
|
|
332
|
+
const lastTool = tools.at(-1);
|
|
333
|
+
if (!lastTool) return;
|
|
334
|
+
lastTool.cache_control = cacheControl;
|
|
335
|
+
}
|
|
336
|
+
function addCacheControlToInstructionMessage(message, cacheControl) {
|
|
337
|
+
return addCacheControlToTextContent(message, cacheControl);
|
|
338
|
+
}
|
|
339
|
+
function addCacheControlToMessage(message, cacheControl) {
|
|
340
|
+
if (message.role === "user" || message.role === "assistant") return addCacheControlToTextContent(message, cacheControl);
|
|
341
|
+
return false;
|
|
342
|
+
}
|
|
343
|
+
function addCacheControlToTextContent(message, cacheControl) {
|
|
344
|
+
const content = message.content;
|
|
345
|
+
if (typeof content === "string") {
|
|
346
|
+
if (content.length === 0) return false;
|
|
347
|
+
message.content = buildCacheControlledTextParts(content, cacheControl);
|
|
348
|
+
return true;
|
|
349
|
+
}
|
|
350
|
+
if (!Array.isArray(content)) return false;
|
|
351
|
+
for (let i = content.length - 1; i >= 0; i--) {
|
|
352
|
+
const part = content[i];
|
|
353
|
+
if (part?.type === "text") {
|
|
354
|
+
const text = part.text;
|
|
355
|
+
content.splice(i, 1, ...buildCacheControlledTextParts(text, cacheControl));
|
|
356
|
+
return true;
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
return false;
|
|
360
|
+
}
|
|
361
|
+
function buildCacheControlledTextParts(text, cacheControl) {
|
|
362
|
+
const split = splitSystemPromptCacheBoundary(text);
|
|
363
|
+
if (!split) return [{
|
|
364
|
+
type: "text",
|
|
365
|
+
text,
|
|
366
|
+
cache_control: cacheControl
|
|
367
|
+
}];
|
|
368
|
+
const parts = [];
|
|
369
|
+
if (split.stablePrefix) parts.push({
|
|
370
|
+
type: "text",
|
|
371
|
+
text: split.stablePrefix,
|
|
372
|
+
cache_control: cacheControl
|
|
373
|
+
});
|
|
374
|
+
if (split.dynamicSuffix) parts.push({
|
|
375
|
+
type: "text",
|
|
376
|
+
text: split.dynamicSuffix
|
|
377
|
+
});
|
|
378
|
+
return parts.length > 0 ? parts : [{
|
|
379
|
+
type: "text",
|
|
380
|
+
text: ""
|
|
381
|
+
}];
|
|
382
|
+
}
|
|
383
|
+
function convertTools(tools, compat) {
|
|
384
|
+
const projection = projectOpenAITools(tools);
|
|
385
|
+
return {
|
|
386
|
+
projection,
|
|
387
|
+
tools: sortPromptCacheToolsByName(projection.tools).map((tool) => ({
|
|
388
|
+
type: "function",
|
|
389
|
+
function: {
|
|
390
|
+
name: tool.name,
|
|
391
|
+
description: tool.description,
|
|
392
|
+
parameters: tool.parameters,
|
|
393
|
+
...compat.supportsStrictMode && { strict: false }
|
|
394
|
+
}
|
|
395
|
+
}))
|
|
396
|
+
};
|
|
397
|
+
}
|
|
398
|
+
//#endregion
|
|
399
|
+
export { streamOpenAICompletions as n, streamSimpleOpenAICompletions as r, openai_completions_exports as t };
|
package/dist/{openai-completions-compat-B0KTleXp.d.mts → openai-completions-compat-BefmHT26.d.mts}
RENAMED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { D as Model, k as OpenAICompletionsCompat } from "./types-
|
|
2
|
-
import
|
|
3
|
-
|
|
1
|
+
import { D as Model, k as OpenAICompletionsCompat } from "./types-DhLoJbFm.mjs";
|
|
2
|
+
import "./index-Qbej8io0.mjs";
|
|
3
|
+
import { a as AiProviderRequestPolicyInput, i as AiProviderRequestCapabilities } from "./host-DsbPMFU6.mjs";
|
|
4
4
|
//#region packages/ai/src/transports/openai-completions-compat.d.ts
|
|
5
5
|
type ProviderRequestCapabilities = AiProviderRequestCapabilities;
|
|
6
6
|
type OpenAICompletionsSessionAffinity = "none" | "openai" | "openrouter";
|