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, 03:00 UTC
Add toolkitslug to modifiers
haxzie committed
May 16, 2025 at 14:42 UTC
e02573ff73cd7bf5ea66b7e9e4a2653ee7cbcbe9
3 files changed
+86
-47
examples/connected-accounts/src/index.ts
+1
-1
@@ -38,7 +38,7 @@ const authConfig = await composio.createAuthConfig('my-toolkit', {
38
*/
39
// hmm so the createConnectedAccount here doesn't feel like i am initiating a connection request -> that is probably why it was called initiateConnection
40
// connectedAccounts.initiate or initialize work
41
-const ConnectionRequest = await composio.createConnectedAccount('default', authConfig.id);
41
+const ConnectionRequest = await composio.initiateConnection('default', authConfig.id);
42
const connectedAccount = await ConnectionRequest.waitForConnection();
43
44
console.log(connectedAccount);
packages/core/src/models/Tools.ts
+78
-45
@@ -237,7 +237,7 @@ export class Tools<
237
if (modifier) {
238
if (typeof modifier === 'function') {
239
modifiedTools = modifiedTools.map(tool => {
240
- return modifier(tool.slug, tool);
240
+ return modifier(tool.slug, tool.toolkit?.slug || 'unkown', tool);
241
});
242
} else {
243
throw new Error('Invalid schema modifier. Not a function.');
@@ -272,7 +272,7 @@ export class Tools<
272
// apply local modifiers if they are provided
273
if (modifier) {
274
if (typeof modifier === 'function') {
275
- modifiedToool = modifier(slug, modifiedToool);
275
+ modifiedToool = modifier(slug, modifiedToool.toolkit?.slug || 'unkown', modifiedToool);
276
} else {
277
throw new Error('Invalid schema modifier. Not a function.');
278
}
@@ -329,6 +329,75 @@ export class Tools<
329
return executeToolFn;
330
}
331
332
+ private async handleCustomToolExecution(
333
+ tool: Tool,
334
+ body: ToolExecuteParams,
335
+ modifiers?: ExecuteToolModifiers
336
+ ): Promise<ToolExecuteResponse> {
337
+ if (modifiers?.beforeToolExecute) {
338
+ if (typeof modifiers.beforeToolExecute === 'function') {
339
+ body = modifiers.beforeToolExecute(tool.slug, 'unkown', body);
340
+ } else {
341
+ throw new Error('Invalid beforeToolExecute modifier. Not a function.');
342
+ }
343
+ }
344
+
345
+ let result = await this.customTools.executeCustomTool(tool.slug, body, {
346
+ userId: body.userId || 'default',
347
+ connectedAccountId: body.connectedAccountId,
348
+ });
349
+
350
+ if (modifiers?.afterToolExecute) {
351
+ if (typeof modifiers.afterToolExecute === 'function') {
352
+ result = modifiers.afterToolExecute(tool.slug, 'unkown', result);
353
+ }
354
+ }
355
+
356
+ return result;
357
+ }
358
+
359
+ private async handleComposioToolExecution(
360
+ tool: Tool,
361
+ body: ToolExecuteParams,
362
+ modifiers?: ExecuteToolModifiers
363
+ ): Promise<ToolExecuteResponse> {
364
+ if (modifiers?.beforeToolExecute) {
365
+ if (typeof modifiers.beforeToolExecute === 'function') {
366
+ body = modifiers.beforeToolExecute(tool.slug, tool.toolkit?.slug || 'unkown', body);
367
+ } else {
368
+ throw new Error('Invalid beforeToolExecute modifier. Not a function.');
369
+ }
370
+ }
371
+ // fetch connected accounts if doesn't exist
372
+ let connectedAccountId = body.connectedAccountId;
373
+ if (!connectedAccountId) {
374
+ connectedAccountId =
375
+ (await this.getConnectedAccountIdForTool(body.userId, tool.slug)) || undefined;
376
+ }
377
+
378
+ let result = await this.client.tools.execute(tool.slug, {
379
+ allow_tracing: body.allowTracing,
380
+ connected_account_id: body.connectedAccountId,
381
+ custom_auth_params: body.customAuthParams,
382
+ arguments: body.arguments,
383
+ entity_id: body.userId,
384
+ version: body.version,
385
+ text: body.text,
386
+ });
387
+ // apply transformations to the response
388
+ result = this.transformToolExecuteResponse(result);
389
+
390
+ // apply local modifiers if they are provided
391
+ if (modifiers?.afterToolExecute) {
392
+ if (typeof modifiers.afterToolExecute === 'function') {
393
+ result = modifiers.afterToolExecute(tool.slug, tool.toolkit?.slug || 'unkown', result);
394
+ } else {
395
+ throw new Error('Invalid afterToolExecute modifier. Not a function.');
396
+ }
397
+ }
398
+ return result;
399
+ }
400
+
401
/**
402
* Exectes a given tool with the provided parameters.
403
*
@@ -350,54 +419,18 @@ export class Tools<
419
}
420
421
try {
353
- // apply local modifiers if they are provided
354
- if (modifiers?.beforeToolExecute) {
355
- if (typeof modifiers.beforeToolExecute === 'function') {
356
- body = modifiers.beforeToolExecute(slug, body);
357
- } else {
358
- throw new Error('Invalid beforeToolExecute modifier. Not a function.');
359
- }
360
- }
361
-
362
- let result: ToolExecuteResponse;
363
- // check if the tool is a custom tool
422
const customTool = await this.customTools.getCustomToolBySlug(slug);
423
if (customTool) {
366
- result = await this.customTools.executeCustomTool(slug, body, {
367
- userId: body.userId || 'default',
368
- connectedAccountId: body.connectedAccountId,
369
- });
424
+ // handle custom tool execution
425
+ return this.handleCustomToolExecution(customTool, body, modifiers);
426
} else {
371
- // fetch connected accounts if doesn't exist
372
- let connectedAccountId = body.connectedAccountId;
373
- if (!connectedAccountId) {
374
- connectedAccountId =
375
- (await this.getConnectedAccountIdForTool(body.userId, slug)) || undefined;
376
- }
377
-
378
- result = await this.client.tools.execute(slug, {
379
- allow_tracing: body.allowTracing,
380
- connected_account_id: body.connectedAccountId,
381
- custom_auth_params: body.customAuthParams,
382
- arguments: body.arguments,
383
- entity_id: body.userId,
384
- version: body.version,
385
- text: body.text,
386
- });
387
- // apply transformations to the response
388
- result = this.transformToolExecuteResponse(result);
389
- }
390
-
391
- // apply local modifiers if they are provided
392
- if (modifiers?.afterToolExecute) {
393
- if (typeof modifiers.afterToolExecute === 'function') {
394
- result = modifiers.afterToolExecute(slug, result);
395
- } else {
396
- throw new Error('Invalid afterToolExecute modifier. Not a function.');
427
+ // handle composio tool execution
428
+ const composioTool = await this.getComposioToolBySlug(body.userId, slug);
429
+ if (!composioTool) {
430
+ throw new Error(`Tool with slug ${slug} not found`);
431
}
432
+ return this.handleComposioToolExecution(composioTool, body, modifiers);
433
}
399
-
400
- return result;
434
} catch (error) {
435
logger.error(`Error executing tool ${slug}: ${error}`);
436
throw error;
packages/core/src/types/modifiers.types.ts
+7
-1
@@ -7,6 +7,7 @@ import { ToolExecuteParams, ToolExecuteResponse, Tool } from './tool.types';
7
*/
8
export type BeforeToolExecuteModifier = (
9
toolSlug: string,
10
+ toolkitSlug: string,
11
toolExecuteParams: ToolExecuteParams
12
) => ToolExecuteParams;
13
@@ -16,6 +17,7 @@ export type BeforeToolExecuteModifier = (
17
*/
18
export type AfterToolExecuteModifier = (
19
toolSlug: string,
20
+ toolkitSlug: string,
21
toolExecuteResponse: ToolExecuteResponse
22
) => ToolExecuteResponse;
23
@@ -23,7 +25,11 @@ export type AfterToolExecuteModifier = (
25
* Modifier for altering the tool schema
26
* Called after the tool schema is retrieved
27
*/
26
-export type TransformToolSchemaModifier = (toolSlug: string, toolSchema: Tool) => Tool;
28
+export type TransformToolSchemaModifier = (
29
+ toolSlug: string,
30
+ toolkitSlug: string,
31
+ toolSchema: Tool
32
+) => Tool;
33
34
/**
35
* Non Agentic tool options. These are used by Non-Agentic toolsets