/ examples / js / tools / subagent-tool.ts
subagent-tool.ts
  1  /**
  2   * SubagentTool Integration Test
  3   * 
  4   * Tests the agent-as-tool pattern with real agents.
  5   * 
  6   * Run: npx ts-node subagent-tool.ts
  7   * Requires: OPENAI_API_KEY
  8   */
  9  
 10  import {
 11      Agent,
 12      SubagentTool,
 13      createSubagentTool,
 14      createSubagentTools,
 15      createDelegator
 16  } from '../../../src/praisonai-ts/dist';
 17  
 18  async function main() {
 19      console.log('=== SubagentTool Integration Test ===\n');
 20  
 21      // Test 1: Create SubagentTool
 22      console.log('1. Testing SubagentTool creation:');
 23      const mockAgent = {
 24          name: 'MockAgent',
 25          chat: async (input: string) => `Echo: ${input}`
 26      };
 27  
 28      const tool = new SubagentTool(mockAgent, {
 29          name: 'echo',
 30          description: 'Echo the input'
 31      });
 32  
 33      console.log('   Tool name:', tool.name);
 34      console.log('   Tool ID:', tool.id);
 35      console.log('   Schema:', JSON.stringify(tool.getSchema()));
 36      console.log('   Success: ✅');
 37  
 38      // Test 2: Execute tool with mock agent
 39      console.log('\n2. Testing tool execution:');
 40      const mockResult = await tool.execute({ input: 'Hello World' });
 41      console.log('   Input: "Hello World"');
 42      console.log('   Output:', mockResult);
 43      console.log('   Success: ✅');
 44  
 45      // Test 3: Factory function
 46      console.log('\n3. Testing createSubagentTool factory:');
 47      const factoryTool = createSubagentTool(mockAgent, { name: 'factory_echo' });
 48      console.log('   Tool name:', factoryTool.name);
 49      console.log('   Success: ✅');
 50  
 51      // Test 4: Multiple subagent tools
 52      console.log('\n4. Testing createSubagentTools (batch):');
 53      const agents = [
 54          { name: 'Agent1', chat: async (i: string) => `A1: ${i}` },
 55          { name: 'Agent2', chat: async (i: string) => `A2: ${i}` }
 56      ];
 57      const tools = createSubagentTools(agents);
 58      console.log('   Created tools:', tools.length);
 59      tools.forEach(t => console.log('    -', t.name));
 60      console.log('   Success: ✅');
 61  
 62      // Test 5: AI SDK format conversion
 63      console.log('\n5. Testing AI SDK format conversion:');
 64      const aiTool = tool.toAISDKTool();
 65      console.log('   Type:', aiTool.type);
 66      console.log('   Function name:', aiTool.function.name);
 67      console.log('   Has execute:', typeof aiTool.execute === 'function');
 68      console.log('   Success: ✅');
 69  
 70      // Test 6: Live test with real agent
 71      if (process.env.OPENAI_API_KEY) {
 72          console.log('\n6. Testing with real Agent (live API):');
 73          try {
 74              const researcher = new Agent({
 75                  name: 'QuickResearcher',
 76                  instructions: 'Answer in exactly 5 words.',
 77                  llm: 'openai/gpt-4o-mini'
 78              });
 79  
 80              const researchTool = createSubagentTool(researcher, {
 81                  name: 'quick_research',
 82                  description: 'Get a quick 5-word answer'
 83              });
 84  
 85              const result = await researchTool.execute({ input: 'What is AI?' });
 86              console.log('   Question: "What is AI?"');
 87              console.log('   Answer:', result);
 88              console.log('   Success: ✅');
 89          } catch (error: any) {
 90              console.log('   Error:', error.message);
 91          }
 92      } else {
 93          console.log('\n6. Live test: Skipped (OPENAI_API_KEY not set)');
 94      }
 95  
 96      // Test 7: Input/Output transforms
 97      console.log('\n7. Testing transforms:');
 98      const transformTool = createSubagentTool(mockAgent, {
 99          name: 'transform_echo',
100          inputTransform: (input) => input.toUpperCase(),
101          outputTransform: (output) => `[TRANSFORMED] ${output}`
102      });
103  
104      const transformResult = await transformTool.execute({ input: 'hello' });
105      console.log('   Input: "hello"');
106      console.log('   Output:', transformResult);
107      console.log('   Success: ✅');
108  
109      console.log('\n=== SubagentTool Tests Complete ===');
110  }
111  
112  main().catch(console.error);