Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
Mirrored from https://github.com/Camo67/composio.git
Synced Nov 21, 04:45 UTC
| 1 | /** |
| 2 | * Anthropic Messages API adapter. |
| 3 | */ |
| 4 | |
| 5 | import Anthropic from '@anthropic-ai/sdk'; |
| 6 | import { z } from 'zod'; |
| 7 | import { SHELL_TOOL, type ModelAdapter, type ModelTurn } from './agent'; |
| 8 | import { addTokens, ZERO_USAGE, type Usage } from './usage'; |
| 9 | |
| 10 | const ShellInput = z.object({ command: z.string() }); |
| 11 | |
| 12 | export const anthropicAdapter = (options: { |
| 13 | baseURL: string; |
| 14 | apiKey: string; |
| 15 | model: string; |
| 16 | system: string; |
| 17 | prompt: string; |
| 18 | maxTokens: number; |
| 19 | }): ModelAdapter => { |
| 20 | // No retries: a miss in strict replay, or a live failure while recording, must surface once. |
| 21 | const client = new Anthropic({ baseURL: options.baseURL, apiKey: options.apiKey, maxRetries: 0 }); |
| 22 | const messages: Anthropic.MessageParam[] = [{ role: 'user', content: options.prompt }]; |
| 23 | let usage: Usage = ZERO_USAGE; |
| 24 | |
| 25 | return { |
| 26 | provider: 'anthropic', |
| 27 | model: options.model, |
| 28 | next: async (): Promise<ModelTurn> => { |
| 29 | const response = await client.messages.create({ |
| 30 | model: options.model, |
| 31 | max_tokens: options.maxTokens, |
| 32 | // The run without the skill has no system prompt at all. |
| 33 | ...(options.system === '' ? {} : { system: options.system }), |
| 34 | tools: [ |
| 35 | { |
| 36 | name: SHELL_TOOL.name, |
| 37 | description: SHELL_TOOL.description, |
| 38 | input_schema: SHELL_TOOL.parameters, |
| 39 | }, |
| 40 | ], |
| 41 | messages, |
| 42 | }); |
| 43 | usage = addTokens(usage, options.model, { |
| 44 | // A replayed answer may carry no usage at all. |
| 45 | input: response.usage?.input_tokens ?? 0, |
| 46 | output: response.usage?.output_tokens ?? 0, |
| 47 | cacheRead: response.usage?.cache_read_input_tokens ?? 0, |
| 48 | cacheWrite: response.usage?.cache_creation_input_tokens ?? 0, |
| 49 | }); |
| 50 | // The whole content goes back unchanged, thinking blocks included. |
| 51 | messages.push({ role: 'assistant', content: response.content }); |
| 52 | |
| 53 | const text = response.content |
| 54 | .filter(block => block.type === 'text') |
| 55 | .map(block => block.text) |
| 56 | .join('\n'); |
| 57 | const calls = response.content |
| 58 | .filter(block => block.type === 'tool_use') |
| 59 | .map(block => ({ id: block.id, command: ShellInput.parse(block.input).command })); |
| 60 | |
| 61 | const continues = response.stop_reason === 'tool_use' || response.stop_reason === 'end_turn'; |
| 62 | return { text, calls, stopped: continues ? undefined : (response.stop_reason ?? 'unknown') }; |
| 63 | }, |
| 64 | record: results => { |
| 65 | messages.push({ |
| 66 | role: 'user', |
| 67 | content: results.map(({ call, payload }) => ({ |
| 68 | type: 'tool_result' as const, |
| 69 | tool_use_id: call.id, |
| 70 | content: payload, |
| 71 | })), |
| 72 | }); |
| 73 | }, |
| 74 | say: message => { |
| 75 | messages.push({ role: 'user', content: message }); |
| 76 | }, |
| 77 | usage: () => usage, |
| 78 | }; |
| 79 | }; |