2026-07-20 12:01:18 +08:00
|
|
|
|
import { invoke } from "@tauri-apps/api/core";
|
2026-07-20 11:31:41 +08:00
|
|
|
|
import { useCallback, useEffect, useMemo, useState } from "react";
|
|
|
|
|
|
|
|
|
|
|
|
import {
|
2026-07-20 13:30:29 +08:00
|
|
|
|
astrbotAttachmentSchema,
|
|
|
|
|
|
astrbotCapabilitySnapshotSchema,
|
2026-07-20 11:31:41 +08:00
|
|
|
|
boundRuntimeEventSchema,
|
2026-07-20 12:25:08 +08:00
|
|
|
|
chatSessionSchema,
|
2026-07-20 11:31:41 +08:00
|
|
|
|
connectionRuntimeStatusSchema,
|
2026-07-20 13:30:29 +08:00
|
|
|
|
type AstrBotAttachment,
|
|
|
|
|
|
type AstrBotCapabilitySnapshot,
|
|
|
|
|
|
type AstrBotModelOption,
|
2026-07-20 11:31:41 +08:00
|
|
|
|
type BoundRuntimeEvent,
|
|
|
|
|
|
type ChatMessage,
|
|
|
|
|
|
type ChatSlot,
|
|
|
|
|
|
type ConnectionRuntimeStatus,
|
|
|
|
|
|
storedChatMessageSchema,
|
|
|
|
|
|
} 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<string, unknown>)[key];
|
|
|
|
|
|
return typeof value === "string" ? value : undefined;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-07-20 12:30:12 +08:00
|
|
|
|
export interface ChatPanelProps {
|
|
|
|
|
|
fixedSlot?: ChatSlot;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function ChatPanel({ fixedSlot }: ChatPanelProps) {
|
2026-07-20 11:31:41 +08:00
|
|
|
|
const [status, setStatus] = useState(emptyStatus);
|
|
|
|
|
|
const [messages, setMessages] = useState<ChatMessage[]>([]);
|
2026-07-20 12:30:12 +08:00
|
|
|
|
const [activeSlot, setActiveSlot] = useState<ChatSlot>(
|
|
|
|
|
|
fixedSlot ?? "companion",
|
|
|
|
|
|
);
|
|
|
|
|
|
const selectedSlot = fixedSlot ?? activeSlot;
|
2026-07-20 11:31:41 +08:00
|
|
|
|
const [draft, setDraft] = useState("");
|
|
|
|
|
|
const [error, setError] = useState<string | null>(null);
|
2026-07-20 13:30:29 +08:00
|
|
|
|
const [capabilities, setCapabilities] =
|
|
|
|
|
|
useState<AstrBotCapabilitySnapshot | null>(null);
|
|
|
|
|
|
const [selectedModel, setSelectedModel] = useState("");
|
|
|
|
|
|
const [pendingAttachments, setPendingAttachments] = useState<
|
|
|
|
|
|
AstrBotAttachment[]
|
|
|
|
|
|
>([]);
|
|
|
|
|
|
const [uploading, setUploading] = useState(false);
|
2026-07-20 12:25:08 +08:00
|
|
|
|
const [unreadCounts, setUnreadCounts] = useState<Record<ChatSlot, number>>({
|
|
|
|
|
|
companion: 0,
|
|
|
|
|
|
operations: 0,
|
|
|
|
|
|
});
|
2026-07-20 11:31:41 +08:00
|
|
|
|
const activeMessages = useMemo(
|
2026-07-20 12:30:12 +08:00
|
|
|
|
() => messages.filter((item) => item.slot === selectedSlot),
|
|
|
|
|
|
[messages, selectedSlot],
|
2026-07-20 11:31:41 +08:00
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
const refresh = useCallback(async () => {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const result = await invoke<unknown>("connection_runtime_status");
|
|
|
|
|
|
setStatus(connectionRuntimeStatusSchema.parse(result));
|
|
|
|
|
|
setError(null);
|
|
|
|
|
|
return true;
|
|
|
|
|
|
} catch (reason) {
|
|
|
|
|
|
setError(reason instanceof Error ? reason.message : String(reason));
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
|
|
const drainEvents = useCallback(async () => {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const result = await invoke<unknown>("drain_connection_runtime_events", {
|
|
|
|
|
|
request: { limit: 64 },
|
|
|
|
|
|
});
|
|
|
|
|
|
const events = boundRuntimeEventSchema.array().parse(result);
|
|
|
|
|
|
if (events.length > 0)
|
|
|
|
|
|
setMessages((current) => applyRuntimeEvents(current, events));
|
|
|
|
|
|
} catch {
|
2026-07-20 13:30:29 +08:00
|
|
|
|
// Keep the browser preview usable without Rust IPC.
|
2026-07-20 11:31:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
2026-07-20 12:25:08 +08:00
|
|
|
|
useEffect(() => {
|
2026-07-20 12:30:12 +08:00
|
|
|
|
const snapshot = status[selectedSlot];
|
2026-07-20 12:25:08 +08:00
|
|
|
|
if (!snapshot.profileId || !snapshot.serverInstanceId) return;
|
|
|
|
|
|
let cancelled = false;
|
|
|
|
|
|
void invoke<unknown>("list_chat_sessions", {
|
|
|
|
|
|
request: {
|
|
|
|
|
|
profileId: snapshot.profileId,
|
|
|
|
|
|
serverInstanceId: snapshot.serverInstanceId,
|
|
|
|
|
|
},
|
|
|
|
|
|
})
|
|
|
|
|
|
.then((result) => {
|
|
|
|
|
|
if (cancelled) return;
|
|
|
|
|
|
const sessions = chatSessionSchema.array().parse(result);
|
|
|
|
|
|
setUnreadCounts((current) => ({
|
|
|
|
|
|
...current,
|
2026-07-20 12:30:12 +08:00
|
|
|
|
[selectedSlot]: sessions.reduce(
|
2026-07-20 12:25:08 +08:00
|
|
|
|
(total, session) => total + session.unreadCount,
|
|
|
|
|
|
0,
|
|
|
|
|
|
),
|
|
|
|
|
|
}));
|
|
|
|
|
|
})
|
|
|
|
|
|
.catch(() => undefined);
|
|
|
|
|
|
return () => {
|
|
|
|
|
|
cancelled = true;
|
|
|
|
|
|
};
|
2026-07-20 12:30:12 +08:00
|
|
|
|
}, [selectedSlot, status]);
|
2026-07-20 12:25:08 +08:00
|
|
|
|
|
2026-07-20 13:30:29 +08:00
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
const snapshot = status[selectedSlot];
|
|
|
|
|
|
setCapabilities(null);
|
|
|
|
|
|
setSelectedModel("");
|
|
|
|
|
|
if (!snapshot.profileId || snapshot.transport !== "astrbot-openapi") return;
|
|
|
|
|
|
let cancelled = false;
|
|
|
|
|
|
void invoke<unknown>("connection_slot_capabilities", {
|
|
|
|
|
|
request: { slot: selectedSlot },
|
|
|
|
|
|
})
|
|
|
|
|
|
.then((result) => {
|
|
|
|
|
|
if (cancelled) return;
|
|
|
|
|
|
const next = astrbotCapabilitySnapshotSchema.parse(result);
|
|
|
|
|
|
setCapabilities(next);
|
|
|
|
|
|
setSelectedModel(
|
|
|
|
|
|
next.models.find((model) => model.isDefault)?.id ??
|
|
|
|
|
|
next.models[0]?.id ??
|
|
|
|
|
|
"",
|
|
|
|
|
|
);
|
|
|
|
|
|
})
|
|
|
|
|
|
.catch(() => undefined);
|
|
|
|
|
|
return () => {
|
|
|
|
|
|
cancelled = true;
|
|
|
|
|
|
};
|
|
|
|
|
|
}, [selectedSlot, status]);
|
|
|
|
|
|
|
2026-07-20 12:25:08 +08:00
|
|
|
|
useEffect(() => {
|
2026-07-20 12:30:12 +08:00
|
|
|
|
const snapshot = status[selectedSlot];
|
2026-07-20 12:25:08 +08:00
|
|
|
|
if (
|
|
|
|
|
|
!snapshot.profileId ||
|
|
|
|
|
|
!snapshot.serverInstanceId ||
|
|
|
|
|
|
!snapshot.activeSessionId
|
|
|
|
|
|
)
|
|
|
|
|
|
return;
|
|
|
|
|
|
void invoke("mark_chat_session_read", {
|
|
|
|
|
|
request: {
|
|
|
|
|
|
profileId: snapshot.profileId,
|
|
|
|
|
|
serverInstanceId: snapshot.serverInstanceId,
|
|
|
|
|
|
sessionId: snapshot.activeSessionId,
|
|
|
|
|
|
},
|
|
|
|
|
|
}).catch(() => undefined);
|
2026-07-20 12:30:12 +08:00
|
|
|
|
setUnreadCounts((current) => ({ ...current, [selectedSlot]: 0 }));
|
|
|
|
|
|
}, [selectedSlot, status]);
|
2026-07-20 12:25:08 +08:00
|
|
|
|
|
2026-07-20 11:31:41 +08:00
|
|
|
|
useEffect(() => {
|
2026-07-20 12:30:12 +08:00
|
|
|
|
const snapshot = status[selectedSlot];
|
2026-07-20 11:31:41 +08:00
|
|
|
|
if (
|
|
|
|
|
|
!snapshot.profileId ||
|
|
|
|
|
|
!snapshot.serverInstanceId ||
|
|
|
|
|
|
!snapshot.activeSessionId
|
|
|
|
|
|
)
|
|
|
|
|
|
return;
|
|
|
|
|
|
let cancelled = false;
|
|
|
|
|
|
void invoke<unknown>("load_chat_messages", {
|
|
|
|
|
|
request: {
|
|
|
|
|
|
profileId: snapshot.profileId,
|
|
|
|
|
|
serverInstanceId: snapshot.serverInstanceId,
|
|
|
|
|
|
sessionId: snapshot.activeSessionId,
|
|
|
|
|
|
limit: 100,
|
|
|
|
|
|
},
|
|
|
|
|
|
})
|
|
|
|
|
|
.then((result) => {
|
|
|
|
|
|
if (cancelled) return;
|
|
|
|
|
|
const records = storedChatMessageSchema.array().parse(result);
|
2026-07-20 13:30:29 +08:00
|
|
|
|
setMessages((current) =>
|
|
|
|
|
|
current
|
|
|
|
|
|
.filter((item) => item.slot !== selectedSlot)
|
|
|
|
|
|
.concat(
|
|
|
|
|
|
records.map((record) => ({
|
|
|
|
|
|
id: record.messageId,
|
|
|
|
|
|
slot: record.slot,
|
|
|
|
|
|
role: record.role,
|
|
|
|
|
|
content: record.content,
|
|
|
|
|
|
streaming: record.streaming,
|
|
|
|
|
|
error: record.error,
|
|
|
|
|
|
})),
|
|
|
|
|
|
),
|
|
|
|
|
|
);
|
2026-07-20 11:31:41 +08:00
|
|
|
|
})
|
|
|
|
|
|
.catch(() => undefined);
|
|
|
|
|
|
return () => {
|
|
|
|
|
|
cancelled = true;
|
|
|
|
|
|
};
|
2026-07-20 12:30:12 +08:00
|
|
|
|
}, [selectedSlot, status]);
|
2026-07-20 11:31:41 +08:00
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
let timer: number | undefined;
|
|
|
|
|
|
void refresh().then((available) => {
|
|
|
|
|
|
if (!available) return;
|
|
|
|
|
|
timer = window.setInterval(() => {
|
|
|
|
|
|
void refresh();
|
|
|
|
|
|
void drainEvents();
|
|
|
|
|
|
}, 500);
|
|
|
|
|
|
});
|
|
|
|
|
|
return () => {
|
|
|
|
|
|
if (timer !== undefined) window.clearInterval(timer);
|
|
|
|
|
|
};
|
|
|
|
|
|
}, [drainEvents, refresh]);
|
|
|
|
|
|
|
2026-07-20 13:30:29 +08:00
|
|
|
|
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<unknown>("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);
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2026-07-20 11:31:41 +08:00
|
|
|
|
const send = async () => {
|
2026-07-20 13:30:29 +08:00
|
|
|
|
const message =
|
|
|
|
|
|
draft.trim() ||
|
|
|
|
|
|
(pendingAttachments.length > 0 ? "请查看我发送的附件。" : "");
|
|
|
|
|
|
if (!message || uploading) return;
|
|
|
|
|
|
const id = `local-${Date.now()}`;
|
|
|
|
|
|
const attachments = pendingAttachments;
|
2026-07-20 11:31:41 +08:00
|
|
|
|
setDraft("");
|
2026-07-20 13:30:29 +08:00
|
|
|
|
setPendingAttachments([]);
|
2026-07-20 11:31:41 +08:00
|
|
|
|
setMessages((current) =>
|
2026-07-20 12:30:12 +08:00
|
|
|
|
current.concat({
|
|
|
|
|
|
id,
|
|
|
|
|
|
slot: selectedSlot,
|
|
|
|
|
|
role: "user",
|
|
|
|
|
|
content: message,
|
|
|
|
|
|
}),
|
2026-07-20 11:31:41 +08:00
|
|
|
|
);
|
|
|
|
|
|
try {
|
|
|
|
|
|
await invoke("send_connection_slot_message", {
|
2026-07-20 13:30:29 +08:00
|
|
|
|
request: {
|
|
|
|
|
|
slot: selectedSlot,
|
|
|
|
|
|
message,
|
|
|
|
|
|
configId: selectedModel || undefined,
|
|
|
|
|
|
attachments,
|
|
|
|
|
|
},
|
2026-07-20 11:31:41 +08:00
|
|
|
|
});
|
|
|
|
|
|
setError(null);
|
|
|
|
|
|
} catch (reason) {
|
|
|
|
|
|
setError(reason instanceof Error ? reason.message : String(reason));
|
|
|
|
|
|
setMessages((current) =>
|
|
|
|
|
|
current.map((item) =>
|
|
|
|
|
|
item.id === id ? { ...item, error: true } : item,
|
|
|
|
|
|
),
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2026-07-20 13:30:29 +08:00
|
|
|
|
const currentModels: AstrBotModelOption[] = capabilities?.models ?? [];
|
2026-07-20 11:31:41 +08:00
|
|
|
|
return (
|
|
|
|
|
|
<section className="chat-panel" aria-label="聊天面板">
|
|
|
|
|
|
<div className="chat-heading">
|
|
|
|
|
|
<div>
|
2026-07-20 13:30:29 +08:00
|
|
|
|
<p className="eyebrow">Shared chat runtime</p>
|
2026-07-20 11:31:41 +08:00
|
|
|
|
<h2>统一聊天运行时</h2>
|
|
|
|
|
|
<p className="chat-note">
|
2026-07-20 13:30:29 +08:00
|
|
|
|
主界面、快速聊天和气泡共享同一套消息状态。
|
2026-07-20 11:31:41 +08:00
|
|
|
|
</p>
|
|
|
|
|
|
</div>
|
2026-07-20 13:30:29 +08:00
|
|
|
|
<div className="chat-slot-tabs" role="tablist" aria-label="聊天槽位">
|
2026-07-20 11:31:41 +08:00
|
|
|
|
{(["companion", "operations"] as const).map((slot) => (
|
|
|
|
|
|
<button
|
|
|
|
|
|
className={activeSlot === slot ? "chat-slot active" : "chat-slot"}
|
|
|
|
|
|
key={slot}
|
2026-07-20 12:30:12 +08:00
|
|
|
|
disabled={Boolean(fixedSlot)}
|
2026-07-20 11:31:41 +08:00
|
|
|
|
onClick={() => setActiveSlot(slot)}
|
|
|
|
|
|
role="tab"
|
|
|
|
|
|
aria-selected={activeSlot === slot}
|
|
|
|
|
|
type="button"
|
|
|
|
|
|
>
|
2026-07-20 13:30:29 +08:00
|
|
|
|
{slot === "companion" ? "陪伴" : "运维"}
|
2026-07-20 12:25:08 +08:00
|
|
|
|
{unreadCounts[slot] > 0 ? (
|
|
|
|
|
|
<strong>{unreadCounts[slot]}</strong>
|
|
|
|
|
|
) : null}
|
2026-07-20 12:01:18 +08:00
|
|
|
|
<span>{connectionStateLabel(status[slot].state)}</span>
|
2026-07-20 11:31:41 +08:00
|
|
|
|
</button>
|
|
|
|
|
|
))}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<div className="chat-runtime-strip">
|
2026-07-20 12:30:12 +08:00
|
|
|
|
<span>Profile:{status[selectedSlot].profileId ?? "未连接"}</span>
|
|
|
|
|
|
<span>Session:{status[selectedSlot].activeSessionId ?? "未建立"}</span>
|
|
|
|
|
|
<span>状态:{connectionStateLabel(status[selectedSlot].state)}</span>
|
2026-07-20 11:31:41 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
<div className="chat-message-list" aria-live="polite">
|
|
|
|
|
|
{activeMessages.length === 0 ? (
|
|
|
|
|
|
<p className="chat-empty">
|
|
|
|
|
|
当前槽位还没有消息。连接 Profile 后即可开始聊天。
|
|
|
|
|
|
</p>
|
|
|
|
|
|
) : (
|
|
|
|
|
|
activeMessages.map((message) => (
|
|
|
|
|
|
<article
|
2026-07-20 13:30:29 +08:00
|
|
|
|
className={`chat-message ${message.role}${message.error ? " failed" : ""}`}
|
2026-07-20 11:31:41 +08:00
|
|
|
|
key={message.id}
|
|
|
|
|
|
>
|
|
|
|
|
|
<span className="chat-message-role">
|
|
|
|
|
|
{message.role === "user"
|
|
|
|
|
|
? "我"
|
|
|
|
|
|
: message.role === "assistant"
|
|
|
|
|
|
? "AstrBot"
|
|
|
|
|
|
: "系统"}
|
|
|
|
|
|
</span>
|
2026-07-20 13:30:29 +08:00
|
|
|
|
<p>{message.content || "(空消息)"}</p>
|
|
|
|
|
|
{message.streaming ? <small>生成中…</small> : null}
|
2026-07-20 11:31:41 +08:00
|
|
|
|
</article>
|
|
|
|
|
|
))
|
|
|
|
|
|
)}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
{error ? <p className="chat-error">{error}</p> : null}
|
2026-07-20 13:30:29 +08:00
|
|
|
|
<div className="chat-composer-toolbar">
|
|
|
|
|
|
<label className="file-picker">
|
|
|
|
|
|
<span>添加图片/文件</span>
|
|
|
|
|
|
<input
|
|
|
|
|
|
type="file"
|
|
|
|
|
|
multiple
|
|
|
|
|
|
onChange={(event) => {
|
|
|
|
|
|
void uploadFiles(event.target.files);
|
|
|
|
|
|
event.target.value = "";
|
|
|
|
|
|
}}
|
|
|
|
|
|
disabled={uploading || !capabilities?.fileUpload}
|
|
|
|
|
|
/>
|
|
|
|
|
|
</label>
|
|
|
|
|
|
<select
|
|
|
|
|
|
aria-label="模型配置"
|
|
|
|
|
|
value={selectedModel}
|
|
|
|
|
|
onChange={(event) => setSelectedModel(event.target.value)}
|
|
|
|
|
|
disabled={currentModels.length === 0}
|
|
|
|
|
|
>
|
|
|
|
|
|
{currentModels.length === 0 ? (
|
|
|
|
|
|
<option value="">使用服务端默认模型</option>
|
|
|
|
|
|
) : (
|
|
|
|
|
|
currentModels.map((model) => (
|
|
|
|
|
|
<option key={model.id} value={model.id}>
|
|
|
|
|
|
{model.name || model.id}
|
|
|
|
|
|
</option>
|
|
|
|
|
|
))
|
|
|
|
|
|
)}
|
|
|
|
|
|
</select>
|
|
|
|
|
|
{uploading ? <span className="toolbar-note">上传中…</span> : null}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
{pendingAttachments.length > 0 ? (
|
|
|
|
|
|
<div className="attachment-list">
|
|
|
|
|
|
{pendingAttachments.map((attachment) => (
|
|
|
|
|
|
<span key={attachment.attachmentId}>
|
|
|
|
|
|
{attachment.fileName}
|
|
|
|
|
|
<button
|
|
|
|
|
|
type="button"
|
|
|
|
|
|
aria-label={`移除 ${attachment.fileName}`}
|
|
|
|
|
|
onClick={() =>
|
|
|
|
|
|
setPendingAttachments((current) =>
|
|
|
|
|
|
current.filter(
|
|
|
|
|
|
(item) => item.attachmentId !== attachment.attachmentId,
|
|
|
|
|
|
),
|
|
|
|
|
|
)
|
|
|
|
|
|
}
|
|
|
|
|
|
>
|
|
|
|
|
|
×
|
|
|
|
|
|
</button>
|
|
|
|
|
|
</span>
|
|
|
|
|
|
))}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
) : null}
|
2026-07-20 11:31:41 +08:00
|
|
|
|
<form
|
|
|
|
|
|
className="chat-composer"
|
|
|
|
|
|
onSubmit={(event) => {
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
void send();
|
|
|
|
|
|
}}
|
|
|
|
|
|
>
|
|
|
|
|
|
<textarea
|
|
|
|
|
|
aria-label="聊天消息"
|
|
|
|
|
|
onChange={(event) => setDraft(event.target.value)}
|
|
|
|
|
|
placeholder={
|
2026-07-20 13:30:29 +08:00
|
|
|
|
selectedSlot === "companion"
|
2026-07-20 11:31:41 +08:00
|
|
|
|
? "和陪伴实例说点什么…"
|
|
|
|
|
|
: "向运维实例描述问题…"
|
|
|
|
|
|
}
|
|
|
|
|
|
value={draft}
|
|
|
|
|
|
rows={3}
|
|
|
|
|
|
/>
|
2026-07-20 13:30:29 +08:00
|
|
|
|
<button
|
|
|
|
|
|
disabled={
|
|
|
|
|
|
(!draft.trim() && pendingAttachments.length === 0) || uploading
|
|
|
|
|
|
}
|
|
|
|
|
|
type="submit"
|
|
|
|
|
|
>
|
2026-07-20 11:31:41 +08:00
|
|
|
|
发送
|
|
|
|
|
|
</button>
|
|
|
|
|
|
</form>
|
|
|
|
|
|
</section>
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function applyRuntimeEvents(
|
|
|
|
|
|
current: ChatMessage[],
|
|
|
|
|
|
events: BoundRuntimeEvent[],
|
|
|
|
|
|
): ChatMessage[] {
|
|
|
|
|
|
const next = current.map((item) => ({ ...item }));
|
|
|
|
|
|
for (const event of events) {
|
|
|
|
|
|
if (event.eventType === "message.delta") {
|
|
|
|
|
|
const id =
|
|
|
|
|
|
event.messageId || payloadString(event, "messageId") || event.eventId;
|
|
|
|
|
|
const delta =
|
|
|
|
|
|
payloadString(event, "delta") || payloadString(event, "content") || "";
|
|
|
|
|
|
const existing = next.find(
|
|
|
|
|
|
(item) => item.id === id && item.slot === event.slot,
|
|
|
|
|
|
);
|
|
|
|
|
|
if (existing) {
|
|
|
|
|
|
existing.content += delta;
|
|
|
|
|
|
existing.streaming = true;
|
|
|
|
|
|
} else
|
|
|
|
|
|
next.push({
|
|
|
|
|
|
id,
|
|
|
|
|
|
slot: event.slot,
|
|
|
|
|
|
role: "assistant",
|
|
|
|
|
|
content: delta,
|
|
|
|
|
|
streaming: true,
|
|
|
|
|
|
});
|
|
|
|
|
|
} else if (event.eventType === "message.completed") {
|
|
|
|
|
|
const id =
|
|
|
|
|
|
event.messageId || payloadString(event, "messageId") || event.eventId;
|
|
|
|
|
|
const existing = next.find(
|
|
|
|
|
|
(item) => item.id === id && item.slot === event.slot,
|
|
|
|
|
|
);
|
|
|
|
|
|
if (existing) existing.streaming = false;
|
|
|
|
|
|
else
|
|
|
|
|
|
next.push({
|
|
|
|
|
|
id,
|
|
|
|
|
|
slot: event.slot,
|
|
|
|
|
|
role: "assistant",
|
|
|
|
|
|
content: payloadString(event, "content") || "",
|
|
|
|
|
|
});
|
|
|
|
|
|
} else if (event.eventType === "error") {
|
|
|
|
|
|
next.push({
|
|
|
|
|
|
id: event.eventId,
|
|
|
|
|
|
slot: event.slot,
|
|
|
|
|
|
role: "system",
|
|
|
|
|
|
content: payloadString(event, "message") || "连接错误",
|
|
|
|
|
|
error: true,
|
|
|
|
|
|
});
|
|
|
|
|
|
} else if (event.eventType === "message.proactive") {
|
|
|
|
|
|
next.push({
|
|
|
|
|
|
id: event.eventId,
|
|
|
|
|
|
slot: event.slot,
|
|
|
|
|
|
role: "assistant",
|
|
|
|
|
|
content: payloadString(event, "content") || "",
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return next;
|
|
|
|
|
|
}
|
2026-07-20 12:01:18 +08:00
|
|
|
|
|
|
|
|
|
|
function connectionStateLabel(
|
|
|
|
|
|
state: ConnectionRuntimeStatus["companion"]["state"],
|
|
|
|
|
|
): string {
|
|
|
|
|
|
const labels: Record<typeof state, string> = {
|
|
|
|
|
|
disconnected: "未连接",
|
|
|
|
|
|
connecting: "连接中",
|
|
|
|
|
|
authenticating: "认证中",
|
|
|
|
|
|
syncing: "同步中",
|
|
|
|
|
|
connected: "已连接",
|
|
|
|
|
|
reconnecting: "重连中",
|
|
|
|
|
|
authExpired: "认证已过期",
|
|
|
|
|
|
incompatible: "协议不兼容",
|
|
|
|
|
|
serverUnavailable: "服务不可用",
|
|
|
|
|
|
offline: "离线",
|
2026-07-20 13:30:29 +08:00
|
|
|
|
certificateChanged: "证书已变更",
|
2026-07-20 12:01:18 +08:00
|
|
|
|
fatalError: "连接错误",
|
|
|
|
|
|
};
|
|
|
|
|
|
return labels[state];
|
|
|
|
|
|
}
|