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