All files / src/components/chat/HumanInput HumanInputRecord.tsx

100% Statements 16/16
83.33% Branches 40/48
100% Functions 5/5
100% Lines 15/15

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                                                2x 1x         16x       2x 2x                         2x                       16x         2x 2x 2x 2x 2x           2x   2x                                                               2x                                                              
import { formatDuration } from "@/components/chat/ExecutionPipeline/nodeMeta";
import { HITL_OUTCOME, StatusBadge } from "@/components/detail";
import {
  ClarifyQuestion,
  NodeHitl,
  answerLabel,
  tookRecommendation,
} from "@/objects/Hitl";
import { parseServerDate } from "@/util/time";
import { Hand, MessageCircleQuestion, ShieldCheck } from "lucide-react";
import React from "react";
 
/**
 * One time a run stopped for a person: what it asked (or the work it held for
 * approval), what they answered, how it ended and how long it waited.
 *
 * The same record in two places — inside the execution trace, on the stage
 * that paused, and in the transcript on the answer it shaped — so a reader
 * can see a human decided something without opening the trace, and see
 * exactly where in the pipeline they decided it when they do.
 */
 
/** How the pause is described, by where it came from. */
export function hitlHeading(hitl: NodeHitl): string {
  if (hitl.pause.interrupt_type === "approval_request") return "Approval requested";
  return hitl.pause.source === "scope"
    ? "Scope clarified before starting"
    : "Agent asked you";
}
 
const AnswerLine: React.FC<{
  question: ClarifyQuestion;
  values: string[] | undefined;
}> = ({ question, values }) => {
  const answered = !!values?.length;
  return (
    <li className="min-w-0">
      <p className="text-xs text-gray-500 dark:text-gray-400">
        {question.question}
      </p>
      <p
        className={`text-sm ${
          answered
            ? "text-gray-800 dark:text-gray-200"
            : "text-gray-400 dark:text-gray-500 italic"
        }`}
      >
        {answered
          ? values!.map((v) => answerLabel(question, v)).join(", ")
          : "Not answered"}
        {answered && tookRecommendation(question, values) && (
          <span className="ml-1.5 text-[10px] font-semibold uppercase tracking-wide text-indigo-600 dark:text-indigo-400">
            recommended
          </span>
        )}
      </p>
    </li>
  );
};
 
const HumanInputRecord: React.FC<{
  hitl: NodeHitl;
  /** Drop the heading — for places that already say what this is. */
  bare?: boolean;
}> = ({ hitl, bare = false }) => {
  const { pause, resume } = hitl;
  const approval = pause.interrupt_type === "approval_request";
  const outcome = resume?.outcome ?? "awaiting";
  const waited = resume?.waited_ms ? formatDuration(resume.waited_ms) : "";
  const answeredAt = resume?.answered_at
    ? parseServerDate(resume.answered_at).toLocaleString(undefined, {
        dateStyle: "medium",
        timeStyle: "short",
      })
    : "";
  const Icon = approval ? Hand : MessageCircleQuestion;
 
  return (
    <div className="rounded-lg border border-amber-200 dark:border-amber-900/60 bg-amber-50/60 dark:bg-amber-950/20 px-3 py-2 space-y-2 min-w-0">
      <div className="flex flex-wrap items-center gap-x-2 gap-y-1">
        {!bare && (
          <span className="flex items-center gap-1.5 text-xs font-semibold text-amber-800 dark:text-amber-300 min-w-0">
            <Icon className="w-3.5 h-3.5 shrink-0" aria-hidden />
            <span className="truncate">{hitlHeading(hitl)}</span>
          </span>
        )}
        <StatusBadge
          status={outcome}
          scale={HITL_OUTCOME}
          label={outcome === "awaiting" ? "waiting for you" : undefined}
        />
        {(waited || answeredAt) && (
          <span className="text-[10px] text-gray-500 dark:text-gray-400 tabular-nums">
            {waited && `waited ${waited}`}
            {waited && answeredAt && " · "}
            {answeredAt}
          </span>
        )}
      </div>
 
      {(pause.reason || (approval && pause.task)) && (
        <p className="text-xs text-gray-600 dark:text-gray-300">
          {approval ? pause.task : pause.reason}
        </p>
      )}
 
      {!approval && (pause.questions?.length ?? 0) > 0 && (
        <ul className="grid gap-1.5 sm:grid-cols-2">
          {pause.questions!.map((q) => (
            <AnswerLine key={q.id} question={q} values={resume?.answers?.[q.id]} />
          ))}
        </ul>
      )}
 
      {approval && resume?.feedback && (
        <p className="text-xs text-gray-700 dark:text-gray-300">
          <span className="text-gray-500 dark:text-gray-400">Feedback: </span>
          {resume.feedback}
        </p>
      )}
 
      {outcome === "skipped" && (
        <p className="text-xs text-gray-500 dark:text-gray-400">
          Skipped — the agent proceeded on its own reading and was told to state
          its assumptions.
        </p>
      )}
 
      {resume?.redacted && (
        <p className="flex items-center gap-1 text-[11px] text-gray-500 dark:text-gray-400">
          <ShieldCheck className="w-3 h-3 shrink-0" aria-hidden />
          A policy rule removed personal data from the answer before the agent
          saw it.
        </p>
      )}
    </div>
  );
};
 
export default HumanInputRecord;