@ai-sdk/cohere 4.0.0-beta.5 → 4.0.0-beta.53
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 +397 -4
- package/README.md +2 -0
- package/dist/index.d.ts +4 -4
- package/dist/index.js +404 -272
- package/dist/index.js.map +1 -1
- package/docs/25-cohere.mdx +59 -4
- package/package.json +14 -14
- package/src/{cohere-chat-options.ts → cohere-chat-language-model-options.ts} +17 -3
- package/src/cohere-chat-language-model.ts +94 -24
- package/src/cohere-chat-prompt.ts +8 -1
- package/src/cohere-embedding-model.ts +23 -6
- package/src/cohere-prepare-tools.ts +3 -3
- package/src/cohere-provider.ts +8 -9
- package/src/convert-cohere-usage.ts +1 -1
- package/src/convert-to-cohere-chat-prompt.ts +119 -54
- package/src/index.ts +8 -6
- package/src/map-cohere-finish-reason.ts +1 -1
- package/src/reranking/{cohere-reranking-options.ts → cohere-reranking-model-options.ts} +5 -1
- package/src/reranking/cohere-reranking-model.ts +5 -6
- package/dist/index.d.mts +0 -117
- package/dist/index.mjs +0 -1067
- package/dist/index.mjs.map +0 -1
- /package/src/{cohere-embedding-options.ts → cohere-embedding-model-options.ts} +0 -0
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type {
|
|
2
2
|
LanguageModelV4,
|
|
3
3
|
LanguageModelV4CallOptions,
|
|
4
4
|
LanguageModelV4Content,
|
|
@@ -6,31 +6,42 @@ import {
|
|
|
6
6
|
LanguageModelV4GenerateResult,
|
|
7
7
|
LanguageModelV4StreamPart,
|
|
8
8
|
LanguageModelV4StreamResult,
|
|
9
|
+
SharedV4Warning,
|
|
9
10
|
} from '@ai-sdk/provider';
|
|
10
11
|
import {
|
|
11
|
-
FetchFunction,
|
|
12
|
-
ParseResult,
|
|
13
12
|
combineHeaders,
|
|
14
13
|
createEventSourceResponseHandler,
|
|
15
14
|
createJsonResponseHandler,
|
|
15
|
+
isCustomReasoning,
|
|
16
|
+
mapReasoningToProviderBudget,
|
|
16
17
|
parseProviderOptions,
|
|
18
|
+
parseJSON,
|
|
17
19
|
postJsonToApi,
|
|
20
|
+
serializeModelOptions,
|
|
21
|
+
WORKFLOW_SERIALIZE,
|
|
22
|
+
WORKFLOW_DESERIALIZE,
|
|
23
|
+
type InferSchema,
|
|
24
|
+
type FetchFunction,
|
|
25
|
+
type ParseResult,
|
|
18
26
|
} from '@ai-sdk/provider-utils';
|
|
19
27
|
import { z } from 'zod/v4';
|
|
20
28
|
import {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
} from './cohere-chat-options';
|
|
29
|
+
cohereLanguageModelChatOptions,
|
|
30
|
+
type CohereChatModelId,
|
|
31
|
+
} from './cohere-chat-language-model-options';
|
|
24
32
|
import { cohereFailedResponseHandler } from './cohere-error';
|
|
25
33
|
import { prepareTools } from './cohere-prepare-tools';
|
|
26
|
-
import {
|
|
34
|
+
import {
|
|
35
|
+
convertCohereUsage,
|
|
36
|
+
type CohereUsageTokens,
|
|
37
|
+
} from './convert-cohere-usage';
|
|
27
38
|
import { convertToCohereChatPrompt } from './convert-to-cohere-chat-prompt';
|
|
28
39
|
import { mapCohereFinishReason } from './map-cohere-finish-reason';
|
|
29
40
|
|
|
30
41
|
type CohereChatConfig = {
|
|
31
42
|
provider: string;
|
|
32
43
|
baseURL: string;
|
|
33
|
-
headers
|
|
44
|
+
headers?: () => Record<string, string | undefined>;
|
|
34
45
|
fetch?: FetchFunction;
|
|
35
46
|
generateId: () => string;
|
|
36
47
|
};
|
|
@@ -40,12 +51,26 @@ export class CohereChatLanguageModel implements LanguageModelV4 {
|
|
|
40
51
|
|
|
41
52
|
readonly modelId: CohereChatModelId;
|
|
42
53
|
|
|
43
|
-
readonly supportedUrls = {
|
|
44
|
-
|
|
54
|
+
readonly supportedUrls: Record<string, RegExp[]> = {
|
|
55
|
+
'image/*': [/^https?:\/\/.*$/],
|
|
45
56
|
};
|
|
46
57
|
|
|
47
58
|
private readonly config: CohereChatConfig;
|
|
48
59
|
|
|
60
|
+
static [WORKFLOW_SERIALIZE](model: CohereChatLanguageModel) {
|
|
61
|
+
return serializeModelOptions({
|
|
62
|
+
modelId: model.modelId,
|
|
63
|
+
config: model.config,
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
static [WORKFLOW_DESERIALIZE](options: {
|
|
68
|
+
modelId: CohereChatModelId;
|
|
69
|
+
config: CohereChatConfig;
|
|
70
|
+
}) {
|
|
71
|
+
return new CohereChatLanguageModel(options.modelId, options.config);
|
|
72
|
+
}
|
|
73
|
+
|
|
49
74
|
constructor(modelId: CohereChatModelId, config: CohereChatConfig) {
|
|
50
75
|
this.modelId = modelId;
|
|
51
76
|
this.config = config;
|
|
@@ -66,23 +91,25 @@ export class CohereChatLanguageModel implements LanguageModelV4 {
|
|
|
66
91
|
stopSequences,
|
|
67
92
|
responseFormat,
|
|
68
93
|
seed,
|
|
94
|
+
reasoning,
|
|
69
95
|
tools,
|
|
70
96
|
toolChoice,
|
|
71
97
|
providerOptions,
|
|
72
98
|
}: LanguageModelV4CallOptions) {
|
|
73
|
-
|
|
99
|
+
const warnings: SharedV4Warning[] = [];
|
|
100
|
+
|
|
74
101
|
const cohereOptions =
|
|
75
102
|
(await parseProviderOptions({
|
|
76
103
|
provider: 'cohere',
|
|
77
104
|
providerOptions,
|
|
78
|
-
schema:
|
|
105
|
+
schema: cohereLanguageModelChatOptions,
|
|
79
106
|
})) ?? {};
|
|
80
107
|
|
|
81
108
|
const {
|
|
82
109
|
messages: chatPrompt,
|
|
83
110
|
documents: cohereDocuments,
|
|
84
111
|
warnings: promptWarnings,
|
|
85
|
-
} = convertToCohereChatPrompt(prompt);
|
|
112
|
+
} = await convertToCohereChatPrompt(prompt);
|
|
86
113
|
|
|
87
114
|
const {
|
|
88
115
|
tools: cohereTools,
|
|
@@ -90,6 +117,8 @@ export class CohereChatLanguageModel implements LanguageModelV4 {
|
|
|
90
117
|
toolWarnings,
|
|
91
118
|
} = prepareTools({ tools, toolChoice });
|
|
92
119
|
|
|
120
|
+
warnings.push(...toolWarnings, ...promptWarnings);
|
|
121
|
+
|
|
93
122
|
return {
|
|
94
123
|
args: {
|
|
95
124
|
// model id:
|
|
@@ -121,15 +150,14 @@ export class CohereChatLanguageModel implements LanguageModelV4 {
|
|
|
121
150
|
// documents for RAG:
|
|
122
151
|
...(cohereDocuments.length > 0 && { documents: cohereDocuments }),
|
|
123
152
|
|
|
124
|
-
// reasoning
|
|
125
|
-
...(
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
},
|
|
153
|
+
// reasoning:
|
|
154
|
+
...resolveCohereThinking({
|
|
155
|
+
reasoning,
|
|
156
|
+
cohereOptions,
|
|
157
|
+
warnings,
|
|
130
158
|
}),
|
|
131
159
|
},
|
|
132
|
-
warnings
|
|
160
|
+
warnings,
|
|
133
161
|
};
|
|
134
162
|
}
|
|
135
163
|
|
|
@@ -144,7 +172,7 @@ export class CohereChatLanguageModel implements LanguageModelV4 {
|
|
|
144
172
|
rawValue: rawResponse,
|
|
145
173
|
} = await postJsonToApi({
|
|
146
174
|
url: `${this.config.baseURL}/chat`,
|
|
147
|
-
headers: combineHeaders(this.config.headers(), options.headers),
|
|
175
|
+
headers: combineHeaders(this.config.headers?.(), options.headers),
|
|
148
176
|
body: args,
|
|
149
177
|
failedResponseHandler: cohereFailedResponseHandler,
|
|
150
178
|
successfulResponseHandler: createJsonResponseHandler(
|
|
@@ -225,7 +253,7 @@ export class CohereChatLanguageModel implements LanguageModelV4 {
|
|
|
225
253
|
|
|
226
254
|
const { responseHeaders, value: response } = await postJsonToApi({
|
|
227
255
|
url: `${this.config.baseURL}/chat`,
|
|
228
|
-
headers: combineHeaders(this.config.headers(), options.headers),
|
|
256
|
+
headers: combineHeaders(this.config.headers?.(), options.headers),
|
|
229
257
|
body: { ...args, stream: true },
|
|
230
258
|
failedResponseHandler: cohereFailedResponseHandler,
|
|
231
259
|
successfulResponseHandler: createEventSourceResponseHandler(
|
|
@@ -260,7 +288,7 @@ export class CohereChatLanguageModel implements LanguageModelV4 {
|
|
|
260
288
|
controller.enqueue({ type: 'stream-start', warnings });
|
|
261
289
|
},
|
|
262
290
|
|
|
263
|
-
transform(chunk, controller) {
|
|
291
|
+
async transform(chunk, controller) {
|
|
264
292
|
if (options.includeRawChunks) {
|
|
265
293
|
controller.enqueue({ type: 'raw', rawValue: chunk.rawValue });
|
|
266
294
|
}
|
|
@@ -385,7 +413,9 @@ export class CohereChatLanguageModel implements LanguageModelV4 {
|
|
|
385
413
|
toolCallId: pendingToolCall.id,
|
|
386
414
|
toolName: pendingToolCall.name,
|
|
387
415
|
input: JSON.stringify(
|
|
388
|
-
|
|
416
|
+
await parseJSON({
|
|
417
|
+
text: pendingToolCall.arguments?.trim() || '{}',
|
|
418
|
+
}),
|
|
389
419
|
),
|
|
390
420
|
});
|
|
391
421
|
|
|
@@ -433,6 +463,46 @@ export class CohereChatLanguageModel implements LanguageModelV4 {
|
|
|
433
463
|
}
|
|
434
464
|
}
|
|
435
465
|
|
|
466
|
+
function resolveCohereThinking({
|
|
467
|
+
reasoning,
|
|
468
|
+
cohereOptions,
|
|
469
|
+
warnings,
|
|
470
|
+
}: {
|
|
471
|
+
reasoning: LanguageModelV4CallOptions['reasoning'];
|
|
472
|
+
cohereOptions: InferSchema<typeof cohereLanguageModelChatOptions>;
|
|
473
|
+
warnings: SharedV4Warning[];
|
|
474
|
+
}): { thinking?: { type: string; token_budget?: number } } {
|
|
475
|
+
if (cohereOptions.thinking) {
|
|
476
|
+
return {
|
|
477
|
+
thinking: {
|
|
478
|
+
type: cohereOptions.thinking.type ?? 'enabled',
|
|
479
|
+
token_budget: cohereOptions.thinking.tokenBudget,
|
|
480
|
+
},
|
|
481
|
+
};
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
if (!isCustomReasoning(reasoning)) {
|
|
485
|
+
return {};
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
if (reasoning === 'none') {
|
|
489
|
+
return { thinking: { type: 'disabled' } };
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
const tokenBudget = mapReasoningToProviderBudget({
|
|
493
|
+
reasoning,
|
|
494
|
+
maxOutputTokens: 32768,
|
|
495
|
+
maxReasoningBudget: 32768,
|
|
496
|
+
warnings,
|
|
497
|
+
});
|
|
498
|
+
|
|
499
|
+
if (tokenBudget == null) {
|
|
500
|
+
return {};
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
return { thinking: { type: 'enabled', token_budget: tokenBudget } };
|
|
504
|
+
}
|
|
505
|
+
|
|
436
506
|
const cohereChatResponseSchema = z.object({
|
|
437
507
|
generation_id: z.string().nullish(),
|
|
438
508
|
message: z.object({
|
|
@@ -13,9 +13,16 @@ export interface CohereSystemMessage {
|
|
|
13
13
|
|
|
14
14
|
export interface CohereUserMessage {
|
|
15
15
|
role: 'user';
|
|
16
|
-
content: string
|
|
16
|
+
content: string | Array<CohereUserMessageContent>;
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
+
export type CohereUserMessageContent =
|
|
20
|
+
| { type: 'text'; text: string }
|
|
21
|
+
| {
|
|
22
|
+
type: 'image_url';
|
|
23
|
+
image_url: { url: string; detail?: 'auto' | 'low' | 'high' };
|
|
24
|
+
};
|
|
25
|
+
|
|
19
26
|
export interface CohereAssistantMessage {
|
|
20
27
|
role: 'assistant';
|
|
21
28
|
content: string | undefined;
|
|
@@ -1,25 +1,28 @@
|
|
|
1
1
|
import {
|
|
2
|
-
EmbeddingModelV4,
|
|
3
2
|
TooManyEmbeddingValuesForCallError,
|
|
3
|
+
type EmbeddingModelV4,
|
|
4
4
|
} from '@ai-sdk/provider';
|
|
5
5
|
import {
|
|
6
6
|
combineHeaders,
|
|
7
7
|
createJsonResponseHandler,
|
|
8
|
-
FetchFunction,
|
|
9
8
|
parseProviderOptions,
|
|
10
9
|
postJsonToApi,
|
|
10
|
+
serializeModelOptions,
|
|
11
|
+
WORKFLOW_SERIALIZE,
|
|
12
|
+
WORKFLOW_DESERIALIZE,
|
|
13
|
+
type FetchFunction,
|
|
11
14
|
} from '@ai-sdk/provider-utils';
|
|
12
15
|
import { z } from 'zod/v4';
|
|
13
16
|
import {
|
|
14
|
-
CohereEmbeddingModelId,
|
|
15
17
|
cohereEmbeddingModelOptions,
|
|
16
|
-
|
|
18
|
+
type CohereEmbeddingModelId,
|
|
19
|
+
} from './cohere-embedding-model-options';
|
|
17
20
|
import { cohereFailedResponseHandler } from './cohere-error';
|
|
18
21
|
|
|
19
22
|
type CohereEmbeddingConfig = {
|
|
20
23
|
provider: string;
|
|
21
24
|
baseURL: string;
|
|
22
|
-
headers
|
|
25
|
+
headers?: () => Record<string, string | undefined>;
|
|
23
26
|
fetch?: FetchFunction;
|
|
24
27
|
};
|
|
25
28
|
|
|
@@ -32,6 +35,20 @@ export class CohereEmbeddingModel implements EmbeddingModelV4 {
|
|
|
32
35
|
|
|
33
36
|
private readonly config: CohereEmbeddingConfig;
|
|
34
37
|
|
|
38
|
+
static [WORKFLOW_SERIALIZE](model: CohereEmbeddingModel) {
|
|
39
|
+
return serializeModelOptions({
|
|
40
|
+
modelId: model.modelId,
|
|
41
|
+
config: model.config,
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
static [WORKFLOW_DESERIALIZE](options: {
|
|
46
|
+
modelId: CohereEmbeddingModelId;
|
|
47
|
+
config: CohereEmbeddingConfig;
|
|
48
|
+
}) {
|
|
49
|
+
return new CohereEmbeddingModel(options.modelId, options.config);
|
|
50
|
+
}
|
|
51
|
+
|
|
35
52
|
constructor(modelId: CohereEmbeddingModelId, config: CohereEmbeddingConfig) {
|
|
36
53
|
this.modelId = modelId;
|
|
37
54
|
this.config = config;
|
|
@@ -70,7 +87,7 @@ export class CohereEmbeddingModel implements EmbeddingModelV4 {
|
|
|
70
87
|
rawValue,
|
|
71
88
|
} = await postJsonToApi({
|
|
72
89
|
url: `${this.config.baseURL}/embed`,
|
|
73
|
-
headers: combineHeaders(this.config.headers(), headers),
|
|
90
|
+
headers: combineHeaders(this.config.headers?.(), headers),
|
|
74
91
|
body: {
|
|
75
92
|
model: this.modelId,
|
|
76
93
|
// The AI SDK only supports 'float' embeddings. Note that the Cohere API
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import {
|
|
2
|
-
LanguageModelV4CallOptions,
|
|
3
|
-
SharedV4Warning,
|
|
4
2
|
UnsupportedFunctionalityError,
|
|
3
|
+
type LanguageModelV4CallOptions,
|
|
4
|
+
type SharedV4Warning,
|
|
5
5
|
} from '@ai-sdk/provider';
|
|
6
|
-
import { CohereToolChoice } from './cohere-chat-prompt';
|
|
6
|
+
import type { CohereToolChoice } from './cohere-chat-prompt';
|
|
7
7
|
|
|
8
8
|
export function prepareTools({
|
|
9
9
|
tools,
|
package/src/cohere-provider.ts
CHANGED
|
@@ -1,24 +1,23 @@
|
|
|
1
1
|
import {
|
|
2
|
-
EmbeddingModelV4,
|
|
3
|
-
LanguageModelV4,
|
|
4
2
|
NoSuchModelError,
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
type EmbeddingModelV4,
|
|
4
|
+
type LanguageModelV4,
|
|
5
|
+
type RerankingModelV4,
|
|
6
|
+
type ProviderV4,
|
|
7
7
|
} from '@ai-sdk/provider';
|
|
8
|
-
|
|
9
8
|
import {
|
|
10
|
-
FetchFunction,
|
|
11
9
|
generateId,
|
|
12
10
|
loadApiKey,
|
|
13
11
|
withoutTrailingSlash,
|
|
14
12
|
withUserAgentSuffix,
|
|
13
|
+
type FetchFunction,
|
|
15
14
|
} from '@ai-sdk/provider-utils';
|
|
16
15
|
import { CohereChatLanguageModel } from './cohere-chat-language-model';
|
|
17
|
-
import { CohereChatModelId } from './cohere-chat-options';
|
|
16
|
+
import type { CohereChatModelId } from './cohere-chat-language-model-options';
|
|
18
17
|
import { CohereEmbeddingModel } from './cohere-embedding-model';
|
|
19
|
-
import { CohereRerankingModelId } from './reranking/cohere-reranking-options';
|
|
18
|
+
import type { CohereRerankingModelId } from './reranking/cohere-reranking-model-options';
|
|
20
19
|
import { CohereRerankingModel } from './reranking/cohere-reranking-model';
|
|
21
|
-
import { CohereEmbeddingModelId } from './cohere-embedding-options';
|
|
20
|
+
import type { CohereEmbeddingModelId } from './cohere-embedding-model-options';
|
|
22
21
|
import { VERSION } from './version';
|
|
23
22
|
|
|
24
23
|
export interface CohereProvider extends ProviderV4 {
|
|
@@ -1,17 +1,31 @@
|
|
|
1
1
|
import {
|
|
2
|
-
SharedV4Warning,
|
|
3
|
-
LanguageModelV4Prompt,
|
|
4
2
|
UnsupportedFunctionalityError,
|
|
3
|
+
type LanguageModelV4FilePart,
|
|
4
|
+
type LanguageModelV4Prompt,
|
|
5
|
+
type SharedV4Warning,
|
|
5
6
|
} from '@ai-sdk/provider';
|
|
6
|
-
import {
|
|
7
|
+
import {
|
|
8
|
+
convertToBase64,
|
|
9
|
+
getTopLevelMediaType,
|
|
10
|
+
parseProviderOptions,
|
|
11
|
+
resolveFullMediaType,
|
|
12
|
+
} from '@ai-sdk/provider-utils';
|
|
13
|
+
import { cohereImagePartProviderOptions } from './cohere-chat-language-model-options';
|
|
14
|
+
import type {
|
|
15
|
+
CohereAssistantMessage,
|
|
16
|
+
CohereChatPrompt,
|
|
17
|
+
CohereUserMessageContent,
|
|
18
|
+
} from './cohere-chat-prompt';
|
|
7
19
|
|
|
8
|
-
export function convertToCohereChatPrompt(
|
|
20
|
+
export async function convertToCohereChatPrompt(
|
|
21
|
+
prompt: LanguageModelV4Prompt,
|
|
22
|
+
): Promise<{
|
|
9
23
|
messages: CohereChatPrompt;
|
|
10
24
|
documents: Array<{
|
|
11
25
|
data: { text: string; title?: string };
|
|
12
26
|
}>;
|
|
13
27
|
warnings: SharedV4Warning[];
|
|
14
|
-
} {
|
|
28
|
+
}> {
|
|
15
29
|
const messages: CohereChatPrompt = [];
|
|
16
30
|
const documents: Array<{ data: { text: string; title?: string } }> = [];
|
|
17
31
|
const warnings: SharedV4Warning[] = [];
|
|
@@ -24,58 +38,88 @@ export function convertToCohereChatPrompt(prompt: LanguageModelV4Prompt): {
|
|
|
24
38
|
}
|
|
25
39
|
|
|
26
40
|
case 'user': {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
41
|
+
const userContentParts: Array<CohereUserMessageContent> = [];
|
|
42
|
+
let hasImage = false;
|
|
43
|
+
|
|
44
|
+
for (const part of content) {
|
|
45
|
+
switch (part.type) {
|
|
46
|
+
case 'text': {
|
|
47
|
+
if (part.text.length > 0) {
|
|
48
|
+
userContentParts.push({ type: 'text', text: part.text });
|
|
49
|
+
}
|
|
50
|
+
break;
|
|
51
|
+
}
|
|
52
|
+
case 'file': {
|
|
53
|
+
if (getTopLevelMediaType(part.mediaType) === 'image') {
|
|
54
|
+
hasImage = true;
|
|
55
|
+
const url = buildImageUrl({ part });
|
|
56
|
+
const cohereOptions =
|
|
57
|
+
(await parseProviderOptions({
|
|
58
|
+
provider: 'cohere',
|
|
59
|
+
providerOptions: part.providerOptions,
|
|
60
|
+
schema: cohereImagePartProviderOptions,
|
|
61
|
+
})) ?? {};
|
|
62
|
+
|
|
63
|
+
userContentParts.push({
|
|
64
|
+
type: 'image_url',
|
|
65
|
+
image_url: {
|
|
66
|
+
url,
|
|
67
|
+
...(cohereOptions.detail
|
|
68
|
+
? { detail: cohereOptions.detail }
|
|
69
|
+
: {}),
|
|
70
|
+
},
|
|
71
|
+
});
|
|
72
|
+
break;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
let textContent: string;
|
|
76
|
+
switch (part.data.type) {
|
|
77
|
+
case 'reference': {
|
|
78
|
+
throw new UnsupportedFunctionalityError({
|
|
79
|
+
functionality: 'file parts with provider references',
|
|
80
|
+
});
|
|
34
81
|
}
|
|
35
|
-
case '
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
// Base64 or text data
|
|
41
|
-
textContent = part.data;
|
|
42
|
-
} else if (part.data instanceof Uint8Array) {
|
|
43
|
-
// Check if the media type is supported for text extraction
|
|
44
|
-
if (
|
|
45
|
-
!(
|
|
46
|
-
part.mediaType?.startsWith('text/') ||
|
|
47
|
-
part.mediaType === 'application/json'
|
|
48
|
-
)
|
|
49
|
-
) {
|
|
50
|
-
throw new UnsupportedFunctionalityError({
|
|
51
|
-
functionality: `document media type: ${part.mediaType}`,
|
|
52
|
-
message: `Media type '${part.mediaType}' is not supported. Supported media types are: text/* and application/json.`,
|
|
53
|
-
});
|
|
54
|
-
}
|
|
55
|
-
textContent = new TextDecoder().decode(part.data);
|
|
56
|
-
} else {
|
|
57
|
-
throw new UnsupportedFunctionalityError({
|
|
58
|
-
functionality: 'File URL data',
|
|
59
|
-
message:
|
|
60
|
-
'URLs should be downloaded by the AI SDK and not reach this point. This indicates a configuration issue.',
|
|
61
|
-
});
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
documents.push({
|
|
65
|
-
data: {
|
|
66
|
-
text: textContent,
|
|
67
|
-
title: part.filename,
|
|
68
|
-
},
|
|
82
|
+
case 'url': {
|
|
83
|
+
throw new UnsupportedFunctionalityError({
|
|
84
|
+
functionality: 'File URL data',
|
|
85
|
+
message:
|
|
86
|
+
'URLs should be downloaded by the AI SDK and not reach this point. This indicates a configuration issue.',
|
|
69
87
|
});
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
88
|
+
}
|
|
89
|
+
case 'text': {
|
|
90
|
+
textContent = part.data.text;
|
|
91
|
+
break;
|
|
92
|
+
}
|
|
93
|
+
case 'data': {
|
|
94
|
+
textContent =
|
|
95
|
+
typeof part.data.data === 'string'
|
|
96
|
+
? part.data.data
|
|
97
|
+
: new TextDecoder().decode(part.data.data);
|
|
98
|
+
break;
|
|
74
99
|
}
|
|
75
100
|
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
101
|
+
|
|
102
|
+
documents.push({
|
|
103
|
+
data: {
|
|
104
|
+
text: textContent,
|
|
105
|
+
title: part.filename,
|
|
106
|
+
},
|
|
107
|
+
});
|
|
108
|
+
break;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
if (hasImage) {
|
|
114
|
+
messages.push({ role: 'user', content: userContentParts });
|
|
115
|
+
} else {
|
|
116
|
+
messages.push({
|
|
117
|
+
role: 'user',
|
|
118
|
+
content: userContentParts
|
|
119
|
+
.map(p => (p.type === 'text' ? p.text : ''))
|
|
120
|
+
.join(''),
|
|
121
|
+
});
|
|
122
|
+
}
|
|
79
123
|
break;
|
|
80
124
|
}
|
|
81
125
|
|
|
@@ -126,7 +170,7 @@ export function convertToCohereChatPrompt(prompt: LanguageModelV4Prompt): {
|
|
|
126
170
|
contentValue = output.value;
|
|
127
171
|
break;
|
|
128
172
|
case 'execution-denied':
|
|
129
|
-
contentValue = output.reason ?? 'Tool execution denied.';
|
|
173
|
+
contentValue = output.reason ?? 'Tool call execution denied.';
|
|
130
174
|
break;
|
|
131
175
|
case 'content':
|
|
132
176
|
case 'json':
|
|
@@ -154,3 +198,24 @@ export function convertToCohereChatPrompt(prompt: LanguageModelV4Prompt): {
|
|
|
154
198
|
|
|
155
199
|
return { messages, documents, warnings };
|
|
156
200
|
}
|
|
201
|
+
|
|
202
|
+
function buildImageUrl({ part }: { part: LanguageModelV4FilePart }): string {
|
|
203
|
+
switch (part.data.type) {
|
|
204
|
+
case 'url': {
|
|
205
|
+
return part.data.url.toString();
|
|
206
|
+
}
|
|
207
|
+
case 'data': {
|
|
208
|
+
return `data:${resolveFullMediaType({ part })};base64,${convertToBase64(part.data.data)}`;
|
|
209
|
+
}
|
|
210
|
+
case 'reference': {
|
|
211
|
+
throw new UnsupportedFunctionalityError({
|
|
212
|
+
functionality: 'image file parts with provider references',
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
case 'text': {
|
|
216
|
+
throw new UnsupportedFunctionalityError({
|
|
217
|
+
functionality: 'image file parts with text data',
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
export type {
|
|
2
|
-
|
|
3
|
-
/** @deprecated Use `
|
|
4
|
-
|
|
5
|
-
|
|
2
|
+
CohereLanguageModelChatOptions,
|
|
3
|
+
/** @deprecated Use `CohereLanguageModelChatOptions` instead. */
|
|
4
|
+
CohereLanguageModelChatOptions as CohereLanguageModelOptions,
|
|
5
|
+
/** @deprecated Use `CohereLanguageModelChatOptions` instead. */
|
|
6
|
+
CohereLanguageModelChatOptions as CohereChatModelOptions,
|
|
7
|
+
} from './cohere-chat-language-model-options';
|
|
6
8
|
export { cohere, createCohere } from './cohere-provider';
|
|
7
9
|
export type { CohereProvider, CohereProviderSettings } from './cohere-provider';
|
|
8
|
-
export type { CohereEmbeddingModelOptions } from './cohere-embedding-options';
|
|
10
|
+
export type { CohereEmbeddingModelOptions } from './cohere-embedding-model-options';
|
|
9
11
|
export type {
|
|
10
12
|
CohereRerankingModelOptions,
|
|
11
13
|
/** @deprecated Use `CohereRerankingModelOptions` instead. */
|
|
12
14
|
CohereRerankingModelOptions as CohereRerankingOptions,
|
|
13
|
-
} from './reranking/cohere-reranking-options';
|
|
15
|
+
} from './reranking/cohere-reranking-model-options';
|
|
14
16
|
export { VERSION } from './version';
|
|
@@ -1,21 +1,20 @@
|
|
|
1
|
-
import { RerankingModelV4, SharedV4Warning } from '@ai-sdk/provider';
|
|
1
|
+
import type { RerankingModelV4, SharedV4Warning } from '@ai-sdk/provider';
|
|
2
2
|
import {
|
|
3
3
|
combineHeaders,
|
|
4
4
|
createJsonResponseHandler,
|
|
5
|
-
FetchFunction,
|
|
6
5
|
parseProviderOptions,
|
|
7
6
|
postJsonToApi,
|
|
7
|
+
type FetchFunction,
|
|
8
8
|
} from '@ai-sdk/provider-utils';
|
|
9
9
|
import { cohereFailedResponseHandler } from '../cohere-error';
|
|
10
10
|
import {
|
|
11
|
-
CohereRerankingInput,
|
|
12
11
|
cohereRerankingResponseSchema,
|
|
12
|
+
type CohereRerankingInput,
|
|
13
13
|
} from './cohere-reranking-api';
|
|
14
14
|
import {
|
|
15
|
-
CohereRerankingModelId,
|
|
16
15
|
cohereRerankingModelOptionsSchema,
|
|
17
|
-
|
|
18
|
-
|
|
16
|
+
type CohereRerankingModelId,
|
|
17
|
+
} from './cohere-reranking-model-options';
|
|
19
18
|
type CohereRerankingConfig = {
|
|
20
19
|
provider: string;
|
|
21
20
|
baseURL: string;
|