All files / src/apps/user/pages/agents/skills/run SkillRunsList.tsx

69.23% Statements 9/13
30.43% Branches 7/23
60% Functions 3/5
69.23% Lines 9/13

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                                      2x           2x       2x 2x   1x   6x           2x 2x   2x                                                                                                                                                                                    
import {
  RUN_STATUS,
  SectionCard,
  Skeleton,
  StatusBadge,
} from "@/components/detail";
import { SkillRunSummary } from "@/objects/Skill";
import { QUERY_KEYS } from "@/services/query_keys";
import { useApiHandler } from "@/services/useApiHandler";
import { parseServerDate, timeAgo } from "@/util/time";
import { useQuery } from "@tanstack/react-query";
import { ChevronRight, Clock, History, Coins } from "lucide-react";
import React from "react";
import { Link } from "react-router-dom";
import { describeCost } from "@/components/cost";
import { skillRunHref } from "@/routes/resourceHref";
import { runDuration, summariseInputs } from "./runDisplay";
 
/** How often the list re-reads while a run is still going. */
const POLL_MS = 3000;
 
/**
 * The caller's runs of one skill, newest first. Runs are private to whoever
 * started them — the skill may be shared, the inputs given to it are not.
 */
const SkillRunsList: React.FC<{ skillId: string; className?: string }> = ({
  skillId,
  className,
}) => {
  const { getApiHandler } = useApiHandler();
  const { data, isLoading } = useQuery<SkillRunSummary[]>({
    queryKey: [QUERY_KEYS.GET.SKILL_RUNS, skillId],
    queryFn: () => getApiHandler().skill_router.listRuns(skillId),
    refetchInterval: (query) =>
      (query.state.data as SkillRunSummary[] | undefined)?.some(
        (r) => r.status === "running",
      )
        ? POLL_MS
        : false,
  });
  const runs: SkillRunSummary[] = data ?? [];
  const now = Date.now();
 
  return (
    <SectionCard
      icon={History}
      title="Your runs"
      subtitle={runs.length ? `${runs.length} most recent` : undefined}
      className={className}
      bodyClassName="p-0"
    >
      {isLoading ? (
        <div className="p-4 space-y-2">
          <Skeleton className="h-12 w-full" />
          <Skeleton className="h-12 w-full" />
        </div>
      ) : runs.length === 0 ? (
        <p className="px-5 py-6 text-sm text-center text-gray-400 dark:text-gray-500 italic">
          No runs yet. Fill in the form above and run the skill — each run is
          kept here with its inputs, outputs and what every node did.
        </p>
      ) : (
        <ul className="divide-y divide-gray-100 dark:divide-gray-700">
          {runs.map((run) => {
            const duration = runDuration(run, now);
            const tokens = run.input_tokens + run.output_tokens;
            return (
              <li key={run.id}>
                <Link
                  to={skillRunHref(skillId, run.id)}
                  className="flex items-center gap-3 px-5 py-3 hover:bg-gray-50 dark:hover:bg-gray-700/40 transition-colors"
                >
                  <StatusBadge
                    status={run.status}
                    scale={RUN_STATUS}
                    size="xs"
                    className="shrink-0 w-20 justify-center"
                  />
                  <div className="min-w-0 flex-1">
                    <p className="text-sm text-gray-800 dark:text-gray-200 truncate">
                      {summariseInputs(run.inputs, run.prompt)}
                    </p>
                    <p className="flex flex-wrap items-center gap-x-3 gap-y-0.5 mt-0.5 text-[11px] text-gray-400 dark:text-gray-500">
                      <span>{timeAgo(parseServerDate(run.started_at))}</span>
                      {duration && (
                        <span className="flex items-center gap-0.5">
                          <Clock className="size-3" />
                          {duration}
                        </span>
                      )}
                      {tokens > 0 && (
                        <span className="flex items-center gap-0.5">
                          <Coins className="size-3" />
                          {tokens.toLocaleString()} tokens
                        </span>
                      )}
                      {(run.cost_usd !== null || run.unpriced_calls > 0) && (
                        <span className="tabular-nums text-gray-500 dark:text-gray-400">
                          {
                            describeCost({
                              cost_usd: run.cost_usd,
                              unpriced: run.unpriced_calls,
                            }).text
                          }
                        </span>
                      )}
                      {run.status === "awaiting_input" && (
                        <span className="text-amber-600 dark:text-amber-400">
                          waiting for you
                        </span>
                      )}
                      {run.skill_version !== null && (
                        <span>v{run.skill_version}</span>
                      )}
                      {run.error && (
                        <span className="text-red-500 dark:text-red-400 truncate max-w-xs">
                          {run.error}
                        </span>
                      )}
                    </p>
                  </div>
                  <ChevronRight className="size-4 text-gray-300 dark:text-gray-600 shrink-0" />
                </Link>
              </li>
            );
          })}
        </ul>
      )}
    </SectionCard>
  );
};
 
export default SkillRunsList;