fix: 第二轮修复 — 数据库启动检查、会话持久化、URL路由、设备排序等
1. DevTools 启动前检查数据库状态,失败时自动尝试启动 2. ai-core 添加数据库断线重连机制 (30秒间隔) 3. Dashboard 添加数据库状态卡片 (启动/停止/重启) 4. Gateway 会话空闲超时管理 (30分钟标记空闲) 5. 会话/消息 PostgreSQL 持久化 (SessionStore + REST API) 6. 前端服务端会话持久化 + URL hash 路由 + 侧边栏管理 7. 管理员回到主对话按钮 8. IoT 设备卡片固定排序 9. 更新相关文档
This commit is contained in:
@@ -1,8 +1,110 @@
|
||||
// 会话API
|
||||
export {
|
||||
createSession,
|
||||
listSessions,
|
||||
getSession,
|
||||
deleteSession,
|
||||
fetchSessionMessages,
|
||||
} from './client';
|
||||
// 会话持久化 API — 对接 Gateway REST API
|
||||
|
||||
import { request } from './client';
|
||||
import type { Session, SessionListResponse, SessionMessagesResponse, SessionResponse } from '@/types/session';
|
||||
|
||||
/**
|
||||
* 获取用户的会话列表
|
||||
* GET /api/v1/sessions?user_id={userId}
|
||||
*/
|
||||
export async function fetchSessions(userId: string): Promise<Session[]> {
|
||||
const resp = await request<SessionListResponse>(
|
||||
`/sessions?user_id=${encodeURIComponent(userId)}`
|
||||
);
|
||||
if (resp.error) {
|
||||
console.error('[sessions] 获取会话列表失败:', resp.error);
|
||||
return [];
|
||||
}
|
||||
// Gateway 返回 { sessions: [...] }
|
||||
return (resp.data as SessionListResponse)?.sessions || [];
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取会话的消息历史
|
||||
* GET /api/v1/sessions/{sessionId}/messages?limit={limit}
|
||||
*/
|
||||
export async function fetchMessages(
|
||||
sessionId: string,
|
||||
limit: number = 50
|
||||
): Promise<SessionMessagesResponse> {
|
||||
const resp = await request<SessionMessagesResponse>(
|
||||
`/sessions/${encodeURIComponent(sessionId)}/messages?limit=${limit}`
|
||||
);
|
||||
if (resp.error) {
|
||||
console.error('[sessions] 获取消息失败:', resp.error);
|
||||
return { messages: [] };
|
||||
}
|
||||
return (resp.data as SessionMessagesResponse) || { messages: [] };
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建新会话
|
||||
* POST /api/v1/sessions
|
||||
*/
|
||||
export async function createSession(
|
||||
userId: string,
|
||||
sessionId: string,
|
||||
title: string = '新的对话',
|
||||
isMain: boolean = false
|
||||
): Promise<SessionResponse | null> {
|
||||
const resp = await request<SessionResponse>('/sessions', {
|
||||
method: 'POST',
|
||||
body: {
|
||||
user_id: userId,
|
||||
session_id: sessionId,
|
||||
title,
|
||||
is_main: isMain,
|
||||
},
|
||||
});
|
||||
if (resp.error) {
|
||||
console.error('[sessions] 创建会话失败:', resp.error);
|
||||
return null;
|
||||
}
|
||||
return resp.data as SessionResponse;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除单个会话 (不删除记忆)
|
||||
* DELETE /api/v1/sessions/{sessionId}
|
||||
*/
|
||||
export async function deleteSession(sessionId: string): Promise<boolean> {
|
||||
const resp = await request(`/sessions/${encodeURIComponent(sessionId)}`, {
|
||||
method: 'DELETE',
|
||||
});
|
||||
if (resp.error) {
|
||||
console.error('[sessions] 删除会话失败:', resp.error);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除用户所有会话 (不删除记忆)
|
||||
* DELETE /api/v1/sessions?user_id={userId}
|
||||
*/
|
||||
export async function deleteAllSessions(userId: string): Promise<boolean> {
|
||||
const resp = await request(`/sessions?user_id=${encodeURIComponent(userId)}`, {
|
||||
method: 'DELETE',
|
||||
});
|
||||
if (resp.error) {
|
||||
console.error('[sessions] 删除所有会话失败:', resp.error);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 清空会话消息但不删除会话本身
|
||||
* DELETE /api/v1/sessions/{sessionId}/messages
|
||||
*/
|
||||
export async function clearSessionMessages(sessionId: string): Promise<boolean> {
|
||||
const resp = await request(
|
||||
`/sessions/${encodeURIComponent(sessionId)}/messages`,
|
||||
{ method: 'DELETE' }
|
||||
);
|
||||
if (resp.error) {
|
||||
console.error('[sessions] 清空消息失败:', resp.error);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user