The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 24, 02:45 UTC
fix(web): preserve drafts when compacting context (#11103)
maria committed
Sep 11, 2026 at 01:46 UTC
ef6fa118749572da1905eca5d0543d1e98f0b1b2
2 files changed
+84
-36
apps/web/src/components/ChatView.tsx
+79
-8
@@ -5881,15 +5881,13 @@ export default function ChatView(props: ChatViewProps) {
5881
pendingApprovals.length > 0 ||
5882
pendingUserInputs.length > 0 ||
5883
showPlanFollowUpPrompt;
5884
- const compactDisabled = compactThreadUnavailable || composerHasUnsentContent;
5884
+ const compactDisabled = compactThreadUnavailable;
5885
const compactDisabledReason = compactDisabled
5886
- ? composerHasUnsentContent
5887
- ? "Send or clear your draft before compacting"
5888
- : !activeProject
5889
- ? "Choose a project before compacting"
5890
- : !manualCompactionProviderAvailable
5891
- ? "Compaction is unavailable for this provider"
5892
- : "Compacting is unavailable right now"
5886
+ ? !activeProject
5887
+ ? "Choose a project before compacting"
5888
+ : !manualCompactionProviderAvailable
5889
+ ? "Compaction is unavailable for this provider"
5890
+ : "Compacting is unavailable right now"
5891
: null;
5892
const resumeCompactionBannerItem = useMemo<ComposerBannerStackItem | null>(() => {
5893
if (
@@ -6479,6 +6477,78 @@ export default function ChatView(props: ChatViewProps) {
6477
],
6478
);
6479
6480
+ const onCompactContext = async () => {
6481
+ if (compactDisabled || !activeThread || !clientSettingsHydrated || sendInFlightRef.current) {
6482
+ return;
6483
+ }
6484
+ const context = composerRef.current?.getSendContext();
6485
+ if (!context?.providerAvailable) return;
6486
+
6487
+ // Compaction is a standalone command; the draft and its attachments stay local.
6488
+ const threadId = activeThread.id;
6489
+ const messageId = newMessageId();
6490
+ const createdAt = new Date().toISOString();
6491
+ sendInFlightRef.current = true;
6492
+ beginLocalDispatch();
6493
+ setThreadError(threadId, null);
6494
+ setOptimisticUserMessages((messages) => [
6495
+ ...messages,
6496
+ {
6497
+ id: messageId,
6498
+ role: "user",
6499
+ text: "/compact",
6500
+ turnId: null,
6501
+ createdAt,
6502
+ updatedAt: createdAt,
6503
+ streaming: false,
6504
+ },
6505
+ ]);
6506
+ scrollToEnd();
6507
+ try {
6508
+ const settingsResult = await persistThreadSettingsForNextTurn({
6509
+ threadId,
6510
+ createdAt,
6511
+ modelSelection: context.selectedModelSelection,
6512
+ ...(localCheckoutBranchMismatch
6513
+ ? { branch: localCheckoutBranchMismatch.currentBranch }
6514
+ : {}),
6515
+ runtimeMode,
6516
+ interactionMode: context.interactionMode,
6517
+ });
6518
+ const result =
6519
+ settingsResult._tag === "Failure"
6520
+ ? settingsResult
6521
+ : await startThreadTurn({
6522
+ environmentId,
6523
+ input: {
6524
+ threadId,
6525
+ message: { messageId, role: "user", text: "/compact", attachments: [] },
6526
+ modelSelection: context.selectedModelSelection,
6527
+ runtimeMode,
6528
+ interactionMode: context.interactionMode,
6529
+ createdAt,
6530
+ },
6531
+ });
6532
+ if (result._tag === "Failure") {
6533
+ setOptimisticUserMessages((messages) =>
6534
+ messages.filter((message) => message.id !== messageId),
6535
+ );
6536
+ resetLocalDispatch();
6537
+ if (!isAtomCommandInterrupted(result)) {
6538
+ const error = squashAtomCommandFailure(result);
6539
+ setThreadError(
6540
+ threadId,
6541
+ error instanceof Error ? error.message : "Failed to compact context.",
6542
+ );
6543
+ }
6544
+ } else {
6545
+ clearUsageLimitsFor(routeThreadKey);
6546
+ }
6547
+ } finally {
6548
+ sendInFlightRef.current = false;
6549
+ }
6550
+ };
6551
+
6552
const onSend = async (
6553
e?: { preventDefault: () => void },
6554
submissionIntent: ComposerSubmissionIntent = "foreground",
@@ -8575,6 +8645,7 @@ export default function ChatView(props: ChatViewProps) {
8645
onPageScrollKeyDown={onComposerPageScrollKeyDown}
8646
onPageScrollKeyUp={onComposerPageScrollKeyUp}
8647
onPageScrollRelease={onComposerPageScrollRelease}
8648
+ onCompactContext={onCompactContext}
8649
onSend={onSend}
8650
onInterrupt={onInterrupt}
8651
onImplementPlanInNewThread={onImplementPlanInNewThread}
apps/web/src/components/chat/ChatComposer.tsx
+5
-28
@@ -1354,6 +1354,7 @@ export interface ChatComposerProps {
1354
onPageScrollRelease: () => void;
1355
1356
// Callbacks
1357
+ onCompactContext: () => void;
1358
onSend: (e?: { preventDefault: () => void }, intent?: ComposerSubmissionIntent) => void;
1359
onInterrupt: () => void;
1360
onImplementPlanInNewThread: () => void;
@@ -1460,6 +1461,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
1461
onPageScrollKeyDown,
1462
onPageScrollKeyUp,
1463
onPageScrollRelease,
1464
+ onCompactContext,
1465
onSend,
1466
onInterrupt,
1467
onImplementPlanInNewThread,
@@ -1995,7 +1997,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
1997
/**
1998
* Count of pasted images still being compressed, per thread. Reserved
1999
* 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
2001
* the next draft.
2002
*/
2003
const pendingImageCompressionsRef = useRef<Map<string, number>>(new Map());
@@ -3097,42 +3099,17 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
3099
) {
3100
return;
3101
}
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();
3103
}, [
3104
activePendingApproval,
3105
activeThreadId,
3106
compactDisabled,
3127
- composerDraftTarget,
3107
isConnecting,
3108
isSendBusy,
3109
noProviderAvailable,
3110
+ onCompactContext,
3111
pendingUserInputs.length,
3112
phase,
3133
- promptRef,
3134
- setComposerDraftPrompt,
3135
- submitComposer,
3113
]);
3114
const expandMobileComposer = useCallback(() => {
3115
if (composerBlurFrameRef.current !== null) {