Skip to content

Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active

2.8 KB
Raw Blame
import { Terminal } from 'lucide-react';
import { useToolExpansion } from '@librechat/chat/react';
import type { ToolCallPhase } from '~/utils/toolCallPhase';
import { useProgress, useLocalize } from '~/hooks';
import ProgressText from './ProgressText';
import MarkdownLite from './MarkdownLite';
import { cn } from '~/utils';

export default function CodeAnalyze({
  initialProgress = 0.1,
  code,
  outputs = [],
  onExpand,
}: {
  initialProgress: number;
  code: string;
  outputs: Record<string, unknown>[];
  onExpand?: () => void;
}) {
  const localize = useLocalize();
  const progress = useProgress(initialProgress);
  const [showCode, setShowCode] = useToolExpansion(true);

  const handleToggleCode = () => {
    setShowCode(!showCode);
    if (!showCode) {
      onExpand?.();
    }
  };

  const logs = outputs.reduce((acc, output) => {
    if (output['logs']) {
      return acc + output['logs'] + '\n';
    }
    return acc;
  }, '');

  /**
   * The legacy assistants-endpoint card: it never receives run-step metadata,
   * so it genuinely has only these two states and maps them directly rather
   * than through `resolveToolCallPhase`, which needs signals this card has no
   * access to. The announcement and the icon below read this same value.
   */
  const phase: ToolCallPhase = progress < 1 ? 'running' : 'completed';

  return (
    <>
      <span className="sr-only" aria-live="polite" aria-atomic="true">
        {phase === 'running' ? localize('com_ui_analyzing') : localize('com_ui_analyzing_finished')}
      </span>
      <div className="relative my-1 flex h-5 shrink-0 items-center gap-2.5">
        <ProgressText
          phase={phase}
          onClick={handleToggleCode}
          inProgressText={localize('com_ui_analyzing')}
          finishedText={localize('com_ui_analyzing_finished')}
          hasInput={!!code.length}
          isExpanded={showCode}
          icon={
            <Terminal
              className={cn(
                'text-text-secondary size-4 shrink-0',
                phase === 'running' && 'animate-pulse',
              )}
              aria-hidden="true"
            />
          }
        />
      </div>
      {showCode && (
        <div className="code-analyze-block bg-surface-code mt-0.5 mb-3 overflow-hidden rounded-xl">
          <MarkdownLite content={code ? `\`\`\`python\n${code}\n\`\`\`` : ''} />
          {logs && (
            <div className="bg-surface-tertiary p-4 text-xs">
              <div className="text-text-secondary mb-1">{localize('com_ui_result')}</div>
              <div className="prose flex flex-col-reverse">
                <pre dir="ltr" className="text-text-primary! shrink-0">
                  {logs}
                </pre>
              </div>
            </div>
          )}
        </div>
      )}
    </>
  );
}