@apertis/ai-sdk-provider 0.1.1 → 1.1.0
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/README.md +78 -1
- package/dist/index.cjs +460 -53
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +68 -7
- package/dist/index.d.ts +68 -7
- package/dist/index.js +465 -50
- package/dist/index.js.map +1 -1
- package/package.json +4 -3
package/dist/index.cjs
CHANGED
|
@@ -26,7 +26,7 @@ __export(index_exports, {
|
|
|
26
26
|
module.exports = __toCommonJS(index_exports);
|
|
27
27
|
|
|
28
28
|
// src/apertis-provider.ts
|
|
29
|
-
var
|
|
29
|
+
var import_provider_utils5 = require("@ai-sdk/provider-utils");
|
|
30
30
|
|
|
31
31
|
// src/apertis-chat-language-model.ts
|
|
32
32
|
var import_provider_utils2 = require("@ai-sdk/provider-utils");
|
|
@@ -115,17 +115,18 @@ var openAIChatChunkSchema = import_zod2.z.object({
|
|
|
115
115
|
|
|
116
116
|
// src/utils/map-finish-reason.ts
|
|
117
117
|
function mapApertisFinishReason(finishReason) {
|
|
118
|
+
const raw = finishReason ?? void 0;
|
|
118
119
|
switch (finishReason) {
|
|
119
120
|
case "stop":
|
|
120
|
-
return "stop";
|
|
121
|
+
return { unified: "stop", raw };
|
|
121
122
|
case "length":
|
|
122
|
-
return "length";
|
|
123
|
+
return { unified: "length", raw };
|
|
123
124
|
case "tool_calls":
|
|
124
|
-
return "tool-calls";
|
|
125
|
+
return { unified: "tool-calls", raw };
|
|
125
126
|
case "content_filter":
|
|
126
|
-
return "content-filter";
|
|
127
|
+
return { unified: "content-filter", raw };
|
|
127
128
|
default:
|
|
128
|
-
return "
|
|
129
|
+
return { unified: "other", raw };
|
|
129
130
|
}
|
|
130
131
|
}
|
|
131
132
|
|
|
@@ -144,13 +145,29 @@ function convertToOpenAIMessages(prompt) {
|
|
|
144
145
|
switch (part.type) {
|
|
145
146
|
case "text":
|
|
146
147
|
return { type: "text", text: part.text };
|
|
147
|
-
case "
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
url
|
|
148
|
+
case "file": {
|
|
149
|
+
if (part.mediaType?.startsWith("image/")) {
|
|
150
|
+
let url;
|
|
151
|
+
if (part.data instanceof URL) {
|
|
152
|
+
url = part.data.toString();
|
|
153
|
+
} else if (typeof part.data === "string") {
|
|
154
|
+
if (part.data.startsWith("http://") || part.data.startsWith("https://")) {
|
|
155
|
+
url = part.data;
|
|
156
|
+
} else {
|
|
157
|
+
url = `data:${part.mediaType};base64,${part.data}`;
|
|
158
|
+
}
|
|
159
|
+
} else {
|
|
160
|
+
url = `data:${part.mediaType};base64,${Buffer.from(part.data).toString("base64")}`;
|
|
152
161
|
}
|
|
153
|
-
|
|
162
|
+
return {
|
|
163
|
+
type: "image_url",
|
|
164
|
+
image_url: { url }
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
throw new Error(
|
|
168
|
+
`Unsupported file type: ${part.mediaType}. Only image/* is supported.`
|
|
169
|
+
);
|
|
170
|
+
}
|
|
154
171
|
default:
|
|
155
172
|
throw new Error(
|
|
156
173
|
`Unsupported user content part type: ${part.type}`
|
|
@@ -161,12 +178,10 @@ function convertToOpenAIMessages(prompt) {
|
|
|
161
178
|
break;
|
|
162
179
|
case "assistant": {
|
|
163
180
|
const textContent = message.content.filter((p) => p.type === "text").map((p) => p.text).join("");
|
|
164
|
-
const toolCalls = message.content.filter(
|
|
165
|
-
(p) => p.type === "tool-call"
|
|
166
|
-
).map((tc) => {
|
|
181
|
+
const toolCalls = message.content.filter((p) => p.type === "tool-call").map((tc) => {
|
|
167
182
|
let arguments_str = "{}";
|
|
168
183
|
try {
|
|
169
|
-
arguments_str = JSON.stringify(tc.
|
|
184
|
+
arguments_str = typeof tc.input === "string" ? tc.input : JSON.stringify(tc.input);
|
|
170
185
|
} catch {
|
|
171
186
|
arguments_str = "{}";
|
|
172
187
|
}
|
|
@@ -185,12 +200,17 @@ function convertToOpenAIMessages(prompt) {
|
|
|
185
200
|
}
|
|
186
201
|
case "tool":
|
|
187
202
|
for (const result of message.content) {
|
|
203
|
+
if (result.type !== "tool-result") continue;
|
|
188
204
|
let content = "{}";
|
|
189
|
-
|
|
190
|
-
|
|
205
|
+
const output = result.output;
|
|
206
|
+
if (typeof output === "string") {
|
|
207
|
+
content = output;
|
|
208
|
+
} else if (Array.isArray(output)) {
|
|
209
|
+
const textParts = output.filter((p) => p.type === "text").map((p) => p.text);
|
|
210
|
+
content = textParts.join("");
|
|
191
211
|
} else {
|
|
192
212
|
try {
|
|
193
|
-
content = JSON.stringify(
|
|
213
|
+
content = JSON.stringify(output);
|
|
194
214
|
} catch {
|
|
195
215
|
content = "{}";
|
|
196
216
|
}
|
|
@@ -215,7 +235,7 @@ function convertToOpenAITools(tools) {
|
|
|
215
235
|
function: {
|
|
216
236
|
name: tool.name,
|
|
217
237
|
description: tool.description,
|
|
218
|
-
parameters: tool.
|
|
238
|
+
parameters: tool.inputSchema
|
|
219
239
|
}
|
|
220
240
|
}));
|
|
221
241
|
}
|
|
@@ -229,7 +249,11 @@ function convertToOpenAIToolChoice(toolChoice) {
|
|
|
229
249
|
case "required":
|
|
230
250
|
return "required";
|
|
231
251
|
case "tool":
|
|
232
|
-
|
|
252
|
+
if (!toolChoice.toolName) return void 0;
|
|
253
|
+
return {
|
|
254
|
+
type: "function",
|
|
255
|
+
function: { name: toolChoice.toolName }
|
|
256
|
+
};
|
|
233
257
|
default:
|
|
234
258
|
return void 0;
|
|
235
259
|
}
|
|
@@ -242,15 +266,17 @@ var ApertisChatLanguageModel = class {
|
|
|
242
266
|
this.settings = settings;
|
|
243
267
|
this.config = config;
|
|
244
268
|
}
|
|
245
|
-
specificationVersion = "
|
|
246
|
-
|
|
247
|
-
|
|
269
|
+
specificationVersion = "v3";
|
|
270
|
+
/**
|
|
271
|
+
* Supported URL patterns for different media types.
|
|
272
|
+
* Supports HTTP(S) image URLs for direct URL passing.
|
|
273
|
+
*/
|
|
274
|
+
supportedUrls = {
|
|
275
|
+
"image/*": [/^https?:\/\/.+$/]
|
|
276
|
+
};
|
|
248
277
|
get provider() {
|
|
249
278
|
return this.config.provider;
|
|
250
279
|
}
|
|
251
|
-
get supportsStructuredOutputs() {
|
|
252
|
-
return true;
|
|
253
|
-
}
|
|
254
280
|
async doGenerate(options) {
|
|
255
281
|
const body = this.buildRequestBody(options, false);
|
|
256
282
|
const { value: response } = await (0, import_provider_utils2.postJsonToApi)({
|
|
@@ -265,20 +291,41 @@ var ApertisChatLanguageModel = class {
|
|
|
265
291
|
abortSignal: options.abortSignal
|
|
266
292
|
});
|
|
267
293
|
const choice = response.choices[0];
|
|
294
|
+
const content = [];
|
|
295
|
+
if (choice.message.content) {
|
|
296
|
+
content.push({
|
|
297
|
+
type: "text",
|
|
298
|
+
text: choice.message.content
|
|
299
|
+
});
|
|
300
|
+
}
|
|
301
|
+
if (choice.message.tool_calls) {
|
|
302
|
+
for (const tc of choice.message.tool_calls) {
|
|
303
|
+
content.push({
|
|
304
|
+
type: "tool-call",
|
|
305
|
+
toolCallId: tc.id,
|
|
306
|
+
toolName: tc.function.name,
|
|
307
|
+
input: tc.function.arguments
|
|
308
|
+
});
|
|
309
|
+
}
|
|
310
|
+
}
|
|
268
311
|
return {
|
|
269
|
-
|
|
270
|
-
toolCalls: choice.message.tool_calls?.map((tc) => ({
|
|
271
|
-
toolCallType: "function",
|
|
272
|
-
toolCallId: tc.id,
|
|
273
|
-
toolName: tc.function.name,
|
|
274
|
-
args: tc.function.arguments
|
|
275
|
-
})),
|
|
312
|
+
content,
|
|
276
313
|
finishReason: mapApertisFinishReason(choice.finish_reason),
|
|
277
314
|
usage: {
|
|
278
|
-
|
|
279
|
-
|
|
315
|
+
inputTokens: {
|
|
316
|
+
total: response.usage?.prompt_tokens ?? 0,
|
|
317
|
+
noCache: void 0,
|
|
318
|
+
cacheRead: void 0,
|
|
319
|
+
cacheWrite: void 0
|
|
320
|
+
},
|
|
321
|
+
outputTokens: {
|
|
322
|
+
total: response.usage?.completion_tokens ?? 0,
|
|
323
|
+
text: void 0,
|
|
324
|
+
reasoning: void 0
|
|
325
|
+
}
|
|
280
326
|
},
|
|
281
|
-
|
|
327
|
+
warnings: [],
|
|
328
|
+
request: { body }
|
|
282
329
|
};
|
|
283
330
|
}
|
|
284
331
|
async doStream(options) {
|
|
@@ -295,6 +342,7 @@ var ApertisChatLanguageModel = class {
|
|
|
295
342
|
abortSignal: options.abortSignal
|
|
296
343
|
});
|
|
297
344
|
const toolCallBuffers = /* @__PURE__ */ new Map();
|
|
345
|
+
let textId = null;
|
|
298
346
|
const transformStream = new TransformStream({
|
|
299
347
|
transform(parseResult, controller) {
|
|
300
348
|
if (!parseResult.success) {
|
|
@@ -304,9 +352,17 @@ var ApertisChatLanguageModel = class {
|
|
|
304
352
|
const choice = chunk.choices[0];
|
|
305
353
|
if (!choice) return;
|
|
306
354
|
if (choice.delta.content) {
|
|
355
|
+
if (!textId) {
|
|
356
|
+
textId = (0, import_provider_utils2.generateId)();
|
|
357
|
+
controller.enqueue({
|
|
358
|
+
type: "text-start",
|
|
359
|
+
id: textId
|
|
360
|
+
});
|
|
361
|
+
}
|
|
307
362
|
controller.enqueue({
|
|
308
363
|
type: "text-delta",
|
|
309
|
-
|
|
364
|
+
id: textId,
|
|
365
|
+
delta: choice.delta.content
|
|
310
366
|
});
|
|
311
367
|
}
|
|
312
368
|
if (choice.delta.tool_calls) {
|
|
@@ -323,14 +379,19 @@ var ApertisChatLanguageModel = class {
|
|
|
323
379
|
}
|
|
324
380
|
}
|
|
325
381
|
if (choice.finish_reason) {
|
|
382
|
+
if (textId) {
|
|
383
|
+
controller.enqueue({
|
|
384
|
+
type: "text-end",
|
|
385
|
+
id: textId
|
|
386
|
+
});
|
|
387
|
+
}
|
|
326
388
|
for (const [, buffer] of toolCallBuffers) {
|
|
327
389
|
if (buffer.name) {
|
|
328
390
|
controller.enqueue({
|
|
329
391
|
type: "tool-call",
|
|
330
|
-
toolCallType: "function",
|
|
331
392
|
toolCallId: buffer.id,
|
|
332
393
|
toolName: buffer.name,
|
|
333
|
-
|
|
394
|
+
input: buffer.arguments
|
|
334
395
|
});
|
|
335
396
|
}
|
|
336
397
|
}
|
|
@@ -339,21 +400,35 @@ var ApertisChatLanguageModel = class {
|
|
|
339
400
|
type: "finish",
|
|
340
401
|
finishReason: mapApertisFinishReason(choice.finish_reason),
|
|
341
402
|
usage: {
|
|
342
|
-
|
|
343
|
-
|
|
403
|
+
inputTokens: {
|
|
404
|
+
total: chunk.usage?.prompt_tokens ?? 0,
|
|
405
|
+
noCache: void 0,
|
|
406
|
+
cacheRead: void 0,
|
|
407
|
+
cacheWrite: void 0
|
|
408
|
+
},
|
|
409
|
+
outputTokens: {
|
|
410
|
+
total: chunk.usage?.completion_tokens ?? 0,
|
|
411
|
+
text: void 0,
|
|
412
|
+
reasoning: void 0
|
|
413
|
+
}
|
|
344
414
|
}
|
|
345
415
|
});
|
|
346
416
|
}
|
|
347
417
|
},
|
|
348
418
|
flush(controller) {
|
|
419
|
+
if (textId) {
|
|
420
|
+
controller.enqueue({
|
|
421
|
+
type: "text-end",
|
|
422
|
+
id: textId
|
|
423
|
+
});
|
|
424
|
+
}
|
|
349
425
|
for (const [, buffer] of toolCallBuffers) {
|
|
350
426
|
if (buffer.name) {
|
|
351
427
|
controller.enqueue({
|
|
352
428
|
type: "tool-call",
|
|
353
|
-
toolCallType: "function",
|
|
354
429
|
toolCallId: buffer.id,
|
|
355
430
|
toolName: buffer.name,
|
|
356
|
-
|
|
431
|
+
input: buffer.arguments
|
|
357
432
|
});
|
|
358
433
|
}
|
|
359
434
|
}
|
|
@@ -361,13 +436,12 @@ var ApertisChatLanguageModel = class {
|
|
|
361
436
|
});
|
|
362
437
|
return {
|
|
363
438
|
stream: response.pipeThrough(transformStream),
|
|
364
|
-
|
|
439
|
+
request: { body }
|
|
365
440
|
};
|
|
366
441
|
}
|
|
367
442
|
buildRequestBody(options, stream) {
|
|
368
|
-
const tools =
|
|
369
|
-
const
|
|
370
|
-
const responseFormat = options.mode.type === "object-json" ? { type: "json_object" } : void 0;
|
|
443
|
+
const tools = this.filterFunctionTools(options.tools);
|
|
444
|
+
const responseFormat = options.responseFormat?.type === "json" ? { type: "json_object" } : void 0;
|
|
371
445
|
const body = {
|
|
372
446
|
model: this.modelId,
|
|
373
447
|
messages: convertToOpenAIMessages(options.prompt),
|
|
@@ -376,7 +450,8 @@ var ApertisChatLanguageModel = class {
|
|
|
376
450
|
if (stream) body.stream_options = { include_usage: true };
|
|
377
451
|
if (options.temperature !== void 0)
|
|
378
452
|
body.temperature = options.temperature;
|
|
379
|
-
if (options.
|
|
453
|
+
if (options.maxOutputTokens !== void 0)
|
|
454
|
+
body.max_tokens = options.maxOutputTokens;
|
|
380
455
|
if (options.topP !== void 0) body.top_p = options.topP;
|
|
381
456
|
if (options.frequencyPenalty !== void 0)
|
|
382
457
|
body.frequency_penalty = options.frequencyPenalty;
|
|
@@ -386,7 +461,7 @@ var ApertisChatLanguageModel = class {
|
|
|
386
461
|
if (options.seed !== void 0) body.seed = options.seed;
|
|
387
462
|
const convertedTools = convertToOpenAITools(tools);
|
|
388
463
|
if (convertedTools !== void 0) body.tools = convertedTools;
|
|
389
|
-
const convertedToolChoice = convertToOpenAIToolChoice(toolChoice);
|
|
464
|
+
const convertedToolChoice = convertToOpenAIToolChoice(options.toolChoice);
|
|
390
465
|
if (convertedToolChoice !== void 0)
|
|
391
466
|
body.tool_choice = convertedToolChoice;
|
|
392
467
|
if (responseFormat !== void 0) body.response_format = responseFormat;
|
|
@@ -405,12 +480,325 @@ var ApertisChatLanguageModel = class {
|
|
|
405
480
|
}
|
|
406
481
|
};
|
|
407
482
|
|
|
483
|
+
// src/apertis-completion-language-model.ts
|
|
484
|
+
var import_provider_utils3 = require("@ai-sdk/provider-utils");
|
|
485
|
+
|
|
486
|
+
// src/schemas/completion-response.ts
|
|
487
|
+
var import_zod3 = require("zod");
|
|
488
|
+
var openAICompletionResponseSchema = import_zod3.z.object({
|
|
489
|
+
id: import_zod3.z.string(),
|
|
490
|
+
object: import_zod3.z.literal("text_completion"),
|
|
491
|
+
created: import_zod3.z.number(),
|
|
492
|
+
model: import_zod3.z.string(),
|
|
493
|
+
choices: import_zod3.z.array(
|
|
494
|
+
import_zod3.z.object({
|
|
495
|
+
text: import_zod3.z.string(),
|
|
496
|
+
index: import_zod3.z.number(),
|
|
497
|
+
logprobs: import_zod3.z.object({
|
|
498
|
+
tokens: import_zod3.z.array(import_zod3.z.string()).optional(),
|
|
499
|
+
token_logprobs: import_zod3.z.array(import_zod3.z.number()).optional(),
|
|
500
|
+
top_logprobs: import_zod3.z.array(import_zod3.z.record(import_zod3.z.number())).optional(),
|
|
501
|
+
text_offset: import_zod3.z.array(import_zod3.z.number()).optional()
|
|
502
|
+
}).nullable().optional(),
|
|
503
|
+
finish_reason: import_zod3.z.string().nullable().optional()
|
|
504
|
+
})
|
|
505
|
+
),
|
|
506
|
+
usage: import_zod3.z.object({
|
|
507
|
+
prompt_tokens: import_zod3.z.number(),
|
|
508
|
+
completion_tokens: import_zod3.z.number(),
|
|
509
|
+
total_tokens: import_zod3.z.number()
|
|
510
|
+
}).optional()
|
|
511
|
+
});
|
|
512
|
+
var openAICompletionChunkSchema = import_zod3.z.object({
|
|
513
|
+
id: import_zod3.z.string(),
|
|
514
|
+
object: import_zod3.z.literal("text_completion"),
|
|
515
|
+
created: import_zod3.z.number(),
|
|
516
|
+
model: import_zod3.z.string(),
|
|
517
|
+
choices: import_zod3.z.array(
|
|
518
|
+
import_zod3.z.object({
|
|
519
|
+
text: import_zod3.z.string(),
|
|
520
|
+
index: import_zod3.z.number(),
|
|
521
|
+
logprobs: import_zod3.z.object({
|
|
522
|
+
tokens: import_zod3.z.array(import_zod3.z.string()).optional(),
|
|
523
|
+
token_logprobs: import_zod3.z.array(import_zod3.z.number()).optional(),
|
|
524
|
+
top_logprobs: import_zod3.z.array(import_zod3.z.record(import_zod3.z.number())).optional(),
|
|
525
|
+
text_offset: import_zod3.z.array(import_zod3.z.number()).optional()
|
|
526
|
+
}).nullable().optional(),
|
|
527
|
+
finish_reason: import_zod3.z.string().nullable().optional()
|
|
528
|
+
})
|
|
529
|
+
),
|
|
530
|
+
usage: import_zod3.z.object({
|
|
531
|
+
prompt_tokens: import_zod3.z.number(),
|
|
532
|
+
completion_tokens: import_zod3.z.number(),
|
|
533
|
+
total_tokens: import_zod3.z.number()
|
|
534
|
+
}).optional().nullable()
|
|
535
|
+
});
|
|
536
|
+
|
|
537
|
+
// src/apertis-completion-language-model.ts
|
|
538
|
+
var ApertisCompletionLanguageModel = class {
|
|
539
|
+
constructor(modelId, settings, config) {
|
|
540
|
+
this.modelId = modelId;
|
|
541
|
+
this.settings = settings;
|
|
542
|
+
this.config = config;
|
|
543
|
+
}
|
|
544
|
+
specificationVersion = "v3";
|
|
545
|
+
supportedUrls = {};
|
|
546
|
+
get provider() {
|
|
547
|
+
return this.config.provider;
|
|
548
|
+
}
|
|
549
|
+
async doGenerate(options) {
|
|
550
|
+
const body = this.buildRequestBody(options, false);
|
|
551
|
+
const { value: response } = await (0, import_provider_utils3.postJsonToApi)({
|
|
552
|
+
url: `${this.config.baseURL}/completions`,
|
|
553
|
+
headers: this.config.headers(),
|
|
554
|
+
body,
|
|
555
|
+
failedResponseHandler: apertisFailedResponseHandler,
|
|
556
|
+
successfulResponseHandler: (0, import_provider_utils3.createJsonResponseHandler)(
|
|
557
|
+
openAICompletionResponseSchema
|
|
558
|
+
),
|
|
559
|
+
fetch: this.config.fetch,
|
|
560
|
+
abortSignal: options.abortSignal
|
|
561
|
+
});
|
|
562
|
+
const choice = response.choices[0];
|
|
563
|
+
const content = [];
|
|
564
|
+
if (choice.text) {
|
|
565
|
+
content.push({
|
|
566
|
+
type: "text",
|
|
567
|
+
text: choice.text
|
|
568
|
+
});
|
|
569
|
+
}
|
|
570
|
+
return {
|
|
571
|
+
content,
|
|
572
|
+
finishReason: this.mapFinishReason(choice.finish_reason),
|
|
573
|
+
usage: {
|
|
574
|
+
inputTokens: {
|
|
575
|
+
total: response.usage?.prompt_tokens ?? 0,
|
|
576
|
+
noCache: void 0,
|
|
577
|
+
cacheRead: void 0,
|
|
578
|
+
cacheWrite: void 0
|
|
579
|
+
},
|
|
580
|
+
outputTokens: {
|
|
581
|
+
total: response.usage?.completion_tokens ?? 0,
|
|
582
|
+
text: void 0,
|
|
583
|
+
reasoning: void 0
|
|
584
|
+
}
|
|
585
|
+
},
|
|
586
|
+
warnings: [],
|
|
587
|
+
request: { body }
|
|
588
|
+
};
|
|
589
|
+
}
|
|
590
|
+
async doStream(options) {
|
|
591
|
+
const body = this.buildRequestBody(options, true);
|
|
592
|
+
const { value: response } = await (0, import_provider_utils3.postJsonToApi)({
|
|
593
|
+
url: `${this.config.baseURL}/completions`,
|
|
594
|
+
headers: this.config.headers(),
|
|
595
|
+
body,
|
|
596
|
+
failedResponseHandler: apertisFailedResponseHandler,
|
|
597
|
+
successfulResponseHandler: (0, import_provider_utils3.createEventSourceResponseHandler)(
|
|
598
|
+
openAICompletionChunkSchema
|
|
599
|
+
),
|
|
600
|
+
fetch: this.config.fetch,
|
|
601
|
+
abortSignal: options.abortSignal
|
|
602
|
+
});
|
|
603
|
+
let textId = null;
|
|
604
|
+
const transformStream = new TransformStream({
|
|
605
|
+
transform(parseResult, controller) {
|
|
606
|
+
if (!parseResult.success) {
|
|
607
|
+
return;
|
|
608
|
+
}
|
|
609
|
+
const chunk = parseResult.value;
|
|
610
|
+
const choice = chunk.choices[0];
|
|
611
|
+
if (!choice) return;
|
|
612
|
+
if (choice.text) {
|
|
613
|
+
if (!textId) {
|
|
614
|
+
textId = (0, import_provider_utils3.generateId)();
|
|
615
|
+
controller.enqueue({
|
|
616
|
+
type: "text-start",
|
|
617
|
+
id: textId
|
|
618
|
+
});
|
|
619
|
+
}
|
|
620
|
+
controller.enqueue({
|
|
621
|
+
type: "text-delta",
|
|
622
|
+
id: textId,
|
|
623
|
+
delta: choice.text
|
|
624
|
+
});
|
|
625
|
+
}
|
|
626
|
+
if (choice.finish_reason) {
|
|
627
|
+
if (textId) {
|
|
628
|
+
controller.enqueue({
|
|
629
|
+
type: "text-end",
|
|
630
|
+
id: textId
|
|
631
|
+
});
|
|
632
|
+
}
|
|
633
|
+
controller.enqueue({
|
|
634
|
+
type: "finish",
|
|
635
|
+
finishReason: {
|
|
636
|
+
unified: choice.finish_reason === "stop" ? "stop" : choice.finish_reason === "length" ? "length" : "other",
|
|
637
|
+
raw: choice.finish_reason ?? void 0
|
|
638
|
+
},
|
|
639
|
+
usage: {
|
|
640
|
+
inputTokens: {
|
|
641
|
+
total: chunk.usage?.prompt_tokens ?? 0,
|
|
642
|
+
noCache: void 0,
|
|
643
|
+
cacheRead: void 0,
|
|
644
|
+
cacheWrite: void 0
|
|
645
|
+
},
|
|
646
|
+
outputTokens: {
|
|
647
|
+
total: chunk.usage?.completion_tokens ?? 0,
|
|
648
|
+
text: void 0,
|
|
649
|
+
reasoning: void 0
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
});
|
|
653
|
+
}
|
|
654
|
+
},
|
|
655
|
+
flush(controller) {
|
|
656
|
+
if (textId) {
|
|
657
|
+
controller.enqueue({
|
|
658
|
+
type: "text-end",
|
|
659
|
+
id: textId
|
|
660
|
+
});
|
|
661
|
+
}
|
|
662
|
+
}
|
|
663
|
+
});
|
|
664
|
+
return {
|
|
665
|
+
stream: response.pipeThrough(transformStream),
|
|
666
|
+
request: { body }
|
|
667
|
+
};
|
|
668
|
+
}
|
|
669
|
+
buildRequestBody(options, stream) {
|
|
670
|
+
const prompt = this.convertPromptToText(options.prompt);
|
|
671
|
+
const body = {
|
|
672
|
+
model: this.modelId,
|
|
673
|
+
prompt,
|
|
674
|
+
stream
|
|
675
|
+
};
|
|
676
|
+
if (stream) body.stream_options = { include_usage: true };
|
|
677
|
+
if (options.maxOutputTokens !== void 0)
|
|
678
|
+
body.max_tokens = options.maxOutputTokens;
|
|
679
|
+
if (options.temperature !== void 0)
|
|
680
|
+
body.temperature = options.temperature;
|
|
681
|
+
if (options.topP !== void 0) body.top_p = options.topP;
|
|
682
|
+
if (options.frequencyPenalty !== void 0)
|
|
683
|
+
body.frequency_penalty = options.frequencyPenalty;
|
|
684
|
+
if (options.presencePenalty !== void 0)
|
|
685
|
+
body.presence_penalty = options.presencePenalty;
|
|
686
|
+
if (options.stopSequences !== void 0) body.stop = options.stopSequences;
|
|
687
|
+
if (options.seed !== void 0) body.seed = options.seed;
|
|
688
|
+
if (this.settings.echo !== void 0) body.echo = this.settings.echo;
|
|
689
|
+
if (this.settings.logprobs !== void 0)
|
|
690
|
+
body.logprobs = this.settings.logprobs;
|
|
691
|
+
if (this.settings.suffix !== void 0) body.suffix = this.settings.suffix;
|
|
692
|
+
if (this.settings.user !== void 0) body.user = this.settings.user;
|
|
693
|
+
return body;
|
|
694
|
+
}
|
|
695
|
+
convertPromptToText(prompt) {
|
|
696
|
+
const parts = [];
|
|
697
|
+
for (const message of prompt) {
|
|
698
|
+
if (message.role === "system") {
|
|
699
|
+
parts.push(message.content);
|
|
700
|
+
} else if (message.role === "user") {
|
|
701
|
+
for (const part of message.content) {
|
|
702
|
+
if (part.type === "text") {
|
|
703
|
+
parts.push(part.text);
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
} else if (message.role === "assistant") {
|
|
707
|
+
for (const part of message.content) {
|
|
708
|
+
if (part.type === "text") {
|
|
709
|
+
parts.push(part.text);
|
|
710
|
+
}
|
|
711
|
+
}
|
|
712
|
+
}
|
|
713
|
+
}
|
|
714
|
+
return parts.join("\n\n");
|
|
715
|
+
}
|
|
716
|
+
mapFinishReason(finishReason) {
|
|
717
|
+
const raw = finishReason ?? void 0;
|
|
718
|
+
switch (finishReason) {
|
|
719
|
+
case "stop":
|
|
720
|
+
return { unified: "stop", raw };
|
|
721
|
+
case "length":
|
|
722
|
+
return { unified: "length", raw };
|
|
723
|
+
default:
|
|
724
|
+
return { unified: "other", raw };
|
|
725
|
+
}
|
|
726
|
+
}
|
|
727
|
+
};
|
|
728
|
+
|
|
729
|
+
// src/apertis-embedding-model.ts
|
|
730
|
+
var import_provider_utils4 = require("@ai-sdk/provider-utils");
|
|
731
|
+
|
|
732
|
+
// src/schemas/embedding-response.ts
|
|
733
|
+
var import_zod4 = require("zod");
|
|
734
|
+
var openAIEmbeddingResponseSchema = import_zod4.z.object({
|
|
735
|
+
object: import_zod4.z.literal("list"),
|
|
736
|
+
data: import_zod4.z.array(
|
|
737
|
+
import_zod4.z.object({
|
|
738
|
+
object: import_zod4.z.literal("embedding"),
|
|
739
|
+
embedding: import_zod4.z.array(import_zod4.z.number()),
|
|
740
|
+
index: import_zod4.z.number()
|
|
741
|
+
})
|
|
742
|
+
),
|
|
743
|
+
model: import_zod4.z.string(),
|
|
744
|
+
usage: import_zod4.z.object({
|
|
745
|
+
prompt_tokens: import_zod4.z.number(),
|
|
746
|
+
total_tokens: import_zod4.z.number()
|
|
747
|
+
}).optional()
|
|
748
|
+
});
|
|
749
|
+
|
|
750
|
+
// src/apertis-embedding-model.ts
|
|
751
|
+
var ApertisEmbeddingModel = class {
|
|
752
|
+
constructor(modelId, settings, config) {
|
|
753
|
+
this.modelId = modelId;
|
|
754
|
+
this.settings = settings;
|
|
755
|
+
this.config = config;
|
|
756
|
+
this.maxEmbeddingsPerCall = settings.maxEmbeddingsPerCall ?? 2048;
|
|
757
|
+
this.supportsParallelCalls = settings.supportsParallelCalls ?? true;
|
|
758
|
+
}
|
|
759
|
+
specificationVersion = "v3";
|
|
760
|
+
maxEmbeddingsPerCall;
|
|
761
|
+
supportsParallelCalls;
|
|
762
|
+
get provider() {
|
|
763
|
+
return this.config.provider;
|
|
764
|
+
}
|
|
765
|
+
async doEmbed(options) {
|
|
766
|
+
const body = {
|
|
767
|
+
model: this.modelId,
|
|
768
|
+
input: options.values,
|
|
769
|
+
encoding_format: "float"
|
|
770
|
+
};
|
|
771
|
+
if (this.settings.dimensions !== void 0) {
|
|
772
|
+
body.dimensions = this.settings.dimensions;
|
|
773
|
+
}
|
|
774
|
+
if (this.settings.user !== void 0) {
|
|
775
|
+
body.user = this.settings.user;
|
|
776
|
+
}
|
|
777
|
+
const { value: response } = await (0, import_provider_utils4.postJsonToApi)({
|
|
778
|
+
url: `${this.config.baseURL}/embeddings`,
|
|
779
|
+
headers: this.config.headers(),
|
|
780
|
+
body,
|
|
781
|
+
failedResponseHandler: apertisFailedResponseHandler,
|
|
782
|
+
successfulResponseHandler: (0, import_provider_utils4.createJsonResponseHandler)(
|
|
783
|
+
openAIEmbeddingResponseSchema
|
|
784
|
+
),
|
|
785
|
+
fetch: this.config.fetch,
|
|
786
|
+
abortSignal: options.abortSignal
|
|
787
|
+
});
|
|
788
|
+
return {
|
|
789
|
+
embeddings: response.data.map((item) => item.embedding),
|
|
790
|
+
usage: response.usage ? { tokens: response.usage.prompt_tokens } : void 0,
|
|
791
|
+
warnings: []
|
|
792
|
+
};
|
|
793
|
+
}
|
|
794
|
+
};
|
|
795
|
+
|
|
408
796
|
// src/apertis-provider.ts
|
|
409
797
|
function createApertis(options = {}) {
|
|
410
|
-
const baseURL = (0,
|
|
798
|
+
const baseURL = (0, import_provider_utils5.withoutTrailingSlash)(options.baseURL) ?? "https://api.apertis.ai/v1";
|
|
411
799
|
const getHeaders = () => ({
|
|
412
800
|
...options.headers,
|
|
413
|
-
Authorization: `Bearer ${(0,
|
|
801
|
+
Authorization: `Bearer ${(0, import_provider_utils5.loadApiKey)({
|
|
414
802
|
apiKey: options.apiKey,
|
|
415
803
|
environmentVariableName: "APERTIS_API_KEY",
|
|
416
804
|
description: "Apertis API key"
|
|
@@ -423,11 +811,30 @@ function createApertis(options = {}) {
|
|
|
423
811
|
headers: getHeaders,
|
|
424
812
|
fetch: options.fetch
|
|
425
813
|
});
|
|
814
|
+
const createCompletionModel = (modelId, settings = {}) => new ApertisCompletionLanguageModel(modelId, settings, {
|
|
815
|
+
provider: "apertis.completion",
|
|
816
|
+
baseURL,
|
|
817
|
+
headers: getHeaders,
|
|
818
|
+
fetch: options.fetch
|
|
819
|
+
});
|
|
820
|
+
const createEmbeddingModel = (modelId, settings = {}) => new ApertisEmbeddingModel(modelId, settings, {
|
|
821
|
+
provider: "apertis.embedding",
|
|
822
|
+
baseURL,
|
|
823
|
+
headers: getHeaders,
|
|
824
|
+
fetch: options.fetch
|
|
825
|
+
});
|
|
426
826
|
const provider = Object.assign(
|
|
427
827
|
(modelId, settings) => createChatModel(modelId, settings),
|
|
428
828
|
{
|
|
829
|
+
specificationVersion: "v3",
|
|
429
830
|
chat: createChatModel,
|
|
430
|
-
languageModel: createChatModel
|
|
831
|
+
languageModel: (modelId) => createChatModel(modelId),
|
|
832
|
+
completion: createCompletionModel,
|
|
833
|
+
embeddingModel: (modelId) => createEmbeddingModel(modelId),
|
|
834
|
+
textEmbeddingModel: createEmbeddingModel,
|
|
835
|
+
imageModel: () => {
|
|
836
|
+
throw new Error("Image models are not supported by Apertis");
|
|
837
|
+
}
|
|
431
838
|
}
|
|
432
839
|
);
|
|
433
840
|
return provider;
|