|
| 1 | +export interface Trace { |
| 2 | + name: string; |
| 3 | + startTime: number; |
| 4 | + endTime?: number; |
| 5 | + duration?: number; |
| 6 | + steps: StepTrace[]; |
| 7 | + error?: string; |
| 8 | +} |
| 9 | + |
| 10 | +export interface StepTrace { |
| 11 | + name: string; |
| 12 | + output: any; |
| 13 | + error?: string; |
| 14 | + traces: string[] | null; |
| 15 | + duration: number; |
| 16 | + startTime: number; |
| 17 | + endTime: number; |
| 18 | +} |
| 19 | + |
| 20 | +export type TraceType = |
| 21 | + | 'workflow' |
| 22 | + | 'agent' |
| 23 | + | 'chunk' |
| 24 | + | 'memory' |
| 25 | + | 'parse' |
| 26 | + | 'embed'; |
| 27 | + |
| 28 | +export type PrimitiveTrace = |
| 29 | + | {chunk: any} |
| 30 | + | {agent: any} |
| 31 | + | {memory: any} |
| 32 | + | {parse: any} |
| 33 | + | {embed: any} |
| 34 | + | {workflow: WorkflowTrace; entityAuthId: string}; |
| 35 | + |
| 36 | +type WorkflowTrace = { |
| 37 | + createdAt: string; |
| 38 | + id: string; |
| 39 | + agentWorkflowId: string; |
| 40 | + name: string; |
| 41 | + startTime: number; |
| 42 | + endTime?: number; |
| 43 | + duration?: number; |
| 44 | + steps: StepTrace[]; |
| 45 | + error?: string; |
| 46 | +}; |
| 47 | + |
| 48 | +export class TraceManager { |
| 49 | + private traces: Map<string, PrimitiveTrace> = new Map(); |
| 50 | + |
| 51 | + createTrace(type: TraceType, traceData: any = {}): string { |
| 52 | + const traceId = crypto.randomUUID(); |
| 53 | + let trace: PrimitiveTrace; |
| 54 | + const createdAt = new Date().toISOString(); |
| 55 | + if (type === 'workflow') { |
| 56 | + trace = { |
| 57 | + workflow: { |
| 58 | + createdAt, |
| 59 | + id: traceId, |
| 60 | + agentWorkflowId: process.env.LANGBASE_AGENT_ID || '', |
| 61 | + name: traceData.name || '', |
| 62 | + startTime: Date.now(), |
| 63 | + steps: [], |
| 64 | + }, |
| 65 | + entityAuthId: '', |
| 66 | + }; |
| 67 | + } else if (type === 'agent') { |
| 68 | + trace = {agent: {...traceData, createdAt, id: traceId}}; |
| 69 | + } else if (type === 'chunk') { |
| 70 | + trace = {chunk: {...traceData, createdAt, id: traceId}}; |
| 71 | + } else if (type === 'memory') { |
| 72 | + trace = {memory: {...traceData, createdAt, id: traceId}}; |
| 73 | + } else if (type === 'parse') { |
| 74 | + trace = {parse: {...traceData, createdAt, id: traceId}}; |
| 75 | + } else if (type === 'embed') { |
| 76 | + trace = {embed: {...traceData, createdAt, id: traceId}}; |
| 77 | + } else { |
| 78 | + throw new Error('Unknown trace type'); |
| 79 | + } |
| 80 | + this.traces.set(traceId, trace); |
| 81 | + return traceId; |
| 82 | + } |
| 83 | + |
| 84 | + addStep(traceId: string, step: StepTrace) { |
| 85 | + const trace = this.traces.get(traceId); |
| 86 | + if (trace && 'workflow' in trace) { |
| 87 | + trace.workflow.steps.push(step); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + endTrace(traceId: string) { |
| 92 | + const trace = this.traces.get(traceId); |
| 93 | + if (trace && 'workflow' in trace) { |
| 94 | + trace.workflow.endTime = Date.now(); |
| 95 | + trace.workflow.duration = |
| 96 | + trace.workflow.endTime - trace.workflow.startTime; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + getTrace(traceId: string): PrimitiveTrace | undefined { |
| 101 | + return this.traces.get(traceId); |
| 102 | + } |
| 103 | + |
| 104 | + printTrace(traceId: string) { |
| 105 | + const trace = this.traces.get(traceId); |
| 106 | + if (!trace) return; |
| 107 | + if ('workflow' in trace) { |
| 108 | + const wf = trace.workflow; |
| 109 | + const duration = wf.endTime |
| 110 | + ? wf.endTime - wf.startTime |
| 111 | + : Date.now() - wf.startTime; |
| 112 | + console.log('\n📊 Workflow Trace:'); |
| 113 | + console.log(`Name: ${wf.name}`); |
| 114 | + console.log(`Duration: ${duration}ms`); |
| 115 | + console.log(`Start Time: ${new Date(wf.startTime).toISOString()}`); |
| 116 | + if (wf.endTime) { |
| 117 | + console.log(`End Time: ${new Date(wf.endTime).toISOString()}`); |
| 118 | + } |
| 119 | + console.log('\nSteps:'); |
| 120 | + wf.steps.forEach(step => { |
| 121 | + console.log(`\n Step: ${step.name}`); |
| 122 | + console.log(` Duration: ${step.duration}ms`); |
| 123 | + if (step.traces && step.traces.length > 0) { |
| 124 | + console.log(` Traces:`, step.traces); |
| 125 | + } |
| 126 | + console.log(` Output:`, step.output); |
| 127 | + }); |
| 128 | + } else { |
| 129 | + console.log('\n📊 Primitive Trace:'); |
| 130 | + console.dir(trace, {depth: 4}); |
| 131 | + } |
| 132 | + } |
| 133 | +} |
0 commit comments