@ai-sdk/cohere 4.0.0-beta.2 → 4.0.0-beta.21
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 +155 -4
- package/README.md +2 -0
- package/dist/index.d.ts +10 -10
- package/dist/index.js +266 -225
- package/dist/index.js.map +1 -1
- package/package.json +7 -9
- package/src/cohere-chat-language-model.ts +72 -25
- package/src/cohere-embedding-model.ts +5 -5
- package/src/cohere-prepare-tools.ts +6 -6
- package/src/cohere-provider.ts +14 -14
- package/src/convert-cohere-usage.ts +2 -2
- package/src/convert-to-cohere-chat-prompt.ts +12 -5
- package/src/map-cohere-finish-reason.ts +2 -2
- package/src/reranking/cohere-reranking-model.ts +6 -6
- package/dist/index.d.mts +0 -117
- package/dist/index.mjs +0 -1067
- package/dist/index.mjs.map +0 -1
package/dist/index.js
CHANGED
|
@@ -1,42 +1,29 @@
|
|
|
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
|
+
} from "@ai-sdk/provider-utils";
|
|
22
|
+
import { z as z3 } from "zod/v4";
|
|
36
23
|
|
|
37
24
|
// src/cohere-chat-options.ts
|
|
38
|
-
|
|
39
|
-
var cohereLanguageModelOptions =
|
|
25
|
+
import { z } from "zod/v4";
|
|
26
|
+
var cohereLanguageModelOptions = z.object({
|
|
40
27
|
/**
|
|
41
28
|
* Configuration for reasoning features (optional)
|
|
42
29
|
*
|
|
@@ -45,25 +32,27 @@ var cohereLanguageModelOptions = import_v4.z.object({
|
|
|
45
32
|
*
|
|
46
33
|
* @see https://docs.cohere.com/reference/chat#request.body.thinking
|
|
47
34
|
*/
|
|
48
|
-
thinking:
|
|
49
|
-
type:
|
|
50
|
-
tokenBudget:
|
|
35
|
+
thinking: z.object({
|
|
36
|
+
type: z.enum(["enabled", "disabled"]).optional(),
|
|
37
|
+
tokenBudget: z.number().optional()
|
|
51
38
|
}).optional()
|
|
52
39
|
});
|
|
53
40
|
|
|
54
41
|
// src/cohere-error.ts
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
var cohereErrorDataSchema =
|
|
58
|
-
message:
|
|
42
|
+
import { createJsonErrorResponseHandler } from "@ai-sdk/provider-utils";
|
|
43
|
+
import { z as z2 } from "zod/v4";
|
|
44
|
+
var cohereErrorDataSchema = z2.object({
|
|
45
|
+
message: z2.string()
|
|
59
46
|
});
|
|
60
|
-
var cohereFailedResponseHandler =
|
|
47
|
+
var cohereFailedResponseHandler = createJsonErrorResponseHandler({
|
|
61
48
|
errorSchema: cohereErrorDataSchema,
|
|
62
49
|
errorToMessage: (data) => data.message
|
|
63
50
|
});
|
|
64
51
|
|
|
65
52
|
// src/cohere-prepare-tools.ts
|
|
66
|
-
|
|
53
|
+
import {
|
|
54
|
+
UnsupportedFunctionalityError
|
|
55
|
+
} from "@ai-sdk/provider";
|
|
67
56
|
function prepareTools({
|
|
68
57
|
tools,
|
|
69
58
|
toolChoice
|
|
@@ -112,7 +101,7 @@ function prepareTools({
|
|
|
112
101
|
};
|
|
113
102
|
default: {
|
|
114
103
|
const _exhaustiveCheck = type;
|
|
115
|
-
throw new
|
|
104
|
+
throw new UnsupportedFunctionalityError({
|
|
116
105
|
functionality: `tool choice type: ${_exhaustiveCheck}`
|
|
117
106
|
});
|
|
118
107
|
}
|
|
@@ -156,7 +145,10 @@ function convertCohereUsage(tokens) {
|
|
|
156
145
|
}
|
|
157
146
|
|
|
158
147
|
// src/convert-to-cohere-chat-prompt.ts
|
|
159
|
-
|
|
148
|
+
import {
|
|
149
|
+
UnsupportedFunctionalityError as UnsupportedFunctionalityError2
|
|
150
|
+
} from "@ai-sdk/provider";
|
|
151
|
+
import { isProviderReference } from "@ai-sdk/provider-utils";
|
|
160
152
|
function convertToCohereChatPrompt(prompt) {
|
|
161
153
|
const messages = [];
|
|
162
154
|
const documents = [];
|
|
@@ -177,19 +169,24 @@ function convertToCohereChatPrompt(prompt) {
|
|
|
177
169
|
return part.text;
|
|
178
170
|
}
|
|
179
171
|
case "file": {
|
|
172
|
+
if (isProviderReference(part.data)) {
|
|
173
|
+
throw new UnsupportedFunctionalityError2({
|
|
174
|
+
functionality: "file parts with provider references"
|
|
175
|
+
});
|
|
176
|
+
}
|
|
180
177
|
let textContent;
|
|
181
178
|
if (typeof part.data === "string") {
|
|
182
179
|
textContent = part.data;
|
|
183
180
|
} else if (part.data instanceof Uint8Array) {
|
|
184
181
|
if (!(((_a = part.mediaType) == null ? void 0 : _a.startsWith("text/")) || part.mediaType === "application/json")) {
|
|
185
|
-
throw new
|
|
182
|
+
throw new UnsupportedFunctionalityError2({
|
|
186
183
|
functionality: `document media type: ${part.mediaType}`,
|
|
187
184
|
message: `Media type '${part.mediaType}' is not supported. Supported media types are: text/* and application/json.`
|
|
188
185
|
});
|
|
189
186
|
}
|
|
190
187
|
textContent = new TextDecoder().decode(part.data);
|
|
191
188
|
} else {
|
|
192
|
-
throw new
|
|
189
|
+
throw new UnsupportedFunctionalityError2({
|
|
193
190
|
functionality: "File URL data",
|
|
194
191
|
message: "URLs should be downloaded by the AI SDK and not reach this point. This indicates a configuration issue."
|
|
195
192
|
});
|
|
@@ -295,7 +292,7 @@ function mapCohereFinishReason(finishReason) {
|
|
|
295
292
|
// src/cohere-chat-language-model.ts
|
|
296
293
|
var CohereChatLanguageModel = class {
|
|
297
294
|
constructor(modelId, config) {
|
|
298
|
-
this.specificationVersion = "
|
|
295
|
+
this.specificationVersion = "v4";
|
|
299
296
|
this.supportedUrls = {
|
|
300
297
|
// No URLs are supported.
|
|
301
298
|
};
|
|
@@ -316,12 +313,14 @@ var CohereChatLanguageModel = class {
|
|
|
316
313
|
stopSequences,
|
|
317
314
|
responseFormat,
|
|
318
315
|
seed,
|
|
316
|
+
reasoning,
|
|
319
317
|
tools,
|
|
320
318
|
toolChoice,
|
|
321
319
|
providerOptions
|
|
322
320
|
}) {
|
|
323
|
-
var _a
|
|
324
|
-
const
|
|
321
|
+
var _a;
|
|
322
|
+
const warnings = [];
|
|
323
|
+
const cohereOptions = (_a = await parseProviderOptions({
|
|
325
324
|
provider: "cohere",
|
|
326
325
|
providerOptions,
|
|
327
326
|
schema: cohereLanguageModelOptions
|
|
@@ -336,6 +335,7 @@ var CohereChatLanguageModel = class {
|
|
|
336
335
|
toolChoice: cohereToolChoice,
|
|
337
336
|
toolWarnings
|
|
338
337
|
} = prepareTools({ tools, toolChoice });
|
|
338
|
+
warnings.push(...toolWarnings, ...promptWarnings);
|
|
339
339
|
return {
|
|
340
340
|
args: {
|
|
341
341
|
// model id:
|
|
@@ -358,15 +358,14 @@ var CohereChatLanguageModel = class {
|
|
|
358
358
|
tool_choice: cohereToolChoice,
|
|
359
359
|
// documents for RAG:
|
|
360
360
|
...cohereDocuments.length > 0 && { documents: cohereDocuments },
|
|
361
|
-
// reasoning
|
|
362
|
-
...
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
}
|
|
361
|
+
// reasoning:
|
|
362
|
+
...resolveCohereThinking({
|
|
363
|
+
reasoning,
|
|
364
|
+
cohereOptions,
|
|
365
|
+
warnings
|
|
366
|
+
})
|
|
368
367
|
},
|
|
369
|
-
warnings
|
|
368
|
+
warnings
|
|
370
369
|
};
|
|
371
370
|
}
|
|
372
371
|
async doGenerate(options) {
|
|
@@ -376,12 +375,12 @@ var CohereChatLanguageModel = class {
|
|
|
376
375
|
responseHeaders,
|
|
377
376
|
value: response,
|
|
378
377
|
rawValue: rawResponse
|
|
379
|
-
} = await
|
|
378
|
+
} = await postJsonToApi({
|
|
380
379
|
url: `${this.config.baseURL}/chat`,
|
|
381
|
-
headers:
|
|
380
|
+
headers: combineHeaders(this.config.headers(), options.headers),
|
|
382
381
|
body: args,
|
|
383
382
|
failedResponseHandler: cohereFailedResponseHandler,
|
|
384
|
-
successfulResponseHandler:
|
|
383
|
+
successfulResponseHandler: createJsonResponseHandler(
|
|
385
384
|
cohereChatResponseSchema
|
|
386
385
|
),
|
|
387
386
|
abortSignal: options.abortSignal,
|
|
@@ -445,12 +444,12 @@ var CohereChatLanguageModel = class {
|
|
|
445
444
|
}
|
|
446
445
|
async doStream(options) {
|
|
447
446
|
const { args, warnings } = await this.getArgs(options);
|
|
448
|
-
const { responseHeaders, value: response } = await
|
|
447
|
+
const { responseHeaders, value: response } = await postJsonToApi({
|
|
449
448
|
url: `${this.config.baseURL}/chat`,
|
|
450
|
-
headers:
|
|
449
|
+
headers: combineHeaders(this.config.headers(), options.headers),
|
|
451
450
|
body: { ...args, stream: true },
|
|
452
451
|
failedResponseHandler: cohereFailedResponseHandler,
|
|
453
|
-
successfulResponseHandler:
|
|
452
|
+
successfulResponseHandler: createEventSourceResponseHandler(
|
|
454
453
|
cohereChatChunkSchema
|
|
455
454
|
),
|
|
456
455
|
abortSignal: options.abortSignal,
|
|
@@ -617,145 +616,176 @@ var CohereChatLanguageModel = class {
|
|
|
617
616
|
};
|
|
618
617
|
}
|
|
619
618
|
};
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
619
|
+
function resolveCohereThinking({
|
|
620
|
+
reasoning,
|
|
621
|
+
cohereOptions,
|
|
622
|
+
warnings
|
|
623
|
+
}) {
|
|
624
|
+
var _a;
|
|
625
|
+
if (cohereOptions.thinking) {
|
|
626
|
+
return {
|
|
627
|
+
thinking: {
|
|
628
|
+
type: (_a = cohereOptions.thinking.type) != null ? _a : "enabled",
|
|
629
|
+
token_budget: cohereOptions.thinking.tokenBudget
|
|
630
|
+
}
|
|
631
|
+
};
|
|
632
|
+
}
|
|
633
|
+
if (!isCustomReasoning(reasoning)) {
|
|
634
|
+
return {};
|
|
635
|
+
}
|
|
636
|
+
if (reasoning === "none") {
|
|
637
|
+
return { thinking: { type: "disabled" } };
|
|
638
|
+
}
|
|
639
|
+
const tokenBudget = mapReasoningToProviderBudget({
|
|
640
|
+
reasoning,
|
|
641
|
+
maxOutputTokens: 32768,
|
|
642
|
+
maxReasoningBudget: 32768,
|
|
643
|
+
warnings
|
|
644
|
+
});
|
|
645
|
+
if (tokenBudget == null) {
|
|
646
|
+
return {};
|
|
647
|
+
}
|
|
648
|
+
return { thinking: { type: "enabled", token_budget: tokenBudget } };
|
|
649
|
+
}
|
|
650
|
+
var cohereChatResponseSchema = z3.object({
|
|
651
|
+
generation_id: z3.string().nullish(),
|
|
652
|
+
message: z3.object({
|
|
653
|
+
role: z3.string(),
|
|
654
|
+
content: z3.array(
|
|
655
|
+
z3.union([
|
|
656
|
+
z3.object({
|
|
657
|
+
type: z3.literal("text"),
|
|
658
|
+
text: z3.string()
|
|
629
659
|
}),
|
|
630
|
-
|
|
631
|
-
type:
|
|
632
|
-
thinking:
|
|
660
|
+
z3.object({
|
|
661
|
+
type: z3.literal("thinking"),
|
|
662
|
+
thinking: z3.string()
|
|
633
663
|
})
|
|
634
664
|
])
|
|
635
665
|
).nullish(),
|
|
636
|
-
tool_plan:
|
|
637
|
-
tool_calls:
|
|
638
|
-
|
|
639
|
-
id:
|
|
640
|
-
type:
|
|
641
|
-
function:
|
|
642
|
-
name:
|
|
643
|
-
arguments:
|
|
666
|
+
tool_plan: z3.string().nullish(),
|
|
667
|
+
tool_calls: z3.array(
|
|
668
|
+
z3.object({
|
|
669
|
+
id: z3.string(),
|
|
670
|
+
type: z3.literal("function"),
|
|
671
|
+
function: z3.object({
|
|
672
|
+
name: z3.string(),
|
|
673
|
+
arguments: z3.string()
|
|
644
674
|
})
|
|
645
675
|
})
|
|
646
676
|
).nullish(),
|
|
647
|
-
citations:
|
|
648
|
-
|
|
649
|
-
start:
|
|
650
|
-
end:
|
|
651
|
-
text:
|
|
652
|
-
sources:
|
|
653
|
-
|
|
654
|
-
type:
|
|
655
|
-
id:
|
|
656
|
-
document:
|
|
657
|
-
id:
|
|
658
|
-
text:
|
|
659
|
-
title:
|
|
677
|
+
citations: z3.array(
|
|
678
|
+
z3.object({
|
|
679
|
+
start: z3.number(),
|
|
680
|
+
end: z3.number(),
|
|
681
|
+
text: z3.string(),
|
|
682
|
+
sources: z3.array(
|
|
683
|
+
z3.object({
|
|
684
|
+
type: z3.string().optional(),
|
|
685
|
+
id: z3.string().optional(),
|
|
686
|
+
document: z3.object({
|
|
687
|
+
id: z3.string().optional(),
|
|
688
|
+
text: z3.string(),
|
|
689
|
+
title: z3.string()
|
|
660
690
|
})
|
|
661
691
|
})
|
|
662
692
|
),
|
|
663
|
-
type:
|
|
693
|
+
type: z3.string().optional()
|
|
664
694
|
})
|
|
665
695
|
).nullish()
|
|
666
696
|
}),
|
|
667
|
-
finish_reason:
|
|
668
|
-
usage:
|
|
669
|
-
billed_units:
|
|
670
|
-
input_tokens:
|
|
671
|
-
output_tokens:
|
|
697
|
+
finish_reason: z3.string(),
|
|
698
|
+
usage: z3.object({
|
|
699
|
+
billed_units: z3.object({
|
|
700
|
+
input_tokens: z3.number(),
|
|
701
|
+
output_tokens: z3.number()
|
|
672
702
|
}),
|
|
673
|
-
tokens:
|
|
674
|
-
input_tokens:
|
|
675
|
-
output_tokens:
|
|
703
|
+
tokens: z3.object({
|
|
704
|
+
input_tokens: z3.number(),
|
|
705
|
+
output_tokens: z3.number()
|
|
676
706
|
})
|
|
677
707
|
})
|
|
678
708
|
});
|
|
679
|
-
var cohereChatChunkSchema =
|
|
680
|
-
|
|
681
|
-
type:
|
|
709
|
+
var cohereChatChunkSchema = z3.discriminatedUnion("type", [
|
|
710
|
+
z3.object({
|
|
711
|
+
type: z3.literal("citation-start")
|
|
682
712
|
}),
|
|
683
|
-
|
|
684
|
-
type:
|
|
713
|
+
z3.object({
|
|
714
|
+
type: z3.literal("citation-end")
|
|
685
715
|
}),
|
|
686
|
-
|
|
687
|
-
type:
|
|
688
|
-
index:
|
|
689
|
-
delta:
|
|
690
|
-
message:
|
|
691
|
-
content:
|
|
692
|
-
|
|
693
|
-
type:
|
|
694
|
-
text:
|
|
716
|
+
z3.object({
|
|
717
|
+
type: z3.literal("content-start"),
|
|
718
|
+
index: z3.number(),
|
|
719
|
+
delta: z3.object({
|
|
720
|
+
message: z3.object({
|
|
721
|
+
content: z3.union([
|
|
722
|
+
z3.object({
|
|
723
|
+
type: z3.literal("text"),
|
|
724
|
+
text: z3.string()
|
|
695
725
|
}),
|
|
696
|
-
|
|
697
|
-
type:
|
|
698
|
-
thinking:
|
|
726
|
+
z3.object({
|
|
727
|
+
type: z3.literal("thinking"),
|
|
728
|
+
thinking: z3.string()
|
|
699
729
|
})
|
|
700
730
|
])
|
|
701
731
|
})
|
|
702
732
|
})
|
|
703
733
|
}),
|
|
704
|
-
|
|
705
|
-
type:
|
|
706
|
-
index:
|
|
707
|
-
delta:
|
|
708
|
-
message:
|
|
709
|
-
content:
|
|
710
|
-
|
|
711
|
-
text:
|
|
734
|
+
z3.object({
|
|
735
|
+
type: z3.literal("content-delta"),
|
|
736
|
+
index: z3.number(),
|
|
737
|
+
delta: z3.object({
|
|
738
|
+
message: z3.object({
|
|
739
|
+
content: z3.union([
|
|
740
|
+
z3.object({
|
|
741
|
+
text: z3.string()
|
|
712
742
|
}),
|
|
713
|
-
|
|
714
|
-
thinking:
|
|
743
|
+
z3.object({
|
|
744
|
+
thinking: z3.string()
|
|
715
745
|
})
|
|
716
746
|
])
|
|
717
747
|
})
|
|
718
748
|
})
|
|
719
749
|
}),
|
|
720
|
-
|
|
721
|
-
type:
|
|
722
|
-
index:
|
|
750
|
+
z3.object({
|
|
751
|
+
type: z3.literal("content-end"),
|
|
752
|
+
index: z3.number()
|
|
723
753
|
}),
|
|
724
|
-
|
|
725
|
-
type:
|
|
726
|
-
id:
|
|
754
|
+
z3.object({
|
|
755
|
+
type: z3.literal("message-start"),
|
|
756
|
+
id: z3.string().nullish()
|
|
727
757
|
}),
|
|
728
|
-
|
|
729
|
-
type:
|
|
730
|
-
delta:
|
|
731
|
-
finish_reason:
|
|
732
|
-
usage:
|
|
733
|
-
tokens:
|
|
734
|
-
input_tokens:
|
|
735
|
-
output_tokens:
|
|
758
|
+
z3.object({
|
|
759
|
+
type: z3.literal("message-end"),
|
|
760
|
+
delta: z3.object({
|
|
761
|
+
finish_reason: z3.string(),
|
|
762
|
+
usage: z3.object({
|
|
763
|
+
tokens: z3.object({
|
|
764
|
+
input_tokens: z3.number(),
|
|
765
|
+
output_tokens: z3.number()
|
|
736
766
|
})
|
|
737
767
|
})
|
|
738
768
|
})
|
|
739
769
|
}),
|
|
740
770
|
// https://docs.cohere.com/v2/docs/streaming#tool-use-stream-events-for-tool-calling
|
|
741
|
-
|
|
742
|
-
type:
|
|
743
|
-
delta:
|
|
744
|
-
message:
|
|
745
|
-
tool_plan:
|
|
771
|
+
z3.object({
|
|
772
|
+
type: z3.literal("tool-plan-delta"),
|
|
773
|
+
delta: z3.object({
|
|
774
|
+
message: z3.object({
|
|
775
|
+
tool_plan: z3.string()
|
|
746
776
|
})
|
|
747
777
|
})
|
|
748
778
|
}),
|
|
749
|
-
|
|
750
|
-
type:
|
|
751
|
-
delta:
|
|
752
|
-
message:
|
|
753
|
-
tool_calls:
|
|
754
|
-
id:
|
|
755
|
-
type:
|
|
756
|
-
function:
|
|
757
|
-
name:
|
|
758
|
-
arguments:
|
|
779
|
+
z3.object({
|
|
780
|
+
type: z3.literal("tool-call-start"),
|
|
781
|
+
delta: z3.object({
|
|
782
|
+
message: z3.object({
|
|
783
|
+
tool_calls: z3.object({
|
|
784
|
+
id: z3.string(),
|
|
785
|
+
type: z3.literal("function"),
|
|
786
|
+
function: z3.object({
|
|
787
|
+
name: z3.string(),
|
|
788
|
+
arguments: z3.string()
|
|
759
789
|
})
|
|
760
790
|
})
|
|
761
791
|
})
|
|
@@ -764,31 +794,38 @@ var cohereChatChunkSchema = import_v43.z.discriminatedUnion("type", [
|
|
|
764
794
|
// A single tool call's `arguments` stream in chunks and must be accumulated
|
|
765
795
|
// in a string and so the full tool object info can only be parsed once we see
|
|
766
796
|
// `tool-call-end`.
|
|
767
|
-
|
|
768
|
-
type:
|
|
769
|
-
delta:
|
|
770
|
-
message:
|
|
771
|
-
tool_calls:
|
|
772
|
-
function:
|
|
773
|
-
arguments:
|
|
797
|
+
z3.object({
|
|
798
|
+
type: z3.literal("tool-call-delta"),
|
|
799
|
+
delta: z3.object({
|
|
800
|
+
message: z3.object({
|
|
801
|
+
tool_calls: z3.object({
|
|
802
|
+
function: z3.object({
|
|
803
|
+
arguments: z3.string()
|
|
774
804
|
})
|
|
775
805
|
})
|
|
776
806
|
})
|
|
777
807
|
})
|
|
778
808
|
}),
|
|
779
|
-
|
|
780
|
-
type:
|
|
809
|
+
z3.object({
|
|
810
|
+
type: z3.literal("tool-call-end")
|
|
781
811
|
})
|
|
782
812
|
]);
|
|
783
813
|
|
|
784
814
|
// src/cohere-embedding-model.ts
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
815
|
+
import {
|
|
816
|
+
TooManyEmbeddingValuesForCallError
|
|
817
|
+
} from "@ai-sdk/provider";
|
|
818
|
+
import {
|
|
819
|
+
combineHeaders as combineHeaders2,
|
|
820
|
+
createJsonResponseHandler as createJsonResponseHandler2,
|
|
821
|
+
parseProviderOptions as parseProviderOptions2,
|
|
822
|
+
postJsonToApi as postJsonToApi2
|
|
823
|
+
} from "@ai-sdk/provider-utils";
|
|
824
|
+
import { z as z5 } from "zod/v4";
|
|
788
825
|
|
|
789
826
|
// src/cohere-embedding-options.ts
|
|
790
|
-
|
|
791
|
-
var cohereEmbeddingModelOptions =
|
|
827
|
+
import { z as z4 } from "zod/v4";
|
|
828
|
+
var cohereEmbeddingModelOptions = z4.object({
|
|
792
829
|
/**
|
|
793
830
|
* Specifies the type of input passed to the model. Default is `search_query`.
|
|
794
831
|
*
|
|
@@ -797,7 +834,7 @@ var cohereEmbeddingModelOptions = import_v44.z.object({
|
|
|
797
834
|
* - "classification": Used for embeddings passed through a text classifier.
|
|
798
835
|
* - "clustering": Used for embeddings run through a clustering algorithm.
|
|
799
836
|
*/
|
|
800
|
-
inputType:
|
|
837
|
+
inputType: z4.enum(["search_document", "search_query", "classification", "clustering"]).optional(),
|
|
801
838
|
/**
|
|
802
839
|
* Specifies how the API will handle inputs longer than the maximum token length.
|
|
803
840
|
* Default is `END`.
|
|
@@ -806,7 +843,7 @@ var cohereEmbeddingModelOptions = import_v44.z.object({
|
|
|
806
843
|
* - "START": Will discard the start of the input until the remaining input is exactly the maximum input token length for the model.
|
|
807
844
|
* - "END": Will discard the end of the input until the remaining input is exactly the maximum input token length for the model.
|
|
808
845
|
*/
|
|
809
|
-
truncate:
|
|
846
|
+
truncate: z4.enum(["NONE", "START", "END"]).optional(),
|
|
810
847
|
/**
|
|
811
848
|
* The number of dimensions of the output embedding.
|
|
812
849
|
* Only available for `embed-v4.0` and newer models.
|
|
@@ -814,13 +851,13 @@ var cohereEmbeddingModelOptions = import_v44.z.object({
|
|
|
814
851
|
* Possible values are `256`, `512`, `1024`, and `1536`.
|
|
815
852
|
* The default is `1536`.
|
|
816
853
|
*/
|
|
817
|
-
outputDimension:
|
|
854
|
+
outputDimension: z4.union([z4.literal(256), z4.literal(512), z4.literal(1024), z4.literal(1536)]).optional()
|
|
818
855
|
});
|
|
819
856
|
|
|
820
857
|
// src/cohere-embedding-model.ts
|
|
821
858
|
var CohereEmbeddingModel = class {
|
|
822
859
|
constructor(modelId, config) {
|
|
823
|
-
this.specificationVersion = "
|
|
860
|
+
this.specificationVersion = "v4";
|
|
824
861
|
this.maxEmbeddingsPerCall = 96;
|
|
825
862
|
this.supportsParallelCalls = true;
|
|
826
863
|
this.modelId = modelId;
|
|
@@ -836,13 +873,13 @@ var CohereEmbeddingModel = class {
|
|
|
836
873
|
providerOptions
|
|
837
874
|
}) {
|
|
838
875
|
var _a;
|
|
839
|
-
const embeddingOptions = await (
|
|
876
|
+
const embeddingOptions = await parseProviderOptions2({
|
|
840
877
|
provider: "cohere",
|
|
841
878
|
providerOptions,
|
|
842
879
|
schema: cohereEmbeddingModelOptions
|
|
843
880
|
});
|
|
844
881
|
if (values.length > this.maxEmbeddingsPerCall) {
|
|
845
|
-
throw new
|
|
882
|
+
throw new TooManyEmbeddingValuesForCallError({
|
|
846
883
|
provider: this.provider,
|
|
847
884
|
modelId: this.modelId,
|
|
848
885
|
maxEmbeddingsPerCall: this.maxEmbeddingsPerCall,
|
|
@@ -853,9 +890,9 @@ var CohereEmbeddingModel = class {
|
|
|
853
890
|
responseHeaders,
|
|
854
891
|
value: response,
|
|
855
892
|
rawValue
|
|
856
|
-
} = await (
|
|
893
|
+
} = await postJsonToApi2({
|
|
857
894
|
url: `${this.config.baseURL}/embed`,
|
|
858
|
-
headers: (
|
|
895
|
+
headers: combineHeaders2(this.config.headers(), headers),
|
|
859
896
|
body: {
|
|
860
897
|
model: this.modelId,
|
|
861
898
|
// The AI SDK only supports 'float' embeddings. Note that the Cohere API
|
|
@@ -868,7 +905,7 @@ var CohereEmbeddingModel = class {
|
|
|
868
905
|
output_dimension: embeddingOptions == null ? void 0 : embeddingOptions.outputDimension
|
|
869
906
|
},
|
|
870
907
|
failedResponseHandler: cohereFailedResponseHandler,
|
|
871
|
-
successfulResponseHandler: (
|
|
908
|
+
successfulResponseHandler: createJsonResponseHandler2(
|
|
872
909
|
cohereTextEmbeddingResponseSchema
|
|
873
910
|
),
|
|
874
911
|
abortSignal,
|
|
@@ -882,46 +919,51 @@ var CohereEmbeddingModel = class {
|
|
|
882
919
|
};
|
|
883
920
|
}
|
|
884
921
|
};
|
|
885
|
-
var cohereTextEmbeddingResponseSchema =
|
|
886
|
-
embeddings:
|
|
887
|
-
float:
|
|
922
|
+
var cohereTextEmbeddingResponseSchema = z5.object({
|
|
923
|
+
embeddings: z5.object({
|
|
924
|
+
float: z5.array(z5.array(z5.number()))
|
|
888
925
|
}),
|
|
889
|
-
meta:
|
|
890
|
-
billed_units:
|
|
891
|
-
input_tokens:
|
|
926
|
+
meta: z5.object({
|
|
927
|
+
billed_units: z5.object({
|
|
928
|
+
input_tokens: z5.number()
|
|
892
929
|
})
|
|
893
930
|
})
|
|
894
931
|
});
|
|
895
932
|
|
|
896
933
|
// src/reranking/cohere-reranking-model.ts
|
|
897
|
-
|
|
934
|
+
import {
|
|
935
|
+
combineHeaders as combineHeaders3,
|
|
936
|
+
createJsonResponseHandler as createJsonResponseHandler3,
|
|
937
|
+
parseProviderOptions as parseProviderOptions3,
|
|
938
|
+
postJsonToApi as postJsonToApi3
|
|
939
|
+
} from "@ai-sdk/provider-utils";
|
|
898
940
|
|
|
899
941
|
// src/reranking/cohere-reranking-api.ts
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
var cohereRerankingResponseSchema =
|
|
903
|
-
() =>
|
|
904
|
-
|
|
905
|
-
id:
|
|
906
|
-
results:
|
|
907
|
-
|
|
908
|
-
index:
|
|
909
|
-
relevance_score:
|
|
942
|
+
import { lazySchema, zodSchema } from "@ai-sdk/provider-utils";
|
|
943
|
+
import { z as z6 } from "zod/v4";
|
|
944
|
+
var cohereRerankingResponseSchema = lazySchema(
|
|
945
|
+
() => zodSchema(
|
|
946
|
+
z6.object({
|
|
947
|
+
id: z6.string().nullish(),
|
|
948
|
+
results: z6.array(
|
|
949
|
+
z6.object({
|
|
950
|
+
index: z6.number(),
|
|
951
|
+
relevance_score: z6.number()
|
|
910
952
|
})
|
|
911
953
|
),
|
|
912
|
-
meta:
|
|
954
|
+
meta: z6.any()
|
|
913
955
|
})
|
|
914
956
|
)
|
|
915
957
|
);
|
|
916
958
|
|
|
917
959
|
// src/reranking/cohere-reranking-options.ts
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
var cohereRerankingModelOptionsSchema = (
|
|
921
|
-
() => (
|
|
922
|
-
|
|
923
|
-
maxTokensPerDoc:
|
|
924
|
-
priority:
|
|
960
|
+
import { lazySchema as lazySchema2, zodSchema as zodSchema2 } from "@ai-sdk/provider-utils";
|
|
961
|
+
import { z as z7 } from "zod/v4";
|
|
962
|
+
var cohereRerankingModelOptionsSchema = lazySchema2(
|
|
963
|
+
() => zodSchema2(
|
|
964
|
+
z7.object({
|
|
965
|
+
maxTokensPerDoc: z7.number().optional(),
|
|
966
|
+
priority: z7.number().optional()
|
|
925
967
|
})
|
|
926
968
|
)
|
|
927
969
|
);
|
|
@@ -929,7 +971,7 @@ var cohereRerankingModelOptionsSchema = (0, import_provider_utils5.lazySchema)(
|
|
|
929
971
|
// src/reranking/cohere-reranking-model.ts
|
|
930
972
|
var CohereRerankingModel = class {
|
|
931
973
|
constructor(modelId, config) {
|
|
932
|
-
this.specificationVersion = "
|
|
974
|
+
this.specificationVersion = "v4";
|
|
933
975
|
this.modelId = modelId;
|
|
934
976
|
this.config = config;
|
|
935
977
|
}
|
|
@@ -946,7 +988,7 @@ var CohereRerankingModel = class {
|
|
|
946
988
|
providerOptions
|
|
947
989
|
}) {
|
|
948
990
|
var _a;
|
|
949
|
-
const rerankingOptions = await (
|
|
991
|
+
const rerankingOptions = await parseProviderOptions3({
|
|
950
992
|
provider: "cohere",
|
|
951
993
|
providerOptions,
|
|
952
994
|
schema: cohereRerankingModelOptionsSchema
|
|
@@ -963,9 +1005,9 @@ var CohereRerankingModel = class {
|
|
|
963
1005
|
responseHeaders,
|
|
964
1006
|
value: response,
|
|
965
1007
|
rawValue
|
|
966
|
-
} = await (
|
|
1008
|
+
} = await postJsonToApi3({
|
|
967
1009
|
url: `${this.config.baseURL}/rerank`,
|
|
968
|
-
headers: (
|
|
1010
|
+
headers: combineHeaders3(this.config.headers(), headers),
|
|
969
1011
|
body: {
|
|
970
1012
|
model: this.modelId,
|
|
971
1013
|
query,
|
|
@@ -975,7 +1017,7 @@ var CohereRerankingModel = class {
|
|
|
975
1017
|
priority: rerankingOptions == null ? void 0 : rerankingOptions.priority
|
|
976
1018
|
},
|
|
977
1019
|
failedResponseHandler: cohereFailedResponseHandler,
|
|
978
|
-
successfulResponseHandler: (
|
|
1020
|
+
successfulResponseHandler: createJsonResponseHandler3(
|
|
979
1021
|
cohereRerankingResponseSchema
|
|
980
1022
|
),
|
|
981
1023
|
abortSignal,
|
|
@@ -997,15 +1039,15 @@ var CohereRerankingModel = class {
|
|
|
997
1039
|
};
|
|
998
1040
|
|
|
999
1041
|
// src/version.ts
|
|
1000
|
-
var VERSION = true ? "4.0.0-beta.
|
|
1042
|
+
var VERSION = true ? "4.0.0-beta.21" : "0.0.0-test";
|
|
1001
1043
|
|
|
1002
1044
|
// src/cohere-provider.ts
|
|
1003
1045
|
function createCohere(options = {}) {
|
|
1004
1046
|
var _a;
|
|
1005
|
-
const baseURL = (_a =
|
|
1006
|
-
const getHeaders = () =>
|
|
1047
|
+
const baseURL = (_a = withoutTrailingSlash(options.baseURL)) != null ? _a : "https://api.cohere.com/v2";
|
|
1048
|
+
const getHeaders = () => withUserAgentSuffix(
|
|
1007
1049
|
{
|
|
1008
|
-
Authorization: `Bearer ${
|
|
1050
|
+
Authorization: `Bearer ${loadApiKey({
|
|
1009
1051
|
apiKey: options.apiKey,
|
|
1010
1052
|
environmentVariableName: "COHERE_API_KEY",
|
|
1011
1053
|
description: "Cohere"
|
|
@@ -1021,7 +1063,7 @@ function createCohere(options = {}) {
|
|
|
1021
1063
|
baseURL,
|
|
1022
1064
|
headers: getHeaders,
|
|
1023
1065
|
fetch: options.fetch,
|
|
1024
|
-
generateId: (_a2 = options.generateId) != null ? _a2 :
|
|
1066
|
+
generateId: (_a2 = options.generateId) != null ? _a2 : generateId
|
|
1025
1067
|
});
|
|
1026
1068
|
};
|
|
1027
1069
|
const createEmbeddingModel = (modelId) => new CohereEmbeddingModel(modelId, {
|
|
@@ -1044,7 +1086,7 @@ function createCohere(options = {}) {
|
|
|
1044
1086
|
}
|
|
1045
1087
|
return createChatModel(modelId);
|
|
1046
1088
|
};
|
|
1047
|
-
provider.specificationVersion = "
|
|
1089
|
+
provider.specificationVersion = "v4";
|
|
1048
1090
|
provider.languageModel = createChatModel;
|
|
1049
1091
|
provider.embedding = createEmbeddingModel;
|
|
1050
1092
|
provider.embeddingModel = createEmbeddingModel;
|
|
@@ -1053,15 +1095,14 @@ function createCohere(options = {}) {
|
|
|
1053
1095
|
provider.reranking = createRerankingModel;
|
|
1054
1096
|
provider.rerankingModel = createRerankingModel;
|
|
1055
1097
|
provider.imageModel = (modelId) => {
|
|
1056
|
-
throw new
|
|
1098
|
+
throw new NoSuchModelError({ modelId, modelType: "imageModel" });
|
|
1057
1099
|
};
|
|
1058
1100
|
return provider;
|
|
1059
1101
|
}
|
|
1060
1102
|
var cohere = createCohere();
|
|
1061
|
-
|
|
1062
|
-
0 && (module.exports = {
|
|
1103
|
+
export {
|
|
1063
1104
|
VERSION,
|
|
1064
1105
|
cohere,
|
|
1065
1106
|
createCohere
|
|
1066
|
-
}
|
|
1107
|
+
};
|
|
1067
1108
|
//# sourceMappingURL=index.js.map
|