@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.86 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 interface DirectHackerNewsAgentEnvironment {
8 COMPOSIO_API_KEY?: string;
9 OPENAI_API_KEY?: string;
10 }
11
12 function requireKey(
13 env: DirectHackerNewsAgentEnvironment,
14 name: keyof DirectHackerNewsAgentEnvironment
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 runDirectHackerNewsAgent(
25 env: DirectHackerNewsAgentEnvironment
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 tools = await composio.tools.get('default', 'HACKERNEWS_GET_USER_BY_USERNAME', {
34 beforeExecute: ({ toolSlug, params }) => {
35 console.log(`🔧 executing ${toolSlug} with ${JSON.stringify(params.arguments)}`);
36 return params;
37 },
38 afterExecute: ({ toolSlug, result }) => {
39 console.log(`✅ ${toolSlug} finished`);
40 return result;
41 },
42 });
43 const openai = createOpenAI({
44 apiKey: openaiApiKey,
45 });
46 const agent = new Agent({
47 id: 'hackernews-agent',
48 name: 'HackerNews Agent',
49 instructions: 'You are a helpful assistant that looks up HackerNews users.',
50 model: openai('gpt-5-mini'),
51 tools,
52 });
53
54 const { text } = await agent.generate(
55 [{ role: 'user', content: 'Tell me about the HackerNews user `pg`.' }],
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 }