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, 01:45 UTC
Allow modifiers to be async
haxzie committed
May 29, 2025 at 11:04 UTC
d65bd3e8d7a55e3b71f0f620c4e500c10ab988e1
2 files changed
+16
-14
packages/core/src/models/Tools.ts
+13
-11
@@ -276,7 +276,7 @@ export class Tools<
276
return [];
277
}
278
const caseTransformedTools = tools.items.map(tool => this.transformToolCases(tool));
279
- // @TODO add checks for connectedAccounts for fetched tools
279
+ // @TODO: Add checks for connectedAccounts for fetched tools
280
// const connectedAccountExists = await this.checkIfConnectedAccountExistsForTools(
281
// userId,
282
// caseTransformedTools
@@ -284,7 +284,6 @@ export class Tools<
284
// if (!connectedAccountExists) {
285
// console.warn('No connected accounts found for the given tools');
286
// }
287
-
287
const customTools = await this.customTools.getCustomTools({
288
toolSlugs: queryParams.data.tools,
289
});
@@ -294,9 +293,10 @@ export class Tools<
293
// apply local modifiers if they are provided
294
if (modifier) {
295
if (typeof modifier === 'function') {
297
- modifiedTools = modifiedTools.map(tool => {
298
- return modifier(tool.slug, tool.toolkit?.slug ?? 'unknown', tool);
299
- });
296
+ const modifiedPromises = modifiedTools.map(tool =>
297
+ modifier(tool.slug, tool.toolkit?.slug ?? 'unknown', tool)
298
+ );
299
+ modifiedTools = await Promise.all(modifiedPromises);
300
} else {
301
throw new ComposioInvalidModifierError('Invalid schema modifier. Not a function.');
302
}
@@ -341,7 +341,7 @@ export class Tools<
341
// apply local modifiers if they are provided
342
if (modifier) {
343
if (typeof modifier === 'function') {
344
- modifiedTool = modifier(slug, modifiedTool.toolkit?.slug ?? 'unknown', modifiedTool);
344
+ modifiedTool = await modifier(slug, modifiedTool.toolkit?.slug ?? 'unknown', modifiedTool);
345
} else {
346
throw new ComposioInvalidModifierError('Invalid schema modifier. Not a function.');
347
}
@@ -437,8 +437,10 @@ export class Tools<
437
438
/**
439
* @internal
440
+ * @description
441
* Creates a function that executes a tool.
441
- * This function is used by agentic the toolsets to execute the tool
442
+ * This function is used by agentic toolsets to execute the tool
443
+ *
444
* @param {string} userId - The user id
445
* @param {ExecuteToolModifiers} modifiers - The modifiers to be applied to the tool
446
* @returns {ExecuteToolFn} The execute tool function
@@ -472,7 +474,7 @@ export class Tools<
474
): Promise<ToolExecuteResponse> {
475
if (modifiers?.beforeExecute) {
476
if (typeof modifiers.beforeExecute === 'function') {
475
- body = modifiers.beforeExecute(tool.slug, 'unknown', body);
477
+ body = await modifiers.beforeExecute(tool.slug, 'unknown', body);
478
} else {
479
throw new ComposioInvalidModifierError('Invalid beforeExecute modifier. Not a function.');
480
}
@@ -485,7 +487,7 @@ export class Tools<
487
488
if (modifiers?.afterExecute) {
489
if (typeof modifiers.afterExecute === 'function') {
488
- result = modifiers.afterExecute(tool.slug, 'unknown', result);
490
+ result = await modifiers.afterExecute(tool.slug, 'unknown', result);
491
} else {
492
throw new ComposioInvalidModifierError('Invalid afterExecute modifier. Not a function.');
493
}
@@ -509,7 +511,7 @@ export class Tools<
511
): Promise<ToolExecuteResponse> {
512
if (modifiers?.beforeExecute) {
513
if (typeof modifiers.beforeExecute === 'function') {
512
- body = modifiers.beforeExecute(tool.slug, tool.toolkit?.slug ?? 'unknown', body);
514
+ body = await modifiers.beforeExecute(tool.slug, tool.toolkit?.slug ?? 'unknown', body);
515
} else {
516
throw new ComposioInvalidModifierError('Invalid beforeExecute modifier. Not a function.');
517
}
@@ -536,7 +538,7 @@ export class Tools<
538
// apply local modifiers if they are provided
539
if (modifiers?.afterExecute) {
540
if (typeof modifiers.afterExecute === 'function') {
539
- result = modifiers.afterExecute(tool.slug, tool.toolkit?.slug ?? 'unknown', result);
541
+ result = await modifiers.afterExecute(tool.slug, tool.toolkit?.slug ?? 'unknown', result);
542
} else {
543
throw new ComposioInvalidModifierError('Invalid afterExecute modifier. Not a function.');
544
}
packages/core/src/types/modifiers.types.ts
+3
-3
@@ -52,7 +52,7 @@ export type beforeExecuteModifier = (
52
toolSlug: string,
53
toolkitSlug: string,
54
toolExecuteParams: ToolExecuteParams
55
-) => ToolExecuteParams;
55
+) => Promise<ToolExecuteParams>;
56
57
/**
58
* Modifier for altering the tool execution response after execution completes.
@@ -108,7 +108,7 @@ export type afterExecuteModifier = (
108
toolSlug: string,
109
toolkitSlug: string,
110
toolExecuteResponse: ToolExecuteResponse
111
-) => ToolExecuteResponse;
111
+) => Promise<ToolExecuteResponse>;
112
113
/**
114
* Modifier for altering the tool schema before it's exposed to consumers.
@@ -173,7 +173,7 @@ export type TransformToolSchemaModifier = (
173
toolSlug: string,
174
toolkitSlug: string,
175
toolSchema: Tool
176
-) => Tool;
176
+) => Tool | Promise<Tool>;
177
178
/**
179
* Options for non-agentic tool configuration.