feat(web): add agent message comments (#1835)
* feat(web): add agent message comments * fix(web): harden agent message comments * fix(web): address message comment review findings * fix(web): allow comments on legacy agent messages * fix(web): address latest message comment review * fix(web): address follow-up message comment review * fix(web): address remaining message comment review * fix(web): keep failed mobile comments open * fix(web): preserve selections over message comments
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { buildContextItems } from "./chat-context-items";
|
||||
import type { AgentMessageComment } from "@/lib/state/slices/comments";
|
||||
|
||||
const comment: AgentMessageComment = {
|
||||
id: "message-comment-1",
|
||||
sessionId: "session-1",
|
||||
source: "agent-message",
|
||||
messageId: "reply-1",
|
||||
selectedText: "selected answer",
|
||||
anchor: {
|
||||
messageId: "reply-1",
|
||||
start: 0,
|
||||
end: 15,
|
||||
selectedText: "selected answer",
|
||||
prefix: "",
|
||||
suffix: " continues",
|
||||
},
|
||||
text: "Please clarify this.",
|
||||
createdAt: "2026-07-20T00:00:00Z",
|
||||
status: "pending",
|
||||
};
|
||||
|
||||
describe("buildContextItems agent-message comments", () => {
|
||||
it("creates one removable context item for pending message comments", () => {
|
||||
const items = buildContextItems({
|
||||
planContextEnabled: false,
|
||||
contextFiles: [],
|
||||
resolvedSessionId: "session-1",
|
||||
removeContextFile: vi.fn(),
|
||||
unpinFile: vi.fn(),
|
||||
addPlan: vi.fn(),
|
||||
promptsMap: new Map(),
|
||||
pendingCommentsByFile: {},
|
||||
handleRemoveCommentFile: vi.fn(),
|
||||
handleRemoveComment: vi.fn(),
|
||||
planComments: [],
|
||||
handleClearPlanComments: vi.fn(),
|
||||
pendingPRFeedback: [],
|
||||
handleRemovePRFeedback: vi.fn(),
|
||||
handleClearPRFeedback: vi.fn(),
|
||||
walkthroughComments: [],
|
||||
handleRemoveWalkthroughComment: vi.fn(),
|
||||
handleClearWalkthroughComments: vi.fn(),
|
||||
messageComments: [comment],
|
||||
handleClearMessageComments: vi.fn(),
|
||||
taskId: "task-1",
|
||||
} as never);
|
||||
|
||||
const item = items.find((candidate) => candidate.kind === "agent-message-comment");
|
||||
expect(item).toMatchObject({
|
||||
kind: "agent-message-comment",
|
||||
label: "1 message comment",
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -6,6 +6,7 @@ import type {
|
||||
PlanComment,
|
||||
PRFeedbackComment,
|
||||
WalkthroughComment,
|
||||
AgentMessageComment,
|
||||
} from "@/lib/state/slices/comments";
|
||||
|
||||
const PLAN_CONTEXT_PATH = "plan:context";
|
||||
@@ -31,6 +32,8 @@ export type BuildContextItemsParams = {
|
||||
walkthroughComments: WalkthroughComment[];
|
||||
handleRemoveWalkthroughComment: (commentId: string) => void;
|
||||
handleClearWalkthroughComments: () => void;
|
||||
messageComments: AgentMessageComment[];
|
||||
handleClearMessageComments: () => void;
|
||||
taskId: string | null;
|
||||
};
|
||||
|
||||
@@ -192,6 +195,20 @@ function buildWalkthroughCommentItems(params: BuildContextItemsParams): ContextI
|
||||
];
|
||||
}
|
||||
|
||||
function buildAgentMessageCommentItems(params: BuildContextItemsParams): ContextItem[] {
|
||||
const { messageComments, handleClearMessageComments } = params;
|
||||
if (messageComments.length === 0) return [];
|
||||
return [
|
||||
{
|
||||
kind: "agent-message-comment" as const,
|
||||
id: "agent-message-comments",
|
||||
label: `${messageComments.length} message comment${messageComments.length === 1 ? "" : "s"}`,
|
||||
comments: messageComments,
|
||||
onRemove: handleClearMessageComments,
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
/** Sort: pinned first, then by kind order, then by label */
|
||||
const KIND_ORDER: Record<string, number> = {
|
||||
plan: 0,
|
||||
@@ -200,8 +217,9 @@ const KIND_ORDER: Record<string, number> = {
|
||||
comment: 3,
|
||||
"plan-comment": 4,
|
||||
"walkthrough-comment": 5,
|
||||
image: 6,
|
||||
"pr-feedback": 7,
|
||||
"agent-message-comment": 6,
|
||||
image: 7,
|
||||
"pr-feedback": 8,
|
||||
};
|
||||
|
||||
export function contextItemSortFn(a: ContextItem, b: ContextItem): number {
|
||||
@@ -221,6 +239,7 @@ export function buildContextItems(params: BuildContextItemsParams): ContextItem[
|
||||
items.push(...buildFileAndPromptItems(params));
|
||||
items.push(...buildCommentItems(params));
|
||||
items.push(...buildWalkthroughCommentItems(params));
|
||||
items.push(...buildAgentMessageCommentItems(params));
|
||||
items.push(...buildPRFeedbackItems(params));
|
||||
|
||||
if (params.planComments.length > 0) {
|
||||
|
||||
@@ -23,6 +23,10 @@ vi.mock("@/components/shared/markdown-components", () => ({
|
||||
|
||||
import { MemoizedMarkdown } from "@/components/shared/memoized-markdown";
|
||||
import { __markdownParseCount, __resetMarkdownCounters } from "@/lib/markdown/normalize-cache";
|
||||
import {
|
||||
createMessageTextAnchor,
|
||||
getMessageCommentDecorations,
|
||||
} from "@/lib/chat/agent-message-comments";
|
||||
|
||||
type Row = { id: string; content: string };
|
||||
|
||||
@@ -121,4 +125,30 @@ describe("chat render-cost evals", () => {
|
||||
render(<MessageList rows={rows} panel={false} />);
|
||||
expect(__markdownParseCount()).toBe(1);
|
||||
});
|
||||
|
||||
it("scenario 5: restoring many inline highlights does not reparse markdown rows", () => {
|
||||
const content = Array.from({ length: 20 }, (_, index) => `reply-${index}`).join(" ");
|
||||
const root = document.createElement("div");
|
||||
root.textContent = content;
|
||||
const comments = Array.from({ length: 20 }, (_, index) => {
|
||||
const selectedText = `reply-${index}`;
|
||||
const start = content.indexOf(selectedText);
|
||||
return {
|
||||
id: `comment-${index}`,
|
||||
sessionId: "session-1",
|
||||
source: "agent-message" as const,
|
||||
messageId: "reply-1",
|
||||
selectedText,
|
||||
text: "feedback",
|
||||
createdAt: "2026-07-20T00:00:00Z",
|
||||
status: "pending" as const,
|
||||
anchor: createMessageTextAnchor("reply-1", content, start, start + selectedText.length),
|
||||
};
|
||||
});
|
||||
|
||||
const parsesBefore = __markdownParseCount();
|
||||
expect(getMessageCommentDecorations(root, comments)).toHaveLength(20);
|
||||
expect(root.querySelectorAll("mark[data-agent-message-comment-id]")).toHaveLength(0);
|
||||
expect(__markdownParseCount() - parsesBefore).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { buildSubmitMessage } from "./chat-input-area";
|
||||
import type { AgentMessageComment } from "@/lib/state/slices/comments";
|
||||
|
||||
const messageComment: AgentMessageComment = {
|
||||
id: "message-comment-1",
|
||||
sessionId: "session-1",
|
||||
source: "agent-message",
|
||||
messageId: "reply-1",
|
||||
selectedText: "answer",
|
||||
anchor: {
|
||||
messageId: "reply-1",
|
||||
start: 0,
|
||||
end: 6,
|
||||
selectedText: "answer",
|
||||
prefix: "",
|
||||
suffix: "",
|
||||
},
|
||||
text: "Please make this more precise.",
|
||||
createdAt: "2026-07-20T00:00:00Z",
|
||||
status: "pending",
|
||||
};
|
||||
|
||||
describe("buildSubmitMessage agent message comments", () => {
|
||||
it("includes selected response context while preserving ordinary prose", () => {
|
||||
const result = buildSubmitMessage({
|
||||
message: "Continue from here.",
|
||||
pendingPRFeedback: [],
|
||||
planComments: [],
|
||||
messageComments: [messageComment],
|
||||
});
|
||||
|
||||
expect(result).toContain("### Agent Message Comments");
|
||||
expect(result).toContain("> answer");
|
||||
expect(result).toContain("Continue from here.");
|
||||
});
|
||||
});
|
||||
@@ -73,6 +73,7 @@ vi.mock("@/lib/ws/connection", () => ({
|
||||
import { useSubmitHandler } from "./chat-input-area";
|
||||
|
||||
beforeEach(() => {
|
||||
handleSendMessageMock.mockReset();
|
||||
handleSendMessageMock.mockResolvedValue(undefined);
|
||||
});
|
||||
|
||||
@@ -123,4 +124,71 @@ describe("useSubmitHandler", () => {
|
||||
variant: "error",
|
||||
});
|
||||
});
|
||||
|
||||
it("reports deterministic preflight failures as not sent", async () => {
|
||||
vi.spyOn(console, "error").mockImplementation(() => undefined);
|
||||
const error = Object.assign(new Error("Connection unavailable. Reconnect and try again."), {
|
||||
name: "MessageSendError",
|
||||
code: "connection-unavailable",
|
||||
});
|
||||
handleSendMessageMock.mockRejectedValueOnce(error);
|
||||
const { result } = renderHook(() => useSubmitHandler(panelState()));
|
||||
|
||||
await act(async () => {
|
||||
await result.current.handleSubmit("hello");
|
||||
});
|
||||
|
||||
expect(toastMock).toHaveBeenCalledWith({
|
||||
title: "Message not sent",
|
||||
description: "Connection unavailable. Reconnect and try again.",
|
||||
variant: "error",
|
||||
});
|
||||
});
|
||||
|
||||
it("keeps agent message comments pending when sending fails", async () => {
|
||||
vi.spyOn(console, "error").mockImplementation(() => undefined);
|
||||
const markCommentsSent = vi.fn();
|
||||
handleSendMessageMock.mockRejectedValueOnce(new Error("send failed"));
|
||||
const { result } = renderHook(() =>
|
||||
useSubmitHandler(
|
||||
panelState({
|
||||
messageComments: [{ id: "comment-1" }],
|
||||
markCommentsSent,
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
await act(async () => {
|
||||
await result.current.handleSubmit("hello");
|
||||
});
|
||||
|
||||
expect(markCommentsSent).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("includes each agent message comment once and marks it sent after success", async () => {
|
||||
const markCommentsSent = vi.fn();
|
||||
const comment = {
|
||||
id: "comment-1",
|
||||
selectedText: "settled answer",
|
||||
text: "Please expand this.",
|
||||
};
|
||||
const { result } = renderHook(() =>
|
||||
useSubmitHandler(
|
||||
panelState({
|
||||
messageComments: [comment],
|
||||
markCommentsSent,
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
await act(async () => {
|
||||
await result.current.handleSubmit("hello");
|
||||
});
|
||||
|
||||
const [sentMessage] = handleSendMessageMock.mock.calls[0] as [string];
|
||||
expect(sentMessage.match(/### Agent Message Comments/g)).toHaveLength(1);
|
||||
expect(sentMessage).toContain("> settled answer");
|
||||
expect(sentMessage).toContain("> Please expand this.");
|
||||
expect(markCommentsSent).toHaveBeenCalledWith(["comment-1"]);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -28,23 +28,34 @@ import {
|
||||
formatPRFeedbackAsMarkdown,
|
||||
formatPlanCommentsAsMarkdown,
|
||||
formatWalkthroughCommentsAsMarkdown,
|
||||
formatAgentMessageCommentsAsMarkdown,
|
||||
} from "@/lib/state/slices/comments/format";
|
||||
import { usePlanActions } from "@/hooks/domains/kanban/use-plan-actions";
|
||||
import { useExecutorEnvironmentAvailability } from "@/hooks/domains/session/use-executor-environment-availability";
|
||||
import { useToast } from "@/components/toast-provider";
|
||||
import { isMessageSendError } from "@/lib/chat/message-send-error";
|
||||
import type { DiffComment } from "@/lib/diff/types";
|
||||
import type { AgentMessageComment } from "@/lib/state/slices/comments";
|
||||
import type { useChatPanelState } from "./use-chat-panel-state";
|
||||
import { cn } from "@/lib/utils";
|
||||
|
||||
const PLAN_CONTEXT_PATH = "plan:context";
|
||||
|
||||
export function buildSubmitMessage(
|
||||
message: string,
|
||||
reviewComments: DiffComment[] | undefined,
|
||||
pendingPRFeedback: import("@/lib/state/slices/comments").PRFeedbackComment[],
|
||||
planComments: import("@/lib/state/slices/comments").PlanComment[],
|
||||
walkthroughComments: import("@/lib/state/slices/comments").WalkthroughComment[] = [],
|
||||
): string {
|
||||
export function buildSubmitMessage({
|
||||
message,
|
||||
reviewComments,
|
||||
pendingPRFeedback,
|
||||
planComments,
|
||||
walkthroughComments = [],
|
||||
messageComments = [],
|
||||
}: {
|
||||
message: string;
|
||||
reviewComments?: DiffComment[];
|
||||
pendingPRFeedback: import("@/lib/state/slices/comments").PRFeedbackComment[];
|
||||
planComments: import("@/lib/state/slices/comments").PlanComment[];
|
||||
walkthroughComments?: import("@/lib/state/slices/comments").WalkthroughComment[];
|
||||
messageComments?: AgentMessageComment[];
|
||||
}): string {
|
||||
let finalMessage = message;
|
||||
if (reviewComments && reviewComments.length > 0) {
|
||||
finalMessage = formatReviewCommentsAsMarkdown(reviewComments) + (message || "");
|
||||
@@ -59,6 +70,12 @@ export function buildSubmitMessage(
|
||||
const planMarkdown = formatPlanCommentsAsMarkdown(planComments);
|
||||
finalMessage = finalMessage ? `${planMarkdown}${finalMessage}` : planMarkdown;
|
||||
}
|
||||
if (messageComments.length > 0) {
|
||||
const messageCommentsMarkdown = formatAgentMessageCommentsAsMarkdown(messageComments);
|
||||
finalMessage = finalMessage
|
||||
? `${messageCommentsMarkdown}${finalMessage}`
|
||||
: messageCommentsMarkdown;
|
||||
}
|
||||
return finalMessage;
|
||||
}
|
||||
|
||||
@@ -101,8 +118,16 @@ function pickInputPlaceholder(a: PlaceholderArgs): string {
|
||||
);
|
||||
}
|
||||
|
||||
function showUnknownMessageSendToast(error: unknown, toast: ReturnType<typeof useToast>["toast"]) {
|
||||
function showMessageSendToast(error: unknown, toast: ReturnType<typeof useToast>["toast"]) {
|
||||
console.error("Failed to send message:", error);
|
||||
if (isMessageSendError(error)) {
|
||||
toast({
|
||||
title: "Message not sent",
|
||||
description: error.message,
|
||||
variant: "error",
|
||||
});
|
||||
return;
|
||||
}
|
||||
toast({
|
||||
title: "Message send status unknown",
|
||||
description:
|
||||
@@ -148,6 +173,7 @@ export function useSubmitHandler(
|
||||
planComments,
|
||||
pendingPRFeedback,
|
||||
walkthroughComments,
|
||||
messageComments,
|
||||
markCommentsSent,
|
||||
clearSessionPlanComments,
|
||||
handleClearPRFeedback,
|
||||
@@ -169,13 +195,14 @@ export function useSubmitHandler(
|
||||
if (isSending) return;
|
||||
setIsSending(true);
|
||||
try {
|
||||
const finalMessage = buildSubmitMessage(
|
||||
const finalMessage = buildSubmitMessage({
|
||||
message,
|
||||
reviewComments,
|
||||
pendingPRFeedback,
|
||||
planComments,
|
||||
walkthroughComments,
|
||||
);
|
||||
messageComments,
|
||||
});
|
||||
const hasReviewComments = !!(reviewComments && reviewComments.length > 0);
|
||||
if (onSend) {
|
||||
// Expand task mentions because onSend bypasses useMessageHandler.buildFinalMessage.
|
||||
@@ -194,6 +221,7 @@ export function useSubmitHandler(
|
||||
}
|
||||
if (reviewComments && reviewComments.length > 0)
|
||||
markCommentsSent(reviewComments.map((c) => c.id));
|
||||
if (messageComments.length > 0) markCommentsSent(messageComments.map((c) => c.id));
|
||||
if (pendingPRFeedback.length > 0) handleClearPRFeedback();
|
||||
if (walkthroughComments.length > 0) handleClearWalkthroughComments();
|
||||
if (planComments.length > 0) clearSessionPlanComments();
|
||||
@@ -205,7 +233,7 @@ export function useSubmitHandler(
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
showUnknownMessageSendToast(error, toast);
|
||||
showMessageSendToast(error, toast);
|
||||
return false;
|
||||
} finally {
|
||||
setIsSending(false);
|
||||
@@ -220,6 +248,7 @@ export function useSubmitHandler(
|
||||
planComments,
|
||||
clearSessionPlanComments,
|
||||
walkthroughComments,
|
||||
messageComments,
|
||||
handleClearWalkthroughComments,
|
||||
pendingPRFeedback,
|
||||
handleClearPRFeedback,
|
||||
@@ -482,7 +511,8 @@ export function ChatInputArea({
|
||||
hasContextComments={
|
||||
panelState.planComments.length > 0 ||
|
||||
panelState.pendingPRFeedback.length > 0 ||
|
||||
panelState.walkthroughComments.length > 0
|
||||
panelState.walkthroughComments.length > 0 ||
|
||||
panelState.messageComments.length > 0
|
||||
}
|
||||
submitKey={panelState.chatSubmitKey}
|
||||
hasAgentCommands={!!(panelState.agentCommands && panelState.agentCommands.length > 0)}
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
"use client";
|
||||
|
||||
import { memo } from "react";
|
||||
import type { AgentMessageCommentContextItem } from "@/lib/types/context";
|
||||
import { SelectionCommentItem } from "./selection-comment-item";
|
||||
|
||||
export const AgentMessageCommentItem = memo(function AgentMessageCommentItem({
|
||||
item,
|
||||
}: {
|
||||
item: AgentMessageCommentContextItem;
|
||||
}) {
|
||||
return (
|
||||
<SelectionCommentItem
|
||||
kind="agent-message-comment"
|
||||
label={item.label}
|
||||
comments={item.comments}
|
||||
onRemove={item.onRemove}
|
||||
/>
|
||||
);
|
||||
});
|
||||
@@ -26,6 +26,7 @@ const ICON_BY_KIND: Record<ContextItemKind, TablerIcon> = {
|
||||
"file-attachment": IconFile,
|
||||
prompt: IconAt,
|
||||
"pr-feedback": IconGitPullRequest,
|
||||
"agent-message-comment": IconMessageDots,
|
||||
};
|
||||
|
||||
type ContextChipProps = {
|
||||
@@ -87,7 +88,8 @@ export const ContextChip = memo(function ContextChip({
|
||||
e.stopPropagation();
|
||||
onRemove();
|
||||
}}
|
||||
className="opacity-0 group-hover:opacity-100 ml-0.5 hover:text-foreground cursor-pointer"
|
||||
aria-label={`Remove ${label}`}
|
||||
className="min-h-11 min-w-11 opacity-100 ml-0.5 hover:text-foreground cursor-pointer sm:min-h-0 sm:min-w-0 sm:opacity-0 sm:group-hover:opacity-100"
|
||||
>
|
||||
<IconX className="h-2.5 w-2.5" />
|
||||
</button>
|
||||
|
||||
@@ -10,6 +10,7 @@ import { ImageItem } from "./image-item";
|
||||
import { FileAttachmentItem } from "./file-attachment-item";
|
||||
import { PRFeedbackItem } from "./pr-feedback-item";
|
||||
import { WalkthroughCommentItem } from "./walkthrough-comment-item";
|
||||
import { AgentMessageCommentItem } from "./agent-message-comment-item";
|
||||
|
||||
export function ContextItemRenderer({
|
||||
item,
|
||||
@@ -37,5 +38,7 @@ export function ContextItemRenderer({
|
||||
return <PRFeedbackItem item={item} />;
|
||||
case "walkthrough-comment":
|
||||
return <WalkthroughCommentItem item={item} />;
|
||||
case "agent-message-comment":
|
||||
return <AgentMessageCommentItem item={item} />;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,33 +2,18 @@
|
||||
|
||||
import { memo } from "react";
|
||||
import type { PlanCommentContextItem } from "@/lib/types/context";
|
||||
import { ContextChip } from "./context-chip";
|
||||
import { SelectionCommentItem } from "./selection-comment-item";
|
||||
|
||||
export const PlanCommentItem = memo(function PlanCommentItem({
|
||||
item,
|
||||
}: {
|
||||
item: PlanCommentContextItem;
|
||||
}) {
|
||||
const preview = (
|
||||
<div className="space-y-1.5">
|
||||
{item.comments.map((comment) => (
|
||||
<div key={comment.id} className="text-xs space-y-0.5">
|
||||
{comment.selectedText && (
|
||||
<div className="text-muted-foreground italic line-clamp-2">
|
||||
“{comment.selectedText}”
|
||||
</div>
|
||||
)}
|
||||
<div className="break-words">{comment.text}</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
);
|
||||
|
||||
return (
|
||||
<ContextChip
|
||||
<SelectionCommentItem
|
||||
kind="plan-comment"
|
||||
label={item.label}
|
||||
preview={preview}
|
||||
comments={item.comments}
|
||||
onClick={item.onOpen}
|
||||
onRemove={item.onRemove}
|
||||
/>
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
"use client";
|
||||
|
||||
import { memo } from "react";
|
||||
import { ContextChip } from "./context-chip";
|
||||
|
||||
type SelectionComment = {
|
||||
id: string;
|
||||
selectedText: string;
|
||||
text: string;
|
||||
};
|
||||
|
||||
export const SelectionCommentItem = memo(function SelectionCommentItem({
|
||||
kind,
|
||||
label,
|
||||
comments,
|
||||
onClick,
|
||||
onRemove,
|
||||
}: {
|
||||
kind: "plan-comment" | "agent-message-comment";
|
||||
label: string;
|
||||
comments: SelectionComment[];
|
||||
onClick?: () => void;
|
||||
onRemove?: () => void;
|
||||
}) {
|
||||
const preview = (
|
||||
<div className="space-y-1.5">
|
||||
{comments.map((comment) => (
|
||||
<div key={comment.id} className="space-y-0.5 text-xs">
|
||||
{comment.selectedText ? (
|
||||
<div className="line-clamp-2 text-muted-foreground italic">
|
||||
“{comment.selectedText}”
|
||||
</div>
|
||||
) : null}
|
||||
<div className="break-words">{comment.text}</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
);
|
||||
|
||||
return (
|
||||
<ContextChip
|
||||
kind={kind}
|
||||
label={label}
|
||||
preview={preview}
|
||||
onClick={onClick}
|
||||
onRemove={onRemove}
|
||||
/>
|
||||
);
|
||||
});
|
||||
@@ -15,6 +15,7 @@ import {
|
||||
getConversationLoadingState,
|
||||
getSessionRunningState,
|
||||
getLastTurnGroupId,
|
||||
getStreamingAgentMessageId,
|
||||
} from "./message-list-shared";
|
||||
|
||||
/**
|
||||
@@ -195,6 +196,7 @@ export const NativeMessageList = memo(function NativeMessageList({
|
||||
});
|
||||
const { loadMore, hasMore, isLoading: isLoadingMore } = useLazyLoadMessages(sessionId);
|
||||
const isRunning = getSessionRunningState(sessionState);
|
||||
const streamingMessageId = getStreamingAgentMessageId(messages);
|
||||
const lastTurnGroupId = useMemo(() => getLastTurnGroupId(items), [items]);
|
||||
const handleScrollToMessage = useScrollToMessage();
|
||||
|
||||
@@ -247,6 +249,7 @@ export const NativeMessageList = memo(function NativeMessageList({
|
||||
onOpenFile={onOpenFile}
|
||||
isLastGroup={item.type === "turn_group" && item.id === lastTurnGroupId}
|
||||
isTurnActive={isRunning}
|
||||
streamingMessageId={streamingMessageId}
|
||||
onScrollToMessage={handleScrollToMessage}
|
||||
/>
|
||||
</div>
|
||||
|
||||
@@ -61,7 +61,12 @@ vi.mock("@/lib/api/domains/session-api", () => ({
|
||||
dismissLastAgentError: vi.fn(),
|
||||
}));
|
||||
|
||||
import { MessageItem, MessageListStatus, getConversationLoadingState } from "./message-list-shared";
|
||||
import {
|
||||
MessageItem,
|
||||
MessageListStatus,
|
||||
getConversationLoadingState,
|
||||
getStreamingAgentMessageId,
|
||||
} from "./message-list-shared";
|
||||
|
||||
const item: RenderItem = { type: "message", message: { id: "m1" } as Message };
|
||||
const noop = () => {};
|
||||
@@ -214,6 +219,25 @@ describe("getConversationLoadingState", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("getStreamingAgentMessageId", () => {
|
||||
it("only marks an agent reply after the latest user prompt as streaming", () => {
|
||||
const message = (id: string, author_type: "user" | "agent", type = "message") =>
|
||||
({ id, author_type, type }) as Message;
|
||||
|
||||
expect(
|
||||
getStreamingAgentMessageId([
|
||||
message("old-reply", "agent"),
|
||||
message("prompt", "user"),
|
||||
message("reply", "agent"),
|
||||
message("status", "agent", "status"),
|
||||
]),
|
||||
).toBe("reply");
|
||||
expect(getStreamingAgentMessageId([message("auto-started-reply", "agent")])).toBe(
|
||||
"auto-started-reply",
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("MessageListStatus", () => {
|
||||
it("renders a conversation loading indicator while existing content remains visible", () => {
|
||||
render(
|
||||
|
||||
@@ -47,6 +47,27 @@ export function getSessionRunningState(sessionState: string | null | undefined)
|
||||
return sessionState === "CREATED" || sessionState === "STARTING" || sessionState === "RUNNING";
|
||||
}
|
||||
|
||||
/** Only the latest ordinary agent row in the active turn can be the streaming reply. */
|
||||
export function getStreamingAgentMessageId(messages: Message[]): string | null {
|
||||
let latestUserIndex = -1;
|
||||
for (let i = messages.length - 1; i >= 0; i--) {
|
||||
if (messages[i].author_type === "user") {
|
||||
latestUserIndex = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
for (let i = messages.length - 1; i > latestUserIndex; i--) {
|
||||
const message = messages[i];
|
||||
if (
|
||||
message.author_type === "agent" &&
|
||||
(message.type === "message" || message.type === "content" || !message.type)
|
||||
) {
|
||||
return message.id;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
export function getLastTurnGroupId(items: RenderItem[]) {
|
||||
for (let i = items.length - 1; i >= 0; i--) {
|
||||
const item = items[i];
|
||||
@@ -220,6 +241,7 @@ export const MessageItem = memo(function MessageItem({
|
||||
onOpenFile,
|
||||
isLastGroup,
|
||||
isTurnActive,
|
||||
streamingMessageId,
|
||||
onScrollToMessage,
|
||||
}: {
|
||||
item: RenderItem;
|
||||
@@ -231,6 +253,7 @@ export const MessageItem = memo(function MessageItem({
|
||||
onOpenFile?: (path: string) => void;
|
||||
isLastGroup: boolean;
|
||||
isTurnActive: boolean;
|
||||
streamingMessageId?: string | null;
|
||||
onScrollToMessage: (id: string) => void;
|
||||
}) {
|
||||
if (item.type === "prepare_progress") {
|
||||
@@ -250,7 +273,13 @@ export const MessageItem = memo(function MessageItem({
|
||||
worktreePath={worktreePath}
|
||||
onOpenFile={onOpenFile}
|
||||
isLastGroup={isLastGroup}
|
||||
isTurnActive={isTurnActive}
|
||||
isTurnActive={
|
||||
isTurnActive &&
|
||||
(streamingMessageId
|
||||
? item.messages.some((message) => message.id === streamingMessageId)
|
||||
: false)
|
||||
}
|
||||
streamingMessageId={streamingMessageId}
|
||||
onScrollToMessage={onScrollToMessage}
|
||||
/>
|
||||
);
|
||||
@@ -264,6 +293,7 @@ export const MessageItem = memo(function MessageItem({
|
||||
childrenByParentToolCallId={childrenByParentToolCallId}
|
||||
worktreePath={worktreePath}
|
||||
sessionId={sessionId ?? undefined}
|
||||
isTurnActive={isTurnActive && item.message.id === streamingMessageId}
|
||||
onOpenFile={onOpenFile}
|
||||
onScrollToMessage={onScrollToMessage}
|
||||
/>
|
||||
|
||||
@@ -17,6 +17,7 @@ import {
|
||||
getConversationLoadingState,
|
||||
getSessionRunningState,
|
||||
getLastTurnGroupId,
|
||||
getStreamingAgentMessageId,
|
||||
} from "./message-list-shared";
|
||||
import { createDebugLogger, isDebug } from "@/lib/debug/log";
|
||||
|
||||
@@ -97,11 +98,19 @@ function useStableFirstItemIndex(items: RenderItem[]) {
|
||||
}
|
||||
|
||||
function useVirtuosoCallbacks(props: VirtuosoBodyProps) {
|
||||
const { items, sessionId, permissionsByToolCallId, childrenByParentToolCallId, taskId } = props;
|
||||
const {
|
||||
items,
|
||||
messages,
|
||||
sessionId,
|
||||
permissionsByToolCallId,
|
||||
childrenByParentToolCallId,
|
||||
taskId,
|
||||
} = props;
|
||||
const { worktreePath, onOpenFile, lastTurnGroupId, isRunning } = props;
|
||||
const { hasMore, isLoadingMore, loadMore } = props;
|
||||
const virtuosoRef = useRef<VirtuosoHandle>(null);
|
||||
const itemCount = items.length;
|
||||
const streamingMessageId = getStreamingAgentMessageId(messages);
|
||||
const firstItemIndex = useStableFirstItemIndex(items);
|
||||
|
||||
const loadCooldownRef = useRef(false);
|
||||
@@ -155,6 +164,7 @@ function useVirtuosoCallbacks(props: VirtuosoBodyProps) {
|
||||
onOpenFile={onOpenFile}
|
||||
isLastGroup={item.type === "turn_group" && item.id === lastTurnGroupId}
|
||||
isTurnActive={isRunning}
|
||||
streamingMessageId={streamingMessageId}
|
||||
onScrollToMessage={handleScrollToMessage}
|
||||
/>
|
||||
</div>
|
||||
@@ -171,6 +181,7 @@ function useVirtuosoCallbacks(props: VirtuosoBodyProps) {
|
||||
onOpenFile,
|
||||
lastTurnGroupId,
|
||||
isRunning,
|
||||
streamingMessageId,
|
||||
handleScrollToMessage,
|
||||
],
|
||||
);
|
||||
|
||||
@@ -39,6 +39,7 @@ type AdapterContext = {
|
||||
sessionId?: string;
|
||||
onOpenFile?: (path: string) => void;
|
||||
onScrollToMessage?: (messageId: string) => void;
|
||||
isTurnActive?: boolean;
|
||||
};
|
||||
|
||||
function TaskDescriptionStartButton({ taskId, sessionId }: { taskId: string; sessionId: string }) {
|
||||
@@ -98,6 +99,7 @@ function TaskDescriptionMessage({
|
||||
worktreePath,
|
||||
onOpenFile,
|
||||
onScrollToMessage,
|
||||
isTurnActive = false,
|
||||
}: {
|
||||
comment: Message;
|
||||
taskId?: string;
|
||||
@@ -105,6 +107,7 @@ function TaskDescriptionMessage({
|
||||
worktreePath?: string;
|
||||
onOpenFile?: (path: string) => void;
|
||||
onScrollToMessage?: (messageId: string) => void;
|
||||
isTurnActive?: boolean;
|
||||
}) {
|
||||
const sessionState = useSessionStateValue(sessionId);
|
||||
const task = useTask(taskId ?? null);
|
||||
@@ -117,6 +120,7 @@ function TaskDescriptionMessage({
|
||||
className="bg-muted/40 text-foreground border-border/60"
|
||||
showRichBlocks={comment.type === "message" || comment.type === "content" || !comment.type}
|
||||
sessionId={sessionId}
|
||||
isTurnActive={isTurnActive}
|
||||
worktreePath={worktreePath}
|
||||
onOpenFile={onOpenFile}
|
||||
onScrollToMessage={onScrollToMessage}
|
||||
@@ -132,6 +136,7 @@ function TaskDescriptionMessage({
|
||||
label="You"
|
||||
className="bg-primary/10 text-foreground border-primary/30"
|
||||
sessionId={sessionId}
|
||||
isTurnActive={isTurnActive}
|
||||
worktreePath={worktreePath}
|
||||
onOpenFile={onOpenFile}
|
||||
onScrollToMessage={onScrollToMessage}
|
||||
@@ -322,6 +327,7 @@ const adapters: MessageAdapter[] = [
|
||||
worktreePath={ctx.worktreePath}
|
||||
onOpenFile={ctx.onOpenFile}
|
||||
onScrollToMessage={ctx.onScrollToMessage}
|
||||
isTurnActive={ctx.isTurnActive}
|
||||
/>
|
||||
);
|
||||
}
|
||||
@@ -335,6 +341,7 @@ const adapters: MessageAdapter[] = [
|
||||
worktreePath={ctx.worktreePath}
|
||||
onOpenFile={ctx.onOpenFile}
|
||||
onScrollToMessage={ctx.onScrollToMessage}
|
||||
isTurnActive={ctx.isTurnActive}
|
||||
/>
|
||||
);
|
||||
}
|
||||
@@ -348,6 +355,7 @@ const adapters: MessageAdapter[] = [
|
||||
worktreePath={ctx.worktreePath}
|
||||
onOpenFile={ctx.onOpenFile}
|
||||
onScrollToMessage={ctx.onScrollToMessage}
|
||||
isTurnActive={ctx.isTurnActive}
|
||||
/>
|
||||
);
|
||||
},
|
||||
@@ -364,6 +372,7 @@ type MessageRendererProps = {
|
||||
sessionId?: string;
|
||||
onOpenFile?: (path: string) => void;
|
||||
onScrollToMessage?: (messageId: string) => void;
|
||||
isTurnActive?: boolean;
|
||||
};
|
||||
|
||||
export const MessageRenderer = memo(function MessageRenderer({
|
||||
@@ -376,6 +385,7 @@ export const MessageRenderer = memo(function MessageRenderer({
|
||||
sessionId,
|
||||
onOpenFile,
|
||||
onScrollToMessage,
|
||||
isTurnActive = false,
|
||||
}: MessageRendererProps) {
|
||||
const ctx = {
|
||||
isTaskDescription,
|
||||
@@ -386,6 +396,7 @@ export const MessageRenderer = memo(function MessageRenderer({
|
||||
sessionId,
|
||||
onOpenFile,
|
||||
onScrollToMessage,
|
||||
isTurnActive,
|
||||
};
|
||||
const adapter =
|
||||
adapters.find((entry) => entry.matches(comment, ctx)) ?? adapters[adapters.length - 1];
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
"use client";
|
||||
|
||||
import { memo } from "react";
|
||||
import type { Message } from "@/lib/types/http";
|
||||
import { RichBlocks } from "@/components/task/chat/messages/rich-blocks";
|
||||
import { MessageActions } from "@/components/task/chat/messages/message-actions";
|
||||
import { MemoizedMarkdown } from "@/components/shared/memoized-markdown";
|
||||
import { MessageCommentSurface } from "./message-comment-surface";
|
||||
|
||||
type AgentMessageContentProps = {
|
||||
comment: Message;
|
||||
showRaw: boolean;
|
||||
onToggleRaw: () => void;
|
||||
showRichBlocks?: boolean;
|
||||
worktreePath?: string;
|
||||
onOpenFile?: (path: string) => void;
|
||||
sessionId?: string | null;
|
||||
isTurnActive: boolean;
|
||||
};
|
||||
|
||||
export const AgentMessageContent = memo(function AgentMessageContent({
|
||||
comment,
|
||||
showRaw,
|
||||
onToggleRaw,
|
||||
showRichBlocks,
|
||||
worktreePath,
|
||||
onOpenFile,
|
||||
sessionId,
|
||||
isTurnActive,
|
||||
}: AgentMessageContentProps) {
|
||||
return (
|
||||
<div className="flex items-start gap-2 sm:gap-3 w-full group">
|
||||
<div className="flex-1 min-w-0">
|
||||
{showRaw ? (
|
||||
<pre className="whitespace-pre-wrap font-mono text-xs bg-muted/20 p-3 rounded-md">
|
||||
{comment.raw_content || comment.content || "(empty)"}
|
||||
</pre>
|
||||
) : (
|
||||
<MessageCommentSurface
|
||||
message={comment}
|
||||
sessionId={sessionId}
|
||||
isTurnActive={isTurnActive}
|
||||
>
|
||||
<div className="markdown-body max-w-none">
|
||||
<MemoizedMarkdown
|
||||
content={comment.content || "(empty)"}
|
||||
worktreePath={worktreePath}
|
||||
onOpenFile={onOpenFile}
|
||||
/>
|
||||
</div>
|
||||
</MessageCommentSurface>
|
||||
)}
|
||||
{!showRaw && showRichBlocks ? <RichBlocks comment={comment} /> : null}
|
||||
<MessageActions
|
||||
message={comment}
|
||||
showCopy={true}
|
||||
showTimestamp={true}
|
||||
showRawToggle={true}
|
||||
showModel={true}
|
||||
showNavigation={false}
|
||||
isRawView={showRaw}
|
||||
onToggleRaw={onToggleRaw}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
});
|
||||
@@ -13,7 +13,6 @@ import type { Components } from "react-markdown";
|
||||
import { IconWand, IconMessageDots, IconFile } from "@tabler/icons-react";
|
||||
import { cn } from "@/lib/utils";
|
||||
import type { Message } from "@/lib/types/http";
|
||||
import { RichBlocks } from "@/components/task/chat/messages/rich-blocks";
|
||||
import { MessageActions } from "@/components/task/chat/messages/message-actions";
|
||||
import { useUserMessageNavigation } from "@/hooks/use-message-navigation";
|
||||
import { SenderTaskBadge, type SenderTaskInfo } from "./sender-task-badge";
|
||||
@@ -33,6 +32,7 @@ import {
|
||||
type WorkflowMessageMetadata,
|
||||
type WorkflowStepMessageInfo,
|
||||
} from "./workflow-step-message-badge";
|
||||
import { AgentMessageContent } from "./agent-message-content";
|
||||
|
||||
type ChatMessageProps = {
|
||||
comment: Message;
|
||||
@@ -43,6 +43,7 @@ type ChatMessageProps = {
|
||||
worktreePath?: string;
|
||||
onOpenFile?: (path: string) => void;
|
||||
onScrollToMessage?: (messageId: string) => void;
|
||||
isTurnActive?: boolean;
|
||||
};
|
||||
|
||||
// Regex to match @file references (file paths after @)
|
||||
@@ -479,57 +480,6 @@ function UserMessageContent({
|
||||
);
|
||||
}
|
||||
|
||||
// ── Agent message sub-component ─────────────────────────────────────
|
||||
|
||||
type AgentMessageProps = {
|
||||
comment: Message;
|
||||
showRaw: boolean;
|
||||
onToggleRaw: () => void;
|
||||
showRichBlocks?: boolean;
|
||||
worktreePath?: string;
|
||||
onOpenFile?: (path: string) => void;
|
||||
};
|
||||
|
||||
function AgentMessageContent({
|
||||
comment,
|
||||
showRaw,
|
||||
onToggleRaw,
|
||||
showRichBlocks,
|
||||
worktreePath,
|
||||
onOpenFile,
|
||||
}: AgentMessageProps) {
|
||||
return (
|
||||
<div className="flex items-start gap-2 sm:gap-3 w-full group">
|
||||
<div className="flex-1 min-w-0">
|
||||
{showRaw ? (
|
||||
<pre className="whitespace-pre-wrap font-mono text-xs bg-muted/20 p-3 rounded-md">
|
||||
{comment.raw_content || comment.content || "(empty)"}
|
||||
</pre>
|
||||
) : (
|
||||
<div className="markdown-body max-w-none">
|
||||
<MemoizedMarkdown
|
||||
content={comment.content || "(empty)"}
|
||||
worktreePath={worktreePath}
|
||||
onOpenFile={onOpenFile}
|
||||
/>
|
||||
{showRichBlocks ? <RichBlocks comment={comment} /> : null}
|
||||
</div>
|
||||
)}
|
||||
<MessageActions
|
||||
message={comment}
|
||||
showCopy={true}
|
||||
showTimestamp={true}
|
||||
showRawToggle={true}
|
||||
showModel={true}
|
||||
showNavigation={false}
|
||||
isRawView={showRaw}
|
||||
onToggleRaw={onToggleRaw}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
// ── Main component ──────────────────────────────────────────────────
|
||||
|
||||
export const ChatMessage = memo(function ChatMessage({
|
||||
@@ -541,6 +491,7 @@ export const ChatMessage = memo(function ChatMessage({
|
||||
worktreePath,
|
||||
onOpenFile,
|
||||
onScrollToMessage,
|
||||
isTurnActive = false,
|
||||
}: ChatMessageProps) {
|
||||
const [showRaw, setShowRaw] = useState(false);
|
||||
const toggleRaw = useCallback(() => setShowRaw((v) => !v), []);
|
||||
@@ -585,6 +536,8 @@ export const ChatMessage = memo(function ChatMessage({
|
||||
showRaw={showRaw}
|
||||
onToggleRaw={toggleRaw}
|
||||
showRichBlocks={showRichBlocks}
|
||||
sessionId={sessionId}
|
||||
isTurnActive={isTurnActive}
|
||||
worktreePath={worktreePath}
|
||||
onOpenFile={onOpenFile}
|
||||
/>
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
import { IconMessage } from "@tabler/icons-react";
|
||||
import type { MessageCommentDecoration } from "@/lib/chat/agent-message-comments";
|
||||
|
||||
function MessageCommentBadges({
|
||||
decorations,
|
||||
onOpen,
|
||||
}: {
|
||||
decorations: MessageCommentDecoration[];
|
||||
onOpen: (commentId: string, position: { x: number; y: number }) => void;
|
||||
}) {
|
||||
return decorations.map((decoration) => (
|
||||
<button
|
||||
key={decoration.comment.id}
|
||||
type="button"
|
||||
className="comment-badge agent-message-comment-badge border-0 bg-transparent p-0"
|
||||
style={{ position: "absolute", left: decoration.left, top: decoration.top }}
|
||||
data-agent-message-comment-id={decoration.comment.id}
|
||||
data-comment-id={decoration.comment.id}
|
||||
aria-label="Edit comment"
|
||||
onClick={(event) => {
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
const rect = event.currentTarget.getBoundingClientRect();
|
||||
onOpen(decoration.comment.id, { x: rect.right, y: rect.bottom });
|
||||
}}
|
||||
>
|
||||
<IconMessage aria-hidden="true" />
|
||||
</button>
|
||||
));
|
||||
}
|
||||
|
||||
function MessageCommentFallbackHighlights({
|
||||
decorations,
|
||||
}: {
|
||||
decorations: MessageCommentDecoration[];
|
||||
}) {
|
||||
return decorations.flatMap((decoration) =>
|
||||
decoration.highlightRects.map((rect, index) => (
|
||||
<span
|
||||
key={`${decoration.comment.id}:${index}`}
|
||||
aria-hidden="true"
|
||||
className="agent-message-comment-fallback"
|
||||
data-agent-message-comment-fallback="true"
|
||||
data-comment-id={decoration.comment.id}
|
||||
style={rect}
|
||||
/>
|
||||
)),
|
||||
);
|
||||
}
|
||||
|
||||
export function MessageCommentDecorationOverlay({
|
||||
decorations,
|
||||
useCustomHighlights,
|
||||
onOpen,
|
||||
}: {
|
||||
decorations: MessageCommentDecoration[];
|
||||
useCustomHighlights: boolean;
|
||||
onOpen: (commentId: string, position: { x: number; y: number }) => void;
|
||||
}) {
|
||||
return (
|
||||
<>
|
||||
{!useCustomHighlights ? <MessageCommentFallbackHighlights decorations={decorations} /> : null}
|
||||
<MessageCommentBadges decorations={decorations} onOpen={onOpen} />
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
export function MessageCustomHighlightStyle({ highlightName }: { highlightName: string }) {
|
||||
return (
|
||||
<style>{`::highlight(${highlightName}) {
|
||||
background-color: color-mix(in oklch, var(--accent) 70%, transparent);
|
||||
color: inherit;
|
||||
text-decoration: underline 2px color-mix(in oklch, var(--accent-foreground) 25%, transparent);
|
||||
text-underline-offset: 2px;
|
||||
}`}</style>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,288 @@
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import { cleanup, fireEvent, render, screen } from "@testing-library/react";
|
||||
import { useCommentsStore } from "@/lib/state/slices/comments";
|
||||
import { createMessageTextAnchor } from "@/lib/chat/agent-message-comments";
|
||||
import { sessionId as toSessionId, taskId as toTaskId, type Message } from "@/lib/types/http";
|
||||
import { MessageCommentSurface } from "./message-comment-surface";
|
||||
|
||||
const COMMENT_TEXT = vi.hoisted(() => "Make this concrete.");
|
||||
const TOUCH_DRAWER = vi.hoisted(() => ({ enabled: false }));
|
||||
|
||||
vi.mock("@/hooks/use-compact-task-chrome", () => ({
|
||||
useTouchDrawer: () => TOUCH_DRAWER.enabled,
|
||||
}));
|
||||
vi.mock("@/hooks/domains/comments/use-run-comment", () => ({
|
||||
useRunComment: () => ({ runComment: vi.fn() }),
|
||||
}));
|
||||
vi.mock("@/components/task/plan-selection-popover", () => ({
|
||||
PlanSelectionPopover: ({
|
||||
selectedText,
|
||||
onAdd,
|
||||
onClose,
|
||||
errorMessage,
|
||||
}: {
|
||||
selectedText: string;
|
||||
onAdd: (feedback: string) => boolean | void;
|
||||
onClose: () => void;
|
||||
errorMessage?: string | null;
|
||||
}) => (
|
||||
<div data-testid="comment-popover">
|
||||
<span>{selectedText}</span>
|
||||
{errorMessage ? <p role="alert">{errorMessage}</p> : null}
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => {
|
||||
if (onAdd(COMMENT_TEXT) !== false) onClose();
|
||||
}}
|
||||
>
|
||||
Save comment
|
||||
</button>
|
||||
</div>
|
||||
),
|
||||
}));
|
||||
|
||||
const SESSION_ID = "session-1";
|
||||
const MESSAGE_TEXT_TEST_ID = "message-text";
|
||||
const SELECTED_QUOTE = "settled answer";
|
||||
|
||||
function message(content: string): Message {
|
||||
return {
|
||||
id: "message-1",
|
||||
session_id: toSessionId(SESSION_ID),
|
||||
task_id: toTaskId("task-1"),
|
||||
author_type: "agent",
|
||||
type: "message",
|
||||
content,
|
||||
created_at: "2026-07-21T00:00:00Z",
|
||||
};
|
||||
}
|
||||
|
||||
function resetComments() {
|
||||
useCommentsStore.setState({
|
||||
byId: {},
|
||||
bySession: {},
|
||||
pendingForChat: [],
|
||||
editingCommentId: null,
|
||||
});
|
||||
sessionStorage.clear();
|
||||
}
|
||||
|
||||
function addPendingComment(content: string, selectedText: string) {
|
||||
const start = content.indexOf(selectedText);
|
||||
useCommentsStore.getState().addComment({
|
||||
id: "comment-1",
|
||||
sessionId: SESSION_ID,
|
||||
source: "agent-message",
|
||||
messageId: "message-1",
|
||||
selectedText,
|
||||
text: COMMENT_TEXT,
|
||||
createdAt: "2026-07-21T00:00:00Z",
|
||||
status: "pending",
|
||||
anchor: createMessageTextAnchor("message-1", content, start, start + selectedText.length),
|
||||
});
|
||||
}
|
||||
|
||||
afterEach(() => {
|
||||
cleanup();
|
||||
resetComments();
|
||||
TOUCH_DRAWER.enabled = false;
|
||||
vi.restoreAllMocks();
|
||||
vi.unstubAllGlobals();
|
||||
});
|
||||
|
||||
describe("MessageCommentSurface", () => {
|
||||
it("anchors a pending selection to the same quote when message text shifts", () => {
|
||||
const original = "The settled answer contains detail.";
|
||||
const updated = "Intro. The settled answer contains detail.";
|
||||
const { rerender } = render(
|
||||
<MessageCommentSurface
|
||||
message={message(original)}
|
||||
sessionId={SESSION_ID}
|
||||
isTurnActive={false}
|
||||
>
|
||||
<span data-testid={MESSAGE_TEXT_TEST_ID}>{original}</span>
|
||||
</MessageCommentSurface>,
|
||||
);
|
||||
|
||||
const text = screen.getByTestId(MESSAGE_TEXT_TEST_ID).firstChild!;
|
||||
const range = document.createRange();
|
||||
const start = original.indexOf(SELECTED_QUOTE);
|
||||
range.setStart(text, start);
|
||||
range.setEnd(text, start + SELECTED_QUOTE.length);
|
||||
const selection = window.getSelection()!;
|
||||
selection.removeAllRanges();
|
||||
selection.addRange(range);
|
||||
fireEvent.mouseUp(screen.getByTestId(MESSAGE_TEXT_TEST_ID).parentElement!);
|
||||
fireEvent.click(screen.getByTestId("agent-message-comment-trigger"));
|
||||
|
||||
rerender(
|
||||
<MessageCommentSurface message={message(updated)} sessionId={SESSION_ID} isTurnActive={false}>
|
||||
<span data-testid={MESSAGE_TEXT_TEST_ID}>{updated}</span>
|
||||
</MessageCommentSurface>,
|
||||
);
|
||||
fireEvent.click(screen.getByRole("button", { name: "Save comment" }));
|
||||
|
||||
const saved = Object.values(useCommentsStore.getState().byId)[0];
|
||||
expect(saved?.source).toBe("agent-message");
|
||||
if (saved?.source !== "agent-message") throw new Error("Expected an agent message comment");
|
||||
expect(saved.selectedText).toBe(SELECTED_QUOTE);
|
||||
expect(saved.anchor.start).toBe(updated.indexOf(SELECTED_QUOTE));
|
||||
});
|
||||
|
||||
it("keeps React-owned text nodes under their rendered parent", () => {
|
||||
const content = "A settled answer.";
|
||||
addPendingComment(content, "settled");
|
||||
|
||||
render(
|
||||
<MessageCommentSurface message={message(content)} sessionId={SESSION_ID} isTurnActive={false}>
|
||||
<span data-testid="react-owned-text">{content}</span>
|
||||
</MessageCommentSurface>,
|
||||
);
|
||||
|
||||
const reactOwnedText = screen.getByTestId("react-owned-text");
|
||||
expect(
|
||||
Array.from(reactOwnedText.childNodes).every((node) => node.nodeType === Node.TEXT_NODE),
|
||||
).toBe(true);
|
||||
expect(document.querySelector('.comment-badge[data-comment-id="comment-1"]')).not.toBeNull();
|
||||
});
|
||||
|
||||
it("keeps feedback open and reports when the selected quote was rewritten", () => {
|
||||
const original = "The settled answer contains detail.";
|
||||
const updated = "The response was completely rewritten.";
|
||||
const { rerender } = render(
|
||||
<MessageCommentSurface
|
||||
message={message(original)}
|
||||
sessionId={SESSION_ID}
|
||||
isTurnActive={false}
|
||||
>
|
||||
<span data-testid={MESSAGE_TEXT_TEST_ID}>{original}</span>
|
||||
</MessageCommentSurface>,
|
||||
);
|
||||
|
||||
const text = screen.getByTestId(MESSAGE_TEXT_TEST_ID).firstChild!;
|
||||
const range = document.createRange();
|
||||
const start = original.indexOf(SELECTED_QUOTE);
|
||||
range.setStart(text, start);
|
||||
range.setEnd(text, start + SELECTED_QUOTE.length);
|
||||
const selection = window.getSelection()!;
|
||||
selection.removeAllRanges();
|
||||
selection.addRange(range);
|
||||
fireEvent.mouseUp(screen.getByTestId(MESSAGE_TEXT_TEST_ID).parentElement!);
|
||||
fireEvent.click(screen.getByTestId("agent-message-comment-trigger"));
|
||||
|
||||
rerender(
|
||||
<MessageCommentSurface message={message(updated)} sessionId={SESSION_ID} isTurnActive={false}>
|
||||
<span data-testid={MESSAGE_TEXT_TEST_ID}>{updated}</span>
|
||||
</MessageCommentSurface>,
|
||||
);
|
||||
fireEvent.click(screen.getByRole("button", { name: "Save comment" }));
|
||||
|
||||
expect(screen.getByTestId("comment-popover")).not.toBeNull();
|
||||
expect(Object.values(useCommentsStore.getState().byId)).toHaveLength(0);
|
||||
expect(screen.getByRole("alert").textContent).toBe(
|
||||
"The agent response changed. Select the text again.",
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("MessageCommentSurface mobile drawer", () => {
|
||||
it("keeps mobile feedback open when the selected quote was rewritten", () => {
|
||||
TOUCH_DRAWER.enabled = true;
|
||||
const original = "The settled answer contains detail.";
|
||||
const updated = "The response was completely rewritten.";
|
||||
const { rerender } = render(
|
||||
<MessageCommentSurface
|
||||
message={message(original)}
|
||||
sessionId={SESSION_ID}
|
||||
isTurnActive={false}
|
||||
>
|
||||
<span data-testid={MESSAGE_TEXT_TEST_ID}>{original}</span>
|
||||
</MessageCommentSurface>,
|
||||
);
|
||||
|
||||
const text = screen.getByTestId(MESSAGE_TEXT_TEST_ID).firstChild!;
|
||||
const range = document.createRange();
|
||||
const start = original.indexOf(SELECTED_QUOTE);
|
||||
range.setStart(text, start);
|
||||
range.setEnd(text, start + SELECTED_QUOTE.length);
|
||||
const selection = window.getSelection()!;
|
||||
selection.removeAllRanges();
|
||||
selection.addRange(range);
|
||||
fireEvent.mouseUp(screen.getByTestId(MESSAGE_TEXT_TEST_ID).parentElement!);
|
||||
fireEvent.click(screen.getByTestId("agent-message-comment-trigger"));
|
||||
const input = screen.getByTestId("agent-message-comment-input");
|
||||
fireEvent.change(input, { target: { value: COMMENT_TEXT } });
|
||||
|
||||
rerender(
|
||||
<MessageCommentSurface message={message(updated)} sessionId={SESSION_ID} isTurnActive={false}>
|
||||
<span data-testid={MESSAGE_TEXT_TEST_ID}>{updated}</span>
|
||||
</MessageCommentSurface>,
|
||||
);
|
||||
fireEvent.click(screen.getByTestId("agent-message-comment-add"));
|
||||
|
||||
expect(screen.getByTestId("agent-message-comment-drawer")).not.toBeNull();
|
||||
expect((input as HTMLTextAreaElement).value).toBe(COMMENT_TEXT);
|
||||
expect(screen.getByRole("alert").textContent).toBe(
|
||||
"The agent response changed. Select the text again.",
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("MessageCommentSurface fallback highlights", () => {
|
||||
it("renders visible ranges when the CSS Highlight API is unavailable", () => {
|
||||
vi.stubGlobal("CSS", {});
|
||||
vi.stubGlobal("Highlight", undefined);
|
||||
const content = "A settled answer.";
|
||||
addPendingComment(content, "settled");
|
||||
|
||||
render(
|
||||
<MessageCommentSurface message={message(content)} sessionId={SESSION_ID} isTurnActive={false}>
|
||||
<span>{content}</span>
|
||||
</MessageCommentSurface>,
|
||||
);
|
||||
|
||||
expect(
|
||||
document.querySelector('[data-agent-message-comment-fallback][data-comment-id="comment-1"]'),
|
||||
).not.toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("MessageCommentSurface highlighted interactions", () => {
|
||||
it("does not open a highlighted comment while text is selected", () => {
|
||||
vi.spyOn(Range.prototype, "getClientRects").mockReturnValue([
|
||||
new DOMRect(0, 0, 100, 20),
|
||||
] as unknown as DOMRectList);
|
||||
const content = "Read the settled answer.";
|
||||
addPendingComment(content, "settled answer");
|
||||
const { container } = render(
|
||||
<MessageCommentSurface message={message(content)} sessionId={SESSION_ID} isTurnActive={false}>
|
||||
<span>{content}</span>
|
||||
</MessageCommentSurface>,
|
||||
);
|
||||
vi.spyOn(window, "getSelection").mockReturnValue({ isCollapsed: false } as Selection);
|
||||
|
||||
fireEvent.click(container.querySelector("[data-agent-message-body]")!, {
|
||||
clientX: 10,
|
||||
clientY: 10,
|
||||
});
|
||||
|
||||
expect(screen.queryByTestId("comment-popover")).toBeNull();
|
||||
});
|
||||
|
||||
it("preserves native link clicks inside a highlighted range", () => {
|
||||
vi.spyOn(Range.prototype, "getClientRects").mockReturnValue([
|
||||
new DOMRect(0, 0, 100, 20),
|
||||
] as unknown as DOMRectList);
|
||||
const content = "Read the docs.";
|
||||
addPendingComment(content, "docs");
|
||||
|
||||
render(
|
||||
<MessageCommentSurface message={message(content)} sessionId={SESSION_ID} isTurnActive={false}>
|
||||
<a href="#docs">{content}</a>
|
||||
</MessageCommentSurface>,
|
||||
);
|
||||
|
||||
expect(fireEvent.click(screen.getByRole("link"), { clientX: 10, clientY: 10 })).toBe(true);
|
||||
expect(screen.queryByTestId("comment-popover")).toBeNull();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,582 @@
|
||||
"use client";
|
||||
|
||||
import {
|
||||
useCallback,
|
||||
useEffect,
|
||||
useId,
|
||||
useRef,
|
||||
useState,
|
||||
type Dispatch,
|
||||
type MouseEvent,
|
||||
type ReactNode,
|
||||
type RefObject,
|
||||
type SetStateAction,
|
||||
} from "react";
|
||||
import { createPortal } from "react-dom";
|
||||
import { useShallow } from "zustand/react/shallow";
|
||||
import { IconMessage, IconPlus, IconPlayerPlay, IconTrash } from "@tabler/icons-react";
|
||||
import { Button } from "@kandev/ui/button";
|
||||
import { Textarea } from "@kandev/ui/textarea";
|
||||
import {
|
||||
Drawer,
|
||||
DrawerContent,
|
||||
DrawerDescription,
|
||||
DrawerHeader,
|
||||
DrawerTitle,
|
||||
} from "@kandev/ui/drawer";
|
||||
import { useTouchDrawer } from "@/hooks/use-compact-task-chrome";
|
||||
import { PlanSelectionPopover } from "@/components/task/plan-selection-popover";
|
||||
import { floatingBounds, placeFloatingRect } from "@/components/task/floating-selection-position";
|
||||
import { useCommentsStore } from "@/lib/state/slices/comments";
|
||||
import type { AgentMessageComment } from "@/lib/state/slices/comments";
|
||||
import { useRunComment } from "@/hooks/domains/comments/use-run-comment";
|
||||
import {
|
||||
agentMessageCommentHighlightName,
|
||||
createMessageTextAnchor,
|
||||
getMessageSelection,
|
||||
isSelectableAgentMessage,
|
||||
resolveMessageTextAnchor,
|
||||
type MessageSelection,
|
||||
} from "@/lib/chat/agent-message-comments";
|
||||
import type { Message } from "@/lib/types/http";
|
||||
import { cn, generateUUID } from "@/lib/utils";
|
||||
import {
|
||||
messageCommentDecorationAtPoint,
|
||||
supportsCustomHighlights,
|
||||
useMessageCommentDecorations,
|
||||
useMessageCommentShortcut,
|
||||
} from "./use-message-comment-dom";
|
||||
import {
|
||||
MessageCommentDecorationOverlay,
|
||||
MessageCustomHighlightStyle,
|
||||
} from "./message-comment-decorations";
|
||||
|
||||
type MessageCommentSurfaceProps = {
|
||||
message: Message;
|
||||
sessionId?: string | null;
|
||||
isTurnActive: boolean;
|
||||
children: ReactNode;
|
||||
};
|
||||
|
||||
type CommentTarget = {
|
||||
selection: MessageSelection;
|
||||
anchor: AgentMessageComment["anchor"];
|
||||
position: { x: number; y: number };
|
||||
editingCommentId?: string;
|
||||
editingText?: string;
|
||||
};
|
||||
|
||||
const EMPTY_COMMENT_IDS: string[] = [];
|
||||
const INTERACTIVE_MESSAGE_TARGETS =
|
||||
'a, button, input, textarea, select, summary, [role="button"], [role="link"], [contenteditable="true"]';
|
||||
|
||||
function isInteractiveMessageTarget(target: EventTarget | null) {
|
||||
return target instanceof Element && Boolean(target.closest(INTERACTIVE_MESSAGE_TARGETS));
|
||||
}
|
||||
|
||||
function useDecorationClickHandler(
|
||||
decorations: ReturnType<typeof useMessageCommentDecorations>,
|
||||
openExistingComment: (commentId: string, position: { x: number; y: number }) => void,
|
||||
) {
|
||||
return useCallback(
|
||||
(event: MouseEvent<HTMLDivElement>) => {
|
||||
if (isInteractiveMessageTarget(event.target)) return;
|
||||
const selection = window.getSelection();
|
||||
if (selection && !selection.isCollapsed) return;
|
||||
const decoration = messageCommentDecorationAtPoint(decorations, event.clientX, event.clientY);
|
||||
if (!decoration) return;
|
||||
event.preventDefault();
|
||||
openExistingComment(decoration.comment.id, { x: event.clientX, y: event.clientY });
|
||||
},
|
||||
[decorations, openExistingComment],
|
||||
);
|
||||
}
|
||||
|
||||
function commentFromTarget(
|
||||
message: Message,
|
||||
sessionId: string,
|
||||
target: CommentTarget,
|
||||
renderedText: string,
|
||||
feedback: string,
|
||||
): AgentMessageComment | null {
|
||||
const resolved = resolveMessageTextAnchor(target.anchor, renderedText);
|
||||
if (!resolved) return null;
|
||||
const anchor = createMessageTextAnchor(message.id, renderedText, resolved.start, resolved.end);
|
||||
return {
|
||||
id: generateUUID(),
|
||||
sessionId,
|
||||
source: "agent-message",
|
||||
messageId: message.id,
|
||||
selectedText: anchor.selectedText,
|
||||
text: feedback.trim(),
|
||||
createdAt: new Date().toISOString(),
|
||||
status: "pending",
|
||||
anchor,
|
||||
};
|
||||
}
|
||||
|
||||
function SelectionCommentTrigger({
|
||||
selection,
|
||||
isTouch,
|
||||
onOpen,
|
||||
portalContainer,
|
||||
}: {
|
||||
selection: MessageSelection;
|
||||
isTouch: boolean;
|
||||
onOpen: () => void;
|
||||
portalContainer?: HTMLElement | null;
|
||||
}) {
|
||||
const size = isTouch ? 44 : 28;
|
||||
const portalRect = portalContainer?.getBoundingClientRect();
|
||||
const outerSize = size + 8;
|
||||
const position = placeFloatingRect({
|
||||
left: selection.rect.right - outerSize,
|
||||
topCandidates: [selection.rect.top - outerSize - 8, selection.rect.bottom + 8],
|
||||
width: outerSize,
|
||||
height: outerSize,
|
||||
bounds: floatingBounds(portalRect),
|
||||
});
|
||||
return createPortal(
|
||||
<div
|
||||
className={cn(
|
||||
"pointer-events-auto z-[60] rounded-lg border border-border/50 bg-popover p-1 shadow-lg",
|
||||
portalContainer ? "absolute" : "fixed",
|
||||
)}
|
||||
style={{
|
||||
left: position.left - (portalRect?.left ?? 0),
|
||||
top: position.top - (portalRect?.top ?? 0),
|
||||
}}
|
||||
>
|
||||
<button
|
||||
type="button"
|
||||
title="Comment (Cmd+Shift+C)"
|
||||
aria-label="Comment on selection"
|
||||
data-testid="agent-message-comment-trigger"
|
||||
className="flex cursor-pointer items-center justify-center rounded bg-accent text-white transition-transform duration-150 ease-out hover:bg-accent/80 active:scale-[0.96]"
|
||||
style={{ width: size, height: size }}
|
||||
onMouseDown={(event) => event.preventDefault()}
|
||||
onClick={onOpen}
|
||||
>
|
||||
<IconMessage className="h-3.5 w-3.5" />
|
||||
</button>
|
||||
</div>,
|
||||
portalContainer ?? document.body,
|
||||
);
|
||||
}
|
||||
|
||||
function DrawerCommentActions({
|
||||
isEditing,
|
||||
disabled,
|
||||
onAdd,
|
||||
onRun,
|
||||
onDelete,
|
||||
}: {
|
||||
isEditing: boolean;
|
||||
disabled: boolean;
|
||||
onAdd: () => void;
|
||||
onRun?: () => void;
|
||||
onDelete?: () => void;
|
||||
}) {
|
||||
return (
|
||||
<div className="flex items-center justify-between gap-3">
|
||||
{isEditing && onDelete ? (
|
||||
<Button
|
||||
type="button"
|
||||
size="sm"
|
||||
variant="ghost"
|
||||
aria-label="Delete comment"
|
||||
onClick={onDelete}
|
||||
className="min-h-11 cursor-pointer px-3 text-muted-foreground hover:text-destructive"
|
||||
>
|
||||
<IconTrash className="h-4 w-4" />
|
||||
</Button>
|
||||
) : (
|
||||
<span />
|
||||
)}
|
||||
<div className="inline-flex">
|
||||
<Button
|
||||
type="button"
|
||||
size="sm"
|
||||
variant={onRun && !isEditing ? "outline" : "default"}
|
||||
disabled={disabled}
|
||||
onClick={onAdd}
|
||||
data-testid="agent-message-comment-add"
|
||||
className={`min-h-11 cursor-pointer gap-1.5 px-4 transition-transform duration-150 ease-out active:scale-[0.96] ${onRun && !isEditing ? "rounded-r-none border-r-0" : ""}`}
|
||||
>
|
||||
<IconPlus className="h-4 w-4" />
|
||||
{isEditing ? "Update" : "Add"}
|
||||
</Button>
|
||||
{onRun && !isEditing ? (
|
||||
<Button
|
||||
type="button"
|
||||
size="sm"
|
||||
disabled={disabled}
|
||||
onClick={onRun}
|
||||
data-testid="agent-message-comment-run"
|
||||
className="min-h-11 cursor-pointer gap-1.5 rounded-l-none px-4 transition-transform duration-150 ease-out active:scale-[0.96]"
|
||||
>
|
||||
<IconPlayerPlay className="h-4 w-4" />
|
||||
Run
|
||||
</Button>
|
||||
) : null}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function MessageCommentDrawer({
|
||||
target,
|
||||
open,
|
||||
onClose,
|
||||
onAdd,
|
||||
onRun,
|
||||
onDelete,
|
||||
errorMessage,
|
||||
}: {
|
||||
target: CommentTarget | null;
|
||||
open: boolean;
|
||||
onClose: () => void;
|
||||
onAdd: (feedback: string) => boolean | void;
|
||||
onRun: (feedback: string) => boolean | void;
|
||||
onDelete: (() => void) | undefined;
|
||||
errorMessage?: string | null;
|
||||
}) {
|
||||
const [feedback, setFeedback] = useState(target?.editingText ?? "");
|
||||
const targetKey = `${target?.editingCommentId ?? "new"}:${target?.selection.start ?? 0}:${target?.selection.end ?? 0}`;
|
||||
|
||||
useEffect(() => {
|
||||
setFeedback(target?.editingText ?? "");
|
||||
}, [targetKey, target?.editingText]);
|
||||
|
||||
if (!target) return null;
|
||||
const isEditing = target.editingCommentId !== undefined;
|
||||
const submit = () => {
|
||||
if (feedback.trim() && onAdd(feedback.trim()) !== false) onClose();
|
||||
};
|
||||
const run = () => {
|
||||
if (feedback.trim() && onRun(feedback.trim()) !== false) onClose();
|
||||
};
|
||||
|
||||
return (
|
||||
<Drawer open={open} onOpenChange={(next) => !next && onClose()}>
|
||||
<DrawerContent
|
||||
className="z-[60] max-h-[82dvh] pb-[calc(1rem+env(safe-area-inset-bottom))]"
|
||||
data-testid="agent-message-comment-drawer"
|
||||
>
|
||||
<DrawerHeader className="shrink-0 px-4 pb-3 text-left">
|
||||
<DrawerTitle>{isEditing ? "Edit comment" : "Comment"}</DrawerTitle>
|
||||
<DrawerDescription className="line-clamp-2 text-pretty italic">
|
||||
“{target.selection.selectedText}”
|
||||
</DrawerDescription>
|
||||
</DrawerHeader>
|
||||
<div className="min-h-0 overflow-y-auto px-4 pb-2">
|
||||
<Textarea
|
||||
value={feedback}
|
||||
onChange={(event) => setFeedback(event.target.value)}
|
||||
onKeyDown={(event) => {
|
||||
if (event.key !== "Enter" || (!event.metaKey && !event.ctrlKey)) return;
|
||||
event.preventDefault();
|
||||
if (event.shiftKey && !isEditing) run();
|
||||
else submit();
|
||||
}}
|
||||
placeholder="Add your comment or instruction..."
|
||||
aria-label="Comment on agent response"
|
||||
className="mb-3 min-h-20 resize-none text-sm border-border/50 focus:border-primary/50"
|
||||
autoFocus
|
||||
data-testid="agent-message-comment-input"
|
||||
/>
|
||||
{errorMessage ? (
|
||||
<p role="alert" className="mb-3 text-xs text-destructive">
|
||||
{errorMessage}
|
||||
</p>
|
||||
) : null}
|
||||
<DrawerCommentActions
|
||||
isEditing={isEditing}
|
||||
disabled={!feedback.trim()}
|
||||
onAdd={submit}
|
||||
onRun={isEditing ? undefined : run}
|
||||
onDelete={onDelete}
|
||||
/>
|
||||
</div>
|
||||
</DrawerContent>
|
||||
</Drawer>
|
||||
);
|
||||
}
|
||||
|
||||
function usePendingCommentsForMessage(sessionId: string | null | undefined, messageId: string) {
|
||||
return useCommentsStore(
|
||||
useShallow((state) => {
|
||||
const commentIds = sessionId
|
||||
? (state.bySession[sessionId] ?? EMPTY_COMMENT_IDS)
|
||||
: EMPTY_COMMENT_IDS;
|
||||
return commentIds.flatMap((id) => {
|
||||
const comment = state.byId[id];
|
||||
return comment &&
|
||||
comment.source === "agent-message" &&
|
||||
comment.status === "pending" &&
|
||||
comment.messageId === messageId
|
||||
? [comment]
|
||||
: [];
|
||||
});
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
function useCommentTargetState(
|
||||
rootRef: RefObject<HTMLDivElement | null>,
|
||||
isSelectable: boolean,
|
||||
messageId: string,
|
||||
) {
|
||||
const [target, setTarget] = useState<CommentTarget | null>(null);
|
||||
const [composerOpen, setComposerOpen] = useState(false);
|
||||
|
||||
const close = useCallback(() => {
|
||||
setComposerOpen(false);
|
||||
setTarget(null);
|
||||
window.getSelection()?.removeAllRanges();
|
||||
}, []);
|
||||
|
||||
const setNewSelection = useCallback(
|
||||
(selection: MessageSelection, openComposer: boolean) => {
|
||||
const renderedText = rootRef.current?.textContent ?? "";
|
||||
setTarget({
|
||||
selection,
|
||||
anchor: createMessageTextAnchor(messageId, renderedText, selection.start, selection.end),
|
||||
position: { x: selection.rect.right, y: selection.rect.bottom },
|
||||
});
|
||||
setComposerOpen(openComposer);
|
||||
},
|
||||
[messageId, rootRef],
|
||||
);
|
||||
|
||||
const captureSelection = useCallback(() => {
|
||||
if (!rootRef.current || !isSelectable) return;
|
||||
const selection = getMessageSelection(rootRef.current, window.getSelection());
|
||||
if (selection) setNewSelection(selection, false);
|
||||
}, [isSelectable, rootRef, setNewSelection]);
|
||||
|
||||
const openFromShortcut = useCallback(
|
||||
(selection: MessageSelection) => setNewSelection(selection, true),
|
||||
[setNewSelection],
|
||||
);
|
||||
useMessageCommentShortcut(rootRef, isSelectable, openFromShortcut);
|
||||
|
||||
useEffect(() => {
|
||||
if (!target || composerOpen) return;
|
||||
const dismiss = (event: Event) => {
|
||||
const element = event.target as HTMLElement | null;
|
||||
if (element?.closest('[data-testid="agent-message-comment-trigger"]')) return;
|
||||
setTarget(null);
|
||||
};
|
||||
const dismissOnScroll = () => setTarget(null);
|
||||
document.addEventListener("pointerdown", dismiss, true);
|
||||
document.addEventListener("scroll", dismissOnScroll, true);
|
||||
return () => {
|
||||
document.removeEventListener("pointerdown", dismiss, true);
|
||||
document.removeEventListener("scroll", dismissOnScroll, true);
|
||||
};
|
||||
}, [composerOpen, target]);
|
||||
|
||||
return { target, setTarget, composerOpen, setComposerOpen, close, captureSelection };
|
||||
}
|
||||
|
||||
function useExistingCommentHandlers(
|
||||
rootRef: RefObject<HTMLDivElement | null>,
|
||||
pendingComments: AgentMessageComment[],
|
||||
setTarget: Dispatch<SetStateAction<CommentTarget | null>>,
|
||||
setComposerOpen: Dispatch<SetStateAction<boolean>>,
|
||||
) {
|
||||
const openExistingComment = useCallback(
|
||||
(commentId: string, position: { x: number; y: number }) => {
|
||||
const comment = pendingComments.find((item) => item.id === commentId);
|
||||
const root = rootRef.current;
|
||||
if (!comment || !root) return;
|
||||
const range = resolveMessageTextAnchor(comment.anchor, root.textContent ?? "");
|
||||
if (!range) return;
|
||||
setTarget({
|
||||
selection: {
|
||||
...range,
|
||||
selectedText: comment.selectedText,
|
||||
rect: new DOMRect(position.x, position.y, 0, 0),
|
||||
},
|
||||
anchor: comment.anchor,
|
||||
position,
|
||||
editingCommentId: comment.id,
|
||||
editingText: comment.text,
|
||||
});
|
||||
setComposerOpen(true);
|
||||
window.getSelection()?.removeAllRanges();
|
||||
},
|
||||
[pendingComments, rootRef, setComposerOpen, setTarget],
|
||||
);
|
||||
|
||||
return openExistingComment;
|
||||
}
|
||||
|
||||
function useMessageCommentActions({
|
||||
message,
|
||||
sessionId,
|
||||
target,
|
||||
rootRef,
|
||||
close,
|
||||
}: {
|
||||
message: Message;
|
||||
sessionId: string | null | undefined;
|
||||
target: CommentTarget | null;
|
||||
rootRef: RefObject<HTMLDivElement | null>;
|
||||
close: () => void;
|
||||
}) {
|
||||
const addComment = useCommentsStore((state) => state.addComment);
|
||||
const updateComment = useCommentsStore((state) => state.updateComment);
|
||||
const removeComment = useCommentsStore((state) => state.removeComment);
|
||||
const [saveError, setSaveError] = useState<string | null>(null);
|
||||
const { runComment } = useRunComment({ sessionId: sessionId ?? null, taskId: message.task_id });
|
||||
|
||||
useEffect(() => setSaveError(null), [target]);
|
||||
|
||||
const saveComment = useCallback(
|
||||
(feedback: string): AgentMessageComment | true | null => {
|
||||
if (!target || !sessionId || !feedback.trim()) return null;
|
||||
if (target.editingCommentId) {
|
||||
setSaveError(null);
|
||||
updateComment(target.editingCommentId, { text: feedback.trim() });
|
||||
return true;
|
||||
}
|
||||
const renderedText = rootRef.current?.textContent ?? "";
|
||||
const comment = commentFromTarget(message, sessionId, target, renderedText, feedback);
|
||||
if (!comment) {
|
||||
setSaveError("The agent response changed. Select the text again.");
|
||||
return null;
|
||||
}
|
||||
setSaveError(null);
|
||||
addComment(comment);
|
||||
return comment;
|
||||
},
|
||||
[addComment, message, rootRef, sessionId, target, updateComment],
|
||||
);
|
||||
|
||||
const handleAdd = useCallback(
|
||||
(feedback: string) => {
|
||||
if (!saveComment(feedback)) return false;
|
||||
return true;
|
||||
},
|
||||
[saveComment],
|
||||
);
|
||||
|
||||
const handleRun = useCallback(
|
||||
(feedback: string) => {
|
||||
const comment = saveComment(feedback);
|
||||
if (!comment) return false;
|
||||
if (comment !== true) {
|
||||
void runComment(comment).catch((error) =>
|
||||
console.error("Failed to run agent message comment:", error),
|
||||
);
|
||||
}
|
||||
return true;
|
||||
},
|
||||
[runComment, saveComment],
|
||||
);
|
||||
|
||||
const deleteComment = useCallback(() => {
|
||||
if (!target?.editingCommentId) return;
|
||||
removeComment(target.editingCommentId);
|
||||
close();
|
||||
}, [close, removeComment, target?.editingCommentId]);
|
||||
const handleDelete = target?.editingCommentId ? deleteComment : undefined;
|
||||
|
||||
return { handleAdd, handleRun, handleDelete, saveError };
|
||||
}
|
||||
|
||||
export function MessageCommentSurface({
|
||||
message,
|
||||
sessionId,
|
||||
isTurnActive,
|
||||
children,
|
||||
}: MessageCommentSurfaceProps) {
|
||||
const rootRef = useRef<HTMLDivElement>(null);
|
||||
const highlightInstanceId = useId();
|
||||
const useDrawer = useTouchDrawer();
|
||||
const pendingComments = usePendingCommentsForMessage(sessionId, message.id);
|
||||
const isSelectable = Boolean(sessionId) && isSelectableAgentMessage(message, isTurnActive, false);
|
||||
const targetState = useCommentTargetState(rootRef, isSelectable, message.id);
|
||||
const { target, composerOpen, setTarget, setComposerOpen, close, captureSelection } = targetState;
|
||||
const openExistingComment = useExistingCommentHandlers(
|
||||
rootRef,
|
||||
pendingComments,
|
||||
setTarget,
|
||||
setComposerOpen,
|
||||
);
|
||||
const actions = useMessageCommentActions({ message, sessionId, target, rootRef, close });
|
||||
const portalContainer = rootRef.current?.closest<HTMLElement>('[role="dialog"]');
|
||||
const highlightName = agentMessageCommentHighlightName(`${message.id}-${highlightInstanceId}`);
|
||||
const hasCustomHighlightSupport = supportsCustomHighlights();
|
||||
const decorations = useMessageCommentDecorations(
|
||||
rootRef,
|
||||
pendingComments,
|
||||
message.content,
|
||||
highlightName,
|
||||
);
|
||||
const handleDecorationClick = useDecorationClickHandler(decorations, openExistingComment);
|
||||
|
||||
return (
|
||||
<>
|
||||
<div
|
||||
ref={rootRef}
|
||||
className="agent-message-comment-body relative"
|
||||
data-agent-message-body="true"
|
||||
data-message-id={message.id}
|
||||
data-agent-message-highlight-name={highlightName}
|
||||
onMouseUp={captureSelection}
|
||||
onTouchEnd={captureSelection}
|
||||
onClick={handleDecorationClick}
|
||||
>
|
||||
{children}
|
||||
<MessageCommentDecorationOverlay
|
||||
decorations={decorations}
|
||||
useCustomHighlights={hasCustomHighlightSupport}
|
||||
onOpen={openExistingComment}
|
||||
/>
|
||||
</div>
|
||||
{pendingComments.length > 0 && hasCustomHighlightSupport ? (
|
||||
<MessageCustomHighlightStyle highlightName={highlightName} />
|
||||
) : null}
|
||||
{target && !composerOpen ? (
|
||||
<SelectionCommentTrigger
|
||||
selection={target.selection}
|
||||
isTouch={useDrawer}
|
||||
onOpen={() => setComposerOpen(true)}
|
||||
portalContainer={portalContainer}
|
||||
/>
|
||||
) : null}
|
||||
{target && composerOpen && !useDrawer ? (
|
||||
<PlanSelectionPopover
|
||||
key={target.editingCommentId ?? `${target.selection.start}:${target.selection.end}`}
|
||||
selectedText={target.selection.selectedText}
|
||||
position={target.position}
|
||||
onAdd={actions.handleAdd}
|
||||
onAddAndRun={target.editingCommentId ? undefined : actions.handleRun}
|
||||
onClose={close}
|
||||
editingComment={target.editingText}
|
||||
onDelete={actions.handleDelete}
|
||||
testId="agent-message-comment-popover"
|
||||
inputTestId="agent-message-comment-input"
|
||||
addButtonTestId="agent-message-comment-add"
|
||||
runButtonTestId="agent-message-comment-run"
|
||||
portalContainer={portalContainer}
|
||||
errorMessage={actions.saveError}
|
||||
/>
|
||||
) : null}
|
||||
{useDrawer ? (
|
||||
<MessageCommentDrawer
|
||||
target={target}
|
||||
open={Boolean(target && composerOpen)}
|
||||
onClose={close}
|
||||
onAdd={actions.handleAdd}
|
||||
onRun={actions.handleRun}
|
||||
onDelete={actions.handleDelete}
|
||||
errorMessage={actions.saveError}
|
||||
/>
|
||||
) : null}
|
||||
</>
|
||||
);
|
||||
}
|
||||
@@ -21,6 +21,7 @@ type TurnGroupMessageProps = {
|
||||
isLastGroup?: boolean;
|
||||
/** Whether the turn is still active (agent is running) */
|
||||
isTurnActive?: boolean;
|
||||
streamingMessageId?: string | null;
|
||||
onScrollToMessage?: (messageId: string) => void;
|
||||
};
|
||||
|
||||
@@ -151,6 +152,8 @@ type TurnGroupContentProps = {
|
||||
taskId?: string;
|
||||
worktreePath?: string;
|
||||
onOpenFile?: (path: string) => void;
|
||||
isTurnActive?: boolean;
|
||||
streamingMessageId?: string | null;
|
||||
onScrollToMessage?: (messageId: string) => void;
|
||||
};
|
||||
|
||||
@@ -276,6 +279,7 @@ function renderMessageEntry(message: Message, props: MessageRenderProps) {
|
||||
childrenByParentToolCallId={props.childrenByParentToolCallId}
|
||||
worktreePath={props.worktreePath}
|
||||
sessionId={props.sessionId ?? undefined}
|
||||
isTurnActive={props.isTurnActive && message.id === props.streamingMessageId}
|
||||
onOpenFile={props.onOpenFile}
|
||||
onScrollToMessage={props.onScrollToMessage}
|
||||
/>
|
||||
@@ -328,6 +332,8 @@ function TurnGroupContent({
|
||||
taskId,
|
||||
worktreePath,
|
||||
onOpenFile,
|
||||
isTurnActive,
|
||||
streamingMessageId,
|
||||
onScrollToMessage,
|
||||
}: TurnGroupContentProps) {
|
||||
const renderProps: MessageRenderProps = {
|
||||
@@ -337,6 +343,8 @@ function TurnGroupContent({
|
||||
taskId,
|
||||
worktreePath,
|
||||
onOpenFile,
|
||||
isTurnActive,
|
||||
streamingMessageId,
|
||||
onScrollToMessage,
|
||||
};
|
||||
const compacted = useMemo(() => compactTurnGroupMessages(group.messages), [group.messages]);
|
||||
@@ -363,6 +371,7 @@ export const TurnGroupMessage = memo(function TurnGroupMessage({
|
||||
onOpenFile,
|
||||
isLastGroup = false,
|
||||
isTurnActive = false,
|
||||
streamingMessageId,
|
||||
onScrollToMessage,
|
||||
}: TurnGroupMessageProps) {
|
||||
const isGroupRunning = hasRunningTool(group.messages);
|
||||
@@ -400,6 +409,8 @@ export const TurnGroupMessage = memo(function TurnGroupMessage({
|
||||
taskId={taskId}
|
||||
worktreePath={worktreePath}
|
||||
onOpenFile={onOpenFile}
|
||||
isTurnActive={isTurnActive}
|
||||
streamingMessageId={streamingMessageId}
|
||||
onScrollToMessage={onScrollToMessage}
|
||||
/>
|
||||
)}
|
||||
|
||||
@@ -0,0 +1,149 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useLayoutEffect, useState, type RefObject } from "react";
|
||||
import {
|
||||
getMessageCommentDecorations,
|
||||
getMessageSelection,
|
||||
type MessageCommentDecoration,
|
||||
type MessageSelection,
|
||||
} from "@/lib/chat/agent-message-comments";
|
||||
import type { AgentMessageComment } from "@/lib/state/slices/comments";
|
||||
|
||||
type ShortcutHandler = (selection: MessageSelection) => void;
|
||||
type ShortcutManager = {
|
||||
handlers: Map<HTMLElement, ShortcutHandler>;
|
||||
listener: (event: KeyboardEvent) => void;
|
||||
};
|
||||
|
||||
const shortcutManagers = new WeakMap<Document, ShortcutManager>();
|
||||
|
||||
function registerMessageCommentShortcut(root: HTMLElement, handler: ShortcutHandler) {
|
||||
const ownerDocument = root.ownerDocument;
|
||||
let manager = shortcutManagers.get(ownerDocument);
|
||||
if (!manager) {
|
||||
const handlers = new Map<HTMLElement, ShortcutHandler>();
|
||||
const listener = (event: KeyboardEvent) => {
|
||||
if (!((event.metaKey || event.ctrlKey) && event.shiftKey && event.key.toLowerCase() === "c"))
|
||||
return;
|
||||
const selection = ownerDocument.getSelection();
|
||||
const anchorElement =
|
||||
selection?.anchorNode instanceof Element
|
||||
? selection.anchorNode
|
||||
: selection?.anchorNode?.parentElement;
|
||||
const selectedRoot = anchorElement?.closest<HTMLElement>("[data-agent-message-body]");
|
||||
if (!selectedRoot) return;
|
||||
const selectedHandler = handlers.get(selectedRoot);
|
||||
if (!selectedHandler) return;
|
||||
const messageSelection = getMessageSelection(selectedRoot, selection);
|
||||
if (!messageSelection) return;
|
||||
event.preventDefault();
|
||||
event.stopImmediatePropagation();
|
||||
selectedHandler(messageSelection);
|
||||
};
|
||||
manager = { handlers, listener };
|
||||
shortcutManagers.set(ownerDocument, manager);
|
||||
ownerDocument.addEventListener("keydown", listener, true);
|
||||
}
|
||||
manager.handlers.set(root, handler);
|
||||
return () => {
|
||||
const current = shortcutManagers.get(ownerDocument);
|
||||
if (!current) return;
|
||||
current.handlers.delete(root);
|
||||
if (current.handlers.size > 0) return;
|
||||
ownerDocument.removeEventListener("keydown", current.listener, true);
|
||||
shortcutManagers.delete(ownerDocument);
|
||||
};
|
||||
}
|
||||
|
||||
export function useMessageCommentShortcut(
|
||||
rootRef: RefObject<HTMLDivElement | null>,
|
||||
isSelectable: boolean,
|
||||
onSelect: ShortcutHandler,
|
||||
) {
|
||||
useEffect(() => {
|
||||
const root = rootRef.current;
|
||||
if (!isSelectable || !root) return;
|
||||
return registerMessageCommentShortcut(root, onSelect);
|
||||
}, [isSelectable, onSelect, rootRef]);
|
||||
}
|
||||
|
||||
type HighlightRegistry = {
|
||||
set: (name: string, highlight: unknown) => void;
|
||||
delete: (name: string) => void;
|
||||
};
|
||||
|
||||
type HighlightConstructor = new (...ranges: Range[]) => unknown;
|
||||
|
||||
function highlightRegistry() {
|
||||
return (
|
||||
globalThis as typeof globalThis & {
|
||||
CSS?: typeof CSS & { highlights?: HighlightRegistry };
|
||||
}
|
||||
).CSS?.highlights;
|
||||
}
|
||||
|
||||
export function supportsCustomHighlights() {
|
||||
const HighlightClass = (globalThis as typeof globalThis & { Highlight?: HighlightConstructor })
|
||||
.Highlight;
|
||||
return Boolean(highlightRegistry() && HighlightClass);
|
||||
}
|
||||
|
||||
function updateCustomHighlight(name: string, ranges: Range[]) {
|
||||
const registry = highlightRegistry();
|
||||
const HighlightClass = (globalThis as typeof globalThis & { Highlight?: HighlightConstructor })
|
||||
.Highlight;
|
||||
if (!registry || !HighlightClass) return;
|
||||
if (ranges.length === 0) registry.delete(name);
|
||||
else registry.set(name, new HighlightClass(...ranges));
|
||||
}
|
||||
|
||||
export function useMessageCommentDecorations(
|
||||
rootRef: RefObject<HTMLDivElement | null>,
|
||||
comments: AgentMessageComment[],
|
||||
messageContent: string,
|
||||
highlightName: string,
|
||||
): MessageCommentDecoration[] {
|
||||
const [decorations, setDecorations] = useState<MessageCommentDecoration[]>([]);
|
||||
|
||||
useLayoutEffect(() => {
|
||||
const root = rootRef.current;
|
||||
if (!root) return;
|
||||
if (comments.length === 0) {
|
||||
highlightRegistry()?.delete(highlightName);
|
||||
setDecorations((current) => (current.length === 0 ? current : []));
|
||||
return;
|
||||
}
|
||||
let disposed = false;
|
||||
const refresh = () => {
|
||||
if (disposed) return;
|
||||
const next = getMessageCommentDecorations(root, comments);
|
||||
updateCustomHighlight(
|
||||
highlightName,
|
||||
next.map((decoration) => decoration.range),
|
||||
);
|
||||
setDecorations(next);
|
||||
};
|
||||
refresh();
|
||||
const observer = typeof ResizeObserver === "undefined" ? null : new ResizeObserver(refresh);
|
||||
observer?.observe(root);
|
||||
return () => {
|
||||
disposed = true;
|
||||
observer?.disconnect();
|
||||
highlightRegistry()?.delete(highlightName);
|
||||
};
|
||||
}, [comments, highlightName, messageContent, rootRef]);
|
||||
|
||||
return decorations;
|
||||
}
|
||||
|
||||
export function messageCommentDecorationAtPoint(
|
||||
decorations: MessageCommentDecoration[],
|
||||
x: number,
|
||||
y: number,
|
||||
) {
|
||||
return decorations.find((decoration) =>
|
||||
Array.from(decoration.range.getClientRects()).some(
|
||||
(rect) => x >= rect.left && x <= rect.right && y >= rect.top && y <= rect.bottom,
|
||||
),
|
||||
);
|
||||
}
|
||||
@@ -13,23 +13,20 @@ import { useProcessedMessages } from "@/hooks/use-processed-messages";
|
||||
import { useSessionModel } from "@/hooks/domains/session/use-session-model";
|
||||
import { useQueue } from "@/hooks/domains/session/use-queue";
|
||||
import { useContextFilesStore, type ContextFile } from "@/lib/state/context-files-store";
|
||||
import {
|
||||
useCommentsStore,
|
||||
isPlanComment,
|
||||
isPRFeedbackComment,
|
||||
isWalkthroughComment,
|
||||
} from "@/lib/state/slices/comments";
|
||||
import { useCommentsStore, isPlanComment, type Comment } from "@/lib/state/slices/comments";
|
||||
import { usePendingDiffCommentsByFile } from "@/hooks/domains/comments/use-diff-comments";
|
||||
import {
|
||||
usePendingPlanComments,
|
||||
usePendingPRFeedback,
|
||||
usePendingWalkthroughComments,
|
||||
usePendingAgentMessageComments,
|
||||
} from "@/hooks/domains/comments/use-pending-comments";
|
||||
import { buildContextItems } from "../chat-context-items";
|
||||
import { useAutoDisablePlanMode, usePlanLayoutHandlers } from "./use-plan-mode-helpers";
|
||||
import type { ContextItem } from "@/lib/types/context";
|
||||
import type { DiffComment } from "@/lib/diff/types";
|
||||
import type {
|
||||
AgentMessageComment,
|
||||
PlanComment,
|
||||
PRFeedbackComment,
|
||||
WalkthroughComment,
|
||||
@@ -53,6 +50,7 @@ export type CommentsState = {
|
||||
pendingCommentsByFile: Record<string, DiffComment[]>;
|
||||
pendingPRFeedback: PRFeedbackComment[];
|
||||
walkthroughComments: WalkthroughComment[];
|
||||
messageComments: AgentMessageComment[];
|
||||
markCommentsSent: (ids: string[]) => void;
|
||||
handleRemoveCommentFile: (filePath: string) => void;
|
||||
handleRemoveComment: (commentId: string) => void;
|
||||
@@ -60,9 +58,19 @@ export type CommentsState = {
|
||||
handleClearPRFeedback: () => void;
|
||||
handleRemoveWalkthroughComment: (commentId: string) => void;
|
||||
handleClearWalkthroughComments: () => void;
|
||||
handleClearMessageComments: () => void;
|
||||
clearSessionPlanComments: () => void;
|
||||
};
|
||||
|
||||
function removePendingCommentsForSession(sessionId: string | null, source: Comment["source"]) {
|
||||
if (!sessionId) return;
|
||||
const state = useCommentsStore.getState();
|
||||
for (const id of [...state.pendingForChat]) {
|
||||
const comment = state.byId[id];
|
||||
if (comment?.sessionId === sessionId && comment.source === source) state.removeComment(id);
|
||||
}
|
||||
}
|
||||
|
||||
/** Re-focus chat input after dockview layout rebuild. */
|
||||
function useRefocusChatAfterLayout() {
|
||||
return useCallback(() => {
|
||||
@@ -257,11 +265,11 @@ export function useCommentsState(resolvedSessionId: string | null): CommentsStat
|
||||
useEffect(() => {
|
||||
if (resolvedSessionId) hydrateComments(resolvedSessionId);
|
||||
}, [resolvedSessionId, hydrateComments]);
|
||||
// Filter pending comments by sessionId to prevent cross-session leakage
|
||||
const planComments = usePendingPlanComments(resolvedSessionId);
|
||||
const pendingCommentsByFile = usePendingDiffCommentsByFile(resolvedSessionId);
|
||||
const pendingPRFeedback = usePendingPRFeedback(resolvedSessionId);
|
||||
const walkthroughComments = usePendingWalkthroughComments(resolvedSessionId);
|
||||
const messageComments = usePendingAgentMessageComments(resolvedSessionId);
|
||||
const markCommentsSent = useCommentsStore((state) => state.markCommentsSent);
|
||||
const removeComment = useCommentsStore((state) => state.removeComment);
|
||||
const clearSessionPlanComments = useCallback(() => {
|
||||
@@ -289,37 +297,24 @@ export function useCommentsState(resolvedSessionId: string | null): CommentsStat
|
||||
[removeComment],
|
||||
);
|
||||
const handleClearPRFeedback = useCallback(() => {
|
||||
if (!resolvedSessionId) return;
|
||||
const state = useCommentsStore.getState();
|
||||
const allPending = [...state.pendingForChat];
|
||||
for (const id of allPending) {
|
||||
const c = state.byId[id];
|
||||
// Only clear PR feedback for the current session
|
||||
if (c && isPRFeedbackComment(c) && c.sessionId === resolvedSessionId) {
|
||||
state.removeComment(id);
|
||||
}
|
||||
}
|
||||
removePendingCommentsForSession(resolvedSessionId, "pr-feedback");
|
||||
}, [resolvedSessionId]);
|
||||
const handleRemoveWalkthroughComment = useCallback(
|
||||
(commentId: string) => removeComment(commentId),
|
||||
[removeComment],
|
||||
);
|
||||
const handleClearWalkthroughComments = useCallback(() => {
|
||||
if (!resolvedSessionId) return;
|
||||
const state = useCommentsStore.getState();
|
||||
const allPending = [...state.pendingForChat];
|
||||
for (const id of allPending) {
|
||||
const c = state.byId[id];
|
||||
if (c && isWalkthroughComment(c) && c.sessionId === resolvedSessionId) {
|
||||
state.removeComment(id);
|
||||
}
|
||||
}
|
||||
removePendingCommentsForSession(resolvedSessionId, "walkthrough");
|
||||
}, [resolvedSessionId]);
|
||||
const handleClearMessageComments = useCallback(() => {
|
||||
removePendingCommentsForSession(resolvedSessionId, "agent-message");
|
||||
}, [resolvedSessionId]);
|
||||
return {
|
||||
planComments,
|
||||
pendingCommentsByFile,
|
||||
pendingPRFeedback,
|
||||
walkthroughComments,
|
||||
messageComments,
|
||||
markCommentsSent,
|
||||
handleRemoveCommentFile,
|
||||
handleRemoveComment,
|
||||
@@ -327,6 +322,7 @@ export function useCommentsState(resolvedSessionId: string | null): CommentsStat
|
||||
handleClearPRFeedback,
|
||||
handleRemoveWalkthroughComment,
|
||||
handleClearWalkthroughComments,
|
||||
handleClearMessageComments,
|
||||
clearSessionPlanComments,
|
||||
};
|
||||
}
|
||||
@@ -387,6 +383,8 @@ function useChatContextItems(opts: ChatContextItemsOptions) {
|
||||
walkthroughComments: comments.walkthroughComments,
|
||||
handleRemoveWalkthroughComment: comments.handleRemoveWalkthroughComment,
|
||||
handleClearWalkthroughComments: comments.handleClearWalkthroughComments,
|
||||
messageComments: comments.messageComments,
|
||||
handleClearMessageComments: comments.handleClearMessageComments,
|
||||
taskId,
|
||||
}),
|
||||
[
|
||||
@@ -410,6 +408,8 @@ function useChatContextItems(opts: ChatContextItemsOptions) {
|
||||
comments.walkthroughComments,
|
||||
comments.handleRemoveWalkthroughComment,
|
||||
comments.handleClearWalkthroughComments,
|
||||
comments.messageComments,
|
||||
comments.handleClearMessageComments,
|
||||
taskId,
|
||||
],
|
||||
);
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { placeFloatingRect, type FloatingBounds } from "./floating-selection-position";
|
||||
|
||||
describe("placeFloatingRect", () => {
|
||||
const dialogBounds: FloatingBounds = { left: 100, top: 50, right: 500, bottom: 450 };
|
||||
|
||||
it("clamps horizontal placement to the portal container", () => {
|
||||
expect(
|
||||
placeFloatingRect({
|
||||
left: 480,
|
||||
topCandidates: [100],
|
||||
width: 340,
|
||||
height: 180,
|
||||
bounds: dialogBounds,
|
||||
}),
|
||||
).toEqual({ left: 152, top: 100 });
|
||||
});
|
||||
|
||||
it("uses the first fitting vertical candidate within the container", () => {
|
||||
expect(
|
||||
placeFloatingRect({
|
||||
left: 120,
|
||||
topCandidates: [430, 246],
|
||||
width: 40,
|
||||
height: 180,
|
||||
bounds: dialogBounds,
|
||||
}),
|
||||
).toEqual({ left: 120, top: 246 });
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,45 @@
|
||||
export type FloatingBounds = {
|
||||
left: number;
|
||||
top: number;
|
||||
right: number;
|
||||
bottom: number;
|
||||
};
|
||||
|
||||
export function floatingBounds(rect?: Pick<DOMRect, "left" | "top" | "right" | "bottom"> | null) {
|
||||
return rect
|
||||
? { left: rect.left, top: rect.top, right: rect.right, bottom: rect.bottom }
|
||||
: { left: 0, top: 0, right: window.innerWidth, bottom: window.innerHeight };
|
||||
}
|
||||
|
||||
function clamp(value: number, minimum: number, maximum: number) {
|
||||
return Math.min(Math.max(value, minimum), maximum);
|
||||
}
|
||||
|
||||
export function placeFloatingRect({
|
||||
left,
|
||||
topCandidates,
|
||||
width,
|
||||
height,
|
||||
bounds,
|
||||
margin = 8,
|
||||
}: {
|
||||
left: number;
|
||||
topCandidates: number[];
|
||||
width: number;
|
||||
height: number;
|
||||
bounds: FloatingBounds;
|
||||
margin?: number;
|
||||
}) {
|
||||
const minimumLeft = bounds.left + margin;
|
||||
const maximumLeft = Math.max(minimumLeft, bounds.right - width - margin);
|
||||
const minimumTop = bounds.top + margin;
|
||||
const maximumTop = Math.max(minimumTop, bounds.bottom - height - margin);
|
||||
const fittingTop = topCandidates.find(
|
||||
(candidate) => candidate >= minimumTop && candidate <= maximumTop,
|
||||
);
|
||||
|
||||
return {
|
||||
left: clamp(left, minimumLeft, maximumLeft),
|
||||
top: fittingTop ?? clamp(topCandidates[0] ?? minimumTop, minimumTop, maximumTop),
|
||||
};
|
||||
}
|
||||
@@ -40,6 +40,27 @@ function comment(id = "comment-1"): DiffComment {
|
||||
};
|
||||
}
|
||||
|
||||
function messageComment() {
|
||||
return {
|
||||
id: "message-comment-1",
|
||||
sessionId: SESSION_ID,
|
||||
source: "agent-message",
|
||||
messageId: "reply-1",
|
||||
selectedText: "settled answer",
|
||||
anchor: {
|
||||
messageId: "reply-1",
|
||||
start: 0,
|
||||
end: 14,
|
||||
selectedText: "settled answer",
|
||||
prefix: "",
|
||||
suffix: "",
|
||||
},
|
||||
text: "Please expand this.",
|
||||
createdAt: "2026-07-20T00:00:00.000Z",
|
||||
status: "pending",
|
||||
} as never;
|
||||
}
|
||||
|
||||
function panelState(overrides: Record<string, unknown> = {}) {
|
||||
return {
|
||||
resolvedSessionId: SESSION_ID,
|
||||
@@ -49,10 +70,12 @@ function panelState(overrides: Record<string, unknown> = {}) {
|
||||
pendingPRFeedback: [],
|
||||
planComments: [],
|
||||
walkthroughComments: [],
|
||||
messageComments: [],
|
||||
planModeEnabled: false,
|
||||
handleClearPRFeedback: vi.fn(),
|
||||
clearSessionPlanComments: vi.fn(),
|
||||
handleClearWalkthroughComments: vi.fn(),
|
||||
handleClearMessageComments: vi.fn(),
|
||||
clearEphemeral: vi.fn(),
|
||||
addContextFile: vi.fn(),
|
||||
...overrides,
|
||||
@@ -85,6 +108,18 @@ describe("passthrough chat composer metadata helpers", () => {
|
||||
expect(result.formatted).toContain("Ship it");
|
||||
});
|
||||
|
||||
it("includes pending agent message comments in passthrough context once", () => {
|
||||
const result = formatPassthroughBaseMessage(
|
||||
"Ship it",
|
||||
undefined,
|
||||
[],
|
||||
panelState({ messageComments: [messageComment()] }),
|
||||
);
|
||||
|
||||
expect(result.formatted.match(/### Agent Message Comments/g)).toHaveLength(1);
|
||||
expect(result.commentsToSend.map((item) => item.id)).toEqual(["message-comment-1"]);
|
||||
});
|
||||
|
||||
it("merges selected context files with inline file, prompt, and task mentions", async () => {
|
||||
const inlineTask: TaskMentionData = {
|
||||
taskId: "task-2",
|
||||
@@ -211,10 +246,12 @@ describe("passthrough chat composer cleanup", () => {
|
||||
pendingPRFeedback: [{ id: "feedback-1" }],
|
||||
planComments: [{ id: "plan-comment-1" }],
|
||||
walkthroughComments: [{ id: "walkthrough-comment-1" }],
|
||||
messageComments: [messageComment()],
|
||||
}) as unknown as {
|
||||
handleClearPRFeedback: ReturnType<typeof vi.fn>;
|
||||
clearSessionPlanComments: ReturnType<typeof vi.fn>;
|
||||
handleClearWalkthroughComments: ReturnType<typeof vi.fn>;
|
||||
handleClearMessageComments: ReturnType<typeof vi.fn>;
|
||||
clearEphemeral: ReturnType<typeof vi.fn>;
|
||||
addContextFile: ReturnType<typeof vi.fn>;
|
||||
};
|
||||
@@ -224,6 +261,7 @@ describe("passthrough chat composer cleanup", () => {
|
||||
expect(state.handleClearPRFeedback).toHaveBeenCalledTimes(1);
|
||||
expect(state.clearSessionPlanComments).toHaveBeenCalledTimes(1);
|
||||
expect(state.handleClearWalkthroughComments).toHaveBeenCalledTimes(1);
|
||||
expect(state.handleClearMessageComments).toHaveBeenCalledTimes(1);
|
||||
expect(state.clearEphemeral).toHaveBeenCalledWith(SESSION_ID);
|
||||
expect(state.addContextFile).toHaveBeenCalledWith(SESSION_ID, {
|
||||
path: PLAN_CONTEXT_PATH,
|
||||
|
||||
@@ -13,6 +13,7 @@ import {
|
||||
} from "./chat/chat-input-container";
|
||||
import type { useChatPanelState } from "./chat/use-chat-panel-state";
|
||||
import type { DiffComment } from "@/lib/diff/types";
|
||||
import type { AgentMessageComment } from "@/lib/state/slices/comments";
|
||||
import type { ContextFile } from "@/lib/state/context-files-store";
|
||||
import type { TaskMentionData } from "@/hooks/use-inline-mention";
|
||||
import { buildContextFilesContext, buildTaskMentionsContext } from "@/hooks/use-message-handler";
|
||||
@@ -52,7 +53,8 @@ export function PassthroughComposerPanel({
|
||||
const hasContextComments =
|
||||
panelState.planComments.length > 0 ||
|
||||
panelState.pendingPRFeedback.length > 0 ||
|
||||
panelState.walkthroughComments.length > 0;
|
||||
panelState.walkthroughComments.length > 0 ||
|
||||
panelState.messageComments.length > 0;
|
||||
return (
|
||||
<div
|
||||
data-testid="passthrough-composer"
|
||||
@@ -97,7 +99,7 @@ export function PassthroughComposerPanel({
|
||||
|
||||
type PassthroughFinalMessage = {
|
||||
content: string;
|
||||
commentsToSend: DiffComment[];
|
||||
commentsToSend: Array<DiffComment | AgentMessageComment>;
|
||||
contextFilesMeta?: Array<{ path: string; name: string }>;
|
||||
};
|
||||
|
||||
@@ -108,21 +110,24 @@ export function formatPassthroughBaseMessage(
|
||||
panelState: ReturnType<typeof useChatPanelState>,
|
||||
) {
|
||||
const commentsToSend = reviewComments ?? pendingComments;
|
||||
const messageComments = panelState.messageComments;
|
||||
const hasStructuredComments =
|
||||
!!reviewComments ||
|
||||
panelState.pendingPRFeedback.length > 0 ||
|
||||
panelState.planComments.length > 0 ||
|
||||
panelState.walkthroughComments.length > 0;
|
||||
panelState.walkthroughComments.length > 0 ||
|
||||
messageComments.length > 0;
|
||||
if (hasStructuredComments) {
|
||||
return {
|
||||
formatted: buildSubmitMessage(
|
||||
content,
|
||||
commentsToSend.length > 0 ? commentsToSend : undefined,
|
||||
panelState.pendingPRFeedback,
|
||||
panelState.planComments,
|
||||
panelState.walkthroughComments,
|
||||
),
|
||||
commentsToSend,
|
||||
formatted: buildSubmitMessage({
|
||||
message: content,
|
||||
reviewComments: commentsToSend.length > 0 ? commentsToSend : undefined,
|
||||
pendingPRFeedback: panelState.pendingPRFeedback,
|
||||
planComments: panelState.planComments,
|
||||
walkthroughComments: panelState.walkthroughComments,
|
||||
messageComments,
|
||||
}),
|
||||
commentsToSend: [...commentsToSend, ...messageComments],
|
||||
};
|
||||
}
|
||||
if (pendingComments.length > 0) {
|
||||
@@ -258,6 +263,9 @@ export function clearPassthroughComposerContext(panelState: ReturnType<typeof us
|
||||
if (panelState.walkthroughComments.length > 0) {
|
||||
panelState.handleClearWalkthroughComments();
|
||||
}
|
||||
if (panelState.messageComments.length > 0) {
|
||||
panelState.handleClearMessageComments();
|
||||
}
|
||||
if (!panelState.resolvedSessionId) return;
|
||||
panelState.clearEphemeral(panelState.resolvedSessionId);
|
||||
if (panelState.planModeEnabled) {
|
||||
|
||||
@@ -160,6 +160,8 @@ vi.mock("./chat/use-chat-panel-state", () => ({
|
||||
planComments: [],
|
||||
pendingPRFeedback: [],
|
||||
walkthroughComments: [],
|
||||
messageComments: [],
|
||||
handleClearMessageComments: vi.fn(),
|
||||
pendingCommentsByFile: mockPendingByFile,
|
||||
}),
|
||||
}));
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import { cleanup, fireEvent, render, screen } from "@testing-library/react";
|
||||
import { PlanSelectionPopover } from "./plan-selection-popover";
|
||||
|
||||
afterEach(cleanup);
|
||||
|
||||
describe("PlanSelectionPopover", () => {
|
||||
it("keeps entered feedback open when the submitter rejects the save", () => {
|
||||
const onClose = vi.fn();
|
||||
render(
|
||||
<PlanSelectionPopover
|
||||
selectedText="settled answer"
|
||||
position={{ x: 100, y: 100 }}
|
||||
onAdd={() => false}
|
||||
onClose={onClose}
|
||||
/>,
|
||||
);
|
||||
|
||||
const input = screen.getByPlaceholderText("Add your comment or instruction...");
|
||||
fireEvent.change(input, { target: { value: "Keep this feedback" } });
|
||||
fireEvent.click(screen.getByRole("button", { name: "Add" }));
|
||||
|
||||
expect(onClose).not.toHaveBeenCalled();
|
||||
expect((input as HTMLTextAreaElement).value).toBe("Keep this feedback");
|
||||
});
|
||||
});
|
||||
@@ -7,6 +7,7 @@ import { Button } from "@kandev/ui/button";
|
||||
import { Textarea } from "@kandev/ui/textarea";
|
||||
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@kandev/ui/tooltip";
|
||||
import { cn } from "@/lib/utils";
|
||||
import { floatingBounds, placeFloatingRect } from "@/components/task/floating-selection-position";
|
||||
|
||||
type SelectionPosition = {
|
||||
x: number;
|
||||
@@ -16,37 +17,23 @@ type SelectionPosition = {
|
||||
type PlanSelectionPopoverProps = {
|
||||
selectedText: string;
|
||||
position: SelectionPosition;
|
||||
onAdd: (comment: string, selectedText: string) => void;
|
||||
onAddAndRun?: (comment: string, selectedText: string) => void;
|
||||
onAdd: (comment: string, selectedText: string) => boolean | void;
|
||||
onAddAndRun?: (comment: string, selectedText: string) => boolean | void;
|
||||
onClose: () => void;
|
||||
editingComment?: string;
|
||||
onDelete?: () => void;
|
||||
testId?: string;
|
||||
inputTestId?: string;
|
||||
addButtonTestId?: string;
|
||||
runButtonTestId?: string;
|
||||
portalContainer?: HTMLElement | null;
|
||||
errorMessage?: string | null;
|
||||
};
|
||||
|
||||
const POPOVER_WIDTH = 340;
|
||||
const POPOVER_HEIGHT = 180;
|
||||
const MARGIN = 8;
|
||||
|
||||
function computePopoverPosition(position: SelectionPosition): { left: number; top: number } {
|
||||
// Place directly below cursor, aligned to left of click
|
||||
let left = position.x;
|
||||
let top = position.y + 4;
|
||||
|
||||
// Clamp horizontal — keep popover on screen
|
||||
if (left + POPOVER_WIDTH > window.innerWidth - MARGIN) {
|
||||
left = window.innerWidth - POPOVER_WIDTH - MARGIN;
|
||||
}
|
||||
if (left < MARGIN) left = MARGIN;
|
||||
|
||||
// If overflows bottom, flip above cursor
|
||||
if (top + POPOVER_HEIGHT > window.innerHeight - MARGIN) {
|
||||
top = position.y - POPOVER_HEIGHT - 4;
|
||||
}
|
||||
if (top < MARGIN) top = MARGIN;
|
||||
|
||||
return { left, top };
|
||||
}
|
||||
|
||||
/** Drag support for the popover. */
|
||||
function useDrag() {
|
||||
const [offset, setOffset] = useState({ dx: 0, dy: 0 });
|
||||
@@ -111,20 +98,18 @@ function usePopoverDismiss(
|
||||
function usePopoverComposer(
|
||||
comment: string,
|
||||
selectedText: string,
|
||||
onAdd: (comment: string, selectedText: string) => void,
|
||||
onAdd: (comment: string, selectedText: string) => boolean | void,
|
||||
onClose: () => void,
|
||||
onAddAndRun?: (comment: string, selectedText: string) => void,
|
||||
onAddAndRun?: (comment: string, selectedText: string) => boolean | void,
|
||||
) {
|
||||
const handleSubmit = useCallback(() => {
|
||||
if (!comment.trim()) return;
|
||||
onAdd(comment.trim(), selectedText);
|
||||
onClose();
|
||||
if (onAdd(comment.trim(), selectedText) !== false) onClose();
|
||||
}, [comment, onAdd, selectedText, onClose]);
|
||||
|
||||
const handleSubmitAndRun = useCallback(() => {
|
||||
if (!comment.trim() || !onAddAndRun) return;
|
||||
onAddAndRun(comment.trim(), selectedText);
|
||||
onClose();
|
||||
if (onAddAndRun(comment.trim(), selectedText) !== false) onClose();
|
||||
}, [comment, onAddAndRun, selectedText, onClose]);
|
||||
|
||||
const handleKeyDown = useCallback(
|
||||
@@ -157,12 +142,16 @@ function PopoverActions({
|
||||
onSubmit,
|
||||
onSubmitAndRun,
|
||||
onDelete,
|
||||
addButtonTestId,
|
||||
runButtonTestId,
|
||||
}: {
|
||||
isEditing: boolean;
|
||||
isDisabled: boolean;
|
||||
onSubmit: () => void;
|
||||
onSubmitAndRun?: () => void;
|
||||
onDelete?: () => void;
|
||||
addButtonTestId?: string;
|
||||
runButtonTestId?: string;
|
||||
}) {
|
||||
return (
|
||||
<div className="mt-2 flex items-center justify-between">
|
||||
@@ -176,6 +165,7 @@ function PopoverActions({
|
||||
size="sm"
|
||||
variant="ghost"
|
||||
onClick={onDelete}
|
||||
aria-label="Delete comment"
|
||||
className="h-6 px-1.5 text-muted-foreground hover:text-destructive cursor-pointer"
|
||||
>
|
||||
<IconTrash className="h-3 w-3" />
|
||||
@@ -191,6 +181,7 @@ function PopoverActions({
|
||||
variant={onSubmitAndRun && !isEditing ? "outline" : "default"}
|
||||
onClick={onSubmit}
|
||||
disabled={isDisabled}
|
||||
data-testid={addButtonTestId}
|
||||
className={`h-7 gap-1 text-xs cursor-pointer ${onSubmitAndRun && !isEditing ? "rounded-r-none border-r-0" : ""}`}
|
||||
>
|
||||
<IconPlus className="h-3 w-3" />
|
||||
@@ -208,6 +199,7 @@ function PopoverActions({
|
||||
size="sm"
|
||||
onClick={onSubmitAndRun}
|
||||
disabled={isDisabled}
|
||||
data-testid={runButtonTestId}
|
||||
className="h-7 gap-1 rounded-l-none text-xs cursor-pointer"
|
||||
>
|
||||
<IconPlayerPlay className="h-3 w-3" />
|
||||
@@ -233,6 +225,12 @@ export function PlanSelectionPopover({
|
||||
onClose,
|
||||
editingComment,
|
||||
onDelete,
|
||||
testId,
|
||||
inputTestId,
|
||||
addButtonTestId,
|
||||
runButtonTestId,
|
||||
portalContainer,
|
||||
errorMessage,
|
||||
}: PlanSelectionPopoverProps) {
|
||||
const [comment, setComment] = useState(editingComment || "");
|
||||
const textareaRef = useRef<HTMLTextAreaElement>(null);
|
||||
@@ -246,7 +244,15 @@ export function PlanSelectionPopover({
|
||||
const effectiveOnAddAndRun = editingComment ? undefined : onAddAndRun;
|
||||
const { handleSubmit, handleSubmitAndRun, handleKeyDown, isDisabled, previewText } =
|
||||
usePopoverComposer(comment, selectedText, onAdd, onClose, effectiveOnAddAndRun);
|
||||
const { left, top } = computePopoverPosition(position);
|
||||
const portalRect = portalContainer?.getBoundingClientRect();
|
||||
const { left, top } = placeFloatingRect({
|
||||
left: position.x,
|
||||
topCandidates: [position.y + 4, position.y - POPOVER_HEIGHT - 4],
|
||||
width: POPOVER_WIDTH,
|
||||
height: POPOVER_HEIGHT,
|
||||
bounds: floatingBounds(portalRect),
|
||||
margin: MARGIN,
|
||||
});
|
||||
|
||||
const handleDelete = onDelete
|
||||
? () => {
|
||||
@@ -259,10 +265,16 @@ export function PlanSelectionPopover({
|
||||
<div
|
||||
ref={popoverRef}
|
||||
className={cn(
|
||||
"fixed z-50 rounded-xl border border-border/50 bg-popover/95 backdrop-blur-sm shadow-xl",
|
||||
"pointer-events-auto z-[60] rounded-xl border border-border/50 bg-popover/95 backdrop-blur-sm shadow-xl",
|
||||
"animate-in fade-in-0 zoom-in-95 duration-150",
|
||||
portalContainer ? "absolute" : "fixed",
|
||||
)}
|
||||
style={{ width: POPOVER_WIDTH, left: left + offset.dx, top: top + offset.dy }}
|
||||
data-testid={testId}
|
||||
style={{
|
||||
width: POPOVER_WIDTH,
|
||||
left: left + offset.dx - (portalRect?.left ?? 0),
|
||||
top: top + offset.dy - (portalRect?.top ?? 0),
|
||||
}}
|
||||
>
|
||||
<div
|
||||
onMouseDown={onMouseDown}
|
||||
@@ -281,16 +293,24 @@ export function PlanSelectionPopover({
|
||||
onKeyDown={handleKeyDown}
|
||||
placeholder="Add your comment or instruction..."
|
||||
className="min-h-[60px] resize-none text-sm border-border/50 focus:border-primary/50"
|
||||
data-testid={inputTestId}
|
||||
/>
|
||||
{errorMessage ? (
|
||||
<p role="alert" className="mt-2 text-xs text-destructive">
|
||||
{errorMessage}
|
||||
</p>
|
||||
) : null}
|
||||
<PopoverActions
|
||||
isEditing={!!editingComment}
|
||||
isDisabled={isDisabled}
|
||||
onSubmit={handleSubmit}
|
||||
onSubmitAndRun={effectiveOnAddAndRun ? handleSubmitAndRun : undefined}
|
||||
onDelete={handleDelete}
|
||||
addButtonTestId={addButtonTestId}
|
||||
runButtonTestId={runButtonTestId}
|
||||
/>
|
||||
</div>
|
||||
</div>,
|
||||
document.body,
|
||||
portalContainer ?? document.body,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import { cleanup, render } from "@testing-library/react";
|
||||
import { sessionId as toSessionId, taskId as toTaskId, type TaskSession } from "@/lib/types/http";
|
||||
|
||||
const mocks = vi.hoisted(() => ({
|
||||
getWebSocketClient: vi.fn(),
|
||||
onSend: null as null | ((content: string) => Promise<void>),
|
||||
}));
|
||||
|
||||
vi.mock("@/lib/ws/connection", () => ({
|
||||
getWebSocketClient: mocks.getWebSocketClient,
|
||||
}));
|
||||
vi.mock("./task-chat-panel", () => ({
|
||||
TaskChatPanel: ({ onSend }: { onSend: (content: string) => Promise<void> }) => {
|
||||
mocks.onSend = onSend;
|
||||
return <div data-testid="preview-chat" />;
|
||||
},
|
||||
}));
|
||||
|
||||
import { PreviewSessionBody } from "./preview-session-tabs";
|
||||
|
||||
const session: TaskSession = {
|
||||
id: toSessionId("session-1"),
|
||||
task_id: toTaskId("task-1"),
|
||||
state: "COMPLETED",
|
||||
started_at: "2026-07-21T00:00:00Z",
|
||||
updated_at: "2026-07-21T00:00:00Z",
|
||||
};
|
||||
|
||||
afterEach(() => {
|
||||
cleanup();
|
||||
mocks.getWebSocketClient.mockReset();
|
||||
mocks.onSend = null;
|
||||
});
|
||||
|
||||
describe("PreviewSessionBody send failures", () => {
|
||||
it("rejects when the WebSocket client is unavailable", async () => {
|
||||
mocks.getWebSocketClient.mockReturnValue(null);
|
||||
render(<PreviewSessionBody session={session} taskId="task-1" />);
|
||||
|
||||
await expect(mocks.onSend?.("hello")).rejects.toMatchObject({
|
||||
name: "MessageSendError",
|
||||
code: "connection-unavailable",
|
||||
message: "Connection unavailable. Reconnect and try again.",
|
||||
});
|
||||
});
|
||||
|
||||
it("rethrows message.add failures to the chat input", async () => {
|
||||
const error = new Error("message.add failed");
|
||||
mocks.getWebSocketClient.mockReturnValue({ request: vi.fn().mockRejectedValue(error) });
|
||||
render(<PreviewSessionBody session={session} taskId="task-1" />);
|
||||
|
||||
await expect(mocks.onSend?.("hello")).rejects.toBe(error);
|
||||
});
|
||||
});
|
||||
@@ -6,11 +6,11 @@ import { GridSpinner } from "@/components/grid-spinner";
|
||||
import { PanelLoadingState } from "@/components/panel-loading-state";
|
||||
import { SessionTabs, type SessionTab } from "@/components/session-tabs";
|
||||
import { useAppStore } from "@/components/state-provider";
|
||||
import { useToast } from "@/components/toast-provider";
|
||||
import { useSessionResumption } from "@/hooks/domains/session/use-session-resumption";
|
||||
import { useTaskSessions } from "@/hooks/use-task-sessions";
|
||||
import type { UseEnsureTaskSessionResult } from "@/hooks/domains/session/use-ensure-task-session";
|
||||
import type { AgentProfileOption } from "@/lib/state/slices";
|
||||
import { MessageSendError } from "@/lib/chat/message-send-error";
|
||||
import type { TaskSession } from "@/lib/types/http";
|
||||
import { getWebSocketClient } from "@/lib/ws/connection";
|
||||
import { EnsureSessionErrorEmptyState } from "./ensure-session-error";
|
||||
@@ -149,24 +149,23 @@ function SessionAgentLogo({ profile }: { profile: AgentProfileOption | null | un
|
||||
return <AgentLogo agentName={profile.agent_name} size={12} className="shrink-0" />;
|
||||
}
|
||||
|
||||
function PreviewSessionBody({ session, taskId }: { session: TaskSession; taskId: string }) {
|
||||
const { toast } = useToast();
|
||||
export function PreviewSessionBody({ session, taskId }: { session: TaskSession; taskId: string }) {
|
||||
const handleSendMessage = useCallback(
|
||||
async (content: string) => {
|
||||
const client = getWebSocketClient();
|
||||
if (!client) return;
|
||||
try {
|
||||
await client.request(
|
||||
"message.add",
|
||||
{ task_id: taskId, session_id: session.id, content },
|
||||
10000,
|
||||
if (!client) {
|
||||
throw new MessageSendError(
|
||||
"connection-unavailable",
|
||||
"Connection unavailable. Reconnect and try again.",
|
||||
);
|
||||
} catch (error) {
|
||||
console.error("Failed to send message:", error);
|
||||
toast({ title: "Failed to send message", variant: "error" });
|
||||
}
|
||||
await client.request(
|
||||
"message.add",
|
||||
{ task_id: taskId, session_id: session.id, content },
|
||||
10000,
|
||||
);
|
||||
},
|
||||
[taskId, session.id, toast],
|
||||
[taskId, session.id],
|
||||
);
|
||||
|
||||
if (session.is_passthrough) {
|
||||
|
||||
Reference in New Issue
Block a user