@devries-cameron20 / composio Mirror · read-only / commits / 582b6f07f
Fork 0

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 global modifiers

haxzie committed May 5, 2025 at 20:31 UTC 582b6f07f544d2fe7f28c902aee3b49394b54dc1
4 files changed +197 -108
examples/openai/src/index.ts
-1
@@ -11,7 +11,6 @@ const composio = new Composio({
11 apiKey: process.env.COMPOSIO_API_KEY,
12 });
13
14 -
14 // Fetch all the tools from the Composio API
15 // these tools are automatically typed and wrapped with the OpenAI Toolset
16 const tool = await composio.getToolBySlug('HACKERNEWS_GET_USER');
packages/core/src/index.ts
+32 -21
@@ -1,30 +1,41 @@
1 // Core exports
2 -import { Composio } from "./composio";
3 -import { ComposioToolset } from "./toolset/ComposioToolset";
4 -import { BaseComposioToolset } from "./toolset/BaseToolset";
5 -import type { Tool, ToolListParamsSchema, ToolListParams } from "./types/tool.types";
6 -import type { Toolset } from "./types/toolset.types";
2 +import { Composio } from './composio';
3 +import { ComposioToolset } from './toolset/ComposioToolset';
4 +import { BaseComposioToolset } from './toolset/BaseToolset';
5 +import type { Tool, ToolListParamsSchema, ToolListParams } from './types/tool.types';
6 +import type { Toolset } from './types/toolset.types';
7
8 // Utils exports
9 -import { jsonSchemaToModel } from "./utils/jsonSchema";
9 +import { jsonSchemaToModel } from './utils/jsonSchema';
10
11 // telemetry exports
12 -import type { InstrumentedInstance } from "./types/telemetry.types";
13 -import { BaseTelemetryTransport } from "./telemetry/TelemetryTransport";
12 +import type { InstrumentedInstance } from './types/telemetry.types';
13 +import { BaseTelemetryTransport } from './telemetry/TelemetryTransport';
14
15 // toolsets exports
16 -import { OpenAIToolset } from "./toolset/OpenAIToolset";
16 +import { OpenAIToolset } from './toolset/OpenAIToolset';
17
18 -export {
19 - Composio,
20 - ComposioToolset,
21 - BaseComposioToolset,
22 - Tool,
23 - ToolListParamsSchema,
24 - ToolListParams,
25 - Toolset,
26 - jsonSchemaToModel,
27 - InstrumentedInstance,
28 - BaseTelemetryTransport ,
29 - OpenAIToolset
18 +// modifiers exports
19 +import type {
20 + BeforeToolExecuteModifer,
21 + AfterToolExecuteModifier,
22 + TransformToolSchemaModifier,
23 +} from './types/modifiers.types';
24 +
25 +export {
26 + Composio,
27 + ComposioToolset,
28 + BaseComposioToolset,
29 + Tool,
30 + ToolListParamsSchema,
31 + ToolListParams,
32 + Toolset,
33 + jsonSchemaToModel,
34 + InstrumentedInstance,
35 + BaseTelemetryTransport,
36 + OpenAIToolset,
37 + // types
38 + BeforeToolExecuteModifer,
39 + AfterToolExecuteModifier,
40 + TransformToolSchemaModifier,
41 };
packages/core/src/models/Modifiers.ts
+159 -83
@@ -1,110 +1,186 @@
1 /**
2 * @fileoverview Modifiers class for Composio SDK, used to manage tool execution modifiers.
3 - *
3 + *
4 * @author Musthaq Ahamad <musthaq@composio.dev>
5 * @date 2025-05-05
6 * @module Modifiers
7 */
8 -import { AfterToolExecuteModifier, BeforeToolExecuteModifer, TransformToolSchemaModifier } from "../types/modifiers.types";
9 -import { InstrumentedInstance } from "../types/telemetry.types";
10 -import { Tool, ToolExecuteParams, ToolExecuteResponse } from "../types/tool.types";
8 +import {
9 + AfterToolExecuteModifier,
10 + BeforeToolExecuteModifer,
11 + GlobalAfterToolExecuteModifier,
12 + GlobalBeforeToolExecuteModifier,
13 + GlobalTransformToolSchemaModifier,
14 + TransformToolSchemaModifier,
15 +} from '../types/modifiers.types';
16 +import { InstrumentedInstance } from '../types/telemetry.types';
17 +import { Tool, ToolExecuteParams, ToolExecuteResponse } from '../types/tool.types';
18
19 /**
20 * Modifiers class
14 - *
21 + *
22 * This class is used to manage the modifiers for the tools in the Composio SDK.
23 * It provides methods to add before and after execution modifiers, as well as schema transformation modifiers.
24 */
25 export class Modifiers implements InstrumentedInstance {
19 - readonly FILE_NAME: string = 'core/models/Modifiers.ts';
20 - private beforeToolExecuteModifiers: Map<string, Set<BeforeToolExecuteModifer>> = new Map();
21 - private afterToolExecuteModifiers: Map<string, Set<AfterToolExecuteModifier>> = new Map();
22 - private transformToolSchemaModifiers: Map<string, Set<TransformToolSchemaModifier>> = new Map();
26 + readonly FILE_NAME: string = 'core/models/Modifiers.ts';
27 + private beforeToolExecuteModifiers: Map<string, Set<BeforeToolExecuteModifer>> = new Map();
28 + private globalBeforeToolExecuteModifiers: Set<GlobalBeforeToolExecuteModifier> = new Set();
29 + private afterToolExecuteModifiers: Map<string, Set<AfterToolExecuteModifier>> = new Map();
30 + private globalAfterToolExecuteModifiers: Set<GlobalAfterToolExecuteModifier> = new Set();
31 + private transformToolSchemaModifiers: Map<string, Set<TransformToolSchemaModifier>> = new Map();
32 + private globalTransformToolSchemaModifiers: Set<GlobalTransformToolSchemaModifier> = new Set();
33
24 - constructor() {}
34 + constructor() {}
35
26 - /**
27 - * Method to modify the tool schema before it is executed.
28 - * @param {string} toolSlug slug of the tool to be modified
29 - * @param modifier function to modify the tool execution parameters
30 - */
31 - useBeforeToolExecute(toolSlug: string, modifier: BeforeToolExecuteModifer) {
32 - if (!this.beforeToolExecuteModifiers.has(toolSlug)) {
33 - this.beforeToolExecuteModifiers.set(toolSlug, new Set());
34 - }
35 - this.beforeToolExecuteModifiers.get(toolSlug)?.add(modifier);
36 + /**
37 + * Function signature for modifying the tool execution parameters.
38 + */
39 + useBeforeToolExecute(globalModifier: GlobalBeforeToolExecuteModifier): void;
40 + useBeforeToolExecute(toolSlug: string, modifier: BeforeToolExecuteModifer): void;
41 + /**
42 + * Method to modify the tool schema before it is executed.
43 + * We provide two methods to register modifiers:
44 + * 1. useBeforeToolExecute(modifier: GlobalBeforeToolExecuteModifier): void
45 + * 2. useBeforeToolExecute(toolSlug: string, modifier: BeforeToolExecuteModifer): void
46 + * @param {string} toolSlug slug of the tool to be modified
47 + * @param modifier function to modify the tool execution parameters
48 + */
49 + useBeforeToolExecute(
50 + arg1: string | GlobalBeforeToolExecuteModifier,
51 + modifier?: BeforeToolExecuteModifer
52 + ): void {
53 + if (typeof arg1 === 'string' && modifier) {
54 + const toolSlug = arg1;
55 + if (!this.beforeToolExecuteModifiers.has(toolSlug)) {
56 + this.beforeToolExecuteModifiers.set(toolSlug, new Set());
57 + }
58 + this.beforeToolExecuteModifiers.get(toolSlug)?.add(modifier);
59 + } else if (typeof arg1 === 'function') {
60 + const globalModifier = arg1;
61 + this.globalBeforeToolExecuteModifiers.add(globalModifier);
62 }
63 + }
64
38 - /**
39 - * Method to apply the before tool execute modifiers.
40 - * @param {string} toolSlug slug of the tool to be modified
41 - * @param {ToolExecuteParams} toolExecuteParams parameters to be passed to the tool
42 - * @returns {ToolExecuteParams} modified parameters
43 - */
44 - applyBeforeToolExecute(toolSlug: string, toolExecuteParams: ToolExecuteParams): ToolExecuteParams {
45 - const modifiers = this.beforeToolExecuteModifiers.get(toolSlug);
46 - if (modifiers) {
47 - for (const modifier of modifiers) {
48 - toolExecuteParams = modifier(toolSlug, toolExecuteParams);
49 - }
50 - }
51 - return toolExecuteParams;
65 + /**
66 + * Method to apply the before tool execute modifiers.
67 + * @param {string} toolSlug slug of the tool to be modified
68 + * @param {ToolExecuteParams} toolExecuteParams parameters to be passed to the tool
69 + * @returns {ToolExecuteParams} modified parameters
70 + */
71 + applyBeforeToolExecute(
72 + toolSlug: string,
73 + toolExecuteParams: ToolExecuteParams
74 + ): ToolExecuteParams {
75 + // Apply global modifiers
76 + let modifiedToolExecuteParams = { ...toolExecuteParams };
77 + for (const globalModifier of this.globalBeforeToolExecuteModifiers) {
78 + modifiedToolExecuteParams = globalModifier(toolSlug, modifiedToolExecuteParams);
79 }
80 + // Apply tool-specific modifiers
81 + const modifiers = this.beforeToolExecuteModifiers.get(toolSlug);
82 + if (modifiers) {
83 + for (const modifier of modifiers) {
84 + modifiedToolExecuteParams = modifier(modifiedToolExecuteParams);
85 + }
86 + }
87 + return modifiedToolExecuteParams;
88 + }
89
90 + /**
91 + * Function signature for modifying the tool execution response.
92 + */
93 + useAfterToolExecute(globalModifier: GlobalAfterToolExecuteModifier): void;
94 + useAfterToolExecute(toolSlug: string, modifier: AfterToolExecuteModifier): void;
95
55 - /**
56 - * Method to modify the tool schema after it is executed.
57 - * @param {string} toolSlug slug of the tool to be modified
58 - * @param modifier function to modify the tool execution response
59 - */
60 - useAfterToolExecute(toolSlug: string, modifier: AfterToolExecuteModifier) {
61 - if (!this.afterToolExecuteModifiers.has(toolSlug)) {
62 - this.afterToolExecuteModifiers.set(toolSlug, new Set());
63 - }
64 - this.afterToolExecuteModifiers.get(toolSlug)?.add(modifier);
96 + /**
97 + * Method to modify the tool schema after it is executed.
98 + * @param {string} toolSlug slug of the tool to be modified
99 + * @param modifier function to modify the tool execution response
100 + */
101 + useAfterToolExecute(
102 + arg1: string | GlobalAfterToolExecuteModifier,
103 + modifier?: AfterToolExecuteModifier
104 + ) {
105 + if (typeof arg1 === 'string' && modifier) {
106 + const toolSlug = arg1;
107 + if (!this.afterToolExecuteModifiers.has(toolSlug)) {
108 + this.afterToolExecuteModifiers.set(toolSlug, new Set());
109 + }
110 + this.afterToolExecuteModifiers.get(toolSlug)?.add(modifier);
111 + } else if (typeof arg1 === 'function') {
112 + const globalModifier = arg1;
113 + this.globalAfterToolExecuteModifiers.add(globalModifier);
114 }
115 + }
116
67 - /**
68 - * Method to apply the after tool execute modifiers.
69 - * @param {string} toolSlug slug of the tool to be modified
70 - * @param {ToolExecuteResponse} toolExecuteResponse response from the tool execution
71 - * @returns {ToolExecuteResponse} modified response
72 - */
73 - applyAfterToolExecute(toolSlug: string, toolExecuteResponse: ToolExecuteResponse): ToolExecuteResponse {
74 - const modifiers = this.afterToolExecuteModifiers.get(toolSlug);
75 - if (modifiers) {
76 - for (const modifier of modifiers) {
77 - toolExecuteResponse = modifier(toolSlug, toolExecuteResponse);
78 - }
79 - }
80 - return toolExecuteResponse;
117 + /**
118 + * Method to apply the after tool execute modifiers.
119 + * @param {string} toolSlug slug of the tool to be modified
120 + * @param {ToolExecuteResponse} toolExecuteResponse response from the tool execution
121 + * @returns {ToolExecuteResponse} modified response
122 + */
123 + applyAfterToolExecute(
124 + toolSlug: string,
125 + toolExecuteResponse: ToolExecuteResponse
126 + ): ToolExecuteResponse {
127 + // Apply global modifiers
128 + let modifiedToolExecuteResponse = { ...toolExecuteResponse };
129 + for (const globalModifier of this.globalAfterToolExecuteModifiers) {
130 + modifiedToolExecuteResponse = globalModifier(toolSlug, modifiedToolExecuteResponse);
131 + }
132 + // Apply tool-specific modifiers
133 + const modifiers = this.afterToolExecuteModifiers.get(toolSlug);
134 + if (modifiers) {
135 + for (const modifier of modifiers) {
136 + modifiedToolExecuteResponse = modifier(modifiedToolExecuteResponse);
137 + }
138 }
139 + return modifiedToolExecuteResponse;
140 + }
141
83 - /**
84 - * Method to register modifiers for the tool schema.
85 - * @param {string} toolSlug slug of the tool to be modified
86 - * @param modifier function to modify the tool schema
87 - */
88 - useTransformToolSchema(toolSlug: string, modifier:TransformToolSchemaModifier) {
89 - if (!this.transformToolSchemaModifiers.has(toolSlug)) {
90 - this.transformToolSchemaModifiers.set(toolSlug, new Set());
91 - }
92 - this.transformToolSchemaModifiers.get(toolSlug)?.add(modifier);
142 + /**
143 + * Function signature for modifying the tool schema.
144 + */
145 + useTransformToolSchema(globalModifier: GlobalTransformToolSchemaModifier): void;
146 + useTransformToolSchema(toolSlug: string, modifier: TransformToolSchemaModifier): void;
147 + /**
148 + * Method to register modifiers for the tool schema.
149 + * @param {string} toolSlug slug of the tool to be modified
150 + * @param modifier function to modify the tool schema
151 + */
152 + useTransformToolSchema(arg1: string | GlobalTransformToolSchemaModifier, modifier?: TransformToolSchemaModifier) {
153 + if (typeof arg1 === 'string' && modifier) {
154 + const toolSlug = arg1;
155 + if (!this.transformToolSchemaModifiers.has(toolSlug)) {
156 + this.transformToolSchemaModifiers.set(toolSlug, new Set());
157 + }
158 + this.transformToolSchemaModifiers.get(toolSlug)?.add(modifier);
159 + } else if (typeof arg1 === 'function') {
160 + const globalModifier = arg1;
161 + this.globalTransformToolSchemaModifiers.add(globalModifier);
162 }
163 + }
164
95 - /**
96 - * Method to apply the transform tool schema modifiers.
97 - * @param toolSlug slug of the tool to be modified
98 - * @param toolSchema schema of the tool to be modified
99 - * @returns {Tool}
100 - */
101 - applyTransformToolSchema(toolSlug: string, toolSchema: Tool): Tool {
102 - const modifiers = this.transformToolSchemaModifiers.get(toolSlug);
103 - if (modifiers) {
104 - for (const modifier of modifiers) {
105 - toolSchema = modifier(toolSlug, toolSchema);
106 - }
107 - }
108 - return toolSchema;
165 + /**
166 + * Method to apply the transform tool schema modifiers.
167 + * @param toolSlug slug of the tool to be modified
168 + * @param toolSchema schema of the tool to be modified
169 + * @returns {Tool}
170 + */
171 + applyTransformToolSchema(toolSlug: string, toolSchema: Tool): Tool {
172 + // Apply global modifiers
173 + let modifiedToolSchema = { ...toolSchema };
174 + for (const globalModifier of this.globalTransformToolSchemaModifiers) {
175 + modifiedToolSchema = globalModifier(toolSlug, modifiedToolSchema);
176 + }
177 + // Apply tool-specific modifiers
178 + const modifiers = this.transformToolSchemaModifiers.get(toolSlug);
179 + if (modifiers) {
180 + for (const modifier of modifiers) {
181 + modifiedToolSchema = modifier(modifiedToolSchema);
182 + }
183 }
110 -}
\ No newline at end of file
184 + return modifiedToolSchema;
185 + }
186 +}
packages/core/src/types/modifiers.types.ts
+6 -3
@@ -1,5 +1,8 @@
1 import { ToolExecuteParams, ToolExecuteResponse, Tool } from "./tool.types";
2
3 -export type BeforeToolExecuteModifer = (toolSlug: string, toolExecuteParams: ToolExecuteParams) => ToolExecuteParams;
4 -export type AfterToolExecuteModifier = (toolSlug: string, toolExecuteResponse: ToolExecuteResponse) => ToolExecuteResponse;
5 -export type TransformToolSchemaModifier = (toolSlug: string, toolSchemModifier: Tool) => Tool;
\ No newline at end of file
3 +export type BeforeToolExecuteModifer = (toolExecuteParams: ToolExecuteParams) => ToolExecuteParams;
4 +export type GlobalBeforeToolExecuteModifier = (toolSlug: string, toolExecuteParams: ToolExecuteParams) => ToolExecuteParams;
5 +export type AfterToolExecuteModifier = (toolExecuteResponse: ToolExecuteResponse) => ToolExecuteResponse;
6 +export type GlobalAfterToolExecuteModifier = (toolSlug: string, toolExecuteResponse: ToolExecuteResponse) => ToolExecuteResponse;
7 +export type TransformToolSchemaModifier = (toolSchemModifier: Tool) => Tool;
8 +export type GlobalTransformToolSchemaModifier = (toolSlug: string, toolSchema: Tool) => Tool;
\ No newline at end of file