๐Ÿ“ฆ Kong / volcano-sdk

๐Ÿ“„ context-integration.test.ts ยท 392 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392import { describe, it, expect, vi, beforeEach } from 'vitest';
import { agent, llmOpenAI, AgentBuilder, LLMHandle } from '../src/volcano-sdk.js';

// Create a mock LLM that records all prompts it receives
const createRecordingLLM = (): LLMHandle & { getPrompts: () => string[], getLastPrompt: () => string } => {
  const prompts: string[] = [];
  
  return {
      gen: vi.fn(async (prompt: string) => {
        prompts.push(prompt);
        
        // Return different responses based on the prompt content
        if (prompt.includes('Create a report')) {
          return 'USE researcher: Find information about testing';
        }
        if (prompt.includes('was delegated:')) {
          return 'Research complete: Found 5 test cases';
        }
        if (prompt.includes('You are analyzing the results')) {
          // This is an ask() query - check what's in the context
          if (prompt.includes('Which agents were used')) {
            if (prompt.includes('Agents Used:')) {
              return 'The researcher agent was used to find information about testing.';
            }
            return 'Based on the execution results, I cannot find specific agent usage information.';
          }
        }
        
        return 'Default response';
      }),
    
    genStream: vi.fn(async function* (prompt: string) {
      prompts.push(prompt);
      
      // Return appropriate response based on prompt
      let response = 'Default response';
      if (prompt.includes('Create a report')) {
        response = 'USE researcher: Find information about testing';
      } else if (prompt.includes('was delegated:')) {
        response = 'Research complete: Found 5 test cases';
      }
      
      for (const char of response) {
        yield char;
      }
    }),
    
    getPrompts: () => prompts,
    getLastPrompt: () => prompts[prompts.length - 1] || ''
  };
};

// Helper to create mock AgentResults with proper ask method
function createMockResults(mockResults: any[]): any {
  return Object.assign(mockResults, {
    ask: async (llm: any, question: string) => {
      const context: string[] = [];
      const agentDelegations: { step: number; agentName: string; task: string }[] = [];
      
      mockResults.forEach((step, idx) => {
        context.push(`Step ${idx + 1}:`);
        
        if (step.prompt) {
          context.push(`  Prompt: ${step.prompt}`);
          
          if (step.llmOutput) {
            const useMatch = step.llmOutput.match(/USE\s+(\w+):\s*(.+?)(?:\n|$)/);
            if (useMatch) {
              agentDelegations.push({ 
                step: idx + 1, 
                agentName: useMatch[1], 
                task: useMatch[2].trim() 
              });
              context.push(`  Delegated to Agent: ${useMatch[1]}`);
              context.push(`  Delegation Task: ${useMatch[2].trim()}`);
            }
            
            const delegationMatch = step.prompt.match(/Agent (\w+) was delegated: (.+)$/);
            if (delegationMatch) {
              context.push(`  Agent Used: ${delegationMatch[1]}`);
              context.push(`  Agent Task: ${delegationMatch[2]}`);
            }
          }
        }
        
        if (step.llmOutput) context.push(`  LLM Output: ${step.llmOutput}`);
        if (step.toolCalls && step.toolCalls.length > 0) {
          context.push(`  Tools Called (${step.toolCalls.length}):`);
          step.toolCalls.forEach(tc => {
            context.push(`    - ${tc.name}: ${JSON.stringify(tc.arguments || {})}`);
            context.push(`      Result: ${JSON.stringify(tc.result)}`);
          });
        }
        if (step.mcp) {
          context.push(`  MCP Tool: ${step.mcp.tool}`);
          context.push(`  Result: ${JSON.stringify(step.mcp.result)}`);
        }
        if (step.durationMs) context.push(`  Duration: ${step.durationMs}ms`);
        
        if ((step as any).__crewTotalTokens) {
          context.push(`  Total Crew Tokens: ${(step as any).__crewTotalTokens}`);
        }
        
        if ((step as any).agentCalls && (step as any).agentCalls.length > 0) {
          context.push(`  Agents Used:`);
          (step as any).agentCalls.forEach((call: any) => {
            context.push(`    - ${call.name}: ${call.task}`);
            context.push(`      Tokens: ${call.tokens}`);
            context.push(`      Duration: ${call.ms}ms`);
          });
        }
        
        context.push('');
      });
      
      if (agentDelegations.length > 0) {
        context.push('\nAgent Delegation Summary:');
        agentDelegations.forEach(d => {
          context.push(`  - Step ${d.step}: ${d.agentName} agent (task: "${d.task}")`);
        });
        context.push('');
      }
      
      const fullContext = context.join('\n');
      const prompt = `You are analyzing the results of an AI agent workflow.\n\nAgent Execution Results:\n${fullContext}\n\nUser Question: ${question}\n\nProvide a clear, concise answer based on the execution results above. Be specific and reference actual data from the results.`;
      return await llm.gen(prompt);
    }
  });
}

describe('Context Integration Tests', () => {
  let recordingLLM: ReturnType<typeof createRecordingLLM>;
  
  beforeEach(() => {
    recordingLLM = createRecordingLLM();
  });
  
  describe('Multi-Agent Context in ask()', () => {
    it('should include agent delegation info in ask() context', async () => {
      // Create agents
      const researcher = agent({
        llm: recordingLLM,
        name: 'researcher',
        description: 'Finds information'
      });
      
      // Run multi-agent workflow  
      const results = await agent({ llm: recordingLLM })
        .then({ 
          prompt: 'Create a report', 
          agents: [researcher]
        })
        .run();
      
      // Now use ask() to query about agents
      await results.ask(recordingLLM, 'Which agents were used and what did they do?');
      
      // Get the prompt that was sent to the LLM
      const askPrompt = recordingLLM.getLastPrompt();
      
      // Verify the context includes agent information
      expect(askPrompt).toContain('Agent Execution Results:');
      expect(askPrompt).toContain('Step 1:');
      expect(askPrompt).toContain('Prompt: Create a report');
      
      // Should detect the agent usage in the context
      expect(askPrompt).toContain('Agents Used:');
      expect(askPrompt).toContain('- researcher: Find information about testing');
      
      // Should have the actual question
      expect(askPrompt).toContain('Which agents were used and what did they do?');
    });
    
    it('should show complete agent execution history', async () => {
      // Mock a complete multi-agent execution
      const mockResults = [
        {
          prompt: 'Build a feature',
          llmOutput: 'USE designer: Create mockups\n\nI need design first.',
          durationMs: 100,
          __tokenCount: 20
        },
        {
          prompt: 'Agent designer was delegated: Create mockups',
          llmOutput: 'Mockups created: [homepage.png, dashboard.png]',
          durationMs: 500,
          __tokenCount: 50
        },
        {
          prompt: 'Continue with implementation', 
          llmOutput: 'USE developer: Implement based on mockups',
          durationMs: 100,
          __tokenCount: 15
        },
        {
          prompt: 'Agent developer was delegated: Implement based on mockups',
          llmOutput: 'Implementation complete with React components',
          durationMs: 800,
          __tokenCount: 100
        },
        {
          prompt: 'Final coordination',
          llmOutput: 'DONE: Feature built successfully',
          durationMs: 200,
          agentCalls: [
            { name: 'designer', task: 'Create mockups', tokens: 50, ms: 500 },
            { name: 'developer', task: 'Implement based on mockups', tokens: 100, ms: 800 }
          ],
          __crewTotalTokens: 185
        } as any
      ];
      
      const results = createMockResults(mockResults);
      
      // Query about the execution
      await results.ask(recordingLLM, 'What was the complete workflow?');
      
      const askPrompt = recordingLLM.getLastPrompt();
      
      // Verify all steps are in context
      expect(askPrompt).toContain('Step 1:');
      expect(askPrompt).toContain('Step 2:');
      expect(askPrompt).toContain('Step 3:');
      expect(askPrompt).toContain('Step 4:');
      expect(askPrompt).toContain('Step 5:');
      
      // Verify agent calls summary
      expect(askPrompt).toContain('Total Crew Tokens: 185');
      expect(askPrompt).toContain('Agents Used:');
      expect(askPrompt).toContain('- designer: Create mockups');
      expect(askPrompt).toContain('Tokens: 50');
      expect(askPrompt).toContain('- developer: Implement based on mockups');
      expect(askPrompt).toContain('Tokens: 100');
      
      // Verify delegation summary
      expect(askPrompt).toContain('Agent Delegation Summary:');
      expect(askPrompt).toContain('designer agent (task: "Create mockups")');
      expect(askPrompt).toContain('developer agent (task: "Implement based on mockups")');
    });
  });
  
  describe('Tool Calls Context', () => {
    it('should include tool call details in context', async () => {
      const mockResults = [
        {
          prompt: 'Calculate something',
          llmOutput: 'I will calculate 15 * 23',
          toolCalls: [
            {
              name: 'calculator',
              arguments: { operation: 'multiply', a: 15, b: 23 },
              endpoint: 'http://localhost:3000',
              result: 345,
              ms: 50
            },
            {
              name: 'converter',
              arguments: { value: 345, from: 'decimal', to: 'hex' },
              endpoint: 'http://localhost:3000', 
              result: '0x159',
              ms: 30
            }
          ],
          durationMs: 200
        }
      ];
      
      const results = createMockResults(mockResults);
      
      await results.ask(recordingLLM, 'What tools were used?');
      
      const askPrompt = recordingLLM.getLastPrompt();
      
      expect(askPrompt).toContain('Tools Called (2):');
      expect(askPrompt).toContain('- calculator: {"operation":"multiply","a":15,"b":23}');
      expect(askPrompt).toContain('Result: 345');
      expect(askPrompt).toContain('- converter: {"value":345,"from":"decimal","to":"hex"}');
      expect(askPrompt).toContain('Result: "0x159"');
    });
  });
  
  describe('Context Truncation', () => {
    it('should handle very long contexts gracefully', async () => {
      // Create a result with very long output
      const longOutput = 'x'.repeat(10000);
      const mockResults = [
        {
          prompt: 'Generate long text',
          llmOutput: longOutput,
          durationMs: 1000
        }
      ];
      
      const results = createMockResults(mockResults);
      
      await results.ask(recordingLLM, 'Summarize');
      
      const askPrompt = recordingLLM.getLastPrompt();
      
      // Context should still be included even if long
      expect(askPrompt).toContain('Step 1:');
      expect(askPrompt).toContain('Prompt: Generate long text');
      expect(askPrompt).toContain('LLM Output:');
      expect(askPrompt.length).toBeGreaterThan(10000); // Should include the long output
    });
  });
  
  describe('Empty and Error Cases', () => {
    it('should handle empty results gracefully', async () => {
      const results = createMockResults([]);
      
      await results.ask(recordingLLM, 'What happened?');
      
      const askPrompt = recordingLLM.getLastPrompt();
      
      expect(askPrompt).toContain('Agent Execution Results:');
      expect(askPrompt).not.toContain('Step 1:'); // No steps
      expect(askPrompt).toContain('User Question: What happened?');
    });
    
    it('should handle results with missing data', async () => {
      const mockResults = [
        { /* empty step */ },
        { prompt: 'Only prompt' },
        { llmOutput: 'Only output' },
        { durationMs: 500 }
      ];
      
      const results = createMockResults(mockResults);
      
      await results.ask(recordingLLM, 'Describe the execution');
      
      const askPrompt = recordingLLM.getLastPrompt();
      
      // Should handle all steps even with missing data
      expect(askPrompt).toContain('Step 1:');
      expect(askPrompt).toContain('Step 2:');
      expect(askPrompt).toContain('Step 3:');
      expect(askPrompt).toContain('Step 4:');
    });
  });
});

describe('History Context Building', () => {
  it('should properly build context for delegated agents', async () => {
    // This tests the buildHistoryContextChunked function behavior
    const mockHistory = [
      {
        prompt: 'Create a blog post about AI',
        llmOutput: 'I will coordinate this task'
      },
      {
        prompt: 'Agent researcher was delegated: Find latest AI breakthroughs',
        llmOutput: 'Found 5 major breakthroughs in 2024...'
      }
    ];
    
    // Import the function if it's exported, otherwise we'll test through agent execution
    const researcherLLM = createRecordingLLM();
    const researcher = agent({
      llm: researcherLLM,
      name: 'researcher',
      hideProgress: true
    }).then({ prompt: 'Research: {{task}}' });
    
    // Test with a simple delegated agent execution
    const mockParentContext = {
      prompt: 'Create a blog post about AI',
      llmOutput: 'USE researcher: Find latest AI breakthroughs'
    };
    
    // Create a new agent instance with parent context
    const delegatedResearcher = agent({
      llm: researcherLLM,
      name: 'researcher',
      hideProgress: true
    }).then({ prompt: 'Find latest AI breakthroughs' });
    
    // Execute the delegated agent
    await delegatedResearcher.run();
    
    // The researcher should have been called
    const prompts = researcherLLM.getPrompts();
    expect(prompts.length).toBeGreaterThan(0);
    
    const researcherPrompt = prompts[0];
    
    // Check that the task was passed correctly
    expect(researcherPrompt).toContain('Find latest AI breakthroughs');
  });
});