feat(nanobot-web): 添加会话创建接口并优化前端会话管理
- 新增 POST /api/sessions/{key} 接口用于立即创建或持久化会话
- 提取 _serialize_session_detail 函数以统一会话数据序列化逻辑
- 前端添加 createSession API 调用函数
- 实现本地存储中的会话ID持久化功能
- 优化 ChatPage 组件中的会话切换逻辑,确保状态正确重置
- 在消息处理中添加会话ID验证,避免跨会话消息混乱
- 新建会话时主动调用创建API并刷新会话列表
This commit is contained in:
@ -509,6 +509,10 @@ export async function listSessions(): Promise<Session[]> {
|
||||
return fetchJSON('/api/sessions');
|
||||
}
|
||||
|
||||
export async function createSession(key: string): Promise<SessionDetail> {
|
||||
return fetchJSON(`/api/sessions/${encodeURIComponent(key)}`, { method: 'POST' });
|
||||
}
|
||||
|
||||
export async function getSession(key: string): Promise<SessionDetail> {
|
||||
return fetchJSON(`/api/sessions/${encodeURIComponent(key)}`);
|
||||
}
|
||||
|
||||
@ -13,6 +13,23 @@ import type {
|
||||
} from '@/types';
|
||||
import type { WsStatus } from '@/lib/api';
|
||||
|
||||
const ACTIVE_SESSION_STORAGE_KEY = 'nanobot_active_session_id';
|
||||
|
||||
function getInitialSessionId(): string {
|
||||
if (typeof window === 'undefined') {
|
||||
return 'web:default';
|
||||
}
|
||||
const saved = window.localStorage.getItem(ACTIVE_SESSION_STORAGE_KEY)?.trim();
|
||||
return saved || 'web:default';
|
||||
}
|
||||
|
||||
function persistSessionId(id: string): void {
|
||||
if (typeof window === 'undefined') {
|
||||
return;
|
||||
}
|
||||
window.localStorage.setItem(ACTIVE_SESSION_STORAGE_KEY, id);
|
||||
}
|
||||
|
||||
interface ChatStore {
|
||||
user: AuthUser | null;
|
||||
isAuthLoading: boolean;
|
||||
@ -105,7 +122,7 @@ function createEventId(event: ProcessWsEvent): string {
|
||||
export const useChatStore = create<ChatStore>((set) => ({
|
||||
user: null,
|
||||
isAuthLoading: true,
|
||||
sessionId: 'web:default',
|
||||
sessionId: getInitialSessionId(),
|
||||
messages: [],
|
||||
isLoading: false,
|
||||
streamingContent: '',
|
||||
@ -125,7 +142,10 @@ export const useChatStore = create<ChatStore>((set) => ({
|
||||
|
||||
setUser: (user) => set({ user }),
|
||||
setIsAuthLoading: (loading) => set({ isAuthLoading: loading }),
|
||||
setSessionId: (id) => set({ sessionId: id }),
|
||||
setSessionId: (id) => {
|
||||
persistSessionId(id);
|
||||
set({ sessionId: id });
|
||||
},
|
||||
setMessages: (msgs) => set({ messages: msgs }),
|
||||
addMessage: (msg) => set((s) => ({ messages: [...s.messages, msg] })),
|
||||
setIsLoading: (loading) => set({ isLoading: loading }),
|
||||
|
||||
Reference in New Issue
Block a user