All files / src/components/session RunHistory.tsx

0% Statements 0/136
0% Branches 0/1
0% Functions 0/1
0% Lines 0/136

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183                                                                                                                                                                                                                                                                                                                                                                             
import { useState } from "react";
import { useQuery } from "@tanstack/react-query";
import { fetchSessionRuns } from "../../api";
import type { RunSession } from "../../types";
import { LoadingSpinner } from "../shared/LoadingSpinner";
import { RunDetailModal } from "./RunDetailModal";
 
export function RunHistory({ sessionId }: { sessionId: string }) {
  const [selectedRun, setSelectedRun] = useState<RunSession | null>(null);
  const [isOpen, setIsOpen] = useState(false);
 
  const runsQuery = useQuery({
    queryKey: ["session-runs", sessionId],
    queryFn: () => fetchSessionRuns(sessionId),
    staleTime: 15_000,
  });
 
  const runs = runsQuery.data ?? [];
  const hasRuns = runs.length > 0;
 
  return (
    <>
      <div className="run-history">
        <button
          type="button"
          className={`run-history__toggle${isOpen ? " is-open" : ""}`}
          onClick={() => setIsOpen((v) => !v)}
          disabled={runsQuery.isLoading && !hasRuns}
          aria-expanded={isOpen}
          aria-label="Toggle run history"
        >
          <svg className="run-history__icon" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true">
            <circle cx="12" cy="12" r="10" />
            <polyline points="12 6 12 12 16 14" />
          </svg>
          <span className="run-history__label">
            Runs{hasRuns ? ` (${runs.length})` : ""}
          </span>
          <svg className="run-history__chevron" width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.5" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true">
            <polyline points="6 9 12 15 18 9" />
          </svg>
        </button>
 
        {isOpen ? (
          <div className="run-history__panel">
            {runsQuery.isLoading ? (
              <div className="run-history__loading">
                <LoadingSpinner size="sm" />
              </div>
            ) : runsQuery.isError ? (
              <div className="run-history__empty">Failed to load runs.</div>
            ) : !hasRuns ? (
              <div className="run-history__empty">No runs recorded yet.</div>
            ) : (
              <div className="run-history__list">
                {runs.map((run) => (
                  <RunCard
                    key={run.run_session_id}
                    run={run}
                    onSelect={() => setSelectedRun(run)}
                  />
                ))}
              </div>
            )}
          </div>
        ) : null}
      </div>
 
      {selectedRun ? (
        <RunDetailModal
          runSessionId={selectedRun.run_session_id}
          onClose={() => setSelectedRun(null)}
        />
      ) : null}
    </>
  );
}
 
function RunCard({
  run,
  onSelect,
}: {
  run: RunSession;
  onSelect: () => void;
}) {
  const statusModifier =
    run.status === "completed" ? "completed"
    : run.status === "failed" ? "failed"
    : run.status === "started" ? "running"
    : "idle";
 
  const agentLabel = run.agent_name ?? run.agent_type ?? "agent";
  const modelLabel = run.model ?? "unknown";
  const durationLabel = run.total_duration_ms != null
    ? formatDuration(run.total_duration_ms)
    : null;
  const totalTokens =
    run.input_tokens + run.output_tokens + run.reasoning_tokens + run.tool_use_tokens;
 
  return (
    <button
      type="button"
      className="run-card"
      onClick={onSelect}
    >
      <div className="run-card__header">
        <span className={`run-card__status status-pill status-pill--${statusModifier}`}>
          {run.status}
        </span>
        <span className="run-card__agent">{agentLabel}</span>
        {run.parent_run_session_id ? (
          <span className="run-card__badge run-card__badge--sub">sub</span>
        ) : null}
      </div>
      <div className="run-card__meta">
        <span className="run-card__model">{modelLabel}</span>
        {run.provider ? (
          <>
            <span className="run-card__sep" aria-hidden="true" />
            <span>{run.provider}</span>
          </>
        ) : null}
        {durationLabel ? (
          <>
            <span className="run-card__sep" aria-hidden="true" />
            <span>{durationLabel}</span>
          </>
        ) : null}
      </div>
      <div className="run-card__stats">
        {totalTokens > 0 ? (
          <span className="run-card__stat">{formatCount(totalTokens)} tok</span>
        ) : null}
        {run.total_api_calls > 0 ? (
          <span className="run-card__stat">{run.total_api_calls} API calls</span>
        ) : null}
        {run.total_tool_calls > 0 ? (
          <span className="run-card__stat">{run.total_tool_calls} tool calls</span>
        ) : null}
        {run.error_count > 0 ? (
          <span className="run-card__stat run-card__stat--error">{run.error_count} errors</span>
        ) : null}
        {run.estimated_cost_usd > 0 ? (
          <span className="run-card__stat">${run.estimated_cost_usd.toFixed(4)}</span>
        ) : null}
      </div>
      <div className="run-card__time">
        {formatTimestamp(run.started_at)}
      </div>
    </button>
  );
}
 
function formatDuration(ms: number): string {
  if (ms < 1000) return `${ms}ms`;
  const s = ms / 1000;
  if (s < 60) return `${s.toFixed(1)}s`;
  const m = Math.floor(s / 60);
  const rs = Math.round(s % 60);
  return `${m}m ${rs}s`;
}
 
function formatCount(n: number): string {
  if (n >= 1_000_000) return `${(n / 1_000_000).toFixed(1)}M`;
  if (n >= 1_000) return `${(n / 1_000).toFixed(1)}k`;
  return String(n);
}
 
function formatTimestamp(iso: string): string {
  try {
    const d = new Date(iso);
    return d.toLocaleString(undefined, {
      month: "short",
      day: "numeric",
      hour: "2-digit",
      minute: "2-digit",
      second: "2-digit",
    });
  } catch {
    return iso;
  }
}