import { invoke } from "@tauri-apps/api/core"; import { listen } from "@tauri-apps/api/event"; import { useCallback, useEffect, useMemo, useRef, useState } from "react"; import { MessageContent } from "./MessageContent"; import { astrbotAttachmentSchema, astrbotCapabilitySnapshotSchema, boundRuntimeEventSchema, chatSessionSchema, connectionRuntimeStatusSchema, type AstrBotAttachment, type AstrBotCapabilitySnapshot, type BoundRuntimeEvent, 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 [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 selectedTransport = selectedSnapshot.transport; const [draft, setDraft] = useState(""); const [error, setError] = useState(null); const [capabilities, setCapabilities] = useState(null); const [pendingAttachments, setPendingAttachments] = useState< AstrBotAttachment[] >([]); const [uploading, setUploading] = useState(false); const [voiceListening, setVoiceListening] = useState(false); const [sending, setSending] = useState>({ companion: false, operations: false, }); const speechRecognition = useRef<{ start: () => void; stop: () => void; } | null>(null); const [unreadCounts, setUnreadCounts] = useState>({ companion: 0, operations: 0, }); 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 refresh = useCallback(async () => { try { const result = await invoke("connection_runtime_status"); const parsed = connectionRuntimeStatusSchema.safeParse(result); if (!parsed.success) { setError("连接状态暂不可用,请检查连接配置。"); return false; } setStatus(parsed.data); setError(null); return true; } catch { setError("连接状态暂不可用,请检查本地运行时。"); return false; } }, []); useEffect(() => { 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) => { unlisten = cleanup; }); return () => unlisten?.(); }, []); 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); setUnreadCounts((current) => ({ ...current, [selectedSlot]: sessions.reduce( (total, session) => total + session.unreadCount, 0, ), })); }) .catch(() => undefined); return () => { cancelled = true; }; }, [ selectedSlot, selectedProfileId, selectedServerInstanceId, selectedSessionId, 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, 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, selectedTransport, ]); useEffect(() => { const snapshot = { profileId: selectedProfileId, serverInstanceId: selectedServerInstanceId, activeSessionId: selectedSessionId, 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, selectedTransport, ]); useEffect(() => { const snapshot = { profileId: selectedProfileId, serverInstanceId: selectedServerInstanceId, activeSessionId: selectedSessionId, 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) => { const localUserMessages = current.filter( (item) => (item.id.startsWith("local-") || item.streaming) && item.slot === selectedSlot && item.sessionId === snapshot.activeSessionId, ); return current .filter( (item) => item.slot !== selectedSlot || item.sessionId !== snapshot.activeSessionId, ) .concat(localUserMessages) .concat( records.map((record) => ({ id: record.messageId, sessionId: record.sessionId, slot: record.slot, role: record.role, content: record.content, streaming: record.streaming, error: record.error, })), ); }); }) .catch(() => undefined); return () => { cancelled = true; }; }, [ selectedSlot, selectedProfileId, selectedServerInstanceId, selectedSessionId, 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; setUploading(true); setError(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: selectedSlot, fileName: file.name, mimeType: file.type || "application/octet-stream", bytes, }, }); uploaded.push(astrbotAttachmentSchema.parse(result)); } setPendingAttachments((current) => current.concat(uploaded)); } catch (reason) { setError(reason instanceof Error ? reason.message : String(reason)); } finally { setUploading(false); } }; const send = async (messageOverride?: string) => { const message = messageOverride?.trim() || draft.trim() || (pendingAttachments.length > 0 ? "Please check the attachment." : ""); if (!message || uploading || sending[selectedSlot]) return; const id = `local-${Date.now()}`; const attachments = messageOverride ? [] : pendingAttachments; setDraft(""); if (!messageOverride) setPendingAttachments([]); setSending((current) => ({ ...current, [selectedSlot]: true })); setMessages((current) => current.concat({ id, sessionId: status[selectedSlot].activeSessionId, slot: selectedSlot, role: "user", content: message, }), ); try { const result = slotRuntimeSnapshotSchema.parse( await invoke("send_connection_slot_message", { request: { slot: selectedSlot, message, attachments, }, }), ); setStatus((current) => ({ ...current, [selectedSlot]: result })); setMessages((current) => current.map((item) => item.id === id ? { ...item, sessionId: result.activeSessionId } : item, ), ); setError(null); } catch (reason) { setError(reason instanceof Error ? reason.message : String(reason)); setMessages((current) => current.map((item) => item.id === id ? { ...item, error: true } : item, ), ); } finally { setSending((current) => ({ ...current, [selectedSlot]: false })); } }; const stopGeneration = async () => { try { await invoke("cancel_connection_slot_message", { request: { slot: selectedSlot }, }); setSending((current) => ({ ...current, [selectedSlot]: false })); setMessages((current) => current.map((item) => item.slot === selectedSlot && item.streaming ? { ...item, streaming: false, content: item.content || "Generation stopped.", error: true, } : item, ), ); setError(null); } catch (reason) { setError(reason instanceof Error ? reason.message : String(reason)); } }; const copyMessage = async (content: string) => { try { await navigator.clipboard.writeText(content); setError(null); } catch { setError("Copy failed. Check clipboard permissions."); } }; 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 () => { if ( !status[selectedSlot].state || status[selectedSlot].state === "disconnected" ) { setError("Connect the selected slot first."); return; } try { const sessionId = crypto.randomUUID(); const result = slotRuntimeSnapshotSchema.parse( await invoke("switch_connection_slot_session", { request: { slot: selectedSlot, sessionId }, }), ); setMessages((current) => current.filter( (item) => item.slot !== selectedSlot || item.sessionId !== status[selectedSlot].activeSessionId, ), ); setStatus((current) => ({ ...current, [selectedSlot]: result })); setError(null); } catch (reason) { setError(reason instanceof Error ? reason.message : String(reason)); } }; const refreshConnection = async () => { await refresh(); }; const reconnectConnection = async () => { const profileId = status[selectedSlot].profileId; if (!profileId) { setError("No Profile is assigned to the selected slot."); return; } try { const result = slotRuntimeSnapshotSchema.parse( await invoke("probe_connection_slot", { request: { slot: selectedSlot, profileId }, }), ); setStatus((current) => ({ ...current, [selectedSlot]: result })); setError(null); } catch (reason) { setError(reason instanceof Error ? reason.message : String(reason)); } }; const disconnectConnection = async () => { try { await invoke("disconnect_connection_slot", { request: { slot: selectedSlot }, }); await refresh(); } catch (reason) { setError(reason instanceof Error ? reason.message : String(reason)); } }; const toggleVoiceInput = () => { 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) { setError("当前 WebView 不支持语音输入。"); return; } const recognition = new Recognition(); recognition.lang = "zh-CN"; recognition.interimResults = true; recognition.onresult = (event) => { const result = event as { results?: ArrayLike>; }; setDraft( Array.from(result.results ?? []) .map((item) => item[0]?.transcript ?? "") .join(""), ); }; recognition.onerror = () => { setError("语音输入失败,请检查麦克风权限。"); 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 (

Shared chat runtime

统一聊天运行时

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

{(["companion", "operations"] as const).map((slot) => ( ))}
Profile {status[selectedSlot].profileId ?? "Not connected"}
Session {status[selectedSlot].activeSessionId ?? "New session"}
Status {connectionStateLabel(status[selectedSlot].state)}
{status[selectedSlot].lastError ? (

{status[selectedSlot].lastError}

) : null} {status[selectedSlot].state === "authExpired" ? (
认证已过期 请到“连接”设置重新登录 AstrBot,当前槽位不会继续发送消息。
) : null} {remoteSessions.length > 0 ? (
{remoteSessions.map((session) => ( ))}
) : null}
{visibleMessages.length === 0 ? (

No messages in this slot yet. Connect a Profile to start chatting.

) : ( visibleMessages.map((message) => { return (
{message.role === "user" ? "You" : message.role === "assistant" ? "AstrBot" : "System"} {message.streaming ? Generating : null}
{message.content ? ( ) : (

Generating?

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

{error}

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