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 21, 04:45 UTC
| 1 | /** |
| 2 | * Cloudflare AI Provider |
| 3 | * |
| 4 | * Author: Musthaq Ahamad <musthaq@composio.dev> |
| 5 | * Legacy Reference: https://github.com/ComposioHQ/composio/blob/master/js/src/frameworks/cloudflare.ts |
| 6 | * |
| 7 | * This provider provides a set of tools for interacting with Cloudflare AI. |
| 8 | * |
| 9 | * @packageDocumentation |
| 10 | * @module providers/cloudflare |
| 11 | */ |
| 12 | import { AiTextGenerationToolInput } from '@cloudflare/workers-types'; |
| 13 | import { |
| 14 | Tool, |
| 15 | ExecuteToolModifiers, |
| 16 | BaseNonAgenticProvider, |
| 17 | ToolExecuteParams, |
| 18 | ExecuteToolFnOptions, |
| 19 | ToolCallExecutionTarget, |
| 20 | ToolCallSession, |
| 21 | McpUrlResponse, |
| 22 | McpServerGetResponse, |
| 23 | normalizeToolArguments, |
| 24 | deduplicateJsonSchemaRequiredArrays, |
| 25 | } from '@composio/core'; |
| 26 | |
| 27 | type AiToolCollection = Record<string, AiTextGenerationToolInput>; |
| 28 | |
| 29 | export class CloudflareProvider extends BaseNonAgenticProvider< |
| 30 | AiToolCollection, |
| 31 | AiTextGenerationToolInput, |
| 32 | McpServerGetResponse |
| 33 | > { |
| 34 | readonly name = 'cloudflare'; |
| 35 | |
| 36 | /** |
| 37 | * Creates a new instance of the CloudflareProvider. |
| 38 | * |
| 39 | * This provider enables integration with Cloudflare AI, |
| 40 | * allowing Composio tools to be used with Cloudflare Workers AI. |
| 41 | * |
| 42 | * @example |
| 43 | * ```typescript |
| 44 | * |
| 45 | * // Use with Composio |
| 46 | * const composio = new Composio({ |
| 47 | * apiKey: 'your-api-key', |
| 48 | * provider: new CloudflareProvider() |
| 49 | * }); |
| 50 | * |
| 51 | * // Use the provider to wrap tools for Cloudflare AI |
| 52 | * const cloudflareTools = provider.wrapTools(composioTools); |
| 53 | * ``` |
| 54 | */ |
| 55 | constructor() { |
| 56 | super(); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Transform MCP URL response into Anthropic-specific format. |
| 61 | * By default, Anthropic uses the standard format (same as default), |
| 62 | * but this method is here to show providers can customize if needed. |
| 63 | * |
| 64 | * @param data - The MCP URL response data |
| 65 | * @returns Standard MCP server response format |
| 66 | */ |
| 67 | wrapMcpServerResponse(data: McpUrlResponse): McpServerGetResponse { |
| 68 | // Anthropic uses the standard format |
| 69 | return data.map(item => ({ |
| 70 | url: new URL(item.url), |
| 71 | name: item.name, |
| 72 | })) as McpServerGetResponse; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Wraps a Composio tool in the Cloudflare AI format. |
| 77 | * |
| 78 | * This method transforms a Composio tool definition into the format |
| 79 | * expected by Cloudflare's AI API for function calling. |
| 80 | * |
| 81 | * @param tool - The Composio tool to wrap |
| 82 | * @returns The wrapped tool in Cloudflare AI format |
| 83 | * |
| 84 | * @example |
| 85 | * ```typescript |
| 86 | * // Wrap a single tool for use with Cloudflare AI |
| 87 | * const composioTool = { |
| 88 | * slug: 'SEARCH_TOOL', |
| 89 | * description: 'Search for information', |
| 90 | * inputParameters: { |
| 91 | * type: 'object', |
| 92 | * properties: { |
| 93 | * query: { type: 'string', description: 'Search query' } |
| 94 | * }, |
| 95 | * required: ['query'] |
| 96 | * } |
| 97 | * }; |
| 98 | * |
| 99 | * const cloudflareTool = provider.wrapTool(composioTool); |
| 100 | * ``` |
| 101 | */ |
| 102 | wrapTool(tool: Tool): AiTextGenerationToolInput { |
| 103 | const inputParameters = deduplicateJsonSchemaRequiredArrays(tool.inputParameters); |
| 104 | const formattedSchema: AiTextGenerationToolInput['function'] = { |
| 105 | name: tool.slug!, |
| 106 | description: tool.description!, |
| 107 | parameters: inputParameters as unknown as { |
| 108 | type: 'object'; |
| 109 | properties: { |
| 110 | [key: string]: { |
| 111 | type: string; |
| 112 | description?: string; |
| 113 | }; |
| 114 | }; |
| 115 | required: string[]; |
| 116 | }, |
| 117 | }; |
| 118 | const cloudflareTool: AiTextGenerationToolInput = { |
| 119 | type: 'function', |
| 120 | function: formattedSchema, |
| 121 | }; |
| 122 | return cloudflareTool; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Wraps a list of Composio tools in the Cloudflare AI format. |
| 127 | * |
| 128 | * This method transforms multiple Composio tool definitions into the format |
| 129 | * expected by Cloudflare's AI API for function calling, organizing them |
| 130 | * into a dictionary keyed by tool slug. |
| 131 | * |
| 132 | * @param tools - Array of Composio tools to wrap |
| 133 | * @returns Dictionary of wrapped tools in Cloudflare AI format |
| 134 | * |
| 135 | * @example |
| 136 | * ```typescript |
| 137 | * // Wrap multiple tools for use with Cloudflare AI |
| 138 | * const composioTools = [ |
| 139 | * { |
| 140 | * slug: 'SEARCH_TOOL', |
| 141 | * description: 'Search for information', |
| 142 | * inputParameters: { |
| 143 | * type: 'object', |
| 144 | * properties: { |
| 145 | * query: { type: 'string' } |
| 146 | * }, |
| 147 | * required: ['query'] |
| 148 | * } |
| 149 | * }, |
| 150 | * { |
| 151 | * slug: 'WEATHER_TOOL', |
| 152 | * description: 'Get weather information', |
| 153 | * inputParameters: { |
| 154 | * type: 'object', |
| 155 | * properties: { |
| 156 | * location: { type: 'string' } |
| 157 | * }, |
| 158 | * required: ['location'] |
| 159 | * } |
| 160 | * } |
| 161 | * ]; |
| 162 | * |
| 163 | * const cloudflareTools = provider.wrapTools(composioTools); |
| 164 | * |
| 165 | * // Use with Cloudflare Workers AI |
| 166 | * // In your Cloudflare Worker: |
| 167 | * const ai = new Ai(env.AI); |
| 168 | * |
| 169 | * const response = await ai.run('@cf/meta/llama-3-8b-instruct', { |
| 170 | * messages: [{ role: 'user', content: 'How is the weather in New York?' }], |
| 171 | * tools: cloudflareTools |
| 172 | * }); |
| 173 | * ``` |
| 174 | */ |
| 175 | wrapTools(tools: Tool[]): AiToolCollection { |
| 176 | return tools.reduce( |
| 177 | (acc, tool) => ({ |
| 178 | ...acc, |
| 179 | [tool.slug]: this.wrapTool(tool), |
| 180 | }), |
| 181 | {} |
| 182 | ); |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Executes a tool call from Cloudflare AI. |
| 187 | * |
| 188 | * This method processes a function call from Cloudflare's AI API, |
| 189 | * executes the corresponding Composio tool, and returns the result. |
| 190 | * |
| 191 | * @param executionTarget - A user ID for direct tools or the session that produced session tools |
| 192 | * @param tool - The tool call object with name and arguments |
| 193 | * @param options - Optional execution options like connected account ID (user ID targets only) |
| 194 | * @param modifiers - Optional execution modifiers for tool behavior (user ID targets only) |
| 195 | * @returns The result of the tool execution as a JSON string |
| 196 | * |
| 197 | * @example |
| 198 | * ```typescript |
| 199 | * // Execute a tool call from Cloudflare AI |
| 200 | * const toolCall = { |
| 201 | * name: 'SEARCH_TOOL', |
| 202 | * arguments: { |
| 203 | * query: 'composio documentation' |
| 204 | * } |
| 205 | * }; |
| 206 | * |
| 207 | * const result = await provider.executeToolCall( |
| 208 | * 'user123', |
| 209 | * toolCall, |
| 210 | * { connectedAccountId: 'conn_xyz456' } |
| 211 | * ); |
| 212 | * |
| 213 | * // Parse the result and use it in your application |
| 214 | * const searchResults = JSON.parse(result); |
| 215 | * console.log(searchResults); |
| 216 | * |
| 217 | * // You can also use the result to continue the conversation |
| 218 | * // In your Cloudflare Worker: |
| 219 | * const ai = new Ai(env.AI); |
| 220 | * |
| 221 | * await ai.run('@cf/meta/llama-3-8b-instruct', { |
| 222 | * messages: [ |
| 223 | * { role: 'user', content: 'Search for Composio' }, |
| 224 | * { role: 'assistant', content: '', tool_calls: [{ name: 'SEARCH_TOOL', arguments: JSON.stringify({ query: 'composio documentation' }) }] }, |
| 225 | * { role: 'tool', tool_call_id: '1', content: result } |
| 226 | * ], |
| 227 | * tools: cloudflareTools |
| 228 | * }); |
| 229 | * |
| 230 | * // For tools from session.tools(), pass the session instead of a user ID |
| 231 | * const sessionResult = await provider.executeToolCall(session, toolCall); |
| 232 | * ``` |
| 233 | */ |
| 234 | async executeToolCall( |
| 235 | session: ToolCallSession, |
| 236 | tool: { name: string; arguments: unknown } |
| 237 | ): Promise<string>; |
| 238 | async executeToolCall( |
| 239 | userId: string, |
| 240 | tool: { name: string; arguments: unknown }, |
| 241 | options?: ExecuteToolFnOptions, |
| 242 | modifiers?: ExecuteToolModifiers |
| 243 | ): Promise<string>; |
| 244 | async executeToolCall( |
| 245 | executionTarget: ToolCallExecutionTarget, |
| 246 | tool: { name: string; arguments: unknown }, |
| 247 | options?: ExecuteToolFnOptions, |
| 248 | modifiers?: ExecuteToolModifiers |
| 249 | ): Promise<string> { |
| 250 | // Models occasionally emit tool arguments as a JSON string rather than an object (issue #2406). |
| 251 | const toolArguments = normalizeToolArguments(tool.arguments, tool.name); |
| 252 | if (typeof executionTarget !== 'string') { |
| 253 | const result = await this.executeToolForTarget( |
| 254 | executionTarget, |
| 255 | tool.name, |
| 256 | toolArguments, |
| 257 | options, |
| 258 | modifiers |
| 259 | ); |
| 260 | return JSON.stringify(result); |
| 261 | } |
| 262 | |
| 263 | // Keep direct execution on `executeTool`, which every supported @composio/core provides. |
| 264 | const payload: ToolExecuteParams = { |
| 265 | arguments: toolArguments, |
| 266 | connectedAccountId: options?.connectedAccountId, |
| 267 | customAuthParams: options?.customAuthParams, |
| 268 | customConnectionData: options?.customConnectionData, |
| 269 | userId: executionTarget, |
| 270 | }; |
| 271 | |
| 272 | const result = await this.executeTool(tool.name, payload, modifiers); |
| 273 | return JSON.stringify(result); |
| 274 | } |
| 275 | } |