๐Ÿ“ฆ Kong / volcano-sdk

๐Ÿ“„ progress.e2e.test.ts ยท 347 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
347import { describe, it, expect } from 'vitest';
import { agent, llmOpenAI } from '../src/volcano-sdk.js';

describe('Progress output e2e with structured logs (live APIs)', () => {
  it('validates default progress works for basic LLM steps', async () => {
    if (!process.env.OPENAI_API_KEY) {
      throw new Error('OPENAI_API_KEY required');
    }

    const llm = llmOpenAI({
      apiKey: process.env.OPENAI_API_KEY!,
      model: 'gpt-4o-mini',
      options: { max_completion_tokens: 64, temperature: 0, top_p: 1 }
    });

    // Capture console output
    const logs: string[] = [];
    const originalLog = console.log;
    const originalWrite = process.stdout.write.bind(process.stdout);
    
    console.log = (...args: any[]) => {
      logs.push(args.join(' '));
      originalLog(...args);
    };
    
    process.stdout.write = (chunk: any): boolean => {
      logs.push(String(chunk));
      return originalWrite(chunk);
    };

    try {
      await agent({ llm })
        .then({ prompt: "Say 'Hello World' exactly" })
        .run();

      // Restore
      console.log = originalLog;
      process.stdout.write = originalWrite;

      // Verify progress elements appeared
      const output = logs.join('');
      
      // Check for header with structured log format
      expect(output).toMatch(/\[.*agent="untitled" status=init\] ๐ŸŒ‹ running Volcano agent/);
      expect(output).toContain('volcano-sdk v');
      expect(output).toContain('https://volcano.dev');
      
      // Check for step indicator with structured log
      expect(output).toMatch(/\[.*agent="untitled" step=1 status=init\] Say 'Hello World' exactly/);
      
      // Check for completion with structured log
      expect(output).toMatch(/\[.*status=complete\] โœ” Complete/);
      
      // Check for workflow summary with structured log
      expect(output).toMatch(/\[.*agent="untitled" status=complete\] ๐ŸŽ‰ agent complete/);
      
    } finally {
      console.log = originalLog;
      process.stdout.write = originalWrite;
    }
  });

  it('validates default progress works for multi-step workflows', { timeout: 30000 }, async () => {
    if (!process.env.OPENAI_API_KEY) {
      throw new Error('OPENAI_API_KEY required');
    }

    const llm = llmOpenAI({
      apiKey: process.env.OPENAI_API_KEY!,
      model: 'gpt-4o-mini',
      options: { max_completion_tokens: 64, temperature: 0, top_p: 1 }
    });

    const logs: string[] = [];
    const originalLog = console.log;
    const originalWrite = process.stdout.write.bind(process.stdout);
    
    console.log = (...args: any[]) => {
      logs.push(args.join(' '));
      originalLog(...args);
    };
    
    process.stdout.write = (chunk: any): boolean => {
      logs.push(String(chunk));
      return originalWrite(chunk);
    };

    try {
      await agent({ llm })
        .then({ prompt: "Say 'Step 1' exactly" })
        .then({ prompt: "Say 'Step 2' exactly" })
        .run();

      console.log = originalLog;
      process.stdout.write = originalWrite;

      const output = logs.join('');
      
      // CRITICAL: Verify both steps shown with structured logs
      expect(output).toMatch(/\[.*agent="untitled" step=1 status=init\] Say 'Step 1' exactly/);
      expect(output).toMatch(/\[.*agent="untitled" step=2 status=init\] Say 'Step 2' exactly/);
      
      // Token progress shows for longer responses (>10 tokens)
      // Short responses may not trigger display
      
      // CRITICAL: Verify both step completions appear
      // Check for step 1 completion
      expect(output).toMatch(/\[.*agent="untitled" step=1 status=complete\] โœ” Complete/);
      // Check for step 2 completion  
      expect(output).toMatch(/\[.*agent="untitled" step=2 status=complete\] โœ” Complete/);
      
      // CRITICAL: Verify no triple newlines
      expect(output).not.toMatch(/\n\n\n/);
      
      // CRITICAL: Verify workflow summary with structured log
      expect(output).toMatch(/\[.*agent="untitled" status=complete\] ๐ŸŽ‰ agent complete/);
      
    } finally {
      console.log = originalLog;
      process.stdout.write = originalWrite;
    }
  });

  it('validates default progress works for agent crews', { timeout: 60000 }, async () => {
    if (!process.env.OPENAI_API_KEY) {
      throw new Error('OPENAI_API_KEY required');
    }

    const llm = llmOpenAI({
      apiKey: process.env.OPENAI_API_KEY!,
      model: 'gpt-4o-mini',
      options: { max_completion_tokens: 64, temperature: 0, top_p: 1 }
    });

    // Define simple agents
    const summarizer = agent({
      llm,
      name: 'summarizer',
      description: 'Creates concise summaries'
    });

    const logs: string[] = [];
    const originalLog = console.log;
    const originalWrite = process.stdout.write.bind(process.stdout);
    
    console.log = (...args: any[]) => {
      logs.push(args.join(' '));
      originalLog(...args);
    };
    
    process.stdout.write = (chunk: any): boolean => {
      logs.push(String(chunk));
      return originalWrite(chunk);
    };

    try {
      await agent({ llm, timeout: 60 })
        .then({
          prompt: 'Summarize the word "AI" in one sentence',
          agents: [summarizer]
        })
        .run();

      console.log = originalLog;
      process.stdout.write = originalWrite;

      const output = logs.join('');
      
      // Verify header with structured log
      expect(output).toMatch(/\[.*agent="untitled" status=init\] ๐ŸŒ‹ running Volcano agent/);
      expect(output).toContain('volcano-sdk v');
      expect(output).toContain('https://volcano.dev');
      
      // Verify step shown with structured log
      expect(output).toMatch(/\[.*agent="untitled" step=1 status=init\] Summarize the word "AI"/);
      
      // CRITICAL: Verify coordinator thinking with structured log
      expect(output).toMatch(/\[.*agent="untitled" status=init\] ๐Ÿง  selecting agents/);
      
      // CRITICAL: Verify coordinator completion with agent selection
      expect(output).toMatch(/\[.*agent="untitled" status=complete\] ๐Ÿง  use "summarizer" agent/);
      
      // CRITICAL: Verify agent was invoked (no steps for delegated agents)
      // Delegated agents created for crews don't have pre-defined steps
      
      // Token progress shows for longer responses
      // (Short responses complete before 10-token threshold)
      
      // CRITICAL: Verify agent completion with structured log
      expect(output).toMatch(/\[.*agent="summarizer".*status=complete\] โœ” Complete \| \d+ tokens \| \d+ tool calls \| \d+\.\d+s/);
      
      // CRITICAL: Verify no triple newlines
      expect(output).not.toMatch(/\n\n\n/);
      
      // Verify final completion with totals and structured log
      expect(output).toMatch(/\[.*agent="untitled" step=1 status=complete\] โœ” Complete/);
      expect(output).toMatch(/\[.*agent="untitled" status=complete\] ๐ŸŽ‰ agent complete \| \d+ tokens \| \d+ tool calls \| \d+\.\d+s/);
      
    } finally {
      console.log = originalLog;
      process.stdout.write = originalWrite;
    }
  });

  it('validates default progress never breaks with errors', { timeout: 30000 }, async () => {
    if (!process.env.OPENAI_API_KEY) {
      throw new Error('OPENAI_API_KEY required');
    }

    const llm = llmOpenAI({
      apiKey: process.env.OPENAI_API_KEY!,
      model: 'gpt-4o-mini',
      options: { max_completion_tokens: 64, temperature: 0, top_p: 1 }
    });

    const logs: string[] = [];
    const originalLog = console.log;
    const originalWrite = process.stdout.write.bind(process.stdout);
    const originalError = console.error;
    
    console.log = (...args: any[]) => {
      logs.push(args.join(' '));
      originalLog(...args);
    };
    
    console.error = (...args: any[]) => {
      logs.push(args.join(' '));
      originalError(...args);
    };
    
    process.stdout.write = (chunk: any): boolean => {
      logs.push(String(chunk));
      return originalWrite(chunk);
    };

    try {
      // This should timeout and retry, but progress should still work
      await agent({ llm, timeout: 1, retry: { retries: 2 } })
        .then({ prompt: "Count to 100 slowly" })
        .run();
    } catch (e) {
      // Expected to fail, that's OK
    }

    console.log = originalLog;
    console.error = originalError;
    process.stdout.write = originalWrite;

    const output = logs.join('');
    
    // Even with errors/timeouts, progress should have shown with structured logs
    expect(output).toMatch(/\[.*agent="untitled" status=init\] ๐ŸŒ‹ running Volcano agent/);
    expect(output).toMatch(/\[.*agent="untitled" step=1 status=init\] Count to 100 slowly/);
    
    // Should have attempted multiple times (retries)
    const stepMatches = output.match(/\[.*step=1 status=init\]/g);
    expect(stepMatches?.length).toBeGreaterThanOrEqual(1);
  });

  it('validates progress output is TTY-aware', async () => {
    if (!process.env.OPENAI_API_KEY) {
      throw new Error('OPENAI_API_KEY required');
    }

    const llm = llmOpenAI({
      apiKey: process.env.OPENAI_API_KEY!,
      model: 'gpt-4o-mini'
    });

    const logs: string[] = [];
    const originalLog = console.log;
    
    console.log = (...args: any[]) => {
      logs.push(args.join(' '));
      originalLog(...args);
    };

    try {
      await agent({ llm })
        .then({ prompt: "Say 'Test'" })
        .run();

      console.log = originalLog;

      const output = logs.join('');
      
      // Progress should work (even if not TTY in test environment) with structured logs
      expect(output).toMatch(/\[.*agent="untitled" status=init\] ๐ŸŒ‹ running Volcano agent/);
      expect(output).toMatch(/\[.*status=complete\] โœ” Complete/);
      
    } finally {
      console.log = originalLog;
    }
  });

  it('validates hideProgress: true suppresses all progress output', { timeout: 10000 }, async () => {
    if (!process.env.OPENAI_API_KEY) {
      throw new Error('OPENAI_API_KEY required');
    }

    const llm = llmOpenAI({
      apiKey: process.env.OPENAI_API_KEY!,
      model: 'gpt-4o-mini',
      options: { max_completion_tokens: 32, temperature: 0, top_p: 1 }
    });

    const logs: string[] = [];
    const originalLog = console.log;
    const originalWrite = process.stdout.write.bind(process.stdout);
    
    console.log = (...args: any[]) => {
      logs.push(args.join(' '));
      originalLog(...args);
    };
    
    process.stdout.write = (chunk: any): boolean => {
      logs.push(String(chunk));
      return originalWrite(chunk);
    };

    try {
      await agent({ llm, hideProgress: true })
        .then({ prompt: "Say 'Hello' exactly" })
        .then({ prompt: "Say 'World' exactly" })
        .run();

      console.log = originalLog;
      process.stdout.write = originalWrite;

      const output = logs.join('');
      
      // CRITICAL: Progress elements should NOT appear when hideProgress: true
      expect(output).not.toContain('๐ŸŒ‹ running Volcano agent');
      expect(output).not.toMatch(/\[.*step=\d+ status=/);
      expect(output).not.toContain('โœ” Complete');
      expect(output).not.toContain('๐ŸŽ‰ agent complete');
      expect(output).not.toContain('โ”โ”โ”');
      // Note: Skip checking '๐Ÿ’ญ' due to potential cross-test contamination in parallel runs
      
    } finally {
      console.log = originalLog;
      process.stdout.write = originalWrite;
    }
  });
});