Skip to content

Commit e1facad

Browse files
feat: save thread messages as drafts, with send button menus everywhere (#353)
Drafts are stored by the server per thread and streamed to every client, so a draft saved on desktop shows on mobile and over MCP. Every send button gets a right-click (web/desktop) or long-press (mobile) menu built from one shared resolver, offering its existing send actions plus Save as draft and the thread's drafts. Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 583c421 commit e1facad

78 files changed

Lines changed: 4171 additions & 227 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎apps/mobile/src/features/home/HomeScreen.tsx‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ import { usePendingThreadOrder } from "../../state/thread-order";
5555
import { threadListEnvironmentsAtom } from "../../state/server";
5656
import type { PendingNewTask } from "../../state/use-pending-new-tasks";
5757
import { useQueuedThreadKeys } from "../../state/use-thread-outbox";
58+
import { useMessageDraftCounts } from "../../state/messageDrafts";
5859
import {
5960
ThreadListV2GroupSectionHeader,
6061
ThreadListV2PendingRow,
@@ -298,6 +299,7 @@ export function HomeScreen(props: HomeScreenProps) {
298299
const selectedThreadKey = primaryColumn?.selectedThreadKey ?? null;
299300
const fullSwipeWidth = primaryColumn && columnMetrics ? columnMetrics.width - 20 : undefined;
300301
const queuedThreadKeys = useQueuedThreadKeys();
302+
const draftCounts = useMessageDraftCounts();
301303
const openSwipeableRef = useRef<SwipeableMethods | null>(null);
302304
const homeScrollGesture = useNativeGesture();
303305
const insets = useSafeAreaInsets();
@@ -641,6 +643,7 @@ export function HomeScreen(props: HomeScreenProps) {
641643
snoozeLabelNow: `${nowMinute}:00.000Z`,
642644
snoozeEnvironmentIds,
643645
queuedThreadKeys,
646+
draftCounts,
644647
moveAvailability: threadMoveAvailability,
645648
shelfPreferencesLoading: !shelfPreferencesLoaded,
646649
}))();
@@ -714,6 +717,7 @@ export function HomeScreen(props: HomeScreenProps) {
714717
thread={thread}
715718
variant={item.item.variant}
716719
hasQueuedMessages={item.hasQueuedMessages}
720+
draftCount={item.draftCount}
717721
snoozed={item.item.snoozed}
718722
pinned={item.item.pinned}
719723
hidden={item.item.hidden}

‎apps/mobile/src/features/threads/ThreadComposer.tsx‎

Lines changed: 78 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,13 @@ import { MOBILE_TYPOGRAPHY } from "../../lib/typography";
105105
import { resolveProviderOptionDescriptors } from "../../lib/providerOptions";
106106
import { Menu } from "../../components/ui/Menu";
107107
import type { ActiveTurnComposerAction } from "@t3tools/client-runtime/state/composer-dispatch";
108+
import {
109+
findComposerSendMenuAction,
110+
resolveComposerSendMenu,
111+
type ComposerSendMenuAction,
112+
type ComposerSendMenuItem,
113+
} from "@t3tools/client-runtime/state/send-menu";
114+
import { composerSendMenuActions } from "./composerSendMenu";
108115
import type { FollowUpBehavior } from "../../lib/followUpBehavior";
109116
import {
110117
resolveComposerSendPresentation,
@@ -178,6 +185,15 @@ export interface ThreadComposerProps {
178185
readonly existingAttachments: ReadonlyArray<ChatAttachment>;
179186
readonly saving: boolean;
180187
readonly onRemoveExistingAttachment: (attachmentId: string) => void;
188+
readonly onCancel: () => void;
189+
} | null;
190+
/** Absent where the server has no drafts or this connection cannot save them. */
191+
readonly messageDrafts?: {
192+
readonly drafts: ReadonlyArray<{ readonly id: string; readonly text: string }>;
193+
readonly busy: boolean;
194+
readonly onSaveDraft: () => void;
195+
readonly onMoveQueuedEditToDrafts: () => void;
196+
readonly onOpenDraft: (draftId: string) => void;
181197
} | null;
182198
readonly followUpBehavior: FollowUpBehavior;
183199
readonly canSteerActiveTurn: boolean;
@@ -249,47 +265,36 @@ const FOLLOW_UP_ACTION_LABEL = {
249265
restart: "Restart turn",
250266
} as const;
251267

252-
const FOLLOW_UP_ACTION_SUBTITLE = {
253-
queue: "Run after the current turn",
254-
steer: "Interrupt what the agent is doing",
255-
restart: "Start the turn over with this message",
256-
} as const;
257-
258268
function SendActionButton(props: {
259269
readonly accessibilityLabel: string;
260270
readonly presentation: ComposerSendPresentation;
261271
readonly disabled: boolean;
262-
readonly onSend: (followUp?: ActiveTurnComposerAction) => void;
272+
readonly menuItems: ReadonlyArray<ComposerSendMenuItem>;
273+
readonly onSend: () => void;
274+
readonly onMenuAction: (action: ComposerSendMenuAction) => void;
263275
}) {
264-
const { presentation } = props;
276+
const { presentation, menuItems } = props;
265277
const button = (
266278
<ComposerActionButton
267279
accessibilityLabel={props.accessibilityLabel}
268280
icon={presentation.icon}
269281
variant="primary"
270282
disabled={props.disabled}
271-
onPress={() => props.onSend()}
283+
onPress={props.onSend}
272284
/>
273285
);
274-
if (!presentation.offersFollowUpChoice || presentation.action === null || props.disabled) {
275-
return button;
276-
}
277-
const actions = [presentation.action, presentation.alternate].filter(
278-
(action): action is ActiveTurnComposerAction => action !== null,
279-
);
286+
if (menuItems.length === 0) return button;
287+
const menu = composerSendMenuActions(menuItems, Platform.OS === "ios" ? "ios" : "android");
280288
return (
281289
<Menu
282290
accessibilityLabel="Choose how to send this message"
283291
shouldOpenOnLongPress
284-
actions={actions.map((action) => ({
285-
id: action,
286-
title: FOLLOW_UP_ACTION_LABEL[action],
287-
subtitle: FOLLOW_UP_ACTION_SUBTITLE[action],
288-
state: action === presentation.action ? ("on" as const) : ("off" as const),
289-
}))}
290-
onPressAction={({ nativeEvent }) =>
291-
props.onSend(nativeEvent.event as ActiveTurnComposerAction)
292-
}
292+
{...(menu.title ? { title: menu.title } : {})}
293+
actions={menu.actions}
294+
onPressAction={({ nativeEvent }) => {
295+
const action = findComposerSendMenuAction(menuItems, nativeEvent.event);
296+
if (action) props.onMenuAction(action);
297+
}}
293298
>
294299
{button}
295300
</Menu>
@@ -621,6 +626,49 @@ export const ThreadComposer = function ThreadComposer(props: ThreadComposerProps
621626
}
622627
onEditorFocusChange?.(false);
623628
};
629+
const messageDrafts = props.messageDrafts ?? null;
630+
const sendMenuItems =
631+
hasContent && (!canSend || messageDrafts?.busy === true)
632+
? []
633+
: resolveComposerSendMenu({
634+
hasContent,
635+
running: props.activeThreadBusy,
636+
canSteer: props.canSteerActiveTurn,
637+
activeTurnDefault: props.followUpBehavior,
638+
// Mobile has no way to start a sibling thread from the thread composer.
639+
canSendInNewThread: false,
640+
canSaveDraft: messageDrafts !== null && !messageDrafts.busy,
641+
isEditingQueuedMessage: queuedEdit !== null,
642+
drafts: messageDrafts?.busy === false ? messageDrafts.drafts : [],
643+
});
644+
const handleSendMenuAction = (action: ComposerSendMenuAction) => {
645+
switch (action.type) {
646+
case "send":
647+
case "update-queued":
648+
void handleSend();
649+
return;
650+
case "queue":
651+
void handleSend(props.activeThreadBusy && props.canSteerActiveTurn ? "queue" : undefined);
652+
return;
653+
case "steer":
654+
void handleSend("steer");
655+
return;
656+
case "save-draft":
657+
messageDrafts?.onSaveDraft();
658+
return;
659+
case "move-to-drafts":
660+
messageDrafts?.onMoveQueuedEditToDrafts();
661+
return;
662+
case "cancel-edit":
663+
queuedEdit?.onCancel();
664+
return;
665+
case "open-draft":
666+
messageDrafts?.onOpenDraft(action.draftId);
667+
return;
668+
case "send-in-new-thread":
669+
return;
670+
}
671+
};
624672
const handleSend = async (followUp?: ActiveTurnComposerAction) => {
625673
if (voiceInput.blocksSubmission || pendingPastedTextAttachmentCountRef.current > 0) return;
626674
if (usageLimitsOffered && isUsageLimitsCommand(draftMessage) && draftAttachments.length === 0) {
@@ -1070,7 +1118,9 @@ export const ThreadComposer = function ThreadComposer(props: ThreadComposerProps
10701118
accessibilityLabel={sendBlockedReason ?? sendLabel}
10711119
presentation={sendPresentation}
10721120
disabled={!canSend}
1073-
onSend={handleSend}
1121+
menuItems={sendMenuItems}
1122+
onSend={() => void handleSend()}
1123+
onMenuAction={handleSendMenuAction}
10741124
/>
10751125
)}
10761126
</View>
@@ -1179,7 +1229,9 @@ export const ThreadComposer = function ThreadComposer(props: ThreadComposerProps
11791229
accessibilityLabel={sendBlockedReason ?? sendLabel}
11801230
presentation={sendPresentation}
11811231
disabled={!canSend}
1182-
onSend={handleSend}
1232+
menuItems={sendMenuItems}
1233+
onSend={() => void handleSend()}
1234+
onMenuAction={handleSendMenuAction}
11831235
/>
11841236
) : null}
11851237
</View>

‎apps/mobile/src/features/threads/ThreadDetailScreen.tsx‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import type { LegendListRef } from "@legendapp/list/react-native";
2525
import { HeaderHeightContext } from "@react-navigation/elements";
2626
import { NativeLayoutObserver } from "../../native/NativeLayoutObserver";
2727
import { deriveBottomControlInsets, type NativeLayoutMetrics } from "../../lib/reserved-regions";
28+
import { MessageDraftId } from "@t3tools/contracts";
2829
import type {
2930
EnvironmentId,
3031
MessageId,
@@ -153,6 +154,9 @@ import { ComposerPopoverHost } from "./ComposerPopoverHost";
153154
import { ThreadFeed, type ThreadFeedHistoryControls } from "./ThreadFeed";
154155
import { useThreadTurnSubagents } from "./ThreadAgentsSheet";
155156
import { ComposerQueuedEditBanner } from "./ComposerQueuedEdit";
157+
import { ThreadDraftsTray } from "./ThreadDraftsTray";
158+
import { defaultMessageDraftSendMode } from "./messageDraftPresentation";
159+
import { useThreadMessageDrafts } from "./useThreadMessageDrafts";
156160
import { useThreadQueuedCount } from "./ThreadQueueControl";
157161
import type { ThreadContentPresentation } from "./threadContentPresentation";
158162
import { resolveThreadFeedSubmissionAnchor } from "./thread-feed-live-follow";
@@ -479,6 +483,13 @@ export const ThreadDetailScreen = function ThreadDetailScreen(props: ThreadDetai
479483
const composerEditorRef = useRef<ComposerEditorHandle>(null);
480484
const isProviderSubagent = isProviderNativeSubagentThread(props.selectedThread.source);
481485
const editingRunId = props.queuedRunEdit?.runId ?? null;
486+
const messageDrafts = useThreadMessageDrafts({
487+
environmentId: props.environmentId,
488+
threadId: props.selectedThread.id,
489+
serverConfig: props.serverConfig,
490+
onOpened: () => composerEditorRef.current?.focus(),
491+
});
492+
const draftsEnabled = messageDrafts.canEdit && props.canOperateThread && !isProviderSubagent;
482493
useEffect(() => {
483494
if (editingRunId === null) return;
484495
const frame = requestAnimationFrame(() => composerEditorRef.current?.focus());
@@ -1268,6 +1279,21 @@ export const ThreadDetailScreen = function ThreadDetailScreen(props: ThreadDetai
12681279
/>
12691280
</Animated.View>
12701281
) : null}
1282+
{draftsEnabled && props.queuedRunEdit === null ? (
1283+
<ThreadDraftsTray
1284+
drafts={messageDrafts.drafts}
1285+
disabled={messageDrafts.busy}
1286+
sendMode={defaultMessageDraftSendMode({
1287+
running: props.activeThreadBusy,
1288+
canSteer: props.canSteerActiveTurn,
1289+
followUpBehavior: props.followUpBehavior,
1290+
})}
1291+
canSteer={props.activeThreadBusy && props.canSteerActiveTurn}
1292+
onOpen={(draft) => void messageDrafts.open(draft.id)}
1293+
onSend={(draft, mode) => void messageDrafts.send(draft.id, mode)}
1294+
onDelete={(draft) => void messageDrafts.remove(draft.id)}
1295+
/>
1296+
) : null}
12711297
<UsageLimitRecoveryCard
12721298
key={props.selectedThread.latestRun?.runId}
12731299
thread={props.selectedThread}
@@ -1466,7 +1492,23 @@ export const ThreadDetailScreen = function ThreadDetailScreen(props: ThreadDetai
14661492
existingAttachments: props.queuedRunEdit.existingAttachments,
14671493
saving: props.isSavingQueuedEdit,
14681494
onRemoveExistingAttachment: props.onRemoveQueuedEditAttachment,
1495+
onCancel: () => {
1496+
voiceInputSession.cancel(props.composerDraftKey);
1497+
props.onCancelQueuedRunEdit();
1498+
},
1499+
}
1500+
}
1501+
messageDrafts={
1502+
draftsEnabled
1503+
? {
1504+
drafts: messageDrafts.drafts,
1505+
busy: messageDrafts.busy,
1506+
onSaveDraft: () => void messageDrafts.saveComposer(),
1507+
onMoveQueuedEditToDrafts: () => void messageDrafts.moveQueuedEdit(),
1508+
onOpenDraft: (draftId) =>
1509+
void messageDrafts.open(MessageDraftId.make(draftId)),
14691510
}
1511+
: null
14701512
}
14711513
bottomInset={composerBottomInset}
14721514
onChangeDraftMessage={props.onChangeDraftMessage}

0 commit comments

Comments
 (0)