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 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 | 2x 15x 2x 2x 2x 2x 2x 18x 2x 11x 2x 11x 2x 8x 8x 8x 2x 8x 2x 8x 8x 2x 8x 8x 2x 2x 1x 1x 1x 1x 1x 1x 1x 2x 1x 1x 1x 1x 1x 2x 2x 8x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 2x 4x 2x 2x 2x 2x 2x 3x 9x 2x 8x 8x 2x 2x 2x 2x 2x 70x 70x 70x 2x 2x 12x 2x 12x 12x 70x 2x 32x 32x 32x 16x 32x 32x 12x | import { kindLabel } from "@/components/chat/ExecutionPipeline/nodeMeta";
import QualityCheckBadge from "@/components/chat/QualityCheck/QualityCheckBadge";
import HumanInputRecord from "@/components/chat/HumanInput/HumanInputRecord";
import RichMarkdown from "@/components/chat/RichMarkdown/RichMarkdown";
import { formatUsd } from "@/components/cost";
import {
DECISION_SECTION,
DECISION_VERDICT,
DetailPage,
encodeResourceId,
MetaGrid,
MetaItem,
REVIEW_STATUS,
RUN_STATUS,
SectionCard,
StatusBadge,
useResourceId,
} from "@/components/detail";
import RuleEventRow from "@/components/governance/RuleEventRow";
import AddToDatasetModal from "@/components/decisions/AddToDatasetModal";
import VerdictModal from "@/components/decisions/VerdictModal";
import {
AnswerSection,
ConfigurationSection,
CostSection,
Decision,
DecisionLaneStep,
DecisionSection,
DecisionSectionKey,
HumanInputSection,
ModelCallsSection,
OutcomeCreate,
OutcomeSection,
QualityCheckSection,
ReadsSection,
RequestSection,
ReviewsSection,
RulesSection,
StepsSection,
ToolCallsSection,
VERDICT_LABELS,
WebSourcesSection,
} from "@/objects/Decision";
import type { WebSearchOutcome, WebSearchRuleEffect } from "@/objects/WebSearch";
import {
describeEngines,
describeSearch,
FAILURE_LABELS,
} from "@/util/webSearchReport";
import { exchangeToNodeHitl } from "@/objects/Hitl";
import { APP_ROUTES, ROUTE_PARAMS } from "@/routes/const";
import {
chatMessageHref,
skillRunHref,
swarmRunHref,
} from "@/routes/resourceHref";
import { QUERY_KEYS } from "@/services/query_keys";
import { useApiHandler } from "@/services/useApiHandler";
import { parseServerDate } from "@/util/time";
import { errorDetail } from "@/util/apiError";
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import {
ClipboardCheck,
Coins,
Cpu,
ExternalLink,
Eye,
Database,
FileText,
Flag,
Gavel,
Globe,
ListTree,
MessageSquare,
ScanSearch,
Settings2,
ShieldCheck,
UserCheck,
Wrench,
} from "lucide-react";
import React, { useState } from "react";
import { Link } from "react-router-dom";
/**
* The decision record: one governed turn — an answer or a skill run — and
* everything it recorded, in one place. What "Why did it do this?" opens from
* an answer, a skill run, a review flag or a Governance → Rules → Activity
* card.
*
* Every section comes from svc-core with a status, and the page shows it
* whenever it is not simply "recorded": a turn that called no tool says so,
* and a turn from before something was recorded says that instead of
* showing an empty list. A section the page does not know yet still shows
* its title, status and note.
*/
const when = (value?: string | null): string =>
value ? parseServerDate(value).toLocaleString() : "—";
const STATUS_LABELS: Record<string, string> = {
empty: "None",
not_recorded: "Not recorded",
unavailable: "Unavailable",
};
const TEXT_BLOCK =
"text-sm text-gray-700 dark:text-gray-200 bg-gray-50 dark:bg-gray-900/40 rounded-lg p-3 whitespace-pre-wrap break-words";
const MUTED = "text-xs text-gray-500 dark:text-gray-400";
const Json: React.FC<{ value: unknown }> = ({ value }) => (
<pre className="text-xs text-gray-700 dark:text-gray-200 bg-gray-50 dark:bg-gray-900/40 rounded-lg p-3 overflow-x-auto">
{JSON.stringify(value, null, 2)}
</pre>
);
const Label: React.FC<{ children: React.ReactNode }> = ({ children }) => (
<p className="mb-1 text-[11px] font-medium uppercase tracking-wide text-gray-400 dark:text-gray-500">
{children}
</p>
);
const tokens = (input?: number | null, output?: number | null): string =>
input == null && output == null
? "—"
: `${(input ?? 0).toLocaleString()} in · ${(output ?? 0).toLocaleString()} out`;
const ms = (value?: number | null): string =>
value == null ? "—" : value >= 1000 ? `${(value / 1000).toFixed(1)} s` : `${value} ms`;
// ─── Sections ─────────────────────────────────────────────────────────────────
const Request: React.FC<{ section: RequestSection }> = ({ section }) => {
const line =
typeof section.command?.line === "string" ? section.command.line : null;
return (
<div className="space-y-3">
{line && (
<div>
<Label>Command</Label>
<code className="text-xs font-mono text-gray-700 dark:text-gray-200">{line}</code>
</div>
)}
{section.prompt && (
<div>
<Label>Question</Label>
<p className={TEXT_BLOCK}>{section.prompt}</p>
</div>
)}
{section.attachments && section.attachments.length > 0 && (
<div>
<Label>Documents attached</Label>
<ul className="text-sm text-gray-700 dark:text-gray-200 space-y-0.5">
{section.attachments.map((a) => (
<li key={a.id}>{a.name}</li>
))}
</ul>
</div>
)}
{section.task && section.task !== section.prompt && (
<div>
<Label>Task as the skill was given it</Label>
<p className={TEXT_BLOCK}>{section.task}</p>
</div>
)}
{section.inputs && Object.keys(section.inputs).length > 0 && (
<div>
<Label>Inputs</Label>
<Json value={section.inputs} />
</div>
)}
</div>
);
};
const Answer: React.FC<{ section: AnswerSection }> = ({ section }) => (
<div className="space-y-3">
<div className="flex items-center gap-2 flex-wrap">
{section.outcome && (
<StatusBadge status={section.outcome} scale={RUN_STATUS} size="xs" />
)}
{section.stopped && <span className={MUTED}>Stopped by the person.</span>}
{section.blocked_by && (
<span className={MUTED}>Refused by the rule “{section.blocked_by}”.</span>
)}
</div>
{section.content && (
<div className="rounded-lg border border-gray-100 dark:border-gray-700 px-4 py-3">
<RichMarkdown content={section.content} />
</div>
)}
{section.outputs && Object.keys(section.outputs).length > 0 && (
<div>
<Label>Outputs</Label>
<Json value={section.outputs} />
</div>
)}
{section.error && <p className="text-sm text-red-600 dark:text-red-400">{section.error}</p>}
</div>
);
const Configuration: React.FC<{ section: ConfigurationSection }> = ({
section,
}) => {
const agentPage = section.agent_id
? APP_ROUTES.USER.INFERENCE.AGENTS.id.replace(
ROUTE_PARAMS.AGENT.id,
encodeResourceId(section.agent_id),
)
: null;
return (
<MetaGrid columns={3}>
{section.agent_id && (
<MetaItem
label="Agent"
value={
agentPage ? (
<Link
// The revision that answered, opened in the agent's history.
to={
section.agent_version != null
? `${agentPage}?version=${section.agent_version}`
: agentPage
}
className="hover:underline"
>
{section.agent_name ?? "Deleted agent"}
{section.agent_version != null ? ` · v${section.agent_version}` : ""}
</Link>
) : (
"—"
)
}
/>
)}
{section.skill_id && (
<MetaItem
label="Skill"
value={
<Link
to={APP_ROUTES.USER.INFERENCE.SKILLS.id.replace(
ROUTE_PARAMS.SKILL.id,
encodeResourceId(section.skill_id),
)}
className="hover:underline"
>
{section.skill_name ?? "Skill"}
{section.skill_version != null ? ` · v${section.skill_version}` : ""}
</Link>
}
/>
)}
{(section.model_name || section.ai_model_id) && (
<MetaItem
label="Model"
value={
section.lighter_model?.answered
? `${section.model_name ?? "—"} (lighter model, a simple request)`
: (section.model_name ?? "—")
}
/>
)}
{section.cost_profile && (
<MetaItem
label="Cost profile"
value={
section.cost_profile.charAt(0).toUpperCase() +
section.cost_profile.slice(1)
}
/>
)}
</MetaGrid>
);
};
const Rules: React.FC<{ section: RulesSection }> = ({ section }) => (
<div className="space-y-2">
{section.events.map((event) => (
<RuleEventRow key={event.id} event={event} />
))}
</div>
);
/** What a turn's web searches came to, as the record opens it. */
const WEB_OUTCOME_LINES: Record<WebSearchOutcome, string> = {
closed: "The workspace's rules left no web source to search.",
limited: "The workspace's Tools rules narrowed what the searches could use.",
found: "The searches found results.",
unavailable: "No search engine could answer.",
empty: "The searches found nothing usable.",
};
const ruleEffect = (effect: WebSearchRuleEffect): string => {
Iif (!effect.acted) return "changed none of the searches";
Iif (effect.closed) return "left no web source to search";
const parts: string[] = [];
if (effect.left_out?.length) parts.push(`left out ${effect.left_out.join(", ")}`);
else Eif (effect.left_out_count) parts.push(`left out ${effect.left_out_count} site(s)`);
else if (effect.narrowed) parts.push("kept the searches to its sources");
Iif (effect.put_first?.length) parts.push(`put ${effect.put_first.join(", ")} first`);
else Iif (effect.put_first_count) parts.push("put its sources first");
return parts.join("; ");
};
const WebSources: React.FC<{ section: WebSourcesSection }> = ({ section }) => {
const summary = section.summary;
const failures = Object.entries(summary?.failures ?? {});
return (
<div className="space-y-3">
{summary && (
<div className="space-y-1.5">
<p className="text-sm text-gray-700 dark:text-gray-200">
{WEB_OUTCOME_LINES[summary.outcome]}
</p>
{failures.length > 0 && (
<p className={MUTED}>
Engines that could not answer:{" "}
{failures
.map(([reason, n]) => `${FAILURE_LABELS[reason] ?? FAILURE_LABELS.error} (${n})`)
.join("; ")}
.
</p>
)}
{summary.rules.length > 0 && (
<ul className="space-y-0.5">
{summary.rules.map((effect, i) => (
<li key={`${effect.rule_id ?? i}-${effect.action}`} className="text-sm">
<span className="font-medium text-gray-900 dark:text-gray-100">
{effect.rule ?? "A Tools rule"}
</span>{" "}
<span className="text-gray-600 dark:text-gray-300">{ruleEffect(effect)}</span>
</li>
))}
</ul>
)}
</div>
)}
<ul className="space-y-2">
{section.searches.map((search, i) => (
<li
key={i}
className="rounded-lg border border-gray-100 dark:border-gray-700 px-3 py-2 space-y-0.5"
>
<div className="flex items-center gap-2 flex-wrap text-sm">
<span className="font-mono text-gray-900 dark:text-gray-100">
{search.query ?? "web_search"}
</span>
{search.report && (
<span className={MUTED}>
{[search.report.search_type, search.report.freshness && `past ${search.report.freshness}`]
.filter(Boolean)
.join(" · ")}
</span>
)}
</div>
{search.report ? (
<>
<p className="text-xs text-gray-600 dark:text-gray-300">
{describeSearch(search.report)}
</p>
{describeEngines(search.report) && (
<p className={MUTED}>{describeEngines(search.report)}</p>
)}
</>
) : (
<p className={MUTED}>Recorded before searches kept a report.</p>
)}
</li>
))}
</ul>
</div>
);
};
const QualityCheck: React.FC<{ section: QualityCheckSection }> = ({
section,
}) => (
<div className="space-y-2">
{section.outcome && (
<div className="flex items-center gap-2">
<QualityCheckBadge outcome={section.outcome} score={section.score ?? undefined} />
{section.score != null && (
<span className={MUTED}>Score {section.score}</span>
)}
</div>
)}
{section.critique && <p className="text-sm text-gray-700 dark:text-gray-200">{section.critique}</p>}
{section.gaps.length > 0 && (
<div>
<Label>Gaps it named</Label>
<ul className="list-disc pl-5 text-sm text-gray-700 dark:text-gray-200 space-y-0.5">
{section.gaps.map((gap, i) => (
<li key={i}>{gap}</li>
))}
</ul>
</div>
)}
</div>
);
const HumanInput: React.FC<{ section: HumanInputSection }> = ({ section }) => (
<div className="space-y-2">
{section.exchanges.map((exchange, i) => (
<HumanInputRecord key={i} hitl={exchangeToNodeHitl(exchange)} />
))}
{section.pending && (
<p className="text-sm text-amber-700 dark:text-amber-300">
Waiting for an answer
{typeof section.pending.message === "string" && section.pending.message
? `: ${section.pending.message}`
: "."}
</p>
)}
</div>
);
const Reviews: React.FC<{ section: ReviewsSection }> = ({ section }) => (
<ul className="space-y-2">
{section.flags.map((flag) => (
<li
key={flag.id}
className="rounded-lg border border-gray-100 dark:border-gray-700 px-3 py-2"
>
<div className="flex items-center gap-2 flex-wrap">
<span className="text-sm font-medium text-gray-900 dark:text-gray-100">
{flag.rule_name ?? "Deleted rule"}
</span>
<StatusBadge status={flag.status} scale={REVIEW_STATUS} size="xs" />
<span className={MUTED}>raised {when(flag.created_at)}</span>
</div>
{flag.reviewed_at && (
<p className="mt-1 text-xs text-gray-600 dark:text-gray-300">
Decided {when(flag.reviewed_at)}
{flag.reviewer_note ? ` — “${flag.reviewer_note}”` : ""}
</p>
)}
</li>
))}
</ul>
);
const BUTTON =
"inline-flex items-center gap-1.5 text-xs px-2.5 py-1.5 rounded-lg border border-gray-200 dark:border-gray-600 text-gray-600 dark:text-gray-300 hover:bg-gray-50 dark:hover:bg-gray-700 transition-colors disabled:opacity-40";
/** The person's verdict, every earlier one, the cases it became — and the
* controls to give one and to add the turn to a dataset. Drawn before any
* verdict too: that is when the controls matter. */
const Outcome: React.FC<{ section: OutcomeSection; decision: Decision }> = ({
section,
decision,
}) => {
const { getApiHandler } = useApiHandler();
const queryClient = useQueryClient();
const [giving, setGiving] = useState(false);
const [adding, setAdding] = useState(false);
const current = section.current ?? null;
const answer =
(decision.sections.find((s) => s.key === "answer") as AnswerSection | undefined)
?.content ?? "";
const record = useMutation({
mutationFn: (body: OutcomeCreate) =>
getApiHandler().decision_router.recordOutcome(decision.id, body),
onSuccess: () => {
setGiving(false);
void queryClient.invalidateQueries({ queryKey: [QUERY_KEYS.GET.DECISION] });
},
});
const earlier = section.history.slice(0, -1).reverse();
return (
<div className="space-y-3">
{current && (
<div className="space-y-2">
<div className="flex items-center gap-2 flex-wrap">
<StatusBadge
status={current.verdict}
scale={DECISION_VERDICT}
label={VERDICT_LABELS[current.verdict]}
size="xs"
/>
{current.label && (
<span className="text-[11px] px-2 py-0.5 rounded-full border border-gray-200 dark:border-gray-600 text-gray-600 dark:text-gray-300">
{current.label}
</span>
)}
<span className={MUTED}>
{current.actor_name ?? "Someone"}, {when(current.created_at)}
{current.source === "review" ? ", resolving a review" : ""}
</span>
</div>
{current.corrected_text && (
<div>
<Label>What it should have said</Label>
<p className={TEXT_BLOCK}>{current.corrected_text}</p>
</div>
)}
{current.note && <p className="text-sm text-gray-700 dark:text-gray-200">{current.note}</p>}
</div>
)}
{earlier.length > 0 && (
<details>
<summary className="cursor-pointer text-xs text-gray-500 dark:text-gray-400">
Earlier verdicts ({earlier.length})
</summary>
<ul className="mt-1 space-y-1">
{earlier.map((o) => (
<li key={o.id} className={MUTED}>
{VERDICT_LABELS[o.verdict]} by {o.actor_name ?? "someone"}, {when(o.created_at)}
</li>
))}
</ul>
</details>
)}
{section.cases.length > 0 && (
<div>
<Label>Added to</Label>
<ul className="space-y-0.5">
{section.cases.map((c) => (
<li key={c.case_id} className="text-sm">
<Link
to={APP_ROUTES.USER.GOVERNANCE.EVALUATION.DATASETS.id.replace(
ROUTE_PARAMS.DATASET.id,
encodeResourceId(c.test_suite_id),
)}
className="hover:underline"
>
{c.test_suite_name ?? "A dataset"}
</Link>
<span className={MUTED}> {when(c.created_at)}</span>
</li>
))}
</ul>
</div>
)}
<div className="flex items-center gap-2 flex-wrap">
<button type="button" className={BUTTON} onClick={() => setGiving(true)}>
<Gavel className="size-3.5" />
{current ? "Change the verdict" : "Give a verdict"}
</button>
<button
type="button"
className={BUTTON}
disabled={!current}
title={current ? undefined : "Give it a verdict first"}
onClick={() => setAdding(true)}
>
<Database className="size-3.5" />
Add to dataset
</button>
</div>
{giving && (
<VerdictModal
initial={current?.verdict ?? "accepted"}
answer={current?.corrected_text ?? answer}
busy={record.isPending}
error={record.isError ? errorDetail(record.error, "Could not record that verdict.") : null}
onSubmit={(body) => record.mutate(body)}
onClose={() => {
setGiving(false);
record.reset();
}}
/>
)}
{adding && current && (
<AddToDatasetModal
invocationId={decision.id}
verdict={current.verdict}
onClose={() => setAdding(false)}
/>
)}
</div>
);
};
const plural = (n: number, one: string, many = `${one}s`): string =>
`${n} ${n === 1 ? one : many}`;
/** A knowledge lane's search, from its queries to what the join packed. */
const LaneLine: React.FC<{ lane: DecisionLaneStep }> = ({ lane }) => {
const funnel = [
plural(lane.knowledge_bases, "knowledge base"),
plural(lane.queries, "query", "queries"),
`${lane.found} found`,
`${lane.judged} judged`,
`${lane.ruled_out} ruled out`,
...(lane.out_of_window ? [`${lane.out_of_window} out of window`] : []),
`${lane.handed_on} handed on`,
...(lane.packed != null ? [`${lane.packed} packed`] : []),
];
return (
<div className="mt-1 text-xs text-gray-600 dark:text-gray-300">
<p>
<span className="font-medium">
{[kindLabel(lane.category), lane.role].filter(Boolean).join(" · ")}
</span>
{" — "}
{funnel.join(" → ")}
</p>
{lane.judge_failure && (
<p className="mt-0.5 text-amber-700 dark:text-amber-300">
Relevance judge failed ({lane.judge_failure}): its passages were kept unjudged.
</p>
)}
{lane.failure && (
<p className="mt-0.5 text-amber-700 dark:text-amber-300">
The search failed ({lane.failure}).
</p>
)}
</div>
);
};
const Steps: React.FC<{ section: StepsSection }> = ({ section }) => (
<ol className="space-y-2">
{section.steps.map((step, i) => (
<li
key={`${step.node_id}-${i}`}
className="rounded-lg border border-gray-100 dark:border-gray-700 px-3 py-2"
>
<div className="flex items-center gap-2 flex-wrap text-sm">
<span className="font-mono text-gray-900 dark:text-gray-100">{step.node_id}</span>
{step.kind === "system" && <span className={MUTED}>platform step</span>}
{step.error && <StatusBadge status="failed" scale={RUN_STATUS} size="xs" />}
<span className={MUTED}>
{[step.model_name, tokens(step.input_tokens, step.output_tokens), ms(step.duration_ms)]
.filter((part) => part && part !== "—")
.join(" · ")}
</span>
</div>
{step.lane && <LaneLine lane={step.lane} />}
{step.tools.length > 0 && (
<p className="mt-1 text-xs text-gray-600 dark:text-gray-300">
Tools: {step.tools.join(", ")}
</p>
)}
{!!step.history_turns_omitted && (
<p className="mt-1 text-xs text-amber-700 dark:text-amber-300">
The {step.history_turns_omitted} earliest{" "}
{step.history_turns_omitted === 1 ? "turn" : "turns"} of the
conversation were not sent: it is longer than this model takes in
one request.
</p>
)}
{step.output_preview && (
<p className="mt-1 text-xs text-gray-500 dark:text-gray-400 line-clamp-3 whitespace-pre-wrap">
{step.output_preview}
</p>
)}
</li>
))}
</ol>
);
const ModelCalls: React.FC<{ section: ModelCallsSection }> = ({ section }) => (
<div className="overflow-x-auto">
<table className="w-full text-xs">
<thead>
<tr className="text-left text-gray-400 dark:text-gray-500">
<th className="py-1 pr-3 font-medium">Model</th>
<th className="py-1 pr-3 font-medium">Step</th>
<th className="py-1 pr-3 font-medium">Tokens</th>
<th className="py-1 pr-3 font-medium">Time</th>
<th className="py-1 pr-3 font-medium">Cost</th>
</tr>
</thead>
<tbody className="text-gray-700 dark:text-gray-200">
{section.calls.map((call, i) => (
<tr key={i} className="border-t border-gray-100 dark:border-gray-700">
<td className="py-1.5 pr-3">
{call.model_display_name ?? call.model_name}
{call.status !== "success" && (
<span className="ml-1 text-red-600 dark:text-red-400">({call.status})</span>
)}
</td>
<td className="py-1.5 pr-3 font-mono">{call.node ?? "—"}</td>
<td className="py-1.5 pr-3">{tokens(call.prompt_tokens, call.completion_tokens)}</td>
<td className="py-1.5 pr-3">{ms(call.latency_ms)}</td>
<td className="py-1.5 pr-3">
{call.estimated_cost_usd != null ? formatUsd(call.estimated_cost_usd) : "Unpriced"}
</td>
</tr>
))}
</tbody>
</table>
</div>
);
const ToolCalls: React.FC<{ section: ToolCallsSection }> = ({ section }) => (
<ul className="space-y-2">
{section.calls.map((call, i) => (
<li
key={i}
className="rounded-lg border border-gray-100 dark:border-gray-700 px-3 py-2 space-y-1.5"
>
<div className="flex items-center gap-2 flex-wrap text-sm">
<span className="font-mono text-gray-900 dark:text-gray-100">{call.tool_name}</span>
{call.status !== "success" && (
<StatusBadge status="failed" scale={RUN_STATUS} label={call.status} size="xs" />
)}
<span className={MUTED}>{ms(call.latency_ms)}</span>
</div>
{call.input_args && Object.keys(call.input_args).length > 0 && (
<details>
<summary className="cursor-pointer text-xs text-gray-500 dark:text-gray-400">
What it was asked
</summary>
<Json value={call.input_args} />
</details>
)}
{call.output && (
<details>
<summary className="cursor-pointer text-xs text-gray-500 dark:text-gray-400">
What it returned{call.truncated ? " (cut for the record)" : ""}
</summary>
<p className={TEXT_BLOCK}>{call.output}</p>
</details>
)}
{call.citations.length > 0 && (
<ul className="space-y-0.5">
{call.citations.map((citation) => (
<li key={citation.index} className="text-xs">
{citation.source_type === "url" ? (
<a
href={citation.source}
target="_blank"
rel="noreferrer"
className="text-blue-600 dark:text-blue-400 hover:underline"
>
[{citation.index}] {citation.title}
</a>
) : (
<span className="text-gray-600 dark:text-gray-300">
[{citation.index}] {citation.title}
</span>
)}
</li>
))}
</ul>
)}
</li>
))}
</ul>
);
const Cost: React.FC<{ section: CostSection }> = ({ section }) => (
<MetaGrid columns={3}>
<MetaItem
label="Cost"
value={
section.cost_usd != null
? `${formatUsd(section.cost_usd)}${section.complete ? "" : " or more"}`
: "Unpriced"
}
/>
<MetaItem label="Tokens" value={tokens(section.input_tokens, section.output_tokens)} />
<MetaItem
label="Model calls"
value={section.entries.reduce((sum, entry) => sum + entry.llm_calls, 0).toLocaleString()}
/>
</MetaGrid>
);
const Reads: React.FC<{ section: ReadsSection }> = ({ section }) => (
<ul className="space-y-1">
{section.reads.map((read, i) => (
<li key={i} className="text-sm text-gray-700 dark:text-gray-200">
{read.reader_name ?? "A deleted account"}{" "}
<span className={MUTED}>
{read.access === "reviewer" ? "as a reviewer" : read.access}, {when(read.read_at)}
</span>
</li>
))}
</ul>
);
/** How each section is drawn, by key. A key svc-core adds before this page
* knows it falls back to its title, status and note. `always` draws the
* body whatever the status — a section that holds controls as well as a
* record. */
const RENDERERS: {
[K in DecisionSectionKey]: {
icon: React.ElementType;
Body: React.FC<{
section: Extract<DecisionSection, { key: K }>;
decision: Decision;
}>;
always?: boolean;
};
} = {
request: { icon: MessageSquare, Body: Request },
answer: { icon: FileText, Body: Answer },
configuration: { icon: Settings2, Body: Configuration },
rules: { icon: ShieldCheck, Body: Rules },
web_sources: { icon: Globe, Body: WebSources },
quality_check: { icon: ClipboardCheck, Body: QualityCheck },
human_input: { icon: UserCheck, Body: HumanInput },
reviews: { icon: Flag, Body: Reviews },
outcome: { icon: Gavel, Body: Outcome, always: true },
steps: { icon: ListTree, Body: Steps },
model_calls: { icon: Cpu, Body: ModelCalls },
tool_calls: { icon: Wrench, Body: ToolCalls },
cost: { icon: Coins, Body: Cost },
reads: { icon: Eye, Body: Reads },
};
export const DecisionSectionCard: React.FC<{
section: DecisionSection;
decision: Decision;
}> = ({ section, decision }) => {
const renderer = RENDERERS[section.key as DecisionSectionKey] as
| {
icon: React.ElementType;
Body: React.FC<{ section: DecisionSection; decision: Decision }>;
always?: boolean;
}
| undefined;
const recorded = section.status === "recorded";
return (
<SectionCard
icon={renderer?.icon}
title={section.title}
className="mb-4"
action={
recorded ? undefined : (
<StatusBadge
status={section.status}
scale={DECISION_SECTION}
label={STATUS_LABELS[section.status]}
size="xs"
/>
)
}
>
{section.note && <p className={`${MUTED} ${recorded ? "mb-3" : ""}`}>{section.note}</p>}
{(recorded || renderer?.always) && renderer && (
<renderer.Body section={section} decision={decision} />
)}
</SectionCard>
);
};
// ─── Page ─────────────────────────────────────────────────────────────────────
/** What kind of turn it is, and where its page is called. */
const KIND_WORDS: Record<Decision["kind"], { what: string; open: string }> = {
answer: { what: "An answer", open: "Open the answer in its chat" },
skill_run: { what: "A skill run", open: "Open the run" },
swarm_task: { what: "A task of a swarm run", open: "Open the swarm run" },
};
/** Where the turn itself lives: the answer in its chat, the skill run, or
* the swarm run the task belongs to. */
const openHref = (decision: Decision): string | null =>
decision.kind === "swarm_task"
? decision.swarm_run_id
? swarmRunHref(decision.swarm_run_id)
: null
: decision.kind === "skill_run"
? decision.skill_id && decision.skill_run_id
? skillRunHref(decision.skill_id, decision.skill_run_id)
: null
: chatMessageHref({
agentId: decision.agent_id,
modelId: decision.ai_model_id,
conversationId: decision.conversation_id,
messageId: decision.message_id,
});
const DecisionBody: React.FC<{ decision: Decision }> = ({ decision }) => {
const open = openHref(decision);
return (
<>
{decision.access === "reviewer" && (
<div
role="note"
className="mb-4 rounded-lg border border-amber-200 dark:border-amber-900/60 bg-amber-50 dark:bg-amber-950/20 px-3 py-2 text-xs text-amber-800 dark:text-amber-200"
>
You are reading this as the reviewer of a flag raised on it. Your read
has been logged, and its owner can see it.
</div>
)}
<SectionCard title="This turn" className="mb-4">
<MetaGrid columns={3}>
<MetaItem
label="What"
value={(KIND_WORDS[decision.kind] ?? KIND_WORDS.answer).what}
/>
<MetaItem
label="Outcome"
value={<StatusBadge status={decision.status} scale={RUN_STATUS} size="xs" />}
/>
<MetaItem
label="When"
value={when(decision.started_at ?? decision.finished_at)}
/>
</MetaGrid>
{open && (
<Link
to={open}
className="mt-4 inline-flex items-center gap-1.5 text-xs text-gray-500 dark:text-gray-400 hover:text-gray-800 dark:hover:text-gray-200"
>
<ExternalLink className="size-3.5" />
{(KIND_WORDS[decision.kind] ?? KIND_WORDS.answer).open}
</Link>
)}
</SectionCard>
{decision.sections.map((section) => (
<DecisionSectionCard key={section.key} section={section} decision={decision} />
))}
</>
);
};
const DecisionViewer: React.FC = () => {
const decisionId = useResourceId("decisionId");
const { getApiHandler } = useApiHandler();
const { data, isLoading, error } = useQuery<Decision | null>({
queryKey: [QUERY_KEYS.GET.DECISION, decisionId],
queryFn: () => getApiHandler().decision_router.get(decisionId!),
enabled: !!decisionId,
});
// Bound to an explicit type: the pinned TypeScript cannot infer react-query
// v5's return type. See docs/guides/frontend-components.md.
const decision: Decision | null = data ?? null;
return (
<DetailPage<Decision>
resource={decisionId ? decision : null}
loading={!!decisionId && isLoading}
error={error}
label="decision record"
title={decision ? `Why ${decision.subject ?? "it"} did this` : undefined}
description="Everything this turn recorded: what was asked, what answered, the rules that ran, the checks, the people involved, the calls it made and what it cost."
icon={<ScanSearch className="size-5" />}
back={APP_ROUTES.USER.GOVERNANCE.activity}
// Not a resource anybody edits: a record read back from audit rows
// (messages, governance events, flags, the ledger, svc-ai's traces),
// none of which is ever changed.
history={false}
resourceId={decisionId}
>
{(d) => <DecisionBody decision={d} />}
</DetailPage>
);
};
export default DecisionViewer;
|