Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/browser/components/ChatPane/ChatPane.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ import { useTranscriptDensity } from "@/browser/hooks/useTranscriptDensity";
import { useReviews } from "@/browser/hooks/useReviews";
import { ReviewsBanner } from "../ReviewsBanner/ReviewsBanner";
import type { ReviewNoteData } from "@/common/types/review";
import { CUSTOM_EVENTS, type CustomEventPayloads } from "@/common/constants/events";
import { useWorkspaceContext } from "@/browser/contexts/WorkspaceContext";
import {
useBackgroundBashActions,
Expand Down Expand Up @@ -728,6 +729,24 @@ const ChatPaneContent: React.FC<ChatPaneContentProps> = (props) => {
[contentRef, disableAutoScroll]
);

useEffect(() => {
const handler = (
event: CustomEvent<CustomEventPayloads[typeof CUSTOM_EVENTS.NAVIGATE_TO_TRANSCRIPT_MESSAGE]>
) => {
if (event.detail.workspaceId !== workspaceId) {
return;
}
handleNavigateToMessage(event.detail.historyId);
};

window.addEventListener(CUSTOM_EVENTS.NAVIGATE_TO_TRANSCRIPT_MESSAGE, handler as EventListener);
return () =>
window.removeEventListener(
CUSTOM_EVENTS.NAVIGATE_TO_TRANSCRIPT_MESSAGE,
handler as EventListener
);
}, [handleNavigateToMessage, workspaceId]);

// Precompute per-user navigation objects so MessageRenderer rows receive stable prop
// references across non-message updates (usage bumps, stats updates, etc.).
const userMessageNavigationByHistoryId = useMemo(() => {
Expand Down
76 changes: 73 additions & 3 deletions src/browser/features/ChatInput/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@ import { normalizeAgentId } from "@/common/utils/agentIds";
import { isGoalRunning } from "@/common/types/goal";
import { WORKSPACE_DEFAULTS } from "@/constants/workspaceDefaults";

const AWAITING_NEW_ATTACHED_REVIEWS = Symbol("awaiting-new-attached-reviews");

// localStorage quotas are environment-dependent and relatively small.
// Be conservative here so we can warn the user before writes start failing.

Expand Down Expand Up @@ -496,6 +498,13 @@ const ChatInputInner: React.FC<ChatInputProps> = (props) => {

// draftReviews takes precedence when restoring or editing message drafts.
const attachedReviews = variant === "workspace" ? (props.attachedReviews ?? []) : [];
const [draftMuxMetadataOverride, setDraftMuxMetadataOverride] = useState<
MuxMessageMetadata | undefined
>(undefined);
const attachedReviewIdsSignature = attachedReviews.map((review) => review.id).join("\u0000");
const clearedAttachedReviewIdsRef = useRef<string | typeof AWAITING_NEW_ATTACHED_REVIEWS | null>(
null
);
const draftReviewIdsByValueRef = useRef(new WeakMap<ReviewNoteDataForDisplay, string>());
const nextDraftReviewIdRef = useRef(0);
const isDraftReviewData = (value: unknown): value is ReviewNoteDataForDisplay =>
Expand Down Expand Up @@ -536,6 +545,31 @@ const ChatInputInner: React.FC<ChatInputProps> = (props) => {
return next;
});

// Empty review replacements clear the current parent-attached reviews but should not hide
// reviews attached later from the Code Review tab.
useEffect(() => {
if (
draftReviews === null ||
draftReviews.length > 0 ||
clearedAttachedReviewIdsRef.current === null
) {
return;
}

if (attachedReviews.length === 0) {
clearedAttachedReviewIdsRef.current = AWAITING_NEW_ATTACHED_REVIEWS;
return;
}

if (
clearedAttachedReviewIdsRef.current === AWAITING_NEW_ATTACHED_REVIEWS ||
clearedAttachedReviewIdsRef.current !== attachedReviewIdsSignature
) {
clearedAttachedReviewIdsRef.current = null;
setDraftReviews(null);
}
}, [attachedReviewIdsSignature, attachedReviews.length, draftReviews]);

// Creation sends can resolve after navigation; guard draft clears on unmounted inputs.
const isMountedRef = useRef(true);
useEffect(() => {
Expand Down Expand Up @@ -645,6 +679,7 @@ const ChatInputInner: React.FC<ChatInputProps> = (props) => {
);
const preEditDraftRef = useRef<DraftState>({ text: "", attachments: [] });
const preEditReviewsRef = useRef<ReviewNoteDataForDisplay[] | null>(null);
const preEditMuxMetadataOverrideRef = useRef<MuxMessageMetadata | undefined>(undefined);
const { open } = useSettings();
const { selectedWorkspace } = useWorkspaceContext();
const { agentId, currentAgent } = useAgent();
Expand Down Expand Up @@ -1209,6 +1244,7 @@ const ChatInputInner: React.FC<ChatInputProps> = (props) => {
(pending: PendingUserMessage) => {
applyDraftFromPending(pending, `restored-${Date.now()}`);
setDraftReviews(pending.reviews);
setDraftMuxMetadataOverride(pending.muxMetadata);
focusMessageInput();
},
[applyDraftFromPending, focusMessageInput, setDraftReviews]
Expand All @@ -1217,6 +1253,7 @@ const ChatInputInner: React.FC<ChatInputProps> = (props) => {
const restorePreEditDraft = useCallback(() => {
setDraft(preEditDraftRef.current);
setDraftReviews(preEditReviewsRef.current);
setDraftMuxMetadataOverride(preEditMuxMetadataOverrideRef.current);
}, [setDraft, setDraftReviews]);

// Method to restore text to input (used by compaction cancel)
Expand Down Expand Up @@ -1307,8 +1344,10 @@ const ChatInputInner: React.FC<ChatInputProps> = (props) => {
if (editingMessage) {
preEditDraftRef.current = getDraft();
preEditReviewsRef.current = draftReviews;
preEditMuxMetadataOverrideRef.current = draftMuxMetadataOverride;
applyDraftFromPending(editingMessage.pending, `edit-${editingMessage.id}`);
setDraftReviews(editingMessage.pending.reviews);
setDraftMuxMetadataOverride(editingMessage.pending.muxMetadata);
// Auto-resize textarea and focus
setTimeout(() => {
if (inputRef.current) {
Expand Down Expand Up @@ -1744,23 +1783,36 @@ const ChatInputInner: React.FC<ChatInputProps> = (props) => {
mode?: "append" | "replace";
fileParts?: FilePart[];
reviews?: ReviewNoteDataForDisplay[];
muxMetadata?: MuxMessageMetadata;
}>;

const { text, mode = "append", fileParts, reviews } = customEvent.detail;
const { text, mode = "append", fileParts, reviews, muxMetadata } = customEvent.detail;
const hasFileParts = !!fileParts && fileParts.length > 0;
const hasReviews = !!reviews && reviews.length > 0;
const hasDraftReplacementPayload =
fileParts !== undefined || reviews !== undefined || muxMetadata !== undefined;
Comment thread
LeonidasZhak marked this conversation as resolved.

if (mode === "replace") {
if (editingMessageForUi) {
return;
}
if (hasFileParts || hasReviews) {
if (hasDraftReplacementPayload) {
onDetachAllReviewsForComposerClear?.();
const clearsReviews = reviews === undefined || reviews.length === 0;
if (clearsReviews) {
clearedAttachedReviewIdsRef.current = attachedReviewIdsSignature;
} else {
clearedAttachedReviewIdsRef.current = null;
}
Comment thread
LeonidasZhak marked this conversation as resolved.
Comment thread
LeonidasZhak marked this conversation as resolved.

restoreDraft({
content: text,
fileParts: fileParts ?? [],
reviews: reviews ?? [],
muxMetadata,
});
} else {
setDraftMuxMetadataOverride(undefined);
restoreText(text);
}
} else if (hasFileParts || hasReviews) {
Expand All @@ -1772,6 +1824,7 @@ const ChatInputInner: React.FC<ChatInputProps> = (props) => {
content: nextText,
fileParts: fileParts ?? [],
reviews: reviews ?? [],
muxMetadata,
},
`restored-${Date.now()}`
);
Expand All @@ -1782,7 +1835,16 @@ const ChatInputInner: React.FC<ChatInputProps> = (props) => {
window.addEventListener(CUSTOM_EVENTS.UPDATE_CHAT_INPUT, handler as EventListener);
return () =>
window.removeEventListener(CUSTOM_EVENTS.UPDATE_CHAT_INPUT, handler as EventListener);
}, [appendText, restoreText, restoreDraft, applyDraftFromPending, getDraft, editingMessageForUi]);
}, [
appendText,
applyDraftFromPending,
attachedReviewIdsSignature,
editingMessageForUi,
getDraft,
onDetachAllReviewsForComposerClear,
restoreDraft,
restoreText,
]);

useEffect(() => {
const handler = (event: CustomEvent<{ workspaceId: string }>) => {
Expand All @@ -1793,6 +1855,7 @@ const ChatInputInner: React.FC<ChatInputProps> = (props) => {
setInput("");
setAttachments([]);
setDraftReviews(null);
setDraftMuxMetadataOverride(undefined);
onDetachAllReviewsForComposerClear?.();
if (inputRef.current) {
inputRef.current.style.height = "";
Expand Down Expand Up @@ -2158,6 +2221,7 @@ const ChatInputInner: React.FC<ChatInputProps> = (props) => {
editMessageId: editingMessageForUi?.id,
onCancelEdit: commandOnCancelEdit,
reviews: reviewsData,
followUpMuxMetadata: draftMuxMetadataOverride,
Comment thread
LeonidasZhak marked this conversation as resolved.
fileParts: commandFileParts.length > 0 ? commandFileParts : undefined,
onMessageSent: variant === "workspace" ? props.onMessageSent : undefined,
onDetachAllReviews: variant === "workspace" ? props.onDetachAllReviews : undefined,
Expand All @@ -2171,6 +2235,7 @@ const ChatInputInner: React.FC<ChatInputProps> = (props) => {
setInput(restoreInput);
} else {
setDraftReviews(null);
setDraftMuxMetadataOverride(undefined);
if (variant === "workspace" && parsed.type === "compact") {
if (reviewIdsForCheck.length > 0) {
props.onCheckReviews?.(reviewIdsForCheck);
Expand Down Expand Up @@ -2561,6 +2626,7 @@ const ChatInputInner: React.FC<ChatInputProps> = (props) => {
// Save current draft state for restoration on error
const preSendDraft = getDraft();
const preSendReviews = draftReviews;
const preSendMuxMetadataOverride = draftMuxMetadataOverride;
const editMessageForSend = editingMessageForUi;

try {
Expand Down Expand Up @@ -2602,6 +2668,7 @@ const ChatInputInner: React.FC<ChatInputProps> = (props) => {
text: parsed.continueMessage ?? "",
fileParts: sendFileParts,
reviews: reviewsData,
muxMetadata: draftMuxMetadataOverride,
}
: undefined,
model: parsed.model,
Expand Down Expand Up @@ -2662,6 +2729,7 @@ const ChatInputInner: React.FC<ChatInputProps> = (props) => {
// so they'll reappear naturally on failure (we only call onCheckReviews on success)
setInput("");
setDraftReviews(null);
setDraftMuxMetadataOverride(undefined);
setAttachments([]);
setHideReviewsDuringSend(true);
// Clear inline height style - VimTextArea's useLayoutEffect will handle sizing
Expand Down Expand Up @@ -2721,6 +2789,7 @@ const ChatInputInner: React.FC<ChatInputProps> = (props) => {
setOptimisticallyDismissedEditId(null);
setDraft(preSendDraft);
setDraftReviews(preSendReviews);
setDraftMuxMetadataOverride(preSendMuxMetadataOverride);
} else {
// Track telemetry for successful message send
telemetry.messageSent(
Expand Down Expand Up @@ -2768,6 +2837,7 @@ const ChatInputInner: React.FC<ChatInputProps> = (props) => {
setOptimisticallyDismissedEditId(null);
setDraft(preSendDraft);
setDraftReviews(preSendReviews);
setDraftMuxMetadataOverride(preSendMuxMetadataOverride);
} finally {
setSendingCount((c) => c - 1);
setHideReviewsDuringSend(false);
Expand Down
Loading