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