@devries-cameron20 / composio Mirror · read-only / commits / 50e23a1c6
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 preprocessor examples

haxzie committed May 12, 2025 at 18:02 UTC 50e23a1c6767bb6f83332d97a436d769d808286e
3 files changed +53 -15
examples/pre-processors/src/index.ts
+16 -2
@@ -1,7 +1,9 @@
1 import { Composio } from '@composio/core';
2 import { VercelToolset } from '@composio/vercel';
3 -import { zodSchema } from 'ai';
3
4 +/**
5 + * Non agentic toolset
6 + */
7 const composio = new Composio({
8 apiKey: process.env.COMPOSIO_API_KEY,
9 });
@@ -14,6 +16,16 @@ const tool = await composio.getToolBySlug('HACKERNEWS_GET_USER', {
16 },
17 });
18
19 +// local modifiers, with tool as helper
20 +// @ts-ignore
21 +composio.toolset.handleToolCall({}, '', {
22 + beforeToolExecute: (toolSlug, toolExecuteParams) => {
23 + if (toolSlug === 'HACKERNEWS_GET_USER') {
24 + toolExecuteParams.arguments = {};
25 + }
26 + return toolExecuteParams;
27 + },
28 +});
29 // global modifiers, with toolslug as helper
30 composio.useTransformToolSchema('HACKERNEWS_GET_USER', toolSchema => {
31 toolSchema.inputParameters = {};
@@ -28,7 +40,9 @@ composio.useTransformToolSchema((toolSlug, toolSchema) => {
40 return toolSchema;
41 });
42
31 -// agentic toolset
43 +/**
44 + * Agentic toolset
45 + */
46 const vercelComposio = new Composio({
47 apiKey: process.env.COMPOSIO_API_KEY,
48 toolset: new VercelToolset(),
packages/core/src/toolset/BaseToolset.ts
+1 -1
@@ -40,7 +40,7 @@ export abstract class BaseNonAgenticToolset<TToolCollection, TTool>
40 /**
41 * Base class for agentic toolsets that support full modifier capabilities
42 * This is used for toolsets that need to handle tool execution modifiers
43 - * eg: Vercel, Cloudflare, etc.
43 + * eg: Vercel, Langchain, etc.
44 */
45 export abstract class BaseAgenticToolset<TToolCollection, TTool>
46 implements Toolset<TTool, TToolCollection>
packages/core/src/toolset/OpenAIToolset.ts
+36 -12
@@ -12,7 +12,7 @@ import { Stream } from 'openai/streaming';
12 import { BaseNonAgenticToolset } from './BaseToolset';
13 import { Tool, ToolListParams } from '../types/tool.types';
14 import logger from '../utils/logger';
15 -import { ModifiersParams } from '../types/modifiers.types';
15 +import { ExecuteToolModifiersParams, ModifiersParams } from '../types/modifiers.types';
16
17 export type OpenAiTool = OpenAI.ChatCompletionTool;
18 export type OpenAiToolCollection = Array<OpenAiTool>;
@@ -65,7 +65,8 @@ export class OpenAIToolset extends BaseNonAgenticToolset<OpenAiToolCollection, O
65 */
66 async executeToolCall(
67 tool: OpenAI.ChatCompletionMessageToolCall,
68 - userId?: string
68 + userId?: string,
69 + modifiers?: ExecuteToolModifiersParams
70 ): Promise<string> {
71 const toolSchema = await this.getComposio().tools.getToolBySlug(tool.function.name);
72 const appSlug = toolSchema?.toolkit?.slug.toLowerCase();
@@ -78,7 +79,7 @@ export class OpenAIToolset extends BaseNonAgenticToolset<OpenAiToolCollection, O
79 connected_account_id: connectedAccountId,
80 arguments: JSON.parse(tool.function.arguments),
81 };
81 - const results = await this.getComposio().tools.execute(toolSchema.slug, payload);
82 + const results = await this.getComposio().tools.execute(toolSchema.slug, payload, modifiers);
83 return JSON.stringify(results);
84 }
85
@@ -89,11 +90,15 @@ export class OpenAIToolset extends BaseNonAgenticToolset<OpenAiToolCollection, O
90 * @param {string} userId - The user id.
91 * @returns {Promise<string[]>} The results of the tool call.
92 */
92 - async handleToolCall(chatCompletion: OpenAI.ChatCompletion, userId?: string) {
93 + async handleToolCall(
94 + chatCompletion: OpenAI.ChatCompletion,
95 + userId?: string,
96 + modifiers?: ExecuteToolModifiersParams
97 + ) {
98 const outputs: string[] = [];
99 for (const message of chatCompletion.choices) {
100 if (message.message.tool_calls) {
96 - outputs.push(await this.executeToolCall(message.message.tool_calls[0], userId));
101 + outputs.push(await this.executeToolCall(message.message.tool_calls[0], userId, modifiers));
102 }
103 }
104 return outputs;
@@ -106,7 +111,11 @@ export class OpenAIToolset extends BaseNonAgenticToolset<OpenAiToolCollection, O
111 * @param {string} userId - The user id.
112 * @returns {Promise<OpenAI.Beta.Threads.Runs.RunSubmitToolOutputsParams.ToolOutput[]>} The tool outputs.
113 */
109 - async handleAssistantMessage(run: OpenAI.Beta.Threads.Run, userId?: string) {
114 + async handleAssistantMessage(
115 + run: OpenAI.Beta.Threads.Run,
116 + userId?: string,
117 + modifiers?: ExecuteToolModifiersParams
118 + ) {
119 const tool_calls = run.required_action?.submit_tool_outputs?.tool_calls || [];
120 const tool_outputs: Array<OpenAI.Beta.Threads.Runs.RunSubmitToolOutputsParams.ToolOutput> =
121 await Promise.all(
@@ -116,7 +125,8 @@ export class OpenAIToolset extends BaseNonAgenticToolset<OpenAiToolCollection, O
125 // Execute each tool call and get the response
126 const tool_response = await this.executeToolCall(
127 tool_call as OpenAI.ChatCompletionMessageToolCall,
119 - userId
128 + userId,
129 + modifiers
130 );
131
132 logger.debug(`Tool call ${tool_call.id} executed with response: ${tool_response}`);
@@ -143,7 +153,8 @@ export class OpenAIToolset extends BaseNonAgenticToolset<OpenAiToolCollection, O
153 client: OpenAI,
154 runStream: Stream<OpenAI.Beta.Assistants.AssistantStreamEvent>,
155 thread: OpenAI.Beta.Threads.Thread,
146 - userId?: string
156 + userId?: string,
157 + modifiers?: ExecuteToolModifiersParams
158 ) {
159 // @TODO: Log the run stream
160 const defaultUserId = this.getComposio()?.userId;
@@ -164,7 +175,11 @@ export class OpenAIToolset extends BaseNonAgenticToolset<OpenAiToolCollection, O
175
176 // Handle the 'requires_action' event
177 if (event.event === 'thread.run.requires_action') {
167 - const toolOutputs = await this.handleAssistantMessage(event.data, userId ?? defaultUserId);
178 + const toolOutputs = await this.handleAssistantMessage(
179 + event.data,
180 + userId ?? defaultUserId,
181 + modifiers
182 + );
183
184 // Submit the tool outputs
185 await client.beta.threads.runs.submitToolOutputs(thread.id, runId, {
@@ -194,7 +209,11 @@ export class OpenAIToolset extends BaseNonAgenticToolset<OpenAiToolCollection, O
209
210 while (['queued', 'in_progress', 'requires_action'].includes(finalRun.status)) {
211 if (finalRun.status === 'requires_action') {
197 - const toolOutputs = await this.handleAssistantMessage(finalRun, userId ?? defaultUserId);
212 + const toolOutputs = await this.handleAssistantMessage(
213 + finalRun,
214 + userId ?? defaultUserId,
215 + modifiers
216 + );
217
218 // Submit tool outputs
219 finalRun = await client.beta.threads.runs.submitToolOutputs(thread.id, runId, {
@@ -221,12 +240,17 @@ export class OpenAIToolset extends BaseNonAgenticToolset<OpenAiToolCollection, O
240 client: OpenAI,
241 run: OpenAI.Beta.Threads.Run,
242 thread: OpenAI.Beta.Threads.Thread,
224 - userId?: string
243 + userId?: string,
244 + modifiers?: ExecuteToolModifiersParams
245 ) {
246 const defaultUserId = this.getComposio()?.userId;
247 while (['queued', 'in_progress', 'requires_action'].includes(run.status)) {
248 // logger.debug(`Current run status: ${run.status}`);
229 - const tool_outputs = await this.handleAssistantMessage(run, userId ?? defaultUserId);
249 + const tool_outputs = await this.handleAssistantMessage(
250 + run,
251 + userId ?? defaultUserId,
252 + modifiers
253 + );
254 if (run.status === 'requires_action') {
255 // logger.debug(
256 // `Submitting tool outputs for run ID: ${run.id} in thread ID: ${thread.id}`