import type { BaseEvent } from "../types.js"; import type { BenchmarkSample } from "./evaluator.js"; function number(event: BaseEvent, key: string): number | undefined { const value = event[key]; return typeof value === "number" && Number.isFinite(value) ? value : undefined; } export function collectBenchmarkSamples(events: readonly BaseEvent[], cohort: string): BenchmarkSample[] { const byTask = new Map(); for (const event of events) { if (!event.taskId) continue; (byTask.get(event.taskId) ?? byTask.set(event.taskId, []).get(event.taskId)!).push(event); } const samples: BenchmarkSample[] = []; for (const [taskId, group] of byTask) { const ordered = [...group].sort((left, right) => left.timestamp.localeCompare(right.timestamp)); const settled = [...ordered].reverse().find((event) => event.eventType === "result.completed"); if (!settled) continue; const started = ordered.find((event) => event.eventType === "request.received") ?? ordered[0]!; const costToGreen = ordered.reduce((sum, event) => sum + (number(event, "creditsEstimated") ?? 0), 0); const timeToGreenMs = Math.max(0, Date.parse(settled.timestamp) - Date.parse(started.timestamp)); if (!Number.isFinite(timeToGreenMs)) continue; samples.push({ taskId, cohort, system: "ultrapi", verified: settled.success === true && settled.verified === true, costToGreen, timeToGreenMs, }); } return samples.sort((left, right) => left.taskId.localeCompare(right.taskId)); }