Skip to content

Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.

fix(console): reuse a send's request id only for its restored draft

raullopez-sandboxb45b240c9564
diff --git a/console/apps/switch-console-desktop/src/renderer/features/chats/components/chat-composer.tsx b/console/apps/switch-console-desktop/src/renderer/features/chats/components/chat-composer.tsx
index 0ca927074..5142c221a 100644
--- a/console/apps/switch-console-desktop/src/renderer/features/chats/components/chat-composer.tsx
+++ b/console/apps/switch-console-desktop/src/renderer/features/chats/components/chat-composer.tsx
@@ -69,24 +69,32 @@ type Suggestion =
/** Chats just created here, whose composer takes focus when it first shows. */
const focusOnOpen = new Set<string>();
/** A send of a draft made before a reload, which may have landed: its id, and the draft as sent. */
-type PendingResend = { requestId: string; text: string; mentions: MentionToken[] };
+type PendingResend = {
+ requestId: string;
+ text: string;
+ mentions: MentionToken[];
+ threadRootId: string | null;
+};
function pendingOf(draft: ChatDraft | null): PendingResend | null {
return draft?.pendingRequestId
- ? { requestId: draft.pendingRequestId, text: draft.text, mentions: draft.mentions }
+ ? {
+ requestId: draft.pendingRequestId,
+ text: draft.text,
+ mentions: draft.mentions,
+ threadRootId: draft.pendingThreadRootId,
+ }
: null;
}
-/** Whether the draft is still exactly what was sent before, so it may go again under the same id. */
+/** Whether the draft is still exactly what was sent before the reload (text and mentions). */
function unchangedSince(
resend: PendingResend | null,
text: string,
- mentions: MentionToken[],
- files: number
+ mentions: MentionToken[]
): resend is PendingResend {
return (
resend !== null &&
- files === 0 &&
resend.text === text &&
JSON.stringify(resend.mentions) === JSON.stringify(mentions)
);
@@ -223,7 +231,10 @@ export const ChatComposer = observer(function ChatComposer({
);
/** The send that emptied the composer: its text stays kept until the server has taken it. */
const [sending, setSending] = useState<string | null>(null);
- /** A send of the draft on screen made before a reload, which may have landed: its id and text. */
+ /**
+ * A send of the restored draft made before a reload, which may have landed:
+ * read from storage only, and gone once that draft is sent again or edited away.
+ */
const [resend, setResend] = useState<PendingResend | null>(() =>
pendingOf(readChatDraft(draftKey))
);
@@ -250,7 +261,7 @@ export const ChatComposer = observer(function ChatComposer({
}
// The send before the reload did land: its draft is done with.
const landed =
- unchangedSince(resend, draft, picked, 0) &&
+ unchangedSince(resend, draft, picked) &&
timeline.messages.some(
(message) => requestIdOfClientTxn(message.clientTxn) === resend.requestId
);
@@ -265,7 +276,8 @@ export const ChatComposer = observer(function ChatComposer({
writeChatDraft(draftKey, {
text: draft,
mentions: picked,
- pendingRequestId: unchangedSince(resend, draft, picked, 0) ? resend.requestId : null,
+ pendingRequestId: unchangedSince(resend, draft, picked) ? resend.requestId : null,
+ pendingThreadRootId: unchangedSince(resend, draft, picked) ? resend.threadRootId : null,
});
}, [draftKey, readFrom, draft, picked, sendState, resend]);
/** A pick the next edit inserts, placed once that edit has moved the others. */
@@ -421,18 +433,30 @@ export const ChatComposer = observer(function ChatComposer({
timeline.pending.length
);
const sent = allowed(composeMentions(body, shiftMentions(draft, body, picked), targets));
+ const threadRootId = replyTo?.messageId ?? null;
const requestId = timeline.send({
body,
- threadRootId: replyTo?.messageId ?? null,
+ threadRootId,
...mentionPayload(sent, groupChats),
files: files.map(({ file }) => chatFile(file)),
- // The same message sent before a reload goes under the same id, so it lands once.
- requestId: unchangedSince(resend, draft, picked, files.length) ? resend.requestId : null,
+ // The restored draft of a send made before a reload, sent again exactly as it
+ // was, goes under the same id, so it lands once; anything else is a new message.
+ requestId:
+ unchangedSince(resend, draft, picked) &&
+ files.length === 0 &&
+ resend.threadRootId === threadRootId
+ ? resend.requestId
+ : null,
});
// Kept under its request id until the server takes it: a reload meanwhile
// finds out whether it landed rather than offering it to be sent twice.
- writeChatDraft(draftKey, { text: draft, mentions: picked, pendingRequestId: requestId });
- setResend({ requestId, text: draft, mentions: picked });
+ writeChatDraft(draftKey, {
+ text: draft,
+ mentions: picked,
+ pendingRequestId: requestId,
+ pendingThreadRootId: threadRootId,
+ });
+ setResend(null);
setSending(requestId);
void chatTitler.sent(serverId, timeline.roomId, body);
setDraft('');
diff --git a/console/apps/switch-console-desktop/src/renderer/features/chats/stores/chat-drafts.ts b/console/apps/switch-console-desktop/src/renderer/features/chats/stores/chat-drafts.ts
index d65a4e083..c92ae8618 100644
--- a/console/apps/switch-console-desktop/src/renderer/features/chats/stores/chat-drafts.ts
+++ b/console/apps/switch-console-desktop/src/renderer/features/chats/stores/chat-drafts.ts
@@ -7,7 +7,13 @@ import type { MentionToken } from '../mentions';
* dropped if it did, and sent again under that id if not, which the server
* takes once.
*/
-export type ChatDraft = { text: string; mentions: MentionToken[]; pendingRequestId: string | null };
+export type ChatDraft = {
+ text: string;
+ mentions: MentionToken[];
+ pendingRequestId: string | null;
+ /** The thread that send replied in; null for the room itself. */
+ pendingThreadRootId: string | null;
+};
const PREFIX = 'chat-draft:';
@@ -52,11 +58,15 @@ export function readChatDraft(key: string): ChatDraft | null {
return null;
}
if (typeof parsed !== 'object' || parsed === null) return null;
- const { text, mentions, pendingRequestId } = parsed as Record<string, unknown>;
+ const { text, mentions, pendingRequestId, pendingThreadRootId } = parsed as Record<
+ string,
+ unknown
+ >;
if (typeof text !== 'string' || !Array.isArray(mentions)) return null;
return {
text,
pendingRequestId: typeof pendingRequestId === 'string' ? pendingRequestId : null,
+ pendingThreadRootId: typeof pendingThreadRootId === 'string' ? pendingThreadRootId : null,
mentions: mentions.filter(
(mention): mention is MentionToken => isMention(mention) && mention.end <= text.length
),
diff --git a/console/apps/switch-console-desktop/src/renderer/tests/browser/chat-drafts.test.tsx b/console/apps/switch-console-desktop/src/renderer/tests/browser/chat-drafts.test.tsx
index 5aec42987..08286fa41 100644
--- a/console/apps/switch-console-desktop/src/renderer/tests/browser/chat-drafts.test.tsx
+++ b/console/apps/switch-console-desktop/src/renderer/tests/browser/chat-drafts.test.tsx
@@ -220,6 +220,7 @@ describe('chat drafts', () => {
text: 'in flight',
mentions: [],
pendingRequestId: 'id-1',
+ pendingThreadRootId: null,
});
});
@@ -276,4 +277,65 @@ describe('chat drafts', () => {
await act(async () => {});
expect(send).toHaveBeenCalledWith('room-1', expect.objectContaining({ requestId: 'id-1' }));
});
+ it('sends the same text twice as two messages, under two request ids', async () => {
+ const send = vi.fn(async (_room: string, _input: { requestId: string }) => []);
+ const textarea = await mount(new ChatTimeline('room-1', api(send as never)));
+ for (let i = 0; i < 2; i++) {
+ await type(textarea, 'ok');
+ await pressEnter(textarea);
+ await act(async () => {});
+ }
+ expect(send.mock.calls.map(([, input]) => (input as { requestId: string }).requestId)).toEqual([
+ 'id-1',
+ 'id-2',
+ ]);
+ });
+
+ it('leaves the same text alone when it is typed again after a send landed', async () => {
+ const send = vi.fn(async (_room: string, input: { requestId: string }) => [
+ {
+ messageId: 'm1',
+ seq: 1,
+ roomId: 'room-1',
+ sentAt: '2026-01-01T00:00:00Z',
+ sender: { clientId: 'c', name: 'Ada', kind: 'human', agentId: null, userId: 'user-a' },
+ source: 'console',
+ body: 'ok',
+ format: null,
+ threadRootId: null,
+ attachments: [],
+ clientTxn: `${input.requestId}:0`,
+ namedAgentIds: [],
+ namedUserIds: [],
+ mentions: [],
+ threadReplyCount: 0,
+ threadLastReplySeq: null,
+ },
+ ]);
+ const textarea = await mount(new ChatTimeline('room-1', api(send as never)));
+ await type(textarea, 'ok');
+ await pressEnter(textarea);
+ await act(async () => {});
+ await type(textarea, 'ok');
+ await act(async () => {});
+ expect(textarea.value).toBe('ok');
+ });
+
+ it('sends a restored draft from another thread under a new request id', async () => {
+ localStorage.setItem(
+ key('user-a', 'ws-1', 'room-1'),
+ JSON.stringify({
+ text: 'in flight',
+ mentions: [],
+ pendingRequestId: 'req-1',
+ pendingThreadRootId: 'root-1',
+ })
+ );
+ const send = vi.fn(async () => []);
+ const textarea = await mount(new ChatTimeline('room-1', api(send)));
+ expect(textarea.value).toBe('in flight');
+ await pressEnter(textarea);
+ await act(async () => {});
+ expect(send).toHaveBeenCalledWith('room-1', expect.objectContaining({ requestId: 'id-1' }));
+ });
});