@mastra/server 0.20.1-alpha.3 → 0.20.1-alpha.4

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.
Files changed (48) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/dist/{chunk-TMU7NSW7.js → chunk-HKW2536J.js} +5852 -2557
  3. package/dist/chunk-HKW2536J.js.map +1 -0
  4. package/dist/{chunk-AFJBHPHA.cjs → chunk-KJJA7GPJ.cjs} +6308 -2994
  5. package/dist/chunk-KJJA7GPJ.cjs.map +1 -0
  6. package/dist/server/handlers/agent-builder.cjs +16 -16
  7. package/dist/server/handlers/agent-builder.js +1 -1
  8. package/dist/server/handlers.cjs +2 -2
  9. package/dist/server/handlers.js +1 -1
  10. package/package.json +5 -5
  11. package/dist/chunk-44GFD2TF.js +0 -419
  12. package/dist/chunk-44GFD2TF.js.map +0 -1
  13. package/dist/chunk-75KU7JB6.cjs +0 -588
  14. package/dist/chunk-75KU7JB6.cjs.map +0 -1
  15. package/dist/chunk-77TGJGDW.cjs +0 -1279
  16. package/dist/chunk-77TGJGDW.cjs.map +0 -1
  17. package/dist/chunk-AFJBHPHA.cjs.map +0 -1
  18. package/dist/chunk-CHDN4NEY.js +0 -1277
  19. package/dist/chunk-CHDN4NEY.js.map +0 -1
  20. package/dist/chunk-EAIAF7Z6.js +0 -571
  21. package/dist/chunk-EAIAF7Z6.js.map +0 -1
  22. package/dist/chunk-TMU7NSW7.js.map +0 -1
  23. package/dist/chunk-WO2SYFUI.js +0 -5945
  24. package/dist/chunk-WO2SYFUI.js.map +0 -1
  25. package/dist/chunk-XCR65STK.cjs +0 -433
  26. package/dist/chunk-XCR65STK.cjs.map +0 -1
  27. package/dist/chunk-YWACVZRO.cjs +0 -5985
  28. package/dist/chunk-YWACVZRO.cjs.map +0 -1
  29. package/dist/dist-36GPHJSB.cjs +0 -2014
  30. package/dist/dist-36GPHJSB.cjs.map +0 -1
  31. package/dist/dist-3WEYC4YO.js +0 -2007
  32. package/dist/dist-3WEYC4YO.js.map +0 -1
  33. package/dist/dist-7MBYKZSM.js +0 -846
  34. package/dist/dist-7MBYKZSM.js.map +0 -1
  35. package/dist/dist-FGYSUA65.cjs +0 -935
  36. package/dist/dist-FGYSUA65.cjs.map +0 -1
  37. package/dist/dist-FNKPY5I5.cjs +0 -1412
  38. package/dist/dist-FNKPY5I5.cjs.map +0 -1
  39. package/dist/dist-H5ZHQKYG.js +0 -3
  40. package/dist/dist-H5ZHQKYG.js.map +0 -1
  41. package/dist/dist-HBUYSRRO.cjs +0 -850
  42. package/dist/dist-HBUYSRRO.cjs.map +0 -1
  43. package/dist/dist-IUCBLZK6.js +0 -1409
  44. package/dist/dist-IUCBLZK6.js.map +0 -1
  45. package/dist/dist-M4HXCUXC.cjs +0 -20
  46. package/dist/dist-M4HXCUXC.cjs.map +0 -1
  47. package/dist/dist-P32YPL35.js +0 -932
  48. package/dist/dist-P32YPL35.js.map +0 -1
@@ -1,846 +0,0 @@
1
- import { createJsonErrorResponseHandler, createProviderDefinedToolFactory, withoutTrailingSlash, loadApiKey, parseProviderOptions, postJsonToApi, createJsonResponseHandler, combineHeaders, generateId, createEventSourceResponseHandler, isParsableJson, convertToBase64, convertBase64ToUint8Array, postFormDataToApi } from './chunk-EAIAF7Z6.js';
2
- import { InvalidResponseDataError, UnsupportedFunctionalityError, NoSuchModelError } from './chunk-44GFD2TF.js';
3
- import { z } from 'zod/v4';
4
-
5
- function convertToGroqChatMessages(prompt) {
6
- const messages = [];
7
- for (const { role, content } of prompt) {
8
- switch (role) {
9
- case "system": {
10
- messages.push({ role: "system", content });
11
- break;
12
- }
13
- case "user": {
14
- if (content.length === 1 && content[0].type === "text") {
15
- messages.push({ role: "user", content: content[0].text });
16
- break;
17
- }
18
- messages.push({
19
- role: "user",
20
- content: content.map((part) => {
21
- switch (part.type) {
22
- case "text": {
23
- return { type: "text", text: part.text };
24
- }
25
- case "file": {
26
- if (!part.mediaType.startsWith("image/")) {
27
- throw new UnsupportedFunctionalityError({
28
- functionality: "Non-image file content parts"
29
- });
30
- }
31
- const mediaType = part.mediaType === "image/*" ? "image/jpeg" : part.mediaType;
32
- return {
33
- type: "image_url",
34
- image_url: {
35
- url: part.data instanceof URL ? part.data.toString() : `data:${mediaType};base64,${convertToBase64(part.data)}`
36
- }
37
- };
38
- }
39
- }
40
- })
41
- });
42
- break;
43
- }
44
- case "assistant": {
45
- let text = "";
46
- let reasoning = "";
47
- const toolCalls = [];
48
- for (const part of content) {
49
- switch (part.type) {
50
- // groq supports reasoning for tool-calls in multi-turn conversations
51
- // https://github.com/vercel/ai/issues/7860
52
- case "reasoning": {
53
- reasoning += part.text;
54
- break;
55
- }
56
- case "text": {
57
- text += part.text;
58
- break;
59
- }
60
- case "tool-call": {
61
- toolCalls.push({
62
- id: part.toolCallId,
63
- type: "function",
64
- function: {
65
- name: part.toolName,
66
- arguments: JSON.stringify(part.input)
67
- }
68
- });
69
- break;
70
- }
71
- }
72
- }
73
- messages.push({
74
- role: "assistant",
75
- content: text,
76
- ...reasoning.length > 0 ? { reasoning } : null,
77
- ...toolCalls.length > 0 ? { tool_calls: toolCalls } : null
78
- });
79
- break;
80
- }
81
- case "tool": {
82
- for (const toolResponse of content) {
83
- const output = toolResponse.output;
84
- let contentValue;
85
- switch (output.type) {
86
- case "text":
87
- case "error-text":
88
- contentValue = output.value;
89
- break;
90
- case "content":
91
- case "json":
92
- case "error-json":
93
- contentValue = JSON.stringify(output.value);
94
- break;
95
- }
96
- messages.push({
97
- role: "tool",
98
- tool_call_id: toolResponse.toolCallId,
99
- content: contentValue
100
- });
101
- }
102
- break;
103
- }
104
- default: {
105
- const _exhaustiveCheck = role;
106
- throw new Error(`Unsupported role: ${_exhaustiveCheck}`);
107
- }
108
- }
109
- }
110
- return messages;
111
- }
112
- function getResponseMetadata({
113
- id,
114
- model,
115
- created
116
- }) {
117
- return {
118
- id: id != null ? id : void 0,
119
- modelId: model != null ? model : void 0,
120
- timestamp: created != null ? new Date(created * 1e3) : void 0
121
- };
122
- }
123
- var groqProviderOptions = z.object({
124
- reasoningFormat: z.enum(["parsed", "raw", "hidden"]).optional(),
125
- reasoningEffort: z.string().optional(),
126
- /**
127
- * Whether to enable parallel function calling during tool use. Default to true.
128
- */
129
- parallelToolCalls: z.boolean().optional(),
130
- /**
131
- * A unique identifier representing your end-user, which can help OpenAI to
132
- * monitor and detect abuse. Learn more.
133
- */
134
- user: z.string().optional(),
135
- /**
136
- * Whether to use structured outputs.
137
- *
138
- * @default true
139
- */
140
- structuredOutputs: z.boolean().optional()
141
- });
142
- var groqErrorDataSchema = z.object({
143
- error: z.object({
144
- message: z.string(),
145
- type: z.string()
146
- })
147
- });
148
- var groqFailedResponseHandler = createJsonErrorResponseHandler({
149
- errorSchema: groqErrorDataSchema,
150
- errorToMessage: (data) => data.error.message
151
- });
152
- var BROWSER_SEARCH_SUPPORTED_MODELS = [
153
- "openai/gpt-oss-20b",
154
- "openai/gpt-oss-120b"
155
- ];
156
- function isBrowserSearchSupportedModel(modelId) {
157
- return BROWSER_SEARCH_SUPPORTED_MODELS.includes(modelId);
158
- }
159
- function getSupportedModelsString() {
160
- return BROWSER_SEARCH_SUPPORTED_MODELS.join(", ");
161
- }
162
- function prepareTools({
163
- tools,
164
- toolChoice,
165
- modelId
166
- }) {
167
- tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
168
- const toolWarnings = [];
169
- if (tools == null) {
170
- return { tools: void 0, toolChoice: void 0, toolWarnings };
171
- }
172
- const groqTools2 = [];
173
- for (const tool of tools) {
174
- if (tool.type === "provider-defined") {
175
- if (tool.id === "groq.browser_search") {
176
- if (!isBrowserSearchSupportedModel(modelId)) {
177
- toolWarnings.push({
178
- type: "unsupported-tool",
179
- tool,
180
- details: `Browser search is only supported on the following models: ${getSupportedModelsString()}. Current model: ${modelId}`
181
- });
182
- } else {
183
- groqTools2.push({
184
- type: "browser_search"
185
- });
186
- }
187
- } else {
188
- toolWarnings.push({ type: "unsupported-tool", tool });
189
- }
190
- } else {
191
- groqTools2.push({
192
- type: "function",
193
- function: {
194
- name: tool.name,
195
- description: tool.description,
196
- parameters: tool.inputSchema
197
- }
198
- });
199
- }
200
- }
201
- if (toolChoice == null) {
202
- return { tools: groqTools2, toolChoice: void 0, toolWarnings };
203
- }
204
- const type = toolChoice.type;
205
- switch (type) {
206
- case "auto":
207
- case "none":
208
- case "required":
209
- return { tools: groqTools2, toolChoice: type, toolWarnings };
210
- case "tool":
211
- return {
212
- tools: groqTools2,
213
- toolChoice: {
214
- type: "function",
215
- function: {
216
- name: toolChoice.toolName
217
- }
218
- },
219
- toolWarnings
220
- };
221
- default: {
222
- const _exhaustiveCheck = type;
223
- throw new UnsupportedFunctionalityError({
224
- functionality: `tool choice type: ${_exhaustiveCheck}`
225
- });
226
- }
227
- }
228
- }
229
- function mapGroqFinishReason(finishReason) {
230
- switch (finishReason) {
231
- case "stop":
232
- return "stop";
233
- case "length":
234
- return "length";
235
- case "content_filter":
236
- return "content-filter";
237
- case "function_call":
238
- case "tool_calls":
239
- return "tool-calls";
240
- default:
241
- return "unknown";
242
- }
243
- }
244
- var GroqChatLanguageModel = class {
245
- constructor(modelId, config) {
246
- this.specificationVersion = "v2";
247
- this.supportedUrls = {
248
- "image/*": [/^https?:\/\/.*$/]
249
- };
250
- this.modelId = modelId;
251
- this.config = config;
252
- }
253
- get provider() {
254
- return this.config.provider;
255
- }
256
- async getArgs({
257
- prompt,
258
- maxOutputTokens,
259
- temperature,
260
- topP,
261
- topK,
262
- frequencyPenalty,
263
- presencePenalty,
264
- stopSequences,
265
- responseFormat,
266
- seed,
267
- stream,
268
- tools,
269
- toolChoice,
270
- providerOptions
271
- }) {
272
- var _a, _b;
273
- const warnings = [];
274
- const groqOptions = await parseProviderOptions({
275
- provider: "groq",
276
- providerOptions,
277
- schema: groqProviderOptions
278
- });
279
- const structuredOutputs = (_a = groqOptions == null ? void 0 : groqOptions.structuredOutputs) != null ? _a : true;
280
- if (topK != null) {
281
- warnings.push({
282
- type: "unsupported-setting",
283
- setting: "topK"
284
- });
285
- }
286
- if ((responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null && !structuredOutputs) {
287
- warnings.push({
288
- type: "unsupported-setting",
289
- setting: "responseFormat",
290
- details: "JSON response format schema is only supported with structuredOutputs"
291
- });
292
- }
293
- const {
294
- tools: groqTools2,
295
- toolChoice: groqToolChoice,
296
- toolWarnings
297
- } = prepareTools({ tools, toolChoice, modelId: this.modelId });
298
- return {
299
- args: {
300
- // model id:
301
- model: this.modelId,
302
- // model specific settings:
303
- user: groqOptions == null ? void 0 : groqOptions.user,
304
- parallel_tool_calls: groqOptions == null ? void 0 : groqOptions.parallelToolCalls,
305
- // standardized settings:
306
- max_tokens: maxOutputTokens,
307
- temperature,
308
- top_p: topP,
309
- frequency_penalty: frequencyPenalty,
310
- presence_penalty: presencePenalty,
311
- stop: stopSequences,
312
- seed,
313
- // response format:
314
- response_format: (responseFormat == null ? void 0 : responseFormat.type) === "json" ? structuredOutputs && responseFormat.schema != null ? {
315
- type: "json_schema",
316
- json_schema: {
317
- schema: responseFormat.schema,
318
- name: (_b = responseFormat.name) != null ? _b : "response",
319
- description: responseFormat.description
320
- }
321
- } : { type: "json_object" } : void 0,
322
- // provider options:
323
- reasoning_format: groqOptions == null ? void 0 : groqOptions.reasoningFormat,
324
- reasoning_effort: groqOptions == null ? void 0 : groqOptions.reasoningEffort,
325
- // messages:
326
- messages: convertToGroqChatMessages(prompt),
327
- // tools:
328
- tools: groqTools2,
329
- tool_choice: groqToolChoice
330
- },
331
- warnings: [...warnings, ...toolWarnings]
332
- };
333
- }
334
- async doGenerate(options) {
335
- var _a, _b, _c, _d, _e, _f, _g;
336
- const { args, warnings } = await this.getArgs({
337
- ...options,
338
- stream: false
339
- });
340
- const body = JSON.stringify(args);
341
- const {
342
- responseHeaders,
343
- value: response,
344
- rawValue: rawResponse
345
- } = await postJsonToApi({
346
- url: this.config.url({
347
- path: "/chat/completions",
348
- modelId: this.modelId
349
- }),
350
- headers: combineHeaders(this.config.headers(), options.headers),
351
- body: args,
352
- failedResponseHandler: groqFailedResponseHandler,
353
- successfulResponseHandler: createJsonResponseHandler(
354
- groqChatResponseSchema
355
- ),
356
- abortSignal: options.abortSignal,
357
- fetch: this.config.fetch
358
- });
359
- const choice = response.choices[0];
360
- const content = [];
361
- const text = choice.message.content;
362
- if (text != null && text.length > 0) {
363
- content.push({ type: "text", text });
364
- }
365
- const reasoning = choice.message.reasoning;
366
- if (reasoning != null && reasoning.length > 0) {
367
- content.push({
368
- type: "reasoning",
369
- text: reasoning
370
- });
371
- }
372
- if (choice.message.tool_calls != null) {
373
- for (const toolCall of choice.message.tool_calls) {
374
- content.push({
375
- type: "tool-call",
376
- toolCallId: (_a = toolCall.id) != null ? _a : generateId(),
377
- toolName: toolCall.function.name,
378
- input: toolCall.function.arguments
379
- });
380
- }
381
- }
382
- return {
383
- content,
384
- finishReason: mapGroqFinishReason(choice.finish_reason),
385
- usage: {
386
- inputTokens: (_c = (_b = response.usage) == null ? void 0 : _b.prompt_tokens) != null ? _c : void 0,
387
- outputTokens: (_e = (_d = response.usage) == null ? void 0 : _d.completion_tokens) != null ? _e : void 0,
388
- totalTokens: (_g = (_f = response.usage) == null ? void 0 : _f.total_tokens) != null ? _g : void 0
389
- },
390
- response: {
391
- ...getResponseMetadata(response),
392
- headers: responseHeaders,
393
- body: rawResponse
394
- },
395
- warnings,
396
- request: { body }
397
- };
398
- }
399
- async doStream(options) {
400
- const { args, warnings } = await this.getArgs({ ...options, stream: true });
401
- const body = JSON.stringify({ ...args, stream: true });
402
- const { responseHeaders, value: response } = await postJsonToApi({
403
- url: this.config.url({
404
- path: "/chat/completions",
405
- modelId: this.modelId
406
- }),
407
- headers: combineHeaders(this.config.headers(), options.headers),
408
- body: {
409
- ...args,
410
- stream: true
411
- },
412
- failedResponseHandler: groqFailedResponseHandler,
413
- successfulResponseHandler: createEventSourceResponseHandler(groqChatChunkSchema),
414
- abortSignal: options.abortSignal,
415
- fetch: this.config.fetch
416
- });
417
- const toolCalls = [];
418
- let finishReason = "unknown";
419
- const usage = {
420
- inputTokens: void 0,
421
- outputTokens: void 0,
422
- totalTokens: void 0
423
- };
424
- let isFirstChunk = true;
425
- let isActiveText = false;
426
- let isActiveReasoning = false;
427
- return {
428
- stream: response.pipeThrough(
429
- new TransformStream({
430
- start(controller) {
431
- controller.enqueue({ type: "stream-start", warnings });
432
- },
433
- transform(chunk, controller) {
434
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p;
435
- if (options.includeRawChunks) {
436
- controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
437
- }
438
- if (!chunk.success) {
439
- finishReason = "error";
440
- controller.enqueue({ type: "error", error: chunk.error });
441
- return;
442
- }
443
- const value = chunk.value;
444
- if ("error" in value) {
445
- finishReason = "error";
446
- controller.enqueue({ type: "error", error: value.error });
447
- return;
448
- }
449
- if (isFirstChunk) {
450
- isFirstChunk = false;
451
- controller.enqueue({
452
- type: "response-metadata",
453
- ...getResponseMetadata(value)
454
- });
455
- }
456
- if (((_a = value.x_groq) == null ? void 0 : _a.usage) != null) {
457
- usage.inputTokens = (_b = value.x_groq.usage.prompt_tokens) != null ? _b : void 0;
458
- usage.outputTokens = (_c = value.x_groq.usage.completion_tokens) != null ? _c : void 0;
459
- usage.totalTokens = (_d = value.x_groq.usage.total_tokens) != null ? _d : void 0;
460
- }
461
- const choice = value.choices[0];
462
- if ((choice == null ? void 0 : choice.finish_reason) != null) {
463
- finishReason = mapGroqFinishReason(choice.finish_reason);
464
- }
465
- if ((choice == null ? void 0 : choice.delta) == null) {
466
- return;
467
- }
468
- const delta = choice.delta;
469
- if (delta.reasoning != null && delta.reasoning.length > 0) {
470
- if (!isActiveReasoning) {
471
- controller.enqueue({
472
- type: "reasoning-start",
473
- id: "reasoning-0"
474
- });
475
- isActiveReasoning = true;
476
- }
477
- controller.enqueue({
478
- type: "reasoning-delta",
479
- id: "reasoning-0",
480
- delta: delta.reasoning
481
- });
482
- }
483
- if (delta.content != null && delta.content.length > 0) {
484
- if (!isActiveText) {
485
- controller.enqueue({ type: "text-start", id: "txt-0" });
486
- isActiveText = true;
487
- }
488
- controller.enqueue({
489
- type: "text-delta",
490
- id: "txt-0",
491
- delta: delta.content
492
- });
493
- }
494
- if (delta.tool_calls != null) {
495
- for (const toolCallDelta of delta.tool_calls) {
496
- const index = toolCallDelta.index;
497
- if (toolCalls[index] == null) {
498
- if (toolCallDelta.type !== "function") {
499
- throw new InvalidResponseDataError({
500
- data: toolCallDelta,
501
- message: `Expected 'function' type.`
502
- });
503
- }
504
- if (toolCallDelta.id == null) {
505
- throw new InvalidResponseDataError({
506
- data: toolCallDelta,
507
- message: `Expected 'id' to be a string.`
508
- });
509
- }
510
- if (((_e = toolCallDelta.function) == null ? void 0 : _e.name) == null) {
511
- throw new InvalidResponseDataError({
512
- data: toolCallDelta,
513
- message: `Expected 'function.name' to be a string.`
514
- });
515
- }
516
- controller.enqueue({
517
- type: "tool-input-start",
518
- id: toolCallDelta.id,
519
- toolName: toolCallDelta.function.name
520
- });
521
- toolCalls[index] = {
522
- id: toolCallDelta.id,
523
- type: "function",
524
- function: {
525
- name: toolCallDelta.function.name,
526
- arguments: (_f = toolCallDelta.function.arguments) != null ? _f : ""
527
- },
528
- hasFinished: false
529
- };
530
- const toolCall2 = toolCalls[index];
531
- if (((_g = toolCall2.function) == null ? void 0 : _g.name) != null && ((_h = toolCall2.function) == null ? void 0 : _h.arguments) != null) {
532
- if (toolCall2.function.arguments.length > 0) {
533
- controller.enqueue({
534
- type: "tool-input-delta",
535
- id: toolCall2.id,
536
- delta: toolCall2.function.arguments
537
- });
538
- }
539
- if (isParsableJson(toolCall2.function.arguments)) {
540
- controller.enqueue({
541
- type: "tool-input-end",
542
- id: toolCall2.id
543
- });
544
- controller.enqueue({
545
- type: "tool-call",
546
- toolCallId: (_i = toolCall2.id) != null ? _i : generateId(),
547
- toolName: toolCall2.function.name,
548
- input: toolCall2.function.arguments
549
- });
550
- toolCall2.hasFinished = true;
551
- }
552
- }
553
- continue;
554
- }
555
- const toolCall = toolCalls[index];
556
- if (toolCall.hasFinished) {
557
- continue;
558
- }
559
- if (((_j = toolCallDelta.function) == null ? void 0 : _j.arguments) != null) {
560
- toolCall.function.arguments += (_l = (_k = toolCallDelta.function) == null ? void 0 : _k.arguments) != null ? _l : "";
561
- }
562
- controller.enqueue({
563
- type: "tool-input-delta",
564
- id: toolCall.id,
565
- delta: (_m = toolCallDelta.function.arguments) != null ? _m : ""
566
- });
567
- if (((_n = toolCall.function) == null ? void 0 : _n.name) != null && ((_o = toolCall.function) == null ? void 0 : _o.arguments) != null && isParsableJson(toolCall.function.arguments)) {
568
- controller.enqueue({
569
- type: "tool-input-end",
570
- id: toolCall.id
571
- });
572
- controller.enqueue({
573
- type: "tool-call",
574
- toolCallId: (_p = toolCall.id) != null ? _p : generateId(),
575
- toolName: toolCall.function.name,
576
- input: toolCall.function.arguments
577
- });
578
- toolCall.hasFinished = true;
579
- }
580
- }
581
- }
582
- },
583
- flush(controller) {
584
- if (isActiveReasoning) {
585
- controller.enqueue({ type: "reasoning-end", id: "reasoning-0" });
586
- }
587
- if (isActiveText) {
588
- controller.enqueue({ type: "text-end", id: "txt-0" });
589
- }
590
- controller.enqueue({
591
- type: "finish",
592
- finishReason,
593
- usage,
594
- ...{}
595
- });
596
- }
597
- })
598
- ),
599
- request: { body },
600
- response: { headers: responseHeaders }
601
- };
602
- }
603
- };
604
- var groqChatResponseSchema = z.object({
605
- id: z.string().nullish(),
606
- created: z.number().nullish(),
607
- model: z.string().nullish(),
608
- choices: z.array(
609
- z.object({
610
- message: z.object({
611
- content: z.string().nullish(),
612
- reasoning: z.string().nullish(),
613
- tool_calls: z.array(
614
- z.object({
615
- id: z.string().nullish(),
616
- type: z.literal("function"),
617
- function: z.object({
618
- name: z.string(),
619
- arguments: z.string()
620
- })
621
- })
622
- ).nullish()
623
- }),
624
- index: z.number(),
625
- finish_reason: z.string().nullish()
626
- })
627
- ),
628
- usage: z.object({
629
- prompt_tokens: z.number().nullish(),
630
- completion_tokens: z.number().nullish(),
631
- total_tokens: z.number().nullish()
632
- }).nullish()
633
- });
634
- var groqChatChunkSchema = z.union([
635
- z.object({
636
- id: z.string().nullish(),
637
- created: z.number().nullish(),
638
- model: z.string().nullish(),
639
- choices: z.array(
640
- z.object({
641
- delta: z.object({
642
- content: z.string().nullish(),
643
- reasoning: z.string().nullish(),
644
- tool_calls: z.array(
645
- z.object({
646
- index: z.number(),
647
- id: z.string().nullish(),
648
- type: z.literal("function").optional(),
649
- function: z.object({
650
- name: z.string().nullish(),
651
- arguments: z.string().nullish()
652
- })
653
- })
654
- ).nullish()
655
- }).nullish(),
656
- finish_reason: z.string().nullable().optional(),
657
- index: z.number()
658
- })
659
- ),
660
- x_groq: z.object({
661
- usage: z.object({
662
- prompt_tokens: z.number().nullish(),
663
- completion_tokens: z.number().nullish(),
664
- total_tokens: z.number().nullish()
665
- }).nullish()
666
- }).nullish()
667
- }),
668
- groqErrorDataSchema
669
- ]);
670
- var groqProviderOptionsSchema = z.object({
671
- language: z.string().nullish(),
672
- prompt: z.string().nullish(),
673
- responseFormat: z.string().nullish(),
674
- temperature: z.number().min(0).max(1).nullish(),
675
- timestampGranularities: z.array(z.string()).nullish()
676
- });
677
- var GroqTranscriptionModel = class {
678
- constructor(modelId, config) {
679
- this.modelId = modelId;
680
- this.config = config;
681
- this.specificationVersion = "v2";
682
- }
683
- get provider() {
684
- return this.config.provider;
685
- }
686
- async getArgs({
687
- audio,
688
- mediaType,
689
- providerOptions
690
- }) {
691
- var _a, _b, _c, _d, _e;
692
- const warnings = [];
693
- const groqOptions = await parseProviderOptions({
694
- provider: "groq",
695
- providerOptions,
696
- schema: groqProviderOptionsSchema
697
- });
698
- const formData = new FormData();
699
- const blob = audio instanceof Uint8Array ? new Blob([audio]) : new Blob([convertBase64ToUint8Array(audio)]);
700
- formData.append("model", this.modelId);
701
- formData.append("file", new File([blob], "audio", { type: mediaType }));
702
- if (groqOptions) {
703
- const transcriptionModelOptions = {
704
- language: (_a = groqOptions.language) != null ? _a : void 0,
705
- prompt: (_b = groqOptions.prompt) != null ? _b : void 0,
706
- response_format: (_c = groqOptions.responseFormat) != null ? _c : void 0,
707
- temperature: (_d = groqOptions.temperature) != null ? _d : void 0,
708
- timestamp_granularities: (_e = groqOptions.timestampGranularities) != null ? _e : void 0
709
- };
710
- for (const key in transcriptionModelOptions) {
711
- const value = transcriptionModelOptions[key];
712
- if (value !== void 0) {
713
- formData.append(key, String(value));
714
- }
715
- }
716
- }
717
- return {
718
- formData,
719
- warnings
720
- };
721
- }
722
- async doGenerate(options) {
723
- var _a, _b, _c, _d, _e;
724
- const currentDate = (_c = (_b = (_a = this.config._internal) == null ? void 0 : _a.currentDate) == null ? void 0 : _b.call(_a)) != null ? _c : /* @__PURE__ */ new Date();
725
- const { formData, warnings } = await this.getArgs(options);
726
- const {
727
- value: response,
728
- responseHeaders,
729
- rawValue: rawResponse
730
- } = await postFormDataToApi({
731
- url: this.config.url({
732
- path: "/audio/transcriptions",
733
- modelId: this.modelId
734
- }),
735
- headers: combineHeaders(this.config.headers(), options.headers),
736
- formData,
737
- failedResponseHandler: groqFailedResponseHandler,
738
- successfulResponseHandler: createJsonResponseHandler(
739
- groqTranscriptionResponseSchema
740
- ),
741
- abortSignal: options.abortSignal,
742
- fetch: this.config.fetch
743
- });
744
- return {
745
- text: response.text,
746
- segments: (_e = (_d = response.segments) == null ? void 0 : _d.map((segment) => ({
747
- text: segment.text,
748
- startSecond: segment.start,
749
- endSecond: segment.end
750
- }))) != null ? _e : [],
751
- language: response.language,
752
- durationInSeconds: response.duration,
753
- warnings,
754
- response: {
755
- timestamp: currentDate,
756
- modelId: this.modelId,
757
- headers: responseHeaders,
758
- body: rawResponse
759
- }
760
- };
761
- }
762
- };
763
- var groqTranscriptionResponseSchema = z.object({
764
- task: z.string(),
765
- language: z.string(),
766
- duration: z.number(),
767
- text: z.string(),
768
- segments: z.array(
769
- z.object({
770
- id: z.number(),
771
- seek: z.number(),
772
- start: z.number(),
773
- end: z.number(),
774
- text: z.string(),
775
- tokens: z.array(z.number()),
776
- temperature: z.number(),
777
- avg_logprob: z.number(),
778
- compression_ratio: z.number(),
779
- no_speech_prob: z.number()
780
- })
781
- ),
782
- x_groq: z.object({
783
- id: z.string()
784
- })
785
- });
786
- var browserSearch = createProviderDefinedToolFactory({
787
- id: "groq.browser_search",
788
- name: "browser_search",
789
- inputSchema: z.object({})
790
- });
791
- var groqTools = {
792
- browserSearch
793
- };
794
- function createGroq(options = {}) {
795
- var _a;
796
- const baseURL = (_a = withoutTrailingSlash(options.baseURL)) != null ? _a : "https://api.groq.com/openai/v1";
797
- const getHeaders = () => ({
798
- Authorization: `Bearer ${loadApiKey({
799
- apiKey: options.apiKey,
800
- environmentVariableName: "GROQ_API_KEY",
801
- description: "Groq"
802
- })}`,
803
- ...options.headers
804
- });
805
- const createChatModel = (modelId) => new GroqChatLanguageModel(modelId, {
806
- provider: "groq.chat",
807
- url: ({ path }) => `${baseURL}${path}`,
808
- headers: getHeaders,
809
- fetch: options.fetch
810
- });
811
- const createLanguageModel = (modelId) => {
812
- if (new.target) {
813
- throw new Error(
814
- "The Groq model function cannot be called with the new keyword."
815
- );
816
- }
817
- return createChatModel(modelId);
818
- };
819
- const createTranscriptionModel = (modelId) => {
820
- return new GroqTranscriptionModel(modelId, {
821
- provider: "groq.transcription",
822
- url: ({ path }) => `${baseURL}${path}`,
823
- headers: getHeaders,
824
- fetch: options.fetch
825
- });
826
- };
827
- const provider = function(modelId) {
828
- return createLanguageModel(modelId);
829
- };
830
- provider.languageModel = createLanguageModel;
831
- provider.chat = createChatModel;
832
- provider.textEmbeddingModel = (modelId) => {
833
- throw new NoSuchModelError({ modelId, modelType: "textEmbeddingModel" });
834
- };
835
- provider.imageModel = (modelId) => {
836
- throw new NoSuchModelError({ modelId, modelType: "imageModel" });
837
- };
838
- provider.transcription = createTranscriptionModel;
839
- provider.tools = groqTools;
840
- return provider;
841
- }
842
- var groq = createGroq();
843
-
844
- export { browserSearch, createGroq, groq };
845
- //# sourceMappingURL=dist-7MBYKZSM.js.map
846
- //# sourceMappingURL=dist-7MBYKZSM.js.map