import {
  parseRelativeDate
} from "./chunk-ZXKLPC2E.js";
import {
  searchTraces
} from "./chunk-CAY57CKF.js";
import "./chunk-KOO5733S.js";

// src/tools/search-traces.ts
async function handleSearchTraces(params) {
  const now = Date.now();
  const startDate = params.startDate ? parseRelativeDate(params.startDate) : now - 864e5;
  const endDate = params.endDate ? parseRelativeDate(params.endDate) : now;
  const format = params.format ?? "digest";
  const result = await searchTraces({
    query: params.query,
    filters: params.filters,
    startDate,
    endDate,
    pageSize: params.pageSize ?? 25,
    scrollId: params.scrollId,
    format
  });
  const traces = result.traces ?? [];
  if (traces.length === 0) {
    return "No traces found matching your query.";
  }
  if (format === "json") {
    return JSON.stringify(result, null, 2);
  }
  const lines = [];
  lines.push(
    `Found ${result.pagination?.totalHits ?? traces.length} traces:
`
  );
  for (const trace of traces) {
    lines.push(`### Trace: ${trace.trace_id}`);
    if (trace.formatted_trace) {
      lines.push(trace.formatted_trace);
    } else {
      const inputStr = trace.input?.value ? String(trace.input.value) : "N/A";
      const outputStr = trace.output?.value ? String(trace.output.value) : "N/A";
      lines.push(
        `- **Input**: ${inputStr.slice(0, 100)}${inputStr.length > 100 ? "..." : ""}`
      );
      lines.push(
        `- **Output**: ${outputStr.slice(0, 100)}${outputStr.length > 100 ? "..." : ""}`
      );
    }
    if (trace.timestamps) {
      lines.push(`- **Time**: ${trace.timestamps.started_at || "N/A"}`);
    }
    if (trace.error) {
      lines.push(`- **Error**: ${JSON.stringify(trace.error)}`);
    }
    lines.push("");
  }
  if (result.pagination?.scrollId) {
    lines.push(
      `
**More results available.** Use scrollId: "${result.pagination.scrollId}" to get next page.`
    );
  }
  lines.push(
    '\n> Tip: Use `get_trace` with a trace_id for full details. Use `search_traces` with `format: "json"` for raw data. Use `discover_schema` to see available filter fields.'
  );
  return lines.join("\n");
}
export {
  handleSearchTraces
};
//# sourceMappingURL=search-traces-M5E5VB4X.js.map