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