Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
TypeScript agentic-aiagentsaiai-agentsaiagentsdeveloper-toolsfunction-callinggpt-4javascriptjsllmllmopsmcppythonremote-mcp-serverssetypescript
Add global modifiers
haxzie582b6f07f544
diff --git a/examples/openai/src/index.ts b/examples/openai/src/index.tsindex 0ae9d454e..9be4bd58d 100644--- a/examples/openai/src/index.ts+++ b/examples/openai/src/index.ts@@ -11,7 +11,6 @@ const composio = new Composio({apiKey: process.env.COMPOSIO_API_KEY,});-// Fetch all the tools from the Composio API// these tools are automatically typed and wrapped with the OpenAI Toolsetconst tool = await composio.getToolBySlug('HACKERNEWS_GET_USER');diff --git a/packages/core/src/index.ts b/packages/core/src/index.tsindex bd6c52632..6c68bcf97 100644--- a/packages/core/src/index.ts+++ b/packages/core/src/index.ts@@ -1,30 +1,41 @@// Core exports-import { Composio } from "./composio";-import { ComposioToolset } from "./toolset/ComposioToolset";-import { BaseComposioToolset } from "./toolset/BaseToolset";-import type { Tool, ToolListParamsSchema, ToolListParams } from "./types/tool.types";-import type { Toolset } from "./types/toolset.types";+import { Composio } from './composio';+import { ComposioToolset } from './toolset/ComposioToolset';+import { BaseComposioToolset } from './toolset/BaseToolset';+import type { Tool, ToolListParamsSchema, ToolListParams } from './types/tool.types';+import type { Toolset } from './types/toolset.types';// Utils exports-import { jsonSchemaToModel } from "./utils/jsonSchema";+import { jsonSchemaToModel } from './utils/jsonSchema';// telemetry exports-import type { InstrumentedInstance } from "./types/telemetry.types";-import { BaseTelemetryTransport } from "./telemetry/TelemetryTransport";+import type { InstrumentedInstance } from './types/telemetry.types';+import { BaseTelemetryTransport } from './telemetry/TelemetryTransport';// toolsets exports-import { OpenAIToolset } from "./toolset/OpenAIToolset";+import { OpenAIToolset } from './toolset/OpenAIToolset';-export {- Composio,- ComposioToolset,- BaseComposioToolset,- Tool,- ToolListParamsSchema,- ToolListParams,- Toolset,- jsonSchemaToModel,- InstrumentedInstance,- BaseTelemetryTransport ,- OpenAIToolset+// modifiers exports+import type {+ BeforeToolExecuteModifer,+ AfterToolExecuteModifier,+ TransformToolSchemaModifier,+} from './types/modifiers.types';++export {+ Composio,+ ComposioToolset,+ BaseComposioToolset,+ Tool,+ ToolListParamsSchema,+ ToolListParams,+ Toolset,+ jsonSchemaToModel,+ InstrumentedInstance,+ BaseTelemetryTransport,+ OpenAIToolset,+ // types+ BeforeToolExecuteModifer,+ AfterToolExecuteModifier,+ TransformToolSchemaModifier,};diff --git a/packages/core/src/models/Modifiers.ts b/packages/core/src/models/Modifiers.tsindex b8f7d43dc..a17a9d35f 100644--- a/packages/core/src/models/Modifiers.ts+++ b/packages/core/src/models/Modifiers.ts@@ -1,110 +1,186 @@/*** @fileoverview Modifiers class for Composio SDK, used to manage tool execution modifiers.- *+ ** @author Musthaq Ahamad <[email protected]>* @date 2025-05-05* @module Modifiers*/-import { AfterToolExecuteModifier, BeforeToolExecuteModifer, TransformToolSchemaModifier } from "../types/modifiers.types";-import { InstrumentedInstance } from "../types/telemetry.types";-import { Tool, ToolExecuteParams, ToolExecuteResponse } from "../types/tool.types";+import {+ AfterToolExecuteModifier,+ BeforeToolExecuteModifer,+ GlobalAfterToolExecuteModifier,+ GlobalBeforeToolExecuteModifier,+ GlobalTransformToolSchemaModifier,+ TransformToolSchemaModifier,+} from '../types/modifiers.types';+import { InstrumentedInstance } from '../types/telemetry.types';+import { Tool, ToolExecuteParams, ToolExecuteResponse } from '../types/tool.types';/*** Modifiers class- *+ ** This class is used to manage the modifiers for the tools in the Composio SDK.* It provides methods to add before and after execution modifiers, as well as schema transformation modifiers.*/export class Modifiers implements InstrumentedInstance {- readonly FILE_NAME: string = 'core/models/Modifiers.ts';- private beforeToolExecuteModifiers: Map<string, Set<BeforeToolExecuteModifer>> = new Map();- private afterToolExecuteModifiers: Map<string, Set<AfterToolExecuteModifier>> = new Map();- private transformToolSchemaModifiers: Map<string, Set<TransformToolSchemaModifier>> = new Map();+ readonly FILE_NAME: string = 'core/models/Modifiers.ts';+ private beforeToolExecuteModifiers: Map<string, Set<BeforeToolExecuteModifer>> = new Map();+ private globalBeforeToolExecuteModifiers: Set<GlobalBeforeToolExecuteModifier> = new Set();+ private afterToolExecuteModifiers: Map<string, Set<AfterToolExecuteModifier>> = new Map();+ private globalAfterToolExecuteModifiers: Set<GlobalAfterToolExecuteModifier> = new Set();+ private transformToolSchemaModifiers: Map<string, Set<TransformToolSchemaModifier>> = new Map();+ private globalTransformToolSchemaModifiers: Set<GlobalTransformToolSchemaModifier> = new Set();- constructor() {}+ constructor() {}- /**- * Method to modify the tool schema before it is executed.- * @param {string} toolSlug slug of the tool to be modified- * @param modifier function to modify the tool execution parameters- */- useBeforeToolExecute(toolSlug: string, modifier: BeforeToolExecuteModifer) {- if (!this.beforeToolExecuteModifiers.has(toolSlug)) {- this.beforeToolExecuteModifiers.set(toolSlug, new Set());- }- this.beforeToolExecuteModifiers.get(toolSlug)?.add(modifier);+ /**+ * Function signature for modifying the tool execution parameters.+ */+ useBeforeToolExecute(globalModifier: GlobalBeforeToolExecuteModifier): void;+ useBeforeToolExecute(toolSlug: string, modifier: BeforeToolExecuteModifer): void;+ /**+ * Method to modify the tool schema before it is executed.+ * We provide two methods to register modifiers:+ * 1. useBeforeToolExecute(modifier: GlobalBeforeToolExecuteModifier): void+ * 2. useBeforeToolExecute(toolSlug: string, modifier: BeforeToolExecuteModifer): void+ * @param {string} toolSlug slug of the tool to be modified+ * @param modifier function to modify the tool execution parameters+ */+ useBeforeToolExecute(+ arg1: string | GlobalBeforeToolExecuteModifier,+ modifier?: BeforeToolExecuteModifer+ ): void {+ if (typeof arg1 === 'string' && modifier) {+ const toolSlug = arg1;+ if (!this.beforeToolExecuteModifiers.has(toolSlug)) {+ this.beforeToolExecuteModifiers.set(toolSlug, new Set());+ }+ this.beforeToolExecuteModifiers.get(toolSlug)?.add(modifier);+ } else if (typeof arg1 === 'function') {+ const globalModifier = arg1;+ this.globalBeforeToolExecuteModifiers.add(globalModifier);}+ }- /**- * Method to apply the before tool execute modifiers.- * @param {string} toolSlug slug of the tool to be modified- * @param {ToolExecuteParams} toolExecuteParams parameters to be passed to the tool- * @returns {ToolExecuteParams} modified parameters- */- applyBeforeToolExecute(toolSlug: string, toolExecuteParams: ToolExecuteParams): ToolExecuteParams {- const modifiers = this.beforeToolExecuteModifiers.get(toolSlug);- if (modifiers) {- for (const modifier of modifiers) {- toolExecuteParams = modifier(toolSlug, toolExecuteParams);- }- }- return toolExecuteParams;+ /**+ * Method to apply the before tool execute modifiers.+ * @param {string} toolSlug slug of the tool to be modified+ * @param {ToolExecuteParams} toolExecuteParams parameters to be passed to the tool+ * @returns {ToolExecuteParams} modified parameters+ */+ applyBeforeToolExecute(+ toolSlug: string,+ toolExecuteParams: ToolExecuteParams+ ): ToolExecuteParams {+ // Apply global modifiers+ let modifiedToolExecuteParams = { ...toolExecuteParams };+ for (const globalModifier of this.globalBeforeToolExecuteModifiers) {+ modifiedToolExecuteParams = globalModifier(toolSlug, modifiedToolExecuteParams);}+ // Apply tool-specific modifiers+ const modifiers = this.beforeToolExecuteModifiers.get(toolSlug);+ if (modifiers) {+ for (const modifier of modifiers) {+ modifiedToolExecuteParams = modifier(modifiedToolExecuteParams);+ }+ }+ return modifiedToolExecuteParams;+ }+ /**+ * Function signature for modifying the tool execution response.+ */+ useAfterToolExecute(globalModifier: GlobalAfterToolExecuteModifier): void;+ useAfterToolExecute(toolSlug: string, modifier: AfterToolExecuteModifier): void;- /**- * Method to modify the tool schema after it is executed.- * @param {string} toolSlug slug of the tool to be modified- * @param modifier function to modify the tool execution response- */- useAfterToolExecute(toolSlug: string, modifier: AfterToolExecuteModifier) {- if (!this.afterToolExecuteModifiers.has(toolSlug)) {- this.afterToolExecuteModifiers.set(toolSlug, new Set());- }- this.afterToolExecuteModifiers.get(toolSlug)?.add(modifier);+ /**+ * Method to modify the tool schema after it is executed.+ * @param {string} toolSlug slug of the tool to be modified+ * @param modifier function to modify the tool execution response+ */+ useAfterToolExecute(+ arg1: string | GlobalAfterToolExecuteModifier,+ modifier?: AfterToolExecuteModifier+ ) {+ if (typeof arg1 === 'string' && modifier) {+ const toolSlug = arg1;+ if (!this.afterToolExecuteModifiers.has(toolSlug)) {+ this.afterToolExecuteModifiers.set(toolSlug, new Set());+ }+ this.afterToolExecuteModifiers.get(toolSlug)?.add(modifier);+ } else if (typeof arg1 === 'function') {+ const globalModifier = arg1;+ this.globalAfterToolExecuteModifiers.add(globalModifier);}+ }- /**- * Method to apply the after tool execute modifiers.- * @param {string} toolSlug slug of the tool to be modified- * @param {ToolExecuteResponse} toolExecuteResponse response from the tool execution- * @returns {ToolExecuteResponse} modified response- */- applyAfterToolExecute(toolSlug: string, toolExecuteResponse: ToolExecuteResponse): ToolExecuteResponse {- const modifiers = this.afterToolExecuteModifiers.get(toolSlug);- if (modifiers) {- for (const modifier of modifiers) {- toolExecuteResponse = modifier(toolSlug, toolExecuteResponse);- }- }- return toolExecuteResponse;+ /**+ * Method to apply the after tool execute modifiers.+ * @param {string} toolSlug slug of the tool to be modified+ * @param {ToolExecuteResponse} toolExecuteResponse response from the tool execution+ * @returns {ToolExecuteResponse} modified response+ */+ applyAfterToolExecute(+ toolSlug: string,+ toolExecuteResponse: ToolExecuteResponse+ ): ToolExecuteResponse {+ // Apply global modifiers+ let modifiedToolExecuteResponse = { ...toolExecuteResponse };+ for (const globalModifier of this.globalAfterToolExecuteModifiers) {+ modifiedToolExecuteResponse = globalModifier(toolSlug, modifiedToolExecuteResponse);+ }+ // Apply tool-specific modifiers+ const modifiers = this.afterToolExecuteModifiers.get(toolSlug);+ if (modifiers) {+ for (const modifier of modifiers) {+ modifiedToolExecuteResponse = modifier(modifiedToolExecuteResponse);+ }}+ return modifiedToolExecuteResponse;+ }- /**- * Method to register modifiers for the tool schema.- * @param {string} toolSlug slug of the tool to be modified- * @param modifier function to modify the tool schema- */- useTransformToolSchema(toolSlug: string, modifier:TransformToolSchemaModifier) {- if (!this.transformToolSchemaModifiers.has(toolSlug)) {- this.transformToolSchemaModifiers.set(toolSlug, new Set());- }- this.transformToolSchemaModifiers.get(toolSlug)?.add(modifier);+ /**+ * Function signature for modifying the tool schema.+ */+ useTransformToolSchema(globalModifier: GlobalTransformToolSchemaModifier): void;+ useTransformToolSchema(toolSlug: string, modifier: TransformToolSchemaModifier): void;+ /**+ * Method to register modifiers for the tool schema.+ * @param {string} toolSlug slug of the tool to be modified+ * @param modifier function to modify the tool schema+ */+ useTransformToolSchema(arg1: string | GlobalTransformToolSchemaModifier, modifier?: TransformToolSchemaModifier) {+ if (typeof arg1 === 'string' && modifier) {+ const toolSlug = arg1;+ if (!this.transformToolSchemaModifiers.has(toolSlug)) {+ this.transformToolSchemaModifiers.set(toolSlug, new Set());+ }+ this.transformToolSchemaModifiers.get(toolSlug)?.add(modifier);+ } else if (typeof arg1 === 'function') {+ const globalModifier = arg1;+ this.globalTransformToolSchemaModifiers.add(globalModifier);}+ }- /**- * Method to apply the transform tool schema modifiers.- * @param toolSlug slug of the tool to be modified- * @param toolSchema schema of the tool to be modified- * @returns {Tool}- */- applyTransformToolSchema(toolSlug: string, toolSchema: Tool): Tool {- const modifiers = this.transformToolSchemaModifiers.get(toolSlug);- if (modifiers) {- for (const modifier of modifiers) {- toolSchema = modifier(toolSlug, toolSchema);- }- }- return toolSchema;+ /**+ * Method to apply the transform tool schema modifiers.+ * @param toolSlug slug of the tool to be modified+ * @param toolSchema schema of the tool to be modified+ * @returns {Tool}+ */+ applyTransformToolSchema(toolSlug: string, toolSchema: Tool): Tool {+ // Apply global modifiers+ let modifiedToolSchema = { ...toolSchema };+ for (const globalModifier of this.globalTransformToolSchemaModifiers) {+ modifiedToolSchema = globalModifier(toolSlug, modifiedToolSchema);+ }+ // Apply tool-specific modifiers+ const modifiers = this.transformToolSchemaModifiers.get(toolSlug);+ if (modifiers) {+ for (const modifier of modifiers) {+ modifiedToolSchema = modifier(modifiedToolSchema);+ }}-}\ No newline at end of file+ return modifiedToolSchema;+ }+}diff --git a/packages/core/src/types/modifiers.types.ts b/packages/core/src/types/modifiers.types.tsindex cd8206c8f..5203ae959 100644--- a/packages/core/src/types/modifiers.types.ts+++ b/packages/core/src/types/modifiers.types.ts@@ -1,5 +1,8 @@import { ToolExecuteParams, ToolExecuteResponse, Tool } from "./tool.types";-export type BeforeToolExecuteModifer = (toolSlug: string, toolExecuteParams: ToolExecuteParams) => ToolExecuteParams;-export type AfterToolExecuteModifier = (toolSlug: string, toolExecuteResponse: ToolExecuteResponse) => ToolExecuteResponse;-export type TransformToolSchemaModifier = (toolSlug: string, toolSchemModifier: Tool) => Tool;\ No newline at end of file+export type BeforeToolExecuteModifer = (toolExecuteParams: ToolExecuteParams) => ToolExecuteParams;+export type GlobalBeforeToolExecuteModifier = (toolSlug: string, toolExecuteParams: ToolExecuteParams) => ToolExecuteParams;+export type AfterToolExecuteModifier = (toolExecuteResponse: ToolExecuteResponse) => ToolExecuteResponse;+export type GlobalAfterToolExecuteModifier = (toolSlug: string, toolExecuteResponse: ToolExecuteResponse) => ToolExecuteResponse;+export type TransformToolSchemaModifier = (toolSchemModifier: Tool) => Tool;+export type GlobalTransformToolSchemaModifier = (toolSlug: string, toolSchema: Tool) => Tool;\ No newline at end of file