添加长期存储,流式检查
Some checks failed
构建并部署 AI Agent 服务 / deploy (push) Has been cancelled

This commit is contained in:
2026-04-17 01:26:05 +08:00
parent 602d551fd1
commit 404efde282
37 changed files with 794 additions and 2095 deletions

View File

@@ -14,14 +14,22 @@ from frontend.config import config
def render_sidebar():
"""渲染左侧栏"""
_render_user_section()
# 顶部放置新对话按钮,像 ChatGPT/DeepSeek 一样显眼
_render_history_actions()
st.divider()
# 历史列表
_render_history_section()
# 底部放用户部分
st.divider()
_render_user_section()
def _render_user_section():
"""渲染用户登录区域"""
st.header("👤 用户")
# st.header("👤 用户") # 移除显眼的标题,改用更柔和的 caption
st.caption("👤 用户管理")
if not AppState.is_logged_in():
_render_login_form()
@@ -32,58 +40,62 @@ def _render_user_section():
def _render_login_form():
"""渲染登录表单"""
username = st.text_input(
"输入用户名(可选)",
"用户名",
key="login_input",
placeholder="留空使用默认用户",
help="未登录将使用 default_user可能导致对话污染"
placeholder="输入用户名...",
help="未登录将使用 default_user可能导致对话污染",
label_visibility="collapsed"
)
if st.button("进入", type="primary", use_container_width=True):
if st.button("进入", type="secondary", use_container_width=True):
AppState.login(username)
_refresh_threads()
st.rerun()
st.info("💡 建议登录以隔离对话历史")
# st.info("💡 建议登录以隔离对话历史") # 移除多余色块
def _render_user_info():
"""渲染用户信息"""
st.success(f"当前用户: `{AppState.get_user_id()}`")
st.markdown(f"**当前用户**: `{AppState.get_user_id()}`")
if st.button("🔄 切换用户", use_container_width=True):
if st.button("切换用户", type="secondary", use_container_width=True):
AppState.logout()
_refresh_threads()
st.rerun()
def _render_history_section():
"""渲染历史对话列表"""
st.header("📚 对话历史")
col1, col2 = st.columns([3, 1])
with col1:
st.caption("📚 对话历史")
with col2:
if st.button("🔄", help="刷新列表", key="refresh_history_btn"):
_refresh_threads()
# 操作按钮
_render_history_actions()
st.divider()
# 历史列表
_render_thread_list()
def _render_history_actions():
"""渲染历史操作按钮"""
if st.button("🔄 刷新列表", use_container_width=True):
_refresh_threads()
if st.button(" 新对话", type="primary", use_container_width=True):
# 移除了 type="primary",让它变成普通的线框按钮,不再是大红块
if st.button(" 新对话", use_container_width=True):
AppState.start_new_thread()
st.rerun()
def _render_thread_list():
"""渲染线程列表"""
# 仅在初次加载时拉取,或由外部主动调用 _refresh_threads() 更新
if "threads_loaded" not in st.session_state:
_refresh_threads()
st.session_state.threads_loaded = True
threads = AppState.get_threads()
if not threads:
st.info("暂无对话历史")
st.caption("暂无对话历史")
return
for thread in threads:
@@ -98,28 +110,23 @@ def _render_thread_item(thread: dict):
thread: 线程信息字典
"""
thread_id = thread["thread_id"]
summary = thread.get("summary", "对话")
message_count = thread.get("message_count", 0)
last_updated = thread.get("last_updated", "")
# 格式化时间
time_str = _format_time(last_updated)
summary = thread.get("summary", "对话")
# 判断是否为当前线程
is_current = thread_id == AppState.get_current_thread_id()
button_type = "primary" if is_current else "secondary"
# 截断摘要
summary_display = summary[:config.summary_max_length]
if len(summary) > config.summary_max_length:
summary_display += "..."
# 根据是否当前线程改变按钮样式
btn_type = "primary" if is_current else "tertiary"
# 为了避免内容过长,截断摘要
display_text = summary[:15] + "..." if len(summary) > 15 else summary
# 渲染按钮
if st.button(
f"💬 {summary_display}\n\n🕐 {time_str} | {message_count}",
display_text,
key=f"thread_{thread_id}",
help=f"完整摘要: {summary}",
use_container_width=True,
type=button_type
type=btn_type
):
_load_thread(thread_id)