AI上下文与成本
AiContextSummary
@mox-hub/patterns · AI · 上下文与成本 · canonical
ai-context-summary@mox-hub/patternsAI Executioncanonicalsrc/components/ai-context-summary.tsx变体演示ai-context-summary
TypeScriptReactNext.js
阶段: growth
复杂度: medium
团队规模: small
健康度
82
Props API
| Prop | 类型 | 默认值 | 说明 |
|---|---|---|---|
| context* | ProjectAIContextData | — | — |
| compact* | boolean | — | — |
| labels* | Partial<AiContextSummaryLabels> | — | — |
源码
import { cn } from '@mox-hub/ui';
import { usePatternLabels } from '../lib/i18n';
/**
* AiContextSummary —— 项目 AI 上下文摘要(AI Execution)。
*
* i18n:内置 zh-CN 默认文案,section 为 `aiContextSummary`(`usePatternLabels`);
* 消费方可经 I18nProvider messages 或 `labels` prop 覆写(显式 labels 优先)。
* Phase / Complexity / Team / Health 等 UI 文案均已进 labels,源码零硬编码英文。
*
* a11y:健康分容器为 `role="meter"`,暴露 `aria-valuemin=0` / `aria-valuemax=100` /
* `aria-valuenow`(值先 clamp 到 0–100)与 `aria-label`(默认「健康度」),读屏可直接
* 读出分数。条宽是唯一动态值,仍走 style,其余样式一律语义 token class。
*/
export interface ProjectAIContextData {
techStack?: string[] | null;
languages?: string[] | null;
frameworks?: string[] | null;
lifecyclePhase?: string | null;
complexityLevel?: string | null;
teamSizeCategory?: string | null;
healthScore?: number | null;
riskIndicators?: {
overdueTaskRatio?: number;
blockedTaskCount?: number;
velocityTrend?: string;
ciFailureRate?: number;
} | null;
}
export interface AiContextSummaryLabels {
phase: string;
complexity: string;
team: string;
health: string;
}
export const AI_CONTEXT_SUMMARY_DEFAULT_LABELS: AiContextSummaryLabels = {
phase: '阶段',
complexity: '复杂度',
team: '团队规模',
health: '健康度',
};
export interface AiContextSummaryProps {
context: ProjectAIContextData;
compact?: boolean;
/** 文案覆写(默认 zh-CN;英文由消费方传入 labels 切换)。 */
labels?: Partial<AiContextSummaryLabels>;
}
export function AiContextSummary({ context, compact, labels }: AiContextSummaryProps) {
const D = usePatternLabels('aiContextSummary', AI_CONTEXT_SUMMARY_DEFAULT_LABELS);
const text: AiContextSummaryLabels = {
phase: labels?.phase ?? D.phase,
complexity: labels?.complexity ?? D.complexity,
team: labels?.team ?? D.team,
health: labels?.health ?? D.health,
};
const chips = [
...(context.techStack ?? []),
...(context.frameworks ?? []),
].slice(0, compact ? 4 : 8);
const metaItems = [
context.lifecyclePhase && { label: text.phase, value: context.lifecyclePhase },
context.complexityLevel && { label: text.complexity, value: context.complexityLevel },
context.teamSizeCategory && { label: text.team, value: context.teamSizeCategory },
].filter(Boolean) as { label: string; value: string }[];
// 健康分 clamp 到 0–100;NaN 视为无值,不渲染条。
const rawHealth = context.healthScore;
const healthScore =
rawHealth == null || Number.isNaN(rawHealth)
? null
: Math.max(0, Math.min(100, rawHealth));
return (
<div className="space-y-3">
{chips.length > 0 && (
<div className="flex flex-wrap gap-1.5">
{chips.map((chip) => (
<span
key={chip}
className="rounded-full bg-muted/50 px-2 py-0.5 text-xs text-muted-foreground"
>
{chip}
</span>
))}
</div>
)}
{metaItems.length > 0 && (
<div className={cn('flex flex-wrap gap-x-4 gap-y-1', compact && 'gap-x-3')}>
{metaItems.map((item) => (
<div key={item.label}>
<span className="text-xs text-muted-foreground">{item.label}: </span>
<span className="text-xs font-medium capitalize text-foreground">{item.value}</span>
</div>
))}
</div>
)}
{healthScore !== null && (
<div className="flex items-center gap-2">
<span className="text-xs text-muted-foreground">{text.health}</span>
<div
role="meter"
aria-label={text.health}
aria-valuemin={0}
aria-valuemax={100}
aria-valuenow={healthScore}
className="h-1.5 w-20 overflow-hidden rounded-full bg-muted/50"
>
<div
className={cn(
'h-full rounded-full transition-[width,background-color]',
healthScore >= 80
? 'bg-accent-green'
: healthScore >= 60
? 'bg-accent-yellow'
: 'bg-accent-red',
)}
style={{ width: `${healthScore}%` }}
/>
</div>
<span className="text-xs font-medium tabular-nums text-foreground">{healthScore}</span>
</div>
)}
</div>
);
}
可访问性与 RTL
- 键盘:交互件保留
:focus-visible焦点环;浮层支持 Escape 关闭与焦点归还,菜单 / 页签按 Base UI 约定支持方向键。 筛选菜单、拖拽排序、执行树与模型 listbox 等关键键盘流由 Playwright 断言覆盖,见规范示例 · 交互与可访问性。 - RTL:间距、对齐与方向性图标使用逻辑属性 (
ms-*/text-start/rtl:-scale-x-100);通过ThemeProvider或I18nProvider的direction="rtl"驱动,`lint:rtl` 会拦截新增物理类。 - 文案:优先用组件
labels/translate/translatorprops,其次用I18nProvider目录;内置 zh-CN 默认值,en-US 为参考翻译。见Theming · i18n 与 RTL。 - 对比度:高对比度 token、2px 控件边框与 3px 焦点环由主题层统一提供,见Foundations · Contrast。
用法说明
项目 AI 上下文摘要:technologies/frameworks chips + lifecycle/complexity/team 元信息 + healthScore 条;compact 收敛 chips 数量。