feat: 中断回复
This commit is contained in:
1 parent
002e71f971
commit
dccce9355d
1 file changed
+35
-2
@@ -33,6 +33,15 @@ import {
|
|||||||
} from "./iframe-chat-submit";
|
} from "./iframe-chat-submit";
|
||||||
|
|
||||||
type BrainyChatStatus = "started" | "finished";
|
type BrainyChatStatus = "started" | "finished";
|
||||||
|
type BrainyChatInterruptMessage = {
|
||||||
|
source?: string;
|
||||||
|
type?: string;
|
||||||
|
payload?: {
|
||||||
|
reason?: string;
|
||||||
|
requestId?: string;
|
||||||
|
};
|
||||||
|
requestId?: string;
|
||||||
|
};
|
||||||
|
|
||||||
function postBrainyChatStatus(status: BrainyChatStatus, requestId: string) {
|
function postBrainyChatStatus(status: BrainyChatStatus, requestId: string) {
|
||||||
window.parent?.postMessage(
|
window.parent?.postMessage(
|
||||||
@@ -206,10 +215,34 @@ export default function IframeChatPage() {
|
|||||||
[sendMessage, threadId],
|
[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(() => {
|
useEffect(() => {
|
||||||
const handleMessage = (event: MessageEvent<BrainyChatSubmitMessage>) => {
|
const handleMessage = (
|
||||||
|
event: MessageEvent<BrainyChatSubmitMessage | BrainyChatInterruptMessage>,
|
||||||
|
) => {
|
||||||
const data = event.data;
|
const data = event.data;
|
||||||
if (data?.source !== "brainy-extension") return;
|
if (data?.source !== "brainy-extension") return;
|
||||||
|
if (data?.type === "brainy:chat-interrupt") {
|
||||||
|
void handleInterrupt(data);
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (data?.type !== "brainy:chat-submit") return;
|
if (data?.type !== "brainy:chat-submit") return;
|
||||||
|
|
||||||
const { submitText, files, iframeContext, additionalKwargs } =
|
const { submitText, files, iframeContext, additionalKwargs } =
|
||||||
@@ -241,7 +274,7 @@ export default function IframeChatPage() {
|
|||||||
return () => {
|
return () => {
|
||||||
window.removeEventListener("message", handleMessage);
|
window.removeEventListener("message", handleMessage);
|
||||||
};
|
};
|
||||||
}, [handleSubmit]);
|
}, [handleInterrupt, handleSubmit]);
|
||||||
|
|
||||||
const messageListPaddingBottom = showFollowups
|
const messageListPaddingBottom = showFollowups
|
||||||
? MESSAGE_LIST_DEFAULT_PADDING_BOTTOM +
|
? MESSAGE_LIST_DEFAULT_PADDING_BOTTOM +
|
||||||
|
|||||||
Reference in new issue
Block a user