Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 1 | /** |
| 2 | * Session Update Example |
| 3 | * |
| 4 | * Demonstrates session.update(): what omitted fields, replaced maps, null, |
| 5 | * an empty toolkit allowlist, and expectedConfigVersion do to a stored |
| 6 | * session, without creating a new one. |
| 7 | * |
| 8 | * Usage: |
| 9 | * COMPOSIO_API_KEY=... bun src/session-update.ts |
| 10 | */ |
| 11 | import { Composio, ComposioSessionConfigConflictError } from "@composio/core"; |
| 12 | |
| 13 | const composio = new Composio({ |
| 14 | apiKey: process.env.COMPOSIO_API_KEY, |
| 15 | baseURL: process.env.COMPOSIO_BASE_URL || undefined, |
| 16 | }); |
| 17 | |
| 18 | // Create a session with a per-toolkit tools map and a stored callback URL |
| 19 | let session = await composio.create("session-update-demo", { |
| 20 | toolkits: ["gmail", "slack"], |
| 21 | tools: { |
| 22 | gmail: { enable: ["GMAIL_FETCH_EMAILS"] }, |
| 23 | slack: { enable: ["SLACK_SEND_MESSAGE"] }, |
| 24 | }, |
| 25 | manageConnections: { enable: true, callbackUrl: "https://example.com/callback" }, |
| 26 | }); |
| 27 | |
| 28 | console.log(`Session created: ${session.sessionId}`); |
| 29 | console.log(`Config version: ${session.configVersion}`); |
| 30 | console.log(`Tools: ${JSON.stringify(session.config.tools)}`); |
| 31 | |
| 32 | // 1. Maps replace, they don't merge: a slack-only tools map removes the gmail entry |
| 33 | await session.update({ |
| 34 | tools: { slack: { enable: ["SLACK_SEND_MESSAGE", "SLACK_LIST_CHANNELS"] } }, |
| 35 | }); |
| 36 | |
| 37 | console.log(`\nAfter the slack-only tools map (gmail entry is gone):`); |
| 38 | console.log(`Tools: ${JSON.stringify(session.config.tools)}`); |
| 39 | |
| 40 | // 2. Remove only the stored callback URL; `enable` and the other settings stay as stored |
| 41 | await session.update({ |
| 42 | manageConnections: { callbackUrl: null }, |
| 43 | }); |
| 44 | |
| 45 | console.log(`\nAfter clearing the callback URL:`); |
| 46 | console.log(`Manage connections: ${JSON.stringify(session.config.manage_connections)}`); |
| 47 | |
| 48 | // 3. Narrow the policy and fix preload in the same update. A preloaded tool that |
| 49 | // falls outside the new policy would be rejected with a 400. |
| 50 | await session.update({ |
| 51 | toolkits: { enable: ["slack"] }, |
| 52 | preload: { tools: ["SLACK_SEND_MESSAGE"] }, |
| 53 | }); |
| 54 | |
| 55 | console.log(`\nAfter narrowing to slack:`); |
| 56 | console.log(`Config version: ${session.configVersion}`); |
| 57 | console.log(`Preload: ${JSON.stringify(session.preload)}`); |
| 58 | |
| 59 | // 4. Concurrent updates. Every update sends the config version this handle last |
| 60 | // observed as its precondition; a stale value is a 409 and nothing is written. |
| 61 | // A second handle on the same session plays the concurrent writer. |
| 62 | const observedVersion = session.configVersion; |
| 63 | const otherClient = await composio.sessions.use(session.sessionId); |
| 64 | await otherClient.update({ workbench: { enable: false } }); |
| 65 | |
| 66 | const widen = { toolkits: { enable: ["gmail", "slack"] }, preload: { tools: [] } }; |
| 67 | try { |
| 68 | await session.update(widen); |
| 69 | } catch (error) { |
| 70 | if (!(error instanceof ComposioSessionConfigConflictError)) throw error; |
| 71 | console.log(`\nConflict at version ${observedVersion}: ${error.message}`); |
| 72 | // Recover: re-read the session, then retry against the fresh version |
| 73 | session = await composio.sessions.use(session.sessionId); |
| 74 | await session.update(widen); |
| 75 | console.log(`Retried at version ${session.configVersion}`); |
| 76 | } |
| 77 | |
| 78 | // 5. Deny every app toolkit. The empty allowlist is sent as-is; an omitted |
| 79 | // `toolkits` would have left the policy unchanged instead. |
| 80 | await session.update({ |
| 81 | toolkits: { enable: [] }, |
| 82 | preload: { tools: [] }, |
| 83 | }); |
| 84 | |
| 85 | console.log(`\nAfter denying every app toolkit:`); |
| 86 | console.log(`Toolkits: ${JSON.stringify(session.config.toolkits)}`); |
| 87 | |
| 88 | // 6. Clear a whole block with null: manageConnections falls back to its default. |
| 89 | // expectedConfigVersion: false skips the precondition, so this write applies |
| 90 | // whatever version the session is at (last writer wins). |
| 91 | await session.update({ |
| 92 | manageConnections: null, |
| 93 | expectedConfigVersion: false, |
| 94 | }); |
| 95 | |
| 96 | console.log(`\nAfter clearing manageConnections:`); |
| 97 | console.log(`Config version: ${session.configVersion}`); |
| 98 | console.log(`Warnings: ${JSON.stringify(session.warnings)}`); |