@ai-sdk/cohere 3.0.75 → 3.0.76

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
@@ -20,7 +20,7 @@ import {
20
20
  parseJSON,
21
21
  postJsonToApi
22
22
  } from "@ai-sdk/provider-utils";
23
- import { z as z3 } from "zod/v4";
23
+ import { z as z4 } from "zod/v4";
24
24
 
25
25
  // src/cohere-chat-options.ts
26
26
  import {
@@ -59,17 +59,67 @@ var cohereImagePartProviderOptions = lazySchema(
59
59
  )
60
60
  );
61
61
 
62
+ // src/cohere-citations.ts
63
+ import { z as z2 } from "zod/v4";
64
+ var cohereCitationSchema = z2.object({
65
+ start: z2.number().nullish(),
66
+ end: z2.number().nullish(),
67
+ text: z2.string().nullish(),
68
+ sources: z2.array(
69
+ z2.object({
70
+ type: z2.string().nullish(),
71
+ id: z2.string().nullish(),
72
+ // Documents are arbitrary objects; only the fields the provider maps
73
+ // are typed, everything else is passed through as provider metadata.
74
+ document: z2.looseObject({
75
+ id: z2.string().nullish(),
76
+ text: z2.string().nullish(),
77
+ title: z2.string().nullish()
78
+ }).nullish(),
79
+ tool_output: z2.looseObject({}).nullish()
80
+ })
81
+ ).nullish(),
82
+ type: z2.string().nullish()
83
+ });
84
+ function mapCohereCitationToSource(citation, generateId2) {
85
+ var _a, _b;
86
+ const document = (_b = (_a = citation.sources) == null ? void 0 : _a.find(
87
+ (source) => source.document != null
88
+ )) == null ? void 0 : _b.document;
89
+ if (document == null) {
90
+ return void 0;
91
+ }
92
+ return {
93
+ type: "source",
94
+ sourceType: "document",
95
+ id: generateId2(),
96
+ mediaType: "text/plain",
97
+ title: document.title || "Document",
98
+ providerMetadata: {
99
+ cohere: {
100
+ ...citation.start != null && { start: citation.start },
101
+ ...citation.end != null && { end: citation.end },
102
+ ...citation.text != null && { text: citation.text },
103
+ ...citation.sources != null && {
104
+ sources: citation.sources
105
+ },
106
+ ...citation.type != null && { citationType: citation.type }
107
+ }
108
+ }
109
+ };
110
+ }
111
+
62
112
  // src/cohere-error.ts
63
113
  import {
64
114
  lazySchema as lazySchema2,
65
115
  zodSchema as zodSchema2,
66
116
  createJsonErrorResponseHandler
67
117
  } from "@ai-sdk/provider-utils";
68
- import { z as z2 } from "zod/v4";
118
+ import { z as z3 } from "zod/v4";
69
119
  var cohereErrorDataSchema = lazySchema2(
70
120
  () => zodSchema2(
71
- z2.object({
72
- message: z2.string()
121
+ z3.object({
122
+ message: z3.string()
73
123
  })
74
124
  )
75
125
  );
@@ -139,6 +189,7 @@ function prepareTools({
139
189
 
140
190
  // src/convert-cohere-usage.ts
141
191
  function convertCohereUsage(usage) {
192
+ var _a, _b, _c, _d;
142
193
  if (usage == null) {
143
194
  return {
144
195
  inputTokens: {
@@ -155,9 +206,8 @@ function convertCohereUsage(usage) {
155
206
  raw: void 0
156
207
  };
157
208
  }
158
- const tokens = usage.tokens;
159
- const inputTokens = tokens.input_tokens;
160
- const outputTokens = tokens.output_tokens;
209
+ const inputTokens = (_b = (_a = usage.tokens) == null ? void 0 : _a.input_tokens) != null ? _b : void 0;
210
+ const outputTokens = (_d = (_c = usage.tokens) == null ? void 0 : _c.output_tokens) != null ? _d : void 0;
161
211
  return {
162
212
  inputTokens: {
163
213
  total: inputTokens,
@@ -445,7 +495,7 @@ var CohereChatLanguageModel = class {
445
495
  };
446
496
  }
447
497
  async doGenerate(options) {
448
- var _a, _b, _c, _d, _e, _f, _g;
498
+ var _a, _b, _c, _d, _e, _f;
449
499
  const { args, warnings } = await this.getArgs(options);
450
500
  const {
451
501
  responseHeaders,
@@ -474,24 +524,15 @@ var CohereChatLanguageModel = class {
474
524
  }
475
525
  }
476
526
  for (const citation of (_b = response.message.citations) != null ? _b : []) {
477
- content.push({
478
- type: "source",
479
- sourceType: "document",
480
- id: this.config.generateId(),
481
- mediaType: "text/plain",
482
- title: ((_d = (_c = citation.sources[0]) == null ? void 0 : _c.document) == null ? void 0 : _d.title) || "Document",
483
- providerMetadata: {
484
- cohere: {
485
- start: citation.start,
486
- end: citation.end,
487
- text: citation.text,
488
- sources: citation.sources,
489
- ...citation.type && { citationType: citation.type }
490
- }
491
- }
492
- });
527
+ const source = mapCohereCitationToSource(
528
+ citation,
529
+ () => this.config.generateId()
530
+ );
531
+ if (source != null) {
532
+ content.push(source);
533
+ }
493
534
  }
494
- for (const toolCall of (_e = response.message.tool_calls) != null ? _e : []) {
535
+ for (const toolCall of (_c = response.message.tool_calls) != null ? _c : []) {
495
536
  content.push({
496
537
  type: "tool-call",
497
538
  toolCallId: toolCall.id,
@@ -505,13 +546,15 @@ var CohereChatLanguageModel = class {
505
546
  content,
506
547
  finishReason: {
507
548
  unified: mapCohereFinishReason(response.finish_reason),
508
- raw: (_f = response.finish_reason) != null ? _f : void 0
549
+ raw: (_d = response.finish_reason) != null ? _d : void 0
509
550
  },
510
551
  usage: convertCohereUsage(response.usage),
511
552
  request: { body: args },
512
553
  response: {
513
554
  // TODO timestamp, model id
514
- id: (_g = response.generation_id) != null ? _g : void 0,
555
+ // Cohere v2 returns `id`; `generation_id` is the v1 field, kept only
556
+ // for compatibility with proxies/mocks that still return it.
557
+ id: (_f = (_e = response.id) != null ? _e : response.generation_id) != null ? _f : void 0,
515
558
  headers: responseHeaders,
516
559
  body: rawResponse
517
560
  },
@@ -538,6 +581,7 @@ var CohereChatLanguageModel = class {
538
581
  let usage = void 0;
539
582
  let pendingToolCall = null;
540
583
  let isActiveReasoning = false;
584
+ const generateId2 = this.config.generateId;
541
585
  return {
542
586
  stream: response.pipeThrough(
543
587
  new TransformStream({
@@ -545,7 +589,7 @@ var CohereChatLanguageModel = class {
545
589
  controller.enqueue({ type: "stream-start", warnings });
546
590
  },
547
591
  async transform(chunk, controller) {
548
- var _a, _b;
592
+ var _a, _b, _c, _d;
549
593
  if (options.includeRawChunks) {
550
594
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
551
595
  }
@@ -603,6 +647,22 @@ var CohereChatLanguageModel = class {
603
647
  });
604
648
  return;
605
649
  }
650
+ case "citation-start": {
651
+ const citations = (_b = (_a = value.delta) == null ? void 0 : _a.message) == null ? void 0 : _b.citations;
652
+ if (citations == null) {
653
+ return;
654
+ }
655
+ for (const citation of Array.isArray(citations) ? citations : [citations]) {
656
+ const source = mapCohereCitationToSource(
657
+ citation,
658
+ generateId2
659
+ );
660
+ if (source != null) {
661
+ controller.enqueue(source);
662
+ }
663
+ }
664
+ return;
665
+ }
606
666
  case "tool-call-start": {
607
667
  const toolId = value.delta.message.tool_calls.id;
608
668
  const toolName = value.delta.message.tool_calls.function.name;
@@ -651,7 +711,7 @@ var CohereChatLanguageModel = class {
651
711
  toolName: pendingToolCall.name,
652
712
  input: JSON.stringify(
653
713
  await parseJSON({
654
- text: ((_a = pendingToolCall.arguments) == null ? void 0 : _a.trim()) || "{}"
714
+ text: ((_c = pendingToolCall.arguments) == null ? void 0 : _c.trim()) || "{}"
655
715
  })
656
716
  )
657
717
  });
@@ -663,7 +723,7 @@ var CohereChatLanguageModel = class {
663
723
  case "message-start": {
664
724
  controller.enqueue({
665
725
  type: "response-metadata",
666
- id: (_b = value.id) != null ? _b : void 0
726
+ id: (_d = value.id) != null ? _d : void 0
667
727
  });
668
728
  return;
669
729
  }
@@ -693,148 +753,139 @@ var CohereChatLanguageModel = class {
693
753
  };
694
754
  }
695
755
  };
696
- var cohereUsageSchema = () => z3.looseObject({
697
- billed_units: z3.looseObject({
698
- input_tokens: z3.number(),
699
- output_tokens: z3.number()
756
+ var cohereUsageSchema = () => z4.looseObject({
757
+ billed_units: z4.looseObject({
758
+ input_tokens: z4.number().nullish(),
759
+ output_tokens: z4.number().nullish()
760
+ }).nullish(),
761
+ tokens: z4.looseObject({
762
+ input_tokens: z4.number().nullish(),
763
+ output_tokens: z4.number().nullish()
700
764
  }).nullish(),
701
- tokens: z3.looseObject({
702
- input_tokens: z3.number(),
703
- output_tokens: z3.number()
704
- }),
705
- cached_tokens: z3.number().nullish()
765
+ cached_tokens: z4.number().nullish()
706
766
  });
707
767
  var cohereChatResponseSchema = lazySchema3(
708
768
  () => zodSchema3(
709
- z3.object({
710
- generation_id: z3.string().nullish(),
711
- message: z3.object({
712
- role: z3.string(),
713
- content: z3.array(
714
- z3.union([
715
- z3.object({
716
- type: z3.literal("text"),
717
- text: z3.string()
769
+ z4.object({
770
+ id: z4.string().nullish(),
771
+ generation_id: z4.string().nullish(),
772
+ message: z4.object({
773
+ role: z4.string(),
774
+ content: z4.array(
775
+ z4.union([
776
+ z4.object({
777
+ type: z4.literal("text"),
778
+ text: z4.string()
718
779
  }),
719
- z3.object({
720
- type: z3.literal("thinking"),
721
- thinking: z3.string()
780
+ z4.object({
781
+ type: z4.literal("thinking"),
782
+ thinking: z4.string()
722
783
  })
723
784
  ])
724
785
  ).nullish(),
725
- tool_plan: z3.string().nullish(),
726
- tool_calls: z3.array(
727
- z3.object({
728
- id: z3.string(),
729
- type: z3.literal("function"),
730
- function: z3.object({
731
- name: z3.string(),
732
- arguments: z3.string()
786
+ tool_plan: z4.string().nullish(),
787
+ tool_calls: z4.array(
788
+ z4.object({
789
+ id: z4.string(),
790
+ type: z4.literal("function"),
791
+ function: z4.object({
792
+ name: z4.string(),
793
+ arguments: z4.string()
733
794
  })
734
795
  })
735
796
  ).nullish(),
736
- citations: z3.array(
737
- z3.object({
738
- start: z3.number(),
739
- end: z3.number(),
740
- text: z3.string(),
741
- sources: z3.array(
742
- z3.object({
743
- type: z3.string().optional(),
744
- id: z3.string().optional(),
745
- document: z3.object({
746
- id: z3.string().optional(),
747
- text: z3.string(),
748
- title: z3.string()
749
- })
750
- })
751
- ),
752
- type: z3.string().optional()
753
- })
754
- ).nullish()
797
+ citations: z4.array(cohereCitationSchema).nullish()
755
798
  }),
756
- finish_reason: z3.string(),
757
- usage: cohereUsageSchema()
799
+ finish_reason: z4.string(),
800
+ usage: cohereUsageSchema().nullish()
758
801
  })
759
802
  )
760
803
  );
761
804
  var cohereChatChunkSchema = lazySchema3(
762
805
  () => zodSchema3(
763
- z3.discriminatedUnion("type", [
764
- z3.object({
765
- type: z3.literal("citation-start")
806
+ z4.discriminatedUnion("type", [
807
+ // `citation-start` events can arrive without citations, and a single
808
+ // citation may be sent instead of an array.
809
+ z4.object({
810
+ type: z4.literal("citation-start"),
811
+ index: z4.number().nullish(),
812
+ delta: z4.object({
813
+ message: z4.object({
814
+ citations: z4.union([cohereCitationSchema, z4.array(cohereCitationSchema)]).nullish()
815
+ }).nullish()
816
+ }).nullish()
766
817
  }),
767
- z3.object({
768
- type: z3.literal("citation-end")
818
+ z4.object({
819
+ type: z4.literal("citation-end")
769
820
  }),
770
- z3.object({
771
- type: z3.literal("content-start"),
772
- index: z3.number(),
773
- delta: z3.object({
774
- message: z3.object({
775
- content: z3.union([
776
- z3.object({
777
- type: z3.literal("text"),
778
- text: z3.string()
821
+ z4.object({
822
+ type: z4.literal("content-start"),
823
+ index: z4.number(),
824
+ delta: z4.object({
825
+ message: z4.object({
826
+ content: z4.union([
827
+ z4.object({
828
+ type: z4.literal("text"),
829
+ text: z4.string()
779
830
  }),
780
- z3.object({
781
- type: z3.literal("thinking"),
782
- thinking: z3.string()
831
+ z4.object({
832
+ type: z4.literal("thinking"),
833
+ thinking: z4.string()
783
834
  })
784
835
  ])
785
836
  })
786
837
  })
787
838
  }),
788
- z3.object({
789
- type: z3.literal("content-delta"),
790
- index: z3.number(),
791
- delta: z3.object({
792
- message: z3.object({
793
- content: z3.union([
794
- z3.object({
795
- text: z3.string()
839
+ z4.object({
840
+ type: z4.literal("content-delta"),
841
+ index: z4.number(),
842
+ delta: z4.object({
843
+ message: z4.object({
844
+ content: z4.union([
845
+ z4.object({
846
+ text: z4.string()
796
847
  }),
797
- z3.object({
798
- thinking: z3.string()
848
+ z4.object({
849
+ thinking: z4.string()
799
850
  })
800
851
  ])
801
852
  })
802
853
  })
803
854
  }),
804
- z3.object({
805
- type: z3.literal("content-end"),
806
- index: z3.number()
855
+ z4.object({
856
+ type: z4.literal("content-end"),
857
+ index: z4.number()
807
858
  }),
808
- z3.object({
809
- type: z3.literal("message-start"),
810
- id: z3.string().nullish()
859
+ z4.object({
860
+ type: z4.literal("message-start"),
861
+ id: z4.string().nullish()
811
862
  }),
812
- z3.object({
813
- type: z3.literal("message-end"),
814
- delta: z3.object({
815
- finish_reason: z3.string(),
863
+ z4.object({
864
+ type: z4.literal("message-end"),
865
+ delta: z4.object({
866
+ finish_reason: z4.string(),
816
867
  usage: cohereUsageSchema()
817
868
  })
818
869
  }),
819
870
  // https://docs.cohere.com/v2/docs/streaming#tool-use-stream-events-for-tool-calling
820
- z3.object({
821
- type: z3.literal("tool-plan-delta"),
822
- delta: z3.object({
823
- message: z3.object({
824
- tool_plan: z3.string()
871
+ z4.object({
872
+ type: z4.literal("tool-plan-delta"),
873
+ delta: z4.object({
874
+ message: z4.object({
875
+ tool_plan: z4.string()
825
876
  })
826
877
  })
827
878
  }),
828
- z3.object({
829
- type: z3.literal("tool-call-start"),
830
- delta: z3.object({
831
- message: z3.object({
832
- tool_calls: z3.object({
833
- id: z3.string(),
834
- type: z3.literal("function"),
835
- function: z3.object({
836
- name: z3.string(),
837
- arguments: z3.string()
879
+ z4.object({
880
+ type: z4.literal("tool-call-start"),
881
+ delta: z4.object({
882
+ message: z4.object({
883
+ tool_calls: z4.object({
884
+ id: z4.string(),
885
+ type: z4.literal("function"),
886
+ function: z4.object({
887
+ name: z4.string(),
888
+ arguments: z4.string()
838
889
  })
839
890
  })
840
891
  })
@@ -843,20 +894,20 @@ var cohereChatChunkSchema = lazySchema3(
843
894
  // A single tool call's `arguments` stream in chunks and must be accumulated
844
895
  // in a string and so the full tool object info can only be parsed once we see
845
896
  // `tool-call-end`.
846
- z3.object({
847
- type: z3.literal("tool-call-delta"),
848
- delta: z3.object({
849
- message: z3.object({
850
- tool_calls: z3.object({
851
- function: z3.object({
852
- arguments: z3.string()
897
+ z4.object({
898
+ type: z4.literal("tool-call-delta"),
899
+ delta: z4.object({
900
+ message: z4.object({
901
+ tool_calls: z4.object({
902
+ function: z4.object({
903
+ arguments: z4.string()
853
904
  })
854
905
  })
855
906
  })
856
907
  })
857
908
  }),
858
- z3.object({
859
- type: z3.literal("tool-call-end")
909
+ z4.object({
910
+ type: z4.literal("tool-call-end")
860
911
  })
861
912
  ])
862
913
  )
@@ -874,17 +925,17 @@ import {
874
925
  parseProviderOptions as parseProviderOptions3,
875
926
  postJsonToApi as postJsonToApi2
876
927
  } from "@ai-sdk/provider-utils";
877
- import { z as z5 } from "zod/v4";
928
+ import { z as z6 } from "zod/v4";
878
929
 
879
930
  // src/cohere-embedding-options.ts
880
931
  import {
881
932
  lazySchema as lazySchema4,
882
933
  zodSchema as zodSchema4
883
934
  } from "@ai-sdk/provider-utils";
884
- import { z as z4 } from "zod/v4";
935
+ import { z as z5 } from "zod/v4";
885
936
  var cohereEmbeddingModelOptions = lazySchema4(
886
937
  () => zodSchema4(
887
- z4.object({
938
+ z5.object({
888
939
  /**
889
940
  * Specifies the type of input passed to the model. Default is `search_query`.
890
941
  *
@@ -893,7 +944,7 @@ var cohereEmbeddingModelOptions = lazySchema4(
893
944
  * - "classification": Used for embeddings passed through a text classifier.
894
945
  * - "clustering": Used for embeddings run through a clustering algorithm.
895
946
  */
896
- inputType: z4.enum([
947
+ inputType: z5.enum([
897
948
  "search_document",
898
949
  "search_query",
899
950
  "classification",
@@ -907,7 +958,7 @@ var cohereEmbeddingModelOptions = lazySchema4(
907
958
  * - "START": Will discard the start of the input until the remaining input is exactly the maximum input token length for the model.
908
959
  * - "END": Will discard the end of the input until the remaining input is exactly the maximum input token length for the model.
909
960
  */
910
- truncate: z4.enum(["NONE", "START", "END"]).optional(),
961
+ truncate: z5.enum(["NONE", "START", "END"]).optional(),
911
962
  /**
912
963
  * The number of dimensions of the output embedding.
913
964
  * Only available for `embed-v4.0` and newer models.
@@ -915,13 +966,13 @@ var cohereEmbeddingModelOptions = lazySchema4(
915
966
  * Embed v4.0 supports `256`, `512`, `1024`, and `1536` (default).
916
967
  * Embed v5.0 Pro and Fast also support `768` and `2048` (default).
917
968
  */
918
- outputDimension: z4.union([
919
- z4.literal(256),
920
- z4.literal(512),
921
- z4.literal(768),
922
- z4.literal(1024),
923
- z4.literal(1536),
924
- z4.literal(2048)
969
+ outputDimension: z5.union([
970
+ z5.literal(256),
971
+ z5.literal(512),
972
+ z5.literal(768),
973
+ z5.literal(1024),
974
+ z5.literal(1536),
975
+ z5.literal(2048)
925
976
  ]).optional()
926
977
  })
927
978
  )
@@ -994,13 +1045,13 @@ var CohereEmbeddingModel = class {
994
1045
  };
995
1046
  var cohereTextEmbeddingResponseSchema = lazySchema5(
996
1047
  () => zodSchema5(
997
- z5.object({
998
- embeddings: z5.object({
999
- float: z5.array(z5.array(z5.number()))
1048
+ z6.object({
1049
+ embeddings: z6.object({
1050
+ float: z6.array(z6.array(z6.number()))
1000
1051
  }),
1001
- meta: z5.object({
1002
- billed_units: z5.object({
1003
- input_tokens: z5.number()
1052
+ meta: z6.object({
1053
+ billed_units: z6.object({
1054
+ input_tokens: z6.number()
1004
1055
  })
1005
1056
  })
1006
1057
  })
@@ -1017,18 +1068,18 @@ import {
1017
1068
 
1018
1069
  // src/reranking/cohere-reranking-api.ts
1019
1070
  import { lazySchema as lazySchema6, zodSchema as zodSchema6 } from "@ai-sdk/provider-utils";
1020
- import { z as z6 } from "zod/v4";
1071
+ import { z as z7 } from "zod/v4";
1021
1072
  var cohereRerankingResponseSchema = lazySchema6(
1022
1073
  () => zodSchema6(
1023
- z6.object({
1024
- id: z6.string().nullish(),
1025
- results: z6.array(
1026
- z6.object({
1027
- index: z6.number(),
1028
- relevance_score: z6.number()
1074
+ z7.object({
1075
+ id: z7.string().nullish(),
1076
+ results: z7.array(
1077
+ z7.object({
1078
+ index: z7.number(),
1079
+ relevance_score: z7.number()
1029
1080
  })
1030
1081
  ),
1031
- meta: z6.any()
1082
+ meta: z7.any()
1032
1083
  })
1033
1084
  )
1034
1085
  );
@@ -1038,12 +1089,12 @@ import {
1038
1089
  lazySchema as lazySchema7,
1039
1090
  zodSchema as zodSchema7
1040
1091
  } from "@ai-sdk/provider-utils";
1041
- import { z as z7 } from "zod/v4";
1092
+ import { z as z8 } from "zod/v4";
1042
1093
  var cohereRerankingModelOptionsSchema = lazySchema7(
1043
1094
  () => zodSchema7(
1044
- z7.object({
1045
- maxTokensPerDoc: z7.number().optional(),
1046
- priority: z7.number().optional()
1095
+ z8.object({
1096
+ maxTokensPerDoc: z8.number().optional(),
1097
+ priority: z8.number().optional()
1047
1098
  })
1048
1099
  )
1049
1100
  );
@@ -1119,7 +1170,7 @@ var CohereRerankingModel = class {
1119
1170
  };
1120
1171
 
1121
1172
  // src/version.ts
1122
- var VERSION = true ? "3.0.75" : "0.0.0-test";
1173
+ var VERSION = true ? "3.0.76" : "0.0.0-test";
1123
1174
 
1124
1175
  // src/cohere-provider.ts
1125
1176
  function createCohere(options = {}) {