2026-05-08 00:41:09 +08:00
|
|
|
|
import { useState, useRef, useEffect } from 'react';
|
|
|
|
|
|
import { Terminal, Image, Play, FileText, ArrowUp } from 'lucide-react';
|
2026-05-07 02:41:01 +08:00
|
|
|
|
|
2026-05-07 04:09:00 +08:00
|
|
|
|
const SLASH_COMMANDS = [
|
|
|
|
|
|
{ cmd: '/run', desc: '执行 pipeline 阶段', icon: Play },
|
|
|
|
|
|
{ cmd: '/status', desc: '查看管线进度', icon: Terminal },
|
|
|
|
|
|
{ cmd: '/images', desc: '生成图片', icon: Image },
|
|
|
|
|
|
{ cmd: '/list', desc: '列出可用账号', icon: FileText },
|
|
|
|
|
|
{ cmd: '/help', desc: '显示帮助', icon: Terminal },
|
|
|
|
|
|
];
|
|
|
|
|
|
|
2026-05-07 03:22:15 +08:00
|
|
|
|
export function ChatInput({ onSend, disabled }: { onSend: (content: string) => void; disabled?: boolean }) {
|
2026-05-07 02:41:01 +08:00
|
|
|
|
const [input, setInput] = useState('');
|
2026-05-07 04:09:00 +08:00
|
|
|
|
const [showCmds, setShowCmds] = useState(false);
|
|
|
|
|
|
const [cmdIdx, setCmdIdx] = useState(0);
|
2026-05-07 02:41:01 +08:00
|
|
|
|
const ref = useRef<HTMLTextAreaElement>(null);
|
|
|
|
|
|
|
2026-05-07 04:09:00 +08:00
|
|
|
|
const matchingCmds = input.startsWith('/')
|
|
|
|
|
|
? SLASH_COMMANDS.filter((c) => c.cmd.startsWith(input.split(' ')[0]))
|
|
|
|
|
|
: [];
|
|
|
|
|
|
|
2026-05-08 00:41:09 +08:00
|
|
|
|
// Auto-show/hide slash command menu
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
if (input.startsWith('/') && matchingCmds.length > 0) {
|
|
|
|
|
|
setShowCmds(true);
|
|
|
|
|
|
setCmdIdx(0);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
setShowCmds(false);
|
|
|
|
|
|
}
|
|
|
|
|
|
}, [input, matchingCmds.length]);
|
|
|
|
|
|
|
2026-05-07 02:41:01 +08:00
|
|
|
|
const handleSend = () => {
|
2026-05-07 04:14:16 +08:00
|
|
|
|
if (!input.trim() || disabled) return;
|
2026-05-07 02:41:01 +08:00
|
|
|
|
onSend(input.trim());
|
|
|
|
|
|
setInput('');
|
2026-05-07 04:14:16 +08:00
|
|
|
|
setShowCmds(false);
|
2026-05-07 02:41:01 +08:00
|
|
|
|
ref.current?.focus();
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const handleKeyDown = (e: React.KeyboardEvent) => {
|
2026-05-07 04:09:00 +08:00
|
|
|
|
if (showCmds && matchingCmds.length > 0) {
|
2026-05-07 04:14:16 +08:00
|
|
|
|
if (e.key === 'Tab') { e.preventDefault(); setInput(matchingCmds[cmdIdx % matchingCmds.length].cmd + ' '); setShowCmds(false); return; }
|
2026-05-07 04:09:00 +08:00
|
|
|
|
if (e.key === 'ArrowDown') { e.preventDefault(); setCmdIdx((i) => (i + 1) % matchingCmds.length); return; }
|
|
|
|
|
|
if (e.key === 'ArrowUp') { e.preventDefault(); setCmdIdx((i) => (i - 1 + matchingCmds.length) % matchingCmds.length); return; }
|
|
|
|
|
|
}
|
|
|
|
|
|
if (e.key === 'Escape') { setShowCmds(false); return; }
|
2026-05-07 02:41:01 +08:00
|
|
|
|
if (e.key === 'Enter' && !e.shiftKey) {
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
handleSend();
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2026-05-07 04:14:16 +08:00
|
|
|
|
const canSend = input.trim().length > 0 && !disabled;
|
|
|
|
|
|
|
2026-05-07 02:41:01 +08:00
|
|
|
|
return (
|
2026-05-07 04:09:00 +08:00
|
|
|
|
<div className="relative">
|
2026-05-07 04:14:16 +08:00
|
|
|
|
{/* Slash command menu */}
|
2026-05-07 04:09:00 +08:00
|
|
|
|
{showCmds && matchingCmds.length > 0 && (
|
2026-05-07 04:14:16 +08:00
|
|
|
|
<div className="mx-6 mb-0.5 bg-white border border-zinc-200 rounded-xl shadow-lg overflow-hidden animate-in slide-in-from-bottom-2">
|
2026-05-07 04:09:00 +08:00
|
|
|
|
{matchingCmds.map((c, i) => {
|
|
|
|
|
|
const Icon = c.icon;
|
|
|
|
|
|
return (
|
|
|
|
|
|
<button
|
|
|
|
|
|
key={c.cmd}
|
|
|
|
|
|
onClick={() => { setInput(c.cmd + ' '); setShowCmds(false); ref.current?.focus(); }}
|
2026-05-07 04:14:16 +08:00
|
|
|
|
className={`w-full flex items-center gap-2.5 px-3.5 py-2 text-sm transition-colors ${
|
2026-05-07 04:09:00 +08:00
|
|
|
|
i === (cmdIdx % matchingCmds.length) ? 'bg-indigo-50 text-indigo-700' : 'text-zinc-600 hover:bg-zinc-50'
|
|
|
|
|
|
}`}
|
|
|
|
|
|
>
|
2026-05-07 04:14:16 +08:00
|
|
|
|
<Icon size={15} className="text-zinc-400" />
|
|
|
|
|
|
<span className="font-mono font-semibold text-xs">{c.cmd}</span>
|
|
|
|
|
|
<span className="text-zinc-400 text-xs">{c.desc}</span>
|
2026-05-07 04:09:00 +08:00
|
|
|
|
</button>
|
|
|
|
|
|
);
|
|
|
|
|
|
})}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
)}
|
2026-05-07 04:14:16 +08:00
|
|
|
|
|
|
|
|
|
|
{/* Input bar */}
|
|
|
|
|
|
<div className="px-4 pb-4 pt-2 bg-gradient-to-t from-white via-white to-transparent">
|
|
|
|
|
|
<div className={`
|
|
|
|
|
|
flex items-end gap-2 bg-zinc-50 rounded-2xl border transition-all duration-200 px-4 py-3
|
|
|
|
|
|
${disabled ? 'border-zinc-200 opacity-60' : 'border-zinc-200 focus-within:border-indigo-300 focus-within:shadow-md focus-within:shadow-indigo-50 focus-within:bg-white'}
|
|
|
|
|
|
`}>
|
2026-05-07 04:09:00 +08:00
|
|
|
|
<textarea
|
|
|
|
|
|
ref={ref}
|
|
|
|
|
|
value={input}
|
|
|
|
|
|
onChange={(e) => setInput(e.target.value)}
|
|
|
|
|
|
onKeyDown={handleKeyDown}
|
|
|
|
|
|
rows={1}
|
2026-05-07 04:14:16 +08:00
|
|
|
|
placeholder={disabled ? '等待回复中...' : '输入消息,或按 / 查看快捷命令...'}
|
|
|
|
|
|
className="flex-1 bg-transparent text-sm resize-none outline-none placeholder:text-zinc-400 text-zinc-700 min-h-[24px] max-h-[120px]"
|
2026-05-07 04:09:00 +08:00
|
|
|
|
disabled={disabled}
|
2026-05-07 04:14:16 +08:00
|
|
|
|
style={{ height: 'auto' }}
|
|
|
|
|
|
onInput={(e) => {
|
|
|
|
|
|
const el = e.currentTarget;
|
|
|
|
|
|
el.style.height = 'auto';
|
|
|
|
|
|
el.style.height = Math.min(el.scrollHeight, 120) + 'px';
|
|
|
|
|
|
}}
|
2026-05-07 04:09:00 +08:00
|
|
|
|
/>
|
2026-05-07 04:14:16 +08:00
|
|
|
|
<button
|
|
|
|
|
|
onClick={handleSend}
|
|
|
|
|
|
disabled={!canSend}
|
|
|
|
|
|
className={`
|
|
|
|
|
|
w-8 h-8 rounded-xl flex items-center justify-center transition-all flex-shrink-0
|
|
|
|
|
|
${canSend
|
|
|
|
|
|
? 'bg-indigo-600 text-white hover:bg-indigo-700 shadow-sm'
|
|
|
|
|
|
: 'bg-zinc-200 text-zinc-400 cursor-not-allowed'}
|
|
|
|
|
|
`}
|
|
|
|
|
|
>
|
|
|
|
|
|
<ArrowUp size={16} strokeWidth={2.5} />
|
|
|
|
|
|
</button>
|
2026-05-07 04:09:00 +08:00
|
|
|
|
</div>
|
2026-05-07 04:14:16 +08:00
|
|
|
|
<p className="text-[10px] text-zinc-400 text-center mt-1.5">
|
|
|
|
|
|
美图 Agent 可能会产生错误内容,请核实重要信息
|
|
|
|
|
|
</p>
|
2026-05-07 02:41:01 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|