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 48 lines 1.44 KB
Raw
1 import { openai } from '@ai-sdk/openai';
2 import { Composio } from '@composio/core';
3 import { VercelProvider } from '@composio/vercel';
4 import { stepCountIs, streamText } from 'ai';
5
6 const composio = new Composio({
7 provider: new VercelProvider(),
8 });
9
10 const userId = process.env.COMPOSIO_EXAMPLES_USER_ID; // the user id from your database
11 if (!userId) {
12 throw new Error('Set COMPOSIO_EXAMPLES_USER_ID');
13 }
14
15 // Enable multi-account mode so the user can connect multiple accounts
16 // per toolkit (e.g. two Gmail accounts).
17 const session = await composio.create(userId, {
18 toolkits: ['gmail'],
19 multiAccount: {
20 enable: true,
21 maxAccountsPerToolkit: 3,
22 requireExplicitSelection: true,
23 },
24 });
25
26 // Set an alias while authorizing a connected account for easier identification.
27 // Aliases are unique per entity and toolkit, so suffix to keep reruns working.
28 const connectionRequest = await session.authorize('gmail', {
29 alias: `work-gmail-${Date.now()}`,
30 });
31
32 console.log(`Visit this URL to authorize: ${connectionRequest.redirectUrl}`);
33
34 // requireExplicitSelection means the new alias is only usable once it is ACTIVE.
35 await connectionRequest.waitForConnection();
36
37 const tools = await session.tools();
38
39 const stream = await streamText({
40 model: openai('gpt-4o-mini'),
41 prompt: 'Find my last email from my work Gmail account',
42 stopWhen: stepCountIs(10),
43 tools,
44 });
45
46 for await (const textPart of stream.textStream) {
47 process.stdout.write(textPart);
48 }