@devries-cameron20 / composio Mirror · read-only
Fork 0

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

next
ts 99 lines 2.95 KB
Raw
1 /**
2 * Tool Router direct_tools session preset with OpenAI Agents
3 *
4 * `sessionPreset: SessionPreset.DIRECT_TOOLS` is a shortcut for sessions where the full
5 * allowed tool set is known upfront. It disables Tool Router meta/helper tools
6 * by default and loads all tools allowed by the filters directly into
7 * `session.tools()` and the MCP tool list.
8 *
9 * Usage:
10 * COMPOSIO_API_KEY=... OPENAI_API_KEY=... bun src/direct-tools-preset.ts
11 */
12 import 'dotenv/config';
13 import assert from 'node:assert/strict';
14 import {
15 Composio,
16 SessionPreset,
17 experimental_createTool,
18 experimental_createToolkit,
19 } from '@composio/core';
20 import { OpenAIAgentsProvider } from '@composio/openai-agents';
21 import { Agent, run } from '@openai/agents';
22 import { z } from 'zod/v3';
23
24 function requireEnv(name: 'COMPOSIO_API_KEY' | 'OPENAI_API_KEY'): string {
25 const value = process.env[name];
26 if (!value) {
27 throw new Error(`Set ${name} before running this example.`);
28 }
29 return value;
30 }
31
32 const composioApiKey = requireEnv('COMPOSIO_API_KEY');
33 requireEnv('OPENAI_API_KEY');
34
35 const composio = new Composio({
36 apiKey: composioApiKey,
37 baseURL: process.env.COMPOSIO_BASE_URL || undefined,
38 provider: new OpenAIAgentsProvider(),
39 });
40
41 const getUserNote = experimental_createTool('GET_USER_NOTE', {
42 name: 'Get Hacker News user note',
43 description: 'Return an internal research note for a Hacker News username.',
44 inputParams: z.object({
45 username: z.string().describe('Hacker News username, for example pg'),
46 }),
47 execute: async ({ username }) => ({
48 username,
49 note:
50 username.toLowerCase() === 'pg'
51 ? 'Paul Graham; YC co-founder and essayist.'
52 : `No curated internal note for ${username}.`,
53 }),
54 });
55
56 const hnResearch = experimental_createToolkit('HN_RESEARCH', {
57 name: 'Hacker News research',
58 description: 'Internal research notes for Hacker News users.',
59 tools: [getUserNote],
60 });
61
62 const session = await composio.create('direct-tools-example-user', {
63 sessionPreset: SessionPreset.DIRECT_TOOLS,
64 toolkits: ['hackernews'],
65 tools: {
66 hackernews: {
67 enable: ['HACKERNEWS_GET_USER'],
68 },
69 },
70 experimental: {
71 localToolkits: [hnResearch],
72 },
73 });
74
75 const tools = await session.tools();
76 const toolNames = tools.map(tool => tool.name);
77 assert(toolNames.includes('HACKERNEWS_GET_USER'));
78 assert(toolNames.includes('LOCAL_HN_RESEARCH_GET_USER_NOTE'));
79 assert(!toolNames.includes('COMPOSIO_SEARCH_TOOLS'));
80
81 console.log('Direct tools exposed to the agent:');
82 for (const tool of tools) {
83 console.log(`- ${tool.name}`);
84 }
85
86 const agent = new Agent({
87 name: 'Direct Tools Demo Agent',
88 instructions: 'Use the provided tools to perform the task.',
89 model: process.env.OPENAI_MODEL ?? 'gpt-5.5',
90 tools,
91 });
92
93 const prompt =
94 process.argv.slice(2).join(' ') ||
95 'Look up user "pg" on Hacker News and include any internal research note.';
96
97 console.log(`\nUser: ${prompt}\n`);
98 const result = await run(agent, prompt);
99 console.log(`Assistant: ${result.finalOutput}`);