EvidencePanel.tsx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. "use client";
  2. import { useEffect, useMemo, useState } from "react";
  3. import { Braces, Database, FileCheck2, X } from "lucide-react";
  4. import { fetchArtifact } from "@/lib/api";
  5. import type { JourneyStep, JourneyView, RunSummary } from "@/lib/types";
  6. export function EvidencePanel({
  7. journey,
  8. run,
  9. step,
  10. onClose,
  11. }: {
  12. journey: JourneyView;
  13. run: RunSummary | null;
  14. step: JourneyStep | null;
  15. onClose: () => void;
  16. }) {
  17. const [artifact, setArtifact] = useState<Record<string, unknown> | null>(null);
  18. const [artifactError, setArtifactError] = useState<string | null>(null);
  19. const artifactId = useMemo(() => firstArtifactId(step), [step]);
  20. useEffect(() => {
  21. setArtifact(null);
  22. setArtifactError(null);
  23. }, [step?.step_id]);
  24. if (!step) {
  25. return (
  26. <aside className="evidence-panel">
  27. <PanelHeader title="原始证据" onClose={onClose} />
  28. <div className="evidence-empty">先在左侧选择一个创作步骤。</div>
  29. </aside>
  30. );
  31. }
  32. async function loadArtifact() {
  33. if (artifactId === null) return;
  34. try {
  35. setArtifact(await fetchArtifact(journey.script_build_id, artifactId));
  36. } catch (reason) {
  37. setArtifactError(reason instanceof Error ? reason.message : "无法读取 Artifact");
  38. }
  39. }
  40. return (
  41. <aside className="evidence-panel">
  42. <PanelHeader title={step.title} onClose={onClose} />
  43. <div className="evidence-scroll">
  44. <section className="evidence-summary">
  45. <span>{step.process.actor}</span>
  46. <p>{step.reasoning}</p>
  47. <dl>
  48. <div><dt>Step</dt><dd>#{step.sequence}</dd></div>
  49. <div><dt>Root Trace</dt><dd>{journey.root_trace_id}</dd></div>
  50. <div><dt>Task</dt><dd>{step.task_id}</dd></div>
  51. {step.attempt_id ? <div><dt>Attempt</dt><dd>{step.attempt_id}</dd></div> : null}
  52. {step.validation_id ? <div><dt>Validation</dt><dd>{step.validation_id}</dd></div> : null}
  53. {step.decision_id ? <div><dt>Decision</dt><dd>{step.decision_id}</dd></div> : null}
  54. <div><dt>Input</dt><dd>{journey.input_summary.snapshot_id}</dd></div>
  55. <div><dt>Digest</dt><dd>{journey.input_summary.digest}</dd></div>
  56. {run?.started_at ? <div><dt>开始时间</dt><dd>{run.started_at}</dd></div> : null}
  57. {run?.completed_at ? <div><dt>结束时间</dt><dd>{run.completed_at}</dd></div> : null}
  58. </dl>
  59. </section>
  60. <EvidenceBlock
  61. icon={Braces}
  62. title="Process 运行元数据"
  63. value={{
  64. actor: step.process.actor,
  65. tool_name: step.process.tool_name,
  66. trace_id: step.process.trace_id,
  67. details: step.process.details,
  68. model: step.model,
  69. tokens: step.tokens,
  70. cost: step.cost,
  71. duration_ms: step.duration_ms,
  72. }}
  73. />
  74. {step.evidence.contract ? (
  75. <EvidenceBlock icon={FileCheck2} title="冻结的 TaskContract" value={step.evidence.contract} />
  76. ) : null}
  77. {step.evidence.tool_calls.length ? (
  78. <EvidenceBlock icon={Braces} title="真实工具调用" value={step.evidence.tool_calls} />
  79. ) : null}
  80. {step.evidence.validation ? (
  81. <EvidenceBlock icon={FileCheck2} title="Validation 结果" value={step.evidence.validation} />
  82. ) : null}
  83. {step.evidence.decision ? (
  84. <EvidenceBlock icon={FileCheck2} title="PlannerDecision" value={step.evidence.decision} />
  85. ) : null}
  86. {artifactId !== null ? (
  87. <section className="artifact-reader">
  88. <header><Database size={15} /><strong>Artifact 内容</strong></header>
  89. {artifact ? <pre>{JSON.stringify(artifact, null, 2)}</pre> : (
  90. <button type="button" onClick={loadArtifact}>从 Host 读取 Artifact #{artifactId}</button>
  91. )}
  92. {artifactError ? <p role="alert">{artifactError}</p> : null}
  93. </section>
  94. ) : null}
  95. </div>
  96. </aside>
  97. );
  98. }
  99. function PanelHeader({ title, onClose }: { title: string; onClose: () => void }) {
  100. return (
  101. <header className="evidence-header">
  102. <div><span>原始证据</span><h2>{title}</h2></div>
  103. <button type="button" onClick={onClose} aria-label="关闭证据面板"><X size={18} /></button>
  104. </header>
  105. );
  106. }
  107. function EvidenceBlock({ icon: Icon, title, value }: { icon: typeof FileCheck2; title: string; value: unknown }) {
  108. return (
  109. <details className="evidence-block" open>
  110. <summary><Icon size={15} /><span>{title}</span></summary>
  111. <pre>{JSON.stringify(value, null, 2)}</pre>
  112. </details>
  113. );
  114. }
  115. function firstArtifactId(step: JourneyStep | null): number | null {
  116. for (const item of step?.evidence.artifact_refs ?? []) {
  117. const uri = typeof item.uri === "string" ? item.uri : "";
  118. const match = uri.match(/^script-build:\/\/artifact-versions\/(\d+)$/);
  119. if (match) return Number(match[1]);
  120. }
  121. return null;
  122. }