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 | import OpenAI from 'openai'; |
| 2 | import { Composio } from '@composio/core'; |
| 3 | import { OpenAIResponsesProvider } from '@composio/openai'; |
| 4 | |
| 5 | const openai = new OpenAI(); |
| 6 | const composio = new Composio({ |
| 7 | provider: new OpenAIResponsesProvider(), |
| 8 | }); |
| 9 | |
| 10 | console.log(`🔄 Fetching tools from composio...`); |
| 11 | const tools = await composio.tools.get('default', 'HACKERNEWS_GET_USER'); |
| 12 | console.log(`✅ Tools fetched from composio`); |
| 13 | |
| 14 | console.log(`🔄 Generating response from OpenAI...`); |
| 15 | const initialResponse = await openai.responses.create({ |
| 16 | model: 'gpt-4.1', |
| 17 | input: 'Tell me about the user `pg` in hackernews', |
| 18 | tools, |
| 19 | }); |
| 20 | console.log(`✅ Response generated from OpenAI`); |
| 21 | console.log(JSON.stringify(initialResponse.output, null, 2)); |
| 22 | |
| 23 | console.log(`🔄 Handling tool calls from response...`); |
| 24 | const modelInputs = await composio.provider.handleResponse( |
| 25 | 'default', |
| 26 | initialResponse, |
| 27 | {}, |
| 28 | { |
| 29 | beforeExecute: async ({ toolSlug, toolkitSlug, params }) => { |
| 30 | console.log(`🔄 Executing tool ${toolSlug} from toolkit ${toolkitSlug}...`); |
| 31 | return params; |
| 32 | }, |
| 33 | afterExecute: async ({ toolSlug, toolkitSlug, result }) => { |
| 34 | console.log(`✅ Tool ${toolSlug} executed`); |
| 35 | return result; |
| 36 | }, |
| 37 | } |
| 38 | ); |
| 39 | |
| 40 | console.log(`🔄 Submitting tool outputs to OpenAI...`); |
| 41 | console.log(JSON.stringify(modelInputs, null, 2)); |
| 42 | const finalResponse = await openai.responses.create({ |
| 43 | model: 'gpt-4.1', |
| 44 | previous_response_id: initialResponse.id, |
| 45 | input: modelInputs, |
| 46 | tools, |
| 47 | }); |
| 48 | |
| 49 | console.log(`✅ Tool outputs submitted to OpenAI`); |
| 50 | const finalContent = finalResponse.output[0]; |
| 51 | if (finalContent.type === 'message' && finalContent.content[0].type === 'output_text') { |
| 52 | console.log(`🤖 OpenAI response`, finalContent.content[0].text); |
| 53 | } else { |
| 54 | console.error(`❌ Unexpected response from OpenAI`); |
| 55 | console.log(JSON.stringify(finalResponse.output, null, 2)); |
| 56 | } |