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, 03:37 UTC
| 1 | /** |
| 2 | * Cumulative stages of the standup-bot example's files, built up one piece at a |
| 3 | * time. Each stage's `code` is the full file at that point; <FileBuildup> diffs |
| 4 | * consecutive stages so the reader watches the file grow. |
| 5 | * |
| 6 | * These are teaching versions: tighter than the real repo (which factors helpers |
| 7 | * into separate files), but the same Composio calls. <RepoBrowser> shows the |
| 8 | * real split. |
| 9 | */ |
| 10 | |
| 11 | export interface BuildStage { |
| 12 | title: string; |
| 13 | description: string; |
| 14 | /** Full file contents at this stage. */ |
| 15 | code: string; |
| 16 | } |
| 17 | |
| 18 | // ── setup.ts: connect the bot's own Slack app ─────────────────────────────── |
| 19 | |
| 20 | const setup1 = `import { Composio } from '@composio/core'; |
| 21 | |
| 22 | const composio = new Composio({ apiKey: process.env.COMPOSIO_API_KEY }); |
| 23 | const AUTH_CONFIG = process.env.COMPOSIO_SLACKBOT_AUTH_CONFIG_ID!; |
| 24 | |
| 25 | // Connect the bot's own Slack app once, so it can post and DM as the bot. |
| 26 | async function main() { |
| 27 | const session = await composio.create('default', { |
| 28 | authConfigs: { slackbot: AUTH_CONFIG }, |
| 29 | }); |
| 30 | |
| 31 | const toolkits = await session.toolkits({ toolkits: ['slackbot'] }); |
| 32 | const active = toolkits.items.find((t) => t.slug === 'slackbot')?.connection?.isActive; |
| 33 | if (active) { |
| 34 | console.log('Bot already connected.'); |
| 35 | return; |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | main(); |
| 40 | `; |
| 41 | |
| 42 | const setup2 = `import { Composio } from '@composio/core'; |
| 43 | |
| 44 | const composio = new Composio({ apiKey: process.env.COMPOSIO_API_KEY }); |
| 45 | const AUTH_CONFIG = process.env.COMPOSIO_SLACKBOT_AUTH_CONFIG_ID!; |
| 46 | |
| 47 | // Connect the bot's own Slack app once, so it can post and DM as the bot. |
| 48 | async function main() { |
| 49 | const session = await composio.create('default', { |
| 50 | authConfigs: { slackbot: AUTH_CONFIG }, |
| 51 | }); |
| 52 | |
| 53 | const toolkits = await session.toolkits({ toolkits: ['slackbot'] }); |
| 54 | const active = toolkits.items.find((t) => t.slug === 'slackbot')?.connection?.isActive; |
| 55 | if (active) { |
| 56 | console.log('Bot already connected.'); |
| 57 | return; |
| 58 | } |
| 59 | |
| 60 | // Not connected: print the Connect Link, then wait for the user to finish. |
| 61 | const connectionRequest = await session.authorize('slackbot'); |
| 62 | console.log('Authorize the bot:', connectionRequest.redirectUrl); |
| 63 | |
| 64 | const account = await connectionRequest.waitForConnection(); |
| 65 | console.log('Bot connected:', account.id); |
| 66 | } |
| 67 | |
| 68 | main(); |
| 69 | `; |
| 70 | |
| 71 | // ── slack.ts: send with tools, open the modal through the proxy ───────────── |
| 72 | |
| 73 | const proxy1 = `import { composio } from './composio'; |
| 74 | |
| 75 | const BOT_USER = 'default'; |
| 76 | |
| 77 | // Sending a message is a named tool, even when it carries interactive buttons: |
| 78 | // SLACKBOT_SEND_MESSAGE takes markdown_text for prose, or Block Kit \`blocks\`. |
| 79 | export async function postMessage(channel: string, text: string, blocks?: unknown[]) { |
| 80 | const res = await composio.tools.execute('SLACKBOT_SEND_MESSAGE', { |
| 81 | userId: BOT_USER, |
| 82 | arguments: blocks ? { channel, blocks } : { channel, markdown_text: text }, |
| 83 | }); |
| 84 | return res.data as { ts?: string }; |
| 85 | } |
| 86 | |
| 87 | // Updating the draft after an edit is a tool too: SLACKBOT_UPDATES_A_MESSAGE. |
| 88 | export async function updateMessage(channel: string, ts: string, blocks: unknown[]) { |
| 89 | await composio.tools.execute('SLACKBOT_UPDATES_A_MESSAGE', { |
| 90 | userId: BOT_USER, |
| 91 | arguments: { channel, ts, blocks }, |
| 92 | }); |
| 93 | } |
| 94 | `; |
| 95 | |
| 96 | const proxy2 = `import { composio } from './composio'; |
| 97 | |
| 98 | const BOT_USER = 'default'; |
| 99 | |
| 100 | // Sending a message is a named tool, even when it carries interactive buttons: |
| 101 | // SLACKBOT_SEND_MESSAGE takes markdown_text for prose, or Block Kit \`blocks\`. |
| 102 | export async function postMessage(channel: string, text: string, blocks?: unknown[]) { |
| 103 | const res = await composio.tools.execute('SLACKBOT_SEND_MESSAGE', { |
| 104 | userId: BOT_USER, |
| 105 | arguments: blocks ? { channel, blocks } : { channel, markdown_text: text }, |
| 106 | }); |
| 107 | return res.data as { ts?: string }; |
| 108 | } |
| 109 | |
| 110 | // Updating the draft after an edit is a tool too: SLACKBOT_UPDATES_A_MESSAGE. |
| 111 | export async function updateMessage(channel: string, ts: string, blocks: unknown[]) { |
| 112 | await composio.tools.execute('SLACKBOT_UPDATES_A_MESSAGE', { |
| 113 | userId: BOT_USER, |
| 114 | arguments: { channel, ts, blocks }, |
| 115 | }); |
| 116 | } |
| 117 | |
| 118 | // Opening a modal (views.open) has no Composio tool, so it drops to the proxy. |
| 119 | // proxyExecute hits the raw endpoint as the bot's connected account, the escape |
| 120 | // hatch for anything the named tools don't cover. |
| 121 | export async function openModal(triggerId: string, view: unknown) { |
| 122 | const { items } = await composio.connectedAccounts.list({ |
| 123 | userIds: [BOT_USER], |
| 124 | toolkitSlugs: ['slackbot'], |
| 125 | statuses: ['ACTIVE'], |
| 126 | }); |
| 127 | await composio.tools.proxyExecute({ |
| 128 | endpoint: '/views.open', |
| 129 | method: 'POST', |
| 130 | body: { trigger_id: triggerId, view }, |
| 131 | connectedAccountId: items[0]?.id, |
| 132 | }); |
| 133 | } |
| 134 | `; |
| 135 | |
| 136 | // ── agent.ts: research connected tools and write the draft ────────────────── |
| 137 | |
| 138 | const agent1 = `import { Composio } from '@composio/core'; |
| 139 | import { VercelProvider } from '@composio/vercel'; |
| 140 | import { generateText, stepCountIs } from 'ai'; |
| 141 | |
| 142 | const composio = new Composio({ |
| 143 | apiKey: process.env.COMPOSIO_API_KEY, |
| 144 | provider: new VercelProvider(), |
| 145 | }); |
| 146 | |
| 147 | // The toolkits the bot can draft from. A member only has some of these connected, |
| 148 | // and that's fine: the session just uses whatever they've actually authorized. |
| 149 | const TOOLKITS = ['github', 'linear', 'notion', 'googlecalendar', 'slack']; |
| 150 | |
| 151 | // Spin up a tool-router session for one member and let the agent research and |
| 152 | // write their standup. session.tools() returns Composio's research meta-tools |
| 153 | // (search / execute / workbench), scoped to those toolkits. |
| 154 | export async function generateDraft(memberEmail: string) { |
| 155 | const session = await composio.create(memberEmail, { toolkits: TOOLKITS }); |
| 156 | const tools = await session.tools(); |
| 157 | |
| 158 | const { text } = await generateText({ |
| 159 | model: 'anthropic/claude-sonnet-4-5', |
| 160 | system: "Write a concise daily standup from the member's recent activity.", |
| 161 | prompt: 'Research and write the standup update.', |
| 162 | tools, |
| 163 | stopWhen: stepCountIs(40), |
| 164 | }); |
| 165 | return text.trim(); |
| 166 | } |
| 167 | `; |
| 168 | |
| 169 | const agent2 = `import { Composio } from '@composio/core'; |
| 170 | import { VercelProvider } from '@composio/vercel'; |
| 171 | import { generateText, stepCountIs } from 'ai'; |
| 172 | |
| 173 | const composio = new Composio({ |
| 174 | apiKey: process.env.COMPOSIO_API_KEY, |
| 175 | provider: new VercelProvider(), |
| 176 | }); |
| 177 | |
| 178 | // The toolkits the bot can draft from. A member only has some of these connected, |
| 179 | // and that's fine: the session just uses whatever they've actually authorized. |
| 180 | const TOOLKITS = ['github', 'linear', 'notion', 'googlecalendar', 'slack']; |
| 181 | |
| 182 | // Spin up a tool-router session for one member and let the agent research and |
| 183 | // write their standup. session.tools() returns Composio's research meta-tools |
| 184 | // (search / execute / workbench), scoped to those toolkits. |
| 185 | export async function generateDraft(memberEmail: string) { |
| 186 | // manageConnections:false strips the connection meta-tools. The agent drafts |
| 187 | // from whatever the member already connected and never starts an OAuth flow |
| 188 | // mid-draft: if a tool needs auth, it's simply not in the session. |
| 189 | const session = await composio.create(memberEmail, { |
| 190 | toolkits: TOOLKITS, |
| 191 | manageConnections: false, |
| 192 | }); |
| 193 | const tools = await session.tools(); |
| 194 | |
| 195 | const { text } = await generateText({ |
| 196 | model: 'anthropic/claude-sonnet-4-5', |
| 197 | system: "Write a concise daily standup from the member's recent activity.", |
| 198 | prompt: 'Research and write the standup update.', |
| 199 | tools, |
| 200 | stopWhen: stepCountIs(40), |
| 201 | }); |
| 202 | return text.trim(); |
| 203 | } |
| 204 | `; |
| 205 | |
| 206 | // ── api/interactivity.ts: route button clicks ────────────────────────────── |
| 207 | |
| 208 | const buttons1 = `import { verifySlackSignature, readRawBody } from './_utils/slack'; |
| 209 | |
| 210 | // Slack POSTs here every time someone clicks a button. Verify it really came |
| 211 | // from Slack, then ack within 3 seconds (Slack retries if you're slow). |
| 212 | export default async function handler(req: Request, res: Response) { |
| 213 | const body = await readRawBody(req); |
| 214 | if (!verifySlackSignature(body, req.headers)) return res.status(401).end(); |
| 215 | |
| 216 | const payload = JSON.parse(new URLSearchParams(body).get('payload') ?? '{}'); |
| 217 | res.status(200).end(); // ack immediately |
| 218 | await handleClick(payload); // then do the slow work |
| 219 | } |
| 220 | `; |
| 221 | |
| 222 | const buttons2 = `import { verifySlackSignature, readRawBody, updateMessage, postAsMember } from './_utils/slack'; |
| 223 | import { generateDraft } from './_utils/agent'; |
| 224 | import { draftMessage, connectMenu } from './_utils/blocks'; |
| 225 | |
| 226 | type SlackInteractionPayload = { |
| 227 | actions?: Array<{ |
| 228 | action_id?: string; |
| 229 | value?: string; |
| 230 | }>; |
| 231 | }; |
| 232 | |
| 233 | // Slack POSTs here every time someone clicks a button. Verify it really came |
| 234 | // from Slack, then ack within 3 seconds (Slack retries if you're slow). |
| 235 | export default async function handler(req: Request, res: Response) { |
| 236 | const body = await readRawBody(req); |
| 237 | if (!verifySlackSignature(body, req.headers)) return res.status(401).end(); |
| 238 | |
| 239 | const payload = JSON.parse(new URLSearchParams(body).get('payload') ?? '{}'); |
| 240 | res.status(200).end(); // ack immediately |
| 241 | await handleClick(payload); // then do the slow work |
| 242 | } |
| 243 | |
| 244 | // Each button carried its context in \`value\`, so the handler knows exactly what |
| 245 | // to do. No model decides anything here: the flow is fixed. |
| 246 | async function handleClick(payload: SlackInteractionPayload) { |
| 247 | const action = payload.actions?.[0]; |
| 248 | const ctx = JSON.parse(action?.value ?? '{}'); |
| 249 | |
| 250 | if (action?.action_id === 'draft') { |
| 251 | const draft = await generateDraft(ctx.memberEmail); // launch the subagent |
| 252 | await updateMessage(ctx.dmChannel, ctx.dmTs, draftMessage(draft, ctx)); |
| 253 | } else if (action?.action_id === 'connect') { |
| 254 | await updateMessage(ctx.dmChannel, ctx.dmTs, connectMenu(ctx)); |
| 255 | } else if (action?.action_id === 'confirm') { |
| 256 | await postAsMember(ctx.memberEmail, ctx.channel, ctx.draft, ctx.threadTs); |
| 257 | } |
| 258 | } |
| 259 | `; |
| 260 | |
| 261 | export const FILE_BUILDS: Record<string, { file: string; stages: BuildStage[] }> = { |
| 262 | setup: { |
| 263 | file: 'scripts/setup.ts', |
| 264 | stages: [ |
| 265 | { |
| 266 | title: 'Bind a session to your Slack auth config', |
| 267 | description: |
| 268 | "The bot authenticates as your own Slack app. Create a session against the slackbot auth config and check whether it's already connected.", |
| 269 | code: setup1, |
| 270 | }, |
| 271 | { |
| 272 | title: 'Authorize and wait', |
| 273 | description: |
| 274 | 'If not connected, `session.authorize()` returns a Connect Link. Print it, then `waitForConnection()` resolves once the bot finishes OAuth. That connected account is the identity the bot posts as.', |
| 275 | code: setup2, |
| 276 | }, |
| 277 | ], |
| 278 | }, |
| 279 | proxy: { |
| 280 | file: 'api/_utils/slack.ts', |
| 281 | stages: [ |
| 282 | { |
| 283 | title: 'Send and update with named tools', |
| 284 | description: |
| 285 | 'Sending and updating are tools: SLACKBOT_SEND_MESSAGE and SLACKBOT_UPDATES_A_MESSAGE both take Block Kit `blocks`, so even the interactive button menus go through tools, not the proxy.', |
| 286 | code: proxy1, |
| 287 | }, |
| 288 | { |
| 289 | title: 'The proxy for the one thing without a tool', |
| 290 | description: |
| 291 | "Opening a modal (views.open) has no Composio tool, so it drops to proxyExecute as the bot's connected account: the escape hatch for anything the named tools don't cover.", |
| 292 | code: proxy2, |
| 293 | }, |
| 294 | ], |
| 295 | }, |
| 296 | buttons: { |
| 297 | file: 'api/interactivity.ts', |
| 298 | stages: [ |
| 299 | { |
| 300 | title: 'Verify the request and ack fast', |
| 301 | description: |
| 302 | "Slack POSTs to this endpoint on every click. Verify the signature, then respond within 3 seconds so Slack doesn't retry, and handle the click after.", |
| 303 | code: buttons1, |
| 304 | }, |
| 305 | { |
| 306 | title: 'Route on the action', |
| 307 | description: |
| 308 | 'The button carried its context in `value`. Switch on the `action_id` and call the right function. Draft launches the subagent; the rest post through the proxy. The flow is deterministic, no model in the loop.', |
| 309 | code: buttons2, |
| 310 | }, |
| 311 | ], |
| 312 | }, |
| 313 | draft: { |
| 314 | file: 'api/_utils/agent.ts', |
| 315 | stages: [ |
| 316 | { |
| 317 | title: 'A tool-router session writes the draft', |
| 318 | description: |
| 319 | "Create a session for the member, scoped to the toolkit catalogue, hand its research tools to the model, and let it investigate and write. You don't list what they connected: the session just uses whatever they've authorized.", |
| 320 | code: agent1, |
| 321 | }, |
| 322 | { |
| 323 | title: 'Keep the agent from connecting mid-draft', |
| 324 | description: |
| 325 | 'manageConnections:false removes the connection meta-tools, so the agent drafts from what the member already connected and never starts an OAuth flow while drafting.', |
| 326 | code: agent2, |
| 327 | }, |
| 328 | ], |
| 329 | }, |
| 330 | }; |