Ordo UI
AI执行与编排

AiExecutionBadge

@mox-hub/patterns · AI · 执行与编排 · canonical

ai-execution-badge@mox-hub/patternsAI Executioncanonicalsrc/components/ai-execution-badge.tsx
变体演示ai-execution-badge
小码
小码等待批准

Props API

Prop类型默认值说明
execution*ActiveAiExecution——
size*"xs" | "sm" | "md"'sm'—
variant*"line" | "compact" | "pill"'pill'—
className*string——
onClick*() => void——
runningLabel*string——

源码

packages/patterns/src/components/ai-execution-badge.tsx
import type { ReactNode } from 'react';
import { Bot, ShieldAlert, AlertCircle } from 'lucide-react';
import { RawButton } from '@mox-hub/ui/components/raw/raw-button';
import { Spinner } from '@mox-hub/ui/components/spinner';
import { cn } from '@mox-hub/ui';
import { usePatternLabels } from '../lib/i18n';

/** 正在执行的 AI 任务最小投影(与 ordo ActiveAiExecution 结构兼容)。 */
export interface ActiveAiExecution {
  status: string;
  agentName: string;
  stepSummary?: string;
}


export type IssueAiExecutionState = ActiveAiExecution;

export interface AiExecutionBadgeProps {
  execution: ActiveAiExecution;
  size?: 'xs' | 'sm' | 'md';
  variant?: 'pill' | 'compact' | 'line';
  className?: string;
  onClick?: () => void;
  /** 执行中占位文案;默认「正在执行」 */
  runningLabel?: string;
}

interface ShellProps {
  children: ReactNode;
  className?: string;
  /** 可交互态的可访问名;纯展示态不挂 aria-label */
  ariaLabel?: string;
  title?: string;
  onClick?: () => void;
  /** 纯展示态的宿主标签:compact/pill 用行内 span,line 用块级 div */
  as?: 'span' | 'div';
}

/**
 * 交互外壳:`onClick` 存在时用原生 button 承载(Tab 可达、Enter/Space 原生触发),
 * 纯展示态回落 span/div。视觉类由各形态传入,焦点环只加在 button 分支上,
 * 保证纯展示态外观逐字节不变。
 */
function Shell({
  children,
  className,
  ariaLabel,
  title,
  onClick,
  as = 'span',
}: ShellProps) {
  if (onClick) {
    return (
      <RawButton
        type="button"
        aria-label={ariaLabel}
        title={title}
        onClick={onClick}
        className={cn(
          // 焦点环沿用 ui/button 的口径;text-start 抵消 UA 对 button 的居中对齐
          'text-start outline-none focus-visible:border-ring focus-visible:ring-2 focus-visible:ring-ring/50',
          className,
        )}
      >
        {children}
      </RawButton>
    );
  }

  const Tag = as;
  return (
    <Tag title={title} className={className}>
      {children}
    </Tag>
  );
}

export function AiExecutionBadge({
  execution,
  size = 'sm',
  variant = 'pill',
  className,
  onClick,
  runningLabel,
}: AiExecutionBadgeProps) {
  const D = usePatternLabels('aiExecutionBadge', { runningLabel: '正在执行' });
  const label = runningLabel ?? D.runningLabel;
  const accessibleLabel = `${execution.agentName}: ${execution.stepSummary || label}`;
  const isPending = execution.status === 'pending_approval';
  const isBlocked = execution.status === 'blocked';

  if (variant === 'compact') {
    return (
      <Shell
        as="span"
        title={accessibleLabel}
        ariaLabel={accessibleLabel}
        onClick={onClick}
        className={cn(
          'inline-flex items-center gap-1 rounded-full px-1 py-0.5 font-medium transition-colors',
          isPending
            ? 'bg-accent-yellow-light/40 text-accent-yellow'
            : isBlocked
              ? 'bg-accent-red-light/40 text-accent-red'
              : 'bg-accent-purple-light/40 text-accent-purple',
          onClick && 'cursor-pointer hover:opacity-80',
          // xs 档只渲染图标、不含中文摘要,text-xs 在此不产生视觉变化;
          // text-3xs 仅保留给数字 / 拉丁元数据,不用于中文文案。
          'text-xs',
          className,
        )}
      >
        <Bot className={cn('shrink-0', size === 'xs' ? 'size-3' : 'size-3.5', !isPending && !isBlocked && 'animate-pulse')} />
        <span className="relative flex size-1.5 shrink-0">
          <span
            className={cn(
              'absolute inline-flex h-full w-full animate-ping rounded-full opacity-75',
              isPending ? 'bg-accent-yellow' : isBlocked ? 'bg-accent-red' : 'bg-accent-purple',
            )}
          />
          <span
            className={cn(
              'relative inline-flex size-1.5 rounded-full',
              isPending ? 'bg-accent-yellow' : isBlocked ? 'bg-accent-red' : 'bg-accent-purple',
            )}
          />
        </span>
      </Shell>
    );
  }

  if (variant === 'line') {
    return (
      <Shell
        as="div"
        ariaLabel={accessibleLabel}
        onClick={onClick}
        className={cn(
          'flex items-center gap-2 rounded-sm border px-2 py-1 text-xs transition-colors',
          isPending
            ? 'border-accent-yellow/30 bg-accent-yellow-light/20 text-accent-yellow'
            : isBlocked
              ? 'border-accent-red/30 bg-accent-red-light/20 text-accent-red'
              : 'border-accent-purple/30 bg-accent-purple-light/20 text-accent-purple',
          onClick && 'cursor-pointer hover:opacity-90',
          className,
        )}
      >
        {isPending ? (
          <ShieldAlert className="size-3.5 shrink-0 animate-bounce" />
        ) : isBlocked ? (
          <AlertCircle className="size-3.5 shrink-0" />
        ) : (
          <Spinner className="size-3.5 shrink-0 text-accent-purple" />
        )}
        <span className="font-mono font-medium">{execution.agentName}</span>
        <span className="truncate opacity-80">{execution.stepSummary || label}</span>
      </Shell>
    );
  }

  // default 'pill'
  return (
    <Shell
      as="span"
      ariaLabel={accessibleLabel}
      onClick={onClick}
      className={cn(
        'inline-flex items-center gap-1.5 rounded-full border px-2 py-0.5 text-xs font-medium transition-colors',
        isPending
          ? 'border-accent-yellow/30 bg-accent-yellow-light/30 text-accent-yellow'
          : isBlocked
            ? 'border-accent-red/30 bg-accent-red-light/30 text-accent-red'
            : 'border-accent-purple/30 bg-accent-purple-light/30 text-accent-purple',
        onClick && 'cursor-pointer hover:bg-accent-purple-light/50',
        className,
      )}
    >
      <Bot className={cn('size-3 shrink-0', !isPending && !isBlocked && 'animate-pulse')} />
      <span className="max-w-28 truncate">{execution.agentName}</span>
      <span className="relative flex size-1.5 shrink-0">
        <span
          className={cn(
            'absolute inline-flex h-full w-full animate-ping rounded-full opacity-75',
            isPending ? 'bg-accent-yellow' : isBlocked ? 'bg-accent-red' : 'bg-accent-purple',
          )}
        />
        <span
          className={cn(
            'relative inline-flex size-1.5 rounded-full',
            isPending ? 'bg-accent-yellow' : isBlocked ? 'bg-accent-red' : 'bg-accent-purple',
          )}
        />
      </span>
    </Shell>
  );
}

可访问性与 RTL

  • 键盘:交互件保留:focus-visible焦点环;浮层支持 Escape 关闭与焦点归还,菜单 / 页签按 Base UI 约定支持方向键。 筛选菜单、拖拽排序、执行树与模型 listbox 等关键键盘流由 Playwright 断言覆盖,见规范示例 · 交互与可访问性。
  • RTL:间距、对齐与方向性图标使用逻辑属性 (ms-* /text-start /rtl:-scale-x-100);通过 ThemeProvider 或I18nProvider 的direction="rtl"驱动,`lint:rtl` 会拦截新增物理类。
  • 文案:优先用组件labels /translate /translator props,其次用I18nProvider目录;内置 zh-CN 默认值,en-US 为参考翻译。见Theming · i18n 与 RTL。
  • 对比度:高对比度 token、2px 控件边框与 3px 焦点环由主题层统一提供,见Foundations · Contrast。

用法说明

AI 执行徽标:pill/compact/line 三形态,按 pending_approval/blocked/运行中三态着色;点击打开详情由 onClick 注入。