UNPKG

@nomyx/assistant

Version:

A powerful assistant library and cli for your AI projects. works with Vertex AI (Claude and Gemini)

56 lines (52 loc) 1.86 kB
import { ChatMessage } from '../../../types/chat'; import { GenericToolSchema, StandardizedToolCall } from '../../../types/tool'; import { AnthropicMessage, AnthropicToolSchema, AnthropicMessageContent } from './types'; export function convertToolSchema(schema: GenericToolSchema): AnthropicToolSchema { return { name: schema.name, description: schema.description, input_schema: { type: 'object', properties: Object.entries(schema.parameters.properties).reduce((acc: any, [key, value]) => { acc[key] = { type: value.type, ...(value.enum ? { enum: value.enum } : {}) }; if(value.description) { acc[key].description = value.description; } return acc; }, {} as { [key: string]: { type: string; description: string; enum?: string[] } }), required: schema.parameters.required || [] } }; } export function convertToolCall(toolUse: any): StandardizedToolCall { return { name: toolUse.name, arguments: toolUse.input }; } export function convertMessages(messages: ChatMessage[]): AnthropicMessage[] { return messages.map(msg => { if (msg.role === 'function') { // Need to use assistant because Anthropic doesn't have a 'system' role return { role: 'assistant', content: `Function ${msg.name} returned: ${msg.content}` }; } return { role: msg.role as 'user' | 'assistant' | 'system', content: msg.content }; }); } export function extractToolCalls(content: string | AnthropicMessageContent[]): StandardizedToolCall[] { if (typeof content === 'string') { return []; // No tool calls in string content } return content .filter((item): item is AnthropicMessageContent & { type: 'tool_use' } => item.type === 'tool_use') .map(item => convertToolCall(item)); }