/** * Message Format Translation * * Translates messages between AgentRouter's internal format and provider-specific formats. * Handles the differences in how each provider represents: * - System prompts * - Message content (text, tool use, tool results) * - Role naming conventions * * Format Reference: * | Feature | Anthropic | OpenAI | Gemini | * |--------------|------------------------|------------------------|---------------------| * | System | separate param | role: "system" message | systemInstruction | * | Content | content blocks | string or array | parts array | * | Tool use | tool_use block | tool_calls array | functionCall | * | Tool result | tool_result block | role: "tool" message | functionResponse | */ import type { Message, ContentBlock } from '../types.js'; /** * Anthropic message format */ export interface AnthropicMessage { role: 'user' | 'assistant'; content: string | AnthropicContentBlock[]; } /** * Anthropic content block */ export interface AnthropicContentBlock { type: 'text' | 'tool_use' | 'tool_result'; text?: string; id?: string; name?: string; input?: Record; tool_use_id?: string; content?: string; } /** * Anthropic API response format */ export interface AnthropicResponse { id: string; type: 'message'; role: 'assistant'; content: AnthropicContentBlock[]; model: string; stop_reason: 'end_turn' | 'max_tokens' | 'stop_sequence' | 'tool_use'; stop_sequence?: string | null; usage: { input_tokens: number; output_tokens: number; cache_creation_input_tokens?: number; cache_read_input_tokens?: number; }; } /** * Anthropic request format (for sending to API) */ export interface AnthropicRequest { model: string; messages: AnthropicMessage[]; system?: string; max_tokens: number; temperature?: number; tools?: AnthropicTool[]; stream?: boolean; } export interface AnthropicTool { name: string; description: string; input_schema: { type: 'object'; properties: Record; required?: string[]; }; } /** * OpenAI message format */ export interface OpenAIMessage { role: 'system' | 'user' | 'assistant' | 'tool'; content: string | OpenAIContentPart[] | null; tool_calls?: OpenAIToolCall[]; tool_call_id?: string; name?: string; } /** * OpenAI content part for multi-modal messages */ export interface OpenAIContentPart { type: 'text' | 'image_url'; text?: string; image_url?: { url: string; detail?: 'auto' | 'low' | 'high'; }; } /** * OpenAI tool call format */ export interface OpenAIToolCall { id: string; type: 'function'; function: { name: string; arguments: string; }; } /** * OpenAI API response format */ export interface OpenAIResponse { id: string; object: 'chat.completion'; created: number; model: string; choices: { index: number; message: OpenAIMessage; finish_reason: 'stop' | 'length' | 'tool_calls' | 'content_filter'; }[]; usage: { prompt_tokens: number; completion_tokens: number; total_tokens: number; }; } /** * Gemini content format */ export interface GeminiContent { role: 'user' | 'model'; parts: GeminiPart[]; } /** * Gemini part format (can be text, function call, or function response) */ export interface GeminiPart { text?: string; functionCall?: { name: string; args: Record; }; functionResponse?: { name: string; response: Record; }; } /** * Gemini API response format */ export interface GeminiResponse { candidates: { content: GeminiContent; finishReason: 'STOP' | 'MAX_TOKENS' | 'SAFETY' | 'RECITATION' | 'OTHER'; safetyRatings?: { category: string; probability: string; }[]; }[]; promptFeedback?: { safetyRatings?: { category: string; probability: string; }[]; }; usageMetadata?: { promptTokenCount: number; candidatesTokenCount: number; totalTokenCount: number; }; } /** * Gemini request format */ export interface GeminiRequest { contents: GeminiContent[]; systemInstruction?: { parts: GeminiPart[]; }; generationConfig?: { temperature?: number; maxOutputTokens?: number; topP?: number; topK?: number; }; tools?: GeminiTool[]; } export interface GeminiTool { functionDeclarations: GeminiFunctionDeclaration[]; } export interface GeminiFunctionDeclaration { name: string; description: string; parameters: GeminiSchema; } export interface GeminiSchema { type: string; description?: string; properties?: Record; required?: string[]; items?: GeminiSchema; enum?: string[]; } /** * Convert internal messages to Anthropic format * * Anthropic: * - System prompt is a separate top-level parameter * - Messages use 'user' | 'assistant' roles * - Content can be string or ContentBlock[] * - Tool results go in user messages with tool_result blocks * * @param messages - Internal Message array * @param systemPrompt - Optional system prompt (becomes separate param in Anthropic) * @returns Anthropic-formatted messages (system prompt handled separately by caller) */ export declare function toAnthropicMessages(messages: Message[], systemPrompt?: string): { messages: AnthropicMessage[]; system?: string; }; /** * Convert internal messages to OpenAI format * * OpenAI: * - System prompt is a message with role: "system" * - Tool use is in assistant message's tool_calls array * - Tool results are separate messages with role: "tool" * * @param messages - Internal Message array * @param systemPrompt - Optional system prompt (becomes first message) * @returns OpenAI-formatted messages */ export declare function toOpenAIMessages(messages: Message[], systemPrompt?: string): OpenAIMessage[]; /** * Convert internal messages to Gemini format * * Gemini: * - System prompt goes in systemInstruction * - Roles are 'user' | 'model' (not 'assistant') * - Content is always an array of parts * - Function calls and responses are specific part types * * @param messages - Internal Message array * @param systemPrompt - Optional system prompt (becomes systemInstruction) * @returns Gemini-formatted contents and system instruction */ export declare function toGeminiContents(messages: Message[], systemPrompt?: string): { contents: GeminiContent[]; systemInstruction?: { parts: GeminiPart[]; }; }; /** * Convert Anthropic response to internal Message format * * @param response - Anthropic API response * @returns Internal Message */ export declare function fromAnthropicResponse(response: AnthropicResponse): Message; /** * Convert OpenAI response to internal Message format * * @param response - OpenAI API response * @returns Internal Message */ export declare function fromOpenAIResponse(response: OpenAIResponse): Message; /** * Convert Gemini response to internal Message format * * @param response - Gemini API response * @returns Internal Message */ export declare function fromGeminiResponse(response: GeminiResponse): Message; /** * Extract text content from a Message */ export declare function extractTextContent(message: Message): string; /** * Check if a message contains tool use */ export declare function hasToolUse(message: Message): boolean; /** * Check if a message contains tool results */ export declare function hasToolResult(message: Message): boolean; /** * Get all tool use blocks from a message */ export declare function getToolUseBlocks(message: Message): ContentBlock[]; /** * Get all tool result blocks from a message */ export declare function getToolResultBlocks(message: Message): ContentBlock[]; //# sourceMappingURL=messages.d.ts.map