@ai-sdk/cohere 0.0.27 → 0.0.28
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 +6 -0
- package/dist/index.d.mts +1 -2
- package/dist/index.d.ts +1 -2
- package/dist/index.js +233 -201
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +234 -203
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -51,13 +51,13 @@ function convertToCohereChatPrompt(prompt) {
|
|
|
51
51
|
for (const { role, content } of prompt) {
|
|
52
52
|
switch (role) {
|
|
53
53
|
case "system": {
|
|
54
|
-
messages.push({ role: "
|
|
54
|
+
messages.push({ role: "system", content });
|
|
55
55
|
break;
|
|
56
56
|
}
|
|
57
57
|
case "user": {
|
|
58
58
|
messages.push({
|
|
59
|
-
role: "
|
|
60
|
-
|
|
59
|
+
role: "user",
|
|
60
|
+
content: content.map((part) => {
|
|
61
61
|
switch (part.type) {
|
|
62
62
|
case "text": {
|
|
63
63
|
return part.text;
|
|
@@ -83,8 +83,12 @@ function convertToCohereChatPrompt(prompt) {
|
|
|
83
83
|
}
|
|
84
84
|
case "tool-call": {
|
|
85
85
|
toolCalls.push({
|
|
86
|
-
|
|
87
|
-
|
|
86
|
+
id: part.toolCallId,
|
|
87
|
+
type: "function",
|
|
88
|
+
function: {
|
|
89
|
+
name: part.toolName,
|
|
90
|
+
arguments: JSON.stringify(part.args)
|
|
91
|
+
}
|
|
88
92
|
});
|
|
89
93
|
break;
|
|
90
94
|
}
|
|
@@ -95,31 +99,23 @@ function convertToCohereChatPrompt(prompt) {
|
|
|
95
99
|
}
|
|
96
100
|
}
|
|
97
101
|
messages.push({
|
|
98
|
-
role: "
|
|
99
|
-
|
|
102
|
+
role: "assistant",
|
|
103
|
+
// note: this is a workaround for a Cohere API bug
|
|
104
|
+
// that requires content to be provided
|
|
105
|
+
// even if there are tool calls
|
|
106
|
+
content: text !== "" ? text : "call tool",
|
|
100
107
|
tool_calls: toolCalls.length > 0 ? toolCalls : void 0
|
|
101
108
|
});
|
|
102
109
|
break;
|
|
103
110
|
}
|
|
104
111
|
case "tool": {
|
|
105
|
-
messages.push(
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
/*
|
|
111
|
-
Note: Currently the tool_results field requires we pass the parameters of the tool results again. It it is blank for two reasons:
|
|
112
|
-
|
|
113
|
-
1. The parameters are already present in chat_history as a tool message
|
|
114
|
-
2. The tool core message of the ai sdk does not include parameters
|
|
115
|
-
|
|
116
|
-
It is possible to traverse through the chat history and get the parameters by id but it's currently empty since there wasn't any degradation in the output when left blank.
|
|
117
|
-
*/
|
|
118
|
-
parameters: {}
|
|
119
|
-
},
|
|
120
|
-
outputs: [toolResult.result]
|
|
112
|
+
messages.push(
|
|
113
|
+
...content.map((toolResult) => ({
|
|
114
|
+
role: "tool",
|
|
115
|
+
content: JSON.stringify(toolResult.result),
|
|
116
|
+
tool_call_id: toolResult.toolCallId
|
|
121
117
|
}))
|
|
122
|
-
|
|
118
|
+
);
|
|
123
119
|
break;
|
|
124
120
|
}
|
|
125
121
|
default: {
|
|
@@ -158,77 +154,38 @@ function prepareTools(mode) {
|
|
|
158
154
|
const tools = ((_a = mode.tools) == null ? void 0 : _a.length) ? mode.tools : void 0;
|
|
159
155
|
const toolWarnings = [];
|
|
160
156
|
if (tools == null) {
|
|
161
|
-
return { tools: void 0,
|
|
157
|
+
return { tools: void 0, tool_choice: void 0, toolWarnings };
|
|
162
158
|
}
|
|
163
159
|
const cohereTools = [];
|
|
164
160
|
for (const tool of tools) {
|
|
165
161
|
if (tool.type === "provider-defined") {
|
|
166
162
|
toolWarnings.push({ type: "unsupported-tool", tool });
|
|
167
163
|
} else {
|
|
168
|
-
const { properties, required } = tool.parameters;
|
|
169
|
-
const parameterDefinitions = {};
|
|
170
|
-
if (properties) {
|
|
171
|
-
for (const [key, value] of Object.entries(properties)) {
|
|
172
|
-
if (typeof value === "object" && value !== null) {
|
|
173
|
-
const { type: JSONType, description } = value;
|
|
174
|
-
let type2;
|
|
175
|
-
if (typeof JSONType === "string") {
|
|
176
|
-
switch (JSONType) {
|
|
177
|
-
case "string":
|
|
178
|
-
type2 = "str";
|
|
179
|
-
break;
|
|
180
|
-
case "number":
|
|
181
|
-
type2 = "float";
|
|
182
|
-
break;
|
|
183
|
-
case "integer":
|
|
184
|
-
type2 = "int";
|
|
185
|
-
break;
|
|
186
|
-
case "boolean":
|
|
187
|
-
type2 = "bool";
|
|
188
|
-
break;
|
|
189
|
-
default:
|
|
190
|
-
throw new import_provider2.UnsupportedFunctionalityError({
|
|
191
|
-
functionality: `Unsupported tool parameter type: ${JSONType}`
|
|
192
|
-
});
|
|
193
|
-
}
|
|
194
|
-
} else {
|
|
195
|
-
throw new import_provider2.UnsupportedFunctionalityError({
|
|
196
|
-
functionality: `Unsupported tool parameter type: ${JSONType}`
|
|
197
|
-
});
|
|
198
|
-
}
|
|
199
|
-
parameterDefinitions[key] = {
|
|
200
|
-
required: required ? required.includes(key) : false,
|
|
201
|
-
type: type2,
|
|
202
|
-
description
|
|
203
|
-
};
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
164
|
cohereTools.push({
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
165
|
+
type: "function",
|
|
166
|
+
function: {
|
|
167
|
+
name: tool.name,
|
|
168
|
+
description: tool.description,
|
|
169
|
+
parameters: tool.parameters
|
|
170
|
+
}
|
|
211
171
|
});
|
|
212
172
|
}
|
|
213
173
|
}
|
|
214
174
|
const toolChoice = mode.toolChoice;
|
|
215
175
|
if (toolChoice == null) {
|
|
216
|
-
return { tools: cohereTools,
|
|
176
|
+
return { tools: cohereTools, tool_choice: void 0, toolWarnings };
|
|
217
177
|
}
|
|
218
178
|
const type = toolChoice.type;
|
|
219
179
|
switch (type) {
|
|
220
180
|
case "auto":
|
|
221
|
-
return { tools: cohereTools,
|
|
222
|
-
case "required":
|
|
223
|
-
return { tools: cohereTools, force_single_step: true, toolWarnings };
|
|
181
|
+
return { tools: cohereTools, tool_choice: type, toolWarnings };
|
|
224
182
|
case "none":
|
|
225
|
-
return { tools: void 0,
|
|
183
|
+
return { tools: void 0, tool_choice: "any", toolWarnings };
|
|
184
|
+
case "required":
|
|
226
185
|
case "tool":
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
toolWarnings
|
|
231
|
-
};
|
|
186
|
+
throw new import_provider2.UnsupportedFunctionalityError({
|
|
187
|
+
functionality: `Unsupported tool choice type: ${type}`
|
|
188
|
+
});
|
|
232
189
|
default: {
|
|
233
190
|
const _exhaustiveCheck = type;
|
|
234
191
|
throw new import_provider2.UnsupportedFunctionalityError({
|
|
@@ -265,8 +222,6 @@ var CohereChatLanguageModel = class {
|
|
|
265
222
|
}) {
|
|
266
223
|
const type = mode.type;
|
|
267
224
|
const chatPrompt = convertToCohereChatPrompt(prompt);
|
|
268
|
-
const lastMessage = chatPrompt.at(-1);
|
|
269
|
-
const history = chatPrompt.slice(0, -1);
|
|
270
225
|
const baseArgs = {
|
|
271
226
|
// model id:
|
|
272
227
|
model: this.modelId,
|
|
@@ -284,17 +239,14 @@ var CohereChatLanguageModel = class {
|
|
|
284
239
|
// response format:
|
|
285
240
|
response_format: (responseFormat == null ? void 0 : responseFormat.type) === "json" ? { type: "json_object", schema: responseFormat.schema } : void 0,
|
|
286
241
|
// messages:
|
|
287
|
-
|
|
288
|
-
...(lastMessage == null ? void 0 : lastMessage.role) === "TOOL" ? { tool_results: lastMessage.tool_results } : {},
|
|
289
|
-
message: lastMessage ? lastMessage.role === "USER" ? lastMessage.message : void 0 : void 0
|
|
242
|
+
messages: chatPrompt
|
|
290
243
|
};
|
|
291
244
|
switch (type) {
|
|
292
245
|
case "regular": {
|
|
293
|
-
const { tools,
|
|
246
|
+
const { tools, tool_choice, toolWarnings } = prepareTools(mode);
|
|
294
247
|
return {
|
|
295
248
|
...baseArgs,
|
|
296
249
|
tools,
|
|
297
|
-
force_single_step,
|
|
298
250
|
warnings: toolWarnings
|
|
299
251
|
};
|
|
300
252
|
}
|
|
@@ -310,13 +262,41 @@ var CohereChatLanguageModel = class {
|
|
|
310
262
|
}
|
|
311
263
|
default: {
|
|
312
264
|
const _exhaustiveCheck = type;
|
|
313
|
-
throw new
|
|
265
|
+
throw new import_provider3.UnsupportedFunctionalityError({
|
|
266
|
+
functionality: `Unsupported mode: ${_exhaustiveCheck}`
|
|
267
|
+
});
|
|
314
268
|
}
|
|
315
269
|
}
|
|
316
270
|
}
|
|
271
|
+
concatenateMessageText(messages) {
|
|
272
|
+
return messages.filter(
|
|
273
|
+
(message) => "content" in message
|
|
274
|
+
).map((message) => message.content).join("");
|
|
275
|
+
}
|
|
276
|
+
/*
|
|
277
|
+
Remove `additionalProperties` and `$schema` from the `parameters` object of each tool.
|
|
278
|
+
Though these are part of JSON schema, Cohere chokes if we include them in the request.
|
|
279
|
+
*/
|
|
280
|
+
// TODO(shaper): Look at defining a type to simplify the params here and a couple of other places.
|
|
281
|
+
removeJsonSchemaExtras(tools) {
|
|
282
|
+
return tools.map((tool) => {
|
|
283
|
+
if (tool.type === "function" && tool.function.parameters && typeof tool.function.parameters === "object") {
|
|
284
|
+
const { additionalProperties, $schema, ...restParameters } = tool.function.parameters;
|
|
285
|
+
return {
|
|
286
|
+
...tool,
|
|
287
|
+
function: {
|
|
288
|
+
...tool.function,
|
|
289
|
+
parameters: restParameters
|
|
290
|
+
}
|
|
291
|
+
};
|
|
292
|
+
}
|
|
293
|
+
return tool;
|
|
294
|
+
});
|
|
295
|
+
}
|
|
317
296
|
async doGenerate(options) {
|
|
318
|
-
var _a;
|
|
297
|
+
var _a, _b, _c, _d;
|
|
319
298
|
const { warnings, ...args } = this.getArgs(options);
|
|
299
|
+
args.tools = args.tools && this.removeJsonSchemaExtras(args.tools);
|
|
320
300
|
const { responseHeaders, value: response } = await (0, import_provider_utils2.postJsonToApi)({
|
|
321
301
|
url: `${this.config.baseURL}/chat`,
|
|
322
302
|
headers: (0, import_provider_utils2.combineHeaders)(this.config.headers(), options.headers),
|
|
@@ -328,30 +308,28 @@ var CohereChatLanguageModel = class {
|
|
|
328
308
|
abortSignal: options.abortSignal,
|
|
329
309
|
fetch: this.config.fetch
|
|
330
310
|
});
|
|
331
|
-
const {
|
|
332
|
-
const generateId2 = this.config.generateId;
|
|
311
|
+
const { messages, ...rawSettings } = args;
|
|
333
312
|
return {
|
|
334
|
-
text: response.text,
|
|
335
|
-
toolCalls: response.tool_calls ? response.tool_calls.map((toolCall) => ({
|
|
336
|
-
toolCallId:
|
|
337
|
-
toolName: toolCall.name,
|
|
338
|
-
args:
|
|
313
|
+
text: (_c = (_b = (_a = response.message.content) == null ? void 0 : _a[0]) == null ? void 0 : _b.text) != null ? _c : "",
|
|
314
|
+
toolCalls: response.message.tool_calls ? response.message.tool_calls.map((toolCall) => ({
|
|
315
|
+
toolCallId: toolCall.id,
|
|
316
|
+
toolName: toolCall.function.name,
|
|
317
|
+
args: toolCall.function.arguments,
|
|
339
318
|
toolCallType: "function"
|
|
340
319
|
})) : [],
|
|
341
320
|
finishReason: mapCohereFinishReason(response.finish_reason),
|
|
342
321
|
usage: {
|
|
343
|
-
promptTokens: response.
|
|
344
|
-
completionTokens: response.
|
|
322
|
+
promptTokens: response.usage.tokens.input_tokens,
|
|
323
|
+
completionTokens: response.usage.tokens.output_tokens
|
|
345
324
|
},
|
|
346
325
|
rawCall: {
|
|
347
326
|
rawPrompt: {
|
|
348
|
-
|
|
349
|
-
message
|
|
327
|
+
messages
|
|
350
328
|
},
|
|
351
329
|
rawSettings
|
|
352
330
|
},
|
|
353
331
|
response: {
|
|
354
|
-
id: (
|
|
332
|
+
id: (_d = response.generation_id) != null ? _d : void 0
|
|
355
333
|
},
|
|
356
334
|
rawResponse: { headers: responseHeaders },
|
|
357
335
|
warnings,
|
|
@@ -360,26 +338,30 @@ var CohereChatLanguageModel = class {
|
|
|
360
338
|
}
|
|
361
339
|
async doStream(options) {
|
|
362
340
|
const { warnings, ...args } = this.getArgs(options);
|
|
341
|
+
args.tools = args.tools && this.removeJsonSchemaExtras(args.tools);
|
|
363
342
|
const body = { ...args, stream: true };
|
|
364
343
|
const { responseHeaders, value: response } = await (0, import_provider_utils2.postJsonToApi)({
|
|
365
344
|
url: `${this.config.baseURL}/chat`,
|
|
366
345
|
headers: (0, import_provider_utils2.combineHeaders)(this.config.headers(), options.headers),
|
|
367
346
|
body,
|
|
368
347
|
failedResponseHandler: cohereFailedResponseHandler,
|
|
369
|
-
successfulResponseHandler: (0, import_provider_utils2.
|
|
348
|
+
successfulResponseHandler: (0, import_provider_utils2.createEventSourceResponseHandler)(
|
|
370
349
|
cohereChatChunkSchema
|
|
371
350
|
),
|
|
372
351
|
abortSignal: options.abortSignal,
|
|
373
352
|
fetch: this.config.fetch
|
|
374
353
|
});
|
|
375
|
-
const {
|
|
354
|
+
const { messages, ...rawSettings } = args;
|
|
376
355
|
let finishReason = "unknown";
|
|
377
356
|
let usage = {
|
|
378
357
|
promptTokens: Number.NaN,
|
|
379
358
|
completionTokens: Number.NaN
|
|
380
359
|
};
|
|
381
|
-
|
|
382
|
-
|
|
360
|
+
let pendingToolCallDelta = {
|
|
361
|
+
toolCallId: "",
|
|
362
|
+
toolName: "",
|
|
363
|
+
argsTextDelta: ""
|
|
364
|
+
};
|
|
383
365
|
return {
|
|
384
366
|
stream: response.pipeThrough(
|
|
385
367
|
new TransformStream({
|
|
@@ -391,69 +373,68 @@ var CohereChatLanguageModel = class {
|
|
|
391
373
|
return;
|
|
392
374
|
}
|
|
393
375
|
const value = chunk.value;
|
|
394
|
-
const type = value.
|
|
376
|
+
const type = value.type;
|
|
395
377
|
switch (type) {
|
|
396
|
-
case "
|
|
378
|
+
case "content-delta": {
|
|
397
379
|
controller.enqueue({
|
|
398
380
|
type: "text-delta",
|
|
399
|
-
textDelta: value.text
|
|
381
|
+
textDelta: value.delta.message.content.text
|
|
400
382
|
});
|
|
401
383
|
return;
|
|
402
384
|
}
|
|
403
|
-
case "tool-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
type: "tool-call-delta",
|
|
417
|
-
toolCallType: "function",
|
|
418
|
-
toolCallId: toolCalls[index].toolCallId,
|
|
419
|
-
toolName: toolCalls[index].toolName,
|
|
420
|
-
argsTextDelta: ""
|
|
421
|
-
});
|
|
422
|
-
} else if (value.tool_call_delta.parameters) {
|
|
423
|
-
controller.enqueue({
|
|
424
|
-
type: "tool-call-delta",
|
|
425
|
-
toolCallType: "function",
|
|
426
|
-
toolCallId: toolCalls[index].toolCallId,
|
|
427
|
-
toolName: toolCalls[index].toolName,
|
|
428
|
-
argsTextDelta: value.tool_call_delta.parameters
|
|
429
|
-
});
|
|
430
|
-
}
|
|
431
|
-
}
|
|
385
|
+
case "tool-call-start": {
|
|
386
|
+
pendingToolCallDelta = {
|
|
387
|
+
toolCallId: value.delta.message.tool_calls.id,
|
|
388
|
+
toolName: value.delta.message.tool_calls.function.name,
|
|
389
|
+
argsTextDelta: value.delta.message.tool_calls.function.arguments
|
|
390
|
+
};
|
|
391
|
+
controller.enqueue({
|
|
392
|
+
type: "tool-call-delta",
|
|
393
|
+
toolCallId: pendingToolCallDelta.toolCallId,
|
|
394
|
+
toolName: pendingToolCallDelta.toolName,
|
|
395
|
+
toolCallType: "function",
|
|
396
|
+
argsTextDelta: pendingToolCallDelta.argsTextDelta
|
|
397
|
+
});
|
|
432
398
|
return;
|
|
433
399
|
}
|
|
434
|
-
case "tool-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
});
|
|
444
|
-
}
|
|
400
|
+
case "tool-call-delta": {
|
|
401
|
+
pendingToolCallDelta.argsTextDelta += value.delta.message.tool_calls.function.arguments;
|
|
402
|
+
controller.enqueue({
|
|
403
|
+
type: "tool-call-delta",
|
|
404
|
+
toolCallId: pendingToolCallDelta.toolCallId,
|
|
405
|
+
toolName: pendingToolCallDelta.toolName,
|
|
406
|
+
toolCallType: "function",
|
|
407
|
+
argsTextDelta: value.delta.message.tool_calls.function.arguments
|
|
408
|
+
});
|
|
445
409
|
return;
|
|
446
410
|
}
|
|
447
|
-
case "
|
|
411
|
+
case "tool-call-end": {
|
|
412
|
+
controller.enqueue({
|
|
413
|
+
type: "tool-call",
|
|
414
|
+
toolCallId: pendingToolCallDelta.toolCallId,
|
|
415
|
+
toolName: pendingToolCallDelta.toolName,
|
|
416
|
+
toolCallType: "function",
|
|
417
|
+
args: JSON.stringify(
|
|
418
|
+
JSON.parse(pendingToolCallDelta.argsTextDelta)
|
|
419
|
+
)
|
|
420
|
+
});
|
|
421
|
+
pendingToolCallDelta = {
|
|
422
|
+
toolCallId: "",
|
|
423
|
+
toolName: "",
|
|
424
|
+
argsTextDelta: ""
|
|
425
|
+
};
|
|
426
|
+
return;
|
|
427
|
+
}
|
|
428
|
+
case "message-start": {
|
|
448
429
|
controller.enqueue({
|
|
449
430
|
type: "response-metadata",
|
|
450
|
-
id: (_a = value.
|
|
431
|
+
id: (_a = value.id) != null ? _a : void 0
|
|
451
432
|
});
|
|
452
433
|
return;
|
|
453
434
|
}
|
|
454
|
-
case "
|
|
455
|
-
finishReason = mapCohereFinishReason(value.finish_reason);
|
|
456
|
-
const tokens = value.
|
|
435
|
+
case "message-end": {
|
|
436
|
+
finishReason = mapCohereFinishReason(value.delta.finish_reason);
|
|
437
|
+
const tokens = value.delta.usage.tokens;
|
|
457
438
|
usage = {
|
|
458
439
|
promptTokens: tokens.input_tokens,
|
|
459
440
|
completionTokens: tokens.output_tokens
|
|
@@ -475,8 +456,7 @@ var CohereChatLanguageModel = class {
|
|
|
475
456
|
),
|
|
476
457
|
rawCall: {
|
|
477
458
|
rawPrompt: {
|
|
478
|
-
|
|
479
|
-
message
|
|
459
|
+
messages
|
|
480
460
|
},
|
|
481
461
|
rawSettings
|
|
482
462
|
},
|
|
@@ -488,68 +468,117 @@ var CohereChatLanguageModel = class {
|
|
|
488
468
|
};
|
|
489
469
|
var cohereChatResponseSchema = import_zod2.z.object({
|
|
490
470
|
generation_id: import_zod2.z.string().nullish(),
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
import_zod2.z.
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
471
|
+
message: import_zod2.z.object({
|
|
472
|
+
role: import_zod2.z.string(),
|
|
473
|
+
content: import_zod2.z.array(
|
|
474
|
+
import_zod2.z.object({
|
|
475
|
+
type: import_zod2.z.string(),
|
|
476
|
+
text: import_zod2.z.string()
|
|
477
|
+
})
|
|
478
|
+
).nullish(),
|
|
479
|
+
tool_calls: import_zod2.z.array(
|
|
480
|
+
import_zod2.z.object({
|
|
481
|
+
id: import_zod2.z.string(),
|
|
482
|
+
type: import_zod2.z.literal("function"),
|
|
483
|
+
function: import_zod2.z.object({
|
|
484
|
+
name: import_zod2.z.string(),
|
|
485
|
+
arguments: import_zod2.z.string()
|
|
486
|
+
})
|
|
487
|
+
})
|
|
488
|
+
).nullish()
|
|
489
|
+
}),
|
|
498
490
|
finish_reason: import_zod2.z.string(),
|
|
499
|
-
|
|
491
|
+
usage: import_zod2.z.object({
|
|
492
|
+
billed_units: import_zod2.z.object({
|
|
493
|
+
input_tokens: import_zod2.z.number(),
|
|
494
|
+
output_tokens: import_zod2.z.number()
|
|
495
|
+
}),
|
|
500
496
|
tokens: import_zod2.z.object({
|
|
501
497
|
input_tokens: import_zod2.z.number(),
|
|
502
498
|
output_tokens: import_zod2.z.number()
|
|
503
499
|
})
|
|
504
500
|
})
|
|
505
501
|
});
|
|
506
|
-
var cohereChatChunkSchema = import_zod2.z.discriminatedUnion("
|
|
507
|
-
import_zod2.z.object({
|
|
508
|
-
event_type: import_zod2.z.literal("stream-start"),
|
|
509
|
-
generation_id: import_zod2.z.string().nullish()
|
|
510
|
-
}),
|
|
502
|
+
var cohereChatChunkSchema = import_zod2.z.discriminatedUnion("type", [
|
|
511
503
|
import_zod2.z.object({
|
|
512
|
-
|
|
504
|
+
type: import_zod2.z.literal("citation-start")
|
|
513
505
|
}),
|
|
514
506
|
import_zod2.z.object({
|
|
515
|
-
|
|
507
|
+
type: import_zod2.z.literal("citation-end")
|
|
516
508
|
}),
|
|
517
509
|
import_zod2.z.object({
|
|
518
|
-
|
|
519
|
-
text: import_zod2.z.string()
|
|
510
|
+
type: import_zod2.z.literal("content-start")
|
|
520
511
|
}),
|
|
521
512
|
import_zod2.z.object({
|
|
522
|
-
|
|
513
|
+
type: import_zod2.z.literal("content-delta"),
|
|
514
|
+
delta: import_zod2.z.object({
|
|
515
|
+
message: import_zod2.z.object({
|
|
516
|
+
content: import_zod2.z.object({
|
|
517
|
+
text: import_zod2.z.string()
|
|
518
|
+
})
|
|
519
|
+
})
|
|
520
|
+
})
|
|
523
521
|
}),
|
|
524
522
|
import_zod2.z.object({
|
|
525
|
-
|
|
526
|
-
tool_calls: import_zod2.z.array(
|
|
527
|
-
import_zod2.z.object({
|
|
528
|
-
name: import_zod2.z.string(),
|
|
529
|
-
parameters: import_zod2.z.unknown({})
|
|
530
|
-
})
|
|
531
|
-
)
|
|
523
|
+
type: import_zod2.z.literal("content-end")
|
|
532
524
|
}),
|
|
533
525
|
import_zod2.z.object({
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
tool_call_delta: import_zod2.z.object({
|
|
537
|
-
index: import_zod2.z.number(),
|
|
538
|
-
name: import_zod2.z.string().optional(),
|
|
539
|
-
parameters: import_zod2.z.string().optional()
|
|
540
|
-
}).optional()
|
|
526
|
+
type: import_zod2.z.literal("message-start"),
|
|
527
|
+
id: import_zod2.z.string().nullish()
|
|
541
528
|
}),
|
|
542
529
|
import_zod2.z.object({
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
530
|
+
type: import_zod2.z.literal("message-end"),
|
|
531
|
+
delta: import_zod2.z.object({
|
|
532
|
+
finish_reason: import_zod2.z.string(),
|
|
533
|
+
usage: import_zod2.z.object({
|
|
547
534
|
tokens: import_zod2.z.object({
|
|
548
535
|
input_tokens: import_zod2.z.number(),
|
|
549
536
|
output_tokens: import_zod2.z.number()
|
|
550
537
|
})
|
|
551
538
|
})
|
|
552
539
|
})
|
|
540
|
+
}),
|
|
541
|
+
// https://docs.cohere.com/v2/docs/streaming#tool-use-stream-events-for-tool-calling
|
|
542
|
+
import_zod2.z.object({
|
|
543
|
+
type: import_zod2.z.literal("tool-plan-delta"),
|
|
544
|
+
delta: import_zod2.z.object({
|
|
545
|
+
message: import_zod2.z.object({
|
|
546
|
+
tool_plan: import_zod2.z.string()
|
|
547
|
+
})
|
|
548
|
+
})
|
|
549
|
+
}),
|
|
550
|
+
import_zod2.z.object({
|
|
551
|
+
type: import_zod2.z.literal("tool-call-start"),
|
|
552
|
+
delta: import_zod2.z.object({
|
|
553
|
+
message: import_zod2.z.object({
|
|
554
|
+
tool_calls: import_zod2.z.object({
|
|
555
|
+
id: import_zod2.z.string(),
|
|
556
|
+
type: import_zod2.z.literal("function"),
|
|
557
|
+
function: import_zod2.z.object({
|
|
558
|
+
name: import_zod2.z.string(),
|
|
559
|
+
arguments: import_zod2.z.string()
|
|
560
|
+
})
|
|
561
|
+
})
|
|
562
|
+
})
|
|
563
|
+
})
|
|
564
|
+
}),
|
|
565
|
+
// A single tool call's `arguments` stream in chunks and must be accumulated
|
|
566
|
+
// in a string and so the full tool object info can only be parsed once we see
|
|
567
|
+
// `tool-call-end`.
|
|
568
|
+
import_zod2.z.object({
|
|
569
|
+
type: import_zod2.z.literal("tool-call-delta"),
|
|
570
|
+
delta: import_zod2.z.object({
|
|
571
|
+
message: import_zod2.z.object({
|
|
572
|
+
tool_calls: import_zod2.z.object({
|
|
573
|
+
function: import_zod2.z.object({
|
|
574
|
+
arguments: import_zod2.z.string()
|
|
575
|
+
})
|
|
576
|
+
})
|
|
577
|
+
})
|
|
578
|
+
})
|
|
579
|
+
}),
|
|
580
|
+
import_zod2.z.object({
|
|
581
|
+
type: import_zod2.z.literal("tool-call-end")
|
|
553
582
|
})
|
|
554
583
|
]);
|
|
555
584
|
|
|
@@ -588,6 +617,11 @@ var CohereEmbeddingModel = class {
|
|
|
588
617
|
headers: (0, import_provider_utils3.combineHeaders)(this.config.headers(), headers),
|
|
589
618
|
body: {
|
|
590
619
|
model: this.modelId,
|
|
620
|
+
// TODO(shaper): There are other embedding types. Do we need to support them?
|
|
621
|
+
// For now we only support 'float' embeddings which are also the only ones
|
|
622
|
+
// the Cohere API docs state are supported for all models.
|
|
623
|
+
// https://docs.cohere.com/v2/reference/embed#request.body.embedding_types
|
|
624
|
+
embedding_types: ["float"],
|
|
591
625
|
texts: values,
|
|
592
626
|
input_type: (_a = this.settings.inputType) != null ? _a : "search_query",
|
|
593
627
|
truncate: this.settings.truncate
|
|
@@ -600,14 +634,16 @@ var CohereEmbeddingModel = class {
|
|
|
600
634
|
fetch: this.config.fetch
|
|
601
635
|
});
|
|
602
636
|
return {
|
|
603
|
-
embeddings: response.embeddings,
|
|
637
|
+
embeddings: response.embeddings.float,
|
|
604
638
|
usage: { tokens: response.meta.billed_units.input_tokens },
|
|
605
639
|
rawResponse: { headers: responseHeaders }
|
|
606
640
|
};
|
|
607
641
|
}
|
|
608
642
|
};
|
|
609
643
|
var cohereTextEmbeddingResponseSchema = import_zod3.z.object({
|
|
610
|
-
embeddings: import_zod3.z.
|
|
644
|
+
embeddings: import_zod3.z.object({
|
|
645
|
+
float: import_zod3.z.array(import_zod3.z.array(import_zod3.z.number()))
|
|
646
|
+
}),
|
|
611
647
|
meta: import_zod3.z.object({
|
|
612
648
|
billed_units: import_zod3.z.object({
|
|
613
649
|
input_tokens: import_zod3.z.number()
|
|
@@ -618,7 +654,7 @@ var cohereTextEmbeddingResponseSchema = import_zod3.z.object({
|
|
|
618
654
|
// src/cohere-provider.ts
|
|
619
655
|
function createCohere(options = {}) {
|
|
620
656
|
var _a;
|
|
621
|
-
const baseURL = (_a = (0, import_provider_utils4.withoutTrailingSlash)(options.baseURL)) != null ? _a : "https://api.cohere.com/
|
|
657
|
+
const baseURL = (_a = (0, import_provider_utils4.withoutTrailingSlash)(options.baseURL)) != null ? _a : "https://api.cohere.com/v2";
|
|
622
658
|
const getHeaders = () => ({
|
|
623
659
|
Authorization: `Bearer ${(0, import_provider_utils4.loadApiKey)({
|
|
624
660
|
apiKey: options.apiKey,
|
|
@@ -627,16 +663,12 @@ function createCohere(options = {}) {
|
|
|
627
663
|
})}`,
|
|
628
664
|
...options.headers
|
|
629
665
|
});
|
|
630
|
-
const createChatModel = (modelId, settings = {}) => {
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
generateId: (_a2 = options.generateId) != null ? _a2 : import_provider_utils4.generateId,
|
|
637
|
-
fetch: options.fetch
|
|
638
|
-
});
|
|
639
|
-
};
|
|
666
|
+
const createChatModel = (modelId, settings = {}) => new CohereChatLanguageModel(modelId, settings, {
|
|
667
|
+
provider: "cohere.chat",
|
|
668
|
+
baseURL,
|
|
669
|
+
headers: getHeaders,
|
|
670
|
+
fetch: options.fetch
|
|
671
|
+
});
|
|
640
672
|
const createTextEmbeddingModel = (modelId, settings = {}) => new CohereEmbeddingModel(modelId, settings, {
|
|
641
673
|
provider: "cohere.textEmbedding",
|
|
642
674
|
baseURL,
|