Skip to content

Commit fa8e587

Browse files
committed
fix(web): preserve drafts when compacting context
1 parent 3836890 commit fa8e587

2 files changed

Lines changed: 84 additions & 36 deletions

File tree

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

Lines changed: 79 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5804,15 +5804,13 @@ export default function ChatView(props: ChatViewProps) {
58045804
pendingApprovals.length > 0 ||
58055805
pendingUserInputs.length > 0 ||
58065806
showPlanFollowUpPrompt;
5807-
const compactDisabled = compactThreadUnavailable || composerHasUnsentContent;
5807+
const compactDisabled = compactThreadUnavailable;
58085808
const compactDisabledReason = compactDisabled
5809-
? composerHasUnsentContent
5810-
? "Send or clear your draft before compacting"
5811-
: !activeProject
5812-
? "Choose a project before compacting"
5813-
: !manualCompactionProviderAvailable
5814-
? "Compaction is unavailable for this provider"
5815-
: "Compacting is unavailable right now"
5809+
? !activeProject
5810+
? "Choose a project before compacting"
5811+
: !manualCompactionProviderAvailable
5812+
? "Compaction is unavailable for this provider"
5813+
: "Compacting is unavailable right now"
58165814
: null;
58175815
const resumeCompactionBannerItem = useMemo<ComposerBannerStackItem | null>(() => {
58185816
if (
@@ -6402,6 +6400,78 @@ export default function ChatView(props: ChatViewProps) {
64026400
],
64036401
);
64046402

6403+
const onCompactContext = async () => {
6404+
if (compactDisabled || !activeThread || !clientSettingsHydrated || sendInFlightRef.current) {
6405+
return;
6406+
}
6407+
const context = composerRef.current?.getSendContext();
6408+
if (!context?.providerAvailable) return;
6409+
6410+
// Compaction is a standalone command; the draft and its attachments stay local.
6411+
const threadId = activeThread.id;
6412+
const messageId = newMessageId();
6413+
const createdAt = new Date().toISOString();
6414+
sendInFlightRef.current = true;
6415+
beginLocalDispatch();
6416+
setThreadError(threadId, null);
6417+
setOptimisticUserMessages((messages) => [
6418+
...messages,
6419+
{
6420+
id: messageId,
6421+
role: "user",
6422+
text: "/compact",
6423+
turnId: null,
6424+
createdAt,
6425+
updatedAt: createdAt,
6426+
streaming: false,
6427+
},
6428+
]);
6429+
scrollToEnd();
6430+
try {
6431+
const settingsResult = await persistThreadSettingsForNextTurn({
6432+
threadId,
6433+
createdAt,
6434+
modelSelection: context.selectedModelSelection,
6435+
...(localCheckoutBranchMismatch
6436+
? { branch: localCheckoutBranchMismatch.currentBranch }
6437+
: {}),
6438+
runtimeMode,
6439+
interactionMode: context.interactionMode,
6440+
});
6441+
const result =
6442+
settingsResult._tag === "Failure"
6443+
? settingsResult
6444+
: await startThreadTurn({
6445+
environmentId,
6446+
input: {
6447+
threadId,
6448+
message: { messageId, role: "user", text: "/compact", attachments: [] },
6449+
modelSelection: context.selectedModelSelection,
6450+
runtimeMode,
6451+
interactionMode: context.interactionMode,
6452+
createdAt,
6453+
},
6454+
});
6455+
if (result._tag === "Failure") {
6456+
setOptimisticUserMessages((messages) =>
6457+
messages.filter((message) => message.id !== messageId),
6458+
);
6459+
resetLocalDispatch();
6460+
if (!isAtomCommandInterrupted(result)) {
6461+
const error = squashAtomCommandFailure(result);
6462+
setThreadError(
6463+
threadId,
6464+
error instanceof Error ? error.message : "Failed to compact context.",
6465+
);
6466+
}
6467+
} else {
6468+
clearUsageLimitsFor(routeThreadKey);
6469+
}
6470+
} finally {
6471+
sendInFlightRef.current = false;
6472+
}
6473+
};
6474+
64056475
const onSend = async (
64066476
e?: { preventDefault: () => void },
64076477
submissionIntent: ComposerSubmissionIntent = "foreground",
@@ -8461,6 +8531,7 @@ export default function ChatView(props: ChatViewProps) {
84618531
onPageScrollKeyDown={onComposerPageScrollKeyDown}
84628532
onPageScrollKeyUp={onComposerPageScrollKeyUp}
84638533
onPageScrollRelease={onComposerPageScrollRelease}
8534+
onCompactContext={onCompactContext}
84648535
onSend={onSend}
84658536
onInterrupt={onInterrupt}
84668537
onImplementPlanInNewThread={onImplementPlanInNewThread}

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

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1354,6 +1354,7 @@ export interface ChatComposerProps {
13541354
onPageScrollRelease: () => void;
13551355

13561356
// Callbacks
1357+
onCompactContext: () => void;
13571358
onSend: (e?: { preventDefault: () => void }, intent?: ComposerSubmissionIntent) => void;
13581359
onInterrupt: () => void;
13591360
onImplementPlanInNewThread: () => void;
@@ -1460,6 +1461,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
14601461
onPageScrollKeyDown,
14611462
onPageScrollKeyUp,
14621463
onPageScrollRelease,
1464+
onCompactContext,
14631465
onSend,
14641466
onInterrupt,
14651467
onImplementPlanInNewThread,
@@ -1995,7 +1997,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
19951997
/**
19961998
* Count of pasted images still being compressed, per thread. Reserved
19971999
* against the attachment limit so concurrent pastes can't overshoot it,
1998-
* and checked before sending or compacting so an image cannot move into
2000+
* and checked before sending so an image cannot move into
19992001
* the next draft.
20002002
*/
20012003
const pendingImageCompressionsRef = useRef<Map<string, number>>(new Map());
@@ -3097,42 +3099,17 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
30973099
) {
30983100
return;
30993101
}
3100-
// The compact buttons cannot see the compression counter (it lives in
3101-
// a ref), so they render enabled during a paste; toast instead of
3102-
// silently ignoring the click.
3103-
if ((pendingImageCompressionsRef.current.get(attachmentTargetKey) ?? 0) > 0) {
3104-
toastManager.add({
3105-
type: "info",
3106-
title: "Still compressing a pasted image.",
3107-
description: "Compact again once its thumbnail appears.",
3108-
});
3109-
return;
3110-
}
3111-
3112-
promptRef.current = "/compact";
3113-
setComposerDraftPrompt(composerDraftTarget, "/compact");
3114-
submitComposer();
3115-
// A blocked dispatch (busy send ref, provider preflight rejection)
3116-
// would leave the injected "/compact" behind as if the user typed it.
3117-
// Clearing here is safe even when the send did dispatch: the send
3118-
// snapshots its prompt synchronously and clears the draft itself.
3119-
if (promptRef.current === "/compact") {
3120-
promptRef.current = "";
3121-
setComposerDraftPrompt(composerDraftTarget, "");
3122-
}
3102+
onCompactContext();
31233103
}, [
31243104
activePendingApproval,
31253105
activeThreadId,
31263106
compactDisabled,
3127-
composerDraftTarget,
31283107
isConnecting,
31293108
isSendBusy,
31303109
noProviderAvailable,
3110+
onCompactContext,
31313111
pendingUserInputs.length,
31323112
phase,
3133-
promptRef,
3134-
setComposerDraftPrompt,
3135-
submitComposer,
31363113
]);
31373114
const expandMobileComposer = useCallback(() => {
31383115
if (composerBlurFrameRef.current !== null) {

0 commit comments

Comments
 (0)