Skip to content

Commit 04dfd84

Browse files
committed
refactor sending prompt to agent from UI
1 parent 3068737 commit 04dfd84

6 files changed

Lines changed: 79 additions & 68 deletions

File tree

apps/code/src/renderer/features/code-review/components/CloudReviewPage.tsx

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1+
import { sendPromptToAgent } from "@features/sessions/utils/sendPromptToAgent";
12
import { useCloudChangedFiles } from "@features/task-detail/hooks/useCloudChangedFiles";
23
import type { FileDiffMetadata } from "@pierre/diffs";
34
import { processFile } from "@pierre/diffs";
45
import { Flex, Spinner, Text } from "@radix-ui/themes";
56
import { useReviewNavigationStore } from "@renderer/features/code-review/stores/reviewNavigationStore";
67
import type { ChangedFile, Task } from "@shared/types";
7-
import { useMemo } from "react";
8-
import { useReviewComment } from "../hooks/useReviewComment";
8+
import { useCallback, useMemo } from "react";
99
import type { DiffOptions, OnCommentCallback } from "../types";
10+
import { buildInlineCommentPrompt } from "../utils/reviewPrompts";
1011
import { InteractiveFileDiff } from "./InteractiveFileDiff";
1112
import {
1213
DeferredDiffPlaceholder,
@@ -26,7 +27,15 @@ export function CloudReviewPage({ task }: CloudReviewPageProps) {
2627
);
2728
const { effectiveBranch, prUrl, isRunActive, remoteFiles, isLoading } =
2829
useCloudChangedFiles(taskId, task, isReviewOpen);
29-
const onComment = useReviewComment(taskId);
30+
const onComment: OnCommentCallback = useCallback(
31+
(filePath, startLine, endLine, side, comment) => {
32+
sendPromptToAgent(
33+
taskId,
34+
buildInlineCommentPrompt(filePath, startLine, endLine, side, comment),
35+
);
36+
},
37+
[taskId],
38+
);
3039

3140
const allPaths = useMemo(() => remoteFiles.map((f) => f.path), [remoteFiles]);
3241

apps/code/src/renderer/features/code-review/components/ReviewPage.tsx

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
import { makeFileKey } from "@features/git-interaction/utils/fileKey";
22
import { usePanelLayoutStore } from "@features/panels/store/panelLayoutStore";
3+
import { sendPromptToAgent } from "@features/sessions/utils/sendPromptToAgent";
34
import { useCwd } from "@features/sidebar/hooks/useCwd";
45
import type { parsePatchFiles } from "@pierre/diffs";
56
import { Flex, Text } from "@radix-ui/themes";
67
import { useReviewNavigationStore } from "@renderer/features/code-review/stores/reviewNavigationStore";
78
import { useTRPC } from "@renderer/trpc/client";
89
import type { ChangedFile, Task } from "@shared/types";
910
import { useQuery } from "@tanstack/react-query";
10-
import { useMemo } from "react";
11-
import { useReviewComment } from "../hooks/useReviewComment";
11+
import { useCallback, useMemo } from "react";
1212
import { useReviewDiffs } from "../hooks/useReviewDiffs";
1313
import type { DiffOptions, OnCommentCallback } from "../types";
14+
import { buildInlineCommentPrompt } from "../utils/reviewPrompts";
1415
import { InteractiveFileDiff } from "./InteractiveFileDiff";
1516
import {
1617
DeferredDiffPlaceholder,
@@ -32,7 +33,15 @@ export function ReviewPage({ task }: ReviewPageProps) {
3233
const isReviewOpen = useReviewNavigationStore(
3334
(s) => (s.reviewModes[taskId] ?? "closed") !== "closed",
3435
);
35-
const onComment = useReviewComment(taskId);
36+
const onComment: OnCommentCallback = useCallback(
37+
(filePath, startLine, endLine, side, comment) => {
38+
sendPromptToAgent(
39+
taskId,
40+
buildInlineCommentPrompt(filePath, startLine, endLine, side, comment),
41+
);
42+
},
43+
[taskId],
44+
);
3645

3746
const {
3847
changedFiles,

apps/code/src/renderer/features/code-review/hooks/useReviewComment.ts

Lines changed: 0 additions & 48 deletions
This file was deleted.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import type { AnnotationSide } from "@pierre/diffs";
2+
3+
function escapeXmlAttr(value: string): string {
4+
return value
5+
.replace(/&/g, "&")
6+
.replace(/</g, "&lt;")
7+
.replace(/>/g, "&gt;")
8+
.replace(/"/g, "&quot;")
9+
.replace(/'/g, "&apos;");
10+
}
11+
12+
export function buildInlineCommentPrompt(
13+
filePath: string,
14+
startLine: number,
15+
endLine: number,
16+
side: AnnotationSide,
17+
comment: string,
18+
): string {
19+
const lineRef =
20+
startLine === endLine
21+
? `line ${startLine}`
22+
: `lines ${startLine}-${endLine}`;
23+
const sideLabel = side === "deletions" ? "old" : "new";
24+
const escapedPath = escapeXmlAttr(filePath);
25+
return `In file <file path="${escapedPath}" />, ${lineRef} (${sideLabel}):\n\n${comment}`;
26+
}

apps/code/src/renderer/features/git-interaction/hooks/useFixWithAgent.ts

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
import { DEFAULT_TAB_IDS } from "@features/panels/constants/panelConstants";
2-
import { usePanelLayoutStore } from "@features/panels/store/panelLayoutStore";
3-
import { findTabInTree } from "@features/panels/store/panelTree";
4-
import { getSessionService } from "@features/sessions/service/service";
51
import { useSessionForTask } from "@features/sessions/stores/sessionStore";
2+
import { sendPromptToAgent } from "@features/sessions/utils/sendPromptToAgent";
63
import { useNavigationStore } from "@stores/navigationStore";
74
import { useCallback } from "react";
85
import type { FixWithAgentPrompt } from "../utils/errorPrompts";
@@ -34,16 +31,7 @@ export function useFixWithAgent(
3431
const { label, context } = buildPrompt(error);
3532

3633
const prompt = `<error_context label="${label}">${context}</error_context>\n\n\`\`\`\n${error}\n\`\`\``;
37-
getSessionService().sendPrompt(taskId, prompt);
38-
39-
const { taskLayouts, setActiveTab } = usePanelLayoutStore.getState();
40-
const layout = taskLayouts[taskId];
41-
if (layout) {
42-
const result = findTabInTree(layout.panelTree, DEFAULT_TAB_IDS.LOGS);
43-
if (result) {
44-
setActiveTab(taskId, result.panelId, DEFAULT_TAB_IDS.LOGS);
45-
}
46-
}
34+
sendPromptToAgent(taskId, prompt);
4735
},
4836
[buildPrompt, taskId, isSessionReady],
4937
);
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { useReviewNavigationStore } from "@features/code-review/stores/reviewNavigationStore";
2+
import { DEFAULT_TAB_IDS } from "@features/panels/constants/panelConstants";
3+
import { usePanelLayoutStore } from "@features/panels/store/panelLayoutStore";
4+
import { findTabInTree } from "@features/panels/store/panelTree";
5+
import { getSessionService } from "@features/sessions/service/service";
6+
7+
/**
8+
* Sends a prompt to the agent session for a task, collapses the review
9+
* panel to split mode if expanded, and switches to the logs/chat tab.
10+
*/
11+
export function sendPromptToAgent(taskId: string, prompt: string): void {
12+
getSessionService().sendPrompt(taskId, prompt);
13+
14+
const { getReviewMode, setReviewMode } = useReviewNavigationStore.getState();
15+
if (getReviewMode(taskId) === "expanded") {
16+
setReviewMode(taskId, "split");
17+
}
18+
19+
const { taskLayouts, setActiveTab } = usePanelLayoutStore.getState();
20+
const layout = taskLayouts[taskId];
21+
if (layout) {
22+
const result = findTabInTree(layout.panelTree, DEFAULT_TAB_IDS.LOGS);
23+
if (result) {
24+
setActiveTab(taskId, result.panelId, DEFAULT_TAB_IDS.LOGS);
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)