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.

6.3 KB
Raw Blame
import type { ActivateToolsState } from '@lobechat/builtin-tool-activator';
import { ActivatorApiName, LobeActivatorIdentifier } from '@lobechat/builtin-tool-activator';
import { AuvApiName, AuvIdentifier } from '@lobechat/builtin-tool-auv';
import { LocalSystemApiName, LocalSystemIdentifier } from '@lobechat/builtin-tool-local-system';
import { getBuiltinInspector } from '@lobechat/builtin-tools/inspectors';
import { getFilePathDisplayInfo } from '@lobechat/shared-tool-ui/components';
import type { ToolIntervention } from '@lobechat/types';
import { safeParseJSON, safeParsePartialJSON } from '@lobechat/utils';
import { Flexbox } from '@lobehub/ui';
import { MessageCircleQuestion } from 'lucide-react';
import { memo } from 'react';

import SafeBoundary from '@/components/ErrorBoundary';

import ExecutionTime from './ExecutionTime';
import StatusIndicator from './StatusIndicator';
import ToolTitle from './ToolTitle';

interface InspectorProps {
  apiName: string;
  arguments?: string;
  identifier: string;
  intervention?: ToolIntervention;
  /**
   * Whether the tool arguments are currently streaming
   */
  isArgumentsStreaming?: boolean;
  /**
   * Whether the tool detail is expanded. Collapsed rows show the plain
   * "<action> <keyword>" title; an expanded row restores the tool's own rich
   * inspector (full command, per-tool chips) since expanding means "show me
   * the details". Computer Use and image reads keep their action-specific
   * inspector visible in both states.
   */
  isExpanded?: boolean;
  /**
   * Whether the tool is currently executing (from operation state)
   */
  isToolCalling?: boolean;
  /** Owning message; native tool ids such as Codex `item_1` repeat across messages. */
  messageId: string;
  result?: { content: string | null; error?: any; state?: any };
  toolCallId: string;
  toolCallStartTime?: number;
}

const Inspectors = memo<InspectorProps>(
  ({
    identifier,
    apiName,
    arguments: argsStr,
    result,
    intervention,
    isArgumentsStreaming,
    isExpanded,
    isToolCalling,
    messageId,
    toolCallId,
    toolCallStartTime,
  }) => {
    const isPending = intervention?.status === 'pending';
    const isAborted = intervention?.status === 'aborted';
    const isRejected = intervention?.status === 'rejected';

    // Align with Tool/index.tsx: isToolCalling uses operation state + busy-message fallback.
    // Do not infer "executing" from missing result alone (ended runs may omit merged result).
    const isToolCallActive = Boolean(isToolCalling) && !isPending && !isAborted && !isRejected;
    const isTitleLoading = isArgumentsStreaming || isToolCallActive;
    const showExecutionTimer = isToolCallActive && !isArgumentsStreaming;

    const activateToolsState = result?.state as ActivateToolsState | undefined;
    let statusSuccessVariant: 'warning' | undefined;
    if (
      identifier === LobeActivatorIdentifier &&
      apiName === ActivatorApiName.activateTools &&
      !isTitleLoading &&
      !result?.error
    ) {
      const notFound = activateToolsState?.notFound;
      const activated = activateToolsState?.activatedTools;
      if (
        Array.isArray(notFound) &&
        notFound.length > 0 &&
        (!activated || activated.length === 0)
      ) {
        statusSuccessVariant = 'warning';
      }
    }

    // askUserQuestion (Claude Code and the builtin user-interaction tool share
    // the apiName) completes as "a question was asked", not "a task succeeded"
    // — keep the question-mark glyph instead of the generic tick.
    const statusSuccessIcon = apiName === 'askUserQuestion' ? MessageCircleQuestion : undefined;

    const args = safeParseJSON(argsStr);
    const partialJson = safeParsePartialJSON(argsStr);

    // Collapsed rows read as one plain sentence ("<action> <keyword>") so a
    // finished run scans as prose; expanding a row is an explicit ask for the
    // details, so it restores the tool's own rich inspector when one exists.
    // Computer Use multiplexes distinct actions behind runCommand; its inspector
    // carries the action/purpose even in the compact row. Image reads likewise
    // need their viewing label instead of the generic read-file title.
    const isComputerUse =
      (identifier === AuvIdentifier || identifier === 'lobe-auv') &&
      apiName === AuvApiName.runCommand;
    const readPath =
      args?.path ||
      args?.filePath ||
      args?.file_path ||
      partialJson?.path ||
      partialJson?.filePath ||
      partialJson?.file_path ||
      result?.state?.path;
    const isImageRead =
      identifier === LocalSystemIdentifier &&
      (apiName === LocalSystemApiName.readFile || apiName === 'readLocalFile') &&
      ((typeof readPath === 'string' && getFilePathDisplayInfo(readPath).isImage) ||
        (Array.isArray(result?.state?.images) && result.state.images.length > 0));
    const CustomInspector =
      isExpanded || isComputerUse || isImageRead
        ? getBuiltinInspector(identifier, apiName)
        : undefined;

    return (
      <Flexbox allowShrink horizontal align={'center'} gap={6}>
        <StatusIndicator
          intervention={intervention}
          isToolExecuting={isToolCalling}
          result={result}
          successIcon={statusSuccessIcon}
          successVariant={statusSuccessVariant}
        />
        {CustomInspector ? (
          <SafeBoundary minHeight={22} resetKeys={[argsStr, result]}>
            <CustomInspector
              apiName={apiName}
              args={args || {}}
              identifier={identifier}
              isArgumentsStreaming={isArgumentsStreaming}
              isLoading={isTitleLoading}
              partialArgs={partialJson}
              pluginState={result?.state}
              result={result}
              toolCallId={toolCallId}
            />
          </SafeBoundary>
        ) : (
          <ToolTitle
            apiName={apiName}
            args={args || undefined}
            identifier={identifier}
            isAborted={isAborted}
            isLoading={isTitleLoading}
            partialArgs={partialJson || undefined}
            result={result}
          />
        )}
        <ExecutionTime
          isExecuting={showExecutionTimer}
          startTime={toolCallStartTime}
          timerKey={`${messageId}:${toolCallId}`}
        />
      </Flexbox>
    );
  },
);

export default Inspectors;