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.

4.5 KB
Raw Blame
import { builtinTools } from '@lobechat/builtin-tools';
import {
  type ResolvedToolSet,
  ToolArgumentsRepairer,
  ToolNameResolver,
} from '@lobechat/context-engine';
import { type ChatToolPayload, type MessageToolCall, type ToolManifest } from '@lobechat/types';

import { type ChatStore } from '@/store/chat/store';
import { getServerConfigStoreState, serverConfigSelectors } from '@/store/serverConfig';
import { useToolStore } from '@/store/tool';
import {
  composioStoreSelectors,
  lobehubSkillStoreSelectors,
  pluginSelectors,
  toolSelectors,
} from '@/store/tool/selectors';
import { type StoreSetter } from '@/store/types';

/**
 * Internal utility methods and runtime state management
 * These are building blocks used by other actions
 */

type Setter = StoreSetter<ChatStore>;
export const pluginInternals = (set: Setter, get: () => ChatStore, _api?: unknown) =>
  new PluginInternalsActionImpl(set, get, _api);

export class PluginInternalsActionImpl {
  constructor(set: Setter, get: () => ChatStore, _api?: unknown) {
    void _api;
    void set;
    void get;
  }

  internal_transformToolCalls = (
    toolCalls: MessageToolCall[],
    offeredToolNames?: string[],
    resolvedTools?: Pick<ResolvedToolSet, 'manifestMap' | 'sourceMap'>,
  ): ChatToolPayload[] => {
    const toolNameResolver = new ToolNameResolver();

    // Build manifests map from tool store
    const toolStoreState = useToolStore.getState();
    const manifests: Record<string, ToolManifest> = {};

    // Track source for each identifier
    const sourceMap: Record<string, 'builtin' | 'mcp' | 'composio' | 'lobehubSkill'> = {};

    // Get all installed plugins (all treated as MCP now)
    const installedPlugins = pluginSelectors.confirmedInstalledPlugins(toolStoreState);
    for (const plugin of installedPlugins) {
      if (plugin.manifest) {
        manifests[plugin.identifier] = plugin.manifest as ToolManifest;
        sourceMap[plugin.identifier] = 'mcp';
      }
    }

    // MCP connectors (every install now): the tools engine lets one replace a
    // same-named plugin, so the names it offered come from the connector
    for (const manifest of toolSelectors.executableConnectorManifests(toolStoreState)) {
      manifests[manifest.identifier] = manifest;
      sourceMap[manifest.identifier] = 'mcp';
    }

    // Get all builtin tools
    for (const tool of builtinTools) {
      if (tool.manifest) {
        manifests[tool.identifier] = tool.manifest as ToolManifest;
        sourceMap[tool.identifier] = 'builtin';
      }
    }

    // Get all Composio tools
    const composioTools = composioStoreSelectors.composioAsLobeTools(toolStoreState);
    for (const tool of composioTools) {
      if (tool.manifest) {
        manifests[tool.identifier] = tool.manifest as ToolManifest;
        sourceMap[tool.identifier] = 'composio';
      }
    }

    // Get all LobeHub Skill tools
    const lobehubSkillTools = lobehubSkillStoreSelectors.lobehubSkillAsLobeTools(toolStoreState);
    for (const tool of lobehubSkillTools) {
      if (tool.manifest) {
        manifests[tool.identifier] = tool.manifest as ToolManifest;
        sourceMap[tool.identifier] = 'lobehubSkill';
      }
    }

    // Connector manifests live in the operation snapshot, not installedPlugins. Use the
    // same manifest that advertised the tool so hashed API names can be restored.
    const serverConfig = resolvedTools && getServerConfigStoreState();
    if (resolvedTools && serverConfig && serverConfigSelectors.enableMcpApps(serverConfig)) {
      for (const [identifier, manifest] of Object.entries(resolvedTools.manifestMap)) {
        // Client runs may omit sourceMap; connector manifests still carry type=mcp.
        const source = resolvedTools.sourceMap[identifier];
        if (source ? source !== 'mcp' : manifest.type !== 'mcp') continue;
        manifests[identifier] = manifest as ToolManifest;
        sourceMap[identifier] = 'mcp';
      }
    }

    // Resolve tool calls and add source field
    const resolved = toolNameResolver.resolve(toolCalls, manifests, offeredToolNames);

    return resolved.map((payload) => {
      // Parse and repair arguments if needed
      const manifest = manifests[payload.identifier];
      const repairer = new ToolArgumentsRepairer(manifest);
      const repairedArgs = repairer.parse(payload.apiName, payload.arguments);

      return {
        ...payload,
        arguments: JSON.stringify(repairedArgs),
        source: sourceMap[payload.identifier],
      };
    });
  };
}

export type PluginInternalsAction = Pick<
  PluginInternalsActionImpl,
  keyof PluginInternalsActionImpl
>;