Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

fix: Ask agent in CMDK not working (#11494)

René Wang27ec25ca10c0
diff --git a/src/app/[variants]/(main)/agent/features/Conversation/MainChatInput/MessageFromUrl.tsx b/src/app/[variants]/(main)/agent/features/Conversation/MainChatInput/MessageFromUrl.tsx
index 27ae1dacc49..6b3685ccce0 100644
--- a/src/app/[variants]/(main)/agent/features/Conversation/MainChatInput/MessageFromUrl.tsx
+++ b/src/app/[variants]/(main)/agent/features/Conversation/MainChatInput/MessageFromUrl.tsx
@@ -1,9 +1,11 @@
'use client';
-import { useEffect } from 'react';
-import { useSearchParams } from 'react-router-dom';
+import { useEffect, useMemo, useRef } from 'react';
+import { useLocation, useSearchParams } from 'react-router-dom';
import { useConversationStore } from '@/features/Conversation';
+import { useAgentStore } from '@/store/agent';
+import { agentSelectors } from '@/store/agent/selectors';
/**
* MessageFromUrl
@@ -12,23 +14,69 @@ import { useConversationStore } from '@/features/Conversation';
* Uses ConversationStore for input and send operations.
*/
const MessageFromUrl = () => {
- const [updateInputMessage, sendMessage] = useConversationStore((s) => [
- s.updateInputMessage,
+ const [sendMessage, context, messagesInit] = useConversationStore((s) => [
s.sendMessage,
+ s.context,
+ s.messagesInit,
]);
+ const agentId = context.agentId;
const [searchParams, setSearchParams] = useSearchParams();
+ const location = useLocation();
+ const isAgentConfigLoading = useAgentStore(agentSelectors.isAgentConfigLoading);
+
+ const routeAgentId = useMemo(() => {
+ const match = location.pathname?.match(/^\/agent\/([^#/?]+)/);
+ return match?.[1];
+ }, [location.pathname]);
+
+ // Track last processed (agentId, message) to prevent duplicate sends on re-render,
+ // while still allowing sending when navigating to a different agent (or message).
+ const lastProcessedSignatureRef = useRef<string | null>(null);
useEffect(() => {
const message = searchParams.get('message');
if (!message) return;
- const params = new URLSearchParams(searchParams.toString());
- params.delete('message');
- setSearchParams(params, { replace: true });
+ // Wait for agentId to be available before sending
+ if (!agentId) return;
+
+ // During agent switching, URL/searchParams may update before ConversationStore context updates.
+ // Only consume the param when the route agentId matches the ConversationStore agentId.
+ if (routeAgentId && routeAgentId !== agentId) return;
+
+ // Ensure required agent info is loaded before consuming the param.
+ // For existing conversations (topicId exists), also wait until messages are initialized
+ // to avoid sending during skeleton fetch states.
+ const isNewConversation = !context.topicId;
+ const isReady = !isAgentConfigLoading && (isNewConversation || messagesInit);
+ if (!isReady) return;
- updateInputMessage(message);
+ const signature = `${agentId}::${message}`;
+ if (lastProcessedSignatureRef.current === signature) return;
+ lastProcessedSignatureRef.current = signature;
+
+ // Use functional update to safely remove message param without affecting other params
+ setSearchParams(
+ (prev) => {
+ const newParams = new URLSearchParams(prev);
+ newParams.delete('message');
+ return newParams;
+ },
+ { replace: true },
+ );
+
+ // Send the message
sendMessage({ message });
- }, [searchParams, setSearchParams, updateInputMessage, sendMessage]);
+ }, [
+ searchParams,
+ setSearchParams,
+ sendMessage,
+ agentId,
+ context.topicId,
+ isAgentConfigLoading,
+ messagesInit,
+ routeAgentId,
+ ]);
return null;
};
diff --git a/src/app/[variants]/(main)/group/features/Conversation/MainChatInput/MessageFromUrl.tsx b/src/app/[variants]/(main)/group/features/Conversation/MainChatInput/MessageFromUrl.tsx
index 27ae1dacc49..a3dfc81f584 100644
--- a/src/app/[variants]/(main)/group/features/Conversation/MainChatInput/MessageFromUrl.tsx
+++ b/src/app/[variants]/(main)/group/features/Conversation/MainChatInput/MessageFromUrl.tsx
@@ -1,6 +1,6 @@
'use client';
-import { useEffect } from 'react';
+import { useEffect, useRef } from 'react';
import { useSearchParams } from 'react-router-dom';
import { useConversationStore } from '@/features/Conversation';
@@ -12,23 +12,37 @@ import { useConversationStore } from '@/features/Conversation';
* Uses ConversationStore for input and send operations.
*/
const MessageFromUrl = () => {
- const [updateInputMessage, sendMessage] = useConversationStore((s) => [
- s.updateInputMessage,
- s.sendMessage,
- ]);
+ const [sendMessage, agentId] = useConversationStore((s) => [s.sendMessage, s.context.agentId]);
const [searchParams, setSearchParams] = useSearchParams();
+ // Track if we've processed the initial message to prevent duplicate sends
+ const hasProcessedRef = useRef(false);
+
useEffect(() => {
+ // Only process once
+ if (hasProcessedRef.current) return;
+
const message = searchParams.get('message');
if (!message) return;
- const params = new URLSearchParams(searchParams.toString());
- params.delete('message');
- setSearchParams(params, { replace: true });
+ // Wait for agentId to be available before sending
+ if (!agentId) return;
+
+ hasProcessedRef.current = true;
+
+ // Use functional update to safely remove message param without affecting other params
+ setSearchParams(
+ (prev) => {
+ const newParams = new URLSearchParams(prev);
+ newParams.delete('message');
+ return newParams;
+ },
+ { replace: true },
+ );
- updateInputMessage(message);
+ // Send the message
sendMessage({ message });
- }, [searchParams, setSearchParams, updateInputMessage, sendMessage]);
+ }, [searchParams, setSearchParams, sendMessage, agentId]);
return null;
};