@ibm-cloud/watsonx-ai
Version:
IBM watsonx.ai Node.js SDK
75 lines • 2.62 kB
JavaScript
/**
* Converts a utility agent tool to a Watsonx text chat parameter tool format. This function
* transforms the tool definition from the utility agent format to the format expected by Watsonx
* text chat APIs.
*
* @example
* ```typescript
* const utilityTool = {
* name: 'calculator',
* description: 'Performs calculations',
* input_schema: { type: 'object', properties: { expression: { type: 'string' } } }
* };
* const watsonxTool = convertUtilityToolToWatsonxTool(utilityTool);
* ```;
*
* @param {WatsonXAI.UtilityAgentTool} utilityTool - The utility agent tool to convert
* @returns {WatsonXAI.TextChatParameterTools} The converted tool in Watsonx format
*/
function convertUtilityToolToWatsonxTool(utilityTool) {
// eslint-disable-next-line @typescript-eslint/naming-convention
const { name, description, input_schema } = utilityTool;
const parseParameters = (input) => {
if (input)
return input;
return {
properties: {
input: { type: 'string', description: 'Input for the tool' },
},
type: 'object',
};
};
const tool = {
type: 'function',
function: {
name,
description,
parameters: parseParameters(input_schema),
},
};
return tool;
}
/**
* Converts a Watsonx tool call to a utility agent tool run request. This function transforms the
* tool call from Watsonx text chat format to the format expected by the utility agent tools run
* API.
*
* @example
* ```typescript
* const toolCall = {
* id: '12345',
* type: 'function',
* function: {
* name: 'calculator',
* arguments: '{"input": "2 + 2"}'
* }
* };
* const runRequest = convertWatsonxToolCallToUtilityToolCall(toolCall);
* ```;
*
* @param {WatsonXAI.TextChatToolCall} toolCall - The Watsonx tool call to convert
* @param {WatsonXAI.JsonObject} [config] - Optional configuration for the tool run
* @returns {WatsonXAI.WxUtilityAgentToolsRunRequest} The converted tool run request
*/
function convertWatsonxToolCallToUtilityToolCall(toolCall, config) {
const { name, arguments: stringifiedArguments } = toolCall.function;
const jsonArguments = JSON.parse(stringifiedArguments);
const { input } = jsonArguments;
return {
input: input !== null && input !== void 0 ? input : jsonArguments,
tool_name: name,
config,
};
}
export { convertUtilityToolToWatsonxTool, convertWatsonxToolCallToUtilityToolCall };
//# sourceMappingURL=converters.mjs.map