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.

3.0 KB
Raw Blame
import { type AssistantContentBlock, type UIChatMessage } from '@lobechat/types';
import { memo, useMemo } from 'react';

import { ReactionPicker } from '../../../components/Reaction';
import { messageStateSelectors, useConversationStore } from '../../../store';
import type { MessageActionsConfig } from '../../../types';
import {
  MessageActionBar,
  type MessageActionContext,
  type MessageActionSlot,
} from '../../components/MessageActionBar';

const DEFAULT_BAR_WITH_TOOLS: MessageActionSlot[] = ['delAndRegenerate', 'copy'];
const DEFAULT_BAR: MessageActionSlot[] = ['edit', 'copy'];
// Ids for tracing sit one level down, as in the single assistant message menu.
const ADVANCED_GROUP: MessageActionSlot = {
  children: ['copyMessageId', 'copyOperationId'],
  key: 'advanced',
};
const DEFAULT_MENU: MessageActionSlot[] = [
  'edit',
  'copy',
  'comments',
  'fork',
  'branching',
  'collapse',
  'divider',
  'share',
  'select',
  'divider',
  ADVANCED_GROUP,
  'divider',
  'regenerate',
  'del',
];
const IN_PROGRESS_BAR: MessageActionSlot[] = ['del'];
// Finished turn whose last child block is a tool call (typical for heterogeneous
// CC/Codex turns, which end on a Bash/Read/Edit/Task block with no trailing text).
// There's no text block to edit/copy, but the turn IS complete β€” it can still be
// shared and multi-selected/forwarded as one aggregated assistant reply. The
// forward serializer keeps child text while excluding child tool payloads.
const NO_TEXT_BLOCK_BAR: MessageActionSlot[] = ['delAndRegenerate'];
const NO_TEXT_BLOCK_MENU: MessageActionSlot[] = [
  'comments',
  'share',
  'select',
  'divider',
  'del',
  'divider',
  ADVANCED_GROUP,
];

interface GroupActionsProps {
  actionsConfig?: MessageActionsConfig;
  contentBlock?: AssistantContentBlock;
  contentId?: string;
  data: UIChatMessage;
  id: string;
}

export const GroupActionsBar = memo<GroupActionsProps>(
  ({ actionsConfig, id, data, contentBlock, contentId }) => {
    const ctx = useMemo<MessageActionContext>(
      () => ({ contentBlock, data, id, role: 'group' }),
      [contentBlock, data, id],
    );

    const isGenerating = useConversationStore(
      messageStateSelectors.isAssistantGroupItemGenerating(id),
    );

    // No finalized text block (group is empty, or its last child is a tool call).
    if (!contentId) {
      // Still streaming β†’ only delete is meaningful.
      if (isGenerating) {
        return <MessageActionBar bar={IN_PROGRESS_BAR} ctx={ctx} />;
      }
      // Finished, but the turn ends on a tool-call block β€” no text to edit/copy,
      // yet it's a complete reply that can still be shared and selected.
      return <MessageActionBar bar={NO_TEXT_BLOCK_BAR} ctx={ctx} menu={NO_TEXT_BLOCK_MENU} />;
    }

    const defaultBar = data.tools ? DEFAULT_BAR_WITH_TOOLS : DEFAULT_BAR;

    return (
      <MessageActionBar
        bar={actionsConfig?.bar ?? defaultBar}
        ctx={ctx}
        leading={<ReactionPicker messageId={id} />}
        menu={actionsConfig?.menu ?? DEFAULT_MENU}
      />
    );
  },
);

GroupActionsBar.displayName = 'GroupActionsBar';