Skip to content

Commit dff7955

Browse files
committed
feat(web): open tool call diffs in the diff panel
1 parent ff71186 commit dff7955

13 files changed

Lines changed: 229 additions & 45 deletions

‎apps/server/src/orchestration-v2/WireProjection.test.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,9 @@ describe("orchestration V2 wire projection", () => {
275275
expect(projected).not.toHaveProperty("newStr");
276276
expect(projected).toMatchObject({ fileName: "src/main.ts", additions: 3, deletions: 1 });
277277
expect(item.diffStr).toBe("+new code");
278+
expect(projectTurnItemForDetail(item)).toMatchObject({ diffStr: "+new code" });
279+
expect(projectTurnItemForDetail(item)).not.toHaveProperty("oldStr");
280+
expect(projectTurnItemForDetail(item)).not.toHaveProperty("newStr");
278281
const failed = projectTurnItemForWire({
279282
...item,
280283
status: "failed",

‎apps/server/src/orchestration-v2/WireProjection.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,14 @@ export function projectTurnItemForDetail(item: OrchestrationV2TurnItem): Orchest
200200
result:
201201
item.result === null ? null : (truncateDetail(item.result, MAX_ON_DEMAND_BYTES) ?? null),
202202
};
203-
case "handoff":
204203
case "file_change":
204+
return {
205+
...projectTurnItemForWire(item),
206+
...(item.diffStr === undefined
207+
? {}
208+
: { diffStr: truncateDetail(item.diffStr, MAX_ON_DEMAND_BYTES) }),
209+
};
210+
case "handoff":
205211
return projectTurnItemForWire(item);
206212
default:
207213
return item;

‎apps/web/src/components/ChatView.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2316,6 +2316,7 @@ export default function ChatView(props: ChatViewProps) {
23162316
createBrowserSurface,
23172317
createBrowserSurfaceStable,
23182318
moveRightPanelSurfaceStable,
2319+
onOpenToolDiffStable,
23192320
onOpenTurnDiffStable,
23202321
openChangesFromThreadPanelStable,
23212322
openFileSurfaceStable,
@@ -3297,6 +3298,7 @@ export default function ChatView(props: ChatViewProps) {
32973298
routeThreadKey={displayedTimelineKey}
32983299
displayThreadKey={displayedTimelineKey}
32993300
announceTurnStatus={!isProviderSubagent}
3301+
onOpenToolDiff={onOpenToolDiffStable}
33003302
onOpenTurnDiff={onOpenTurnDiffStable}
33013303
onOpenThread={onOpenRelatedThreadStable}
33023304
parentThreadLink={parentThreadLink}

‎apps/web/src/components/DiffPanel.tsx‎

Lines changed: 68 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ import { useOpenInPreferredEditor } from "../editorPreferences";
2626
import { useFileContextMenuHandler } from "../fileContextMenu";
2727
import { type DraftId } from "../composerDraftStore";
2828
import { openDiffFilePrimaryAction } from "../diffFileActions";
29-
import { useCheckpointDiff } from "~/state/queries";
29+
import { toolCallPatch } from "../lib/toolCallDiff";
30+
import { useCheckpointDiff, useTurnItemDetail } from "~/state/queries";
3031
import { revisionedQuery } from "~/state/revisionedQuery";
3132
import { cn } from "~/lib/utils";
3233
import {
@@ -252,6 +253,14 @@ export default function DiffPanel({
252253
const diffSelection = useDiffPanelStore((state) =>
253254
selectThreadDiffPanelSelection(state.byThreadKey, routeThreadRef),
254255
);
256+
const toolCallSelection = diffSelection.kind === "tool-call" ? diffSelection : null;
257+
const isGitSelection = diffSelection.kind === "branch" || diffSelection.kind === "unstaged";
258+
const toolCallDetail = useTurnItemDetail(
259+
toolCallSelection && routeThreadRef
260+
? { ...toolCallSelection, environmentId: routeThreadRef.environmentId }
261+
: null,
262+
);
263+
const toolCallItem = toolCallDetail.data?.item;
255264
const isGitRepo = gitStatusQuery.data?.isRepo ?? true;
256265
const turnDiffSummaries = useThreadCheckpointSummaries(routeThreadRef);
257266
const orderedTurnDiffSummaries = turnDiffSummaries.toSorted((left, right) => {
@@ -283,24 +292,31 @@ export default function DiffPanel({
283292
orderedTurnDiffSummaries[0]);
284293
const selectedCheckpointTurnCount = selectedTurn?.checkpointTurnCount;
285294
const latestTurn = orderedTurnDiffSummaries[0];
286-
const selectedScopeLabel =
287-
selectedRunId === null
295+
const selectedScopeLabel = toolCallSelection
296+
? "Tool call"
297+
: selectedRunId === null
288298
? selectedGitScope === "unstaged"
289299
? "Uncommitted"
290300
: "Changes"
291301
: selectedTurn?.runId === latestTurn?.runId
292302
? "Latest turn"
293303
: `Turn ${selectedCheckpointTurnCount ?? "?"}`;
294-
const reviewSectionId = selectedTurn ? `turn:${selectedTurn.runId}` : selectedGitScope;
304+
const reviewSectionId = toolCallSelection
305+
? `tool-call:${toolCallSelection.threadId}:${toolCallSelection.itemId}`
306+
: selectedTurn
307+
? `turn:${selectedTurn.runId}`
308+
: selectedGitScope;
295309
const collapseScopeKey = routeThreadRef
296310
? `${routeThreadRef.environmentId}:${routeThreadRef.threadId}:${reviewSectionId}`
297311
: null;
298312
const codeViewMountKey = `${collapseScopeKey ?? reviewSectionId}:${codeViewRevision}`;
299-
const reviewSectionTitle = selectedTurn
300-
? `Turn ${selectedCheckpointTurnCount ?? "?"}`
301-
: selectedGitScope === "unstaged"
302-
? "Uncommitted"
303-
: "Changes";
313+
const reviewSectionTitle = toolCallSelection
314+
? "Tool call"
315+
: selectedTurn
316+
? `Turn ${selectedCheckpointTurnCount ?? "?"}`
317+
: selectedGitScope === "unstaged"
318+
? "Uncommitted"
319+
: "Changes";
304320
const selectedCheckpointRange =
305321
typeof selectedCheckpointTurnCount === "number"
306322
? {
@@ -320,15 +336,15 @@ export default function DiffPanel({
320336
{ enabled: isGitRepo && selectedTurn !== undefined },
321337
);
322338
const branchDiffPreviewInput =
323-
canReadFiles && selectedRunId === null && activeThread && activeCwd
339+
canReadFiles && isGitSelection && activeThread && activeCwd
324340
? {
325341
cwd: activeCwd,
326342
...(selectedBaseRef ? { baseRef: selectedBaseRef } : {}),
327343
ignoreWhitespace: diffIgnoreWhitespace,
328344
}
329345
: undefined;
330346
const canRefreshGitDiff =
331-
isGitRepo && selectedRunId === null && activeThread != null && activeCwd != null;
347+
isGitRepo && isGitSelection && activeThread != null && activeCwd != null;
332348
// Read under the workspace mutation id, so each mutation refetches the diff; the panel stays
333349
// mounted while hidden, so a mutation made then is already reflected when it shows.
334350
const branchDiffPreview = useEnvironmentQuery(
@@ -355,25 +371,42 @@ export default function DiffPanel({
355371
const preview = branchDiffPreview.data;
356372
const gitDiff = selectedGitSource?.diff;
357373

358-
const selectedPatch = selectedTurn ? activeCheckpointDiff.data?.diff : gitDiff;
359-
const isSelectedPatchTruncated = !selectedTurn && selectedGitSource?.truncated === true;
360-
const selectedPatchQuery = selectedTurn ? activeCheckpointDiff : branchDiffPreview;
374+
const selectedPatch = toolCallSelection
375+
? toolCallItem?.type === "file_change"
376+
? toolCallPatch(toolCallItem)
377+
: undefined
378+
: selectedTurn
379+
? activeCheckpointDiff.data?.diff
380+
: gitDiff;
381+
const isSelectedPatchTruncated = isGitSelection && selectedGitSource?.truncated === true;
382+
const selectedPatchQuery = toolCallSelection
383+
? toolCallDetail
384+
: selectedTurn
385+
? activeCheckpointDiff
386+
: branchDiffPreview;
361387
const isLoadingSelectedPatch = selectedPatchQuery.isPending && selectedPatchQuery.data === null;
362-
const selectedPatchError = selectedTurn ? activeCheckpointDiff.error : branchDiffPreview.error;
388+
const selectedPatchError = toolCallSelection
389+
? toolCallDetail.error
390+
: selectedTurn
391+
? activeCheckpointDiff.error
392+
: branchDiffPreview.error;
363393
const hasResolvedPatch = typeof selectedPatch === "string";
364394
const hasNoNetChanges = hasResolvedPatch && selectedPatch.trim().length === 0;
365395
const lazySource =
366-
!selectedTurn && selectedGitSource?.truncated && selectedGitSource.files
396+
isGitSelection && selectedGitSource?.truncated && selectedGitSource.files
367397
? selectedGitSource
368398
: null;
369399
const renderablePatch = getRenderablePatch(
370400
lazySource ? undefined : selectedPatch,
371401
`diff-panel:${resolvedTheme}`,
372-
{ compactPartialHunkOffsets: selectedRunId === null },
402+
{
403+
compactPartialHunkOffsets: isGitSelection,
404+
...(toolCallSelection ? { ignoreWhitespace: diffIgnoreWhitespace } : {}),
405+
},
373406
);
374407
const fileStats = new Map(lazySource?.files?.map((file) => [file.path, file]));
375408
const sourceLineStat =
376-
!selectedTurn && selectedGitSource?.files
409+
isGitSelection && selectedGitSource?.files
377410
? selectedGitSource.files.reduce(
378411
(total, file) => ({
379412
additions: total.additions + file.additions,
@@ -383,7 +416,7 @@ export default function DiffPanel({
383416
)
384417
: null;
385418
const currentLoadDiffFiles =
386-
selectedRunId === null && activeThread && preview && selectedGitSource && canReadFiles
419+
isGitSelection && activeThread && preview && selectedGitSource && canReadFiles
387420
? createGitDiffFileContentsLoader(getDiffFileContents, {
388421
environmentId: activeThread.environmentId,
389422
cwd: preview.cwd,
@@ -400,10 +433,7 @@ export default function DiffPanel({
400433
return currentLoadDiffFiles(fileDiff);
401434
});
402435
const localBranchRefs = useEnvironmentQuery(
403-
selectedRunId === null &&
404-
selectedGitScope === "branch" &&
405-
activeThread &&
406-
branchDiffPreview.data?.cwd
436+
isGitSelection && selectedGitScope === "branch" && activeThread && branchDiffPreview.data?.cwd
407437
? vcsEnvironment.listRefs({
408438
environmentId: activeThread.environmentId,
409439
input: {
@@ -417,10 +447,7 @@ export default function DiffPanel({
417447
: null,
418448
);
419449
const remoteBranchRefs = useEnvironmentQuery(
420-
selectedRunId === null &&
421-
selectedGitScope === "branch" &&
422-
activeThread &&
423-
branchDiffPreview.data?.cwd
450+
isGitSelection && selectedGitScope === "branch" && activeThread && branchDiffPreview.data?.cwd
424451
? vcsEnvironment.listRefs({
425452
environmentId: activeThread.environmentId,
426453
input: {
@@ -630,8 +657,9 @@ export default function DiffPanel({
630657
useDiffPanelStore.getState().selectBranchBaseRef(routeThreadRef, baseRef);
631658
};
632659
const selectedTurnValue = selectedTurn ? `turn:${selectedTurn.runId}` : "";
633-
const selectedScopeValue =
634-
selectedRunId === null
660+
const selectedScopeValue = toolCallSelection
661+
? ""
662+
: selectedRunId === null
635663
? selectedGitScope
636664
: selectedTurn?.runId === latestTurn?.runId
637665
? "latest"
@@ -698,7 +726,7 @@ export default function DiffPanel({
698726
</DropdownMenuSub>
699727
</DropdownMenuContent>
700728
</DropdownMenu>
701-
{selectedRunId === null && selectedGitScope === "branch" && selectedGitSource?.baseRef && (
729+
{isGitSelection && selectedGitScope === "branch" && selectedGitSource?.baseRef && (
702730
<div
703731
className="flex min-w-0 max-w-full items-center gap-2 overflow-hidden text-xs text-muted-foreground"
704732
aria-label={`Comparing ${selectedGitSource.headRef ?? "HEAD"} against ${selectedGitSource.baseRef}`}
@@ -831,7 +859,7 @@ export default function DiffPanel({
831859
)}
832860
</div>
833861
<div className="flex shrink-0 items-center gap-1 [-webkit-app-region:no-drag]">
834-
{codeViewFiles.length > 0 || (!selectedTurn && selectedGitSource?.files?.length) ? (
862+
{codeViewFiles.length > 0 || (isGitSelection && selectedGitSource?.files?.length) ? (
835863
<DiffStatLabel
836864
additions={diffLineStat.additions}
837865
deletions={diffLineStat.deletions}
@@ -973,15 +1001,15 @@ export default function DiffPanel({
9731001
<div className="flex flex-1 items-center justify-center px-5 text-center text-xs text-muted-foreground/70">
9741002
Select a thread to inspect turn diffs.
9751003
</div>
976-
) : !isGitRepo ? (
1004+
) : !isGitRepo && !toolCallSelection ? (
9771005
<div className="flex flex-1 items-center justify-center px-5 text-center text-xs text-muted-foreground/70">
9781006
Turn diffs are unavailable because this project is not a git repository.
9791007
</div>
9801008
) : selectedRunId !== null && orderedTurnDiffSummaries.length === 0 ? (
9811009
<div className="flex flex-1 items-center justify-center px-5 text-center text-xs text-muted-foreground/70">
9821010
No completed turns yet.
9831011
</div>
984-
) : selectedRunId === null && !canReadFiles ? (
1012+
) : isGitSelection && !canReadFiles ? (
9851013
fileAccess.isPending ? (
9861014
<DiffPanelLoadingState label="Checking file access..." />
9871015
) : (
@@ -1007,11 +1035,13 @@ export default function DiffPanel({
10071035
isLoadingSelectedPatch ? (
10081036
<DiffPanelLoadingState
10091037
label={
1010-
selectedTurn
1011-
? "Loading checkpoint diff..."
1012-
: selectedGitScope === "unstaged"
1013-
? "Loading uncommitted changes..."
1014-
: "Loading changes..."
1038+
toolCallSelection
1039+
? "Loading tool call diff..."
1040+
: selectedTurn
1041+
? "Loading checkpoint diff..."
1042+
: selectedGitScope === "unstaged"
1043+
? "Loading uncommitted changes..."
1044+
: "Loading changes..."
10151045
}
10161046
/>
10171047
) : (

‎apps/web/src/components/chat/MessagesTimeline.tsx‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { ToolCallDiffTarget } from "../../diffPanelStore";
12
import { AudioPreviewUnmountContext } from "../files/AudioPreview";
23
import { ThreadFindTimelineContext } from "./ThreadFindProvider";
34
import { MarkdownFindContext } from "./markdownFindContext";
@@ -232,6 +233,7 @@ interface MessagesTimelineProps {
232233
displayThreadKey?: string;
233234
/** Off where another region already speaks the thread's status, like a subagent's bar. */
234235
announceTurnStatus?: boolean;
236+
onOpenToolDiff?: (target: ToolCallDiffTarget) => void;
235237
onOpenTurnDiff?: (runId: RunId, filePath?: string) => void;
236238
onOpenThread: (threadId: OrchestrationV2TurnItem["threadId"]) => void;
237239
renderHtmlRender?: TimelineRowSharedState["renderHtmlRender"];
@@ -336,6 +338,7 @@ const ConversationTimeline = memo(function ConversationTimeline({
336338
routeThreadKey,
337339
displayThreadKey,
338340
announceTurnStatus = true,
341+
onOpenToolDiff,
339342
onOpenTurnDiff,
340343
onOpenThread,
341344
renderHtmlRender,
@@ -1024,6 +1027,7 @@ const ConversationTimeline = memo(function ConversationTimeline({
10241027
onAppFullscreenChange,
10251028
onFileDownload,
10261029
openPullRequest,
1030+
onOpenToolDiff,
10271031
onOpenTurnDiff,
10281032
onOpenThread,
10291033
renderHtmlRender,

‎apps/web/src/components/chat/TimelineWorkEntryRow.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ function WorkEntryLogRow(props: WorkEntryRowProps) {
530530
environmentId={ctx.activeThreadEnvironmentId}
531531
cwd={ctx.markdownCwd}
532532
workspaceRoot={workspaceRoot}
533-
onOpenTurnDiff={ctx.onOpenTurnDiff}
533+
onOpenToolDiff={ctx.onOpenToolDiff}
534534
onImageExpand={ctx.onImageExpand}
535535
source={ctx.turnItemSource}
536536
/>

‎apps/web/src/components/chat/V2ItemInspector.tsx‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import type { EnvironmentId, OrchestrationV2ProjectedTurnItem, RunId } from "@t3tools/contracts";
1+
import type { ToolCallDiffTarget } from "../../diffPanelStore";
2+
import type { EnvironmentId, OrchestrationV2ProjectedTurnItem } from "@t3tools/contracts";
23
import {
34
toolCallLines,
45
turnItemDetailRevision,
@@ -32,7 +33,7 @@ interface V2ItemInspectorProps {
3233
readonly environmentId: EnvironmentId;
3334
readonly cwd?: string | undefined;
3435
readonly workspaceRoot?: string | undefined;
35-
readonly onOpenTurnDiff?: ((runId: RunId, filePath?: string) => void) | undefined;
36+
readonly onOpenToolDiff?: ((target: ToolCallDiffTarget) => void) | undefined;
3637
readonly onImageExpand?: ((preview: ExpandedImagePreview) => void) | undefined;
3738
readonly source?: TurnItemSource | undefined;
3839
}
@@ -289,11 +290,17 @@ export const V2ItemInspector = memo(function V2ItemInspector(props: V2ItemInspec
289290
<span className="text-diff-deletion">-{formatDiffCount(item.deletions ?? 0)}</span>
290291
</span>
291292
) : null}
292-
{item.runId !== null && props.onOpenTurnDiff ? (
293+
{props.onOpenToolDiff ? (
293294
<Button
294295
size="xs"
295296
variant="outline"
296-
onClick={() => props.onOpenTurnDiff?.(item.runId!, item.fileName)}
297+
onClick={() =>
298+
props.onOpenToolDiff?.({
299+
threadId: props.projectedItem.sourceThreadId,
300+
itemId: props.projectedItem.sourceItemId,
301+
revision: turnItemDetailRevision(item),
302+
})
303+
}
297304
>
298305
Open diff
299306
</Button>

‎apps/web/src/components/chat/timelineRowContext.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { ToolCallDiffTarget } from "../../diffPanelStore";
12
import type { WorktreeSetupSnapshot } from "@t3tools/contracts";
23
import {
34
type EnvironmentId,
@@ -56,6 +57,7 @@ export interface TimelineRowSharedState {
5657
findActive: boolean;
5758
onImageExpand: (preview: ExpandedImagePreview) => void;
5859
displayThreadKey?: string;
60+
onOpenToolDiff: ((target: ToolCallDiffTarget) => void) | undefined;
5961
/** Unset where there is no diff to open, so the timeline shows no "Open diff" button. */
6062
onOpenTurnDiff: ((runId: RunId, filePath?: string) => void) | undefined;
6163
/**

‎apps/web/src/components/chat/useRightPanelActions.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { ToolCallDiffTarget } from "../../diffPanelStore";
12
import type { EnvironmentProject } from "@t3tools/client-runtime/state/shell";
23
import type { ThreadPreviewState } from "../../previewStateStore";
34
import { formatFilePathForClipboard } from "@t3tools/client-runtime/media-actions";
@@ -302,6 +303,15 @@ export function useRightPanelActions(input: RightPanelActionsInput) {
302303
useRightPanelStore.getState().open(activeThreadRef, "diff");
303304
onDiffPanelOpen?.();
304305
};
306+
const onOpenToolDiffStable = useStableCallback((target: ToolCallDiffTarget) => {
307+
if (!isServerThread || !activeThreadRef) return;
308+
const diffOpen =
309+
selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, activeThreadRef) === "diff";
310+
explicitDiffOpenRef.current = diffOpen ? null : activeThreadRef;
311+
useDiffPanelStore.getState().selectToolCall(activeThreadRef, target);
312+
useRightPanelStore.getState().open(activeThreadRef, "diff");
313+
onDiffPanelOpen?.();
314+
});
305315
const onOpenTurnDiffStable = useStableCallback(onOpenTurnDiff);
306316
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- subscribes to the preview action bus to toggle the preview panel
307317
useEffect(
@@ -338,6 +348,7 @@ export function useRightPanelActions(input: RightPanelActionsInput) {
338348
createBrowserSurface,
339349
createBrowserSurfaceStable,
340350
moveRightPanelSurfaceStable,
351+
onOpenToolDiffStable,
341352
onOpenTurnDiffStable,
342353
onToggleDiffStable,
343354
openChangesFromThreadPanelStable,

0 commit comments

Comments
 (0)