@ai-sdk/cohere 2.0.48 → 2.0.49

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
@@ -19,7 +19,7 @@ import {
19
19
  parseProviderOptions as parseProviderOptions2,
20
20
  postJsonToApi
21
21
  } from "@ai-sdk/provider-utils";
22
- import { z as z3 } from "zod/v4";
22
+ import { z as z4 } from "zod/v4";
23
23
 
24
24
  // src/cohere-chat-options.ts
25
25
  import {
@@ -58,17 +58,67 @@ var cohereImagePartProviderOptions = lazySchema(
58
58
  )
59
59
  );
60
60
 
61
+ // src/cohere-citations.ts
62
+ import { z as z2 } from "zod/v4";
63
+ var cohereCitationSchema = z2.object({
64
+ start: z2.number().nullish(),
65
+ end: z2.number().nullish(),
66
+ text: z2.string().nullish(),
67
+ sources: z2.array(
68
+ z2.object({
69
+ type: z2.string().nullish(),
70
+ id: z2.string().nullish(),
71
+ // Documents are arbitrary objects; only the fields the provider maps
72
+ // are typed, everything else is passed through as provider metadata.
73
+ document: z2.looseObject({
74
+ id: z2.string().nullish(),
75
+ text: z2.string().nullish(),
76
+ title: z2.string().nullish()
77
+ }).nullish(),
78
+ tool_output: z2.looseObject({}).nullish()
79
+ })
80
+ ).nullish(),
81
+ type: z2.string().nullish()
82
+ });
83
+ function mapCohereCitationToSource(citation, generateId3) {
84
+ var _a, _b;
85
+ const document = (_b = (_a = citation.sources) == null ? void 0 : _a.find(
86
+ (source) => source.document != null
87
+ )) == null ? void 0 : _b.document;
88
+ if (document == null) {
89
+ return void 0;
90
+ }
91
+ return {
92
+ type: "source",
93
+ sourceType: "document",
94
+ id: generateId3(),
95
+ mediaType: "text/plain",
96
+ title: document.title || "Document",
97
+ providerMetadata: {
98
+ cohere: {
99
+ ...citation.start != null && { start: citation.start },
100
+ ...citation.end != null && { end: citation.end },
101
+ ...citation.text != null && { text: citation.text },
102
+ ...citation.sources != null && {
103
+ sources: citation.sources
104
+ },
105
+ ...citation.type != null && { citationType: citation.type }
106
+ }
107
+ }
108
+ };
109
+ }
110
+
61
111
  // src/cohere-error.ts
62
112
  import {
63
113
  lazySchema as lazySchema2,
64
114
  zodSchema as zodSchema2,
65
115
  createJsonErrorResponseHandler
66
116
  } from "@ai-sdk/provider-utils";
67
- import { z as z2 } from "zod/v4";
117
+ import { z as z3 } from "zod/v4";
68
118
  var cohereErrorDataSchema = lazySchema2(
69
119
  () => zodSchema2(
70
- z2.object({
71
- message: z2.string()
120
+ z3.object({
121
+ message: z3.string()
72
122
  })
73
123
  )
74
124
  );
@@ -400,7 +450,7 @@ var CohereChatLanguageModel = class {
400
450
  };
401
451
  }
402
452
  async doGenerate(options) {
403
- var _a, _b, _c, _d, _e, _f;
453
+ var _a, _b, _c, _d, _e;
404
454
  const { args, warnings } = await this.getArgs(options);
405
455
  const {
406
456
  responseHeaders,
@@ -429,24 +479,15 @@ var CohereChatLanguageModel = class {
429
479
  }
430
480
  }
431
481
  for (const citation of (_b = response.message.citations) != null ? _b : []) {
432
- content.push({
433
- type: "source",
434
- sourceType: "document",
435
- id: this.config.generateId(),
436
- mediaType: "text/plain",
437
- title: ((_d = (_c = citation.sources[0]) == null ? void 0 : _c.document) == null ? void 0 : _d.title) || "Document",
438
- providerMetadata: {
439
- cohere: {
440
- start: citation.start,
441
- end: citation.end,
442
- text: citation.text,
443
- sources: citation.sources,
444
- ...citation.type && { citationType: citation.type }
445
- }
446
- }
447
- });
482
+ const source = mapCohereCitationToSource(
483
+ citation,
484
+ () => this.config.generateId()
485
+ );
486
+ if (source != null) {
487
+ content.push(source);
488
+ }
448
489
  }
449
- for (const toolCall of (_e = response.message.tool_calls) != null ? _e : []) {
490
+ for (const toolCall of (_c = response.message.tool_calls) != null ? _c : []) {
450
491
  content.push({
451
492
  type: "tool-call",
452
493
  toolCallId: toolCall.id,
@@ -459,15 +500,13 @@ var CohereChatLanguageModel = class {
459
500
  return {
460
501
  content,
461
502
  finishReason: mapCohereFinishReason(response.finish_reason),
462
- usage: {
463
- inputTokens: response.usage.tokens.input_tokens,
464
- outputTokens: response.usage.tokens.output_tokens,
465
- totalTokens: response.usage.tokens.input_tokens + response.usage.tokens.output_tokens
466
- },
503
+ usage: convertCohereUsage(response.usage),
467
504
  request: { body: args },
468
505
  response: {
469
506
  // TODO timestamp, model id
470
- id: (_f = response.generation_id) != null ? _f : void 0,
507
+ // Cohere v2 returns `id`; `generation_id` is the v1 field, kept only
508
+ // for compatibility with proxies/mocks that still return it.
509
+ id: (_e = (_d = response.id) != null ? _d : response.generation_id) != null ? _e : void 0,
471
510
  headers: responseHeaders,
472
511
  body: rawResponse
473
512
  },
@@ -488,13 +527,10 @@ var CohereChatLanguageModel = class {
488
527
  fetch: this.config.fetch
489
528
  });
490
529
  let finishReason = "unknown";
491
- const usage = {
492
- inputTokens: void 0,
493
- outputTokens: void 0,
494
- totalTokens: void 0
495
- };
530
+ let rawUsage = void 0;
496
531
  let pendingToolCall = null;
497
532
  let isActiveReasoning = false;
533
+ const generateId3 = this.config.generateId;
498
534
  return {
499
535
  stream: response.pipeThrough(
500
536
  new TransformStream({
@@ -502,7 +538,7 @@ var CohereChatLanguageModel = class {
502
538
  controller.enqueue({ type: "stream-start", warnings });
503
539
  },
504
540
  transform(chunk, controller) {
505
- var _a, _b;
541
+ var _a, _b, _c, _d, _e;
506
542
  if (options.includeRawChunks) {
507
543
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
508
544
  }
@@ -560,6 +596,22 @@ var CohereChatLanguageModel = class {
560
596
  });
561
597
  return;
562
598
  }
599
+ case "citation-start": {
600
+ const citations = (_b = (_a = value.delta) == null ? void 0 : _a.message) == null ? void 0 : _b.citations;
601
+ if (citations == null) {
602
+ return;
603
+ }
604
+ for (const citation of Array.isArray(citations) ? citations : [citations]) {
605
+ const source = mapCohereCitationToSource(
606
+ citation,
607
+ generateId3
608
+ );
609
+ if (source != null) {
610
+ controller.enqueue(source);
611
+ }
612
+ }
613
+ return;
614
+ }
563
615
  case "tool-call-start": {
564
616
  const toolId = value.delta.message.tool_calls.id;
565
617
  const toolName = value.delta.message.tool_calls.function.name;
@@ -607,7 +659,7 @@ var CohereChatLanguageModel = class {
607
659
  toolCallId: pendingToolCall.id,
608
660
  toolName: pendingToolCall.name,
609
661
  input: JSON.stringify(
610
- JSON.parse(((_a = pendingToolCall.arguments) == null ? void 0 : _a.trim()) || "{}")
662
+ JSON.parse(((_c = pendingToolCall.arguments) == null ? void 0 : _c.trim()) || "{}")
611
663
  )
612
664
  });
613
665
  pendingToolCall.hasFinished = true;
@@ -618,16 +670,13 @@ var CohereChatLanguageModel = class {
618
670
  case "message-start": {
619
671
  controller.enqueue({
620
672
  type: "response-metadata",
621
- id: (_b = value.id) != null ? _b : void 0
673
+ id: (_d = value.id) != null ? _d : void 0
622
674
  });
623
675
  return;
624
676
  }
625
677
  case "message-end": {
626
678
  finishReason = mapCohereFinishReason(value.delta.finish_reason);
627
- const tokens = value.delta.usage.tokens;
628
- usage.inputTokens = tokens.input_tokens;
629
- usage.outputTokens = tokens.output_tokens;
630
- usage.totalTokens = tokens.input_tokens + tokens.output_tokens;
679
+ rawUsage = (_e = value.delta.usage) != null ? _e : void 0;
631
680
  return;
632
681
  }
633
682
  default: {
@@ -638,7 +687,7 @@ var CohereChatLanguageModel = class {
638
687
  controller.enqueue({
639
688
  type: "finish",
640
689
  finishReason,
641
- usage
690
+ usage: convertCohereUsage(rawUsage)
642
691
  });
643
692
  }
644
693
  })
@@ -648,151 +697,148 @@ var CohereChatLanguageModel = class {
648
697
  };
649
698
  }
650
699
  };
700
+ var cohereUsageSchema = () => z4.looseObject({
701
+ billed_units: z4.looseObject({
702
+ input_tokens: z4.number().nullish(),
703
+ output_tokens: z4.number().nullish()
704
+ }).nullish(),
705
+ tokens: z4.looseObject({
706
+ input_tokens: z4.number().nullish(),
707
+ output_tokens: z4.number().nullish()
708
+ }).nullish()
709
+ });
710
+ function convertCohereUsage(usage) {
711
+ var _a, _b, _c, _d;
712
+ const inputTokens = (_b = (_a = usage == null ? void 0 : usage.tokens) == null ? void 0 : _a.input_tokens) != null ? _b : void 0;
713
+ const outputTokens = (_d = (_c = usage == null ? void 0 : usage.tokens) == null ? void 0 : _c.output_tokens) != null ? _d : void 0;
714
+ return {
715
+ inputTokens,
716
+ outputTokens,
717
+ totalTokens: inputTokens != null && outputTokens != null ? inputTokens + outputTokens : void 0
718
+ };
719
+ }
651
720
  var cohereChatResponseSchema = lazySchema3(
652
721
  () => zodSchema3(
653
- z3.object({
654
- generation_id: z3.string().nullish(),
655
- message: z3.object({
656
- role: z3.string(),
657
- content: z3.array(
658
- z3.union([
659
- z3.object({
660
- type: z3.literal("text"),
661
- text: z3.string()
722
+ z4.object({
723
+ id: z4.string().nullish(),
724
+ generation_id: z4.string().nullish(),
725
+ message: z4.object({
726
+ role: z4.string(),
727
+ content: z4.array(
728
+ z4.union([
729
+ z4.object({
730
+ type: z4.literal("text"),
731
+ text: z4.string()
662
732
  }),
663
- z3.object({
664
- type: z3.literal("thinking"),
665
- thinking: z3.string()
733
+ z4.object({
734
+ type: z4.literal("thinking"),
735
+ thinking: z4.string()
666
736
  })
667
737
  ])
668
738
  ).nullish(),
669
- tool_plan: z3.string().nullish(),
670
- tool_calls: z3.array(
671
- z3.object({
672
- id: z3.string(),
673
- type: z3.literal("function"),
674
- function: z3.object({
675
- name: z3.string(),
676
- arguments: z3.string()
739
+ tool_plan: z4.string().nullish(),
740
+ tool_calls: z4.array(
741
+ z4.object({
742
+ id: z4.string(),
743
+ type: z4.literal("function"),
744
+ function: z4.object({
745
+ name: z4.string(),
746
+ arguments: z4.string()
677
747
  })
678
748
  })
679
749
  ).nullish(),
680
- citations: z3.array(
681
- z3.object({
682
- start: z3.number(),
683
- end: z3.number(),
684
- text: z3.string(),
685
- sources: z3.array(
686
- z3.object({
687
- type: z3.string().optional(),
688
- id: z3.string().optional(),
689
- document: z3.object({
690
- id: z3.string().optional(),
691
- text: z3.string(),
692
- title: z3.string()
693
- })
694
- })
695
- ),
696
- type: z3.string().optional()
697
- })
698
- ).nullish()
750
+ citations: z4.array(cohereCitationSchema).nullish()
699
751
  }),
700
- finish_reason: z3.string(),
701
- usage: z3.object({
702
- billed_units: z3.object({
703
- input_tokens: z3.number(),
704
- output_tokens: z3.number()
705
- }),
706
- tokens: z3.object({
707
- input_tokens: z3.number(),
708
- output_tokens: z3.number()
709
- })
710
- })
752
+ finish_reason: z4.string(),
753
+ usage: cohereUsageSchema().nullish()
711
754
  })
712
755
  )
713
756
  );
714
757
  var cohereChatChunkSchema = lazySchema3(
715
758
  () => zodSchema3(
716
- z3.discriminatedUnion("type", [
717
- z3.object({
718
- type: z3.literal("citation-start")
759
+ z4.discriminatedUnion("type", [
760
+ // `citation-start` events can arrive without citations, and a single
761
+ // citation may be sent instead of an array.
762
+ z4.object({
763
+ type: z4.literal("citation-start"),
764
+ index: z4.number().nullish(),
765
+ delta: z4.object({
766
+ message: z4.object({
767
+ citations: z4.union([cohereCitationSchema, z4.array(cohereCitationSchema)]).nullish()
768
+ }).nullish()
769
+ }).nullish()
719
770
  }),
720
- z3.object({
721
- type: z3.literal("citation-end")
771
+ z4.object({
772
+ type: z4.literal("citation-end")
722
773
  }),
723
- z3.object({
724
- type: z3.literal("content-start"),
725
- index: z3.number(),
726
- delta: z3.object({
727
- message: z3.object({
728
- content: z3.union([
729
- z3.object({
730
- type: z3.literal("text"),
731
- text: z3.string()
774
+ z4.object({
775
+ type: z4.literal("content-start"),
776
+ index: z4.number(),
777
+ delta: z4.object({
778
+ message: z4.object({
779
+ content: z4.union([
780
+ z4.object({
781
+ type: z4.literal("text"),
782
+ text: z4.string()
732
783
  }),
733
- z3.object({
734
- type: z3.literal("thinking"),
735
- thinking: z3.string()
784
+ z4.object({
785
+ type: z4.literal("thinking"),
786
+ thinking: z4.string()
736
787
  })
737
788
  ])
738
789
  })
739
790
  })
740
791
  }),
741
- z3.object({
742
- type: z3.literal("content-delta"),
743
- index: z3.number(),
744
- delta: z3.object({
745
- message: z3.object({
746
- content: z3.union([
747
- z3.object({
748
- text: z3.string()
792
+ z4.object({
793
+ type: z4.literal("content-delta"),
794
+ index: z4.number(),
795
+ delta: z4.object({
796
+ message: z4.object({
797
+ content: z4.union([
798
+ z4.object({
799
+ text: z4.string()
749
800
  }),
750
- z3.object({
751
- thinking: z3.string()
801
+ z4.object({
802
+ thinking: z4.string()
752
803
  })
753
804
  ])
754
805
  })
755
806
  })
756
807
  }),
757
- z3.object({
758
- type: z3.literal("content-end"),
759
- index: z3.number()
808
+ z4.object({
809
+ type: z4.literal("content-end"),
810
+ index: z4.number()
760
811
  }),
761
- z3.object({
762
- type: z3.literal("message-start"),
763
- id: z3.string().nullish()
812
+ z4.object({
813
+ type: z4.literal("message-start"),
814
+ id: z4.string().nullish()
764
815
  }),
765
- z3.object({
766
- type: z3.literal("message-end"),
767
- delta: z3.object({
768
- finish_reason: z3.string(),
769
- usage: z3.object({
770
- tokens: z3.object({
771
- input_tokens: z3.number(),
772
- output_tokens: z3.number()
773
- })
774
- })
816
+ z4.object({
817
+ type: z4.literal("message-end"),
818
+ delta: z4.object({
819
+ finish_reason: z4.string(),
820
+ usage: cohereUsageSchema().nullish()
775
821
  })
776
822
  }),
777
823
  // https://docs.cohere.com/v2/docs/streaming#tool-use-stream-events-for-tool-calling
778
- z3.object({
779
- type: z3.literal("tool-plan-delta"),
780
- delta: z3.object({
781
- message: z3.object({
782
- tool_plan: z3.string()
824
+ z4.object({
825
+ type: z4.literal("tool-plan-delta"),
826
+ delta: z4.object({
827
+ message: z4.object({
828
+ tool_plan: z4.string()
783
829
  })
784
830
  })
785
831
  }),
786
- z3.object({
787
- type: z3.literal("tool-call-start"),
788
- delta: z3.object({
789
- message: z3.object({
790
- tool_calls: z3.object({
791
- id: z3.string(),
792
- type: z3.literal("function"),
793
- function: z3.object({
794
- name: z3.string(),
795
- arguments: z3.string()
832
+ z4.object({
833
+ type: z4.literal("tool-call-start"),
834
+ delta: z4.object({
835
+ message: z4.object({
836
+ tool_calls: z4.object({
837
+ id: z4.string(),
838
+ type: z4.literal("function"),
839
+ function: z4.object({
840
+ name: z4.string(),
841
+ arguments: z4.string()
796
842
  })
797
843
  })
798
844
  })
@@ -801,20 +847,20 @@ var cohereChatChunkSchema = lazySchema3(
801
847
  // A single tool call's `arguments` stream in chunks and must be accumulated
802
848
  // in a string and so the full tool object info can only be parsed once we see
803
849
  // `tool-call-end`.
804
- z3.object({
805
- type: z3.literal("tool-call-delta"),
806
- delta: z3.object({
807
- message: z3.object({
808
- tool_calls: z3.object({
809
- function: z3.object({
810
- arguments: z3.string()
850
+ z4.object({
851
+ type: z4.literal("tool-call-delta"),
852
+ delta: z4.object({
853
+ message: z4.object({
854
+ tool_calls: z4.object({
855
+ function: z4.object({
856
+ arguments: z4.string()
811
857
  })
812
858
  })
813
859
  })
814
860
  })
815
861
  }),
816
- z3.object({
817
- type: z3.literal("tool-call-end")
862
+ z4.object({
863
+ type: z4.literal("tool-call-end")
818
864
  })
819
865
  ])
820
866
  )
@@ -832,17 +878,17 @@ import {
832
878
  parseProviderOptions as parseProviderOptions3,
833
879
  postJsonToApi as postJsonToApi2
834
880
  } from "@ai-sdk/provider-utils";
835
- import { z as z5 } from "zod/v4";
881
+ import { z as z6 } from "zod/v4";
836
882
 
837
883
  // src/cohere-embedding-options.ts
838
884
  import {
839
885
  lazySchema as lazySchema4,
840
886
  zodSchema as zodSchema4
841
887
  } from "@ai-sdk/provider-utils";
842
- import { z as z4 } from "zod/v4";
888
+ import { z as z5 } from "zod/v4";
843
889
  var cohereEmbeddingOptions = lazySchema4(
844
890
  () => zodSchema4(
845
- z4.object({
891
+ z5.object({
846
892
  /**
847
893
  * Specifies the type of input passed to the model. Default is `search_query`.
848
894
  *
@@ -851,7 +897,7 @@ var cohereEmbeddingOptions = lazySchema4(
851
897
  * - "classification": Used for embeddings passed through a text classifier.
852
898
  * - "clustering": Used for embeddings run through a clustering algorithm.
853
899
  */
854
- inputType: z4.enum([
900
+ inputType: z5.enum([
855
901
  "search_document",
856
902
  "search_query",
857
903
  "classification",
@@ -865,7 +911,7 @@ var cohereEmbeddingOptions = lazySchema4(
865
911
  * - "START": Will discard the start of the input until the remaining input is exactly the maximum input token length for the model.
866
912
  * - "END": Will discard the end of the input until the remaining input is exactly the maximum input token length for the model.
867
913
  */
868
- truncate: z4.enum(["NONE", "START", "END"]).optional()
914
+ truncate: z5.enum(["NONE", "START", "END"]).optional()
869
915
  })
870
916
  )
871
917
  );
@@ -935,13 +981,13 @@ var CohereEmbeddingModel = class {
935
981
  };
936
982
  var cohereTextEmbeddingResponseSchema = lazySchema5(
937
983
  () => zodSchema5(
938
- z5.object({
939
- embeddings: z5.object({
940
- float: z5.array(z5.array(z5.number()))
984
+ z6.object({
985
+ embeddings: z6.object({
986
+ float: z6.array(z6.array(z6.number()))
941
987
  }),
942
- meta: z5.object({
943
- billed_units: z5.object({
944
- input_tokens: z5.number()
988
+ meta: z6.object({
989
+ billed_units: z6.object({
990
+ input_tokens: z6.number()
945
991
  })
946
992
  })
947
993
  })
@@ -949,7 +995,7 @@ var cohereTextEmbeddingResponseSchema = lazySchema5(
949
995
  );
950
996
 
951
997
  // src/version.ts
952
- var VERSION = true ? "2.0.48" : "0.0.0-test";
998
+ var VERSION = true ? "2.0.49" : "0.0.0-test";
953
999
 
954
1000
  // src/cohere-provider.ts
955
1001
  function createCohere(options = {}) {