// src/trajectory.ts
async function buildTrajectory(store, runId) {
  const spans = await store.spans({ runId });
  const events = await store.events({ runId });
  const childrenOf = /* @__PURE__ */ new Map();
  for (const s of spans) {
    const arr = childrenOf.get(s.parentSpanId) ?? [];
    arr.push(s);
    childrenOf.set(s.parentSpanId, arr);
  }
  for (const arr of childrenOf.values()) arr.sort((a, b) => a.startedAt - b.startedAt);
  const eventsBySpan = /* @__PURE__ */ new Map();
  for (const e of events) {
    if (!e.spanId) continue;
    const arr = eventsBySpan.get(e.spanId) ?? [];
    arr.push(e);
    eventsBySpan.set(e.spanId, arr);
  }
  const steps = [];
  const walk = (spanId, depth) => {
    const kids = childrenOf.get(spanId) ?? [];
    for (const child of kids) {
      steps.push({
        index: steps.length,
        span: child,
        depth,
        events: eventsBySpan.get(child.spanId) ?? []
      });
      walk(child.spanId, depth + 1);
    }
  };
  walk(void 0, 0);
  const llmTurns = steps.filter((s) => s.span.kind === "llm").length;
  const toolCalls = steps.filter((s) => s.span.kind === "tool").length;
  const judgeVerdicts = steps.filter((s) => s.span.kind === "judge").length;
  const retrievals = steps.filter((s) => s.span.kind === "retrieval").length;
  let totalDurationMs = 0;
  if (steps.length > 0) {
    const starts = spans.map((s) => s.startedAt);
    const ends = spans.map((s) => s.endedAt ?? s.startedAt);
    totalDurationMs = Math.max(...ends) - Math.min(...starts);
  }
  return { runId, steps, llmTurns, toolCalls, judgeVerdicts, retrievals, totalDurationMs };
}

export {
  buildTrajectory
};
//# sourceMappingURL=chunk-RZTMDUO7.js.map