2026-07-20 12:01:18 +08:00
|
|
|
|
import { invoke } from "@tauri-apps/api/core";
|
2026-07-21 14:43:14 +08:00
|
|
|
|
import { listen } from "@tauri-apps/api/event";
|
|
|
|
|
|
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
2026-07-20 11:31:41 +08:00
|
|
|
|
|
2026-07-26 22:31:16 +08:00
|
|
|
|
import { errorMessage } from "../../lib/errorMessage";
|
2026-07-21 15:25:11 +08:00
|
|
|
|
import { MessageContent } from "./MessageContent";
|
|
|
|
|
|
|
2026-07-20 11:31:41 +08:00
|
|
|
|
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,
|
2026-07-20 11:31:41 +08:00
|
|
|
|
type BoundRuntimeEvent,
|
2026-07-22 02:52:31 +08:00
|
|
|
|
type ChatSession,
|
|
|
|
|
|
mergeLoadedChatMessages,
|
2026-07-20 14:32:07 +08:00
|
|
|
|
remoteChatSessionSchema,
|
2026-07-20 11:31:41 +08:00
|
|
|
|
type ChatMessage,
|
|
|
|
|
|
type ChatSlot,
|
2026-07-20 14:32:07 +08:00
|
|
|
|
type RemoteChatSession,
|
2026-07-20 11:31:41 +08:00
|
|
|
|
type ConnectionRuntimeStatus,
|
|
|
|
|
|
storedChatMessageSchema,
|
2026-07-21 14:43:14 +08:00
|
|
|
|
slotRuntimeSnapshotSchema,
|
2026-07-20 11:31:41 +08:00
|
|
|
|
} 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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-07-21 14:43:14 +08:00
|
|
|
|
type SpeechRecognitionLike = {
|
|
|
|
|
|
lang: string;
|
|
|
|
|
|
interimResults: boolean;
|
|
|
|
|
|
onresult: ((event: unknown) => void) | null;
|
|
|
|
|
|
onerror: (() => void) | null;
|
|
|
|
|
|
onend: (() => void) | null;
|
|
|
|
|
|
start: () => void;
|
|
|
|
|
|
stop: () => void;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2026-07-20 12:30:12 +08:00
|
|
|
|
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 14:32:07 +08:00
|
|
|
|
const [remoteSessions, setRemoteSessions] = useState<RemoteChatSession[]>([]);
|
2026-07-22 02:52:31 +08:00
|
|
|
|
const [storedSessions, setStoredSessions] = useState<
|
|
|
|
|
|
Record<ChatSlot, ChatSession[]>
|
|
|
|
|
|
>({
|
|
|
|
|
|
companion: [],
|
|
|
|
|
|
operations: [],
|
|
|
|
|
|
});
|
2026-07-20 12:30:12 +08:00
|
|
|
|
const [activeSlot, setActiveSlot] = useState<ChatSlot>(
|
|
|
|
|
|
fixedSlot ?? "companion",
|
|
|
|
|
|
);
|
|
|
|
|
|
const selectedSlot = fixedSlot ?? activeSlot;
|
2026-07-20 14:32:07 +08:00
|
|
|
|
const selectedSnapshot = status[selectedSlot];
|
|
|
|
|
|
const selectedProfileId = selectedSnapshot.profileId;
|
|
|
|
|
|
const selectedServerInstanceId = selectedSnapshot.serverInstanceId;
|
|
|
|
|
|
const selectedSessionId = selectedSnapshot.activeSessionId;
|
2026-07-22 02:52:31 +08:00
|
|
|
|
const historySessionId =
|
|
|
|
|
|
selectedSessionId ?? storedSessions[selectedSlot][0]?.sessionId;
|
2026-07-20 14:32:07 +08:00
|
|
|
|
const selectedTransport = selectedSnapshot.transport;
|
fix(chat): scope draft, attachments, and errors per slot
The composer draft, the pending attachment list and the error banner
were single values shared by both slots. Uploading on companion and
then switching to operations routed the companion instance's
attachment ids into the operations instance, the operations error
banner stayed visible on the companion tab, and a half-typed message
followed the user across tabs.
Make all three Record<ChatSlot, ...>, mirroring the sending,
unreadCounts and storedSessions state that was already per slot. Every
asynchronous action now pins its slot at the start, so an upload or a
send that resolves after the user switched tabs still lands on the slot
it belonged to.
Splitting the error state is required rather than incidental. refresh()
runs every 500ms and wrote the shared error on failure and cleared it on
success, so any send or upload error was silently wiped within half a
second. The polling failure describes the IPC channel rather than a
slot, so it keeps its own runtimeError; slotError is owned solely by
user actions and takes precedence when both are present.
All three new tests were checked against a mutation that reads the
companion slot regardless of selection, and all three fail under it.
Refs: F-16 (audit P1-11).
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-27 00:28:27 +08:00
|
|
|
|
const [draft, setDraft] = useState<Record<ChatSlot, string>>({
|
|
|
|
|
|
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<string | null>(null);
|
|
|
|
|
|
const [slotError, setSlotError] = useState<Record<ChatSlot, string | null>>({
|
|
|
|
|
|
companion: null,
|
|
|
|
|
|
operations: null,
|
|
|
|
|
|
});
|
2026-07-20 13:30:29 +08:00
|
|
|
|
const [capabilities, setCapabilities] =
|
|
|
|
|
|
useState<AstrBotCapabilitySnapshot | null>(null);
|
|
|
|
|
|
const [pendingAttachments, setPendingAttachments] = useState<
|
fix(chat): scope draft, attachments, and errors per slot
The composer draft, the pending attachment list and the error banner
were single values shared by both slots. Uploading on companion and
then switching to operations routed the companion instance's
attachment ids into the operations instance, the operations error
banner stayed visible on the companion tab, and a half-typed message
followed the user across tabs.
Make all three Record<ChatSlot, ...>, mirroring the sending,
unreadCounts and storedSessions state that was already per slot. Every
asynchronous action now pins its slot at the start, so an upload or a
send that resolves after the user switched tabs still lands on the slot
it belonged to.
Splitting the error state is required rather than incidental. refresh()
runs every 500ms and wrote the shared error on failure and cleared it on
success, so any send or upload error was silently wiped within half a
second. The polling failure describes the IPC channel rather than a
slot, so it keeps its own runtimeError; slotError is owned solely by
user actions and takes precedence when both are present.
All three new tests were checked against a mutation that reads the
companion slot regardless of selection, and all three fail under it.
Refs: F-16 (audit P1-11).
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-27 00:28:27 +08:00
|
|
|
|
Record<ChatSlot, AstrBotAttachment[]>
|
|
|
|
|
|
>({
|
|
|
|
|
|
companion: [],
|
|
|
|
|
|
operations: [],
|
|
|
|
|
|
});
|
2026-07-20 13:30:29 +08:00
|
|
|
|
const [uploading, setUploading] = useState(false);
|
2026-07-21 14:43:14 +08:00
|
|
|
|
const [voiceListening, setVoiceListening] = useState(false);
|
2026-07-21 15:25:11 +08:00
|
|
|
|
const [sending, setSending] = useState<Record<ChatSlot, boolean>>({
|
|
|
|
|
|
companion: false,
|
|
|
|
|
|
operations: false,
|
|
|
|
|
|
});
|
2026-07-27 02:21:30 +08:00
|
|
|
|
const messageListRef = useRef<HTMLDivElement | null>(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);
|
2026-07-21 14:43:14 +08:00
|
|
|
|
const speechRecognition = useRef<{
|
|
|
|
|
|
start: () => void;
|
|
|
|
|
|
stop: () => void;
|
|
|
|
|
|
} | null>(null);
|
2026-07-20 12:25:08 +08:00
|
|
|
|
const [unreadCounts, setUnreadCounts] = useState<Record<ChatSlot, number>>({
|
|
|
|
|
|
companion: 0,
|
|
|
|
|
|
operations: 0,
|
|
|
|
|
|
});
|
fix(chat): scope draft, attachments, and errors per slot
The composer draft, the pending attachment list and the error banner
were single values shared by both slots. Uploading on companion and
then switching to operations routed the companion instance's
attachment ids into the operations instance, the operations error
banner stayed visible on the companion tab, and a half-typed message
followed the user across tabs.
Make all three Record<ChatSlot, ...>, mirroring the sending,
unreadCounts and storedSessions state that was already per slot. Every
asynchronous action now pins its slot at the start, so an upload or a
send that resolves after the user switched tabs still lands on the slot
it belonged to.
Splitting the error state is required rather than incidental. refresh()
runs every 500ms and wrote the shared error on failure and cleared it on
success, so any send or upload error was silently wiped within half a
second. The polling failure describes the IPC channel rather than a
slot, so it keeps its own runtimeError; slotError is owned solely by
user actions and takes precedence when both are present.
All three new tests were checked against a mutation that reads the
companion slot regardless of selection, and all three fail under it.
Refs: F-16 (audit P1-11).
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-27 00:28:27 +08:00
|
|
|
|
const selectedAttachments = pendingAttachments[selectedSlot];
|
|
|
|
|
|
// A slot-scoped action error wins over the runtime-wide polling error.
|
|
|
|
|
|
const visibleError = slotError[selectedSlot] ?? runtimeError;
|
2026-07-20 11:31:41 +08:00
|
|
|
|
const activeMessages = useMemo(
|
2026-07-20 14:32:07 +08:00
|
|
|
|
() =>
|
|
|
|
|
|
messages.filter(
|
|
|
|
|
|
(item) =>
|
|
|
|
|
|
item.slot === selectedSlot &&
|
|
|
|
|
|
(!status[selectedSlot].activeSessionId ||
|
|
|
|
|
|
item.sessionId === status[selectedSlot].activeSessionId),
|
|
|
|
|
|
),
|
|
|
|
|
|
[messages, selectedSlot, status],
|
2026-07-20 11:31:41 +08:00
|
|
|
|
);
|
2026-07-21 14:43:14 +08:00
|
|
|
|
const visibleMessages = useMemo(
|
|
|
|
|
|
() =>
|
|
|
|
|
|
activeMessages.filter(
|
2026-07-21 15:25:11 +08:00
|
|
|
|
(message) =>
|
|
|
|
|
|
message.content.trim().length > 0 ||
|
|
|
|
|
|
message.streaming ||
|
|
|
|
|
|
message.error,
|
2026-07-21 14:43:14 +08:00
|
|
|
|
),
|
|
|
|
|
|
[activeMessages],
|
|
|
|
|
|
);
|
2026-07-20 11:31:41 +08:00
|
|
|
|
|
2026-07-22 02:52:31 +08:00
|
|
|
|
const displaySessions =
|
|
|
|
|
|
remoteSessions.length > 0
|
|
|
|
|
|
? remoteSessions
|
|
|
|
|
|
: storedSessions[selectedSlot].map((session) => ({
|
|
|
|
|
|
sessionId: session.sessionId,
|
|
|
|
|
|
displayName: undefined,
|
|
|
|
|
|
updatedAt: session.lastMessageAt,
|
|
|
|
|
|
}));
|
|
|
|
|
|
|
2026-07-20 11:31:41 +08:00
|
|
|
|
const refresh = useCallback(async () => {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const result = await invoke<unknown>("connection_runtime_status");
|
2026-07-21 14:43:14 +08:00
|
|
|
|
const parsed = connectionRuntimeStatusSchema.safeParse(result);
|
|
|
|
|
|
if (!parsed.success) {
|
fix(chat): scope draft, attachments, and errors per slot
The composer draft, the pending attachment list and the error banner
were single values shared by both slots. Uploading on companion and
then switching to operations routed the companion instance's
attachment ids into the operations instance, the operations error
banner stayed visible on the companion tab, and a half-typed message
followed the user across tabs.
Make all three Record<ChatSlot, ...>, mirroring the sending,
unreadCounts and storedSessions state that was already per slot. Every
asynchronous action now pins its slot at the start, so an upload or a
send that resolves after the user switched tabs still lands on the slot
it belonged to.
Splitting the error state is required rather than incidental. refresh()
runs every 500ms and wrote the shared error on failure and cleared it on
success, so any send or upload error was silently wiped within half a
second. The polling failure describes the IPC channel rather than a
slot, so it keeps its own runtimeError; slotError is owned solely by
user actions and takes precedence when both are present.
All three new tests were checked against a mutation that reads the
companion slot regardless of selection, and all three fail under it.
Refs: F-16 (audit P1-11).
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-27 00:28:27 +08:00
|
|
|
|
setRuntimeError("连接状态暂不可用,请检查连接配置。");
|
2026-07-21 14:43:14 +08:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
setStatus(parsed.data);
|
fix(chat): scope draft, attachments, and errors per slot
The composer draft, the pending attachment list and the error banner
were single values shared by both slots. Uploading on companion and
then switching to operations routed the companion instance's
attachment ids into the operations instance, the operations error
banner stayed visible on the companion tab, and a half-typed message
followed the user across tabs.
Make all three Record<ChatSlot, ...>, mirroring the sending,
unreadCounts and storedSessions state that was already per slot. Every
asynchronous action now pins its slot at the start, so an upload or a
send that resolves after the user switched tabs still lands on the slot
it belonged to.
Splitting the error state is required rather than incidental. refresh()
runs every 500ms and wrote the shared error on failure and cleared it on
success, so any send or upload error was silently wiped within half a
second. The polling failure describes the IPC channel rather than a
slot, so it keeps its own runtimeError; slotError is owned solely by
user actions and takes precedence when both are present.
All three new tests were checked against a mutation that reads the
companion slot regardless of selection, and all three fail under it.
Refs: F-16 (audit P1-11).
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-27 00:28:27 +08:00
|
|
|
|
setRuntimeError(null);
|
2026-07-20 11:31:41 +08:00
|
|
|
|
return true;
|
2026-07-21 14:43:14 +08:00
|
|
|
|
} catch {
|
fix(chat): scope draft, attachments, and errors per slot
The composer draft, the pending attachment list and the error banner
were single values shared by both slots. Uploading on companion and
then switching to operations routed the companion instance's
attachment ids into the operations instance, the operations error
banner stayed visible on the companion tab, and a half-typed message
followed the user across tabs.
Make all three Record<ChatSlot, ...>, mirroring the sending,
unreadCounts and storedSessions state that was already per slot. Every
asynchronous action now pins its slot at the start, so an upload or a
send that resolves after the user switched tabs still lands on the slot
it belonged to.
Splitting the error state is required rather than incidental. refresh()
runs every 500ms and wrote the shared error on failure and cleared it on
success, so any send or upload error was silently wiped within half a
second. The polling failure describes the IPC channel rather than a
slot, so it keeps its own runtimeError; slotError is owned solely by
user actions and takes precedence when both are present.
All three new tests were checked against a mutation that reads the
companion slot regardless of selection, and all three fail under it.
Refs: F-16 (audit P1-11).
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-27 00:28:27 +08:00
|
|
|
|
setRuntimeError("连接状态暂不可用,请检查本地运行时。");
|
2026-07-20 11:31:41 +08:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
fix(chat): scope draft, attachments, and errors per slot
The composer draft, the pending attachment list and the error banner
were single values shared by both slots. Uploading on companion and
then switching to operations routed the companion instance's
attachment ids into the operations instance, the operations error
banner stayed visible on the companion tab, and a half-typed message
followed the user across tabs.
Make all three Record<ChatSlot, ...>, mirroring the sending,
unreadCounts and storedSessions state that was already per slot. Every
asynchronous action now pins its slot at the start, so an upload or a
send that resolves after the user switched tabs still lands on the slot
it belonged to.
Splitting the error state is required rather than incidental. refresh()
runs every 500ms and wrote the shared error on failure and cleared it on
success, so any send or upload error was silently wiped within half a
second. The polling failure describes the IPC channel rather than a
slot, so it keeps its own runtimeError; slotError is owned solely by
user actions and takes precedence when both are present.
All three new tests were checked against a mutation that reads the
companion slot regardless of selection, and all three fail under it.
Refs: F-16 (audit P1-11).
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-27 00:28:27 +08:00
|
|
|
|
const reportSlotError = (slot: ChatSlot, message: string | null) => {
|
|
|
|
|
|
setSlotError((current) => ({ ...current, [slot]: message }));
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2026-07-27 02:21:30 +08:00
|
|
|
|
/// 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]);
|
|
|
|
|
|
|
2026-07-21 14:43:14 +08:00
|
|
|
|
useEffect(() => {
|
fix(ui): release tauri event listeners on unmount
All four listen() call sites assigned the resolved unregister function
unconditionally. When the effect was cleaned up before the promise
settled, that assignment landed on a dead closure and the listener was
never unregistered. React StrictMode mounts, cleans up and mounts again,
so the first listener leaked on every development mount. In ChatPanel
that meant applyRuntimeEvents ran twice per event and, because a delta
is appended to the existing content, every character of a streamed reply
was doubled.
Track whether the effect is still live and unregister immediately when
the promise settles late. Each site also gains a catch, since these were
all bare void listen(...).then(...) chains — a rejected listen, such as
the missing desktop-pet capability, became an unhandled rejection rather
than anything observable.
The listener mock in src/test/setup.ts resolved eagerly, which made the
race impossible to express. Replace it with a controllable double in
src/test/tauriEvent.ts that defers resolution until the test asks for
it, tracks each registration separately, and only dispatches to handlers
that have not been unregistered. The registry is plain module state, so
setup.ts resets it after each test.
ChatPanel had no test file; this adds one. Both new tests were checked
against a mutation restoring the old assignment, and both fail under it.
Refs: F-15 (audit P1-10).
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-27 00:23:42 +08:00
|
|
|
|
// `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;
|
2026-07-21 14:43:14 +08:00
|
|
|
|
let unlisten: (() => void) | undefined;
|
|
|
|
|
|
void listen<unknown>("connection-runtime-event", (event) => {
|
|
|
|
|
|
const parsed = boundRuntimeEventSchema.safeParse(event.payload);
|
|
|
|
|
|
if (parsed.success) {
|
|
|
|
|
|
setMessages((current) => applyRuntimeEvents(current, [parsed.data]));
|
|
|
|
|
|
}
|
fix(ui): release tauri event listeners on unmount
All four listen() call sites assigned the resolved unregister function
unconditionally. When the effect was cleaned up before the promise
settled, that assignment landed on a dead closure and the listener was
never unregistered. React StrictMode mounts, cleans up and mounts again,
so the first listener leaked on every development mount. In ChatPanel
that meant applyRuntimeEvents ran twice per event and, because a delta
is appended to the existing content, every character of a streamed reply
was doubled.
Track whether the effect is still live and unregister immediately when
the promise settles late. Each site also gains a catch, since these were
all bare void listen(...).then(...) chains — a rejected listen, such as
the missing desktop-pet capability, became an unhandled rejection rather
than anything observable.
The listener mock in src/test/setup.ts resolved eagerly, which made the
race impossible to express. Replace it with a controllable double in
src/test/tauriEvent.ts that defers resolution until the test asks for
it, tracks each registration separately, and only dispatches to handlers
that have not been unregistered. The registry is plain module state, so
setup.ts resets it after each test.
ChatPanel had no test file; this adds one. Both new tests were checked
against a mutation restoring the old assignment, and both fail under it.
Refs: F-15 (audit P1-10).
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-27 00:23:42 +08:00
|
|
|
|
})
|
|
|
|
|
|
.then((cleanup) => {
|
|
|
|
|
|
if (active) {
|
|
|
|
|
|
unlisten = cleanup;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
cleanup();
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
.catch(() => undefined);
|
|
|
|
|
|
return () => {
|
|
|
|
|
|
active = false;
|
|
|
|
|
|
unlisten?.();
|
|
|
|
|
|
unlisten = undefined;
|
|
|
|
|
|
};
|
2026-07-20 11:31:41 +08:00
|
|
|
|
}, []);
|
|
|
|
|
|
|
2026-07-20 12:25:08 +08:00
|
|
|
|
useEffect(() => {
|
2026-07-20 14:32:07 +08:00
|
|
|
|
const snapshot = {
|
|
|
|
|
|
profileId: selectedProfileId,
|
|
|
|
|
|
serverInstanceId: selectedServerInstanceId,
|
|
|
|
|
|
activeSessionId: selectedSessionId,
|
|
|
|
|
|
transport: selectedTransport,
|
|
|
|
|
|
};
|
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,
|
2026-07-21 14:43:14 +08:00
|
|
|
|
slot: selectedSlot,
|
2026-07-20 12:25:08 +08:00
|
|
|
|
serverInstanceId: snapshot.serverInstanceId,
|
|
|
|
|
|
},
|
|
|
|
|
|
})
|
|
|
|
|
|
.then((result) => {
|
|
|
|
|
|
if (cancelled) return;
|
|
|
|
|
|
const sessions = chatSessionSchema.array().parse(result);
|
2026-07-22 02:52:31 +08:00
|
|
|
|
setStoredSessions((current) => ({
|
|
|
|
|
|
...current,
|
|
|
|
|
|
[selectedSlot]: sessions,
|
|
|
|
|
|
}));
|
|
|
|
|
|
|
2026-07-20 12:25:08 +08:00
|
|
|
|
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 14:32:07 +08:00
|
|
|
|
}, [
|
|
|
|
|
|
selectedSlot,
|
|
|
|
|
|
selectedProfileId,
|
|
|
|
|
|
selectedServerInstanceId,
|
|
|
|
|
|
selectedSessionId,
|
2026-07-22 02:52:31 +08:00
|
|
|
|
historySessionId,
|
2026-07-20 14:32:07 +08:00
|
|
|
|
selectedTransport,
|
|
|
|
|
|
]);
|
2026-07-20 12:25:08 +08:00
|
|
|
|
|
2026-07-20 13:30:29 +08:00
|
|
|
|
useEffect(() => {
|
2026-07-20 14:32:07 +08:00
|
|
|
|
const snapshot = {
|
|
|
|
|
|
profileId: selectedProfileId,
|
|
|
|
|
|
serverInstanceId: selectedServerInstanceId,
|
|
|
|
|
|
activeSessionId: selectedSessionId,
|
|
|
|
|
|
transport: selectedTransport,
|
|
|
|
|
|
};
|
|
|
|
|
|
setRemoteSessions([]);
|
|
|
|
|
|
if (!snapshot.profileId) return;
|
|
|
|
|
|
let cancelled = false;
|
|
|
|
|
|
void invoke<unknown>("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,
|
2026-07-22 02:52:31 +08:00
|
|
|
|
historySessionId,
|
2026-07-20 14:32:07 +08:00
|
|
|
|
selectedTransport,
|
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
const snapshot = {
|
|
|
|
|
|
profileId: selectedProfileId,
|
|
|
|
|
|
serverInstanceId: selectedServerInstanceId,
|
|
|
|
|
|
activeSessionId: selectedSessionId,
|
|
|
|
|
|
transport: selectedTransport,
|
|
|
|
|
|
};
|
2026-07-20 13:30:29 +08:00
|
|
|
|
setCapabilities(null);
|
2026-07-21 15:25:11 +08:00
|
|
|
|
if (!snapshot.profileId || snapshot.transport !== "astr-bot-open-api")
|
|
|
|
|
|
return;
|
2026-07-20 13:30:29 +08:00
|
|
|
|
let cancelled = false;
|
|
|
|
|
|
void invoke<unknown>("connection_slot_capabilities", {
|
|
|
|
|
|
request: { slot: selectedSlot },
|
|
|
|
|
|
})
|
|
|
|
|
|
.then((result) => {
|
|
|
|
|
|
if (cancelled) return;
|
|
|
|
|
|
const next = astrbotCapabilitySnapshotSchema.parse(result);
|
|
|
|
|
|
setCapabilities(next);
|
|
|
|
|
|
})
|
|
|
|
|
|
.catch(() => undefined);
|
|
|
|
|
|
return () => {
|
|
|
|
|
|
cancelled = true;
|
|
|
|
|
|
};
|
2026-07-20 14:32:07 +08:00
|
|
|
|
}, [
|
|
|
|
|
|
selectedSlot,
|
|
|
|
|
|
selectedProfileId,
|
|
|
|
|
|
selectedServerInstanceId,
|
|
|
|
|
|
selectedSessionId,
|
2026-07-22 02:52:31 +08:00
|
|
|
|
historySessionId,
|
2026-07-20 14:32:07 +08:00
|
|
|
|
selectedTransport,
|
|
|
|
|
|
]);
|
2026-07-20 13:30:29 +08:00
|
|
|
|
|
2026-07-20 12:25:08 +08:00
|
|
|
|
useEffect(() => {
|
2026-07-20 14:32:07 +08:00
|
|
|
|
const snapshot = {
|
|
|
|
|
|
profileId: selectedProfileId,
|
|
|
|
|
|
serverInstanceId: selectedServerInstanceId,
|
2026-07-22 02:52:31 +08:00
|
|
|
|
activeSessionId: historySessionId,
|
2026-07-20 14:32:07 +08:00
|
|
|
|
transport: selectedTransport,
|
|
|
|
|
|
};
|
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,
|
2026-07-21 14:43:14 +08:00
|
|
|
|
slot: selectedSlot,
|
2026-07-20 12:25:08 +08:00
|
|
|
|
serverInstanceId: snapshot.serverInstanceId,
|
|
|
|
|
|
sessionId: snapshot.activeSessionId,
|
|
|
|
|
|
},
|
|
|
|
|
|
}).catch(() => undefined);
|
2026-07-20 12:30:12 +08:00
|
|
|
|
setUnreadCounts((current) => ({ ...current, [selectedSlot]: 0 }));
|
2026-07-20 14:32:07 +08:00
|
|
|
|
}, [
|
|
|
|
|
|
selectedSlot,
|
|
|
|
|
|
selectedProfileId,
|
|
|
|
|
|
selectedServerInstanceId,
|
|
|
|
|
|
selectedSessionId,
|
2026-07-22 02:52:31 +08:00
|
|
|
|
historySessionId,
|
2026-07-20 14:32:07 +08:00
|
|
|
|
selectedTransport,
|
|
|
|
|
|
]);
|
2026-07-20 12:25:08 +08:00
|
|
|
|
|
2026-07-20 11:31:41 +08:00
|
|
|
|
useEffect(() => {
|
2026-07-20 14:32:07 +08:00
|
|
|
|
const snapshot = {
|
|
|
|
|
|
profileId: selectedProfileId,
|
|
|
|
|
|
serverInstanceId: selectedServerInstanceId,
|
2026-07-22 02:52:31 +08:00
|
|
|
|
activeSessionId: historySessionId,
|
2026-07-20 14:32:07 +08:00
|
|
|
|
transport: selectedTransport,
|
|
|
|
|
|
};
|
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,
|
2026-07-21 14:43:14 +08:00
|
|
|
|
slot: selectedSlot,
|
2026-07-20 11:31:41 +08:00
|
|
|
|
serverInstanceId: snapshot.serverInstanceId,
|
|
|
|
|
|
sessionId: snapshot.activeSessionId,
|
|
|
|
|
|
limit: 100,
|
|
|
|
|
|
},
|
|
|
|
|
|
})
|
|
|
|
|
|
.then((result) => {
|
|
|
|
|
|
if (cancelled) return;
|
|
|
|
|
|
const records = storedChatMessageSchema.array().parse(result);
|
2026-07-22 02:52:31 +08:00
|
|
|
|
setMessages((current) =>
|
|
|
|
|
|
mergeLoadedChatMessages(
|
|
|
|
|
|
current,
|
|
|
|
|
|
records,
|
|
|
|
|
|
selectedSlot,
|
|
|
|
|
|
snapshot.activeSessionId,
|
|
|
|
|
|
),
|
|
|
|
|
|
);
|
2026-07-20 11:31:41 +08:00
|
|
|
|
})
|
|
|
|
|
|
.catch(() => undefined);
|
|
|
|
|
|
return () => {
|
|
|
|
|
|
cancelled = true;
|
|
|
|
|
|
};
|
2026-07-20 14:32:07 +08:00
|
|
|
|
}, [
|
|
|
|
|
|
selectedSlot,
|
|
|
|
|
|
selectedProfileId,
|
|
|
|
|
|
selectedServerInstanceId,
|
|
|
|
|
|
selectedSessionId,
|
2026-07-22 02:52:31 +08:00
|
|
|
|
historySessionId,
|
2026-07-20 14:32:07 +08:00
|
|
|
|
selectedTransport,
|
|
|
|
|
|
]);
|
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();
|
|
|
|
|
|
}, 500);
|
|
|
|
|
|
});
|
|
|
|
|
|
return () => {
|
|
|
|
|
|
if (timer !== undefined) window.clearInterval(timer);
|
|
|
|
|
|
};
|
2026-07-21 14:43:14 +08:00
|
|
|
|
}, [refresh]);
|
2026-07-20 11:31:41 +08:00
|
|
|
|
|
2026-07-20 13:30:29 +08:00
|
|
|
|
const uploadFiles = async (files: FileList | null) => {
|
|
|
|
|
|
if (!files?.length) return;
|
fix(chat): scope draft, attachments, and errors per slot
The composer draft, the pending attachment list and the error banner
were single values shared by both slots. Uploading on companion and
then switching to operations routed the companion instance's
attachment ids into the operations instance, the operations error
banner stayed visible on the companion tab, and a half-typed message
followed the user across tabs.
Make all three Record<ChatSlot, ...>, mirroring the sending,
unreadCounts and storedSessions state that was already per slot. Every
asynchronous action now pins its slot at the start, so an upload or a
send that resolves after the user switched tabs still lands on the slot
it belonged to.
Splitting the error state is required rather than incidental. refresh()
runs every 500ms and wrote the shared error on failure and cleared it on
success, so any send or upload error was silently wiped within half a
second. The polling failure describes the IPC channel rather than a
slot, so it keeps its own runtimeError; slotError is owned solely by
user actions and takes precedence when both are present.
All three new tests were checked against a mutation that reads the
companion slot regardless of selection, and all three fail under it.
Refs: F-16 (audit P1-11).
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-27 00:28:27 +08:00
|
|
|
|
// 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;
|
2026-07-20 13:30:29 +08:00
|
|
|
|
setUploading(true);
|
fix(chat): scope draft, attachments, and errors per slot
The composer draft, the pending attachment list and the error banner
were single values shared by both slots. Uploading on companion and
then switching to operations routed the companion instance's
attachment ids into the operations instance, the operations error
banner stayed visible on the companion tab, and a half-typed message
followed the user across tabs.
Make all three Record<ChatSlot, ...>, mirroring the sending,
unreadCounts and storedSessions state that was already per slot. Every
asynchronous action now pins its slot at the start, so an upload or a
send that resolves after the user switched tabs still lands on the slot
it belonged to.
Splitting the error state is required rather than incidental. refresh()
runs every 500ms and wrote the shared error on failure and cleared it on
success, so any send or upload error was silently wiped within half a
second. The polling failure describes the IPC channel rather than a
slot, so it keeps its own runtimeError; slotError is owned solely by
user actions and takes precedence when both are present.
All three new tests were checked against a mutation that reads the
companion slot regardless of selection, and all three fail under it.
Refs: F-16 (audit P1-11).
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-27 00:28:27 +08:00
|
|
|
|
reportSlotError(slot, null);
|
2026-07-20 13:30:29 +08:00
|
|
|
|
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: {
|
fix(chat): scope draft, attachments, and errors per slot
The composer draft, the pending attachment list and the error banner
were single values shared by both slots. Uploading on companion and
then switching to operations routed the companion instance's
attachment ids into the operations instance, the operations error
banner stayed visible on the companion tab, and a half-typed message
followed the user across tabs.
Make all three Record<ChatSlot, ...>, mirroring the sending,
unreadCounts and storedSessions state that was already per slot. Every
asynchronous action now pins its slot at the start, so an upload or a
send that resolves after the user switched tabs still lands on the slot
it belonged to.
Splitting the error state is required rather than incidental. refresh()
runs every 500ms and wrote the shared error on failure and cleared it on
success, so any send or upload error was silently wiped within half a
second. The polling failure describes the IPC channel rather than a
slot, so it keeps its own runtimeError; slotError is owned solely by
user actions and takes precedence when both are present.
All three new tests were checked against a mutation that reads the
companion slot regardless of selection, and all three fail under it.
Refs: F-16 (audit P1-11).
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-27 00:28:27 +08:00
|
|
|
|
slot,
|
2026-07-20 13:30:29 +08:00
|
|
|
|
fileName: file.name,
|
|
|
|
|
|
mimeType: file.type || "application/octet-stream",
|
|
|
|
|
|
bytes,
|
|
|
|
|
|
},
|
|
|
|
|
|
});
|
|
|
|
|
|
uploaded.push(astrbotAttachmentSchema.parse(result));
|
|
|
|
|
|
}
|
fix(chat): scope draft, attachments, and errors per slot
The composer draft, the pending attachment list and the error banner
were single values shared by both slots. Uploading on companion and
then switching to operations routed the companion instance's
attachment ids into the operations instance, the operations error
banner stayed visible on the companion tab, and a half-typed message
followed the user across tabs.
Make all three Record<ChatSlot, ...>, mirroring the sending,
unreadCounts and storedSessions state that was already per slot. Every
asynchronous action now pins its slot at the start, so an upload or a
send that resolves after the user switched tabs still lands on the slot
it belonged to.
Splitting the error state is required rather than incidental. refresh()
runs every 500ms and wrote the shared error on failure and cleared it on
success, so any send or upload error was silently wiped within half a
second. The polling failure describes the IPC channel rather than a
slot, so it keeps its own runtimeError; slotError is owned solely by
user actions and takes precedence when both are present.
All three new tests were checked against a mutation that reads the
companion slot regardless of selection, and all three fail under it.
Refs: F-16 (audit P1-11).
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-27 00:28:27 +08:00
|
|
|
|
setPendingAttachments((current) => ({
|
|
|
|
|
|
...current,
|
|
|
|
|
|
[slot]: current[slot].concat(uploaded),
|
|
|
|
|
|
}));
|
2026-07-20 13:30:29 +08:00
|
|
|
|
} catch (reason) {
|
fix(chat): scope draft, attachments, and errors per slot
The composer draft, the pending attachment list and the error banner
were single values shared by both slots. Uploading on companion and
then switching to operations routed the companion instance's
attachment ids into the operations instance, the operations error
banner stayed visible on the companion tab, and a half-typed message
followed the user across tabs.
Make all three Record<ChatSlot, ...>, mirroring the sending,
unreadCounts and storedSessions state that was already per slot. Every
asynchronous action now pins its slot at the start, so an upload or a
send that resolves after the user switched tabs still lands on the slot
it belonged to.
Splitting the error state is required rather than incidental. refresh()
runs every 500ms and wrote the shared error on failure and cleared it on
success, so any send or upload error was silently wiped within half a
second. The polling failure describes the IPC channel rather than a
slot, so it keeps its own runtimeError; slotError is owned solely by
user actions and takes precedence when both are present.
All three new tests were checked against a mutation that reads the
companion slot regardless of selection, and all three fail under it.
Refs: F-16 (audit P1-11).
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-27 00:28:27 +08:00
|
|
|
|
reportSlotError(slot, errorMessage(reason));
|
2026-07-20 13:30:29 +08:00
|
|
|
|
} finally {
|
|
|
|
|
|
setUploading(false);
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2026-07-21 15:25:11 +08:00
|
|
|
|
const send = async (messageOverride?: string) => {
|
fix(chat): scope draft, attachments, and errors per slot
The composer draft, the pending attachment list and the error banner
were single values shared by both slots. Uploading on companion and
then switching to operations routed the companion instance's
attachment ids into the operations instance, the operations error
banner stayed visible on the companion tab, and a half-typed message
followed the user across tabs.
Make all three Record<ChatSlot, ...>, mirroring the sending,
unreadCounts and storedSessions state that was already per slot. Every
asynchronous action now pins its slot at the start, so an upload or a
send that resolves after the user switched tabs still lands on the slot
it belonged to.
Splitting the error state is required rather than incidental. refresh()
runs every 500ms and wrote the shared error on failure and cleared it on
success, so any send or upload error was silently wiped within half a
second. The polling failure describes the IPC channel rather than a
slot, so it keeps its own runtimeError; slotError is owned solely by
user actions and takes precedence when both are present.
All three new tests were checked against a mutation that reads the
companion slot regardless of selection, and all three fail under it.
Refs: F-16 (audit P1-11).
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-27 00:28:27 +08:00
|
|
|
|
const slot = selectedSlot;
|
|
|
|
|
|
const slotAttachments = pendingAttachments[slot];
|
2026-07-20 13:30:29 +08:00
|
|
|
|
const message =
|
2026-07-21 15:25:11 +08:00
|
|
|
|
messageOverride?.trim() ||
|
fix(chat): scope draft, attachments, and errors per slot
The composer draft, the pending attachment list and the error banner
were single values shared by both slots. Uploading on companion and
then switching to operations routed the companion instance's
attachment ids into the operations instance, the operations error
banner stayed visible on the companion tab, and a half-typed message
followed the user across tabs.
Make all three Record<ChatSlot, ...>, mirroring the sending,
unreadCounts and storedSessions state that was already per slot. Every
asynchronous action now pins its slot at the start, so an upload or a
send that resolves after the user switched tabs still lands on the slot
it belonged to.
Splitting the error state is required rather than incidental. refresh()
runs every 500ms and wrote the shared error on failure and cleared it on
success, so any send or upload error was silently wiped within half a
second. The polling failure describes the IPC channel rather than a
slot, so it keeps its own runtimeError; slotError is owned solely by
user actions and takes precedence when both are present.
All three new tests were checked against a mutation that reads the
companion slot regardless of selection, and all three fail under it.
Refs: F-16 (audit P1-11).
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-27 00:28:27 +08:00
|
|
|
|
draft[slot].trim() ||
|
|
|
|
|
|
(slotAttachments.length > 0 ? "Please check the attachment." : "");
|
|
|
|
|
|
if (!message || uploading || sending[slot]) return;
|
2026-07-20 13:30:29 +08:00
|
|
|
|
const id = `local-${Date.now()}`;
|
fix(chat): scope draft, attachments, and errors per slot
The composer draft, the pending attachment list and the error banner
were single values shared by both slots. Uploading on companion and
then switching to operations routed the companion instance's
attachment ids into the operations instance, the operations error
banner stayed visible on the companion tab, and a half-typed message
followed the user across tabs.
Make all three Record<ChatSlot, ...>, mirroring the sending,
unreadCounts and storedSessions state that was already per slot. Every
asynchronous action now pins its slot at the start, so an upload or a
send that resolves after the user switched tabs still lands on the slot
it belonged to.
Splitting the error state is required rather than incidental. refresh()
runs every 500ms and wrote the shared error on failure and cleared it on
success, so any send or upload error was silently wiped within half a
second. The polling failure describes the IPC channel rather than a
slot, so it keeps its own runtimeError; slotError is owned solely by
user actions and takes precedence when both are present.
All three new tests were checked against a mutation that reads the
companion slot regardless of selection, and all three fail under it.
Refs: F-16 (audit P1-11).
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-27 00:28:27 +08:00
|
|
|
|
const attachments = messageOverride ? [] : slotAttachments;
|
|
|
|
|
|
setDraft((current) => ({ ...current, [slot]: "" }));
|
|
|
|
|
|
if (!messageOverride) {
|
|
|
|
|
|
setPendingAttachments((current) => ({ ...current, [slot]: [] }));
|
|
|
|
|
|
}
|
|
|
|
|
|
setSending((current) => ({ ...current, [slot]: true }));
|
2026-07-20 11:31:41 +08:00
|
|
|
|
setMessages((current) =>
|
2026-07-20 12:30:12 +08:00
|
|
|
|
current.concat({
|
|
|
|
|
|
id,
|
fix(chat): scope draft, attachments, and errors per slot
The composer draft, the pending attachment list and the error banner
were single values shared by both slots. Uploading on companion and
then switching to operations routed the companion instance's
attachment ids into the operations instance, the operations error
banner stayed visible on the companion tab, and a half-typed message
followed the user across tabs.
Make all three Record<ChatSlot, ...>, mirroring the sending,
unreadCounts and storedSessions state that was already per slot. Every
asynchronous action now pins its slot at the start, so an upload or a
send that resolves after the user switched tabs still lands on the slot
it belonged to.
Splitting the error state is required rather than incidental. refresh()
runs every 500ms and wrote the shared error on failure and cleared it on
success, so any send or upload error was silently wiped within half a
second. The polling failure describes the IPC channel rather than a
slot, so it keeps its own runtimeError; slotError is owned solely by
user actions and takes precedence when both are present.
All three new tests were checked against a mutation that reads the
companion slot regardless of selection, and all three fail under it.
Refs: F-16 (audit P1-11).
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-27 00:28:27 +08:00
|
|
|
|
sessionId: status[slot].activeSessionId,
|
|
|
|
|
|
slot,
|
2026-07-20 12:30:12 +08:00
|
|
|
|
role: "user",
|
|
|
|
|
|
content: message,
|
|
|
|
|
|
}),
|
2026-07-20 11:31:41 +08:00
|
|
|
|
);
|
|
|
|
|
|
try {
|
2026-07-21 14:43:14 +08:00
|
|
|
|
const result = slotRuntimeSnapshotSchema.parse(
|
|
|
|
|
|
await invoke<unknown>("send_connection_slot_message", {
|
|
|
|
|
|
request: {
|
fix(chat): scope draft, attachments, and errors per slot
The composer draft, the pending attachment list and the error banner
were single values shared by both slots. Uploading on companion and
then switching to operations routed the companion instance's
attachment ids into the operations instance, the operations error
banner stayed visible on the companion tab, and a half-typed message
followed the user across tabs.
Make all three Record<ChatSlot, ...>, mirroring the sending,
unreadCounts and storedSessions state that was already per slot. Every
asynchronous action now pins its slot at the start, so an upload or a
send that resolves after the user switched tabs still lands on the slot
it belonged to.
Splitting the error state is required rather than incidental. refresh()
runs every 500ms and wrote the shared error on failure and cleared it on
success, so any send or upload error was silently wiped within half a
second. The polling failure describes the IPC channel rather than a
slot, so it keeps its own runtimeError; slotError is owned solely by
user actions and takes precedence when both are present.
All three new tests were checked against a mutation that reads the
companion slot regardless of selection, and all three fail under it.
Refs: F-16 (audit P1-11).
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-27 00:28:27 +08:00
|
|
|
|
slot,
|
2026-07-21 14:43:14 +08:00
|
|
|
|
message,
|
|
|
|
|
|
attachments,
|
|
|
|
|
|
},
|
|
|
|
|
|
}),
|
|
|
|
|
|
);
|
fix(chat): scope draft, attachments, and errors per slot
The composer draft, the pending attachment list and the error banner
were single values shared by both slots. Uploading on companion and
then switching to operations routed the companion instance's
attachment ids into the operations instance, the operations error
banner stayed visible on the companion tab, and a half-typed message
followed the user across tabs.
Make all three Record<ChatSlot, ...>, mirroring the sending,
unreadCounts and storedSessions state that was already per slot. Every
asynchronous action now pins its slot at the start, so an upload or a
send that resolves after the user switched tabs still lands on the slot
it belonged to.
Splitting the error state is required rather than incidental. refresh()
runs every 500ms and wrote the shared error on failure and cleared it on
success, so any send or upload error was silently wiped within half a
second. The polling failure describes the IPC channel rather than a
slot, so it keeps its own runtimeError; slotError is owned solely by
user actions and takes precedence when both are present.
All three new tests were checked against a mutation that reads the
companion slot regardless of selection, and all three fail under it.
Refs: F-16 (audit P1-11).
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-27 00:28:27 +08:00
|
|
|
|
setStatus((current) => ({ ...current, [slot]: result }));
|
2026-07-21 14:43:14 +08:00
|
|
|
|
setMessages((current) =>
|
|
|
|
|
|
current.map((item) =>
|
|
|
|
|
|
item.id === id
|
|
|
|
|
|
? { ...item, sessionId: result.activeSessionId }
|
|
|
|
|
|
: item,
|
|
|
|
|
|
),
|
|
|
|
|
|
);
|
fix(chat): scope draft, attachments, and errors per slot
The composer draft, the pending attachment list and the error banner
were single values shared by both slots. Uploading on companion and
then switching to operations routed the companion instance's
attachment ids into the operations instance, the operations error
banner stayed visible on the companion tab, and a half-typed message
followed the user across tabs.
Make all three Record<ChatSlot, ...>, mirroring the sending,
unreadCounts and storedSessions state that was already per slot. Every
asynchronous action now pins its slot at the start, so an upload or a
send that resolves after the user switched tabs still lands on the slot
it belonged to.
Splitting the error state is required rather than incidental. refresh()
runs every 500ms and wrote the shared error on failure and cleared it on
success, so any send or upload error was silently wiped within half a
second. The polling failure describes the IPC channel rather than a
slot, so it keeps its own runtimeError; slotError is owned solely by
user actions and takes precedence when both are present.
All three new tests were checked against a mutation that reads the
companion slot regardless of selection, and all three fail under it.
Refs: F-16 (audit P1-11).
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-27 00:28:27 +08:00
|
|
|
|
reportSlotError(slot, null);
|
2026-07-20 11:31:41 +08:00
|
|
|
|
} catch (reason) {
|
fix(chat): scope draft, attachments, and errors per slot
The composer draft, the pending attachment list and the error banner
were single values shared by both slots. Uploading on companion and
then switching to operations routed the companion instance's
attachment ids into the operations instance, the operations error
banner stayed visible on the companion tab, and a half-typed message
followed the user across tabs.
Make all three Record<ChatSlot, ...>, mirroring the sending,
unreadCounts and storedSessions state that was already per slot. Every
asynchronous action now pins its slot at the start, so an upload or a
send that resolves after the user switched tabs still lands on the slot
it belonged to.
Splitting the error state is required rather than incidental. refresh()
runs every 500ms and wrote the shared error on failure and cleared it on
success, so any send or upload error was silently wiped within half a
second. The polling failure describes the IPC channel rather than a
slot, so it keeps its own runtimeError; slotError is owned solely by
user actions and takes precedence when both are present.
All three new tests were checked against a mutation that reads the
companion slot regardless of selection, and all three fail under it.
Refs: F-16 (audit P1-11).
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-27 00:28:27 +08:00
|
|
|
|
reportSlotError(slot, errorMessage(reason));
|
2026-07-20 11:31:41 +08:00
|
|
|
|
setMessages((current) =>
|
|
|
|
|
|
current.map((item) =>
|
|
|
|
|
|
item.id === id ? { ...item, error: true } : item,
|
|
|
|
|
|
),
|
|
|
|
|
|
);
|
2026-07-21 15:25:11 +08:00
|
|
|
|
} finally {
|
fix(chat): scope draft, attachments, and errors per slot
The composer draft, the pending attachment list and the error banner
were single values shared by both slots. Uploading on companion and
then switching to operations routed the companion instance's
attachment ids into the operations instance, the operations error
banner stayed visible on the companion tab, and a half-typed message
followed the user across tabs.
Make all three Record<ChatSlot, ...>, mirroring the sending,
unreadCounts and storedSessions state that was already per slot. Every
asynchronous action now pins its slot at the start, so an upload or a
send that resolves after the user switched tabs still lands on the slot
it belonged to.
Splitting the error state is required rather than incidental. refresh()
runs every 500ms and wrote the shared error on failure and cleared it on
success, so any send or upload error was silently wiped within half a
second. The polling failure describes the IPC channel rather than a
slot, so it keeps its own runtimeError; slotError is owned solely by
user actions and takes precedence when both are present.
All three new tests were checked against a mutation that reads the
companion slot regardless of selection, and all three fail under it.
Refs: F-16 (audit P1-11).
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-27 00:28:27 +08:00
|
|
|
|
setSending((current) => ({ ...current, [slot]: false }));
|
2026-07-21 15:25:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const stopGeneration = async () => {
|
fix(chat): scope draft, attachments, and errors per slot
The composer draft, the pending attachment list and the error banner
were single values shared by both slots. Uploading on companion and
then switching to operations routed the companion instance's
attachment ids into the operations instance, the operations error
banner stayed visible on the companion tab, and a half-typed message
followed the user across tabs.
Make all three Record<ChatSlot, ...>, mirroring the sending,
unreadCounts and storedSessions state that was already per slot. Every
asynchronous action now pins its slot at the start, so an upload or a
send that resolves after the user switched tabs still lands on the slot
it belonged to.
Splitting the error state is required rather than incidental. refresh()
runs every 500ms and wrote the shared error on failure and cleared it on
success, so any send or upload error was silently wiped within half a
second. The polling failure describes the IPC channel rather than a
slot, so it keeps its own runtimeError; slotError is owned solely by
user actions and takes precedence when both are present.
All three new tests were checked against a mutation that reads the
companion slot regardless of selection, and all three fail under it.
Refs: F-16 (audit P1-11).
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-27 00:28:27 +08:00
|
|
|
|
const slot = selectedSlot;
|
2026-07-21 15:25:11 +08:00
|
|
|
|
try {
|
|
|
|
|
|
await invoke("cancel_connection_slot_message", {
|
fix(chat): scope draft, attachments, and errors per slot
The composer draft, the pending attachment list and the error banner
were single values shared by both slots. Uploading on companion and
then switching to operations routed the companion instance's
attachment ids into the operations instance, the operations error
banner stayed visible on the companion tab, and a half-typed message
followed the user across tabs.
Make all three Record<ChatSlot, ...>, mirroring the sending,
unreadCounts and storedSessions state that was already per slot. Every
asynchronous action now pins its slot at the start, so an upload or a
send that resolves after the user switched tabs still lands on the slot
it belonged to.
Splitting the error state is required rather than incidental. refresh()
runs every 500ms and wrote the shared error on failure and cleared it on
success, so any send or upload error was silently wiped within half a
second. The polling failure describes the IPC channel rather than a
slot, so it keeps its own runtimeError; slotError is owned solely by
user actions and takes precedence when both are present.
All three new tests were checked against a mutation that reads the
companion slot regardless of selection, and all three fail under it.
Refs: F-16 (audit P1-11).
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-27 00:28:27 +08:00
|
|
|
|
request: { slot },
|
2026-07-21 15:25:11 +08:00
|
|
|
|
});
|
fix(chat): scope draft, attachments, and errors per slot
The composer draft, the pending attachment list and the error banner
were single values shared by both slots. Uploading on companion and
then switching to operations routed the companion instance's
attachment ids into the operations instance, the operations error
banner stayed visible on the companion tab, and a half-typed message
followed the user across tabs.
Make all three Record<ChatSlot, ...>, mirroring the sending,
unreadCounts and storedSessions state that was already per slot. Every
asynchronous action now pins its slot at the start, so an upload or a
send that resolves after the user switched tabs still lands on the slot
it belonged to.
Splitting the error state is required rather than incidental. refresh()
runs every 500ms and wrote the shared error on failure and cleared it on
success, so any send or upload error was silently wiped within half a
second. The polling failure describes the IPC channel rather than a
slot, so it keeps its own runtimeError; slotError is owned solely by
user actions and takes precedence when both are present.
All three new tests were checked against a mutation that reads the
companion slot regardless of selection, and all three fail under it.
Refs: F-16 (audit P1-11).
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-27 00:28:27 +08:00
|
|
|
|
setSending((current) => ({ ...current, [slot]: false }));
|
2026-07-21 15:25:11 +08:00
|
|
|
|
setMessages((current) =>
|
|
|
|
|
|
current.map((item) =>
|
fix(chat): scope draft, attachments, and errors per slot
The composer draft, the pending attachment list and the error banner
were single values shared by both slots. Uploading on companion and
then switching to operations routed the companion instance's
attachment ids into the operations instance, the operations error
banner stayed visible on the companion tab, and a half-typed message
followed the user across tabs.
Make all three Record<ChatSlot, ...>, mirroring the sending,
unreadCounts and storedSessions state that was already per slot. Every
asynchronous action now pins its slot at the start, so an upload or a
send that resolves after the user switched tabs still lands on the slot
it belonged to.
Splitting the error state is required rather than incidental. refresh()
runs every 500ms and wrote the shared error on failure and cleared it on
success, so any send or upload error was silently wiped within half a
second. The polling failure describes the IPC channel rather than a
slot, so it keeps its own runtimeError; slotError is owned solely by
user actions and takes precedence when both are present.
All three new tests were checked against a mutation that reads the
companion slot regardless of selection, and all three fail under it.
Refs: F-16 (audit P1-11).
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-27 00:28:27 +08:00
|
|
|
|
item.slot === slot && item.streaming
|
2026-07-21 15:25:11 +08:00
|
|
|
|
? {
|
|
|
|
|
|
...item,
|
|
|
|
|
|
streaming: false,
|
|
|
|
|
|
content: item.content || "Generation stopped.",
|
|
|
|
|
|
error: true,
|
|
|
|
|
|
}
|
|
|
|
|
|
: item,
|
|
|
|
|
|
),
|
|
|
|
|
|
);
|
fix(chat): scope draft, attachments, and errors per slot
The composer draft, the pending attachment list and the error banner
were single values shared by both slots. Uploading on companion and
then switching to operations routed the companion instance's
attachment ids into the operations instance, the operations error
banner stayed visible on the companion tab, and a half-typed message
followed the user across tabs.
Make all three Record<ChatSlot, ...>, mirroring the sending,
unreadCounts and storedSessions state that was already per slot. Every
asynchronous action now pins its slot at the start, so an upload or a
send that resolves after the user switched tabs still lands on the slot
it belonged to.
Splitting the error state is required rather than incidental. refresh()
runs every 500ms and wrote the shared error on failure and cleared it on
success, so any send or upload error was silently wiped within half a
second. The polling failure describes the IPC channel rather than a
slot, so it keeps its own runtimeError; slotError is owned solely by
user actions and takes precedence when both are present.
All three new tests were checked against a mutation that reads the
companion slot regardless of selection, and all three fail under it.
Refs: F-16 (audit P1-11).
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-27 00:28:27 +08:00
|
|
|
|
reportSlotError(slot, null);
|
2026-07-21 15:25:11 +08:00
|
|
|
|
} catch (reason) {
|
fix(chat): scope draft, attachments, and errors per slot
The composer draft, the pending attachment list and the error banner
were single values shared by both slots. Uploading on companion and
then switching to operations routed the companion instance's
attachment ids into the operations instance, the operations error
banner stayed visible on the companion tab, and a half-typed message
followed the user across tabs.
Make all three Record<ChatSlot, ...>, mirroring the sending,
unreadCounts and storedSessions state that was already per slot. Every
asynchronous action now pins its slot at the start, so an upload or a
send that resolves after the user switched tabs still lands on the slot
it belonged to.
Splitting the error state is required rather than incidental. refresh()
runs every 500ms and wrote the shared error on failure and cleared it on
success, so any send or upload error was silently wiped within half a
second. The polling failure describes the IPC channel rather than a
slot, so it keeps its own runtimeError; slotError is owned solely by
user actions and takes precedence when both are present.
All three new tests were checked against a mutation that reads the
companion slot regardless of selection, and all three fail under it.
Refs: F-16 (audit P1-11).
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-27 00:28:27 +08:00
|
|
|
|
reportSlotError(slot, errorMessage(reason));
|
2026-07-21 15:25:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const copyMessage = async (content: string) => {
|
fix(chat): scope draft, attachments, and errors per slot
The composer draft, the pending attachment list and the error banner
were single values shared by both slots. Uploading on companion and
then switching to operations routed the companion instance's
attachment ids into the operations instance, the operations error
banner stayed visible on the companion tab, and a half-typed message
followed the user across tabs.
Make all three Record<ChatSlot, ...>, mirroring the sending,
unreadCounts and storedSessions state that was already per slot. Every
asynchronous action now pins its slot at the start, so an upload or a
send that resolves after the user switched tabs still lands on the slot
it belonged to.
Splitting the error state is required rather than incidental. refresh()
runs every 500ms and wrote the shared error on failure and cleared it on
success, so any send or upload error was silently wiped within half a
second. The polling failure describes the IPC channel rather than a
slot, so it keeps its own runtimeError; slotError is owned solely by
user actions and takes precedence when both are present.
All three new tests were checked against a mutation that reads the
companion slot regardless of selection, and all three fail under it.
Refs: F-16 (audit P1-11).
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-27 00:28:27 +08:00
|
|
|
|
const slot = selectedSlot;
|
2026-07-21 15:25:11 +08:00
|
|
|
|
try {
|
|
|
|
|
|
await navigator.clipboard.writeText(content);
|
fix(chat): scope draft, attachments, and errors per slot
The composer draft, the pending attachment list and the error banner
were single values shared by both slots. Uploading on companion and
then switching to operations routed the companion instance's
attachment ids into the operations instance, the operations error
banner stayed visible on the companion tab, and a half-typed message
followed the user across tabs.
Make all three Record<ChatSlot, ...>, mirroring the sending,
unreadCounts and storedSessions state that was already per slot. Every
asynchronous action now pins its slot at the start, so an upload or a
send that resolves after the user switched tabs still lands on the slot
it belonged to.
Splitting the error state is required rather than incidental. refresh()
runs every 500ms and wrote the shared error on failure and cleared it on
success, so any send or upload error was silently wiped within half a
second. The polling failure describes the IPC channel rather than a
slot, so it keeps its own runtimeError; slotError is owned solely by
user actions and takes precedence when both are present.
All three new tests were checked against a mutation that reads the
companion slot regardless of selection, and all three fail under it.
Refs: F-16 (audit P1-11).
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-27 00:28:27 +08:00
|
|
|
|
reportSlotError(slot, null);
|
2026-07-21 15:25:11 +08:00
|
|
|
|
} catch {
|
fix(chat): scope draft, attachments, and errors per slot
The composer draft, the pending attachment list and the error banner
were single values shared by both slots. Uploading on companion and
then switching to operations routed the companion instance's
attachment ids into the operations instance, the operations error
banner stayed visible on the companion tab, and a half-typed message
followed the user across tabs.
Make all three Record<ChatSlot, ...>, mirroring the sending,
unreadCounts and storedSessions state that was already per slot. Every
asynchronous action now pins its slot at the start, so an upload or a
send that resolves after the user switched tabs still lands on the slot
it belonged to.
Splitting the error state is required rather than incidental. refresh()
runs every 500ms and wrote the shared error on failure and cleared it on
success, so any send or upload error was silently wiped within half a
second. The polling failure describes the IPC channel rather than a
slot, so it keeps its own runtimeError; slotError is owned solely by
user actions and takes precedence when both are present.
All three new tests were checked against a mutation that reads the
companion slot regardless of selection, and all three fail under it.
Refs: F-16 (audit P1-11).
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-27 00:28:27 +08:00
|
|
|
|
reportSlotError(slot, "复制失败,请检查剪贴板权限。");
|
2026-07-21 15:25:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
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 () => {
|
fix(chat): scope draft, attachments, and errors per slot
The composer draft, the pending attachment list and the error banner
were single values shared by both slots. Uploading on companion and
then switching to operations routed the companion instance's
attachment ids into the operations instance, the operations error
banner stayed visible on the companion tab, and a half-typed message
followed the user across tabs.
Make all three Record<ChatSlot, ...>, mirroring the sending,
unreadCounts and storedSessions state that was already per slot. Every
asynchronous action now pins its slot at the start, so an upload or a
send that resolves after the user switched tabs still lands on the slot
it belonged to.
Splitting the error state is required rather than incidental. refresh()
runs every 500ms and wrote the shared error on failure and cleared it on
success, so any send or upload error was silently wiped within half a
second. The polling failure describes the IPC channel rather than a
slot, so it keeps its own runtimeError; slotError is owned solely by
user actions and takes precedence when both are present.
All three new tests were checked against a mutation that reads the
companion slot regardless of selection, and all three fail under it.
Refs: F-16 (audit P1-11).
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-27 00:28:27 +08:00
|
|
|
|
const slot = selectedSlot;
|
|
|
|
|
|
if (!status[slot].state || status[slot].state === "disconnected") {
|
|
|
|
|
|
reportSlotError(slot, "Connect the selected slot first.");
|
2026-07-21 15:25:11 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
try {
|
|
|
|
|
|
const sessionId = crypto.randomUUID();
|
|
|
|
|
|
const result = slotRuntimeSnapshotSchema.parse(
|
|
|
|
|
|
await invoke<unknown>("switch_connection_slot_session", {
|
fix(chat): scope draft, attachments, and errors per slot
The composer draft, the pending attachment list and the error banner
were single values shared by both slots. Uploading on companion and
then switching to operations routed the companion instance's
attachment ids into the operations instance, the operations error
banner stayed visible on the companion tab, and a half-typed message
followed the user across tabs.
Make all three Record<ChatSlot, ...>, mirroring the sending,
unreadCounts and storedSessions state that was already per slot. Every
asynchronous action now pins its slot at the start, so an upload or a
send that resolves after the user switched tabs still lands on the slot
it belonged to.
Splitting the error state is required rather than incidental. refresh()
runs every 500ms and wrote the shared error on failure and cleared it on
success, so any send or upload error was silently wiped within half a
second. The polling failure describes the IPC channel rather than a
slot, so it keeps its own runtimeError; slotError is owned solely by
user actions and takes precedence when both are present.
All three new tests were checked against a mutation that reads the
companion slot regardless of selection, and all three fail under it.
Refs: F-16 (audit P1-11).
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-27 00:28:27 +08:00
|
|
|
|
request: { slot, sessionId },
|
2026-07-21 15:25:11 +08:00
|
|
|
|
}),
|
|
|
|
|
|
);
|
|
|
|
|
|
setMessages((current) =>
|
|
|
|
|
|
current.filter(
|
|
|
|
|
|
(item) =>
|
fix(chat): scope draft, attachments, and errors per slot
The composer draft, the pending attachment list and the error banner
were single values shared by both slots. Uploading on companion and
then switching to operations routed the companion instance's
attachment ids into the operations instance, the operations error
banner stayed visible on the companion tab, and a half-typed message
followed the user across tabs.
Make all three Record<ChatSlot, ...>, mirroring the sending,
unreadCounts and storedSessions state that was already per slot. Every
asynchronous action now pins its slot at the start, so an upload or a
send that resolves after the user switched tabs still lands on the slot
it belonged to.
Splitting the error state is required rather than incidental. refresh()
runs every 500ms and wrote the shared error on failure and cleared it on
success, so any send or upload error was silently wiped within half a
second. The polling failure describes the IPC channel rather than a
slot, so it keeps its own runtimeError; slotError is owned solely by
user actions and takes precedence when both are present.
All three new tests were checked against a mutation that reads the
companion slot regardless of selection, and all three fail under it.
Refs: F-16 (audit P1-11).
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-27 00:28:27 +08:00
|
|
|
|
item.slot !== slot ||
|
|
|
|
|
|
item.sessionId !== status[slot].activeSessionId,
|
2026-07-21 15:25:11 +08:00
|
|
|
|
),
|
|
|
|
|
|
);
|
fix(chat): scope draft, attachments, and errors per slot
The composer draft, the pending attachment list and the error banner
were single values shared by both slots. Uploading on companion and
then switching to operations routed the companion instance's
attachment ids into the operations instance, the operations error
banner stayed visible on the companion tab, and a half-typed message
followed the user across tabs.
Make all three Record<ChatSlot, ...>, mirroring the sending,
unreadCounts and storedSessions state that was already per slot. Every
asynchronous action now pins its slot at the start, so an upload or a
send that resolves after the user switched tabs still lands on the slot
it belonged to.
Splitting the error state is required rather than incidental. refresh()
runs every 500ms and wrote the shared error on failure and cleared it on
success, so any send or upload error was silently wiped within half a
second. The polling failure describes the IPC channel rather than a
slot, so it keeps its own runtimeError; slotError is owned solely by
user actions and takes precedence when both are present.
All three new tests were checked against a mutation that reads the
companion slot regardless of selection, and all three fail under it.
Refs: F-16 (audit P1-11).
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-27 00:28:27 +08:00
|
|
|
|
setStatus((current) => ({ ...current, [slot]: result }));
|
|
|
|
|
|
reportSlotError(slot, null);
|
2026-07-21 15:25:11 +08:00
|
|
|
|
} catch (reason) {
|
fix(chat): scope draft, attachments, and errors per slot
The composer draft, the pending attachment list and the error banner
were single values shared by both slots. Uploading on companion and
then switching to operations routed the companion instance's
attachment ids into the operations instance, the operations error
banner stayed visible on the companion tab, and a half-typed message
followed the user across tabs.
Make all three Record<ChatSlot, ...>, mirroring the sending,
unreadCounts and storedSessions state that was already per slot. Every
asynchronous action now pins its slot at the start, so an upload or a
send that resolves after the user switched tabs still lands on the slot
it belonged to.
Splitting the error state is required rather than incidental. refresh()
runs every 500ms and wrote the shared error on failure and cleared it on
success, so any send or upload error was silently wiped within half a
second. The polling failure describes the IPC channel rather than a
slot, so it keeps its own runtimeError; slotError is owned solely by
user actions and takes precedence when both are present.
All three new tests were checked against a mutation that reads the
companion slot regardless of selection, and all three fail under it.
Refs: F-16 (audit P1-11).
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-27 00:28:27 +08:00
|
|
|
|
reportSlotError(slot, errorMessage(reason));
|
2026-07-21 15:25:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const refreshConnection = async () => {
|
|
|
|
|
|
await refresh();
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const reconnectConnection = async () => {
|
fix(chat): scope draft, attachments, and errors per slot
The composer draft, the pending attachment list and the error banner
were single values shared by both slots. Uploading on companion and
then switching to operations routed the companion instance's
attachment ids into the operations instance, the operations error
banner stayed visible on the companion tab, and a half-typed message
followed the user across tabs.
Make all three Record<ChatSlot, ...>, mirroring the sending,
unreadCounts and storedSessions state that was already per slot. Every
asynchronous action now pins its slot at the start, so an upload or a
send that resolves after the user switched tabs still lands on the slot
it belonged to.
Splitting the error state is required rather than incidental. refresh()
runs every 500ms and wrote the shared error on failure and cleared it on
success, so any send or upload error was silently wiped within half a
second. The polling failure describes the IPC channel rather than a
slot, so it keeps its own runtimeError; slotError is owned solely by
user actions and takes precedence when both are present.
All three new tests were checked against a mutation that reads the
companion slot regardless of selection, and all three fail under it.
Refs: F-16 (audit P1-11).
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-27 00:28:27 +08:00
|
|
|
|
const slot = selectedSlot;
|
|
|
|
|
|
const profileId = status[slot].profileId;
|
2026-07-21 15:25:11 +08:00
|
|
|
|
if (!profileId) {
|
fix(chat): scope draft, attachments, and errors per slot
The composer draft, the pending attachment list and the error banner
were single values shared by both slots. Uploading on companion and
then switching to operations routed the companion instance's
attachment ids into the operations instance, the operations error
banner stayed visible on the companion tab, and a half-typed message
followed the user across tabs.
Make all three Record<ChatSlot, ...>, mirroring the sending,
unreadCounts and storedSessions state that was already per slot. Every
asynchronous action now pins its slot at the start, so an upload or a
send that resolves after the user switched tabs still lands on the slot
it belonged to.
Splitting the error state is required rather than incidental. refresh()
runs every 500ms and wrote the shared error on failure and cleared it on
success, so any send or upload error was silently wiped within half a
second. The polling failure describes the IPC channel rather than a
slot, so it keeps its own runtimeError; slotError is owned solely by
user actions and takes precedence when both are present.
All three new tests were checked against a mutation that reads the
companion slot regardless of selection, and all three fail under it.
Refs: F-16 (audit P1-11).
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-27 00:28:27 +08:00
|
|
|
|
reportSlotError(slot, "No Profile is assigned to the selected slot.");
|
2026-07-21 15:25:11 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
try {
|
|
|
|
|
|
const result = slotRuntimeSnapshotSchema.parse(
|
|
|
|
|
|
await invoke<unknown>("probe_connection_slot", {
|
fix(chat): scope draft, attachments, and errors per slot
The composer draft, the pending attachment list and the error banner
were single values shared by both slots. Uploading on companion and
then switching to operations routed the companion instance's
attachment ids into the operations instance, the operations error
banner stayed visible on the companion tab, and a half-typed message
followed the user across tabs.
Make all three Record<ChatSlot, ...>, mirroring the sending,
unreadCounts and storedSessions state that was already per slot. Every
asynchronous action now pins its slot at the start, so an upload or a
send that resolves after the user switched tabs still lands on the slot
it belonged to.
Splitting the error state is required rather than incidental. refresh()
runs every 500ms and wrote the shared error on failure and cleared it on
success, so any send or upload error was silently wiped within half a
second. The polling failure describes the IPC channel rather than a
slot, so it keeps its own runtimeError; slotError is owned solely by
user actions and takes precedence when both are present.
All three new tests were checked against a mutation that reads the
companion slot regardless of selection, and all three fail under it.
Refs: F-16 (audit P1-11).
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-27 00:28:27 +08:00
|
|
|
|
request: { slot, profileId },
|
2026-07-21 15:25:11 +08:00
|
|
|
|
}),
|
|
|
|
|
|
);
|
fix(chat): scope draft, attachments, and errors per slot
The composer draft, the pending attachment list and the error banner
were single values shared by both slots. Uploading on companion and
then switching to operations routed the companion instance's
attachment ids into the operations instance, the operations error
banner stayed visible on the companion tab, and a half-typed message
followed the user across tabs.
Make all three Record<ChatSlot, ...>, mirroring the sending,
unreadCounts and storedSessions state that was already per slot. Every
asynchronous action now pins its slot at the start, so an upload or a
send that resolves after the user switched tabs still lands on the slot
it belonged to.
Splitting the error state is required rather than incidental. refresh()
runs every 500ms and wrote the shared error on failure and cleared it on
success, so any send or upload error was silently wiped within half a
second. The polling failure describes the IPC channel rather than a
slot, so it keeps its own runtimeError; slotError is owned solely by
user actions and takes precedence when both are present.
All three new tests were checked against a mutation that reads the
companion slot regardless of selection, and all three fail under it.
Refs: F-16 (audit P1-11).
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-27 00:28:27 +08:00
|
|
|
|
setStatus((current) => ({ ...current, [slot]: result }));
|
|
|
|
|
|
reportSlotError(slot, null);
|
2026-07-21 15:25:11 +08:00
|
|
|
|
} catch (reason) {
|
fix(chat): scope draft, attachments, and errors per slot
The composer draft, the pending attachment list and the error banner
were single values shared by both slots. Uploading on companion and
then switching to operations routed the companion instance's
attachment ids into the operations instance, the operations error
banner stayed visible on the companion tab, and a half-typed message
followed the user across tabs.
Make all three Record<ChatSlot, ...>, mirroring the sending,
unreadCounts and storedSessions state that was already per slot. Every
asynchronous action now pins its slot at the start, so an upload or a
send that resolves after the user switched tabs still lands on the slot
it belonged to.
Splitting the error state is required rather than incidental. refresh()
runs every 500ms and wrote the shared error on failure and cleared it on
success, so any send or upload error was silently wiped within half a
second. The polling failure describes the IPC channel rather than a
slot, so it keeps its own runtimeError; slotError is owned solely by
user actions and takes precedence when both are present.
All three new tests were checked against a mutation that reads the
companion slot regardless of selection, and all three fail under it.
Refs: F-16 (audit P1-11).
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-27 00:28:27 +08:00
|
|
|
|
reportSlotError(slot, errorMessage(reason));
|
2026-07-21 15:25:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const disconnectConnection = async () => {
|
fix(chat): scope draft, attachments, and errors per slot
The composer draft, the pending attachment list and the error banner
were single values shared by both slots. Uploading on companion and
then switching to operations routed the companion instance's
attachment ids into the operations instance, the operations error
banner stayed visible on the companion tab, and a half-typed message
followed the user across tabs.
Make all three Record<ChatSlot, ...>, mirroring the sending,
unreadCounts and storedSessions state that was already per slot. Every
asynchronous action now pins its slot at the start, so an upload or a
send that resolves after the user switched tabs still lands on the slot
it belonged to.
Splitting the error state is required rather than incidental. refresh()
runs every 500ms and wrote the shared error on failure and cleared it on
success, so any send or upload error was silently wiped within half a
second. The polling failure describes the IPC channel rather than a
slot, so it keeps its own runtimeError; slotError is owned solely by
user actions and takes precedence when both are present.
All three new tests were checked against a mutation that reads the
companion slot regardless of selection, and all three fail under it.
Refs: F-16 (audit P1-11).
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-27 00:28:27 +08:00
|
|
|
|
const slot = selectedSlot;
|
2026-07-21 15:25:11 +08:00
|
|
|
|
try {
|
|
|
|
|
|
await invoke("disconnect_connection_slot", {
|
fix(chat): scope draft, attachments, and errors per slot
The composer draft, the pending attachment list and the error banner
were single values shared by both slots. Uploading on companion and
then switching to operations routed the companion instance's
attachment ids into the operations instance, the operations error
banner stayed visible on the companion tab, and a half-typed message
followed the user across tabs.
Make all three Record<ChatSlot, ...>, mirroring the sending,
unreadCounts and storedSessions state that was already per slot. Every
asynchronous action now pins its slot at the start, so an upload or a
send that resolves after the user switched tabs still lands on the slot
it belonged to.
Splitting the error state is required rather than incidental. refresh()
runs every 500ms and wrote the shared error on failure and cleared it on
success, so any send or upload error was silently wiped within half a
second. The polling failure describes the IPC channel rather than a
slot, so it keeps its own runtimeError; slotError is owned solely by
user actions and takes precedence when both are present.
All three new tests were checked against a mutation that reads the
companion slot regardless of selection, and all three fail under it.
Refs: F-16 (audit P1-11).
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-27 00:28:27 +08:00
|
|
|
|
request: { slot },
|
2026-07-21 15:25:11 +08:00
|
|
|
|
});
|
|
|
|
|
|
await refresh();
|
|
|
|
|
|
} catch (reason) {
|
fix(chat): scope draft, attachments, and errors per slot
The composer draft, the pending attachment list and the error banner
were single values shared by both slots. Uploading on companion and
then switching to operations routed the companion instance's
attachment ids into the operations instance, the operations error
banner stayed visible on the companion tab, and a half-typed message
followed the user across tabs.
Make all three Record<ChatSlot, ...>, mirroring the sending,
unreadCounts and storedSessions state that was already per slot. Every
asynchronous action now pins its slot at the start, so an upload or a
send that resolves after the user switched tabs still lands on the slot
it belonged to.
Splitting the error state is required rather than incidental. refresh()
runs every 500ms and wrote the shared error on failure and cleared it on
success, so any send or upload error was silently wiped within half a
second. The polling failure describes the IPC channel rather than a
slot, so it keeps its own runtimeError; slotError is owned solely by
user actions and takes precedence when both are present.
All three new tests were checked against a mutation that reads the
companion slot regardless of selection, and all three fail under it.
Refs: F-16 (audit P1-11).
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-27 00:28:27 +08:00
|
|
|
|
reportSlotError(slot, errorMessage(reason));
|
2026-07-20 11:31:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2026-07-21 14:43:14 +08:00
|
|
|
|
const toggleVoiceInput = () => {
|
fix(chat): scope draft, attachments, and errors per slot
The composer draft, the pending attachment list and the error banner
were single values shared by both slots. Uploading on companion and
then switching to operations routed the companion instance's
attachment ids into the operations instance, the operations error
banner stayed visible on the companion tab, and a half-typed message
followed the user across tabs.
Make all three Record<ChatSlot, ...>, mirroring the sending,
unreadCounts and storedSessions state that was already per slot. Every
asynchronous action now pins its slot at the start, so an upload or a
send that resolves after the user switched tabs still lands on the slot
it belonged to.
Splitting the error state is required rather than incidental. refresh()
runs every 500ms and wrote the shared error on failure and cleared it on
success, so any send or upload error was silently wiped within half a
second. The polling failure describes the IPC channel rather than a
slot, so it keeps its own runtimeError; slotError is owned solely by
user actions and takes precedence when both are present.
All three new tests were checked against a mutation that reads the
companion slot regardless of selection, and all three fail under it.
Refs: F-16 (audit P1-11).
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-27 00:28:27 +08:00
|
|
|
|
const slot = selectedSlot;
|
2026-07-21 14:43:14 +08:00
|
|
|
|
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) {
|
fix(chat): scope draft, attachments, and errors per slot
The composer draft, the pending attachment list and the error banner
were single values shared by both slots. Uploading on companion and
then switching to operations routed the companion instance's
attachment ids into the operations instance, the operations error
banner stayed visible on the companion tab, and a half-typed message
followed the user across tabs.
Make all three Record<ChatSlot, ...>, mirroring the sending,
unreadCounts and storedSessions state that was already per slot. Every
asynchronous action now pins its slot at the start, so an upload or a
send that resolves after the user switched tabs still lands on the slot
it belonged to.
Splitting the error state is required rather than incidental. refresh()
runs every 500ms and wrote the shared error on failure and cleared it on
success, so any send or upload error was silently wiped within half a
second. The polling failure describes the IPC channel rather than a
slot, so it keeps its own runtimeError; slotError is owned solely by
user actions and takes precedence when both are present.
All three new tests were checked against a mutation that reads the
companion slot regardless of selection, and all three fail under it.
Refs: F-16 (audit P1-11).
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-27 00:28:27 +08:00
|
|
|
|
reportSlotError(slot, "当前 WebView 不支持语音输入。");
|
2026-07-21 14:43:14 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
const recognition = new Recognition();
|
|
|
|
|
|
recognition.lang = "zh-CN";
|
|
|
|
|
|
recognition.interimResults = true;
|
|
|
|
|
|
recognition.onresult = (event) => {
|
|
|
|
|
|
const result = event as {
|
|
|
|
|
|
results?: ArrayLike<ArrayLike<{ transcript?: string }>>;
|
|
|
|
|
|
};
|
fix(chat): scope draft, attachments, and errors per slot
The composer draft, the pending attachment list and the error banner
were single values shared by both slots. Uploading on companion and
then switching to operations routed the companion instance's
attachment ids into the operations instance, the operations error
banner stayed visible on the companion tab, and a half-typed message
followed the user across tabs.
Make all three Record<ChatSlot, ...>, mirroring the sending,
unreadCounts and storedSessions state that was already per slot. Every
asynchronous action now pins its slot at the start, so an upload or a
send that resolves after the user switched tabs still lands on the slot
it belonged to.
Splitting the error state is required rather than incidental. refresh()
runs every 500ms and wrote the shared error on failure and cleared it on
success, so any send or upload error was silently wiped within half a
second. The polling failure describes the IPC channel rather than a
slot, so it keeps its own runtimeError; slotError is owned solely by
user actions and takes precedence when both are present.
All three new tests were checked against a mutation that reads the
companion slot regardless of selection, and all three fail under it.
Refs: F-16 (audit P1-11).
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-27 00:28:27 +08:00
|
|
|
|
const transcript = Array.from(result.results ?? [])
|
|
|
|
|
|
.map((item) => item[0]?.transcript ?? "")
|
|
|
|
|
|
.join("");
|
|
|
|
|
|
setDraft((current) => ({ ...current, [slot]: transcript }));
|
2026-07-21 14:43:14 +08:00
|
|
|
|
};
|
|
|
|
|
|
recognition.onerror = () => {
|
fix(chat): scope draft, attachments, and errors per slot
The composer draft, the pending attachment list and the error banner
were single values shared by both slots. Uploading on companion and
then switching to operations routed the companion instance's
attachment ids into the operations instance, the operations error
banner stayed visible on the companion tab, and a half-typed message
followed the user across tabs.
Make all three Record<ChatSlot, ...>, mirroring the sending,
unreadCounts and storedSessions state that was already per slot. Every
asynchronous action now pins its slot at the start, so an upload or a
send that resolves after the user switched tabs still lands on the slot
it belonged to.
Splitting the error state is required rather than incidental. refresh()
runs every 500ms and wrote the shared error on failure and cleared it on
success, so any send or upload error was silently wiped within half a
second. The polling failure describes the IPC channel rather than a
slot, so it keeps its own runtimeError; slotError is owned solely by
user actions and takes precedence when both are present.
All three new tests were checked against a mutation that reads the
companion slot regardless of selection, and all three fail under it.
Refs: F-16 (audit P1-11).
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-27 00:28:27 +08:00
|
|
|
|
reportSlotError(slot, "语音输入失败,请检查麦克风权限。");
|
2026-07-21 14:43:14 +08:00
|
|
|
|
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);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2026-07-20 11:31:41 +08:00
|
|
|
|
return (
|
|
|
|
|
|
<section className="chat-panel" aria-label="聊天面板">
|
|
|
|
|
|
<div className="chat-heading">
|
|
|
|
|
|
<div>
|
2026-07-22 02:52:31 +08:00
|
|
|
|
<p className="eyebrow">共享聊天运行时</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}
|
2026-07-22 00:27:06 +08:00
|
|
|
|
|
|
|
|
|
|
aria-controls={`chat-panel-${slot}`}
|
|
|
|
|
|
|
|
|
|
|
|
tabIndex={fixedSlot || activeSlot === slot ? 0 : -1}
|
|
|
|
|
|
|
|
|
|
|
|
onKeyDown={(event) => {
|
|
|
|
|
|
if (event.key !== "ArrowRight" && event.key !== "ArrowLeft")
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
|
|
|
|
setActiveSlot(
|
|
|
|
|
|
slot === "companion" ? "operations" : "companion",
|
|
|
|
|
|
);
|
|
|
|
|
|
}}
|
|
|
|
|
|
|
2026-07-20 11:31:41 +08:00
|
|
|
|
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-21 15:25:11 +08:00
|
|
|
|
<div>
|
2026-07-22 02:52:31 +08:00
|
|
|
|
<span>配置</span>
|
|
|
|
|
|
<strong>{status[selectedSlot].profileId ?? "未连接"}</strong>
|
2026-07-21 15:25:11 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
<div>
|
2026-07-22 02:52:31 +08:00
|
|
|
|
<span>会话</span>
|
|
|
|
|
|
<strong>{status[selectedSlot].activeSessionId ?? "新会话"}</strong>
|
2026-07-21 15:25:11 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
<div>
|
2026-07-22 02:52:31 +08:00
|
|
|
|
<span>状态</span>
|
2026-07-21 15:25:11 +08:00
|
|
|
|
<strong>{connectionStateLabel(status[selectedSlot].state)}</strong>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<div className="chat-runtime-actions">
|
|
|
|
|
|
<button type="button" onClick={() => void refreshConnection()}>
|
2026-07-22 02:52:31 +08:00
|
|
|
|
刷新
|
2026-07-21 15:25:11 +08:00
|
|
|
|
</button>
|
|
|
|
|
|
<button type="button" onClick={() => void reconnectConnection()}>
|
2026-07-22 02:52:31 +08:00
|
|
|
|
重连
|
2026-07-21 15:25:11 +08:00
|
|
|
|
</button>
|
|
|
|
|
|
<button
|
|
|
|
|
|
type="button"
|
|
|
|
|
|
onClick={() => void disconnectConnection()}
|
|
|
|
|
|
disabled={status[selectedSlot].state === "disconnected"}
|
|
|
|
|
|
>
|
2026-07-22 02:52:31 +08:00
|
|
|
|
断开
|
2026-07-21 15:25:11 +08:00
|
|
|
|
</button>
|
|
|
|
|
|
<button type="button" onClick={() => void startNewSession()}>
|
2026-07-22 02:52:31 +08:00
|
|
|
|
新对话
|
2026-07-21 15:25:11 +08:00
|
|
|
|
</button>
|
|
|
|
|
|
</div>
|
2026-07-20 11:31:41 +08:00
|
|
|
|
</div>
|
2026-07-21 15:25:11 +08:00
|
|
|
|
{status[selectedSlot].lastError ? (
|
|
|
|
|
|
<p className="chat-runtime-error" role="status">
|
|
|
|
|
|
{status[selectedSlot].lastError}
|
|
|
|
|
|
</p>
|
|
|
|
|
|
) : null}
|
2026-07-20 16:52:29 +08:00
|
|
|
|
{status[selectedSlot].state === "authExpired" ? (
|
|
|
|
|
|
<div className="warning-banner" role="alert">
|
|
|
|
|
|
<strong>认证已过期</strong>
|
2026-07-21 14:43:14 +08:00
|
|
|
|
<span>
|
|
|
|
|
|
请到“连接”设置重新登录 AstrBot,当前槽位不会继续发送消息。
|
|
|
|
|
|
</span>
|
2026-07-20 16:52:29 +08:00
|
|
|
|
<button type="button" onClick={() => void invoke("open_main_window")}>
|
|
|
|
|
|
打开连接设置
|
|
|
|
|
|
</button>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
) : null}
|
2026-07-22 02:52:31 +08:00
|
|
|
|
{displaySessions.length > 0 ? (
|
2026-07-20 14:32:07 +08:00
|
|
|
|
<div className="chat-session-list" aria-label="Chat sessions">
|
2026-07-22 02:52:31 +08:00
|
|
|
|
{displaySessions.map((session, index) => (
|
2026-07-20 14:32:07 +08:00
|
|
|
|
<button
|
|
|
|
|
|
className={
|
|
|
|
|
|
session.sessionId === status[selectedSlot].activeSessionId
|
|
|
|
|
|
? "chat-session active"
|
|
|
|
|
|
: "chat-session"
|
|
|
|
|
|
}
|
|
|
|
|
|
key={session.sessionId}
|
2026-07-22 02:52:31 +08:00
|
|
|
|
title={`会话标识:${session.sessionId}`}
|
2026-07-20 14:32:07 +08:00
|
|
|
|
type="button"
|
|
|
|
|
|
onClick={() => {
|
|
|
|
|
|
void invoke("switch_connection_slot_session", {
|
|
|
|
|
|
request: { slot: selectedSlot, sessionId: session.sessionId },
|
|
|
|
|
|
})
|
|
|
|
|
|
.then(() => void refresh())
|
|
|
|
|
|
.catch((reason) =>
|
fix(chat): scope draft, attachments, and errors per slot
The composer draft, the pending attachment list and the error banner
were single values shared by both slots. Uploading on companion and
then switching to operations routed the companion instance's
attachment ids into the operations instance, the operations error
banner stayed visible on the companion tab, and a half-typed message
followed the user across tabs.
Make all three Record<ChatSlot, ...>, mirroring the sending,
unreadCounts and storedSessions state that was already per slot. Every
asynchronous action now pins its slot at the start, so an upload or a
send that resolves after the user switched tabs still lands on the slot
it belonged to.
Splitting the error state is required rather than incidental. refresh()
runs every 500ms and wrote the shared error on failure and cleared it on
success, so any send or upload error was silently wiped within half a
second. The polling failure describes the IPC channel rather than a
slot, so it keeps its own runtimeError; slotError is owned solely by
user actions and takes precedence when both are present.
All three new tests were checked against a mutation that reads the
companion slot regardless of selection, and all three fail under it.
Refs: F-16 (audit P1-11).
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-27 00:28:27 +08:00
|
|
|
|
reportSlotError(selectedSlot, errorMessage(reason)),
|
2026-07-20 14:32:07 +08:00
|
|
|
|
);
|
|
|
|
|
|
}}
|
|
|
|
|
|
>
|
2026-07-22 02:52:31 +08:00
|
|
|
|
<strong>{session.displayName || `会话 ${index + 1}`}</strong>
|
|
|
|
|
|
<small>{formatSessionTime(session.updatedAt)}</small>
|
2026-07-20 14:32:07 +08:00
|
|
|
|
</button>
|
|
|
|
|
|
))}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
) : null}
|
2026-07-22 00:27:06 +08:00
|
|
|
|
<div
|
|
|
|
|
|
id={`chat-panel-${selectedSlot}`}
|
|
|
|
|
|
className="chat-message-list"
|
2026-07-27 02:21:30 +08:00
|
|
|
|
ref={messageListRef}
|
|
|
|
|
|
onScroll={(event) => {
|
|
|
|
|
|
const list = event.currentTarget;
|
|
|
|
|
|
const distanceFromTail =
|
|
|
|
|
|
list.scrollHeight - list.scrollTop - list.clientHeight;
|
|
|
|
|
|
followTail.current = distanceFromTail <= TAIL_THRESHOLD_PX;
|
|
|
|
|
|
}}
|
2026-07-22 00:27:06 +08:00
|
|
|
|
role="tabpanel"
|
|
|
|
|
|
aria-label={`${selectedSlot} messages`}
|
|
|
|
|
|
aria-busy={sending[selectedSlot]}
|
|
|
|
|
|
aria-live="polite"
|
|
|
|
|
|
>
|
2026-07-21 14:43:14 +08:00
|
|
|
|
{visibleMessages.length === 0 ? (
|
2026-07-20 11:31:41 +08:00
|
|
|
|
<p className="chat-empty">
|
2026-07-22 02:52:31 +08:00
|
|
|
|
当前槽位还没有消息。连接 Profile 后即可开始聊天。
|
2026-07-20 11:31:41 +08:00
|
|
|
|
</p>
|
|
|
|
|
|
) : (
|
2026-07-21 15:25:11 +08:00
|
|
|
|
visibleMessages.map((message) => {
|
|
|
|
|
|
return (
|
|
|
|
|
|
<article
|
|
|
|
|
|
className={`chat-message-row ${message.role}${message.error ? " failed" : ""}`}
|
|
|
|
|
|
key={message.id}
|
|
|
|
|
|
>
|
|
|
|
|
|
<div className="chat-avatar" aria-hidden="true">
|
|
|
|
|
|
{message.role === "user"
|
|
|
|
|
|
? "You"
|
|
|
|
|
|
: message.role === "assistant"
|
|
|
|
|
|
? "A"
|
|
|
|
|
|
: "!"}
|
2026-07-21 14:43:14 +08:00
|
|
|
|
</div>
|
2026-07-22 02:52:31 +08:00
|
|
|
|
<div className="chat-message-content">
|
|
|
|
|
|
<div className="chat-bubble">
|
|
|
|
|
|
<div className="chat-bubble-header">
|
|
|
|
|
|
<span>
|
|
|
|
|
|
{message.role === "user"
|
|
|
|
|
|
? "你"
|
|
|
|
|
|
: message.role === "assistant"
|
|
|
|
|
|
? "AstrBot"
|
|
|
|
|
|
: "系统"}
|
|
|
|
|
|
</span>
|
|
|
|
|
|
{message.streaming ? <small>生成中</small> : null}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
{message.content ? (
|
fix(chat): scope draft, attachments, and errors per slot
The composer draft, the pending attachment list and the error banner
were single values shared by both slots. Uploading on companion and
then switching to operations routed the companion instance's
attachment ids into the operations instance, the operations error
banner stayed visible on the companion tab, and a half-typed message
followed the user across tabs.
Make all three Record<ChatSlot, ...>, mirroring the sending,
unreadCounts and storedSessions state that was already per slot. Every
asynchronous action now pins its slot at the start, so an upload or a
send that resolves after the user switched tabs still lands on the slot
it belonged to.
Splitting the error state is required rather than incidental. refresh()
runs every 500ms and wrote the shared error on failure and cleared it on
success, so any send or upload error was silently wiped within half a
second. The polling failure describes the IPC channel rather than a
slot, so it keeps its own runtimeError; slotError is owned solely by
user actions and takes precedence when both are present.
All three new tests were checked against a mutation that reads the
companion slot regardless of selection, and all three fail under it.
Refs: F-16 (audit P1-11).
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-27 00:28:27 +08:00
|
|
|
|
<MessageContent
|
|
|
|
|
|
content={message.content}
|
|
|
|
|
|
slot={selectedSlot}
|
|
|
|
|
|
/>
|
2026-07-22 02:52:31 +08:00
|
|
|
|
) : (
|
|
|
|
|
|
<p className="message-placeholder">正在生成…</p>
|
|
|
|
|
|
)}
|
2026-07-21 15:25:11 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
{message.role === "assistant" &&
|
|
|
|
|
|
message.content &&
|
|
|
|
|
|
!message.streaming ? (
|
2026-07-22 02:52:31 +08:00
|
|
|
|
<div className="chat-message-actions" aria-label="消息操作">
|
2026-07-21 15:25:11 +08:00
|
|
|
|
<button
|
|
|
|
|
|
type="button"
|
2026-07-22 02:52:31 +08:00
|
|
|
|
aria-label="复制消息"
|
|
|
|
|
|
title="复制消息"
|
2026-07-21 15:25:11 +08:00
|
|
|
|
onClick={() => void copyMessage(message.content)}
|
|
|
|
|
|
>
|
2026-07-22 02:52:31 +08:00
|
|
|
|
<span
|
|
|
|
|
|
className="message-action-icon icon-copy"
|
|
|
|
|
|
aria-hidden="true"
|
|
|
|
|
|
/>
|
fix(chat): repair the message row layout and action buttons
Three defects reported from real-machine testing, all rooted in the same
few conflicting CSS blocks, so they are fixed together — separating them
would leave intermediate states that render worse than either end.
The action buttons showed wrapped vertical text inside a 2rem box.
styles.css already defined .message-action-label, a clip-rect
screen-reader-only class clearly written for these buttons, but nothing
in the tree ever used it: ChatPanel rendered a bare span. Wire the class
up so the buttons are icon-only, and drop the border so they read as
plain icons rather than pills.
.chat-message-actions and its buttons were defined five times across the
stylesheet, three of those styling them as bordered pills that the final
block then partially overrode. Remove .chat-message-actions button from
the three pill rule groups and the small-screen grid rule, leaving one
definition next to .message-action-icon. .chat-runtime-actions and
.chat-stop-button keep the pill treatment; they are different controls.
The avatar rendered below its bubble instead of beside it.
.chat-message-row used align-items: flex-end, and .chat-message-content
is taller than the bubble because the action bar reserves a row, so the
avatar was pinned to the bottom of that taller box. Align to the top.
Removing visibility: hidden from the hidden action bar also closes F-40
(audit P2-24): visibility removed the buttons from the tab order, so
:focus-within could never fire and a keyboard user could never reveal
them. Opacity alone keeps them focusable.
Verified against the real stylesheet in a browser: avatar and bubble
tops differ by 0px, the button computes to 34x34 with 0px border, the
label measures 1x1, and focusing a button takes the bar from opacity 0
to 1.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-27 00:59:11 +08:00
|
|
|
|
<span className="message-action-label">复制</span>
|
2026-07-21 15:25:11 +08:00
|
|
|
|
</button>
|
|
|
|
|
|
<button
|
|
|
|
|
|
type="button"
|
2026-07-22 02:52:31 +08:00
|
|
|
|
aria-label="重试消息"
|
|
|
|
|
|
title="重试消息"
|
2026-07-21 15:25:11 +08:00
|
|
|
|
onClick={() => retryMessage(message.id)}
|
|
|
|
|
|
>
|
2026-07-22 02:52:31 +08:00
|
|
|
|
<span
|
|
|
|
|
|
className="message-action-icon icon-retry"
|
|
|
|
|
|
aria-hidden="true"
|
|
|
|
|
|
/>
|
fix(chat): repair the message row layout and action buttons
Three defects reported from real-machine testing, all rooted in the same
few conflicting CSS blocks, so they are fixed together — separating them
would leave intermediate states that render worse than either end.
The action buttons showed wrapped vertical text inside a 2rem box.
styles.css already defined .message-action-label, a clip-rect
screen-reader-only class clearly written for these buttons, but nothing
in the tree ever used it: ChatPanel rendered a bare span. Wire the class
up so the buttons are icon-only, and drop the border so they read as
plain icons rather than pills.
.chat-message-actions and its buttons were defined five times across the
stylesheet, three of those styling them as bordered pills that the final
block then partially overrode. Remove .chat-message-actions button from
the three pill rule groups and the small-screen grid rule, leaving one
definition next to .message-action-icon. .chat-runtime-actions and
.chat-stop-button keep the pill treatment; they are different controls.
The avatar rendered below its bubble instead of beside it.
.chat-message-row used align-items: flex-end, and .chat-message-content
is taller than the bubble because the action bar reserves a row, so the
avatar was pinned to the bottom of that taller box. Align to the top.
Removing visibility: hidden from the hidden action bar also closes F-40
(audit P2-24): visibility removed the buttons from the tab order, so
:focus-within could never fire and a keyboard user could never reveal
them. Opacity alone keeps them focusable.
Verified against the real stylesheet in a browser: avatar and bubble
tops differ by 0px, the button computes to 34x34 with 0px border, the
label measures 1x1, and focusing a button takes the bar from opacity 0
to 1.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-27 00:59:11 +08:00
|
|
|
|
<span className="message-action-label">重试</span>
|
2026-07-21 15:25:11 +08:00
|
|
|
|
</button>
|
|
|
|
|
|
<button
|
|
|
|
|
|
type="button"
|
2026-07-22 02:52:31 +08:00
|
|
|
|
aria-label="朗读消息"
|
|
|
|
|
|
title="朗读消息"
|
2026-07-21 15:25:11 +08:00
|
|
|
|
onClick={() => speakMessage(message.content)}
|
|
|
|
|
|
>
|
2026-07-22 02:52:31 +08:00
|
|
|
|
<span
|
|
|
|
|
|
className="message-action-icon icon-read"
|
|
|
|
|
|
aria-hidden="true"
|
|
|
|
|
|
/>
|
fix(chat): repair the message row layout and action buttons
Three defects reported from real-machine testing, all rooted in the same
few conflicting CSS blocks, so they are fixed together — separating them
would leave intermediate states that render worse than either end.
The action buttons showed wrapped vertical text inside a 2rem box.
styles.css already defined .message-action-label, a clip-rect
screen-reader-only class clearly written for these buttons, but nothing
in the tree ever used it: ChatPanel rendered a bare span. Wire the class
up so the buttons are icon-only, and drop the border so they read as
plain icons rather than pills.
.chat-message-actions and its buttons were defined five times across the
stylesheet, three of those styling them as bordered pills that the final
block then partially overrode. Remove .chat-message-actions button from
the three pill rule groups and the small-screen grid rule, leaving one
definition next to .message-action-icon. .chat-runtime-actions and
.chat-stop-button keep the pill treatment; they are different controls.
The avatar rendered below its bubble instead of beside it.
.chat-message-row used align-items: flex-end, and .chat-message-content
is taller than the bubble because the action bar reserves a row, so the
avatar was pinned to the bottom of that taller box. Align to the top.
Removing visibility: hidden from the hidden action bar also closes F-40
(audit P2-24): visibility removed the buttons from the tab order, so
:focus-within could never fire and a keyboard user could never reveal
them. Opacity alone keeps them focusable.
Verified against the real stylesheet in a browser: avatar and bubble
tops differ by 0px, the button computes to 34x34 with 0px border, the
label measures 1x1, and focusing a button takes the bar from opacity 0
to 1.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-27 00:59:11 +08:00
|
|
|
|
<span className="message-action-label">朗读</span>
|
2026-07-21 15:25:11 +08:00
|
|
|
|
</button>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
) : null}
|
|
|
|
|
|
{message.streaming && sending[selectedSlot] ? (
|
|
|
|
|
|
<button
|
|
|
|
|
|
className="chat-stop-button"
|
|
|
|
|
|
type="button"
|
|
|
|
|
|
onClick={() => void stopGeneration()}
|
|
|
|
|
|
>
|
2026-07-22 02:52:31 +08:00
|
|
|
|
停止生成
|
2026-07-21 15:25:11 +08:00
|
|
|
|
</button>
|
|
|
|
|
|
) : null}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</article>
|
|
|
|
|
|
);
|
|
|
|
|
|
})
|
2026-07-20 11:31:41 +08:00
|
|
|
|
)}
|
|
|
|
|
|
</div>
|
fix(chat): scope draft, attachments, and errors per slot
The composer draft, the pending attachment list and the error banner
were single values shared by both slots. Uploading on companion and
then switching to operations routed the companion instance's
attachment ids into the operations instance, the operations error
banner stayed visible on the companion tab, and a half-typed message
followed the user across tabs.
Make all three Record<ChatSlot, ...>, mirroring the sending,
unreadCounts and storedSessions state that was already per slot. Every
asynchronous action now pins its slot at the start, so an upload or a
send that resolves after the user switched tabs still lands on the slot
it belonged to.
Splitting the error state is required rather than incidental. refresh()
runs every 500ms and wrote the shared error on failure and cleared it on
success, so any send or upload error was silently wiped within half a
second. The polling failure describes the IPC channel rather than a
slot, so it keeps its own runtimeError; slotError is owned solely by
user actions and takes precedence when both are present.
All three new tests were checked against a mutation that reads the
companion slot regardless of selection, and all three fail under it.
Refs: F-16 (audit P1-11).
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-27 00:28:27 +08:00
|
|
|
|
{visibleError ? (
|
2026-07-22 00:27:06 +08:00
|
|
|
|
<p className="chat-error" role="alert">
|
fix(chat): scope draft, attachments, and errors per slot
The composer draft, the pending attachment list and the error banner
were single values shared by both slots. Uploading on companion and
then switching to operations routed the companion instance's
attachment ids into the operations instance, the operations error
banner stayed visible on the companion tab, and a half-typed message
followed the user across tabs.
Make all three Record<ChatSlot, ...>, mirroring the sending,
unreadCounts and storedSessions state that was already per slot. Every
asynchronous action now pins its slot at the start, so an upload or a
send that resolves after the user switched tabs still lands on the slot
it belonged to.
Splitting the error state is required rather than incidental. refresh()
runs every 500ms and wrote the shared error on failure and cleared it on
success, so any send or upload error was silently wiped within half a
second. The polling failure describes the IPC channel rather than a
slot, so it keeps its own runtimeError; slotError is owned solely by
user actions and takes precedence when both are present.
All three new tests were checked against a mutation that reads the
companion slot regardless of selection, and all three fail under it.
Refs: F-16 (audit P1-11).
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-27 00:28:27 +08:00
|
|
|
|
{visibleError}
|
2026-07-22 00:27:06 +08:00
|
|
|
|
</p>
|
|
|
|
|
|
) : null}
|
2026-07-20 13:30:29 +08:00
|
|
|
|
<div className="chat-composer-toolbar">
|
|
|
|
|
|
<label className="file-picker">
|
|
|
|
|
|
<span>添加图片/文件</span>
|
|
|
|
|
|
<input
|
2026-07-22 00:27:06 +08:00
|
|
|
|
className="file-picker-input"
|
2026-07-20 13:30:29 +08:00
|
|
|
|
type="file"
|
|
|
|
|
|
multiple
|
|
|
|
|
|
onChange={(event) => {
|
|
|
|
|
|
void uploadFiles(event.target.files);
|
|
|
|
|
|
event.target.value = "";
|
|
|
|
|
|
}}
|
|
|
|
|
|
disabled={uploading || !capabilities?.fileUpload}
|
|
|
|
|
|
/>
|
|
|
|
|
|
</label>
|
2026-07-21 14:43:14 +08:00
|
|
|
|
|
2026-07-20 13:30:29 +08:00
|
|
|
|
{uploading ? <span className="toolbar-note">上传中…</span> : null}
|
2026-07-21 14:43:14 +08:00
|
|
|
|
<button
|
|
|
|
|
|
type="button"
|
|
|
|
|
|
onClick={toggleVoiceInput}
|
|
|
|
|
|
className={voiceListening ? "voice-button active" : "voice-button"}
|
2026-07-22 00:27:06 +08:00
|
|
|
|
aria-pressed={voiceListening}
|
2026-07-21 14:43:14 +08:00
|
|
|
|
>
|
|
|
|
|
|
{voiceListening ? "停止录音" : "语音输入"}
|
|
|
|
|
|
</button>
|
2026-07-20 13:30:29 +08:00
|
|
|
|
</div>
|
fix(chat): scope draft, attachments, and errors per slot
The composer draft, the pending attachment list and the error banner
were single values shared by both slots. Uploading on companion and
then switching to operations routed the companion instance's
attachment ids into the operations instance, the operations error
banner stayed visible on the companion tab, and a half-typed message
followed the user across tabs.
Make all three Record<ChatSlot, ...>, mirroring the sending,
unreadCounts and storedSessions state that was already per slot. Every
asynchronous action now pins its slot at the start, so an upload or a
send that resolves after the user switched tabs still lands on the slot
it belonged to.
Splitting the error state is required rather than incidental. refresh()
runs every 500ms and wrote the shared error on failure and cleared it on
success, so any send or upload error was silently wiped within half a
second. The polling failure describes the IPC channel rather than a
slot, so it keeps its own runtimeError; slotError is owned solely by
user actions and takes precedence when both are present.
All three new tests were checked against a mutation that reads the
companion slot regardless of selection, and all three fail under it.
Refs: F-16 (audit P1-11).
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-27 00:28:27 +08:00
|
|
|
|
{selectedAttachments.length > 0 ? (
|
2026-07-20 13:30:29 +08:00
|
|
|
|
<div className="attachment-list">
|
fix(chat): scope draft, attachments, and errors per slot
The composer draft, the pending attachment list and the error banner
were single values shared by both slots. Uploading on companion and
then switching to operations routed the companion instance's
attachment ids into the operations instance, the operations error
banner stayed visible on the companion tab, and a half-typed message
followed the user across tabs.
Make all three Record<ChatSlot, ...>, mirroring the sending,
unreadCounts and storedSessions state that was already per slot. Every
asynchronous action now pins its slot at the start, so an upload or a
send that resolves after the user switched tabs still lands on the slot
it belonged to.
Splitting the error state is required rather than incidental. refresh()
runs every 500ms and wrote the shared error on failure and cleared it on
success, so any send or upload error was silently wiped within half a
second. The polling failure describes the IPC channel rather than a
slot, so it keeps its own runtimeError; slotError is owned solely by
user actions and takes precedence when both are present.
All three new tests were checked against a mutation that reads the
companion slot regardless of selection, and all three fail under it.
Refs: F-16 (audit P1-11).
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-27 00:28:27 +08:00
|
|
|
|
{selectedAttachments.map((attachment) => (
|
2026-07-20 13:30:29 +08:00
|
|
|
|
<span key={attachment.attachmentId}>
|
|
|
|
|
|
{attachment.fileName}
|
|
|
|
|
|
<button
|
|
|
|
|
|
type="button"
|
|
|
|
|
|
aria-label={`移除 ${attachment.fileName}`}
|
|
|
|
|
|
onClick={() =>
|
fix(chat): scope draft, attachments, and errors per slot
The composer draft, the pending attachment list and the error banner
were single values shared by both slots. Uploading on companion and
then switching to operations routed the companion instance's
attachment ids into the operations instance, the operations error
banner stayed visible on the companion tab, and a half-typed message
followed the user across tabs.
Make all three Record<ChatSlot, ...>, mirroring the sending,
unreadCounts and storedSessions state that was already per slot. Every
asynchronous action now pins its slot at the start, so an upload or a
send that resolves after the user switched tabs still lands on the slot
it belonged to.
Splitting the error state is required rather than incidental. refresh()
runs every 500ms and wrote the shared error on failure and cleared it on
success, so any send or upload error was silently wiped within half a
second. The polling failure describes the IPC channel rather than a
slot, so it keeps its own runtimeError; slotError is owned solely by
user actions and takes precedence when both are present.
All three new tests were checked against a mutation that reads the
companion slot regardless of selection, and all three fail under it.
Refs: F-16 (audit P1-11).
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-27 00:28:27 +08:00
|
|
|
|
setPendingAttachments((current) => ({
|
|
|
|
|
|
...current,
|
|
|
|
|
|
[selectedSlot]: current[selectedSlot].filter(
|
2026-07-20 13:30:29 +08:00
|
|
|
|
(item) => item.attachmentId !== attachment.attachmentId,
|
|
|
|
|
|
),
|
fix(chat): scope draft, attachments, and errors per slot
The composer draft, the pending attachment list and the error banner
were single values shared by both slots. Uploading on companion and
then switching to operations routed the companion instance's
attachment ids into the operations instance, the operations error
banner stayed visible on the companion tab, and a half-typed message
followed the user across tabs.
Make all three Record<ChatSlot, ...>, mirroring the sending,
unreadCounts and storedSessions state that was already per slot. Every
asynchronous action now pins its slot at the start, so an upload or a
send that resolves after the user switched tabs still lands on the slot
it belonged to.
Splitting the error state is required rather than incidental. refresh()
runs every 500ms and wrote the shared error on failure and cleared it on
success, so any send or upload error was silently wiped within half a
second. The polling failure describes the IPC channel rather than a
slot, so it keeps its own runtimeError; slotError is owned solely by
user actions and takes precedence when both are present.
All three new tests were checked against a mutation that reads the
companion slot regardless of selection, and all three fail under it.
Refs: F-16 (audit P1-11).
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-27 00:28:27 +08:00
|
|
|
|
}))
|
2026-07-20 13:30:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
>
|
|
|
|
|
|
×
|
|
|
|
|
|
</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="聊天消息"
|
fix(chat): scope draft, attachments, and errors per slot
The composer draft, the pending attachment list and the error banner
were single values shared by both slots. Uploading on companion and
then switching to operations routed the companion instance's
attachment ids into the operations instance, the operations error
banner stayed visible on the companion tab, and a half-typed message
followed the user across tabs.
Make all three Record<ChatSlot, ...>, mirroring the sending,
unreadCounts and storedSessions state that was already per slot. Every
asynchronous action now pins its slot at the start, so an upload or a
send that resolves after the user switched tabs still lands on the slot
it belonged to.
Splitting the error state is required rather than incidental. refresh()
runs every 500ms and wrote the shared error on failure and cleared it on
success, so any send or upload error was silently wiped within half a
second. The polling failure describes the IPC channel rather than a
slot, so it keeps its own runtimeError; slotError is owned solely by
user actions and takes precedence when both are present.
All three new tests were checked against a mutation that reads the
companion slot regardless of selection, and all three fail under it.
Refs: F-16 (audit P1-11).
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-27 00:28:27 +08:00
|
|
|
|
onChange={(event) =>
|
|
|
|
|
|
setDraft((current) => ({
|
|
|
|
|
|
...current,
|
|
|
|
|
|
[selectedSlot]: event.target.value,
|
|
|
|
|
|
}))
|
|
|
|
|
|
}
|
2026-07-20 11:31:41 +08:00
|
|
|
|
placeholder={
|
2026-07-20 13:30:29 +08:00
|
|
|
|
selectedSlot === "companion"
|
2026-07-22 02:52:31 +08:00
|
|
|
|
? "和陪伴实例说点什么…(Enter 发送,Shift+Enter 换行)"
|
|
|
|
|
|
: "向运维实例描述问题…(Enter 发送,Shift+Enter 换行)"
|
2026-07-20 11:31:41 +08:00
|
|
|
|
}
|
fix(chat): scope draft, attachments, and errors per slot
The composer draft, the pending attachment list and the error banner
were single values shared by both slots. Uploading on companion and
then switching to operations routed the companion instance's
attachment ids into the operations instance, the operations error
banner stayed visible on the companion tab, and a half-typed message
followed the user across tabs.
Make all three Record<ChatSlot, ...>, mirroring the sending,
unreadCounts and storedSessions state that was already per slot. Every
asynchronous action now pins its slot at the start, so an upload or a
send that resolves after the user switched tabs still lands on the slot
it belonged to.
Splitting the error state is required rather than incidental. refresh()
runs every 500ms and wrote the shared error on failure and cleared it on
success, so any send or upload error was silently wiped within half a
second. The polling failure describes the IPC channel rather than a
slot, so it keeps its own runtimeError; slotError is owned solely by
user actions and takes precedence when both are present.
All three new tests were checked against a mutation that reads the
companion slot regardless of selection, and all three fail under it.
Refs: F-16 (audit P1-11).
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-27 00:28:27 +08:00
|
|
|
|
value={draft[selectedSlot]}
|
2026-07-20 11:31:41 +08:00
|
|
|
|
rows={3}
|
2026-07-22 02:52:31 +08:00
|
|
|
|
onKeyDown={(event) => {
|
|
|
|
|
|
if (
|
|
|
|
|
|
event.key !== "Enter" ||
|
|
|
|
|
|
event.shiftKey ||
|
|
|
|
|
|
event.nativeEvent.isComposing
|
|
|
|
|
|
)
|
|
|
|
|
|
return;
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
void send();
|
|
|
|
|
|
}}
|
2026-07-20 11:31:41 +08:00
|
|
|
|
/>
|
2026-07-20 13:30:29 +08:00
|
|
|
|
<button
|
2026-07-22 02:52:31 +08:00
|
|
|
|
className="chat-send-button"
|
2026-07-20 13:30:29 +08:00
|
|
|
|
disabled={
|
fix(chat): scope draft, attachments, and errors per slot
The composer draft, the pending attachment list and the error banner
were single values shared by both slots. Uploading on companion and
then switching to operations routed the companion instance's
attachment ids into the operations instance, the operations error
banner stayed visible on the companion tab, and a half-typed message
followed the user across tabs.
Make all three Record<ChatSlot, ...>, mirroring the sending,
unreadCounts and storedSessions state that was already per slot. Every
asynchronous action now pins its slot at the start, so an upload or a
send that resolves after the user switched tabs still lands on the slot
it belonged to.
Splitting the error state is required rather than incidental. refresh()
runs every 500ms and wrote the shared error on failure and cleared it on
success, so any send or upload error was silently wiped within half a
second. The polling failure describes the IPC channel rather than a
slot, so it keeps its own runtimeError; slotError is owned solely by
user actions and takes precedence when both are present.
All three new tests were checked against a mutation that reads the
companion slot regardless of selection, and all three fail under it.
Refs: F-16 (audit P1-11).
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-27 00:28:27 +08:00
|
|
|
|
(!draft[selectedSlot].trim() && selectedAttachments.length === 0) ||
|
|
|
|
|
|
uploading
|
2026-07-20 13:30:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
type="submit"
|
|
|
|
|
|
>
|
2026-07-22 02:52:31 +08:00
|
|
|
|
<span className="send-button-icon" aria-hidden="true">
|
|
|
|
|
|
↑
|
|
|
|
|
|
</span>
|
|
|
|
|
|
<span>发送</span>
|
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(
|
2026-07-21 14:43:14 +08:00
|
|
|
|
(item) =>
|
|
|
|
|
|
item.id === id &&
|
|
|
|
|
|
item.slot === event.slot &&
|
|
|
|
|
|
item.sessionId === event.sessionId,
|
2026-07-20 11:31:41 +08:00
|
|
|
|
);
|
|
|
|
|
|
if (existing) {
|
|
|
|
|
|
existing.content += delta;
|
|
|
|
|
|
existing.streaming = true;
|
|
|
|
|
|
} else
|
|
|
|
|
|
next.push({
|
|
|
|
|
|
id,
|
2026-07-20 14:32:07 +08:00
|
|
|
|
sessionId: event.sessionId,
|
2026-07-20 11:31:41 +08:00
|
|
|
|
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(
|
2026-07-21 14:43:14 +08:00
|
|
|
|
(item) =>
|
|
|
|
|
|
item.id === id &&
|
|
|
|
|
|
item.slot === event.slot &&
|
|
|
|
|
|
item.sessionId === event.sessionId,
|
2026-07-20 11:31:41 +08:00
|
|
|
|
);
|
2026-07-21 14:43:14 +08:00
|
|
|
|
const completedContent = payloadString(event, "content")?.trim() ?? "";
|
|
|
|
|
|
if (existing) {
|
|
|
|
|
|
if (completedContent) {
|
|
|
|
|
|
existing.content = completedContent;
|
|
|
|
|
|
} else if (!existing.content.trim()) {
|
|
|
|
|
|
existing.content = "未收到有效回复";
|
|
|
|
|
|
existing.error = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
existing.streaming = false;
|
|
|
|
|
|
} else
|
2026-07-20 11:31:41 +08:00
|
|
|
|
next.push({
|
|
|
|
|
|
id,
|
2026-07-20 14:32:07 +08:00
|
|
|
|
sessionId: event.sessionId,
|
2026-07-20 11:31:41 +08:00
|
|
|
|
slot: event.slot,
|
|
|
|
|
|
role: "assistant",
|
2026-07-21 14:43:14 +08:00
|
|
|
|
content: completedContent || "未收到有效回复",
|
|
|
|
|
|
error: !completedContent,
|
2026-07-20 11:31:41 +08:00
|
|
|
|
});
|
|
|
|
|
|
} else if (event.eventType === "error") {
|
|
|
|
|
|
next.push({
|
|
|
|
|
|
id: event.eventId,
|
2026-07-20 14:32:07 +08:00
|
|
|
|
sessionId: event.sessionId,
|
2026-07-20 11:31:41 +08:00
|
|
|
|
slot: event.slot,
|
|
|
|
|
|
role: "system",
|
|
|
|
|
|
content: payloadString(event, "message") || "连接错误",
|
|
|
|
|
|
error: true,
|
|
|
|
|
|
});
|
|
|
|
|
|
} else if (event.eventType === "message.proactive") {
|
|
|
|
|
|
next.push({
|
|
|
|
|
|
id: event.eventId,
|
2026-07-20 14:32:07 +08:00
|
|
|
|
sessionId: event.sessionId,
|
2026-07-20 11:31:41 +08:00
|
|
|
|
slot: event.slot,
|
|
|
|
|
|
role: "assistant",
|
|
|
|
|
|
content: payloadString(event, "content") || "",
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return next;
|
|
|
|
|
|
}
|
2026-07-20 12:01:18 +08:00
|
|
|
|
|
2026-07-26 22:31:44 +08:00
|
|
|
|
function formatSessionTime(value?: string | null): string {
|
2026-07-22 02:52:31 +08:00
|
|
|
|
if (!value) return "本地历史";
|
|
|
|
|
|
const date = new Date(value);
|
|
|
|
|
|
if (Number.isNaN(date.getTime())) return "本地历史";
|
|
|
|
|
|
return new Intl.DateTimeFormat("zh-CN", {
|
|
|
|
|
|
month: "2-digit",
|
|
|
|
|
|
day: "2-digit",
|
|
|
|
|
|
hour: "2-digit",
|
|
|
|
|
|
minute: "2-digit",
|
|
|
|
|
|
}).format(date);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
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];
|
|
|
|
|
|
}
|