From 009df13fe8445ff2ab4a7f777eadab461a2a31f0 Mon Sep 17 00:00:00 2001 From: viettranx Date: Sat, 7 Mar 2026 14:40:19 +0700 Subject: [PATCH] fix(chat): extract agentId from session key when resuming sessions When a user refreshes the page and resumes an existing chat session, the frontend agentId could default to "default" instead of the correct agent embedded in the session key. This caused the backend to resolve the wrong agent, blocking MCP tools that were granted to the intended agent. Backend: parse agent key from session key format "agent:{key}:{rest}" when agentId param is empty in chat.send. Frontend: initialize agentId from URL session key on page load and sync it when selecting a different session. --- internal/gateway/methods/chat.go | 11 ++++++++++- ui/web/src/pages/chat/chat-page.tsx | 17 ++++++++++++++--- 2 files changed, 24 insertions(+), 4 deletions(-) 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(