@ai-sdk/groq 2.0.0-canary.2 → 2.0.0-canary.20

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,12 +31,11 @@ var import_provider_utils4 = require("@ai-sdk/provider-utils");
31
31
 
32
32
  // src/groq-chat-language-model.ts
33
33
  var import_provider3 = require("@ai-sdk/provider");
34
- var import_provider_utils3 = require("@ai-sdk/provider-utils");
35
- var import_zod2 = require("zod");
34
+ var import_provider_utils2 = require("@ai-sdk/provider-utils");
35
+ var import_zod3 = require("zod");
36
36
 
37
37
  // src/convert-to-groq-chat-messages.ts
38
38
  var import_provider = require("@ai-sdk/provider");
39
- var import_provider_utils = require("@ai-sdk/provider-utils");
40
39
  function convertToGroqChatMessages(prompt) {
41
40
  const messages = [];
42
41
  for (const { role, content } of prompt) {
@@ -53,24 +52,24 @@ function convertToGroqChatMessages(prompt) {
53
52
  messages.push({
54
53
  role: "user",
55
54
  content: content.map((part) => {
56
- var _a;
57
55
  switch (part.type) {
58
56
  case "text": {
59
57
  return { type: "text", text: part.text };
60
58
  }
61
- case "image": {
59
+ case "file": {
60
+ if (!part.mediaType.startsWith("image/")) {
61
+ throw new import_provider.UnsupportedFunctionalityError({
62
+ functionality: "Non-image file content parts"
63
+ });
64
+ }
65
+ const mediaType = part.mediaType === "image/*" ? "image/jpeg" : part.mediaType;
62
66
  return {
63
67
  type: "image_url",
64
68
  image_url: {
65
- url: part.image instanceof URL ? part.image.toString() : `data:${(_a = part.mimeType) != null ? _a : "image/jpeg"};base64,${(0, import_provider_utils.convertUint8ArrayToBase64)(part.image)}`
69
+ url: part.data instanceof URL ? part.data.toString() : `data:${mediaType};base64,${part.data}`
66
70
  }
67
71
  };
68
72
  }
69
- case "file": {
70
- throw new import_provider.UnsupportedFunctionalityError({
71
- functionality: "File content parts in user messages"
72
- });
73
- }
74
73
  }
75
74
  })
76
75
  });
@@ -137,16 +136,31 @@ function getResponseMetadata({
137
136
  };
138
137
  }
139
138
 
140
- // src/groq-error.ts
139
+ // src/groq-chat-options.ts
141
140
  var import_zod = require("zod");
142
- var import_provider_utils2 = require("@ai-sdk/provider-utils");
143
- var groqErrorDataSchema = import_zod.z.object({
144
- error: import_zod.z.object({
145
- message: import_zod.z.string(),
146
- type: import_zod.z.string()
141
+ var groqProviderOptions = import_zod.z.object({
142
+ reasoningFormat: import_zod.z.enum(["parsed", "raw", "hidden"]).optional(),
143
+ /**
144
+ * Whether to enable parallel function calling during tool use. Default to true.
145
+ */
146
+ parallelToolCalls: import_zod.z.boolean().optional(),
147
+ /**
148
+ * A unique identifier representing your end-user, which can help OpenAI to
149
+ * monitor and detect abuse. Learn more.
150
+ */
151
+ user: import_zod.z.string().optional()
152
+ });
153
+
154
+ // src/groq-error.ts
155
+ var import_zod2 = require("zod");
156
+ var import_provider_utils = require("@ai-sdk/provider-utils");
157
+ var groqErrorDataSchema = import_zod2.z.object({
158
+ error: import_zod2.z.object({
159
+ message: import_zod2.z.string(),
160
+ type: import_zod2.z.string()
147
161
  })
148
162
  });
149
- var groqFailedResponseHandler = (0, import_provider_utils2.createJsonErrorResponseHandler)({
163
+ var groqFailedResponseHandler = (0, import_provider_utils.createJsonErrorResponseHandler)({
150
164
  errorSchema: groqErrorDataSchema,
151
165
  errorToMessage: (data) => data.error.message
152
166
  });
@@ -154,15 +168,14 @@ var groqFailedResponseHandler = (0, import_provider_utils2.createJsonErrorRespon
154
168
  // src/groq-prepare-tools.ts
155
169
  var import_provider2 = require("@ai-sdk/provider");
156
170
  function prepareTools({
157
- mode
171
+ tools,
172
+ toolChoice
158
173
  }) {
159
- var _a;
160
- const tools = ((_a = mode.tools) == null ? void 0 : _a.length) ? mode.tools : void 0;
174
+ tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
161
175
  const toolWarnings = [];
162
176
  if (tools == null) {
163
- return { tools: void 0, tool_choice: void 0, toolWarnings };
177
+ return { tools: void 0, toolChoice: void 0, toolWarnings };
164
178
  }
165
- const toolChoice = mode.toolChoice;
166
179
  const groqTools = [];
167
180
  for (const tool of tools) {
168
181
  if (tool.type === "provider-defined") {
@@ -179,18 +192,18 @@ function prepareTools({
179
192
  }
180
193
  }
181
194
  if (toolChoice == null) {
182
- return { tools: groqTools, tool_choice: void 0, toolWarnings };
195
+ return { tools: groqTools, toolChoice: void 0, toolWarnings };
183
196
  }
184
197
  const type = toolChoice.type;
185
198
  switch (type) {
186
199
  case "auto":
187
200
  case "none":
188
201
  case "required":
189
- return { tools: groqTools, tool_choice: type, toolWarnings };
202
+ return { tools: groqTools, toolChoice: type, toolWarnings };
190
203
  case "tool":
191
204
  return {
192
205
  tools: groqTools,
193
- tool_choice: {
206
+ toolChoice: {
194
207
  type: "function",
195
208
  function: {
196
209
  name: toolChoice.toolName
@@ -201,7 +214,7 @@ function prepareTools({
201
214
  default: {
202
215
  const _exhaustiveCheck = type;
203
216
  throw new import_provider2.UnsupportedFunctionalityError({
204
- functionality: `Unsupported tool choice type: ${_exhaustiveCheck}`
217
+ functionality: `tool choice type: ${_exhaustiveCheck}`
205
218
  });
206
219
  }
207
220
  }
@@ -226,24 +239,20 @@ function mapGroqFinishReason(finishReason) {
226
239
 
227
240
  // src/groq-chat-language-model.ts
228
241
  var GroqChatLanguageModel = class {
229
- constructor(modelId, settings, config) {
242
+ constructor(modelId, config) {
230
243
  this.specificationVersion = "v2";
231
- this.supportsStructuredOutputs = false;
232
- this.defaultObjectGenerationMode = "json";
244
+ this.supportedUrls = {
245
+ "image/*": [/^https?:\/\/.*$/]
246
+ };
233
247
  this.modelId = modelId;
234
- this.settings = settings;
235
248
  this.config = config;
236
249
  }
237
250
  get provider() {
238
251
  return this.config.provider;
239
252
  }
240
- get supportsImageUrls() {
241
- return !this.settings.downloadImages;
242
- }
243
- getArgs({
244
- mode,
253
+ async getArgs({
245
254
  prompt,
246
- maxTokens,
255
+ maxOutputTokens,
247
256
  temperature,
248
257
  topP,
249
258
  topK,
@@ -253,9 +262,10 @@ var GroqChatLanguageModel = class {
253
262
  responseFormat,
254
263
  seed,
255
264
  stream,
256
- providerMetadata
265
+ tools,
266
+ toolChoice,
267
+ providerOptions
257
268
  }) {
258
- const type = mode.type;
259
269
  const warnings = [];
260
270
  if (topK != null) {
261
271
  warnings.push({
@@ -270,169 +280,148 @@ var GroqChatLanguageModel = class {
270
280
  details: "JSON response format schema is not supported"
271
281
  });
272
282
  }
273
- const groqOptions = (0, import_provider_utils3.parseProviderOptions)({
283
+ const groqOptions = await (0, import_provider_utils2.parseProviderOptions)({
274
284
  provider: "groq",
275
- providerOptions: providerMetadata,
276
- schema: import_zod2.z.object({
277
- reasoningFormat: import_zod2.z.enum(["parsed", "raw", "hidden"]).nullish()
278
- })
285
+ providerOptions,
286
+ schema: groqProviderOptions
279
287
  });
280
- const baseArgs = {
281
- // model id:
282
- model: this.modelId,
283
- // model specific settings:
284
- user: this.settings.user,
285
- parallel_tool_calls: this.settings.parallelToolCalls,
286
- // standardized settings:
287
- max_tokens: maxTokens,
288
- temperature,
289
- top_p: topP,
290
- frequency_penalty: frequencyPenalty,
291
- presence_penalty: presencePenalty,
292
- stop: stopSequences,
293
- seed,
294
- // response format:
295
- response_format: (
296
- // json object response format is not supported for streaming:
297
- stream === false && (responseFormat == null ? void 0 : responseFormat.type) === "json" ? { type: "json_object" } : void 0
298
- ),
299
- // provider options:
300
- reasoning_format: groqOptions == null ? void 0 : groqOptions.reasoningFormat,
301
- // messages:
302
- messages: convertToGroqChatMessages(prompt)
288
+ const {
289
+ tools: groqTools,
290
+ toolChoice: groqToolChoice,
291
+ toolWarnings
292
+ } = prepareTools({ tools, toolChoice });
293
+ return {
294
+ args: {
295
+ // model id:
296
+ model: this.modelId,
297
+ // model specific settings:
298
+ user: groqOptions == null ? void 0 : groqOptions.user,
299
+ parallel_tool_calls: groqOptions == null ? void 0 : groqOptions.parallelToolCalls,
300
+ // standardized settings:
301
+ max_tokens: maxOutputTokens,
302
+ temperature,
303
+ top_p: topP,
304
+ frequency_penalty: frequencyPenalty,
305
+ presence_penalty: presencePenalty,
306
+ stop: stopSequences,
307
+ seed,
308
+ // response format:
309
+ response_format: (
310
+ // json object response format is not supported for streaming:
311
+ stream === false && (responseFormat == null ? void 0 : responseFormat.type) === "json" ? { type: "json_object" } : void 0
312
+ ),
313
+ // provider options:
314
+ reasoning_format: groqOptions == null ? void 0 : groqOptions.reasoningFormat,
315
+ // messages:
316
+ messages: convertToGroqChatMessages(prompt),
317
+ // tools:
318
+ tools: groqTools,
319
+ tool_choice: groqToolChoice
320
+ },
321
+ warnings: [...warnings, ...toolWarnings]
303
322
  };
304
- switch (type) {
305
- case "regular": {
306
- const { tools, tool_choice, toolWarnings } = prepareTools({ mode });
307
- return {
308
- args: {
309
- ...baseArgs,
310
- tools,
311
- tool_choice
312
- },
313
- warnings: [...warnings, ...toolWarnings]
314
- };
315
- }
316
- case "object-json": {
317
- return {
318
- args: {
319
- ...baseArgs,
320
- response_format: (
321
- // json object response format is not supported for streaming:
322
- stream === false ? { type: "json_object" } : void 0
323
- )
324
- },
325
- warnings
326
- };
327
- }
328
- case "object-tool": {
329
- return {
330
- args: {
331
- ...baseArgs,
332
- tool_choice: {
333
- type: "function",
334
- function: { name: mode.tool.name }
335
- },
336
- tools: [
337
- {
338
- type: "function",
339
- function: {
340
- name: mode.tool.name,
341
- description: mode.tool.description,
342
- parameters: mode.tool.parameters
343
- }
344
- }
345
- ]
346
- },
347
- warnings
348
- };
349
- }
350
- default: {
351
- const _exhaustiveCheck = type;
352
- throw new Error(`Unsupported type: ${_exhaustiveCheck}`);
353
- }
354
- }
355
323
  }
356
324
  async doGenerate(options) {
357
325
  var _a, _b, _c, _d, _e, _f, _g;
358
- const { args, warnings } = this.getArgs({ ...options, stream: false });
326
+ const { args, warnings } = await this.getArgs({
327
+ ...options,
328
+ stream: false
329
+ });
359
330
  const body = JSON.stringify(args);
360
331
  const {
361
332
  responseHeaders,
362
333
  value: response,
363
334
  rawValue: rawResponse
364
- } = await (0, import_provider_utils3.postJsonToApi)({
335
+ } = await (0, import_provider_utils2.postJsonToApi)({
365
336
  url: this.config.url({
366
337
  path: "/chat/completions",
367
338
  modelId: this.modelId
368
339
  }),
369
- headers: (0, import_provider_utils3.combineHeaders)(this.config.headers(), options.headers),
340
+ headers: (0, import_provider_utils2.combineHeaders)(this.config.headers(), options.headers),
370
341
  body: args,
371
342
  failedResponseHandler: groqFailedResponseHandler,
372
- successfulResponseHandler: (0, import_provider_utils3.createJsonResponseHandler)(
343
+ successfulResponseHandler: (0, import_provider_utils2.createJsonResponseHandler)(
373
344
  groqChatResponseSchema
374
345
  ),
375
346
  abortSignal: options.abortSignal,
376
347
  fetch: this.config.fetch
377
348
  });
378
- const { messages: rawPrompt, ...rawSettings } = args;
379
349
  const choice = response.choices[0];
380
- return {
381
- text: (_a = choice.message.content) != null ? _a : void 0,
382
- reasoning: (_b = choice.message.reasoning) != null ? _b : void 0,
383
- toolCalls: (_c = choice.message.tool_calls) == null ? void 0 : _c.map((toolCall) => {
384
- var _a2;
385
- return {
350
+ const content = [];
351
+ const text = choice.message.content;
352
+ if (text != null && text.length > 0) {
353
+ content.push({ type: "text", text });
354
+ }
355
+ const reasoning = choice.message.reasoning;
356
+ if (reasoning != null && reasoning.length > 0) {
357
+ content.push({
358
+ type: "reasoning",
359
+ text: reasoning
360
+ });
361
+ }
362
+ if (choice.message.tool_calls != null) {
363
+ for (const toolCall of choice.message.tool_calls) {
364
+ content.push({
365
+ type: "tool-call",
386
366
  toolCallType: "function",
387
- toolCallId: (_a2 = toolCall.id) != null ? _a2 : (0, import_provider_utils3.generateId)(),
367
+ toolCallId: (_a = toolCall.id) != null ? _a : (0, import_provider_utils2.generateId)(),
388
368
  toolName: toolCall.function.name,
389
369
  args: toolCall.function.arguments
390
- };
391
- }),
370
+ });
371
+ }
372
+ }
373
+ return {
374
+ content,
392
375
  finishReason: mapGroqFinishReason(choice.finish_reason),
393
376
  usage: {
394
- promptTokens: (_e = (_d = response.usage) == null ? void 0 : _d.prompt_tokens) != null ? _e : NaN,
395
- completionTokens: (_g = (_f = response.usage) == null ? void 0 : _f.completion_tokens) != null ? _g : NaN
377
+ inputTokens: (_c = (_b = response.usage) == null ? void 0 : _b.prompt_tokens) != null ? _c : void 0,
378
+ outputTokens: (_e = (_d = response.usage) == null ? void 0 : _d.completion_tokens) != null ? _e : void 0,
379
+ totalTokens: (_g = (_f = response.usage) == null ? void 0 : _f.total_tokens) != null ? _g : void 0
380
+ },
381
+ response: {
382
+ ...getResponseMetadata(response),
383
+ headers: responseHeaders,
384
+ body: rawResponse
396
385
  },
397
- rawCall: { rawPrompt, rawSettings },
398
- rawResponse: { headers: responseHeaders, body: rawResponse },
399
- response: getResponseMetadata(response),
400
386
  warnings,
401
387
  request: { body }
402
388
  };
403
389
  }
404
390
  async doStream(options) {
405
- const { args, warnings } = this.getArgs({ ...options, stream: true });
391
+ const { args, warnings } = await this.getArgs({ ...options, stream: true });
406
392
  const body = JSON.stringify({ ...args, stream: true });
407
- const { responseHeaders, value: response } = await (0, import_provider_utils3.postJsonToApi)({
393
+ const { responseHeaders, value: response } = await (0, import_provider_utils2.postJsonToApi)({
408
394
  url: this.config.url({
409
395
  path: "/chat/completions",
410
396
  modelId: this.modelId
411
397
  }),
412
- headers: (0, import_provider_utils3.combineHeaders)(this.config.headers(), options.headers),
398
+ headers: (0, import_provider_utils2.combineHeaders)(this.config.headers(), options.headers),
413
399
  body: {
414
400
  ...args,
415
401
  stream: true
416
402
  },
417
403
  failedResponseHandler: groqFailedResponseHandler,
418
- successfulResponseHandler: (0, import_provider_utils3.createEventSourceResponseHandler)(groqChatChunkSchema),
404
+ successfulResponseHandler: (0, import_provider_utils2.createEventSourceResponseHandler)(groqChatChunkSchema),
419
405
  abortSignal: options.abortSignal,
420
406
  fetch: this.config.fetch
421
407
  });
422
- const { messages: rawPrompt, ...rawSettings } = args;
423
408
  const toolCalls = [];
424
409
  let finishReason = "unknown";
425
- let usage = {
426
- promptTokens: void 0,
427
- completionTokens: void 0
410
+ const usage = {
411
+ inputTokens: void 0,
412
+ outputTokens: void 0,
413
+ totalTokens: void 0
428
414
  };
429
415
  let isFirstChunk = true;
430
416
  let providerMetadata;
431
417
  return {
432
418
  stream: response.pipeThrough(
433
419
  new TransformStream({
420
+ start(controller) {
421
+ controller.enqueue({ type: "stream-start", warnings });
422
+ },
434
423
  transform(chunk, controller) {
435
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o;
424
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p;
436
425
  if (!chunk.success) {
437
426
  finishReason = "error";
438
427
  controller.enqueue({ type: "error", error: chunk.error });
@@ -452,10 +441,9 @@ var GroqChatLanguageModel = class {
452
441
  });
453
442
  }
454
443
  if (((_a = value.x_groq) == null ? void 0 : _a.usage) != null) {
455
- usage = {
456
- promptTokens: (_b = value.x_groq.usage.prompt_tokens) != null ? _b : void 0,
457
- completionTokens: (_c = value.x_groq.usage.completion_tokens) != null ? _c : void 0
458
- };
444
+ usage.inputTokens = (_b = value.x_groq.usage.prompt_tokens) != null ? _b : void 0;
445
+ usage.outputTokens = (_c = value.x_groq.usage.completion_tokens) != null ? _c : void 0;
446
+ usage.totalTokens = (_d = value.x_groq.usage.total_tokens) != null ? _d : void 0;
459
447
  }
460
448
  const choice = value.choices[0];
461
449
  if ((choice == null ? void 0 : choice.finish_reason) != null) {
@@ -468,13 +456,13 @@ var GroqChatLanguageModel = class {
468
456
  if (delta.reasoning != null && delta.reasoning.length > 0) {
469
457
  controller.enqueue({
470
458
  type: "reasoning",
471
- textDelta: delta.reasoning
459
+ text: delta.reasoning
472
460
  });
473
461
  }
474
462
  if (delta.content != null && delta.content.length > 0) {
475
463
  controller.enqueue({
476
- type: "text-delta",
477
- textDelta: delta.content
464
+ type: "text",
465
+ text: delta.content
478
466
  });
479
467
  }
480
468
  if (delta.tool_calls != null) {
@@ -493,7 +481,7 @@ var GroqChatLanguageModel = class {
493
481
  message: `Expected 'id' to be a string.`
494
482
  });
495
483
  }
496
- if (((_d = toolCallDelta.function) == null ? void 0 : _d.name) == null) {
484
+ if (((_e = toolCallDelta.function) == null ? void 0 : _e.name) == null) {
497
485
  throw new import_provider3.InvalidResponseDataError({
498
486
  data: toolCallDelta,
499
487
  message: `Expected 'function.name' to be a string.`
@@ -504,12 +492,12 @@ var GroqChatLanguageModel = class {
504
492
  type: "function",
505
493
  function: {
506
494
  name: toolCallDelta.function.name,
507
- arguments: (_e = toolCallDelta.function.arguments) != null ? _e : ""
495
+ arguments: (_f = toolCallDelta.function.arguments) != null ? _f : ""
508
496
  },
509
497
  hasFinished: false
510
498
  };
511
499
  const toolCall2 = toolCalls[index];
512
- if (((_f = toolCall2.function) == null ? void 0 : _f.name) != null && ((_g = toolCall2.function) == null ? void 0 : _g.arguments) != null) {
500
+ if (((_g = toolCall2.function) == null ? void 0 : _g.name) != null && ((_h = toolCall2.function) == null ? void 0 : _h.arguments) != null) {
513
501
  if (toolCall2.function.arguments.length > 0) {
514
502
  controller.enqueue({
515
503
  type: "tool-call-delta",
@@ -519,11 +507,11 @@ var GroqChatLanguageModel = class {
519
507
  argsTextDelta: toolCall2.function.arguments
520
508
  });
521
509
  }
522
- if ((0, import_provider_utils3.isParsableJson)(toolCall2.function.arguments)) {
510
+ if ((0, import_provider_utils2.isParsableJson)(toolCall2.function.arguments)) {
523
511
  controller.enqueue({
524
512
  type: "tool-call",
525
513
  toolCallType: "function",
526
- toolCallId: (_h = toolCall2.id) != null ? _h : (0, import_provider_utils3.generateId)(),
514
+ toolCallId: (_i = toolCall2.id) != null ? _i : (0, import_provider_utils2.generateId)(),
527
515
  toolName: toolCall2.function.name,
528
516
  args: toolCall2.function.arguments
529
517
  });
@@ -536,21 +524,21 @@ var GroqChatLanguageModel = class {
536
524
  if (toolCall.hasFinished) {
537
525
  continue;
538
526
  }
539
- if (((_i = toolCallDelta.function) == null ? void 0 : _i.arguments) != null) {
540
- toolCall.function.arguments += (_k = (_j = toolCallDelta.function) == null ? void 0 : _j.arguments) != null ? _k : "";
527
+ if (((_j = toolCallDelta.function) == null ? void 0 : _j.arguments) != null) {
528
+ toolCall.function.arguments += (_l = (_k = toolCallDelta.function) == null ? void 0 : _k.arguments) != null ? _l : "";
541
529
  }
542
530
  controller.enqueue({
543
531
  type: "tool-call-delta",
544
532
  toolCallType: "function",
545
533
  toolCallId: toolCall.id,
546
534
  toolName: toolCall.function.name,
547
- argsTextDelta: (_l = toolCallDelta.function.arguments) != null ? _l : ""
535
+ argsTextDelta: (_m = toolCallDelta.function.arguments) != null ? _m : ""
548
536
  });
549
- if (((_m = toolCall.function) == null ? void 0 : _m.name) != null && ((_n = toolCall.function) == null ? void 0 : _n.arguments) != null && (0, import_provider_utils3.isParsableJson)(toolCall.function.arguments)) {
537
+ if (((_n = toolCall.function) == null ? void 0 : _n.name) != null && ((_o = toolCall.function) == null ? void 0 : _o.arguments) != null && (0, import_provider_utils2.isParsableJson)(toolCall.function.arguments)) {
550
538
  controller.enqueue({
551
539
  type: "tool-call",
552
540
  toolCallType: "function",
553
- toolCallId: (_o = toolCall.id) != null ? _o : (0, import_provider_utils3.generateId)(),
541
+ toolCallId: (_p = toolCall.id) != null ? _p : (0, import_provider_utils2.generateId)(),
554
542
  toolName: toolCall.function.name,
555
543
  args: toolCall.function.arguments
556
544
  });
@@ -560,91 +548,207 @@ var GroqChatLanguageModel = class {
560
548
  }
561
549
  },
562
550
  flush(controller) {
563
- var _a, _b;
564
551
  controller.enqueue({
565
552
  type: "finish",
566
553
  finishReason,
567
- usage: {
568
- promptTokens: (_a = usage.promptTokens) != null ? _a : NaN,
569
- completionTokens: (_b = usage.completionTokens) != null ? _b : NaN
570
- },
554
+ usage,
571
555
  ...providerMetadata != null ? { providerMetadata } : {}
572
556
  });
573
557
  }
574
558
  })
575
559
  ),
576
- rawCall: { rawPrompt, rawSettings },
577
- rawResponse: { headers: responseHeaders },
578
- warnings,
579
- request: { body }
560
+ request: { body },
561
+ response: { headers: responseHeaders }
580
562
  };
581
563
  }
582
564
  };
583
- var groqChatResponseSchema = import_zod2.z.object({
584
- id: import_zod2.z.string().nullish(),
585
- created: import_zod2.z.number().nullish(),
586
- model: import_zod2.z.string().nullish(),
587
- choices: import_zod2.z.array(
588
- import_zod2.z.object({
589
- message: import_zod2.z.object({
590
- content: import_zod2.z.string().nullish(),
591
- reasoning: import_zod2.z.string().nullish(),
592
- tool_calls: import_zod2.z.array(
593
- import_zod2.z.object({
594
- id: import_zod2.z.string().nullish(),
595
- type: import_zod2.z.literal("function"),
596
- function: import_zod2.z.object({
597
- name: import_zod2.z.string(),
598
- arguments: import_zod2.z.string()
565
+ var groqChatResponseSchema = import_zod3.z.object({
566
+ id: import_zod3.z.string().nullish(),
567
+ created: import_zod3.z.number().nullish(),
568
+ model: import_zod3.z.string().nullish(),
569
+ choices: import_zod3.z.array(
570
+ import_zod3.z.object({
571
+ message: import_zod3.z.object({
572
+ content: import_zod3.z.string().nullish(),
573
+ reasoning: import_zod3.z.string().nullish(),
574
+ tool_calls: import_zod3.z.array(
575
+ import_zod3.z.object({
576
+ id: import_zod3.z.string().nullish(),
577
+ type: import_zod3.z.literal("function"),
578
+ function: import_zod3.z.object({
579
+ name: import_zod3.z.string(),
580
+ arguments: import_zod3.z.string()
599
581
  })
600
582
  })
601
583
  ).nullish()
602
584
  }),
603
- index: import_zod2.z.number(),
604
- finish_reason: import_zod2.z.string().nullish()
585
+ index: import_zod3.z.number(),
586
+ finish_reason: import_zod3.z.string().nullish()
605
587
  })
606
588
  ),
607
- usage: import_zod2.z.object({
608
- prompt_tokens: import_zod2.z.number().nullish(),
609
- completion_tokens: import_zod2.z.number().nullish()
589
+ usage: import_zod3.z.object({
590
+ prompt_tokens: import_zod3.z.number().nullish(),
591
+ completion_tokens: import_zod3.z.number().nullish(),
592
+ total_tokens: import_zod3.z.number().nullish()
610
593
  }).nullish()
611
594
  });
612
- var groqChatChunkSchema = import_zod2.z.union([
613
- import_zod2.z.object({
614
- id: import_zod2.z.string().nullish(),
615
- created: import_zod2.z.number().nullish(),
616
- model: import_zod2.z.string().nullish(),
617
- choices: import_zod2.z.array(
618
- import_zod2.z.object({
619
- delta: import_zod2.z.object({
620
- content: import_zod2.z.string().nullish(),
621
- reasoning: import_zod2.z.string().nullish(),
622
- tool_calls: import_zod2.z.array(
623
- import_zod2.z.object({
624
- index: import_zod2.z.number(),
625
- id: import_zod2.z.string().nullish(),
626
- type: import_zod2.z.literal("function").optional(),
627
- function: import_zod2.z.object({
628
- name: import_zod2.z.string().nullish(),
629
- arguments: import_zod2.z.string().nullish()
595
+ var groqChatChunkSchema = import_zod3.z.union([
596
+ import_zod3.z.object({
597
+ id: import_zod3.z.string().nullish(),
598
+ created: import_zod3.z.number().nullish(),
599
+ model: import_zod3.z.string().nullish(),
600
+ choices: import_zod3.z.array(
601
+ import_zod3.z.object({
602
+ delta: import_zod3.z.object({
603
+ content: import_zod3.z.string().nullish(),
604
+ reasoning: import_zod3.z.string().nullish(),
605
+ tool_calls: import_zod3.z.array(
606
+ import_zod3.z.object({
607
+ index: import_zod3.z.number(),
608
+ id: import_zod3.z.string().nullish(),
609
+ type: import_zod3.z.literal("function").optional(),
610
+ function: import_zod3.z.object({
611
+ name: import_zod3.z.string().nullish(),
612
+ arguments: import_zod3.z.string().nullish()
630
613
  })
631
614
  })
632
615
  ).nullish()
633
616
  }).nullish(),
634
- finish_reason: import_zod2.z.string().nullable().optional(),
635
- index: import_zod2.z.number()
617
+ finish_reason: import_zod3.z.string().nullable().optional(),
618
+ index: import_zod3.z.number()
636
619
  })
637
620
  ),
638
- x_groq: import_zod2.z.object({
639
- usage: import_zod2.z.object({
640
- prompt_tokens: import_zod2.z.number().nullish(),
641
- completion_tokens: import_zod2.z.number().nullish()
621
+ x_groq: import_zod3.z.object({
622
+ usage: import_zod3.z.object({
623
+ prompt_tokens: import_zod3.z.number().nullish(),
624
+ completion_tokens: import_zod3.z.number().nullish(),
625
+ total_tokens: import_zod3.z.number().nullish()
642
626
  }).nullish()
643
627
  }).nullish()
644
628
  }),
645
629
  groqErrorDataSchema
646
630
  ]);
647
631
 
632
+ // src/groq-transcription-model.ts
633
+ var import_provider_utils3 = require("@ai-sdk/provider-utils");
634
+ var import_zod4 = require("zod");
635
+ var groqProviderOptionsSchema = import_zod4.z.object({
636
+ language: import_zod4.z.string().nullish(),
637
+ prompt: import_zod4.z.string().nullish(),
638
+ responseFormat: import_zod4.z.string().nullish(),
639
+ temperature: import_zod4.z.number().min(0).max(1).nullish(),
640
+ timestampGranularities: import_zod4.z.array(import_zod4.z.string()).nullish()
641
+ });
642
+ var GroqTranscriptionModel = class {
643
+ constructor(modelId, config) {
644
+ this.modelId = modelId;
645
+ this.config = config;
646
+ this.specificationVersion = "v1";
647
+ }
648
+ get provider() {
649
+ return this.config.provider;
650
+ }
651
+ async getArgs({
652
+ audio,
653
+ mediaType,
654
+ providerOptions
655
+ }) {
656
+ var _a, _b, _c, _d, _e;
657
+ const warnings = [];
658
+ const groqOptions = await (0, import_provider_utils3.parseProviderOptions)({
659
+ provider: "groq",
660
+ providerOptions,
661
+ schema: groqProviderOptionsSchema
662
+ });
663
+ const formData = new FormData();
664
+ const blob = audio instanceof Uint8Array ? new Blob([audio]) : new Blob([(0, import_provider_utils3.convertBase64ToUint8Array)(audio)]);
665
+ formData.append("model", this.modelId);
666
+ formData.append("file", new File([blob], "audio", { type: mediaType }));
667
+ if (groqOptions) {
668
+ const transcriptionModelOptions = {
669
+ language: (_a = groqOptions.language) != null ? _a : void 0,
670
+ prompt: (_b = groqOptions.prompt) != null ? _b : void 0,
671
+ response_format: (_c = groqOptions.responseFormat) != null ? _c : void 0,
672
+ temperature: (_d = groqOptions.temperature) != null ? _d : void 0,
673
+ timestamp_granularities: (_e = groqOptions.timestampGranularities) != null ? _e : void 0
674
+ };
675
+ for (const key in transcriptionModelOptions) {
676
+ const value = transcriptionModelOptions[key];
677
+ if (value !== void 0) {
678
+ formData.append(key, String(value));
679
+ }
680
+ }
681
+ }
682
+ return {
683
+ formData,
684
+ warnings
685
+ };
686
+ }
687
+ async doGenerate(options) {
688
+ var _a, _b, _c, _d, _e;
689
+ const currentDate = (_c = (_b = (_a = this.config._internal) == null ? void 0 : _a.currentDate) == null ? void 0 : _b.call(_a)) != null ? _c : /* @__PURE__ */ new Date();
690
+ const { formData, warnings } = await this.getArgs(options);
691
+ const {
692
+ value: response,
693
+ responseHeaders,
694
+ rawValue: rawResponse
695
+ } = await (0, import_provider_utils3.postFormDataToApi)({
696
+ url: this.config.url({
697
+ path: "/audio/transcriptions",
698
+ modelId: this.modelId
699
+ }),
700
+ headers: (0, import_provider_utils3.combineHeaders)(this.config.headers(), options.headers),
701
+ formData,
702
+ failedResponseHandler: groqFailedResponseHandler,
703
+ successfulResponseHandler: (0, import_provider_utils3.createJsonResponseHandler)(
704
+ groqTranscriptionResponseSchema
705
+ ),
706
+ abortSignal: options.abortSignal,
707
+ fetch: this.config.fetch
708
+ });
709
+ return {
710
+ text: response.text,
711
+ segments: (_e = (_d = response.segments) == null ? void 0 : _d.map((segment) => ({
712
+ text: segment.text,
713
+ startSecond: segment.start,
714
+ endSecond: segment.end
715
+ }))) != null ? _e : [],
716
+ language: response.language,
717
+ durationInSeconds: response.duration,
718
+ warnings,
719
+ response: {
720
+ timestamp: currentDate,
721
+ modelId: this.modelId,
722
+ headers: responseHeaders,
723
+ body: rawResponse
724
+ }
725
+ };
726
+ }
727
+ };
728
+ var groqTranscriptionResponseSchema = import_zod4.z.object({
729
+ task: import_zod4.z.string(),
730
+ language: import_zod4.z.string(),
731
+ duration: import_zod4.z.number(),
732
+ text: import_zod4.z.string(),
733
+ segments: import_zod4.z.array(
734
+ import_zod4.z.object({
735
+ id: import_zod4.z.number(),
736
+ seek: import_zod4.z.number(),
737
+ start: import_zod4.z.number(),
738
+ end: import_zod4.z.number(),
739
+ text: import_zod4.z.string(),
740
+ tokens: import_zod4.z.array(import_zod4.z.number()),
741
+ temperature: import_zod4.z.number(),
742
+ avg_logprob: import_zod4.z.number(),
743
+ compression_ratio: import_zod4.z.number(),
744
+ no_speech_prob: import_zod4.z.number()
745
+ })
746
+ ),
747
+ x_groq: import_zod4.z.object({
748
+ id: import_zod4.z.string()
749
+ })
750
+ });
751
+
648
752
  // src/groq-provider.ts
649
753
  function createGroq(options = {}) {
650
754
  var _a;
@@ -657,22 +761,30 @@ function createGroq(options = {}) {
657
761
  })}`,
658
762
  ...options.headers
659
763
  });
660
- const createChatModel = (modelId, settings = {}) => new GroqChatLanguageModel(modelId, settings, {
764
+ const createChatModel = (modelId) => new GroqChatLanguageModel(modelId, {
661
765
  provider: "groq.chat",
662
766
  url: ({ path }) => `${baseURL}${path}`,
663
767
  headers: getHeaders,
664
768
  fetch: options.fetch
665
769
  });
666
- const createLanguageModel = (modelId, settings) => {
770
+ const createLanguageModel = (modelId) => {
667
771
  if (new.target) {
668
772
  throw new Error(
669
773
  "The Groq model function cannot be called with the new keyword."
670
774
  );
671
775
  }
672
- return createChatModel(modelId, settings);
776
+ return createChatModel(modelId);
777
+ };
778
+ const createTranscriptionModel = (modelId) => {
779
+ return new GroqTranscriptionModel(modelId, {
780
+ provider: "groq.transcription",
781
+ url: ({ path }) => `${baseURL}${path}`,
782
+ headers: getHeaders,
783
+ fetch: options.fetch
784
+ });
673
785
  };
674
- const provider = function(modelId, settings) {
675
- return createLanguageModel(modelId, settings);
786
+ const provider = function(modelId) {
787
+ return createLanguageModel(modelId);
676
788
  };
677
789
  provider.languageModel = createLanguageModel;
678
790
  provider.chat = createChatModel;
@@ -682,6 +794,7 @@ function createGroq(options = {}) {
682
794
  provider.imageModel = (modelId) => {
683
795
  throw new import_provider4.NoSuchModelError({ modelId, modelType: "imageModel" });
684
796
  };
797
+ provider.transcription = createTranscriptionModel;
685
798
  return provider;
686
799
  }
687
800
  var groq = createGroq();