@nomyx/assistant
Version:
A powerful assistant library and cli for your AI projects. works with Vertex AI (Claude and Gemini)
72 lines (71 loc) • 2.37 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.convertToolSchema = convertToolSchema;
exports.convertToolCall = convertToolCall;
exports.convertToolCalls = convertToolCalls;
function convertToolSchema(schema) {
return {
name: schema.name,
description: schema.description,
parameters: {
type: 'object',
properties: convertParameters(schema.parameters.properties),
required: schema.parameters.required,
},
};
}
function convertParameters(params) {
const result = {};
for (const [key, value] of Object.entries(params)) {
result[key] = convertParameterDefinition(value);
}
return result;
}
function convertParameterDefinition(param) {
const result = { type: param.type };
if (param.description)
result.description = param.description;
if (param.enum)
result.enum = param.enum;
if (param.items)
result.items = convertParameterDefinition(param.items);
if (param.properties)
result.properties = convertParameters(param.properties);
if (param.required)
result.required = param.required;
return result;
}
function convertToolCall(call) {
try {
const convertedCall = {
name: call.function.name,
arguments: JSON.parse(call.function.arguments || '{}'),
};
validateToolCall(convertedCall);
return convertedCall;
}
catch (error) {
throw new Error(`Failed to convert tool call: ${error instanceof Error ? error.message : String(error)}`);
}
}
function convertToolCalls(toolCalls) {
if (!toolCalls || toolCalls.length === 0)
return [];
return toolCalls.map(call => {
try {
return convertToolCall(call);
}
catch (error) {
console.error('Error converting individual tool call', { call, error });
return null;
}
}).filter((call) => call !== null);
}
function validateToolCall(toolCall) {
if (typeof toolCall.name !== 'string' || toolCall.name.trim() === '') {
throw new Error('Invalid tool call: name must be a non-empty string');
}
if (typeof toolCall.arguments !== 'object' || toolCall.arguments === null) {
throw new Error('Invalid tool call: arguments must be an object');
}
}