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 preprocessor examples
haxzie50e23a1c6767
diff --git a/examples/pre-processors/src/index.ts b/examples/pre-processors/src/index.tsindex ea837c9a3..357c292eb 100644--- a/examples/pre-processors/src/index.ts+++ b/examples/pre-processors/src/index.ts@@ -1,7 +1,9 @@import { Composio } from '@composio/core';import { VercelToolset } from '@composio/vercel';-import { zodSchema } from 'ai';+/**+ * Non agentic toolset+ */const composio = new Composio({apiKey: process.env.COMPOSIO_API_KEY,});@@ -14,6 +16,16 @@ const tool = await composio.getToolBySlug('HACKERNEWS_GET_USER', {},});+// local modifiers, with tool as helper+// @ts-ignore+composio.toolset.handleToolCall({}, '', {+ beforeToolExecute: (toolSlug, toolExecuteParams) => {+ if (toolSlug === 'HACKERNEWS_GET_USER') {+ toolExecuteParams.arguments = {};+ }+ return toolExecuteParams;+ },+});// global modifiers, with toolslug as helpercomposio.useTransformToolSchema('HACKERNEWS_GET_USER', toolSchema => {toolSchema.inputParameters = {};@@ -28,7 +40,9 @@ composio.useTransformToolSchema((toolSlug, toolSchema) => {return toolSchema;});-// agentic toolset+/**+ * Agentic toolset+ */const vercelComposio = new Composio({apiKey: process.env.COMPOSIO_API_KEY,toolset: new VercelToolset(),diff --git a/packages/core/src/toolset/BaseToolset.ts b/packages/core/src/toolset/BaseToolset.tsindex 49e74cf4e..5e3314818 100644--- a/packages/core/src/toolset/BaseToolset.ts+++ b/packages/core/src/toolset/BaseToolset.ts@@ -40,7 +40,7 @@ export abstract class BaseNonAgenticToolset<TToolCollection, TTool>/*** Base class for agentic toolsets that support full modifier capabilities* This is used for toolsets that need to handle tool execution modifiers- * eg: Vercel, Cloudflare, etc.+ * eg: Vercel, Langchain, etc.*/export abstract class BaseAgenticToolset<TToolCollection, TTool>implements Toolset<TTool, TToolCollection>diff --git a/packages/core/src/toolset/OpenAIToolset.ts b/packages/core/src/toolset/OpenAIToolset.tsindex b0eea45ec..8c0e56877 100644--- a/packages/core/src/toolset/OpenAIToolset.ts+++ b/packages/core/src/toolset/OpenAIToolset.ts@@ -12,7 +12,7 @@ import { Stream } from 'openai/streaming';import { BaseNonAgenticToolset } from './BaseToolset';import { Tool, ToolListParams } from '../types/tool.types';import logger from '../utils/logger';-import { ModifiersParams } from '../types/modifiers.types';+import { ExecuteToolModifiersParams, ModifiersParams } from '../types/modifiers.types';export type OpenAiTool = OpenAI.ChatCompletionTool;export type OpenAiToolCollection = Array<OpenAiTool>;@@ -65,7 +65,8 @@ export class OpenAIToolset extends BaseNonAgenticToolset<OpenAiToolCollection, O*/async executeToolCall(tool: OpenAI.ChatCompletionMessageToolCall,- userId?: string+ userId?: string,+ modifiers?: ExecuteToolModifiersParams): Promise<string> {const toolSchema = await this.getComposio().tools.getToolBySlug(tool.function.name);const appSlug = toolSchema?.toolkit?.slug.toLowerCase();@@ -78,7 +79,7 @@ export class OpenAIToolset extends BaseNonAgenticToolset<OpenAiToolCollection, Oconnected_account_id: connectedAccountId,arguments: JSON.parse(tool.function.arguments),};- const results = await this.getComposio().tools.execute(toolSchema.slug, payload);+ const results = await this.getComposio().tools.execute(toolSchema.slug, payload, modifiers);return JSON.stringify(results);}@@ -89,11 +90,15 @@ export class OpenAIToolset extends BaseNonAgenticToolset<OpenAiToolCollection, O* @param {string} userId - The user id.* @returns {Promise<string[]>} The results of the tool call.*/- async handleToolCall(chatCompletion: OpenAI.ChatCompletion, userId?: string) {+ async handleToolCall(+ chatCompletion: OpenAI.ChatCompletion,+ userId?: string,+ modifiers?: ExecuteToolModifiersParams+ ) {const outputs: string[] = [];for (const message of chatCompletion.choices) {if (message.message.tool_calls) {- outputs.push(await this.executeToolCall(message.message.tool_calls[0], userId));+ outputs.push(await this.executeToolCall(message.message.tool_calls[0], userId, modifiers));}}return outputs;@@ -106,7 +111,11 @@ export class OpenAIToolset extends BaseNonAgenticToolset<OpenAiToolCollection, O* @param {string} userId - The user id.* @returns {Promise<OpenAI.Beta.Threads.Runs.RunSubmitToolOutputsParams.ToolOutput[]>} The tool outputs.*/- async handleAssistantMessage(run: OpenAI.Beta.Threads.Run, userId?: string) {+ async handleAssistantMessage(+ run: OpenAI.Beta.Threads.Run,+ userId?: string,+ modifiers?: ExecuteToolModifiersParams+ ) {const tool_calls = run.required_action?.submit_tool_outputs?.tool_calls || [];const tool_outputs: Array<OpenAI.Beta.Threads.Runs.RunSubmitToolOutputsParams.ToolOutput> =await Promise.all(@@ -116,7 +125,8 @@ export class OpenAIToolset extends BaseNonAgenticToolset<OpenAiToolCollection, O// Execute each tool call and get the responseconst tool_response = await this.executeToolCall(tool_call as OpenAI.ChatCompletionMessageToolCall,- userId+ userId,+ modifiers);logger.debug(`Tool call ${tool_call.id} executed with response: ${tool_response}`);@@ -143,7 +153,8 @@ export class OpenAIToolset extends BaseNonAgenticToolset<OpenAiToolCollection, Oclient: OpenAI,runStream: Stream<OpenAI.Beta.Assistants.AssistantStreamEvent>,thread: OpenAI.Beta.Threads.Thread,- userId?: string+ userId?: string,+ modifiers?: ExecuteToolModifiersParams) {// @TODO: Log the run streamconst defaultUserId = this.getComposio()?.userId;@@ -164,7 +175,11 @@ export class OpenAIToolset extends BaseNonAgenticToolset<OpenAiToolCollection, O// Handle the 'requires_action' eventif (event.event === 'thread.run.requires_action') {- const toolOutputs = await this.handleAssistantMessage(event.data, userId ?? defaultUserId);+ const toolOutputs = await this.handleAssistantMessage(+ event.data,+ userId ?? defaultUserId,+ modifiers+ );// Submit the tool outputsawait client.beta.threads.runs.submitToolOutputs(thread.id, runId, {@@ -194,7 +209,11 @@ export class OpenAIToolset extends BaseNonAgenticToolset<OpenAiToolCollection, Owhile (['queued', 'in_progress', 'requires_action'].includes(finalRun.status)) {if (finalRun.status === 'requires_action') {- const toolOutputs = await this.handleAssistantMessage(finalRun, userId ?? defaultUserId);+ const toolOutputs = await this.handleAssistantMessage(+ finalRun,+ userId ?? defaultUserId,+ modifiers+ );// Submit tool outputsfinalRun = await client.beta.threads.runs.submitToolOutputs(thread.id, runId, {@@ -221,12 +240,17 @@ export class OpenAIToolset extends BaseNonAgenticToolset<OpenAiToolCollection, Oclient: OpenAI,run: OpenAI.Beta.Threads.Run,thread: OpenAI.Beta.Threads.Thread,- userId?: string+ userId?: string,+ modifiers?: ExecuteToolModifiersParams) {const defaultUserId = this.getComposio()?.userId;while (['queued', 'in_progress', 'requires_action'].includes(run.status)) {// logger.debug(`Current run status: ${run.status}`);- const tool_outputs = await this.handleAssistantMessage(run, userId ?? defaultUserId);+ const tool_outputs = await this.handleAssistantMessage(+ run,+ userId ?? defaultUserId,+ modifiers+ );if (run.status === 'requires_action') {// logger.debug(// `Submitting tool outputs for run ID: ${run.id} in thread ID: ${thread.id}`