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 | * Focused E2E test: Custom tools + custom toolkits against live backend. |
| 3 | * |
| 4 | * Tests session creation, local execution, search indexing, and all three |
| 5 | * tool types (standalone, extendsToolkit, custom toolkit). |
| 6 | * |
| 7 | * Usage: |
| 8 | * COMPOSIO_API_KEY=... COMPOSIO_BASE_URL=... bun src/custom-tools-e2e.ts |
| 9 | */ |
| 10 | import { Composio, experimental_createTool, experimental_createToolkit } from "@composio/core"; |
| 11 | import { z } from "zod/v3"; |
| 12 | |
| 13 | const apiKey = process.env.COMPOSIO_API_KEY; |
| 14 | const baseURL = process.env.COMPOSIO_BASE_URL; |
| 15 | if (!apiKey) { |
| 16 | console.error("COMPOSIO_API_KEY is required"); |
| 17 | process.exit(1); |
| 18 | } |
| 19 | |
| 20 | let passed = 0; |
| 21 | let failed = 0; |
| 22 | |
| 23 | function ok(label: string) { |
| 24 | passed++; |
| 25 | console.log(` ✓ ${label}`); |
| 26 | } |
| 27 | function fail(label: string, err: unknown) { |
| 28 | failed++; |
| 29 | console.error(` ✗ ${label}: ${err instanceof Error ? err.message : err}`); |
| 30 | } |
| 31 | |
| 32 | // ── Shared in-memory user store ────────────────────────────────── |
| 33 | |
| 34 | const userStore: Record<string, Record<string, unknown>> = { |
| 35 | "user-1": { id: "user-1", name: "Alice Johnson", email: "alice@acme.com", role: "admin", status: "active" }, |
| 36 | "user-2": { id: "user-2", name: "Bob Smith", email: "bob@acme.com", role: "developer", status: "active" }, |
| 37 | "user-3": { id: "user-3", name: "Carol Williams", email: "carol@acme.com", role: "viewer", status: "suspended" }, |
| 38 | }; |
| 39 | |
| 40 | // ──────────────────────────────────────────────────────────────── |
| 41 | // 1. Standalone tools (no auth) → LOCAL_<SLUG> |
| 42 | // ──────────────────────────────────────────────────────────────── |
| 43 | |
| 44 | const getUserDetails = experimental_createTool("GET_USER_DETAILS", { |
| 45 | name: "Get user details", |
| 46 | description: "Retrieve user profile information including name, email, role, and account status by user ID", |
| 47 | inputParams: z.object({ |
| 48 | user_id: z.string().describe("The unique user identifier"), |
| 49 | }), |
| 50 | execute: async (input) => { |
| 51 | const user = userStore[input.user_id]; |
| 52 | if (!user) throw new Error(`User "${input.user_id}" not found`); |
| 53 | return { ...user }; |
| 54 | }, |
| 55 | }); |
| 56 | |
| 57 | const listUserKeys = experimental_createTool("LIST_USER_KEYS", { |
| 58 | name: "List user API keys", |
| 59 | description: "List all active API keys for a given user, including creation date and last used timestamp", |
| 60 | inputParams: z.object({ |
| 61 | user_id: z.string().describe("The user whose keys to list"), |
| 62 | include_expired: z.boolean().default(false), |
| 63 | }), |
| 64 | execute: async (input) => { |
| 65 | const keys = [ |
| 66 | { key_id: "key_abc", prefix: "sk-***abc", expired: false }, |
| 67 | { key_id: "key_old", prefix: "sk-***old", expired: true }, |
| 68 | ]; |
| 69 | const filtered = input.include_expired ? keys : keys.filter((k) => !k.expired); |
| 70 | return { user_id: input.user_id, api_keys: filtered, total: filtered.length }; |
| 71 | }, |
| 72 | }); |
| 73 | |
| 74 | // ──────────────────────────────────────────────────────────────── |
| 75 | // 2. Extension tool (inherits gmail auth) → LOCAL_GMAIL_<SLUG> |
| 76 | // ──────────────────────────────────────────────────────────────── |
| 77 | |
| 78 | const formatGmailEmail = experimental_createTool("FORMAT_GMAIL_EMAIL", { |
| 79 | name: "Format Gmail email", |
| 80 | description: "Format and prepare an email draft with proper HTML formatting for sending via Gmail", |
| 81 | extendsToolkit: "gmail", |
| 82 | inputParams: z.object({ |
| 83 | to: z.string().describe("Recipient email address"), |
| 84 | subject: z.string().describe("Email subject line"), |
| 85 | body: z.string().describe("Email body content"), |
| 86 | }), |
| 87 | execute: async (input, ctx) => { |
| 88 | const htmlBody = input.body.replace(/\n/g, "<br>"); |
| 89 | return { |
| 90 | formatted: true, |
| 91 | to: input.to, |
| 92 | subject: input.subject, |
| 93 | html_body: htmlBody, |
| 94 | user_id: ctx.userId, |
| 95 | }; |
| 96 | }, |
| 97 | }); |
| 98 | |
| 99 | // ──────────────────────────────────────────────────────────────── |
| 100 | // 3. Custom toolkit (USER_MANAGEMENT) → LOCAL_USER_MANAGEMENT_<SLUG> |
| 101 | // ──────────────────────────────────────────────────────────────── |
| 102 | |
| 103 | const setUserRole = experimental_createTool("SET_USER_ROLE", { |
| 104 | name: "Set user role", |
| 105 | description: "Change the role of an existing user (admin, developer, viewer)", |
| 106 | inputParams: z.object({ |
| 107 | user_id: z.string().describe("The user to update"), |
| 108 | role: z.enum(["admin", "developer", "viewer"]).describe("The new role to assign"), |
| 109 | }), |
| 110 | execute: async (input) => { |
| 111 | const user = userStore[input.user_id]; |
| 112 | if (!user) throw new Error(`User "${input.user_id}" not found`); |
| 113 | const previousRole = user.role; |
| 114 | user.role = input.role; |
| 115 | return { user_id: input.user_id, previous_role: previousRole, new_role: input.role }; |
| 116 | }, |
| 117 | }); |
| 118 | |
| 119 | const updateUserStatus = experimental_createTool("UPDATE_USER_STATUS", { |
| 120 | name: "Update user status", |
| 121 | description: "Activate or suspend a user account", |
| 122 | inputParams: z.object({ |
| 123 | user_id: z.string().describe("The user to update"), |
| 124 | status: z.enum(["active", "suspended"]).describe("The new account status"), |
| 125 | reason: z.string().optional().describe("Reason for the status change"), |
| 126 | }), |
| 127 | execute: async (input) => { |
| 128 | const user = userStore[input.user_id]; |
| 129 | if (!user) throw new Error(`User "${input.user_id}" not found`); |
| 130 | const previousStatus = user.status; |
| 131 | user.status = input.status; |
| 132 | return { user_id: input.user_id, previous_status: previousStatus, new_status: input.status, reason: input.reason ?? "No reason provided" }; |
| 133 | }, |
| 134 | }); |
| 135 | |
| 136 | const userManagement = experimental_createToolkit("USER_MANAGEMENT", { |
| 137 | name: "User Management", |
| 138 | description: "Tools for managing user profiles, roles, and account status", |
| 139 | tools: [setUserRole, updateUserStatus], |
| 140 | }); |
| 141 | |
| 142 | const localTools = [getUserDetails, listUserKeys, formatGmailEmail]; |
| 143 | const localToolkits = [userManagement]; |
| 144 | |
| 145 | // ──────────────────────────────────────────────────────────────── |
| 146 | // Run tests |
| 147 | // ──────────────────────────────────────────────────────────────── |
| 148 | |
| 149 | const composio = new Composio({ apiKey, baseURL: baseURL ?? undefined }); |
| 150 | |
| 151 | async function main() { |
| 152 | const userId = "custom-tools-e2e-user"; |
| 153 | console.log(`\nCreating session for user: ${userId}`); |
| 154 | console.log(`Base URL: ${baseURL ?? "(default)"}\n`); |
| 155 | |
| 156 | // ── Session creation with all 3 tool types ── |
| 157 | let session: Awaited<ReturnType<typeof composio.create>>; |
| 158 | try { |
| 159 | session = await composio.create(userId, { |
| 160 | toolkits: ["weathermap", "gmail"], |
| 161 | manageConnections: false, |
| 162 | experimental: { |
| 163 | localTools, |
| 164 | localToolkits, |
| 165 | }, |
| 166 | }); |
| 167 | ok(`Session created: ${session.sessionId}`); |
| 168 | } catch (err) { |
| 169 | fail("Session creation", err); |
| 170 | console.error("\nCannot continue without a session."); |
| 171 | process.exit(1); |
| 172 | } |
| 173 | |
| 174 | // ── Existing session reuse with local custom definitions ── |
| 175 | try { |
| 176 | const reusedSession = await composio.use(session.sessionId, { localTools, localToolkits }); |
| 177 | const result = await reusedSession.execute("GET_USER_DETAILS", { user_id: "user-1" }); |
| 178 | if (result.data?.name !== "Alice Johnson") throw new Error(`Unexpected: ${JSON.stringify(result.data)}`); |
| 179 | ok("Use: existing session binds custom tools for local execution"); |
| 180 | } catch (err) { |
| 181 | fail("Use: existing session with custom tools", err); |
| 182 | } |
| 183 | |
| 184 | // ════════════════════════════════════════════════════════════════ |
| 185 | // Standalone tools (LOCAL_GET_USER_DETAILS, LOCAL_LIST_USER_KEYS) |
| 186 | // ════════════════════════════════════════════════════════════════ |
| 187 | |
| 188 | try { |
| 189 | const result = await session.execute("GET_USER_DETAILS", { user_id: "user-1" }); |
| 190 | if (result.data?.name !== "Alice Johnson") throw new Error(`Unexpected: ${JSON.stringify(result.data)}`); |
| 191 | ok("Standalone: execute by original slug (GET_USER_DETAILS)"); |
| 192 | } catch (err) { |
| 193 | fail("Standalone: execute by original slug", err); |
| 194 | } |
| 195 | |
| 196 | try { |
| 197 | const result = await session.execute("LOCAL_GET_USER_DETAILS", { user_id: "user-2" }); |
| 198 | if (result.data?.name !== "Bob Smith") throw new Error(`Unexpected: ${JSON.stringify(result.data)}`); |
| 199 | ok("Standalone: execute by prefixed slug (LOCAL_GET_USER_DETAILS)"); |
| 200 | } catch (err) { |
| 201 | fail("Standalone: execute by prefixed slug", err); |
| 202 | } |
| 203 | |
| 204 | try { |
| 205 | const result = await session.execute("LIST_USER_KEYS", { user_id: "user-1" }); |
| 206 | if (result.data?.total !== 1) throw new Error(`Expected 1 active key, got: ${JSON.stringify(result.data)}`); |
| 207 | ok("Standalone: second tool routes correctly (LIST_USER_KEYS)"); |
| 208 | } catch (err) { |
| 209 | fail("Standalone: second tool routes correctly", err); |
| 210 | } |
| 211 | |
| 212 | try { |
| 213 | const result = await session.execute("LIST_USER_KEYS", { user_id: "user-1", include_expired: true }); |
| 214 | if (result.data?.total !== 2) throw new Error(`Expected 2 keys with expired, got: ${JSON.stringify(result.data)}`); |
| 215 | ok("Standalone: Zod defaults overridden (include_expired=true → 2 keys)"); |
| 216 | } catch (err) { |
| 217 | fail("Standalone: Zod defaults overridden", err); |
| 218 | } |
| 219 | |
| 220 | // ════════════════════════════════════════════════════════════════ |
| 221 | // Extension tool (LOCAL_GMAIL_FORMAT_GMAIL_EMAIL) |
| 222 | // ════════════════════════════════════════════════════════════════ |
| 223 | |
| 224 | try { |
| 225 | const result = await session.execute("FORMAT_GMAIL_EMAIL", { |
| 226 | to: "test@example.com", |
| 227 | subject: "Hello", |
| 228 | body: "Line 1\nLine 2", |
| 229 | }); |
| 230 | if (result.error) throw new Error(`Execute error: ${result.error}`); |
| 231 | if (result.data?.formatted !== true) throw new Error(`Not formatted: ${JSON.stringify(result.data)}`); |
| 232 | if (result.data?.html_body !== "Line 1<br>Line 2") throw new Error(`HTML mismatch: ${result.data?.html_body}`); |
| 233 | if (result.data?.user_id !== userId) throw new Error(`UserId mismatch: ${result.data?.user_id}`); |
| 234 | ok("Extension: execute by original slug (FORMAT_GMAIL_EMAIL) + session context"); |
| 235 | } catch (err) { |
| 236 | fail("Extension: execute by original slug", err); |
| 237 | } |
| 238 | |
| 239 | try { |
| 240 | const result = await session.execute("LOCAL_GMAIL_FORMAT_GMAIL_EMAIL", { |
| 241 | to: "x@y.com", |
| 242 | subject: "Test", |
| 243 | body: "body", |
| 244 | }); |
| 245 | if (result.data?.to !== "x@y.com") throw new Error(`Unexpected: ${JSON.stringify(result.data)}`); |
| 246 | ok("Extension: execute by prefixed slug (LOCAL_GMAIL_FORMAT_GMAIL_EMAIL)"); |
| 247 | } catch (err) { |
| 248 | fail("Extension: execute by prefixed slug", err); |
| 249 | } |
| 250 | |
| 251 | // ════════════════════════════════════════════════════════════════ |
| 252 | // Custom toolkit (LOCAL_USER_MANAGEMENT_SET_USER_ROLE, etc.) |
| 253 | // ════════════════════════════════════════════════════════════════ |
| 254 | |
| 255 | try { |
| 256 | const result = await session.execute("SET_USER_ROLE", { user_id: "user-3", role: "developer" }); |
| 257 | if (result.data?.previous_role !== "viewer") throw new Error(`Previous role wrong: ${JSON.stringify(result.data)}`); |
| 258 | if (result.data?.new_role !== "developer") throw new Error(`New role wrong: ${JSON.stringify(result.data)}`); |
| 259 | ok("Toolkit: execute by original slug (SET_USER_ROLE)"); |
| 260 | } catch (err) { |
| 261 | fail("Toolkit: execute by original slug", err); |
| 262 | } |
| 263 | |
| 264 | try { |
| 265 | const result = await session.execute("LOCAL_USER_MANAGEMENT_SET_USER_ROLE", { user_id: "user-2", role: "admin" }); |
| 266 | if (result.data?.new_role !== "admin") throw new Error(`Unexpected: ${JSON.stringify(result.data)}`); |
| 267 | ok("Toolkit: execute by prefixed slug (LOCAL_USER_MANAGEMENT_SET_USER_ROLE)"); |
| 268 | } catch (err) { |
| 269 | fail("Toolkit: execute by prefixed slug", err); |
| 270 | } |
| 271 | |
| 272 | try { |
| 273 | const result = await session.execute("UPDATE_USER_STATUS", { user_id: "user-3", status: "active", reason: "Suspended by mistake" }); |
| 274 | if (result.data?.previous_status !== "suspended") throw new Error(`Previous status wrong: ${JSON.stringify(result.data)}`); |
| 275 | if (result.data?.new_status !== "active") throw new Error(`New status wrong: ${JSON.stringify(result.data)}`); |
| 276 | if (result.data?.reason !== "Suspended by mistake") throw new Error(`Reason wrong: ${JSON.stringify(result.data)}`); |
| 277 | ok("Toolkit: second tool + optional param (UPDATE_USER_STATUS with reason)"); |
| 278 | } catch (err) { |
| 279 | fail("Toolkit: second tool + optional param", err); |
| 280 | } |
| 281 | |
| 282 | try { |
| 283 | const result = await session.execute("LOCAL_USER_MANAGEMENT_UPDATE_USER_STATUS", { user_id: "user-1", status: "suspended" }); |
| 284 | if (result.data?.reason !== "No reason provided") throw new Error(`Default reason not applied: ${JSON.stringify(result.data)}`); |
| 285 | ok("Toolkit: prefixed slug + optional param default (reason → 'No reason provided')"); |
| 286 | } catch (err) { |
| 287 | fail("Toolkit: prefixed slug + optional param default", err); |
| 288 | } |
| 289 | |
| 290 | // Verify state mutations persisted across calls |
| 291 | try { |
| 292 | const result = await session.execute("GET_USER_DETAILS", { user_id: "user-3" }); |
| 293 | if (result.data?.role !== "developer") throw new Error(`Role not updated: ${JSON.stringify(result.data)}`); |
| 294 | if (result.data?.status !== "active") throw new Error(`Status not updated: ${JSON.stringify(result.data)}`); |
| 295 | ok("Toolkit: state mutations persisted (user-3 is now developer+active)"); |
| 296 | } catch (err) { |
| 297 | fail("Toolkit: state mutations persisted", err); |
| 298 | } |
| 299 | |
| 300 | // ════════════════════════════════════════════════════════════════ |
| 301 | // Cross-cutting: case insensitive, error handling, search, remote |
| 302 | // ════════════════════════════════════════════════════════════════ |
| 303 | |
| 304 | try { |
| 305 | const result = await session.execute("get_user_details", { user_id: "user-1" }); |
| 306 | if (!result.data?.name) throw new Error(`Unexpected: ${JSON.stringify(result.data)}`); |
| 307 | ok("Case-insensitive slug lookup (get_user_details → GET_USER_DETAILS)"); |
| 308 | } catch (err) { |
| 309 | fail("Case-insensitive slug", err); |
| 310 | } |
| 311 | |
| 312 | try { |
| 313 | const result = await session.execute("GET_USER_DETAILS", { user_id: "nonexistent" }); |
| 314 | if (!result.error?.includes("not found")) throw new Error(`Expected 'not found' error, got: ${result.error}`); |
| 315 | ok("Error handling: thrown error wrapped in response"); |
| 316 | } catch (err) { |
| 317 | fail("Error handling", err); |
| 318 | } |
| 319 | |
| 320 | // Search — standalone tools |
| 321 | try { |
| 322 | const searchResult = await session.search({ query: "get user profile details" }); |
| 323 | const toolSlugs = searchResult.results.flatMap((r) => r.primaryToolSlugs); |
| 324 | const found = toolSlugs.find((s) => s === "LOCAL_GET_USER_DETAILS"); |
| 325 | if (!found) console.log(" (search returned:", toolSlugs.join(", "), ")"); |
| 326 | ok(`Search: standalone tool found=${!!found} (${searchResult.results.length} results)`); |
| 327 | } catch (err) { |
| 328 | fail("Search: standalone tools", err); |
| 329 | } |
| 330 | |
| 331 | // Search — toolkit tools |
| 332 | try { |
| 333 | const searchResult = await session.search({ query: "change user role to admin" }); |
| 334 | const toolSlugs = searchResult.results.flatMap((r) => r.primaryToolSlugs); |
| 335 | const found = toolSlugs.find((s) => s.includes("USER_MANAGEMENT")); |
| 336 | if (!found) console.log(" (search returned:", toolSlugs.join(", "), ")"); |
| 337 | ok(`Search: toolkit tool found=${!!found} (${searchResult.results.length} results)`); |
| 338 | } catch (err) { |
| 339 | fail("Search: toolkit tools", err); |
| 340 | } |
| 341 | |
| 342 | // Search — extension tool |
| 343 | try { |
| 344 | const searchResult = await session.search({ query: "format email draft for gmail" }); |
| 345 | const toolSlugs = searchResult.results.flatMap((r) => r.primaryToolSlugs); |
| 346 | const found = toolSlugs.find((s) => s.includes("GMAIL") && s.startsWith("LOCAL_")); |
| 347 | if (!found) console.log(" (search returned:", toolSlugs.join(", "), ")"); |
| 348 | ok(`Search: extension tool found=${!!found} (${searchResult.results.length} results)`); |
| 349 | } catch (err) { |
| 350 | fail("Search: extension tool", err); |
| 351 | } |
| 352 | |
| 353 | // Remote tool still works alongside custom tools |
| 354 | try { |
| 355 | const result = await session.execute("WEATHERMAP_WEATHER", { location: "Tokyo" }); |
| 356 | if (result.error) throw new Error(`Remote error: ${result.error}`); |
| 357 | ok(`Remote tool execution (WEATHERMAP_WEATHER) — keys: ${Object.keys(result.data ?? {}).join(", ")}`); |
| 358 | } catch (err) { |
| 359 | fail("Remote tool execution", err); |
| 360 | } |
| 361 | |
| 362 | // ── Summary ── |
| 363 | console.log(`\n${passed} passed, ${failed} failed out of ${passed + failed} tests`); |
| 364 | if (failed > 0) process.exit(1); |
| 365 | } |
| 366 | |
| 367 | main().catch((err) => { |
| 368 | console.error("Fatal:", err); |
| 369 | process.exit(1); |
| 370 | }); |