Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
Improved TSDoc/JSDoc Documentation with Examples (#19)
* Update TSDoc/JSDoc documentation with examples and improved descriptions Co-Authored-By: musthaq@composio.dev <musthu.gm@gmail.com> * Add comprehensive JSDoc documentation to provider packages Co-Authored-By: musthaq@composio.dev <musthu.gm@gmail.com> * Update docs * Add changeset --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: musthaq@composio.dev <musthu.gm@gmail.com>
devin-ai-integration[bot] committed
May 30, 2025 at 18:01 UTC
f1442026e129dc879169df85528ead2d3892883a
11 files changed
+1145
-80
.changeset/five-ties-tell.md
new
+12
@@ -0,0 +1,12 @@
1
+---
2
+'@composio/cloudflare': patch
3
+'@composio/anthropic': patch
4
+'@composio/langchain': patch
5
+'@composio/google': patch
6
+'@composio/vercel': patch
7
+'@composio/core': patch
8
+'@composio/mastra': patch
9
+'@composio/openai': patch
10
+---
11
+
12
+Update tsdocs across all the functions
packages/core/src/composio.ts
+30
-7
@@ -56,13 +56,36 @@ export class Composio<TProvider extends BaseComposioProvider<unknown, unknown> =
56
connectedAccounts: ConnectedAccounts;
57
58
/**
59
- * @param {Object} config Configuration for the Composio SDK.
60
- * @param {string} config.apiKey The API key for the Composio SDK.
61
- * @param {string} config.baseURL The base URL for the Composio SDK.
62
- * @param {string} config.runtime The runtime for the Composio SDK.
63
- * @param {boolean} config.allowTracking Whether to allow analytics / tracking. Defaults to true.
64
- * @param {boolean} config.allowTracing Whether to allow tracing. Defaults to true.
65
- * @param {TProvider} config.provider The provider to use for this Composio instance.
59
+ * Creates a new instance of the Composio SDK.
60
+ *
61
+ * The constructor initializes the SDK with the provided configuration options,
62
+ * sets up the API client, and initializes all core models (tools, toolkits, etc.).
63
+ *
64
+ * @param {ComposioConfig<TProvider>} config - Configuration options for the Composio SDK
65
+ * @param {string} [config.apiKey] - The API key for authenticating with the Composio API
66
+ * @param {string} [config.baseURL] - The base URL for the Composio API (defaults to production URL)
67
+ * @param {boolean} [config.allowTracking=true] - Whether to allow anonymous usage analytics
68
+ * @param {boolean} [config.allowTracing=true] - Whether to allow request tracing for debugging
69
+ * @param {TProvider} [config.provider] - The provider to use for this Composio instance (defaults to OpenAIProvider)
70
+ * @param {BaseTelemetryTransport} [config.telemetryTransport] - Custom telemetry transport implementation
71
+ *
72
+ * @example
73
+ * ```typescript
74
+ * // Initialize with default configuration
75
+ * const composio = new Composio();
76
+ *
77
+ * // Initialize with custom API key and base URL
78
+ * const composio = new Composio({
79
+ * apiKey: 'your-api-key',
80
+ * baseURL: 'https://api.composio.dev'
81
+ * });
82
+ *
83
+ * // Initialize with custom provider
84
+ * const composio = new Composio({
85
+ * apiKey: 'your-api-key',
86
+ * provider: new CustomProvider()
87
+ * });
88
+ * ```
89
*/
90
constructor(config?: ComposioConfig<TProvider>) {
91
const { baseURL: baseURLParsed, apiKey: apiKeyParsed } = getSDKConfig(
packages/core/src/models/ConnectedAccounts.ts
+34
-3
@@ -279,8 +279,21 @@ export class ConnectedAccounts {
279
* Update the status of a connected account
280
* @param {string} nanoid - Unique identifier of the connected account
281
* @param {ConnectedAccountUpdateStatusParams} params - Parameters for updating the status
282
- * @param {RequestOptions} options - Request options
282
* @returns {Promise<ConnectedAccountUpdateStatusResponse>} Updated connected account details
283
+ *
284
+ * @example
285
+ * ```typescript
286
+ * // Enable a connected account
287
+ * const updatedAccount = await composio.connectedAccounts.updateStatus('conn_abc123', {
288
+ * enabled: true
289
+ * });
290
+ *
291
+ * // Disable a connected account with a reason
292
+ * const disabledAccount = await composio.connectedAccounts.updateStatus('conn_abc123', {
293
+ * enabled: false,
294
+ * reason: 'Token expired'
295
+ * });
296
+ * ```
297
*/
298
async updateStatus(
299
nanoid: string,
@@ -292,8 +305,14 @@ export class ConnectedAccounts {
305
/**
306
* Enable a connected account
307
* @param {string} nanoid - Unique identifier of the connected account
295
- * @param {RequestOptions} options - Request options
308
* @returns {Promise<ConnectedAccountUpdateStatusResponse>} Updated connected account details
309
+ *
310
+ * @example
311
+ * ```typescript
312
+ * // Enable a previously disabled connected account
313
+ * const enabledAccount = await composio.connectedAccounts.enable('conn_abc123');
314
+ * console.log(enabledAccount.isDisabled); // false
315
+ * ```
316
*/
317
async enable(nanoid: string): Promise<ConnectedAccountUpdateStatusResponse> {
318
return this.client.connectedAccounts.updateStatus(nanoid, { enabled: true });
@@ -302,8 +321,20 @@ export class ConnectedAccounts {
321
/**
322
* Disable a connected account
323
* @param {string} nanoid - Unique identifier of the connected account
305
- * @param {RequestOptions} options - Request options
324
* @returns {Promise<ConnectedAccountUpdateStatusResponse>} Updated connected account details
325
+ *
326
+ * @example
327
+ * ```typescript
328
+ * // Disable a connected account
329
+ * const disabledAccount = await composio.connectedAccounts.disable('conn_abc123');
330
+ * console.log(disabledAccount.isDisabled); // true
331
+ *
332
+ * // You can also use updateStatus with a reason
333
+ * // const disabledAccount = await composio.connectedAccounts.updateStatus('conn_abc123', {
334
+ * // enabled: false,
335
+ * // reason: 'No longer needed'
336
+ * // });
337
+ * ```
338
*/
339
async disable(nanoid: string): Promise<ConnectedAccountUpdateStatusResponse> {
340
return this.client.connectedAccounts.updateStatus(nanoid, { enabled: false });
packages/core/src/models/CustomTools.ts
+42
@@ -44,6 +44,26 @@ export class CustomTools {
44
* This is just an in memory registry and is not persisted.
45
* @param {CustomToolOptions} toolOptions CustomToolOptions
46
* @returns {Tool} The tool created
47
+ *
48
+ * @example
49
+ * ```typescript
50
+ * // Create a custom tool with input parameters
51
+ * const customTool = await composio.customTools.createTool({
52
+ * name: 'My Custom Tool',
53
+ * description: 'A tool that performs a custom operation',
54
+ * slug: 'MY_CUSTOM_TOOL',
55
+ * inputParams: z.object({
56
+ * query: z.string().describe('The search query'),
57
+ * limit: z.number().optional().describe('Maximum number of results')
58
+ * }),
59
+ * execute: async (input, authCredentials, executeToolRequest) => {
60
+ * // Custom implementation logic
61
+ * return {
62
+ * data: { results: ['result1', 'result2'] }
63
+ * };
64
+ * }
65
+ * });
66
+ * ```
67
*/
68
async createTool(toolOptions: CustomToolOptions): Promise<Tool> {
69
const { slug, execute, inputParams, name, description } = toolOptions;
@@ -87,6 +107,17 @@ export class CustomTools {
107
* Get all the custom tools from the registry.
108
* @param {string[]} param0.toolSlugs The slugs of the tools to get
109
* @returns {ToolList} The list of tools
110
+ *
111
+ * @example
112
+ * ```typescript
113
+ * // Get all custom tools
114
+ * const allTools = await composio.customTools.getCustomTools({});
115
+ *
116
+ * // Get specific custom tools by slug
117
+ * const specificTools = await composio.customTools.getCustomTools({
118
+ * toolSlugs: ['MY_CUSTOM_TOOL', 'ANOTHER_CUSTOM_TOOL']
119
+ * });
120
+ * ```
121
*/
122
async getCustomTools({ toolSlugs }: { toolSlugs?: string[] }): Promise<ToolList> {
123
const tools: Tool[] = [];
@@ -111,6 +142,17 @@ export class CustomTools {
142
* Get a custom tool by slug from the registry.
143
* @param {string} slug The slug of the tool to get
144
* @returns {Tool} The tool
145
+ *
146
+ * @example
147
+ * ```typescript
148
+ * // Get a specific custom tool by its slug
149
+ * const myTool = await composio.customTools.getCustomToolBySlug('MY_CUSTOM_TOOL');
150
+ * if (myTool) {
151
+ * console.log(`Found tool: ${myTool.name}`);
152
+ * } else {
153
+ * console.log('Tool not found');
154
+ * }
155
+ * ```
156
*/
157
async getCustomToolBySlug(slug: string): Promise<Tool | undefined> {
158
if (!slug) {
packages/core/src/models/Tools.ts
+40
-6
@@ -642,9 +642,16 @@ export class Tools<
642
/**
643
* Fetches the list of all available tools in the Composio SDK.
644
*
645
- * This method is mostly used by the CLI to get the list of tools.
645
+ * This method is mostly used by the CLI to get the list of tools.
646
* No filtering is done on the tools, the list is cached in the backend, no further optimization is required.
647
- * @returns {string} - The list of all available tools as a string
647
+ * @returns {Promise<ToolRetrieveEnumResponse>} The complete list of all available tools with their metadata
648
+ *
649
+ * @example
650
+ * ```typescript
651
+ * // Get all available tools as an enum
652
+ * const toolsEnum = await composio.tools.getToolsEnum();
653
+ * console.log(toolsEnum.items);
654
+ * ```
655
*/
656
async getToolsEnum(): Promise<ToolRetrieveEnumResponse> {
657
return this.client.tools.retrieveEnum();
@@ -657,16 +664,43 @@ export class Tools<
664
*
665
* @param {string} slug - The ID of the tool to find input for
666
* @param {ToolGetInputParams} body - The parameters to be passed to the tool
660
- * @returns
667
+ * @returns {Promise<ToolGetInputResponse>} The input parameters schema for the specified tool
668
+ *
669
+ * @example
670
+ * ```typescript
671
+ * // Get input parameters for a specific tool
672
+ * const inputParams = await composio.tools.getInput('GITHUB_CREATE_ISSUE', {
673
+ * userId: 'default'
674
+ * });
675
+ * console.log(inputParams.schema);
676
+ * ```
677
*/
678
async getInput(slug: string, body: ToolGetInputParams): Promise<ToolGetInputResponse> {
679
return this.client.tools.getInput(slug, body);
680
}
681
682
/**
667
- * Proxies a custom request to a toolkit/integration
668
- * @param body
669
- * @returns
683
+ * Proxies a custom request to a toolkit/integration.
684
+ *
685
+ * This method allows sending custom requests to a specific toolkit or integration
686
+ * when you need more flexibility than the standard tool execution methods provide.
687
+ *
688
+ * @param {ToolProxyParams} body - The parameters for the proxy request including toolkit slug and custom data
689
+ * @returns {Promise<ToolProxyResponse>} The response from the proxied request
690
+ *
691
+ * @example
692
+ * ```typescript
693
+ * // Send a custom request to a toolkit
694
+ * const response = await composio.tools.proxyExecute({
695
+ * toolkitSlug: 'github',
696
+ * userId: 'default',
697
+ * data: {
698
+ * endpoint: '/repos/owner/repo/issues',
699
+ * method: 'GET'
700
+ * }
701
+ * });
702
+ * console.log(response.data);
703
+ * ```
704
*/
705
async proxyExecute(body: ToolProxyParams): Promise<ToolProxyResponse> {
706
return this.client.tools.proxy(body);
packages/core/src/provider/OpenAIProvider.ts
+277
-26
@@ -19,11 +19,57 @@ export type OpenAiTool = OpenAI.ChatCompletionTool;
19
export type OpenAiToolCollection = Array<OpenAiTool>;
20
export class OpenAIProvider extends BaseNonAgenticProvider<OpenAiToolCollection, OpenAiTool> {
21
readonly name = 'openai';
22
+
23
/**
23
- * Absract method to wrap a tool in the provider.
24
- * This method is implemented by the provider.
25
- * @param tool - The tool to wrap.
26
- * @returns The wrapped tool.
24
+ * Creates a new instance of the OpenAIProvider.
25
+ *
26
+ * This is the default provider for the Composio SDK and is automatically
27
+ * available without additional installation.
28
+ *
29
+ * @example
30
+ * ```typescript
31
+ * // The OpenAIProvider is used by default when initializing Composio
32
+ * const composio = new Composio({
33
+ * apiKey: 'your-api-key'
34
+ * });
35
+ *
36
+ * // You can also explicitly specify it
37
+ * const composio = new Composio({
38
+ * apiKey: 'your-api-key',
39
+ * provider: new OpenAIProvider()
40
+ * });
41
+ * ```
42
+ */
43
+ constructor() {
44
+ super();
45
+ }
46
+
47
+ /**
48
+ * Wraps a Composio tool in the OpenAI function calling format.
49
+ *
50
+ * This method transforms a Composio tool definition into the format
51
+ * expected by OpenAI's function calling API.
52
+ *
53
+ * @param tool - The Composio tool to wrap
54
+ * @returns The wrapped tool in OpenAI format
55
+ *
56
+ * @example
57
+ * ```typescript
58
+ * // Wrap a single tool for use with OpenAI
59
+ * const composioTool = {
60
+ * slug: 'SEARCH_TOOL',
61
+ * description: 'Search for information',
62
+ * inputParameters: {
63
+ * type: 'object',
64
+ * properties: {
65
+ * query: { type: 'string' }
66
+ * },
67
+ * required: ['query']
68
+ * }
69
+ * };
70
+ *
71
+ * const openAITool = provider.wrapTool(composioTool);
72
+ * ```
73
*/
74
override wrapTool = (tool: Tool): OpenAiTool => {
75
const formattedSchema: OpenAI.FunctionDefinition = {
@@ -37,15 +83,79 @@ export class OpenAIProvider extends BaseNonAgenticProvider<OpenAiToolCollection,
83
};
84
};
85
86
+ /**
87
+ * Wraps multiple Composio tools in the OpenAI function calling format.
88
+ *
89
+ * This method transforms a list of Composio tools into the format
90
+ * expected by OpenAI's function calling API.
91
+ *
92
+ * @param tools - Array of Composio tools to wrap
93
+ * @returns Array of wrapped tools in OpenAI format
94
+ *
95
+ * @example
96
+ * ```typescript
97
+ * // Wrap multiple tools for use with OpenAI
98
+ * const composioTools = [
99
+ * {
100
+ * slug: 'SEARCH_TOOL',
101
+ * description: 'Search for information',
102
+ * inputParameters: {
103
+ * type: 'object',
104
+ * properties: {
105
+ * query: { type: 'string' }
106
+ * }
107
+ * }
108
+ * },
109
+ * {
110
+ * slug: 'WEATHER_TOOL',
111
+ * description: 'Get weather information',
112
+ * inputParameters: {
113
+ * type: 'object',
114
+ * properties: {
115
+ * location: { type: 'string' }
116
+ * }
117
+ * }
118
+ * }
119
+ * ];
120
+ *
121
+ * const openAITools = provider.wrapTools(composioTools);
122
+ * ```
123
+ */
124
override wrapTools = (tools: Tool[]): OpenAiToolCollection => {
125
return tools.map(tool => this.wrapTool(tool));
126
};
127
128
/**
45
- * Execute a tool call.
46
- * @param {OpenAI.ChatCompletionMessageToolCall} tool - The tool to execute.
47
- * @param {string} userId - The user id.
48
- * @returns {Promise<string>} The result of the tool call.
129
+ * Executes a tool call from OpenAI's chat completion.
130
+ *
131
+ * This method processes a tool call from OpenAI's chat completion API,
132
+ * executes the corresponding Composio tool, and returns the result.
133
+ *
134
+ * @param {string} userId - The user ID for authentication and tracking
135
+ * @param {OpenAI.ChatCompletionMessageToolCall} tool - The tool call from OpenAI
136
+ * @param {ExecuteToolFnOptions} [options] - Optional execution options
137
+ * @param {ExecuteToolModifiers} [modifiers] - Optional execution modifiers
138
+ * @returns {Promise<string>} The result of the tool call as a JSON string
139
+ *
140
+ * @example
141
+ * ```typescript
142
+ * // Execute a tool call from OpenAI
143
+ * const toolCall = {
144
+ * id: 'call_abc123',
145
+ * type: 'function',
146
+ * function: {
147
+ * name: 'SEARCH_TOOL',
148
+ * arguments: '{"query":"composio documentation"}'
149
+ * }
150
+ * };
151
+ *
152
+ * const result = await provider.executeToolCall(
153
+ * 'user123',
154
+ * toolCall,
155
+ * { connectedAccountId: 'conn_xyz456' }
156
+ * );
157
+ * console.log(JSON.parse(result));
158
+ * ```
159
*/
160
async executeToolCall(
161
userId: string,
@@ -64,11 +174,46 @@ export class OpenAIProvider extends BaseNonAgenticProvider<OpenAiToolCollection,
174
}
175
176
/**
67
- * Handle the tool call from the assistant.
177
+ * Handles tool calls from OpenAI's chat completion response.
178
*
69
- * @param {OpenAI.ChatCompletion} chatCompletion - The chat completion object.
70
- * @param {string} userId - The user id.
71
- * @returns {Promise<string[]>} The results of the tool call.
179
+ * This method processes tool calls from an OpenAI chat completion response,
180
+ * executes each tool call, and returns the results.
181
+ *
182
+ * @param {string} userId - The user ID for authentication and tracking
183
+ * @param {OpenAI.ChatCompletion} chatCompletion - The chat completion response from OpenAI
184
+ * @param {ExecuteToolFnOptions} [options] - Optional execution options
185
+ * @param {ExecuteToolModifiers} [modifiers] - Optional execution modifiers
186
+ * @returns {Promise<string[]>} Array of tool execution results as JSON strings
187
+ *
188
+ * @example
189
+ * ```typescript
190
+ * // Handle tool calls from a chat completion response
191
+ * const chatCompletion = {
192
+ * choices: [
193
+ * {
194
+ * message: {
195
+ * tool_calls: [
196
+ * {
197
+ * id: 'call_abc123',
198
+ * type: 'function',
199
+ * function: {
200
+ * name: 'SEARCH_TOOL',
201
+ * arguments: '{"query":"composio documentation"}'
202
+ * }
203
+ * }
204
+ * ]
205
+ * }
206
+ * }
207
+ * ]
208
+ * };
209
+ *
210
+ * const results = await provider.handleToolCalls(
211
+ * 'user123',
212
+ * chatCompletion,
213
+ * { connectedAccountId: 'conn_xyz456' }
214
+ * );
215
+ * console.log(results); // Array of tool execution results
216
+ * ```
217
*/
218
async handleToolCalls(
219
userId: string,
@@ -88,11 +233,51 @@ export class OpenAIProvider extends BaseNonAgenticProvider<OpenAiToolCollection,
233
}
234
235
/**
91
- * Handles all the tool calls from the assistant.
236
+ * Handles all the tool calls from the OpenAI Assistant API.
237
+ *
238
+ * This method processes tool calls from an OpenAI Assistant run,
239
+ * executes each tool call, and returns the tool outputs for submission.
240
*
93
- * @param {OpenAI.Beta.Threads.Run} run - The run object.
94
- * @param {string} userId - The user id.
95
- * @returns {Promise<OpenAI.Beta.Threads.Runs.RunSubmitToolOutputsParams.ToolOutput[]>} The tool outputs.
241
+ * @param {string} userId - The user ID for authentication and tracking
242
+ * @param {OpenAI.Beta.Threads.Run} run - The Assistant run object containing tool calls
243
+ * @param {ExecuteToolFnOptions} [options] - Optional execution options
244
+ * @param {ExecuteToolModifiers} [modifiers] - Optional execution modifiers
245
+ * @returns {Promise<OpenAI.Beta.Threads.Runs.RunSubmitToolOutputsParams.ToolOutput[]>} Array of tool outputs for submission
246
+ *
247
+ * @example
248
+ * ```typescript
249
+ * // Handle tool calls from an OpenAI Assistant run
250
+ * const run = {
251
+ * id: 'run_abc123',
252
+ * required_action: {
253
+ * submit_tool_outputs: {
254
+ * tool_calls: [
255
+ * {
256
+ * id: 'call_xyz789',
257
+ * type: 'function',
258
+ * function: {
259
+ * name: 'SEARCH_TOOL',
260
+ * arguments: '{"query":"composio documentation"}'
261
+ * }
262
+ * }
263
+ * ]
264
+ * }
265
+ * }
266
+ * };
267
+ *
268
+ * const toolOutputs = await provider.handleAssistantMessage(
269
+ * 'user123',
270
+ * run,
271
+ * { connectedAccountId: 'conn_xyz456' }
272
+ * );
273
+ *
274
+ * // Submit tool outputs back to OpenAI
275
+ * await openai.beta.threads.runs.submitToolOutputs(
276
+ * thread.id,
277
+ * run.id,
278
+ * { tool_outputs: toolOutputs }
279
+ * );
280
+ * ```
281
*/
282
async handleAssistantMessage(
283
userId: string,
@@ -128,11 +313,43 @@ export class OpenAIProvider extends BaseNonAgenticProvider<OpenAiToolCollection,
313
/**
314
* Waits for the assistant stream and handles the tool calls.
315
*
131
- * @param {OpenAI} client - The OpenAI client.
132
- * @param {Stream<OpenAI.Beta.Assistants.AssistantStreamEvent>} runStream - The run stream.
133
- * @param {OpenAI.Beta.Threads.Thread} thread - The thread object.
134
- * @param {string} userId - The user id.
135
- * @returns {AsyncGenerator<OpenAI.Beta.Assistants.AssistantStreamEvent, void, unknown>} The run stream.
316
+ * This method processes an OpenAI Assistant stream, handles any tool calls
317
+ * that require action, and yields each event from the stream. It's designed
318
+ * for streaming Assistant responses while handling tool calls in real-time.
319
+ *
320
+ * @param {string} userId - The user ID for authentication and tracking
321
+ * @param {OpenAI} client - The OpenAI client instance
322
+ * @param {Stream<OpenAI.Beta.Assistants.AssistantStreamEvent>} runStream - The Assistant run stream
323
+ * @param {OpenAI.Beta.Threads.Thread} thread - The thread object
324
+ * @param {ExecuteToolFnOptions} [options] - Optional execution options
325
+ * @param {ExecuteToolModifiers} [modifiers] - Optional execution modifiers
326
+ * @returns {AsyncGenerator<OpenAI.Beta.Assistants.AssistantStreamEvent, void, unknown>} Generator yielding stream events
327
+ *
328
+ * @example
329
+ * ```typescript
330
+ * // Process an OpenAI Assistant stream with tool calls
331
+ * const thread = await openai.beta.threads.create();
332
+ * const runStream = openai.beta.threads.runs.stream(thread.id, {
333
+ * assistant_id: 'asst_abc123',
334
+ * tools: provider.wrapTools(composioTools)
335
+ * });
336
+ *
337
+ * // Process the stream and handle tool calls
338
+ * const streamProcessor = provider.waitAndHandleAssistantStreamToolCalls(
339
+ * 'user123',
340
+ * openai,
341
+ * runStream,
342
+ * thread,
343
+ * { connectedAccountId: 'conn_xyz456' }
344
+ * );
345
+ *
346
+ * // Consume the stream events
347
+ * for await (const event of streamProcessor) {
348
+ * if (event.event === 'thread.message.delta') {
349
+ * console.log(event.data.delta.content);
350
+ * }
351
+ * }
352
+ * ```
353
*/
354
async *waitAndHandleAssistantStreamToolCalls(
355
userId: string,
@@ -212,11 +429,45 @@ export class OpenAIProvider extends BaseNonAgenticProvider<OpenAiToolCollection,
429
/**
430
* Waits for the assistant tool calls and handles them.
431
*
215
- * @param {OpenAI} client - The OpenAI client.
216
- * @param {OpenAI.Beta.Threads.Run} run - The run object.
217
- * @param {OpenAI.Beta.Threads.Thread} thread - The thread object.
218
- * @param {string} userId - The user id.
219
- * @returns {Promise<OpenAI.Beta.Threads.Run>} The run object.
432
+ * This method polls an OpenAI Assistant run until it completes or requires action,
433
+ * handles any tool calls, and returns the final run object. It's designed for
434
+ * non-streaming Assistant interactions.
435
+ *
436
+ * @param {string} userId - The user ID for authentication and tracking
437
+ * @param {OpenAI} client - The OpenAI client instance
438
+ * @param {OpenAI.Beta.Threads.Run} run - The initial run object
439
+ * @param {OpenAI.Beta.Threads.Thread} thread - The thread object
440
+ * @param {ExecuteToolFnOptions} [options] - Optional execution options
441
+ * @param {ExecuteToolModifiers} [modifiers] - Optional execution modifiers
442
+ * @returns {Promise<OpenAI.Beta.Threads.Run>} The final run object after completion
443
+ *
444
+ * @example
445
+ * ```typescript
446
+ * // Process an OpenAI Assistant run with tool calls
447
+ * const thread = await openai.beta.threads.create();
448
+ * await openai.beta.threads.messages.create(thread.id, {
449
+ * role: 'user',
450
+ * content: 'Find information about Composio'
451
+ * });
452
+ *
453
+ * let run = await openai.beta.threads.runs.create(thread.id, {
454
+ * assistant_id: 'asst_abc123',
455
+ * tools: provider.wrapTools(composioTools)
456
+ * });
457
+ *
458
+ * // Wait for the run to complete, handling any tool calls
459
+ * run = await provider.waitAndHandleAssistantToolCalls(
460
+ * 'user123',
461
+ * openai,
462
+ * run,
463
+ * thread,
464
+ * { connectedAccountId: 'conn_xyz456' }
465
+ * );
466
+ *
467
+ * // Get the final messages after run completion
468
+ * const messages = await openai.beta.threads.messages.list(thread.id);
469
+ * console.log(messages.data[0].content);
470
+ * ```
471
*/
472
async waitAndHandleAssistantToolCalls(
473
userId: string,
packages/providers/anthropic/src/index.ts
+148
-12
@@ -51,6 +51,31 @@ export class AnthropicProvider extends BaseNonAgenticProvider<
51
readonly name = 'anthropic';
52
private chacheTools: boolean = false;
53
54
+ /**
55
+ * Creates a new instance of the AnthropicProvider.
56
+ *
57
+ * @param {Object} [options] - Configuration options for the provider
58
+ * @param {boolean} [options.cacheTools=false] - Whether to cache tools using Anthropic's ephemeral cache
59
+ *
60
+ * @example
61
+ * ```typescript
62
+ * // Initialize with default settings (no caching)
63
+ * const provider = new AnthropicProvider();
64
+ *
65
+ * // Initialize with tool caching enabled
66
+ * const providerWithCaching = new AnthropicProvider({
67
+ * cacheTools: true
68
+ * });
69
+ *
70
+ * // Use with Composio
71
+ * const composio = new Composio({
72
+ * apiKey: 'your-api-key',
73
+ * provider: new AnthropicProvider({
74
+ * cacheTools: true
75
+ * })
76
+ * });
77
+ * ```
78
+ */
79
constructor(options?: { cacheTools?: boolean }) {
80
super();
81
this.chacheTools = options?.cacheTools ?? false;
@@ -58,9 +83,31 @@ export class AnthropicProvider extends BaseNonAgenticProvider<
83
}
84
85
/**
61
- * Wrap a tool in the Anthropic format.
62
- * @param tool - The tool to wrap.
63
- * @returns The wrapped tool in Anthropic format.
86
+ * Wraps a Composio tool in the Anthropic format.
87
+ *
88
+ * This method transforms a Composio tool definition into the format
89
+ * expected by Anthropic's Claude API for tool use.
90
+ *
91
+ * @param tool - The Composio tool to wrap
92
+ * @returns The wrapped tool in Anthropic format
93
+ *
94
+ * @example
95
+ * ```typescript
96
+ * // Wrap a single tool for use with Anthropic
97
+ * const composioTool = {
98
+ * slug: 'SEARCH_TOOL',
99
+ * description: 'Search for information',
100
+ * inputParameters: {
101
+ * type: 'object',
102
+ * properties: {
103
+ * query: { type: 'string' }
104
+ * },
105
+ * required: ['query']
106
+ * }
107
+ * };
108
+ *
109
+ * const anthropicTool = provider.wrapTool(composioTool);
110
+ * ```
111
*/
112
override wrapTool(tool: ComposioTool): AnthropicTool {
113
return {
@@ -76,21 +123,78 @@ export class AnthropicProvider extends BaseNonAgenticProvider<
123
}
124
125
/**
79
- * Wrap a list of tools in the Anthropic format.
80
- * @param tools - The tools to wrap.
81
- * @returns The wrapped tools in Anthropic format.
126
+ * Wraps a list of Composio tools in the Anthropic format.
127
+ *
128
+ * This method transforms multiple Composio tool definitions into the format
129
+ * expected by Anthropic's Claude API for tool use.
130
+ *
131
+ * @param tools - Array of Composio tools to wrap
132
+ * @returns Array of wrapped tools in Anthropic format
133
+ *
134
+ * @example
135
+ * ```typescript
136
+ * // Wrap multiple tools for use with Anthropic
137
+ * const composioTools = [
138
+ * {
139
+ * slug: 'SEARCH_TOOL',
140
+ * description: 'Search for information',
141
+ * inputParameters: {
142
+ * type: 'object',
143
+ * properties: {
144
+ * query: { type: 'string' }
145
+ * }
146
+ * }
147
+ * },
148
+ * {
149
+ * slug: 'WEATHER_TOOL',
150
+ * description: 'Get weather information',
151
+ * inputParameters: {
152
+ * type: 'object',
153
+ * properties: {
154
+ * location: { type: 'string' }
155
+ * }
156
+ * }
157
+ * }
158
+ * ];
159
+ *
160
+ * const anthropicTools = provider.wrapTools(composioTools);
161
+ * ```
162
*/
163
override wrapTools(tools: ComposioTool[]): AnthropicToolCollection {
164
return tools.map(tool => this.wrapTool(tool));
165
}
166
167
/**
88
- * Execute a tool call from Anthropic.
89
- * @param userId - The user ID
168
+ * Executes a tool call from Anthropic's Claude API.
169
+ *
170
+ * This method processes a tool call from Anthropic's Claude API,
171
+ * executes the corresponding Composio tool, and returns the result.
172
+ *
173
+ * @param userId - The user ID for authentication and tracking
174
* @param toolUse - The tool use object from Anthropic
175
* @param options - Additional options for tool execution
176
* @param modifiers - Modifiers for tool execution
93
- * @returns The result of the tool execution as a string
177
+ * @returns The result of the tool execution as a JSON string
178
+ *
179
+ * @example
180
+ * ```typescript
181
+ * // Execute a tool call from Anthropic
182
+ * const toolUse = {
183
+ * type: 'tool_use',
184
+ * id: 'tu_abc123',
185
+ * name: 'SEARCH_TOOL',
186
+ * input: {
187
+ * query: 'composio documentation'
188
+ * }
189
+ * };
190
+ *
191
+ * const result = await provider.executeToolCall(
192
+ * 'user123',
193
+ * toolUse,
194
+ * { connectedAccountId: 'conn_xyz456' }
195
+ * );
196
+ * console.log(JSON.parse(result));
197
+ * ```
198
*/
199
async executeToolCall(
200
userId: string,
@@ -109,12 +213,44 @@ export class AnthropicProvider extends BaseNonAgenticProvider<
213
}
214
215
/**
112
- * Handle tool calls from Anthropic's message response
113
- * @param userId - The user ID
216
+ * Handles tool calls from Anthropic's message response.
217
+ *
218
+ * This method processes tool calls from an Anthropic message response,
219
+ * extracts the tool use blocks, executes each tool call, and returns the results.
220
+ *
221
+ * @param userId - The user ID for authentication and tracking
222
* @param message - The message response from Anthropic
223
* @param options - Additional options for tool execution
224
* @param modifiers - Modifiers for tool execution
117
- * @returns Array of tool execution results
225
+ * @returns Array of tool execution results as JSON strings
226
+ *
227
+ * @example
228
+ * ```typescript
229
+ * // Handle tool calls from an Anthropic message response
230
+ * const anthropic = new Anthropic({ apiKey: 'your-anthropic-api-key' });
231
+ *
232
+ * const message = await anthropic.messages.create({
233
+ * model: 'claude-3-opus-20240229',
234
+ * max_tokens: 1024,
235
+ * tools: provider.wrapTools(composioTools),
236
+ * messages: [
237
+ * {
238
+ * role: 'user',
239
+ * content: 'Search for information about Composio'
240
+ * }
241
+ * ]
242
+ * });
243
+ *
244
+ * // Process any tool calls in the response
245
+ * const results = await provider.handleToolCalls(
246
+ * 'user123',
247
+ * message,
248
+ * { connectedAccountId: 'conn_xyz456' }
249
+ * );
250
+ *
251
+ * // Use the results to continue the conversation
252
+ * console.log(results);
253
+ * ```
254
*/
255
async handleToolCalls(
256
userId: string,
packages/providers/cloudflare/src/index.ts
+144
-4
@@ -26,6 +26,56 @@ export class CloudflareProvider extends BaseNonAgenticProvider<
26
> {
27
readonly name = 'cloudflare';
28
29
+ /**
30
+ * Creates a new instance of the CloudflareProvider.
31
+ *
32
+ * This provider enables integration with Cloudflare AI,
33
+ * allowing Composio tools to be used with Cloudflare Workers AI.
34
+ *
35
+ * @example
36
+ * ```typescript
37
+ *
38
+ * // Use with Composio
39
+ * const composio = new Composio({
40
+ * apiKey: 'your-api-key',
41
+ * provider: new CloudflareProvider()
42
+ * });
43
+ *
44
+ * // Use the provider to wrap tools for Cloudflare AI
45
+ * const cloudflareTools = provider.wrapTools(composioTools);
46
+ * ```
47
+ */
48
+ constructor() {
49
+ super();
50
+ }
51
+
52
+ /**
53
+ * Wraps a Composio tool in the Cloudflare AI format.
54
+ *
55
+ * This method transforms a Composio tool definition into the format
56
+ * expected by Cloudflare's AI API for function calling.
57
+ *
58
+ * @param tool - The Composio tool to wrap
59
+ * @returns The wrapped tool in Cloudflare AI format
60
+ *
61
+ * @example
62
+ * ```typescript
63
+ * // Wrap a single tool for use with Cloudflare AI
64
+ * const composioTool = {
65
+ * slug: 'SEARCH_TOOL',
66
+ * description: 'Search for information',
67
+ * inputParameters: {
68
+ * type: 'object',
69
+ * properties: {
70
+ * query: { type: 'string', description: 'Search query' }
71
+ * },
72
+ * required: ['query']
73
+ * }
74
+ * };
75
+ *
76
+ * const cloudflareTool = provider.wrapTool(composioTool);
77
+ * ```
78
+ */
79
wrapTool(tool: Tool): AiTextGenerationToolInput {
80
const formattedSchema: AiTextGenerationToolInput['function'] = {
81
name: tool.slug!,
@@ -48,6 +98,56 @@ export class CloudflareProvider extends BaseNonAgenticProvider<
98
return cloudflareTool;
99
}
100
101
+ /**
102
+ * Wraps a list of Composio tools in the Cloudflare AI format.
103
+ *
104
+ * This method transforms multiple Composio tool definitions into the format
105
+ * expected by Cloudflare's AI API for function calling, organizing them
106
+ * into a dictionary keyed by tool slug.
107
+ *
108
+ * @param tools - Array of Composio tools to wrap
109
+ * @returns Dictionary of wrapped tools in Cloudflare AI format
110
+ *
111
+ * @example
112
+ * ```typescript
113
+ * // Wrap multiple tools for use with Cloudflare AI
114
+ * const composioTools = [
115
+ * {
116
+ * slug: 'SEARCH_TOOL',
117
+ * description: 'Search for information',
118
+ * inputParameters: {
119
+ * type: 'object',
120
+ * properties: {
121
+ * query: { type: 'string' }
122
+ * },
123
+ * required: ['query']
124
+ * }
125
+ * },
126
+ * {
127
+ * slug: 'WEATHER_TOOL',
128
+ * description: 'Get weather information',
129
+ * inputParameters: {
130
+ * type: 'object',
131
+ * properties: {
132
+ * location: { type: 'string' }
133
+ * },
134
+ * required: ['location']
135
+ * }
136
+ * }
137
+ * ];
138
+ *
139
+ * const cloudflareTools = provider.wrapTools(composioTools);
140
+ *
141
+ * // Use with Cloudflare Workers AI
142
+ * // In your Cloudflare Worker:
143
+ * const ai = new Ai(env.AI);
144
+ *
145
+ * const response = await ai.run('@cf/meta/llama-3-8b-instruct', {
146
+ * messages: [{ role: 'user', content: 'How is the weather in New York?' }],
147
+ * tools: cloudflareTools
148
+ * });
149
+ * ```
150
+ */
151
wrapTools(tools: Tool[]): AiToolCollection {
152
return tools.reduce(
153
(acc, tool) => ({
@@ -59,10 +159,50 @@ export class CloudflareProvider extends BaseNonAgenticProvider<
159
}
160
161
/**
62
- * Execute a tool call.
63
- * @param tool - The tool to execute.
64
- * @param userId - The user id.
65
- * @returns The results of the tool call.
162
+ * Executes a tool call from Cloudflare AI.
163
+ *
164
+ * This method processes a function call from Cloudflare's AI API,
165
+ * executes the corresponding Composio tool, and returns the result.
166
+ *
167
+ * @param userId - The user ID for authentication and tracking
168
+ * @param tool - The tool call object with name and arguments
169
+ * @param options - Optional execution options like connected account ID
170
+ * @param modifiers - Optional execution modifiers for tool behavior
171
+ * @returns The result of the tool execution as a JSON string
172
+ *
173
+ * @example
174
+ * ```typescript
175
+ * // Execute a tool call from Cloudflare AI
176
+ * const toolCall = {
177
+ * name: 'SEARCH_TOOL',
178
+ * arguments: {
179
+ * query: 'composio documentation'
180
+ * }
181
+ * };
182
+ *
183
+ * const result = await provider.executeToolCall(
184
+ * 'user123',
185
+ * toolCall,
186
+ * { connectedAccountId: 'conn_xyz456' }
187
+ * );
188
+ *
189
+ * // Parse the result and use it in your application
190
+ * const searchResults = JSON.parse(result);
191
+ * console.log(searchResults);
192
+ *
193
+ * // You can also use the result to continue the conversation
194
+ * // In your Cloudflare Worker:
195
+ * const ai = new Ai(env.AI);
196
+ *
197
+ * await ai.run('@cf/meta/llama-3-8b-instruct', {
198
+ * messages: [
199
+ * { role: 'user', content: 'Search for Composio' },
200
+ * { role: 'assistant', content: '', tool_calls: [{ name: 'SEARCH_TOOL', arguments: JSON.stringify({ query: 'composio documentation' }) }] },
201
+ * { role: 'tool', tool_call_id: '1', content: result }
202
+ * ],
203
+ * tools: cloudflareTools
204
+ * });
205
+ * ```
206
*/
207
async executeToolCall(
208
userId: string,
packages/providers/google/src/index.ts
+147
-12
@@ -44,9 +44,64 @@ export class GoogleProvider extends BaseNonAgenticProvider<GoogleGenAIToolCollec
44
readonly name = 'google';
45
46
/**
47
- * Wrap a Composio tool in the Google GenAI function declaration format.
48
- * @param tool - The Composio tool to wrap.
49
- * @returns The wrapped tool in Google GenAI format.
47
+ * Creates a new instance of the GoogleProvider.
48
+ *
49
+ * This provider enables integration with Google's GenAI API,
50
+ * supporting both the Gemini Developer API and Vertex AI implementations.
51
+ *
52
+ * @example
53
+ * ```typescript
54
+ * // Initialize the Google provider
55
+ * const provider = new GoogleProvider();
56
+ *
57
+ * // Use with Composio
58
+ * const composio = new Composio({
59
+ * apiKey: 'your-api-key',
60
+ * provider: new GoogleProvider()
61
+ * });
62
+ *
63
+ * // Use the provider to wrap tools for Google GenAI
64
+ * const googleTools = provider.wrapTools(composioTools);
65
+ * ```
66
+ */
67
+ constructor() {
68
+ super();
69
+ }
70
+
71
+ /**
72
+ * Wraps a Composio tool in the Google GenAI function declaration format.
73
+ *
74
+ * This method transforms a Composio tool definition into the format
75
+ * expected by Google's GenAI API for function calling.
76
+ *
77
+ * @param tool - The Composio tool to wrap
78
+ * @returns The wrapped tool in Google GenAI format
79
+ *
80
+ * @example
81
+ * ```typescript
82
+ * // Wrap a single tool for use with Google GenAI
83
+ * const composioTool = {
84
+ * slug: 'SEARCH_TOOL',
85
+ * description: 'Search for information',
86
+ * inputParameters: {
87
+ * type: 'object',
88
+ * properties: {
89
+ * query: { type: 'string' }
90
+ * },
91
+ * required: ['query']
92
+ * }
93
+ * };
94
+ *
95
+ * const googleTool = provider.wrapTool(composioTool);
96
+ * // Use with Google GenAI SDK
97
+ * const genAI = new GoogleGenerativeAI('YOUR_API_KEY');
98
+ * const model = genAI.getGenerativeModel({ model: 'gemini-pro' });
99
+ *
100
+ * const result = await model.generateContent({
101
+ * contents: [{ role: 'user', parts: [{ text: 'Search for Composio' }] }],
102
+ * tools: [googleTool]
103
+ * });
104
+ * ```
105
*/
106
wrapTool(tool: Tool): GoogleTool {
107
return {
@@ -62,21 +117,101 @@ export class GoogleProvider extends BaseNonAgenticProvider<GoogleGenAIToolCollec
117
}
118
119
/**
65
- * Wrap a list of Composio tools in the Google GenAI function declaration format.
66
- * @param tools - The Composio tools to wrap.
67
- * @returns The wrapped tools in Google GenAI format.
120
+ * Wraps a list of Composio tools in the Google GenAI function declaration format.
121
+ *
122
+ * This method transforms multiple Composio tool definitions into the format
123
+ * expected by Google's GenAI API for function calling.
124
+ *
125
+ * @param tools - Array of Composio tools to wrap
126
+ * @returns Array of wrapped tools in Google GenAI format
127
+ *
128
+ * @example
129
+ * ```typescript
130
+ * // Wrap multiple tools for use with Google GenAI
131
+ * const composioTools = [
132
+ * {
133
+ * slug: 'SEARCH_TOOL',
134
+ * description: 'Search for information',
135
+ * inputParameters: {
136
+ * type: 'object',
137
+ * properties: {
138
+ * query: { type: 'string' }
139
+ * },
140
+ * required: ['query']
141
+ * }
142
+ * },
143
+ * {
144
+ * slug: 'WEATHER_TOOL',
145
+ * description: 'Get weather information',
146
+ * inputParameters: {
147
+ * type: 'object',
148
+ * properties: {
149
+ * location: { type: 'string' }
150
+ * },
151
+ * required: ['location']
152
+ * }
153
+ * }
154
+ * ];
155
+ *
156
+ * const googleTools = provider.wrapTools(composioTools);
157
+ *
158
+ * // Use with Google GenAI SDK
159
+ * const genAI = new GoogleGenerativeAI('YOUR_API_KEY');
160
+ * const model = genAI.getGenerativeModel({ model: 'gemini-pro' });
161
+ *
162
+ * const result = await model.generateContent({
163
+ * contents: [{ role: 'user', parts: [{ text: 'How is the weather in New York?' }] }],
164
+ * tools: googleTools
165
+ * });
166
+ * ```
167
*/
168
wrapTools(tools: Tool[]): GoogleGenAIToolCollection {
169
return tools.map(tool => this.wrapTool(tool));
170
}
171
172
/**
74
- * Execute a tool call from Google GenAI.
75
- * @param userId - The user id.
76
- * @param tool - The Google GenAI function call to execute.
77
- * @param options - Optional execution options.
78
- * @param modifiers - Optional execution modifiers.
79
- * @returns The result of the tool call as a JSON string.
173
+ * Executes a tool call from Google GenAI.
174
+ *
175
+ * This method processes a function call from Google's GenAI API,
176
+ * executes the corresponding Composio tool, and returns the result.
177
+ *
178
+ * @param userId - The user ID for authentication and tracking
179
+ * @param tool - The Google GenAI function call to execute
180
+ * @param options - Optional execution options like connected account ID
181
+ * @param modifiers - Optional execution modifiers for tool behavior
182
+ * @returns The result of the tool execution as a JSON string
183
+ *
184
+ * @example
185
+ * ```typescript
186
+ * // Execute a tool call from Google GenAI
187
+ * const functionCall = {
188
+ * name: 'SEARCH_TOOL',
189
+ * args: {
190
+ * query: 'composio documentation'
191
+ * }
192
+ * };
193
+ *
194
+ * const result = await provider.executeToolCall(
195
+ * 'user123',
196
+ * functionCall,
197
+ * { connectedAccountId: 'conn_xyz456' }
198
+ * );
199
+ *
200
+ * // Parse the result and use it in your application
201
+ * const searchResults = JSON.parse(result);
202
+ * console.log(searchResults);
203
+ *
204
+ * // You can also use the result to continue the conversation
205
+ * const genAI = new GoogleGenerativeAI('YOUR_API_KEY');
206
+ * const model = genAI.getGenerativeModel({ model: 'gemini-pro' });
207
+ *
208
+ * await model.generateContent({
209
+ * contents: [
210
+ * { role: 'user', parts: [{ text: 'Search for Composio' }] },
211
+ * { role: 'model', parts: [{ functionResponse: { name: 'SEARCH_TOOL', response: result } }] }
212
+ * ]
213
+ * });
214
+ * ```
215
*/
216
async executeToolCall(
217
userId: string,
packages/providers/langchain/src/index.ts
+138
-4
@@ -18,11 +18,83 @@ export class LangchainProvider extends BaseAgenticProvider<
18
DynamicStructuredTool
19
> {
20
readonly name = 'langchain';
21
+
22
/**
22
- * Abstract method to wrap a tool in the provider.
23
- * This method is implemented by the provider.
24
- * @param tool - The tool to wrap.
25
- * @returns The wrapped tool.
23
+ * Creates a new instance of the LangchainProvider.
24
+ *
25
+ * This provider enables integration with the Langchain framework,
26
+ * allowing Composio tools to be used with Langchain agents and chains.
27
+ *
28
+ * @example
29
+ * ```typescript
30
+ * // Initialize the Langchain provider
31
+ * const provider = new LangchainProvider();
32
+ *
33
+ * // Use with Composio
34
+ * const composio = new Composio({
35
+ * apiKey: 'your-api-key',
36
+ * provider: new LangchainProvider()
37
+ * });
38
+ *
39
+ * // Use the provider to wrap tools for Langchain
40
+ * const langchainTools = provider.wrapTools(composioTools, composio.tools.execute);
41
+ * ```
42
+ */
43
+ constructor() {
44
+ super();
45
+ }
46
+
47
+ /**
48
+ * Wraps a Composio tool in the Langchain DynamicStructuredTool format.
49
+ *
50
+ * This method transforms a Composio tool definition into a Langchain
51
+ * DynamicStructuredTool that can be used with Langchain agents and chains.
52
+ *
53
+ * @param tool - The Composio tool to wrap
54
+ * @param executeTool - Function to execute the tool
55
+ * @returns The wrapped tool as a Langchain DynamicStructuredTool
56
+ *
57
+ * @example
58
+ * ```typescript
59
+ * // Wrap a single tool for use with Langchain
60
+ * const composioTool = {
61
+ * slug: 'SEARCH_TOOL',
62
+ * description: 'Search for information',
63
+ * toolkit: { name: 'search_toolkit' },
64
+ * inputParameters: {
65
+ * type: 'object',
66
+ * properties: {
67
+ * query: { type: 'string' }
68
+ * },
69
+ * required: ['query']
70
+ * }
71
+ * };
72
+ *
73
+ * // Create a Langchain tool using the provider
74
+ * const langchainTool = provider.wrapTool(
75
+ * composioTool,
76
+ * composio.tools.execute
77
+ * );
78
+ *
79
+ * // Use with Langchain
80
+ * import { AgentExecutor, createOpenAIFunctionsAgent } from 'langchain/agents';
81
+ * import { ChatOpenAI } from '@langchain/openai';
82
+ *
83
+ * const model = new ChatOpenAI({ temperature: 0 });
84
+ * const agent = await createOpenAIFunctionsAgent({
85
+ * llm: model,
86
+ * tools: [langchainTool]
87
+ * });
88
+ *
89
+ * const executor = new AgentExecutor({
90
+ * agent,
91
+ * tools: [langchainTool]
92
+ * });
93
+ *
94
+ * const result = await executor.invoke({
95
+ * input: "Search for information about Composio"
96
+ * });
97
+ * ```
98
*/
99
wrapTool(tool: Tool, executeTool: ExecuteToolFn): DynamicStructuredTool {
100
const toolName = tool.slug;
@@ -47,6 +119,68 @@ export class LangchainProvider extends BaseAgenticProvider<
119
});
120
}
121
122
+ /**
123
+ * Wraps a list of Composio tools in the Langchain DynamicStructuredTool format.
124
+ *
125
+ * This method transforms multiple Composio tool definitions into Langchain
126
+ * DynamicStructuredTools that can be used with Langchain agents and chains.
127
+ *
128
+ * @param tools - Array of Composio tools to wrap
129
+ * @param executeTool - Function to execute the tools
130
+ * @returns Array of wrapped tools as Langchain DynamicStructuredTools
131
+ *
132
+ * @example
133
+ * ```typescript
134
+ * // Wrap multiple tools for use with Langchain
135
+ * const composioTools = [
136
+ * {
137
+ * slug: 'SEARCH_TOOL',
138
+ * description: 'Search for information',
139
+ * toolkit: { name: 'search_toolkit' },
140
+ * inputParameters: {
141
+ * type: 'object',
142
+ * properties: {
143
+ * query: { type: 'string' }
144
+ * },
145
+ * required: ['query']
146
+ * }
147
+ * },
148
+ * {
149
+ * slug: 'WEATHER_TOOL',
150
+ * description: 'Get weather information',
151
+ * toolkit: { name: 'weather_toolkit' },
152
+ * inputParameters: {
153
+ * type: 'object',
154
+ * properties: {
155
+ * location: { type: 'string' }
156
+ * },
157
+ * required: ['location']
158
+ * }
159
+ * }
160
+ * ];
161
+ *
162
+ * // Create Langchain tools using the provider
163
+ * const langchainTools = provider.wrapTools(
164
+ * composioTools,
165
+ * composio.tools.execute
166
+ * );
167
+ *
168
+ * // Use with Langchain
169
+ * import { AgentExecutor, createOpenAIFunctionsAgent } from 'langchain/agents';
170
+ * import { ChatOpenAI } from '@langchain/openai';
171
+ *
172
+ * const model = new ChatOpenAI({ temperature: 0 });
173
+ * const agent = await createOpenAIFunctionsAgent({
174
+ * llm: model,
175
+ * tools: langchainTools
176
+ * });
177
+ *
178
+ * const executor = new AgentExecutor({
179
+ * agent,
180
+ * tools: langchainTools
181
+ * });
182
+ * ```
183
+ */
184
wrapTools(tools: Tool[], executeTool: ExecuteToolFn): LangChainToolCollection {
185
return tools.map(tool => this.wrapTool(tool, executeTool));
186
}
packages/providers/vercel/src/index.ts
+133
-6
@@ -19,9 +19,78 @@ export class VercelProvider extends BaseAgenticProvider<VercelToolCollection, Ve
19
readonly name = 'vercel';
20
21
/**
22
- * Wrap a Composio tool in a Vercel tool.
23
- * @param {ComposioTool} composioTool - The Composio tool to wrap.
24
- * @returns {VercelTool} The wrapped Vercel tool.
22
+ * Creates a new instance of the VercelProvider.
23
+ *
24
+ * This provider enables integration with the Vercel AI SDK,
25
+ * allowing Composio tools to be used with Vercel AI applications.
26
+ *
27
+ * @example
28
+ * ```typescript
29
+ * // Initialize the Vercel provider
30
+ * const provider = new VercelProvider();
31
+ *
32
+ * // Use with Composio
33
+ * const composio = new Composio({
34
+ * apiKey: 'your-api-key',
35
+ * provider: new VercelProvider()
36
+ * });
37
+ *
38
+ * // Use the provider to wrap tools for Vercel AI SDK
39
+ * const vercelTools = provider.wrapTools(composioTools, composio.tools.execute);
40
+ * ```
41
+ */
42
+ constructor() {
43
+ super();
44
+ }
45
+
46
+ /**
47
+ * Wraps a Composio tool in a Vercel AI SDK tool format.
48
+ *
49
+ * This method transforms a Composio tool definition into the format
50
+ * expected by Vercel's AI SDK for function calling.
51
+ *
52
+ * @param {ComposioTool} composioTool - The Composio tool to wrap
53
+ * @param {ExecuteToolFn} executeTool - Function to execute the tool
54
+ * @returns {VercelTool} The wrapped Vercel tool
55
+ *
56
+ * @example
57
+ * ```typescript
58
+ * // Wrap a single tool for use with Vercel AI SDK
59
+ * const composioTool = {
60
+ * slug: 'SEARCH_TOOL',
61
+ * description: 'Search for information',
62
+ * inputParameters: {
63
+ * type: 'object',
64
+ * properties: {
65
+ * query: { type: 'string' }
66
+ * },
67
+ * required: ['query']
68
+ * }
69
+ * };
70
+ *
71
+ * // Create a Vercel tool using the provider
72
+ * const vercelTool = provider.wrapTool(
73
+ * composioTool,
74
+ * composio.tools.execute
75
+ * );
76
+ *
77
+ * // Use with Vercel AI SDK
78
+ * import { StreamingTextResponse, Message } from 'ai';
79
+ * import { OpenAI } from 'openai';
80
+ *
81
+ * export async function POST(req: Request) {
82
+ * const { messages } = await req.json();
83
+ * const openai = new OpenAI();
84
+ *
85
+ * const response = await openai.chat.completions.create({
86
+ * model: 'gpt-4',
87
+ * messages,
88
+ * tools: [vercelTool]
89
+ * });
90
+ *
91
+ * return new StreamingTextResponse(response.choices[0].message);
92
+ * }
93
+ * ```
94
*/
95
wrapTool(composioTool: ComposioTool, executeTool: ExecuteToolFn): VercelTool {
96
return tool({
@@ -35,9 +104,67 @@ export class VercelProvider extends BaseAgenticProvider<VercelToolCollection, Ve
104
}
105
106
/**
38
- * Wrap a list of Composio tools as a Vercel tool collection.
39
- * @param {ComposioTool[]} tools - The list of Composio tools to wrap.
40
- * @returns {VercelToolCollection} The wrapped Vercel tool collection.
107
+ * Wraps a list of Composio tools as a Vercel AI SDK tool collection.
108
+ *
109
+ * This method transforms multiple Composio tool definitions into the format
110
+ * expected by Vercel's AI SDK for function calling, organizing them
111
+ * into a dictionary keyed by tool slug.
112
+ *
113
+ * @param {ComposioTool[]} tools - Array of Composio tools to wrap
114
+ * @param {ExecuteToolFn} executeTool - Function to execute the tools
115
+ * @returns {VercelToolCollection} Dictionary of wrapped tools in Vercel AI SDK format
116
+ *
117
+ * @example
118
+ * ```typescript
119
+ * // Wrap multiple tools for use with Vercel AI SDK
120
+ * const composioTools = [
121
+ * {
122
+ * slug: 'SEARCH_TOOL',
123
+ * description: 'Search for information',
124
+ * inputParameters: {
125
+ * type: 'object',
126
+ * properties: {
127
+ * query: { type: 'string' }
128
+ * },
129
+ * required: ['query']
130
+ * }
131
+ * },
132
+ * {
133
+ * slug: 'WEATHER_TOOL',
134
+ * description: 'Get weather information',
135
+ * inputParameters: {
136
+ * type: 'object',
137
+ * properties: {
138
+ * location: { type: 'string' }
139
+ * },
140
+ * required: ['location']
141
+ * }
142
+ * }
143
+ * ];
144
+ *
145
+ * // Create Vercel tools using the provider
146
+ * const vercelTools = provider.wrapTools(
147
+ * composioTools,
148
+ * composio.tools.execute
149
+ * );
150
+ *
151
+ * // Use with Vercel AI SDK in a Next.js API route
152
+ * import { StreamingTextResponse } from 'ai';
153
+ * import { OpenAI } from 'openai';
154
+ *
155
+ * export async function POST(req: Request) {
156
+ * const { messages } = await req.json();
157
+ * const openai = new OpenAI();
158
+ *
159
+ * const response = await openai.chat.completions.create({
160
+ * model: 'gpt-4',
161
+ * messages,
162
+ * tools: Object.values(vercelTools)
163
+ * });
164
+ *
165
+ * return new StreamingTextResponse(response.choices[0].message);
166
+ * }
167
+ * ```
168
*/
169
wrapTools(tools: ComposioTool[], executeTool: ExecuteToolFn): VercelToolCollection {
170
return tools.reduce((acc, tool) => {