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.

23.3 KB
Raw Blame
import { formatAgentProfile } from '@lobechat/prompts';
import type { BuiltinToolResult } from '@lobechat/types';

import { agentService } from '@/services/agent';
import type { GroupMemberConfig } from '@/services/chatGroup';
import { chatGroupService } from '@/services/chatGroup';
import { useAgentStore } from '@/store/agent';
import { getChatGroupStoreState } from '@/store/agentGroup';
import { agentGroupSelectors } from '@/store/agentGroup/selectors';
import { useGroupProfileStore } from '@/store/groupProfile';

import type {
  BatchCreateAgentsParams,
  BatchCreateAgentsState,
  CreateAgentParams,
  CreateAgentState,
  CreateGroupParams,
  CreateGroupState,
  GetAgentInfoParams,
  InviteAgentParams,
  InviteAgentState,
  RemoveAgentParams,
  RemoveAgentState,
  SearchAgentParams,
  SearchAgentState,
  UpdateAgentPromptParams,
  UpdateGroupParams,
  UpdateGroupPromptParams,
  UpdateGroupPromptState,
  UpdateGroupState,
} from '../types';

/**
 * Group Agent Builder Execution Runtime
 * Handles the execution logic for Group Agent Builder APIs
 * Extends AgentBuilder functionality with group-specific operations
 */
export class GroupAgentBuilderExecutionRuntime {
  // ==================== Agent Info ====================

  /**
   * Get detailed information about a specific agent
   */
  async getAgentInfo(
    groupId: string | undefined,
    args: GetAgentInfoParams,
  ): Promise<BuiltinToolResult> {
    if (!groupId) {
      return {
        content: 'No group context available',
        error: { message: 'No group context available', type: 'NoGroupContext' },
        success: false,
      };
    }

    const state = getChatGroupStoreState();
    const agent = agentGroupSelectors.getAgentByIdFromGroup(groupId, args.agentId)(state);

    if (!agent) {
      return {
        content: `Agent "${args.agentId}" not found in this group`,
        error: { message: `Agent "${args.agentId}" not found`, type: 'AgentNotFound' },
        success: false,
      };
    }

    // Return formatted agent profile for the supervisor
    return { content: formatAgentProfile(agent), state: agent, success: true };
  }

  // ==================== Group Member Management ====================

  /**
   * Search for agents that can be invited to the group
   */
  async searchAgent(args: SearchAgentParams): Promise<BuiltinToolResult> {
    const { query, limit = 10 } = args;

    try {
      // A builtin can never join a group (`addAgentsToGroup` refuses it), so the
      // inbox has to be excluded by the query itself: filtering the page
      // afterwards lets a recently updated inbox consume a `limit` slot and hide
      // an addable agent behind it.
      const results = await agentService.queryAgents({
        includeInbox: false,
        keyword: query,
        limit,
      });

      const agents = results.map((agent) => ({
        avatar: agent.avatar,
        description: agent.description,
        id: agent.id,
        name: agent.name ?? undefined,
        title: agent.title,
      }));

      const total = agents.length;

      if (total === 0) {
        return {
          content: query
            ? `No agents found matching "${query}".`
            : 'No agents found. You can create a new agent or search with different keywords.',
          state: { agents: [], query, total: 0 } as SearchAgentState,
          success: true,
        };
      }

      // Format agents list for LLM consumption. Lead with the name the agent is
      // known by and keep the title as its role, so a named agent stays
      // recognizable to the model.
      const agentList = agents
        .map((a, i) => {
          const name = a.name?.trim();
          const label = name
            ? `${name}${a.title ? ` [title: ${a.title}]` : ''}`
            : a.title || 'Untitled';
          return `${i + 1}. ${label} (ID: ${a.id})${a.description ? ` - ${a.description}` : ''}`;
        })
        .join('\n');

      return {
        content: query
          ? `Found ${total} agent${total > 1 ? 's' : ''} matching "${query}":\n${agentList}`
          : `Found ${total} agent${total > 1 ? 's' : ''}:\n${agentList}`,
        state: { agents, query, total } as SearchAgentState,
        success: true,
      };
    } catch (error) {
      return this.handleError(error, 'Failed to search agents');
    }
  }

  /**
   * Create a new group with an auto-generated supervisor agent
   */
  async createGroup(args: CreateGroupParams): Promise<BuiltinToolResult> {
    try {
      const state = getChatGroupStoreState();
      const groupConfig = {
        ...(args.openingMessage !== undefined && { openingMessage: args.openingMessage }),
        ...(args.openingQuestions !== undefined && { openingQuestions: args.openingQuestions }),
      };

      const { group, supervisorAgentId } = await chatGroupService.createGroup({
        avatar: args.avatar,
        backgroundColor: args.backgroundColor,
        config: Object.keys(groupConfig).length > 0 ? groupConfig : undefined,
        content: args.prompt,
        description: args.description,
        title: args.title,
      });

      state.internal_addGroup(group);

      if (args.supervisor) {
        const {
          avatar,
          backgroundColor,
          description,
          model,
          params,
          provider,
          systemRole,
          tags,
          title: supervisorTitle,
        } = args.supervisor;

        const supervisorConfig = {
          ...(model !== undefined && { model }),
          ...(params !== undefined && { params }),
          ...(provider !== undefined && { provider }),
          ...(systemRole !== undefined && { systemRole }),
        };
        const supervisorMeta = {
          ...(avatar !== undefined && { avatar }),
          ...(backgroundColor !== undefined && { backgroundColor }),
          ...(description !== undefined && { description }),
          ...(tags !== undefined && { tags }),
          ...(supervisorTitle !== undefined && { title: supervisorTitle }),
        };
        const tasks = [];

        if (Object.keys(supervisorConfig).length > 0) {
          tasks.push(agentService.updateAgentConfig(supervisorAgentId, supervisorConfig));
        }

        if (Object.keys(supervisorMeta).length > 0) {
          tasks.push(agentService.updateAgentMeta(supervisorAgentId, supervisorMeta));
        }

        if (tasks.length > 0) {
          await Promise.all(tasks);
        }
      }

      await state.internal_fetchGroupDetail(group.id);

      return {
        content: `Successfully created group "${args.title}" with ID: ${group.id}`,
        state: {
          groupId: group.id,
          success: true,
          supervisorAgentId,
          title: args.title,
        } as CreateGroupState,
        success: true,
      };
    } catch (error) {
      return this.handleError(error, 'Failed to create group');
    }
  }

  /**
   * Create a new agent and add it to the group
   */
  async createAgent(groupId: string, args: CreateAgentParams): Promise<BuiltinToolResult> {
    try {
      const state = getChatGroupStoreState();
      const group = agentGroupSelectors.getGroupById(groupId)(state);

      if (!group) {
        return {
          content: 'Group not found',
          error: { message: 'Group not found', type: 'GroupNotFound' },
          success: false,
        };
      }

      // Create a virtual agent only (no session needed for group agents)
      // Map 'tools' from LLM input to 'plugins' for internal API
      const result = await agentService.createAgentOnly({
        config: {
          avatar: args.avatar,
          description: args.description,
          plugins: args.tools,
          systemRole: args.systemRole,
          title: args.title,
          virtual: true,
        },
        groupId,
      });

      if (!result.agentId) {
        return {
          content: 'Failed to create agent: No agent ID returned',
          error: { message: 'No agent ID returned', type: 'CreateError' },
          success: false,
        };
      }

      // Refresh the group detail in the store
      await state.refreshGroupDetail(groupId);

      return {
        content: `Successfully created agent "${args.title}" and added it to the group.`,
        state: {
          agentId: result.agentId,
          success: true,
          title: args.title,
        } as CreateAgentState,
        success: true,
      };
    } catch (error) {
      return this.handleError(error, 'Failed to create agent');
    }
  }

  /**
   * Create multiple agents at once and add them to the group
   * Uses batch API for efficiency (single request instead of N requests)
   */
  async batchCreateAgents(
    groupId: string,
    args: BatchCreateAgentsParams,
  ): Promise<BuiltinToolResult> {
    try {
      const state = getChatGroupStoreState();
      const group = agentGroupSelectors.getGroupById(groupId)(state);

      if (!group) {
        return {
          content: 'Group not found',
          error: { message: 'Group not found', type: 'GroupNotFound' },
          success: false,
        };
      }

      // Use batch API to create all agents in one request
      // Map 'tools' from LLM input to 'plugins' for internal API
      const agentConfigs: GroupMemberConfig[] = args.agents.map((agentDef) => ({
        avatar: agentDef.avatar,
        description: agentDef.description,
        plugins: agentDef.tools,
        systemRole: agentDef.systemRole,
        title: agentDef.title,
      }));

      const { agents: createdAgents } = await chatGroupService.batchCreateAgentsInGroup(
        groupId,
        agentConfigs,
      );

      // Refresh the group detail in the store
      await state.refreshGroupDetail(groupId);

      const results = createdAgents.map((agent, index) => ({
        agentId: agent.id,
        success: true,
        title: args.agents[index].title,
      }));

      const createdList = results.map((r) => `- ${r.title} (ID: ${r.agentId})`).join('\n');

      return {
        content: `Successfully created ${results.length} agent${results.length > 1 ? 's' : ''}:\n${createdList}`,
        state: {
          agents: results,
          failedCount: 0,
          successCount: results.length,
        } as BatchCreateAgentsState,
        success: true,
      };
    } catch (error) {
      return this.handleError(error, 'Failed to create agents');
    }
  }

  /**
   * Invite an agent to the group
   */
  async inviteAgent(groupId: string, args: InviteAgentParams): Promise<BuiltinToolResult> {
    try {
      const state = getChatGroupStoreState();
      const group = agentGroupSelectors.getGroupById(groupId)(state);

      if (!group) {
        return {
          content: 'Group not found',
          error: { message: 'Group not found', type: 'GroupNotFound' },
          success: false,
        };
      }

      // Check if agent is already in the group
      const existingAgents = group.agents || [];
      const existingAgent = existingAgents.find((a) => a.id === args.agentId);

      if (existingAgent) {
        return {
          content: `Agent ${existingAgent.title || args.agentId} is already in the group`,
          state: {
            agentAvatar: existingAgent.avatar,
            agentId: args.agentId,
            agentName: existingAgent.title,
            success: false,
          } as InviteAgentState,
          success: false,
        };
      }

      // Add the agent to the group via service
      const result = await chatGroupService.addAgentsToGroup(groupId, [args.agentId]);

      // Refresh the group detail in the store
      await state.refreshGroupDetail(groupId);

      const wasAdded = result.added.length > 0;

      // Get the agent info from the updated group
      const updatedGroup = agentGroupSelectors.getGroupById(groupId)(getChatGroupStoreState());
      const addedAgent = updatedGroup?.agents?.find((a) => a.id === args.agentId);
      const agentName = addedAgent?.title;
      const agentAvatar = addedAgent?.avatar;

      const agentDisplay = agentName ? `${agentName} (ID: ${args.agentId})` : args.agentId;

      return {
        content: wasAdded
          ? `Successfully invited agent ${agentDisplay} to the group`
          : `Agent ${agentDisplay} was already in the group`,
        state: {
          agentAvatar,
          agentId: args.agentId,
          agentName,
          success: wasAdded,
        } as InviteAgentState,
        success: wasAdded,
      };
    } catch (error) {
      return this.handleError(error, 'Failed to invite agent');
    }
  }

  /**
   * Remove an agent from the group
   */
  async removeAgent(groupId: string, args: RemoveAgentParams): Promise<BuiltinToolResult> {
    try {
      const state = getChatGroupStoreState();
      const group = agentGroupSelectors.getGroupById(groupId)(state);

      if (!group) {
        return {
          content: 'Group not found',
          error: { message: 'Group not found', type: 'GroupNotFound' },
          success: false,
        };
      }

      // Check if agent is in the group
      const existingAgents = group.agents || [];
      const agent = existingAgents.find((a) => a.id === args.agentId);

      if (!agent) {
        return {
          content: `Agent ${args.agentId} is not in the group`,
          state: {
            agentId: args.agentId,
            success: false,
          } as RemoveAgentState,
          success: false,
        };
      }

      // Get agent info before removing
      const agentName = agent.title;
      const agentAvatar = agent.avatar;

      const agentDisplay = agentName ? `${agentName} (ID: ${args.agentId})` : args.agentId;

      // Check if this is the supervisor agent (cannot be removed)
      if (group.supervisorAgentId === args.agentId) {
        return {
          content: `Cannot remove supervisor agent ${agentDisplay} from the group`,
          state: {
            agentAvatar,
            agentId: args.agentId,
            agentName,
            success: false,
          } as RemoveAgentState,
          success: false,
        };
      }

      // Remove the agent from the group via service
      await chatGroupService.removeAgentsFromGroup(groupId, [args.agentId]);

      // Refresh the group detail in the store
      await state.refreshGroupDetail(groupId);

      return {
        content: `Successfully removed agent ${agentDisplay} from the group`,
        state: {
          agentAvatar,
          agentId: args.agentId,
          agentName,
          success: true,
        } as RemoveAgentState,
        success: true,
      };
    } catch (error) {
      return this.handleError(error, 'Failed to remove agent');
    }
  }

  // ==================== Group Configuration ====================

  /**
   * Update a specific agent's system prompt (systemRole)
   */
  async updateAgentPrompt(
    groupId: string,
    args: UpdateAgentPromptParams,
  ): Promise<BuiltinToolResult> {
    try {
      const { agentId, prompt } = args;

      // Get previous prompt for state
      const state = getChatGroupStoreState();
      const group = agentGroupSelectors.getGroupById(groupId)(state);
      const agent = group?.agents?.find((a) => a.id === agentId);
      const previousPrompt = agent?.systemRole ?? undefined;

      // Update the agent's systemRole via agent store
      await useAgentStore.getState().updateAgentConfigById(agentId, { systemRole: prompt });

      // Refresh the group detail in the store to sync agent data
      await state.refreshGroupDetail(groupId);

      // IMPORTANT: Directly update the editor content instead of manipulating store data.
      // This bypasses the priority issue between editorData (JSON) and systemRole (markdown).
      // The editor will auto-save and sync both fields properly after the update.
      useGroupProfileStore.getState().setAgentBuilderContent(agentId, prompt);

      const content = prompt
        ? `Successfully updated agent ${agentId} system prompt (${prompt.length} characters)`
        : `Successfully cleared agent ${agentId} system prompt`;

      return {
        content,
        state: {
          agentId,
          newPrompt: prompt,
          previousPrompt,
          success: true,
        },
        success: true,
      };
    } catch (error) {
      return this.handleError(error, 'Failed to update agent prompt');
    }
  }

  /**
   * Update group configuration and metadata (unified method)
   */
  async updateGroup(args: UpdateGroupParams): Promise<BuiltinToolResult> {
    try {
      const { currentGroup, group, groupId, isCurrentGroup, state } = await this.resolveGroupTarget(
        args.groupId,
      );

      if (!group || !groupId) {
        return {
          content: args.groupId ? `Group "${args.groupId}" not found` : 'No active group found',
          error: {
            message: args.groupId ? `Group "${args.groupId}" not found` : 'No active group found',
            type: args.groupId ? 'GroupNotFound' : 'NoGroupContext',
          },
          success: false,
        };
      }

      const { config, meta } = args;

      if (!config && !meta) {
        return {
          content: 'No configuration or metadata provided',
          error: { message: 'No configuration or metadata provided', type: 'NoDataProvided' },
          success: false,
        };
      }

      const updatedFields: string[] = [];
      const resultState: UpdateGroupState = { success: true };

      // Update config if provided
      if (config) {
        const configUpdate: { openingMessage?: string; openingQuestions?: string[] } = {};

        if (config.openingMessage !== undefined) {
          configUpdate.openingMessage = config.openingMessage;
          updatedFields.push(
            config.openingMessage
              ? `openingMessage (${config.openingMessage.length} chars)`
              : 'openingMessage (cleared)',
          );
        }

        if (config.openingQuestions !== undefined) {
          configUpdate.openingQuestions = config.openingQuestions;
          updatedFields.push(
            config.openingQuestions.length > 0
              ? `openingQuestions (${config.openingQuestions.length} questions)`
              : 'openingQuestions (cleared)',
          );
        }

        if (Object.keys(configUpdate).length > 0) {
          if (isCurrentGroup && currentGroup) {
            await state.updateGroupConfig(configUpdate);
          } else {
            await chatGroupService.updateGroup(groupId, {
              config: { ...group.config, ...configUpdate },
            });
          }

          resultState.updatedConfig = configUpdate;
        }
      }

      // Update meta if provided
      if (meta && Object.keys(meta).length > 0) {
        if (isCurrentGroup && currentGroup) {
          await state.updateGroupMeta(meta);
        } else {
          await chatGroupService.updateGroup(groupId, meta);
        }

        resultState.updatedMeta = meta;

        if (meta.avatar !== undefined) {
          updatedFields.push(`avatar (${meta.avatar || 'cleared'})`);
        }
        if (meta.title !== undefined) {
          updatedFields.push(`title (${meta.title || 'cleared'})`);
        }
        if (meta.description !== undefined) {
          updatedFields.push(
            meta.description
              ? `description (${meta.description.length} chars)`
              : 'description (cleared)',
          );
        }
        if (meta.backgroundColor !== undefined) {
          updatedFields.push(`backgroundColor (${meta.backgroundColor || 'cleared'})`);
        }
      }

      // Refresh the group detail in the store to ensure data sync
      await state.internal_fetchGroupDetail(groupId);

      const content = `Successfully updated group: ${updatedFields.join(', ')}`;

      return {
        content,
        state: resultState,
        success: true,
      };
    } catch (error) {
      return this.handleError(error, 'Failed to update group');
    }
  }

  /**
   * Update group shared prompt/content
   */
  async updateGroupPrompt(args: UpdateGroupPromptParams): Promise<BuiltinToolResult> {
    try {
      const { group, groupId, isCurrentGroup, state } = await this.resolveGroupTarget(args.groupId);

      if (!group || !groupId) {
        return {
          content: args.groupId ? `Group "${args.groupId}" not found` : 'No active group found',
          error: {
            message: args.groupId ? `Group "${args.groupId}" not found` : 'No active group found',
            type: args.groupId ? 'GroupNotFound' : 'NoGroupContext',
          },
          success: false,
        };
      }

      const previousPrompt = group.content ?? undefined;

      if (args.streaming && isCurrentGroup) {
        // Use streaming mode for typewriter effect
        await this.streamUpdateGroupPrompt(groupId, args.prompt);
      } else {
        // Update the content directly
        await chatGroupService.updateGroup(groupId, { content: args.prompt });
      }

      // Refresh the group detail in the store to ensure data sync
      await state.internal_fetchGroupDetail(groupId);

      // IMPORTANT: Directly update the editor content instead of manipulating store data.
      // This bypasses the priority issue between editorData (JSON) and content (markdown).
      // The editor will auto-save and sync both fields properly after the update.
      if (isCurrentGroup) {
        useGroupProfileStore.getState().setAgentBuilderContent(groupId, args.prompt);
      }

      const content = args.prompt
        ? `Successfully updated group shared prompt (${args.prompt.length} characters)`
        : 'Successfully cleared group shared prompt';

      return {
        content,
        state: {
          newPrompt: args.prompt,
          previousPrompt,
          success: true,
        } as UpdateGroupPromptState,
        success: true,
      };
    } catch (error) {
      return this.handleErrorWithState(error, 'Failed to update group prompt', {
        newPrompt: args.prompt,
        success: false,
      } as UpdateGroupPromptState);
    }
  }

  /**
   * Stream update group prompt with typewriter effect
   */
  private async streamUpdateGroupPrompt(groupId: string, prompt: string): Promise<void> {
    const state = getChatGroupStoreState();

    await state.updateGroup(groupId, { content: prompt });
  }

  private async resolveGroupTarget(groupId?: string) {
    const state = getChatGroupStoreState();
    const currentGroup = state.activeGroupId
      ? agentGroupSelectors.getGroupById(state.activeGroupId)(state)
      : undefined;
    const targetGroupId = groupId ?? currentGroup?.id;

    if (!targetGroupId) {
      return { currentGroup, group: undefined, groupId: undefined, isCurrentGroup: false, state };
    }

    const group =
      agentGroupSelectors.getGroupById(targetGroupId)(state) ??
      (await chatGroupService.getGroup(targetGroupId)) ??
      undefined;

    return {
      currentGroup,
      group,
      groupId: targetGroupId,
      isCurrentGroup: currentGroup?.id === targetGroupId,
      state,
    };
  }

  // ==================== Error Handling ====================

  private handleError(error: unknown, context: string): BuiltinToolResult {
    const err = error as Error;
    return {
      content: `${context}: ${err.message}`,
      error: {
        body: error,
        message: err.message,
        type: 'RuntimeError',
      },
      success: false,
    };
  }

  private handleErrorWithState<T extends object>(
    error: unknown,
    context: string,
    state: T,
  ): BuiltinToolResult {
    const err = error as Error;
    return {
      content: `${context}: ${err.message}`,
      error: {
        body: error,
        message: err.message,
        type: 'RuntimeError',
      },
      state,
      success: false,
    };
  }
}