@ai-sdk/openai-compatible 1.0.0-canary.1 → 1.0.0-canary.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/index.mjs CHANGED
@@ -17,10 +17,9 @@ import { z as z2 } from "zod";
17
17
  import {
18
18
  UnsupportedFunctionalityError
19
19
  } from "@ai-sdk/provider";
20
- import { convertUint8ArrayToBase64 } from "@ai-sdk/provider-utils";
21
20
  function getOpenAIMetadata(message) {
22
21
  var _a, _b;
23
- return (_b = (_a = message == null ? void 0 : message.providerMetadata) == null ? void 0 : _a.openaiCompatible) != null ? _b : {};
22
+ return (_b = (_a = message == null ? void 0 : message.providerOptions) == null ? void 0 : _a.openaiCompatible) != null ? _b : {};
24
23
  }
25
24
  function convertToOpenAICompatibleChatMessages(prompt) {
26
25
  const messages = [];
@@ -43,25 +42,26 @@ function convertToOpenAICompatibleChatMessages(prompt) {
43
42
  messages.push({
44
43
  role: "user",
45
44
  content: content.map((part) => {
46
- var _a;
47
45
  const partMetadata = getOpenAIMetadata(part);
48
46
  switch (part.type) {
49
47
  case "text": {
50
48
  return { type: "text", text: part.text, ...partMetadata };
51
49
  }
52
- case "image": {
53
- return {
54
- type: "image_url",
55
- image_url: {
56
- url: part.image instanceof URL ? part.image.toString() : `data:${(_a = part.mimeType) != null ? _a : "image/jpeg"};base64,${convertUint8ArrayToBase64(part.image)}`
57
- },
58
- ...partMetadata
59
- };
60
- }
61
50
  case "file": {
62
- throw new UnsupportedFunctionalityError({
63
- functionality: "File content parts in user messages"
64
- });
51
+ if (part.mediaType.startsWith("image/")) {
52
+ const mediaType = part.mediaType === "image/*" ? "image/jpeg" : part.mediaType;
53
+ return {
54
+ type: "image_url",
55
+ image_url: {
56
+ url: part.data instanceof URL ? part.data.toString() : `data:${mediaType};base64,${part.data}`
57
+ },
58
+ ...partMetadata
59
+ };
60
+ } else {
61
+ throw new UnsupportedFunctionalityError({
62
+ functionality: `file part media type ${part.mediaType}`
63
+ });
64
+ }
65
65
  }
66
66
  }
67
67
  }),
@@ -175,16 +175,14 @@ import {
175
175
  UnsupportedFunctionalityError as UnsupportedFunctionalityError2
176
176
  } from "@ai-sdk/provider";
177
177
  function prepareTools({
178
- mode,
179
- structuredOutputs
178
+ tools,
179
+ toolChoice
180
180
  }) {
181
- var _a;
182
- const tools = ((_a = mode.tools) == null ? void 0 : _a.length) ? mode.tools : void 0;
181
+ tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
183
182
  const toolWarnings = [];
184
183
  if (tools == null) {
185
- return { tools: void 0, tool_choice: void 0, toolWarnings };
184
+ return { tools: void 0, toolChoice: void 0, toolWarnings };
186
185
  }
187
- const toolChoice = mode.toolChoice;
188
186
  const openaiCompatTools = [];
189
187
  for (const tool of tools) {
190
188
  if (tool.type === "provider-defined") {
@@ -201,29 +199,27 @@ function prepareTools({
201
199
  }
202
200
  }
203
201
  if (toolChoice == null) {
204
- return { tools: openaiCompatTools, tool_choice: void 0, toolWarnings };
202
+ return { tools: openaiCompatTools, toolChoice: void 0, toolWarnings };
205
203
  }
206
204
  const type = toolChoice.type;
207
205
  switch (type) {
208
206
  case "auto":
209
207
  case "none":
210
208
  case "required":
211
- return { tools: openaiCompatTools, tool_choice: type, toolWarnings };
209
+ return { tools: openaiCompatTools, toolChoice: type, toolWarnings };
212
210
  case "tool":
213
211
  return {
214
212
  tools: openaiCompatTools,
215
- tool_choice: {
213
+ toolChoice: {
216
214
  type: "function",
217
- function: {
218
- name: toolChoice.toolName
219
- }
215
+ function: { name: toolChoice.toolName }
220
216
  },
221
217
  toolWarnings
222
218
  };
223
219
  default: {
224
220
  const _exhaustiveCheck = type;
225
221
  throw new UnsupportedFunctionalityError2({
226
- functionality: `Unsupported tool choice type: ${_exhaustiveCheck}`
222
+ functionality: `tool choice type: ${_exhaustiveCheck}`
227
223
  });
228
224
  }
229
225
  }
@@ -255,7 +251,6 @@ var OpenAICompatibleChatLanguageModel = class {
255
251
  return this.config.provider.split(".")[0].trim();
256
252
  }
257
253
  getArgs({
258
- mode,
259
254
  prompt,
260
255
  maxTokens,
261
256
  temperature,
@@ -263,19 +258,17 @@ var OpenAICompatibleChatLanguageModel = class {
263
258
  topK,
264
259
  frequencyPenalty,
265
260
  presencePenalty,
266
- providerMetadata,
261
+ providerOptions,
267
262
  stopSequences,
268
263
  responseFormat,
269
- seed
264
+ seed,
265
+ toolChoice,
266
+ tools
270
267
  }) {
271
- var _a, _b;
272
- const type = mode.type;
268
+ var _a;
273
269
  const warnings = [];
274
270
  if (topK != null) {
275
- warnings.push({
276
- type: "unsupported-setting",
277
- setting: "topK"
278
- });
271
+ warnings.push({ type: "unsupported-setting", setting: "topK" });
279
272
  }
280
273
  if ((responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null && !this.supportsStructuredOutputs) {
281
274
  warnings.push({
@@ -284,85 +277,45 @@ var OpenAICompatibleChatLanguageModel = class {
284
277
  details: "JSON response format schema is only supported with structuredOutputs"
285
278
  });
286
279
  }
287
- const baseArgs = {
288
- // model id:
289
- model: this.modelId,
290
- // model specific settings:
291
- user: this.settings.user,
292
- // standardized settings:
293
- max_tokens: maxTokens,
294
- temperature,
295
- top_p: topP,
296
- frequency_penalty: frequencyPenalty,
297
- presence_penalty: presencePenalty,
298
- response_format: (responseFormat == null ? void 0 : responseFormat.type) === "json" ? this.supportsStructuredOutputs === true && responseFormat.schema != null ? {
299
- type: "json_schema",
300
- json_schema: {
301
- schema: responseFormat.schema,
302
- name: (_a = responseFormat.name) != null ? _a : "response",
303
- description: responseFormat.description
304
- }
305
- } : { type: "json_object" } : void 0,
306
- stop: stopSequences,
307
- seed,
308
- ...providerMetadata == null ? void 0 : providerMetadata[this.providerOptionsName],
309
- // messages:
310
- messages: convertToOpenAICompatibleChatMessages(prompt)
280
+ const {
281
+ tools: openaiTools,
282
+ toolChoice: openaiToolChoice,
283
+ toolWarnings
284
+ } = prepareTools({
285
+ tools,
286
+ toolChoice
287
+ });
288
+ return {
289
+ args: {
290
+ // model id:
291
+ model: this.modelId,
292
+ // model specific settings:
293
+ user: this.settings.user,
294
+ // standardized settings:
295
+ max_tokens: maxTokens,
296
+ temperature,
297
+ top_p: topP,
298
+ frequency_penalty: frequencyPenalty,
299
+ presence_penalty: presencePenalty,
300
+ response_format: (responseFormat == null ? void 0 : responseFormat.type) === "json" ? this.supportsStructuredOutputs === true && responseFormat.schema != null ? {
301
+ type: "json_schema",
302
+ json_schema: {
303
+ schema: responseFormat.schema,
304
+ name: (_a = responseFormat.name) != null ? _a : "response",
305
+ description: responseFormat.description
306
+ }
307
+ } : { type: "json_object" } : void 0,
308
+ stop: stopSequences,
309
+ seed,
310
+ ...providerOptions == null ? void 0 : providerOptions[this.providerOptionsName],
311
+ // messages:
312
+ messages: convertToOpenAICompatibleChatMessages(prompt),
313
+ // tools:
314
+ tools: openaiTools,
315
+ tool_choice: openaiToolChoice
316
+ },
317
+ warnings: [...warnings, ...toolWarnings]
311
318
  };
312
- switch (type) {
313
- case "regular": {
314
- const { tools, tool_choice, toolWarnings } = prepareTools({
315
- mode,
316
- structuredOutputs: this.supportsStructuredOutputs
317
- });
318
- return {
319
- args: { ...baseArgs, tools, tool_choice },
320
- warnings: [...warnings, ...toolWarnings]
321
- };
322
- }
323
- case "object-json": {
324
- return {
325
- args: {
326
- ...baseArgs,
327
- response_format: this.supportsStructuredOutputs === true && mode.schema != null ? {
328
- type: "json_schema",
329
- json_schema: {
330
- schema: mode.schema,
331
- name: (_b = mode.name) != null ? _b : "response",
332
- description: mode.description
333
- }
334
- } : { type: "json_object" }
335
- },
336
- warnings
337
- };
338
- }
339
- case "object-tool": {
340
- return {
341
- args: {
342
- ...baseArgs,
343
- tool_choice: {
344
- type: "function",
345
- function: { name: mode.tool.name }
346
- },
347
- tools: [
348
- {
349
- type: "function",
350
- function: {
351
- name: mode.tool.name,
352
- description: mode.tool.description,
353
- parameters: mode.tool.parameters
354
- }
355
- }
356
- ]
357
- },
358
- warnings
359
- };
360
- }
361
- default: {
362
- const _exhaustiveCheck = type;
363
- throw new Error(`Unsupported type: ${_exhaustiveCheck}`);
364
- }
365
- }
366
319
  }
367
320
  async doGenerate(options) {
368
321
  var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
@@ -783,9 +736,6 @@ var createOpenAICompatibleChatChunkSchema = (errorSchema) => z2.union([
783
736
  ]);
784
737
 
785
738
  // src/openai-compatible-completion-language-model.ts
786
- import {
787
- UnsupportedFunctionalityError as UnsupportedFunctionalityError4
788
- } from "@ai-sdk/provider";
789
739
  import {
790
740
  combineHeaders as combineHeaders2,
791
741
  createEventSourceResponseHandler as createEventSourceResponseHandler2,
@@ -830,13 +780,8 @@ function convertToOpenAICompatibleCompletionPrompt({
830
780
  case "text": {
831
781
  return part.text;
832
782
  }
833
- case "image": {
834
- throw new UnsupportedFunctionalityError3({
835
- functionality: "images"
836
- });
837
- }
838
783
  }
839
- }).join("");
784
+ }).filter(Boolean).join("");
840
785
  text += `${user}:
841
786
  ${userMessage}
842
787
 
@@ -905,7 +850,6 @@ var OpenAICompatibleCompletionLanguageModel = class {
905
850
  return this.config.provider.split(".")[0].trim();
906
851
  }
907
852
  getArgs({
908
- mode,
909
853
  inputFormat,
910
854
  prompt,
911
855
  maxTokens,
@@ -917,16 +861,19 @@ var OpenAICompatibleCompletionLanguageModel = class {
917
861
  stopSequences: userStopSequences,
918
862
  responseFormat,
919
863
  seed,
920
- providerMetadata
864
+ providerOptions,
865
+ tools,
866
+ toolChoice
921
867
  }) {
922
- var _a;
923
- const type = mode.type;
924
868
  const warnings = [];
925
869
  if (topK != null) {
926
- warnings.push({
927
- type: "unsupported-setting",
928
- setting: "topK"
929
- });
870
+ warnings.push({ type: "unsupported-setting", setting: "topK" });
871
+ }
872
+ if (tools == null ? void 0 : tools.length) {
873
+ warnings.push({ type: "unsupported-setting", setting: "tools" });
874
+ }
875
+ if (toolChoice != null) {
876
+ warnings.push({ type: "unsupported-setting", setting: "toolChoice" });
930
877
  }
931
878
  if (responseFormat != null && responseFormat.type !== "text") {
932
879
  warnings.push({
@@ -937,56 +884,30 @@ var OpenAICompatibleCompletionLanguageModel = class {
937
884
  }
938
885
  const { prompt: completionPrompt, stopSequences } = convertToOpenAICompatibleCompletionPrompt({ prompt, inputFormat });
939
886
  const stop = [...stopSequences != null ? stopSequences : [], ...userStopSequences != null ? userStopSequences : []];
940
- const baseArgs = {
941
- // model id:
942
- model: this.modelId,
943
- // model specific settings:
944
- echo: this.settings.echo,
945
- logit_bias: this.settings.logitBias,
946
- suffix: this.settings.suffix,
947
- user: this.settings.user,
948
- // standardized settings:
949
- max_tokens: maxTokens,
950
- temperature,
951
- top_p: topP,
952
- frequency_penalty: frequencyPenalty,
953
- presence_penalty: presencePenalty,
954
- seed,
955
- ...providerMetadata == null ? void 0 : providerMetadata[this.providerOptionsName],
956
- // prompt:
957
- prompt: completionPrompt,
958
- // stop sequences:
959
- stop: stop.length > 0 ? stop : void 0
887
+ return {
888
+ args: {
889
+ // model id:
890
+ model: this.modelId,
891
+ // model specific settings:
892
+ echo: this.settings.echo,
893
+ logit_bias: this.settings.logitBias,
894
+ suffix: this.settings.suffix,
895
+ user: this.settings.user,
896
+ // standardized settings:
897
+ max_tokens: maxTokens,
898
+ temperature,
899
+ top_p: topP,
900
+ frequency_penalty: frequencyPenalty,
901
+ presence_penalty: presencePenalty,
902
+ seed,
903
+ ...providerOptions == null ? void 0 : providerOptions[this.providerOptionsName],
904
+ // prompt:
905
+ prompt: completionPrompt,
906
+ // stop sequences:
907
+ stop: stop.length > 0 ? stop : void 0
908
+ },
909
+ warnings
960
910
  };
961
- switch (type) {
962
- case "regular": {
963
- if ((_a = mode.tools) == null ? void 0 : _a.length) {
964
- throw new UnsupportedFunctionalityError4({
965
- functionality: "tools"
966
- });
967
- }
968
- if (mode.toolChoice) {
969
- throw new UnsupportedFunctionalityError4({
970
- functionality: "toolChoice"
971
- });
972
- }
973
- return { args: baseArgs, warnings };
974
- }
975
- case "object-json": {
976
- throw new UnsupportedFunctionalityError4({
977
- functionality: "object-json mode"
978
- });
979
- }
980
- case "object-tool": {
981
- throw new UnsupportedFunctionalityError4({
982
- functionality: "object-tool mode"
983
- });
984
- }
985
- default: {
986
- const _exhaustiveCheck = type;
987
- throw new Error(`Unsupported type: ${_exhaustiveCheck}`);
988
- }
989
- }
990
911
  }
991
912
  async doGenerate(options) {
992
913
  var _a, _b, _c, _d;