2026-05-07 03:48:14 +08:00
|
|
|
|
import { useEffect, useState, useCallback, useRef } from 'react';
|
|
|
|
|
|
import { useParams, useNavigate } from 'react-router-dom';
|
2026-05-07 02:41:01 +08:00
|
|
|
|
import { useAppStore } from '@/store';
|
|
|
|
|
|
import { useChat } from '@/hooks/useChat';
|
|
|
|
|
|
import { ChatMessage } from './ChatMessage';
|
|
|
|
|
|
import { ChatInput } from './ChatInput';
|
2026-05-07 04:09:00 +08:00
|
|
|
|
import { PipelineProgress } from './PipelineProgress';
|
2026-05-07 02:46:16 +08:00
|
|
|
|
import { Button } from '@/components/ui/button';
|
2026-05-07 04:09:00 +08:00
|
|
|
|
import { RefreshCw, Loader2, StopCircle, X } from 'lucide-react';
|
2026-05-07 03:48:14 +08:00
|
|
|
|
import { api } from '@/lib/api';
|
2026-05-07 04:09:00 +08:00
|
|
|
|
import type { Account, Message } from '@/types';
|
2026-05-07 02:41:01 +08:00
|
|
|
|
|
2026-05-07 02:24:43 +08:00
|
|
|
|
export function ChatView() {
|
2026-05-07 03:48:14 +08:00
|
|
|
|
const { conversationId } = useParams<{ conversationId?: string }>();
|
|
|
|
|
|
const navigate = useNavigate();
|
2026-05-07 23:48:26 +08:00
|
|
|
|
const { setConversations, selectedAccountId } = useAppStore();
|
|
|
|
|
|
const { messages, connected, thinking, toolStatus, pipeline, send, stop, createConversation, removeMessage } = useChat(conversationId || null);
|
2026-05-07 02:46:16 +08:00
|
|
|
|
const [manifestPath, setManifestPath] = useState<string | null>(null);
|
2026-05-07 03:48:14 +08:00
|
|
|
|
const [accounts, setAccounts] = useState<Account[]>([]);
|
2026-05-07 04:09:00 +08:00
|
|
|
|
const [quote, setQuote] = useState<string | null>(null);
|
2026-05-07 23:48:26 +08:00
|
|
|
|
const [pendingMessage, setPendingMessage] = useState<string | null>(null);
|
2026-05-07 03:48:14 +08:00
|
|
|
|
const creatingRef = useRef(false);
|
|
|
|
|
|
|
2026-05-07 04:09:00 +08:00
|
|
|
|
useEffect(() => { api.listAccounts().then(setAccounts).catch(() => {}); }, []);
|
2026-05-07 02:41:01 +08:00
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
2026-05-07 23:48:26 +08:00
|
|
|
|
api.listConversations().then(setConversations).catch(() => {});
|
2026-05-07 02:41:01 +08:00
|
|
|
|
}, [messages]);
|
|
|
|
|
|
|
2026-05-07 02:46:16 +08:00
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
const toolMsgs = messages.filter((m) => m.role === 'tool');
|
|
|
|
|
|
if (toolMsgs.length > 0) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const lastTool = JSON.parse(toolMsgs[toolMsgs.length - 1].content);
|
|
|
|
|
|
if (lastTool.manifest) setManifestPath(lastTool.manifest);
|
|
|
|
|
|
} catch {}
|
|
|
|
|
|
}
|
|
|
|
|
|
}, [messages]);
|
|
|
|
|
|
|
2026-05-07 23:48:26 +08:00
|
|
|
|
// After navigating to a new conversation, send the pending message
|
2026-05-07 03:48:14 +08:00
|
|
|
|
useEffect(() => {
|
2026-05-07 23:48:26 +08:00
|
|
|
|
if (conversationId && connected && pendingMessage && messages.length === 0) {
|
|
|
|
|
|
send(pendingMessage);
|
|
|
|
|
|
setPendingMessage(null);
|
2026-05-07 03:48:14 +08:00
|
|
|
|
}
|
2026-05-07 23:48:26 +08:00
|
|
|
|
}, [conversationId, connected, pendingMessage]);
|
2026-05-07 02:41:01 +08:00
|
|
|
|
|
2026-05-07 02:46:16 +08:00
|
|
|
|
const handleResume = async () => {
|
|
|
|
|
|
if (!manifestPath) return;
|
2026-05-07 04:09:00 +08:00
|
|
|
|
await fetch('/api/pipeline/resume', {
|
|
|
|
|
|
method: 'POST', headers: { 'Content-Type': 'application/json' },
|
|
|
|
|
|
body: JSON.stringify({ manifest: manifestPath }),
|
|
|
|
|
|
});
|
2026-05-07 02:46:16 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
2026-05-07 04:09:00 +08:00
|
|
|
|
const findPrecedingUser = useCallback((msgId: string): Message | null => {
|
|
|
|
|
|
const idx = messages.findIndex((m) => m.id === msgId);
|
|
|
|
|
|
if (idx <= 0) return null;
|
|
|
|
|
|
for (let i = idx - 1; i >= 0; i--) {
|
|
|
|
|
|
if (messages[i].role === 'user') return messages[i];
|
|
|
|
|
|
}
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}, [messages]);
|
|
|
|
|
|
|
|
|
|
|
|
const handleRegenerate = useCallback((msgId: string) => {
|
|
|
|
|
|
const prev = findPrecedingUser(msgId);
|
|
|
|
|
|
if (prev) send(prev.content);
|
|
|
|
|
|
}, [findPrecedingUser, send]);
|
|
|
|
|
|
|
|
|
|
|
|
const handleContinue = useCallback(() => { send('请继续'); }, [send]);
|
|
|
|
|
|
const handleQuote = useCallback((content: string) => { setQuote(content.slice(0, 200)); }, []);
|
2026-05-07 23:48:26 +08:00
|
|
|
|
const handleDeleteMsg = useCallback((msgId: string) => {
|
|
|
|
|
|
removeMessage(msgId);
|
|
|
|
|
|
}, [removeMessage]);
|
|
|
|
|
|
|
|
|
|
|
|
// Delayed conversation creation
|
|
|
|
|
|
const handleSendNew = useCallback(async (content: string) => {
|
|
|
|
|
|
if (creatingRef.current) return;
|
|
|
|
|
|
creatingRef.current = true;
|
|
|
|
|
|
setPendingMessage(content);
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
createConversation(content.slice(0, 30), selectedAccountId || undefined);
|
|
|
|
|
|
setTimeout(async () => {
|
|
|
|
|
|
const list = await api.listConversations();
|
|
|
|
|
|
setConversations(list);
|
|
|
|
|
|
if (list.length > 0) {
|
|
|
|
|
|
navigate(`/chat/${list[0].id}`);
|
|
|
|
|
|
}
|
|
|
|
|
|
creatingRef.current = false;
|
|
|
|
|
|
}, 600);
|
|
|
|
|
|
} catch {
|
|
|
|
|
|
creatingRef.current = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}, [createConversation, selectedAccountId]);
|
2026-05-07 04:09:00 +08:00
|
|
|
|
|
|
|
|
|
|
const handleSend = useCallback((content: string) => {
|
2026-05-07 23:48:26 +08:00
|
|
|
|
if (quote) { content = `> ${quote}\n\n${content}`; setQuote(null); }
|
|
|
|
|
|
if (conversationId) {
|
|
|
|
|
|
send(content);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
handleSendNew(content);
|
|
|
|
|
|
}
|
|
|
|
|
|
}, [send, quote, conversationId]);
|
2026-05-07 04:09:00 +08:00
|
|
|
|
|
|
|
|
|
|
const handleStop = useCallback(() => { stop(); }, [stop]);
|
|
|
|
|
|
|
2026-05-07 23:48:26 +08:00
|
|
|
|
// Empty state - no conversation selected
|
2026-05-07 03:48:14 +08:00
|
|
|
|
if (!conversationId) {
|
2026-05-07 02:41:01 +08:00
|
|
|
|
return (
|
2026-05-07 23:48:26 +08:00
|
|
|
|
<div className="flex-1 flex flex-col items-center justify-center gap-4 bg-white">
|
2026-05-07 03:48:14 +08:00
|
|
|
|
<div className="text-4xl mb-2">💬</div>
|
|
|
|
|
|
<h2 className="text-lg font-semibold text-zinc-800">开始新对话</h2>
|
2026-05-07 23:48:26 +08:00
|
|
|
|
<p className="text-sm text-zinc-500">输入消息开始创作,对话将自动创建</p>
|
|
|
|
|
|
<div className="w-full max-w-xl px-4 mt-4">
|
|
|
|
|
|
<ChatInput onSend={handleSend} disabled={thinking || creatingRef.current} />
|
|
|
|
|
|
</div>
|
|
|
|
|
|
{accounts.length > 0 && (
|
|
|
|
|
|
<div className="flex items-center gap-2 mt-3">
|
|
|
|
|
|
<span className="text-xs text-zinc-400">当前账号:</span>
|
2026-05-07 03:48:14 +08:00
|
|
|
|
<select
|
|
|
|
|
|
value={selectedAccountId || ''}
|
2026-05-07 23:48:26 +08:00
|
|
|
|
onChange={(e) => useAppStore.getState().setSelectedAccountId(e.target.value || null)}
|
|
|
|
|
|
className="h-7 rounded border border-zinc-200 bg-zinc-50 px-2 text-xs text-zinc-600"
|
2026-05-07 03:48:14 +08:00
|
|
|
|
>
|
2026-05-07 23:48:26 +08:00
|
|
|
|
<option value="">不指定</option>
|
2026-05-07 04:09:00 +08:00
|
|
|
|
{accounts.map((a) => <option key={a.id} value={a.id}>{a.name}</option>)}
|
2026-05-07 03:48:14 +08:00
|
|
|
|
</select>
|
2026-05-07 23:48:26 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
)}
|
2026-05-07 02:41:01 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-07 23:48:26 +08:00
|
|
|
|
// Active conversation
|
2026-05-07 02:41:01 +08:00
|
|
|
|
return (
|
2026-05-07 23:48:26 +08:00
|
|
|
|
<div className="flex-1 flex flex-col bg-white h-full">
|
|
|
|
|
|
{/* Header bar */}
|
|
|
|
|
|
<div className="px-4 py-2 border-b border-zinc-200 flex items-center justify-between flex-shrink-0">
|
|
|
|
|
|
<div className="flex items-center gap-1.5">
|
|
|
|
|
|
<div className={`w-1.5 h-1.5 rounded-full ${connected ? 'bg-green-500' : 'bg-red-400'}`} />
|
|
|
|
|
|
<span className="text-xs text-zinc-400">{connected ? '在线' : '连接中'}</span>
|
2026-05-07 02:46:16 +08:00
|
|
|
|
</div>
|
2026-05-07 04:09:00 +08:00
|
|
|
|
<div className="flex items-center gap-2">
|
|
|
|
|
|
{manifestPath && (
|
|
|
|
|
|
<Button size="sm" variant="outline" className="h-7 text-xs" onClick={handleResume}>
|
|
|
|
|
|
<RefreshCw size={12} className="mr-1" />断点续跑
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
)}
|
|
|
|
|
|
{thinking && (
|
|
|
|
|
|
<Button size="sm" variant="outline" className="h-7 text-xs text-red-500 border-red-200 hover:bg-red-50" onClick={handleStop}>
|
|
|
|
|
|
<StopCircle size={12} className="mr-1" />停止
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
)}
|
|
|
|
|
|
</div>
|
2026-05-07 02:41:01 +08:00
|
|
|
|
</div>
|
2026-05-07 03:22:15 +08:00
|
|
|
|
|
2026-05-07 23:48:26 +08:00
|
|
|
|
{/* Chat area */}
|
|
|
|
|
|
<div className="flex-1 overflow-hidden">
|
|
|
|
|
|
<div className="h-full flex flex-col">
|
|
|
|
|
|
{/* Message list */}
|
|
|
|
|
|
<div className="flex-1 overflow-y-auto px-4 py-4">
|
|
|
|
|
|
{messages.map((msg, i) => (
|
|
|
|
|
|
msg.role !== 'tool' && (
|
|
|
|
|
|
<ChatMessage
|
|
|
|
|
|
key={msg.id}
|
|
|
|
|
|
message={msg}
|
|
|
|
|
|
isLast={i === messages.length - 1}
|
|
|
|
|
|
isThinking={thinking}
|
|
|
|
|
|
onRegenerate={msg.role === 'assistant' ? handleRegenerate : undefined}
|
|
|
|
|
|
onContinue={msg.role === 'assistant' ? handleContinue : undefined}
|
|
|
|
|
|
onQuote={handleQuote}
|
|
|
|
|
|
onDelete={handleDeleteMsg}
|
|
|
|
|
|
/>
|
|
|
|
|
|
)
|
|
|
|
|
|
))}
|
|
|
|
|
|
|
|
|
|
|
|
{pipeline && (
|
|
|
|
|
|
<PipelineProgress
|
|
|
|
|
|
phase={pipeline.phase}
|
|
|
|
|
|
progress={pipeline.progress}
|
|
|
|
|
|
currentItem={pipeline.currentItem}
|
|
|
|
|
|
totalItems={pipeline.totalItems}
|
|
|
|
|
|
status={pipeline.status}
|
|
|
|
|
|
/>
|
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
|
|
{thinking && !pipeline && (
|
|
|
|
|
|
<div className="flex items-center gap-2 text-zinc-400 text-sm py-2">
|
|
|
|
|
|
<Loader2 size={14} className="animate-spin" />
|
|
|
|
|
|
{toolStatus || '思考中...'}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
)}
|
2026-05-07 03:22:15 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
|
2026-05-07 23:48:26 +08:00
|
|
|
|
{/* Quote bar */}
|
|
|
|
|
|
{quote && (
|
|
|
|
|
|
<div className="mx-4 px-3 py-1.5 bg-zinc-50 border border-zinc-200 rounded-lg flex items-center gap-2 text-xs text-zinc-500 flex-shrink-0">
|
|
|
|
|
|
<span className="flex-1 truncate">引用: {quote}</span>
|
|
|
|
|
|
<button onClick={() => setQuote(null)} className="text-zinc-400 hover:text-zinc-600"><X size={12} /></button>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
)}
|
2026-05-07 04:09:00 +08:00
|
|
|
|
|
2026-05-07 23:48:26 +08:00
|
|
|
|
{/* Input */}
|
|
|
|
|
|
<ChatInput onSend={handleSend} disabled={thinking} />
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
2026-05-07 02:41:01 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
);
|
2026-05-07 02:24:43 +08:00
|
|
|
|
}
|