Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

next
ts 70 lines 2.14 KB
Raw
1 import { Composio } from '@composio/core';
2 import { OpenAI } from 'openai';
3 import path from 'path';
4
5 const openai = new OpenAI({
6 apiKey: process.env.OPENAI_API_KEY,
7 });
8
9 /**
10 * Initialize Composio
11 * OpenAI Provider is automatically installed and initialized
12 */
13 const composio = new Composio({
14 apiKey: process.env.COMPOSIO_API_KEY,
15 // Local file paths under fileUploadDirs are uploaded automatically when the tool expects a file
16 dangerouslyAllowAutoUploadDownloadFiles: true,
17 fileUploadDirs: [import.meta.dirname],
18 });
19
20 const userId = process.env.COMPOSIO_EXAMPLES_USER_ID; // a user with a Google Drive connection
21 if (!userId) {
22 throw new Error('Set COMPOSIO_EXAMPLES_USER_ID');
23 }
24
25 /**
26 * Get the tools
27 * This tool is automatically typed and wrapped with the OpenAI Provider
28 */
29 console.log('🔄 Getting tools...');
30 const tools = await composio.tools.get(userId, 'GOOGLEDRIVE_UPLOAD_FILE');
31 console.log('✅ Tools fetched successfully...');
32 console.log(JSON.stringify(tools, null, 2));
33 /**
34 * Define a task for the assistant based on the tools in hand
35 */
36 const fileToUpload = path.join(import.meta.dirname, 'image.png');
37 const task = `Upload the file ${fileToUpload} to Google Drive`;
38
39 /**
40 * Define the messages for the assistant
41 */
42 const messages: OpenAI.ChatCompletionMessageParam[] = [
43 { role: 'system', content: 'You are a helpful assistant that can help with tasks.' },
44 { role: 'user', content: task },
45 ];
46
47 /**
48 * Create a chat completion
49 */
50 console.log('🔄 Creating chat completion...');
51 const response = await openai.chat.completions.create({
52 model: 'gpt-4o-mini',
53 messages,
54 tools: tools,
55 tool_choice: 'auto',
56 });
57 console.log('✅ Chat completion created successfully...');
58 /**
59 * If the assistant has tool calls, execute them and log the result
60 */
61 console.log('🔄 Calling tool...');
62 if (
63 response.choices[0].message.tool_calls &&
64 response.choices[0].message.tool_calls[0].type === 'function'
65 ) {
66 console.log(`✅ Calling tool ${response.choices[0].message.tool_calls[0].function.name}`);
67 const result = await composio.provider.handleToolCalls(userId, response);
68 console.log(result);
69 }
70 console.log('✅ Tool called successfully...');