diff --git a/internal/gateway/methods/chat.go b/internal/gateway/methods/chat.go index 3843c465..41e233fa 100644 --- a/internal/gateway/methods/chat.go +++ b/internal/gateway/methods/chat.go @@ -60,7 +60,16 @@ func (m *ChatMethods) handleSend(ctx context.Context, client *gateway.Client, re } if params.AgentID == "" { - params.AgentID = "default" + // Extract agent key from session key (format: "agent:{key}:{rest}") + // so resuming an existing session routes to the correct agent. + if params.SessionKey != "" { + if agentKey, _ := sessions.ParseSessionKey(params.SessionKey); agentKey != "" { + params.AgentID = agentKey + } + } + if params.AgentID == "" { + params.AgentID = "default" + } } loop, err := m.agents.Get(params.AgentID) diff --git a/ui/web/src/pages/chat/chat-page.tsx b/ui/web/src/pages/chat/chat-page.tsx index 98cd4fa1..3ba0ae28 100644 --- a/ui/web/src/pages/chat/chat-page.tsx +++ b/ui/web/src/pages/chat/chat-page.tsx @@ -10,7 +10,7 @@ import { ChatInput } from "@/components/chat/chat-input"; import { useChatSessions } from "./hooks/use-chat-sessions"; import { useChatMessages } from "./hooks/use-chat-messages"; import { useChatSend } from "./hooks/use-chat-send"; -import { isOwnSession } from "@/lib/session-key"; +import { isOwnSession, parseSessionKey } from "@/lib/session-key"; export function ChatPage() { const { sessionKey: urlSessionKey } = useParams<{ sessionKey: string }>(); @@ -18,7 +18,13 @@ export function ChatPage() { const connected = useAuthStore((s) => s.connected); const userId = useAuthStore((s) => s.userId); - const [agentId, setAgentId] = useState("default"); + const [agentId, setAgentId] = useState(() => { + if (urlSessionKey) { + const { agentId: parsed } = parseSessionKey(urlSessionKey); + if (parsed) return parsed; + } + return "default"; + }); const [sessionKey, setSessionKey] = useState(urlSessionKey ?? ""); const { @@ -78,10 +84,15 @@ export function ChatPage() { const handleSessionSelect = useCallback( (key: string) => { + // Sync agentId from session key to ensure correct routing + const { agentId: parsed } = parseSessionKey(key); + if (parsed && parsed !== agentId) { + setAgentId(parsed); + } setSessionKey(key); navigate(`/chat/${encodeURIComponent(key)}`); }, - [navigate], + [navigate, agentId], ); const handleAgentChange = useCallback(