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