@ai-sdk/cohere 2.0.8 → 2.0.9

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 CHANGED
@@ -13,15 +13,33 @@ import {
13
13
  combineHeaders,
14
14
  createEventSourceResponseHandler,
15
15
  createJsonResponseHandler,
16
+ parseProviderOptions,
16
17
  postJsonToApi
17
18
  } from "@ai-sdk/provider-utils";
18
- import { z as z2 } from "zod/v4";
19
+ import { z as z3 } from "zod/v4";
20
+
21
+ // src/cohere-chat-options.ts
22
+ import { z } from "zod/v4";
23
+ var cohereChatModelOptions = z.object({
24
+ /**
25
+ * Configuration for reasoning features (optional)
26
+ *
27
+ * Can be set to an object with the two properties `type` and `tokenBudget`. `type` can be set to `'enabled'` or `'disabled'` (defaults to `'enabled'`).
28
+ * `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
29
+ *
30
+ * @see https://docs.cohere.com/reference/chat#request.body.thinking
31
+ */
32
+ thinking: z.object({
33
+ type: z.enum(["enabled", "disabled"]).optional(),
34
+ tokenBudget: z.number().optional()
35
+ }).optional()
36
+ });
19
37
 
20
38
  // src/cohere-error.ts
21
39
  import { createJsonErrorResponseHandler } from "@ai-sdk/provider-utils";
22
- import { z } from "zod/v4";
23
- var cohereErrorDataSchema = z.object({
24
- message: z.string()
40
+ import { z as z2 } from "zod/v4";
41
+ var cohereErrorDataSchema = z2.object({
42
+ message: z2.string()
25
43
  });
26
44
  var cohereFailedResponseHandler = createJsonErrorResponseHandler({
27
45
  errorSchema: cohereErrorDataSchema,
@@ -232,7 +250,7 @@ var CohereChatLanguageModel = class {
232
250
  get provider() {
233
251
  return this.config.provider;
234
252
  }
235
- getArgs({
253
+ async getArgs({
236
254
  prompt,
237
255
  maxOutputTokens,
238
256
  temperature,
@@ -244,8 +262,15 @@ var CohereChatLanguageModel = class {
244
262
  responseFormat,
245
263
  seed,
246
264
  tools,
247
- toolChoice
265
+ toolChoice,
266
+ providerOptions
248
267
  }) {
268
+ var _a, _b;
269
+ const cohereOptions = (_a = await parseProviderOptions({
270
+ provider: "cohere",
271
+ providerOptions,
272
+ schema: cohereChatModelOptions
273
+ })) != null ? _a : {};
249
274
  const {
250
275
  messages: chatPrompt,
251
276
  documents: cohereDocuments,
@@ -277,14 +302,21 @@ var CohereChatLanguageModel = class {
277
302
  tools: cohereTools,
278
303
  tool_choice: cohereToolChoice,
279
304
  // documents for RAG:
280
- ...cohereDocuments.length > 0 && { documents: cohereDocuments }
305
+ ...cohereDocuments.length > 0 && { documents: cohereDocuments },
306
+ // reasoning
307
+ ...cohereOptions.thinking && {
308
+ thinking: {
309
+ type: (_b = cohereOptions.thinking.type) != null ? _b : "enabled",
310
+ token_budget: cohereOptions.thinking.tokenBudget
311
+ }
312
+ }
281
313
  },
282
314
  warnings: [...toolWarnings, ...promptWarnings]
283
315
  };
284
316
  }
285
317
  async doGenerate(options) {
286
- var _a, _b, _c, _d, _e, _f, _g, _h, _i;
287
- const { args, warnings } = this.getArgs(options);
318
+ var _a, _b, _c, _d, _e, _f;
319
+ const { args, warnings } = await this.getArgs(options);
288
320
  const {
289
321
  responseHeaders,
290
322
  value: response,
@@ -301,16 +333,23 @@ var CohereChatLanguageModel = class {
301
333
  fetch: this.config.fetch
302
334
  });
303
335
  const content = [];
304
- if (((_b = (_a = response.message.content) == null ? void 0 : _a[0]) == null ? void 0 : _b.text) != null && ((_d = (_c = response.message.content) == null ? void 0 : _c[0]) == null ? void 0 : _d.text.length) > 0) {
305
- content.push({ type: "text", text: response.message.content[0].text });
336
+ for (const item of (_a = response.message.content) != null ? _a : []) {
337
+ if (item.type === "text" && item.text.length > 0) {
338
+ content.push({ type: "text", text: item.text });
339
+ continue;
340
+ }
341
+ if (item.type === "thinking" && item.thinking.length > 0) {
342
+ content.push({ type: "reasoning", text: item.thinking });
343
+ continue;
344
+ }
306
345
  }
307
- for (const citation of (_e = response.message.citations) != null ? _e : []) {
346
+ for (const citation of (_b = response.message.citations) != null ? _b : []) {
308
347
  content.push({
309
348
  type: "source",
310
349
  sourceType: "document",
311
350
  id: this.config.generateId(),
312
351
  mediaType: "text/plain",
313
- title: ((_g = (_f = citation.sources[0]) == null ? void 0 : _f.document) == null ? void 0 : _g.title) || "Document",
352
+ title: ((_d = (_c = citation.sources[0]) == null ? void 0 : _c.document) == null ? void 0 : _d.title) || "Document",
314
353
  providerMetadata: {
315
354
  cohere: {
316
355
  start: citation.start,
@@ -322,7 +361,7 @@ var CohereChatLanguageModel = class {
322
361
  }
323
362
  });
324
363
  }
325
- for (const toolCall of (_h = response.message.tool_calls) != null ? _h : []) {
364
+ for (const toolCall of (_e = response.message.tool_calls) != null ? _e : []) {
326
365
  content.push({
327
366
  type: "tool-call",
328
367
  toolCallId: toolCall.id,
@@ -343,7 +382,7 @@ var CohereChatLanguageModel = class {
343
382
  request: { body: args },
344
383
  response: {
345
384
  // TODO timestamp, model id
346
- id: (_i = response.generation_id) != null ? _i : void 0,
385
+ id: (_f = response.generation_id) != null ? _f : void 0,
347
386
  headers: responseHeaders,
348
387
  body: rawResponse
349
388
  },
@@ -351,7 +390,7 @@ var CohereChatLanguageModel = class {
351
390
  };
352
391
  }
353
392
  async doStream(options) {
354
- const { args, warnings } = this.getArgs(options);
393
+ const { args, warnings } = await this.getArgs(options);
355
394
  const { responseHeaders, value: response } = await postJsonToApi({
356
395
  url: `${this.config.baseURL}/chat`,
357
396
  headers: combineHeaders(this.config.headers(), options.headers),
@@ -370,6 +409,7 @@ var CohereChatLanguageModel = class {
370
409
  totalTokens: void 0
371
410
  };
372
411
  let pendingToolCall = null;
412
+ let isActiveReasoning = false;
373
413
  return {
374
414
  stream: response.pipeThrough(
375
415
  new TransformStream({
@@ -390,6 +430,14 @@ var CohereChatLanguageModel = class {
390
430
  const type = value.type;
391
431
  switch (type) {
392
432
  case "content-start": {
433
+ if (value.delta.message.content.type === "thinking") {
434
+ controller.enqueue({
435
+ type: "reasoning-start",
436
+ id: String(value.index)
437
+ });
438
+ isActiveReasoning = true;
439
+ return;
440
+ }
393
441
  controller.enqueue({
394
442
  type: "text-start",
395
443
  id: String(value.index)
@@ -397,6 +445,14 @@ var CohereChatLanguageModel = class {
397
445
  return;
398
446
  }
399
447
  case "content-delta": {
448
+ if ("thinking" in value.delta.message.content) {
449
+ controller.enqueue({
450
+ type: "reasoning-delta",
451
+ id: String(value.index),
452
+ delta: value.delta.message.content.thinking
453
+ });
454
+ return;
455
+ }
400
456
  controller.enqueue({
401
457
  type: "text-delta",
402
458
  id: String(value.index),
@@ -405,6 +461,14 @@ var CohereChatLanguageModel = class {
405
461
  return;
406
462
  }
407
463
  case "content-end": {
464
+ if (isActiveReasoning) {
465
+ controller.enqueue({
466
+ type: "reasoning-end",
467
+ id: String(value.index)
468
+ });
469
+ isActiveReasoning = false;
470
+ return;
471
+ }
408
472
  controller.enqueue({
409
473
  type: "text-end",
410
474
  id: String(value.index)
@@ -500,120 +564,145 @@ var CohereChatLanguageModel = class {
500
564
  };
501
565
  }
502
566
  };
503
- var cohereChatResponseSchema = z2.object({
504
- generation_id: z2.string().nullish(),
505
- message: z2.object({
506
- role: z2.string(),
507
- content: z2.array(
508
- z2.object({
509
- type: z2.string(),
510
- text: z2.string()
511
- })
567
+ var cohereChatResponseSchema = z3.object({
568
+ generation_id: z3.string().nullish(),
569
+ message: z3.object({
570
+ role: z3.string(),
571
+ content: z3.array(
572
+ z3.union([
573
+ z3.object({
574
+ type: z3.literal("text"),
575
+ text: z3.string()
576
+ }),
577
+ z3.object({
578
+ type: z3.literal("thinking"),
579
+ thinking: z3.string()
580
+ })
581
+ ])
512
582
  ).nullish(),
513
- tool_plan: z2.string().nullish(),
514
- tool_calls: z2.array(
515
- z2.object({
516
- id: z2.string(),
517
- type: z2.literal("function"),
518
- function: z2.object({
519
- name: z2.string(),
520
- arguments: z2.string()
583
+ tool_plan: z3.string().nullish(),
584
+ tool_calls: z3.array(
585
+ z3.object({
586
+ id: z3.string(),
587
+ type: z3.literal("function"),
588
+ function: z3.object({
589
+ name: z3.string(),
590
+ arguments: z3.string()
521
591
  })
522
592
  })
523
593
  ).nullish(),
524
- citations: z2.array(
525
- z2.object({
526
- start: z2.number(),
527
- end: z2.number(),
528
- text: z2.string(),
529
- sources: z2.array(
530
- z2.object({
531
- type: z2.string().optional(),
532
- id: z2.string().optional(),
533
- document: z2.object({
534
- id: z2.string().optional(),
535
- text: z2.string(),
536
- title: z2.string()
594
+ citations: z3.array(
595
+ z3.object({
596
+ start: z3.number(),
597
+ end: z3.number(),
598
+ text: z3.string(),
599
+ sources: z3.array(
600
+ z3.object({
601
+ type: z3.string().optional(),
602
+ id: z3.string().optional(),
603
+ document: z3.object({
604
+ id: z3.string().optional(),
605
+ text: z3.string(),
606
+ title: z3.string()
537
607
  })
538
608
  })
539
609
  ),
540
- type: z2.string().optional()
610
+ type: z3.string().optional()
541
611
  })
542
612
  ).nullish()
543
613
  }),
544
- finish_reason: z2.string(),
545
- usage: z2.object({
546
- billed_units: z2.object({
547
- input_tokens: z2.number(),
548
- output_tokens: z2.number()
614
+ finish_reason: z3.string(),
615
+ usage: z3.object({
616
+ billed_units: z3.object({
617
+ input_tokens: z3.number(),
618
+ output_tokens: z3.number()
549
619
  }),
550
- tokens: z2.object({
551
- input_tokens: z2.number(),
552
- output_tokens: z2.number()
620
+ tokens: z3.object({
621
+ input_tokens: z3.number(),
622
+ output_tokens: z3.number()
553
623
  })
554
624
  })
555
625
  });
556
- var cohereChatChunkSchema = z2.discriminatedUnion("type", [
557
- z2.object({
558
- type: z2.literal("citation-start")
626
+ var cohereChatChunkSchema = z3.discriminatedUnion("type", [
627
+ z3.object({
628
+ type: z3.literal("citation-start")
559
629
  }),
560
- z2.object({
561
- type: z2.literal("citation-end")
630
+ z3.object({
631
+ type: z3.literal("citation-end")
562
632
  }),
563
- z2.object({
564
- type: z2.literal("content-start"),
565
- index: z2.number()
633
+ z3.object({
634
+ type: z3.literal("content-start"),
635
+ index: z3.number(),
636
+ delta: z3.object({
637
+ message: z3.object({
638
+ content: z3.union([
639
+ z3.object({
640
+ type: z3.literal("text"),
641
+ text: z3.string()
642
+ }),
643
+ z3.object({
644
+ type: z3.literal("thinking"),
645
+ thinking: z3.string()
646
+ })
647
+ ])
648
+ })
649
+ })
566
650
  }),
567
- z2.object({
568
- type: z2.literal("content-delta"),
569
- index: z2.number(),
570
- delta: z2.object({
571
- message: z2.object({
572
- content: z2.object({
573
- text: z2.string()
574
- })
651
+ z3.object({
652
+ type: z3.literal("content-delta"),
653
+ index: z3.number(),
654
+ delta: z3.object({
655
+ message: z3.object({
656
+ content: z3.union([
657
+ z3.object({
658
+ text: z3.string()
659
+ }),
660
+ z3.object({
661
+ thinking: z3.string()
662
+ })
663
+ ])
575
664
  })
576
665
  })
577
666
  }),
578
- z2.object({
579
- type: z2.literal("content-end"),
580
- index: z2.number()
667
+ z3.object({
668
+ type: z3.literal("content-end"),
669
+ index: z3.number()
581
670
  }),
582
- z2.object({
583
- type: z2.literal("message-start"),
584
- id: z2.string().nullish()
671
+ z3.object({
672
+ type: z3.literal("message-start"),
673
+ id: z3.string().nullish()
585
674
  }),
586
- z2.object({
587
- type: z2.literal("message-end"),
588
- delta: z2.object({
589
- finish_reason: z2.string(),
590
- usage: z2.object({
591
- tokens: z2.object({
592
- input_tokens: z2.number(),
593
- output_tokens: z2.number()
675
+ z3.object({
676
+ type: z3.literal("message-end"),
677
+ delta: z3.object({
678
+ finish_reason: z3.string(),
679
+ usage: z3.object({
680
+ tokens: z3.object({
681
+ input_tokens: z3.number(),
682
+ output_tokens: z3.number()
594
683
  })
595
684
  })
596
685
  })
597
686
  }),
598
687
  // https://docs.cohere.com/v2/docs/streaming#tool-use-stream-events-for-tool-calling
599
- z2.object({
600
- type: z2.literal("tool-plan-delta"),
601
- delta: z2.object({
602
- message: z2.object({
603
- tool_plan: z2.string()
688
+ z3.object({
689
+ type: z3.literal("tool-plan-delta"),
690
+ delta: z3.object({
691
+ message: z3.object({
692
+ tool_plan: z3.string()
604
693
  })
605
694
  })
606
695
  }),
607
- z2.object({
608
- type: z2.literal("tool-call-start"),
609
- delta: z2.object({
610
- message: z2.object({
611
- tool_calls: z2.object({
612
- id: z2.string(),
613
- type: z2.literal("function"),
614
- function: z2.object({
615
- name: z2.string(),
616
- arguments: z2.string()
696
+ z3.object({
697
+ type: z3.literal("tool-call-start"),
698
+ delta: z3.object({
699
+ message: z3.object({
700
+ tool_calls: z3.object({
701
+ id: z3.string(),
702
+ type: z3.literal("function"),
703
+ function: z3.object({
704
+ name: z3.string(),
705
+ arguments: z3.string()
617
706
  })
618
707
  })
619
708
  })
@@ -622,20 +711,20 @@ var cohereChatChunkSchema = z2.discriminatedUnion("type", [
622
711
  // A single tool call's `arguments` stream in chunks and must be accumulated
623
712
  // in a string and so the full tool object info can only be parsed once we see
624
713
  // `tool-call-end`.
625
- z2.object({
626
- type: z2.literal("tool-call-delta"),
627
- delta: z2.object({
628
- message: z2.object({
629
- tool_calls: z2.object({
630
- function: z2.object({
631
- arguments: z2.string()
714
+ z3.object({
715
+ type: z3.literal("tool-call-delta"),
716
+ delta: z3.object({
717
+ message: z3.object({
718
+ tool_calls: z3.object({
719
+ function: z3.object({
720
+ arguments: z3.string()
632
721
  })
633
722
  })
634
723
  })
635
724
  })
636
725
  }),
637
- z2.object({
638
- type: z2.literal("tool-call-end")
726
+ z3.object({
727
+ type: z3.literal("tool-call-end")
639
728
  })
640
729
  ]);
641
730
 
@@ -646,14 +735,14 @@ import {
646
735
  import {
647
736
  combineHeaders as combineHeaders2,
648
737
  createJsonResponseHandler as createJsonResponseHandler2,
649
- parseProviderOptions,
738
+ parseProviderOptions as parseProviderOptions2,
650
739
  postJsonToApi as postJsonToApi2
651
740
  } from "@ai-sdk/provider-utils";
652
- import { z as z4 } from "zod/v4";
741
+ import { z as z5 } from "zod/v4";
653
742
 
654
743
  // src/cohere-embedding-options.ts
655
- import { z as z3 } from "zod/v4";
656
- var cohereEmbeddingOptions = z3.object({
744
+ import { z as z4 } from "zod/v4";
745
+ var cohereEmbeddingOptions = z4.object({
657
746
  /**
658
747
  * Specifies the type of input passed to the model. Default is `search_query`.
659
748
  *
@@ -662,7 +751,7 @@ var cohereEmbeddingOptions = z3.object({
662
751
  * - "classification": Used for embeddings passed through a text classifier.
663
752
  * - "clustering": Used for embeddings run through a clustering algorithm.
664
753
  */
665
- inputType: z3.enum(["search_document", "search_query", "classification", "clustering"]).optional(),
754
+ inputType: z4.enum(["search_document", "search_query", "classification", "clustering"]).optional(),
666
755
  /**
667
756
  * Specifies how the API will handle inputs longer than the maximum token length.
668
757
  * Default is `END`.
@@ -671,7 +760,7 @@ var cohereEmbeddingOptions = z3.object({
671
760
  * - "START": Will discard the start of the input until the remaining input is exactly the maximum input token length for the model.
672
761
  * - "END": Will discard the end of the input until the remaining input is exactly the maximum input token length for the model.
673
762
  */
674
- truncate: z3.enum(["NONE", "START", "END"]).optional()
763
+ truncate: z4.enum(["NONE", "START", "END"]).optional()
675
764
  });
676
765
 
677
766
  // src/cohere-embedding-model.ts
@@ -693,7 +782,7 @@ var CohereEmbeddingModel = class {
693
782
  providerOptions
694
783
  }) {
695
784
  var _a;
696
- const embeddingOptions = await parseProviderOptions({
785
+ const embeddingOptions = await parseProviderOptions2({
697
786
  provider: "cohere",
698
787
  providerOptions,
699
788
  schema: cohereEmbeddingOptions
@@ -737,13 +826,13 @@ var CohereEmbeddingModel = class {
737
826
  };
738
827
  }
739
828
  };
740
- var cohereTextEmbeddingResponseSchema = z4.object({
741
- embeddings: z4.object({
742
- float: z4.array(z4.array(z4.number()))
829
+ var cohereTextEmbeddingResponseSchema = z5.object({
830
+ embeddings: z5.object({
831
+ float: z5.array(z5.array(z5.number()))
743
832
  }),
744
- meta: z4.object({
745
- billed_units: z4.object({
746
- input_tokens: z4.number()
833
+ meta: z5.object({
834
+ billed_units: z5.object({
835
+ input_tokens: z5.number()
747
836
  })
748
837
  })
749
838
  });