import { useEffect, useRef, useState } from 'react' import { Menu, Send, Square, Wrench, Copy, Trash2, RotateCcw, Brain, Loader2, Check, PanelLeftOpen, Plus, } from 'lucide-react' import { useStore, useActiveConversation, useActiveProject } from '../store/useStore' import Markdown from './Markdown' function CopyBtn({ text }) { const [ok, setOk] = useState(false) return ( ) } function ToolCallBlock({ call, result }) { let args = call.function?.arguments || '' try { args = JSON.stringify(JSON.parse(args), null, 2) } catch { /* 串流中可能還不完整 */ } const pending = !result || result.pending return (
{pending ? : } {call.function?.name || '…'} {pending ? '執行中…' : String(result.content).slice(0, 60).replace(/\s+/g, ' ')}
參數
{args || '{}'}
{result && !result.pending && (
結果
{String(result.content)}
)}
) } function AssistantMessage({ msg, results, onDelete, agentIcon }) { const isError = msg.error const isInfo = msg.info return (
{agentIcon || '🤖'}
{msg.reasoning && (
推理過程
{msg.reasoning}
)} {(msg.content || (msg.streaming && !msg.tool_calls?.length && !msg.reasoning)) && (
{msg.content ? : 思考中}
)} {(msg.tool_calls || []).map((c, i) => )} {!msg.streaming && (
{msg.content && } {msg.model && {msg.model}}
)}
) } export default function ChatView({ onOpenSidebar, desktopCollapsed, onExpandDesktop }) { const conv = useActiveConversation() const project = useActiveProject() const agents = useStore((s) => s.agents) const providers = useStore((s) => s.providers) const running = useStore((s) => (conv ? s.running[conv.id] : 0)) const { send, stop, regenerate, deleteMessage, createConversation } = useStore.getState() const [input, setInput] = useState('') const scrollRef = useRef(null) const stickRef = useRef(true) const taRef = useRef(null) const agent = agents.find((a) => a.id === conv?.agentId) const provider = providers.find((p) => p.id === conv?.providerId) // 使用者沒往上捲時自動捲到底 useEffect(() => { const el = scrollRef.current if (el && stickRef.current) el.scrollTop = el.scrollHeight }, [conv?.messages]) useEffect(() => { const ta = taRef.current if (!ta) return ta.style.height = 'auto' ta.style.height = `${Math.min(ta.scrollHeight, 220)}px` }, [input]) const submit = async () => { const text = input.trim() if (!text || running) return let id = conv?.id if (!id) id = createConversation() setInput('') stickRef.current = true await useStore.getState().send(id, text) } const results = {} ;(conv?.messages || []).forEach((m) => { if (m.role === 'tool') results[m.tool_call_id] = m }) const lastIsAssistant = conv?.messages?.length && conv.messages[conv.messages.length - 1].role !== 'user' return (
{/* 頂列:只有開關側邊欄與標題,所有設定都在側邊欄 */}
{desktopCollapsed && ( )}
{conv?.title || '新對話'}
{project?.name} · {agent ? `${agent.icon} ${agent.name}` : '未選 Agent'} · {provider ? `${provider.name} / ${conv?.model || '未選模型'}` : '未選供應商'}
{ const el = e.currentTarget stickRef.current = el.scrollHeight - el.scrollTop - el.clientHeight < 80 }} className="flex-1 overflow-y-auto" >
{(!conv || conv.messages.length === 0) && (
{agent?.icon || '💬'}
{agent?.name || '開始新對話'}

{agent?.description || '輸入訊息開始'}

專案「{project?.name}」· 可用工具 {agent?.tools?.length || 0} 個

{!conv && ( )}
)} {(conv?.messages || []).map((m) => { if (m.role === 'tool') return null // 結果併入對應的工具呼叫區塊顯示 if (m.role === 'user') { return (
{m.content}
{!running && ( )}
) } return ( a.id === m.agentId)?.icon || agent?.icon} onDelete={() => !running && deleteMessage(conv.id, m.id)} /> ) })} {running > 0 && (
第 {running} 步執行中(不設逾時,可隨時停止)
)} {!running && lastIsAssistant && (
)}