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.

2.1 KB
Raw Blame
import { type ReactNode } from 'react';
import { memo, useMemo } from 'react';
import { useParams } from 'react-router';

import { ConversationProvider } from '@/features/Conversation';
import { useOperationState } from '@/hooks/useOperationState';
import { useChatStore } from '@/store/chat';
import { type MessageMapKeyInput } from '@/store/chat/utils/messageMapKey';

interface AgentBuilderProviderProps {
  agentId: string;
  children: ReactNode;
}

/**
 * Group Agent Builder Conversation Provider
 * Provides context for the Group Agent Builder chat panel
 * Uses 'group_agent_builder' scope with groupId to isolate messages per group
 */
const AgentBuilderProvider = memo<AgentBuilderProviderProps>(({ agentId, children }) => {
  // Use activeTopicId from chatStore (synced with URL query 'bt' via ProfileHydration)
  const activeTopicId = useChatStore((s) => s.activeTopicId);

  // The group being configured comes from the ROUTE, not the global store: the
  // send path samples its fallback (`activeGroupId`) after async preflight work,
  // so switching groups while a send is starting would hand the server the newly
  // active group and let the tool runtime stamp the wrong one.
  const { gid } = useParams<{ gid: string }>();

  // Build conversation context for group agent builder
  // Using group_agent_builder scope with groupId for per-group message isolation
  const context = useMemo<MessageMapKeyInput & { editingGroupId?: string }>(
    () => ({
      agentId,
      // NOT `groupId`: that would key the builder's messages into the group's
      // own chat bucket. `editingGroupId` is ignored by `messageMapKey` and only
      // travels to the server as `ExecAgentAppContext.editingGroupId`.
      editingGroupId: gid,
      scope: 'group_agent_builder',
      topicId: activeTopicId,
    }),
    [agentId, activeTopicId, gid],
  );

  // Get operation state for reactive updates
  const operationState = useOperationState(context);

  return (
    <ConversationProvider context={context} operationState={operationState}>
      {children}
    </ConversationProvider>
  );
});

export default AgentBuilderProvider;