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