feat(nanobot-web): 添加会话创建接口并优化前端会话管理

- 新增 POST /api/sessions/{key} 接口用于立即创建或持久化会话
- 提取 _serialize_session_detail 函数以统一会话数据序列化逻辑
- 前端添加 createSession API 调用函数
- 实现本地存储中的会话ID持久化功能
- 优化 ChatPage 组件中的会话切换逻辑,确保状态正确重置
- 在消息处理中添加会话ID验证,避免跨会话消息混乱
- 新建会话时主动调用创建API并刷新会话列表
This commit is contained in:
2026-03-16 17:37:25 +08:00
parent b3767dd4ab
commit 344aee93fa
4 changed files with 62 additions and 9 deletions

View File

@ -1580,11 +1580,8 @@ def _register_routes(app: FastAPI) -> None:
sm: SessionManager = app.state.session_manager
return sm.list_sessions()
@app.get("/api/sessions/{key:path}")
async def get_session(key: str):
"""Get a session's message history."""
sm: SessionManager = app.state.session_manager
session = sm.get_or_create(key)
def _serialize_session_detail(session: Session) -> dict[str, Any]:
"""Build the filtered session payload returned to the web UI."""
# Filter out tool messages and assistant messages with tool_calls
# (intermediate steps), only keep user messages and final assistant replies
visible_messages = []
@ -1616,6 +1613,21 @@ def _register_routes(app: FastAPI) -> None:
"updated_at": session.updated_at.isoformat(),
}
@app.post("/api/sessions/{key:path}")
async def create_session(key: str):
"""Create or persist a session immediately."""
sm: SessionManager = app.state.session_manager
session = sm.get_or_create(key)
sm.save(session)
return _serialize_session_detail(session)
@app.get("/api/sessions/{key:path}")
async def get_session(key: str):
"""Get a session's message history."""
sm: SessionManager = app.state.session_manager
session = sm.get_or_create(key)
return _serialize_session_detail(session)
@app.delete("/api/sessions/{key:path}")
async def delete_session(key: str):
"""Delete a session."""