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 | import { Composio } from '@composio/core'; |
| 2 | import { OpenAIProvider } from '@composio/openai'; |
| 3 | import OpenAI from 'openai'; |
| 4 | |
| 5 | const MODEL = 'gpt-5-mini'; |
| 6 | const USER_ID = 'example-user'; |
| 7 | const MAX_STEPS = 10; |
| 8 | |
| 9 | interface DirectHackerNewsAgentEnvironment { |
| 10 | COMPOSIO_API_KEY?: string; |
| 11 | OPENAI_API_KEY?: string; |
| 12 | } |
| 13 | |
| 14 | function requireKey( |
| 15 | env: DirectHackerNewsAgentEnvironment, |
| 16 | name: keyof DirectHackerNewsAgentEnvironment |
| 17 | ): string { |
| 18 | const value = env[name]; |
| 19 | if (!value) { |
| 20 | throw new Error(`${name} is required to run the HackerNews agent.`); |
| 21 | } |
| 22 | |
| 23 | return value; |
| 24 | } |
| 25 | |
| 26 | export async function runDirectHackerNewsAgent( |
| 27 | env: DirectHackerNewsAgentEnvironment |
| 28 | ): Promise<string> { |
| 29 | const composioApiKey = requireKey(env, 'COMPOSIO_API_KEY'); |
| 30 | const openaiApiKey = requireKey(env, 'OPENAI_API_KEY'); |
| 31 | const openai = new OpenAI({ |
| 32 | apiKey: openaiApiKey, |
| 33 | maxRetries: 1, |
| 34 | timeout: 60_000, |
| 35 | }); |
| 36 | const composio = new Composio({ |
| 37 | apiKey: composioApiKey, |
| 38 | provider: new OpenAIProvider(), |
| 39 | }); |
| 40 | const tools = await composio.tools.get(USER_ID, 'HACKERNEWS_GET_USER_BY_USERNAME'); |
| 41 | const messages: OpenAI.ChatCompletionMessageParam[] = [ |
| 42 | { |
| 43 | role: 'system', |
| 44 | content: 'You are a helpful assistant that looks up HackerNews users.', |
| 45 | }, |
| 46 | { role: 'user', content: 'Tell me about the HackerNews user `pg`.' }, |
| 47 | ]; |
| 48 | let hasSuccessfulToolExecution = false; |
| 49 | |
| 50 | for (let step = 0; step < MAX_STEPS; step += 1) { |
| 51 | let toolChoice: 'auto' | 'required' = 'auto'; |
| 52 | if (!hasSuccessfulToolExecution) { |
| 53 | toolChoice = 'required'; |
| 54 | } |
| 55 | |
| 56 | const response = await openai.chat.completions.create({ |
| 57 | model: MODEL, |
| 58 | messages, |
| 59 | tools, |
| 60 | tool_choice: toolChoice, |
| 61 | }); |
| 62 | const message = response.choices[0]?.message; |
| 63 | if (!message) { |
| 64 | throw new Error('OpenAI returned no assistant message.'); |
| 65 | } |
| 66 | |
| 67 | messages.push(message); |
| 68 | if (!message.tool_calls?.length) { |
| 69 | if (!hasSuccessfulToolExecution) { |
| 70 | throw new Error('Agent returned without successfully executing a Composio tool.'); |
| 71 | } |
| 72 | |
| 73 | const text = message.content?.trim(); |
| 74 | if (!text) { |
| 75 | throw new Error('Agent returned empty output — expected a non-empty response.'); |
| 76 | } |
| 77 | return text; |
| 78 | } |
| 79 | |
| 80 | const toolMessages = await composio.provider.handleToolCalls(USER_ID, response, undefined, { |
| 81 | beforeExecute: ({ toolSlug, params }) => { |
| 82 | console.log(`🔧 executing ${toolSlug} with ${JSON.stringify(params.arguments)}`); |
| 83 | return params; |
| 84 | }, |
| 85 | afterExecute: ({ toolSlug, result }) => { |
| 86 | if (result.successful) { |
| 87 | hasSuccessfulToolExecution = true; |
| 88 | } |
| 89 | console.log(`✅ ${toolSlug} finished`); |
| 90 | return result; |
| 91 | }, |
| 92 | }); |
| 93 | messages.push(...toolMessages); |
| 94 | } |
| 95 | |
| 96 | throw new Error(`Agent exceeded the ${MAX_STEPS}-step tool-call budget.`); |
| 97 | } |