@ai-sdk/cohere 4.0.0-beta.20 → 4.0.0-beta.21

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