MCPcopy
hub / github.com/Doorman11991/smallcode / PgBackend

Class PgBackend

src/compiled/cognition/traces.ts:121–158  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

119}
120
121class PgBackend implements SpanBackend {
122 async write(span: CognitionSpan): Promise<void> {
123 // Lazy import — keeps tests that don't exercise PG free of pg.
124 // eslint-disable-next-line @typescript-eslint/no-require-imports
125 const { query } = require("../db") as { query: (sql: string, args: unknown[]) => Promise<unknown> };
126 await query(
127 `INSERT INTO cognition_traces (
128 span_id, trace_id, parent_span_id, workflow, step, kind, prompt, model, provider,
129 input_redacted, output_redacted, validate_result, confidence,
130 prompt_tokens, completion_tokens, cost_usd, latency_ms, cache_hit,
131 status, error_code, metadata, started_at, finished_at
132 ) VALUES (
133 $1, $2, $3, $4, $5, $6, $7, $8, $9,
134 $10, $11, $12, $13, $14, $15, $16, $17, $18,
135 $19, $20, $21, to_timestamp($22 / 1000.0), to_timestamp($23 / 1000.0)
136 )
137 ON CONFLICT (span_id) DO NOTHING`,
138 [
139 span.span_id, span.trace_id, span.parent_span_id,
140 span.workflow, span.step, span.kind, span.prompt, span.model, span.provider,
141 JSON.stringify(span.input_redacted), JSON.stringify(span.output_redacted),
142 span.validate_result, span.confidence,
143 span.prompt_tokens, span.completion_tokens, span.cost_usd, span.latency_ms, span.cache_hit,
144 span.status, span.error_code, JSON.stringify(span.metadata),
145 span.started_at, span.finished_at,
146 ],
147 );
148 }
149 async loadTrace(trace_id: string): Promise<CognitionSpan[]> {
150 // eslint-disable-next-line @typescript-eslint/no-require-imports
151 const { query } = require("../db") as { query: (sql: string, args: unknown[]) => Promise<Record<string, unknown>[]> };
152 const rows = await query(
153 "SELECT * FROM cognition_traces WHERE trace_id = $1 ORDER BY started_at",
154 [trace_id],
155 );
156 return rows.map(rowToSpan);
157 }
158}
159
160class NoopBackend implements SpanBackend {
161 async write(_span: CognitionSpan): Promise<void> { /* drop */ }

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected