Files
seal-use/src/domains/langfuse/summary.ts
T
gitea 8d7f930596 Split Langfuse flow heuristics out of the trace service
service.ts interleaved orchestration (cache-first trace access) with ~200
lines of flow heuristics — stage word tables, evidence collection, source
classification — and carried near-isomorphic copies of the collector for the
attachment and rule flows. None of it had tests.

flows.ts now owns the heuristics: one collectEvidence shared by both flows,
stage vocabulary as data tables so tuning a word list cannot alter a code
path, and an explicit attachment/rule difference (only the rule flow falls
back to scanning every observation when a stage has no named candidates).
summary.ts holds the trace/observation projections both the service and the
flows need, which also keeps service <-> flows acyclic.

Adds 15 tests over the previously untested branches, including the two
behaviors the extraction surfaced: the "unknown" classification arm is
unreachable as written, and a filename-only needle set can never reach the
OCR/inference grading.
2026-10-11 19:33:29 +08:00

63 lines
2.2 KiB
TypeScript

import { stringifyValue } from "./search.js";
import type { LangfuseObservationSummary, LangfuseTraceSummary } from "./types.js";
/**
* Size-before-content projections of a cached trace.
*
* Both the outline/search responses and the flow evidence builders need these
* (a stage reports its candidate observations in summary form), so they live
* here rather than in `service.ts` — otherwise `flows.ts` would have to import
* the service that imports it back.
*/
export function summarizeTrace(trace: LangfuseTraceSummary) {
return {
id: trace.id,
name: trace.name,
projectId: trace.projectId,
sessionId: trace.sessionId,
userId: trace.userId,
timestamp: trace.timestamp,
environment: trace.environment,
tags: trace.tags,
inputLength: stringifyValue(trace.input).length,
outputLength: stringifyValue(trace.output).length,
metadata: trace.metadata,
latency: trace.latency,
totalCost: trace.totalCost
};
}
export function summarizeObservation(observation: LangfuseObservationSummary) {
return {
id: observation.id,
traceId: observation.traceId,
parentObservationId: observation.parentObservationId,
type: observation.type,
name: observation.name,
startTime: observation.startTime,
endTime: observation.endTime,
model: observation.providedModelName ?? observation.model,
inputLength: stringifyValue(observation.input).length,
outputLength: stringifyValue(observation.output).length,
inputBytes: byteLength(stringifyValue(observation.input)),
outputBytes: byteLength(stringifyValue(observation.output)),
metadataKeys: objectKeys(observation.metadata),
usageDetails: observation.usageDetails,
costDetails: observation.costDetails,
totalCost: observation.totalCost
};
}
export function objectKeys(value: unknown): string[] {
return value && typeof value === "object" && !Array.isArray(value) ? Object.keys(value) : [];
}
export function byteLength(value: string): number {
return Buffer.byteLength(value, "utf8");
}
export function uniqueStrings(values: Array<string | undefined>): string[] {
return [...new Set(values.filter((value): value is string => Boolean(value?.trim())))];
}