import { invoke } from "@tauri-apps/api/core"; import { listen } from "@tauri-apps/api/event"; import { useCallback, useEffect, useMemo, useRef, useState } from "react"; 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 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 () => { const message = draft.trim() || (pendingAttachments.length > 0 ? "请查看我发送的附件。" : ""); if (!message || uploading) return; const id = `local-${Date.now()}`; const attachments = pendingAttachments; setDraft(""); setPendingAttachments([]); 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, ), ); } }; 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 ?? "未连接"} Session:{status[selectedSlot].activeSessionId ?? "未建立"} 状态:{connectionStateLabel(status[selectedSlot].state)}
{status[selectedSlot].state === "authExpired" ? (
认证已过期 请到“连接”设置重新登录 AstrBot,当前槽位不会继续发送消息。
) : null} {remoteSessions.length > 0 ? (
{remoteSessions.map((session) => ( ))}
) : null}
{visibleMessages.length === 0 ? (

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

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

{message.content || "生成中…"}

{message.role === "assistant" && message.content ? ( ) : null}
)) )}
{error ?

{error}

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