fix: 修复空会话返回 null 导致前端崩溃的问题 #194

Merged
huanghaosheng merged 3 commits from develop into v2 2026-06-22 12:40:38 +08:00
5 changed files with 32 additions and 12 deletions

View File

@@ -273,6 +273,9 @@ func (h *ConversationHandler) GetMessages(c *gin.Context) {
return
}
count, _ := h.msgRepo.GetMessageCount(c.Request.Context(), sessionID)
if messages == nil {
messages = []store.StoredMessage{}
}
c.JSON(http.StatusOK, gin.H{
"messages": messages,
"total": count,
@@ -314,6 +317,9 @@ func (h *ConversationHandler) GetMessages(c *gin.Context) {
}
messages := allMessages[start:]
if messages == nil {
messages = []models.Message{}
}
c.JSON(http.StatusOK, gin.H{
"messages": messages,
"total": total,

View File

@@ -90,7 +90,7 @@ func (r *PgMessageRepository) queryMessages(ctx context.Context, query string, a
}
defer pgxRows.Close()
var messages []StoredMessage
messages := make([]StoredMessage, 0)
for pgxRows.Next() {
var m StoredMessage
if err := pgxRows.Scan(&m.ID, &m.SessionID, &m.Role, &m.Content, &m.TokensUsed, &m.CreatedAt); err != nil {

View File

@@ -217,7 +217,15 @@ body {
background: rgba(10, 10, 15, 0.45);
backdrop-filter: blur(3px);
-webkit-backdrop-filter: blur(3px);
animation: fadeIn 0.2s ease;
cursor: pointer;
opacity: 0;
pointer-events: none;
transition: opacity 0.3s var(--transition-smooth);
}
.sidebar-backdrop--visible {
opacity: 1;
pointer-events: auto;
}
.sidebar {
@@ -232,13 +240,18 @@ body {
background: var(--color-surface);
border-right: 1px solid var(--color-border);
overflow: hidden;
animation: slideInLeft 0.3s var(--transition-smooth);
box-shadow: 8px 0 40px rgba(10, 10, 15, 0.4);
transition: transform 0.3s var(--transition-smooth);
}
@keyframes slideInLeft {
from { transform: translateX(-100%); }
to { transform: translateX(0); }
.sidebar--collapsed {
transform: translateX(-100%);
pointer-events: none;
}
.sidebar--open {
transform: translateX(0);
pointer-events: auto;
}
.sidebar--collapsed {

View File

@@ -118,8 +118,6 @@ export function SessionSidebar({
return groups;
}, [sessions, searchQuery]);
if (!open) return null;
const groupLabels: Record<TimeGroup, string> = {
today: t("sidebar.today"),
yesterday: t("sidebar.yesterday"),
@@ -128,8 +126,11 @@ export function SessionSidebar({
return (
<>
<div className="sidebar-backdrop" onClick={onClose} />
<div className="sidebar">
<div
className={`sidebar-backdrop ${open ? 'sidebar-backdrop--visible' : ''}`}
onClick={onClose}
/>
<div className={`sidebar ${open ? 'sidebar--open' : 'sidebar--collapsed'}`}>
{/* 头部:新建 + 收起 */}
<div className="sidebar__header">
<button className="sidebar__new-btn" onClick={onNewSession}>

View File

@@ -80,7 +80,7 @@ export function useSessionList(accessToken?: string | null) {
try {
const res = await listConversations(accessToken);
if (res.data) {
const list = res.data.conversations.map(toSessionSummary);
const list = (res.data.conversations || []).map(toSessionSummary);
setSessions(list);
// 恢复上次选中的会话(如果仍然存在)
const lastId = localStorage.getItem(LAST_ACTIVE_KEY);
@@ -193,7 +193,7 @@ export function useSessionList(accessToken?: string | null) {
try {
const res = await getConversationMessages(accessToken, sessionId);
if (res.data) {
return res.data.messages.map(toChatMessage);
return (res.data.messages || []).map(toChatMessage);
}
} catch (err) {
console.error("[SessionList] 加载消息失败:", err);