feat: 生成中状态
This commit is contained in:
1 parent
49cfc687f9
commit
0122d3e361
1 file changed
+29
-1
@@ -1,7 +1,7 @@
|
||||
"use client";
|
||||
|
||||
import { useRouter } from "next/navigation";
|
||||
import { useCallback, useEffect, useMemo, useState } from "react";
|
||||
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
||||
|
||||
import { type PromptInputMessage } from "@/components/ai-elements/prompt-input";
|
||||
import { useArtifacts } from "@/components/workspace/artifacts";
|
||||
@@ -46,6 +46,22 @@ type BrainyChatSubmitMessage = {
|
||||
requestId?: string;
|
||||
};
|
||||
|
||||
type BrainyChatStatus = "started" | "finished";
|
||||
|
||||
function postBrainyChatStatus(status: BrainyChatStatus, requestId: string) {
|
||||
window.parent?.postMessage(
|
||||
{
|
||||
source: "brainy-iframe",
|
||||
type: "brainy:chat-status",
|
||||
payload: {
|
||||
status,
|
||||
requestId,
|
||||
},
|
||||
},
|
||||
"*",
|
||||
);
|
||||
}
|
||||
|
||||
function buildIframeMessageText({
|
||||
text,
|
||||
prompt,
|
||||
@@ -101,6 +117,7 @@ export default function IframeChatPage() {
|
||||
const { data: promptPrefixSettings } = usePromptPrefixSettings();
|
||||
const [promptPrefixOn, setPromptPrefixOn] = useState(false);
|
||||
const [selectedAgentId, setSelectedAgentId] = useState<string | null>(null);
|
||||
const pendingRequestIdRef = useRef<string | null>(null);
|
||||
const { selectedArtifact } = useArtifacts();
|
||||
useSpecificChatMode();
|
||||
|
||||
@@ -130,8 +147,17 @@ export default function IframeChatPage() {
|
||||
"",
|
||||
`${window.location.pathname}${window.location.search}#${routeBase}/chats_iframe/${createdThreadId}`,
|
||||
);
|
||||
|
||||
if (pendingRequestIdRef.current) {
|
||||
postBrainyChatStatus("started", pendingRequestIdRef.current);
|
||||
}
|
||||
},
|
||||
onFinish: (state) => {
|
||||
if (pendingRequestIdRef.current) {
|
||||
postBrainyChatStatus("finished", pendingRequestIdRef.current);
|
||||
pendingRequestIdRef.current = null;
|
||||
}
|
||||
|
||||
if (document.hidden || !document.hasFocus()) {
|
||||
let body = "Conversation finished";
|
||||
const lastMessage = state.messages.at(-1);
|
||||
@@ -266,6 +292,8 @@ export default function IframeChatPage() {
|
||||
|
||||
if (!submitText) return;
|
||||
|
||||
pendingRequestIdRef.current = data.requestId ?? null;
|
||||
|
||||
const iframeContext: Record<string, unknown> = {};
|
||||
if (data.requestId) {
|
||||
iframeContext.brainy_request_id = data.requestId;
|
||||
|
||||
Reference in new issue
Block a user