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, 06:00 UTC
| 1 | # @composio/cloudflare |
| 2 | |
| 3 | The Cloudflare provider formats Composio tools for Cloudflare Workers AI function calling (`AiTextGenerationToolInput`). |
| 4 | |
| 5 | ## Installation |
| 6 | |
| 7 | ```bash |
| 8 | npm install @composio/core @composio/cloudflare @cloudflare/workers-types |
| 9 | ``` |
| 10 | |
| 11 | Set `COMPOSIO_API_KEY` with your API key from [the dashboard](https://dashboard.composio.dev/settings). In a Worker, add it as a secret (`wrangler secret put COMPOSIO_API_KEY`) and bind Workers AI as `AI` in your `wrangler.toml`; no separate model API key is needed. |
| 12 | |
| 13 | ## Quickstart |
| 14 | |
| 15 | Create a session for your user, pass its tools to `env.AI.run`, and execute any tool calls the model returns: |
| 16 | |
| 17 | ```typescript |
| 18 | import { Composio } from '@composio/core'; |
| 19 | import { CloudflareProvider } from '@composio/cloudflare'; |
| 20 | |
| 21 | export default { |
| 22 | async fetch(request: Request, env: Env): Promise<Response> { |
| 23 | const composio = new Composio({ |
| 24 | apiKey: env.COMPOSIO_API_KEY, |
| 25 | provider: new CloudflareProvider(), |
| 26 | }); |
| 27 | |
| 28 | // Create a session for your user |
| 29 | const session = await composio.create('user_123'); |
| 30 | const tools = await session.tools(); |
| 31 | |
| 32 | const response = await env.AI.run('@cf/meta/llama-3.1-8b-instruct', { |
| 33 | messages: [ |
| 34 | { role: 'system', content: 'You are a helpful assistant.' }, |
| 35 | { role: 'user', content: 'Summarize my emails from today' }, |
| 36 | ], |
| 37 | // session.tools() returns a collection keyed by tool slug |
| 38 | tools: Object.values(tools), |
| 39 | }); |
| 40 | |
| 41 | const results = []; |
| 42 | for (const toolCall of response.tool_calls ?? []) { |
| 43 | results.push(await composio.provider.executeToolCall(session, toolCall)); |
| 44 | } |
| 45 | |
| 46 | return Response.json({ response, results }); |
| 47 | }, |
| 48 | }; |
| 49 | ``` |
| 50 | |
| 51 | `executeToolCall` takes the `{ name, arguments }` tool calls that Workers AI emits, runs the matching Composio tool through the session that produced the tools, and returns the result as a JSON string. For tools fetched with `tools.get()`, pass the user ID instead: `executeToolCall(userId, toolCall, options?, modifiers?)`. |
| 52 | |
| 53 | ## Links |
| 54 | |
| 55 | - [Composio documentation](https://docs.composio.dev) |