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): a reload reopens the chat that was open, unsent New chat or not

raullopez-sandbox86b8eb055f93
diff --git a/console/apps/switch-console-desktop/src/renderer/features/chats/start-direct-chat.ts b/console/apps/switch-console-desktop/src/renderer/features/chats/start-direct-chat.ts
index ee95d77c9..c530aef90 100644
--- a/console/apps/switch-console-desktop/src/renderer/features/chats/start-direct-chat.ts
+++ b/console/apps/switch-console-desktop/src/renderer/features/chats/start-direct-chat.ts
@@ -43,6 +43,8 @@ export const directChats = new DirectChatStarter({
if (appState.navigation.viewParamsStore[viewId]?.roomId === from)
appState.navigation.updateViewParams(viewId, { roomId: chat.roomId });
});
+ // Saved now, not on the next debounce: a reload before then would reopen the unsaved id, which is gone.
+ void rpc.viewState.save('navigation', appState.navigation.snapshot);
chatTitler.created(chat, false);
const first = chatsStore.timelines.peek(serverId, chat.roomId)?.pending[0];
if (first) void chatTitler.sent(serverId, chat.roomId, first.body);
diff --git a/console/apps/switch-console-desktop/src/renderer/features/managed-agents/agent-chats.tsx b/console/apps/switch-console-desktop/src/renderer/features/managed-agents/agent-chats.tsx
index 9632ffaef..4ec456a07 100644
--- a/console/apps/switch-console-desktop/src/renderer/features/managed-agents/agent-chats.tsx
+++ b/console/apps/switch-console-desktop/src/renderer/features/managed-agents/agent-chats.tsx
@@ -133,6 +133,14 @@ function conversationsOf({ serverId, workspaceId, agentId }: ManagedAgentView) {
};
}
+/** Whether `roomId` is the agent's unsaved chat, open now or kept from before a reload. */
+function isUnsavedChatOf(agent: ManagedAgentView, roomId: string): boolean {
+ return (
+ directChats.unsavedChat(roomId) !== undefined ||
+ directChats.waiting(agent.serverId, agent.agentId) === roomId
+ );
+}
+
/**
* The direct chat open on an agent's page: the one in the params, archived or
* not, else its newest; none while the open chat is unsaved, so nothing of
@@ -142,7 +150,7 @@ export function openAgentChat(
agent: ManagedAgentView,
roomId: string | undefined
): ChatSummary | null {
- if (roomId && directChats.unsavedChat(roomId)) return null;
+ if (roomId && isUnsavedChatOf(agent, roomId)) return null;
const { direct, archived } = conversationsOf(agent);
return [...direct, ...archived].find((chat) => chat.roomId === roomId) ?? direct[0] ?? null;
}
@@ -175,12 +183,21 @@ export const AgentChats = observer(function AgentChats({ agent }: { agent: Manag
const { sections, section } = useAgentSection(serverId, agent, params.section);
const thisComputer = useRunsOnThisComputer(agent);
const shownRoom = openAgentRoom(agent, params.room, section);
- const roomId = shownRoom
- ? null
- : params.roomId && directChats.unsavedChat(params.roomId)
- ? params.roomId
- : (openAgentChat(agent, params.roomId)?.roomId ?? null);
+ // The unsaved chat open before a reload is kept, but not in memory yet: it opens again.
+ const restoring =
+ !!params.roomId &&
+ !directChats.unsavedChat(params.roomId) &&
+ isUnsavedChatOf(agent, params.roomId);
+ const roomId =
+ shownRoom || restoring
+ ? null
+ : params.roomId && directChats.unsavedChat(params.roomId)
+ ? params.roomId
+ : (openAgentChat(agent, params.roomId)?.roomId ?? null);
const open = (id: string) => setParams({ roomId: id, section: undefined, room: undefined });
+ useEffect(() => {
+ if (restoring) setParams({ roomId: directChats.start(serverId, agentId) });
+ }, [restoring, serverId, agentId, setParams]);
// The chat open here that becomes a group follows into its room, rather than another chat taking its place.
useOpenGroupAsRoom(shownRoom || section ? null : (params.roomId ?? null), 'managedAgent');
const newChat = () => open(directChats.start(serverId, agentId));
@@ -314,7 +331,7 @@ export const AgentChats = observer(function AgentChats({ agent }: { agent: Manag
className="pointer-events-none absolute inset-x-0 top-0 z-10 h-6 bg-linear-to-b from-background to-transparent"
/>
</>
- ) : listing || autoStart ? null : (
+ ) : listing || autoStart || restoring ? null : (
<EmptyState
label="No chat open"
description="Start a chat with this agent."
diff --git a/console/apps/switch-console-desktop/src/renderer/tests/browser/agent-chats-sections.test.tsx b/console/apps/switch-console-desktop/src/renderer/tests/browser/agent-chats-sections.test.tsx
index 19153c695..192227741 100644
--- a/console/apps/switch-console-desktop/src/renderer/tests/browser/agent-chats-sections.test.tsx
+++ b/console/apps/switch-console-desktop/src/renderer/tests/browser/agent-chats-sections.test.tsx
@@ -454,6 +454,122 @@ it('lists an unsent New chat kept from before a restart, with its draft, and reo
}
});
+function directChat(agentId: string, roomId: string, name: string, seq: number | null) {
+ return {
+ roomId,
+ name,
+ channelType: 'direct',
+ bridgeType: null,
+ channelName: null,
+ agents: [
+ {
+ id: agentId,
+ name: 'pm-agent',
+ displayName: null,
+ iconUrl: null,
+ provider: null,
+ alias: null,
+ ownerName: null,
+ ownedByViewer: true,
+ },
+ ],
+ canManage: true,
+ ownsAgent: true,
+ memberUserIds: [],
+ memberCount: 1,
+ unreadCount: 0,
+ lastMessage:
+ seq === null
+ ? null
+ : { seq, sentAt: `2026-01-0${seq}T00:00:00Z`, preview: '', senderName: '' },
+ } satisfies ChatSummary;
+}
+
+/** The rows highlighted as open in the Direct chats list, by their text: a kept draft's ends in "Draft". */
+const activeRows = () =>
+ [...container.querySelectorAll('[data-active="true"]')].map((row) => row.textContent);
+
+it('reopens the created chat whose first send failed, open before a reload, beside a kept draft', async () => {
+ const status = vi
+ .spyOn(switchServersStore, 'statusFor')
+ .mockReturnValue({ user: { id: 'user-a' } } as ReturnType<typeof switchServersStore.statusFor>);
+ const workspace = vi.spyOn(workspacesStore, 'idOnServerInScope').mockReturnValue('ws-1');
+ const scope = { serverId: 'server-1', userId: 'user-a', workspaceId: 'ws-1' };
+ const agent = { ...AGENT, agentId: 'agent-5' };
+ try {
+ // Another New chat with this agent, unsent, kept with its draft.
+ localStorage.setItem(unsavedChatKey({ ...scope, agentId: 'agent-5' })!, 'other');
+ localStorage.setItem(
+ chatDraftKey({ ...scope, roomId: 'unsaved-chat:other' }),
+ JSON.stringify({ text: 'another thought', mentions: [] })
+ );
+ runInAction(() => {
+ chatsStore.serverId = 'server-1';
+ });
+ chatsStore.upsert(directChat('agent-5', 'made', 'New Chat', null));
+ chatsStore.upsert(directChat('agent-5', 'newest', 'Newest', 5));
+ navigation.reset({
+ serverId: 'server-1',
+ agentId: 'agent-5',
+ name: 'pm-agent',
+ roomId: 'made',
+ });
+ await act(async () => root.render(<AgentChats agent={agent} />));
+ expect(navigation.params().roomId).toBe('made');
+ expect(openAgentChat(agent, 'made')?.roomId).toBe('made');
+ expect(activeRows()).toEqual(['New Chat']);
+ expect(container.textContent).toContain('New ChatDraft');
+ expect(directChats.unsavedChat('unsaved-chat:other')).toBeUndefined();
+ } finally {
+ status.mockRestore();
+ workspace.mockRestore();
+ localStorage.clear();
+ runInAction(() => {
+ chatsStore.chats.delete('made');
+ chatsStore.chats.delete('newest');
+ chatsStore.serverId = null;
+ });
+ }
+});
+
+it('reopens the unsent New chat that was open before a reload, not the newest chat', async () => {
+ const status = vi
+ .spyOn(switchServersStore, 'statusFor')
+ .mockReturnValue({ user: { id: 'user-a' } } as ReturnType<typeof switchServersStore.statusFor>);
+ const workspace = vi.spyOn(workspacesStore, 'idOnServerInScope').mockReturnValue('ws-1');
+ const scope = { serverId: 'server-1', userId: 'user-a', workspaceId: 'ws-1' };
+ // An agent no other test opened a chat with: the starter is the app's own, shared by every test.
+ const agent = { ...AGENT, agentId: 'agent-4' };
+ const roomId = 'unsaved-chat:open-before-reload';
+ try {
+ localStorage.setItem(unsavedChatKey({ ...scope, agentId: 'agent-4' })!, 'open-before-reload');
+ localStorage.setItem(
+ chatDraftKey({ ...scope, roomId }),
+ JSON.stringify({ text: 'half a thought', mentions: [] })
+ );
+ // What navigation restored: the unsaved chat was the one open.
+ navigation.reset({ serverId: 'server-1', agentId: 'agent-4', name: 'pm-agent', roomId });
+ runInAction(() => {
+ chatsStore.serverId = 'server-1';
+ });
+ chatsStore.upsert(directChat('agent-4', 'newest', 'Newest', 5));
+ await act(async () => root.render(<AgentChats agent={agent} />));
+ expect(directChats.unsavedChat(roomId)).toBeDefined();
+ expect(navigation.params().roomId).toBe(roomId);
+ expect(openAgentChat(agent, roomId)).toBeNull();
+ // Only the open chat is highlighted.
+ expect(activeRows()).toEqual(['New ChatDraft']);
+ } finally {
+ runInAction(() => {
+ chatsStore.chats.delete('newest');
+ chatsStore.serverId = null;
+ });
+ status.mockRestore();
+ workspace.mockRestore();
+ localStorage.clear();
+ }
+});
+
it('offers a new room or an existing one from the Rooms section, with the agent picked', async () => {
const { showModal } = await import('@renderer/lib/modal/modal-provider');
vi.mocked(showModal).mockClear();