sarvam-ai-sdk 0.0.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 ADDED
@@ -0,0 +1,1037 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __defProps = Object.defineProperties;
3
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
4
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
7
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
8
+ var __spreadValues = (a, b) => {
9
+ for (var prop in b || (b = {}))
10
+ if (__hasOwnProp.call(b, prop))
11
+ __defNormalProp(a, prop, b[prop]);
12
+ if (__getOwnPropSymbols)
13
+ for (var prop of __getOwnPropSymbols(b)) {
14
+ if (__propIsEnum.call(b, prop))
15
+ __defNormalProp(a, prop, b[prop]);
16
+ }
17
+ return a;
18
+ };
19
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
20
+ var __objRest = (source, exclude) => {
21
+ var target = {};
22
+ for (var prop in source)
23
+ if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
24
+ target[prop] = source[prop];
25
+ if (source != null && __getOwnPropSymbols)
26
+ for (var prop of __getOwnPropSymbols(source)) {
27
+ if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
28
+ target[prop] = source[prop];
29
+ }
30
+ return target;
31
+ };
32
+
33
+ // src/sarvam-provider.ts
34
+ import {
35
+ loadApiKey,
36
+ withoutTrailingSlash
37
+ } from "@ai-sdk/provider-utils";
38
+
39
+ // src/sarvam-chat-language-model.ts
40
+ import {
41
+ InvalidResponseDataError
42
+ } from "@ai-sdk/provider";
43
+ import {
44
+ combineHeaders,
45
+ createEventSourceResponseHandler,
46
+ createJsonResponseHandler,
47
+ generateId,
48
+ isParsableJson,
49
+ parseProviderOptions,
50
+ postJsonToApi
51
+ } from "@ai-sdk/provider-utils";
52
+ import { z as z2 } from "zod";
53
+
54
+ // src/convert-to-sarvam-chat-messages.ts
55
+ import {
56
+ UnsupportedFunctionalityError
57
+ } from "@ai-sdk/provider";
58
+ import { convertUint8ArrayToBase64 } from "@ai-sdk/provider-utils";
59
+ function convertToSarvamChatMessages(prompt) {
60
+ const messages = [];
61
+ for (const { role, content } of prompt) {
62
+ switch (role) {
63
+ case "system": {
64
+ messages.push({ role: "system", content });
65
+ break;
66
+ }
67
+ case "user": {
68
+ if (content.length === 1 && content[0].type === "text") {
69
+ messages.push({ role: "user", content: content[0].text });
70
+ break;
71
+ }
72
+ messages.push({
73
+ role: "user",
74
+ content: content.map((part) => {
75
+ var _a;
76
+ switch (part.type) {
77
+ case "text": {
78
+ return { type: "text", text: part.text };
79
+ }
80
+ case "image": {
81
+ return {
82
+ type: "image_url",
83
+ image_url: {
84
+ url: part.image instanceof URL ? part.image.toString() : `data:${(_a = part.mimeType) != null ? _a : "image/jpeg"};base64,${convertUint8ArrayToBase64(part.image)}`
85
+ }
86
+ };
87
+ }
88
+ case "file": {
89
+ throw new UnsupportedFunctionalityError({
90
+ functionality: "File content parts in user messages"
91
+ });
92
+ }
93
+ }
94
+ })
95
+ });
96
+ break;
97
+ }
98
+ case "assistant": {
99
+ let text = "";
100
+ const toolCalls = [];
101
+ for (const part of content) {
102
+ switch (part.type) {
103
+ case "text": {
104
+ text += part.text;
105
+ break;
106
+ }
107
+ case "tool-call": {
108
+ toolCalls.push({
109
+ id: part.toolCallId,
110
+ type: "function",
111
+ function: {
112
+ name: part.toolName,
113
+ arguments: JSON.stringify(part.args)
114
+ }
115
+ });
116
+ break;
117
+ }
118
+ }
119
+ }
120
+ messages.push({
121
+ role: "assistant",
122
+ content: text,
123
+ tool_calls: toolCalls.length > 0 ? toolCalls : void 0
124
+ });
125
+ break;
126
+ }
127
+ case "tool": {
128
+ for (const toolResponse of content) {
129
+ messages.push({
130
+ role: "tool",
131
+ tool_call_id: toolResponse.toolCallId,
132
+ content: JSON.stringify(toolResponse.result)
133
+ });
134
+ }
135
+ break;
136
+ }
137
+ default: {
138
+ const _exhaustiveCheck = role;
139
+ throw new Error(`Unsupported role: ${_exhaustiveCheck}`);
140
+ }
141
+ }
142
+ }
143
+ return messages;
144
+ }
145
+
146
+ // src/get-response-metadata.ts
147
+ function getResponseMetadata({
148
+ id,
149
+ model,
150
+ created
151
+ }) {
152
+ return {
153
+ id: id != null ? id : void 0,
154
+ modelId: model != null ? model : void 0,
155
+ timestamp: created != null ? new Date(created * 1e3) : void 0
156
+ };
157
+ }
158
+
159
+ // src/sarvam-error.ts
160
+ import { z } from "zod";
161
+ import { createJsonErrorResponseHandler } from "@ai-sdk/provider-utils";
162
+ var sarvamErrorDataSchema = z.object({
163
+ error: z.object({
164
+ message: z.string(),
165
+ type: z.string()
166
+ })
167
+ });
168
+ var sarvamFailedResponseHandler = createJsonErrorResponseHandler({
169
+ errorSchema: sarvamErrorDataSchema,
170
+ errorToMessage: (data) => data.error.message
171
+ });
172
+
173
+ // src/sarvam-prepare-tools.ts
174
+ import {
175
+ UnsupportedFunctionalityError as UnsupportedFunctionalityError2
176
+ } from "@ai-sdk/provider";
177
+ function prepareTools({
178
+ mode
179
+ }) {
180
+ var _a;
181
+ const tools = ((_a = mode.tools) == null ? void 0 : _a.length) ? mode.tools : void 0;
182
+ const toolWarnings = [];
183
+ if (tools == null) {
184
+ return { tools: void 0, tool_choice: void 0, toolWarnings };
185
+ }
186
+ const toolChoice = mode.toolChoice;
187
+ const sarvamTools = [];
188
+ for (const tool of tools) {
189
+ if (tool.type === "provider-defined") {
190
+ toolWarnings.push({ type: "unsupported-tool", tool });
191
+ } else {
192
+ sarvamTools.push({
193
+ type: "function",
194
+ function: {
195
+ name: tool.name,
196
+ description: tool.description,
197
+ parameters: tool.parameters
198
+ }
199
+ });
200
+ }
201
+ }
202
+ if (toolChoice == null) {
203
+ return { tools: sarvamTools, tool_choice: void 0, toolWarnings };
204
+ }
205
+ const type = toolChoice.type;
206
+ switch (type) {
207
+ case "auto":
208
+ case "none":
209
+ case "required":
210
+ return { tools: sarvamTools, tool_choice: type, toolWarnings };
211
+ case "tool":
212
+ return {
213
+ tools: sarvamTools,
214
+ tool_choice: {
215
+ type: "function",
216
+ function: {
217
+ name: toolChoice.toolName
218
+ }
219
+ },
220
+ toolWarnings
221
+ };
222
+ default: {
223
+ const _exhaustiveCheck = type;
224
+ throw new UnsupportedFunctionalityError2({
225
+ functionality: `Unsupported tool choice type: ${_exhaustiveCheck}`
226
+ });
227
+ }
228
+ }
229
+ }
230
+
231
+ // src/map-sarvam-finish-reason.ts
232
+ function mapSarvamFinishReason(finishReason) {
233
+ switch (finishReason) {
234
+ case "stop":
235
+ return "stop";
236
+ case "length":
237
+ return "length";
238
+ case "content_filter":
239
+ return "content-filter";
240
+ case "function_call":
241
+ case "tool_calls":
242
+ return "tool-calls";
243
+ default:
244
+ return "unknown";
245
+ }
246
+ }
247
+
248
+ // src/sarvam-chat-language-model.ts
249
+ var SarvamChatLanguageModel = class {
250
+ constructor(modelId, settings, config) {
251
+ this.specificationVersion = "v1";
252
+ this.supportsStructuredOutputs = false;
253
+ this.defaultObjectGenerationMode = "json";
254
+ this.modelId = modelId;
255
+ this.settings = settings;
256
+ this.config = config;
257
+ }
258
+ get provider() {
259
+ return this.config.provider;
260
+ }
261
+ get supportsImageUrls() {
262
+ return !this.settings.downloadImages;
263
+ }
264
+ getArgs({
265
+ mode,
266
+ prompt,
267
+ maxTokens,
268
+ temperature,
269
+ topP,
270
+ topK,
271
+ frequencyPenalty,
272
+ presencePenalty,
273
+ stopSequences,
274
+ responseFormat,
275
+ seed,
276
+ stream,
277
+ providerMetadata
278
+ }) {
279
+ const type = mode.type;
280
+ const warnings = [];
281
+ if (topK != null) {
282
+ warnings.push({
283
+ type: "unsupported-setting",
284
+ setting: "topK"
285
+ });
286
+ }
287
+ if (responseFormat != null && responseFormat.type === "json" && responseFormat.schema != null) {
288
+ warnings.push({
289
+ type: "unsupported-setting",
290
+ setting: "responseFormat",
291
+ details: "JSON response format schema is not supported"
292
+ });
293
+ }
294
+ const sarvamOptions = parseProviderOptions({
295
+ provider: "sarvam",
296
+ providerOptions: providerMetadata,
297
+ schema: z2.object({
298
+ reasoningFormat: z2.enum(["parsed", "raw", "hidden"]).nullish()
299
+ })
300
+ });
301
+ const baseArgs = {
302
+ // model id:
303
+ model: this.modelId,
304
+ // model specific settings:
305
+ user: this.settings.user,
306
+ parallel_tool_calls: this.settings.parallelToolCalls,
307
+ // standardized settings:
308
+ max_tokens: maxTokens,
309
+ temperature,
310
+ top_p: topP,
311
+ frequency_penalty: frequencyPenalty,
312
+ presence_penalty: presencePenalty,
313
+ stop: stopSequences,
314
+ seed,
315
+ // response format:
316
+ response_format: (
317
+ // json object response format is not supported for streaming:
318
+ stream === false && (responseFormat == null ? void 0 : responseFormat.type) === "json" ? { type: "json_object" } : void 0
319
+ ),
320
+ // provider options:
321
+ reasoning_format: sarvamOptions == null ? void 0 : sarvamOptions.reasoningFormat,
322
+ // messages:
323
+ messages: convertToSarvamChatMessages(prompt)
324
+ };
325
+ switch (type) {
326
+ case "regular": {
327
+ const { tools, tool_choice, toolWarnings } = prepareTools({
328
+ mode
329
+ });
330
+ return {
331
+ args: __spreadProps(__spreadValues({}, baseArgs), {
332
+ tools,
333
+ tool_choice
334
+ }),
335
+ warnings: [...warnings, ...toolWarnings]
336
+ };
337
+ }
338
+ case "object-json": {
339
+ return {
340
+ args: __spreadProps(__spreadValues({}, baseArgs), {
341
+ response_format: (
342
+ // json object response format is not supported for streaming:
343
+ stream === false ? { type: "json_object" } : void 0
344
+ )
345
+ }),
346
+ warnings
347
+ };
348
+ }
349
+ case "object-tool": {
350
+ return {
351
+ args: __spreadProps(__spreadValues({}, baseArgs), {
352
+ tool_choice: {
353
+ type: "function",
354
+ function: { name: mode.tool.name }
355
+ },
356
+ tools: [
357
+ {
358
+ type: "function",
359
+ function: {
360
+ name: mode.tool.name,
361
+ description: mode.tool.description,
362
+ parameters: mode.tool.parameters
363
+ }
364
+ }
365
+ ]
366
+ }),
367
+ warnings
368
+ };
369
+ }
370
+ default: {
371
+ const _exhaustiveCheck = type;
372
+ throw new Error(`Unsupported type: ${_exhaustiveCheck}`);
373
+ }
374
+ }
375
+ }
376
+ async doGenerate(options) {
377
+ var _b, _c, _d, _e, _f, _g, _h;
378
+ const { args, warnings } = this.getArgs(__spreadProps(__spreadValues({}, options), { stream: false }));
379
+ const body = JSON.stringify(args);
380
+ const {
381
+ responseHeaders,
382
+ value: response,
383
+ rawValue: rawResponse
384
+ } = await postJsonToApi({
385
+ url: this.config.url({
386
+ path: "/chat/completions",
387
+ modelId: this.modelId
388
+ }),
389
+ headers: combineHeaders(this.config.headers(), options.headers),
390
+ body: args,
391
+ failedResponseHandler: sarvamFailedResponseHandler,
392
+ successfulResponseHandler: createJsonResponseHandler(
393
+ sarvamChatResponseSchema
394
+ ),
395
+ abortSignal: options.abortSignal,
396
+ fetch: this.config.fetch
397
+ });
398
+ const _a = args, { messages: rawPrompt } = _a, rawSettings = __objRest(_a, ["messages"]);
399
+ const choice = response.choices[0];
400
+ return {
401
+ text: (_b = choice.message.content) != null ? _b : void 0,
402
+ reasoning: (_c = choice.message.reasoning) != null ? _c : void 0,
403
+ toolCalls: (_d = choice.message.tool_calls) == null ? void 0 : _d.map((toolCall) => {
404
+ var _a2;
405
+ return {
406
+ toolCallType: "function",
407
+ toolCallId: (_a2 = toolCall.id) != null ? _a2 : generateId(),
408
+ toolName: toolCall.function.name,
409
+ args: toolCall.function.arguments
410
+ };
411
+ }),
412
+ finishReason: mapSarvamFinishReason(choice.finish_reason),
413
+ usage: {
414
+ promptTokens: (_f = (_e = response.usage) == null ? void 0 : _e.prompt_tokens) != null ? _f : NaN,
415
+ completionTokens: (_h = (_g = response.usage) == null ? void 0 : _g.completion_tokens) != null ? _h : NaN
416
+ },
417
+ rawCall: { rawPrompt, rawSettings },
418
+ rawResponse: { headers: responseHeaders, body: rawResponse },
419
+ response: getResponseMetadata(response),
420
+ warnings,
421
+ request: { body }
422
+ };
423
+ }
424
+ async doStream(options) {
425
+ const { args, warnings } = this.getArgs(__spreadProps(__spreadValues({}, options), { stream: true }));
426
+ const body = JSON.stringify(__spreadProps(__spreadValues({}, args), { stream: true }));
427
+ const { responseHeaders, value: response } = await postJsonToApi({
428
+ url: this.config.url({
429
+ path: "/chat/completions",
430
+ modelId: this.modelId
431
+ }),
432
+ headers: combineHeaders(this.config.headers(), options.headers),
433
+ body: __spreadProps(__spreadValues({}, args), {
434
+ stream: true
435
+ }),
436
+ failedResponseHandler: sarvamFailedResponseHandler,
437
+ successfulResponseHandler: createEventSourceResponseHandler(sarvamChatChunkSchema),
438
+ abortSignal: options.abortSignal,
439
+ fetch: this.config.fetch
440
+ });
441
+ const _a = args, { messages: rawPrompt } = _a, rawSettings = __objRest(_a, ["messages"]);
442
+ const toolCalls = [];
443
+ let finishReason = "unknown";
444
+ let usage = {
445
+ promptTokens: void 0,
446
+ completionTokens: void 0
447
+ };
448
+ let isFirstChunk = true;
449
+ let providerMetadata;
450
+ return {
451
+ stream: response.pipeThrough(
452
+ new TransformStream({
453
+ transform(chunk, controller) {
454
+ var _a2, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o;
455
+ if (!chunk.success) {
456
+ finishReason = "error";
457
+ controller.enqueue({
458
+ type: "error",
459
+ error: chunk.error
460
+ });
461
+ return;
462
+ }
463
+ const value = chunk.value;
464
+ if ("error" in value) {
465
+ finishReason = "error";
466
+ controller.enqueue({
467
+ type: "error",
468
+ error: value.error
469
+ });
470
+ return;
471
+ }
472
+ if (isFirstChunk) {
473
+ isFirstChunk = false;
474
+ controller.enqueue(__spreadValues({
475
+ type: "response-metadata"
476
+ }, getResponseMetadata(value)));
477
+ }
478
+ if (((_a2 = value.x_sarvam) == null ? void 0 : _a2.usage) != null) {
479
+ usage = {
480
+ promptTokens: (_b = value.x_sarvam.usage.prompt_tokens) != null ? _b : void 0,
481
+ completionTokens: (_c = value.x_sarvam.usage.completion_tokens) != null ? _c : void 0
482
+ };
483
+ }
484
+ const choice = value.choices[0];
485
+ if ((choice == null ? void 0 : choice.finish_reason) != null) {
486
+ finishReason = mapSarvamFinishReason(
487
+ choice.finish_reason
488
+ );
489
+ }
490
+ if ((choice == null ? void 0 : choice.delta) == null) {
491
+ return;
492
+ }
493
+ const delta = choice.delta;
494
+ if (delta.reasoning != null && delta.reasoning.length > 0) {
495
+ controller.enqueue({
496
+ type: "reasoning",
497
+ textDelta: delta.reasoning
498
+ });
499
+ }
500
+ if (delta.content != null && delta.content.length > 0) {
501
+ controller.enqueue({
502
+ type: "text-delta",
503
+ textDelta: delta.content
504
+ });
505
+ }
506
+ if (delta.tool_calls != null) {
507
+ for (const toolCallDelta of delta.tool_calls) {
508
+ const index = toolCallDelta.index;
509
+ if (toolCalls[index] == null) {
510
+ if (toolCallDelta.type !== "function") {
511
+ throw new InvalidResponseDataError({
512
+ data: toolCallDelta,
513
+ message: `Expected 'function' type.`
514
+ });
515
+ }
516
+ if (toolCallDelta.id == null) {
517
+ throw new InvalidResponseDataError({
518
+ data: toolCallDelta,
519
+ message: `Expected 'id' to be a string.`
520
+ });
521
+ }
522
+ if (((_d = toolCallDelta.function) == null ? void 0 : _d.name) == null) {
523
+ throw new InvalidResponseDataError({
524
+ data: toolCallDelta,
525
+ message: `Expected 'function.name' to be a string.`
526
+ });
527
+ }
528
+ toolCalls[index] = {
529
+ id: toolCallDelta.id,
530
+ type: "function",
531
+ function: {
532
+ name: toolCallDelta.function.name,
533
+ arguments: (_e = toolCallDelta.function.arguments) != null ? _e : ""
534
+ },
535
+ hasFinished: false
536
+ };
537
+ const toolCall2 = toolCalls[index];
538
+ if (((_f = toolCall2.function) == null ? void 0 : _f.name) != null && ((_g = toolCall2.function) == null ? void 0 : _g.arguments) != null) {
539
+ if (toolCall2.function.arguments.length > 0) {
540
+ controller.enqueue({
541
+ type: "tool-call-delta",
542
+ toolCallType: "function",
543
+ toolCallId: toolCall2.id,
544
+ toolName: toolCall2.function.name,
545
+ argsTextDelta: toolCall2.function.arguments
546
+ });
547
+ }
548
+ if (isParsableJson(
549
+ toolCall2.function.arguments
550
+ )) {
551
+ controller.enqueue({
552
+ type: "tool-call",
553
+ toolCallType: "function",
554
+ toolCallId: (_h = toolCall2.id) != null ? _h : generateId(),
555
+ toolName: toolCall2.function.name,
556
+ args: toolCall2.function.arguments
557
+ });
558
+ toolCall2.hasFinished = true;
559
+ }
560
+ }
561
+ continue;
562
+ }
563
+ const toolCall = toolCalls[index];
564
+ if (toolCall.hasFinished) {
565
+ continue;
566
+ }
567
+ if (((_i = toolCallDelta.function) == null ? void 0 : _i.arguments) != null) {
568
+ toolCall.function.arguments += (_k = (_j = toolCallDelta.function) == null ? void 0 : _j.arguments) != null ? _k : "";
569
+ }
570
+ controller.enqueue({
571
+ type: "tool-call-delta",
572
+ toolCallType: "function",
573
+ toolCallId: toolCall.id,
574
+ toolName: toolCall.function.name,
575
+ argsTextDelta: (_l = toolCallDelta.function.arguments) != null ? _l : ""
576
+ });
577
+ if (((_m = toolCall.function) == null ? void 0 : _m.name) != null && ((_n = toolCall.function) == null ? void 0 : _n.arguments) != null && isParsableJson(toolCall.function.arguments)) {
578
+ controller.enqueue({
579
+ type: "tool-call",
580
+ toolCallType: "function",
581
+ toolCallId: (_o = toolCall.id) != null ? _o : generateId(),
582
+ toolName: toolCall.function.name,
583
+ args: toolCall.function.arguments
584
+ });
585
+ toolCall.hasFinished = true;
586
+ }
587
+ }
588
+ }
589
+ },
590
+ flush(controller) {
591
+ var _a2, _b;
592
+ controller.enqueue(__spreadValues({
593
+ type: "finish",
594
+ finishReason,
595
+ usage: {
596
+ promptTokens: (_a2 = usage.promptTokens) != null ? _a2 : NaN,
597
+ completionTokens: (_b = usage.completionTokens) != null ? _b : NaN
598
+ }
599
+ }, providerMetadata != null ? { providerMetadata } : {}));
600
+ }
601
+ })
602
+ ),
603
+ rawCall: { rawPrompt, rawSettings },
604
+ rawResponse: { headers: responseHeaders },
605
+ warnings,
606
+ request: { body }
607
+ };
608
+ }
609
+ };
610
+ var sarvamChatResponseSchema = z2.object({
611
+ id: z2.string().nullish(),
612
+ created: z2.number().nullish(),
613
+ model: z2.string().nullish(),
614
+ choices: z2.array(
615
+ z2.object({
616
+ message: z2.object({
617
+ content: z2.string().nullish(),
618
+ reasoning: z2.string().nullish(),
619
+ tool_calls: z2.array(
620
+ z2.object({
621
+ id: z2.string().nullish(),
622
+ type: z2.literal("function"),
623
+ function: z2.object({
624
+ name: z2.string(),
625
+ arguments: z2.string()
626
+ })
627
+ })
628
+ ).nullish()
629
+ }),
630
+ index: z2.number(),
631
+ finish_reason: z2.string().nullish()
632
+ })
633
+ ),
634
+ usage: z2.object({
635
+ prompt_tokens: z2.number().nullish(),
636
+ completion_tokens: z2.number().nullish()
637
+ }).nullish()
638
+ });
639
+ var sarvamChatChunkSchema = z2.union([
640
+ z2.object({
641
+ id: z2.string().nullish(),
642
+ created: z2.number().nullish(),
643
+ model: z2.string().nullish(),
644
+ choices: z2.array(
645
+ z2.object({
646
+ delta: z2.object({
647
+ content: z2.string().nullish(),
648
+ reasoning: z2.string().nullish(),
649
+ tool_calls: z2.array(
650
+ z2.object({
651
+ index: z2.number(),
652
+ id: z2.string().nullish(),
653
+ type: z2.literal("function").optional(),
654
+ function: z2.object({
655
+ name: z2.string().nullish(),
656
+ arguments: z2.string().nullish()
657
+ })
658
+ })
659
+ ).nullish()
660
+ }).nullish(),
661
+ finish_reason: z2.string().nullable().optional(),
662
+ index: z2.number()
663
+ })
664
+ ),
665
+ x_sarvam: z2.object({
666
+ usage: z2.object({
667
+ prompt_tokens: z2.number().nullish(),
668
+ completion_tokens: z2.number().nullish()
669
+ }).nullish()
670
+ }).nullish()
671
+ }),
672
+ sarvamErrorDataSchema
673
+ ]);
674
+
675
+ // src/sarvam-transcription-model.ts
676
+ import {
677
+ combineHeaders as combineHeaders2,
678
+ createJsonResponseHandler as createJsonResponseHandler2,
679
+ parseProviderOptions as parseProviderOptions2,
680
+ postFormDataToApi
681
+ } from "@ai-sdk/provider-utils";
682
+ import { z as z4 } from "zod";
683
+
684
+ // src/sarvam-transcription-settings.ts
685
+ import { z as z3 } from "zod";
686
+ var SarvamProviderOptionsSchema = z3.object({
687
+ with_timestamps: z3.boolean().nullish().default(false),
688
+ /**
689
+ * Enables speaker diarization, which identifies and separates different speakers in the audio.
690
+ * When set to true, the API will provide speaker-specific segments in the response.
691
+ * Note: This parameter is currently in Beta mode.
692
+ */
693
+ with_diarization: z3.boolean().nullish().default(false),
694
+ /**
695
+ * Number of speakers to be detected in the audio.
696
+ * This is used when with_diarization is set to true.
697
+ * Can be null.
698
+ */
699
+ num_speakers: z3.number().int().nullish()
700
+ });
701
+
702
+ // src/sarvam-transcription-model.ts
703
+ var SarvamTranscriptionModel = class {
704
+ constructor(modelId, languageCode, config) {
705
+ this.modelId = modelId;
706
+ this.languageCode = languageCode;
707
+ this.config = config;
708
+ this.specificationVersion = "v1";
709
+ }
710
+ get provider() {
711
+ return this.config.provider;
712
+ }
713
+ getArgs({
714
+ audio,
715
+ mediaType,
716
+ providerOptions
717
+ }) {
718
+ const warnings = [];
719
+ if (this.modelId === "saarika:v1" && this.languageCode === "unknown")
720
+ throw new Error(
721
+ "Language code unknown is not supported for model saarika:v1"
722
+ );
723
+ const sarvamOptions = parseProviderOptions2({
724
+ provider: "sarvam",
725
+ providerOptions: {
726
+ sarvam: __spreadValues(__spreadValues({}, providerOptions == null ? void 0 : providerOptions.sarvam), this.config.transcription)
727
+ },
728
+ schema: SarvamProviderOptionsSchema
729
+ });
730
+ const formData = new FormData();
731
+ const blob = audio instanceof Blob ? audio : new Blob([audio], { type: mediaType });
732
+ formData.append("file", blob);
733
+ formData.append("model", this.modelId);
734
+ if (sarvamOptions) {
735
+ formData.append("language_code", this.languageCode);
736
+ formData.append(
737
+ "with_timestamps",
738
+ sarvamOptions.with_timestamps ? "true" : "false"
739
+ );
740
+ formData.append(
741
+ "with_diarization",
742
+ sarvamOptions.with_diarization ? "true" : "false"
743
+ );
744
+ if (sarvamOptions.num_speakers !== null && sarvamOptions.num_speakers !== void 0) {
745
+ formData.append(
746
+ "num_speakers",
747
+ sarvamOptions.num_speakers.toString()
748
+ );
749
+ }
750
+ }
751
+ return {
752
+ formData,
753
+ warnings
754
+ };
755
+ }
756
+ async doGenerate(options) {
757
+ var _a, _b, _c, _d, _e;
758
+ const currentDate = (_c = (_b = (_a = this.config._internal) == null ? void 0 : _a.currentDate) == null ? void 0 : _b.call(_a)) != null ? _c : /* @__PURE__ */ new Date();
759
+ const { formData, warnings } = this.getArgs(options);
760
+ const {
761
+ value: response,
762
+ responseHeaders,
763
+ rawValue: rawResponse
764
+ } = await postFormDataToApi({
765
+ url: this.config.url({
766
+ path: "/speech-to-text",
767
+ modelId: this.modelId
768
+ }),
769
+ headers: combineHeaders2(this.config.headers(), options.headers),
770
+ formData,
771
+ failedResponseHandler: sarvamFailedResponseHandler,
772
+ successfulResponseHandler: createJsonResponseHandler2(
773
+ sarvamTranscriptionResponseSchema
774
+ ),
775
+ abortSignal: options.abortSignal,
776
+ fetch: this.config.fetch
777
+ });
778
+ return {
779
+ text: response.transcript,
780
+ segments: response.timestamps ? response.timestamps.words.map((word, index) => ({
781
+ text: word,
782
+ startSecond: response.timestamps.start_time_seconds[index],
783
+ endSecond: response.timestamps.end_time_seconds[index]
784
+ })) : [],
785
+ language: response.language_code ? response.language_code : void 0,
786
+ durationInSeconds: (_e = (_d = response.timestamps) == null ? void 0 : _d.end_time_seconds[response.timestamps.end_time_seconds.length - 1]) != null ? _e : void 0,
787
+ warnings,
788
+ response: {
789
+ timestamp: currentDate,
790
+ modelId: this.modelId,
791
+ headers: responseHeaders,
792
+ body: rawResponse
793
+ }
794
+ };
795
+ }
796
+ };
797
+ var sarvamTranscriptionResponseSchema = z4.object({
798
+ request_id: z4.string().nullable(),
799
+ transcript: z4.string(),
800
+ language_code: z4.string().nullable(),
801
+ timestamps: z4.object({
802
+ end_time_seconds: z4.array(z4.number()),
803
+ start_time_seconds: z4.array(z4.number()),
804
+ words: z4.array(z4.string())
805
+ }).optional(),
806
+ diarized_transcript: z4.object({
807
+ entries: z4.array(
808
+ z4.object({
809
+ end_time_seconds: z4.array(z4.number()),
810
+ start_time_seconds: z4.array(z4.number()),
811
+ transcript: z4.string(),
812
+ speaker_id: z4.string()
813
+ })
814
+ )
815
+ }).optional()
816
+ });
817
+
818
+ // src/sarvam-speech-model.ts
819
+ import {
820
+ combineHeaders as combineHeaders3,
821
+ createJsonResponseHandler as createJsonResponseHandler3,
822
+ parseProviderOptions as parseProviderOptions3,
823
+ postJsonToApi as postJsonToApi2
824
+ } from "@ai-sdk/provider-utils";
825
+
826
+ // src/sarvam-speech-settings.ts
827
+ import { z as z5 } from "zod";
828
+ var SpeakerSchema = z5.enum([
829
+ "meera",
830
+ "pavithra",
831
+ "maitreyi",
832
+ "arvind",
833
+ "amol",
834
+ "amartya",
835
+ "diya",
836
+ "neel",
837
+ "misha",
838
+ "vian",
839
+ "arjun",
840
+ "maya",
841
+ "anushka",
842
+ "abhilash",
843
+ "manisha",
844
+ "vidya",
845
+ "arya",
846
+ "karun",
847
+ "hitesh"
848
+ ]).default("meera");
849
+ var SarvamProviderOptionsSchema2 = z5.object({
850
+ speaker: SpeakerSchema,
851
+ pitch: z5.number().min(-0.75).max(0.75).default(0),
852
+ pace: z5.number().min(0.5).max(2).default(1),
853
+ loudness: z5.number().min(0.3).max(3).default(1),
854
+ speech_sample_rate: z5.union([
855
+ z5.literal(8e3),
856
+ z5.literal(16e3),
857
+ z5.literal(22050),
858
+ z5.literal(24e3)
859
+ ]).default(22050),
860
+ enable_preprocessing: z5.boolean().default(false)
861
+ }).partial();
862
+
863
+ // src/sarvam-speech-model.ts
864
+ import { z as z6 } from "zod";
865
+ var SarvamSpeechModel = class {
866
+ constructor(modelId, languageCode, config) {
867
+ this.modelId = modelId;
868
+ this.languageCode = languageCode;
869
+ this.config = config;
870
+ this.specificationVersion = "v1";
871
+ }
872
+ get provider() {
873
+ return this.config.provider;
874
+ }
875
+ getArgs({
876
+ text,
877
+ voice,
878
+ outputFormat = "wav",
879
+ // speed,
880
+ // instructions,
881
+ providerOptions
882
+ }) {
883
+ const warnings = [];
884
+ const sarvamOptions = parseProviderOptions3({
885
+ provider: "sarvam",
886
+ providerOptions: {
887
+ sarvam: __spreadValues(__spreadValues({}, providerOptions == null ? void 0 : providerOptions.sarvam), this.config.speech)
888
+ },
889
+ schema: SarvamProviderOptionsSchema2
890
+ });
891
+ const getSpeaker = () => {
892
+ if (sarvamOptions == null ? void 0 : sarvamOptions.speaker) return sarvamOptions.speaker;
893
+ if (voice) {
894
+ return SpeakerSchema.parse(voice);
895
+ }
896
+ switch (this.modelId) {
897
+ case "bulbul:v1":
898
+ return "meera";
899
+ case "bulbul:v2":
900
+ return "manisha";
901
+ }
902
+ return "meera";
903
+ };
904
+ const requestBody = {
905
+ model: this.modelId,
906
+ text,
907
+ target_language_code: this.languageCode,
908
+ speaker: getSpeaker()
909
+ // response_format: "wav",
910
+ // speed,
911
+ // instructions,
912
+ };
913
+ if (outputFormat) {
914
+ if (["mp3", "opus", "aac", "flac", "wav", "pcm"].includes(
915
+ outputFormat
916
+ )) {
917
+ requestBody.response_format = outputFormat;
918
+ } else {
919
+ warnings.push({
920
+ type: "unsupported-setting",
921
+ setting: "outputFormat",
922
+ details: `Unsupported output format: ${outputFormat}. Using mp3 instead.`
923
+ });
924
+ }
925
+ }
926
+ if (sarvamOptions) {
927
+ const speechModelOptions = {};
928
+ for (const key in speechModelOptions) {
929
+ const value = speechModelOptions[key];
930
+ if (value !== void 0) {
931
+ requestBody[key] = value;
932
+ }
933
+ }
934
+ }
935
+ return {
936
+ requestBody,
937
+ warnings
938
+ };
939
+ }
940
+ async doGenerate(options) {
941
+ var _a, _b, _c;
942
+ const currentDate = (_c = (_b = (_a = this.config._internal) == null ? void 0 : _a.currentDate) == null ? void 0 : _b.call(_a)) != null ? _c : /* @__PURE__ */ new Date();
943
+ const { requestBody, warnings } = this.getArgs(options);
944
+ const {
945
+ value,
946
+ responseHeaders,
947
+ rawValue: rawResponse
948
+ } = await postJsonToApi2({
949
+ url: this.config.url({
950
+ path: "/text-to-speech",
951
+ modelId: this.modelId
952
+ }),
953
+ headers: combineHeaders3(this.config.headers(), options.headers),
954
+ body: requestBody,
955
+ failedResponseHandler: sarvamFailedResponseHandler,
956
+ successfulResponseHandler: createJsonResponseHandler3(
957
+ z6.object({
958
+ request_id: z6.string(),
959
+ audios: z6.array(z6.string())
960
+ })
961
+ ),
962
+ abortSignal: options.abortSignal,
963
+ fetch: this.config.fetch
964
+ });
965
+ const audio = value.audios[0];
966
+ return {
967
+ audio,
968
+ warnings,
969
+ request: {
970
+ body: JSON.stringify(requestBody)
971
+ },
972
+ response: {
973
+ timestamp: currentDate,
974
+ modelId: this.modelId,
975
+ headers: responseHeaders,
976
+ body: rawResponse
977
+ }
978
+ };
979
+ }
980
+ };
981
+
982
+ // src/sarvam-provider.ts
983
+ function createSarvam(options = {}) {
984
+ var _a;
985
+ const baseURL = (_a = withoutTrailingSlash(options.baseURL)) != null ? _a : "https://api.sarvam.ai";
986
+ const ApiKey = loadApiKey({
987
+ apiKey: options.apiKey,
988
+ environmentVariableName: "SARVAM_API_KEY",
989
+ description: "Sarvam"
990
+ });
991
+ const getHeaders = () => __spreadValues({
992
+ Authorization: `Bearer ${ApiKey}`,
993
+ "api-subscription-key": ApiKey
994
+ }, options.headers);
995
+ const createChatModel = (modelId, settings = {}) => new SarvamChatLanguageModel(modelId, settings, {
996
+ provider: "sarvam.chat",
997
+ url: ({ path }) => `${baseURL}/v1${path}`,
998
+ headers: getHeaders,
999
+ fetch: options.fetch
1000
+ });
1001
+ const createLanguageModel = (modelId, settings) => {
1002
+ if (new.target) {
1003
+ throw new Error(
1004
+ "The Sarvam model function cannot be called with the new keyword."
1005
+ );
1006
+ }
1007
+ return createChatModel(modelId, settings);
1008
+ };
1009
+ const createTranscriptionModel = (modelId, languageCode = "unknown", settings) => {
1010
+ return new SarvamTranscriptionModel(modelId, languageCode, {
1011
+ provider: "sarvam.transcription",
1012
+ url: ({ path }) => `${baseURL}${path}`,
1013
+ headers: getHeaders,
1014
+ fetch: options.fetch,
1015
+ transcription: settings
1016
+ });
1017
+ };
1018
+ const createSpeechModel = (modelId, languageCode, settings) => new SarvamSpeechModel(modelId, languageCode, {
1019
+ provider: `sarvam.speech`,
1020
+ url: ({ path }) => `${baseURL}${path}`,
1021
+ headers: getHeaders,
1022
+ fetch: options.fetch,
1023
+ speech: settings
1024
+ });
1025
+ const provider = (modelId, settings) => createLanguageModel(modelId, settings);
1026
+ provider.languageModel = createLanguageModel;
1027
+ provider.chat = createChatModel;
1028
+ provider.transcription = createTranscriptionModel;
1029
+ provider.speech = createSpeechModel;
1030
+ return provider;
1031
+ }
1032
+ var sarvam = createSarvam();
1033
+ export {
1034
+ createSarvam,
1035
+ sarvam
1036
+ };
1037
+ //# sourceMappingURL=index.js.map