stop generating store message

This commit is contained in:
2025-02-08 00:09:49 +08:00
parent 75bf4a419d
commit 5b4a0507ae

View File

@@ -40,9 +40,28 @@ import { Switch } from "@/components/ui/switch";
import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert"; import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert";
import { AppChatStoreContext, AppContext } from "./App"; import { AppChatStoreContext, AppContext } from "./App";
import APIListMenu from "@/components/ListAPI";
import { ImageGenDrawer } from "@/components/ImageGenDrawer"; import { ImageGenDrawer } from "@/components/ImageGenDrawer";
import { abort } from "process";
const createMessageFromCurrentBuffer = (
chunkMessages: string[],
reasoningChunks: string[],
tools: ToolCall[]
): ChatStoreMessage => {
return {
role: "assistant",
content: chunkMessages.join(""),
reasoning_content: reasoningChunks.join(""),
tool_calls: tools.length > 0 ? tools : undefined,
// 补全其他必填字段的默认值(根据你的类型定义)
hide: false,
token: 0, // 需要实际的token计算逻辑
example: false,
audio: null,
logprobs: null,
response_model_name: null,
usage: null,
};
};
export default function ChatBOX() { export default function ChatBOX() {
const { db, selectedChatIndex, setSelectedChatIndex, handleNewChatStore } = const { db, selectedChatIndex, setSelectedChatIndex, handleNewChatStore } =
@@ -93,6 +112,8 @@ export default function ChatBOX() {
}; };
let response_model_name: string | null = null; let response_model_name: string | null = null;
let usage: Usage | null = null; let usage: Usage | null = null;
try {
for await (const i of client.processStreamResponse(response, signal)) { for await (const i of client.processStreamResponse(response, signal)) {
if (signal?.aborted) break; if (signal?.aborted) break;
response_model_name = i.model; response_model_name = i.model;
@@ -164,7 +185,28 @@ export default function ChatBOX() {
}) })
); );
} }
} catch (e: any) {
if (e.name === "AbortError") {
// 1. 立即保存当前buffer中的内容
if (allChunkMessage.length > 0 || allReasoningContentChunk.length > 0) {
const partialMsg = createMessageFromCurrentBuffer(
allChunkMessage,
allReasoningContentChunk,
allChunkTool
);
chatStore.history.push(partialMsg);
setChatStore({ ...chatStore });
}
// 2. 不隐藏错误,重新抛出给上层
throw e;
}
// 其他错误直接抛出
throw e;
} finally {
setShowGenerating(false); setShowGenerating(false);
setGeneratingMessage("");
}
const content = allChunkMessage.join(""); const content = allChunkMessage.join("");
const reasoning_content = allReasoningContentChunk.join(""); const reasoning_content = allReasoningContentChunk.join("");