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.7 KB
Raw Blame
import { AGENT_CHAT_TOPIC_URL } from '@lobechat/const';
import { toast } from '@lobehub/ui';
import isEqual from 'fast-deep-equal';
import { useCallback } from 'react';
import { useTranslation } from 'react-i18next';

import { useWorkspaceAwareNavigate } from '@/features/Workspace/useWorkspaceAwareNavigate';
import { useChatStore } from '@/store/chat';
import type { ForwardTarget } from '@/store/chat/slices/forward/action';

import { messageStateSelectors, useConversationStore } from '../store';

export type { ForwardTarget } from '@/store/chat/slices/forward/action';

/**
 * Returns a callback that forwards the currently-selected messages to one or
 * more target agents. The chat store serialises and sends the transcript into
 * one isolated topic per target; this hook owns only selection UX and navigation.
 */
export const useForwardMessages = () => {
  const { t } = useTranslation('chat');

  const navigate = useWorkspaceAwareNavigate();
  const forwardMessages = useChatStore((s) => s.forwardMessages);
  const clearPortalStack = useChatStore((s) => s.clearPortalStack);
  const exitSelectionMode = useConversationStore((s) => s.exitSelectionMode);

  // The conversation store is context-scoped (no global getState), so read the
  // selected messages reactively. They're frozen while the picker is open.
  const selectedMessages = useConversationStore(
    messageStateSelectors.forwardableSelectedMessages,
    isEqual,
  );

  return useCallback(
    async (targets: ForwardTarget[], note?: string) => {
      if (selectedMessages.length === 0) {
        toast.warning(t('messageForward.empty'));
        return;
      }
      if (targets.length === 0) return;

      const primaryTarget = targets[0];
      exitSelectionMode();

      void forwardMessages({
        header: t('messageForward.transcript.header', { count: selectedMessages.length }),
        messages: selectedMessages,
        note,
        onTopicCreated: (target, topicId) => {
          if (target.id !== primaryTarget.id) return;
          clearPortalStack();
          navigate(AGENT_CHAT_TOPIC_URL(target.id, topicId));
        },
        roleLabel: (role) =>
          role === 'user' ? t('messageForward.role.user') : t('messageForward.role.assistant'),
        targets,
      }).then((result) => {
        if (result.succeeded.length > 0) {
          toast.success(
            targets.length === 1
              ? t('messageForward.success', { title: primaryTarget.title || '' })
              : t('messageForward.successMulti', { count: result.succeeded.length }),
          );
        }
        if (result.failed.length > 0) toast.error(t('messageForward.failed'));
      });
    },
    [t, navigate, clearPortalStack, forwardMessages, exitSelectionMode, selectedMessages],
  );
};