@ai-sdk/cohere
Advanced tools
Comparing version 0.0.17 to 0.0.18
@@ -85,5 +85,7 @@ "use strict"; | ||
case "tool-call": { | ||
throw new import_provider.UnsupportedFunctionalityError({ | ||
functionality: "tool-call" | ||
toolCalls.push({ | ||
name: part.toolName, | ||
parameters: part.args | ||
}); | ||
break; | ||
} | ||
@@ -104,5 +106,21 @@ default: { | ||
case "tool": { | ||
throw new import_provider.UnsupportedFunctionalityError({ | ||
functionality: "tool role" | ||
messages.push({ | ||
role: "TOOL", | ||
tool_results: content.map((toolResult) => ({ | ||
call: { | ||
name: toolResult.toolName, | ||
/* | ||
Note: Currently the tool_results field requires we pass the parameters of the tool results again. It it is blank for two reasons: | ||
1. The parameters are already present in chat_history as a tool message | ||
2. The tool core message of the ai sdk does not include parameters | ||
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. | ||
*/ | ||
parameters: {} | ||
}, | ||
outputs: [toolResult.result] | ||
})) | ||
}); | ||
break; | ||
} | ||
@@ -167,3 +185,3 @@ default: { | ||
const history = chatPrompt.slice(0, -1); | ||
const args = { | ||
const baseArgs = { | ||
// model id: | ||
@@ -186,2 +204,3 @@ model: this.modelId, | ||
chat_history: history, | ||
...(lastMessage == null ? void 0 : lastMessage.role) === "TOOL" ? { tool_results: lastMessage.tool_results } : {}, | ||
message: lastMessage ? lastMessage.role === "USER" ? lastMessage.message : void 0 : void 0 | ||
@@ -191,3 +210,3 @@ }; | ||
case "regular": { | ||
return args; | ||
return { ...baseArgs, ...prepareToolsAndToolChoice(mode) }; | ||
} | ||
@@ -224,4 +243,11 @@ case "object-json": { | ||
const { chat_history, message, ...rawSettings } = args; | ||
const generateId2 = this.config.generateId; | ||
return { | ||
text: response.text, | ||
toolCalls: response.tool_calls ? response.tool_calls.map((toolCall) => ({ | ||
toolCallId: generateId2(), | ||
toolName: toolCall.name, | ||
args: JSON.stringify(toolCall.parameters), | ||
toolCallType: "function" | ||
})) : [], | ||
finishReason: mapCohereFinishReason(response.finish_reason), | ||
@@ -265,2 +291,4 @@ usage: { | ||
}; | ||
const generateId2 = this.config.generateId; | ||
const toolCalls = []; | ||
return { | ||
@@ -285,2 +313,46 @@ stream: response.pipeThrough( | ||
} | ||
case "tool-calls-chunk": { | ||
if (value.tool_call_delta) { | ||
const { index } = value.tool_call_delta; | ||
if (toolCalls[index] === void 0) { | ||
const toolCallId = generateId2(); | ||
toolCalls[index] = { | ||
toolCallId, | ||
toolName: "" | ||
}; | ||
} | ||
if (value.tool_call_delta.name) { | ||
toolCalls[index].toolName = value.tool_call_delta.name; | ||
controller.enqueue({ | ||
type: "tool-call-delta", | ||
toolCallType: "function", | ||
toolCallId: toolCalls[index].toolCallId, | ||
toolName: toolCalls[index].toolName, | ||
argsTextDelta: "" | ||
}); | ||
} else if (value.tool_call_delta.parameters) { | ||
controller.enqueue({ | ||
type: "tool-call-delta", | ||
toolCallType: "function", | ||
toolCallId: toolCalls[index].toolCallId, | ||
toolName: toolCalls[index].toolName, | ||
argsTextDelta: value.tool_call_delta.parameters | ||
}); | ||
} | ||
} | ||
return; | ||
} | ||
case "tool-calls-generation": { | ||
for (let index = 0; index < value.tool_calls.length; index++) { | ||
const toolCall = value.tool_calls[index]; | ||
controller.enqueue({ | ||
type: "tool-call", | ||
toolCallId: toolCalls[index].toolCallId, | ||
toolName: toolCalls[index].toolName, | ||
toolCallType: "function", | ||
args: JSON.stringify(toolCall.parameters) | ||
}); | ||
} | ||
return; | ||
} | ||
case "stream-end": { | ||
@@ -322,2 +394,8 @@ finishReason = mapCohereFinishReason(value.finish_reason); | ||
text: import_zod2.z.string(), | ||
tool_calls: import_zod2.z.array( | ||
import_zod2.z.object({ | ||
name: import_zod2.z.string(), | ||
parameters: import_zod2.z.unknown({}) | ||
}) | ||
).optional(), | ||
finish_reason: import_zod2.z.string(), | ||
@@ -349,5 +427,20 @@ meta: import_zod2.z.object({ | ||
import_zod2.z.object({ | ||
event_type: import_zod2.z.literal("tool-calls-generation") | ||
event_type: import_zod2.z.literal("tool-calls-generation"), | ||
tool_calls: import_zod2.z.array( | ||
import_zod2.z.object({ | ||
name: import_zod2.z.string(), | ||
parameters: import_zod2.z.unknown({}) | ||
}) | ||
) | ||
}), | ||
import_zod2.z.object({ | ||
event_type: import_zod2.z.literal("tool-calls-chunk"), | ||
text: import_zod2.z.string().optional(), | ||
tool_call_delta: import_zod2.z.object({ | ||
index: import_zod2.z.number(), | ||
name: import_zod2.z.string().optional(), | ||
parameters: import_zod2.z.string().optional() | ||
}).optional() | ||
}), | ||
import_zod2.z.object({ | ||
event_type: import_zod2.z.literal("stream-end"), | ||
@@ -365,2 +458,79 @@ finish_reason: import_zod2.z.string(), | ||
]); | ||
function prepareToolsAndToolChoice(mode) { | ||
var _a; | ||
const tools = ((_a = mode.tools) == null ? void 0 : _a.length) ? mode.tools : void 0; | ||
if (tools == null) { | ||
return { tools: void 0 }; | ||
} | ||
const mappedTools = tools.map((tool) => { | ||
const { properties, required } = tool.parameters; | ||
const parameterDefinitions = {}; | ||
if (properties) { | ||
for (const [key, value] of Object.entries(properties)) { | ||
if (typeof value === "object" && value !== null) { | ||
const { type: JSONType, description } = value; | ||
let type2; | ||
if (typeof JSONType === "string") { | ||
switch (JSONType) { | ||
case "string": | ||
type2 = "str"; | ||
break; | ||
case "number": | ||
type2 = "float"; | ||
break; | ||
case "integer": | ||
type2 = "int"; | ||
break; | ||
case "boolean": | ||
type2 = "bool"; | ||
break; | ||
default: | ||
throw new import_provider2.UnsupportedFunctionalityError({ | ||
functionality: "tool call parameter of non-primitive type" | ||
}); | ||
} | ||
} else { | ||
throw new import_provider2.UnsupportedFunctionalityError({ | ||
functionality: "tool call parameter of non-primitive type" | ||
}); | ||
} | ||
parameterDefinitions[key] = { | ||
required: required ? required.includes(key) : false, | ||
type: type2, | ||
description | ||
}; | ||
} | ||
} | ||
} | ||
return { | ||
name: tool.name, | ||
description: tool.description, | ||
parameterDefinitions | ||
}; | ||
}); | ||
const toolChoice = mode.toolChoice; | ||
if (toolChoice == null) { | ||
return { tools: mappedTools, force_single_step: false }; | ||
} | ||
const type = toolChoice.type; | ||
switch (type) { | ||
case "auto": | ||
return { tools: mappedTools, force_single_step: false }; | ||
case "required": | ||
return { tools: mappedTools, force_single_step: true }; | ||
case "none": | ||
return { tools: void 0, force_single_step: false }; | ||
case "tool": | ||
return { | ||
tools: mappedTools.filter((tool) => tool.name === toolChoice.toolName), | ||
force_single_step: true | ||
}; | ||
default: { | ||
const _exhaustiveCheck = type; | ||
throw new import_provider2.UnsupportedFunctionalityError({ | ||
functionality: `Unsupported tool choice type: ${_exhaustiveCheck}` | ||
}); | ||
} | ||
} | ||
} | ||
@@ -367,0 +537,0 @@ // src/cohere-provider.ts |
@@ -85,5 +85,7 @@ "use strict"; | ||
case "tool-call": { | ||
throw new import_provider.UnsupportedFunctionalityError({ | ||
functionality: "tool-call" | ||
toolCalls.push({ | ||
name: part.toolName, | ||
parameters: part.args | ||
}); | ||
break; | ||
} | ||
@@ -104,5 +106,21 @@ default: { | ||
case "tool": { | ||
throw new import_provider.UnsupportedFunctionalityError({ | ||
functionality: "tool role" | ||
messages.push({ | ||
role: "TOOL", | ||
tool_results: content.map((toolResult) => ({ | ||
call: { | ||
name: toolResult.toolName, | ||
/* | ||
Note: Currently the tool_results field requires we pass the parameters of the tool results again. It it is blank for two reasons: | ||
1. The parameters are already present in chat_history as a tool message | ||
2. The tool core message of the ai sdk does not include parameters | ||
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. | ||
*/ | ||
parameters: {} | ||
}, | ||
outputs: [toolResult.result] | ||
})) | ||
}); | ||
break; | ||
} | ||
@@ -167,3 +185,3 @@ default: { | ||
const history = chatPrompt.slice(0, -1); | ||
const args = { | ||
const baseArgs = { | ||
// model id: | ||
@@ -186,2 +204,3 @@ model: this.modelId, | ||
chat_history: history, | ||
...(lastMessage == null ? void 0 : lastMessage.role) === "TOOL" ? { tool_results: lastMessage.tool_results } : {}, | ||
message: lastMessage ? lastMessage.role === "USER" ? lastMessage.message : void 0 : void 0 | ||
@@ -191,3 +210,3 @@ }; | ||
case "regular": { | ||
return args; | ||
return { ...baseArgs, ...prepareToolsAndToolChoice(mode) }; | ||
} | ||
@@ -224,4 +243,11 @@ case "object-json": { | ||
const { chat_history, message, ...rawSettings } = args; | ||
const generateId2 = this.config.generateId; | ||
return { | ||
text: response.text, | ||
toolCalls: response.tool_calls ? response.tool_calls.map((toolCall) => ({ | ||
toolCallId: generateId2(), | ||
toolName: toolCall.name, | ||
args: JSON.stringify(toolCall.parameters), | ||
toolCallType: "function" | ||
})) : [], | ||
finishReason: mapCohereFinishReason(response.finish_reason), | ||
@@ -265,2 +291,4 @@ usage: { | ||
}; | ||
const generateId2 = this.config.generateId; | ||
const toolCalls = []; | ||
return { | ||
@@ -285,2 +313,46 @@ stream: response.pipeThrough( | ||
} | ||
case "tool-calls-chunk": { | ||
if (value.tool_call_delta) { | ||
const { index } = value.tool_call_delta; | ||
if (toolCalls[index] === void 0) { | ||
const toolCallId = generateId2(); | ||
toolCalls[index] = { | ||
toolCallId, | ||
toolName: "" | ||
}; | ||
} | ||
if (value.tool_call_delta.name) { | ||
toolCalls[index].toolName = value.tool_call_delta.name; | ||
controller.enqueue({ | ||
type: "tool-call-delta", | ||
toolCallType: "function", | ||
toolCallId: toolCalls[index].toolCallId, | ||
toolName: toolCalls[index].toolName, | ||
argsTextDelta: "" | ||
}); | ||
} else if (value.tool_call_delta.parameters) { | ||
controller.enqueue({ | ||
type: "tool-call-delta", | ||
toolCallType: "function", | ||
toolCallId: toolCalls[index].toolCallId, | ||
toolName: toolCalls[index].toolName, | ||
argsTextDelta: value.tool_call_delta.parameters | ||
}); | ||
} | ||
} | ||
return; | ||
} | ||
case "tool-calls-generation": { | ||
for (let index = 0; index < value.tool_calls.length; index++) { | ||
const toolCall = value.tool_calls[index]; | ||
controller.enqueue({ | ||
type: "tool-call", | ||
toolCallId: toolCalls[index].toolCallId, | ||
toolName: toolCalls[index].toolName, | ||
toolCallType: "function", | ||
args: JSON.stringify(toolCall.parameters) | ||
}); | ||
} | ||
return; | ||
} | ||
case "stream-end": { | ||
@@ -322,2 +394,8 @@ finishReason = mapCohereFinishReason(value.finish_reason); | ||
text: import_zod2.z.string(), | ||
tool_calls: import_zod2.z.array( | ||
import_zod2.z.object({ | ||
name: import_zod2.z.string(), | ||
parameters: import_zod2.z.unknown({}) | ||
}) | ||
).optional(), | ||
finish_reason: import_zod2.z.string(), | ||
@@ -349,5 +427,20 @@ meta: import_zod2.z.object({ | ||
import_zod2.z.object({ | ||
event_type: import_zod2.z.literal("tool-calls-generation") | ||
event_type: import_zod2.z.literal("tool-calls-generation"), | ||
tool_calls: import_zod2.z.array( | ||
import_zod2.z.object({ | ||
name: import_zod2.z.string(), | ||
parameters: import_zod2.z.unknown({}) | ||
}) | ||
) | ||
}), | ||
import_zod2.z.object({ | ||
event_type: import_zod2.z.literal("tool-calls-chunk"), | ||
text: import_zod2.z.string().optional(), | ||
tool_call_delta: import_zod2.z.object({ | ||
index: import_zod2.z.number(), | ||
name: import_zod2.z.string().optional(), | ||
parameters: import_zod2.z.string().optional() | ||
}).optional() | ||
}), | ||
import_zod2.z.object({ | ||
event_type: import_zod2.z.literal("stream-end"), | ||
@@ -365,2 +458,79 @@ finish_reason: import_zod2.z.string(), | ||
]); | ||
function prepareToolsAndToolChoice(mode) { | ||
var _a; | ||
const tools = ((_a = mode.tools) == null ? void 0 : _a.length) ? mode.tools : void 0; | ||
if (tools == null) { | ||
return { tools: void 0 }; | ||
} | ||
const mappedTools = tools.map((tool) => { | ||
const { properties, required } = tool.parameters; | ||
const parameterDefinitions = {}; | ||
if (properties) { | ||
for (const [key, value] of Object.entries(properties)) { | ||
if (typeof value === "object" && value !== null) { | ||
const { type: JSONType, description } = value; | ||
let type2; | ||
if (typeof JSONType === "string") { | ||
switch (JSONType) { | ||
case "string": | ||
type2 = "str"; | ||
break; | ||
case "number": | ||
type2 = "float"; | ||
break; | ||
case "integer": | ||
type2 = "int"; | ||
break; | ||
case "boolean": | ||
type2 = "bool"; | ||
break; | ||
default: | ||
throw new import_provider2.UnsupportedFunctionalityError({ | ||
functionality: "tool call parameter of non-primitive type" | ||
}); | ||
} | ||
} else { | ||
throw new import_provider2.UnsupportedFunctionalityError({ | ||
functionality: "tool call parameter of non-primitive type" | ||
}); | ||
} | ||
parameterDefinitions[key] = { | ||
required: required ? required.includes(key) : false, | ||
type: type2, | ||
description | ||
}; | ||
} | ||
} | ||
} | ||
return { | ||
name: tool.name, | ||
description: tool.description, | ||
parameterDefinitions | ||
}; | ||
}); | ||
const toolChoice = mode.toolChoice; | ||
if (toolChoice == null) { | ||
return { tools: mappedTools, force_single_step: false }; | ||
} | ||
const type = toolChoice.type; | ||
switch (type) { | ||
case "auto": | ||
return { tools: mappedTools, force_single_step: false }; | ||
case "required": | ||
return { tools: mappedTools, force_single_step: true }; | ||
case "none": | ||
return { tools: void 0, force_single_step: false }; | ||
case "tool": | ||
return { | ||
tools: mappedTools.filter((tool) => tool.name === toolChoice.toolName), | ||
force_single_step: true | ||
}; | ||
default: { | ||
const _exhaustiveCheck = type; | ||
throw new import_provider2.UnsupportedFunctionalityError({ | ||
functionality: `Unsupported tool choice type: ${_exhaustiveCheck}` | ||
}); | ||
} | ||
} | ||
} | ||
@@ -367,0 +537,0 @@ // src/cohere-provider.ts |
{ | ||
"name": "@ai-sdk/cohere", | ||
"version": "0.0.17", | ||
"version": "0.0.18", | ||
"license": "Apache-2.0", | ||
@@ -21,4 +21,4 @@ "sideEffects": false, | ||
"dependencies": { | ||
"@ai-sdk/provider": "0.0.20", | ||
"@ai-sdk/provider-utils": "1.0.13" | ||
"@ai-sdk/provider": "0.0.21", | ||
"@ai-sdk/provider-utils": "1.0.14" | ||
}, | ||
@@ -25,0 +25,0 @@ "devDependencies": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
123418
1711
+ Added@ai-sdk/provider@0.0.21(transitive)
+ Added@ai-sdk/provider-utils@1.0.14(transitive)
- Removed@ai-sdk/provider@0.0.20(transitive)
- Removed@ai-sdk/provider-utils@1.0.13(transitive)
Updated@ai-sdk/provider@0.0.21