Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

V2ItemInspector.tsx

298 lines11.5 KB

1import type {
2 EnvironmentId,
3 OrchestrationV2ProjectedTurnItem,
4 RunId,
5 ThreadId,
6} from "@t3tools/contracts";
7import { ExternalLinkIcon, GitBranchIcon, RotateCcwIcon } from "lucide-react";
8import { memo, type ReactNode } from "react";
9​
10import { useV2ItemSupport } from "../../state/v2ItemSupport";
11import { formatWorkspaceRelativePath } from "../../filePathDisplay";
12import { Button } from "../ui/button";
13import ChatMarkdown from "../ChatMarkdown";
14​
15interface V2ItemInspectorProps {
16 readonly projectedItem: OrchestrationV2ProjectedTurnItem;
17 readonly environmentId: EnvironmentId;
18 readonly cwd?: string | undefined;
19 readonly workspaceRoot?: string | undefined;
20 readonly onOpenThread: (threadId: ThreadId) => void;
21 readonly onOpenTurnDiff: (runId: RunId, filePath?: string) => void;
22 readonly onRollbackCheckpoint?: (input: {
23 readonly checkpointId: string;
24 readonly scopeId: string;
25 }) => void;
27​
28function durationLabel(startedAt: unknown, completedAt: unknown): string | null {
29 if (startedAt == null) return null;
30 const start = Date.parse(String(startedAt));
31 const end = completedAt == null ? Date.now() : Date.parse(String(completedAt));
32 if (!Number.isFinite(start) || !Number.isFinite(end)) return null;
33 const milliseconds = Math.max(0, end - start);
34 if (milliseconds < 1_000) return `${milliseconds}ms`;
35 if (milliseconds < 60_000) return `${(milliseconds / 1_000).toFixed(1)}s`;
36 return `${Math.floor(milliseconds / 60_000)}m ${Math.floor((milliseconds % 60_000) / 1_000)}s`;
38​
39function DataField(props: { readonly label: string; readonly children: ReactNode }) {
40 return (
41 <div className="min-w-0">
42 <dt className="text-[10px] font-medium tracking-wide uppercase text-muted-foreground/65">
43 {props.label}
44 </dt>
45 <dd className="mt-0.5 min-w-0 break-words font-mono text-[11px] text-foreground/80">
46 {props.children}
47 </dd>
48 </div>
49 );
51​
52function StructuredValue({ value }: { readonly value: unknown }) {
53 const text = typeof value === "string" ? value : JSON.stringify(value, null, 2);
54 if (!text) return null;
55 return (
56 <pre className="max-h-80 overflow-auto whitespace-pre-wrap break-words rounded-md border border-border/50 bg-background/60 p-2 font-mono text-[11px] leading-relaxed text-muted-foreground select-text">
57 {text}
58 </pre>
59 );
61​
62export const V2ItemInspector = memo(function V2ItemInspector(props: V2ItemInspectorProps) {
63 const { item } = props.projectedItem;
64 const support = useV2ItemSupport({
65 environmentId: props.environmentId,
66 sourceThreadId: props.projectedItem.sourceThreadId,
67 sourceItemId: props.projectedItem.sourceItemId,
68 });
69 const duration = durationLabel(item.startedAt, item.completedAt);
70 const latestAttempt = support.attempts.at(-1) ?? null;
71 const runtimeRequest = support.runtimeRequest;
72​
73 return (
74 <div className="space-y-2 text-xs" data-v2-item-inspector={item.type}>
75 <dl className="grid grid-cols-2 gap-x-4 gap-y-2 rounded-md border border-border/45 bg-muted/15 p-2 sm:grid-cols-3">
76 <DataField label="Item">{item.type}</DataField>
77 <DataField label="Status">{item.status}</DataField>
78 {duration ? <DataField label="Duration">{duration}</DataField> : null}
79 {support.run ? <DataField label="Run">{support.run.status}</DataField> : null}
80 {latestAttempt ? (
81 <DataField label="Attempt">
82 {latestAttempt.attemptOrdinal} · {latestAttempt.status} · {latestAttempt.reason}
83 </DataField>
84 ) : null}
85 {support.node ? (
86 <DataField label="Node">
87 {support.node.kind} · {support.node.status}
88 </DataField>
89 ) : null}
90 {support.providerThread ? (
91 <DataField label="Provider thread">
92 {support.providerThread.providerInstanceId} · {support.providerThread.status}
93 </DataField>
94 ) : null}
95 {support.providerTurn ? (
96 <DataField label="Provider turn">{support.providerTurn.status}</DataField>
97 ) : null}
98 {support.providerSession ? (
99 <DataField label="Session">
100 {support.providerSession.status} · {support.providerSession.model ?? "default model"}
101 </DataField>
102 ) : null}
103 {support.providerSession ? (
104 <DataField label="Working directory">{support.providerSession.cwd}</DataField>
105 ) : null}
106 {runtimeRequest ? (
107 <DataField label="Request">
108 {runtimeRequest.status} · {runtimeRequest.responseCapability.type}
109 </DataField>
110 ) : null}
111 </dl>
112​
113 {support.attempts.length > 1 ? (
114 <details className="rounded-md border border-border/45 bg-background/40">
115 <summary className="cursor-pointer px-2 py-1.5 text-[11px] font-medium text-muted-foreground">
116 Attempt history · {support.attempts.length}
117 </summary>
118 <ol className="space-y-1 border-t border-border/45 p-2 font-mono text-[11px] text-muted-foreground">
119 {support.attempts.map((attempt) => (
120 <li key={attempt.id} className="flex items-center justify-between gap-3">
121 <span>
122 Attempt {attempt.attemptOrdinal} · {attempt.reason.replaceAll("_", " ")}
123 </span>
124 <span>{attempt.status}</span>
125 </li>
126 ))}
127 </ol>
128 </details>
129 ) : null}
130​
131 {item.type === "reasoning" && item.text ? (
132 <div className="rounded-md border border-border/45 bg-muted/15 p-2 italic text-muted-foreground">
133 <ChatMarkdown text={item.text} cwd={props.cwd} lineBreaks />
134 </div>
135 ) : null}
136​
137 {item.type === "command_execution" ? (
138 <div className="space-y-2">
139 <StructuredValue value={item.input} />
140 {item.output !== undefined ? <StructuredValue value={item.output} /> : null}
141 {item.exitCode !== undefined ? (
142 <p className={item.exitCode === 0 ? "text-emerald-600" : "text-destructive"}>
143 Process exited with code {item.exitCode}
144 </p>
145 ) : null}
146 </div>
147 ) : null}
148​
149 {item.type === "file_change" ? (
150 <div className="flex flex-wrap items-center gap-2">
151 <span className="font-mono text-muted-foreground">
152 {formatWorkspaceRelativePath(item.fileName, props.workspaceRoot)}
153 </span>
154 {item.additions !== undefined || item.deletions !== undefined ? (
155 <span>
156 <span className="text-emerald-600">+{item.additions ?? 0}</span>{" "}
157 <span className="text-destructive">-{item.deletions ?? 0}</span>
158 </span>
159 ) : null}
160 {item.runId !== null ? (
161 <Button
162 size="xs"
163 variant="outline"
164 onClick={() => props.onOpenTurnDiff(item.runId!, item.fileName)}
165 >
166 Open diff
167 </Button>
168 ) : null}
169 {item.diffStr ? <StructuredValue value={item.diffStr} /> : null}
170 </div>
171 ) : null}
172​
173 {item.type === "file_search" && item.results ? (
174 <ul className="space-y-1 rounded-md border border-border/45 p-2">
175 {item.results.map((result) => (
176 <li key={JSON.stringify(result)}>
177 <span className="font-mono text-foreground/80">
178 {formatWorkspaceRelativePath(result.fileName, props.workspaceRoot)}
179 {result.line === undefined ? "" : `:${result.line}`}
180 {result.column === undefined ? "" : `:${result.column}`}
181 </span>
182 {result.preview ? (
183 <p className="mt-0.5 whitespace-pre-wrap text-muted-foreground">{result.preview}</p>
184 ) : null}
185 </li>
186 ))}
187 </ul>
188 ) : null}
189​
190 {item.type === "web_search" && item.results ? (
191 <ul className="space-y-1.5 rounded-md border border-border/45 p-2">
192 {item.results.map((result) => (
193 <li key={JSON.stringify(result)}>
194 {result.url ? (
195 <a
196 href={result.url}
197 target="_blank"
198 rel="noreferrer"
199 className="inline-flex items-center gap-1 font-medium text-foreground hover:underline"
200 >
201 {result.title ?? result.url}
202 <ExternalLinkIcon className="size-3" />
203 </a>
204 ) : (
205 <p className="font-medium text-foreground">{result.title ?? "Search result"}</p>
206 )}
207 {result.snippet ? <p className="text-muted-foreground">{result.snippet}</p> : null}
208 </li>
209 ))}
210 </ul>
211 ) : null}
212​
213 {item.type === "dynamic_tool" ? (
214 <div className="grid gap-2 sm:grid-cols-2">
215 <div>
216 <p className="mb-1 text-[10px] font-medium tracking-wide uppercase text-muted-foreground">
217 Input
218 </p>
219 <StructuredValue value={item.input} />
220 </div>
221 {item.output !== undefined ? (
222 <div>
223 <p className="mb-1 text-[10px] font-medium tracking-wide uppercase text-muted-foreground">
224 Output
225 </p>
226 <StructuredValue value={item.output} />
227 </div>
228 ) : null}
229 </div>
230 ) : null}
231​
232 {item.type === "checkpoint" ? (
233 <div className="flex flex-wrap items-center gap-2">
234 <span className="text-muted-foreground">
235 {support.checkpoint?.status ?? item.status} · {item.files.length} files
236 </span>
237 {props.onRollbackCheckpoint && support.checkpoint?.status === "ready" ? (
238 <Button
239 size="xs"
240 variant="outline"
241 onClick={() =>
242 props.onRollbackCheckpoint?.({
243 checkpointId: item.checkpointId,
244 scopeId: item.scopeId,
245 })
246 }
247 >
248 <RotateCcwIcon className="size-3" />
249 Roll back
250 </Button>
251 ) : null}
252 </div>
253 ) : null}
254​
255 {item.type === "fork" ? (
256 <Button size="xs" variant="outline" onClick={() => props.onOpenThread(item.targetThreadId)}>
257 <GitBranchIcon className="size-3" />
258 Open fork
259 </Button>
260 ) : null}
261​
262 {item.type === "subagent" && item.childThreadId !== null ? (
263 <Button size="xs" variant="outline" onClick={() => props.onOpenThread(item.childThreadId!)}>
264 Open subagent thread
265 </Button>
266 ) : null}
267​
268 {item.type === "handoff" ? (
269 <div className="space-y-1 rounded-md border border-border/45 p-2 text-muted-foreground">
270 <p>
271 {item.fromProviderInstanceIds.join(", ")} → {item.toProviderInstanceId}
272 </p>
273 <p>
274 {item.strategy.replaceAll("_", " ")} · {support.contextHandoff?.status ?? item.status}
275 </p>
276 {support.contextTransfer ? (
277 <p>
278 Transfer {support.contextTransfer.type.replaceAll("_", " ")} ·{" "}
279 {support.contextTransfer.status}
280 </p>
281 ) : null}
282 </div>
283 ) : null}
284​
285 <details
286 className="group/raw rounded-md border border-border/45 bg-background/40"
287 data-v2-structured-details="true"
288 >
289 <summary className="flex cursor-pointer list-none items-center px-2 py-1.5 text-[11px] font-medium text-muted-foreground hover:text-foreground [&::-webkit-details-marker]:hidden">
290 Structured details
291 </summary>
292 <div className="border-t border-border/45 p-2">
293 <StructuredValue value={item} />
294 </div>
295 </details>
296 </div>
297 );
298});