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.

5.1 KB
Raw Blame
'use client';

import type { AgentGroupMember, BuiltinRenderProps } from '@lobechat/types';
import {
  Accordion,
  Block,
  createStaticStyles,
  Flexbox,
  Markdown,
  Text,
  useTheme,
} from '@lobehub/ui';
import { memo, useMemo } from 'react';
import { useTranslation } from 'react-i18next';

import AgentAvatar from '@/components/AgentAvatar';
import { useAgentGroupStore } from '@/store/agentGroup';
import { agentGroupSelectors } from '@/store/agentGroup/selectors';

import type { ExecuteTasksParams } from '../../../types';
import { MemberTaskDetail, MemberTaskTitle, useMemberAnchors } from '../../MemberProgress';

const styles = createStaticStyles(({ css, cssVar }) => ({
  assignee: css`
    display: flex;
    flex-shrink: 0;
    gap: 6px;
    align-items: center;

    font-size: 12px;
    color: ${cssVar.colorTextSecondary};
  `,
  container: css`
    .accordion-action {
      margin-inline-end: 8px;
      opacity: 1 !important;
    }
  `,
  index: css`
    flex-shrink: 0;
    font-size: 12px;
    color: ${cssVar.colorTextQuaternary};
  `,
  instruction: css`
    font-size: 13px;
    line-height: 1.6;
    color: ${cssVar.colorTextSecondary};
  `,
  resultBox: css`
    overflow: hidden;
  `,
  resultLabel: css`
    padding-inline: 4px;
    font-size: 12px;
    color: ${cssVar.colorTextTertiary};
  `,

  taskTitle: css`
    overflow: hidden;
    font-size: 14px;
    text-overflow: ellipsis;
    white-space: nowrap;
  `,
}));

/**
 * ExecuteTasks Render component for Group Management tool
 * Accordion-style task list with expandable instruction and assignee on the right.
 * A server-run call also shows each member's status, and expanding a task shows
 * what that member did in its own thread.
 */
const ExecuteTasksRender = memo<BuiltinRenderProps<ExecuteTasksParams, unknown, string>>(
  ({ args, content, toolCallId }) => {
    const { t } = useTranslation('tool');
    const theme = useTheme();
    const { tasks } = args || {};
    const resultContent = typeof content === 'string' ? content.trim() : '';

    // Get active group ID and agents from store
    const activeGroupId = useAgentGroupStore(agentGroupSelectors.activeGroupId);
    const groupAgents = useAgentGroupStore((s) =>
      activeGroupId ? agentGroupSelectors.getGroupAgents(activeGroupId)(s) : [],
    );

    // Get agent details for each task
    const tasksWithAgents = useMemo(() => {
      if (!tasks?.length) return [];
      return tasks.map((task) => ({
        ...task,
        agent: groupAgents.find((agent) => agent.id === task.agentId) as
          AgentGroupMember | undefined,
      }));
    }, [tasks, groupAgents]);

    const anchors = useMemberAnchors(toolCallId);

    if (!tasksWithAgents.length && !resultContent) return null;

    return (
      <Flexbox className={styles.container} gap={12}>
        {!!tasksWithAgents.length && (
          <Accordion
            defaultValue={[]}
            gap={0}
            variant={'borderless'}
            items={tasksWithAgents.map((task, index) => ({
              action: (
                <div className={styles.assignee}>
                  <AgentAvatar
                    agentId={task.agentId}
                    avatar={task.agent?.avatar}
                    background={task.agent?.backgroundColor || theme.colorBgContainer}
                    name={task.agent?.title}
                    shape={'circle'}
                    size={20}
                  />
                  <span>{task.agent?.title}</span>
                </div>
              ),
              alwaysShowAction: true,
              children: (task.instruction || anchors[index]) && (
                <Flexbox gap={12} style={{ marginTop: 8 }}>
                  {task.instruction && (
                    <Block padding={12} variant={'filled'}>
                      <Text className={styles.instruction}>{task.instruction}</Text>
                    </Block>
                  )}
                  {anchors[index] && <MemberTaskDetail anchor={anchors[index]} />}
                </Flexbox>
              ),
              key: task.agentId || String(index),
              title: (
                <Flexbox horizontal align={'center'} gap={8} style={{ minWidth: 0 }}>
                  <span className={styles.index}>{index + 1}.</span>
                  <MemberTaskTitle anchor={anchors[index]}>
                    <Text className={styles.taskTitle} weight={500}>
                      {task.title || 'Task'}
                    </Text>
                  </MemberTaskTitle>
                </Flexbox>
              ),
            }))}
          />
        )}

        {resultContent && (
          <Flexbox gap={4}>
            <Text className={styles.resultLabel}>
              {t('agentGroupManagement.executeTasks.results')}
            </Text>
            <Block className={styles.resultBox} padding={12} variant={'filled'}>
              <Markdown style={{ maxHeight: 320, overflow: 'auto' }} variant={'chat'}>
                {resultContent}
              </Markdown>
            </Block>
          </Flexbox>
        )}
      </Flexbox>
    );
  },
);

ExecuteTasksRender.displayName = 'ExecuteTasksRender';

export default ExecuteTasksRender;