@ai-sdk/cohere 2.0.47 → 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
  );
@@ -81,7 +131,23 @@ var cohereFailedResponseHandler = createJsonErrorResponseHandler({
81
131
  import {
82
132
  UnsupportedFunctionalityError
83
133
  } from "@ai-sdk/provider";
84
- import { convertToBase64, parseProviderOptions } from "@ai-sdk/provider-utils";
134
+ import {
135
+ stringifyWithBigIntAndCycles,
136
+ secureJsonParse,
137
+ convertToBase64,
138
+ parseProviderOptions
139
+ } from "@ai-sdk/provider-utils";
140
+ function serializeCohereToolCallArguments(input) {
141
+ if (typeof input === "string") {
142
+ try {
143
+ secureJsonParse(input);
144
+ return input;
145
+ } catch (e) {
146
+ return JSON.stringify(input);
147
+ }
148
+ }
149
+ return stringifyWithBigIntAndCycles(input);
150
+ }
85
151
  async function convertToCohereChatPrompt(prompt) {
86
152
  var _a, _b;
87
153
  const messages = [];
@@ -174,7 +240,7 @@ async function convertToCohereChatPrompt(prompt) {
174
240
  type: "function",
175
241
  function: {
176
242
  name: part.toolName,
177
- arguments: JSON.stringify(part.input)
243
+ arguments: serializeCohereToolCallArguments(part.input)
178
244
  }
179
245
  });
180
246
  break;
@@ -202,7 +268,7 @@ async function convertToCohereChatPrompt(prompt) {
202
268
  case "content":
203
269
  case "json":
204
270
  case "error-json":
205
- contentValue = JSON.stringify(output.value);
271
+ contentValue = stringifyWithBigIntAndCycles(output.value);
206
272
  break;
207
273
  }
208
274
  return {
@@ -384,7 +450,7 @@ var CohereChatLanguageModel = class {
384
450
  };
385
451
  }
386
452
  async doGenerate(options) {
387
- var _a, _b, _c, _d, _e, _f;
453
+ var _a, _b, _c, _d, _e;
388
454
  const { args, warnings } = await this.getArgs(options);
389
455
  const {
390
456
  responseHeaders,
@@ -413,24 +479,15 @@ var CohereChatLanguageModel = class {
413
479
  }
414
480
  }
415
481
  for (const citation of (_b = response.message.citations) != null ? _b : []) {
416
- content.push({
417
- type: "source",
418
- sourceType: "document",
419
- id: this.config.generateId(),
420
- mediaType: "text/plain",
421
- title: ((_d = (_c = citation.sources[0]) == null ? void 0 : _c.document) == null ? void 0 : _d.title) || "Document",
422
- providerMetadata: {
423
- cohere: {
424
- start: citation.start,
425
- end: citation.end,
426
- text: citation.text,
427
- sources: citation.sources,
428
- ...citation.type && { citationType: citation.type }
429
- }
430
- }
431
- });
482
+ const source = mapCohereCitationToSource(
483
+ citation,
484
+ () => this.config.generateId()
485
+ );
486
+ if (source != null) {
487
+ content.push(source);
488
+ }
432
489
  }
433
- for (const toolCall of (_e = response.message.tool_calls) != null ? _e : []) {
490
+ for (const toolCall of (_c = response.message.tool_calls) != null ? _c : []) {
434
491
  content.push({
435
492
  type: "tool-call",
436
493
  toolCallId: toolCall.id,
@@ -443,15 +500,13 @@ var CohereChatLanguageModel = class {
443
500
  return {
444
501
  content,
445
502
  finishReason: mapCohereFinishReason(response.finish_reason),
446
- usage: {
447
- inputTokens: response.usage.tokens.input_tokens,
448
- outputTokens: response.usage.tokens.output_tokens,
449
- totalTokens: response.usage.tokens.input_tokens + response.usage.tokens.output_tokens
450
- },
503
+ usage: convertCohereUsage(response.usage),
451
504
  request: { body: args },
452
505
  response: {
453
506
  // TODO timestamp, model id
454
- 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,
455
510
  headers: responseHeaders,
456
511
  body: rawResponse
457
512
  },
@@ -472,13 +527,10 @@ var CohereChatLanguageModel = class {
472
527
  fetch: this.config.fetch
473
528
  });
474
529
  let finishReason = "unknown";
475
- const usage = {
476
- inputTokens: void 0,
477
- outputTokens: void 0,
478
- totalTokens: void 0
479
- };
530
+ let rawUsage = void 0;
480
531
  let pendingToolCall = null;
481
532
  let isActiveReasoning = false;
533
+ const generateId3 = this.config.generateId;
482
534
  return {
483
535
  stream: response.pipeThrough(
484
536
  new TransformStream({
@@ -486,7 +538,7 @@ var CohereChatLanguageModel = class {
486
538
  controller.enqueue({ type: "stream-start", warnings });
487
539
  },
488
540
  transform(chunk, controller) {
489
- var _a, _b;
541
+ var _a, _b, _c, _d, _e;
490
542
  if (options.includeRawChunks) {
491
543
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
492
544
  }
@@ -544,6 +596,22 @@ var CohereChatLanguageModel = class {
544
596
  });
545
597
  return;
546
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
+ }
547
615
  case "tool-call-start": {
548
616
  const toolId = value.delta.message.tool_calls.id;
549
617
  const toolName = value.delta.message.tool_calls.function.name;
@@ -591,7 +659,7 @@ var CohereChatLanguageModel = class {
591
659
  toolCallId: pendingToolCall.id,
592
660
  toolName: pendingToolCall.name,
593
661
  input: JSON.stringify(
594
- JSON.parse(((_a = pendingToolCall.arguments) == null ? void 0 : _a.trim()) || "{}")
662
+ JSON.parse(((_c = pendingToolCall.arguments) == null ? void 0 : _c.trim()) || "{}")
595
663
  )
596
664
  });
597
665
  pendingToolCall.hasFinished = true;
@@ -602,16 +670,13 @@ var CohereChatLanguageModel = class {
602
670
  case "message-start": {
603
671
  controller.enqueue({
604
672
  type: "response-metadata",
605
- id: (_b = value.id) != null ? _b : void 0
673
+ id: (_d = value.id) != null ? _d : void 0
606
674
  });
607
675
  return;
608
676
  }
609
677
  case "message-end": {
610
678
  finishReason = mapCohereFinishReason(value.delta.finish_reason);
611
- const tokens = value.delta.usage.tokens;
612
- usage.inputTokens = tokens.input_tokens;
613
- usage.outputTokens = tokens.output_tokens;
614
- usage.totalTokens = tokens.input_tokens + tokens.output_tokens;
679
+ rawUsage = (_e = value.delta.usage) != null ? _e : void 0;
615
680
  return;
616
681
  }
617
682
  default: {
@@ -622,7 +687,7 @@ var CohereChatLanguageModel = class {
622
687
  controller.enqueue({
623
688
  type: "finish",
624
689
  finishReason,
625
- usage
690
+ usage: convertCohereUsage(rawUsage)
626
691
  });
627
692
  }
628
693
  })
@@ -632,151 +697,148 @@ var CohereChatLanguageModel = class {
632
697
  };
633
698
  }
634
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
+ }
635
720
  var cohereChatResponseSchema = lazySchema3(
636
721
  () => zodSchema3(
637
- z3.object({
638
- generation_id: z3.string().nullish(),
639
- message: z3.object({
640
- role: z3.string(),
641
- content: z3.array(
642
- z3.union([
643
- z3.object({
644
- type: z3.literal("text"),
645
- 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()
646
732
  }),
647
- z3.object({
648
- type: z3.literal("thinking"),
649
- thinking: z3.string()
733
+ z4.object({
734
+ type: z4.literal("thinking"),
735
+ thinking: z4.string()
650
736
  })
651
737
  ])
652
738
  ).nullish(),
653
- tool_plan: z3.string().nullish(),
654
- tool_calls: z3.array(
655
- z3.object({
656
- id: z3.string(),
657
- type: z3.literal("function"),
658
- function: z3.object({
659
- name: z3.string(),
660
- 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()
661
747
  })
662
748
  })
663
749
  ).nullish(),
664
- citations: z3.array(
665
- z3.object({
666
- start: z3.number(),
667
- end: z3.number(),
668
- text: z3.string(),
669
- sources: z3.array(
670
- z3.object({
671
- type: z3.string().optional(),
672
- id: z3.string().optional(),
673
- document: z3.object({
674
- id: z3.string().optional(),
675
- text: z3.string(),
676
- title: z3.string()
677
- })
678
- })
679
- ),
680
- type: z3.string().optional()
681
- })
682
- ).nullish()
750
+ citations: z4.array(cohereCitationSchema).nullish()
683
751
  }),
684
- finish_reason: z3.string(),
685
- usage: z3.object({
686
- billed_units: z3.object({
687
- input_tokens: z3.number(),
688
- output_tokens: z3.number()
689
- }),
690
- tokens: z3.object({
691
- input_tokens: z3.number(),
692
- output_tokens: z3.number()
693
- })
694
- })
752
+ finish_reason: z4.string(),
753
+ usage: cohereUsageSchema().nullish()
695
754
  })
696
755
  )
697
756
  );
698
757
  var cohereChatChunkSchema = lazySchema3(
699
758
  () => zodSchema3(
700
- z3.discriminatedUnion("type", [
701
- z3.object({
702
- 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()
703
770
  }),
704
- z3.object({
705
- type: z3.literal("citation-end")
771
+ z4.object({
772
+ type: z4.literal("citation-end")
706
773
  }),
707
- z3.object({
708
- type: z3.literal("content-start"),
709
- index: z3.number(),
710
- delta: z3.object({
711
- message: z3.object({
712
- content: z3.union([
713
- z3.object({
714
- type: z3.literal("text"),
715
- 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()
716
783
  }),
717
- z3.object({
718
- type: z3.literal("thinking"),
719
- thinking: z3.string()
784
+ z4.object({
785
+ type: z4.literal("thinking"),
786
+ thinking: z4.string()
720
787
  })
721
788
  ])
722
789
  })
723
790
  })
724
791
  }),
725
- z3.object({
726
- type: z3.literal("content-delta"),
727
- index: z3.number(),
728
- delta: z3.object({
729
- message: z3.object({
730
- content: z3.union([
731
- z3.object({
732
- 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()
733
800
  }),
734
- z3.object({
735
- thinking: z3.string()
801
+ z4.object({
802
+ thinking: z4.string()
736
803
  })
737
804
  ])
738
805
  })
739
806
  })
740
807
  }),
741
- z3.object({
742
- type: z3.literal("content-end"),
743
- index: z3.number()
808
+ z4.object({
809
+ type: z4.literal("content-end"),
810
+ index: z4.number()
744
811
  }),
745
- z3.object({
746
- type: z3.literal("message-start"),
747
- id: z3.string().nullish()
812
+ z4.object({
813
+ type: z4.literal("message-start"),
814
+ id: z4.string().nullish()
748
815
  }),
749
- z3.object({
750
- type: z3.literal("message-end"),
751
- delta: z3.object({
752
- finish_reason: z3.string(),
753
- usage: z3.object({
754
- tokens: z3.object({
755
- input_tokens: z3.number(),
756
- output_tokens: z3.number()
757
- })
758
- })
816
+ z4.object({
817
+ type: z4.literal("message-end"),
818
+ delta: z4.object({
819
+ finish_reason: z4.string(),
820
+ usage: cohereUsageSchema().nullish()
759
821
  })
760
822
  }),
761
823
  // https://docs.cohere.com/v2/docs/streaming#tool-use-stream-events-for-tool-calling
762
- z3.object({
763
- type: z3.literal("tool-plan-delta"),
764
- delta: z3.object({
765
- message: z3.object({
766
- 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()
767
829
  })
768
830
  })
769
831
  }),
770
- z3.object({
771
- type: z3.literal("tool-call-start"),
772
- delta: z3.object({
773
- message: z3.object({
774
- tool_calls: z3.object({
775
- id: z3.string(),
776
- type: z3.literal("function"),
777
- function: z3.object({
778
- name: z3.string(),
779
- 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()
780
842
  })
781
843
  })
782
844
  })
@@ -785,20 +847,20 @@ var cohereChatChunkSchema = lazySchema3(
785
847
  // A single tool call's `arguments` stream in chunks and must be accumulated
786
848
  // in a string and so the full tool object info can only be parsed once we see
787
849
  // `tool-call-end`.
788
- z3.object({
789
- type: z3.literal("tool-call-delta"),
790
- delta: z3.object({
791
- message: z3.object({
792
- tool_calls: z3.object({
793
- function: z3.object({
794
- 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()
795
857
  })
796
858
  })
797
859
  })
798
860
  })
799
861
  }),
800
- z3.object({
801
- type: z3.literal("tool-call-end")
862
+ z4.object({
863
+ type: z4.literal("tool-call-end")
802
864
  })
803
865
  ])
804
866
  )
@@ -816,17 +878,17 @@ import {
816
878
  parseProviderOptions as parseProviderOptions3,
817
879
  postJsonToApi as postJsonToApi2
818
880
  } from "@ai-sdk/provider-utils";
819
- import { z as z5 } from "zod/v4";
881
+ import { z as z6 } from "zod/v4";
820
882
 
821
883
  // src/cohere-embedding-options.ts
822
884
  import {
823
885
  lazySchema as lazySchema4,
824
886
  zodSchema as zodSchema4
825
887
  } from "@ai-sdk/provider-utils";
826
- import { z as z4 } from "zod/v4";
888
+ import { z as z5 } from "zod/v4";
827
889
  var cohereEmbeddingOptions = lazySchema4(
828
890
  () => zodSchema4(
829
- z4.object({
891
+ z5.object({
830
892
  /**
831
893
  * Specifies the type of input passed to the model. Default is `search_query`.
832
894
  *
@@ -835,7 +897,7 @@ var cohereEmbeddingOptions = lazySchema4(
835
897
  * - "classification": Used for embeddings passed through a text classifier.
836
898
  * - "clustering": Used for embeddings run through a clustering algorithm.
837
899
  */
838
- inputType: z4.enum([
900
+ inputType: z5.enum([
839
901
  "search_document",
840
902
  "search_query",
841
903
  "classification",
@@ -849,7 +911,7 @@ var cohereEmbeddingOptions = lazySchema4(
849
911
  * - "START": Will discard the start of the input until the remaining input is exactly the maximum input token length for the model.
850
912
  * - "END": Will discard the end of the input until the remaining input is exactly the maximum input token length for the model.
851
913
  */
852
- truncate: z4.enum(["NONE", "START", "END"]).optional()
914
+ truncate: z5.enum(["NONE", "START", "END"]).optional()
853
915
  })
854
916
  )
855
917
  );
@@ -919,13 +981,13 @@ var CohereEmbeddingModel = class {
919
981
  };
920
982
  var cohereTextEmbeddingResponseSchema = lazySchema5(
921
983
  () => zodSchema5(
922
- z5.object({
923
- embeddings: z5.object({
924
- float: z5.array(z5.array(z5.number()))
984
+ z6.object({
985
+ embeddings: z6.object({
986
+ float: z6.array(z6.array(z6.number()))
925
987
  }),
926
- meta: z5.object({
927
- billed_units: z5.object({
928
- input_tokens: z5.number()
988
+ meta: z6.object({
989
+ billed_units: z6.object({
990
+ input_tokens: z6.number()
929
991
  })
930
992
  })
931
993
  })
@@ -933,7 +995,7 @@ var cohereTextEmbeddingResponseSchema = lazySchema5(
933
995
  );
934
996
 
935
997
  // src/version.ts
936
- var VERSION = true ? "2.0.47" : "0.0.0-test";
998
+ var VERSION = true ? "2.0.49" : "0.0.0-test";
937
999
 
938
1000
  // src/cohere-provider.ts
939
1001
  function createCohere(options = {}) {