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.

3.2 KB
Raw Blame
'use client';

import {
  GitHubInspector,
  LINEAR_TOOL_NAMES,
  LinearInspector,
} from '@lobechat/shared-tool-ui/inspectors';
import {
  highlightTextStyles,
  inspectorTextStyles,
  shinyTextStyles,
} from '@lobechat/shared-tool-ui/styles';
import type { BuiltinInspectorProps } from '@lobechat/types';
import { cx } from '@lobehub/ui';
import type { ComponentType } from 'react';
import { memo } from 'react';
import { useTranslation } from 'react-i18next';

import type { CodexMcpToolArgs, CodexMcpToolState } from './mcpToolUtils';
import {
  getCodexGithubMcpApiName,
  getCodexLinearMcpApiName,
  getMcpInputRecord,
  getMcpServer,
  getMcpToolName,
} from './mcpToolUtils';

const LINEAR_TOOL_NAME_SET = new Set<string>([...LINEAR_TOOL_NAMES, 'fetch', 'search']);
const SharedLinearInspector = LinearInspector as ComponentType<
  BuiltinInspectorProps<Record<string, unknown>>
>;
const SharedGitHubInspector = GitHubInspector as ComponentType<
  BuiltinInspectorProps<Record<string, unknown>>
>;

const McpToolInspector = memo<BuiltinInspectorProps<CodexMcpToolArgs, CodexMcpToolState>>(
  ({ args, partialArgs, isArgumentsStreaming, isLoading, pluginState }) => {
    const { t } = useTranslation('plugin');
    const label = t('builtins.codex.apiName.mcp_tool_call', {
      defaultValue: 'Call MCP tool',
    });
    const server = getMcpServer(args, pluginState) || getMcpServer(partialArgs);
    const tool = getMcpToolName(args, pluginState) || getMcpToolName(partialArgs);
    const input = getMcpInputRecord(args, pluginState);
    const partialInput = getMcpInputRecord(partialArgs);
    const linearApiName = getCodexLinearMcpApiName({
      input: input || partialInput,
      server,
      toolName: tool,
    });
    const githubApiName = getCodexGithubMcpApiName({
      server,
      toolName: tool,
    });

    if (LINEAR_TOOL_NAME_SET.has(linearApiName)) {
      return (
        <SharedLinearInspector
          apiName={linearApiName}
          args={input || {}}
          identifier={'codex'}
          isArgumentsStreaming={isArgumentsStreaming}
          isLoading={isLoading}
          partialArgs={partialInput || {}}
          pluginState={pluginState}
        />
      );
    }

    if (githubApiName) {
      return (
        <SharedGitHubInspector
          apiName={githubApiName}
          args={input || {}}
          identifier={'codex'}
          isArgumentsStreaming={isArgumentsStreaming}
          isLoading={isLoading}
          partialArgs={partialInput || {}}
          pluginState={pluginState}
        />
      );
    }

    const target = [server, tool].filter(Boolean).join(' > ');

    if (isArgumentsStreaming && !target) {
      return <div className={cx(inspectorTextStyles.root, shinyTextStyles.shinyText)}>{label}</div>;
    }

    return (
      <div className={inspectorTextStyles.root}>
        <span className={cx((isArgumentsStreaming || isLoading) && shinyTextStyles.shinyText)}>
          {label}
        </span>
        {target && (
          <>
            <span>: </span>
            <span className={highlightTextStyles.primary}>{target}</span>
          </>
        )}
      </div>
    );
  },
);

McpToolInspector.displayName = 'CodexMcpToolInspector';

export default McpToolInspector;