重构聊天会话管理,支持会话历史和存储
This commit is contained in:
@@ -1,8 +1,7 @@
|
||||
import type { BranchGroup, Message, PersistedChatState } from "./GlobalChatbox.types";
|
||||
import type { BranchGroup, Message } from "./GlobalChatbox.types";
|
||||
|
||||
export const createId = () =>
|
||||
`${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;
|
||||
export const CHAT_STORAGE_KEY = "tjwater_agent_chat_state_v1";
|
||||
export const PRESET_PROMPTS = [
|
||||
"分析当前管网中的水力瓶颈管道,并给出改造建议。",
|
||||
"帮我分析当前管网压力异常点,并按风险等级排序。",
|
||||
@@ -29,34 +28,6 @@ export const stripMarkdown = (md: string): string =>
|
||||
.replace(/<[^>]+>/g, "")
|
||||
.trim();
|
||||
|
||||
export const getInitialChatState = (): PersistedChatState => {
|
||||
if (typeof window === "undefined") {
|
||||
return { messages: [], sessionId: undefined };
|
||||
}
|
||||
try {
|
||||
const storedRaw = window.localStorage.getItem(CHAT_STORAGE_KEY);
|
||||
if (!storedRaw) return { messages: [], sessionId: undefined };
|
||||
const parsed = JSON.parse(storedRaw) as PersistedChatState;
|
||||
if (!Array.isArray(parsed.messages)) {
|
||||
console.error("[GlobalChatbox] Invalid persisted messages format.");
|
||||
window.localStorage.removeItem(CHAT_STORAGE_KEY);
|
||||
return { messages: [], sessionId: undefined };
|
||||
}
|
||||
return {
|
||||
messages: Array.isArray(parsed.messages) ? parsed.messages : [],
|
||||
sessionId: parsed.sessionId,
|
||||
branchGroups: Array.isArray(parsed.branchGroups) ? parsed.branchGroups : [],
|
||||
};
|
||||
} catch (error) {
|
||||
console.error(
|
||||
"[GlobalChatbox] Failed to read persisted chat state:",
|
||||
error,
|
||||
);
|
||||
window.localStorage.removeItem(CHAT_STORAGE_KEY);
|
||||
return { messages: [], sessionId: undefined };
|
||||
}
|
||||
};
|
||||
|
||||
export const cloneMessage = (message: Message): Message => ({
|
||||
...message,
|
||||
progress: message.progress ? [...message.progress] : undefined,
|
||||
|
||||
Reference in New Issue
Block a user