@ai-sdk/cohere 4.0.0-beta.9 → 4.0.0-canary.34

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.mjs DELETED
@@ -1,1102 +0,0 @@
1
- // src/cohere-provider.ts
2
- import {
3
- NoSuchModelError
4
- } from "@ai-sdk/provider";
5
- import {
6
- generateId,
7
- loadApiKey,
8
- withoutTrailingSlash,
9
- withUserAgentSuffix
10
- } from "@ai-sdk/provider-utils";
11
-
12
- // src/cohere-chat-language-model.ts
13
- import {
14
- combineHeaders,
15
- createEventSourceResponseHandler,
16
- createJsonResponseHandler,
17
- isCustomReasoning,
18
- mapReasoningToProviderBudget,
19
- parseProviderOptions,
20
- postJsonToApi
21
- } from "@ai-sdk/provider-utils";
22
- import { z as z3 } from "zod/v4";
23
-
24
- // src/cohere-chat-options.ts
25
- import { z } from "zod/v4";
26
- var cohereLanguageModelOptions = z.object({
27
- /**
28
- * Configuration for reasoning features (optional)
29
- *
30
- * Can be set to an object with the two properties `type` and `tokenBudget`. `type` can be set to `'enabled'` or `'disabled'` (defaults to `'enabled'`).
31
- * `tokenBudget` is the maximum number of tokens the model can use for thinking, which must be set to a positive integer. The model will stop thinking if it reaches the thinking token budget and will proceed with the response
32
- *
33
- * @see https://docs.cohere.com/reference/chat#request.body.thinking
34
- */
35
- thinking: z.object({
36
- type: z.enum(["enabled", "disabled"]).optional(),
37
- tokenBudget: z.number().optional()
38
- }).optional()
39
- });
40
-
41
- // src/cohere-error.ts
42
- import { createJsonErrorResponseHandler } from "@ai-sdk/provider-utils";
43
- import { z as z2 } from "zod/v4";
44
- var cohereErrorDataSchema = z2.object({
45
- message: z2.string()
46
- });
47
- var cohereFailedResponseHandler = createJsonErrorResponseHandler({
48
- errorSchema: cohereErrorDataSchema,
49
- errorToMessage: (data) => data.message
50
- });
51
-
52
- // src/cohere-prepare-tools.ts
53
- import {
54
- UnsupportedFunctionalityError
55
- } from "@ai-sdk/provider";
56
- function prepareTools({
57
- tools,
58
- toolChoice
59
- }) {
60
- tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
61
- const toolWarnings = [];
62
- if (tools == null) {
63
- return { tools: void 0, toolChoice: void 0, toolWarnings };
64
- }
65
- const cohereTools = [];
66
- for (const tool of tools) {
67
- if (tool.type === "provider") {
68
- toolWarnings.push({
69
- type: "unsupported",
70
- feature: `provider-defined tool ${tool.id}`
71
- });
72
- } else {
73
- cohereTools.push({
74
- type: "function",
75
- function: {
76
- name: tool.name,
77
- description: tool.description,
78
- parameters: tool.inputSchema
79
- }
80
- });
81
- }
82
- }
83
- if (toolChoice == null) {
84
- return { tools: cohereTools, toolChoice: void 0, toolWarnings };
85
- }
86
- const type = toolChoice.type;
87
- switch (type) {
88
- case "auto":
89
- return { tools: cohereTools, toolChoice: void 0, toolWarnings };
90
- case "none":
91
- return { tools: cohereTools, toolChoice: "NONE", toolWarnings };
92
- case "required":
93
- return { tools: cohereTools, toolChoice: "REQUIRED", toolWarnings };
94
- case "tool":
95
- return {
96
- tools: cohereTools.filter(
97
- (tool) => tool.function.name === toolChoice.toolName
98
- ),
99
- toolChoice: "REQUIRED",
100
- toolWarnings
101
- };
102
- default: {
103
- const _exhaustiveCheck = type;
104
- throw new UnsupportedFunctionalityError({
105
- functionality: `tool choice type: ${_exhaustiveCheck}`
106
- });
107
- }
108
- }
109
- }
110
-
111
- // src/convert-cohere-usage.ts
112
- function convertCohereUsage(tokens) {
113
- if (tokens == null) {
114
- return {
115
- inputTokens: {
116
- total: void 0,
117
- noCache: void 0,
118
- cacheRead: void 0,
119
- cacheWrite: void 0
120
- },
121
- outputTokens: {
122
- total: void 0,
123
- text: void 0,
124
- reasoning: void 0
125
- },
126
- raw: void 0
127
- };
128
- }
129
- const inputTokens = tokens.input_tokens;
130
- const outputTokens = tokens.output_tokens;
131
- return {
132
- inputTokens: {
133
- total: inputTokens,
134
- noCache: inputTokens,
135
- cacheRead: void 0,
136
- cacheWrite: void 0
137
- },
138
- outputTokens: {
139
- total: outputTokens,
140
- text: outputTokens,
141
- reasoning: void 0
142
- },
143
- raw: tokens
144
- };
145
- }
146
-
147
- // src/convert-to-cohere-chat-prompt.ts
148
- import {
149
- UnsupportedFunctionalityError as UnsupportedFunctionalityError2
150
- } from "@ai-sdk/provider";
151
- function convertToCohereChatPrompt(prompt) {
152
- const messages = [];
153
- const documents = [];
154
- const warnings = [];
155
- for (const { role, content } of prompt) {
156
- switch (role) {
157
- case "system": {
158
- messages.push({ role: "system", content });
159
- break;
160
- }
161
- case "user": {
162
- messages.push({
163
- role: "user",
164
- content: content.map((part) => {
165
- var _a;
166
- switch (part.type) {
167
- case "text": {
168
- return part.text;
169
- }
170
- case "file": {
171
- let textContent;
172
- if (typeof part.data === "string") {
173
- textContent = part.data;
174
- } else if (part.data instanceof Uint8Array) {
175
- if (!(((_a = part.mediaType) == null ? void 0 : _a.startsWith("text/")) || part.mediaType === "application/json")) {
176
- throw new UnsupportedFunctionalityError2({
177
- functionality: `document media type: ${part.mediaType}`,
178
- message: `Media type '${part.mediaType}' is not supported. Supported media types are: text/* and application/json.`
179
- });
180
- }
181
- textContent = new TextDecoder().decode(part.data);
182
- } else {
183
- throw new UnsupportedFunctionalityError2({
184
- functionality: "File URL data",
185
- message: "URLs should be downloaded by the AI SDK and not reach this point. This indicates a configuration issue."
186
- });
187
- }
188
- documents.push({
189
- data: {
190
- text: textContent,
191
- title: part.filename
192
- }
193
- });
194
- return "";
195
- }
196
- }
197
- }).join("")
198
- });
199
- break;
200
- }
201
- case "assistant": {
202
- let text = "";
203
- const toolCalls = [];
204
- for (const part of content) {
205
- switch (part.type) {
206
- case "text": {
207
- text += part.text;
208
- break;
209
- }
210
- case "tool-call": {
211
- toolCalls.push({
212
- id: part.toolCallId,
213
- type: "function",
214
- function: {
215
- name: part.toolName,
216
- arguments: JSON.stringify(part.input)
217
- }
218
- });
219
- break;
220
- }
221
- }
222
- }
223
- messages.push({
224
- role: "assistant",
225
- content: toolCalls.length > 0 ? void 0 : text,
226
- tool_calls: toolCalls.length > 0 ? toolCalls : void 0,
227
- tool_plan: void 0
228
- });
229
- break;
230
- }
231
- case "tool": {
232
- messages.push(
233
- ...content.filter((toolResult) => toolResult.type !== "tool-approval-response").map((toolResult) => {
234
- var _a;
235
- const output = toolResult.output;
236
- let contentValue;
237
- switch (output.type) {
238
- case "text":
239
- case "error-text":
240
- contentValue = output.value;
241
- break;
242
- case "execution-denied":
243
- contentValue = (_a = output.reason) != null ? _a : "Tool execution denied.";
244
- break;
245
- case "content":
246
- case "json":
247
- case "error-json":
248
- contentValue = JSON.stringify(output.value);
249
- break;
250
- }
251
- return {
252
- role: "tool",
253
- content: contentValue,
254
- tool_call_id: toolResult.toolCallId
255
- };
256
- })
257
- );
258
- break;
259
- }
260
- default: {
261
- const _exhaustiveCheck = role;
262
- throw new Error(`Unsupported role: ${_exhaustiveCheck}`);
263
- }
264
- }
265
- }
266
- return { messages, documents, warnings };
267
- }
268
-
269
- // src/map-cohere-finish-reason.ts
270
- function mapCohereFinishReason(finishReason) {
271
- switch (finishReason) {
272
- case "COMPLETE":
273
- case "STOP_SEQUENCE":
274
- return "stop";
275
- case "MAX_TOKENS":
276
- return "length";
277
- case "ERROR":
278
- return "error";
279
- case "TOOL_CALL":
280
- return "tool-calls";
281
- default:
282
- return "other";
283
- }
284
- }
285
-
286
- // src/cohere-chat-language-model.ts
287
- var CohereChatLanguageModel = class {
288
- constructor(modelId, config) {
289
- this.specificationVersion = "v4";
290
- this.supportedUrls = {
291
- // No URLs are supported.
292
- };
293
- this.modelId = modelId;
294
- this.config = config;
295
- }
296
- get provider() {
297
- return this.config.provider;
298
- }
299
- async getArgs({
300
- prompt,
301
- maxOutputTokens,
302
- temperature,
303
- topP,
304
- topK,
305
- frequencyPenalty,
306
- presencePenalty,
307
- stopSequences,
308
- responseFormat,
309
- seed,
310
- reasoning,
311
- tools,
312
- toolChoice,
313
- providerOptions
314
- }) {
315
- var _a;
316
- const warnings = [];
317
- const cohereOptions = (_a = await parseProviderOptions({
318
- provider: "cohere",
319
- providerOptions,
320
- schema: cohereLanguageModelOptions
321
- })) != null ? _a : {};
322
- const {
323
- messages: chatPrompt,
324
- documents: cohereDocuments,
325
- warnings: promptWarnings
326
- } = convertToCohereChatPrompt(prompt);
327
- const {
328
- tools: cohereTools,
329
- toolChoice: cohereToolChoice,
330
- toolWarnings
331
- } = prepareTools({ tools, toolChoice });
332
- warnings.push(...toolWarnings, ...promptWarnings);
333
- return {
334
- args: {
335
- // model id:
336
- model: this.modelId,
337
- // standardized settings:
338
- frequency_penalty: frequencyPenalty,
339
- presence_penalty: presencePenalty,
340
- max_tokens: maxOutputTokens,
341
- temperature,
342
- p: topP,
343
- k: topK,
344
- seed,
345
- stop_sequences: stopSequences,
346
- // response format:
347
- response_format: (responseFormat == null ? void 0 : responseFormat.type) === "json" ? { type: "json_object", json_schema: responseFormat.schema } : void 0,
348
- // messages:
349
- messages: chatPrompt,
350
- // tools:
351
- tools: cohereTools,
352
- tool_choice: cohereToolChoice,
353
- // documents for RAG:
354
- ...cohereDocuments.length > 0 && { documents: cohereDocuments },
355
- // reasoning:
356
- ...resolveCohereThinking({
357
- reasoning,
358
- cohereOptions,
359
- warnings
360
- })
361
- },
362
- warnings
363
- };
364
- }
365
- async doGenerate(options) {
366
- var _a, _b, _c, _d, _e, _f, _g;
367
- const { args, warnings } = await this.getArgs(options);
368
- const {
369
- responseHeaders,
370
- value: response,
371
- rawValue: rawResponse
372
- } = await postJsonToApi({
373
- url: `${this.config.baseURL}/chat`,
374
- headers: combineHeaders(this.config.headers(), options.headers),
375
- body: args,
376
- failedResponseHandler: cohereFailedResponseHandler,
377
- successfulResponseHandler: createJsonResponseHandler(
378
- cohereChatResponseSchema
379
- ),
380
- abortSignal: options.abortSignal,
381
- fetch: this.config.fetch
382
- });
383
- const content = [];
384
- for (const item of (_a = response.message.content) != null ? _a : []) {
385
- if (item.type === "text" && item.text.length > 0) {
386
- content.push({ type: "text", text: item.text });
387
- continue;
388
- }
389
- if (item.type === "thinking" && item.thinking.length > 0) {
390
- content.push({ type: "reasoning", text: item.thinking });
391
- continue;
392
- }
393
- }
394
- for (const citation of (_b = response.message.citations) != null ? _b : []) {
395
- content.push({
396
- type: "source",
397
- sourceType: "document",
398
- id: this.config.generateId(),
399
- mediaType: "text/plain",
400
- title: ((_d = (_c = citation.sources[0]) == null ? void 0 : _c.document) == null ? void 0 : _d.title) || "Document",
401
- providerMetadata: {
402
- cohere: {
403
- start: citation.start,
404
- end: citation.end,
405
- text: citation.text,
406
- sources: citation.sources,
407
- ...citation.type && { citationType: citation.type }
408
- }
409
- }
410
- });
411
- }
412
- for (const toolCall of (_e = response.message.tool_calls) != null ? _e : []) {
413
- content.push({
414
- type: "tool-call",
415
- toolCallId: toolCall.id,
416
- toolName: toolCall.function.name,
417
- // Cohere sometimes returns `null` for tool call arguments for tools
418
- // defined as having no arguments.
419
- input: toolCall.function.arguments.replace(/^null$/, "{}")
420
- });
421
- }
422
- return {
423
- content,
424
- finishReason: {
425
- unified: mapCohereFinishReason(response.finish_reason),
426
- raw: (_f = response.finish_reason) != null ? _f : void 0
427
- },
428
- usage: convertCohereUsage(response.usage.tokens),
429
- request: { body: args },
430
- response: {
431
- // TODO timestamp, model id
432
- id: (_g = response.generation_id) != null ? _g : void 0,
433
- headers: responseHeaders,
434
- body: rawResponse
435
- },
436
- warnings
437
- };
438
- }
439
- async doStream(options) {
440
- const { args, warnings } = await this.getArgs(options);
441
- const { responseHeaders, value: response } = await postJsonToApi({
442
- url: `${this.config.baseURL}/chat`,
443
- headers: combineHeaders(this.config.headers(), options.headers),
444
- body: { ...args, stream: true },
445
- failedResponseHandler: cohereFailedResponseHandler,
446
- successfulResponseHandler: createEventSourceResponseHandler(
447
- cohereChatChunkSchema
448
- ),
449
- abortSignal: options.abortSignal,
450
- fetch: this.config.fetch
451
- });
452
- let finishReason = {
453
- unified: "other",
454
- raw: void 0
455
- };
456
- let usage = void 0;
457
- let pendingToolCall = null;
458
- let isActiveReasoning = false;
459
- return {
460
- stream: response.pipeThrough(
461
- new TransformStream({
462
- start(controller) {
463
- controller.enqueue({ type: "stream-start", warnings });
464
- },
465
- transform(chunk, controller) {
466
- var _a, _b;
467
- if (options.includeRawChunks) {
468
- controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
469
- }
470
- if (!chunk.success) {
471
- finishReason = { unified: "error", raw: void 0 };
472
- controller.enqueue({ type: "error", error: chunk.error });
473
- return;
474
- }
475
- const value = chunk.value;
476
- const type = value.type;
477
- switch (type) {
478
- case "content-start": {
479
- if (value.delta.message.content.type === "thinking") {
480
- controller.enqueue({
481
- type: "reasoning-start",
482
- id: String(value.index)
483
- });
484
- isActiveReasoning = true;
485
- return;
486
- }
487
- controller.enqueue({
488
- type: "text-start",
489
- id: String(value.index)
490
- });
491
- return;
492
- }
493
- case "content-delta": {
494
- if ("thinking" in value.delta.message.content) {
495
- controller.enqueue({
496
- type: "reasoning-delta",
497
- id: String(value.index),
498
- delta: value.delta.message.content.thinking
499
- });
500
- return;
501
- }
502
- controller.enqueue({
503
- type: "text-delta",
504
- id: String(value.index),
505
- delta: value.delta.message.content.text
506
- });
507
- return;
508
- }
509
- case "content-end": {
510
- if (isActiveReasoning) {
511
- controller.enqueue({
512
- type: "reasoning-end",
513
- id: String(value.index)
514
- });
515
- isActiveReasoning = false;
516
- return;
517
- }
518
- controller.enqueue({
519
- type: "text-end",
520
- id: String(value.index)
521
- });
522
- return;
523
- }
524
- case "tool-call-start": {
525
- const toolId = value.delta.message.tool_calls.id;
526
- const toolName = value.delta.message.tool_calls.function.name;
527
- const initialArgs = value.delta.message.tool_calls.function.arguments;
528
- pendingToolCall = {
529
- id: toolId,
530
- name: toolName,
531
- arguments: initialArgs,
532
- hasFinished: false
533
- };
534
- controller.enqueue({
535
- type: "tool-input-start",
536
- id: toolId,
537
- toolName
538
- });
539
- if (initialArgs.length > 0) {
540
- controller.enqueue({
541
- type: "tool-input-delta",
542
- id: toolId,
543
- delta: initialArgs
544
- });
545
- }
546
- return;
547
- }
548
- case "tool-call-delta": {
549
- if (pendingToolCall && !pendingToolCall.hasFinished) {
550
- const argsDelta = value.delta.message.tool_calls.function.arguments;
551
- pendingToolCall.arguments += argsDelta;
552
- controller.enqueue({
553
- type: "tool-input-delta",
554
- id: pendingToolCall.id,
555
- delta: argsDelta
556
- });
557
- }
558
- return;
559
- }
560
- case "tool-call-end": {
561
- if (pendingToolCall && !pendingToolCall.hasFinished) {
562
- controller.enqueue({
563
- type: "tool-input-end",
564
- id: pendingToolCall.id
565
- });
566
- controller.enqueue({
567
- type: "tool-call",
568
- toolCallId: pendingToolCall.id,
569
- toolName: pendingToolCall.name,
570
- input: JSON.stringify(
571
- JSON.parse(((_a = pendingToolCall.arguments) == null ? void 0 : _a.trim()) || "{}")
572
- )
573
- });
574
- pendingToolCall.hasFinished = true;
575
- pendingToolCall = null;
576
- }
577
- return;
578
- }
579
- case "message-start": {
580
- controller.enqueue({
581
- type: "response-metadata",
582
- id: (_b = value.id) != null ? _b : void 0
583
- });
584
- return;
585
- }
586
- case "message-end": {
587
- finishReason = {
588
- unified: mapCohereFinishReason(value.delta.finish_reason),
589
- raw: value.delta.finish_reason
590
- };
591
- usage = value.delta.usage.tokens;
592
- return;
593
- }
594
- default: {
595
- return;
596
- }
597
- }
598
- },
599
- flush(controller) {
600
- controller.enqueue({
601
- type: "finish",
602
- finishReason,
603
- usage: convertCohereUsage(usage)
604
- });
605
- }
606
- })
607
- ),
608
- request: { body: { ...args, stream: true } },
609
- response: { headers: responseHeaders }
610
- };
611
- }
612
- };
613
- function resolveCohereThinking({
614
- reasoning,
615
- cohereOptions,
616
- warnings
617
- }) {
618
- var _a;
619
- if (cohereOptions.thinking) {
620
- return {
621
- thinking: {
622
- type: (_a = cohereOptions.thinking.type) != null ? _a : "enabled",
623
- token_budget: cohereOptions.thinking.tokenBudget
624
- }
625
- };
626
- }
627
- if (!isCustomReasoning(reasoning)) {
628
- return {};
629
- }
630
- if (reasoning === "none") {
631
- return { thinking: { type: "disabled" } };
632
- }
633
- const tokenBudget = mapReasoningToProviderBudget({
634
- reasoning,
635
- maxOutputTokens: 32768,
636
- maxReasoningBudget: 32768,
637
- warnings
638
- });
639
- if (tokenBudget == null) {
640
- return {};
641
- }
642
- return { thinking: { type: "enabled", token_budget: tokenBudget } };
643
- }
644
- var cohereChatResponseSchema = z3.object({
645
- generation_id: z3.string().nullish(),
646
- message: z3.object({
647
- role: z3.string(),
648
- content: z3.array(
649
- z3.union([
650
- z3.object({
651
- type: z3.literal("text"),
652
- text: z3.string()
653
- }),
654
- z3.object({
655
- type: z3.literal("thinking"),
656
- thinking: z3.string()
657
- })
658
- ])
659
- ).nullish(),
660
- tool_plan: z3.string().nullish(),
661
- tool_calls: z3.array(
662
- z3.object({
663
- id: z3.string(),
664
- type: z3.literal("function"),
665
- function: z3.object({
666
- name: z3.string(),
667
- arguments: z3.string()
668
- })
669
- })
670
- ).nullish(),
671
- citations: z3.array(
672
- z3.object({
673
- start: z3.number(),
674
- end: z3.number(),
675
- text: z3.string(),
676
- sources: z3.array(
677
- z3.object({
678
- type: z3.string().optional(),
679
- id: z3.string().optional(),
680
- document: z3.object({
681
- id: z3.string().optional(),
682
- text: z3.string(),
683
- title: z3.string()
684
- })
685
- })
686
- ),
687
- type: z3.string().optional()
688
- })
689
- ).nullish()
690
- }),
691
- finish_reason: z3.string(),
692
- usage: z3.object({
693
- billed_units: z3.object({
694
- input_tokens: z3.number(),
695
- output_tokens: z3.number()
696
- }),
697
- tokens: z3.object({
698
- input_tokens: z3.number(),
699
- output_tokens: z3.number()
700
- })
701
- })
702
- });
703
- var cohereChatChunkSchema = z3.discriminatedUnion("type", [
704
- z3.object({
705
- type: z3.literal("citation-start")
706
- }),
707
- z3.object({
708
- type: z3.literal("citation-end")
709
- }),
710
- z3.object({
711
- type: z3.literal("content-start"),
712
- index: z3.number(),
713
- delta: z3.object({
714
- message: z3.object({
715
- content: z3.union([
716
- z3.object({
717
- type: z3.literal("text"),
718
- text: z3.string()
719
- }),
720
- z3.object({
721
- type: z3.literal("thinking"),
722
- thinking: z3.string()
723
- })
724
- ])
725
- })
726
- })
727
- }),
728
- z3.object({
729
- type: z3.literal("content-delta"),
730
- index: z3.number(),
731
- delta: z3.object({
732
- message: z3.object({
733
- content: z3.union([
734
- z3.object({
735
- text: z3.string()
736
- }),
737
- z3.object({
738
- thinking: z3.string()
739
- })
740
- ])
741
- })
742
- })
743
- }),
744
- z3.object({
745
- type: z3.literal("content-end"),
746
- index: z3.number()
747
- }),
748
- z3.object({
749
- type: z3.literal("message-start"),
750
- id: z3.string().nullish()
751
- }),
752
- z3.object({
753
- type: z3.literal("message-end"),
754
- delta: z3.object({
755
- finish_reason: z3.string(),
756
- usage: z3.object({
757
- tokens: z3.object({
758
- input_tokens: z3.number(),
759
- output_tokens: z3.number()
760
- })
761
- })
762
- })
763
- }),
764
- // https://docs.cohere.com/v2/docs/streaming#tool-use-stream-events-for-tool-calling
765
- z3.object({
766
- type: z3.literal("tool-plan-delta"),
767
- delta: z3.object({
768
- message: z3.object({
769
- tool_plan: z3.string()
770
- })
771
- })
772
- }),
773
- z3.object({
774
- type: z3.literal("tool-call-start"),
775
- delta: z3.object({
776
- message: z3.object({
777
- tool_calls: z3.object({
778
- id: z3.string(),
779
- type: z3.literal("function"),
780
- function: z3.object({
781
- name: z3.string(),
782
- arguments: z3.string()
783
- })
784
- })
785
- })
786
- })
787
- }),
788
- // A single tool call's `arguments` stream in chunks and must be accumulated
789
- // in a string and so the full tool object info can only be parsed once we see
790
- // `tool-call-end`.
791
- z3.object({
792
- type: z3.literal("tool-call-delta"),
793
- delta: z3.object({
794
- message: z3.object({
795
- tool_calls: z3.object({
796
- function: z3.object({
797
- arguments: z3.string()
798
- })
799
- })
800
- })
801
- })
802
- }),
803
- z3.object({
804
- type: z3.literal("tool-call-end")
805
- })
806
- ]);
807
-
808
- // src/cohere-embedding-model.ts
809
- import {
810
- TooManyEmbeddingValuesForCallError
811
- } from "@ai-sdk/provider";
812
- import {
813
- combineHeaders as combineHeaders2,
814
- createJsonResponseHandler as createJsonResponseHandler2,
815
- parseProviderOptions as parseProviderOptions2,
816
- postJsonToApi as postJsonToApi2
817
- } from "@ai-sdk/provider-utils";
818
- import { z as z5 } from "zod/v4";
819
-
820
- // src/cohere-embedding-options.ts
821
- import { z as z4 } from "zod/v4";
822
- var cohereEmbeddingModelOptions = z4.object({
823
- /**
824
- * Specifies the type of input passed to the model. Default is `search_query`.
825
- *
826
- * - "search_document": Used for embeddings stored in a vector database for search use-cases.
827
- * - "search_query": Used for embeddings of search queries run against a vector DB to find relevant documents.
828
- * - "classification": Used for embeddings passed through a text classifier.
829
- * - "clustering": Used for embeddings run through a clustering algorithm.
830
- */
831
- inputType: z4.enum(["search_document", "search_query", "classification", "clustering"]).optional(),
832
- /**
833
- * Specifies how the API will handle inputs longer than the maximum token length.
834
- * Default is `END`.
835
- *
836
- * - "NONE": If selected, when the input exceeds the maximum input token length will return an error.
837
- * - "START": Will discard the start of the input until the remaining input is exactly the maximum input token length for the model.
838
- * - "END": Will discard the end of the input until the remaining input is exactly the maximum input token length for the model.
839
- */
840
- truncate: z4.enum(["NONE", "START", "END"]).optional(),
841
- /**
842
- * The number of dimensions of the output embedding.
843
- * Only available for `embed-v4.0` and newer models.
844
- *
845
- * Possible values are `256`, `512`, `1024`, and `1536`.
846
- * The default is `1536`.
847
- */
848
- outputDimension: z4.union([z4.literal(256), z4.literal(512), z4.literal(1024), z4.literal(1536)]).optional()
849
- });
850
-
851
- // src/cohere-embedding-model.ts
852
- var CohereEmbeddingModel = class {
853
- constructor(modelId, config) {
854
- this.specificationVersion = "v4";
855
- this.maxEmbeddingsPerCall = 96;
856
- this.supportsParallelCalls = true;
857
- this.modelId = modelId;
858
- this.config = config;
859
- }
860
- get provider() {
861
- return this.config.provider;
862
- }
863
- async doEmbed({
864
- values,
865
- headers,
866
- abortSignal,
867
- providerOptions
868
- }) {
869
- var _a;
870
- const embeddingOptions = await parseProviderOptions2({
871
- provider: "cohere",
872
- providerOptions,
873
- schema: cohereEmbeddingModelOptions
874
- });
875
- if (values.length > this.maxEmbeddingsPerCall) {
876
- throw new TooManyEmbeddingValuesForCallError({
877
- provider: this.provider,
878
- modelId: this.modelId,
879
- maxEmbeddingsPerCall: this.maxEmbeddingsPerCall,
880
- values
881
- });
882
- }
883
- const {
884
- responseHeaders,
885
- value: response,
886
- rawValue
887
- } = await postJsonToApi2({
888
- url: `${this.config.baseURL}/embed`,
889
- headers: combineHeaders2(this.config.headers(), headers),
890
- body: {
891
- model: this.modelId,
892
- // The AI SDK only supports 'float' embeddings. Note that the Cohere API
893
- // supports other embedding types, but they are not currently supported by the AI SDK.
894
- // https://docs.cohere.com/v2/reference/embed#request.body.embedding_types
895
- embedding_types: ["float"],
896
- texts: values,
897
- input_type: (_a = embeddingOptions == null ? void 0 : embeddingOptions.inputType) != null ? _a : "search_query",
898
- truncate: embeddingOptions == null ? void 0 : embeddingOptions.truncate,
899
- output_dimension: embeddingOptions == null ? void 0 : embeddingOptions.outputDimension
900
- },
901
- failedResponseHandler: cohereFailedResponseHandler,
902
- successfulResponseHandler: createJsonResponseHandler2(
903
- cohereTextEmbeddingResponseSchema
904
- ),
905
- abortSignal,
906
- fetch: this.config.fetch
907
- });
908
- return {
909
- warnings: [],
910
- embeddings: response.embeddings.float,
911
- usage: { tokens: response.meta.billed_units.input_tokens },
912
- response: { headers: responseHeaders, body: rawValue }
913
- };
914
- }
915
- };
916
- var cohereTextEmbeddingResponseSchema = z5.object({
917
- embeddings: z5.object({
918
- float: z5.array(z5.array(z5.number()))
919
- }),
920
- meta: z5.object({
921
- billed_units: z5.object({
922
- input_tokens: z5.number()
923
- })
924
- })
925
- });
926
-
927
- // src/reranking/cohere-reranking-model.ts
928
- import {
929
- combineHeaders as combineHeaders3,
930
- createJsonResponseHandler as createJsonResponseHandler3,
931
- parseProviderOptions as parseProviderOptions3,
932
- postJsonToApi as postJsonToApi3
933
- } from "@ai-sdk/provider-utils";
934
-
935
- // src/reranking/cohere-reranking-api.ts
936
- import { lazySchema, zodSchema } from "@ai-sdk/provider-utils";
937
- import { z as z6 } from "zod/v4";
938
- var cohereRerankingResponseSchema = lazySchema(
939
- () => zodSchema(
940
- z6.object({
941
- id: z6.string().nullish(),
942
- results: z6.array(
943
- z6.object({
944
- index: z6.number(),
945
- relevance_score: z6.number()
946
- })
947
- ),
948
- meta: z6.any()
949
- })
950
- )
951
- );
952
-
953
- // src/reranking/cohere-reranking-options.ts
954
- import { lazySchema as lazySchema2, zodSchema as zodSchema2 } from "@ai-sdk/provider-utils";
955
- import { z as z7 } from "zod/v4";
956
- var cohereRerankingModelOptionsSchema = lazySchema2(
957
- () => zodSchema2(
958
- z7.object({
959
- maxTokensPerDoc: z7.number().optional(),
960
- priority: z7.number().optional()
961
- })
962
- )
963
- );
964
-
965
- // src/reranking/cohere-reranking-model.ts
966
- var CohereRerankingModel = class {
967
- constructor(modelId, config) {
968
- this.specificationVersion = "v4";
969
- this.modelId = modelId;
970
- this.config = config;
971
- }
972
- get provider() {
973
- return this.config.provider;
974
- }
975
- // current implementation is based on v2 of the API: https://docs.cohere.com/v2/reference/rerank
976
- async doRerank({
977
- documents,
978
- headers,
979
- query,
980
- topN,
981
- abortSignal,
982
- providerOptions
983
- }) {
984
- var _a;
985
- const rerankingOptions = await parseProviderOptions3({
986
- provider: "cohere",
987
- providerOptions,
988
- schema: cohereRerankingModelOptionsSchema
989
- });
990
- const warnings = [];
991
- if (documents.type === "object") {
992
- warnings.push({
993
- type: "compatibility",
994
- feature: "object documents",
995
- details: "Object documents are converted to strings."
996
- });
997
- }
998
- const {
999
- responseHeaders,
1000
- value: response,
1001
- rawValue
1002
- } = await postJsonToApi3({
1003
- url: `${this.config.baseURL}/rerank`,
1004
- headers: combineHeaders3(this.config.headers(), headers),
1005
- body: {
1006
- model: this.modelId,
1007
- query,
1008
- documents: documents.type === "text" ? documents.values : documents.values.map((value) => JSON.stringify(value)),
1009
- top_n: topN,
1010
- max_tokens_per_doc: rerankingOptions == null ? void 0 : rerankingOptions.maxTokensPerDoc,
1011
- priority: rerankingOptions == null ? void 0 : rerankingOptions.priority
1012
- },
1013
- failedResponseHandler: cohereFailedResponseHandler,
1014
- successfulResponseHandler: createJsonResponseHandler3(
1015
- cohereRerankingResponseSchema
1016
- ),
1017
- abortSignal,
1018
- fetch: this.config.fetch
1019
- });
1020
- return {
1021
- ranking: response.results.map((result) => ({
1022
- index: result.index,
1023
- relevanceScore: result.relevance_score
1024
- })),
1025
- warnings,
1026
- response: {
1027
- id: (_a = response.id) != null ? _a : void 0,
1028
- headers: responseHeaders,
1029
- body: rawValue
1030
- }
1031
- };
1032
- }
1033
- };
1034
-
1035
- // src/version.ts
1036
- var VERSION = true ? "4.0.0-beta.9" : "0.0.0-test";
1037
-
1038
- // src/cohere-provider.ts
1039
- function createCohere(options = {}) {
1040
- var _a;
1041
- const baseURL = (_a = withoutTrailingSlash(options.baseURL)) != null ? _a : "https://api.cohere.com/v2";
1042
- const getHeaders = () => withUserAgentSuffix(
1043
- {
1044
- Authorization: `Bearer ${loadApiKey({
1045
- apiKey: options.apiKey,
1046
- environmentVariableName: "COHERE_API_KEY",
1047
- description: "Cohere"
1048
- })}`,
1049
- ...options.headers
1050
- },
1051
- `ai-sdk/cohere/${VERSION}`
1052
- );
1053
- const createChatModel = (modelId) => {
1054
- var _a2;
1055
- return new CohereChatLanguageModel(modelId, {
1056
- provider: "cohere.chat",
1057
- baseURL,
1058
- headers: getHeaders,
1059
- fetch: options.fetch,
1060
- generateId: (_a2 = options.generateId) != null ? _a2 : generateId
1061
- });
1062
- };
1063
- const createEmbeddingModel = (modelId) => new CohereEmbeddingModel(modelId, {
1064
- provider: "cohere.textEmbedding",
1065
- baseURL,
1066
- headers: getHeaders,
1067
- fetch: options.fetch
1068
- });
1069
- const createRerankingModel = (modelId) => new CohereRerankingModel(modelId, {
1070
- provider: "cohere.reranking",
1071
- baseURL,
1072
- headers: getHeaders,
1073
- fetch: options.fetch
1074
- });
1075
- const provider = function(modelId) {
1076
- if (new.target) {
1077
- throw new Error(
1078
- "The Cohere model function cannot be called with the new keyword."
1079
- );
1080
- }
1081
- return createChatModel(modelId);
1082
- };
1083
- provider.specificationVersion = "v4";
1084
- provider.languageModel = createChatModel;
1085
- provider.embedding = createEmbeddingModel;
1086
- provider.embeddingModel = createEmbeddingModel;
1087
- provider.textEmbedding = createEmbeddingModel;
1088
- provider.textEmbeddingModel = createEmbeddingModel;
1089
- provider.reranking = createRerankingModel;
1090
- provider.rerankingModel = createRerankingModel;
1091
- provider.imageModel = (modelId) => {
1092
- throw new NoSuchModelError({ modelId, modelType: "imageModel" });
1093
- };
1094
- return provider;
1095
- }
1096
- var cohere = createCohere();
1097
- export {
1098
- VERSION,
1099
- cohere,
1100
- createCohere
1101
- };
1102
- //# sourceMappingURL=index.mjs.map