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.9 KB
Raw Blame
import { Accordion, Divider, Flexbox, Icon, Text } from '@lobehub/ui';
import { cssVar } from '@lobehub/ui/stylex/cssVar.stylex';
import * as stylex from '@stylexjs/stylex';
import { ChevronRight } from 'lucide-react';
import { memo, type ReactNode, useMemo, useState } from 'react';
import { useTranslation } from 'react-i18next';

import { useDensity } from '@/styles/density';

const PROCESS_KEY = 'process';

const styles = stylex.create({
  title: {
    minWidth: '0',
    color: { 'default': cssVar.colorTextTertiary, ':hover': cssVar.colorText },
    whiteSpace: 'nowrap',
    transition: `color 150ms ${cssVar.motionEaseOut}`,
  },
});

/**
 * The Accordion's borderless header paints a hover fill that bleeds outward
 * through a negative inline margin, and `message-body`'s `overflow: hidden`
 * clips that into cut corners. Stripping the header chrome leaves a bare text
 * row sitting on the answer's baseline, with hover reading as the text
 * brightening instead. Inline styles are deliberate — they outrank the
 * package's `:hover` background rule without an `!important`.
 */
const HEADER_STYLE = { background: 'transparent', margin: 0 } as const;
/** `font: inherit` drops the trigger's built-in 500 weight so the summary row
 *  reads as plain body text instead of a heading. */
const TRIGGER_STYLE = { font: 'inherit', padding: 0 } as const;
/** The stripped header has no padding of its own, so the expanded process would
 *  otherwise start flush against it. */
const CONTENT_STYLE = { paddingBlockStart: 8 } as const;
const COMPACT_CONTENT_STYLE = { paddingBlockEnd: 4, paddingBlockStart: 4 } as const;

interface ProcessFoldProps {
  /** Rendered process (reasoning + tools + intermediate prose); shown only when expanded. */
  children: ReactNode;
  /** Whether the process starts expanded. */
  defaultExpanded?: boolean;
  /** Formatted turn duration, e.g. "3m 37s". Hidden when absent. */
  durationText?: string;
  /** Number of steps in the turn = count of assistant (call_llm) messages. */
  stepCount: number;
}

/**
 * Codex-style "已处理 {duration}" header that folds a finished turn's *process*
 * (reasoning + tool calls + intermediate narration) into one persistent,
 * toggleable row. The turn's final answer is rendered separately and stays
 * visible regardless of this state. Purely a view affordance — never persisted.
 */
const ProcessFold = memo<ProcessFoldProps>(
  ({ children, durationText, stepCount, defaultExpanded = false }) => {
    const { t } = useTranslation('chat');
    const [expanded, setExpanded] = useState(defaultExpanded);
    const { compact } = useDensity();
    const value = useMemo(() => (expanded ? [PROCESS_KEY] : []), [expanded]);

    const title = (
      <Flexbox horizontal align={'center'} className={stylex.props(styles.title).className} gap={6}>
        <Text style={{ color: 'inherit', fontSize: compact ? 13 : undefined, minWidth: 0 }}>
          {durationText
            ? t('turnProcess.ranFor', { count: stepCount, duration: durationText })
            : t('turnProcess.done', { count: stepCount })}
        </Text>
        <Icon
          icon={ChevronRight}
          size={14}
          style={{
            flex: 'none',
            transform: expanded ? 'rotate(90deg)' : undefined,
            transition: 'transform 200ms',
          }}
        />
      </Flexbox>
    );

    return (
      <>
        <Accordion
          hideIndicator
          items={[{ children, key: PROCESS_KEY, title }]}
          value={value}
          variant={'borderless'}
          styles={{
            content: compact ? COMPACT_CONTENT_STYLE : CONTENT_STYLE,
            header: HEADER_STYLE,
            trigger: TRIGGER_STYLE,
          }}
          onValueChange={(next) => setExpanded(next.includes(PROCESS_KEY))}
        />
        <Divider style={{ marginBlock: 0 }} />
      </>
    );
  },
);

ProcessFold.displayName = 'ProcessFold';

export default ProcessFold;