import {
  isJudgeSpan,
  isLlmSpan,
  isToolSpan
} from "./chunk-5BKGXME7.js";

// src/trace/query.ts
async function runsForScenario(store, scenarioId) {
  return store.listRuns({ scenarioId });
}
async function llmSpans(store, runId) {
  const spans = await store.spans({ runId, kind: "llm" });
  return spans.filter(isLlmSpan);
}
async function toolSpans(store, runId, toolName) {
  const spans = await store.spans({ runId, kind: "tool", toolName });
  return spans.filter(isToolSpan);
}
async function judgeSpans(store, runId) {
  const spans = await store.spans({ runId, kind: "judge" });
  return spans.filter(isJudgeSpan);
}
function groupBy(items, key) {
  const map = /* @__PURE__ */ new Map();
  for (const item of items) {
    const k = key(item);
    let bucket = map.get(k);
    if (!bucket) {
      bucket = [];
      map.set(k, bucket);
    }
    bucket.push(item);
  }
  return map;
}
function argHash(args) {
  return stableStringify(args);
}
function stableStringify(value) {
  if (value === null || typeof value !== "object") return JSON.stringify(value);
  if (Array.isArray(value)) return `[${value.map(stableStringify).join(",")}]`;
  const keys = Object.keys(value).sort();
  const parts = keys.map(
    (k) => `${JSON.stringify(k)}:${stableStringify(value[k])}`
  );
  return `{${parts.join(",")}}`;
}
function aggregateLlm(spans) {
  return spans.reduce(
    (acc, s) => ({
      inputTokens: acc.inputTokens + (s.inputTokens ?? 0),
      outputTokens: acc.outputTokens + (s.outputTokens ?? 0),
      cachedTokens: acc.cachedTokens + (s.cachedTokens ?? 0),
      costUsd: acc.costUsd + (s.costUsd ?? 0)
    }),
    { inputTokens: 0, outputTokens: 0, cachedTokens: 0, costUsd: 0 }
  );
}
function runFailureClass(run) {
  if (run.outcome?.failureClass) return run.outcome.failureClass;
  if (run.status === "completed" && run.outcome?.pass !== false) return "success";
  if (run.status === "aborted") return "budget_exceeded";
  return "unknown";
}

export {
  runsForScenario,
  llmSpans,
  toolSpans,
  judgeSpans,
  groupBy,
  argHash,
  aggregateLlm,
  runFailureClass
};
//# sourceMappingURL=chunk-47X6LRCE.js.map