@ai-sdk/cohere 4.0.0-beta.3 → 4.0.0-beta.31
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 +240 -4
- package/README.md +2 -0
- package/dist/index.d.ts +10 -10
- package/dist/index.js +319 -252
- package/dist/index.js.map +1 -1
- package/package.json +11 -11
- package/src/cohere-chat-language-model.ts +100 -33
- package/src/cohere-embedding-model.ts +26 -9
- package/src/cohere-prepare-tools.ts +7 -7
- package/src/cohere-provider.ts +18 -19
- package/src/convert-cohere-usage.ts +2 -2
- package/src/convert-to-cohere-chat-prompt.ts +31 -30
- package/src/map-cohere-finish-reason.ts +2 -2
- package/src/reranking/cohere-reranking-model.ts +9 -10
- package/src/reranking/cohere-reranking-options.ts +5 -1
- 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,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,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,15 +291,24 @@ 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
|
-
this.specificationVersion = "
|
|
296
|
+
this.specificationVersion = "v4";
|
|
299
297
|
this.supportedUrls = {
|
|
300
298
|
// No URLs are supported.
|
|
301
299
|
};
|
|
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
|
}
|
|
@@ -316,12 +323,14 @@ var CohereChatLanguageModel = class {
|
|
|
316
323
|
stopSequences,
|
|
317
324
|
responseFormat,
|
|
318
325
|
seed,
|
|
326
|
+
reasoning,
|
|
319
327
|
tools,
|
|
320
328
|
toolChoice,
|
|
321
329
|
providerOptions
|
|
322
330
|
}) {
|
|
323
|
-
var _a
|
|
324
|
-
const
|
|
331
|
+
var _a;
|
|
332
|
+
const warnings = [];
|
|
333
|
+
const cohereOptions = (_a = await parseProviderOptions({
|
|
325
334
|
provider: "cohere",
|
|
326
335
|
providerOptions,
|
|
327
336
|
schema: cohereLanguageModelOptions
|
|
@@ -336,6 +345,7 @@ var CohereChatLanguageModel = class {
|
|
|
336
345
|
toolChoice: cohereToolChoice,
|
|
337
346
|
toolWarnings
|
|
338
347
|
} = prepareTools({ tools, toolChoice });
|
|
348
|
+
warnings.push(...toolWarnings, ...promptWarnings);
|
|
339
349
|
return {
|
|
340
350
|
args: {
|
|
341
351
|
// model id:
|
|
@@ -358,37 +368,36 @@ var CohereChatLanguageModel = class {
|
|
|
358
368
|
tool_choice: cohereToolChoice,
|
|
359
369
|
// documents for RAG:
|
|
360
370
|
...cohereDocuments.length > 0 && { documents: cohereDocuments },
|
|
361
|
-
// reasoning
|
|
362
|
-
...
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
}
|
|
371
|
+
// reasoning:
|
|
372
|
+
...resolveCohereThinking({
|
|
373
|
+
reasoning,
|
|
374
|
+
cohereOptions,
|
|
375
|
+
warnings
|
|
376
|
+
})
|
|
368
377
|
},
|
|
369
|
-
warnings
|
|
378
|
+
warnings
|
|
370
379
|
};
|
|
371
380
|
}
|
|
372
381
|
async doGenerate(options) {
|
|
373
|
-
var _a, _b, _c, _d, _e, _f, _g;
|
|
382
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i;
|
|
374
383
|
const { args, warnings } = await this.getArgs(options);
|
|
375
384
|
const {
|
|
376
385
|
responseHeaders,
|
|
377
386
|
value: response,
|
|
378
387
|
rawValue: rawResponse
|
|
379
|
-
} = await
|
|
388
|
+
} = await postJsonToApi({
|
|
380
389
|
url: `${this.config.baseURL}/chat`,
|
|
381
|
-
headers: (
|
|
390
|
+
headers: combineHeaders((_b = (_a = this.config).headers) == null ? void 0 : _b.call(_a), options.headers),
|
|
382
391
|
body: args,
|
|
383
392
|
failedResponseHandler: cohereFailedResponseHandler,
|
|
384
|
-
successfulResponseHandler:
|
|
393
|
+
successfulResponseHandler: createJsonResponseHandler(
|
|
385
394
|
cohereChatResponseSchema
|
|
386
395
|
),
|
|
387
396
|
abortSignal: options.abortSignal,
|
|
388
397
|
fetch: this.config.fetch
|
|
389
398
|
});
|
|
390
399
|
const content = [];
|
|
391
|
-
for (const item of (
|
|
400
|
+
for (const item of (_c = response.message.content) != null ? _c : []) {
|
|
392
401
|
if (item.type === "text" && item.text.length > 0) {
|
|
393
402
|
content.push({ type: "text", text: item.text });
|
|
394
403
|
continue;
|
|
@@ -398,13 +407,13 @@ var CohereChatLanguageModel = class {
|
|
|
398
407
|
continue;
|
|
399
408
|
}
|
|
400
409
|
}
|
|
401
|
-
for (const citation of (
|
|
410
|
+
for (const citation of (_d = response.message.citations) != null ? _d : []) {
|
|
402
411
|
content.push({
|
|
403
412
|
type: "source",
|
|
404
413
|
sourceType: "document",
|
|
405
414
|
id: this.config.generateId(),
|
|
406
415
|
mediaType: "text/plain",
|
|
407
|
-
title: ((
|
|
416
|
+
title: ((_f = (_e = citation.sources[0]) == null ? void 0 : _e.document) == null ? void 0 : _f.title) || "Document",
|
|
408
417
|
providerMetadata: {
|
|
409
418
|
cohere: {
|
|
410
419
|
start: citation.start,
|
|
@@ -416,7 +425,7 @@ var CohereChatLanguageModel = class {
|
|
|
416
425
|
}
|
|
417
426
|
});
|
|
418
427
|
}
|
|
419
|
-
for (const toolCall of (
|
|
428
|
+
for (const toolCall of (_g = response.message.tool_calls) != null ? _g : []) {
|
|
420
429
|
content.push({
|
|
421
430
|
type: "tool-call",
|
|
422
431
|
toolCallId: toolCall.id,
|
|
@@ -430,13 +439,13 @@ var CohereChatLanguageModel = class {
|
|
|
430
439
|
content,
|
|
431
440
|
finishReason: {
|
|
432
441
|
unified: mapCohereFinishReason(response.finish_reason),
|
|
433
|
-
raw: (
|
|
442
|
+
raw: (_h = response.finish_reason) != null ? _h : void 0
|
|
434
443
|
},
|
|
435
444
|
usage: convertCohereUsage(response.usage.tokens),
|
|
436
445
|
request: { body: args },
|
|
437
446
|
response: {
|
|
438
447
|
// TODO timestamp, model id
|
|
439
|
-
id: (
|
|
448
|
+
id: (_i = response.generation_id) != null ? _i : void 0,
|
|
440
449
|
headers: responseHeaders,
|
|
441
450
|
body: rawResponse
|
|
442
451
|
},
|
|
@@ -444,13 +453,14 @@ var CohereChatLanguageModel = class {
|
|
|
444
453
|
};
|
|
445
454
|
}
|
|
446
455
|
async doStream(options) {
|
|
456
|
+
var _a, _b;
|
|
447
457
|
const { args, warnings } = await this.getArgs(options);
|
|
448
|
-
const { responseHeaders, value: response } = await
|
|
458
|
+
const { responseHeaders, value: response } = await postJsonToApi({
|
|
449
459
|
url: `${this.config.baseURL}/chat`,
|
|
450
|
-
headers: (
|
|
460
|
+
headers: combineHeaders((_b = (_a = this.config).headers) == null ? void 0 : _b.call(_a), options.headers),
|
|
451
461
|
body: { ...args, stream: true },
|
|
452
462
|
failedResponseHandler: cohereFailedResponseHandler,
|
|
453
|
-
successfulResponseHandler:
|
|
463
|
+
successfulResponseHandler: createEventSourceResponseHandler(
|
|
454
464
|
cohereChatChunkSchema
|
|
455
465
|
),
|
|
456
466
|
abortSignal: options.abortSignal,
|
|
@@ -470,7 +480,7 @@ var CohereChatLanguageModel = class {
|
|
|
470
480
|
controller.enqueue({ type: "stream-start", warnings });
|
|
471
481
|
},
|
|
472
482
|
transform(chunk, controller) {
|
|
473
|
-
var
|
|
483
|
+
var _a2, _b2;
|
|
474
484
|
if (options.includeRawChunks) {
|
|
475
485
|
controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
|
|
476
486
|
}
|
|
@@ -575,7 +585,7 @@ var CohereChatLanguageModel = class {
|
|
|
575
585
|
toolCallId: pendingToolCall.id,
|
|
576
586
|
toolName: pendingToolCall.name,
|
|
577
587
|
input: JSON.stringify(
|
|
578
|
-
JSON.parse(((
|
|
588
|
+
JSON.parse(((_a2 = pendingToolCall.arguments) == null ? void 0 : _a2.trim()) || "{}")
|
|
579
589
|
)
|
|
580
590
|
});
|
|
581
591
|
pendingToolCall.hasFinished = true;
|
|
@@ -586,7 +596,7 @@ var CohereChatLanguageModel = class {
|
|
|
586
596
|
case "message-start": {
|
|
587
597
|
controller.enqueue({
|
|
588
598
|
type: "response-metadata",
|
|
589
|
-
id: (
|
|
599
|
+
id: (_b2 = value.id) != null ? _b2 : void 0
|
|
590
600
|
});
|
|
591
601
|
return;
|
|
592
602
|
}
|
|
@@ -617,145 +627,176 @@ var CohereChatLanguageModel = class {
|
|
|
617
627
|
};
|
|
618
628
|
}
|
|
619
629
|
};
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
630
|
+
function resolveCohereThinking({
|
|
631
|
+
reasoning,
|
|
632
|
+
cohereOptions,
|
|
633
|
+
warnings
|
|
634
|
+
}) {
|
|
635
|
+
var _a;
|
|
636
|
+
if (cohereOptions.thinking) {
|
|
637
|
+
return {
|
|
638
|
+
thinking: {
|
|
639
|
+
type: (_a = cohereOptions.thinking.type) != null ? _a : "enabled",
|
|
640
|
+
token_budget: cohereOptions.thinking.tokenBudget
|
|
641
|
+
}
|
|
642
|
+
};
|
|
643
|
+
}
|
|
644
|
+
if (!isCustomReasoning(reasoning)) {
|
|
645
|
+
return {};
|
|
646
|
+
}
|
|
647
|
+
if (reasoning === "none") {
|
|
648
|
+
return { thinking: { type: "disabled" } };
|
|
649
|
+
}
|
|
650
|
+
const tokenBudget = mapReasoningToProviderBudget({
|
|
651
|
+
reasoning,
|
|
652
|
+
maxOutputTokens: 32768,
|
|
653
|
+
maxReasoningBudget: 32768,
|
|
654
|
+
warnings
|
|
655
|
+
});
|
|
656
|
+
if (tokenBudget == null) {
|
|
657
|
+
return {};
|
|
658
|
+
}
|
|
659
|
+
return { thinking: { type: "enabled", token_budget: tokenBudget } };
|
|
660
|
+
}
|
|
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()
|
|
629
670
|
}),
|
|
630
|
-
|
|
631
|
-
type:
|
|
632
|
-
thinking:
|
|
671
|
+
z3.object({
|
|
672
|
+
type: z3.literal("thinking"),
|
|
673
|
+
thinking: z3.string()
|
|
633
674
|
})
|
|
634
675
|
])
|
|
635
676
|
).nullish(),
|
|
636
|
-
tool_plan:
|
|
637
|
-
tool_calls:
|
|
638
|
-
|
|
639
|
-
id:
|
|
640
|
-
type:
|
|
641
|
-
function:
|
|
642
|
-
name:
|
|
643
|
-
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()
|
|
644
685
|
})
|
|
645
686
|
})
|
|
646
687
|
).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:
|
|
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()
|
|
660
701
|
})
|
|
661
702
|
})
|
|
662
703
|
),
|
|
663
|
-
type:
|
|
704
|
+
type: z3.string().optional()
|
|
664
705
|
})
|
|
665
706
|
).nullish()
|
|
666
707
|
}),
|
|
667
|
-
finish_reason:
|
|
668
|
-
usage:
|
|
669
|
-
billed_units:
|
|
670
|
-
input_tokens:
|
|
671
|
-
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()
|
|
672
713
|
}),
|
|
673
|
-
tokens:
|
|
674
|
-
input_tokens:
|
|
675
|
-
output_tokens:
|
|
714
|
+
tokens: z3.object({
|
|
715
|
+
input_tokens: z3.number(),
|
|
716
|
+
output_tokens: z3.number()
|
|
676
717
|
})
|
|
677
718
|
})
|
|
678
719
|
});
|
|
679
|
-
var cohereChatChunkSchema =
|
|
680
|
-
|
|
681
|
-
type:
|
|
720
|
+
var cohereChatChunkSchema = z3.discriminatedUnion("type", [
|
|
721
|
+
z3.object({
|
|
722
|
+
type: z3.literal("citation-start")
|
|
682
723
|
}),
|
|
683
|
-
|
|
684
|
-
type:
|
|
724
|
+
z3.object({
|
|
725
|
+
type: z3.literal("citation-end")
|
|
685
726
|
}),
|
|
686
|
-
|
|
687
|
-
type:
|
|
688
|
-
index:
|
|
689
|
-
delta:
|
|
690
|
-
message:
|
|
691
|
-
content:
|
|
692
|
-
|
|
693
|
-
type:
|
|
694
|
-
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()
|
|
695
736
|
}),
|
|
696
|
-
|
|
697
|
-
type:
|
|
698
|
-
thinking:
|
|
737
|
+
z3.object({
|
|
738
|
+
type: z3.literal("thinking"),
|
|
739
|
+
thinking: z3.string()
|
|
699
740
|
})
|
|
700
741
|
])
|
|
701
742
|
})
|
|
702
743
|
})
|
|
703
744
|
}),
|
|
704
|
-
|
|
705
|
-
type:
|
|
706
|
-
index:
|
|
707
|
-
delta:
|
|
708
|
-
message:
|
|
709
|
-
content:
|
|
710
|
-
|
|
711
|
-
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()
|
|
712
753
|
}),
|
|
713
|
-
|
|
714
|
-
thinking:
|
|
754
|
+
z3.object({
|
|
755
|
+
thinking: z3.string()
|
|
715
756
|
})
|
|
716
757
|
])
|
|
717
758
|
})
|
|
718
759
|
})
|
|
719
760
|
}),
|
|
720
|
-
|
|
721
|
-
type:
|
|
722
|
-
index:
|
|
761
|
+
z3.object({
|
|
762
|
+
type: z3.literal("content-end"),
|
|
763
|
+
index: z3.number()
|
|
723
764
|
}),
|
|
724
|
-
|
|
725
|
-
type:
|
|
726
|
-
id:
|
|
765
|
+
z3.object({
|
|
766
|
+
type: z3.literal("message-start"),
|
|
767
|
+
id: z3.string().nullish()
|
|
727
768
|
}),
|
|
728
|
-
|
|
729
|
-
type:
|
|
730
|
-
delta:
|
|
731
|
-
finish_reason:
|
|
732
|
-
usage:
|
|
733
|
-
tokens:
|
|
734
|
-
input_tokens:
|
|
735
|
-
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()
|
|
736
777
|
})
|
|
737
778
|
})
|
|
738
779
|
})
|
|
739
780
|
}),
|
|
740
781
|
// https://docs.cohere.com/v2/docs/streaming#tool-use-stream-events-for-tool-calling
|
|
741
|
-
|
|
742
|
-
type:
|
|
743
|
-
delta:
|
|
744
|
-
message:
|
|
745
|
-
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()
|
|
746
787
|
})
|
|
747
788
|
})
|
|
748
789
|
}),
|
|
749
|
-
|
|
750
|
-
type:
|
|
751
|
-
delta:
|
|
752
|
-
message:
|
|
753
|
-
tool_calls:
|
|
754
|
-
id:
|
|
755
|
-
type:
|
|
756
|
-
function:
|
|
757
|
-
name:
|
|
758
|
-
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()
|
|
759
800
|
})
|
|
760
801
|
})
|
|
761
802
|
})
|
|
@@ -764,31 +805,41 @@ var cohereChatChunkSchema = import_v43.z.discriminatedUnion("type", [
|
|
|
764
805
|
// A single tool call's `arguments` stream in chunks and must be accumulated
|
|
765
806
|
// in a string and so the full tool object info can only be parsed once we see
|
|
766
807
|
// `tool-call-end`.
|
|
767
|
-
|
|
768
|
-
type:
|
|
769
|
-
delta:
|
|
770
|
-
message:
|
|
771
|
-
tool_calls:
|
|
772
|
-
function:
|
|
773
|
-
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()
|
|
774
815
|
})
|
|
775
816
|
})
|
|
776
817
|
})
|
|
777
818
|
})
|
|
778
819
|
}),
|
|
779
|
-
|
|
780
|
-
type:
|
|
820
|
+
z3.object({
|
|
821
|
+
type: z3.literal("tool-call-end")
|
|
781
822
|
})
|
|
782
823
|
]);
|
|
783
824
|
|
|
784
825
|
// src/cohere-embedding-model.ts
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
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";
|
|
788
839
|
|
|
789
840
|
// src/cohere-embedding-options.ts
|
|
790
|
-
|
|
791
|
-
var cohereEmbeddingModelOptions =
|
|
841
|
+
import { z as z4 } from "zod/v4";
|
|
842
|
+
var cohereEmbeddingModelOptions = z4.object({
|
|
792
843
|
/**
|
|
793
844
|
* Specifies the type of input passed to the model. Default is `search_query`.
|
|
794
845
|
*
|
|
@@ -797,7 +848,7 @@ var cohereEmbeddingModelOptions = import_v44.z.object({
|
|
|
797
848
|
* - "classification": Used for embeddings passed through a text classifier.
|
|
798
849
|
* - "clustering": Used for embeddings run through a clustering algorithm.
|
|
799
850
|
*/
|
|
800
|
-
inputType:
|
|
851
|
+
inputType: z4.enum(["search_document", "search_query", "classification", "clustering"]).optional(),
|
|
801
852
|
/**
|
|
802
853
|
* Specifies how the API will handle inputs longer than the maximum token length.
|
|
803
854
|
* Default is `END`.
|
|
@@ -806,7 +857,7 @@ var cohereEmbeddingModelOptions = import_v44.z.object({
|
|
|
806
857
|
* - "START": Will discard the start of the input until the remaining input is exactly the maximum input token length for the model.
|
|
807
858
|
* - "END": Will discard the end of the input until the remaining input is exactly the maximum input token length for the model.
|
|
808
859
|
*/
|
|
809
|
-
truncate:
|
|
860
|
+
truncate: z4.enum(["NONE", "START", "END"]).optional(),
|
|
810
861
|
/**
|
|
811
862
|
* The number of dimensions of the output embedding.
|
|
812
863
|
* Only available for `embed-v4.0` and newer models.
|
|
@@ -814,18 +865,27 @@ var cohereEmbeddingModelOptions = import_v44.z.object({
|
|
|
814
865
|
* Possible values are `256`, `512`, `1024`, and `1536`.
|
|
815
866
|
* The default is `1536`.
|
|
816
867
|
*/
|
|
817
|
-
outputDimension:
|
|
868
|
+
outputDimension: z4.union([z4.literal(256), z4.literal(512), z4.literal(1024), z4.literal(1536)]).optional()
|
|
818
869
|
});
|
|
819
870
|
|
|
820
871
|
// src/cohere-embedding-model.ts
|
|
821
|
-
var CohereEmbeddingModel = class {
|
|
872
|
+
var CohereEmbeddingModel = class _CohereEmbeddingModel {
|
|
822
873
|
constructor(modelId, config) {
|
|
823
|
-
this.specificationVersion = "
|
|
874
|
+
this.specificationVersion = "v4";
|
|
824
875
|
this.maxEmbeddingsPerCall = 96;
|
|
825
876
|
this.supportsParallelCalls = true;
|
|
826
877
|
this.modelId = modelId;
|
|
827
878
|
this.config = config;
|
|
828
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
|
+
}
|
|
829
889
|
get provider() {
|
|
830
890
|
return this.config.provider;
|
|
831
891
|
}
|
|
@@ -835,14 +895,14 @@ var CohereEmbeddingModel = class {
|
|
|
835
895
|
abortSignal,
|
|
836
896
|
providerOptions
|
|
837
897
|
}) {
|
|
838
|
-
var _a;
|
|
839
|
-
const embeddingOptions = await (
|
|
898
|
+
var _a, _b, _c;
|
|
899
|
+
const embeddingOptions = await parseProviderOptions2({
|
|
840
900
|
provider: "cohere",
|
|
841
901
|
providerOptions,
|
|
842
902
|
schema: cohereEmbeddingModelOptions
|
|
843
903
|
});
|
|
844
904
|
if (values.length > this.maxEmbeddingsPerCall) {
|
|
845
|
-
throw new
|
|
905
|
+
throw new TooManyEmbeddingValuesForCallError({
|
|
846
906
|
provider: this.provider,
|
|
847
907
|
modelId: this.modelId,
|
|
848
908
|
maxEmbeddingsPerCall: this.maxEmbeddingsPerCall,
|
|
@@ -853,9 +913,9 @@ var CohereEmbeddingModel = class {
|
|
|
853
913
|
responseHeaders,
|
|
854
914
|
value: response,
|
|
855
915
|
rawValue
|
|
856
|
-
} = await (
|
|
916
|
+
} = await postJsonToApi2({
|
|
857
917
|
url: `${this.config.baseURL}/embed`,
|
|
858
|
-
headers: (
|
|
918
|
+
headers: combineHeaders2((_b = (_a = this.config).headers) == null ? void 0 : _b.call(_a), headers),
|
|
859
919
|
body: {
|
|
860
920
|
model: this.modelId,
|
|
861
921
|
// The AI SDK only supports 'float' embeddings. Note that the Cohere API
|
|
@@ -863,12 +923,12 @@ var CohereEmbeddingModel = class {
|
|
|
863
923
|
// https://docs.cohere.com/v2/reference/embed#request.body.embedding_types
|
|
864
924
|
embedding_types: ["float"],
|
|
865
925
|
texts: values,
|
|
866
|
-
input_type: (
|
|
926
|
+
input_type: (_c = embeddingOptions == null ? void 0 : embeddingOptions.inputType) != null ? _c : "search_query",
|
|
867
927
|
truncate: embeddingOptions == null ? void 0 : embeddingOptions.truncate,
|
|
868
928
|
output_dimension: embeddingOptions == null ? void 0 : embeddingOptions.outputDimension
|
|
869
929
|
},
|
|
870
930
|
failedResponseHandler: cohereFailedResponseHandler,
|
|
871
|
-
successfulResponseHandler: (
|
|
931
|
+
successfulResponseHandler: createJsonResponseHandler2(
|
|
872
932
|
cohereTextEmbeddingResponseSchema
|
|
873
933
|
),
|
|
874
934
|
abortSignal,
|
|
@@ -882,46 +942,54 @@ var CohereEmbeddingModel = class {
|
|
|
882
942
|
};
|
|
883
943
|
}
|
|
884
944
|
};
|
|
885
|
-
var cohereTextEmbeddingResponseSchema =
|
|
886
|
-
embeddings:
|
|
887
|
-
float:
|
|
945
|
+
var cohereTextEmbeddingResponseSchema = z5.object({
|
|
946
|
+
embeddings: z5.object({
|
|
947
|
+
float: z5.array(z5.array(z5.number()))
|
|
888
948
|
}),
|
|
889
|
-
meta:
|
|
890
|
-
billed_units:
|
|
891
|
-
input_tokens:
|
|
949
|
+
meta: z5.object({
|
|
950
|
+
billed_units: z5.object({
|
|
951
|
+
input_tokens: z5.number()
|
|
892
952
|
})
|
|
893
953
|
})
|
|
894
954
|
});
|
|
895
955
|
|
|
896
956
|
// src/reranking/cohere-reranking-model.ts
|
|
897
|
-
|
|
957
|
+
import {
|
|
958
|
+
combineHeaders as combineHeaders3,
|
|
959
|
+
createJsonResponseHandler as createJsonResponseHandler3,
|
|
960
|
+
parseProviderOptions as parseProviderOptions3,
|
|
961
|
+
postJsonToApi as postJsonToApi3
|
|
962
|
+
} from "@ai-sdk/provider-utils";
|
|
898
963
|
|
|
899
964
|
// src/reranking/cohere-reranking-api.ts
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
var cohereRerankingResponseSchema =
|
|
903
|
-
() =>
|
|
904
|
-
|
|
905
|
-
id:
|
|
906
|
-
results:
|
|
907
|
-
|
|
908
|
-
index:
|
|
909
|
-
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()
|
|
910
975
|
})
|
|
911
976
|
),
|
|
912
|
-
meta:
|
|
977
|
+
meta: z6.any()
|
|
913
978
|
})
|
|
914
979
|
)
|
|
915
980
|
);
|
|
916
981
|
|
|
917
982
|
// src/reranking/cohere-reranking-options.ts
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
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()
|
|
925
993
|
})
|
|
926
994
|
)
|
|
927
995
|
);
|
|
@@ -929,7 +997,7 @@ var cohereRerankingModelOptionsSchema = (0, import_provider_utils5.lazySchema)(
|
|
|
929
997
|
// src/reranking/cohere-reranking-model.ts
|
|
930
998
|
var CohereRerankingModel = class {
|
|
931
999
|
constructor(modelId, config) {
|
|
932
|
-
this.specificationVersion = "
|
|
1000
|
+
this.specificationVersion = "v4";
|
|
933
1001
|
this.modelId = modelId;
|
|
934
1002
|
this.config = config;
|
|
935
1003
|
}
|
|
@@ -946,7 +1014,7 @@ var CohereRerankingModel = class {
|
|
|
946
1014
|
providerOptions
|
|
947
1015
|
}) {
|
|
948
1016
|
var _a;
|
|
949
|
-
const rerankingOptions = await (
|
|
1017
|
+
const rerankingOptions = await parseProviderOptions3({
|
|
950
1018
|
provider: "cohere",
|
|
951
1019
|
providerOptions,
|
|
952
1020
|
schema: cohereRerankingModelOptionsSchema
|
|
@@ -963,9 +1031,9 @@ var CohereRerankingModel = class {
|
|
|
963
1031
|
responseHeaders,
|
|
964
1032
|
value: response,
|
|
965
1033
|
rawValue
|
|
966
|
-
} = await (
|
|
1034
|
+
} = await postJsonToApi3({
|
|
967
1035
|
url: `${this.config.baseURL}/rerank`,
|
|
968
|
-
headers: (
|
|
1036
|
+
headers: combineHeaders3(this.config.headers(), headers),
|
|
969
1037
|
body: {
|
|
970
1038
|
model: this.modelId,
|
|
971
1039
|
query,
|
|
@@ -975,7 +1043,7 @@ var CohereRerankingModel = class {
|
|
|
975
1043
|
priority: rerankingOptions == null ? void 0 : rerankingOptions.priority
|
|
976
1044
|
},
|
|
977
1045
|
failedResponseHandler: cohereFailedResponseHandler,
|
|
978
|
-
successfulResponseHandler: (
|
|
1046
|
+
successfulResponseHandler: createJsonResponseHandler3(
|
|
979
1047
|
cohereRerankingResponseSchema
|
|
980
1048
|
),
|
|
981
1049
|
abortSignal,
|
|
@@ -997,15 +1065,15 @@ var CohereRerankingModel = class {
|
|
|
997
1065
|
};
|
|
998
1066
|
|
|
999
1067
|
// src/version.ts
|
|
1000
|
-
var VERSION = true ? "4.0.0-beta.
|
|
1068
|
+
var VERSION = true ? "4.0.0-beta.31" : "0.0.0-test";
|
|
1001
1069
|
|
|
1002
1070
|
// src/cohere-provider.ts
|
|
1003
1071
|
function createCohere(options = {}) {
|
|
1004
1072
|
var _a;
|
|
1005
|
-
const baseURL = (_a =
|
|
1006
|
-
const getHeaders = () =>
|
|
1073
|
+
const baseURL = (_a = withoutTrailingSlash(options.baseURL)) != null ? _a : "https://api.cohere.com/v2";
|
|
1074
|
+
const getHeaders = () => withUserAgentSuffix(
|
|
1007
1075
|
{
|
|
1008
|
-
Authorization: `Bearer ${
|
|
1076
|
+
Authorization: `Bearer ${loadApiKey({
|
|
1009
1077
|
apiKey: options.apiKey,
|
|
1010
1078
|
environmentVariableName: "COHERE_API_KEY",
|
|
1011
1079
|
description: "Cohere"
|
|
@@ -1021,7 +1089,7 @@ function createCohere(options = {}) {
|
|
|
1021
1089
|
baseURL,
|
|
1022
1090
|
headers: getHeaders,
|
|
1023
1091
|
fetch: options.fetch,
|
|
1024
|
-
generateId: (_a2 = options.generateId) != null ? _a2 :
|
|
1092
|
+
generateId: (_a2 = options.generateId) != null ? _a2 : generateId
|
|
1025
1093
|
});
|
|
1026
1094
|
};
|
|
1027
1095
|
const createEmbeddingModel = (modelId) => new CohereEmbeddingModel(modelId, {
|
|
@@ -1044,7 +1112,7 @@ function createCohere(options = {}) {
|
|
|
1044
1112
|
}
|
|
1045
1113
|
return createChatModel(modelId);
|
|
1046
1114
|
};
|
|
1047
|
-
provider.specificationVersion = "
|
|
1115
|
+
provider.specificationVersion = "v4";
|
|
1048
1116
|
provider.languageModel = createChatModel;
|
|
1049
1117
|
provider.embedding = createEmbeddingModel;
|
|
1050
1118
|
provider.embeddingModel = createEmbeddingModel;
|
|
@@ -1053,15 +1121,14 @@ function createCohere(options = {}) {
|
|
|
1053
1121
|
provider.reranking = createRerankingModel;
|
|
1054
1122
|
provider.rerankingModel = createRerankingModel;
|
|
1055
1123
|
provider.imageModel = (modelId) => {
|
|
1056
|
-
throw new
|
|
1124
|
+
throw new NoSuchModelError({ modelId, modelType: "imageModel" });
|
|
1057
1125
|
};
|
|
1058
1126
|
return provider;
|
|
1059
1127
|
}
|
|
1060
1128
|
var cohere = createCohere();
|
|
1061
|
-
|
|
1062
|
-
0 && (module.exports = {
|
|
1129
|
+
export {
|
|
1063
1130
|
VERSION,
|
|
1064
1131
|
cohere,
|
|
1065
1132
|
createCohere
|
|
1066
|
-
}
|
|
1133
|
+
};
|
|
1067
1134
|
//# sourceMappingURL=index.js.map
|