@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 66 lines 1.79 KB
Raw
1 import { createOpenAI } from '@ai-sdk/openai';
2 import { Composio } from '@composio/core';
3 import { MastraProvider } from '@composio/mastra';
4 import { Agent } from '@mastra/core/agent';
5 import { stepCountIs } from 'ai';
6
7 export interface ToolRouterHackerNewsAgentEnvironment {
8 COMPOSIO_API_KEY?: string;
9 OPENAI_API_KEY?: string;
10 }
11
12 function requireKey(
13 env: ToolRouterHackerNewsAgentEnvironment,
14 name: keyof ToolRouterHackerNewsAgentEnvironment
15 ): string {
16 const value = env[name];
17 if (!value) {
18 throw new Error(`${name} is required to run the HackerNews agent.`);
19 }
20
21 return value;
22 }
23
24 export async function runToolRouterHackerNewsAgent(
25 env: ToolRouterHackerNewsAgentEnvironment
26 ): Promise<string> {
27 const composioApiKey = requireKey(env, 'COMPOSIO_API_KEY');
28 const openaiApiKey = requireKey(env, 'OPENAI_API_KEY');
29 const composio = new Composio({
30 apiKey: composioApiKey,
31 provider: new MastraProvider(),
32 });
33 const session = await composio.sessions.create('example-user', {
34 toolkits: ['hackernews'],
35 });
36 const tools = await session.tools();
37 const openai = createOpenAI({
38 apiKey: openaiApiKey,
39 });
40 const agent = new Agent({
41 id: 'hackernews-router-agent',
42 name: 'HackerNews Router Agent',
43 instructions:
44 'You are a helpful assistant. Use the available Composio tools to search for and read HackerNews data before answering.',
45 model: openai('gpt-5-mini'),
46 tools,
47 });
48
49 const { text } = await agent.generate(
50 [
51 {
52 role: 'user',
53 content: 'What are the current top 3 HackerNews stories? Give me their titles.',
54 },
55 ],
56 {
57 stopWhen: stepCountIs(10),
58 }
59 );
60
61 if (!text || text.trim().length === 0) {
62 throw new Error('Agent returned empty output — expected a non-empty response.');
63 }
64
65 return text;
66 }