import { invoke } from "@tauri-apps/api/core"; import { listen } from "@tauri-apps/api/event"; import { useCallback, useEffect, useMemo, useRef, useState } from "react"; import { errorMessage } from "../../lib/errorMessage"; import { MessageContent } from "./MessageContent"; import { astrbotAttachmentSchema, astrbotCapabilitySnapshotSchema, boundRuntimeEventSchema, chatSessionSchema, connectionRuntimeStatusSchema, type AstrBotAttachment, type AstrBotCapabilitySnapshot, type BoundRuntimeEvent, type ChatSession, mergeLoadedChatMessages, remoteChatSessionSchema, type ChatMessage, type ChatSlot, type RemoteChatSession, type ConnectionRuntimeStatus, storedChatMessageSchema, slotRuntimeSnapshotSchema, } from "./types"; const emptyStatus: ConnectionRuntimeStatus = { companion: { slot: "companion", state: "disconnected" }, operations: { slot: "operations", state: "disconnected" }, }; function payloadString( event: BoundRuntimeEvent, key: string, ): string | undefined { if (!event.payload || typeof event.payload !== "object") return undefined; const value = (event.payload as Record)[key]; return typeof value === "string" ? value : undefined; } export interface ChatPanelProps { fixedSlot?: ChatSlot; } type SpeechRecognitionLike = { lang: string; interimResults: boolean; onresult: ((event: unknown) => void) | null; onerror: (() => void) | null; onend: (() => void) | null; start: () => void; stop: () => void; }; export function ChatPanel({ fixedSlot }: ChatPanelProps) { const [status, setStatus] = useState(emptyStatus); const [messages, setMessages] = useState([]); const [remoteSessions, setRemoteSessions] = useState([]); const [storedSessions, setStoredSessions] = useState< Record >({ companion: [], operations: [], }); const [activeSlot, setActiveSlot] = useState( fixedSlot ?? "companion", ); const selectedSlot = fixedSlot ?? activeSlot; const selectedSnapshot = status[selectedSlot]; const selectedProfileId = selectedSnapshot.profileId; const selectedServerInstanceId = selectedSnapshot.serverInstanceId; const selectedSessionId = selectedSnapshot.activeSessionId; const historySessionId = selectedSessionId ?? storedSessions[selectedSlot][0]?.sessionId; const selectedTransport = selectedSnapshot.transport; const [draft, setDraft] = useState>({ companion: "", operations: "", }); // Two separate errors on purpose. `refresh()` runs every 500ms and used to // write the shared error on failure and clear it on success, so any send or // upload error was wiped within half a second. The polling failure describes // the IPC channel, not a slot, so it keeps its own state and the per-slot // error is owned solely by user actions. const [runtimeError, setRuntimeError] = useState(null); const [slotError, setSlotError] = useState>({ companion: null, operations: null, }); const [capabilities, setCapabilities] = useState(null); const [pendingAttachments, setPendingAttachments] = useState< Record >({ companion: [], operations: [], }); const [uploading, setUploading] = useState(false); const [voiceListening, setVoiceListening] = useState(false); const [sending, setSending] = useState>({ companion: false, operations: false, }); const messageListRef = useRef(null); /// Whether the view should follow new content. /// /// Starts true so opening a conversation lands on the newest message, and /// stays true until the reader scrolls up — following unconditionally would /// yank someone out of the history they were reading. const followTail = useRef(true); const speechRecognition = useRef<{ start: () => void; stop: () => void; } | null>(null); const [unreadCounts, setUnreadCounts] = useState>({ companion: 0, operations: 0, }); const selectedAttachments = pendingAttachments[selectedSlot]; // A slot-scoped action error wins over the runtime-wide polling error. const visibleError = slotError[selectedSlot] ?? runtimeError; const activeMessages = useMemo( () => messages.filter( (item) => item.slot === selectedSlot && (!status[selectedSlot].activeSessionId || item.sessionId === status[selectedSlot].activeSessionId), ), [messages, selectedSlot, status], ); const visibleMessages = useMemo( () => activeMessages.filter( (message) => message.content.trim().length > 0 || message.streaming || message.error, ), [activeMessages], ); const displaySessions = remoteSessions.length > 0 ? remoteSessions : storedSessions[selectedSlot].map((session) => ({ sessionId: session.sessionId, displayName: undefined, updatedAt: session.lastMessageAt, })); const refresh = useCallback(async () => { try { const result = await invoke("connection_runtime_status"); const parsed = connectionRuntimeStatusSchema.safeParse(result); if (!parsed.success) { setRuntimeError("连接状态暂不可用,请检查连接配置。"); return false; } setStatus(parsed.data); setRuntimeError(null); return true; } catch { setRuntimeError("连接状态暂不可用,请检查本地运行时。"); return false; } }, []); const reportSlotError = (slot: ChatSlot, message: string | null) => { setSlotError((current) => ({ ...current, [slot]: message })); }; /// Distance from the bottom that still counts as "reading the newest". const TAIL_THRESHOLD_PX = 80; const scrollToTail = useCallback((behavior: ScrollBehavior) => { const list = messageListRef.current; if (!list) return; // `scrollTo` carries the smooth behaviour, but it is not universally // available — jsdom has no implementation at all — so fall back to the // property, which every engine supports. if (typeof list.scrollTo === "function") { list.scrollTo({ top: list.scrollHeight, behavior }); } else { list.scrollTop = list.scrollHeight; } }, []); // Follow the tail whenever the rendered messages change: a sent message, a // streamed delta, or a freshly loaded history. The list had no scroll code at // all, so every send left the new message below the fold. useEffect(() => { if (!followTail.current) return; scrollToTail("smooth"); }, [visibleMessages, scrollToTail]); // Opening a conversation, or switching slot or session, starts at the newest // message rather than at the top. useEffect(() => { followTail.current = true; const frame = window.requestAnimationFrame(() => scrollToTail("auto")); return () => window.cancelAnimationFrame(frame); }, [selectedSlot, historySessionId, scrollToTail]); useEffect(() => { // `listen()` resolves after the effect may already have been cleaned up. // Assigning the unregister function unconditionally dropped it in that // case, and under StrictMode's mount/cleanup/mount the first listener // leaked permanently — every delta was then applied twice, doubling each // character of the streamed reply. let active = true; let unlisten: (() => void) | undefined; void listen("connection-runtime-event", (event) => { const parsed = boundRuntimeEventSchema.safeParse(event.payload); if (parsed.success) { setMessages((current) => applyRuntimeEvents(current, [parsed.data])); } }) .then((cleanup) => { if (active) { unlisten = cleanup; } else { cleanup(); } }) .catch(() => undefined); return () => { active = false; unlisten?.(); unlisten = undefined; }; }, []); useEffect(() => { const snapshot = { profileId: selectedProfileId, serverInstanceId: selectedServerInstanceId, activeSessionId: selectedSessionId, transport: selectedTransport, }; if (!snapshot.profileId || !snapshot.serverInstanceId) return; let cancelled = false; void invoke("list_chat_sessions", { request: { profileId: snapshot.profileId, slot: selectedSlot, serverInstanceId: snapshot.serverInstanceId, }, }) .then((result) => { if (cancelled) return; const sessions = chatSessionSchema.array().parse(result); setStoredSessions((current) => ({ ...current, [selectedSlot]: sessions, })); setUnreadCounts((current) => ({ ...current, [selectedSlot]: sessions.reduce( (total, session) => total + session.unreadCount, 0, ), })); }) .catch(() => undefined); return () => { cancelled = true; }; }, [ selectedSlot, selectedProfileId, selectedServerInstanceId, selectedSessionId, historySessionId, selectedTransport, ]); useEffect(() => { const snapshot = { profileId: selectedProfileId, serverInstanceId: selectedServerInstanceId, activeSessionId: selectedSessionId, transport: selectedTransport, }; setRemoteSessions([]); if (!snapshot.profileId) return; let cancelled = false; void invoke("connection_slot_sessions", { request: { slot: selectedSlot }, }) .then((result) => { if (cancelled) return; setRemoteSessions(remoteChatSessionSchema.array().parse(result)); }) .catch(() => undefined); return () => { cancelled = true; }; }, [ selectedSlot, selectedProfileId, selectedServerInstanceId, selectedSessionId, historySessionId, selectedTransport, ]); useEffect(() => { const snapshot = { profileId: selectedProfileId, serverInstanceId: selectedServerInstanceId, activeSessionId: selectedSessionId, transport: selectedTransport, }; setCapabilities(null); if (!snapshot.profileId || snapshot.transport !== "astr-bot-open-api") return; let cancelled = false; void invoke("connection_slot_capabilities", { request: { slot: selectedSlot }, }) .then((result) => { if (cancelled) return; const next = astrbotCapabilitySnapshotSchema.parse(result); setCapabilities(next); }) .catch(() => undefined); return () => { cancelled = true; }; }, [ selectedSlot, selectedProfileId, selectedServerInstanceId, selectedSessionId, historySessionId, selectedTransport, ]); useEffect(() => { const snapshot = { profileId: selectedProfileId, serverInstanceId: selectedServerInstanceId, activeSessionId: historySessionId, transport: selectedTransport, }; if ( !snapshot.profileId || !snapshot.serverInstanceId || !snapshot.activeSessionId ) return; void invoke("mark_chat_session_read", { request: { profileId: snapshot.profileId, slot: selectedSlot, serverInstanceId: snapshot.serverInstanceId, sessionId: snapshot.activeSessionId, }, }).catch(() => undefined); setUnreadCounts((current) => ({ ...current, [selectedSlot]: 0 })); }, [ selectedSlot, selectedProfileId, selectedServerInstanceId, selectedSessionId, historySessionId, selectedTransport, ]); useEffect(() => { const snapshot = { profileId: selectedProfileId, serverInstanceId: selectedServerInstanceId, activeSessionId: historySessionId, transport: selectedTransport, }; if ( !snapshot.profileId || !snapshot.serverInstanceId || !snapshot.activeSessionId ) return; let cancelled = false; void invoke("load_chat_messages", { request: { profileId: snapshot.profileId, slot: selectedSlot, serverInstanceId: snapshot.serverInstanceId, sessionId: snapshot.activeSessionId, limit: 100, }, }) .then((result) => { if (cancelled) return; const records = storedChatMessageSchema.array().parse(result); setMessages((current) => mergeLoadedChatMessages( current, records, selectedSlot, snapshot.activeSessionId, ), ); }) .catch(() => undefined); return () => { cancelled = true; }; }, [ selectedSlot, selectedProfileId, selectedServerInstanceId, selectedSessionId, historySessionId, selectedTransport, ]); useEffect(() => { let timer: number | undefined; void refresh().then((available) => { if (!available) return; timer = window.setInterval(() => { void refresh(); }, 500); }); return () => { if (timer !== undefined) window.clearInterval(timer); }; }, [refresh]); const uploadFiles = async (files: FileList | null) => { if (!files?.length) return; // Pin the slot for the whole action. An upload that resolves after the user // switched tabs must land on the slot it was uploaded to, not the one that // happens to be selected when the promise settles. const slot = selectedSlot; setUploading(true); reportSlotError(slot, null); try { const uploaded: AstrBotAttachment[] = []; for (const file of Array.from(files)) { if (file.size > 16 * 1024 * 1024) throw new Error(`${file.name} 超过 16 MiB 限制`); const bytes = Array.from(new Uint8Array(await file.arrayBuffer())); const result = await invoke("upload_connection_slot_file", { request: { slot, fileName: file.name, mimeType: file.type || "application/octet-stream", bytes, }, }); uploaded.push(astrbotAttachmentSchema.parse(result)); } setPendingAttachments((current) => ({ ...current, [slot]: current[slot].concat(uploaded), })); } catch (reason) { reportSlotError(slot, errorMessage(reason)); } finally { setUploading(false); } }; const send = async (messageOverride?: string) => { const slot = selectedSlot; const slotAttachments = pendingAttachments[slot]; const message = messageOverride?.trim() || draft[slot].trim() || (slotAttachments.length > 0 ? "Please check the attachment." : ""); if (!message || uploading || sending[slot]) return; const id = `local-${Date.now()}`; const attachments = messageOverride ? [] : slotAttachments; setDraft((current) => ({ ...current, [slot]: "" })); if (!messageOverride) { setPendingAttachments((current) => ({ ...current, [slot]: [] })); } setSending((current) => ({ ...current, [slot]: true })); setMessages((current) => current.concat({ id, sessionId: status[slot].activeSessionId, slot, role: "user", content: message, }), ); try { const result = slotRuntimeSnapshotSchema.parse( await invoke("send_connection_slot_message", { request: { slot, message, attachments, }, }), ); setStatus((current) => ({ ...current, [slot]: result })); setMessages((current) => current.map((item) => item.id === id ? { ...item, sessionId: result.activeSessionId } : item, ), ); reportSlotError(slot, null); } catch (reason) { reportSlotError(slot, errorMessage(reason)); setMessages((current) => current.map((item) => item.id === id ? { ...item, error: true } : item, ), ); } finally { setSending((current) => ({ ...current, [slot]: false })); } }; const stopGeneration = async () => { const slot = selectedSlot; try { await invoke("cancel_connection_slot_message", { request: { slot }, }); setSending((current) => ({ ...current, [slot]: false })); setMessages((current) => current.map((item) => item.slot === slot && item.streaming ? { ...item, streaming: false, content: item.content || "Generation stopped.", error: true, } : item, ), ); reportSlotError(slot, null); } catch (reason) { reportSlotError(slot, errorMessage(reason)); } }; const copyMessage = async (content: string) => { const slot = selectedSlot; try { await navigator.clipboard.writeText(content); reportSlotError(slot, null); } catch { reportSlotError(slot, "复制失败,请检查剪贴板权限。"); } }; const retryMessage = (messageId: string) => { const index = messages.findIndex((item) => item.id === messageId); const previousUser = [...messages] .slice(0, index) .reverse() .find((item) => item.role === "user" && item.slot === selectedSlot); if (previousUser) void send(previousUser.content); }; const startNewSession = async () => { const slot = selectedSlot; if (!status[slot].state || status[slot].state === "disconnected") { reportSlotError(slot, "Connect the selected slot first."); return; } try { const sessionId = crypto.randomUUID(); const result = slotRuntimeSnapshotSchema.parse( await invoke("switch_connection_slot_session", { request: { slot, sessionId }, }), ); setMessages((current) => current.filter( (item) => item.slot !== slot || item.sessionId !== status[slot].activeSessionId, ), ); setStatus((current) => ({ ...current, [slot]: result })); reportSlotError(slot, null); } catch (reason) { reportSlotError(slot, errorMessage(reason)); } }; const refreshConnection = async () => { await refresh(); }; const reconnectConnection = async () => { const slot = selectedSlot; const profileId = status[slot].profileId; if (!profileId) { reportSlotError(slot, "No Profile is assigned to the selected slot."); return; } try { const result = slotRuntimeSnapshotSchema.parse( await invoke("probe_connection_slot", { request: { slot, profileId }, }), ); setStatus((current) => ({ ...current, [slot]: result })); reportSlotError(slot, null); } catch (reason) { reportSlotError(slot, errorMessage(reason)); } }; const disconnectConnection = async () => { const slot = selectedSlot; try { await invoke("disconnect_connection_slot", { request: { slot }, }); await refresh(); } catch (reason) { reportSlotError(slot, errorMessage(reason)); } }; const toggleVoiceInput = () => { const slot = selectedSlot; if (voiceListening) { speechRecognition.current?.stop(); setVoiceListening(false); return; } const browserWindow = window as unknown as { SpeechRecognition?: new () => SpeechRecognitionLike; webkitSpeechRecognition?: new () => SpeechRecognitionLike; }; const Recognition = browserWindow.SpeechRecognition ?? browserWindow.webkitSpeechRecognition; if (!Recognition) { reportSlotError(slot, "当前 WebView 不支持语音输入。"); return; } const recognition = new Recognition(); recognition.lang = "zh-CN"; recognition.interimResults = true; recognition.onresult = (event) => { const result = event as { results?: ArrayLike>; }; const transcript = Array.from(result.results ?? []) .map((item) => item[0]?.transcript ?? "") .join(""); setDraft((current) => ({ ...current, [slot]: transcript })); }; recognition.onerror = () => { reportSlotError(slot, "语音输入失败,请检查麦克风权限。"); setVoiceListening(false); }; recognition.onend = () => setVoiceListening(false); speechRecognition.current = recognition; recognition.start(); setVoiceListening(true); }; const speakMessage = (content: string) => { if (!window.speechSynthesis) return; window.speechSynthesis.cancel(); const utterance = new SpeechSynthesisUtterance(content); utterance.lang = "zh-CN"; window.speechSynthesis.speak(utterance); }; return (

共享聊天运行时

统一聊天运行时

主界面、快速聊天和气泡共享同一套消息状态。

{(["companion", "operations"] as const).map((slot) => ( ))}
配置 {status[selectedSlot].profileId ?? "未连接"}
会话 {status[selectedSlot].activeSessionId ?? "新会话"}
状态 {connectionStateLabel(status[selectedSlot].state)}
{status[selectedSlot].lastError ? (

{status[selectedSlot].lastError}

) : null} {status[selectedSlot].state === "authExpired" ? (
认证已过期 请到“连接”设置重新登录 AstrBot,当前槽位不会继续发送消息。
) : null} {displaySessions.length > 0 ? (
{displaySessions.map((session, index) => ( ))}
) : null}
{ const list = event.currentTarget; const distanceFromTail = list.scrollHeight - list.scrollTop - list.clientHeight; followTail.current = distanceFromTail <= TAIL_THRESHOLD_PX; }} role="tabpanel" aria-label={`${selectedSlot} messages`} aria-busy={sending[selectedSlot]} aria-live="polite" > {visibleMessages.length === 0 ? (

当前槽位还没有消息。连接 Profile 后即可开始聊天。

) : ( visibleMessages.map((message) => { return (
{message.role === "user" ? "你" : message.role === "assistant" ? "AstrBot" : "系统"} {message.streaming ? 生成中 : null}
{message.content ? ( ) : (

正在生成…

)}
{message.role === "assistant" && message.content && !message.streaming ? (
) : null} {message.streaming && sending[selectedSlot] ? ( ) : null}
); }) )}
{visibleError ? (

{visibleError}

) : null}
{uploading ? 上传中… : null}
{selectedAttachments.length > 0 ? (
{selectedAttachments.map((attachment) => ( {attachment.fileName} ))}
) : null}
{ event.preventDefault(); void send(); }} >