AI助手与对话
OmniDock
@mox-hub/patterns · AI · 助手与对话 · canonical
omni-dock@mox-hub/patternsAI Executioncanonicalsrc/components/omni-dock.tsx变体演示omni-dock
附带上下文: @小码
Props API
| Prop | 类型 | 默认值 | 说明 |
|---|---|---|---|
| onSendMessage* | (text: string, model: string) => void | — | — |
| activeAgentName* | string | — | — |
| labels* | Partial<OmniDockLabels> | — | — |
| models* | OmniDockModel[] | — | — |
| model* | string | — | — |
| onModelChange* | (id: string) => void | — | — |
| quickCommands* | OmniDockQuickCommand[] | — | — |
源码
import { useState, type FormEvent, type ReactNode } from 'react';
import { Bot, Send, Sparkles } from 'lucide-react';
import { cn } from '@mox-hub/ui';
import { Button } from '@mox-hub/ui/components/button';
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from '@mox-hub/ui/components/select';
import { usePatternLabels } from '../lib/i18n';
/**
* OmniDock —— 底部协同输入坞(AI 输入面)。
*
* i18n:内置 zh-CN 默认文案,section 为 `omniDock`(`usePatternLabels`);
* 消费方可经 I18nProvider messages 或 `labels` prop 覆写(显式 labels 优先)。
*
* a11y:模型选择改用 `ui/select`(base-ui)——Trigger 为 combobox,弹层为 listbox,
* 方向键 / Enter / Escape 与焦点归还由 ui 组件负责,本件不再手搓
* `role="option"` 与 roving tabindex;发送、快捷指令均为 button 语义(`Button`)。
*
* 数据:模型列表与快捷指令均可 props 注入;未注入时使用下方 demo 默认值。
*/
export interface OmniDockLabels {
inputPlaceholder: string;
modelThinking: string;
modelMultimodal: string;
modelReasoning: string;
contextOnTitle: string;
contextOn: (name: string) => string;
contextOffTitle: string;
contextOff: string;
quickPlan: string;
quickPlanCommand: string;
quickVerify: string;
quickVerifyCommand: string;
send: string;
modelSelect: string;
modelMenu: string;
}
export const OMNI_DOCK_DEFAULT_LABELS: OmniDockLabels = {
inputPlaceholder: '输入协同指令,或使用 / 唤起技能,@ 呼唤指定 Agent...',
modelThinking: '深度思考',
modelMultimodal: '多模态',
modelReasoning: '逻辑推演',
contextOnTitle:
'发送时会把这名同事的当前状态(在做哪张单、执行状态、最近进展、待决数)一并交给 AI;消息本身仍走既有派发,不指定具体执行者',
contextOn: (name: string) => `附带上下文: @${name}`,
contextOffTitle: '未选中同事,消息不带盯盘上下文',
// 修复:原值为 '{D.contextOff}' 字面量,未选同事时界面会直接显示该占位符。
contextOff: '未附带同事上下文',
quickPlan: '/plan 任务拆解',
quickPlanCommand: '/plan CAP-P-01 任务拆解',
quickVerify: '/verify 门禁审计',
quickVerifyCommand: '/verify 运行契约门禁审计',
send: '发送',
modelSelect: '选择模型',
modelMenu: '可选模型',
};
export interface OmniDockModel {
id: string;
label: string;
badge?: ReactNode;
}
export interface OmniDockQuickCommand {
label: string;
value: string;
}
export interface OmniDockProps {
onSendMessage: (text: string, model: string) => void;
activeAgentName?: string | null;
labels?: Partial<OmniDockLabels>;
/** 可选模型列表;未传时使用内置 demo 模型。 */
models?: OmniDockModel[];
/** 受控当前模型 id;未传时内部默认取 models[0].id。 */
model?: string;
onModelChange?: (id: string) => void;
/** 快捷指令;未传时使用 /plan、/verify 两条中文默认。 */
quickCommands?: OmniDockQuickCommand[];
}
/**
* 内置 demo 模型(仅作未注入时兜底)。badgeKey 指向 labels 里的徽标文案,
* 使 Provider / labels 覆写仍能生效。
*/
const DEMO_MODELS = [
{ id: 'claude-3-7-sonnet', label: 'Claude 3.7 Sonnet', badgeKey: 'modelThinking' },
{ id: 'gpt-4o', label: 'GPT-4o Omniscient', badgeKey: 'modelMultimodal' },
{ id: 'deepseek-r1', label: 'DeepSeek R1', badgeKey: 'modelReasoning' },
] as const satisfies readonly {
id: string;
label: string;
badgeKey: keyof Pick<OmniDockLabels, 'modelThinking' | 'modelMultimodal' | 'modelReasoning'>;
}[];
/** 合并显式 labels(忽略 undefined,避免覆盖内置默认值)。 */
function resolveLabels(
base: OmniDockLabels,
override?: Partial<OmniDockLabels>,
): OmniDockLabels {
if (!override) return base;
const defined = Object.fromEntries(
Object.entries(override).filter(([, value]) => value !== undefined),
) as Partial<OmniDockLabels>;
return { ...base, ...defined };
}
export function OmniDock({
onSendMessage,
activeAgentName,
labels,
models,
model,
onModelChange,
quickCommands,
}: OmniDockProps) {
const D = usePatternLabels('omniDock', OMNI_DOCK_DEFAULT_LABELS);
const text = resolveLabels(D, labels);
const [input, setInput] = useState('');
const [internalModelId, setInternalModelId] = useState<string | null>(null);
const [pulseActive, setPulseActive] = useState(false);
const isControlled = model !== undefined;
const activeModelId = isControlled ? model : internalModelId;
const resolvedModels: OmniDockModel[] =
models ??
DEMO_MODELS.map((demo) => ({
id: demo.id,
label: demo.label,
badge: text[demo.badgeKey],
}));
// 受控值 / 内部值都可能指向已不存在的模型 → 回退第一个。
const selectedModel =
resolvedModels.find((item) => item.id === activeModelId) ?? resolvedModels[0];
const selectedModelId = selectedModel?.id ?? '';
const resolvedQuickCommands: OmniDockQuickCommand[] =
quickCommands ?? [
{ label: text.quickPlan, value: text.quickPlanCommand },
{ label: text.quickVerify, value: text.quickVerifyCommand },
];
const handleModelChange = (id: string) => {
if (!isControlled) setInternalModelId(id);
onModelChange?.(id);
};
const handleSubmit = (e: FormEvent) => {
e.preventDefault();
const trimmed = input.trim();
if (!trimmed) return;
// 触发声光能量波纹反馈
setPulseActive(true);
setTimeout(() => setPulseActive(false), 800);
onSendMessage(trimmed, selectedModelId);
setInput('');
};
const handleQuickCommand = (value: string) => {
if (!value) return;
onSendMessage(value, selectedModelId);
};
return (
// 2026-10-02 布局重排:原为 `fixed bottom-6` 悬浮层(必然叠压内容),改为页面
// flex column 的**文档流**尾部——页面各分区各管各的高度,坞不再需要"让位"补偿。
// 主题切换 / 返回控制面 / ESC 与页头重复,已收编回页头(ESC 监听随移页面级)。
<div className="mx-auto flex w-full max-w-3xl shrink-0 flex-col items-center gap-1.5 px-6 pb-4">
{/* 顶部悬浮快捷建议条(坞内只留与**输入**同语境的事:附带上文与快捷指令) */}
<div className="flex w-full items-center px-4 text-xs">
<div className="flex items-center gap-2">
{/*
徽章写的必须是**真的会发生的事**。原文案「定向协同: @小码」承诺了两件
都没发生的事:①消息并没有定向给小码(`dispatch` 是把内容交给 CLI 执行,
不指定执行者);②小码的状态当时根本没进派发内容(AI 不知道"它"是谁)。
现在只承诺切实做到的那一件:发送时把这位同事的当前状态**附带**上去。
*/}
{activeAgentName ? (
<span
className="flex items-center gap-1 rounded-full bg-accent-purple-light/50 px-2 py-0.5 font-mono text-xs text-accent-purple"
title={text.contextOnTitle}
>
<Bot className="size-3" />
<span>{text.contextOn(activeAgentName)}</span>
</span>
) : (
<span
className="font-mono text-xs text-muted-foreground"
title={text.contextOffTitle}
>
{text.contextOff}
</span>
)}
{/* 快捷指令 */}
<div className="ms-2 hidden items-center gap-1.5 sm:flex">
{resolvedQuickCommands.map((command) => (
<Button
key={`${command.value}-${command.label}`}
type="button"
variant="subtle"
size="xs"
className="font-mono"
onClick={() => handleQuickCommand(command.value)}
>
{command.label}
</Button>
))}
</div>
</div>
</div>
{/* Dock 主舱体 */}
<form
onSubmit={handleSubmit}
className={cn(
'relative flex w-full items-center gap-2 rounded-xl border border-foreground/10 bg-background/95 p-2 backdrop-blur-2xl transition-[background-color,border-color,box-shadow] duration-slow',
!pulseActive && 'shadow-xs',
)}
>
{/* 模型切换胶囊(ui/select:combobox + listbox,键盘与焦点由 ui 负责) */}
{resolvedModels.length > 0 ? (
<Select
value={selectedModelId}
onValueChange={(next) => handleModelChange(String(next))}
items={resolvedModels.map((item) => ({
value: item.id,
label: item.label,
}))}
>
<SelectTrigger
size="sm"
aria-label={text.modelSelect}
className="h-control-sm max-w-40 shrink-0 rounded-xl border-0 bg-foreground/5 px-3 text-xs font-medium shadow-none"
>
<Sparkles aria-hidden className="size-3.5 shrink-0 text-accent-purple" />
<SelectValue />
</SelectTrigger>
<SelectContent
side="top"
align="start"
aria-label={text.modelMenu}
className="w-56 rounded-xl"
>
{resolvedModels.map((item) => (
<SelectItem key={item.id} value={item.id} className="text-xs">
<span className="truncate">{item.label}</span>
{item.badge ? (
<span className="rounded-sm bg-foreground/10 px-1.5 py-0.5 font-mono text-2xs text-muted-foreground">
{item.badge}
</span>
) : null}
</SelectItem>
))}
</SelectContent>
</Select>
) : null}
{/* 自然语言输入框 */}
<div className="relative flex-1">
<input
type="text"
value={input}
onChange={(e) => setInput(e.target.value)}
placeholder={text.inputPlaceholder}
className="w-full rounded-md border-none bg-transparent px-3 py-2 text-sm font-normal text-foreground outline-none placeholder:text-muted-foreground/50 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-ring"
/>
</div>
{/* 发送按钮 */}
<Button
type="submit"
disabled={!input.trim()}
aria-label={text.send}
size="icon"
className={cn(
'shrink-0 rounded-xl transition-transform duration-normal',
// 文字色随底走:可用态底是 accent-purple(→ primary-foreground),
// 禁用态底是 foreground/10 中性微叠层(未禁用时由 Button disabled 态统一降透明)。
input.trim()
? 'scale-100 bg-accent-purple text-primary-foreground shadow-xs hover:scale-105 hover:bg-accent-purple/90'
: 'bg-foreground/10 text-muted-foreground',
)}
>
<Send aria-hidden className="size-4" />
</Button>
</form>
</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。
用法说明
底部协同输入坞:上下文徽标 + 快捷指令 + 模型切换 + 自然语言输入;onSendMessage(text, model) 由消费方接派发。