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 { Hono } from 'hono'; |
| 3 | import OpenAI from 'openai'; |
| 4 | |
| 5 | const app = new Hono(); |
| 6 | |
| 7 | const composio = new Composio({ |
| 8 | apiKey: process.env.COMPOSIO_API_KEY, |
| 9 | }); |
| 10 | |
| 11 | /** |
| 12 | * Fetch the tools from the Composio API |
| 13 | */ |
| 14 | app.get('/tools', async (c) => { |
| 15 | const tools = await composio.tools.get('default', { |
| 16 | toolkits: ['hackernews'], |
| 17 | }); |
| 18 | return c.json(tools); |
| 19 | }); |
| 20 | |
| 21 | /** |
| 22 | * Executing a tool request to fetch user details from the hackernews toolkit |
| 23 | * This is a simple example of how to execute a tool request |
| 24 | * |
| 25 | * @example |
| 26 | * ```curl |
| 27 | * curl -X POST 'http://localhost:8787/user' \ |
| 28 | * --header 'Content-Type: application/json' \ |
| 29 | * --data-raw '{ |
| 30 | * "user": "pg" |
| 31 | * }' |
| 32 | * ``` |
| 33 | */ |
| 34 | app.post('/user', async (c) => { |
| 35 | const composio = new Composio({ |
| 36 | apiKey: process.env.COMPOSIO_API_KEY, |
| 37 | }); |
| 38 | |
| 39 | const { user } = await c.req.json(); |
| 40 | const userDetails = await composio.tools.execute('HACKERNEWS_GET_USER', { |
| 41 | arguments: { |
| 42 | username: user, |
| 43 | }, |
| 44 | userId: 'default', |
| 45 | }); |
| 46 | return c.json(userDetails.data); |
| 47 | }); |
| 48 | |
| 49 | /** |
| 50 | * File upload example using the composio api using external link. |
| 51 | * |
| 52 | * Connect your google drive to the composio account before running this example. |
| 53 | * |
| 54 | * @example |
| 55 | * ```curl |
| 56 | * curl -X POST 'http://localhost:8787/upload-url' \ |
| 57 | * --header 'Content-Type: application/json' \ |
| 58 | * --data-raw '{ |
| 59 | * "fileUrl": "https://github.com/composiohq.png" |
| 60 | * }' |
| 61 | * ``` |
| 62 | */ |
| 63 | app.post('/upload-url', async (c) => { |
| 64 | const { fileUrl } = await c.req.json(); |
| 65 | const fileDetails = await composio.tools.execute('GOOGLEDRIVE_UPLOAD_FILE', { |
| 66 | arguments: { |
| 67 | file_to_upload: fileUrl, |
| 68 | }, |
| 69 | userId: 'default', |
| 70 | }); |
| 71 | return c.json(fileDetails); |
| 72 | }); |
| 73 | |
| 74 | /** |
| 75 | * File upload example using the composio api using file upload. |
| 76 | * |
| 77 | * @example |
| 78 | * ```curl |
| 79 | * curl -X POST 'http://localhost:8787/upload-file' \ |
| 80 | * --header 'Content-Type: multipart/form-data' \ |
| 81 | * --form 'file=./path/to/file' |
| 82 | * ``` |
| 83 | */ |
| 84 | app.post('/upload-file', async (c) => { |
| 85 | // get the blob from the request |
| 86 | const body = await c.req.parseBody(); |
| 87 | const file = body.file; |
| 88 | const fileDetails = await composio.tools.execute('GOOGLEDRIVE_UPLOAD_FILE', { |
| 89 | arguments: { |
| 90 | file_to_upload: file, |
| 91 | }, |
| 92 | userId: 'default', |
| 93 | }); |
| 94 | return c.json(fileDetails); |
| 95 | }); |
| 96 | |
| 97 | /** |
| 98 | * File download example using the composio api using file download. |
| 99 | * Pass your google drive file id to the composio api to download the file. |
| 100 | * |
| 101 | * @example |
| 102 | * ```curl |
| 103 | * curl -X POST 'http://localhost:8787/download-file' \ |
| 104 | * --header 'Content-Type: application/json' \ |
| 105 | * --data-raw '{ |
| 106 | * "fileId": "1234567890" |
| 107 | * }' |
| 108 | * ``` |
| 109 | */ |
| 110 | app.post('/download-file', async (c) => { |
| 111 | const { fileId } = await c.req.json(); |
| 112 | const fileDetails = await composio.tools.execute('GOOGLEDRIVE_DOWNLOAD_FILE', { |
| 113 | arguments: { |
| 114 | file_id: fileId, |
| 115 | }, |
| 116 | userId: 'default', |
| 117 | }); |
| 118 | return c.json(fileDetails); |
| 119 | }); |
| 120 | |
| 121 | app.post('/hackernews-chat', async (c) => { |
| 122 | const { message } = await c.req.json(); |
| 123 | |
| 124 | const openai = new OpenAI(); |
| 125 | |
| 126 | const tools = await composio.tools.get('default', { |
| 127 | toolkits: ['hackernews'], |
| 128 | }); |
| 129 | |
| 130 | const messages: OpenAI.ChatCompletionMessageParam[] = [ |
| 131 | { role: 'system', content: 'You are a helpful assistant that can use tools to answer questions.' }, |
| 132 | { role: 'user', content: message }, |
| 133 | ]; |
| 134 | |
| 135 | const response = await openai.chat.completions.create({ |
| 136 | model: 'gpt-4o-mini', |
| 137 | messages: [{ role: 'user', content: message }], |
| 138 | tools, |
| 139 | }); |
| 140 | |
| 141 | // push the assistant message to the messages array |
| 142 | messages.push(response.choices[0].message); |
| 143 | |
| 144 | // handle the tool calls |
| 145 | const toolResponses = await composio.provider.handleToolCalls('default', response); |
| 146 | |
| 147 | // push the tool responses to the messages array |
| 148 | messages.push(...toolResponses); |
| 149 | |
| 150 | if (toolResponses.length > 0) { |
| 151 | // create the final response |
| 152 | const finalResponse = await openai.chat.completions.create({ |
| 153 | model: 'gpt-4o-mini', |
| 154 | messages, |
| 155 | }); |
| 156 | |
| 157 | return c.json(finalResponse.choices); |
| 158 | } else { |
| 159 | return c.json(response.choices); |
| 160 | } |
| 161 | }); |
| 162 | |
| 163 | export default app; |