release-note 0.0.3 → 0.0.5
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/bin.cjs +57 -98
- package/dist/bin.mjs +50 -92
- package/dist/chunk-CNf5ZN-e.mjs +37 -0
- package/dist/chunk-Cek0wNdY.cjs +64 -0
- package/dist/dist-BKDBZrkD.mjs +1372 -0
- package/dist/dist-DGil98Eq.cjs +1373 -0
- package/dist/{generate-DqPPuOfE.cjs → generate-BvQfZAwr.cjs} +4812 -5562
- package/dist/{generate-omq2Fgxt.mjs → generate-CLz75nUt.mjs} +4782 -5524
- package/dist/index.cjs +2 -4
- package/dist/index.d.cts +1 -2
- package/dist/index.d.mts +1 -2
- package/dist/index.mjs +1 -3
- package/dist/token-BTkzJW-X.mjs +56 -0
- package/dist/token-K-kJmAE8.cjs +60 -0
- package/package.json +1 -2
- package/dist/asyncToGenerator-J8hMsHFb.mjs +0 -139
- package/dist/asyncToGenerator-OdY5smjc.cjs +0 -190
- package/dist/bin.cjs.map +0 -1
- package/dist/bin.mjs.map +0 -1
- package/dist/dist-Cjmk9o-a.cjs +0 -1362
- package/dist/dist-Cjmk9o-a.cjs.map +0 -1
- package/dist/dist-WE8_S7U0.mjs +0 -1357
- package/dist/dist-WE8_S7U0.mjs.map +0 -1
- package/dist/generate-DqPPuOfE.cjs.map +0 -1
- package/dist/generate-omq2Fgxt.mjs.map +0 -1
- package/dist/index.cjs.map +0 -1
- package/dist/index.mjs.map +0 -1
- package/dist/token-CI4ILhUM.cjs +0 -70
- package/dist/token-CI4ILhUM.cjs.map +0 -1
- package/dist/token-Cbnu-uz2.mjs +0 -66
- package/dist/token-Cbnu-uz2.mjs.map +0 -1
|
@@ -0,0 +1,1373 @@
|
|
|
1
|
+
require("./chunk-Cek0wNdY.cjs");
|
|
2
|
+
const require_generate = require("./generate-BvQfZAwr.cjs");
|
|
3
|
+
//#region node_modules/.pnpm/@ai-sdk+openai-compatible@2.0.48_zod@4.4.3/node_modules/@ai-sdk/openai-compatible/dist/index.mjs
|
|
4
|
+
function toCamelCase(str) {
|
|
5
|
+
return str.replace(/[_-]([a-z])/g, (g) => g[1].toUpperCase());
|
|
6
|
+
}
|
|
7
|
+
function resolveProviderOptionsKey(rawName, providerOptions) {
|
|
8
|
+
const camelName = toCamelCase(rawName);
|
|
9
|
+
if (camelName !== rawName && (providerOptions == null ? void 0 : providerOptions[camelName]) != null) return camelName;
|
|
10
|
+
return rawName;
|
|
11
|
+
}
|
|
12
|
+
var defaultOpenAICompatibleErrorStructure = {
|
|
13
|
+
errorSchema: require_generate.object({ error: require_generate.object({
|
|
14
|
+
message: require_generate.string(),
|
|
15
|
+
type: require_generate.string().nullish(),
|
|
16
|
+
param: require_generate.any().nullish(),
|
|
17
|
+
code: require_generate.union([require_generate.string(), require_generate.number()]).nullish()
|
|
18
|
+
}) }),
|
|
19
|
+
errorToMessage: (data) => data.error.message
|
|
20
|
+
};
|
|
21
|
+
function convertOpenAICompatibleChatUsage(usage) {
|
|
22
|
+
var _a, _b, _c, _d, _e, _f;
|
|
23
|
+
if (usage == null) return {
|
|
24
|
+
inputTokens: {
|
|
25
|
+
total: void 0,
|
|
26
|
+
noCache: void 0,
|
|
27
|
+
cacheRead: void 0,
|
|
28
|
+
cacheWrite: void 0
|
|
29
|
+
},
|
|
30
|
+
outputTokens: {
|
|
31
|
+
total: void 0,
|
|
32
|
+
text: void 0,
|
|
33
|
+
reasoning: void 0
|
|
34
|
+
},
|
|
35
|
+
raw: void 0
|
|
36
|
+
};
|
|
37
|
+
const promptTokens = (_a = usage.prompt_tokens) != null ? _a : 0;
|
|
38
|
+
const completionTokens = (_b = usage.completion_tokens) != null ? _b : 0;
|
|
39
|
+
const cacheReadTokens = (_d = (_c = usage.prompt_tokens_details) == null ? void 0 : _c.cached_tokens) != null ? _d : 0;
|
|
40
|
+
const reasoningTokens = (_f = (_e = usage.completion_tokens_details) == null ? void 0 : _e.reasoning_tokens) != null ? _f : 0;
|
|
41
|
+
return {
|
|
42
|
+
inputTokens: {
|
|
43
|
+
total: promptTokens,
|
|
44
|
+
noCache: promptTokens - cacheReadTokens,
|
|
45
|
+
cacheRead: cacheReadTokens,
|
|
46
|
+
cacheWrite: void 0
|
|
47
|
+
},
|
|
48
|
+
outputTokens: {
|
|
49
|
+
total: completionTokens,
|
|
50
|
+
text: completionTokens - reasoningTokens,
|
|
51
|
+
reasoning: reasoningTokens
|
|
52
|
+
},
|
|
53
|
+
raw: usage
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
function getOpenAIMetadata(message) {
|
|
57
|
+
var _a, _b;
|
|
58
|
+
return (_b = (_a = message == null ? void 0 : message.providerOptions) == null ? void 0 : _a.openaiCompatible) != null ? _b : {};
|
|
59
|
+
}
|
|
60
|
+
function getAudioFormat(mediaType) {
|
|
61
|
+
switch (mediaType) {
|
|
62
|
+
case "audio/wav": return "wav";
|
|
63
|
+
case "audio/mp3":
|
|
64
|
+
case "audio/mpeg": return "mp3";
|
|
65
|
+
default: return null;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
function convertToOpenAICompatibleChatMessages(prompt) {
|
|
69
|
+
var _a, _b, _c;
|
|
70
|
+
const messages = [];
|
|
71
|
+
for (const { role, content, ...message } of prompt) {
|
|
72
|
+
const metadata = getOpenAIMetadata({ ...message });
|
|
73
|
+
switch (role) {
|
|
74
|
+
case "system":
|
|
75
|
+
messages.push({
|
|
76
|
+
role: "system",
|
|
77
|
+
content,
|
|
78
|
+
...metadata
|
|
79
|
+
});
|
|
80
|
+
break;
|
|
81
|
+
case "user":
|
|
82
|
+
if (content.length === 1 && content[0].type === "text") {
|
|
83
|
+
messages.push({
|
|
84
|
+
role: "user",
|
|
85
|
+
content: content[0].text,
|
|
86
|
+
...getOpenAIMetadata(content[0])
|
|
87
|
+
});
|
|
88
|
+
break;
|
|
89
|
+
}
|
|
90
|
+
messages.push({
|
|
91
|
+
role: "user",
|
|
92
|
+
content: content.map((part) => {
|
|
93
|
+
var _a2;
|
|
94
|
+
const partMetadata = getOpenAIMetadata(part);
|
|
95
|
+
switch (part.type) {
|
|
96
|
+
case "text": return {
|
|
97
|
+
type: "text",
|
|
98
|
+
text: part.text,
|
|
99
|
+
...partMetadata
|
|
100
|
+
};
|
|
101
|
+
case "file":
|
|
102
|
+
if (part.mediaType.startsWith("image/")) {
|
|
103
|
+
const mediaType = part.mediaType === "image/*" ? "image/jpeg" : part.mediaType;
|
|
104
|
+
return {
|
|
105
|
+
type: "image_url",
|
|
106
|
+
image_url: { url: part.data instanceof URL ? part.data.toString() : `data:${mediaType};base64,${require_generate.convertToBase64(part.data)}` },
|
|
107
|
+
...partMetadata
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
if (part.mediaType.startsWith("audio/")) {
|
|
111
|
+
if (part.data instanceof URL) throw new require_generate.UnsupportedFunctionalityError({ functionality: "audio file parts with URLs" });
|
|
112
|
+
const format = getAudioFormat(part.mediaType);
|
|
113
|
+
if (format === null) throw new require_generate.UnsupportedFunctionalityError({ functionality: `audio media type ${part.mediaType}` });
|
|
114
|
+
return {
|
|
115
|
+
type: "input_audio",
|
|
116
|
+
input_audio: {
|
|
117
|
+
data: require_generate.convertToBase64(part.data),
|
|
118
|
+
format
|
|
119
|
+
},
|
|
120
|
+
...partMetadata
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
if (part.mediaType === "application/pdf") {
|
|
124
|
+
if (part.data instanceof URL) throw new require_generate.UnsupportedFunctionalityError({ functionality: "PDF file parts with URLs" });
|
|
125
|
+
return {
|
|
126
|
+
type: "file",
|
|
127
|
+
file: {
|
|
128
|
+
filename: (_a2 = part.filename) != null ? _a2 : "document.pdf",
|
|
129
|
+
file_data: `data:application/pdf;base64,${require_generate.convertToBase64(part.data)}`
|
|
130
|
+
},
|
|
131
|
+
...partMetadata
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
if (part.mediaType.startsWith("text/")) return {
|
|
135
|
+
type: "text",
|
|
136
|
+
text: part.data instanceof URL ? part.data.toString() : typeof part.data === "string" ? new TextDecoder().decode(require_generate.convertBase64ToUint8Array(part.data)) : new TextDecoder().decode(part.data),
|
|
137
|
+
...partMetadata
|
|
138
|
+
};
|
|
139
|
+
throw new require_generate.UnsupportedFunctionalityError({ functionality: `file part media type ${part.mediaType}` });
|
|
140
|
+
}
|
|
141
|
+
}),
|
|
142
|
+
...metadata
|
|
143
|
+
});
|
|
144
|
+
break;
|
|
145
|
+
case "assistant": {
|
|
146
|
+
let text = "";
|
|
147
|
+
let reasoning = "";
|
|
148
|
+
const toolCalls = [];
|
|
149
|
+
for (const part of content) {
|
|
150
|
+
const partMetadata = getOpenAIMetadata(part);
|
|
151
|
+
switch (part.type) {
|
|
152
|
+
case "text":
|
|
153
|
+
text += part.text;
|
|
154
|
+
break;
|
|
155
|
+
case "reasoning":
|
|
156
|
+
reasoning += part.text;
|
|
157
|
+
break;
|
|
158
|
+
case "tool-call": {
|
|
159
|
+
const thoughtSignature = (_b = (_a = part.providerOptions) == null ? void 0 : _a.google) == null ? void 0 : _b.thoughtSignature;
|
|
160
|
+
toolCalls.push({
|
|
161
|
+
id: part.toolCallId,
|
|
162
|
+
type: "function",
|
|
163
|
+
function: {
|
|
164
|
+
name: part.toolName,
|
|
165
|
+
arguments: JSON.stringify(part.input)
|
|
166
|
+
},
|
|
167
|
+
...partMetadata,
|
|
168
|
+
...thoughtSignature ? { extra_content: { google: { thought_signature: String(thoughtSignature) } } } : {}
|
|
169
|
+
});
|
|
170
|
+
break;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
messages.push({
|
|
175
|
+
role: "assistant",
|
|
176
|
+
content: toolCalls.length > 0 ? text || null : text,
|
|
177
|
+
...reasoning.length > 0 ? { reasoning_content: reasoning } : {},
|
|
178
|
+
tool_calls: toolCalls.length > 0 ? toolCalls : void 0,
|
|
179
|
+
...metadata
|
|
180
|
+
});
|
|
181
|
+
break;
|
|
182
|
+
}
|
|
183
|
+
case "tool":
|
|
184
|
+
for (const toolResponse of content) {
|
|
185
|
+
if (toolResponse.type === "tool-approval-response") continue;
|
|
186
|
+
const output = toolResponse.output;
|
|
187
|
+
let contentValue;
|
|
188
|
+
switch (output.type) {
|
|
189
|
+
case "text":
|
|
190
|
+
case "error-text":
|
|
191
|
+
contentValue = output.value;
|
|
192
|
+
break;
|
|
193
|
+
case "execution-denied":
|
|
194
|
+
contentValue = (_c = output.reason) != null ? _c : "Tool execution denied.";
|
|
195
|
+
break;
|
|
196
|
+
case "content":
|
|
197
|
+
case "json":
|
|
198
|
+
case "error-json":
|
|
199
|
+
contentValue = JSON.stringify(output.value);
|
|
200
|
+
break;
|
|
201
|
+
}
|
|
202
|
+
const toolResponseMetadata = getOpenAIMetadata(toolResponse);
|
|
203
|
+
messages.push({
|
|
204
|
+
role: "tool",
|
|
205
|
+
tool_call_id: toolResponse.toolCallId,
|
|
206
|
+
content: contentValue,
|
|
207
|
+
...toolResponseMetadata
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
break;
|
|
211
|
+
default: throw new Error(`Unsupported role: ${role}`);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
return messages;
|
|
215
|
+
}
|
|
216
|
+
function getResponseMetadata({ id, model, created }) {
|
|
217
|
+
return {
|
|
218
|
+
id: id != null ? id : void 0,
|
|
219
|
+
modelId: model != null ? model : void 0,
|
|
220
|
+
timestamp: created != null ? /* @__PURE__ */ new Date(created * 1e3) : void 0
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
function mapOpenAICompatibleFinishReason(finishReason) {
|
|
224
|
+
switch (finishReason) {
|
|
225
|
+
case "stop": return "stop";
|
|
226
|
+
case "length": return "length";
|
|
227
|
+
case "content_filter": return "content-filter";
|
|
228
|
+
case "function_call":
|
|
229
|
+
case "tool_calls": return "tool-calls";
|
|
230
|
+
default: return "other";
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
var openaiCompatibleLanguageModelChatOptions = require_generate.object({
|
|
234
|
+
/**
|
|
235
|
+
* A unique identifier representing your end-user, which can help the provider to
|
|
236
|
+
* monitor and detect abuse.
|
|
237
|
+
*/
|
|
238
|
+
user: require_generate.string().optional(),
|
|
239
|
+
/**
|
|
240
|
+
* Reasoning effort for reasoning models. Defaults to `medium`.
|
|
241
|
+
*/
|
|
242
|
+
reasoningEffort: require_generate.string().optional(),
|
|
243
|
+
/**
|
|
244
|
+
* Controls the verbosity of the generated text. Defaults to `medium`.
|
|
245
|
+
*/
|
|
246
|
+
textVerbosity: require_generate.string().optional(),
|
|
247
|
+
/**
|
|
248
|
+
* Whether to use strict JSON schema validation.
|
|
249
|
+
* When true, the model uses constrained decoding to guarantee schema compliance.
|
|
250
|
+
* Only used when the provider supports structured outputs and a schema is provided.
|
|
251
|
+
*
|
|
252
|
+
* @default true
|
|
253
|
+
*/
|
|
254
|
+
strictJsonSchema: require_generate.boolean().optional()
|
|
255
|
+
});
|
|
256
|
+
function prepareTools({ tools, toolChoice }) {
|
|
257
|
+
tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
|
|
258
|
+
const toolWarnings = [];
|
|
259
|
+
if (tools == null) return {
|
|
260
|
+
tools: void 0,
|
|
261
|
+
toolChoice: void 0,
|
|
262
|
+
toolWarnings
|
|
263
|
+
};
|
|
264
|
+
const openaiCompatTools = [];
|
|
265
|
+
for (const tool of tools) if (tool.type === "provider") toolWarnings.push({
|
|
266
|
+
type: "unsupported",
|
|
267
|
+
feature: `provider-defined tool ${tool.id}`
|
|
268
|
+
});
|
|
269
|
+
else openaiCompatTools.push({
|
|
270
|
+
type: "function",
|
|
271
|
+
function: {
|
|
272
|
+
name: tool.name,
|
|
273
|
+
description: tool.description,
|
|
274
|
+
parameters: tool.inputSchema,
|
|
275
|
+
...tool.strict != null ? { strict: tool.strict } : {}
|
|
276
|
+
}
|
|
277
|
+
});
|
|
278
|
+
if (toolChoice == null) return {
|
|
279
|
+
tools: openaiCompatTools,
|
|
280
|
+
toolChoice: void 0,
|
|
281
|
+
toolWarnings
|
|
282
|
+
};
|
|
283
|
+
const type = toolChoice.type;
|
|
284
|
+
switch (type) {
|
|
285
|
+
case "auto":
|
|
286
|
+
case "none":
|
|
287
|
+
case "required": return {
|
|
288
|
+
tools: openaiCompatTools,
|
|
289
|
+
toolChoice: type,
|
|
290
|
+
toolWarnings
|
|
291
|
+
};
|
|
292
|
+
case "tool": return {
|
|
293
|
+
tools: openaiCompatTools,
|
|
294
|
+
toolChoice: {
|
|
295
|
+
type: "function",
|
|
296
|
+
function: { name: toolChoice.toolName }
|
|
297
|
+
},
|
|
298
|
+
toolWarnings
|
|
299
|
+
};
|
|
300
|
+
default: throw new require_generate.UnsupportedFunctionalityError({ functionality: `tool choice type: ${type}` });
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
var OpenAICompatibleChatLanguageModel = class {
|
|
304
|
+
constructor(modelId, config) {
|
|
305
|
+
this.specificationVersion = "v3";
|
|
306
|
+
var _a, _b;
|
|
307
|
+
this.modelId = modelId;
|
|
308
|
+
this.config = config;
|
|
309
|
+
const errorStructure = (_a = config.errorStructure) != null ? _a : defaultOpenAICompatibleErrorStructure;
|
|
310
|
+
this.chunkSchema = createOpenAICompatibleChatChunkSchema(errorStructure.errorSchema);
|
|
311
|
+
this.failedResponseHandler = require_generate.createJsonErrorResponseHandler(errorStructure);
|
|
312
|
+
this.supportsStructuredOutputs = (_b = config.supportsStructuredOutputs) != null ? _b : false;
|
|
313
|
+
}
|
|
314
|
+
get provider() {
|
|
315
|
+
return this.config.provider;
|
|
316
|
+
}
|
|
317
|
+
get providerOptionsName() {
|
|
318
|
+
return this.config.provider.split(".")[0].trim();
|
|
319
|
+
}
|
|
320
|
+
get supportedUrls() {
|
|
321
|
+
var _a, _b, _c;
|
|
322
|
+
return (_c = (_b = (_a = this.config).supportedUrls) == null ? void 0 : _b.call(_a)) != null ? _c : {};
|
|
323
|
+
}
|
|
324
|
+
transformRequestBody(args) {
|
|
325
|
+
var _a, _b, _c;
|
|
326
|
+
return (_c = (_b = (_a = this.config).transformRequestBody) == null ? void 0 : _b.call(_a, args)) != null ? _c : args;
|
|
327
|
+
}
|
|
328
|
+
convertUsage(usage) {
|
|
329
|
+
var _a, _b, _c;
|
|
330
|
+
return (_c = (_b = (_a = this.config).convertUsage) == null ? void 0 : _b.call(_a, usage)) != null ? _c : convertOpenAICompatibleChatUsage(usage);
|
|
331
|
+
}
|
|
332
|
+
async getArgs({ prompt, maxOutputTokens, temperature, topP, topK, frequencyPenalty, presencePenalty, providerOptions, stopSequences, responseFormat, seed, toolChoice, tools }) {
|
|
333
|
+
var _a, _b, _c, _d, _e;
|
|
334
|
+
const warnings = [];
|
|
335
|
+
const deprecatedOptions = await require_generate.parseProviderOptions({
|
|
336
|
+
provider: "openai-compatible",
|
|
337
|
+
providerOptions,
|
|
338
|
+
schema: openaiCompatibleLanguageModelChatOptions
|
|
339
|
+
});
|
|
340
|
+
if (deprecatedOptions != null) warnings.push({
|
|
341
|
+
type: "other",
|
|
342
|
+
message: `The 'openai-compatible' key in providerOptions is deprecated. Use 'openaiCompatible' instead.`
|
|
343
|
+
});
|
|
344
|
+
const compatibleOptions = Object.assign(deprecatedOptions != null ? deprecatedOptions : {}, (_a = await require_generate.parseProviderOptions({
|
|
345
|
+
provider: "openaiCompatible",
|
|
346
|
+
providerOptions,
|
|
347
|
+
schema: openaiCompatibleLanguageModelChatOptions
|
|
348
|
+
})) != null ? _a : {}, (_b = await require_generate.parseProviderOptions({
|
|
349
|
+
provider: this.providerOptionsName,
|
|
350
|
+
providerOptions,
|
|
351
|
+
schema: openaiCompatibleLanguageModelChatOptions
|
|
352
|
+
})) != null ? _b : {}, (_c = await require_generate.parseProviderOptions({
|
|
353
|
+
provider: toCamelCase(this.providerOptionsName),
|
|
354
|
+
providerOptions,
|
|
355
|
+
schema: openaiCompatibleLanguageModelChatOptions
|
|
356
|
+
})) != null ? _c : {});
|
|
357
|
+
const strictJsonSchema = (_d = compatibleOptions == null ? void 0 : compatibleOptions.strictJsonSchema) != null ? _d : true;
|
|
358
|
+
if (topK != null) warnings.push({
|
|
359
|
+
type: "unsupported",
|
|
360
|
+
feature: "topK"
|
|
361
|
+
});
|
|
362
|
+
if ((responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null && !this.supportsStructuredOutputs) warnings.push({
|
|
363
|
+
type: "unsupported",
|
|
364
|
+
feature: "responseFormat",
|
|
365
|
+
details: "JSON response format schema is only supported with structuredOutputs"
|
|
366
|
+
});
|
|
367
|
+
const { tools: openaiTools, toolChoice: openaiToolChoice, toolWarnings } = prepareTools({
|
|
368
|
+
tools,
|
|
369
|
+
toolChoice
|
|
370
|
+
});
|
|
371
|
+
return {
|
|
372
|
+
metadataKey: resolveProviderOptionsKey(this.providerOptionsName, providerOptions),
|
|
373
|
+
args: {
|
|
374
|
+
model: this.modelId,
|
|
375
|
+
user: compatibleOptions.user,
|
|
376
|
+
max_tokens: maxOutputTokens,
|
|
377
|
+
temperature,
|
|
378
|
+
top_p: topP,
|
|
379
|
+
frequency_penalty: frequencyPenalty,
|
|
380
|
+
presence_penalty: presencePenalty,
|
|
381
|
+
response_format: (responseFormat == null ? void 0 : responseFormat.type) === "json" ? this.supportsStructuredOutputs === true && responseFormat.schema != null ? {
|
|
382
|
+
type: "json_schema",
|
|
383
|
+
json_schema: {
|
|
384
|
+
schema: responseFormat.schema,
|
|
385
|
+
strict: strictJsonSchema,
|
|
386
|
+
name: (_e = responseFormat.name) != null ? _e : "response",
|
|
387
|
+
description: responseFormat.description
|
|
388
|
+
}
|
|
389
|
+
} : { type: "json_object" } : void 0,
|
|
390
|
+
stop: stopSequences,
|
|
391
|
+
seed,
|
|
392
|
+
...Object.fromEntries(Object.entries({
|
|
393
|
+
...providerOptions == null ? void 0 : providerOptions[this.providerOptionsName],
|
|
394
|
+
...providerOptions == null ? void 0 : providerOptions[toCamelCase(this.providerOptionsName)]
|
|
395
|
+
}).filter(([key]) => !Object.keys(openaiCompatibleLanguageModelChatOptions.shape).includes(key))),
|
|
396
|
+
reasoning_effort: compatibleOptions.reasoningEffort,
|
|
397
|
+
verbosity: compatibleOptions.textVerbosity,
|
|
398
|
+
messages: convertToOpenAICompatibleChatMessages(prompt),
|
|
399
|
+
tools: openaiTools,
|
|
400
|
+
tool_choice: openaiToolChoice
|
|
401
|
+
},
|
|
402
|
+
warnings: [...warnings, ...toolWarnings]
|
|
403
|
+
};
|
|
404
|
+
}
|
|
405
|
+
async doGenerate(options) {
|
|
406
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
407
|
+
const { args, warnings, metadataKey } = await this.getArgs({ ...options });
|
|
408
|
+
const transformedBody = this.transformRequestBody(args);
|
|
409
|
+
const body = JSON.stringify(transformedBody);
|
|
410
|
+
const { responseHeaders, value: responseBody, rawValue: rawResponse } = await require_generate.postJsonToApi({
|
|
411
|
+
url: this.config.url({
|
|
412
|
+
path: "/chat/completions",
|
|
413
|
+
modelId: this.modelId
|
|
414
|
+
}),
|
|
415
|
+
headers: require_generate.combineHeaders(this.config.headers(), options.headers),
|
|
416
|
+
body: transformedBody,
|
|
417
|
+
failedResponseHandler: this.failedResponseHandler,
|
|
418
|
+
successfulResponseHandler: require_generate.createJsonResponseHandler(OpenAICompatibleChatResponseSchema),
|
|
419
|
+
abortSignal: options.abortSignal,
|
|
420
|
+
fetch: this.config.fetch
|
|
421
|
+
});
|
|
422
|
+
const choice = responseBody.choices[0];
|
|
423
|
+
const content = [];
|
|
424
|
+
const text = choice.message.content;
|
|
425
|
+
if (text != null && text.length > 0) content.push({
|
|
426
|
+
type: "text",
|
|
427
|
+
text
|
|
428
|
+
});
|
|
429
|
+
const reasoning = (_a = choice.message.reasoning_content) != null ? _a : choice.message.reasoning;
|
|
430
|
+
if (reasoning != null && reasoning.length > 0) content.push({
|
|
431
|
+
type: "reasoning",
|
|
432
|
+
text: reasoning
|
|
433
|
+
});
|
|
434
|
+
if (choice.message.tool_calls != null) for (const toolCall of choice.message.tool_calls) {
|
|
435
|
+
const thoughtSignature = (_c = (_b = toolCall.extra_content) == null ? void 0 : _b.google) == null ? void 0 : _c.thought_signature;
|
|
436
|
+
content.push({
|
|
437
|
+
type: "tool-call",
|
|
438
|
+
toolCallId: (_d = toolCall.id) != null ? _d : require_generate.generateId(),
|
|
439
|
+
toolName: toolCall.function.name,
|
|
440
|
+
input: toolCall.function.arguments,
|
|
441
|
+
...thoughtSignature ? { providerMetadata: { [metadataKey]: { thoughtSignature } } } : {}
|
|
442
|
+
});
|
|
443
|
+
}
|
|
444
|
+
const providerMetadata = {
|
|
445
|
+
[metadataKey]: {},
|
|
446
|
+
...await ((_f = (_e = this.config.metadataExtractor) == null ? void 0 : _e.extractMetadata) == null ? void 0 : _f.call(_e, { parsedBody: rawResponse }))
|
|
447
|
+
};
|
|
448
|
+
const completionTokenDetails = (_g = responseBody.usage) == null ? void 0 : _g.completion_tokens_details;
|
|
449
|
+
if ((completionTokenDetails == null ? void 0 : completionTokenDetails.accepted_prediction_tokens) != null) providerMetadata[metadataKey].acceptedPredictionTokens = completionTokenDetails == null ? void 0 : completionTokenDetails.accepted_prediction_tokens;
|
|
450
|
+
if ((completionTokenDetails == null ? void 0 : completionTokenDetails.rejected_prediction_tokens) != null) providerMetadata[metadataKey].rejectedPredictionTokens = completionTokenDetails == null ? void 0 : completionTokenDetails.rejected_prediction_tokens;
|
|
451
|
+
return {
|
|
452
|
+
content,
|
|
453
|
+
finishReason: {
|
|
454
|
+
unified: mapOpenAICompatibleFinishReason(choice.finish_reason),
|
|
455
|
+
raw: (_h = choice.finish_reason) != null ? _h : void 0
|
|
456
|
+
},
|
|
457
|
+
usage: this.convertUsage(responseBody.usage),
|
|
458
|
+
providerMetadata,
|
|
459
|
+
request: { body },
|
|
460
|
+
response: {
|
|
461
|
+
...getResponseMetadata(responseBody),
|
|
462
|
+
headers: responseHeaders,
|
|
463
|
+
body: rawResponse
|
|
464
|
+
},
|
|
465
|
+
warnings
|
|
466
|
+
};
|
|
467
|
+
}
|
|
468
|
+
async doStream(options) {
|
|
469
|
+
var _a;
|
|
470
|
+
const { args, warnings, metadataKey } = await this.getArgs({ ...options });
|
|
471
|
+
const body = this.transformRequestBody({
|
|
472
|
+
...args,
|
|
473
|
+
stream: true,
|
|
474
|
+
stream_options: this.config.includeUsage ? { include_usage: true } : void 0
|
|
475
|
+
});
|
|
476
|
+
const metadataExtractor = (_a = this.config.metadataExtractor) == null ? void 0 : _a.createStreamExtractor();
|
|
477
|
+
const { responseHeaders, value: response } = await require_generate.postJsonToApi({
|
|
478
|
+
url: this.config.url({
|
|
479
|
+
path: "/chat/completions",
|
|
480
|
+
modelId: this.modelId
|
|
481
|
+
}),
|
|
482
|
+
headers: require_generate.combineHeaders(this.config.headers(), options.headers),
|
|
483
|
+
body,
|
|
484
|
+
failedResponseHandler: this.failedResponseHandler,
|
|
485
|
+
successfulResponseHandler: require_generate.createEventSourceResponseHandler(this.chunkSchema),
|
|
486
|
+
abortSignal: options.abortSignal,
|
|
487
|
+
fetch: this.config.fetch
|
|
488
|
+
});
|
|
489
|
+
const toolCalls = [];
|
|
490
|
+
let finishReason = {
|
|
491
|
+
unified: "other",
|
|
492
|
+
raw: void 0
|
|
493
|
+
};
|
|
494
|
+
let usage = void 0;
|
|
495
|
+
let isFirstChunk = true;
|
|
496
|
+
const providerOptionsName = metadataKey;
|
|
497
|
+
let isActiveReasoning = false;
|
|
498
|
+
let isActiveText = false;
|
|
499
|
+
const convertUsage = (usage2) => this.convertUsage(usage2);
|
|
500
|
+
return {
|
|
501
|
+
stream: response.pipeThrough(new TransformStream({
|
|
502
|
+
start(controller) {
|
|
503
|
+
controller.enqueue({
|
|
504
|
+
type: "stream-start",
|
|
505
|
+
warnings
|
|
506
|
+
});
|
|
507
|
+
},
|
|
508
|
+
transform(chunk, controller) {
|
|
509
|
+
var _a2, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r;
|
|
510
|
+
if (options.includeRawChunks) controller.enqueue({
|
|
511
|
+
type: "raw",
|
|
512
|
+
rawValue: chunk.rawValue
|
|
513
|
+
});
|
|
514
|
+
if (!chunk.success) {
|
|
515
|
+
finishReason = {
|
|
516
|
+
unified: "error",
|
|
517
|
+
raw: void 0
|
|
518
|
+
};
|
|
519
|
+
controller.enqueue({
|
|
520
|
+
type: "error",
|
|
521
|
+
error: chunk.error
|
|
522
|
+
});
|
|
523
|
+
return;
|
|
524
|
+
}
|
|
525
|
+
metadataExtractor?.processChunk(chunk.rawValue);
|
|
526
|
+
if ("error" in chunk.value) {
|
|
527
|
+
finishReason = {
|
|
528
|
+
unified: "error",
|
|
529
|
+
raw: void 0
|
|
530
|
+
};
|
|
531
|
+
controller.enqueue({
|
|
532
|
+
type: "error",
|
|
533
|
+
error: chunk.value.error.message
|
|
534
|
+
});
|
|
535
|
+
return;
|
|
536
|
+
}
|
|
537
|
+
const value = chunk.value;
|
|
538
|
+
if (isFirstChunk) {
|
|
539
|
+
isFirstChunk = false;
|
|
540
|
+
controller.enqueue({
|
|
541
|
+
type: "response-metadata",
|
|
542
|
+
...getResponseMetadata(value)
|
|
543
|
+
});
|
|
544
|
+
}
|
|
545
|
+
if (value.usage != null) usage = value.usage;
|
|
546
|
+
const choice = value.choices[0];
|
|
547
|
+
if ((choice == null ? void 0 : choice.finish_reason) != null) finishReason = {
|
|
548
|
+
unified: mapOpenAICompatibleFinishReason(choice.finish_reason),
|
|
549
|
+
raw: (_a2 = choice.finish_reason) != null ? _a2 : void 0
|
|
550
|
+
};
|
|
551
|
+
if ((choice == null ? void 0 : choice.delta) == null) return;
|
|
552
|
+
const delta = choice.delta;
|
|
553
|
+
const reasoningContent = (_b = delta.reasoning_content) != null ? _b : delta.reasoning;
|
|
554
|
+
if (reasoningContent) {
|
|
555
|
+
if (!isActiveReasoning) {
|
|
556
|
+
controller.enqueue({
|
|
557
|
+
type: "reasoning-start",
|
|
558
|
+
id: "reasoning-0"
|
|
559
|
+
});
|
|
560
|
+
isActiveReasoning = true;
|
|
561
|
+
}
|
|
562
|
+
controller.enqueue({
|
|
563
|
+
type: "reasoning-delta",
|
|
564
|
+
id: "reasoning-0",
|
|
565
|
+
delta: reasoningContent
|
|
566
|
+
});
|
|
567
|
+
}
|
|
568
|
+
if (delta.content) {
|
|
569
|
+
if (isActiveReasoning) {
|
|
570
|
+
controller.enqueue({
|
|
571
|
+
type: "reasoning-end",
|
|
572
|
+
id: "reasoning-0"
|
|
573
|
+
});
|
|
574
|
+
isActiveReasoning = false;
|
|
575
|
+
}
|
|
576
|
+
if (!isActiveText) {
|
|
577
|
+
controller.enqueue({
|
|
578
|
+
type: "text-start",
|
|
579
|
+
id: "txt-0"
|
|
580
|
+
});
|
|
581
|
+
isActiveText = true;
|
|
582
|
+
}
|
|
583
|
+
controller.enqueue({
|
|
584
|
+
type: "text-delta",
|
|
585
|
+
id: "txt-0",
|
|
586
|
+
delta: delta.content
|
|
587
|
+
});
|
|
588
|
+
}
|
|
589
|
+
if (delta.tool_calls != null) {
|
|
590
|
+
if (isActiveReasoning) {
|
|
591
|
+
controller.enqueue({
|
|
592
|
+
type: "reasoning-end",
|
|
593
|
+
id: "reasoning-0"
|
|
594
|
+
});
|
|
595
|
+
isActiveReasoning = false;
|
|
596
|
+
}
|
|
597
|
+
for (const toolCallDelta of delta.tool_calls) {
|
|
598
|
+
const index = (_c = toolCallDelta.index) != null ? _c : toolCalls.length;
|
|
599
|
+
if (toolCalls[index] == null) {
|
|
600
|
+
if (toolCallDelta.id == null) throw new require_generate.InvalidResponseDataError({
|
|
601
|
+
data: toolCallDelta,
|
|
602
|
+
message: `Expected 'id' to be a string.`
|
|
603
|
+
});
|
|
604
|
+
if (((_d = toolCallDelta.function) == null ? void 0 : _d.name) == null) throw new require_generate.InvalidResponseDataError({
|
|
605
|
+
data: toolCallDelta,
|
|
606
|
+
message: `Expected 'function.name' to be a string.`
|
|
607
|
+
});
|
|
608
|
+
controller.enqueue({
|
|
609
|
+
type: "tool-input-start",
|
|
610
|
+
id: toolCallDelta.id,
|
|
611
|
+
toolName: toolCallDelta.function.name
|
|
612
|
+
});
|
|
613
|
+
toolCalls[index] = {
|
|
614
|
+
id: toolCallDelta.id,
|
|
615
|
+
type: "function",
|
|
616
|
+
function: {
|
|
617
|
+
name: toolCallDelta.function.name,
|
|
618
|
+
arguments: (_e = toolCallDelta.function.arguments) != null ? _e : ""
|
|
619
|
+
},
|
|
620
|
+
hasFinished: false,
|
|
621
|
+
thoughtSignature: (_h = (_g = (_f = toolCallDelta.extra_content) == null ? void 0 : _f.google) == null ? void 0 : _g.thought_signature) != null ? _h : void 0
|
|
622
|
+
};
|
|
623
|
+
const toolCall2 = toolCalls[index];
|
|
624
|
+
if (((_i = toolCall2.function) == null ? void 0 : _i.name) != null && ((_j = toolCall2.function) == null ? void 0 : _j.arguments) != null) {
|
|
625
|
+
if (toolCall2.function.arguments.length > 0) controller.enqueue({
|
|
626
|
+
type: "tool-input-delta",
|
|
627
|
+
id: toolCall2.id,
|
|
628
|
+
delta: toolCall2.function.arguments
|
|
629
|
+
});
|
|
630
|
+
if (require_generate.isParsableJson(toolCall2.function.arguments)) {
|
|
631
|
+
controller.enqueue({
|
|
632
|
+
type: "tool-input-end",
|
|
633
|
+
id: toolCall2.id
|
|
634
|
+
});
|
|
635
|
+
controller.enqueue({
|
|
636
|
+
type: "tool-call",
|
|
637
|
+
toolCallId: (_k = toolCall2.id) != null ? _k : require_generate.generateId(),
|
|
638
|
+
toolName: toolCall2.function.name,
|
|
639
|
+
input: toolCall2.function.arguments,
|
|
640
|
+
...toolCall2.thoughtSignature ? { providerMetadata: { [providerOptionsName]: { thoughtSignature: toolCall2.thoughtSignature } } } : {}
|
|
641
|
+
});
|
|
642
|
+
toolCall2.hasFinished = true;
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
continue;
|
|
646
|
+
}
|
|
647
|
+
const toolCall = toolCalls[index];
|
|
648
|
+
if (toolCall.hasFinished) continue;
|
|
649
|
+
if (((_l = toolCallDelta.function) == null ? void 0 : _l.arguments) != null) toolCall.function.arguments += (_n = (_m = toolCallDelta.function) == null ? void 0 : _m.arguments) != null ? _n : "";
|
|
650
|
+
controller.enqueue({
|
|
651
|
+
type: "tool-input-delta",
|
|
652
|
+
id: toolCall.id,
|
|
653
|
+
delta: (_o = toolCallDelta.function.arguments) != null ? _o : ""
|
|
654
|
+
});
|
|
655
|
+
if (((_p = toolCall.function) == null ? void 0 : _p.name) != null && ((_q = toolCall.function) == null ? void 0 : _q.arguments) != null && require_generate.isParsableJson(toolCall.function.arguments)) {
|
|
656
|
+
controller.enqueue({
|
|
657
|
+
type: "tool-input-end",
|
|
658
|
+
id: toolCall.id
|
|
659
|
+
});
|
|
660
|
+
controller.enqueue({
|
|
661
|
+
type: "tool-call",
|
|
662
|
+
toolCallId: (_r = toolCall.id) != null ? _r : require_generate.generateId(),
|
|
663
|
+
toolName: toolCall.function.name,
|
|
664
|
+
input: toolCall.function.arguments,
|
|
665
|
+
...toolCall.thoughtSignature ? { providerMetadata: { [providerOptionsName]: { thoughtSignature: toolCall.thoughtSignature } } } : {}
|
|
666
|
+
});
|
|
667
|
+
toolCall.hasFinished = true;
|
|
668
|
+
}
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
},
|
|
672
|
+
flush(controller) {
|
|
673
|
+
var _a2, _b, _c, _d, _e;
|
|
674
|
+
if (isActiveReasoning) controller.enqueue({
|
|
675
|
+
type: "reasoning-end",
|
|
676
|
+
id: "reasoning-0"
|
|
677
|
+
});
|
|
678
|
+
if (isActiveText) controller.enqueue({
|
|
679
|
+
type: "text-end",
|
|
680
|
+
id: "txt-0"
|
|
681
|
+
});
|
|
682
|
+
for (const toolCall of toolCalls.filter((toolCall2) => !toolCall2.hasFinished)) {
|
|
683
|
+
controller.enqueue({
|
|
684
|
+
type: "tool-input-end",
|
|
685
|
+
id: toolCall.id
|
|
686
|
+
});
|
|
687
|
+
controller.enqueue({
|
|
688
|
+
type: "tool-call",
|
|
689
|
+
toolCallId: (_a2 = toolCall.id) != null ? _a2 : require_generate.generateId(),
|
|
690
|
+
toolName: toolCall.function.name,
|
|
691
|
+
input: toolCall.function.arguments,
|
|
692
|
+
...toolCall.thoughtSignature ? { providerMetadata: { [providerOptionsName]: { thoughtSignature: toolCall.thoughtSignature } } } : {}
|
|
693
|
+
});
|
|
694
|
+
}
|
|
695
|
+
const providerMetadata = {
|
|
696
|
+
[providerOptionsName]: {},
|
|
697
|
+
...metadataExtractor == null ? void 0 : metadataExtractor.buildMetadata()
|
|
698
|
+
};
|
|
699
|
+
if (((_b = usage == null ? void 0 : usage.completion_tokens_details) == null ? void 0 : _b.accepted_prediction_tokens) != null) providerMetadata[providerOptionsName].acceptedPredictionTokens = (_c = usage == null ? void 0 : usage.completion_tokens_details) == null ? void 0 : _c.accepted_prediction_tokens;
|
|
700
|
+
if (((_d = usage == null ? void 0 : usage.completion_tokens_details) == null ? void 0 : _d.rejected_prediction_tokens) != null) providerMetadata[providerOptionsName].rejectedPredictionTokens = (_e = usage == null ? void 0 : usage.completion_tokens_details) == null ? void 0 : _e.rejected_prediction_tokens;
|
|
701
|
+
controller.enqueue({
|
|
702
|
+
type: "finish",
|
|
703
|
+
finishReason,
|
|
704
|
+
usage: convertUsage(usage),
|
|
705
|
+
providerMetadata
|
|
706
|
+
});
|
|
707
|
+
}
|
|
708
|
+
})),
|
|
709
|
+
request: { body },
|
|
710
|
+
response: { headers: responseHeaders }
|
|
711
|
+
};
|
|
712
|
+
}
|
|
713
|
+
};
|
|
714
|
+
var openaiCompatibleTokenUsageSchema = require_generate.looseObject({
|
|
715
|
+
prompt_tokens: require_generate.number().nullish(),
|
|
716
|
+
completion_tokens: require_generate.number().nullish(),
|
|
717
|
+
total_tokens: require_generate.number().nullish(),
|
|
718
|
+
prompt_tokens_details: require_generate.object({ cached_tokens: require_generate.number().nullish() }).nullish(),
|
|
719
|
+
completion_tokens_details: require_generate.object({
|
|
720
|
+
reasoning_tokens: require_generate.number().nullish(),
|
|
721
|
+
accepted_prediction_tokens: require_generate.number().nullish(),
|
|
722
|
+
rejected_prediction_tokens: require_generate.number().nullish()
|
|
723
|
+
}).nullish()
|
|
724
|
+
}).nullish();
|
|
725
|
+
var OpenAICompatibleChatResponseSchema = require_generate.looseObject({
|
|
726
|
+
id: require_generate.string().nullish(),
|
|
727
|
+
created: require_generate.number().nullish(),
|
|
728
|
+
model: require_generate.string().nullish(),
|
|
729
|
+
choices: require_generate.array(require_generate.object({
|
|
730
|
+
message: require_generate.object({
|
|
731
|
+
role: require_generate.literal("assistant").nullish(),
|
|
732
|
+
content: require_generate.string().nullish(),
|
|
733
|
+
reasoning_content: require_generate.string().nullish(),
|
|
734
|
+
reasoning: require_generate.string().nullish(),
|
|
735
|
+
tool_calls: require_generate.array(require_generate.object({
|
|
736
|
+
id: require_generate.string().nullish(),
|
|
737
|
+
function: require_generate.object({
|
|
738
|
+
name: require_generate.string(),
|
|
739
|
+
arguments: require_generate.string()
|
|
740
|
+
}),
|
|
741
|
+
extra_content: require_generate.object({ google: require_generate.object({ thought_signature: require_generate.string().nullish() }).nullish() }).nullish()
|
|
742
|
+
})).nullish()
|
|
743
|
+
}),
|
|
744
|
+
finish_reason: require_generate.string().nullish()
|
|
745
|
+
})),
|
|
746
|
+
usage: openaiCompatibleTokenUsageSchema
|
|
747
|
+
});
|
|
748
|
+
var chunkBaseSchema = require_generate.looseObject({
|
|
749
|
+
id: require_generate.string().nullish(),
|
|
750
|
+
created: require_generate.number().nullish(),
|
|
751
|
+
model: require_generate.string().nullish(),
|
|
752
|
+
choices: require_generate.array(require_generate.object({
|
|
753
|
+
delta: require_generate.object({
|
|
754
|
+
role: require_generate._enum(["assistant", ""]).nullish(),
|
|
755
|
+
content: require_generate.string().nullish(),
|
|
756
|
+
reasoning_content: require_generate.string().nullish(),
|
|
757
|
+
reasoning: require_generate.string().nullish(),
|
|
758
|
+
tool_calls: require_generate.array(require_generate.object({
|
|
759
|
+
index: require_generate.number().nullish(),
|
|
760
|
+
id: require_generate.string().nullish(),
|
|
761
|
+
function: require_generate.object({
|
|
762
|
+
name: require_generate.string().nullish(),
|
|
763
|
+
arguments: require_generate.string().nullish()
|
|
764
|
+
}),
|
|
765
|
+
extra_content: require_generate.object({ google: require_generate.object({ thought_signature: require_generate.string().nullish() }).nullish() }).nullish()
|
|
766
|
+
})).nullish()
|
|
767
|
+
}).nullish(),
|
|
768
|
+
finish_reason: require_generate.string().nullish()
|
|
769
|
+
})),
|
|
770
|
+
usage: openaiCompatibleTokenUsageSchema
|
|
771
|
+
});
|
|
772
|
+
var createOpenAICompatibleChatChunkSchema = (errorSchema) => require_generate.union([chunkBaseSchema, errorSchema]);
|
|
773
|
+
function convertOpenAICompatibleCompletionUsage(usage) {
|
|
774
|
+
var _a, _b;
|
|
775
|
+
if (usage == null) return {
|
|
776
|
+
inputTokens: {
|
|
777
|
+
total: void 0,
|
|
778
|
+
noCache: void 0,
|
|
779
|
+
cacheRead: void 0,
|
|
780
|
+
cacheWrite: void 0
|
|
781
|
+
},
|
|
782
|
+
outputTokens: {
|
|
783
|
+
total: void 0,
|
|
784
|
+
text: void 0,
|
|
785
|
+
reasoning: void 0
|
|
786
|
+
},
|
|
787
|
+
raw: void 0
|
|
788
|
+
};
|
|
789
|
+
const promptTokens = (_a = usage.prompt_tokens) != null ? _a : 0;
|
|
790
|
+
const completionTokens = (_b = usage.completion_tokens) != null ? _b : 0;
|
|
791
|
+
return {
|
|
792
|
+
inputTokens: {
|
|
793
|
+
total: promptTokens,
|
|
794
|
+
noCache: promptTokens,
|
|
795
|
+
cacheRead: void 0,
|
|
796
|
+
cacheWrite: void 0
|
|
797
|
+
},
|
|
798
|
+
outputTokens: {
|
|
799
|
+
total: completionTokens,
|
|
800
|
+
text: completionTokens,
|
|
801
|
+
reasoning: void 0
|
|
802
|
+
},
|
|
803
|
+
raw: usage
|
|
804
|
+
};
|
|
805
|
+
}
|
|
806
|
+
function convertToOpenAICompatibleCompletionPrompt({ prompt, user = "user", assistant = "assistant" }) {
|
|
807
|
+
let text = "";
|
|
808
|
+
if (prompt[0].role === "system") {
|
|
809
|
+
text += `${prompt[0].content}
|
|
810
|
+
|
|
811
|
+
`;
|
|
812
|
+
prompt = prompt.slice(1);
|
|
813
|
+
}
|
|
814
|
+
for (const { role, content } of prompt) switch (role) {
|
|
815
|
+
case "system": throw new require_generate.InvalidPromptError({
|
|
816
|
+
message: "Unexpected system message in prompt: ${content}",
|
|
817
|
+
prompt
|
|
818
|
+
});
|
|
819
|
+
case "user": {
|
|
820
|
+
const userMessage = content.map((part) => {
|
|
821
|
+
switch (part.type) {
|
|
822
|
+
case "text": return part.text;
|
|
823
|
+
}
|
|
824
|
+
}).filter(Boolean).join("");
|
|
825
|
+
text += `${user}:
|
|
826
|
+
${userMessage}
|
|
827
|
+
|
|
828
|
+
`;
|
|
829
|
+
break;
|
|
830
|
+
}
|
|
831
|
+
case "assistant": {
|
|
832
|
+
const assistantMessage = content.map((part) => {
|
|
833
|
+
switch (part.type) {
|
|
834
|
+
case "text": return part.text;
|
|
835
|
+
case "tool-call": throw new require_generate.UnsupportedFunctionalityError({ functionality: "tool-call messages" });
|
|
836
|
+
}
|
|
837
|
+
}).join("");
|
|
838
|
+
text += `${assistant}:
|
|
839
|
+
${assistantMessage}
|
|
840
|
+
|
|
841
|
+
`;
|
|
842
|
+
break;
|
|
843
|
+
}
|
|
844
|
+
case "tool": throw new require_generate.UnsupportedFunctionalityError({ functionality: "tool messages" });
|
|
845
|
+
default: throw new Error(`Unsupported role: ${role}`);
|
|
846
|
+
}
|
|
847
|
+
text += `${assistant}:
|
|
848
|
+
`;
|
|
849
|
+
return {
|
|
850
|
+
prompt: text,
|
|
851
|
+
stopSequences: [`
|
|
852
|
+
${user}:`]
|
|
853
|
+
};
|
|
854
|
+
}
|
|
855
|
+
function getResponseMetadata2({ id, model, created }) {
|
|
856
|
+
return {
|
|
857
|
+
id: id != null ? id : void 0,
|
|
858
|
+
modelId: model != null ? model : void 0,
|
|
859
|
+
timestamp: created != null ? /* @__PURE__ */ new Date(created * 1e3) : void 0
|
|
860
|
+
};
|
|
861
|
+
}
|
|
862
|
+
function mapOpenAICompatibleFinishReason2(finishReason) {
|
|
863
|
+
switch (finishReason) {
|
|
864
|
+
case "stop": return "stop";
|
|
865
|
+
case "length": return "length";
|
|
866
|
+
case "content_filter": return "content-filter";
|
|
867
|
+
case "function_call":
|
|
868
|
+
case "tool_calls": return "tool-calls";
|
|
869
|
+
default: return "other";
|
|
870
|
+
}
|
|
871
|
+
}
|
|
872
|
+
var openaiCompatibleLanguageModelCompletionOptions = require_generate.object({
|
|
873
|
+
/**
|
|
874
|
+
* Echo back the prompt in addition to the completion.
|
|
875
|
+
*/
|
|
876
|
+
echo: require_generate.boolean().optional(),
|
|
877
|
+
/**
|
|
878
|
+
* Modify the likelihood of specified tokens appearing in the completion.
|
|
879
|
+
*
|
|
880
|
+
* Accepts a JSON object that maps tokens (specified by their token ID in
|
|
881
|
+
* the GPT tokenizer) to an associated bias value from -100 to 100.
|
|
882
|
+
*/
|
|
883
|
+
logitBias: require_generate.record(require_generate.string(), require_generate.number()).optional(),
|
|
884
|
+
/**
|
|
885
|
+
* The suffix that comes after a completion of inserted text.
|
|
886
|
+
*/
|
|
887
|
+
suffix: require_generate.string().optional(),
|
|
888
|
+
/**
|
|
889
|
+
* A unique identifier representing your end-user, which can help providers to
|
|
890
|
+
* monitor and detect abuse.
|
|
891
|
+
*/
|
|
892
|
+
user: require_generate.string().optional()
|
|
893
|
+
});
|
|
894
|
+
var OpenAICompatibleCompletionLanguageModel = class {
|
|
895
|
+
constructor(modelId, config) {
|
|
896
|
+
this.specificationVersion = "v3";
|
|
897
|
+
var _a;
|
|
898
|
+
this.modelId = modelId;
|
|
899
|
+
this.config = config;
|
|
900
|
+
const errorStructure = (_a = config.errorStructure) != null ? _a : defaultOpenAICompatibleErrorStructure;
|
|
901
|
+
this.chunkSchema = createOpenAICompatibleCompletionChunkSchema(errorStructure.errorSchema);
|
|
902
|
+
this.failedResponseHandler = require_generate.createJsonErrorResponseHandler(errorStructure);
|
|
903
|
+
}
|
|
904
|
+
get provider() {
|
|
905
|
+
return this.config.provider;
|
|
906
|
+
}
|
|
907
|
+
get providerOptionsName() {
|
|
908
|
+
return this.config.provider.split(".")[0].trim();
|
|
909
|
+
}
|
|
910
|
+
get supportedUrls() {
|
|
911
|
+
var _a, _b, _c;
|
|
912
|
+
return (_c = (_b = (_a = this.config).supportedUrls) == null ? void 0 : _b.call(_a)) != null ? _c : {};
|
|
913
|
+
}
|
|
914
|
+
async getArgs({ prompt, maxOutputTokens, temperature, topP, topK, frequencyPenalty, presencePenalty, stopSequences: userStopSequences, responseFormat, seed, providerOptions, tools, toolChoice }) {
|
|
915
|
+
var _a, _b;
|
|
916
|
+
const warnings = [];
|
|
917
|
+
const completionOptions = Object.assign((_a = await require_generate.parseProviderOptions({
|
|
918
|
+
provider: this.providerOptionsName,
|
|
919
|
+
providerOptions,
|
|
920
|
+
schema: openaiCompatibleLanguageModelCompletionOptions
|
|
921
|
+
})) != null ? _a : {}, (_b = await require_generate.parseProviderOptions({
|
|
922
|
+
provider: toCamelCase(this.providerOptionsName),
|
|
923
|
+
providerOptions,
|
|
924
|
+
schema: openaiCompatibleLanguageModelCompletionOptions
|
|
925
|
+
})) != null ? _b : {});
|
|
926
|
+
if (topK != null) warnings.push({
|
|
927
|
+
type: "unsupported",
|
|
928
|
+
feature: "topK"
|
|
929
|
+
});
|
|
930
|
+
if (tools == null ? void 0 : tools.length) warnings.push({
|
|
931
|
+
type: "unsupported",
|
|
932
|
+
feature: "tools"
|
|
933
|
+
});
|
|
934
|
+
if (toolChoice != null) warnings.push({
|
|
935
|
+
type: "unsupported",
|
|
936
|
+
feature: "toolChoice"
|
|
937
|
+
});
|
|
938
|
+
if (responseFormat != null && responseFormat.type !== "text") warnings.push({
|
|
939
|
+
type: "unsupported",
|
|
940
|
+
feature: "responseFormat",
|
|
941
|
+
details: "JSON response format is not supported."
|
|
942
|
+
});
|
|
943
|
+
const { prompt: completionPrompt, stopSequences } = convertToOpenAICompatibleCompletionPrompt({ prompt });
|
|
944
|
+
const stop = [...stopSequences != null ? stopSequences : [], ...userStopSequences != null ? userStopSequences : []];
|
|
945
|
+
return {
|
|
946
|
+
args: {
|
|
947
|
+
model: this.modelId,
|
|
948
|
+
echo: completionOptions.echo,
|
|
949
|
+
logit_bias: completionOptions.logitBias,
|
|
950
|
+
suffix: completionOptions.suffix,
|
|
951
|
+
user: completionOptions.user,
|
|
952
|
+
max_tokens: maxOutputTokens,
|
|
953
|
+
temperature,
|
|
954
|
+
top_p: topP,
|
|
955
|
+
frequency_penalty: frequencyPenalty,
|
|
956
|
+
presence_penalty: presencePenalty,
|
|
957
|
+
seed,
|
|
958
|
+
...providerOptions == null ? void 0 : providerOptions[this.providerOptionsName],
|
|
959
|
+
...providerOptions == null ? void 0 : providerOptions[toCamelCase(this.providerOptionsName)],
|
|
960
|
+
prompt: completionPrompt,
|
|
961
|
+
stop: stop.length > 0 ? stop : void 0
|
|
962
|
+
},
|
|
963
|
+
warnings
|
|
964
|
+
};
|
|
965
|
+
}
|
|
966
|
+
async doGenerate(options) {
|
|
967
|
+
const { args, warnings } = await this.getArgs(options);
|
|
968
|
+
const { responseHeaders, value: response, rawValue: rawResponse } = await require_generate.postJsonToApi({
|
|
969
|
+
url: this.config.url({
|
|
970
|
+
path: "/completions",
|
|
971
|
+
modelId: this.modelId
|
|
972
|
+
}),
|
|
973
|
+
headers: require_generate.combineHeaders(this.config.headers(), options.headers),
|
|
974
|
+
body: args,
|
|
975
|
+
failedResponseHandler: this.failedResponseHandler,
|
|
976
|
+
successfulResponseHandler: require_generate.createJsonResponseHandler(openaiCompatibleCompletionResponseSchema),
|
|
977
|
+
abortSignal: options.abortSignal,
|
|
978
|
+
fetch: this.config.fetch
|
|
979
|
+
});
|
|
980
|
+
const choice = response.choices[0];
|
|
981
|
+
const content = [];
|
|
982
|
+
if (choice.text != null && choice.text.length > 0) content.push({
|
|
983
|
+
type: "text",
|
|
984
|
+
text: choice.text
|
|
985
|
+
});
|
|
986
|
+
return {
|
|
987
|
+
content,
|
|
988
|
+
usage: convertOpenAICompatibleCompletionUsage(response.usage),
|
|
989
|
+
finishReason: {
|
|
990
|
+
unified: mapOpenAICompatibleFinishReason2(choice.finish_reason),
|
|
991
|
+
raw: choice.finish_reason
|
|
992
|
+
},
|
|
993
|
+
request: { body: args },
|
|
994
|
+
response: {
|
|
995
|
+
...getResponseMetadata2(response),
|
|
996
|
+
headers: responseHeaders,
|
|
997
|
+
body: rawResponse
|
|
998
|
+
},
|
|
999
|
+
warnings
|
|
1000
|
+
};
|
|
1001
|
+
}
|
|
1002
|
+
async doStream(options) {
|
|
1003
|
+
const { args, warnings } = await this.getArgs(options);
|
|
1004
|
+
const body = {
|
|
1005
|
+
...args,
|
|
1006
|
+
stream: true,
|
|
1007
|
+
stream_options: this.config.includeUsage ? { include_usage: true } : void 0
|
|
1008
|
+
};
|
|
1009
|
+
const { responseHeaders, value: response } = await require_generate.postJsonToApi({
|
|
1010
|
+
url: this.config.url({
|
|
1011
|
+
path: "/completions",
|
|
1012
|
+
modelId: this.modelId
|
|
1013
|
+
}),
|
|
1014
|
+
headers: require_generate.combineHeaders(this.config.headers(), options.headers),
|
|
1015
|
+
body,
|
|
1016
|
+
failedResponseHandler: this.failedResponseHandler,
|
|
1017
|
+
successfulResponseHandler: require_generate.createEventSourceResponseHandler(this.chunkSchema),
|
|
1018
|
+
abortSignal: options.abortSignal,
|
|
1019
|
+
fetch: this.config.fetch
|
|
1020
|
+
});
|
|
1021
|
+
let finishReason = {
|
|
1022
|
+
unified: "other",
|
|
1023
|
+
raw: void 0
|
|
1024
|
+
};
|
|
1025
|
+
let usage = void 0;
|
|
1026
|
+
let isFirstChunk = true;
|
|
1027
|
+
return {
|
|
1028
|
+
stream: response.pipeThrough(new TransformStream({
|
|
1029
|
+
start(controller) {
|
|
1030
|
+
controller.enqueue({
|
|
1031
|
+
type: "stream-start",
|
|
1032
|
+
warnings
|
|
1033
|
+
});
|
|
1034
|
+
},
|
|
1035
|
+
transform(chunk, controller) {
|
|
1036
|
+
var _a;
|
|
1037
|
+
if (options.includeRawChunks) controller.enqueue({
|
|
1038
|
+
type: "raw",
|
|
1039
|
+
rawValue: chunk.rawValue
|
|
1040
|
+
});
|
|
1041
|
+
if (!chunk.success) {
|
|
1042
|
+
finishReason = {
|
|
1043
|
+
unified: "error",
|
|
1044
|
+
raw: void 0
|
|
1045
|
+
};
|
|
1046
|
+
controller.enqueue({
|
|
1047
|
+
type: "error",
|
|
1048
|
+
error: chunk.error
|
|
1049
|
+
});
|
|
1050
|
+
return;
|
|
1051
|
+
}
|
|
1052
|
+
const value = chunk.value;
|
|
1053
|
+
if ("error" in value) {
|
|
1054
|
+
finishReason = {
|
|
1055
|
+
unified: "error",
|
|
1056
|
+
raw: void 0
|
|
1057
|
+
};
|
|
1058
|
+
controller.enqueue({
|
|
1059
|
+
type: "error",
|
|
1060
|
+
error: value.error
|
|
1061
|
+
});
|
|
1062
|
+
return;
|
|
1063
|
+
}
|
|
1064
|
+
if (isFirstChunk) {
|
|
1065
|
+
isFirstChunk = false;
|
|
1066
|
+
controller.enqueue({
|
|
1067
|
+
type: "response-metadata",
|
|
1068
|
+
...getResponseMetadata2(value)
|
|
1069
|
+
});
|
|
1070
|
+
controller.enqueue({
|
|
1071
|
+
type: "text-start",
|
|
1072
|
+
id: "0"
|
|
1073
|
+
});
|
|
1074
|
+
}
|
|
1075
|
+
if (value.usage != null) usage = value.usage;
|
|
1076
|
+
const choice = value.choices[0];
|
|
1077
|
+
if ((choice == null ? void 0 : choice.finish_reason) != null) finishReason = {
|
|
1078
|
+
unified: mapOpenAICompatibleFinishReason2(choice.finish_reason),
|
|
1079
|
+
raw: (_a = choice.finish_reason) != null ? _a : void 0
|
|
1080
|
+
};
|
|
1081
|
+
if ((choice == null ? void 0 : choice.text) != null) controller.enqueue({
|
|
1082
|
+
type: "text-delta",
|
|
1083
|
+
id: "0",
|
|
1084
|
+
delta: choice.text
|
|
1085
|
+
});
|
|
1086
|
+
},
|
|
1087
|
+
flush(controller) {
|
|
1088
|
+
if (!isFirstChunk) controller.enqueue({
|
|
1089
|
+
type: "text-end",
|
|
1090
|
+
id: "0"
|
|
1091
|
+
});
|
|
1092
|
+
controller.enqueue({
|
|
1093
|
+
type: "finish",
|
|
1094
|
+
finishReason,
|
|
1095
|
+
usage: convertOpenAICompatibleCompletionUsage(usage)
|
|
1096
|
+
});
|
|
1097
|
+
}
|
|
1098
|
+
})),
|
|
1099
|
+
request: { body },
|
|
1100
|
+
response: { headers: responseHeaders }
|
|
1101
|
+
};
|
|
1102
|
+
}
|
|
1103
|
+
};
|
|
1104
|
+
var usageSchema = require_generate.object({
|
|
1105
|
+
prompt_tokens: require_generate.number(),
|
|
1106
|
+
completion_tokens: require_generate.number(),
|
|
1107
|
+
total_tokens: require_generate.number()
|
|
1108
|
+
});
|
|
1109
|
+
var openaiCompatibleCompletionResponseSchema = require_generate.object({
|
|
1110
|
+
id: require_generate.string().nullish(),
|
|
1111
|
+
created: require_generate.number().nullish(),
|
|
1112
|
+
model: require_generate.string().nullish(),
|
|
1113
|
+
choices: require_generate.array(require_generate.object({
|
|
1114
|
+
text: require_generate.string(),
|
|
1115
|
+
finish_reason: require_generate.string()
|
|
1116
|
+
})),
|
|
1117
|
+
usage: usageSchema.nullish()
|
|
1118
|
+
});
|
|
1119
|
+
var createOpenAICompatibleCompletionChunkSchema = (errorSchema) => require_generate.union([require_generate.object({
|
|
1120
|
+
id: require_generate.string().nullish(),
|
|
1121
|
+
created: require_generate.number().nullish(),
|
|
1122
|
+
model: require_generate.string().nullish(),
|
|
1123
|
+
choices: require_generate.array(require_generate.object({
|
|
1124
|
+
text: require_generate.string(),
|
|
1125
|
+
finish_reason: require_generate.string().nullish(),
|
|
1126
|
+
index: require_generate.number()
|
|
1127
|
+
})),
|
|
1128
|
+
usage: usageSchema.nullish()
|
|
1129
|
+
}), errorSchema]);
|
|
1130
|
+
var openaiCompatibleEmbeddingModelOptions = require_generate.object({
|
|
1131
|
+
/**
|
|
1132
|
+
* The number of dimensions the resulting output embeddings should have.
|
|
1133
|
+
* Only supported in text-embedding-3 and later models.
|
|
1134
|
+
*/
|
|
1135
|
+
dimensions: require_generate.number().optional(),
|
|
1136
|
+
/**
|
|
1137
|
+
* A unique identifier representing your end-user, which can help providers to
|
|
1138
|
+
* monitor and detect abuse.
|
|
1139
|
+
*/
|
|
1140
|
+
user: require_generate.string().optional()
|
|
1141
|
+
});
|
|
1142
|
+
var OpenAICompatibleEmbeddingModel = class {
|
|
1143
|
+
constructor(modelId, config) {
|
|
1144
|
+
this.specificationVersion = "v3";
|
|
1145
|
+
this.modelId = modelId;
|
|
1146
|
+
this.config = config;
|
|
1147
|
+
}
|
|
1148
|
+
get provider() {
|
|
1149
|
+
return this.config.provider;
|
|
1150
|
+
}
|
|
1151
|
+
get maxEmbeddingsPerCall() {
|
|
1152
|
+
var _a;
|
|
1153
|
+
return (_a = this.config.maxEmbeddingsPerCall) != null ? _a : 2048;
|
|
1154
|
+
}
|
|
1155
|
+
get supportsParallelCalls() {
|
|
1156
|
+
var _a;
|
|
1157
|
+
return (_a = this.config.supportsParallelCalls) != null ? _a : true;
|
|
1158
|
+
}
|
|
1159
|
+
get providerOptionsName() {
|
|
1160
|
+
return this.config.provider.split(".")[0].trim();
|
|
1161
|
+
}
|
|
1162
|
+
async doEmbed({ values, headers, abortSignal, providerOptions }) {
|
|
1163
|
+
var _a, _b, _c;
|
|
1164
|
+
const warnings = [];
|
|
1165
|
+
const deprecatedOptions = await require_generate.parseProviderOptions({
|
|
1166
|
+
provider: "openai-compatible",
|
|
1167
|
+
providerOptions,
|
|
1168
|
+
schema: openaiCompatibleEmbeddingModelOptions
|
|
1169
|
+
});
|
|
1170
|
+
if (deprecatedOptions != null) warnings.push({
|
|
1171
|
+
type: "other",
|
|
1172
|
+
message: `The 'openai-compatible' key in providerOptions is deprecated. Use 'openaiCompatible' instead.`
|
|
1173
|
+
});
|
|
1174
|
+
const compatibleOptions = Object.assign(deprecatedOptions != null ? deprecatedOptions : {}, (_a = await require_generate.parseProviderOptions({
|
|
1175
|
+
provider: "openaiCompatible",
|
|
1176
|
+
providerOptions,
|
|
1177
|
+
schema: openaiCompatibleEmbeddingModelOptions
|
|
1178
|
+
})) != null ? _a : {}, (_b = await require_generate.parseProviderOptions({
|
|
1179
|
+
provider: this.providerOptionsName,
|
|
1180
|
+
providerOptions,
|
|
1181
|
+
schema: openaiCompatibleEmbeddingModelOptions
|
|
1182
|
+
})) != null ? _b : {});
|
|
1183
|
+
if (values.length > this.maxEmbeddingsPerCall) throw new require_generate.TooManyEmbeddingValuesForCallError({
|
|
1184
|
+
provider: this.provider,
|
|
1185
|
+
modelId: this.modelId,
|
|
1186
|
+
maxEmbeddingsPerCall: this.maxEmbeddingsPerCall,
|
|
1187
|
+
values
|
|
1188
|
+
});
|
|
1189
|
+
const { responseHeaders, value: response, rawValue } = await require_generate.postJsonToApi({
|
|
1190
|
+
url: this.config.url({
|
|
1191
|
+
path: "/embeddings",
|
|
1192
|
+
modelId: this.modelId
|
|
1193
|
+
}),
|
|
1194
|
+
headers: require_generate.combineHeaders(this.config.headers(), headers),
|
|
1195
|
+
body: {
|
|
1196
|
+
model: this.modelId,
|
|
1197
|
+
input: values,
|
|
1198
|
+
encoding_format: "float",
|
|
1199
|
+
dimensions: compatibleOptions.dimensions,
|
|
1200
|
+
user: compatibleOptions.user
|
|
1201
|
+
},
|
|
1202
|
+
failedResponseHandler: require_generate.createJsonErrorResponseHandler((_c = this.config.errorStructure) != null ? _c : defaultOpenAICompatibleErrorStructure),
|
|
1203
|
+
successfulResponseHandler: require_generate.createJsonResponseHandler(openaiTextEmbeddingResponseSchema),
|
|
1204
|
+
abortSignal,
|
|
1205
|
+
fetch: this.config.fetch
|
|
1206
|
+
});
|
|
1207
|
+
return {
|
|
1208
|
+
warnings,
|
|
1209
|
+
embeddings: response.data.map((item) => item.embedding),
|
|
1210
|
+
usage: response.usage ? { tokens: response.usage.prompt_tokens } : void 0,
|
|
1211
|
+
providerMetadata: response.providerMetadata,
|
|
1212
|
+
response: {
|
|
1213
|
+
headers: responseHeaders,
|
|
1214
|
+
body: rawValue
|
|
1215
|
+
}
|
|
1216
|
+
};
|
|
1217
|
+
}
|
|
1218
|
+
};
|
|
1219
|
+
var openaiTextEmbeddingResponseSchema = require_generate.object({
|
|
1220
|
+
data: require_generate.array(require_generate.object({ embedding: require_generate.array(require_generate.number()) })),
|
|
1221
|
+
usage: require_generate.object({ prompt_tokens: require_generate.number() }).nullish(),
|
|
1222
|
+
providerMetadata: require_generate.record(require_generate.string(), require_generate.record(require_generate.string(), require_generate.any())).optional()
|
|
1223
|
+
});
|
|
1224
|
+
var OpenAICompatibleImageModel = class {
|
|
1225
|
+
constructor(modelId, config) {
|
|
1226
|
+
this.modelId = modelId;
|
|
1227
|
+
this.config = config;
|
|
1228
|
+
this.specificationVersion = "v3";
|
|
1229
|
+
this.maxImagesPerCall = 10;
|
|
1230
|
+
}
|
|
1231
|
+
get provider() {
|
|
1232
|
+
return this.config.provider;
|
|
1233
|
+
}
|
|
1234
|
+
/**
|
|
1235
|
+
* The provider options key used to extract provider-specific options.
|
|
1236
|
+
*/
|
|
1237
|
+
get providerOptionsKey() {
|
|
1238
|
+
return this.config.provider.split(".")[0].trim();
|
|
1239
|
+
}
|
|
1240
|
+
getArgs(providerOptions) {
|
|
1241
|
+
return {
|
|
1242
|
+
...providerOptions[this.providerOptionsKey],
|
|
1243
|
+
...providerOptions[toCamelCase(this.providerOptionsKey)]
|
|
1244
|
+
};
|
|
1245
|
+
}
|
|
1246
|
+
async doGenerate({ prompt, n, size, aspectRatio, seed, providerOptions, headers, abortSignal, files, mask }) {
|
|
1247
|
+
var _a, _b, _c, _d, _e;
|
|
1248
|
+
const warnings = [];
|
|
1249
|
+
if (aspectRatio != null) warnings.push({
|
|
1250
|
+
type: "unsupported",
|
|
1251
|
+
feature: "aspectRatio",
|
|
1252
|
+
details: "This model does not support aspect ratio. Use `size` instead."
|
|
1253
|
+
});
|
|
1254
|
+
if (seed != null) warnings.push({
|
|
1255
|
+
type: "unsupported",
|
|
1256
|
+
feature: "seed"
|
|
1257
|
+
});
|
|
1258
|
+
const currentDate = (_c = (_b = (_a = this.config._internal) == null ? void 0 : _a.currentDate) == null ? void 0 : _b.call(_a)) != null ? _c : /* @__PURE__ */ new Date();
|
|
1259
|
+
const args = this.getArgs(providerOptions);
|
|
1260
|
+
if (files != null && files.length > 0) {
|
|
1261
|
+
const { value: response2, responseHeaders: responseHeaders2 } = await require_generate.postFormDataToApi({
|
|
1262
|
+
url: this.config.url({
|
|
1263
|
+
path: "/images/edits",
|
|
1264
|
+
modelId: this.modelId
|
|
1265
|
+
}),
|
|
1266
|
+
headers: require_generate.combineHeaders(this.config.headers(), headers),
|
|
1267
|
+
formData: require_generate.convertToFormData({
|
|
1268
|
+
model: this.modelId,
|
|
1269
|
+
prompt,
|
|
1270
|
+
image: await Promise.all(files.map((file) => fileToBlob(file))),
|
|
1271
|
+
mask: mask != null ? await fileToBlob(mask) : void 0,
|
|
1272
|
+
n,
|
|
1273
|
+
size,
|
|
1274
|
+
...args
|
|
1275
|
+
}),
|
|
1276
|
+
failedResponseHandler: require_generate.createJsonErrorResponseHandler((_d = this.config.errorStructure) != null ? _d : defaultOpenAICompatibleErrorStructure),
|
|
1277
|
+
successfulResponseHandler: require_generate.createJsonResponseHandler(openaiCompatibleImageResponseSchema),
|
|
1278
|
+
abortSignal,
|
|
1279
|
+
fetch: this.config.fetch
|
|
1280
|
+
});
|
|
1281
|
+
return {
|
|
1282
|
+
images: response2.data.map((item) => item.b64_json),
|
|
1283
|
+
warnings,
|
|
1284
|
+
response: {
|
|
1285
|
+
timestamp: currentDate,
|
|
1286
|
+
modelId: this.modelId,
|
|
1287
|
+
headers: responseHeaders2
|
|
1288
|
+
}
|
|
1289
|
+
};
|
|
1290
|
+
}
|
|
1291
|
+
const { value: response, responseHeaders } = await require_generate.postJsonToApi({
|
|
1292
|
+
url: this.config.url({
|
|
1293
|
+
path: "/images/generations",
|
|
1294
|
+
modelId: this.modelId
|
|
1295
|
+
}),
|
|
1296
|
+
headers: require_generate.combineHeaders(this.config.headers(), headers),
|
|
1297
|
+
body: {
|
|
1298
|
+
model: this.modelId,
|
|
1299
|
+
prompt,
|
|
1300
|
+
n,
|
|
1301
|
+
size,
|
|
1302
|
+
...args,
|
|
1303
|
+
response_format: "b64_json"
|
|
1304
|
+
},
|
|
1305
|
+
failedResponseHandler: require_generate.createJsonErrorResponseHandler((_e = this.config.errorStructure) != null ? _e : defaultOpenAICompatibleErrorStructure),
|
|
1306
|
+
successfulResponseHandler: require_generate.createJsonResponseHandler(openaiCompatibleImageResponseSchema),
|
|
1307
|
+
abortSignal,
|
|
1308
|
+
fetch: this.config.fetch
|
|
1309
|
+
});
|
|
1310
|
+
return {
|
|
1311
|
+
images: response.data.map((item) => item.b64_json),
|
|
1312
|
+
warnings,
|
|
1313
|
+
response: {
|
|
1314
|
+
timestamp: currentDate,
|
|
1315
|
+
modelId: this.modelId,
|
|
1316
|
+
headers: responseHeaders
|
|
1317
|
+
}
|
|
1318
|
+
};
|
|
1319
|
+
}
|
|
1320
|
+
};
|
|
1321
|
+
var openaiCompatibleImageResponseSchema = require_generate.object({ data: require_generate.array(require_generate.object({ b64_json: require_generate.string() })) });
|
|
1322
|
+
async function fileToBlob(file) {
|
|
1323
|
+
if (file.type === "url") return require_generate.downloadBlob(file.url);
|
|
1324
|
+
const data = file.data instanceof Uint8Array ? file.data : require_generate.convertBase64ToUint8Array(file.data);
|
|
1325
|
+
return new Blob([data], { type: file.mediaType });
|
|
1326
|
+
}
|
|
1327
|
+
var VERSION = "2.0.48";
|
|
1328
|
+
function createOpenAICompatible(options) {
|
|
1329
|
+
const baseURL = require_generate.withoutTrailingSlash(options.baseURL);
|
|
1330
|
+
const providerName = options.name;
|
|
1331
|
+
const headers = {
|
|
1332
|
+
...options.apiKey && { Authorization: `Bearer ${options.apiKey}` },
|
|
1333
|
+
...options.headers
|
|
1334
|
+
};
|
|
1335
|
+
const getHeaders = () => require_generate.withUserAgentSuffix(headers, `ai-sdk/openai-compatible/${VERSION}`);
|
|
1336
|
+
const getCommonModelConfig = (modelType) => ({
|
|
1337
|
+
provider: `${providerName}.${modelType}`,
|
|
1338
|
+
url: ({ path }) => {
|
|
1339
|
+
const url = new URL(`${baseURL}${path}`);
|
|
1340
|
+
if (options.queryParams) url.search = new URLSearchParams(options.queryParams).toString();
|
|
1341
|
+
return url.toString();
|
|
1342
|
+
},
|
|
1343
|
+
headers: getHeaders,
|
|
1344
|
+
fetch: options.fetch
|
|
1345
|
+
});
|
|
1346
|
+
const createLanguageModel = (modelId) => createChatModel(modelId);
|
|
1347
|
+
const createChatModel = (modelId) => new OpenAICompatibleChatLanguageModel(modelId, {
|
|
1348
|
+
...getCommonModelConfig("chat"),
|
|
1349
|
+
includeUsage: options.includeUsage,
|
|
1350
|
+
supportsStructuredOutputs: options.supportsStructuredOutputs,
|
|
1351
|
+
supportedUrls: options.supportedUrls,
|
|
1352
|
+
transformRequestBody: options.transformRequestBody,
|
|
1353
|
+
metadataExtractor: options.metadataExtractor,
|
|
1354
|
+
convertUsage: options.convertUsage
|
|
1355
|
+
});
|
|
1356
|
+
const createCompletionModel = (modelId) => new OpenAICompatibleCompletionLanguageModel(modelId, {
|
|
1357
|
+
...getCommonModelConfig("completion"),
|
|
1358
|
+
includeUsage: options.includeUsage
|
|
1359
|
+
});
|
|
1360
|
+
const createEmbeddingModel = (modelId) => new OpenAICompatibleEmbeddingModel(modelId, { ...getCommonModelConfig("embedding") });
|
|
1361
|
+
const createImageModel = (modelId) => new OpenAICompatibleImageModel(modelId, getCommonModelConfig("image"));
|
|
1362
|
+
const provider = (modelId) => createLanguageModel(modelId);
|
|
1363
|
+
provider.specificationVersion = "v3";
|
|
1364
|
+
provider.languageModel = createLanguageModel;
|
|
1365
|
+
provider.chatModel = createChatModel;
|
|
1366
|
+
provider.completionModel = createCompletionModel;
|
|
1367
|
+
provider.embeddingModel = createEmbeddingModel;
|
|
1368
|
+
provider.textEmbeddingModel = createEmbeddingModel;
|
|
1369
|
+
provider.imageModel = createImageModel;
|
|
1370
|
+
return provider;
|
|
1371
|
+
}
|
|
1372
|
+
//#endregion
|
|
1373
|
+
exports.createOpenAICompatible = createOpenAICompatible;
|