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.4 KB
Raw Blame
import { Flexbox } from '@lobehub/ui';
import { memo } from 'react';

import { useDensity } from '@/styles/density';

import { dataSelectors, useConversationStore } from '../../store';
import Tool from './Tool';
import { shouldRenderToolCall } from './toolRenderRules';

interface ToolsRendererProps {
  disableEditing?: boolean;
  messageId: string;
  toolIds?: string[];
}

export const Tools = memo<ToolsRendererProps>(({ disableEditing, messageId, toolIds }) => {
  const { compact } = useDensity();
  // Subscribe only to the visible tool ids of this block. Streaming chunks that
  // change a single tool's args/result do not invalidate this string array, so
  // sibling Tool components stay isolated and only the changed one re-renders.
  // The store's default equality is `shallow`, which compares the ids by value.
  const visibleToolCallIds = useConversationStore((s) => {
    const tools = dataSelectors.getToolsInBlock(messageId)(s);
    if (!tools || tools.length === 0) return [];
    return tools
      .filter((t) => shouldRenderToolCall(t) && (!toolIds || toolIds.includes(t.id)))
      .map((t) => t.id);
  });

  if (visibleToolCallIds.length === 0) return null;

  return (
    <Flexbox gap={compact ? 0 : 8}>
      {visibleToolCallIds.map((toolCallId) => (
        <Tool
          assistantMessageId={messageId}
          disableEditing={disableEditing}
          id={toolCallId}
          key={toolCallId}
        />
      ))}
    </Flexbox>
  );
});