feat: 中断回复

This commit is contained in:
ZyGuo committed 2026-06-04 17:00:56 +08:00
1 parent 002e71f971
commit dccce9355d
1 file changed
+35 -2
+35 -2
View File
@@ -33,6 +33,15 @@ import {
} from "./iframe-chat-submit";
type BrainyChatStatus = "started" | "finished";
type BrainyChatInterruptMessage = {
source?: string;
type?: string;
payload?: {
reason?: string;
requestId?: string;
};
requestId?: string;
};
function postBrainyChatStatus(status: BrainyChatStatus, requestId: string) {
window.parent?.postMessage(
@@ -206,10 +215,34 @@ export default function IframeChatPage() {
[sendMessage, threadId],
);
const handleInterrupt = useCallback(
async (message: BrainyChatInterruptMessage) => {
const targetRequestId = message.payload?.requestId?.trim();
if (targetRequestId) {
pendingRequestIdRef.current = targetRequestId;
}
console.log("收到插件中断消息", {
reason: message.payload?.reason,
requestId: message.requestId,
targetRequestId,
});
await thread.stop();
},
[thread],
);
useEffect(() => {
const handleMessage = (event: MessageEvent<BrainyChatSubmitMessage>) => {
const handleMessage = (
event: MessageEvent<BrainyChatSubmitMessage | BrainyChatInterruptMessage>,
) => {
const data = event.data;
if (data?.source !== "brainy-extension") return;
if (data?.type === "brainy:chat-interrupt") {
void handleInterrupt(data);
return;
}
if (data?.type !== "brainy:chat-submit") return;
const { submitText, files, iframeContext, additionalKwargs } =
@@ -241,7 +274,7 @@ export default function IframeChatPage() {
return () => {
window.removeEventListener("message", handleMessage);
};
}, [handleSubmit]);
}, [handleInterrupt, handleSubmit]);
const messageListPaddingBottom = showFollowups
? MESSAGE_LIST_DEFAULT_PADDING_BOTTOM +