UNPKG

@robota-sdk/anthropic

Version:

Anthropic Claude integration for Robota SDK - Claude 3, Claude 2, function calling, and tool integration with Anthropic's API

167 lines (157 loc) 4.61 kB
import '@robota-sdk/core'; // src/provider.ts // src/adapter.ts var AnthropicConversationAdapter = class { /** * Convert UniversalMessage array to Anthropic Messages API format * * @param messages - Array of universal messages * @returns Array of messages in Anthropic Messages API format */ static toAnthropicMessages(messages) { const anthropicMessages = []; for (const message of messages) { const messageRole = message.role; if (messageRole === "system") { continue; } if (messageRole === "user") { const userMsg = message; anthropicMessages.push({ role: "user", content: userMsg.content }); } else if (messageRole === "assistant") { const assistantMsg = message; let content = assistantMsg.content || ""; if (assistantMsg.toolCalls) { const toolCalls = assistantMsg.toolCalls; for (const tc of toolCalls) { content += ` Tool Call: ${tc.function.name}(${tc.function.arguments})`; } } anthropicMessages.push({ role: "assistant", content }); } else if (messageRole === "tool") { const toolMsg = message; anthropicMessages.push({ role: "user", content: `[Tool Result from ${toolMsg.name}]: ${toolMsg.content}` }); } } return anthropicMessages; } /** * Convert UniversalMessage array to Anthropic prompt format (legacy) */ static toAnthropicPrompt(messages, systemPrompt) { let prompt = ""; const finalSystemPrompt = this.extractSystemPrompt(messages, systemPrompt); if (finalSystemPrompt) { prompt += finalSystemPrompt + "\n\n"; } for (const message of messages) { const messageRole = message.role; if (messageRole === "system") { continue; } if (messageRole === "user") { const userMsg = message; prompt += ` Human: ${userMsg.content}`; } else if (messageRole === "assistant") { const assistantMsg = message; let content = assistantMsg.content || ""; if (assistantMsg.toolCalls) { const toolCalls = assistantMsg.toolCalls; for (const tc of toolCalls) { content += ` Tool Call: ${tc.function.name}(${tc.function.arguments})`; } } prompt += ` Assistant: ${content}`; } else if (messageRole === "tool") { const toolMsg = message; prompt += ` Human: [Tool Result from ${toolMsg.name}]: ${toolMsg.content}`; } } if (messages.length > 0) { const lastMessage = messages[messages.length - 1]; if (lastMessage.role === "user") { prompt += "\n\nAssistant:"; } } return prompt; } /** * Extract system messages and combine them as system prompt */ static extractSystemPrompt(messages, fallbackSystemPrompt) { const systemMessages = messages.filter((msg) => msg.role === "system"); if (systemMessages.length > 0) { return systemMessages.map((msg) => msg.content).join("\n\n"); } return fallbackSystemPrompt; } /** * Helper for message conversion testing (converts each message individually) */ static convertMessage(msg) { const messageRole = msg.role; if (messageRole === "user") { const userMsg = msg; return { role: "human", content: userMsg.content }; } if (messageRole === "assistant") { const assistantMsg = msg; let content = assistantMsg.content || ""; if (assistantMsg.toolCalls) { const toolCalls = assistantMsg.toolCalls; for (const tc of toolCalls) { content += ` Tool Call: ${tc.function.name}(${tc.function.arguments})`; } } return { role: "assistant", content }; } if (messageRole === "tool") { const toolMsg = msg; return { role: "human", content: `[Tool Result from ${toolMsg.name}]: ${toolMsg.content}` }; } if (messageRole === "system") { const systemMsg = msg; return { role: "system", content: systemMsg.content }; } const _exhaustiveCheck = msg; return _exhaustiveCheck; } }; // src/index.ts var AnthropicProvider = class { constructor(_options) { } // To be implemented }; function createAnthropicProvider(_options) { } export { AnthropicConversationAdapter, AnthropicProvider, createAnthropicProvider }; //# sourceMappingURL=index.js.map //# sourceMappingURL=index.js.map