@openclaw/ai 2026.8.1-beta.3 → 2026.8.2
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-DTGZ4VMY.mjs} +25 -23
- package/dist/{anthropic-compaction-replay-4Zx7-aCr.mjs → anthropic-compaction-replay-CKCDrZJv.mjs} +34 -35
- package/dist/{anthropic-payload-policy-CSx7MoKH.d.mts → anthropic-payload-policy-BTzmNC14.d.mts} +3 -2
- package/dist/{api-registry-DFxSnFqG.d.mts → api-registry-BbUHEKER.d.mts} +2 -1
- package/dist/{azure-openai-responses-Aa4mVcYG.mjs → azure-openai-responses-BmqaDXM1.mjs} +6 -5
- package/dist/{number-coercion-H9qHik3g.mjs → base64-CEFBpSkN.mjs} +73 -1
- package/dist/diagnostics.d.mts +2 -3
- package/dist/diagnostics.mjs +1 -1
- package/dist/{env-api-keys-DrgeBuva.mjs → env-api-keys-bktO00EJ.mjs} +2 -15
- package/dist/{event-stream-D91-ohD_.d.mts → event-stream-DcvAPSBe.d.mts} +1 -2
- package/dist/event-stream-pXT3FI6X.d.mts +1 -0
- package/dist/event-stream.d.mts +2 -1
- package/dist/{google-yDRnipeq.mjs → google-CqjVZMwo.mjs} +8 -6
- package/dist/{google-shared-M1dl64nz.mjs → google-shared-Bnqni5j3.mjs} +34 -25
- package/dist/{google-vertex-DCJHM_a5.mjs → google-vertex-D_WREEJi.mjs} +8 -6
- package/dist/{host-DF_yxOsA.mjs → host-BIaiBURL.mjs} +4 -8
- package/dist/{host-DJq_FGcz.d.mts → host-DKFUAmCn.d.mts} +3 -3
- package/dist/index-CefaUGSS.d.mts +111 -0
- package/dist/index.d.mts +9 -10
- package/dist/index.mjs +4 -5
- package/dist/internal/anthropic.d.mts +51 -49
- package/dist/internal/anthropic.mjs +5 -5
- package/dist/internal/openai-responses-payload-policy.d.mts +3 -3
- package/dist/internal/openai-responses-payload-policy.mjs +2 -3
- package/dist/internal/openai.d.mts +65 -63
- package/dist/internal/openai.mjs +10 -9
- package/dist/internal/retry-after.d.mts +2 -3
- package/dist/internal/runtime.d.mts +51 -40
- package/dist/internal/runtime.mjs +9 -9
- package/dist/internal/shared.d.mts +25 -25
- 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-C24jVd6f.mjs} +34 -32
- package/dist/{openai-chatgpt-responses-C339VIfT.mjs → openai-chatgpt-responses-Czum0oXz.mjs} +38 -20
- package/dist/openai-completions-BfFwac6f.mjs +404 -0
- package/dist/{openai-completions-compat-B0KTleXp.d.mts → openai-completions-compat-SZhi0Lf4.d.mts} +3 -3
- package/dist/openai-completions-stream-DGOvau4J.mjs +1625 -0
- package/dist/{openai-responses-BXEcr-Tp.mjs → openai-responses-BAryVEJM.mjs} +11 -7
- package/dist/openai-responses-compaction-window-DO7yV7az.mjs +686 -0
- package/dist/{openai-responses-contracts-CD75pQ0m.d.mts → openai-responses-contracts-BthRXano.d.mts} +14 -5
- package/dist/{openai-responses-payload-policy-CNGFT9zr.d.mts → openai-responses-payload-policy-rLRPsSmB.d.mts} +1 -0
- package/dist/{openai-responses-prompt-observer-internal-BG-1AsBI.mjs → openai-responses-prompt-observer-internal--R0kZLOZ.mjs} +3 -2
- package/dist/{openai-responses-shared-Cvt5MgEm.mjs → openai-responses-shared-CxgdGBvm.mjs} +332 -1484
- package/dist/openai-stop-reason-Drnn_6Qj.mjs +28 -0
- package/dist/openai-tool-schema-CSMh5e9O.mjs +1697 -0
- package/dist/{provider-error-C8mBRJDl.mjs → provider-error-BI8kIViN.mjs} +14 -88
- package/dist/{provider-options-Cc5ZQ6Aa.d.mts → provider-options-DJd_9euj.d.mts} +13 -4
- package/dist/{provider-transcript-transform--gtf0ZbR.mjs → provider-transcript-transform-WvJmFUAf.mjs} +1 -1
- package/dist/provider-types.d.mts +18 -17
- package/dist/providers.d.mts +5 -6
- package/dist/providers.mjs +9 -9
- package/dist/{reasoning-tag-text-partitioner-5ygO2rZc.mjs → reasoning-tag-text-partitioner-BsUX44Tq.mjs} +30 -20
- package/dist/rolldown-runtime-BhDjJH2R.mjs +15 -0
- package/dist/{sanitize-unicode-BP7HLLZn.mjs → sanitize-unicode-S6binQG-.mjs} +2 -9
- package/dist/simple-options-rdOPzr7Z.mjs +155 -0
- package/dist/{anthropic-BQXQudmA.mjs → src-C8U7lkoa.mjs} +75 -1
- package/dist/{stream-first-event-timeout-MK28puvq.mjs → stream-first-event-timeout-DcNjoFQE.mjs} +1 -1
- 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-BtZiml7r.mjs} +22 -32
- package/dist/transport-stream-shared-BbG7PThh.d.mts +138 -0
- package/dist/{simple-options-oqsXB6U3.mjs → transport-stream-shared-D8nQWCUo.mjs} +10 -118
- package/dist/{transport-utils-Bv0FHkd0.mjs → transport-utils-CtuS1Upe.mjs} +10 -10
- package/dist/transports.d.mts +130 -189
- package/dist/transports.mjs +227 -1249
- package/dist/{types-DTAP49HF.d.mts → types-Beemor2E.d.mts} +58 -24
- package/dist/types-COdhCX0U.d.mts +1 -0
- package/dist/types.d.mts +6 -5
- package/dist/types.mjs +3 -4
- package/dist/utf16-slice-qz3nsy87.mjs +84 -0
- package/dist/{validation-DYNNrU9u.d.mts → validation-CC6s7Smp.d.mts} +1 -2
- package/dist/{validation-B61OhAio.mjs → validation-CJZtym2g.mjs} +7 -5
- package/dist/validation.d.mts +1 -1
- package/dist/validation.mjs +1 -1
- package/package.json +6 -6
- package/dist/anthropic-BECQCNdF.d.mts +0 -98
- 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-responses-contracts-DvILoqBk.mjs +0 -247
- package/dist/openai-responses-payload-policy-BDo8CQsx.mjs +0 -208
- package/dist/openai-stop-reason-9OONKyAm.mjs +0 -579
- package/dist/openai-tool-projection-CHGLt03m.mjs +0 -426
- package/dist/provider-options-AvldWZt8.mjs +0 -21
- 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
|
@@ -0,0 +1,404 @@
|
|
|
1
|
+
import { t as __exportAll } from "./rolldown-runtime-BhDjJH2R.mjs";
|
|
2
|
+
import { t as AssistantMessageEventStream } from "./event-stream-uSMZJ3FA.mjs";
|
|
3
|
+
import { i as clampThinkingLevel } from "./sanitize-unicode-S6binQG-.mjs";
|
|
4
|
+
import { n as getAiTransportHost } from "./host-BIaiBURL.mjs";
|
|
5
|
+
import { n as projectProviderError } from "./provider-error-BI8kIViN.mjs";
|
|
6
|
+
import { n as clampOpenAIPromptCacheKey } from "./openai-prompt-cache-mZTCdRPo.mjs";
|
|
7
|
+
import { t as resolveCacheRetention } from "./cache-retention-0x979a5V.mjs";
|
|
8
|
+
import { m as sortPromptCacheToolsByName, n as buildBaseOptions, o as reasoningTagTextPolicy, u as splitSystemPromptCacheBoundary } from "./simple-options-rdOPzr7Z.mjs";
|
|
9
|
+
import { n as getEnvApiKey } from "./env-api-keys-bktO00EJ.mjs";
|
|
10
|
+
import { _ as transportAbortError, y as withProviderResponseHook } from "./transport-stream-shared-D8nQWCUo.mjs";
|
|
11
|
+
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-DGOvau4J.mjs";
|
|
12
|
+
import { C as createOpenAIProviderAcceptanceHook, T as isOpenAICompletionsThinkingEnabled, j as resolveOpenAIClientBaseUrl, v as projectOpenAITools, y as reconcileOpenAICompletionsToolChoice } from "./openai-tool-schema-CSMh5e9O.mjs";
|
|
13
|
+
import { i as getFirstStreamEventTimeoutMs, r as getFirstStreamEventTimeoutHandler, t as createFirstStreamEventAbortController } from "./stream-first-event-timeout-DcNjoFQE.mjs";
|
|
14
|
+
import { i as resolveCloudflareBaseUrl, n as hasCopilotVisionInput, r as isCloudflareProvider, t as buildCopilotDynamicHeaders } from "./github-copilot-headers-NCJtz9i0.mjs";
|
|
15
|
+
import OpenAI from "openai";
|
|
16
|
+
//#region packages/ai/src/providers/openai-completions.ts
|
|
17
|
+
var openai_completions_exports = /* @__PURE__ */ __exportAll({
|
|
18
|
+
convertMessages: () => convertMessages,
|
|
19
|
+
streamOpenAICompletions: () => streamOpenAICompletions,
|
|
20
|
+
streamSimpleOpenAICompletions: () => streamSimpleOpenAICompletions
|
|
21
|
+
});
|
|
22
|
+
const streamOpenAICompletions = (model, context, options) => {
|
|
23
|
+
const stream = new AssistantMessageEventStream();
|
|
24
|
+
(async () => {
|
|
25
|
+
const output = {
|
|
26
|
+
role: "assistant",
|
|
27
|
+
content: [],
|
|
28
|
+
api: model.api,
|
|
29
|
+
provider: model.provider,
|
|
30
|
+
model: model.id,
|
|
31
|
+
usage: {
|
|
32
|
+
input: 0,
|
|
33
|
+
output: 0,
|
|
34
|
+
cacheRead: 0,
|
|
35
|
+
cacheWrite: 0,
|
|
36
|
+
totalTokens: 0,
|
|
37
|
+
cost: {
|
|
38
|
+
input: 0,
|
|
39
|
+
output: 0,
|
|
40
|
+
cacheRead: 0,
|
|
41
|
+
cacheWrite: 0,
|
|
42
|
+
total: 0
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
stopReason: "stop",
|
|
46
|
+
timestamp: Date.now()
|
|
47
|
+
};
|
|
48
|
+
const provisionalCommentaryTags = /* @__PURE__ */ new Map();
|
|
49
|
+
let firstEventAbort;
|
|
50
|
+
try {
|
|
51
|
+
const apiKey = options?.apiKey || getEnvApiKey(model.provider) || "";
|
|
52
|
+
const compat = resolveOpenAICompletionsCompat(model);
|
|
53
|
+
const shouldEmitReasoning = Boolean(model.reasoning && options?.reasoningEffort && isOpenAICompletionsThinkingEnabled(options.reasoningEffort));
|
|
54
|
+
const cacheRetention = resolveCacheRetention(options?.cacheRetention);
|
|
55
|
+
const cacheSessionId = cacheRetention === "none" ? void 0 : options?.sessionId;
|
|
56
|
+
const client = createClient(model, context, apiKey, options?.headers, cacheSessionId, compat);
|
|
57
|
+
let params = buildParams(model, context, options, compat, cacheRetention);
|
|
58
|
+
const nextParams = await options?.onPayload?.(params, model);
|
|
59
|
+
if (nextParams !== void 0) params = nextParams;
|
|
60
|
+
firstEventAbort = createFirstStreamEventAbortController(options?.signal);
|
|
61
|
+
const requestOptions = {
|
|
62
|
+
signal: firstEventAbort.signal,
|
|
63
|
+
...options?.timeoutMs !== void 0 ? { timeout: options.timeoutMs } : {},
|
|
64
|
+
maxRetries: options?.maxRetries ?? 0
|
|
65
|
+
};
|
|
66
|
+
const { data: openaiStream, response } = await client.chat.completions.create(params, requestOptions).withResponse();
|
|
67
|
+
const hookedOpenAIStream = withProviderResponseHook({
|
|
68
|
+
stream: openaiStream,
|
|
69
|
+
signal: firstEventAbort.signal,
|
|
70
|
+
abort: firstEventAbort.abort,
|
|
71
|
+
hook: createOpenAIProviderAcceptanceHook(options, response, model),
|
|
72
|
+
onReady: () => stream.push({
|
|
73
|
+
type: "start",
|
|
74
|
+
partial: output
|
|
75
|
+
})
|
|
76
|
+
});
|
|
77
|
+
const finishedBlocks = /* @__PURE__ */ new Set();
|
|
78
|
+
const contentIndices = /* @__PURE__ */ new WeakMap();
|
|
79
|
+
let openTextBlock;
|
|
80
|
+
let openThinkingBlock;
|
|
81
|
+
const finishBlock = (block) => {
|
|
82
|
+
const contentIndex = contentIndices.get(block);
|
|
83
|
+
if (contentIndex === void 0 || finishedBlocks.has(block)) return;
|
|
84
|
+
finishedBlocks.add(block);
|
|
85
|
+
if (block.type === "text") {
|
|
86
|
+
openTextBlock = void 0;
|
|
87
|
+
stream.push({
|
|
88
|
+
type: "text_end",
|
|
89
|
+
contentIndex,
|
|
90
|
+
content: block.text,
|
|
91
|
+
partial: output
|
|
92
|
+
});
|
|
93
|
+
} else if (block.type === "thinking") {
|
|
94
|
+
openThinkingBlock = void 0;
|
|
95
|
+
stream.push({
|
|
96
|
+
type: "thinking_end",
|
|
97
|
+
contentIndex,
|
|
98
|
+
content: block.thinking,
|
|
99
|
+
partial: output
|
|
100
|
+
});
|
|
101
|
+
} else if (block.type === "toolCall") stream.push({
|
|
102
|
+
type: "toolcall_end",
|
|
103
|
+
contentIndex,
|
|
104
|
+
toolCall: block,
|
|
105
|
+
partial: output
|
|
106
|
+
});
|
|
107
|
+
};
|
|
108
|
+
const directEventStream = { push(event) {
|
|
109
|
+
if (event.type === "text_start" || event.type === "thinking_start" || event.type === "toolcall_start") {
|
|
110
|
+
const block = output.content[event.contentIndex];
|
|
111
|
+
if (block) {
|
|
112
|
+
contentIndices.set(block, event.contentIndex);
|
|
113
|
+
if (block.type === "text") openTextBlock = block;
|
|
114
|
+
else if (block.type === "thinking") openThinkingBlock = block;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
stream.push(event);
|
|
118
|
+
} };
|
|
119
|
+
try {
|
|
120
|
+
await processCompletionsStream(hookedOpenAIStream, output, model, directEventStream, {
|
|
121
|
+
mode: "direct",
|
|
122
|
+
beforeContentBlock(nextType) {
|
|
123
|
+
if (openThinkingBlock) finishBlock(openThinkingBlock);
|
|
124
|
+
if (openTextBlock && nextType !== "toolCall") finishBlock(openTextBlock);
|
|
125
|
+
},
|
|
126
|
+
provisionalCommentaryTags,
|
|
127
|
+
signal: options?.signal,
|
|
128
|
+
emitReasoning: shouldEmitReasoning,
|
|
129
|
+
strictReasoningTags: reasoningTagTextPolicy.isStrict(options),
|
|
130
|
+
firstEventTimeoutMs: getFirstStreamEventTimeoutMs(options),
|
|
131
|
+
abortFirstEventStream: firstEventAbort.abort,
|
|
132
|
+
onFirstEventTimeout: getFirstStreamEventTimeoutHandler(options)
|
|
133
|
+
});
|
|
134
|
+
if (options?.signal?.aborted) throw transportAbortError(options.signal);
|
|
135
|
+
if (output.stopReason === "aborted" || output.stopReason === "error") throw new Error(output.errorMessage || (output.stopReason === "aborted" ? "Request was aborted" : "Provider returned an invalid tool call"));
|
|
136
|
+
} catch (error) {
|
|
137
|
+
for (const block of output.content) if (block.type !== "toolCall") finishBlock(block);
|
|
138
|
+
throw error;
|
|
139
|
+
}
|
|
140
|
+
for (const block of output.content) if (block.type !== "toolCall" || output.stopReason === "toolUse") finishBlock(block);
|
|
141
|
+
stream.push({
|
|
142
|
+
type: "done",
|
|
143
|
+
reason: output.stopReason,
|
|
144
|
+
message: output
|
|
145
|
+
});
|
|
146
|
+
stream.end();
|
|
147
|
+
} catch (error) {
|
|
148
|
+
const terminal = projectProviderError(error, options?.signal);
|
|
149
|
+
Object.assign(output, terminal);
|
|
150
|
+
finalizeOpenAICompletionsToolCalls(output, { allowSilentToolCallPromotion: false });
|
|
151
|
+
clearPendingCommentaryText(provisionalCommentaryTags);
|
|
152
|
+
tagUnresolvedTextAsCommentary(output);
|
|
153
|
+
for (const block of output.content) {
|
|
154
|
+
delete block.index;
|
|
155
|
+
delete block.partialArgs;
|
|
156
|
+
delete block.streamIndex;
|
|
157
|
+
}
|
|
158
|
+
stream.push({
|
|
159
|
+
type: "error",
|
|
160
|
+
reason: terminal.stopReason,
|
|
161
|
+
error: output
|
|
162
|
+
});
|
|
163
|
+
stream.end();
|
|
164
|
+
} finally {
|
|
165
|
+
firstEventAbort?.dispose();
|
|
166
|
+
}
|
|
167
|
+
})();
|
|
168
|
+
return stream;
|
|
169
|
+
};
|
|
170
|
+
const streamSimpleOpenAICompletions = (model, context, options) => {
|
|
171
|
+
const apiKey = options?.apiKey || getEnvApiKey(model.provider);
|
|
172
|
+
if (!apiKey) throw new Error(`No API key for provider: ${model.provider}`);
|
|
173
|
+
const base = buildBaseOptions(model, options, apiKey);
|
|
174
|
+
const clampedReasoning = options?.reasoning ? clampThinkingLevel(model, options.reasoning) : void 0;
|
|
175
|
+
const reasoningEffort = clampedReasoning === "off" ? void 0 : clampedReasoning === "max" ? "xhigh" : clampedReasoning;
|
|
176
|
+
const toolChoice = options?.toolChoice;
|
|
177
|
+
return streamOpenAICompletions(model, context, {
|
|
178
|
+
...base,
|
|
179
|
+
reasoningEffort,
|
|
180
|
+
toolChoice
|
|
181
|
+
});
|
|
182
|
+
};
|
|
183
|
+
function createClient(model, context, apiKey, optionsHeaders, sessionId, compat = resolveOpenAICompletionsCompat(model)) {
|
|
184
|
+
if (!apiKey) throw new Error(`No API key for provider: ${model.provider}`);
|
|
185
|
+
const headers = { ...model.headers };
|
|
186
|
+
if (model.provider === "github-copilot") {
|
|
187
|
+
const hasImages = hasCopilotVisionInput(context.messages);
|
|
188
|
+
const copilotHeaders = buildCopilotDynamicHeaders({
|
|
189
|
+
messages: context.messages,
|
|
190
|
+
hasImages
|
|
191
|
+
});
|
|
192
|
+
Object.assign(headers, copilotHeaders);
|
|
193
|
+
}
|
|
194
|
+
if (sessionId && compat.sessionAffinity !== "none") {
|
|
195
|
+
if (compat.sessionAffinity === "openrouter") headers["x-session-id"] = sessionId;
|
|
196
|
+
else {
|
|
197
|
+
headers.session_id = sessionId;
|
|
198
|
+
headers["x-client-request-id"] = sessionId;
|
|
199
|
+
headers["x-session-affinity"] = sessionId;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
if (optionsHeaders) Object.assign(headers, optionsHeaders);
|
|
203
|
+
const defaultHeaders = model.provider === "cloudflare-ai-gateway" ? {
|
|
204
|
+
...headers,
|
|
205
|
+
Authorization: headers.Authorization ?? null,
|
|
206
|
+
"cf-aig-authorization": `Bearer ${apiKey}`
|
|
207
|
+
} : headers;
|
|
208
|
+
const baseUrl = isCloudflareProvider(model.provider) ? resolveCloudflareBaseUrl(model) : model.baseUrl;
|
|
209
|
+
return new OpenAI({
|
|
210
|
+
apiKey,
|
|
211
|
+
baseURL: resolveOpenAIClientBaseUrl(model, baseUrl),
|
|
212
|
+
dangerouslyAllowBrowser: true,
|
|
213
|
+
defaultHeaders,
|
|
214
|
+
fetch: getAiTransportHost().buildModelFetch(model)
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
function buildParams(model, context, options, compat = resolveOpenAICompletionsCompat(model), cacheRetention = resolveCacheRetention(options?.cacheRetention)) {
|
|
218
|
+
const cacheControl = getCompatCacheControl(compat, cacheRetention);
|
|
219
|
+
const cacheOptOutIndexes = /* @__PURE__ */ new Set();
|
|
220
|
+
const messages = convertMessages(model, context, compat, {
|
|
221
|
+
cacheOptOutIndexes,
|
|
222
|
+
preserveSystemPromptCacheBoundary: cacheControl !== void 0
|
|
223
|
+
});
|
|
224
|
+
const supportsPromptCacheKey = model.baseUrl.includes("api.openai.com") || compat.supportsPromptCacheKey;
|
|
225
|
+
const promptCacheKey = supportsPromptCacheKey && cacheRetention !== "none" ? clampOpenAIPromptCacheKey(options?.promptCacheKey ?? options?.sessionId) : void 0;
|
|
226
|
+
const params = {
|
|
227
|
+
model: model.id,
|
|
228
|
+
messages,
|
|
229
|
+
stream: true,
|
|
230
|
+
prompt_cache_key: promptCacheKey,
|
|
231
|
+
prompt_cache_retention: supportsPromptCacheKey && cacheRetention === "long" && compat.supportsLongCacheRetention ? "24h" : void 0
|
|
232
|
+
};
|
|
233
|
+
if (compat.supportsUsageInStreaming) params.stream_options = { include_usage: true };
|
|
234
|
+
if (compat.supportsStore) params.store = false;
|
|
235
|
+
if (options?.maxTokens) {
|
|
236
|
+
const maxTokens = clampOpenAICompletionsMaxTokens(model, options.maxTokens);
|
|
237
|
+
if (compat.maxTokensField === "max_tokens") params.max_tokens = maxTokens;
|
|
238
|
+
else params.max_completion_tokens = maxTokens;
|
|
239
|
+
}
|
|
240
|
+
if (options?.temperature !== void 0) params.temperature = options.temperature;
|
|
241
|
+
if (options?.stop !== void 0 && options.stop.length > 0) params.stop = options.stop;
|
|
242
|
+
const requestedResponseFormat = options?.responseFormat;
|
|
243
|
+
const responseFormat = requestedResponseFormat === void 0 ? void 0 : resolveOpenAICompletionsResponseFormat(shouldOmitOllamaCompatResponseFormat({
|
|
244
|
+
provider: model.provider,
|
|
245
|
+
baseUrl: model.baseUrl,
|
|
246
|
+
hasTools: () => Boolean(context.tools?.length)
|
|
247
|
+
}) ? void 0 : requestedResponseFormat, compat.supportsJsonSchemaResponseFormat);
|
|
248
|
+
if (responseFormat !== void 0) params.response_format = responseFormat;
|
|
249
|
+
let toolProjection;
|
|
250
|
+
if (context.tools) {
|
|
251
|
+
const converted = convertTools(context.tools, compat);
|
|
252
|
+
toolProjection = converted.projection;
|
|
253
|
+
if (converted.tools.length > 0) params.tools = converted.tools;
|
|
254
|
+
else if (hasToolCallHistory(context.messages)) params.tools = [];
|
|
255
|
+
if (compat.zaiToolStream && converted.tools.length > 0) params.tool_stream = true;
|
|
256
|
+
} else if (hasToolCallHistory(context.messages)) params.tools = [];
|
|
257
|
+
if (cacheControl) applyAnthropicCacheControl(messages, params.tools, cacheControl, cacheOptOutIndexes);
|
|
258
|
+
if (options?.toolChoice) {
|
|
259
|
+
const toolChoice = reconcileOpenAICompletionsToolChoice(options.toolChoice, toolProjection ?? projectOpenAITools([]));
|
|
260
|
+
if (toolChoice !== void 0) params.tool_choice = toolChoice;
|
|
261
|
+
}
|
|
262
|
+
const reasoningEffortMap = resolveOpenAIReasoningEffortMap(model);
|
|
263
|
+
const thinkingLevelMap = model.thinkingLevelMap;
|
|
264
|
+
const reasoningEffort = options?.reasoningEffort === void 0 ? void 0 : reasoningEffortMap[options.reasoningEffort] ?? thinkingLevelMap?.[options.reasoningEffort] ?? options.reasoningEffort;
|
|
265
|
+
const reasoningEnabled = reasoningEffort !== void 0 && reasoningEffort !== "none";
|
|
266
|
+
const offReasoningEffort = reasoningEffortMap.off ?? model.thinkingLevelMap?.off;
|
|
267
|
+
if (compat.thinkingFormat === "zai" && model.reasoning) params.thinking = reasoningEnabled ? {
|
|
268
|
+
type: "enabled",
|
|
269
|
+
clear_thinking: false
|
|
270
|
+
} : { type: "disabled" };
|
|
271
|
+
else if (compat.thinkingFormat === "qwen" && model.reasoning) params.enable_thinking = reasoningEnabled;
|
|
272
|
+
else if (compat.thinkingFormat === "qwen-chat-template" && model.reasoning) params.chat_template_kwargs = {
|
|
273
|
+
enable_thinking: reasoningEnabled,
|
|
274
|
+
preserve_thinking: true
|
|
275
|
+
};
|
|
276
|
+
else if (compat.thinkingFormat === "deepseek" && model.reasoning) {
|
|
277
|
+
params.thinking = { type: reasoningEnabled ? "enabled" : "disabled" };
|
|
278
|
+
if (reasoningEnabled && compat.supportsReasoningEffort) params.reasoning_effort = reasoningEffort;
|
|
279
|
+
} else if (compat.thinkingFormat === "openrouter" && model.reasoning) {
|
|
280
|
+
const openRouterParams = params;
|
|
281
|
+
if (reasoningEnabled) openRouterParams.reasoning = { effort: reasoningEffort };
|
|
282
|
+
else if (offReasoningEffort !== null) openRouterParams.reasoning = { effort: offReasoningEffort ?? "none" };
|
|
283
|
+
} else if (compat.thinkingFormat === "together" && model.reasoning) {
|
|
284
|
+
const togetherParams = params;
|
|
285
|
+
togetherParams.reasoning = { enabled: reasoningEnabled };
|
|
286
|
+
if (reasoningEnabled && compat.supportsReasoningEffort) togetherParams.reasoning_effort = reasoningEffort;
|
|
287
|
+
} else if (reasoningEnabled && model.reasoning && compat.supportsReasoningEffort) params.reasoning_effort = reasoningEffort;
|
|
288
|
+
else if (model.reasoning && compat.supportsReasoningEffort) {
|
|
289
|
+
if (typeof offReasoningEffort === "string") params.reasoning_effort = offReasoningEffort;
|
|
290
|
+
}
|
|
291
|
+
if (compat.openRouterRouting) params.provider = compat.openRouterRouting;
|
|
292
|
+
if (model.baseUrl.includes("ai-gateway.vercel.sh") && model.compat?.vercelGatewayRouting) {
|
|
293
|
+
const routing = model.compat.vercelGatewayRouting;
|
|
294
|
+
if (routing.only || routing.order) {
|
|
295
|
+
const gatewayOptions = {};
|
|
296
|
+
if (routing.only) gatewayOptions.only = routing.only;
|
|
297
|
+
if (routing.order) gatewayOptions.order = routing.order;
|
|
298
|
+
params.providerOptions = { gateway: gatewayOptions };
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
return params;
|
|
302
|
+
}
|
|
303
|
+
function clampOpenAICompletionsMaxTokens(model, requestedMaxTokens) {
|
|
304
|
+
const modelMaxTokens = typeof model.maxTokens === "number" && Number.isFinite(model.maxTokens) && model.maxTokens > 0 ? Math.floor(model.maxTokens) : void 0;
|
|
305
|
+
return modelMaxTokens === void 0 || requestedMaxTokens <= modelMaxTokens ? requestedMaxTokens : modelMaxTokens;
|
|
306
|
+
}
|
|
307
|
+
function getCompatCacheControl(compat, cacheRetention) {
|
|
308
|
+
if (compat.cacheControlFormat !== "anthropic" || cacheRetention === "none") return;
|
|
309
|
+
const ttl = cacheRetention === "long" && compat.supportsLongCacheRetention ? "1h" : void 0;
|
|
310
|
+
return {
|
|
311
|
+
type: "ephemeral",
|
|
312
|
+
...ttl ? { ttl } : {}
|
|
313
|
+
};
|
|
314
|
+
}
|
|
315
|
+
function applyAnthropicCacheControl(messages, tools, cacheControl, cacheOptOutIndexes) {
|
|
316
|
+
addCacheControlToSystemPrompt(messages, cacheControl);
|
|
317
|
+
addCacheControlToLastTool(tools, cacheControl);
|
|
318
|
+
addCacheControlToLastConversationMessage(messages, cacheControl, cacheOptOutIndexes);
|
|
319
|
+
}
|
|
320
|
+
function addCacheControlToSystemPrompt(messages, cacheControl) {
|
|
321
|
+
for (const message of messages) if (message.role === "system" || message.role === "developer") {
|
|
322
|
+
addCacheControlToInstructionMessage(message, cacheControl);
|
|
323
|
+
return;
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
function addCacheControlToLastConversationMessage(messages, cacheControl, cacheOptOutIndexes) {
|
|
327
|
+
for (let i = messages.length - 1; i >= 0; i--) {
|
|
328
|
+
const message = messages[i];
|
|
329
|
+
if (!message || cacheOptOutIndexes.has(i)) continue;
|
|
330
|
+
if (message.role === "user" || message.role === "assistant") {
|
|
331
|
+
if (addCacheControlToMessage(message, cacheControl)) return;
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
function addCacheControlToLastTool(tools, cacheControl) {
|
|
336
|
+
if (!tools || tools.length === 0) return;
|
|
337
|
+
const lastTool = tools.at(-1);
|
|
338
|
+
if (!lastTool) return;
|
|
339
|
+
lastTool.cache_control = cacheControl;
|
|
340
|
+
}
|
|
341
|
+
function addCacheControlToInstructionMessage(message, cacheControl) {
|
|
342
|
+
return addCacheControlToTextContent(message, cacheControl);
|
|
343
|
+
}
|
|
344
|
+
function addCacheControlToMessage(message, cacheControl) {
|
|
345
|
+
if (message.role === "user" || message.role === "assistant") return addCacheControlToTextContent(message, cacheControl);
|
|
346
|
+
return false;
|
|
347
|
+
}
|
|
348
|
+
function addCacheControlToTextContent(message, cacheControl) {
|
|
349
|
+
const content = message.content;
|
|
350
|
+
if (typeof content === "string") {
|
|
351
|
+
if (content.length === 0) return false;
|
|
352
|
+
message.content = buildCacheControlledTextParts(content, cacheControl);
|
|
353
|
+
return true;
|
|
354
|
+
}
|
|
355
|
+
if (!Array.isArray(content)) return false;
|
|
356
|
+
for (let i = content.length - 1; i >= 0; i--) {
|
|
357
|
+
const part = content[i];
|
|
358
|
+
if (part?.type === "text") {
|
|
359
|
+
const text = part.text;
|
|
360
|
+
content.splice(i, 1, ...buildCacheControlledTextParts(text, cacheControl));
|
|
361
|
+
return true;
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
return false;
|
|
365
|
+
}
|
|
366
|
+
function buildCacheControlledTextParts(text, cacheControl) {
|
|
367
|
+
const split = splitSystemPromptCacheBoundary(text);
|
|
368
|
+
if (!split) return [{
|
|
369
|
+
type: "text",
|
|
370
|
+
text,
|
|
371
|
+
cache_control: cacheControl
|
|
372
|
+
}];
|
|
373
|
+
const parts = [];
|
|
374
|
+
if (split.stablePrefix) parts.push({
|
|
375
|
+
type: "text",
|
|
376
|
+
text: split.stablePrefix,
|
|
377
|
+
cache_control: cacheControl
|
|
378
|
+
});
|
|
379
|
+
if (split.dynamicSuffix) parts.push({
|
|
380
|
+
type: "text",
|
|
381
|
+
text: split.dynamicSuffix
|
|
382
|
+
});
|
|
383
|
+
return parts.length > 0 ? parts : [{
|
|
384
|
+
type: "text",
|
|
385
|
+
text: ""
|
|
386
|
+
}];
|
|
387
|
+
}
|
|
388
|
+
function convertTools(tools, compat) {
|
|
389
|
+
const projection = projectOpenAITools(tools);
|
|
390
|
+
return {
|
|
391
|
+
projection,
|
|
392
|
+
tools: sortPromptCacheToolsByName(projection.tools).map((tool) => ({
|
|
393
|
+
type: "function",
|
|
394
|
+
function: {
|
|
395
|
+
name: tool.name,
|
|
396
|
+
description: tool.description,
|
|
397
|
+
parameters: tool.parameters,
|
|
398
|
+
...compat.supportsStrictMode && { strict: false }
|
|
399
|
+
}
|
|
400
|
+
}))
|
|
401
|
+
};
|
|
402
|
+
}
|
|
403
|
+
//#endregion
|
|
404
|
+
export { streamOpenAICompletions as n, streamSimpleOpenAICompletions as r, openai_completions_exports as t };
|
package/dist/{openai-completions-compat-B0KTleXp.d.mts → openai-completions-compat-SZhi0Lf4.d.mts}
RENAMED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
|
|
1
|
+
import "./index-CefaUGSS.mjs";
|
|
2
|
+
import { D as Model, j as OpenAICompletionsCompat } from "./types-Beemor2E.mjs";
|
|
3
|
+
import { a as AiProviderRequestPolicyInput, i as AiProviderRequestCapabilities } from "./host-DKFUAmCn.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";
|