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

V2ItemInspector.tsx

234 lines8.8 KB

1import type {
2 EnvironmentId,
3 OrchestrationV2ProjectedTurnItem,
4 RunId,
5 ThreadId,
6} from "@t3tools/contracts";
7import { ExternalLinkIcon, GitBranchIcon, RotateCcwIcon } from "lucide-react";
8import { memo } from "react";
9​
10import { useV2ItemSupport } from "../../state/v2ItemSupport";
11import { formatWorkspaceRelativePath } from "../../filePathDisplay";
12import { Button } from "../ui/button";
13import ChatMarkdown from "../ChatMarkdown";
14import { resolveExternalWebLinkHref } from "./externalLinkContextMenu";
15​
16interface V2ItemInspectorProps {
17 readonly projectedItem: OrchestrationV2ProjectedTurnItem;
18 readonly environmentId: EnvironmentId;
19 readonly cwd?: string | undefined;
20 readonly workspaceRoot?: string | undefined;
21 readonly onOpenThread: (threadId: ThreadId) => void;
22 readonly onOpenTurnDiff: (runId: RunId, filePath?: string) => void;
23 readonly onRollbackCheckpoint?: (input: {
24 readonly checkpointId: string;
25 readonly scopeId: string;
26 }) => void;
28​
29function StructuredValue({ value }: { readonly value: unknown }) {
30 const text = typeof value === "string" ? value : JSON.stringify(value, null, 2);
31 if (!text) return null;
32 return (
33 <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-2xs leading-relaxed text-muted-foreground select-text">
34 {text}
35 </pre>
36 );
38​
39export const V2ItemInspector = memo(function V2ItemInspector(props: V2ItemInspectorProps) {
40 const { item } = props.projectedItem;
41 const support = useV2ItemSupport({
42 environmentId: props.environmentId,
43 sourceThreadId: props.projectedItem.sourceThreadId,
44 sourceItemId: props.projectedItem.sourceItemId,
45 });
46 return (
47 <div className="space-y-2 text-xs" data-v2-item-inspector={item.type}>
48 {item.type === "reasoning" && item.text ? (
49 <div className="rounded-md border border-border/45 bg-muted/15 p-2 italic text-muted-foreground">
50 <ChatMarkdown
51 text={item.text}
52 cwd={props.cwd}
53 threadRef={{
54 environmentId: props.environmentId,
55 threadId: props.projectedItem.sourceThreadId,
56 }}
57 lineBreaks
58 />
59 </div>
60 ) : null}
61​
62 {item.type === "command_execution" ? (
63 <div className="space-y-2">
64 <StructuredValue value={item.input} />
65 {item.exitCode !== undefined ? (
66 <p className={item.exitCode === 0 ? "text-success" : "text-destructive"}>
67 Process exited with code {item.exitCode}
68 </p>
69 ) : null}
70 </div>
71 ) : null}
72​
73 {item.type === "file_change" ? (
74 <div className="space-y-2">
75 <div className="flex flex-wrap items-center gap-2">
76 <span className="font-mono text-muted-foreground">
77 {formatWorkspaceRelativePath(item.fileName, props.workspaceRoot)}
78 </span>
79 {item.additions !== undefined || item.deletions !== undefined ? (
80 <span>
81 <span className="text-success">+{item.additions ?? 0}</span>{" "}
82 <span className="text-destructive">-{item.deletions ?? 0}</span>
83 </span>
84 ) : null}
85 {item.runId !== null ? (
86 <Button
87 size="xs"
88 variant="outline"
89 onClick={() => props.onOpenTurnDiff(item.runId!, item.fileName)}
90 >
91 Open diff
92 </Button>
93 ) : null}
94 </div>
95 {item.changes !== undefined && item.changes.length > 0 ? (
96 <ul className="space-y-1 font-mono text-muted-foreground">
97 {item.changes.map((change, index) => (
98 <li key={`${change.operation}:${change.path}:${index}`}>
99 {change.operation} {change.oldPath ? `${change.oldPath} → ` : ""}
100 {formatWorkspaceRelativePath(change.path, props.workspaceRoot)}
101 {change.fileType || change.mimeType
102 ? ` (${[change.fileType, change.mimeType].filter(Boolean).join(", ")})`
103 : ""}
104 </li>
105 ))}
106 </ul>
107 ) : null}
108 </div>
109 ) : null}
110​
111 {item.type === "file_search" && item.results ? (
112 <ul className="space-y-1 rounded-md border border-border/45 p-2">
113 {item.results.map((result) => (
114 <li key={JSON.stringify(result)}>
115 <span className="font-mono text-foreground/80">
116 {formatWorkspaceRelativePath(result.fileName, props.workspaceRoot)}
117 {result.line === undefined ? "" : `:${result.line}`}
118 {result.column === undefined ? "" : `:${result.column}`}
119 </span>
120 {result.preview ? (
121 <p className="mt-0.5 whitespace-pre-wrap text-muted-foreground">{result.preview}</p>
122 ) : null}
123 </li>
124 ))}
125 </ul>
126 ) : null}
127​
128 {item.type === "web_search" && item.results ? (
129 <ul className="space-y-1.5 rounded-md border border-border/45 p-2">
130 {item.results.map((result) => {
131 const safeHref = resolveExternalWebLinkHref(result.url);
132 return (
133 <li key={JSON.stringify(result)}>
134 {safeHref ? (
135 <a
136 href={safeHref}
137 target="_blank"
138 rel="noreferrer"
139 className="inline-flex items-center gap-1 font-medium text-foreground hover:underline"
140 >
141 {result.title ?? result.url}
142 <ExternalLinkIcon className="size-3" />
143 </a>
144 ) : (
145 <p className="font-medium text-foreground">
146 {result.title ?? result.url ?? "Search result"}
147 </p>
148 )}
149 {result.snippet ? <p className="text-muted-foreground">{result.snippet}</p> : null}
150 </li>
151 );
152 })}
153 </ul>
154 ) : null}
155​
156 {item.type === "dynamic_tool" ? (
157 <div>
158 <p className="mb-1 text-3xs font-medium tracking-wide uppercase text-muted-foreground">
159 Input
160 </p>
161 <StructuredValue value={item.input} />
162 </div>
163 ) : null}
164​
165 {item.type === "approval_request" ? <StructuredValue value={item.prompt} /> : null}
166 {item.type === "user_input_request" ? (
167 <StructuredValue value={item.questions.map((question) => question.question).join("\n\n")} />
168 ) : null}
169 {item.type === "notification" ? <StructuredValue value={item.detail} /> : null}
170 {item.type === "system_notice" ? <StructuredValue value={item.message} /> : null}
171 {item.type === "proposed_plan" ? <StructuredValue value={item.markdown} /> : null}
172 {item.type === "todo_list" ? (
173 <StructuredValue
174 value={item.steps
175 .map((step) => `${step.status === "completed" ? "✓" : "○"} ${step.text}`)
176 .join("\n")}
177 />
178 ) : null}
179​
180 {item.type === "checkpoint" ? (
181 <div className="flex flex-wrap items-center gap-2">
182 <span className="text-muted-foreground">
183 {support.checkpoint?.status ?? item.status} · {item.files.length} files
184 </span>
185 {props.onRollbackCheckpoint && support.checkpoint?.status === "ready" ? (
186 <Button
187 size="xs"
188 variant="outline"
189 onClick={() =>
190 props.onRollbackCheckpoint?.({
191 checkpointId: item.checkpointId,
192 scopeId: item.scopeId,
193 })
194 }
195 >
196 <RotateCcwIcon className="size-3" />
197 Roll back
198 </Button>
199 ) : null}
200 </div>
201 ) : null}
202​
203 {item.type === "fork" ? (
204 <Button size="xs" variant="outline" onClick={() => props.onOpenThread(item.targetThreadId)}>
205 <GitBranchIcon className="size-3" />
206 Open fork
207 </Button>
208 ) : null}
209​
210 {item.type === "subagent" && item.childThreadId !== null ? (
211 <Button size="xs" variant="outline" onClick={() => props.onOpenThread(item.childThreadId!)}>
212 Open subagent thread
213 </Button>
214 ) : null}
215​
216 {item.type === "handoff" ? (
217 <div className="space-y-1 rounded-md border border-border/45 p-2 text-muted-foreground">
218 <p>
219 {item.fromProviderInstanceIds.join(", ")} → {item.toProviderInstanceId}
220 </p>
221 <p>
222 {item.strategy.replaceAll("_", " ")} · {support.contextHandoff?.status ?? item.status}
223 </p>
224 {support.contextTransfer ? (
225 <p>
226 Transfer {support.contextTransfer.type.replaceAll("_", " ")} ·{" "}
227 {support.contextTransfer.status}
228 </p>
229 ) : null}
230 </div>
231 ) : null}
232 </div>
233 );
234});