@composio/anthropic 0.3.4 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js DELETED
@@ -1,244 +0,0 @@
1
- // src/index.ts
2
- import {
3
- BaseNonAgenticProvider,
4
- logger
5
- } from "@composio/core";
6
- var AnthropicProvider = class extends BaseNonAgenticProvider {
7
- name = "anthropic";
8
- chacheTools = false;
9
- /**
10
- * Creates a new instance of the AnthropicProvider.
11
- *
12
- * @param {Object} [options] - Configuration options for the provider
13
- * @param {boolean} [options.cacheTools=false] - Whether to cache tools using Anthropic's ephemeral cache
14
- *
15
- * @example
16
- * ```typescript
17
- * // Initialize with default settings (no caching)
18
- * const provider = new AnthropicProvider();
19
- *
20
- * // Initialize with tool caching enabled
21
- * const providerWithCaching = new AnthropicProvider({
22
- * cacheTools: true
23
- * });
24
- *
25
- * // Use with Composio
26
- * const composio = new Composio({
27
- * apiKey: 'your-api-key',
28
- * provider: new AnthropicProvider({
29
- * cacheTools: true
30
- * })
31
- * });
32
- * ```
33
- */
34
- constructor(options) {
35
- super();
36
- this.chacheTools = options?.cacheTools ?? false;
37
- logger.debug(`AnthropicProvider initialized [cacheTools: ${this.chacheTools}]`);
38
- }
39
- /**
40
- * Wraps a Composio tool in the Anthropic format.
41
- *
42
- * This method transforms a Composio tool definition into the format
43
- * expected by Anthropic's Claude API for tool use.
44
- *
45
- * @param tool - The Composio tool to wrap
46
- * @returns The wrapped tool in Anthropic format
47
- *
48
- * @example
49
- * ```typescript
50
- * // Wrap a single tool for use with Anthropic
51
- * const composioTool = {
52
- * slug: 'SEARCH_TOOL',
53
- * description: 'Search for information',
54
- * inputParameters: {
55
- * type: 'object',
56
- * properties: {
57
- * query: { type: 'string' }
58
- * },
59
- * required: ['query']
60
- * }
61
- * };
62
- *
63
- * const anthropicTool = provider.wrapTool(composioTool);
64
- * ```
65
- */
66
- wrapTool(tool) {
67
- return {
68
- name: tool.slug,
69
- description: tool.description || "",
70
- input_schema: tool.inputParameters || {
71
- type: "object",
72
- properties: {},
73
- required: []
74
- },
75
- cache_control: this.chacheTools ? { type: "ephemeral" } : void 0
76
- };
77
- }
78
- /**
79
- * Wraps a list of Composio tools in the Anthropic format.
80
- *
81
- * This method transforms multiple Composio tool definitions into the format
82
- * expected by Anthropic's Claude API for tool use.
83
- *
84
- * @param tools - Array of Composio tools to wrap
85
- * @returns Array of wrapped tools in Anthropic format
86
- *
87
- * @example
88
- * ```typescript
89
- * // Wrap multiple tools for use with Anthropic
90
- * const composioTools = [
91
- * {
92
- * slug: 'SEARCH_TOOL',
93
- * description: 'Search for information',
94
- * inputParameters: {
95
- * type: 'object',
96
- * properties: {
97
- * query: { type: 'string' }
98
- * }
99
- * }
100
- * },
101
- * {
102
- * slug: 'WEATHER_TOOL',
103
- * description: 'Get weather information',
104
- * inputParameters: {
105
- * type: 'object',
106
- * properties: {
107
- * location: { type: 'string' }
108
- * }
109
- * }
110
- * }
111
- * ];
112
- *
113
- * const anthropicTools = provider.wrapTools(composioTools);
114
- * ```
115
- */
116
- wrapTools(tools) {
117
- return tools.map((tool) => this.wrapTool(tool));
118
- }
119
- /**
120
- * Executes a tool call from Anthropic's Claude API.
121
- *
122
- * This method processes a tool call from Anthropic's Claude API,
123
- * executes the corresponding Composio tool, and returns the result.
124
- *
125
- * @param userId - The user ID for authentication and tracking
126
- * @param toolUse - The tool use object from Anthropic
127
- * @param options - Additional options for tool execution
128
- * @param modifiers - Modifiers for tool execution
129
- * @returns The result of the tool execution as a JSON string
130
- *
131
- * @example
132
- * ```typescript
133
- * // Execute a tool call from Anthropic
134
- * const toolUse = {
135
- * type: 'tool_use',
136
- * id: 'tu_abc123',
137
- * name: 'SEARCH_TOOL',
138
- * input: {
139
- * query: 'composio documentation'
140
- * }
141
- * };
142
- *
143
- * const result = await provider.executeToolCall(
144
- * 'user123',
145
- * toolUse,
146
- * { connectedAccountId: 'conn_xyz456' }
147
- * );
148
- * console.log(JSON.parse(result));
149
- * ```
150
- */
151
- async executeToolCall(userId, toolUse, options, modifiers) {
152
- const payload = {
153
- arguments: toolUse.input,
154
- connectedAccountId: options?.connectedAccountId,
155
- customAuthParams: options?.customAuthParams,
156
- customConnectionData: options?.customConnectionData,
157
- userId
158
- };
159
- const result = await this.executeTool(toolUse.name, payload, modifiers);
160
- return JSON.stringify(result.data);
161
- }
162
- /**
163
- * Handles tool calls from Anthropic's message response.
164
- *
165
- * This method processes tool calls from an Anthropic message response,
166
- * extracts the tool use blocks, executes each tool call, and returns the results.
167
- *
168
- * @param userId - The user ID for authentication and tracking
169
- * @param message - The message response from Anthropic
170
- * @param options - Additional options for tool execution
171
- * @param modifiers - Modifiers for tool execution
172
- * @returns Array of tool execution results as JSON strings
173
- *
174
- * @example
175
- * ```typescript
176
- * // Handle tool calls from an Anthropic message response
177
- * const anthropic = new Anthropic({ apiKey: 'your-anthropic-api-key' });
178
- *
179
- * const message = await anthropic.messages.create({
180
- * model: 'claude-3-opus-20240229',
181
- * max_tokens: 1024,
182
- * tools: provider.wrapTools(composioTools),
183
- * messages: [
184
- * {
185
- * role: 'user',
186
- * content: 'Search for information about Composio'
187
- * }
188
- * ]
189
- * });
190
- *
191
- * // Process any tool calls in the response
192
- * const results = await provider.handleToolCalls(
193
- * 'user123',
194
- * message,
195
- * { connectedAccountId: 'conn_xyz456' }
196
- * );
197
- *
198
- * // Use the results to continue the conversation
199
- * console.log(results);
200
- * ```
201
- */
202
- async handleToolCalls(userId, message, options, modifiers) {
203
- const outputs = [];
204
- const toolUseBlocks = [];
205
- for (const content of message.content) {
206
- if (typeof content === "object" && content !== null && "type" in content && typeof content.type === "string" && content.type.toString() === "tool_use" && "id" in content && "name" in content && "input" in content) {
207
- toolUseBlocks.push({
208
- type: "tool_use",
209
- id: String(content.id),
210
- name: String(content.name),
211
- input: content.input
212
- });
213
- }
214
- }
215
- for (const toolUse of toolUseBlocks) {
216
- const toolResult = await this.executeToolCall(userId, toolUse, options, modifiers);
217
- outputs.push({
218
- type: "tool_result",
219
- tool_use_id: toolUse.id,
220
- content: toolResult,
221
- cache_control: this.chacheTools ? { type: "ephemeral" } : void 0
222
- });
223
- }
224
- return outputs.length > 0 ? [{ role: "user", content: outputs }] : [];
225
- }
226
- /**
227
- * Transform MCP URL response into Anthropic-specific format.
228
- * By default, Anthropic uses the standard format (same as default),
229
- * but this method is here to show providers can customize if needed.
230
- *
231
- * @param data - The MCP URL response data
232
- * @returns Standard MCP server response format
233
- */
234
- wrapMcpServerResponse(data) {
235
- return data.map((item) => ({
236
- url: item.url,
237
- name: item.name,
238
- type: "url"
239
- }));
240
- }
241
- };
242
- export {
243
- AnthropicProvider
244
- };