@ai-sdk/cohere 4.0.51 → 4.0.53

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.js CHANGED
@@ -1,1171 +1,918 @@
1
- // src/cohere-provider.ts
2
- import {
3
- NoSuchModelError
4
- } from "@ai-sdk/provider";
5
- import {
6
- generateId,
7
- loadApiKey,
8
- withoutTrailingSlash,
9
- withUserAgentSuffix
10
- } from "@ai-sdk/provider-utils";
11
-
12
- // src/cohere-chat-language-model.ts
13
- import {
14
- combineHeaders,
15
- createEventSourceResponseHandler,
16
- createJsonResponseHandler,
17
- isCustomReasoning,
18
- mapReasoningToProviderBudget,
19
- parseProviderOptions as parseProviderOptions2,
20
- parseJSON,
21
- postJsonToApi,
22
- serializeModelOptions,
23
- WORKFLOW_SERIALIZE,
24
- WORKFLOW_DESERIALIZE
25
- } from "@ai-sdk/provider-utils";
26
- import { z as z3 } from "zod/v4";
27
-
28
- // src/cohere-chat-language-model-options.ts
1
+ import { NoSuchModelError, TooManyEmbeddingValuesForCallError, UnsupportedFunctionalityError } from "@ai-sdk/provider";
2
+ import { WORKFLOW_DESERIALIZE, WORKFLOW_SERIALIZE, combineHeaders, convertToBase64, createEventSourceResponseHandler, createJsonErrorResponseHandler, createJsonResponseHandler, createNullLanguageModelUsage, generateId, getTopLevelMediaType, isCustomReasoning, lazySchema, loadApiKey, mapReasoningToProviderBudget, parseJSON, parseProviderOptions, postJsonToApi, resolveFullMediaType, serializeModelOptions, withUserAgentSuffix, withoutTrailingSlash, zodSchema } from "@ai-sdk/provider-utils";
29
3
  import { z } from "zod/v4";
30
- var cohereLanguageModelChatOptions = z.object({
31
- /**
32
- * Configuration for reasoning features (optional)
33
- *
34
- * Can be set to an object with the two properties `type` and `tokenBudget`. `type` can be set to `'enabled'` or `'disabled'` (defaults to `'enabled'`).
35
- * `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
36
- *
37
- * @see https://docs.cohere.com/reference/chat#request.body.thinking
38
- */
39
- thinking: z.object({
40
- type: z.enum(["enabled", "disabled"]).optional(),
41
- tokenBudget: z.number().optional()
42
- }).optional()
43
- });
44
- var cohereImagePartProviderOptions = z.object({
45
- /**
46
- * Image fidelity level passed through as `image_url.detail` on the Cohere chat API.
47
- *
48
- * @see https://docs.cohere.com/docs/image-inputs
49
- */
50
- detail: z.enum(["auto", "low", "high"]).optional()
51
- });
52
-
53
- // src/cohere-error.ts
54
- import { createJsonErrorResponseHandler } from "@ai-sdk/provider-utils";
55
- import { z as z2 } from "zod/v4";
56
- var cohereErrorDataSchema = z2.object({
57
- message: z2.string()
58
- });
59
- var cohereFailedResponseHandler = createJsonErrorResponseHandler({
60
- errorSchema: cohereErrorDataSchema,
61
- errorToMessage: (data) => data.message
4
+ //#region src/cohere-chat-language-model-options.ts
5
+ const cohereLanguageModelChatOptions = z.object({
6
+ /**
7
+ * Configuration for reasoning features (optional)
8
+ *
9
+ * Can be set to an object with the two properties `type` and `tokenBudget`. `type` can be set to `'enabled'` or `'disabled'` (defaults to `'enabled'`).
10
+ * `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
11
+ *
12
+ * @see https://docs.cohere.com/reference/chat#request.body.thinking
13
+ */
14
+ thinking: z.object({
15
+ type: z.enum(["enabled", "disabled"]).optional(),
16
+ tokenBudget: z.number().optional()
17
+ }).optional() });
18
+ const cohereImagePartProviderOptions = z.object({
19
+ /**
20
+ * Image fidelity level passed through as `image_url.detail` on the Cohere chat API.
21
+ *
22
+ * @see https://docs.cohere.com/docs/image-inputs
23
+ */
24
+ detail: z.enum([
25
+ "auto",
26
+ "low",
27
+ "high"
28
+ ]).optional() });
29
+ //#endregion
30
+ //#region src/cohere-error.ts
31
+ const cohereErrorDataSchema = z.object({ message: z.string() });
32
+ const cohereFailedResponseHandler = createJsonErrorResponseHandler({
33
+ errorSchema: cohereErrorDataSchema,
34
+ errorToMessage: (data) => data.message
62
35
  });
63
-
64
- // src/cohere-prepare-tools.ts
65
- import {
66
- UnsupportedFunctionalityError
67
- } from "@ai-sdk/provider";
68
- function prepareTools({
69
- tools,
70
- toolChoice
71
- }) {
72
- tools = tools?.length ? tools : void 0;
73
- const toolWarnings = [];
74
- if (tools == null) {
75
- return { tools: void 0, toolChoice: void 0, toolWarnings };
76
- }
77
- const cohereTools = [];
78
- for (const tool of tools) {
79
- if (tool.type === "provider") {
80
- toolWarnings.push({
81
- type: "unsupported",
82
- feature: `provider-defined tool ${tool.id}`
83
- });
84
- } else {
85
- cohereTools.push({
86
- type: "function",
87
- function: {
88
- name: tool.name,
89
- description: tool.description,
90
- parameters: tool.inputSchema
91
- }
92
- });
93
- }
94
- }
95
- if (toolChoice == null) {
96
- return { tools: cohereTools, toolChoice: void 0, toolWarnings };
97
- }
98
- const type = toolChoice.type;
99
- switch (type) {
100
- case "auto":
101
- return { tools: cohereTools, toolChoice: void 0, toolWarnings };
102
- case "none":
103
- return { tools: cohereTools, toolChoice: "NONE", toolWarnings };
104
- case "required":
105
- return { tools: cohereTools, toolChoice: "REQUIRED", toolWarnings };
106
- case "tool":
107
- return {
108
- tools: cohereTools.filter(
109
- (tool) => tool.function.name === toolChoice.toolName
110
- ),
111
- toolChoice: "REQUIRED",
112
- toolWarnings
113
- };
114
- default: {
115
- const _exhaustiveCheck = type;
116
- throw new UnsupportedFunctionalityError({
117
- functionality: `tool choice type: ${_exhaustiveCheck}`
118
- });
119
- }
120
- }
36
+ //#endregion
37
+ //#region src/cohere-prepare-tools.ts
38
+ function prepareTools({ tools, toolChoice }) {
39
+ tools = tools?.length ? tools : void 0;
40
+ const toolWarnings = [];
41
+ if (tools == null) return {
42
+ tools: void 0,
43
+ toolChoice: void 0,
44
+ toolWarnings
45
+ };
46
+ const cohereTools = [];
47
+ for (const tool of tools) if (tool.type === "provider") toolWarnings.push({
48
+ type: "unsupported",
49
+ feature: `provider-defined tool ${tool.id}`
50
+ });
51
+ else cohereTools.push({
52
+ type: "function",
53
+ function: {
54
+ name: tool.name,
55
+ description: tool.description,
56
+ parameters: tool.inputSchema
57
+ }
58
+ });
59
+ if (toolChoice == null) return {
60
+ tools: cohereTools,
61
+ toolChoice: void 0,
62
+ toolWarnings
63
+ };
64
+ const type = toolChoice.type;
65
+ switch (type) {
66
+ case "auto": return {
67
+ tools: cohereTools,
68
+ toolChoice: void 0,
69
+ toolWarnings
70
+ };
71
+ case "none": return {
72
+ tools: cohereTools,
73
+ toolChoice: "NONE",
74
+ toolWarnings
75
+ };
76
+ case "required": return {
77
+ tools: cohereTools,
78
+ toolChoice: "REQUIRED",
79
+ toolWarnings
80
+ };
81
+ case "tool": return {
82
+ tools: cohereTools.filter((tool) => tool.function.name === toolChoice.toolName),
83
+ toolChoice: "REQUIRED",
84
+ toolWarnings
85
+ };
86
+ default: throw new UnsupportedFunctionalityError({ functionality: `tool choice type: ${type}` });
87
+ }
121
88
  }
122
-
123
- // src/convert-cohere-usage.ts
124
- import { createNullLanguageModelUsage } from "@ai-sdk/provider-utils";
89
+ //#endregion
90
+ //#region src/convert-cohere-usage.ts
125
91
  function convertCohereUsage(usage) {
126
- if (usage == null) {
127
- return createNullLanguageModelUsage();
128
- }
129
- const tokens = usage.tokens;
130
- const inputTokens = tokens.input_tokens;
131
- const outputTokens = tokens.output_tokens;
132
- return {
133
- inputTokens: {
134
- total: inputTokens,
135
- noCache: inputTokens,
136
- cacheRead: void 0,
137
- cacheWrite: void 0
138
- },
139
- outputTokens: {
140
- total: outputTokens,
141
- text: outputTokens,
142
- reasoning: void 0
143
- },
144
- raw: usage
145
- };
92
+ if (usage == null) return createNullLanguageModelUsage();
93
+ const tokens = usage.tokens;
94
+ const inputTokens = tokens.input_tokens;
95
+ const outputTokens = tokens.output_tokens;
96
+ return {
97
+ inputTokens: {
98
+ total: inputTokens,
99
+ noCache: inputTokens,
100
+ cacheRead: void 0,
101
+ cacheWrite: void 0
102
+ },
103
+ outputTokens: {
104
+ total: outputTokens,
105
+ text: outputTokens,
106
+ reasoning: void 0
107
+ },
108
+ raw: usage
109
+ };
146
110
  }
147
-
148
- // src/convert-to-cohere-chat-prompt.ts
149
- import {
150
- UnsupportedFunctionalityError as UnsupportedFunctionalityError2
151
- } from "@ai-sdk/provider";
152
- import {
153
- convertToBase64,
154
- getTopLevelMediaType,
155
- parseProviderOptions,
156
- resolveFullMediaType
157
- } from "@ai-sdk/provider-utils";
111
+ //#endregion
112
+ //#region src/convert-to-cohere-chat-prompt.ts
158
113
  async function convertToCohereChatPrompt(prompt) {
159
- const messages = [];
160
- const documents = [];
161
- const warnings = [];
162
- for (const { role, content } of prompt) {
163
- switch (role) {
164
- case "system": {
165
- messages.push({ role: "system", content });
166
- break;
167
- }
168
- case "user": {
169
- const userContentParts = [];
170
- let hasImage = false;
171
- for (const part of content) {
172
- switch (part.type) {
173
- case "text": {
174
- if (part.text.length > 0) {
175
- userContentParts.push({ type: "text", text: part.text });
176
- }
177
- break;
178
- }
179
- case "file": {
180
- if (getTopLevelMediaType(part.mediaType) === "image") {
181
- hasImage = true;
182
- const url = buildImageUrl({ part });
183
- const cohereOptions = await parseProviderOptions({
184
- provider: "cohere",
185
- providerOptions: part.providerOptions,
186
- schema: cohereImagePartProviderOptions
187
- }) ?? {};
188
- userContentParts.push({
189
- type: "image_url",
190
- image_url: {
191
- url,
192
- ...cohereOptions.detail ? { detail: cohereOptions.detail } : {}
193
- }
194
- });
195
- break;
196
- }
197
- let textContent;
198
- switch (part.data.type) {
199
- case "reference": {
200
- throw new UnsupportedFunctionalityError2({
201
- functionality: "file parts with provider references"
202
- });
203
- }
204
- case "url": {
205
- throw new UnsupportedFunctionalityError2({
206
- functionality: "File URL data",
207
- message: "URLs should be downloaded by the AI SDK and not reach this point. This indicates a configuration issue."
208
- });
209
- }
210
- case "text": {
211
- textContent = part.data.text;
212
- break;
213
- }
214
- case "data": {
215
- textContent = typeof part.data.data === "string" ? part.data.data : new TextDecoder().decode(part.data.data);
216
- break;
217
- }
218
- }
219
- documents.push({
220
- data: {
221
- text: textContent,
222
- title: part.filename
223
- }
224
- });
225
- break;
226
- }
227
- }
228
- }
229
- if (hasImage) {
230
- messages.push({ role: "user", content: userContentParts });
231
- } else {
232
- messages.push({
233
- role: "user",
234
- content: userContentParts.map((p) => p.type === "text" ? p.text : "").join("")
235
- });
236
- }
237
- break;
238
- }
239
- case "assistant": {
240
- let text = "";
241
- const toolCalls = [];
242
- for (const part of content) {
243
- switch (part.type) {
244
- case "text": {
245
- text += part.text;
246
- break;
247
- }
248
- case "tool-call": {
249
- toolCalls.push({
250
- id: part.toolCallId,
251
- type: "function",
252
- function: {
253
- name: part.toolName,
254
- arguments: JSON.stringify(part.input)
255
- }
256
- });
257
- break;
258
- }
259
- }
260
- }
261
- messages.push({
262
- role: "assistant",
263
- content: toolCalls.length > 0 ? void 0 : text,
264
- tool_calls: toolCalls.length > 0 ? toolCalls : void 0,
265
- tool_plan: void 0
266
- });
267
- break;
268
- }
269
- case "tool": {
270
- messages.push(
271
- ...content.filter((toolResult) => toolResult.type !== "tool-approval-response").map((toolResult) => {
272
- const output = toolResult.output;
273
- let contentValue;
274
- switch (output.type) {
275
- case "text":
276
- case "error-text":
277
- contentValue = output.value;
278
- break;
279
- case "execution-denied":
280
- contentValue = output.reason ?? "Tool call execution denied.";
281
- break;
282
- case "content":
283
- case "json":
284
- case "error-json":
285
- contentValue = JSON.stringify(output.value);
286
- break;
287
- }
288
- return {
289
- role: "tool",
290
- content: contentValue,
291
- tool_call_id: toolResult.toolCallId
292
- };
293
- })
294
- );
295
- break;
296
- }
297
- default: {
298
- const _exhaustiveCheck = role;
299
- throw new Error(`Unsupported role: ${_exhaustiveCheck}`);
300
- }
301
- }
302
- }
303
- return { messages, documents, warnings };
114
+ const messages = [];
115
+ const documents = [];
116
+ const warnings = [];
117
+ for (const { role, content } of prompt) switch (role) {
118
+ case "system":
119
+ messages.push({
120
+ role: "system",
121
+ content
122
+ });
123
+ break;
124
+ case "user": {
125
+ const userContentParts = [];
126
+ let hasImage = false;
127
+ for (const part of content) switch (part.type) {
128
+ case "text":
129
+ if (part.text.length > 0) userContentParts.push({
130
+ type: "text",
131
+ text: part.text
132
+ });
133
+ break;
134
+ case "file": {
135
+ if (getTopLevelMediaType(part.mediaType) === "image") {
136
+ hasImage = true;
137
+ const url = buildImageUrl({ part });
138
+ const cohereOptions = await parseProviderOptions({
139
+ provider: "cohere",
140
+ providerOptions: part.providerOptions,
141
+ schema: cohereImagePartProviderOptions
142
+ }) ?? {};
143
+ userContentParts.push({
144
+ type: "image_url",
145
+ image_url: {
146
+ url,
147
+ ...cohereOptions.detail ? { detail: cohereOptions.detail } : {}
148
+ }
149
+ });
150
+ break;
151
+ }
152
+ let textContent;
153
+ switch (part.data.type) {
154
+ case "reference": throw new UnsupportedFunctionalityError({ functionality: "file parts with provider references" });
155
+ case "url": throw new UnsupportedFunctionalityError({
156
+ functionality: "File URL data",
157
+ message: "URLs should be downloaded by the AI SDK and not reach this point. This indicates a configuration issue."
158
+ });
159
+ case "text":
160
+ textContent = part.data.text;
161
+ break;
162
+ case "data": textContent = typeof part.data.data === "string" ? part.data.data : new TextDecoder().decode(part.data.data);
163
+ }
164
+ documents.push({ data: {
165
+ text: textContent,
166
+ title: part.filename
167
+ } });
168
+ break;
169
+ }
170
+ }
171
+ if (hasImage) messages.push({
172
+ role: "user",
173
+ content: userContentParts
174
+ });
175
+ else messages.push({
176
+ role: "user",
177
+ content: userContentParts.map((p) => p.type === "text" ? p.text : "").join("")
178
+ });
179
+ break;
180
+ }
181
+ case "assistant": {
182
+ let text = "";
183
+ const toolCalls = [];
184
+ for (const part of content) switch (part.type) {
185
+ case "text":
186
+ text += part.text;
187
+ break;
188
+ case "tool-call": toolCalls.push({
189
+ id: part.toolCallId,
190
+ type: "function",
191
+ function: {
192
+ name: part.toolName,
193
+ arguments: JSON.stringify(part.input)
194
+ }
195
+ });
196
+ }
197
+ messages.push({
198
+ role: "assistant",
199
+ content: toolCalls.length > 0 ? void 0 : text,
200
+ tool_calls: toolCalls.length > 0 ? toolCalls : void 0,
201
+ tool_plan: void 0
202
+ });
203
+ break;
204
+ }
205
+ case "tool":
206
+ messages.push(...content.filter((toolResult) => toolResult.type !== "tool-approval-response").map((toolResult) => {
207
+ const output = toolResult.output;
208
+ let contentValue;
209
+ switch (output.type) {
210
+ case "text":
211
+ case "error-text":
212
+ contentValue = output.value;
213
+ break;
214
+ case "execution-denied":
215
+ contentValue = output.reason ?? "Tool call execution denied.";
216
+ break;
217
+ case "content":
218
+ case "json":
219
+ case "error-json": contentValue = JSON.stringify(output.value);
220
+ }
221
+ return {
222
+ role: "tool",
223
+ content: contentValue,
224
+ tool_call_id: toolResult.toolCallId
225
+ };
226
+ }));
227
+ break;
228
+ default: throw new Error(`Unsupported role: ${role}`);
229
+ }
230
+ return {
231
+ messages,
232
+ documents,
233
+ warnings
234
+ };
304
235
  }
305
236
  function buildImageUrl({ part }) {
306
- switch (part.data.type) {
307
- case "url": {
308
- return part.data.url.toString();
309
- }
310
- case "data": {
311
- return `data:${resolveFullMediaType({ part })};base64,${convertToBase64(part.data.data)}`;
312
- }
313
- case "reference": {
314
- throw new UnsupportedFunctionalityError2({
315
- functionality: "image file parts with provider references"
316
- });
317
- }
318
- case "text": {
319
- throw new UnsupportedFunctionalityError2({
320
- functionality: "image file parts with text data"
321
- });
322
- }
323
- }
237
+ switch (part.data.type) {
238
+ case "url": return part.data.url.toString();
239
+ case "data": return `data:${resolveFullMediaType({ part })};base64,${convertToBase64(part.data.data)}`;
240
+ case "reference": throw new UnsupportedFunctionalityError({ functionality: "image file parts with provider references" });
241
+ case "text": throw new UnsupportedFunctionalityError({ functionality: "image file parts with text data" });
242
+ }
324
243
  }
325
-
326
- // src/map-cohere-finish-reason.ts
244
+ //#endregion
245
+ //#region src/map-cohere-finish-reason.ts
327
246
  function mapCohereFinishReason(finishReason) {
328
- switch (finishReason) {
329
- case "COMPLETE":
330
- case "STOP_SEQUENCE":
331
- return "stop";
332
- case "MAX_TOKENS":
333
- return "length";
334
- case "ERROR":
335
- return "error";
336
- case "TOOL_CALL":
337
- return "tool-calls";
338
- default:
339
- return "other";
340
- }
247
+ switch (finishReason) {
248
+ case "COMPLETE":
249
+ case "STOP_SEQUENCE": return "stop";
250
+ case "MAX_TOKENS": return "length";
251
+ case "ERROR": return "error";
252
+ case "TOOL_CALL": return "tool-calls";
253
+ default: return "other";
254
+ }
341
255
  }
342
-
343
- // src/cohere-chat-language-model.ts
344
- var CohereChatLanguageModel = class _CohereChatLanguageModel {
345
- constructor(modelId, config) {
346
- this.specificationVersion = "v4";
347
- this.supportedUrls = {
348
- "image/*": [/^https?:\/\/.*$/]
349
- };
350
- this.modelId = modelId;
351
- this.config = config;
352
- }
353
- static [WORKFLOW_SERIALIZE](model) {
354
- return serializeModelOptions({
355
- modelId: model.modelId,
356
- config: model.config
357
- });
358
- }
359
- static [WORKFLOW_DESERIALIZE](options) {
360
- return new _CohereChatLanguageModel(options.modelId, options.config);
361
- }
362
- get provider() {
363
- return this.config.provider;
364
- }
365
- async getArgs({
366
- prompt,
367
- maxOutputTokens,
368
- temperature,
369
- topP,
370
- topK,
371
- frequencyPenalty,
372
- presencePenalty,
373
- stopSequences,
374
- responseFormat,
375
- seed,
376
- reasoning,
377
- tools,
378
- toolChoice,
379
- providerOptions
380
- }) {
381
- const warnings = [];
382
- const cohereOptions = await parseProviderOptions2({
383
- provider: "cohere",
384
- providerOptions,
385
- schema: cohereLanguageModelChatOptions
386
- }) ?? {};
387
- const {
388
- messages: chatPrompt,
389
- documents: cohereDocuments,
390
- warnings: promptWarnings
391
- } = await convertToCohereChatPrompt(prompt);
392
- const {
393
- tools: cohereTools,
394
- toolChoice: cohereToolChoice,
395
- toolWarnings
396
- } = prepareTools({ tools, toolChoice });
397
- warnings.push(...toolWarnings, ...promptWarnings);
398
- return {
399
- args: {
400
- // model id:
401
- model: this.modelId,
402
- // standardized settings:
403
- frequency_penalty: frequencyPenalty,
404
- presence_penalty: presencePenalty,
405
- max_tokens: maxOutputTokens,
406
- temperature,
407
- p: topP,
408
- k: topK,
409
- seed,
410
- stop_sequences: stopSequences,
411
- // response format:
412
- response_format: responseFormat?.type === "json" ? { type: "json_object", json_schema: responseFormat.schema } : void 0,
413
- // messages:
414
- messages: chatPrompt,
415
- // tools:
416
- tools: cohereTools,
417
- tool_choice: cohereToolChoice,
418
- // documents for RAG:
419
- ...cohereDocuments.length > 0 && { documents: cohereDocuments },
420
- // reasoning:
421
- ...resolveCohereThinking({
422
- reasoning,
423
- cohereOptions,
424
- warnings
425
- })
426
- },
427
- warnings
428
- };
429
- }
430
- async doGenerate(options) {
431
- const { args, warnings } = await this.getArgs(options);
432
- const {
433
- responseHeaders,
434
- value: response,
435
- rawValue: rawResponse
436
- } = await postJsonToApi({
437
- url: `${this.config.baseURL}/chat`,
438
- headers: combineHeaders(this.config.headers?.(), options.headers),
439
- body: args,
440
- failedResponseHandler: cohereFailedResponseHandler,
441
- successfulResponseHandler: createJsonResponseHandler(
442
- cohereChatResponseSchema
443
- ),
444
- abortSignal: options.abortSignal,
445
- fetch: this.config.fetch
446
- });
447
- const content = [];
448
- for (const item of response.message.content ?? []) {
449
- if (item.type === "text" && item.text.length > 0) {
450
- content.push({ type: "text", text: item.text });
451
- continue;
452
- }
453
- if (item.type === "thinking" && item.thinking.length > 0) {
454
- content.push({ type: "reasoning", text: item.thinking });
455
- continue;
456
- }
457
- }
458
- for (const citation of response.message.citations ?? []) {
459
- content.push({
460
- type: "source",
461
- sourceType: "document",
462
- id: this.config.generateId(),
463
- mediaType: "text/plain",
464
- title: citation.sources[0]?.document?.title || "Document",
465
- providerMetadata: {
466
- cohere: {
467
- start: citation.start,
468
- end: citation.end,
469
- text: citation.text,
470
- sources: citation.sources,
471
- ...citation.type && { citationType: citation.type }
472
- }
473
- }
474
- });
475
- }
476
- for (const toolCall of response.message.tool_calls ?? []) {
477
- content.push({
478
- type: "tool-call",
479
- toolCallId: toolCall.id,
480
- toolName: toolCall.function.name,
481
- // Cohere sometimes returns `null` for tool call arguments for tools
482
- // defined as having no arguments.
483
- input: toolCall.function.arguments.replace(/^null$/, "{}")
484
- });
485
- }
486
- return {
487
- content,
488
- finishReason: {
489
- unified: mapCohereFinishReason(response.finish_reason),
490
- raw: response.finish_reason ?? void 0
491
- },
492
- usage: convertCohereUsage(response.usage),
493
- request: { body: args },
494
- response: {
495
- // TODO timestamp, model id
496
- id: response.generation_id ?? void 0,
497
- headers: responseHeaders,
498
- body: rawResponse
499
- },
500
- warnings
501
- };
502
- }
503
- async doStream(options) {
504
- const { args, warnings } = await this.getArgs(options);
505
- const { responseHeaders, value: response } = await postJsonToApi({
506
- url: `${this.config.baseURL}/chat`,
507
- headers: combineHeaders(this.config.headers?.(), options.headers),
508
- body: { ...args, stream: true },
509
- failedResponseHandler: cohereFailedResponseHandler,
510
- successfulResponseHandler: createEventSourceResponseHandler(
511
- cohereChatChunkSchema
512
- ),
513
- abortSignal: options.abortSignal,
514
- fetch: this.config.fetch
515
- });
516
- let finishReason = {
517
- unified: "other",
518
- raw: void 0
519
- };
520
- let usage = void 0;
521
- let pendingToolCall = null;
522
- let isActiveReasoning = false;
523
- return {
524
- stream: response.pipeThrough(
525
- new TransformStream({
526
- start(controller) {
527
- controller.enqueue({ type: "stream-start", warnings });
528
- },
529
- async transform(chunk, controller) {
530
- if (options.includeRawChunks) {
531
- controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
532
- }
533
- if (!chunk.success) {
534
- finishReason = { unified: "error", raw: void 0 };
535
- controller.enqueue({ type: "error", error: chunk.error });
536
- return;
537
- }
538
- const value = chunk.value;
539
- const type = value.type;
540
- switch (type) {
541
- case "content-start": {
542
- if (value.delta.message.content.type === "thinking") {
543
- controller.enqueue({
544
- type: "reasoning-start",
545
- id: String(value.index)
546
- });
547
- isActiveReasoning = true;
548
- return;
549
- }
550
- controller.enqueue({
551
- type: "text-start",
552
- id: String(value.index)
553
- });
554
- return;
555
- }
556
- case "content-delta": {
557
- if ("thinking" in value.delta.message.content) {
558
- controller.enqueue({
559
- type: "reasoning-delta",
560
- id: String(value.index),
561
- delta: value.delta.message.content.thinking
562
- });
563
- return;
564
- }
565
- controller.enqueue({
566
- type: "text-delta",
567
- id: String(value.index),
568
- delta: value.delta.message.content.text
569
- });
570
- return;
571
- }
572
- case "content-end": {
573
- if (isActiveReasoning) {
574
- controller.enqueue({
575
- type: "reasoning-end",
576
- id: String(value.index)
577
- });
578
- isActiveReasoning = false;
579
- return;
580
- }
581
- controller.enqueue({
582
- type: "text-end",
583
- id: String(value.index)
584
- });
585
- return;
586
- }
587
- case "tool-call-start": {
588
- const toolId = value.delta.message.tool_calls.id;
589
- const toolName = value.delta.message.tool_calls.function.name;
590
- const initialArgs = value.delta.message.tool_calls.function.arguments;
591
- pendingToolCall = {
592
- id: toolId,
593
- name: toolName,
594
- arguments: initialArgs,
595
- hasFinished: false
596
- };
597
- controller.enqueue({
598
- type: "tool-input-start",
599
- id: toolId,
600
- toolName
601
- });
602
- if (initialArgs.length > 0) {
603
- controller.enqueue({
604
- type: "tool-input-delta",
605
- id: toolId,
606
- delta: initialArgs
607
- });
608
- }
609
- return;
610
- }
611
- case "tool-call-delta": {
612
- if (pendingToolCall && !pendingToolCall.hasFinished) {
613
- const argsDelta = value.delta.message.tool_calls.function.arguments;
614
- pendingToolCall.arguments += argsDelta;
615
- controller.enqueue({
616
- type: "tool-input-delta",
617
- id: pendingToolCall.id,
618
- delta: argsDelta
619
- });
620
- }
621
- return;
622
- }
623
- case "tool-call-end": {
624
- if (pendingToolCall && !pendingToolCall.hasFinished) {
625
- controller.enqueue({
626
- type: "tool-input-end",
627
- id: pendingToolCall.id
628
- });
629
- controller.enqueue({
630
- type: "tool-call",
631
- toolCallId: pendingToolCall.id,
632
- toolName: pendingToolCall.name,
633
- input: JSON.stringify(
634
- await parseJSON({
635
- text: pendingToolCall.arguments?.trim() || "{}"
636
- })
637
- )
638
- });
639
- pendingToolCall.hasFinished = true;
640
- pendingToolCall = null;
641
- }
642
- return;
643
- }
644
- case "message-start": {
645
- controller.enqueue({
646
- type: "response-metadata",
647
- id: value.id ?? void 0
648
- });
649
- return;
650
- }
651
- case "message-end": {
652
- finishReason = {
653
- unified: mapCohereFinishReason(value.delta.finish_reason),
654
- raw: value.delta.finish_reason
655
- };
656
- usage = value.delta.usage;
657
- return;
658
- }
659
- default: {
660
- }
661
- }
662
- },
663
- flush(controller) {
664
- controller.enqueue({
665
- type: "finish",
666
- finishReason,
667
- usage: convertCohereUsage(usage)
668
- });
669
- }
670
- })
671
- ),
672
- request: { body: { ...args, stream: true } },
673
- response: { headers: responseHeaders }
674
- };
675
- }
256
+ //#endregion
257
+ //#region src/cohere-chat-language-model.ts
258
+ var CohereChatLanguageModel = class CohereChatLanguageModel {
259
+ static [WORKFLOW_SERIALIZE](model) {
260
+ return serializeModelOptions({
261
+ modelId: model.modelId,
262
+ config: model.config
263
+ });
264
+ }
265
+ static [WORKFLOW_DESERIALIZE](options) {
266
+ return new CohereChatLanguageModel(options.modelId, options.config);
267
+ }
268
+ constructor(modelId, config) {
269
+ this.specificationVersion = "v4";
270
+ this.supportedUrls = { "image/*": [/^https?:\/\/.*$/] };
271
+ this.modelId = modelId;
272
+ this.config = config;
273
+ }
274
+ get provider() {
275
+ return this.config.provider;
276
+ }
277
+ async getArgs({ prompt, maxOutputTokens, temperature, topP, topK, frequencyPenalty, presencePenalty, stopSequences, responseFormat, seed, reasoning, tools, toolChoice, providerOptions }) {
278
+ const warnings = [];
279
+ const cohereOptions = await parseProviderOptions({
280
+ provider: "cohere",
281
+ providerOptions,
282
+ schema: cohereLanguageModelChatOptions
283
+ }) ?? {};
284
+ const { messages: chatPrompt, documents: cohereDocuments, warnings: promptWarnings } = await convertToCohereChatPrompt(prompt);
285
+ const { tools: cohereTools, toolChoice: cohereToolChoice, toolWarnings } = prepareTools({
286
+ tools,
287
+ toolChoice
288
+ });
289
+ warnings.push(...toolWarnings, ...promptWarnings);
290
+ return {
291
+ args: {
292
+ model: this.modelId,
293
+ frequency_penalty: frequencyPenalty,
294
+ presence_penalty: presencePenalty,
295
+ max_tokens: maxOutputTokens,
296
+ temperature,
297
+ p: topP,
298
+ k: topK,
299
+ seed,
300
+ stop_sequences: stopSequences,
301
+ response_format: responseFormat?.type === "json" ? {
302
+ type: "json_object",
303
+ json_schema: responseFormat.schema
304
+ } : void 0,
305
+ messages: chatPrompt,
306
+ tools: cohereTools,
307
+ tool_choice: cohereToolChoice,
308
+ ...cohereDocuments.length > 0 && { documents: cohereDocuments },
309
+ ...resolveCohereThinking({
310
+ reasoning,
311
+ cohereOptions,
312
+ warnings
313
+ })
314
+ },
315
+ warnings
316
+ };
317
+ }
318
+ async doGenerate(options) {
319
+ const { args, warnings } = await this.getArgs(options);
320
+ const { responseHeaders, value: response, rawValue: rawResponse } = await postJsonToApi({
321
+ url: `${this.config.baseURL}/chat`,
322
+ headers: combineHeaders(this.config.headers?.(), options.headers),
323
+ body: args,
324
+ failedResponseHandler: cohereFailedResponseHandler,
325
+ successfulResponseHandler: createJsonResponseHandler(cohereChatResponseSchema),
326
+ abortSignal: options.abortSignal,
327
+ fetch: this.config.fetch
328
+ });
329
+ const content = [];
330
+ for (const item of response.message.content ?? []) {
331
+ if (item.type === "text" && item.text.length > 0) {
332
+ content.push({
333
+ type: "text",
334
+ text: item.text
335
+ });
336
+ continue;
337
+ }
338
+ if (item.type === "thinking" && item.thinking.length > 0) {
339
+ content.push({
340
+ type: "reasoning",
341
+ text: item.thinking
342
+ });
343
+ continue;
344
+ }
345
+ }
346
+ for (const citation of response.message.citations ?? []) content.push({
347
+ type: "source",
348
+ sourceType: "document",
349
+ id: this.config.generateId(),
350
+ mediaType: "text/plain",
351
+ title: citation.sources[0]?.document?.title || "Document",
352
+ providerMetadata: { cohere: {
353
+ start: citation.start,
354
+ end: citation.end,
355
+ text: citation.text,
356
+ sources: citation.sources,
357
+ ...citation.type && { citationType: citation.type }
358
+ } }
359
+ });
360
+ for (const toolCall of response.message.tool_calls ?? []) content.push({
361
+ type: "tool-call",
362
+ toolCallId: toolCall.id,
363
+ toolName: toolCall.function.name,
364
+ input: toolCall.function.arguments.replace(/^null$/, "{}")
365
+ });
366
+ return {
367
+ content,
368
+ finishReason: {
369
+ unified: mapCohereFinishReason(response.finish_reason),
370
+ raw: response.finish_reason ?? void 0
371
+ },
372
+ usage: convertCohereUsage(response.usage),
373
+ request: { body: args },
374
+ response: {
375
+ id: response.generation_id ?? void 0,
376
+ headers: responseHeaders,
377
+ body: rawResponse
378
+ },
379
+ warnings
380
+ };
381
+ }
382
+ async doStream(options) {
383
+ const { args, warnings } = await this.getArgs(options);
384
+ const { responseHeaders, value: response } = await postJsonToApi({
385
+ url: `${this.config.baseURL}/chat`,
386
+ headers: combineHeaders(this.config.headers?.(), options.headers),
387
+ body: {
388
+ ...args,
389
+ stream: true
390
+ },
391
+ failedResponseHandler: cohereFailedResponseHandler,
392
+ successfulResponseHandler: createEventSourceResponseHandler(cohereChatChunkSchema),
393
+ abortSignal: options.abortSignal,
394
+ fetch: this.config.fetch
395
+ });
396
+ let finishReason = {
397
+ unified: "other",
398
+ raw: void 0
399
+ };
400
+ let usage = void 0;
401
+ let pendingToolCall = null;
402
+ let isActiveReasoning = false;
403
+ return {
404
+ stream: response.pipeThrough(new TransformStream({
405
+ start(controller) {
406
+ controller.enqueue({
407
+ type: "stream-start",
408
+ warnings
409
+ });
410
+ },
411
+ async transform(chunk, controller) {
412
+ if (options.includeRawChunks) controller.enqueue({
413
+ type: "raw",
414
+ rawValue: chunk.rawValue
415
+ });
416
+ if (!chunk.success) {
417
+ finishReason = {
418
+ unified: "error",
419
+ raw: void 0
420
+ };
421
+ controller.enqueue({
422
+ type: "error",
423
+ error: chunk.error
424
+ });
425
+ return;
426
+ }
427
+ const value = chunk.value;
428
+ switch (value.type) {
429
+ case "content-start":
430
+ if (value.delta.message.content.type === "thinking") {
431
+ controller.enqueue({
432
+ type: "reasoning-start",
433
+ id: String(value.index)
434
+ });
435
+ isActiveReasoning = true;
436
+ return;
437
+ }
438
+ controller.enqueue({
439
+ type: "text-start",
440
+ id: String(value.index)
441
+ });
442
+ return;
443
+ case "content-delta":
444
+ if ("thinking" in value.delta.message.content) {
445
+ controller.enqueue({
446
+ type: "reasoning-delta",
447
+ id: String(value.index),
448
+ delta: value.delta.message.content.thinking
449
+ });
450
+ return;
451
+ }
452
+ controller.enqueue({
453
+ type: "text-delta",
454
+ id: String(value.index),
455
+ delta: value.delta.message.content.text
456
+ });
457
+ return;
458
+ case "content-end":
459
+ if (isActiveReasoning) {
460
+ controller.enqueue({
461
+ type: "reasoning-end",
462
+ id: String(value.index)
463
+ });
464
+ isActiveReasoning = false;
465
+ return;
466
+ }
467
+ controller.enqueue({
468
+ type: "text-end",
469
+ id: String(value.index)
470
+ });
471
+ return;
472
+ case "tool-call-start": {
473
+ const toolId = value.delta.message.tool_calls.id;
474
+ const toolName = value.delta.message.tool_calls.function.name;
475
+ const initialArgs = value.delta.message.tool_calls.function.arguments;
476
+ pendingToolCall = {
477
+ id: toolId,
478
+ name: toolName,
479
+ arguments: initialArgs,
480
+ hasFinished: false
481
+ };
482
+ controller.enqueue({
483
+ type: "tool-input-start",
484
+ id: toolId,
485
+ toolName
486
+ });
487
+ if (initialArgs.length > 0) controller.enqueue({
488
+ type: "tool-input-delta",
489
+ id: toolId,
490
+ delta: initialArgs
491
+ });
492
+ return;
493
+ }
494
+ case "tool-call-delta":
495
+ if (pendingToolCall && !pendingToolCall.hasFinished) {
496
+ const argsDelta = value.delta.message.tool_calls.function.arguments;
497
+ pendingToolCall.arguments += argsDelta;
498
+ controller.enqueue({
499
+ type: "tool-input-delta",
500
+ id: pendingToolCall.id,
501
+ delta: argsDelta
502
+ });
503
+ }
504
+ return;
505
+ case "tool-call-end":
506
+ if (pendingToolCall && !pendingToolCall.hasFinished) {
507
+ controller.enqueue({
508
+ type: "tool-input-end",
509
+ id: pendingToolCall.id
510
+ });
511
+ controller.enqueue({
512
+ type: "tool-call",
513
+ toolCallId: pendingToolCall.id,
514
+ toolName: pendingToolCall.name,
515
+ input: JSON.stringify(await parseJSON({ text: pendingToolCall.arguments?.trim() || "{}" }))
516
+ });
517
+ pendingToolCall.hasFinished = true;
518
+ pendingToolCall = null;
519
+ }
520
+ return;
521
+ case "message-start":
522
+ controller.enqueue({
523
+ type: "response-metadata",
524
+ id: value.id ?? void 0
525
+ });
526
+ return;
527
+ case "message-end":
528
+ finishReason = {
529
+ unified: mapCohereFinishReason(value.delta.finish_reason),
530
+ raw: value.delta.finish_reason
531
+ };
532
+ usage = value.delta.usage;
533
+ return;
534
+ }
535
+ },
536
+ flush(controller) {
537
+ controller.enqueue({
538
+ type: "finish",
539
+ finishReason,
540
+ usage: convertCohereUsage(usage)
541
+ });
542
+ }
543
+ })),
544
+ request: { body: {
545
+ ...args,
546
+ stream: true
547
+ } },
548
+ response: { headers: responseHeaders }
549
+ };
550
+ }
676
551
  };
677
- function resolveCohereThinking({
678
- reasoning,
679
- cohereOptions,
680
- warnings
681
- }) {
682
- if (cohereOptions.thinking) {
683
- return {
684
- thinking: {
685
- type: cohereOptions.thinking.type ?? "enabled",
686
- token_budget: cohereOptions.thinking.tokenBudget
687
- }
688
- };
689
- }
690
- if (!isCustomReasoning(reasoning)) {
691
- return {};
692
- }
693
- if (reasoning === "none") {
694
- return { thinking: { type: "disabled" } };
695
- }
696
- const tokenBudget = mapReasoningToProviderBudget({
697
- reasoning,
698
- maxOutputTokens: 32768,
699
- maxReasoningBudget: 32768,
700
- warnings
701
- });
702
- if (tokenBudget == null) {
703
- return {};
704
- }
705
- return { thinking: { type: "enabled", token_budget: tokenBudget } };
552
+ function resolveCohereThinking({ reasoning, cohereOptions, warnings }) {
553
+ if (cohereOptions.thinking) return { thinking: {
554
+ type: cohereOptions.thinking.type ?? "enabled",
555
+ token_budget: cohereOptions.thinking.tokenBudget
556
+ } };
557
+ if (!isCustomReasoning(reasoning)) return {};
558
+ if (reasoning === "none") return { thinking: { type: "disabled" } };
559
+ const tokenBudget = mapReasoningToProviderBudget({
560
+ reasoning,
561
+ maxOutputTokens: 32768,
562
+ maxReasoningBudget: 32768,
563
+ warnings
564
+ });
565
+ if (tokenBudget == null) return {};
566
+ return { thinking: {
567
+ type: "enabled",
568
+ token_budget: tokenBudget
569
+ } };
706
570
  }
707
- var cohereUsageSchema = z3.looseObject({
708
- billed_units: z3.looseObject({
709
- input_tokens: z3.number(),
710
- output_tokens: z3.number()
711
- }).nullish(),
712
- tokens: z3.looseObject({
713
- input_tokens: z3.number(),
714
- output_tokens: z3.number()
715
- }),
716
- cached_tokens: z3.number().nullish()
571
+ const cohereUsageSchema = z.looseObject({
572
+ billed_units: z.looseObject({
573
+ input_tokens: z.number(),
574
+ output_tokens: z.number()
575
+ }).nullish(),
576
+ tokens: z.looseObject({
577
+ input_tokens: z.number(),
578
+ output_tokens: z.number()
579
+ }),
580
+ cached_tokens: z.number().nullish()
717
581
  });
718
- var cohereChatResponseSchema = z3.object({
719
- generation_id: z3.string().nullish(),
720
- message: z3.object({
721
- role: z3.string(),
722
- content: z3.array(
723
- z3.union([
724
- z3.object({
725
- type: z3.literal("text"),
726
- text: z3.string()
727
- }),
728
- z3.object({
729
- type: z3.literal("thinking"),
730
- thinking: z3.string()
731
- })
732
- ])
733
- ).nullish(),
734
- tool_plan: z3.string().nullish(),
735
- tool_calls: z3.array(
736
- z3.object({
737
- id: z3.string(),
738
- type: z3.literal("function"),
739
- function: z3.object({
740
- name: z3.string(),
741
- arguments: z3.string()
742
- })
743
- })
744
- ).nullish(),
745
- citations: z3.array(
746
- z3.object({
747
- start: z3.number(),
748
- end: z3.number(),
749
- text: z3.string(),
750
- sources: z3.array(
751
- z3.object({
752
- type: z3.string().optional(),
753
- id: z3.string().optional(),
754
- document: z3.object({
755
- id: z3.string().optional(),
756
- text: z3.string(),
757
- title: z3.string()
758
- })
759
- })
760
- ),
761
- type: z3.string().optional()
762
- })
763
- ).nullish()
764
- }),
765
- finish_reason: z3.string(),
766
- usage: cohereUsageSchema
582
+ const cohereChatResponseSchema = z.object({
583
+ generation_id: z.string().nullish(),
584
+ message: z.object({
585
+ role: z.string(),
586
+ content: z.array(z.union([z.object({
587
+ type: z.literal("text"),
588
+ text: z.string()
589
+ }), z.object({
590
+ type: z.literal("thinking"),
591
+ thinking: z.string()
592
+ })])).nullish(),
593
+ tool_plan: z.string().nullish(),
594
+ tool_calls: z.array(z.object({
595
+ id: z.string(),
596
+ type: z.literal("function"),
597
+ function: z.object({
598
+ name: z.string(),
599
+ arguments: z.string()
600
+ })
601
+ })).nullish(),
602
+ citations: z.array(z.object({
603
+ start: z.number(),
604
+ end: z.number(),
605
+ text: z.string(),
606
+ sources: z.array(z.object({
607
+ type: z.string().optional(),
608
+ id: z.string().optional(),
609
+ document: z.object({
610
+ id: z.string().optional(),
611
+ text: z.string(),
612
+ title: z.string()
613
+ })
614
+ })),
615
+ type: z.string().optional()
616
+ })).nullish()
617
+ }),
618
+ finish_reason: z.string(),
619
+ usage: cohereUsageSchema
767
620
  });
768
- var cohereChatChunkSchema = z3.discriminatedUnion("type", [
769
- z3.object({
770
- type: z3.literal("citation-start")
771
- }),
772
- z3.object({
773
- type: z3.literal("citation-end")
774
- }),
775
- z3.object({
776
- type: z3.literal("content-start"),
777
- index: z3.number(),
778
- delta: z3.object({
779
- message: z3.object({
780
- content: z3.union([
781
- z3.object({
782
- type: z3.literal("text"),
783
- text: z3.string()
784
- }),
785
- z3.object({
786
- type: z3.literal("thinking"),
787
- thinking: z3.string()
788
- })
789
- ])
790
- })
791
- })
792
- }),
793
- z3.object({
794
- type: z3.literal("content-delta"),
795
- index: z3.number(),
796
- delta: z3.object({
797
- message: z3.object({
798
- content: z3.union([
799
- z3.object({
800
- text: z3.string()
801
- }),
802
- z3.object({
803
- thinking: z3.string()
804
- })
805
- ])
806
- })
807
- })
808
- }),
809
- z3.object({
810
- type: z3.literal("content-end"),
811
- index: z3.number()
812
- }),
813
- z3.object({
814
- type: z3.literal("message-start"),
815
- id: z3.string().nullish()
816
- }),
817
- z3.object({
818
- type: z3.literal("message-end"),
819
- delta: z3.object({
820
- finish_reason: z3.string(),
821
- usage: cohereUsageSchema
822
- })
823
- }),
824
- // https://docs.cohere.com/v2/docs/streaming#tool-use-stream-events-for-tool-calling
825
- z3.object({
826
- type: z3.literal("tool-plan-delta"),
827
- delta: z3.object({
828
- message: z3.object({
829
- tool_plan: z3.string()
830
- })
831
- })
832
- }),
833
- z3.object({
834
- type: z3.literal("tool-call-start"),
835
- delta: z3.object({
836
- message: z3.object({
837
- tool_calls: z3.object({
838
- id: z3.string(),
839
- type: z3.literal("function"),
840
- function: z3.object({
841
- name: z3.string(),
842
- arguments: z3.string()
843
- })
844
- })
845
- })
846
- })
847
- }),
848
- // A single tool call's `arguments` stream in chunks and must be accumulated
849
- // in a string and so the full tool object info can only be parsed once we see
850
- // `tool-call-end`.
851
- z3.object({
852
- type: z3.literal("tool-call-delta"),
853
- delta: z3.object({
854
- message: z3.object({
855
- tool_calls: z3.object({
856
- function: z3.object({
857
- arguments: z3.string()
858
- })
859
- })
860
- })
861
- })
862
- }),
863
- z3.object({
864
- type: z3.literal("tool-call-end")
865
- })
621
+ const cohereChatChunkSchema = z.discriminatedUnion("type", [
622
+ z.object({ type: z.literal("citation-start") }),
623
+ z.object({ type: z.literal("citation-end") }),
624
+ z.object({
625
+ type: z.literal("content-start"),
626
+ index: z.number(),
627
+ delta: z.object({ message: z.object({ content: z.union([z.object({
628
+ type: z.literal("text"),
629
+ text: z.string()
630
+ }), z.object({
631
+ type: z.literal("thinking"),
632
+ thinking: z.string()
633
+ })]) }) })
634
+ }),
635
+ z.object({
636
+ type: z.literal("content-delta"),
637
+ index: z.number(),
638
+ delta: z.object({ message: z.object({ content: z.union([z.object({ text: z.string() }), z.object({ thinking: z.string() })]) }) })
639
+ }),
640
+ z.object({
641
+ type: z.literal("content-end"),
642
+ index: z.number()
643
+ }),
644
+ z.object({
645
+ type: z.literal("message-start"),
646
+ id: z.string().nullish()
647
+ }),
648
+ z.object({
649
+ type: z.literal("message-end"),
650
+ delta: z.object({
651
+ finish_reason: z.string(),
652
+ usage: cohereUsageSchema
653
+ })
654
+ }),
655
+ z.object({
656
+ type: z.literal("tool-plan-delta"),
657
+ delta: z.object({ message: z.object({ tool_plan: z.string() }) })
658
+ }),
659
+ z.object({
660
+ type: z.literal("tool-call-start"),
661
+ delta: z.object({ message: z.object({ tool_calls: z.object({
662
+ id: z.string(),
663
+ type: z.literal("function"),
664
+ function: z.object({
665
+ name: z.string(),
666
+ arguments: z.string()
667
+ })
668
+ }) }) })
669
+ }),
670
+ z.object({
671
+ type: z.literal("tool-call-delta"),
672
+ delta: z.object({ message: z.object({ tool_calls: z.object({ function: z.object({ arguments: z.string() }) }) }) })
673
+ }),
674
+ z.object({ type: z.literal("tool-call-end") })
866
675
  ]);
867
-
868
- // src/cohere-embedding-model.ts
869
- import {
870
- TooManyEmbeddingValuesForCallError
871
- } from "@ai-sdk/provider";
872
- import {
873
- combineHeaders as combineHeaders2,
874
- createJsonResponseHandler as createJsonResponseHandler2,
875
- parseProviderOptions as parseProviderOptions3,
876
- postJsonToApi as postJsonToApi2,
877
- serializeModelOptions as serializeModelOptions2,
878
- WORKFLOW_SERIALIZE as WORKFLOW_SERIALIZE2,
879
- WORKFLOW_DESERIALIZE as WORKFLOW_DESERIALIZE2
880
- } from "@ai-sdk/provider-utils";
881
- import { z as z5 } from "zod/v4";
882
-
883
- // src/cohere-embedding-model-options.ts
884
- import { z as z4 } from "zod/v4";
885
- var cohereEmbeddingModelOptions = z4.object({
886
- /**
887
- * Specifies the type of input passed to the model. Default is `search_query`.
888
- *
889
- * - "search_document": Used for embeddings stored in a vector database for search use-cases.
890
- * - "search_query": Used for embeddings of search queries run against a vector DB to find relevant documents.
891
- * - "classification": Used for embeddings passed through a text classifier.
892
- * - "clustering": Used for embeddings run through a clustering algorithm.
893
- */
894
- inputType: z4.enum(["search_document", "search_query", "classification", "clustering"]).optional(),
895
- /**
896
- * Specifies how the API will handle inputs longer than the maximum token length.
897
- * Default is `END`.
898
- *
899
- * - "NONE": If selected, when the input exceeds the maximum input token length will return an error.
900
- * - "START": Will discard the start of the input until the remaining input is exactly the maximum input token length for the model.
901
- * - "END": Will discard the end of the input until the remaining input is exactly the maximum input token length for the model.
902
- */
903
- truncate: z4.enum(["NONE", "START", "END"]).optional(),
904
- /**
905
- * The number of dimensions of the output embedding.
906
- * Only available for `embed-v4.0` and newer models.
907
- *
908
- * Possible values are `256`, `512`, `1024`, and `1536`.
909
- * The default is `1536`.
910
- */
911
- outputDimension: z4.union([z4.literal(256), z4.literal(512), z4.literal(1024), z4.literal(1536)]).optional()
676
+ //#endregion
677
+ //#region src/cohere-embedding-model-options.ts
678
+ const cohereEmbeddingModelOptions = z.object({
679
+ /**
680
+ * Specifies the type of input passed to the model. Default is `search_query`.
681
+ *
682
+ * - "search_document": Used for embeddings stored in a vector database for search use-cases.
683
+ * - "search_query": Used for embeddings of search queries run against a vector DB to find relevant documents.
684
+ * - "classification": Used for embeddings passed through a text classifier.
685
+ * - "clustering": Used for embeddings run through a clustering algorithm.
686
+ */
687
+ inputType: z.enum([
688
+ "search_document",
689
+ "search_query",
690
+ "classification",
691
+ "clustering"
692
+ ]).optional(),
693
+ /**
694
+ * Specifies how the API will handle inputs longer than the maximum token length.
695
+ * Default is `END`.
696
+ *
697
+ * - "NONE": If selected, when the input exceeds the maximum input token length will return an error.
698
+ * - "START": Will discard the start of the input until the remaining input is exactly the maximum input token length for the model.
699
+ * - "END": Will discard the end of the input until the remaining input is exactly the maximum input token length for the model.
700
+ */
701
+ truncate: z.enum([
702
+ "NONE",
703
+ "START",
704
+ "END"
705
+ ]).optional(),
706
+ /**
707
+ * The number of dimensions of the output embedding.
708
+ * Only available for `embed-v4.0` and newer models.
709
+ *
710
+ * Possible values are `256`, `512`, `1024`, and `1536`.
711
+ * The default is `1536`.
712
+ */
713
+ outputDimension: z.union([
714
+ z.literal(256),
715
+ z.literal(512),
716
+ z.literal(1024),
717
+ z.literal(1536)
718
+ ]).optional()
912
719
  });
913
-
914
- // src/cohere-embedding-model.ts
915
- var CohereEmbeddingModel = class _CohereEmbeddingModel {
916
- constructor(modelId, config) {
917
- this.specificationVersion = "v4";
918
- this.maxEmbeddingsPerCall = 96;
919
- this.supportsParallelCalls = true;
920
- this.modelId = modelId;
921
- this.config = config;
922
- }
923
- static [WORKFLOW_SERIALIZE2](model) {
924
- return serializeModelOptions2({
925
- modelId: model.modelId,
926
- config: model.config
927
- });
928
- }
929
- static [WORKFLOW_DESERIALIZE2](options) {
930
- return new _CohereEmbeddingModel(options.modelId, options.config);
931
- }
932
- get provider() {
933
- return this.config.provider;
934
- }
935
- async doEmbed({
936
- values,
937
- headers,
938
- abortSignal,
939
- providerOptions
940
- }) {
941
- const embeddingOptions = await parseProviderOptions3({
942
- provider: "cohere",
943
- providerOptions,
944
- schema: cohereEmbeddingModelOptions
945
- });
946
- if (values.length > this.maxEmbeddingsPerCall) {
947
- throw new TooManyEmbeddingValuesForCallError({
948
- provider: this.provider,
949
- modelId: this.modelId,
950
- maxEmbeddingsPerCall: this.maxEmbeddingsPerCall,
951
- values
952
- });
953
- }
954
- const {
955
- responseHeaders,
956
- value: response,
957
- rawValue
958
- } = await postJsonToApi2({
959
- url: `${this.config.baseURL}/embed`,
960
- headers: combineHeaders2(this.config.headers?.(), headers),
961
- body: {
962
- model: this.modelId,
963
- // The AI SDK only supports 'float' embeddings. Note that the Cohere API
964
- // supports other embedding types, but they are not currently supported by the AI SDK.
965
- // https://docs.cohere.com/v2/reference/embed#request.body.embedding_types
966
- embedding_types: ["float"],
967
- texts: values,
968
- input_type: embeddingOptions?.inputType ?? "search_query",
969
- truncate: embeddingOptions?.truncate,
970
- output_dimension: embeddingOptions?.outputDimension
971
- },
972
- failedResponseHandler: cohereFailedResponseHandler,
973
- successfulResponseHandler: createJsonResponseHandler2(
974
- cohereTextEmbeddingResponseSchema
975
- ),
976
- abortSignal,
977
- fetch: this.config.fetch
978
- });
979
- return {
980
- warnings: [],
981
- embeddings: response.embeddings.float,
982
- usage: { tokens: response.meta.billed_units.input_tokens },
983
- response: { headers: responseHeaders, body: rawValue }
984
- };
985
- }
720
+ //#endregion
721
+ //#region src/cohere-embedding-model.ts
722
+ var CohereEmbeddingModel = class CohereEmbeddingModel {
723
+ static [WORKFLOW_SERIALIZE](model) {
724
+ return serializeModelOptions({
725
+ modelId: model.modelId,
726
+ config: model.config
727
+ });
728
+ }
729
+ static [WORKFLOW_DESERIALIZE](options) {
730
+ return new CohereEmbeddingModel(options.modelId, options.config);
731
+ }
732
+ constructor(modelId, config) {
733
+ this.specificationVersion = "v4";
734
+ this.maxEmbeddingsPerCall = 96;
735
+ this.supportsParallelCalls = true;
736
+ this.modelId = modelId;
737
+ this.config = config;
738
+ }
739
+ get provider() {
740
+ return this.config.provider;
741
+ }
742
+ async doEmbed({ values, headers, abortSignal, providerOptions }) {
743
+ const embeddingOptions = await parseProviderOptions({
744
+ provider: "cohere",
745
+ providerOptions,
746
+ schema: cohereEmbeddingModelOptions
747
+ });
748
+ if (values.length > this.maxEmbeddingsPerCall) throw new TooManyEmbeddingValuesForCallError({
749
+ provider: this.provider,
750
+ modelId: this.modelId,
751
+ maxEmbeddingsPerCall: this.maxEmbeddingsPerCall,
752
+ values
753
+ });
754
+ const { responseHeaders, value: response, rawValue } = await postJsonToApi({
755
+ url: `${this.config.baseURL}/embed`,
756
+ headers: combineHeaders(this.config.headers?.(), headers),
757
+ body: {
758
+ model: this.modelId,
759
+ embedding_types: ["float"],
760
+ texts: values,
761
+ input_type: embeddingOptions?.inputType ?? "search_query",
762
+ truncate: embeddingOptions?.truncate,
763
+ output_dimension: embeddingOptions?.outputDimension
764
+ },
765
+ failedResponseHandler: cohereFailedResponseHandler,
766
+ successfulResponseHandler: createJsonResponseHandler(cohereTextEmbeddingResponseSchema),
767
+ abortSignal,
768
+ fetch: this.config.fetch
769
+ });
770
+ return {
771
+ warnings: [],
772
+ embeddings: response.embeddings.float,
773
+ usage: { tokens: response.meta.billed_units.input_tokens },
774
+ response: {
775
+ headers: responseHeaders,
776
+ body: rawValue
777
+ }
778
+ };
779
+ }
986
780
  };
987
- var cohereTextEmbeddingResponseSchema = z5.object({
988
- embeddings: z5.object({
989
- float: z5.array(z5.array(z5.number()))
990
- }),
991
- meta: z5.object({
992
- billed_units: z5.object({
993
- input_tokens: z5.number()
994
- })
995
- })
781
+ const cohereTextEmbeddingResponseSchema = z.object({
782
+ embeddings: z.object({ float: z.array(z.array(z.number())) }),
783
+ meta: z.object({ billed_units: z.object({ input_tokens: z.number() }) })
996
784
  });
997
-
998
- // src/reranking/cohere-reranking-model.ts
999
- import {
1000
- combineHeaders as combineHeaders3,
1001
- createJsonResponseHandler as createJsonResponseHandler3,
1002
- parseProviderOptions as parseProviderOptions4,
1003
- postJsonToApi as postJsonToApi3
1004
- } from "@ai-sdk/provider-utils";
1005
-
1006
- // src/reranking/cohere-reranking-api.ts
1007
- import { lazySchema, zodSchema } from "@ai-sdk/provider-utils";
1008
- import { z as z6 } from "zod/v4";
1009
- var cohereRerankingResponseSchema = lazySchema(
1010
- () => zodSchema(
1011
- z6.object({
1012
- id: z6.string().nullish(),
1013
- results: z6.array(
1014
- z6.object({
1015
- index: z6.number(),
1016
- relevance_score: z6.number()
1017
- })
1018
- ),
1019
- meta: z6.any()
1020
- })
1021
- )
1022
- );
1023
-
1024
- // src/reranking/cohere-reranking-model-options.ts
1025
- import {
1026
- lazySchema as lazySchema2,
1027
- zodSchema as zodSchema2
1028
- } from "@ai-sdk/provider-utils";
1029
- import { z as z7 } from "zod/v4";
1030
- var cohereRerankingModelOptionsSchema = lazySchema2(
1031
- () => zodSchema2(
1032
- z7.object({
1033
- maxTokensPerDoc: z7.number().optional(),
1034
- priority: z7.number().optional()
1035
- })
1036
- )
1037
- );
1038
-
1039
- // src/reranking/cohere-reranking-model.ts
785
+ //#endregion
786
+ //#region src/reranking/cohere-reranking-api.ts
787
+ const cohereRerankingResponseSchema = lazySchema(() => zodSchema(z.object({
788
+ id: z.string().nullish(),
789
+ results: z.array(z.object({
790
+ index: z.number(),
791
+ relevance_score: z.number()
792
+ })),
793
+ meta: z.any()
794
+ })));
795
+ //#endregion
796
+ //#region src/reranking/cohere-reranking-model-options.ts
797
+ const cohereRerankingModelOptionsSchema = lazySchema(() => zodSchema(z.object({
798
+ maxTokensPerDoc: z.number().optional(),
799
+ priority: z.number().optional()
800
+ })));
801
+ //#endregion
802
+ //#region src/reranking/cohere-reranking-model.ts
1040
803
  var CohereRerankingModel = class {
1041
- constructor(modelId, config) {
1042
- this.specificationVersion = "v4";
1043
- this.modelId = modelId;
1044
- this.config = config;
1045
- }
1046
- get provider() {
1047
- return this.config.provider;
1048
- }
1049
- // current implementation is based on v2 of the API: https://docs.cohere.com/v2/reference/rerank
1050
- async doRerank({
1051
- documents,
1052
- headers,
1053
- query,
1054
- topN,
1055
- abortSignal,
1056
- providerOptions
1057
- }) {
1058
- const rerankingOptions = await parseProviderOptions4({
1059
- provider: "cohere",
1060
- providerOptions,
1061
- schema: cohereRerankingModelOptionsSchema
1062
- });
1063
- const warnings = [];
1064
- if (documents.type === "object") {
1065
- warnings.push({
1066
- type: "compatibility",
1067
- feature: "object documents",
1068
- details: "Object documents are converted to strings."
1069
- });
1070
- }
1071
- const {
1072
- responseHeaders,
1073
- value: response,
1074
- rawValue
1075
- } = await postJsonToApi3({
1076
- url: `${this.config.baseURL}/rerank`,
1077
- headers: combineHeaders3(this.config.headers(), headers),
1078
- body: {
1079
- model: this.modelId,
1080
- query,
1081
- documents: documents.type === "text" ? documents.values : documents.values.map((value) => JSON.stringify(value)),
1082
- top_n: topN,
1083
- max_tokens_per_doc: rerankingOptions?.maxTokensPerDoc,
1084
- priority: rerankingOptions?.priority
1085
- },
1086
- failedResponseHandler: cohereFailedResponseHandler,
1087
- successfulResponseHandler: createJsonResponseHandler3(
1088
- cohereRerankingResponseSchema
1089
- ),
1090
- abortSignal,
1091
- fetch: this.config.fetch
1092
- });
1093
- return {
1094
- ranking: response.results.map((result) => ({
1095
- index: result.index,
1096
- relevanceScore: result.relevance_score
1097
- })),
1098
- warnings,
1099
- response: {
1100
- id: response.id ?? void 0,
1101
- headers: responseHeaders,
1102
- body: rawValue
1103
- }
1104
- };
1105
- }
804
+ constructor(modelId, config) {
805
+ this.specificationVersion = "v4";
806
+ this.modelId = modelId;
807
+ this.config = config;
808
+ }
809
+ get provider() {
810
+ return this.config.provider;
811
+ }
812
+ async doRerank({ documents, headers, query, topN, abortSignal, providerOptions }) {
813
+ const rerankingOptions = await parseProviderOptions({
814
+ provider: "cohere",
815
+ providerOptions,
816
+ schema: cohereRerankingModelOptionsSchema
817
+ });
818
+ const warnings = [];
819
+ if (documents.type === "object") warnings.push({
820
+ type: "compatibility",
821
+ feature: "object documents",
822
+ details: "Object documents are converted to strings."
823
+ });
824
+ const { responseHeaders, value: response, rawValue } = await postJsonToApi({
825
+ url: `${this.config.baseURL}/rerank`,
826
+ headers: combineHeaders(this.config.headers(), headers),
827
+ body: {
828
+ model: this.modelId,
829
+ query,
830
+ documents: documents.type === "text" ? documents.values : documents.values.map((value) => JSON.stringify(value)),
831
+ top_n: topN,
832
+ max_tokens_per_doc: rerankingOptions?.maxTokensPerDoc,
833
+ priority: rerankingOptions?.priority
834
+ },
835
+ failedResponseHandler: cohereFailedResponseHandler,
836
+ successfulResponseHandler: createJsonResponseHandler(cohereRerankingResponseSchema),
837
+ abortSignal,
838
+ fetch: this.config.fetch
839
+ });
840
+ return {
841
+ ranking: response.results.map((result) => ({
842
+ index: result.index,
843
+ relevanceScore: result.relevance_score
844
+ })),
845
+ warnings,
846
+ response: {
847
+ id: response.id ?? void 0,
848
+ headers: responseHeaders,
849
+ body: rawValue
850
+ }
851
+ };
852
+ }
1106
853
  };
1107
-
1108
- // src/version.ts
1109
- var VERSION = true ? "4.0.51" : "0.0.0-test";
1110
-
1111
- // src/cohere-provider.ts
854
+ //#endregion
855
+ //#region src/version.ts
856
+ const VERSION = "4.0.53";
857
+ //#endregion
858
+ //#region src/cohere-provider.ts
859
+ /**
860
+ * Create a Cohere AI provider instance.
861
+ */
1112
862
  function createCohere(options = {}) {
1113
- const baseURL = withoutTrailingSlash(options.baseURL) ?? "https://api.cohere.com/v2";
1114
- const getHeaders = () => withUserAgentSuffix(
1115
- {
1116
- Authorization: `Bearer ${loadApiKey({
1117
- apiKey: options.apiKey,
1118
- environmentVariableName: "COHERE_API_KEY",
1119
- description: "Cohere"
1120
- })}`,
1121
- ...options.headers
1122
- },
1123
- `ai-sdk/cohere/${VERSION}`
1124
- );
1125
- const createChatModel = (modelId) => new CohereChatLanguageModel(modelId, {
1126
- provider: "cohere.chat",
1127
- baseURL,
1128
- headers: getHeaders,
1129
- fetch: options.fetch,
1130
- generateId: options.generateId ?? generateId
1131
- });
1132
- const createEmbeddingModel = (modelId) => new CohereEmbeddingModel(modelId, {
1133
- provider: "cohere.textEmbedding",
1134
- baseURL,
1135
- headers: getHeaders,
1136
- fetch: options.fetch
1137
- });
1138
- const createRerankingModel = (modelId) => new CohereRerankingModel(modelId, {
1139
- provider: "cohere.reranking",
1140
- baseURL,
1141
- headers: getHeaders,
1142
- fetch: options.fetch
1143
- });
1144
- const provider = function(modelId) {
1145
- if (new.target) {
1146
- throw new Error(
1147
- "The Cohere model function cannot be called with the new keyword."
1148
- );
1149
- }
1150
- return createChatModel(modelId);
1151
- };
1152
- provider.specificationVersion = "v4";
1153
- provider.languageModel = createChatModel;
1154
- provider.embedding = createEmbeddingModel;
1155
- provider.embeddingModel = createEmbeddingModel;
1156
- provider.textEmbedding = createEmbeddingModel;
1157
- provider.textEmbeddingModel = createEmbeddingModel;
1158
- provider.reranking = createRerankingModel;
1159
- provider.rerankingModel = createRerankingModel;
1160
- provider.imageModel = (modelId) => {
1161
- throw new NoSuchModelError({ modelId, modelType: "imageModel" });
1162
- };
1163
- return provider;
863
+ const baseURL = withoutTrailingSlash(options.baseURL) ?? "https://api.cohere.com/v2";
864
+ const getHeaders = () => withUserAgentSuffix({
865
+ Authorization: `Bearer ${loadApiKey({
866
+ apiKey: options.apiKey,
867
+ environmentVariableName: "COHERE_API_KEY",
868
+ description: "Cohere"
869
+ })}`,
870
+ ...options.headers
871
+ }, `ai-sdk-cohere/${VERSION}`);
872
+ const createChatModel = (modelId) => new CohereChatLanguageModel(modelId, {
873
+ provider: "cohere.chat",
874
+ baseURL,
875
+ headers: getHeaders,
876
+ fetch: options.fetch,
877
+ generateId: options.generateId ?? generateId
878
+ });
879
+ const createEmbeddingModel = (modelId) => new CohereEmbeddingModel(modelId, {
880
+ provider: "cohere.textEmbedding",
881
+ baseURL,
882
+ headers: getHeaders,
883
+ fetch: options.fetch
884
+ });
885
+ const createRerankingModel = (modelId) => new CohereRerankingModel(modelId, {
886
+ provider: "cohere.reranking",
887
+ baseURL,
888
+ headers: getHeaders,
889
+ fetch: options.fetch
890
+ });
891
+ const provider = function(modelId) {
892
+ if (new.target) throw new Error("The Cohere model function cannot be called with the new keyword.");
893
+ return createChatModel(modelId);
894
+ };
895
+ provider.specificationVersion = "v4";
896
+ provider.languageModel = createChatModel;
897
+ provider.embedding = createEmbeddingModel;
898
+ provider.embeddingModel = createEmbeddingModel;
899
+ provider.textEmbedding = createEmbeddingModel;
900
+ provider.textEmbeddingModel = createEmbeddingModel;
901
+ provider.reranking = createRerankingModel;
902
+ provider.rerankingModel = createRerankingModel;
903
+ provider.imageModel = (modelId) => {
904
+ throw new NoSuchModelError({
905
+ modelId,
906
+ modelType: "imageModel"
907
+ });
908
+ };
909
+ return provider;
1164
910
  }
1165
- var cohere = createCohere();
1166
- export {
1167
- VERSION,
1168
- cohere,
1169
- createCohere
1170
- };
911
+ /**
912
+ * Default Cohere provider instance.
913
+ */
914
+ const cohere = createCohere();
915
+ //#endregion
916
+ export { VERSION, cohere, createCohere };
917
+
1171
918
  //# sourceMappingURL=index.js.map