File size: 1,715 Bytes
67b042f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
09644e5
67b042f
 
 
 
 
 
 
 
 
09644e5
 
 
67b042f
 
 
 
 
 
 
 
 
 
 
 
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
import { AgentTrace, AgentStep, AgentTraceMetadata } from '@/types/agent';

/**
 * Export the complete trace as JSON
 * @param trace The agent trace
 * @param steps The trace steps
 * @param metadata The final metadata
 * @returns A JSON object containing the entire trace
 */
export const exportTraceToJson = (
  trace: AgentTrace,
  steps: AgentStep[],
  metadata?: AgentTraceMetadata
): string => {
  const exportData = {
    trace: {
      id: trace.id,
      timestamp: trace.timestamp,
      instruction: trace.instruction,
      modelId: trace.modelId,
      isRunning: trace.isRunning,
    },
    metadata: metadata || trace.traceMetadata,
    steps: steps.map((step) => ({
      traceId: step.traceId,
      stepId: step.stepId,
      error: step.error,
      thought: step.thought,
      actions: step.actions,
      duration: step.duration,
      inputTokensUsed: step.inputTokensUsed,
      outputTokensUsed: step.outputTokensUsed,
      step_evaluation: step.step_evaluation,
      // Don't include base64 image to reduce JSON size
      hasImage: !!step.image,
    })),
    exportedAt: new Date().toISOString(),
  };

  return JSON.stringify(exportData, null, 2);
};

/**
 * Download a JSON with a filename
 * @param jsonString JSON string to download
 * @param filename Filename to download
 */
export const downloadJson = (jsonString: string, filename: string = 'trace.json') => {
  const blob = new Blob([jsonString], { type: 'application/json' });
  const url = URL.createObjectURL(blob);
  const link = document.createElement('a');
  link.href = url;
  link.download = filename;
  document.body.appendChild(link);
  link.click();
  document.body.removeChild(link);
  URL.revokeObjectURL(url);
};