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): an agent's page follows its open chat into the room it becomes

raullopez-sandbox2dbdb211bf41
diff --git a/console/apps/switch-console-desktop/src/renderer/features/chats/chat-view.tsx b/console/apps/switch-console-desktop/src/renderer/features/chats/chat-view.tsx
index e29b1efcb..4452673eb 100644
--- a/console/apps/switch-console-desktop/src/renderer/features/chats/chat-view.tsx
+++ b/console/apps/switch-console-desktop/src/renderer/features/chats/chat-view.tsx
@@ -3,12 +3,12 @@ import { useEffect } from 'react';
import type { GuardResult, ViewDefinition } from '@renderer/app/view-registry';
import { managedAgentLabel } from '@renderer/features/managed-agents/managed-agent-state';
import { useManagedAgents } from '@renderer/features/managed-agents/use-managed-agents';
-import { isConsoleGroupChatSummary } from '@renderer/features/switch-servers/room-filters';
import { Titlebar } from '@renderer/lib/components/titlebar/Titlebar';
import { useParams } from '@renderer/lib/layout/navigation-provider';
import { appState } from '@renderer/lib/stores/app-state';
import { ChatHeaderControls, ChatHeaderTitle } from './components/chat-header';
import { ChatPanel, useAgentActivities } from './components/chat-panel';
+import { useOpenGroupAsRoom } from './console-chat-redirect';
import { agentChatsFlag } from './stores/agent-chats-flag';
import { chatsStore } from './stores/chats';
@@ -67,29 +67,10 @@ function useLeaveWhenChatsOff(params: ChatParams): boolean | undefined {
return enabled;
}
-/**
- * A group chat opens as a room, including a direct chat the moment it becomes
- * one; its draft and loaded messages are held by chat, so they come along.
- */
-function useOpenGroupAsRoom(roomId: string): void {
- const chat = chatsStore.chat(roomId);
- const group = chat !== undefined && isConsoleGroupChatSummary(chat);
- useEffect(() => {
- if (!group) return;
- appState.history.prune(
- (entry) =>
- entry.kind === 'view' &&
- entry.viewId === 'chat' &&
- (entry.params as { roomId?: string }).roomId === roomId
- );
- appState.navigation.navigate('room', { roomId });
- }, [group, roomId]);
-}
-
const ChatMainPanel = observer(function ChatMainPanel(): React.JSX.Element | null {
const { params } = useParams('chat');
const enabled = useLeaveWhenChatsOff(params);
- useOpenGroupAsRoom(params.roomId);
+ useOpenGroupAsRoom(params.roomId, 'chat');
if (enabled !== true) return null;
return (
<ChatPanel
diff --git a/console/apps/switch-console-desktop/src/renderer/features/chats/console-chat-redirect.test.ts b/console/apps/switch-console-desktop/src/renderer/features/chats/console-chat-redirect.test.ts
new file mode 100644
index 000000000..ea2ce8c62
--- /dev/null
+++ b/console/apps/switch-console-desktop/src/renderer/features/chats/console-chat-redirect.test.ts
@@ -0,0 +1,35 @@
+import { describe, expect, it, vi } from 'vitest';
+import type { ChatSummary } from '@shared/core/chats/chats';
+import { groupToOpenAsRoom } from './console-chat-redirect';
+
+const chats = vi.hoisted(() => new Map<string, Partial<ChatSummary>>());
+vi.mock('@renderer/lib/ipc', () => ({ events: { on: vi.fn() }, rpc: {} }));
+vi.mock('./stores/chats', () => ({ chatsStore: { chat: (roomId: string) => chats.get(roomId) } }));
+
+const agent = { id: 'a1' } as ChatSummary['agents'][number];
+
+describe('groupToOpenAsRoom', () => {
+ it('follows the open chat into its room the moment it becomes a group', () => {
+ chats.set('r1', { roomId: 'r1', channelType: 'direct', bridgeType: null, agents: [agent] });
+ expect(groupToOpenAsRoom('r1')).toBeNull();
+ // Someone added a person: the summary turns the chat into a group.
+ chats.set('r1', {
+ roomId: 'r1',
+ channelType: 'channel_private',
+ bridgeType: null,
+ agents: [agent],
+ });
+ expect(groupToOpenAsRoom('r1')).toBe('r1');
+ });
+
+ it('leaves nothing to follow with no chat open or a bridged room', () => {
+ expect(groupToOpenAsRoom(null)).toBeNull();
+ chats.set('r2', {
+ roomId: 'r2',
+ channelType: 'channel_private',
+ bridgeType: 'slack',
+ agents: [agent],
+ });
+ expect(groupToOpenAsRoom('r2')).toBeNull();
+ });
+});
diff --git a/console/apps/switch-console-desktop/src/renderer/features/chats/console-chat-redirect.tsx b/console/apps/switch-console-desktop/src/renderer/features/chats/console-chat-redirect.tsx
index 06652a5b0..78e083bdb 100644
--- a/console/apps/switch-console-desktop/src/renderer/features/chats/console-chat-redirect.tsx
+++ b/console/apps/switch-console-desktop/src/renderer/features/chats/console-chat-redirect.tsx
@@ -21,6 +21,33 @@ export function opensAsConsoleChat(roomId: string): boolean {
return chat ? isConsoleGroupChatSummary(chat) : chatsStore.removed.has(roomId);
}
+/** The chat to open as a room: the one shown, once it is a group. */
+export function groupToOpenAsRoom(roomId: string | null): string | null {
+ if (roomId === null) return null;
+ const chat = chatsStore.chat(roomId);
+ return chat !== undefined && isConsoleGroupChatSummary(chat) ? roomId : null;
+}
+
+/**
+ * A group chat opens as a room, including a direct chat the moment it becomes
+ * one; its draft and loaded messages are held by chat, so they come along.
+ * `from` is the view showing it, whose history entry for the chat is dropped
+ * so Back does not return there and open the room again.
+ */
+export function useOpenGroupAsRoom(roomId: string | null, from: 'chat' | 'managedAgent'): void {
+ const group = groupToOpenAsRoom(roomId);
+ useEffect(() => {
+ if (group === null) return;
+ appState.history.prune(
+ (entry) =>
+ entry.kind === 'view' &&
+ entry.viewId === from &&
+ (entry.params as { roomId?: string }).roomId === group
+ );
+ appState.navigation.navigate('room', { roomId: group });
+ }, [group, from]);
+}
+
/**
* On the room page, with agent_chats on: a 1-1 Console chat is not a room, so
* it opens in the chat UI instead, whether reached from a deep link, search, a
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 a52dd266c..996f43771 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
@@ -4,6 +4,7 @@ import { type ReactNode, useEffect, useRef, useState } from 'react';
import { agentConversations } from '@renderer/features/chats/agent-conversations';
import { ChatPanel } from '@renderer/features/chats/components/chat-panel';
import { ChatRow } from '@renderer/features/chats/components/chat-row';
+import { useOpenGroupAsRoom } from '@renderer/features/chats/console-chat-redirect';
import { directChats } from '@renderer/features/chats/start-direct-chat';
import { chatsStore } from '@renderer/features/chats/stores/chats';
import { RoomListRow } from '@renderer/features/sidebar/main-section';
@@ -133,6 +134,8 @@ export const AgentChats = observer(function AgentChats({ agent }: { agent: Manag
const shownRoom = openAgentRoom(agent, params.room, section);
const roomId = shownRoom ? null : (openAgentChat(agent, params.roomId)?.roomId ?? null);
const open = (id: string) => setParams({ roomId: id, section: undefined, room: undefined });
+ // 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 = async () => {
const chat = await directChats.start(serverId, agentId);
if (chat) open(chat.roomId);
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 9c1488959..0456cb4b4 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
@@ -98,8 +98,12 @@ vi.mock('@renderer/features/chats/components/chat-panel', async (importOriginal)
}));
import '@renderer/index.css';
+import { runInAction } from 'mobx';
+import { chatsStore } from '@renderer/features/chats/stores/chats';
import { AgentChats } from '@renderer/features/managed-agents/agent-chats';
import { roomEmbedSlotStore } from '@renderer/features/switch-rooms/room-embed-slot';
+import { appState } from '@renderer/lib/stores/app-state';
+import type { ChatSummary } from '@shared/core/chats/chats';
const AGENT: ManagedAgentView = {
serverId: 'server-1',
@@ -310,3 +314,59 @@ it("opens a room whose platform can't be drawn here on its settings", async () =
await act(async () => roomRow('agents-only').click());
expect(embeddedRoom()?.textContent).toBe('Settings of room-2');
});
+
+it('follows the open direct chat into its room when it becomes a group, not an older chat', async () => {
+ const navigate = vi.spyOn(appState.navigation, 'navigate').mockImplementation(() => {});
+ const agentInChat = {
+ id: 'agent-1',
+ name: 'pm-agent',
+ displayName: null,
+ iconUrl: null,
+ provider: null,
+ alias: null,
+ ownerName: null,
+ ownedByViewer: true,
+ };
+ const chat = (roomId: string, channelType: ChatSummary['channelType'], seq: number) =>
+ ({
+ roomId,
+ name: roomId,
+ channelType,
+ bridgeType: null,
+ channelName: null,
+ agents: [agentInChat],
+ canManage: true,
+ ownsAgent: true,
+ memberUserIds: [],
+ memberCount: 1,
+ unreadCount: 0,
+ lastMessage: { seq, sentAt: `2026-01-0${seq}T00:00:00Z`, preview: '', senderName: '' },
+ }) satisfies ChatSummary;
+ try {
+ runInAction(() => {
+ chatsStore.serverId = 'server-1';
+ });
+ chatsStore.upsert(chat('older', 'direct', 1));
+ chatsStore.upsert(chat('current', 'direct', 2));
+ navigation.reset({
+ serverId: 'server-1',
+ agentId: 'agent-1',
+ name: 'pm-agent',
+ roomId: 'current',
+ });
+ await act(async () => root.render(<AgentChats agent={AGENT} />));
+ expect(navigate).not.toHaveBeenCalled();
+
+ // Someone adds a person: the open chat is now a group.
+ await act(async () => chatsStore.upsert(chat('current', 'channel_private', 3)));
+ expect(navigate).toHaveBeenCalledWith('room', { roomId: 'current' });
+ expect(navigation.params().roomId).toBe('current');
+ } finally {
+ navigate.mockRestore();
+ runInAction(() => {
+ chatsStore.chats.delete('older');
+ chatsStore.chats.delete('current');
+ chatsStore.serverId = null;
+ });
+ }
+});