@ai-sdk/openai-compatible 1.0.0-canary.6 → 1.0.0-canary.8

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/index.js CHANGED
@@ -31,7 +31,7 @@ module.exports = __toCommonJS(src_exports);
31
31
  // src/openai-compatible-chat-language-model.ts
32
32
  var import_provider3 = require("@ai-sdk/provider");
33
33
  var import_provider_utils = require("@ai-sdk/provider-utils");
34
- var import_zod2 = require("zod");
34
+ var import_zod3 = require("zod");
35
35
 
36
36
  // src/convert-to-openai-compatible-chat-messages.ts
37
37
  var import_provider = require("@ai-sdk/provider");
@@ -170,17 +170,27 @@ function mapOpenAICompatibleFinishReason(finishReason) {
170
170
  }
171
171
  }
172
172
 
173
- // src/openai-compatible-error.ts
173
+ // src/openai-compatible-chat-options.ts
174
174
  var import_zod = require("zod");
175
- var openaiCompatibleErrorDataSchema = import_zod.z.object({
176
- error: import_zod.z.object({
177
- message: import_zod.z.string(),
175
+ var openaiCompatibleProviderOptions = import_zod.z.object({
176
+ /**
177
+ * A unique identifier representing your end-user, which can help the provider to
178
+ * monitor and detect abuse.
179
+ */
180
+ user: import_zod.z.string().optional()
181
+ });
182
+
183
+ // src/openai-compatible-error.ts
184
+ var import_zod2 = require("zod");
185
+ var openaiCompatibleErrorDataSchema = import_zod2.z.object({
186
+ error: import_zod2.z.object({
187
+ message: import_zod2.z.string(),
178
188
  // The additional information below is handled loosely to support
179
189
  // OpenAI-compatible providers that have slightly different error
180
190
  // responses:
181
- type: import_zod.z.string().nullish(),
182
- param: import_zod.z.any().nullish(),
183
- code: import_zod.z.union([import_zod.z.string(), import_zod.z.number()]).nullish()
191
+ type: import_zod2.z.string().nullish(),
192
+ param: import_zod2.z.any().nullish(),
193
+ code: import_zod2.z.union([import_zod2.z.string(), import_zod2.z.number()]).nullish()
184
194
  })
185
195
  });
186
196
  var defaultOpenAICompatibleErrorStructure = {
@@ -244,11 +254,10 @@ function prepareTools({
244
254
  // src/openai-compatible-chat-language-model.ts
245
255
  var OpenAICompatibleChatLanguageModel = class {
246
256
  // type inferred via constructor
247
- constructor(modelId, settings, config) {
257
+ constructor(modelId, config) {
248
258
  this.specificationVersion = "v2";
249
259
  var _a, _b;
250
260
  this.modelId = modelId;
251
- this.settings = settings;
252
261
  this.config = config;
253
262
  const errorStructure = (_a = config.errorStructure) != null ? _a : defaultOpenAICompatibleErrorStructure;
254
263
  this.chunkSchema = createOpenAICompatibleChatChunkSchema(
@@ -281,8 +290,20 @@ var OpenAICompatibleChatLanguageModel = class {
281
290
  toolChoice,
282
291
  tools
283
292
  }) {
284
- var _a;
293
+ var _a, _b, _c;
285
294
  const warnings = [];
295
+ const compatibleOptions = Object.assign(
296
+ (_a = (0, import_provider_utils.parseProviderOptions)({
297
+ provider: "openai-compatible",
298
+ providerOptions,
299
+ schema: openaiCompatibleProviderOptions
300
+ })) != null ? _a : {},
301
+ (_b = (0, import_provider_utils.parseProviderOptions)({
302
+ provider: this.providerOptionsName,
303
+ providerOptions,
304
+ schema: openaiCompatibleProviderOptions
305
+ })) != null ? _b : {}
306
+ );
286
307
  if (topK != null) {
287
308
  warnings.push({ type: "unsupported-setting", setting: "topK" });
288
309
  }
@@ -306,7 +327,7 @@ var OpenAICompatibleChatLanguageModel = class {
306
327
  // model id:
307
328
  model: this.modelId,
308
329
  // model specific settings:
309
- user: this.settings.user,
330
+ user: compatibleOptions.user,
310
331
  // standardized settings:
311
332
  max_tokens: maxOutputTokens,
312
333
  temperature,
@@ -317,7 +338,7 @@ var OpenAICompatibleChatLanguageModel = class {
317
338
  type: "json_schema",
318
339
  json_schema: {
319
340
  schema: responseFormat.schema,
320
- name: (_a = responseFormat.name) != null ? _a : "response",
341
+ name: (_c = responseFormat.name) != null ? _c : "response",
321
342
  description: responseFormat.description
322
343
  }
323
344
  } : { type: "json_object" } : void 0,
@@ -334,7 +355,7 @@ var OpenAICompatibleChatLanguageModel = class {
334
355
  };
335
356
  }
336
357
  async doGenerate(options) {
337
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
358
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i;
338
359
  const { args, warnings } = this.getArgs({ ...options });
339
360
  const body = JSON.stringify(args);
340
361
  const {
@@ -355,16 +376,39 @@ var OpenAICompatibleChatLanguageModel = class {
355
376
  abortSignal: options.abortSignal,
356
377
  fetch: this.config.fetch
357
378
  });
358
- const { messages: rawPrompt, ...rawSettings } = args;
359
379
  const choice = responseBody.choices[0];
380
+ const content = [];
381
+ const text = choice.message.content;
382
+ if (text != null && text.length > 0) {
383
+ content.push({ type: "text", text });
384
+ }
385
+ const reasoning = choice.message.reasoning_content;
386
+ if (reasoning != null && reasoning.length > 0) {
387
+ content.push({
388
+ type: "reasoning",
389
+ reasoningType: "text",
390
+ text: reasoning
391
+ });
392
+ }
393
+ if (choice.message.tool_calls != null) {
394
+ for (const toolCall of choice.message.tool_calls) {
395
+ content.push({
396
+ type: "tool-call",
397
+ toolCallType: "function",
398
+ toolCallId: (_a = toolCall.id) != null ? _a : (0, import_provider_utils.generateId)(),
399
+ toolName: toolCall.function.name,
400
+ args: toolCall.function.arguments
401
+ });
402
+ }
403
+ }
360
404
  const providerMetadata = {
361
405
  [this.providerOptionsName]: {},
362
- ...(_b = (_a = this.config.metadataExtractor) == null ? void 0 : _a.extractMetadata) == null ? void 0 : _b.call(_a, {
406
+ ...(_c = (_b = this.config.metadataExtractor) == null ? void 0 : _b.extractMetadata) == null ? void 0 : _c.call(_b, {
363
407
  parsedBody: rawResponse
364
408
  })
365
409
  };
366
- const completionTokenDetails = (_c = responseBody.usage) == null ? void 0 : _c.completion_tokens_details;
367
- const promptTokenDetails = (_d = responseBody.usage) == null ? void 0 : _d.prompt_tokens_details;
410
+ const completionTokenDetails = (_d = responseBody.usage) == null ? void 0 : _d.completion_tokens_details;
411
+ const promptTokenDetails = (_e = responseBody.usage) == null ? void 0 : _e.prompt_tokens_details;
368
412
  if ((completionTokenDetails == null ? void 0 : completionTokenDetails.reasoning_tokens) != null) {
369
413
  providerMetadata[this.providerOptionsName].reasoningTokens = completionTokenDetails == null ? void 0 : completionTokenDetails.reasoning_tokens;
370
414
  }
@@ -378,21 +422,11 @@ var OpenAICompatibleChatLanguageModel = class {
378
422
  providerMetadata[this.providerOptionsName].cachedPromptTokens = promptTokenDetails == null ? void 0 : promptTokenDetails.cached_tokens;
379
423
  }
380
424
  return {
381
- text: (_e = choice.message.content) != null ? _e : void 0,
382
- reasoning: (_f = choice.message.reasoning_content) != null ? _f : void 0,
383
- toolCalls: (_g = choice.message.tool_calls) == null ? void 0 : _g.map((toolCall) => {
384
- var _a2;
385
- return {
386
- toolCallType: "function",
387
- toolCallId: (_a2 = toolCall.id) != null ? _a2 : (0, import_provider_utils.generateId)(),
388
- toolName: toolCall.function.name,
389
- args: toolCall.function.arguments
390
- };
391
- }),
425
+ content,
392
426
  finishReason: mapOpenAICompatibleFinishReason(choice.finish_reason),
393
427
  usage: {
394
- inputTokens: (_i = (_h = responseBody.usage) == null ? void 0 : _h.prompt_tokens) != null ? _i : void 0,
395
- outputTokens: (_k = (_j = responseBody.usage) == null ? void 0 : _j.completion_tokens) != null ? _k : void 0
428
+ inputTokens: (_g = (_f = responseBody.usage) == null ? void 0 : _f.prompt_tokens) != null ? _g : void 0,
429
+ outputTokens: (_i = (_h = responseBody.usage) == null ? void 0 : _h.completion_tokens) != null ? _i : void 0
396
430
  },
397
431
  providerMetadata,
398
432
  request: { body },
@@ -445,6 +479,9 @@ var OpenAICompatibleChatLanguageModel = class {
445
479
  return {
446
480
  stream: response.pipeThrough(
447
481
  new TransformStream({
482
+ start(controller) {
483
+ controller.enqueue({ type: "stream-start", warnings });
484
+ },
448
485
  // TODO we lost type safety on Chunk, most likely due to the error schema. MUST FIX
449
486
  transform(chunk, controller) {
450
487
  var _a2, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
@@ -502,13 +539,14 @@ var OpenAICompatibleChatLanguageModel = class {
502
539
  if (delta.reasoning_content != null) {
503
540
  controller.enqueue({
504
541
  type: "reasoning",
505
- textDelta: delta.reasoning_content
542
+ reasoningType: "text",
543
+ text: delta.reasoning_content
506
544
  });
507
545
  }
508
546
  if (delta.content != null) {
509
547
  controller.enqueue({
510
- type: "text-delta",
511
- textDelta: delta.content
548
+ type: "text",
549
+ text: delta.content
512
550
  });
513
551
  }
514
552
  if (delta.tool_calls != null) {
@@ -624,73 +662,72 @@ var OpenAICompatibleChatLanguageModel = class {
624
662
  })
625
663
  ),
626
664
  request: { body },
627
- response: { headers: responseHeaders },
628
- warnings
665
+ response: { headers: responseHeaders }
629
666
  };
630
667
  }
631
668
  };
632
- var openaiCompatibleTokenUsageSchema = import_zod2.z.object({
633
- prompt_tokens: import_zod2.z.number().nullish(),
634
- completion_tokens: import_zod2.z.number().nullish(),
635
- prompt_tokens_details: import_zod2.z.object({
636
- cached_tokens: import_zod2.z.number().nullish()
669
+ var openaiCompatibleTokenUsageSchema = import_zod3.z.object({
670
+ prompt_tokens: import_zod3.z.number().nullish(),
671
+ completion_tokens: import_zod3.z.number().nullish(),
672
+ prompt_tokens_details: import_zod3.z.object({
673
+ cached_tokens: import_zod3.z.number().nullish()
637
674
  }).nullish(),
638
- completion_tokens_details: import_zod2.z.object({
639
- reasoning_tokens: import_zod2.z.number().nullish(),
640
- accepted_prediction_tokens: import_zod2.z.number().nullish(),
641
- rejected_prediction_tokens: import_zod2.z.number().nullish()
675
+ completion_tokens_details: import_zod3.z.object({
676
+ reasoning_tokens: import_zod3.z.number().nullish(),
677
+ accepted_prediction_tokens: import_zod3.z.number().nullish(),
678
+ rejected_prediction_tokens: import_zod3.z.number().nullish()
642
679
  }).nullish()
643
680
  }).nullish();
644
- var OpenAICompatibleChatResponseSchema = import_zod2.z.object({
645
- id: import_zod2.z.string().nullish(),
646
- created: import_zod2.z.number().nullish(),
647
- model: import_zod2.z.string().nullish(),
648
- choices: import_zod2.z.array(
649
- import_zod2.z.object({
650
- message: import_zod2.z.object({
651
- role: import_zod2.z.literal("assistant").nullish(),
652
- content: import_zod2.z.string().nullish(),
653
- reasoning_content: import_zod2.z.string().nullish(),
654
- tool_calls: import_zod2.z.array(
655
- import_zod2.z.object({
656
- id: import_zod2.z.string().nullish(),
657
- type: import_zod2.z.literal("function"),
658
- function: import_zod2.z.object({
659
- name: import_zod2.z.string(),
660
- arguments: import_zod2.z.string()
681
+ var OpenAICompatibleChatResponseSchema = import_zod3.z.object({
682
+ id: import_zod3.z.string().nullish(),
683
+ created: import_zod3.z.number().nullish(),
684
+ model: import_zod3.z.string().nullish(),
685
+ choices: import_zod3.z.array(
686
+ import_zod3.z.object({
687
+ message: import_zod3.z.object({
688
+ role: import_zod3.z.literal("assistant").nullish(),
689
+ content: import_zod3.z.string().nullish(),
690
+ reasoning_content: import_zod3.z.string().nullish(),
691
+ tool_calls: import_zod3.z.array(
692
+ import_zod3.z.object({
693
+ id: import_zod3.z.string().nullish(),
694
+ type: import_zod3.z.literal("function"),
695
+ function: import_zod3.z.object({
696
+ name: import_zod3.z.string(),
697
+ arguments: import_zod3.z.string()
661
698
  })
662
699
  })
663
700
  ).nullish()
664
701
  }),
665
- finish_reason: import_zod2.z.string().nullish()
702
+ finish_reason: import_zod3.z.string().nullish()
666
703
  })
667
704
  ),
668
705
  usage: openaiCompatibleTokenUsageSchema
669
706
  });
670
- var createOpenAICompatibleChatChunkSchema = (errorSchema) => import_zod2.z.union([
671
- import_zod2.z.object({
672
- id: import_zod2.z.string().nullish(),
673
- created: import_zod2.z.number().nullish(),
674
- model: import_zod2.z.string().nullish(),
675
- choices: import_zod2.z.array(
676
- import_zod2.z.object({
677
- delta: import_zod2.z.object({
678
- role: import_zod2.z.enum(["assistant"]).nullish(),
679
- content: import_zod2.z.string().nullish(),
680
- reasoning_content: import_zod2.z.string().nullish(),
681
- tool_calls: import_zod2.z.array(
682
- import_zod2.z.object({
683
- index: import_zod2.z.number(),
684
- id: import_zod2.z.string().nullish(),
685
- type: import_zod2.z.literal("function").optional(),
686
- function: import_zod2.z.object({
687
- name: import_zod2.z.string().nullish(),
688
- arguments: import_zod2.z.string().nullish()
707
+ var createOpenAICompatibleChatChunkSchema = (errorSchema) => import_zod3.z.union([
708
+ import_zod3.z.object({
709
+ id: import_zod3.z.string().nullish(),
710
+ created: import_zod3.z.number().nullish(),
711
+ model: import_zod3.z.string().nullish(),
712
+ choices: import_zod3.z.array(
713
+ import_zod3.z.object({
714
+ delta: import_zod3.z.object({
715
+ role: import_zod3.z.enum(["assistant"]).nullish(),
716
+ content: import_zod3.z.string().nullish(),
717
+ reasoning_content: import_zod3.z.string().nullish(),
718
+ tool_calls: import_zod3.z.array(
719
+ import_zod3.z.object({
720
+ index: import_zod3.z.number(),
721
+ id: import_zod3.z.string().nullish(),
722
+ type: import_zod3.z.literal("function").nullish(),
723
+ function: import_zod3.z.object({
724
+ name: import_zod3.z.string().nullish(),
725
+ arguments: import_zod3.z.string().nullish()
689
726
  })
690
727
  })
691
728
  ).nullish()
692
729
  }).nullish(),
693
- finish_reason: import_zod2.z.string().nullish()
730
+ finish_reason: import_zod3.z.string().nullish()
694
731
  })
695
732
  ),
696
733
  usage: openaiCompatibleTokenUsageSchema
@@ -700,7 +737,7 @@ var createOpenAICompatibleChatChunkSchema = (errorSchema) => import_zod2.z.union
700
737
 
701
738
  // src/openai-compatible-completion-language-model.ts
702
739
  var import_provider_utils2 = require("@ai-sdk/provider-utils");
703
- var import_zod3 = require("zod");
740
+ var import_zod5 = require("zod");
704
741
 
705
742
  // src/convert-to-openai-compatible-completion-prompt.ts
706
743
  var import_provider4 = require("@ai-sdk/provider");
@@ -781,15 +818,39 @@ ${user}:`]
781
818
  };
782
819
  }
783
820
 
821
+ // src/openai-compatible-completion-options.ts
822
+ var import_zod4 = require("zod");
823
+ var openaiCompatibleCompletionProviderOptions = import_zod4.z.object({
824
+ /**
825
+ * Echo back the prompt in addition to the completion.
826
+ */
827
+ echo: import_zod4.z.boolean().optional(),
828
+ /**
829
+ * Modify the likelihood of specified tokens appearing in the completion.
830
+ *
831
+ * Accepts a JSON object that maps tokens (specified by their token ID in
832
+ * the GPT tokenizer) to an associated bias value from -100 to 100.
833
+ */
834
+ logitBias: import_zod4.z.record(import_zod4.z.number(), import_zod4.z.number()).optional(),
835
+ /**
836
+ * The suffix that comes after a completion of inserted text.
837
+ */
838
+ suffix: import_zod4.z.string().optional(),
839
+ /**
840
+ * A unique identifier representing your end-user, which can help providers to
841
+ * monitor and detect abuse.
842
+ */
843
+ user: import_zod4.z.string().optional()
844
+ });
845
+
784
846
  // src/openai-compatible-completion-language-model.ts
785
847
  var OpenAICompatibleCompletionLanguageModel = class {
786
848
  // type inferred via constructor
787
- constructor(modelId, settings, config) {
849
+ constructor(modelId, config) {
788
850
  this.specificationVersion = "v2";
789
851
  this.defaultObjectGenerationMode = void 0;
790
852
  var _a;
791
853
  this.modelId = modelId;
792
- this.settings = settings;
793
854
  this.config = config;
794
855
  const errorStructure = (_a = config.errorStructure) != null ? _a : defaultOpenAICompatibleErrorStructure;
795
856
  this.chunkSchema = createOpenAICompatibleCompletionChunkSchema(
@@ -819,7 +880,13 @@ var OpenAICompatibleCompletionLanguageModel = class {
819
880
  tools,
820
881
  toolChoice
821
882
  }) {
883
+ var _a;
822
884
  const warnings = [];
885
+ const completionOptions = (_a = (0, import_provider_utils2.parseProviderOptions)({
886
+ provider: this.providerOptionsName,
887
+ providerOptions,
888
+ schema: openaiCompatibleCompletionProviderOptions
889
+ })) != null ? _a : {};
823
890
  if (topK != null) {
824
891
  warnings.push({ type: "unsupported-setting", setting: "topK" });
825
892
  }
@@ -843,10 +910,10 @@ var OpenAICompatibleCompletionLanguageModel = class {
843
910
  // model id:
844
911
  model: this.modelId,
845
912
  // model specific settings:
846
- echo: this.settings.echo,
847
- logit_bias: this.settings.logitBias,
848
- suffix: this.settings.suffix,
849
- user: this.settings.user,
913
+ echo: completionOptions.echo,
914
+ logit_bias: completionOptions.logitBias,
915
+ suffix: completionOptions.suffix,
916
+ user: completionOptions.user,
850
917
  // standardized settings:
851
918
  max_tokens: maxOutputTokens,
852
919
  temperature,
@@ -885,8 +952,12 @@ var OpenAICompatibleCompletionLanguageModel = class {
885
952
  fetch: this.config.fetch
886
953
  });
887
954
  const choice = response.choices[0];
955
+ const content = [];
956
+ if (choice.text != null && choice.text.length > 0) {
957
+ content.push({ type: "text", text: choice.text });
958
+ }
888
959
  return {
889
- text: choice.text,
960
+ content,
890
961
  usage: {
891
962
  inputTokens: (_b = (_a = response.usage) == null ? void 0 : _a.prompt_tokens) != null ? _b : void 0,
892
963
  outputTokens: (_d = (_c = response.usage) == null ? void 0 : _c.completion_tokens) != null ? _d : void 0
@@ -930,6 +1001,9 @@ var OpenAICompatibleCompletionLanguageModel = class {
930
1001
  return {
931
1002
  stream: response.pipeThrough(
932
1003
  new TransformStream({
1004
+ start(controller) {
1005
+ controller.enqueue({ type: "stream-start", warnings });
1006
+ },
933
1007
  transform(chunk, controller) {
934
1008
  var _a, _b;
935
1009
  if (!chunk.success) {
@@ -962,8 +1036,8 @@ var OpenAICompatibleCompletionLanguageModel = class {
962
1036
  }
963
1037
  if ((choice == null ? void 0 : choice.text) != null) {
964
1038
  controller.enqueue({
965
- type: "text-delta",
966
- textDelta: choice.text
1039
+ type: "text",
1040
+ text: choice.text
967
1041
  });
968
1042
  }
969
1043
  },
@@ -977,41 +1051,40 @@ var OpenAICompatibleCompletionLanguageModel = class {
977
1051
  })
978
1052
  ),
979
1053
  request: { body },
980
- response: { headers: responseHeaders },
981
- warnings
1054
+ response: { headers: responseHeaders }
982
1055
  };
983
1056
  }
984
1057
  };
985
- var openaiCompatibleCompletionResponseSchema = import_zod3.z.object({
986
- id: import_zod3.z.string().nullish(),
987
- created: import_zod3.z.number().nullish(),
988
- model: import_zod3.z.string().nullish(),
989
- choices: import_zod3.z.array(
990
- import_zod3.z.object({
991
- text: import_zod3.z.string(),
992
- finish_reason: import_zod3.z.string()
1058
+ var openaiCompatibleCompletionResponseSchema = import_zod5.z.object({
1059
+ id: import_zod5.z.string().nullish(),
1060
+ created: import_zod5.z.number().nullish(),
1061
+ model: import_zod5.z.string().nullish(),
1062
+ choices: import_zod5.z.array(
1063
+ import_zod5.z.object({
1064
+ text: import_zod5.z.string(),
1065
+ finish_reason: import_zod5.z.string()
993
1066
  })
994
1067
  ),
995
- usage: import_zod3.z.object({
996
- prompt_tokens: import_zod3.z.number(),
997
- completion_tokens: import_zod3.z.number()
1068
+ usage: import_zod5.z.object({
1069
+ prompt_tokens: import_zod5.z.number(),
1070
+ completion_tokens: import_zod5.z.number()
998
1071
  }).nullish()
999
1072
  });
1000
- var createOpenAICompatibleCompletionChunkSchema = (errorSchema) => import_zod3.z.union([
1001
- import_zod3.z.object({
1002
- id: import_zod3.z.string().nullish(),
1003
- created: import_zod3.z.number().nullish(),
1004
- model: import_zod3.z.string().nullish(),
1005
- choices: import_zod3.z.array(
1006
- import_zod3.z.object({
1007
- text: import_zod3.z.string(),
1008
- finish_reason: import_zod3.z.string().nullish(),
1009
- index: import_zod3.z.number()
1073
+ var createOpenAICompatibleCompletionChunkSchema = (errorSchema) => import_zod5.z.union([
1074
+ import_zod5.z.object({
1075
+ id: import_zod5.z.string().nullish(),
1076
+ created: import_zod5.z.number().nullish(),
1077
+ model: import_zod5.z.string().nullish(),
1078
+ choices: import_zod5.z.array(
1079
+ import_zod5.z.object({
1080
+ text: import_zod5.z.string(),
1081
+ finish_reason: import_zod5.z.string().nullish(),
1082
+ index: import_zod5.z.number()
1010
1083
  })
1011
1084
  ),
1012
- usage: import_zod3.z.object({
1013
- prompt_tokens: import_zod3.z.number(),
1014
- completion_tokens: import_zod3.z.number()
1085
+ usage: import_zod5.z.object({
1086
+ prompt_tokens: import_zod5.z.number(),
1087
+ completion_tokens: import_zod5.z.number()
1015
1088
  }).nullish()
1016
1089
  }),
1017
1090
  errorSchema
@@ -1020,12 +1093,28 @@ var createOpenAICompatibleCompletionChunkSchema = (errorSchema) => import_zod3.z
1020
1093
  // src/openai-compatible-embedding-model.ts
1021
1094
  var import_provider5 = require("@ai-sdk/provider");
1022
1095
  var import_provider_utils3 = require("@ai-sdk/provider-utils");
1023
- var import_zod4 = require("zod");
1096
+ var import_zod7 = require("zod");
1097
+
1098
+ // src/openai-compatible-embedding-options.ts
1099
+ var import_zod6 = require("zod");
1100
+ var openaiCompatibleEmbeddingProviderOptions = import_zod6.z.object({
1101
+ /**
1102
+ * The number of dimensions the resulting output embeddings should have.
1103
+ * Only supported in text-embedding-3 and later models.
1104
+ */
1105
+ dimensions: import_zod6.z.number().optional(),
1106
+ /**
1107
+ * A unique identifier representing your end-user, which can help providers to
1108
+ * monitor and detect abuse.
1109
+ */
1110
+ user: import_zod6.z.string().optional()
1111
+ });
1112
+
1113
+ // src/openai-compatible-embedding-model.ts
1024
1114
  var OpenAICompatibleEmbeddingModel = class {
1025
- constructor(modelId, settings, config) {
1026
- this.specificationVersion = "v1";
1115
+ constructor(modelId, config) {
1116
+ this.specificationVersion = "v2";
1027
1117
  this.modelId = modelId;
1028
- this.settings = settings;
1029
1118
  this.config = config;
1030
1119
  }
1031
1120
  get provider() {
@@ -1039,12 +1128,28 @@ var OpenAICompatibleEmbeddingModel = class {
1039
1128
  var _a;
1040
1129
  return (_a = this.config.supportsParallelCalls) != null ? _a : true;
1041
1130
  }
1131
+ get providerOptionsName() {
1132
+ return this.config.provider.split(".")[0].trim();
1133
+ }
1042
1134
  async doEmbed({
1043
1135
  values,
1044
1136
  headers,
1045
- abortSignal
1137
+ abortSignal,
1138
+ providerOptions
1046
1139
  }) {
1047
- var _a;
1140
+ var _a, _b, _c;
1141
+ const compatibleOptions = Object.assign(
1142
+ (_a = (0, import_provider_utils3.parseProviderOptions)({
1143
+ provider: "openai-compatible",
1144
+ providerOptions,
1145
+ schema: openaiCompatibleEmbeddingProviderOptions
1146
+ })) != null ? _a : {},
1147
+ (_b = (0, import_provider_utils3.parseProviderOptions)({
1148
+ provider: this.providerOptionsName,
1149
+ providerOptions,
1150
+ schema: openaiCompatibleEmbeddingProviderOptions
1151
+ })) != null ? _b : {}
1152
+ );
1048
1153
  if (values.length > this.maxEmbeddingsPerCall) {
1049
1154
  throw new import_provider5.TooManyEmbeddingValuesForCallError({
1050
1155
  provider: this.provider,
@@ -1053,7 +1158,11 @@ var OpenAICompatibleEmbeddingModel = class {
1053
1158
  values
1054
1159
  });
1055
1160
  }
1056
- const { responseHeaders, value: response } = await (0, import_provider_utils3.postJsonToApi)({
1161
+ const {
1162
+ responseHeaders,
1163
+ value: response,
1164
+ rawValue
1165
+ } = await (0, import_provider_utils3.postJsonToApi)({
1057
1166
  url: this.config.url({
1058
1167
  path: "/embeddings",
1059
1168
  modelId: this.modelId
@@ -1063,11 +1172,11 @@ var OpenAICompatibleEmbeddingModel = class {
1063
1172
  model: this.modelId,
1064
1173
  input: values,
1065
1174
  encoding_format: "float",
1066
- dimensions: this.settings.dimensions,
1067
- user: this.settings.user
1175
+ dimensions: compatibleOptions.dimensions,
1176
+ user: compatibleOptions.user
1068
1177
  },
1069
1178
  failedResponseHandler: (0, import_provider_utils3.createJsonErrorResponseHandler)(
1070
- (_a = this.config.errorStructure) != null ? _a : defaultOpenAICompatibleErrorStructure
1179
+ (_c = this.config.errorStructure) != null ? _c : defaultOpenAICompatibleErrorStructure
1071
1180
  ),
1072
1181
  successfulResponseHandler: (0, import_provider_utils3.createJsonResponseHandler)(
1073
1182
  openaiTextEmbeddingResponseSchema
@@ -1078,18 +1187,18 @@ var OpenAICompatibleEmbeddingModel = class {
1078
1187
  return {
1079
1188
  embeddings: response.data.map((item) => item.embedding),
1080
1189
  usage: response.usage ? { tokens: response.usage.prompt_tokens } : void 0,
1081
- rawResponse: { headers: responseHeaders }
1190
+ response: { headers: responseHeaders, body: rawValue }
1082
1191
  };
1083
1192
  }
1084
1193
  };
1085
- var openaiTextEmbeddingResponseSchema = import_zod4.z.object({
1086
- data: import_zod4.z.array(import_zod4.z.object({ embedding: import_zod4.z.array(import_zod4.z.number()) })),
1087
- usage: import_zod4.z.object({ prompt_tokens: import_zod4.z.number() }).nullish()
1194
+ var openaiTextEmbeddingResponseSchema = import_zod7.z.object({
1195
+ data: import_zod7.z.array(import_zod7.z.object({ embedding: import_zod7.z.array(import_zod7.z.number()) })),
1196
+ usage: import_zod7.z.object({ prompt_tokens: import_zod7.z.number() }).nullish()
1088
1197
  });
1089
1198
 
1090
1199
  // src/openai-compatible-image-model.ts
1091
1200
  var import_provider_utils4 = require("@ai-sdk/provider-utils");
1092
- var import_zod5 = require("zod");
1201
+ var import_zod8 = require("zod");
1093
1202
  var OpenAICompatibleImageModel = class {
1094
1203
  constructor(modelId, settings, config) {
1095
1204
  this.modelId = modelId;
@@ -1162,8 +1271,8 @@ var OpenAICompatibleImageModel = class {
1162
1271
  };
1163
1272
  }
1164
1273
  };
1165
- var openaiCompatibleImageResponseSchema = import_zod5.z.object({
1166
- data: import_zod5.z.array(import_zod5.z.object({ b64_json: import_zod5.z.string() }))
1274
+ var openaiCompatibleImageResponseSchema = import_zod8.z.object({
1275
+ data: import_zod8.z.array(import_zod8.z.object({ b64_json: import_zod8.z.string() }))
1167
1276
  });
1168
1277
 
1169
1278
  // src/openai-compatible-provider.ts
@@ -1187,27 +1296,24 @@ function createOpenAICompatible(options) {
1187
1296
  headers: getHeaders,
1188
1297
  fetch: options.fetch
1189
1298
  });
1190
- const createLanguageModel = (modelId, settings = {}) => createChatModel(modelId, settings);
1191
- const createChatModel = (modelId, settings = {}) => new OpenAICompatibleChatLanguageModel(modelId, settings, {
1299
+ const createLanguageModel = (modelId) => createChatModel(modelId);
1300
+ const createChatModel = (modelId) => new OpenAICompatibleChatLanguageModel(modelId, {
1192
1301
  ...getCommonModelConfig("chat"),
1193
1302
  defaultObjectGenerationMode: "tool"
1194
1303
  });
1195
- const createCompletionModel = (modelId, settings = {}) => new OpenAICompatibleCompletionLanguageModel(
1304
+ const createCompletionModel = (modelId) => new OpenAICompatibleCompletionLanguageModel(
1196
1305
  modelId,
1197
- settings,
1198
1306
  getCommonModelConfig("completion")
1199
1307
  );
1200
- const createEmbeddingModel = (modelId, settings = {}) => new OpenAICompatibleEmbeddingModel(
1201
- modelId,
1202
- settings,
1203
- getCommonModelConfig("embedding")
1204
- );
1308
+ const createEmbeddingModel = (modelId) => new OpenAICompatibleEmbeddingModel(modelId, {
1309
+ ...getCommonModelConfig("embedding")
1310
+ });
1205
1311
  const createImageModel = (modelId, settings = {}) => new OpenAICompatibleImageModel(
1206
1312
  modelId,
1207
1313
  settings,
1208
1314
  getCommonModelConfig("image")
1209
1315
  );
1210
- const provider = (modelId, settings) => createLanguageModel(modelId, settings);
1316
+ const provider = (modelId) => createLanguageModel(modelId);
1211
1317
  provider.languageModel = createLanguageModel;
1212
1318
  provider.chatModel = createChatModel;
1213
1319
  provider.completionModel = createCompletionModel;