@ai-sdk/openai-compatible 0.2.13 → 1.0.0-alpha.1

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
@@ -30,15 +30,14 @@ module.exports = __toCommonJS(src_exports);
30
30
 
31
31
  // src/openai-compatible-chat-language-model.ts
32
32
  var import_provider3 = require("@ai-sdk/provider");
33
- var import_provider_utils2 = require("@ai-sdk/provider-utils");
34
- var import_zod2 = require("zod");
33
+ var import_provider_utils = require("@ai-sdk/provider-utils");
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");
38
- var import_provider_utils = require("@ai-sdk/provider-utils");
39
38
  function getOpenAIMetadata(message) {
40
39
  var _a, _b;
41
- return (_b = (_a = message == null ? void 0 : message.providerMetadata) == null ? void 0 : _a.openaiCompatible) != null ? _b : {};
40
+ return (_b = (_a = message == null ? void 0 : message.providerOptions) == null ? void 0 : _a.openaiCompatible) != null ? _b : {};
42
41
  }
43
42
  function convertToOpenAICompatibleChatMessages(prompt) {
44
43
  const messages = [];
@@ -61,25 +60,26 @@ function convertToOpenAICompatibleChatMessages(prompt) {
61
60
  messages.push({
62
61
  role: "user",
63
62
  content: content.map((part) => {
64
- var _a;
65
63
  const partMetadata = getOpenAIMetadata(part);
66
64
  switch (part.type) {
67
65
  case "text": {
68
66
  return { type: "text", text: part.text, ...partMetadata };
69
67
  }
70
- case "image": {
71
- return {
72
- type: "image_url",
73
- image_url: {
74
- url: part.image instanceof URL ? part.image.toString() : `data:${(_a = part.mimeType) != null ? _a : "image/jpeg"};base64,${(0, import_provider_utils.convertUint8ArrayToBase64)(part.image)}`
75
- },
76
- ...partMetadata
77
- };
78
- }
79
68
  case "file": {
80
- throw new import_provider.UnsupportedFunctionalityError({
81
- functionality: "File content parts in user messages"
82
- });
69
+ if (part.mediaType.startsWith("image/")) {
70
+ const mediaType = part.mediaType === "image/*" ? "image/jpeg" : part.mediaType;
71
+ return {
72
+ type: "image_url",
73
+ image_url: {
74
+ url: part.data instanceof URL ? part.data.toString() : `data:${mediaType};base64,${part.data}`
75
+ },
76
+ ...partMetadata
77
+ };
78
+ } else {
79
+ throw new import_provider.UnsupportedFunctionalityError({
80
+ functionality: `file part media type ${part.mediaType}`
81
+ });
82
+ }
83
83
  }
84
84
  }
85
85
  }),
@@ -170,17 +170,31 @@ 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
+ * Reasoning effort for reasoning models. Defaults to `medium`.
183
+ */
184
+ reasoningEffort: import_zod.z.enum(["low", "medium", "high"]).optional()
185
+ });
186
+
187
+ // src/openai-compatible-error.ts
188
+ var import_zod2 = require("zod");
189
+ var openaiCompatibleErrorDataSchema = import_zod2.z.object({
190
+ error: import_zod2.z.object({
191
+ message: import_zod2.z.string(),
178
192
  // The additional information below is handled loosely to support
179
193
  // OpenAI-compatible providers that have slightly different error
180
194
  // 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()
195
+ type: import_zod2.z.string().nullish(),
196
+ param: import_zod2.z.any().nullish(),
197
+ code: import_zod2.z.union([import_zod2.z.string(), import_zod2.z.number()]).nullish()
184
198
  })
185
199
  });
186
200
  var defaultOpenAICompatibleErrorStructure = {
@@ -191,16 +205,14 @@ var defaultOpenAICompatibleErrorStructure = {
191
205
  // src/openai-compatible-prepare-tools.ts
192
206
  var import_provider2 = require("@ai-sdk/provider");
193
207
  function prepareTools({
194
- mode,
195
- structuredOutputs
208
+ tools,
209
+ toolChoice
196
210
  }) {
197
- var _a;
198
- const tools = ((_a = mode.tools) == null ? void 0 : _a.length) ? mode.tools : void 0;
211
+ tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
199
212
  const toolWarnings = [];
200
213
  if (tools == null) {
201
- return { tools: void 0, tool_choice: void 0, toolWarnings };
214
+ return { tools: void 0, toolChoice: void 0, toolWarnings };
202
215
  }
203
- const toolChoice = mode.toolChoice;
204
216
  const openaiCompatTools = [];
205
217
  for (const tool of tools) {
206
218
  if (tool.type === "provider-defined") {
@@ -217,29 +229,27 @@ function prepareTools({
217
229
  }
218
230
  }
219
231
  if (toolChoice == null) {
220
- return { tools: openaiCompatTools, tool_choice: void 0, toolWarnings };
232
+ return { tools: openaiCompatTools, toolChoice: void 0, toolWarnings };
221
233
  }
222
234
  const type = toolChoice.type;
223
235
  switch (type) {
224
236
  case "auto":
225
237
  case "none":
226
238
  case "required":
227
- return { tools: openaiCompatTools, tool_choice: type, toolWarnings };
239
+ return { tools: openaiCompatTools, toolChoice: type, toolWarnings };
228
240
  case "tool":
229
241
  return {
230
242
  tools: openaiCompatTools,
231
- tool_choice: {
243
+ toolChoice: {
232
244
  type: "function",
233
- function: {
234
- name: toolChoice.toolName
235
- }
245
+ function: { name: toolChoice.toolName }
236
246
  },
237
247
  toolWarnings
238
248
  };
239
249
  default: {
240
250
  const _exhaustiveCheck = type;
241
251
  throw new import_provider2.UnsupportedFunctionalityError({
242
- functionality: `Unsupported tool choice type: ${_exhaustiveCheck}`
252
+ functionality: `tool choice type: ${_exhaustiveCheck}`
243
253
  });
244
254
  }
245
255
  }
@@ -248,50 +258,59 @@ function prepareTools({
248
258
  // src/openai-compatible-chat-language-model.ts
249
259
  var OpenAICompatibleChatLanguageModel = class {
250
260
  // type inferred via constructor
251
- constructor(modelId, settings, config) {
252
- this.specificationVersion = "v1";
261
+ constructor(modelId, config) {
262
+ this.specificationVersion = "v2";
253
263
  var _a, _b;
254
264
  this.modelId = modelId;
255
- this.settings = settings;
256
265
  this.config = config;
257
266
  const errorStructure = (_a = config.errorStructure) != null ? _a : defaultOpenAICompatibleErrorStructure;
258
267
  this.chunkSchema = createOpenAICompatibleChatChunkSchema(
259
268
  errorStructure.errorSchema
260
269
  );
261
- this.failedResponseHandler = (0, import_provider_utils2.createJsonErrorResponseHandler)(errorStructure);
270
+ this.failedResponseHandler = (0, import_provider_utils.createJsonErrorResponseHandler)(errorStructure);
262
271
  this.supportsStructuredOutputs = (_b = config.supportsStructuredOutputs) != null ? _b : false;
263
272
  }
264
- get defaultObjectGenerationMode() {
265
- return this.config.defaultObjectGenerationMode;
266
- }
267
273
  get provider() {
268
274
  return this.config.provider;
269
275
  }
270
276
  get providerOptionsName() {
271
277
  return this.config.provider.split(".")[0].trim();
272
278
  }
273
- getArgs({
274
- mode,
279
+ get supportedUrls() {
280
+ var _a, _b, _c;
281
+ return (_c = (_b = (_a = this.config).supportedUrls) == null ? void 0 : _b.call(_a)) != null ? _c : {};
282
+ }
283
+ async getArgs({
275
284
  prompt,
276
- maxTokens,
285
+ maxOutputTokens,
277
286
  temperature,
278
287
  topP,
279
288
  topK,
280
289
  frequencyPenalty,
281
290
  presencePenalty,
282
- providerMetadata,
291
+ providerOptions,
283
292
  stopSequences,
284
293
  responseFormat,
285
- seed
294
+ seed,
295
+ toolChoice,
296
+ tools
286
297
  }) {
287
- var _a, _b, _c, _d, _e;
288
- const type = mode.type;
298
+ var _a, _b, _c;
289
299
  const warnings = [];
300
+ const compatibleOptions = Object.assign(
301
+ (_a = await (0, import_provider_utils.parseProviderOptions)({
302
+ provider: "openai-compatible",
303
+ providerOptions,
304
+ schema: openaiCompatibleProviderOptions
305
+ })) != null ? _a : {},
306
+ (_b = await (0, import_provider_utils.parseProviderOptions)({
307
+ provider: this.providerOptionsName,
308
+ providerOptions,
309
+ schema: openaiCompatibleProviderOptions
310
+ })) != null ? _b : {}
311
+ );
290
312
  if (topK != null) {
291
- warnings.push({
292
- type: "unsupported-setting",
293
- setting: "topK"
294
- });
313
+ warnings.push({ type: "unsupported-setting", setting: "topK" });
295
314
  }
296
315
  if ((responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null && !this.supportsStructuredOutputs) {
297
316
  warnings.push({
@@ -300,212 +319,129 @@ var OpenAICompatibleChatLanguageModel = class {
300
319
  details: "JSON response format schema is only supported with structuredOutputs"
301
320
  });
302
321
  }
303
- const baseArgs = {
304
- // model id:
305
- model: this.modelId,
306
- // model specific settings:
307
- user: this.settings.user,
308
- // standardized settings:
309
- max_tokens: maxTokens,
310
- temperature,
311
- top_p: topP,
312
- frequency_penalty: frequencyPenalty,
313
- presence_penalty: presencePenalty,
314
- response_format: (responseFormat == null ? void 0 : responseFormat.type) === "json" ? this.supportsStructuredOutputs === true && responseFormat.schema != null ? {
315
- type: "json_schema",
316
- json_schema: {
317
- schema: responseFormat.schema,
318
- name: (_a = responseFormat.name) != null ? _a : "response",
319
- description: responseFormat.description
320
- }
321
- } : { type: "json_object" } : void 0,
322
- stop: stopSequences,
323
- seed,
324
- ...providerMetadata == null ? void 0 : providerMetadata[this.providerOptionsName],
325
- reasoning_effort: (_d = (_b = providerMetadata == null ? void 0 : providerMetadata[this.providerOptionsName]) == null ? void 0 : _b.reasoningEffort) != null ? _d : (_c = providerMetadata == null ? void 0 : providerMetadata["openai-compatible"]) == null ? void 0 : _c.reasoningEffort,
326
- // messages:
327
- messages: convertToOpenAICompatibleChatMessages(prompt)
322
+ const {
323
+ tools: openaiTools,
324
+ toolChoice: openaiToolChoice,
325
+ toolWarnings
326
+ } = prepareTools({
327
+ tools,
328
+ toolChoice
329
+ });
330
+ return {
331
+ args: {
332
+ // model id:
333
+ model: this.modelId,
334
+ // model specific settings:
335
+ user: compatibleOptions.user,
336
+ // standardized settings:
337
+ max_tokens: maxOutputTokens,
338
+ temperature,
339
+ top_p: topP,
340
+ frequency_penalty: frequencyPenalty,
341
+ presence_penalty: presencePenalty,
342
+ response_format: (responseFormat == null ? void 0 : responseFormat.type) === "json" ? this.supportsStructuredOutputs === true && responseFormat.schema != null ? {
343
+ type: "json_schema",
344
+ json_schema: {
345
+ schema: responseFormat.schema,
346
+ name: (_c = responseFormat.name) != null ? _c : "response",
347
+ description: responseFormat.description
348
+ }
349
+ } : { type: "json_object" } : void 0,
350
+ stop: stopSequences,
351
+ seed,
352
+ ...providerOptions == null ? void 0 : providerOptions[this.providerOptionsName],
353
+ reasoning_effort: compatibleOptions.reasoningEffort,
354
+ // messages:
355
+ messages: convertToOpenAICompatibleChatMessages(prompt),
356
+ // tools:
357
+ tools: openaiTools,
358
+ tool_choice: openaiToolChoice
359
+ },
360
+ warnings: [...warnings, ...toolWarnings]
328
361
  };
329
- switch (type) {
330
- case "regular": {
331
- const { tools, tool_choice, toolWarnings } = prepareTools({
332
- mode,
333
- structuredOutputs: this.supportsStructuredOutputs
334
- });
335
- return {
336
- args: { ...baseArgs, tools, tool_choice },
337
- warnings: [...warnings, ...toolWarnings]
338
- };
339
- }
340
- case "object-json": {
341
- return {
342
- args: {
343
- ...baseArgs,
344
- response_format: this.supportsStructuredOutputs === true && mode.schema != null ? {
345
- type: "json_schema",
346
- json_schema: {
347
- schema: mode.schema,
348
- name: (_e = mode.name) != null ? _e : "response",
349
- description: mode.description
350
- }
351
- } : { type: "json_object" }
352
- },
353
- warnings
354
- };
355
- }
356
- case "object-tool": {
357
- return {
358
- args: {
359
- ...baseArgs,
360
- tool_choice: {
361
- type: "function",
362
- function: { name: mode.tool.name }
363
- },
364
- tools: [
365
- {
366
- type: "function",
367
- function: {
368
- name: mode.tool.name,
369
- description: mode.tool.description,
370
- parameters: mode.tool.parameters
371
- }
372
- }
373
- ]
374
- },
375
- warnings
376
- };
377
- }
378
- default: {
379
- const _exhaustiveCheck = type;
380
- throw new Error(`Unsupported type: ${_exhaustiveCheck}`);
381
- }
382
- }
383
362
  }
384
363
  async doGenerate(options) {
385
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
386
- const { args, warnings } = this.getArgs({ ...options });
364
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p;
365
+ const { args, warnings } = await this.getArgs({ ...options });
387
366
  const body = JSON.stringify(args);
388
367
  const {
389
368
  responseHeaders,
390
369
  value: responseBody,
391
370
  rawValue: rawResponse
392
- } = await (0, import_provider_utils2.postJsonToApi)({
371
+ } = await (0, import_provider_utils.postJsonToApi)({
393
372
  url: this.config.url({
394
373
  path: "/chat/completions",
395
374
  modelId: this.modelId
396
375
  }),
397
- headers: (0, import_provider_utils2.combineHeaders)(this.config.headers(), options.headers),
376
+ headers: (0, import_provider_utils.combineHeaders)(this.config.headers(), options.headers),
398
377
  body: args,
399
378
  failedResponseHandler: this.failedResponseHandler,
400
- successfulResponseHandler: (0, import_provider_utils2.createJsonResponseHandler)(
379
+ successfulResponseHandler: (0, import_provider_utils.createJsonResponseHandler)(
401
380
  OpenAICompatibleChatResponseSchema
402
381
  ),
403
382
  abortSignal: options.abortSignal,
404
383
  fetch: this.config.fetch
405
384
  });
406
- const { messages: rawPrompt, ...rawSettings } = args;
407
385
  const choice = responseBody.choices[0];
386
+ const content = [];
387
+ const text = choice.message.content;
388
+ if (text != null && text.length > 0) {
389
+ content.push({ type: "text", text });
390
+ }
391
+ const reasoning = choice.message.reasoning_content;
392
+ if (reasoning != null && reasoning.length > 0) {
393
+ content.push({
394
+ type: "reasoning",
395
+ text: reasoning
396
+ });
397
+ }
398
+ if (choice.message.tool_calls != null) {
399
+ for (const toolCall of choice.message.tool_calls) {
400
+ content.push({
401
+ type: "tool-call",
402
+ toolCallType: "function",
403
+ toolCallId: (_a = toolCall.id) != null ? _a : (0, import_provider_utils.generateId)(),
404
+ toolName: toolCall.function.name,
405
+ args: toolCall.function.arguments
406
+ });
407
+ }
408
+ }
408
409
  const providerMetadata = {
409
410
  [this.providerOptionsName]: {},
410
- ...(_b = (_a = this.config.metadataExtractor) == null ? void 0 : _a.extractMetadata) == null ? void 0 : _b.call(_a, {
411
+ ...await ((_c = (_b = this.config.metadataExtractor) == null ? void 0 : _b.extractMetadata) == null ? void 0 : _c.call(_b, {
411
412
  parsedBody: rawResponse
412
- })
413
+ }))
413
414
  };
414
- const completionTokenDetails = (_c = responseBody.usage) == null ? void 0 : _c.completion_tokens_details;
415
- const promptTokenDetails = (_d = responseBody.usage) == null ? void 0 : _d.prompt_tokens_details;
416
- if ((completionTokenDetails == null ? void 0 : completionTokenDetails.reasoning_tokens) != null) {
417
- providerMetadata[this.providerOptionsName].reasoningTokens = completionTokenDetails == null ? void 0 : completionTokenDetails.reasoning_tokens;
418
- }
415
+ const completionTokenDetails = (_d = responseBody.usage) == null ? void 0 : _d.completion_tokens_details;
419
416
  if ((completionTokenDetails == null ? void 0 : completionTokenDetails.accepted_prediction_tokens) != null) {
420
417
  providerMetadata[this.providerOptionsName].acceptedPredictionTokens = completionTokenDetails == null ? void 0 : completionTokenDetails.accepted_prediction_tokens;
421
418
  }
422
419
  if ((completionTokenDetails == null ? void 0 : completionTokenDetails.rejected_prediction_tokens) != null) {
423
420
  providerMetadata[this.providerOptionsName].rejectedPredictionTokens = completionTokenDetails == null ? void 0 : completionTokenDetails.rejected_prediction_tokens;
424
421
  }
425
- if ((promptTokenDetails == null ? void 0 : promptTokenDetails.cached_tokens) != null) {
426
- providerMetadata[this.providerOptionsName].cachedPromptTokens = promptTokenDetails == null ? void 0 : promptTokenDetails.cached_tokens;
427
- }
428
422
  return {
429
- text: (_e = choice.message.content) != null ? _e : void 0,
430
- reasoning: (_f = choice.message.reasoning_content) != null ? _f : void 0,
431
- toolCalls: (_g = choice.message.tool_calls) == null ? void 0 : _g.map((toolCall) => {
432
- var _a2;
433
- return {
434
- toolCallType: "function",
435
- toolCallId: (_a2 = toolCall.id) != null ? _a2 : (0, import_provider_utils2.generateId)(),
436
- toolName: toolCall.function.name,
437
- args: toolCall.function.arguments
438
- };
439
- }),
423
+ content,
440
424
  finishReason: mapOpenAICompatibleFinishReason(choice.finish_reason),
441
425
  usage: {
442
- promptTokens: (_i = (_h = responseBody.usage) == null ? void 0 : _h.prompt_tokens) != null ? _i : NaN,
443
- completionTokens: (_k = (_j = responseBody.usage) == null ? void 0 : _j.completion_tokens) != null ? _k : NaN
426
+ inputTokens: (_f = (_e = responseBody.usage) == null ? void 0 : _e.prompt_tokens) != null ? _f : void 0,
427
+ outputTokens: (_h = (_g = responseBody.usage) == null ? void 0 : _g.completion_tokens) != null ? _h : void 0,
428
+ totalTokens: (_j = (_i = responseBody.usage) == null ? void 0 : _i.total_tokens) != null ? _j : void 0,
429
+ reasoningTokens: (_m = (_l = (_k = responseBody.usage) == null ? void 0 : _k.completion_tokens_details) == null ? void 0 : _l.reasoning_tokens) != null ? _m : void 0,
430
+ cachedInputTokens: (_p = (_o = (_n = responseBody.usage) == null ? void 0 : _n.prompt_tokens_details) == null ? void 0 : _o.cached_tokens) != null ? _p : void 0
444
431
  },
445
432
  providerMetadata,
446
- rawCall: { rawPrompt, rawSettings },
447
- rawResponse: { headers: responseHeaders, body: rawResponse },
448
- response: getResponseMetadata(responseBody),
449
- warnings,
450
- request: { body }
433
+ request: { body },
434
+ response: {
435
+ ...getResponseMetadata(responseBody),
436
+ headers: responseHeaders,
437
+ body: rawResponse
438
+ },
439
+ warnings
451
440
  };
452
441
  }
453
442
  async doStream(options) {
454
443
  var _a;
455
- if (this.settings.simulateStreaming) {
456
- const result = await this.doGenerate(options);
457
- const simulatedStream = new ReadableStream({
458
- start(controller) {
459
- controller.enqueue({ type: "response-metadata", ...result.response });
460
- if (result.reasoning) {
461
- if (Array.isArray(result.reasoning)) {
462
- for (const part of result.reasoning) {
463
- if (part.type === "text") {
464
- controller.enqueue({
465
- type: "reasoning",
466
- textDelta: part.text
467
- });
468
- }
469
- }
470
- } else {
471
- controller.enqueue({
472
- type: "reasoning",
473
- textDelta: result.reasoning
474
- });
475
- }
476
- }
477
- if (result.text) {
478
- controller.enqueue({
479
- type: "text-delta",
480
- textDelta: result.text
481
- });
482
- }
483
- if (result.toolCalls) {
484
- for (const toolCall of result.toolCalls) {
485
- controller.enqueue({
486
- type: "tool-call",
487
- ...toolCall
488
- });
489
- }
490
- }
491
- controller.enqueue({
492
- type: "finish",
493
- finishReason: result.finishReason,
494
- usage: result.usage,
495
- logprobs: result.logprobs,
496
- providerMetadata: result.providerMetadata
497
- });
498
- controller.close();
499
- }
500
- });
501
- return {
502
- stream: simulatedStream,
503
- rawCall: result.rawCall,
504
- rawResponse: result.rawResponse,
505
- warnings: result.warnings
506
- };
507
- }
508
- const { args, warnings } = this.getArgs({ ...options });
444
+ const { args, warnings } = await this.getArgs({ ...options });
509
445
  const body = {
510
446
  ...args,
511
447
  stream: true,
@@ -513,24 +449,23 @@ var OpenAICompatibleChatLanguageModel = class {
513
449
  stream_options: this.config.includeUsage ? { include_usage: true } : void 0
514
450
  };
515
451
  const metadataExtractor = (_a = this.config.metadataExtractor) == null ? void 0 : _a.createStreamExtractor();
516
- const { responseHeaders, value: response } = await (0, import_provider_utils2.postJsonToApi)({
452
+ const { responseHeaders, value: response } = await (0, import_provider_utils.postJsonToApi)({
517
453
  url: this.config.url({
518
454
  path: "/chat/completions",
519
455
  modelId: this.modelId
520
456
  }),
521
- headers: (0, import_provider_utils2.combineHeaders)(this.config.headers(), options.headers),
457
+ headers: (0, import_provider_utils.combineHeaders)(this.config.headers(), options.headers),
522
458
  body,
523
459
  failedResponseHandler: this.failedResponseHandler,
524
- successfulResponseHandler: (0, import_provider_utils2.createEventSourceResponseHandler)(
460
+ successfulResponseHandler: (0, import_provider_utils.createEventSourceResponseHandler)(
525
461
  this.chunkSchema
526
462
  ),
527
463
  abortSignal: options.abortSignal,
528
464
  fetch: this.config.fetch
529
465
  });
530
- const { messages: rawPrompt, ...rawSettings } = args;
531
466
  const toolCalls = [];
532
467
  let finishReason = "unknown";
533
- let usage = {
468
+ const usage = {
534
469
  completionTokens: void 0,
535
470
  completionTokensDetails: {
536
471
  reasoningTokens: void 0,
@@ -540,13 +475,17 @@ var OpenAICompatibleChatLanguageModel = class {
540
475
  promptTokens: void 0,
541
476
  promptTokensDetails: {
542
477
  cachedTokens: void 0
543
- }
478
+ },
479
+ totalTokens: void 0
544
480
  };
545
481
  let isFirstChunk = true;
546
- let providerOptionsName = this.providerOptionsName;
482
+ const providerOptionsName = this.providerOptionsName;
547
483
  return {
548
484
  stream: response.pipeThrough(
549
485
  new TransformStream({
486
+ start(controller) {
487
+ controller.enqueue({ type: "stream-start", warnings });
488
+ },
550
489
  // TODO we lost type safety on Chunk, most likely due to the error schema. MUST FIX
551
490
  transform(chunk, controller) {
552
491
  var _a2, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
@@ -573,11 +512,13 @@ var OpenAICompatibleChatLanguageModel = class {
573
512
  const {
574
513
  prompt_tokens,
575
514
  completion_tokens,
515
+ total_tokens,
576
516
  prompt_tokens_details,
577
517
  completion_tokens_details
578
518
  } = value.usage;
579
519
  usage.promptTokens = prompt_tokens != null ? prompt_tokens : void 0;
580
520
  usage.completionTokens = completion_tokens != null ? completion_tokens : void 0;
521
+ usage.totalTokens = total_tokens != null ? total_tokens : void 0;
581
522
  if ((completion_tokens_details == null ? void 0 : completion_tokens_details.reasoning_tokens) != null) {
582
523
  usage.completionTokensDetails.reasoningTokens = completion_tokens_details == null ? void 0 : completion_tokens_details.reasoning_tokens;
583
524
  }
@@ -604,13 +545,13 @@ var OpenAICompatibleChatLanguageModel = class {
604
545
  if (delta.reasoning_content != null) {
605
546
  controller.enqueue({
606
547
  type: "reasoning",
607
- textDelta: delta.reasoning_content
548
+ text: delta.reasoning_content
608
549
  });
609
550
  }
610
551
  if (delta.content != null) {
611
552
  controller.enqueue({
612
- type: "text-delta",
613
- textDelta: delta.content
553
+ type: "text",
554
+ text: delta.content
614
555
  });
615
556
  }
616
557
  if (delta.tool_calls != null) {
@@ -655,11 +596,11 @@ var OpenAICompatibleChatLanguageModel = class {
655
596
  argsTextDelta: toolCall2.function.arguments
656
597
  });
657
598
  }
658
- if ((0, import_provider_utils2.isParsableJson)(toolCall2.function.arguments)) {
599
+ if ((0, import_provider_utils.isParsableJson)(toolCall2.function.arguments)) {
659
600
  controller.enqueue({
660
601
  type: "tool-call",
661
602
  toolCallType: "function",
662
- toolCallId: (_e = toolCall2.id) != null ? _e : (0, import_provider_utils2.generateId)(),
603
+ toolCallId: (_e = toolCall2.id) != null ? _e : (0, import_provider_utils.generateId)(),
663
604
  toolName: toolCall2.function.name,
664
605
  args: toolCall2.function.arguments
665
606
  });
@@ -682,11 +623,11 @@ var OpenAICompatibleChatLanguageModel = class {
682
623
  toolName: toolCall.function.name,
683
624
  argsTextDelta: (_i = toolCallDelta.function.arguments) != null ? _i : ""
684
625
  });
685
- if (((_j = toolCall.function) == null ? void 0 : _j.name) != null && ((_k = toolCall.function) == null ? void 0 : _k.arguments) != null && (0, import_provider_utils2.isParsableJson)(toolCall.function.arguments)) {
626
+ if (((_j = toolCall.function) == null ? void 0 : _j.name) != null && ((_k = toolCall.function) == null ? void 0 : _k.arguments) != null && (0, import_provider_utils.isParsableJson)(toolCall.function.arguments)) {
686
627
  controller.enqueue({
687
628
  type: "tool-call",
688
629
  toolCallType: "function",
689
- toolCallId: (_l = toolCall.id) != null ? _l : (0, import_provider_utils2.generateId)(),
630
+ toolCallId: (_l = toolCall.id) != null ? _l : (0, import_provider_utils.generateId)(),
690
631
  toolName: toolCall.function.name,
691
632
  args: toolCall.function.arguments
692
633
  });
@@ -696,104 +637,100 @@ var OpenAICompatibleChatLanguageModel = class {
696
637
  }
697
638
  },
698
639
  flush(controller) {
699
- var _a2, _b;
640
+ var _a2, _b, _c, _d, _e;
700
641
  const providerMetadata = {
701
642
  [providerOptionsName]: {},
702
643
  ...metadataExtractor == null ? void 0 : metadataExtractor.buildMetadata()
703
644
  };
704
- if (usage.completionTokensDetails.reasoningTokens != null) {
705
- providerMetadata[providerOptionsName].reasoningTokens = usage.completionTokensDetails.reasoningTokens;
706
- }
707
645
  if (usage.completionTokensDetails.acceptedPredictionTokens != null) {
708
646
  providerMetadata[providerOptionsName].acceptedPredictionTokens = usage.completionTokensDetails.acceptedPredictionTokens;
709
647
  }
710
648
  if (usage.completionTokensDetails.rejectedPredictionTokens != null) {
711
649
  providerMetadata[providerOptionsName].rejectedPredictionTokens = usage.completionTokensDetails.rejectedPredictionTokens;
712
650
  }
713
- if (usage.promptTokensDetails.cachedTokens != null) {
714
- providerMetadata[providerOptionsName].cachedPromptTokens = usage.promptTokensDetails.cachedTokens;
715
- }
716
651
  controller.enqueue({
717
652
  type: "finish",
718
653
  finishReason,
719
654
  usage: {
720
- promptTokens: (_a2 = usage.promptTokens) != null ? _a2 : NaN,
721
- completionTokens: (_b = usage.completionTokens) != null ? _b : NaN
655
+ inputTokens: (_a2 = usage.promptTokens) != null ? _a2 : void 0,
656
+ outputTokens: (_b = usage.completionTokens) != null ? _b : void 0,
657
+ totalTokens: (_c = usage.totalTokens) != null ? _c : void 0,
658
+ reasoningTokens: (_d = usage.completionTokensDetails.reasoningTokens) != null ? _d : void 0,
659
+ cachedInputTokens: (_e = usage.promptTokensDetails.cachedTokens) != null ? _e : void 0
722
660
  },
723
661
  providerMetadata
724
662
  });
725
663
  }
726
664
  })
727
665
  ),
728
- rawCall: { rawPrompt, rawSettings },
729
- rawResponse: { headers: responseHeaders },
730
- warnings,
731
- request: { body: JSON.stringify(body) }
666
+ request: { body },
667
+ response: { headers: responseHeaders }
732
668
  };
733
669
  }
734
670
  };
735
- var openaiCompatibleTokenUsageSchema = import_zod2.z.object({
736
- prompt_tokens: import_zod2.z.number().nullish(),
737
- completion_tokens: import_zod2.z.number().nullish(),
738
- prompt_tokens_details: import_zod2.z.object({
739
- cached_tokens: import_zod2.z.number().nullish()
671
+ var openaiCompatibleTokenUsageSchema = import_zod3.z.object({
672
+ prompt_tokens: import_zod3.z.number().nullish(),
673
+ completion_tokens: import_zod3.z.number().nullish(),
674
+ total_tokens: import_zod3.z.number().nullish(),
675
+ prompt_tokens_details: import_zod3.z.object({
676
+ cached_tokens: import_zod3.z.number().nullish()
740
677
  }).nullish(),
741
- completion_tokens_details: import_zod2.z.object({
742
- reasoning_tokens: import_zod2.z.number().nullish(),
743
- accepted_prediction_tokens: import_zod2.z.number().nullish(),
744
- rejected_prediction_tokens: import_zod2.z.number().nullish()
678
+ completion_tokens_details: import_zod3.z.object({
679
+ reasoning_tokens: import_zod3.z.number().nullish(),
680
+ accepted_prediction_tokens: import_zod3.z.number().nullish(),
681
+ rejected_prediction_tokens: import_zod3.z.number().nullish()
745
682
  }).nullish()
746
683
  }).nullish();
747
- var OpenAICompatibleChatResponseSchema = import_zod2.z.object({
748
- id: import_zod2.z.string().nullish(),
749
- created: import_zod2.z.number().nullish(),
750
- model: import_zod2.z.string().nullish(),
751
- choices: import_zod2.z.array(
752
- import_zod2.z.object({
753
- message: import_zod2.z.object({
754
- role: import_zod2.z.literal("assistant").nullish(),
755
- content: import_zod2.z.string().nullish(),
756
- reasoning_content: import_zod2.z.string().nullish(),
757
- tool_calls: import_zod2.z.array(
758
- import_zod2.z.object({
759
- id: import_zod2.z.string().nullish(),
760
- type: import_zod2.z.literal("function"),
761
- function: import_zod2.z.object({
762
- name: import_zod2.z.string(),
763
- arguments: import_zod2.z.string()
684
+ var OpenAICompatibleChatResponseSchema = import_zod3.z.object({
685
+ id: import_zod3.z.string().nullish(),
686
+ created: import_zod3.z.number().nullish(),
687
+ model: import_zod3.z.string().nullish(),
688
+ choices: import_zod3.z.array(
689
+ import_zod3.z.object({
690
+ message: import_zod3.z.object({
691
+ role: import_zod3.z.literal("assistant").nullish(),
692
+ content: import_zod3.z.string().nullish(),
693
+ reasoning_content: import_zod3.z.string().nullish(),
694
+ tool_calls: import_zod3.z.array(
695
+ import_zod3.z.object({
696
+ id: import_zod3.z.string().nullish(),
697
+ type: import_zod3.z.literal("function"),
698
+ function: import_zod3.z.object({
699
+ name: import_zod3.z.string(),
700
+ arguments: import_zod3.z.string()
764
701
  })
765
702
  })
766
703
  ).nullish()
767
704
  }),
768
- finish_reason: import_zod2.z.string().nullish()
705
+ finish_reason: import_zod3.z.string().nullish()
769
706
  })
770
707
  ),
771
708
  usage: openaiCompatibleTokenUsageSchema
772
709
  });
773
- var createOpenAICompatibleChatChunkSchema = (errorSchema) => import_zod2.z.union([
774
- import_zod2.z.object({
775
- id: import_zod2.z.string().nullish(),
776
- created: import_zod2.z.number().nullish(),
777
- model: import_zod2.z.string().nullish(),
778
- choices: import_zod2.z.array(
779
- import_zod2.z.object({
780
- delta: import_zod2.z.object({
781
- role: import_zod2.z.enum(["assistant"]).nullish(),
782
- content: import_zod2.z.string().nullish(),
783
- reasoning_content: import_zod2.z.string().nullish(),
784
- tool_calls: import_zod2.z.array(
785
- import_zod2.z.object({
786
- index: import_zod2.z.number(),
787
- id: import_zod2.z.string().nullish(),
788
- type: import_zod2.z.literal("function").nullish(),
789
- function: import_zod2.z.object({
790
- name: import_zod2.z.string().nullish(),
791
- arguments: import_zod2.z.string().nullish()
710
+ var createOpenAICompatibleChatChunkSchema = (errorSchema) => import_zod3.z.union([
711
+ import_zod3.z.object({
712
+ id: import_zod3.z.string().nullish(),
713
+ created: import_zod3.z.number().nullish(),
714
+ model: import_zod3.z.string().nullish(),
715
+ choices: import_zod3.z.array(
716
+ import_zod3.z.object({
717
+ delta: import_zod3.z.object({
718
+ role: import_zod3.z.enum(["assistant"]).nullish(),
719
+ content: import_zod3.z.string().nullish(),
720
+ reasoning_content: import_zod3.z.string().nullish(),
721
+ tool_calls: import_zod3.z.array(
722
+ import_zod3.z.object({
723
+ index: import_zod3.z.number(),
724
+ id: import_zod3.z.string().nullish(),
725
+ type: import_zod3.z.literal("function").nullish(),
726
+ function: import_zod3.z.object({
727
+ name: import_zod3.z.string().nullish(),
728
+ arguments: import_zod3.z.string().nullish()
792
729
  })
793
730
  })
794
731
  ).nullish()
795
732
  }).nullish(),
796
- finish_reason: import_zod2.z.string().nullish()
733
+ finish_reason: import_zod3.z.string().nullish()
797
734
  })
798
735
  ),
799
736
  usage: openaiCompatibleTokenUsageSchema
@@ -802,21 +739,16 @@ var createOpenAICompatibleChatChunkSchema = (errorSchema) => import_zod2.z.union
802
739
  ]);
803
740
 
804
741
  // src/openai-compatible-completion-language-model.ts
805
- var import_provider5 = require("@ai-sdk/provider");
806
- var import_provider_utils3 = require("@ai-sdk/provider-utils");
807
- var import_zod3 = require("zod");
742
+ var import_provider_utils2 = require("@ai-sdk/provider-utils");
743
+ var import_zod5 = require("zod");
808
744
 
809
745
  // src/convert-to-openai-compatible-completion-prompt.ts
810
746
  var import_provider4 = require("@ai-sdk/provider");
811
747
  function convertToOpenAICompatibleCompletionPrompt({
812
748
  prompt,
813
- inputFormat,
814
749
  user = "user",
815
750
  assistant = "assistant"
816
751
  }) {
817
- if (inputFormat === "prompt" && prompt.length === 1 && prompt[0].role === "user" && prompt[0].content.length === 1 && prompt[0].content[0].type === "text") {
818
- return { prompt: prompt[0].content[0].text };
819
- }
820
752
  let text = "";
821
753
  if (prompt[0].role === "system") {
822
754
  text += `${prompt[0].content}
@@ -838,13 +770,8 @@ function convertToOpenAICompatibleCompletionPrompt({
838
770
  case "text": {
839
771
  return part.text;
840
772
  }
841
- case "image": {
842
- throw new import_provider4.UnsupportedFunctionalityError({
843
- functionality: "images"
844
- });
845
- }
846
773
  }
847
- }).join("");
774
+ }).filter(Boolean).join("");
848
775
  text += `${user}:
849
776
  ${userMessage}
850
777
 
@@ -890,21 +817,44 @@ ${user}:`]
890
817
  };
891
818
  }
892
819
 
820
+ // src/openai-compatible-completion-options.ts
821
+ var import_zod4 = require("zod");
822
+ var openaiCompatibleCompletionProviderOptions = import_zod4.z.object({
823
+ /**
824
+ * Echo back the prompt in addition to the completion.
825
+ */
826
+ echo: import_zod4.z.boolean().optional(),
827
+ /**
828
+ * Modify the likelihood of specified tokens appearing in the completion.
829
+ *
830
+ * Accepts a JSON object that maps tokens (specified by their token ID in
831
+ * the GPT tokenizer) to an associated bias value from -100 to 100.
832
+ */
833
+ logitBias: import_zod4.z.record(import_zod4.z.string(), import_zod4.z.number()).optional(),
834
+ /**
835
+ * The suffix that comes after a completion of inserted text.
836
+ */
837
+ suffix: import_zod4.z.string().optional(),
838
+ /**
839
+ * A unique identifier representing your end-user, which can help providers to
840
+ * monitor and detect abuse.
841
+ */
842
+ user: import_zod4.z.string().optional()
843
+ });
844
+
893
845
  // src/openai-compatible-completion-language-model.ts
894
846
  var OpenAICompatibleCompletionLanguageModel = class {
895
847
  // type inferred via constructor
896
- constructor(modelId, settings, config) {
897
- this.specificationVersion = "v1";
898
- this.defaultObjectGenerationMode = void 0;
848
+ constructor(modelId, config) {
849
+ this.specificationVersion = "v2";
899
850
  var _a;
900
851
  this.modelId = modelId;
901
- this.settings = settings;
902
852
  this.config = config;
903
853
  const errorStructure = (_a = config.errorStructure) != null ? _a : defaultOpenAICompatibleErrorStructure;
904
854
  this.chunkSchema = createOpenAICompatibleCompletionChunkSchema(
905
855
  errorStructure.errorSchema
906
856
  );
907
- this.failedResponseHandler = (0, import_provider_utils3.createJsonErrorResponseHandler)(errorStructure);
857
+ this.failedResponseHandler = (0, import_provider_utils2.createJsonErrorResponseHandler)(errorStructure);
908
858
  }
909
859
  get provider() {
910
860
  return this.config.provider;
@@ -912,11 +862,13 @@ var OpenAICompatibleCompletionLanguageModel = class {
912
862
  get providerOptionsName() {
913
863
  return this.config.provider.split(".")[0].trim();
914
864
  }
915
- getArgs({
916
- mode,
917
- inputFormat,
865
+ get supportedUrls() {
866
+ var _a, _b, _c;
867
+ return (_c = (_b = (_a = this.config).supportedUrls) == null ? void 0 : _b.call(_a)) != null ? _c : {};
868
+ }
869
+ async getArgs({
918
870
  prompt,
919
- maxTokens,
871
+ maxOutputTokens,
920
872
  temperature,
921
873
  topP,
922
874
  topK,
@@ -925,16 +877,25 @@ var OpenAICompatibleCompletionLanguageModel = class {
925
877
  stopSequences: userStopSequences,
926
878
  responseFormat,
927
879
  seed,
928
- providerMetadata
880
+ providerOptions,
881
+ tools,
882
+ toolChoice
929
883
  }) {
930
884
  var _a;
931
- const type = mode.type;
932
885
  const warnings = [];
886
+ const completionOptions = (_a = await (0, import_provider_utils2.parseProviderOptions)({
887
+ provider: this.providerOptionsName,
888
+ providerOptions,
889
+ schema: openaiCompatibleCompletionProviderOptions
890
+ })) != null ? _a : {};
933
891
  if (topK != null) {
934
- warnings.push({
935
- type: "unsupported-setting",
936
- setting: "topK"
937
- });
892
+ warnings.push({ type: "unsupported-setting", setting: "topK" });
893
+ }
894
+ if (tools == null ? void 0 : tools.length) {
895
+ warnings.push({ type: "unsupported-setting", setting: "tools" });
896
+ }
897
+ if (toolChoice != null) {
898
+ warnings.push({ type: "unsupported-setting", setting: "toolChoice" });
938
899
  }
939
900
  if (responseFormat != null && responseFormat.type !== "text") {
940
901
  warnings.push({
@@ -943,129 +904,113 @@ var OpenAICompatibleCompletionLanguageModel = class {
943
904
  details: "JSON response format is not supported."
944
905
  });
945
906
  }
946
- const { prompt: completionPrompt, stopSequences } = convertToOpenAICompatibleCompletionPrompt({ prompt, inputFormat });
907
+ const { prompt: completionPrompt, stopSequences } = convertToOpenAICompatibleCompletionPrompt({ prompt });
947
908
  const stop = [...stopSequences != null ? stopSequences : [], ...userStopSequences != null ? userStopSequences : []];
948
- const baseArgs = {
949
- // model id:
950
- model: this.modelId,
951
- // model specific settings:
952
- echo: this.settings.echo,
953
- logit_bias: this.settings.logitBias,
954
- suffix: this.settings.suffix,
955
- user: this.settings.user,
956
- // standardized settings:
957
- max_tokens: maxTokens,
958
- temperature,
959
- top_p: topP,
960
- frequency_penalty: frequencyPenalty,
961
- presence_penalty: presencePenalty,
962
- seed,
963
- ...providerMetadata == null ? void 0 : providerMetadata[this.providerOptionsName],
964
- // prompt:
965
- prompt: completionPrompt,
966
- // stop sequences:
967
- stop: stop.length > 0 ? stop : void 0
909
+ return {
910
+ args: {
911
+ // model id:
912
+ model: this.modelId,
913
+ // model specific settings:
914
+ echo: completionOptions.echo,
915
+ logit_bias: completionOptions.logitBias,
916
+ suffix: completionOptions.suffix,
917
+ user: completionOptions.user,
918
+ // standardized settings:
919
+ max_tokens: maxOutputTokens,
920
+ temperature,
921
+ top_p: topP,
922
+ frequency_penalty: frequencyPenalty,
923
+ presence_penalty: presencePenalty,
924
+ seed,
925
+ ...providerOptions == null ? void 0 : providerOptions[this.providerOptionsName],
926
+ // prompt:
927
+ prompt: completionPrompt,
928
+ // stop sequences:
929
+ stop: stop.length > 0 ? stop : void 0
930
+ },
931
+ warnings
968
932
  };
969
- switch (type) {
970
- case "regular": {
971
- if ((_a = mode.tools) == null ? void 0 : _a.length) {
972
- throw new import_provider5.UnsupportedFunctionalityError({
973
- functionality: "tools"
974
- });
975
- }
976
- if (mode.toolChoice) {
977
- throw new import_provider5.UnsupportedFunctionalityError({
978
- functionality: "toolChoice"
979
- });
980
- }
981
- return { args: baseArgs, warnings };
982
- }
983
- case "object-json": {
984
- throw new import_provider5.UnsupportedFunctionalityError({
985
- functionality: "object-json mode"
986
- });
987
- }
988
- case "object-tool": {
989
- throw new import_provider5.UnsupportedFunctionalityError({
990
- functionality: "object-tool mode"
991
- });
992
- }
993
- default: {
994
- const _exhaustiveCheck = type;
995
- throw new Error(`Unsupported type: ${_exhaustiveCheck}`);
996
- }
997
- }
998
933
  }
999
934
  async doGenerate(options) {
1000
- var _a, _b, _c, _d;
1001
- const { args, warnings } = this.getArgs(options);
935
+ var _a, _b, _c, _d, _e, _f;
936
+ const { args, warnings } = await this.getArgs(options);
1002
937
  const {
1003
938
  responseHeaders,
1004
939
  value: response,
1005
940
  rawValue: rawResponse
1006
- } = await (0, import_provider_utils3.postJsonToApi)({
941
+ } = await (0, import_provider_utils2.postJsonToApi)({
1007
942
  url: this.config.url({
1008
943
  path: "/completions",
1009
944
  modelId: this.modelId
1010
945
  }),
1011
- headers: (0, import_provider_utils3.combineHeaders)(this.config.headers(), options.headers),
946
+ headers: (0, import_provider_utils2.combineHeaders)(this.config.headers(), options.headers),
1012
947
  body: args,
1013
948
  failedResponseHandler: this.failedResponseHandler,
1014
- successfulResponseHandler: (0, import_provider_utils3.createJsonResponseHandler)(
949
+ successfulResponseHandler: (0, import_provider_utils2.createJsonResponseHandler)(
1015
950
  openaiCompatibleCompletionResponseSchema
1016
951
  ),
1017
952
  abortSignal: options.abortSignal,
1018
953
  fetch: this.config.fetch
1019
954
  });
1020
- const { prompt: rawPrompt, ...rawSettings } = args;
1021
955
  const choice = response.choices[0];
956
+ const content = [];
957
+ if (choice.text != null && choice.text.length > 0) {
958
+ content.push({ type: "text", text: choice.text });
959
+ }
1022
960
  return {
1023
- text: choice.text,
961
+ content,
1024
962
  usage: {
1025
- promptTokens: (_b = (_a = response.usage) == null ? void 0 : _a.prompt_tokens) != null ? _b : NaN,
1026
- completionTokens: (_d = (_c = response.usage) == null ? void 0 : _c.completion_tokens) != null ? _d : NaN
963
+ inputTokens: (_b = (_a = response.usage) == null ? void 0 : _a.prompt_tokens) != null ? _b : void 0,
964
+ outputTokens: (_d = (_c = response.usage) == null ? void 0 : _c.completion_tokens) != null ? _d : void 0,
965
+ totalTokens: (_f = (_e = response.usage) == null ? void 0 : _e.total_tokens) != null ? _f : void 0
1027
966
  },
1028
967
  finishReason: mapOpenAICompatibleFinishReason(choice.finish_reason),
1029
- rawCall: { rawPrompt, rawSettings },
1030
- rawResponse: { headers: responseHeaders, body: rawResponse },
1031
- response: getResponseMetadata(response),
1032
- warnings,
1033
- request: { body: JSON.stringify(args) }
968
+ request: { body: args },
969
+ response: {
970
+ ...getResponseMetadata(response),
971
+ headers: responseHeaders,
972
+ body: rawResponse
973
+ },
974
+ warnings
1034
975
  };
1035
976
  }
1036
977
  async doStream(options) {
1037
- const { args, warnings } = this.getArgs(options);
978
+ const { args, warnings } = await this.getArgs(options);
1038
979
  const body = {
1039
980
  ...args,
1040
981
  stream: true,
1041
982
  // only include stream_options when in strict compatibility mode:
1042
983
  stream_options: this.config.includeUsage ? { include_usage: true } : void 0
1043
984
  };
1044
- const { responseHeaders, value: response } = await (0, import_provider_utils3.postJsonToApi)({
985
+ const { responseHeaders, value: response } = await (0, import_provider_utils2.postJsonToApi)({
1045
986
  url: this.config.url({
1046
987
  path: "/completions",
1047
988
  modelId: this.modelId
1048
989
  }),
1049
- headers: (0, import_provider_utils3.combineHeaders)(this.config.headers(), options.headers),
990
+ headers: (0, import_provider_utils2.combineHeaders)(this.config.headers(), options.headers),
1050
991
  body,
1051
992
  failedResponseHandler: this.failedResponseHandler,
1052
- successfulResponseHandler: (0, import_provider_utils3.createEventSourceResponseHandler)(
993
+ successfulResponseHandler: (0, import_provider_utils2.createEventSourceResponseHandler)(
1053
994
  this.chunkSchema
1054
995
  ),
1055
996
  abortSignal: options.abortSignal,
1056
997
  fetch: this.config.fetch
1057
998
  });
1058
- const { prompt: rawPrompt, ...rawSettings } = args;
1059
999
  let finishReason = "unknown";
1060
- let usage = {
1061
- promptTokens: Number.NaN,
1062
- completionTokens: Number.NaN
1000
+ const usage = {
1001
+ inputTokens: void 0,
1002
+ outputTokens: void 0,
1003
+ totalTokens: void 0
1063
1004
  };
1064
1005
  let isFirstChunk = true;
1065
1006
  return {
1066
1007
  stream: response.pipeThrough(
1067
1008
  new TransformStream({
1009
+ start(controller) {
1010
+ controller.enqueue({ type: "stream-start", warnings });
1011
+ },
1068
1012
  transform(chunk, controller) {
1013
+ var _a, _b, _c;
1069
1014
  if (!chunk.success) {
1070
1015
  finishReason = "error";
1071
1016
  controller.enqueue({ type: "error", error: chunk.error });
@@ -1085,10 +1030,9 @@ var OpenAICompatibleCompletionLanguageModel = class {
1085
1030
  });
1086
1031
  }
1087
1032
  if (value.usage != null) {
1088
- usage = {
1089
- promptTokens: value.usage.prompt_tokens,
1090
- completionTokens: value.usage.completion_tokens
1091
- };
1033
+ usage.inputTokens = (_a = value.usage.prompt_tokens) != null ? _a : void 0;
1034
+ usage.outputTokens = (_b = value.usage.completion_tokens) != null ? _b : void 0;
1035
+ usage.totalTokens = (_c = value.usage.total_tokens) != null ? _c : void 0;
1092
1036
  }
1093
1037
  const choice = value.choices[0];
1094
1038
  if ((choice == null ? void 0 : choice.finish_reason) != null) {
@@ -1098,8 +1042,8 @@ var OpenAICompatibleCompletionLanguageModel = class {
1098
1042
  }
1099
1043
  if ((choice == null ? void 0 : choice.text) != null) {
1100
1044
  controller.enqueue({
1101
- type: "text-delta",
1102
- textDelta: choice.text
1045
+ type: "text",
1046
+ text: choice.text
1103
1047
  });
1104
1048
  }
1105
1049
  },
@@ -1112,57 +1056,70 @@ var OpenAICompatibleCompletionLanguageModel = class {
1112
1056
  }
1113
1057
  })
1114
1058
  ),
1115
- rawCall: { rawPrompt, rawSettings },
1116
- rawResponse: { headers: responseHeaders },
1117
- warnings,
1118
- request: { body: JSON.stringify(body) }
1059
+ request: { body },
1060
+ response: { headers: responseHeaders }
1119
1061
  };
1120
1062
  }
1121
1063
  };
1122
- var openaiCompatibleCompletionResponseSchema = import_zod3.z.object({
1123
- id: import_zod3.z.string().nullish(),
1124
- created: import_zod3.z.number().nullish(),
1125
- model: import_zod3.z.string().nullish(),
1126
- choices: import_zod3.z.array(
1127
- import_zod3.z.object({
1128
- text: import_zod3.z.string(),
1129
- finish_reason: import_zod3.z.string()
1064
+ var usageSchema = import_zod5.z.object({
1065
+ prompt_tokens: import_zod5.z.number(),
1066
+ completion_tokens: import_zod5.z.number(),
1067
+ total_tokens: import_zod5.z.number()
1068
+ });
1069
+ var openaiCompatibleCompletionResponseSchema = import_zod5.z.object({
1070
+ id: import_zod5.z.string().nullish(),
1071
+ created: import_zod5.z.number().nullish(),
1072
+ model: import_zod5.z.string().nullish(),
1073
+ choices: import_zod5.z.array(
1074
+ import_zod5.z.object({
1075
+ text: import_zod5.z.string(),
1076
+ finish_reason: import_zod5.z.string()
1130
1077
  })
1131
1078
  ),
1132
- usage: import_zod3.z.object({
1133
- prompt_tokens: import_zod3.z.number(),
1134
- completion_tokens: import_zod3.z.number()
1135
- }).nullish()
1079
+ usage: usageSchema.nullish()
1136
1080
  });
1137
- var createOpenAICompatibleCompletionChunkSchema = (errorSchema) => import_zod3.z.union([
1138
- import_zod3.z.object({
1139
- id: import_zod3.z.string().nullish(),
1140
- created: import_zod3.z.number().nullish(),
1141
- model: import_zod3.z.string().nullish(),
1142
- choices: import_zod3.z.array(
1143
- import_zod3.z.object({
1144
- text: import_zod3.z.string(),
1145
- finish_reason: import_zod3.z.string().nullish(),
1146
- index: import_zod3.z.number()
1081
+ var createOpenAICompatibleCompletionChunkSchema = (errorSchema) => import_zod5.z.union([
1082
+ import_zod5.z.object({
1083
+ id: import_zod5.z.string().nullish(),
1084
+ created: import_zod5.z.number().nullish(),
1085
+ model: import_zod5.z.string().nullish(),
1086
+ choices: import_zod5.z.array(
1087
+ import_zod5.z.object({
1088
+ text: import_zod5.z.string(),
1089
+ finish_reason: import_zod5.z.string().nullish(),
1090
+ index: import_zod5.z.number()
1147
1091
  })
1148
1092
  ),
1149
- usage: import_zod3.z.object({
1150
- prompt_tokens: import_zod3.z.number(),
1151
- completion_tokens: import_zod3.z.number()
1152
- }).nullish()
1093
+ usage: usageSchema.nullish()
1153
1094
  }),
1154
1095
  errorSchema
1155
1096
  ]);
1156
1097
 
1157
1098
  // src/openai-compatible-embedding-model.ts
1158
- var import_provider6 = require("@ai-sdk/provider");
1159
- var import_provider_utils4 = require("@ai-sdk/provider-utils");
1160
- var import_zod4 = require("zod");
1099
+ var import_provider5 = require("@ai-sdk/provider");
1100
+ var import_provider_utils3 = require("@ai-sdk/provider-utils");
1101
+ var import_zod7 = require("zod");
1102
+
1103
+ // src/openai-compatible-embedding-options.ts
1104
+ var import_zod6 = require("zod");
1105
+ var openaiCompatibleEmbeddingProviderOptions = import_zod6.z.object({
1106
+ /**
1107
+ * The number of dimensions the resulting output embeddings should have.
1108
+ * Only supported in text-embedding-3 and later models.
1109
+ */
1110
+ dimensions: import_zod6.z.number().optional(),
1111
+ /**
1112
+ * A unique identifier representing your end-user, which can help providers to
1113
+ * monitor and detect abuse.
1114
+ */
1115
+ user: import_zod6.z.string().optional()
1116
+ });
1117
+
1118
+ // src/openai-compatible-embedding-model.ts
1161
1119
  var OpenAICompatibleEmbeddingModel = class {
1162
- constructor(modelId, settings, config) {
1163
- this.specificationVersion = "v1";
1120
+ constructor(modelId, config) {
1121
+ this.specificationVersion = "v2";
1164
1122
  this.modelId = modelId;
1165
- this.settings = settings;
1166
1123
  this.config = config;
1167
1124
  }
1168
1125
  get provider() {
@@ -1176,37 +1133,57 @@ var OpenAICompatibleEmbeddingModel = class {
1176
1133
  var _a;
1177
1134
  return (_a = this.config.supportsParallelCalls) != null ? _a : true;
1178
1135
  }
1136
+ get providerOptionsName() {
1137
+ return this.config.provider.split(".")[0].trim();
1138
+ }
1179
1139
  async doEmbed({
1180
1140
  values,
1181
1141
  headers,
1182
- abortSignal
1142
+ abortSignal,
1143
+ providerOptions
1183
1144
  }) {
1184
- var _a;
1145
+ var _a, _b, _c;
1146
+ const compatibleOptions = Object.assign(
1147
+ (_a = await (0, import_provider_utils3.parseProviderOptions)({
1148
+ provider: "openai-compatible",
1149
+ providerOptions,
1150
+ schema: openaiCompatibleEmbeddingProviderOptions
1151
+ })) != null ? _a : {},
1152
+ (_b = await (0, import_provider_utils3.parseProviderOptions)({
1153
+ provider: this.providerOptionsName,
1154
+ providerOptions,
1155
+ schema: openaiCompatibleEmbeddingProviderOptions
1156
+ })) != null ? _b : {}
1157
+ );
1185
1158
  if (values.length > this.maxEmbeddingsPerCall) {
1186
- throw new import_provider6.TooManyEmbeddingValuesForCallError({
1159
+ throw new import_provider5.TooManyEmbeddingValuesForCallError({
1187
1160
  provider: this.provider,
1188
1161
  modelId: this.modelId,
1189
1162
  maxEmbeddingsPerCall: this.maxEmbeddingsPerCall,
1190
1163
  values
1191
1164
  });
1192
1165
  }
1193
- const { responseHeaders, value: response } = await (0, import_provider_utils4.postJsonToApi)({
1166
+ const {
1167
+ responseHeaders,
1168
+ value: response,
1169
+ rawValue
1170
+ } = await (0, import_provider_utils3.postJsonToApi)({
1194
1171
  url: this.config.url({
1195
1172
  path: "/embeddings",
1196
1173
  modelId: this.modelId
1197
1174
  }),
1198
- headers: (0, import_provider_utils4.combineHeaders)(this.config.headers(), headers),
1175
+ headers: (0, import_provider_utils3.combineHeaders)(this.config.headers(), headers),
1199
1176
  body: {
1200
1177
  model: this.modelId,
1201
1178
  input: values,
1202
1179
  encoding_format: "float",
1203
- dimensions: this.settings.dimensions,
1204
- user: this.settings.user
1180
+ dimensions: compatibleOptions.dimensions,
1181
+ user: compatibleOptions.user
1205
1182
  },
1206
- failedResponseHandler: (0, import_provider_utils4.createJsonErrorResponseHandler)(
1207
- (_a = this.config.errorStructure) != null ? _a : defaultOpenAICompatibleErrorStructure
1183
+ failedResponseHandler: (0, import_provider_utils3.createJsonErrorResponseHandler)(
1184
+ (_c = this.config.errorStructure) != null ? _c : defaultOpenAICompatibleErrorStructure
1208
1185
  ),
1209
- successfulResponseHandler: (0, import_provider_utils4.createJsonResponseHandler)(
1186
+ successfulResponseHandler: (0, import_provider_utils3.createJsonResponseHandler)(
1210
1187
  openaiTextEmbeddingResponseSchema
1211
1188
  ),
1212
1189
  abortSignal,
@@ -1215,28 +1192,24 @@ var OpenAICompatibleEmbeddingModel = class {
1215
1192
  return {
1216
1193
  embeddings: response.data.map((item) => item.embedding),
1217
1194
  usage: response.usage ? { tokens: response.usage.prompt_tokens } : void 0,
1218
- rawResponse: { headers: responseHeaders }
1195
+ response: { headers: responseHeaders, body: rawValue }
1219
1196
  };
1220
1197
  }
1221
1198
  };
1222
- var openaiTextEmbeddingResponseSchema = import_zod4.z.object({
1223
- data: import_zod4.z.array(import_zod4.z.object({ embedding: import_zod4.z.array(import_zod4.z.number()) })),
1224
- usage: import_zod4.z.object({ prompt_tokens: import_zod4.z.number() }).nullish()
1199
+ var openaiTextEmbeddingResponseSchema = import_zod7.z.object({
1200
+ data: import_zod7.z.array(import_zod7.z.object({ embedding: import_zod7.z.array(import_zod7.z.number()) })),
1201
+ usage: import_zod7.z.object({ prompt_tokens: import_zod7.z.number() }).nullish()
1225
1202
  });
1226
1203
 
1227
1204
  // src/openai-compatible-image-model.ts
1228
- var import_provider_utils5 = require("@ai-sdk/provider-utils");
1229
- var import_zod5 = require("zod");
1205
+ var import_provider_utils4 = require("@ai-sdk/provider-utils");
1206
+ var import_zod8 = require("zod");
1230
1207
  var OpenAICompatibleImageModel = class {
1231
- constructor(modelId, settings, config) {
1208
+ constructor(modelId, config) {
1232
1209
  this.modelId = modelId;
1233
- this.settings = settings;
1234
1210
  this.config = config;
1235
- this.specificationVersion = "v1";
1236
- }
1237
- get maxImagesPerCall() {
1238
- var _a;
1239
- return (_a = this.settings.maxImagesPerCall) != null ? _a : 10;
1211
+ this.specificationVersion = "v2";
1212
+ this.maxImagesPerCall = 10;
1240
1213
  }
1241
1214
  get provider() {
1242
1215
  return this.config.provider;
@@ -1264,25 +1237,24 @@ var OpenAICompatibleImageModel = class {
1264
1237
  warnings.push({ type: "unsupported-setting", setting: "seed" });
1265
1238
  }
1266
1239
  const currentDate = (_c = (_b = (_a = this.config._internal) == null ? void 0 : _a.currentDate) == null ? void 0 : _b.call(_a)) != null ? _c : /* @__PURE__ */ new Date();
1267
- const { value: response, responseHeaders } = await (0, import_provider_utils5.postJsonToApi)({
1240
+ const { value: response, responseHeaders } = await (0, import_provider_utils4.postJsonToApi)({
1268
1241
  url: this.config.url({
1269
1242
  path: "/images/generations",
1270
1243
  modelId: this.modelId
1271
1244
  }),
1272
- headers: (0, import_provider_utils5.combineHeaders)(this.config.headers(), headers),
1245
+ headers: (0, import_provider_utils4.combineHeaders)(this.config.headers(), headers),
1273
1246
  body: {
1274
1247
  model: this.modelId,
1275
1248
  prompt,
1276
1249
  n,
1277
1250
  size,
1278
1251
  ...(_d = providerOptions.openai) != null ? _d : {},
1279
- response_format: "b64_json",
1280
- ...this.settings.user ? { user: this.settings.user } : {}
1252
+ response_format: "b64_json"
1281
1253
  },
1282
- failedResponseHandler: (0, import_provider_utils5.createJsonErrorResponseHandler)(
1254
+ failedResponseHandler: (0, import_provider_utils4.createJsonErrorResponseHandler)(
1283
1255
  (_e = this.config.errorStructure) != null ? _e : defaultOpenAICompatibleErrorStructure
1284
1256
  ),
1285
- successfulResponseHandler: (0, import_provider_utils5.createJsonResponseHandler)(
1257
+ successfulResponseHandler: (0, import_provider_utils4.createJsonResponseHandler)(
1286
1258
  openaiCompatibleImageResponseSchema
1287
1259
  ),
1288
1260
  abortSignal,
@@ -1299,14 +1271,14 @@ var OpenAICompatibleImageModel = class {
1299
1271
  };
1300
1272
  }
1301
1273
  };
1302
- var openaiCompatibleImageResponseSchema = import_zod5.z.object({
1303
- 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() }))
1304
1276
  });
1305
1277
 
1306
1278
  // src/openai-compatible-provider.ts
1307
- var import_provider_utils6 = require("@ai-sdk/provider-utils");
1279
+ var import_provider_utils5 = require("@ai-sdk/provider-utils");
1308
1280
  function createOpenAICompatible(options) {
1309
- const baseURL = (0, import_provider_utils6.withoutTrailingSlash)(options.baseURL);
1281
+ const baseURL = (0, import_provider_utils5.withoutTrailingSlash)(options.baseURL);
1310
1282
  const providerName = options.name;
1311
1283
  const getHeaders = () => ({
1312
1284
  ...options.apiKey && { Authorization: `Bearer ${options.apiKey}` },
@@ -1324,27 +1296,20 @@ function createOpenAICompatible(options) {
1324
1296
  headers: getHeaders,
1325
1297
  fetch: options.fetch
1326
1298
  });
1327
- const createLanguageModel = (modelId, settings = {}) => createChatModel(modelId, settings);
1328
- const createChatModel = (modelId, settings = {}) => new OpenAICompatibleChatLanguageModel(modelId, settings, {
1329
- ...getCommonModelConfig("chat"),
1330
- defaultObjectGenerationMode: "tool"
1331
- });
1332
- const createCompletionModel = (modelId, settings = {}) => new OpenAICompatibleCompletionLanguageModel(
1333
- modelId,
1334
- settings,
1335
- getCommonModelConfig("completion")
1336
- );
1337
- const createEmbeddingModel = (modelId, settings = {}) => new OpenAICompatibleEmbeddingModel(
1299
+ const createLanguageModel = (modelId) => createChatModel(modelId);
1300
+ const createChatModel = (modelId) => new OpenAICompatibleChatLanguageModel(
1338
1301
  modelId,
1339
- settings,
1340
- getCommonModelConfig("embedding")
1302
+ getCommonModelConfig("chat")
1341
1303
  );
1342
- const createImageModel = (modelId, settings = {}) => new OpenAICompatibleImageModel(
1304
+ const createCompletionModel = (modelId) => new OpenAICompatibleCompletionLanguageModel(
1343
1305
  modelId,
1344
- settings,
1345
- getCommonModelConfig("image")
1306
+ getCommonModelConfig("completion")
1346
1307
  );
1347
- const provider = (modelId, settings) => createLanguageModel(modelId, settings);
1308
+ const createEmbeddingModel = (modelId) => new OpenAICompatibleEmbeddingModel(modelId, {
1309
+ ...getCommonModelConfig("embedding")
1310
+ });
1311
+ const createImageModel = (modelId) => new OpenAICompatibleImageModel(modelId, getCommonModelConfig("image"));
1312
+ const provider = (modelId) => createLanguageModel(modelId);
1348
1313
  provider.languageModel = createLanguageModel;
1349
1314
  provider.chatModel = createChatModel;
1350
1315
  provider.completionModel = createCompletionModel;