@robota-sdk/anthropic
Version:
Anthropic Claude integration for Robota SDK - Claude 3, Claude 2, function calling, and tool integration with Anthropic's API
1 lines • 3.17 kB
JavaScript
import l from'@anthropic-ai/sdk';import {BaseAIProvider}from'@robota-sdk/agents';var n=class extends BaseAIProvider{name="anthropic";version="1.0.0";client;options;constructor(t){if(super(),this.options=t,t.client)this.client=t.client;else if(t.apiKey)this.client=new l({apiKey:t.apiKey,...t.timeout&&{timeout:t.timeout},...t.baseURL&&{baseURL:t.baseURL}});else throw new Error("Either Anthropic client or apiKey is required")}async chat(t,e){this.validateMessages(t);let s=this.convertToAnthropicFormat(t);if(!e?.model)throw new Error("Model is required in ChatOptions. Please specify a model in defaultModel configuration.");let o={model:e.model,messages:s,max_tokens:e?.maxTokens||4096};e?.temperature!==void 0&&(o.temperature=e.temperature),e?.tools&&(o.tools=this.convertToolsToAnthropicFormat(e.tools));let r=await this.client.messages.create(o);return this.convertFromAnthropicResponse(r)}async*chatStream(t,e){this.validateMessages(t);let s=this.convertToAnthropicFormat(t);if(!e?.model)throw new Error("Model is required in ChatOptions. Please specify a model in defaultModel configuration.");let o={model:e.model,messages:s,max_tokens:e?.maxTokens||4096,stream:true};e?.temperature!==void 0&&(o.temperature=e.temperature),e?.tools&&(o.tools=this.convertToolsToAnthropicFormat(e.tools));let r=await this.client.messages.create(o);for await(let a of r)a.type==="content_block_delta"&&a.delta.type==="text_delta"&&(yield {role:"assistant",content:a.delta.text,timestamp:new Date});}supportsTools(){return true}validateConfig(){return !!this.client&&!!this.options&&!!this.options.apiKey}async dispose(){}convertToAnthropicFormat(t){return t.map(e=>{if(e.role==="user")return {role:"user",content:e.content||""};if(e.role==="assistant"){let s=e;return s.toolCalls&&s.toolCalls.length>0?{role:"assistant",content:null,tool_calls:s.toolCalls.map(o=>({id:o.id,type:"function",function:{name:o.function.name,arguments:JSON.stringify(o.function.arguments)}}))}:{role:"assistant",content:s.content||""}}else return {role:"user",content:e.content||""}})}convertFromAnthropicResponse(t){if(!t.content||t.content.length===0)throw new Error("No content in Anthropic response");let e=t.content[0];if(e&&e.type==="text"){let o={role:"assistant",content:e.text,timestamp:new Date};return t.usage&&(o.metadata={inputTokens:t.usage.input_tokens,outputTokens:t.usage.output_tokens,model:t.model},t.stop_reason&&(o.metadata.stopReason=t.stop_reason)),o}else if(e&&e.type==="tool_use"){let s=e;return {role:"assistant",content:"",timestamp:new Date,toolCalls:[{id:s.id,type:"function",function:{name:s.name,arguments:JSON.stringify(s.input)}}]}}throw new Error(`Unsupported content type: ${e.type}`)}convertToolsToAnthropicFormat(t){return t.map(e=>({name:e.name,description:e.description,input_schema:e.parameters}))}validateMessages(t){if(!Array.isArray(t))throw new Error("Messages must be an array");if(t.length===0)throw new Error("Messages array cannot be empty");for(let e of t)if(!e.role||!["user","assistant","system","tool"].includes(e.role))throw new Error(`Invalid message role: ${e.role}`)}};function u(i){}export{n as AnthropicProvider,u as createAnthropicProvider};