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.5 KB
Raw Blame
import { LOADING_FLAT } from '@lobechat/const';
import { type UIChatMessage } from '@lobechat/types';
import { memo, useMemo } from 'react';

import type { MessageActionsConfig } from '../../../types';
import {
  MessageActionBar,
  type MessageActionContext,
  type MessageActionSlot,
} from '../../components/MessageActionBar';

const DEFAULT_BAR_WITH_TOOLS: MessageActionSlot[] = ['copy'];
const DEFAULT_BAR: MessageActionSlot[] = ['edit', 'copy'];
// Ids for tracing sit one level down, as in the assistant message menu.
const ADVANCED_GROUP: MessageActionSlot = {
  children: ['copyMessageId', 'copyOperationId'],
  key: 'advanced',
};
const DEFAULT_MENU: MessageActionSlot[] = [
  'edit',
  'copy',
  'comments',
  'collapse',
  'divider',
  'share',
  'divider',
  ADVANCED_GROUP,
  'divider',
  'regenerate',
  'del',
];
const ERROR_BAR: MessageActionSlot[] = ['regenerate', 'del'];
const EMPTY_ERROR_MENU: MessageActionSlot[] = ['copyOperationId'];
const ERROR_MENU: MessageActionSlot[] = [
  'edit',
  'copy',
  'copyOperationId',
  'comments',
  'divider',
  'del',
];

interface AssistantActionsBarProps {
  actionsConfig?: MessageActionsConfig;
  data: UIChatMessage;
  id: string;
}

/**
 * Action bar for Task / Tasks / GroupTasks messages. Uses `assistant` role
 * context but with a slimmer default menu (no translate / delAndRegenerate).
 */
export const AssistantActionsBar = memo<AssistantActionsBarProps>(({ actionsConfig, id, data }) => {
  const ctx = useMemo<MessageActionContext>(() => ({ data, id, role: 'assistant' }), [data, id]);

  const { content, error, tools } = data;

  // Empty error messages render only an interception card — nothing to edit
  // or copy, so only the operation-id action remains menu-worthy
  // (failed runs are exactly what it traces; the menu collapses away when the
  // action opts out). When the turn streamed content before erroring, keep
  // edit/copy so the partial reply stays salvageable.
  if (error) {
    const hasContent = !!content && content !== LOADING_FLAT && String(content).trim() !== '';
    return (
      <MessageActionBar
        bar={ERROR_BAR}
        ctx={ctx}
        menu={hasContent ? ERROR_MENU : EMPTY_ERROR_MENU}
      />
    );
  }

  const defaultBar = tools ? DEFAULT_BAR_WITH_TOOLS : DEFAULT_BAR;

  return (
    <MessageActionBar
      bar={actionsConfig?.bar ?? defaultBar}
      ctx={ctx}
      menu={actionsConfig?.menu ?? DEFAULT_MENU}
    />
  );
});

AssistantActionsBar.displayName = 'TaskAssistantActionsBar';