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 | * Tool Router preload with OpenAI Agents |
| 3 | * |
| 4 | * Shows direct tool exposure for: |
| 5 | * 1. Composio tools via `preload.tools` |
| 6 | * 2. SDK custom tools via `preload: true` on the custom tool or toolkit |
| 7 | * |
| 8 | * Usage: |
| 9 | * COMPOSIO_API_KEY=... OPENAI_API_KEY=... bun src/preload.ts |
| 10 | */ |
| 11 | import 'dotenv/config'; |
| 12 | import assert from 'node:assert/strict'; |
| 13 | import { Composio, experimental_createTool, experimental_createToolkit } from '@composio/core'; |
| 14 | import { OpenAIAgentsProvider } from '@composio/openai-agents'; |
| 15 | import { Agent, run } from '@openai/agents'; |
| 16 | import { z } from 'zod/v3'; |
| 17 | |
| 18 | function requireEnv(name: 'COMPOSIO_API_KEY' | 'OPENAI_API_KEY'): string { |
| 19 | const value = process.env[name]; |
| 20 | if (!value) { |
| 21 | throw new Error(`Set ${name} before running this example.`); |
| 22 | } |
| 23 | return value; |
| 24 | } |
| 25 | |
| 26 | const composioApiKey = requireEnv('COMPOSIO_API_KEY'); |
| 27 | requireEnv('OPENAI_API_KEY'); |
| 28 | |
| 29 | const internalUsers: Record<string, Record<string, unknown>> = { |
| 30 | 'user-1': { |
| 31 | id: 'user-1', |
| 32 | name: 'Ada Lovelace', |
| 33 | email: 'ada@example.com', |
| 34 | team: 'platform', |
| 35 | plan: 'enterprise', |
| 36 | }, |
| 37 | 'user-2': { |
| 38 | id: 'user-2', |
| 39 | name: 'Grace Hopper', |
| 40 | email: 'grace@example.com', |
| 41 | team: 'developer-tools', |
| 42 | plan: 'startup', |
| 43 | }, |
| 44 | }; |
| 45 | |
| 46 | const lookupInternalUser = experimental_createTool('LOOKUP_INTERNAL_USER', { |
| 47 | name: 'Lookup internal user', |
| 48 | description: 'Look up an internal demo user profile by user ID.', |
| 49 | preload: true, |
| 50 | inputParams: z.object({ |
| 51 | user_id: z.string().describe('Internal user ID, for example user-1'), |
| 52 | }), |
| 53 | execute: async ({ user_id }) => { |
| 54 | const user = internalUsers[user_id]; |
| 55 | if (!user) { |
| 56 | throw new Error(`User "${user_id}" not found`); |
| 57 | } |
| 58 | return user; |
| 59 | }, |
| 60 | }); |
| 61 | |
| 62 | const searchInternalUsers = experimental_createTool('SEARCH_INTERNAL_USERS', { |
| 63 | name: 'Search internal users', |
| 64 | description: |
| 65 | 'Search demo internal users by team. This custom tool is search-only because preload is not enabled.', |
| 66 | inputParams: z.object({ |
| 67 | team: z.string().describe('Team name to search for'), |
| 68 | }), |
| 69 | execute: async ({ team }) => ({ |
| 70 | results: Object.values(internalUsers).filter(user => user.team === team), |
| 71 | }), |
| 72 | }); |
| 73 | |
| 74 | const getAccountHealth = experimental_createTool('GET_ACCOUNT_HEALTH', { |
| 75 | name: 'Get account health', |
| 76 | description: 'Return internal account health details for a user.', |
| 77 | inputParams: z.object({ |
| 78 | user_id: z.string().describe('Internal user ID'), |
| 79 | }), |
| 80 | execute: async ({ user_id }) => ({ |
| 81 | user_id, |
| 82 | health: 'green', |
| 83 | open_incidents: 0, |
| 84 | renewal_risk: 'low', |
| 85 | }), |
| 86 | }); |
| 87 | |
| 88 | const getAccountAuditLog = experimental_createTool('GET_ACCOUNT_AUDIT_LOG', { |
| 89 | name: 'Get account audit log', |
| 90 | description: |
| 91 | 'Return internal account audit events. This overrides the toolkit preload default and remains search-only.', |
| 92 | preload: false, |
| 93 | inputParams: z.object({ |
| 94 | user_id: z.string().describe('Internal user ID'), |
| 95 | }), |
| 96 | execute: async ({ user_id }) => ({ |
| 97 | user_id, |
| 98 | events: ['login', 'settings_viewed'], |
| 99 | }), |
| 100 | }); |
| 101 | |
| 102 | const internalAdmin = experimental_createToolkit('INTERNAL_ADMIN', { |
| 103 | name: 'Internal admin', |
| 104 | description: 'Demo internal administration tools.', |
| 105 | preload: true, |
| 106 | tools: [getAccountHealth, getAccountAuditLog], |
| 107 | }); |
| 108 | |
| 109 | const composio = new Composio({ |
| 110 | apiKey: composioApiKey, |
| 111 | baseURL: process.env.COMPOSIO_BASE_URL || undefined, |
| 112 | provider: new OpenAIAgentsProvider(), |
| 113 | }); |
| 114 | |
| 115 | const session = await composio.create('preload-example-user', { |
| 116 | toolkits: ['hackernews'], |
| 117 | tools: { |
| 118 | hackernews: { |
| 119 | enable: ['HACKERNEWS_GET_USER'], |
| 120 | }, |
| 121 | }, |
| 122 | preload: { |
| 123 | tools: ['HACKERNEWS_GET_USER'], |
| 124 | }, |
| 125 | manageConnections: false, |
| 126 | experimental: { |
| 127 | localTools: [lookupInternalUser, searchInternalUsers], |
| 128 | localToolkits: [internalAdmin], |
| 129 | }, |
| 130 | }); |
| 131 | |
| 132 | const tools = await session.tools(); |
| 133 | const toolNames = tools.map(tool => tool.name); |
| 134 | assert(toolNames.includes('HACKERNEWS_GET_USER')); |
| 135 | assert(toolNames.includes('LOCAL_LOOKUP_INTERNAL_USER')); |
| 136 | assert(toolNames.includes('LOCAL_INTERNAL_ADMIN_GET_ACCOUNT_HEALTH')); |
| 137 | assert(!toolNames.includes('LOCAL_SEARCH_INTERNAL_USERS')); |
| 138 | assert(!toolNames.includes('LOCAL_INTERNAL_ADMIN_GET_ACCOUNT_AUDIT_LOG')); |
| 139 | |
| 140 | console.log('Direct tools exposed to the agent:'); |
| 141 | for (const tool of tools) { |
| 142 | console.log(`- ${tool.name}`); |
| 143 | } |
| 144 | |
| 145 | const agent = new Agent({ |
| 146 | name: 'Preload Demo Agent', |
| 147 | instructions: 'Use the provided tools to perform the task.', |
| 148 | model: process.env.OPENAI_MODEL ?? 'gpt-5.5', |
| 149 | tools, |
| 150 | }); |
| 151 | |
| 152 | const prompt = |
| 153 | process.argv.slice(2).join(' ') || |
| 154 | 'Look up Hacker News user "pg", then look up internal user user-1 and their account health. Summarize the useful facts.'; |
| 155 | |
| 156 | console.log(`\nUser: ${prompt}\n`); |
| 157 | const result = await run(agent, prompt); |
| 158 | console.log(`Assistant: ${result.finalOutput}`); |