@ai-sdk/cohere 3.0.74 → 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.
@@ -0,0 +1,77 @@
1
+ import type { JSONArray, LanguageModelV3Source } from '@ai-sdk/provider';
2
+ import { z } from 'zod/v4';
3
+
4
+ /**
5
+ * Citation payload Cohere returns for text content, in non-streaming responses
6
+ * and in `citation-start` stream events.
7
+ *
8
+ * All fields are optional in the API specification, and a source can reference
9
+ * either a document or tool output, so partial citations are validated instead
10
+ * of discarding the whole response.
11
+ */
12
+ export const cohereCitationSchema = z.object({
13
+ start: z.number().nullish(),
14
+ end: z.number().nullish(),
15
+ text: z.string().nullish(),
16
+ sources: z
17
+ .array(
18
+ z.object({
19
+ type: z.string().nullish(),
20
+ id: z.string().nullish(),
21
+ // Documents are arbitrary objects; only the fields the provider maps
22
+ // are typed, everything else is passed through as provider metadata.
23
+ document: z
24
+ .looseObject({
25
+ id: z.string().nullish(),
26
+ text: z.string().nullish(),
27
+ title: z.string().nullish(),
28
+ })
29
+ .nullish(),
30
+ tool_output: z.looseObject({}).nullish(),
31
+ }),
32
+ )
33
+ .nullish(),
34
+ type: z.string().nullish(),
35
+ });
36
+
37
+ export type CohereCitation = z.infer<typeof cohereCitationSchema>;
38
+
39
+ /**
40
+ * Maps a Cohere citation to a document source.
41
+ *
42
+ * The first source with a document provides the title; all sources are kept in
43
+ * `providerMetadata`. Citations that only reference tool output have no
44
+ * document to point at and return `undefined` (their data stays in the response
45
+ * body).
46
+ */
47
+ export function mapCohereCitationToSource(
48
+ citation: CohereCitation,
49
+ generateId: () => string,
50
+ ): LanguageModelV3Source | undefined {
51
+ const document = citation.sources?.find(
52
+ source => source.document != null,
53
+ )?.document;
54
+
55
+ if (document == null) {
56
+ return undefined;
57
+ }
58
+
59
+ return {
60
+ type: 'source',
61
+ sourceType: 'document',
62
+ id: generateId(),
63
+ mediaType: 'text/plain',
64
+ title: document.title || 'Document',
65
+ providerMetadata: {
66
+ cohere: {
67
+ ...(citation.start != null && { start: citation.start }),
68
+ ...(citation.end != null && { end: citation.end }),
69
+ ...(citation.text != null && { text: citation.text }),
70
+ ...(citation.sources != null && {
71
+ sources: citation.sources as JSONArray,
72
+ }),
73
+ ...(citation.type != null && { citationType: citation.type }),
74
+ },
75
+ },
76
+ };
77
+ }
@@ -1,13 +1,13 @@
1
1
  import type { LanguageModelV3Usage } from '@ai-sdk/provider';
2
2
 
3
3
  export type CohereUsageTokens = {
4
- input_tokens: number;
5
- output_tokens: number;
4
+ input_tokens?: number | null;
5
+ output_tokens?: number | null;
6
6
  };
7
7
 
8
8
  export type CohereUsage = {
9
9
  billed_units?: CohereUsageTokens | null;
10
- tokens: CohereUsageTokens;
10
+ tokens?: CohereUsageTokens | null;
11
11
  cached_tokens?: number | null;
12
12
  };
13
13
 
@@ -31,9 +31,11 @@ export function convertCohereUsage(
31
31
  };
32
32
  }
33
33
 
34
- const tokens = usage.tokens;
35
- const inputTokens = tokens.input_tokens;
36
- const outputTokens = tokens.output_tokens;
34
+ // `tokens` is the only measure that matches the unified token counts.
35
+ // `billed_units` count billed units rather than tokens, so they are kept in
36
+ // `raw` only and never used as token counts.
37
+ const inputTokens = usage.tokens?.input_tokens ?? undefined;
38
+ const outputTokens = usage.tokens?.output_tokens ?? undefined;
37
39
 
38
40
  return {
39
41
  inputTokens: {
@@ -4,7 +4,12 @@ import {
4
4
  type LanguageModelV3Prompt,
5
5
  type SharedV3Warning,
6
6
  } from '@ai-sdk/provider';
7
- import { convertToBase64, parseProviderOptions } from '@ai-sdk/provider-utils';
7
+ import {
8
+ secureJsonParse,
9
+ stringifyWithBigIntAndCycles,
10
+ convertToBase64,
11
+ parseProviderOptions,
12
+ } from '@ai-sdk/provider-utils';
8
13
  import { cohereImagePartProviderOptions } from './cohere-chat-options';
9
14
  import type {
10
15
  CohereAssistantMessage,
@@ -12,6 +17,18 @@ import type {
12
17
  CohereUserMessageContent,
13
18
  } from './cohere-chat-prompt';
14
19
 
20
+ function serializeCohereToolCallArguments(input: unknown): string {
21
+ if (typeof input === 'string') {
22
+ try {
23
+ secureJsonParse(input);
24
+ return input;
25
+ } catch {
26
+ return JSON.stringify(input);
27
+ }
28
+ }
29
+ return stringifyWithBigIntAndCycles(input);
30
+ }
31
+
15
32
  export async function convertToCohereChatPrompt(
16
33
  prompt: LanguageModelV3Prompt,
17
34
  ): Promise<{
@@ -132,7 +149,7 @@ export async function convertToCohereChatPrompt(
132
149
  type: 'function' as const,
133
150
  function: {
134
151
  name: part.toolName,
135
- arguments: JSON.stringify(part.input),
152
+ arguments: serializeCohereToolCallArguments(part.input),
136
153
  },
137
154
  });
138
155
  break;
@@ -168,7 +185,7 @@ export async function convertToCohereChatPrompt(
168
185
  case 'content':
169
186
  case 'json':
170
187
  case 'error-json':
171
- contentValue = JSON.stringify(output.value);
188
+ contentValue = stringifyWithBigIntAndCycles(output.value);
172
189
  break;
173
190
  }
174
191