MCPcopy Create free account
hub / github.com/ShipSecAI/studio / TraceAdapter

Class TraceAdapter

worker/src/adapters/trace.adapter.ts:11–139  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

9 * can persist events to PostgreSQL via Drizzle when a database instance is provided.
10 */
11export class TraceAdapter implements ITraceService {
12 private readonly bufferEnabled: boolean;
13 private readonly eventsByRun: Map<string, TraceEvent[]> | undefined;
14 private readonly sequenceByRun = new Map<string, number>();
15 private readonly metadataByRun = new Map<
16 string,
17 { workflowId?: string; organizationId?: string | null }
18 >();
19 private readonly logger: Pick<Console, 'log' | 'error'>;
20
21 constructor(
22 private readonly db?: NodePgDatabase<typeof schema>,
23 options: {
24 buffer?: boolean;
25 logger?: Pick<Console, 'log' | 'error'>;
26 } = {},
27 ) {
28 this.bufferEnabled = options.buffer ?? false;
29 this.eventsByRun = this.bufferEnabled ? new Map<string, TraceEvent[]>() : undefined;
30 this.logger = options.logger ?? console;
31 }
32
33 record(event: TraceEvent): void {
34 if (this.bufferEnabled && this.eventsByRun) {
35 const list = this.eventsByRun.get(event.runId) ?? [];
36 list.push(event);
37 this.eventsByRun.set(event.runId, list);
38 }
39
40 const context =
41 event.message !== undefined
42 ? `${event.type} - ${event.nodeRef}: ${event.message}`
43 : `${event.type} - ${event.nodeRef}`;
44 this.logger.log(`[TRACE][${event.level}] ${context}`);
45
46 if (!this.db) {
47 return;
48 }
49
50 const sequence = this.nextSequence(event.runId);
51 void this.persist(event, sequence).catch((error) => {
52 this.logger.error('[TRACE] Failed to persist trace event', error);
53 });
54 }
55
56 getEvents(runId: string): TraceEvent[] {
57 if (!this.bufferEnabled || !this.eventsByRun) {
58 return [];
59 }
60 return this.eventsByRun.get(runId) ?? [];
61 }
62
63 clear(): void {
64 if (this.eventsByRun) {
65 this.eventsByRun.clear();
66 }
67 this.sequenceByRun.clear();
68 this.metadataByRun.clear();

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected