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