@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 +251 -264
- package/dist/index.d.cts +5195 -235
- package/dist/index.d.mts +5229 -0
- package/dist/index.mjs +253 -0
- package/package.json +19 -11
- package/dist/index.d.ts +0 -269
- package/dist/index.js +0 -244
package/dist/index.cjs
CHANGED
|
@@ -1,266 +1,253 @@
|
|
|
1
|
-
|
|
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
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
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
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
});
|
|
251
|
+
|
|
252
|
+
//#endregion
|
|
253
|
+
exports.AnthropicProvider = AnthropicProvider;
|