@ai-sdk/cohere 4.0.0-beta.9 → 4.0.0-canary.33
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/CHANGELOG.md +213 -0
- package/README.md +2 -0
- package/dist/index.d.ts +3 -3
- package/dist/index.js +279 -245
- package/dist/index.js.map +1 -1
- package/docs/25-cohere.mdx +2 -2
- package/package.json +11 -9
- package/src/{cohere-chat-options.ts → cohere-chat-language-model-options.ts} +3 -3
- package/src/cohere-chat-language-model.ts +33 -13
- package/src/cohere-embedding-model.ts +23 -6
- package/src/cohere-prepare-tools.ts +3 -3
- package/src/cohere-provider.ts +8 -9
- package/src/convert-cohere-usage.ts +1 -1
- package/src/convert-to-cohere-chat-prompt.ts +28 -27
- package/src/index.ts +8 -6
- package/src/map-cohere-finish-reason.ts +1 -1
- package/src/reranking/{cohere-reranking-options.ts → cohere-reranking-model-options.ts} +5 -1
- package/src/reranking/cohere-reranking-model.ts +5 -6
- package/dist/index.d.mts +0 -117
- package/dist/index.mjs +0 -1102
- package/dist/index.mjs.map +0 -1
- /package/src/{cohere-embedding-options.ts → cohere-embedding-model-options.ts} +0 -0
package/dist/index.js
CHANGED
|
@@ -1,42 +1,32 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
|
|
20
|
-
// src/index.ts
|
|
21
|
-
var index_exports = {};
|
|
22
|
-
__export(index_exports, {
|
|
23
|
-
VERSION: () => VERSION,
|
|
24
|
-
cohere: () => cohere,
|
|
25
|
-
createCohere: () => createCohere
|
|
26
|
-
});
|
|
27
|
-
module.exports = __toCommonJS(index_exports);
|
|
28
|
-
|
|
29
1
|
// src/cohere-provider.ts
|
|
30
|
-
|
|
31
|
-
|
|
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";
|
|
32
11
|
|
|
33
12
|
// src/cohere-chat-language-model.ts
|
|
34
|
-
|
|
35
|
-
|
|
13
|
+
import {
|
|
14
|
+
combineHeaders,
|
|
15
|
+
createEventSourceResponseHandler,
|
|
16
|
+
createJsonResponseHandler,
|
|
17
|
+
isCustomReasoning,
|
|
18
|
+
mapReasoningToProviderBudget,
|
|
19
|
+
parseProviderOptions,
|
|
20
|
+
postJsonToApi,
|
|
21
|
+
serializeModelOptions,
|
|
22
|
+
WORKFLOW_SERIALIZE,
|
|
23
|
+
WORKFLOW_DESERIALIZE
|
|
24
|
+
} from "@ai-sdk/provider-utils";
|
|
25
|
+
import { z as z3 } from "zod/v4";
|
|
36
26
|
|
|
37
|
-
// src/cohere-chat-options.ts
|
|
38
|
-
|
|
39
|
-
var
|
|
27
|
+
// src/cohere-chat-language-model-options.ts
|
|
28
|
+
import { z } from "zod/v4";
|
|
29
|
+
var cohereLanguageModelChatOptions = z.object({
|
|
40
30
|
/**
|
|
41
31
|
* Configuration for reasoning features (optional)
|
|
42
32
|
*
|
|
@@ -45,25 +35,27 @@ var cohereLanguageModelOptions = import_v4.z.object({
|
|
|
45
35
|
*
|
|
46
36
|
* @see https://docs.cohere.com/reference/chat#request.body.thinking
|
|
47
37
|
*/
|
|
48
|
-
thinking:
|
|
49
|
-
type:
|
|
50
|
-
tokenBudget:
|
|
38
|
+
thinking: z.object({
|
|
39
|
+
type: z.enum(["enabled", "disabled"]).optional(),
|
|
40
|
+
tokenBudget: z.number().optional()
|
|
51
41
|
}).optional()
|
|
52
42
|
});
|
|
53
43
|
|
|
54
44
|
// src/cohere-error.ts
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
var cohereErrorDataSchema =
|
|
58
|
-
message:
|
|
45
|
+
import { createJsonErrorResponseHandler } from "@ai-sdk/provider-utils";
|
|
46
|
+
import { z as z2 } from "zod/v4";
|
|
47
|
+
var cohereErrorDataSchema = z2.object({
|
|
48
|
+
message: z2.string()
|
|
59
49
|
});
|
|
60
|
-
var cohereFailedResponseHandler =
|
|
50
|
+
var cohereFailedResponseHandler = createJsonErrorResponseHandler({
|
|
61
51
|
errorSchema: cohereErrorDataSchema,
|
|
62
52
|
errorToMessage: (data) => data.message
|
|
63
53
|
});
|
|
64
54
|
|
|
65
55
|
// src/cohere-prepare-tools.ts
|
|
66
|
-
|
|
56
|
+
import {
|
|
57
|
+
UnsupportedFunctionalityError
|
|
58
|
+
} from "@ai-sdk/provider";
|
|
67
59
|
function prepareTools({
|
|
68
60
|
tools,
|
|
69
61
|
toolChoice
|
|
@@ -112,7 +104,7 @@ function prepareTools({
|
|
|
112
104
|
};
|
|
113
105
|
default: {
|
|
114
106
|
const _exhaustiveCheck = type;
|
|
115
|
-
throw new
|
|
107
|
+
throw new UnsupportedFunctionalityError({
|
|
116
108
|
functionality: `tool choice type: ${_exhaustiveCheck}`
|
|
117
109
|
});
|
|
118
110
|
}
|
|
@@ -156,7 +148,9 @@ function convertCohereUsage(tokens) {
|
|
|
156
148
|
}
|
|
157
149
|
|
|
158
150
|
// src/convert-to-cohere-chat-prompt.ts
|
|
159
|
-
|
|
151
|
+
import {
|
|
152
|
+
UnsupportedFunctionalityError as UnsupportedFunctionalityError2
|
|
153
|
+
} from "@ai-sdk/provider";
|
|
160
154
|
function convertToCohereChatPrompt(prompt) {
|
|
161
155
|
const messages = [];
|
|
162
156
|
const documents = [];
|
|
@@ -171,28 +165,32 @@ function convertToCohereChatPrompt(prompt) {
|
|
|
171
165
|
messages.push({
|
|
172
166
|
role: "user",
|
|
173
167
|
content: content.map((part) => {
|
|
174
|
-
var _a;
|
|
175
168
|
switch (part.type) {
|
|
176
169
|
case "text": {
|
|
177
170
|
return part.text;
|
|
178
171
|
}
|
|
179
172
|
case "file": {
|
|
180
173
|
let textContent;
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
throw new import_provider2.UnsupportedFunctionalityError({
|
|
186
|
-
functionality: `document media type: ${part.mediaType}`,
|
|
187
|
-
message: `Media type '${part.mediaType}' is not supported. Supported media types are: text/* and application/json.`
|
|
174
|
+
switch (part.data.type) {
|
|
175
|
+
case "reference": {
|
|
176
|
+
throw new UnsupportedFunctionalityError2({
|
|
177
|
+
functionality: "file parts with provider references"
|
|
188
178
|
});
|
|
189
179
|
}
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
}
|
|
180
|
+
case "url": {
|
|
181
|
+
throw new UnsupportedFunctionalityError2({
|
|
182
|
+
functionality: "File URL data",
|
|
183
|
+
message: "URLs should be downloaded by the AI SDK and not reach this point. This indicates a configuration issue."
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
case "text": {
|
|
187
|
+
textContent = part.data.text;
|
|
188
|
+
break;
|
|
189
|
+
}
|
|
190
|
+
case "data": {
|
|
191
|
+
textContent = typeof part.data.data === "string" ? part.data.data : new TextDecoder().decode(part.data.data);
|
|
192
|
+
break;
|
|
193
|
+
}
|
|
196
194
|
}
|
|
197
195
|
documents.push({
|
|
198
196
|
data: {
|
|
@@ -249,7 +247,7 @@ function convertToCohereChatPrompt(prompt) {
|
|
|
249
247
|
contentValue = output.value;
|
|
250
248
|
break;
|
|
251
249
|
case "execution-denied":
|
|
252
|
-
contentValue = (_a = output.reason) != null ? _a : "Tool execution denied.";
|
|
250
|
+
contentValue = (_a = output.reason) != null ? _a : "Tool call execution denied.";
|
|
253
251
|
break;
|
|
254
252
|
case "content":
|
|
255
253
|
case "json":
|
|
@@ -293,7 +291,7 @@ function mapCohereFinishReason(finishReason) {
|
|
|
293
291
|
}
|
|
294
292
|
|
|
295
293
|
// src/cohere-chat-language-model.ts
|
|
296
|
-
var CohereChatLanguageModel = class {
|
|
294
|
+
var CohereChatLanguageModel = class _CohereChatLanguageModel {
|
|
297
295
|
constructor(modelId, config) {
|
|
298
296
|
this.specificationVersion = "v4";
|
|
299
297
|
this.supportedUrls = {
|
|
@@ -302,6 +300,15 @@ var CohereChatLanguageModel = class {
|
|
|
302
300
|
this.modelId = modelId;
|
|
303
301
|
this.config = config;
|
|
304
302
|
}
|
|
303
|
+
static [WORKFLOW_SERIALIZE](model) {
|
|
304
|
+
return serializeModelOptions({
|
|
305
|
+
modelId: model.modelId,
|
|
306
|
+
config: model.config
|
|
307
|
+
});
|
|
308
|
+
}
|
|
309
|
+
static [WORKFLOW_DESERIALIZE](options) {
|
|
310
|
+
return new _CohereChatLanguageModel(options.modelId, options.config);
|
|
311
|
+
}
|
|
305
312
|
get provider() {
|
|
306
313
|
return this.config.provider;
|
|
307
314
|
}
|
|
@@ -323,10 +330,10 @@ var CohereChatLanguageModel = class {
|
|
|
323
330
|
}) {
|
|
324
331
|
var _a;
|
|
325
332
|
const warnings = [];
|
|
326
|
-
const cohereOptions = (_a = await
|
|
333
|
+
const cohereOptions = (_a = await parseProviderOptions({
|
|
327
334
|
provider: "cohere",
|
|
328
335
|
providerOptions,
|
|
329
|
-
schema:
|
|
336
|
+
schema: cohereLanguageModelChatOptions
|
|
330
337
|
})) != null ? _a : {};
|
|
331
338
|
const {
|
|
332
339
|
messages: chatPrompt,
|
|
@@ -372,25 +379,25 @@ var CohereChatLanguageModel = class {
|
|
|
372
379
|
};
|
|
373
380
|
}
|
|
374
381
|
async doGenerate(options) {
|
|
375
|
-
var _a, _b, _c, _d, _e, _f, _g;
|
|
382
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i;
|
|
376
383
|
const { args, warnings } = await this.getArgs(options);
|
|
377
384
|
const {
|
|
378
385
|
responseHeaders,
|
|
379
386
|
value: response,
|
|
380
387
|
rawValue: rawResponse
|
|
381
|
-
} = await
|
|
388
|
+
} = await postJsonToApi({
|
|
382
389
|
url: `${this.config.baseURL}/chat`,
|
|
383
|
-
headers: (
|
|
390
|
+
headers: combineHeaders((_b = (_a = this.config).headers) == null ? void 0 : _b.call(_a), options.headers),
|
|
384
391
|
body: args,
|
|
385
392
|
failedResponseHandler: cohereFailedResponseHandler,
|
|
386
|
-
successfulResponseHandler:
|
|
393
|
+
successfulResponseHandler: createJsonResponseHandler(
|
|
387
394
|
cohereChatResponseSchema
|
|
388
395
|
),
|
|
389
396
|
abortSignal: options.abortSignal,
|
|
390
397
|
fetch: this.config.fetch
|
|
391
398
|
});
|
|
392
399
|
const content = [];
|
|
393
|
-
for (const item of (
|
|
400
|
+
for (const item of (_c = response.message.content) != null ? _c : []) {
|
|
394
401
|
if (item.type === "text" && item.text.length > 0) {
|
|
395
402
|
content.push({ type: "text", text: item.text });
|
|
396
403
|
continue;
|
|
@@ -400,13 +407,13 @@ var CohereChatLanguageModel = class {
|
|
|
400
407
|
continue;
|
|
401
408
|
}
|
|
402
409
|
}
|
|
403
|
-
for (const citation of (
|
|
410
|
+
for (const citation of (_d = response.message.citations) != null ? _d : []) {
|
|
404
411
|
content.push({
|
|
405
412
|
type: "source",
|
|
406
413
|
sourceType: "document",
|
|
407
414
|
id: this.config.generateId(),
|
|
408
415
|
mediaType: "text/plain",
|
|
409
|
-
title: ((
|
|
416
|
+
title: ((_f = (_e = citation.sources[0]) == null ? void 0 : _e.document) == null ? void 0 : _f.title) || "Document",
|
|
410
417
|
providerMetadata: {
|
|
411
418
|
cohere: {
|
|
412
419
|
start: citation.start,
|
|
@@ -418,7 +425,7 @@ var CohereChatLanguageModel = class {
|
|
|
418
425
|
}
|
|
419
426
|
});
|
|
420
427
|
}
|
|
421
|
-
for (const toolCall of (
|
|
428
|
+
for (const toolCall of (_g = response.message.tool_calls) != null ? _g : []) {
|
|
422
429
|
content.push({
|
|
423
430
|
type: "tool-call",
|
|
424
431
|
toolCallId: toolCall.id,
|
|
@@ -432,13 +439,13 @@ var CohereChatLanguageModel = class {
|
|
|
432
439
|
content,
|
|
433
440
|
finishReason: {
|
|
434
441
|
unified: mapCohereFinishReason(response.finish_reason),
|
|
435
|
-
raw: (
|
|
442
|
+
raw: (_h = response.finish_reason) != null ? _h : void 0
|
|
436
443
|
},
|
|
437
444
|
usage: convertCohereUsage(response.usage.tokens),
|
|
438
445
|
request: { body: args },
|
|
439
446
|
response: {
|
|
440
447
|
// TODO timestamp, model id
|
|
441
|
-
id: (
|
|
448
|
+
id: (_i = response.generation_id) != null ? _i : void 0,
|
|
442
449
|
headers: responseHeaders,
|
|
443
450
|
body: rawResponse
|
|
444
451
|
},
|
|
@@ -446,13 +453,14 @@ var CohereChatLanguageModel = class {
|
|
|
446
453
|
};
|
|
447
454
|
}
|
|
448
455
|
async doStream(options) {
|
|
456
|
+
var _a, _b;
|
|
449
457
|
const { args, warnings } = await this.getArgs(options);
|
|
450
|
-
const { responseHeaders, value: response } = await
|
|
458
|
+
const { responseHeaders, value: response } = await postJsonToApi({
|
|
451
459
|
url: `${this.config.baseURL}/chat`,
|
|
452
|
-
headers: (
|
|
460
|
+
headers: combineHeaders((_b = (_a = this.config).headers) == null ? void 0 : _b.call(_a), options.headers),
|
|
453
461
|
body: { ...args, stream: true },
|
|
454
462
|
failedResponseHandler: cohereFailedResponseHandler,
|
|
455
|
-
successfulResponseHandler:
|
|
463
|
+
successfulResponseHandler: createEventSourceResponseHandler(
|
|
456
464
|
cohereChatChunkSchema
|
|
457
465
|
),
|
|
458
466
|
abortSignal: options.abortSignal,
|
|
@@ -472,7 +480,7 @@ var CohereChatLanguageModel = class {
|
|
|
472
480
|
controller.enqueue({ type: "stream-start", warnings });
|
|
473
481
|
},
|
|
474
482
|
transform(chunk, controller) {
|
|
475
|
-
var
|
|
483
|
+
var _a2, _b2;
|
|
476
484
|
if (options.includeRawChunks) {
|
|
477
485
|
controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
|
|
478
486
|
}
|
|
@@ -577,7 +585,7 @@ var CohereChatLanguageModel = class {
|
|
|
577
585
|
toolCallId: pendingToolCall.id,
|
|
578
586
|
toolName: pendingToolCall.name,
|
|
579
587
|
input: JSON.stringify(
|
|
580
|
-
JSON.parse(((
|
|
588
|
+
JSON.parse(((_a2 = pendingToolCall.arguments) == null ? void 0 : _a2.trim()) || "{}")
|
|
581
589
|
)
|
|
582
590
|
});
|
|
583
591
|
pendingToolCall.hasFinished = true;
|
|
@@ -588,7 +596,7 @@ var CohereChatLanguageModel = class {
|
|
|
588
596
|
case "message-start": {
|
|
589
597
|
controller.enqueue({
|
|
590
598
|
type: "response-metadata",
|
|
591
|
-
id: (
|
|
599
|
+
id: (_b2 = value.id) != null ? _b2 : void 0
|
|
592
600
|
});
|
|
593
601
|
return;
|
|
594
602
|
}
|
|
@@ -633,13 +641,13 @@ function resolveCohereThinking({
|
|
|
633
641
|
}
|
|
634
642
|
};
|
|
635
643
|
}
|
|
636
|
-
if (!
|
|
644
|
+
if (!isCustomReasoning(reasoning)) {
|
|
637
645
|
return {};
|
|
638
646
|
}
|
|
639
647
|
if (reasoning === "none") {
|
|
640
648
|
return { thinking: { type: "disabled" } };
|
|
641
649
|
}
|
|
642
|
-
const tokenBudget =
|
|
650
|
+
const tokenBudget = mapReasoningToProviderBudget({
|
|
643
651
|
reasoning,
|
|
644
652
|
maxOutputTokens: 32768,
|
|
645
653
|
maxReasoningBudget: 32768,
|
|
@@ -650,145 +658,145 @@ function resolveCohereThinking({
|
|
|
650
658
|
}
|
|
651
659
|
return { thinking: { type: "enabled", token_budget: tokenBudget } };
|
|
652
660
|
}
|
|
653
|
-
var cohereChatResponseSchema =
|
|
654
|
-
generation_id:
|
|
655
|
-
message:
|
|
656
|
-
role:
|
|
657
|
-
content:
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
type:
|
|
661
|
-
text:
|
|
661
|
+
var cohereChatResponseSchema = z3.object({
|
|
662
|
+
generation_id: z3.string().nullish(),
|
|
663
|
+
message: z3.object({
|
|
664
|
+
role: z3.string(),
|
|
665
|
+
content: z3.array(
|
|
666
|
+
z3.union([
|
|
667
|
+
z3.object({
|
|
668
|
+
type: z3.literal("text"),
|
|
669
|
+
text: z3.string()
|
|
662
670
|
}),
|
|
663
|
-
|
|
664
|
-
type:
|
|
665
|
-
thinking:
|
|
671
|
+
z3.object({
|
|
672
|
+
type: z3.literal("thinking"),
|
|
673
|
+
thinking: z3.string()
|
|
666
674
|
})
|
|
667
675
|
])
|
|
668
676
|
).nullish(),
|
|
669
|
-
tool_plan:
|
|
670
|
-
tool_calls:
|
|
671
|
-
|
|
672
|
-
id:
|
|
673
|
-
type:
|
|
674
|
-
function:
|
|
675
|
-
name:
|
|
676
|
-
arguments:
|
|
677
|
+
tool_plan: z3.string().nullish(),
|
|
678
|
+
tool_calls: z3.array(
|
|
679
|
+
z3.object({
|
|
680
|
+
id: z3.string(),
|
|
681
|
+
type: z3.literal("function"),
|
|
682
|
+
function: z3.object({
|
|
683
|
+
name: z3.string(),
|
|
684
|
+
arguments: z3.string()
|
|
677
685
|
})
|
|
678
686
|
})
|
|
679
687
|
).nullish(),
|
|
680
|
-
citations:
|
|
681
|
-
|
|
682
|
-
start:
|
|
683
|
-
end:
|
|
684
|
-
text:
|
|
685
|
-
sources:
|
|
686
|
-
|
|
687
|
-
type:
|
|
688
|
-
id:
|
|
689
|
-
document:
|
|
690
|
-
id:
|
|
691
|
-
text:
|
|
692
|
-
title:
|
|
688
|
+
citations: z3.array(
|
|
689
|
+
z3.object({
|
|
690
|
+
start: z3.number(),
|
|
691
|
+
end: z3.number(),
|
|
692
|
+
text: z3.string(),
|
|
693
|
+
sources: z3.array(
|
|
694
|
+
z3.object({
|
|
695
|
+
type: z3.string().optional(),
|
|
696
|
+
id: z3.string().optional(),
|
|
697
|
+
document: z3.object({
|
|
698
|
+
id: z3.string().optional(),
|
|
699
|
+
text: z3.string(),
|
|
700
|
+
title: z3.string()
|
|
693
701
|
})
|
|
694
702
|
})
|
|
695
703
|
),
|
|
696
|
-
type:
|
|
704
|
+
type: z3.string().optional()
|
|
697
705
|
})
|
|
698
706
|
).nullish()
|
|
699
707
|
}),
|
|
700
|
-
finish_reason:
|
|
701
|
-
usage:
|
|
702
|
-
billed_units:
|
|
703
|
-
input_tokens:
|
|
704
|
-
output_tokens:
|
|
708
|
+
finish_reason: z3.string(),
|
|
709
|
+
usage: z3.object({
|
|
710
|
+
billed_units: z3.object({
|
|
711
|
+
input_tokens: z3.number(),
|
|
712
|
+
output_tokens: z3.number()
|
|
705
713
|
}),
|
|
706
|
-
tokens:
|
|
707
|
-
input_tokens:
|
|
708
|
-
output_tokens:
|
|
714
|
+
tokens: z3.object({
|
|
715
|
+
input_tokens: z3.number(),
|
|
716
|
+
output_tokens: z3.number()
|
|
709
717
|
})
|
|
710
718
|
})
|
|
711
719
|
});
|
|
712
|
-
var cohereChatChunkSchema =
|
|
713
|
-
|
|
714
|
-
type:
|
|
720
|
+
var cohereChatChunkSchema = z3.discriminatedUnion("type", [
|
|
721
|
+
z3.object({
|
|
722
|
+
type: z3.literal("citation-start")
|
|
715
723
|
}),
|
|
716
|
-
|
|
717
|
-
type:
|
|
724
|
+
z3.object({
|
|
725
|
+
type: z3.literal("citation-end")
|
|
718
726
|
}),
|
|
719
|
-
|
|
720
|
-
type:
|
|
721
|
-
index:
|
|
722
|
-
delta:
|
|
723
|
-
message:
|
|
724
|
-
content:
|
|
725
|
-
|
|
726
|
-
type:
|
|
727
|
-
text:
|
|
727
|
+
z3.object({
|
|
728
|
+
type: z3.literal("content-start"),
|
|
729
|
+
index: z3.number(),
|
|
730
|
+
delta: z3.object({
|
|
731
|
+
message: z3.object({
|
|
732
|
+
content: z3.union([
|
|
733
|
+
z3.object({
|
|
734
|
+
type: z3.literal("text"),
|
|
735
|
+
text: z3.string()
|
|
728
736
|
}),
|
|
729
|
-
|
|
730
|
-
type:
|
|
731
|
-
thinking:
|
|
737
|
+
z3.object({
|
|
738
|
+
type: z3.literal("thinking"),
|
|
739
|
+
thinking: z3.string()
|
|
732
740
|
})
|
|
733
741
|
])
|
|
734
742
|
})
|
|
735
743
|
})
|
|
736
744
|
}),
|
|
737
|
-
|
|
738
|
-
type:
|
|
739
|
-
index:
|
|
740
|
-
delta:
|
|
741
|
-
message:
|
|
742
|
-
content:
|
|
743
|
-
|
|
744
|
-
text:
|
|
745
|
+
z3.object({
|
|
746
|
+
type: z3.literal("content-delta"),
|
|
747
|
+
index: z3.number(),
|
|
748
|
+
delta: z3.object({
|
|
749
|
+
message: z3.object({
|
|
750
|
+
content: z3.union([
|
|
751
|
+
z3.object({
|
|
752
|
+
text: z3.string()
|
|
745
753
|
}),
|
|
746
|
-
|
|
747
|
-
thinking:
|
|
754
|
+
z3.object({
|
|
755
|
+
thinking: z3.string()
|
|
748
756
|
})
|
|
749
757
|
])
|
|
750
758
|
})
|
|
751
759
|
})
|
|
752
760
|
}),
|
|
753
|
-
|
|
754
|
-
type:
|
|
755
|
-
index:
|
|
761
|
+
z3.object({
|
|
762
|
+
type: z3.literal("content-end"),
|
|
763
|
+
index: z3.number()
|
|
756
764
|
}),
|
|
757
|
-
|
|
758
|
-
type:
|
|
759
|
-
id:
|
|
765
|
+
z3.object({
|
|
766
|
+
type: z3.literal("message-start"),
|
|
767
|
+
id: z3.string().nullish()
|
|
760
768
|
}),
|
|
761
|
-
|
|
762
|
-
type:
|
|
763
|
-
delta:
|
|
764
|
-
finish_reason:
|
|
765
|
-
usage:
|
|
766
|
-
tokens:
|
|
767
|
-
input_tokens:
|
|
768
|
-
output_tokens:
|
|
769
|
+
z3.object({
|
|
770
|
+
type: z3.literal("message-end"),
|
|
771
|
+
delta: z3.object({
|
|
772
|
+
finish_reason: z3.string(),
|
|
773
|
+
usage: z3.object({
|
|
774
|
+
tokens: z3.object({
|
|
775
|
+
input_tokens: z3.number(),
|
|
776
|
+
output_tokens: z3.number()
|
|
769
777
|
})
|
|
770
778
|
})
|
|
771
779
|
})
|
|
772
780
|
}),
|
|
773
781
|
// https://docs.cohere.com/v2/docs/streaming#tool-use-stream-events-for-tool-calling
|
|
774
|
-
|
|
775
|
-
type:
|
|
776
|
-
delta:
|
|
777
|
-
message:
|
|
778
|
-
tool_plan:
|
|
782
|
+
z3.object({
|
|
783
|
+
type: z3.literal("tool-plan-delta"),
|
|
784
|
+
delta: z3.object({
|
|
785
|
+
message: z3.object({
|
|
786
|
+
tool_plan: z3.string()
|
|
779
787
|
})
|
|
780
788
|
})
|
|
781
789
|
}),
|
|
782
|
-
|
|
783
|
-
type:
|
|
784
|
-
delta:
|
|
785
|
-
message:
|
|
786
|
-
tool_calls:
|
|
787
|
-
id:
|
|
788
|
-
type:
|
|
789
|
-
function:
|
|
790
|
-
name:
|
|
791
|
-
arguments:
|
|
790
|
+
z3.object({
|
|
791
|
+
type: z3.literal("tool-call-start"),
|
|
792
|
+
delta: z3.object({
|
|
793
|
+
message: z3.object({
|
|
794
|
+
tool_calls: z3.object({
|
|
795
|
+
id: z3.string(),
|
|
796
|
+
type: z3.literal("function"),
|
|
797
|
+
function: z3.object({
|
|
798
|
+
name: z3.string(),
|
|
799
|
+
arguments: z3.string()
|
|
792
800
|
})
|
|
793
801
|
})
|
|
794
802
|
})
|
|
@@ -797,31 +805,41 @@ var cohereChatChunkSchema = import_v43.z.discriminatedUnion("type", [
|
|
|
797
805
|
// A single tool call's `arguments` stream in chunks and must be accumulated
|
|
798
806
|
// in a string and so the full tool object info can only be parsed once we see
|
|
799
807
|
// `tool-call-end`.
|
|
800
|
-
|
|
801
|
-
type:
|
|
802
|
-
delta:
|
|
803
|
-
message:
|
|
804
|
-
tool_calls:
|
|
805
|
-
function:
|
|
806
|
-
arguments:
|
|
808
|
+
z3.object({
|
|
809
|
+
type: z3.literal("tool-call-delta"),
|
|
810
|
+
delta: z3.object({
|
|
811
|
+
message: z3.object({
|
|
812
|
+
tool_calls: z3.object({
|
|
813
|
+
function: z3.object({
|
|
814
|
+
arguments: z3.string()
|
|
807
815
|
})
|
|
808
816
|
})
|
|
809
817
|
})
|
|
810
818
|
})
|
|
811
819
|
}),
|
|
812
|
-
|
|
813
|
-
type:
|
|
820
|
+
z3.object({
|
|
821
|
+
type: z3.literal("tool-call-end")
|
|
814
822
|
})
|
|
815
823
|
]);
|
|
816
824
|
|
|
817
825
|
// src/cohere-embedding-model.ts
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
826
|
+
import {
|
|
827
|
+
TooManyEmbeddingValuesForCallError
|
|
828
|
+
} from "@ai-sdk/provider";
|
|
829
|
+
import {
|
|
830
|
+
combineHeaders as combineHeaders2,
|
|
831
|
+
createJsonResponseHandler as createJsonResponseHandler2,
|
|
832
|
+
parseProviderOptions as parseProviderOptions2,
|
|
833
|
+
postJsonToApi as postJsonToApi2,
|
|
834
|
+
serializeModelOptions as serializeModelOptions2,
|
|
835
|
+
WORKFLOW_SERIALIZE as WORKFLOW_SERIALIZE2,
|
|
836
|
+
WORKFLOW_DESERIALIZE as WORKFLOW_DESERIALIZE2
|
|
837
|
+
} from "@ai-sdk/provider-utils";
|
|
838
|
+
import { z as z5 } from "zod/v4";
|
|
821
839
|
|
|
822
|
-
// src/cohere-embedding-options.ts
|
|
823
|
-
|
|
824
|
-
var cohereEmbeddingModelOptions =
|
|
840
|
+
// src/cohere-embedding-model-options.ts
|
|
841
|
+
import { z as z4 } from "zod/v4";
|
|
842
|
+
var cohereEmbeddingModelOptions = z4.object({
|
|
825
843
|
/**
|
|
826
844
|
* Specifies the type of input passed to the model. Default is `search_query`.
|
|
827
845
|
*
|
|
@@ -830,7 +848,7 @@ var cohereEmbeddingModelOptions = import_v44.z.object({
|
|
|
830
848
|
* - "classification": Used for embeddings passed through a text classifier.
|
|
831
849
|
* - "clustering": Used for embeddings run through a clustering algorithm.
|
|
832
850
|
*/
|
|
833
|
-
inputType:
|
|
851
|
+
inputType: z4.enum(["search_document", "search_query", "classification", "clustering"]).optional(),
|
|
834
852
|
/**
|
|
835
853
|
* Specifies how the API will handle inputs longer than the maximum token length.
|
|
836
854
|
* Default is `END`.
|
|
@@ -839,7 +857,7 @@ var cohereEmbeddingModelOptions = import_v44.z.object({
|
|
|
839
857
|
* - "START": Will discard the start of the input until the remaining input is exactly the maximum input token length for the model.
|
|
840
858
|
* - "END": Will discard the end of the input until the remaining input is exactly the maximum input token length for the model.
|
|
841
859
|
*/
|
|
842
|
-
truncate:
|
|
860
|
+
truncate: z4.enum(["NONE", "START", "END"]).optional(),
|
|
843
861
|
/**
|
|
844
862
|
* The number of dimensions of the output embedding.
|
|
845
863
|
* Only available for `embed-v4.0` and newer models.
|
|
@@ -847,11 +865,11 @@ var cohereEmbeddingModelOptions = import_v44.z.object({
|
|
|
847
865
|
* Possible values are `256`, `512`, `1024`, and `1536`.
|
|
848
866
|
* The default is `1536`.
|
|
849
867
|
*/
|
|
850
|
-
outputDimension:
|
|
868
|
+
outputDimension: z4.union([z4.literal(256), z4.literal(512), z4.literal(1024), z4.literal(1536)]).optional()
|
|
851
869
|
});
|
|
852
870
|
|
|
853
871
|
// src/cohere-embedding-model.ts
|
|
854
|
-
var CohereEmbeddingModel = class {
|
|
872
|
+
var CohereEmbeddingModel = class _CohereEmbeddingModel {
|
|
855
873
|
constructor(modelId, config) {
|
|
856
874
|
this.specificationVersion = "v4";
|
|
857
875
|
this.maxEmbeddingsPerCall = 96;
|
|
@@ -859,6 +877,15 @@ var CohereEmbeddingModel = class {
|
|
|
859
877
|
this.modelId = modelId;
|
|
860
878
|
this.config = config;
|
|
861
879
|
}
|
|
880
|
+
static [WORKFLOW_SERIALIZE2](model) {
|
|
881
|
+
return serializeModelOptions2({
|
|
882
|
+
modelId: model.modelId,
|
|
883
|
+
config: model.config
|
|
884
|
+
});
|
|
885
|
+
}
|
|
886
|
+
static [WORKFLOW_DESERIALIZE2](options) {
|
|
887
|
+
return new _CohereEmbeddingModel(options.modelId, options.config);
|
|
888
|
+
}
|
|
862
889
|
get provider() {
|
|
863
890
|
return this.config.provider;
|
|
864
891
|
}
|
|
@@ -868,14 +895,14 @@ var CohereEmbeddingModel = class {
|
|
|
868
895
|
abortSignal,
|
|
869
896
|
providerOptions
|
|
870
897
|
}) {
|
|
871
|
-
var _a;
|
|
872
|
-
const embeddingOptions = await (
|
|
898
|
+
var _a, _b, _c;
|
|
899
|
+
const embeddingOptions = await parseProviderOptions2({
|
|
873
900
|
provider: "cohere",
|
|
874
901
|
providerOptions,
|
|
875
902
|
schema: cohereEmbeddingModelOptions
|
|
876
903
|
});
|
|
877
904
|
if (values.length > this.maxEmbeddingsPerCall) {
|
|
878
|
-
throw new
|
|
905
|
+
throw new TooManyEmbeddingValuesForCallError({
|
|
879
906
|
provider: this.provider,
|
|
880
907
|
modelId: this.modelId,
|
|
881
908
|
maxEmbeddingsPerCall: this.maxEmbeddingsPerCall,
|
|
@@ -886,9 +913,9 @@ var CohereEmbeddingModel = class {
|
|
|
886
913
|
responseHeaders,
|
|
887
914
|
value: response,
|
|
888
915
|
rawValue
|
|
889
|
-
} = await (
|
|
916
|
+
} = await postJsonToApi2({
|
|
890
917
|
url: `${this.config.baseURL}/embed`,
|
|
891
|
-
headers: (
|
|
918
|
+
headers: combineHeaders2((_b = (_a = this.config).headers) == null ? void 0 : _b.call(_a), headers),
|
|
892
919
|
body: {
|
|
893
920
|
model: this.modelId,
|
|
894
921
|
// The AI SDK only supports 'float' embeddings. Note that the Cohere API
|
|
@@ -896,12 +923,12 @@ var CohereEmbeddingModel = class {
|
|
|
896
923
|
// https://docs.cohere.com/v2/reference/embed#request.body.embedding_types
|
|
897
924
|
embedding_types: ["float"],
|
|
898
925
|
texts: values,
|
|
899
|
-
input_type: (
|
|
926
|
+
input_type: (_c = embeddingOptions == null ? void 0 : embeddingOptions.inputType) != null ? _c : "search_query",
|
|
900
927
|
truncate: embeddingOptions == null ? void 0 : embeddingOptions.truncate,
|
|
901
928
|
output_dimension: embeddingOptions == null ? void 0 : embeddingOptions.outputDimension
|
|
902
929
|
},
|
|
903
930
|
failedResponseHandler: cohereFailedResponseHandler,
|
|
904
|
-
successfulResponseHandler: (
|
|
931
|
+
successfulResponseHandler: createJsonResponseHandler2(
|
|
905
932
|
cohereTextEmbeddingResponseSchema
|
|
906
933
|
),
|
|
907
934
|
abortSignal,
|
|
@@ -915,46 +942,54 @@ var CohereEmbeddingModel = class {
|
|
|
915
942
|
};
|
|
916
943
|
}
|
|
917
944
|
};
|
|
918
|
-
var cohereTextEmbeddingResponseSchema =
|
|
919
|
-
embeddings:
|
|
920
|
-
float:
|
|
945
|
+
var cohereTextEmbeddingResponseSchema = z5.object({
|
|
946
|
+
embeddings: z5.object({
|
|
947
|
+
float: z5.array(z5.array(z5.number()))
|
|
921
948
|
}),
|
|
922
|
-
meta:
|
|
923
|
-
billed_units:
|
|
924
|
-
input_tokens:
|
|
949
|
+
meta: z5.object({
|
|
950
|
+
billed_units: z5.object({
|
|
951
|
+
input_tokens: z5.number()
|
|
925
952
|
})
|
|
926
953
|
})
|
|
927
954
|
});
|
|
928
955
|
|
|
929
956
|
// src/reranking/cohere-reranking-model.ts
|
|
930
|
-
|
|
957
|
+
import {
|
|
958
|
+
combineHeaders as combineHeaders3,
|
|
959
|
+
createJsonResponseHandler as createJsonResponseHandler3,
|
|
960
|
+
parseProviderOptions as parseProviderOptions3,
|
|
961
|
+
postJsonToApi as postJsonToApi3
|
|
962
|
+
} from "@ai-sdk/provider-utils";
|
|
931
963
|
|
|
932
964
|
// src/reranking/cohere-reranking-api.ts
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
var cohereRerankingResponseSchema =
|
|
936
|
-
() =>
|
|
937
|
-
|
|
938
|
-
id:
|
|
939
|
-
results:
|
|
940
|
-
|
|
941
|
-
index:
|
|
942
|
-
relevance_score:
|
|
965
|
+
import { lazySchema, zodSchema } from "@ai-sdk/provider-utils";
|
|
966
|
+
import { z as z6 } from "zod/v4";
|
|
967
|
+
var cohereRerankingResponseSchema = lazySchema(
|
|
968
|
+
() => zodSchema(
|
|
969
|
+
z6.object({
|
|
970
|
+
id: z6.string().nullish(),
|
|
971
|
+
results: z6.array(
|
|
972
|
+
z6.object({
|
|
973
|
+
index: z6.number(),
|
|
974
|
+
relevance_score: z6.number()
|
|
943
975
|
})
|
|
944
976
|
),
|
|
945
|
-
meta:
|
|
977
|
+
meta: z6.any()
|
|
946
978
|
})
|
|
947
979
|
)
|
|
948
980
|
);
|
|
949
981
|
|
|
950
|
-
// src/reranking/cohere-reranking-options.ts
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
982
|
+
// src/reranking/cohere-reranking-model-options.ts
|
|
983
|
+
import {
|
|
984
|
+
lazySchema as lazySchema2,
|
|
985
|
+
zodSchema as zodSchema2
|
|
986
|
+
} from "@ai-sdk/provider-utils";
|
|
987
|
+
import { z as z7 } from "zod/v4";
|
|
988
|
+
var cohereRerankingModelOptionsSchema = lazySchema2(
|
|
989
|
+
() => zodSchema2(
|
|
990
|
+
z7.object({
|
|
991
|
+
maxTokensPerDoc: z7.number().optional(),
|
|
992
|
+
priority: z7.number().optional()
|
|
958
993
|
})
|
|
959
994
|
)
|
|
960
995
|
);
|
|
@@ -979,7 +1014,7 @@ var CohereRerankingModel = class {
|
|
|
979
1014
|
providerOptions
|
|
980
1015
|
}) {
|
|
981
1016
|
var _a;
|
|
982
|
-
const rerankingOptions = await (
|
|
1017
|
+
const rerankingOptions = await parseProviderOptions3({
|
|
983
1018
|
provider: "cohere",
|
|
984
1019
|
providerOptions,
|
|
985
1020
|
schema: cohereRerankingModelOptionsSchema
|
|
@@ -996,9 +1031,9 @@ var CohereRerankingModel = class {
|
|
|
996
1031
|
responseHeaders,
|
|
997
1032
|
value: response,
|
|
998
1033
|
rawValue
|
|
999
|
-
} = await (
|
|
1034
|
+
} = await postJsonToApi3({
|
|
1000
1035
|
url: `${this.config.baseURL}/rerank`,
|
|
1001
|
-
headers: (
|
|
1036
|
+
headers: combineHeaders3(this.config.headers(), headers),
|
|
1002
1037
|
body: {
|
|
1003
1038
|
model: this.modelId,
|
|
1004
1039
|
query,
|
|
@@ -1008,7 +1043,7 @@ var CohereRerankingModel = class {
|
|
|
1008
1043
|
priority: rerankingOptions == null ? void 0 : rerankingOptions.priority
|
|
1009
1044
|
},
|
|
1010
1045
|
failedResponseHandler: cohereFailedResponseHandler,
|
|
1011
|
-
successfulResponseHandler: (
|
|
1046
|
+
successfulResponseHandler: createJsonResponseHandler3(
|
|
1012
1047
|
cohereRerankingResponseSchema
|
|
1013
1048
|
),
|
|
1014
1049
|
abortSignal,
|
|
@@ -1030,15 +1065,15 @@ var CohereRerankingModel = class {
|
|
|
1030
1065
|
};
|
|
1031
1066
|
|
|
1032
1067
|
// src/version.ts
|
|
1033
|
-
var VERSION = true ? "4.0.0-
|
|
1068
|
+
var VERSION = true ? "4.0.0-canary.33" : "0.0.0-test";
|
|
1034
1069
|
|
|
1035
1070
|
// src/cohere-provider.ts
|
|
1036
1071
|
function createCohere(options = {}) {
|
|
1037
1072
|
var _a;
|
|
1038
|
-
const baseURL = (_a =
|
|
1039
|
-
const getHeaders = () =>
|
|
1073
|
+
const baseURL = (_a = withoutTrailingSlash(options.baseURL)) != null ? _a : "https://api.cohere.com/v2";
|
|
1074
|
+
const getHeaders = () => withUserAgentSuffix(
|
|
1040
1075
|
{
|
|
1041
|
-
Authorization: `Bearer ${
|
|
1076
|
+
Authorization: `Bearer ${loadApiKey({
|
|
1042
1077
|
apiKey: options.apiKey,
|
|
1043
1078
|
environmentVariableName: "COHERE_API_KEY",
|
|
1044
1079
|
description: "Cohere"
|
|
@@ -1054,7 +1089,7 @@ function createCohere(options = {}) {
|
|
|
1054
1089
|
baseURL,
|
|
1055
1090
|
headers: getHeaders,
|
|
1056
1091
|
fetch: options.fetch,
|
|
1057
|
-
generateId: (_a2 = options.generateId) != null ? _a2 :
|
|
1092
|
+
generateId: (_a2 = options.generateId) != null ? _a2 : generateId
|
|
1058
1093
|
});
|
|
1059
1094
|
};
|
|
1060
1095
|
const createEmbeddingModel = (modelId) => new CohereEmbeddingModel(modelId, {
|
|
@@ -1086,15 +1121,14 @@ function createCohere(options = {}) {
|
|
|
1086
1121
|
provider.reranking = createRerankingModel;
|
|
1087
1122
|
provider.rerankingModel = createRerankingModel;
|
|
1088
1123
|
provider.imageModel = (modelId) => {
|
|
1089
|
-
throw new
|
|
1124
|
+
throw new NoSuchModelError({ modelId, modelType: "imageModel" });
|
|
1090
1125
|
};
|
|
1091
1126
|
return provider;
|
|
1092
1127
|
}
|
|
1093
1128
|
var cohere = createCohere();
|
|
1094
|
-
|
|
1095
|
-
0 && (module.exports = {
|
|
1129
|
+
export {
|
|
1096
1130
|
VERSION,
|
|
1097
1131
|
cohere,
|
|
1098
1132
|
createCohere
|
|
1099
|
-
}
|
|
1133
|
+
};
|
|
1100
1134
|
//# sourceMappingURL=index.js.map
|