Skip to content

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

Add toolkitslug to modifiers

haxziee02573ff73cd
diff --git a/examples/connected-accounts/src/index.ts b/examples/connected-accounts/src/index.ts
index e65a20a72..9587414e4 100644
--- a/examples/connected-accounts/src/index.ts
+++ b/examples/connected-accounts/src/index.ts
@@ -38,7 +38,7 @@ const authConfig = await composio.createAuthConfig('my-toolkit', {
*/
// hmm so the createConnectedAccount here doesn't feel like i am initiating a connection request -> that is probably why it was called initiateConnection
// connectedAccounts.initiate or initialize work
-const ConnectionRequest = await composio.createConnectedAccount('default', authConfig.id);
+const ConnectionRequest = await composio.initiateConnection('default', authConfig.id);
const connectedAccount = await ConnectionRequest.waitForConnection();
console.log(connectedAccount);
diff --git a/packages/core/src/models/Tools.ts b/packages/core/src/models/Tools.ts
index d62977d34..3324b104e 100644
--- a/packages/core/src/models/Tools.ts
+++ b/packages/core/src/models/Tools.ts
@@ -237,7 +237,7 @@ export class Tools<
if (modifier) {
if (typeof modifier === 'function') {
modifiedTools = modifiedTools.map(tool => {
- return modifier(tool.slug, tool);
+ return modifier(tool.slug, tool.toolkit?.slug || 'unkown', tool);
});
} else {
throw new Error('Invalid schema modifier. Not a function.');
@@ -272,7 +272,7 @@ export class Tools<
// apply local modifiers if they are provided
if (modifier) {
if (typeof modifier === 'function') {
- modifiedToool = modifier(slug, modifiedToool);
+ modifiedToool = modifier(slug, modifiedToool.toolkit?.slug || 'unkown', modifiedToool);
} else {
throw new Error('Invalid schema modifier. Not a function.');
}
@@ -329,6 +329,75 @@ export class Tools<
return executeToolFn;
}
+ private async handleCustomToolExecution(
+ tool: Tool,
+ body: ToolExecuteParams,
+ modifiers?: ExecuteToolModifiers
+ ): Promise<ToolExecuteResponse> {
+ if (modifiers?.beforeToolExecute) {
+ if (typeof modifiers.beforeToolExecute === 'function') {
+ body = modifiers.beforeToolExecute(tool.slug, 'unkown', body);
+ } else {
+ throw new Error('Invalid beforeToolExecute modifier. Not a function.');
+ }
+ }
+
+ let result = await this.customTools.executeCustomTool(tool.slug, body, {
+ userId: body.userId || 'default',
+ connectedAccountId: body.connectedAccountId,
+ });
+
+ if (modifiers?.afterToolExecute) {
+ if (typeof modifiers.afterToolExecute === 'function') {
+ result = modifiers.afterToolExecute(tool.slug, 'unkown', result);
+ }
+ }
+
+ return result;
+ }
+
+ private async handleComposioToolExecution(
+ tool: Tool,
+ body: ToolExecuteParams,
+ modifiers?: ExecuteToolModifiers
+ ): Promise<ToolExecuteResponse> {
+ if (modifiers?.beforeToolExecute) {
+ if (typeof modifiers.beforeToolExecute === 'function') {
+ body = modifiers.beforeToolExecute(tool.slug, tool.toolkit?.slug || 'unkown', body);
+ } else {
+ throw new Error('Invalid beforeToolExecute modifier. Not a function.');
+ }
+ }
+ // fetch connected accounts if doesn't exist
+ let connectedAccountId = body.connectedAccountId;
+ if (!connectedAccountId) {
+ connectedAccountId =
+ (await this.getConnectedAccountIdForTool(body.userId, tool.slug)) || undefined;
+ }
+
+ let result = await this.client.tools.execute(tool.slug, {
+ allow_tracing: body.allowTracing,
+ connected_account_id: body.connectedAccountId,
+ custom_auth_params: body.customAuthParams,
+ arguments: body.arguments,
+ entity_id: body.userId,
+ version: body.version,
+ text: body.text,
+ });
+ // apply transformations to the response
+ result = this.transformToolExecuteResponse(result);
+
+ // apply local modifiers if they are provided
+ if (modifiers?.afterToolExecute) {
+ if (typeof modifiers.afterToolExecute === 'function') {
+ result = modifiers.afterToolExecute(tool.slug, tool.toolkit?.slug || 'unkown', result);
+ } else {
+ throw new Error('Invalid afterToolExecute modifier. Not a function.');
+ }
+ }
+ return result;
+ }
+
/**
* Exectes a given tool with the provided parameters.
*
@@ -350,54 +419,18 @@ export class Tools<
}
try {
- // apply local modifiers if they are provided
- if (modifiers?.beforeToolExecute) {
- if (typeof modifiers.beforeToolExecute === 'function') {
- body = modifiers.beforeToolExecute(slug, body);
- } else {
- throw new Error('Invalid beforeToolExecute modifier. Not a function.');
- }
- }
-
- let result: ToolExecuteResponse;
- // check if the tool is a custom tool
const customTool = await this.customTools.getCustomToolBySlug(slug);
if (customTool) {
- result = await this.customTools.executeCustomTool(slug, body, {
- userId: body.userId || 'default',
- connectedAccountId: body.connectedAccountId,
- });
+ // handle custom tool execution
+ return this.handleCustomToolExecution(customTool, body, modifiers);
} else {
- // fetch connected accounts if doesn't exist
- let connectedAccountId = body.connectedAccountId;
- if (!connectedAccountId) {
- connectedAccountId =
- (await this.getConnectedAccountIdForTool(body.userId, slug)) || undefined;
- }
-
- result = await this.client.tools.execute(slug, {
- allow_tracing: body.allowTracing,
- connected_account_id: body.connectedAccountId,
- custom_auth_params: body.customAuthParams,
- arguments: body.arguments,
- entity_id: body.userId,
- version: body.version,
- text: body.text,
- });
- // apply transformations to the response
- result = this.transformToolExecuteResponse(result);
- }
-
- // apply local modifiers if they are provided
- if (modifiers?.afterToolExecute) {
- if (typeof modifiers.afterToolExecute === 'function') {
- result = modifiers.afterToolExecute(slug, result);
- } else {
- throw new Error('Invalid afterToolExecute modifier. Not a function.');
+ // handle composio tool execution
+ const composioTool = await this.getComposioToolBySlug(body.userId, slug);
+ if (!composioTool) {
+ throw new Error(`Tool with slug ${slug} not found`);
}
+ return this.handleComposioToolExecution(composioTool, body, modifiers);
}
-
- return result;
} catch (error) {
logger.error(`Error executing tool ${slug}: ${error}`);
throw error;
diff --git a/packages/core/src/types/modifiers.types.ts b/packages/core/src/types/modifiers.types.ts
index b42c6ce4d..32549abb9 100644
--- a/packages/core/src/types/modifiers.types.ts
+++ b/packages/core/src/types/modifiers.types.ts
@@ -7,6 +7,7 @@ import { ToolExecuteParams, ToolExecuteResponse, Tool } from './tool.types';
*/
export type BeforeToolExecuteModifier = (
toolSlug: string,
+ toolkitSlug: string,
toolExecuteParams: ToolExecuteParams
) => ToolExecuteParams;
@@ -16,6 +17,7 @@ export type BeforeToolExecuteModifier = (
*/
export type AfterToolExecuteModifier = (
toolSlug: string,
+ toolkitSlug: string,
toolExecuteResponse: ToolExecuteResponse
) => ToolExecuteResponse;
@@ -23,7 +25,11 @@ export type AfterToolExecuteModifier = (
* Modifier for altering the tool schema
* Called after the tool schema is retrieved
*/
-export type TransformToolSchemaModifier = (toolSlug: string, toolSchema: Tool) => Tool;
+export type TransformToolSchemaModifier = (
+ toolSlug: string,
+ toolkitSlug: string,
+ toolSchema: Tool
+) => Tool;
/**
* Non Agentic tool options. These are used by Non-Agentic toolsets