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.

1.5 KB
Raw Blame
import { useParams, useSearchParams } from 'react-router';

import { useResolvedAgentRouteId } from '@/features/AgentRoute/useResolvedAgentRouteId';
import { useProjectIdByRef } from '@/store/project';
import { useProjectTopics } from '@/store/projectWorkingDirectory';

export const useAgentConversationCoordinate = () => {
  const params = useParams<{ aid?: string; projectId?: string; topicId?: string }>();
  const [searchParams] = useSearchParams();
  const { agentId } = useResolvedAgentRouteId(params.aid);
  // Project conversation routes carry :projectId/:topicId instead of :aid, so the
  // owning agent resolves from the project topic — still route-derived per tab,
  // never the global pointer (another tab may own it).
  // The route param may be the project's slug, while the topics are keyed by its id.
  const { id: projectId } = useProjectIdByRef(params.aid ? undefined : params.projectId);
  const projectTopics = useProjectTopics(projectId);
  // A blank draft a sidebar "+" opened has no topic yet; its owner rides in the
  // route (`?agent=`) and stays there through the first send's topic switch,
  // until the project list has the new topic.
  const projectTopicAgentId = params.projectId
    ? (projectTopics.find((topic) => topic.id === params.topicId)?.agentId ??
      searchParams.get('agent') ??
      undefined)
    : undefined;

  return [
    agentId ?? projectTopicAgentId,
    params.topicId ?? null,
    searchParams.get('thread'),
  ] as const;
};