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 23, 01:45 UTC
feat: Add Anthropic provider implementation with tests and example
Co-Authored-By: musthaq@composio.dev <musthu.gm@gmail.com>
Devin AI committed
May 28, 2025 at 05:13 UTC
afd96e1d36e38d0501e5d610cfaec282dab3d837
8 files changed
+816
examples/anthropic/package.json
new
+25
@@ -0,0 +1,25 @@
1
+{
2
+ "name": "anthropic-example",
3
+ "private": true,
4
+ "version": "0.1.0",
5
+ "description": "Example for using Anthropic provider with Composio SDK",
6
+ "main": "index.js",
7
+ "scripts": {
8
+ "start": "bun src/index.ts",
9
+ "test": "echo \"Error: no test specified\" && exit 1"
10
+ },
11
+ "keywords": [],
12
+ "author": "",
13
+ "license": "ISC",
14
+ "packageManager": "pnpm@10.8.0",
15
+ "dependencies": {
16
+ "@anthropic-ai/sdk": "^0.18.0",
17
+ "@composio/core": "workspace:*",
18
+ "@composio/anthropic": "workspace:*",
19
+ "dotenv": "^16.4.1"
20
+ },
21
+ "devDependencies": {
22
+ "@types/bun": "^1.2.9",
23
+ "typescript": "^5.8.3"
24
+ }
25
+}
examples/anthropic/src/index.ts
new
+115
@@ -0,0 +1,115 @@
1
+/**
2
+ * Example for using Anthropic provider with Composio SDK
3
+ */
4
+import { Composio } from '@composio/core';
5
+import { AnthropicProvider } from '@composio/anthropic';
6
+import Anthropic from '@anthropic-ai/sdk';
7
+import 'dotenv/config';
8
+
9
+if (!process.env.COMPOSIO_API_KEY) {
10
+ console.error('Missing COMPOSIO_API_KEY environment variable');
11
+ process.exit(1);
12
+}
13
+
14
+if (!process.env.ANTHROPIC_API_KEY) {
15
+ console.error('Missing ANTHROPIC_API_KEY environment variable');
16
+ process.exit(1);
17
+}
18
+
19
+const anthropic = new Anthropic({
20
+ apiKey: process.env.ANTHROPIC_API_KEY,
21
+});
22
+
23
+const composio = new Composio({
24
+ apiKey: process.env.COMPOSIO_API_KEY,
25
+ provider: new AnthropicProvider(),
26
+});
27
+
28
+async function main() {
29
+ try {
30
+ console.log('Fetching tools from Composio...');
31
+ const tools = await composio.tools.get('default', 'HACKERNEWS_GET_USER');
32
+ console.log(`Fetched ${tools.length} tools`);
33
+
34
+ console.log('Creating message with Anthropic...');
35
+ const message = await anthropic.messages.create({
36
+ model: 'claude-3-sonnet-20240229',
37
+ max_tokens: 1024,
38
+ tools: tools,
39
+ messages: [
40
+ {
41
+ role: 'user',
42
+ content: "Fetch the details of the user 'pg' from Hacker News and summarize their profile",
43
+ },
44
+ ],
45
+ });
46
+
47
+ console.log('Message created, processing tool calls if any...');
48
+
49
+ const toolUseBlocks = message.content.filter(
50
+ (content: any) => content.type === 'tool_use'
51
+ );
52
+
53
+ if (toolUseBlocks.length > 0) {
54
+ console.log(`Found ${toolUseBlocks.length} tool calls`);
55
+
56
+ const toolResults = await composio.provider.handleToolCalls(
57
+ 'default',
58
+ message
59
+ );
60
+
61
+ console.log('Tool results:', toolResults);
62
+
63
+ const followUpMessage = await anthropic.messages.create({
64
+ model: 'claude-3-sonnet-20240229',
65
+ max_tokens: 1024,
66
+ messages: [
67
+ {
68
+ role: 'user',
69
+ content: "Fetch the details of the user 'pg' from Hacker News and summarize their profile",
70
+ },
71
+ {
72
+ role: 'assistant',
73
+ content: [
74
+ {
75
+ type: 'text',
76
+ text: "I'll fetch the details for the Hacker News user 'pg'.",
77
+ },
78
+ ...toolUseBlocks,
79
+ ],
80
+ },
81
+ {
82
+ role: 'user',
83
+ content: [
84
+ {
85
+ type: 'tool_result',
86
+ tool_use_id: toolUseBlocks[0].id,
87
+ content: toolResults[0],
88
+ },
89
+ ],
90
+ },
91
+ ],
92
+ });
93
+
94
+ const finalResponse = followUpMessage.content
95
+ .filter((content: any) => content.type === 'text')
96
+ .map((content: any) => content.text)
97
+ .join('\n');
98
+
99
+ console.log('Final response:');
100
+ console.log(finalResponse);
101
+ } else {
102
+ const response = message.content
103
+ .filter((content: any) => content.type === 'text')
104
+ .map((content: any) => content.text)
105
+ .join('\n');
106
+
107
+ console.log('Response (no tool calls):');
108
+ console.log(response);
109
+ }
110
+ } catch (error) {
111
+ console.error('Error:', error);
112
+ }
113
+}
114
+
115
+main();
packages/providers/anthropic/README.md
new
+89
@@ -0,0 +1,89 @@
1
+# @composio/anthropic
2
+
3
+Agentic Provider for Anthropic in Composio SDK.
4
+
5
+## Features
6
+
7
+- **Full Modifier Support**: Support for both schema and execution modifiers
8
+- **Tool Execution**: Execute tools with proper parameter handling
9
+- **Type Safety**: Full TypeScript support with proper type definitions
10
+
11
+## Installation
12
+
13
+```bash
14
+npm install @composio/anthropic
15
+# or
16
+yarn add @composio/anthropic
17
+# or
18
+pnpm add @composio/anthropic
19
+```
20
+
21
+## Quick Start
22
+
23
+```typescript
24
+import { Composio } from '@composio/core';
25
+import { AnthropicProvider } from '@composio/anthropic';
26
+
27
+// Initialize Composio with Anthropic provider
28
+const composio = new Composio({
29
+ apiKey: 'your-composio-api-key',
30
+ provider: new AnthropicProvider(),
31
+});
32
+
33
+// Get available tools
34
+const tools = await composio.tools.get('user123', {
35
+ toolkits: ['gmail', 'googlecalendar'],
36
+ limit: 10,
37
+});
38
+```
39
+
40
+## Usage Examples
41
+
42
+### Basic Example
43
+
44
+```typescript
45
+import { Composio } from '@composio/core';
46
+import { AnthropicProvider } from '@composio/anthropic';
47
+
48
+// Initialize Composio
49
+const composio = new Composio({
50
+ apiKey: process.env.COMPOSIO_API_KEY,
51
+ provider: new AnthropicProvider(),
52
+});
53
+
54
+// Get tools
55
+const tools = await composio.tools.get('user123', {
56
+ toolkits: ['gmail'],
57
+});
58
+
59
+// Use tools with Anthropic
60
+// Add your usage example here
61
+```
62
+
63
+## API Reference
64
+
65
+### AnthropicProvider Class
66
+
67
+The `AnthropicProvider` class extends `BaseAgenticProvider` and provides anthropic-specific functionality.
68
+
69
+#### Methods
70
+
71
+##### `wrapTool(tool: Tool, executeTool: ExecuteToolFn): AnthropicTool`
72
+
73
+Wraps a tool in the anthropic format.
74
+
75
+```typescript
76
+const tool = provider.wrapTool(composioTool, executeTool);
77
+```
78
+
79
+## Contributing
80
+
81
+We welcome contributions! Please see our [Contributing Guide](../../CONTRIBUTING.md) for more details.
82
+
83
+## License
84
+
85
+ISC License
86
+
87
+## Support
88
+
89
+For support, please visit our [Documentation](https://docs.composio.dev) or join our [Discord Community](https://discord.gg/composio).
packages/providers/anthropic/package.json
new
+43
@@ -0,0 +1,43 @@
1
+{
2
+ "name": "@composio/anthropic",
3
+ "version": "0.1.0",
4
+ "description": "Non-Agentic Provider for Anthropic in Composio SDK",
5
+ "main": "src/index.ts",
6
+ "type": "module",
7
+ "publishConfig": {
8
+ "access": "public",
9
+ "main": "dist/index.js",
10
+ "types": "dist/index.d.ts"
11
+ },
12
+ "exports": {
13
+ ".": {
14
+ "import": "./dist/index.js",
15
+ "types": "./dist/index.d.ts",
16
+ "require": "./dist/index.cjs"
17
+ }
18
+ },
19
+ "files": [
20
+ "README.md",
21
+ "dist"
22
+ ],
23
+ "scripts": {
24
+ "build": "tsup",
25
+ "test": "vitest run"
26
+ },
27
+ "keywords": ["composio", "provider", "anthropic"],
28
+ "author": "",
29
+ "license": "ISC",
30
+ "packageManager": "pnpm@10.8.0",
31
+ "dependencies": {
32
+ "@anthropic-ai/sdk": "^0.18.0"
33
+ },
34
+ "peerDependencies": {
35
+ "@composio/core": "^0.1.0"
36
+ },
37
+ "devDependencies": {
38
+ "@composio/core": "workspace:*",
39
+ "tsup": "^8.4.0",
40
+ "typescript": "^5.8.3",
41
+ "vitest": "^1.4.0"
42
+ }
43
+}
packages/providers/anthropic/src/index.ts
new
+159
@@ -0,0 +1,159 @@
1
+/**
2
+ * Anthropic Provider
3
+ *
4
+ * This provider provides a set of tools for interacting with Anthropic's API.
5
+ * It implements the non-agentic provider interface for Anthropic's Claude models.
6
+ *
7
+ * @packageDocumentation
8
+ * @module providers/anthropic
9
+ */
10
+import {
11
+ BaseNonAgenticProvider,
12
+ Tool,
13
+ ExecuteToolModifiers,
14
+ ExecuteToolFnOptions,
15
+ ToolExecuteParams
16
+} from '@composio/core';
17
+import Anthropic from '@anthropic-ai/sdk';
18
+
19
+/**
20
+ * Anthropic tool format based on their API docs
21
+ */
22
+export interface AnthropicTool {
23
+ name: string;
24
+ description: string;
25
+ input_schema: {
26
+ type: string;
27
+ properties: Record<string, unknown>;
28
+ required: string[];
29
+ };
30
+}
31
+
32
+/**
33
+ * Collection of Anthropic tools
34
+ */
35
+export type AnthropicToolCollection = AnthropicTool[];
36
+
37
+/**
38
+ * Type for Anthropic tool use block in message content
39
+ */
40
+export interface AnthropicToolUseBlock {
41
+ type: 'tool_use';
42
+ id: string;
43
+ name: string;
44
+ input: Record<string, unknown>;
45
+}
46
+
47
+/**
48
+ * Type for Anthropic message content block
49
+ */
50
+export type AnthropicContentBlock = {
51
+ type: string;
52
+ [key: string]: unknown;
53
+};
54
+
55
+/**
56
+ * Anthropic Provider implementation for Composio
57
+ */
58
+export class AnthropicProvider extends BaseNonAgenticProvider<AnthropicToolCollection, AnthropicTool> {
59
+ readonly name = 'anthropic';
60
+
61
+ /**
62
+ * Wrap a tool in the Anthropic format.
63
+ * @param tool - The tool to wrap.
64
+ * @returns The wrapped tool in Anthropic format.
65
+ */
66
+ override wrapTool(tool: Tool): AnthropicTool {
67
+ const properties = tool.inputParameters?.properties || {};
68
+ const required = tool.inputParameters?.required || [];
69
+
70
+ return {
71
+ name: tool.slug,
72
+ description: tool.description || '',
73
+ input_schema: {
74
+ type: 'object',
75
+ properties: properties as Record<string, unknown>,
76
+ required: required as string[],
77
+ },
78
+ };
79
+ }
80
+
81
+ /**
82
+ * Wrap a list of tools in the Anthropic format.
83
+ * @param tools - The tools to wrap.
84
+ * @returns The wrapped tools in Anthropic format.
85
+ */
86
+ override wrapTools(tools: Tool[]): AnthropicToolCollection {
87
+ return tools.map(tool => this.wrapTool(tool));
88
+ }
89
+
90
+ /**
91
+ * Execute a tool call from Anthropic.
92
+ * @param userId - The user ID
93
+ * @param toolUse - The tool use object from Anthropic
94
+ * @param options - Additional options for tool execution
95
+ * @param modifiers - Modifiers for tool execution
96
+ * @returns The result of the tool execution as a string
97
+ */
98
+ async executeToolCall(
99
+ userId: string,
100
+ toolUse: AnthropicToolUseBlock,
101
+ options?: ExecuteToolFnOptions,
102
+ modifiers?: ExecuteToolModifiers
103
+ ): Promise<string> {
104
+ const payload: ToolExecuteParams = {
105
+ arguments: toolUse.input,
106
+ connectedAccountId: options?.connectedAccountId,
107
+ customAuthParams: options?.customAuthParams,
108
+ userId: userId,
109
+ };
110
+ const result = await this.executeTool(toolUse.name, payload, modifiers);
111
+ return JSON.stringify(result.data);
112
+ }
113
+
114
+ /**
115
+ * Handle tool calls from Anthropic's message response
116
+ * @param userId - The user ID
117
+ * @param message - The message response from Anthropic
118
+ * @param options - Additional options for tool execution
119
+ * @param modifiers - Modifiers for tool execution
120
+ * @returns Array of tool execution results
121
+ */
122
+ async handleToolCalls(
123
+ userId: string,
124
+ message: Anthropic.Message,
125
+ options?: ExecuteToolFnOptions,
126
+ modifiers?: ExecuteToolModifiers
127
+ ): Promise<string[]> {
128
+ const outputs: string[] = [];
129
+
130
+ // Filter and map tool use blocks from message content
131
+ const toolUseBlocks: AnthropicToolUseBlock[] = [];
132
+
133
+ for (const content of message.content) {
134
+ if (
135
+ typeof content === 'object' &&
136
+ content !== null &&
137
+ 'type' in content &&
138
+ typeof content.type === 'string' &&
139
+ content.type.toString() === 'tool_use' &&
140
+ 'id' in content &&
141
+ 'name' in content &&
142
+ 'input' in content
143
+ ) {
144
+ toolUseBlocks.push({
145
+ type: 'tool_use',
146
+ id: String(content.id),
147
+ name: String(content.name),
148
+ input: content.input as Record<string, unknown>
149
+ });
150
+ }
151
+ }
152
+
153
+ for (const toolUse of toolUseBlocks) {
154
+ outputs.push(await this.executeToolCall(userId, toolUse, options, modifiers));
155
+ }
156
+
157
+ return outputs;
158
+ }
159
+}
packages/providers/anthropic/test/anthropic.test.ts
new
+359
@@ -0,0 +1,359 @@
1
+import { describe, it, expect, vi, beforeEach } from 'vitest';
2
+import { AnthropicProvider, AnthropicTool, AnthropicToolUseBlock } from '../src';
3
+import { Tool } from '@composio/core';
4
+import Anthropic from '@anthropic-ai/sdk';
5
+
6
+vi.mock('@anthropic-ai/sdk', () => {
7
+ return {
8
+ default: vi.fn().mockImplementation(() => ({
9
+ messages: {
10
+ create: vi.fn().mockResolvedValue({
11
+ id: 'msg_123',
12
+ content: [
13
+ { type: 'text', text: 'Hello' },
14
+ {
15
+ type: 'tool_use',
16
+ id: 'tu_123',
17
+ name: 'test-tool',
18
+ input: { input: 'test-value' },
19
+ },
20
+ ],
21
+ }),
22
+ },
23
+ })),
24
+ };
25
+});
26
+
27
+describe('AnthropicProvider', () => {
28
+ let provider: AnthropicProvider;
29
+ let mockTool: Tool;
30
+ let mockExecuteToolFn: any;
31
+
32
+ beforeEach(() => {
33
+ provider = new AnthropicProvider();
34
+
35
+ mockExecuteToolFn = vi.fn().mockResolvedValue({
36
+ data: { result: 'success' },
37
+ error: null,
38
+ successful: true,
39
+ });
40
+ provider._setExecuteToolFn(mockExecuteToolFn);
41
+
42
+ mockTool = {
43
+ slug: 'test-tool',
44
+ name: 'Test Tool',
45
+ description: 'A tool for testing',
46
+ inputParameters: {
47
+ type: 'object',
48
+ properties: {
49
+ input: {
50
+ type: 'string',
51
+ description: 'Test input',
52
+ },
53
+ },
54
+ required: ['input'],
55
+ },
56
+ tags: [],
57
+ };
58
+
59
+ vi.clearAllMocks();
60
+ });
61
+
62
+ describe('name property', () => {
63
+ it('should have the correct name', () => {
64
+ expect(provider.name).toBe('anthropic');
65
+ });
66
+ });
67
+
68
+ describe('_isAgentic property', () => {
69
+ it('should be non-agentic', () => {
70
+ expect(provider._isAgentic).toBe(false);
71
+ });
72
+ });
73
+
74
+ describe('wrapTool', () => {
75
+ it('should wrap a tool in Anthropic format', () => {
76
+ const wrapped = provider.wrapTool(mockTool) as AnthropicTool;
77
+
78
+ expect(wrapped).toEqual({
79
+ name: mockTool.slug,
80
+ description: mockTool.description,
81
+ input_schema: {
82
+ type: 'object',
83
+ properties: mockTool.inputParameters?.properties,
84
+ required: mockTool.inputParameters?.required,
85
+ },
86
+ });
87
+ });
88
+
89
+ it('should handle tools without input parameters', () => {
90
+ const toolWithoutParams: Tool = {
91
+ ...mockTool,
92
+ inputParameters: undefined,
93
+ };
94
+
95
+ const wrapped = provider.wrapTool(toolWithoutParams) as AnthropicTool;
96
+
97
+ expect(wrapped).toEqual({
98
+ name: toolWithoutParams.slug,
99
+ description: toolWithoutParams.description,
100
+ input_schema: {
101
+ type: 'object',
102
+ properties: {} as Record<string, unknown>,
103
+ required: [] as string[],
104
+ },
105
+ });
106
+ });
107
+ });
108
+
109
+ describe('wrapTools', () => {
110
+ it('should wrap multiple tools', () => {
111
+ const anotherTool: Tool = {
112
+ ...mockTool,
113
+ slug: 'another-tool',
114
+ name: 'Another Tool',
115
+ };
116
+ const tools = [mockTool, anotherTool];
117
+
118
+ const wrapped = provider.wrapTools(tools);
119
+
120
+ expect(wrapped).toHaveLength(2);
121
+
122
+ expect(wrapped[0]).toEqual({
123
+ name: mockTool.slug,
124
+ description: mockTool.description,
125
+ input_schema: {
126
+ type: 'object',
127
+ properties: mockTool.inputParameters?.properties,
128
+ required: mockTool.inputParameters?.required,
129
+ },
130
+ });
131
+
132
+ expect(wrapped[1]).toEqual({
133
+ name: anotherTool.slug,
134
+ description: anotherTool.description,
135
+ input_schema: {
136
+ type: 'object',
137
+ properties: anotherTool.inputParameters?.properties,
138
+ required: anotherTool.inputParameters?.required,
139
+ },
140
+ });
141
+ });
142
+
143
+ it('should return an empty array for empty tools array', () => {
144
+ const wrapped = provider.wrapTools([]);
145
+ expect(wrapped).toEqual([]);
146
+ });
147
+ });
148
+
149
+ describe('executeToolCall', () => {
150
+ it('should execute a tool call and return the result as string', async () => {
151
+ const userId = 'test-user';
152
+ const toolUse: AnthropicToolUseBlock = {
153
+ type: 'tool_use',
154
+ id: 'tu_123',
155
+ name: 'test-tool',
156
+ input: { input: 'test-value' },
157
+ };
158
+
159
+ const result = await provider.executeToolCall(userId, toolUse);
160
+
161
+ expect(mockExecuteToolFn).toHaveBeenCalledWith(
162
+ 'test-tool',
163
+ {
164
+ arguments: { input: 'test-value' },
165
+ userId: 'test-user',
166
+ connectedAccountId: undefined,
167
+ customAuthParams: undefined,
168
+ },
169
+ undefined
170
+ );
171
+ expect(result).toBe(
172
+ JSON.stringify({ result: 'success' })
173
+ );
174
+ });
175
+
176
+ it('should pass options to executeTool', async () => {
177
+ const userId = 'test-user';
178
+ const toolUse: AnthropicToolUseBlock = {
179
+ type: 'tool_use',
180
+ id: 'tu_123',
181
+ name: 'test-tool',
182
+ input: { input: 'test-value' },
183
+ };
184
+
185
+ const options = {
186
+ connectedAccountId: 'conn-123',
187
+ customAuthParams: {
188
+ parameters: [{ name: 'token', value: 'abc123', in: 'header' as const }],
189
+ },
190
+ };
191
+
192
+ const modifiers = {
193
+ beforeExecute: vi.fn(params => params),
194
+ afterExecute: vi.fn(response => response),
195
+ };
196
+
197
+ await provider.executeToolCall(userId, toolUse, options, modifiers);
198
+
199
+ expect(mockExecuteToolFn).toHaveBeenCalledWith(
200
+ 'test-tool',
201
+ {
202
+ arguments: { input: 'test-value' },
203
+ userId: 'test-user',
204
+ connectedAccountId: 'conn-123',
205
+ customAuthParams: options.customAuthParams,
206
+ },
207
+ modifiers
208
+ );
209
+ });
210
+ });
211
+
212
+ describe('handleToolCalls', () => {
213
+ it('should handle tool calls from Anthropic message', async () => {
214
+ const userId = 'test-user';
215
+ const message = {
216
+ id: 'msg_123',
217
+ content: [
218
+ { type: 'text', text: 'Hello' },
219
+ {
220
+ type: 'tool_use',
221
+ id: 'tu_123',
222
+ name: 'test-tool',
223
+ input: { input: 'test-value' },
224
+ },
225
+ ],
226
+ } as Anthropic.Message;
227
+
228
+ const executeToolCallSpy = vi.spyOn(provider, 'executeToolCall');
229
+ executeToolCallSpy.mockResolvedValue(JSON.stringify({ result: 'success' }));
230
+
231
+ const results = await provider.handleToolCalls(userId, message);
232
+
233
+ expect(executeToolCallSpy).toHaveBeenCalledWith(
234
+ userId,
235
+ expect.objectContaining({
236
+ type: 'tool_use',
237
+ id: 'tu_123',
238
+ name: 'test-tool',
239
+ input: { input: 'test-value' },
240
+ }),
241
+ undefined,
242
+ undefined
243
+ );
244
+ expect(results).toEqual([JSON.stringify({ result: 'success' })]);
245
+ });
246
+
247
+ it('should handle multiple tool calls', async () => {
248
+ const userId = 'test-user';
249
+ const message = {
250
+ id: 'msg_123',
251
+ content: [
252
+ { type: 'text', text: 'Hello' },
253
+ {
254
+ type: 'tool_use',
255
+ id: 'tu_123',
256
+ name: 'test-tool-1',
257
+ input: { input: 'test-value-1' },
258
+ },
259
+ {
260
+ type: 'tool_use',
261
+ id: 'tu_456',
262
+ name: 'test-tool-2',
263
+ input: { input: 'test-value-2' },
264
+ },
265
+ ],
266
+ } as Anthropic.Message;
267
+
268
+ const executeToolCallSpy = vi.spyOn(provider, 'executeToolCall');
269
+ executeToolCallSpy
270
+ .mockResolvedValueOnce(JSON.stringify({ result: 'success-1' }))
271
+ .mockResolvedValueOnce(JSON.stringify({ result: 'success-2' }));
272
+
273
+ const results = await provider.handleToolCalls(userId, message);
274
+
275
+ expect(executeToolCallSpy).toHaveBeenCalledTimes(2);
276
+ expect(executeToolCallSpy).toHaveBeenNthCalledWith(
277
+ 1,
278
+ userId,
279
+ expect.objectContaining({
280
+ type: 'tool_use',
281
+ id: 'tu_123',
282
+ name: 'test-tool-1',
283
+ input: { input: 'test-value-1' },
284
+ }),
285
+ undefined,
286
+ undefined
287
+ );
288
+ expect(executeToolCallSpy).toHaveBeenNthCalledWith(
289
+ 2,
290
+ userId,
291
+ expect.objectContaining({
292
+ type: 'tool_use',
293
+ id: 'tu_456',
294
+ name: 'test-tool-2',
295
+ input: { input: 'test-value-2' },
296
+ }),
297
+ undefined,
298
+ undefined
299
+ );
300
+ expect(results).toEqual([
301
+ JSON.stringify({ result: 'success-1' }),
302
+ JSON.stringify({ result: 'success-2' }),
303
+ ]);
304
+ });
305
+
306
+ it('should handle messages without tool calls', async () => {
307
+ const userId = 'test-user';
308
+ const message = {
309
+ id: 'msg_123',
310
+ content: [
311
+ { type: 'text', text: 'Hello' },
312
+ ],
313
+ } as Anthropic.Message;
314
+
315
+ const executeToolCallSpy = vi.spyOn(provider, 'executeToolCall');
316
+
317
+ const results = await provider.handleToolCalls(userId, message);
318
+
319
+ expect(executeToolCallSpy).not.toHaveBeenCalled();
320
+ expect(results).toEqual([]);
321
+ });
322
+ });
323
+
324
+ describe('executeTool', () => {
325
+ it('should execute a tool using the global execute function', async () => {
326
+ const toolSlug = 'test-tool';
327
+ const toolParams = {
328
+ userId: 'test-user',
329
+ arguments: { input: 'test-value' },
330
+ };
331
+
332
+ const result = await provider.executeTool(toolSlug, toolParams);
333
+
334
+ expect(mockExecuteToolFn).toHaveBeenCalledWith(toolSlug, toolParams, undefined);
335
+ expect(result).toEqual({
336
+ data: { result: 'success' },
337
+ error: null,
338
+ successful: true,
339
+ });
340
+ });
341
+
342
+ it('should pass modifiers to the global execute function', async () => {
343
+ const toolSlug = 'test-tool';
344
+ const toolParams = {
345
+ userId: 'test-user',
346
+ arguments: { input: 'test-value' },
347
+ };
348
+
349
+ const modifiers = {
350
+ beforeExecute: vi.fn(params => params),
351
+ afterExecute: vi.fn(response => response),
352
+ };
353
+
354
+ await provider.executeTool(toolSlug, toolParams, modifiers);
355
+
356
+ expect(mockExecuteToolFn).toHaveBeenCalledWith(toolSlug, toolParams, modifiers);
357
+ });
358
+ });
359
+});
packages/providers/anthropic/tsconfig.json
new
+16
@@ -0,0 +1,16 @@
1
+{
2
+ "extends": "../../../tsconfig.base.json",
3
+ "compilerOptions": {
4
+ "target": "ESNext",
5
+ "module": "ESNext",
6
+ "declaration": true,
7
+ "declarationDir": "./dist",
8
+ "outDir": "./dist",
9
+ "strict": true,
10
+ "esModuleInterop": true,
11
+ "moduleResolution": "node",
12
+ "skipLibCheck": true,
13
+ "resolveJsonModule": true
14
+ },
15
+ "include": ["src"]
16
+}
packages/providers/anthropic/tsup.config.ts
new
+10
@@ -0,0 +1,10 @@
1
+import { defineConfig } from 'tsup';
2
+
3
+export default defineConfig({
4
+ entry: ['src/index.ts'],
5
+ format: ['esm', 'cjs'],
6
+ dts: true,
7
+ clean: true,
8
+ minify: false,
9
+ outDir: 'dist',
10
+});