@nomyx/assistant
Version:
A powerful assistant library and cli for your AI projects. works with Vertex AI (Claude and Gemini)
122 lines (121 loc) • 4.43 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.convertToolSchema = convertToolSchema;
exports.convertToolCall = convertToolCall;
exports.convertMessages = convertMessages;
exports.extractToolCalls = extractToolCalls;
const types_1 = require("./types");
function convertToolSchema(schema, modelType) {
if (modelType === types_1.VertexModel.GEMINI_PRO || modelType === types_1.VertexModel.GEMINI_PRO_VISION) {
return convertGeminiToolSchema(schema);
}
else if (modelType === types_1.VertexModel.CLAUDE_3_SONNET || modelType === types_1.VertexModel.CLAUDE_3_OPUS) {
return convertClaudeToolSchema(schema);
}
else {
throw new Error(`Unsupported model type for tool schema conversion: ${modelType}`);
}
}
function convertGeminiToolSchema(schema) {
return {
name: schema.name,
description: schema.description,
parameters: {
type: 'object',
properties: Object.entries(schema.parameters.properties).reduce((acc, [key, value]) => {
acc[key] = {
type: value.type,
description: value.description || '',
...(value.enum ? { enum: value.enum } : {})
};
return acc;
}, {}),
required: schema.parameters.required || []
}
};
}
function convertClaudeToolSchema(schema) {
return {
name: schema.name,
description: schema.description,
parameters: {
type: 'object',
properties: Object.entries(schema.parameters.properties).reduce((acc, [key, value]) => {
acc[key] = {
type: value.type,
description: value.description || '',
...(value.enum ? { enum: value.enum } : {})
};
return acc;
}, {}),
required: schema.parameters.required || []
}
};
}
function convertToolCall(toolCall, modelType) {
if (modelType === types_1.VertexModel.GEMINI_PRO || modelType === types_1.VertexModel.GEMINI_PRO_VISION) {
return convertGeminiToolCall(toolCall);
}
else if (modelType === types_1.VertexModel.CLAUDE_3_SONNET || modelType === types_1.VertexModel.CLAUDE_3_OPUS) {
return convertClaudeToolCall(toolCall);
}
else {
throw new Error(`Unsupported model type for tool call conversion: ${modelType}`);
}
}
function convertGeminiToolCall(toolCall) {
return {
name: toolCall.name,
arguments: JSON.parse(toolCall.args)
};
}
function convertClaudeToolCall(toolCall) {
return {
name: toolCall.name,
arguments: toolCall.input
};
}
function convertMessages(messages, modelType) {
if (modelType === types_1.VertexModel.GEMINI_PRO || modelType === types_1.VertexModel.GEMINI_PRO_VISION) {
return convertGeminiMessages(messages);
}
else if (modelType === types_1.VertexModel.CLAUDE_3_SONNET || modelType === types_1.VertexModel.CLAUDE_3_OPUS) {
return convertClaudeMessages(messages);
}
else {
throw new Error(`Unsupported model type for message conversion: ${modelType}`);
}
}
function convertGeminiMessages(messages) {
return messages.map(msg => ({
author: msg.role === 'user' ? 'user' : 'bot',
content: msg.content
}));
}
function convertClaudeMessages(messages) {
return messages.map(msg => ({
role: msg.role,
content: msg.content
}));
}
function extractToolCalls(content, modelType) {
if (modelType === types_1.VertexModel.GEMINI_PRO || modelType === types_1.VertexModel.GEMINI_PRO_VISION) {
return extractGeminiToolCalls(content);
}
else if (modelType === types_1.VertexModel.CLAUDE_3_SONNET || modelType === types_1.VertexModel.CLAUDE_3_OPUS) {
return extractClaudeToolCalls(content);
}
else {
throw new Error(`Unsupported model type for extracting tool calls: ${modelType}`);
}
}
function extractGeminiToolCalls(content) {
// Implement Gemini-specific tool call extraction logic
// This is a placeholder and should be updated based on Gemini's actual format
return [];
}
function extractClaudeToolCalls(content) {
return content
.filter(item => item.type === 'tool_use')
.map(item => convertClaudeToolCall(item.tool_use));
}