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.
This commit is contained in:
viettranx committed 2026-03-07 14:40:19 +07:00
1 parent 84113cff2f
commit 009df13fe8
2 files changed
+24 -4

No files matched your search

+10 -1
View File
@@ -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)
+14 -3
View File
@@ -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(