- 新增 ConversationHistoryManager.get_tenant_summary() 按租户聚合会话统计 - get_sessions_paginated() 和 get_conversation_analytics() 增加 tenant_id 过滤 - 新增 GET /api/conversations/tenants 租户汇总端点 - sessions 和 analytics API 端点支持 tenant_id 查询参数 - 前端实现租户卡片列表视图和租户详情会话表格视图 - 实现面包屑导航、搜索范围限定、统计面板上下文切换 - 会话删除后自动检测空租户并返回列表视图 - dashboard.html 添加租户视图 DOM 容器 - 交互模式与知识库租户分组视图保持一致
9.2 KiB
9.2 KiB
Requirements Document
Introduction
对话历史租户分组展示功能。当前对话历史页面以扁平的会话列表展示所有 ChatSession 记录,缺乏租户(市场)维度的组织结构。本功能将对话历史页面改造为两层结构:第一层按租户分组展示汇总信息(会话总数、消息总数、最近活跃时间等),第二层展示某个租户下的具体会话列表。点击具体会话仍可查看消息详情(保留现有功能)。交互模式与知识库租户分组视图保持一致,包括卡片视图、面包屑导航、搜索范围限定和统计面板上下文切换。
Glossary
- Dashboard: Flask + Jinja2 + Bootstrap 5 构建的 Web 管理后台主页面(
dashboard.html) - Conversation_Tab: Dashboard 中
#conversation-history-tab区域,用于展示和管理对话历史 - Conversation_API: Flask Blueprint
conversations_bp,提供对话相关的 REST API(/api/conversations/*) - History_Manager:
ConversationHistoryManager类,封装对话历史的数据库查询与业务逻辑 - Tenant: 租户,即市场标识(如
market_a、market_b),通过ChatSession.tenant_id字段区分 - Tenant_Summary: 租户汇总信息,包含租户 ID、会话总数、消息总数、活跃会话数、最近活跃时间等聚合数据
- Tenant_List_View: 第一层视图,以卡片形式展示所有租户的对话汇总信息
- Tenant_Detail_View: 第二层视图,展示某个租户下的具体会话列表(含分页、筛选)
- ChatSession: SQLAlchemy 数据模型,包含
tenant_id、session_id、title、status、message_count、source、created_at、updated_at等字段 - Conversation: SQLAlchemy 数据模型,表示单条对话消息,包含
tenant_id、session_id、user_message、assistant_response等字段
Requirements
Requirement 1: 租户汇总 API
User Story: 作为管理员,我希望后端提供按租户分组的对话会话汇总接口,以便前端展示每个租户的对话统计。
Acceptance Criteria
- WHEN a GET request is sent to
/api/conversations/tenants, THE Conversation_API SHALL return a JSON array of Tenant_Summary objects, each containingtenant_id,session_count,message_count,active_session_count, andlast_active_time - THE Conversation_API SHALL compute
session_countby counting all ChatSession records for each Tenant - THE Conversation_API SHALL compute
message_countby summing themessage_countfield of all ChatSession records for each Tenant - THE Conversation_API SHALL compute
active_session_countby counting ChatSession records withstatus == 'active'for each Tenant - THE Conversation_API SHALL sort the Tenant_Summary array by
last_active_timein descending order - WHEN no ChatSession records exist, THE Conversation_API SHALL return an empty JSON array with HTTP status 200
- IF a database query error occurs, THEN THE Conversation_API SHALL return an error response with HTTP status 500 and a descriptive error message
Requirement 2: 租户会话列表 API
User Story: 作为管理员,我希望后端提供按租户筛选的会话分页接口,以便在点击某个租户后查看该租户下的具体会话列表。
Acceptance Criteria
- WHEN a GET request with query parameter
tenant_idis sent to/api/conversations/sessions, THE Conversation_API SHALL return only the ChatSession records belonging to the specified Tenant - THE Conversation_API SHALL support pagination via
pageandper_pagequery parameters when filtering bytenant_id - THE Conversation_API SHALL support
statusandsearchquery parameters for further filtering within a Tenant - WHEN the
tenant_idparameter value does not match any existing ChatSession records, THE Conversation_API SHALL return an empty session list withtotalequal to 0 and HTTP status 200 - THE History_Manager SHALL accept
tenant_idas a filter parameter in theget_sessions_paginatedmethod and return paginated results scoped to the specified Tenant
Requirement 3: 租户列表视图(第一层)
User Story: 作为管理员,我希望对话历史页面首先展示按租户分组的汇总卡片,以便快速了解各市场的对话活跃度。
Acceptance Criteria
- WHEN the Conversation_Tab is activated, THE Dashboard SHALL display a Tenant_List_View showing one card per Tenant
- THE Tenant_List_View SHALL display the following information for each Tenant: tenant_id(租户名称), session_count(会话总数), message_count(消息总数), active_session_count(活跃会话数), last_active_time(最近活跃时间)
- WHEN the Tenant_List_View is loading data, THE Dashboard SHALL display a loading spinner in the Conversation_Tab area
- WHEN no tenants exist, THE Dashboard SHALL display a placeholder message indicating that no conversation sessions are available
- THE Tenant_List_View SHALL refresh its data when the user clicks a refresh button
Requirement 4: 租户详情视图(第二层)
User Story: 作为管理员,我希望点击某个租户卡片后能查看该租户下的具体会话列表,以便管理和审查对话内容。
Acceptance Criteria
- WHEN a user clicks on a Tenant card in the Tenant_List_View, THE Dashboard SHALL transition to the Tenant_Detail_View showing ChatSession records for the selected Tenant
- THE Tenant_Detail_View SHALL display each ChatSession with the following fields: title(会话标题), message_count(消息数), status(状态), source(来源), created_at(创建时间), updated_at(最近更新时间)
- THE Tenant_Detail_View SHALL provide a breadcrumb navigation showing "对话历史 > {tenant_id}" to indicate the current context
- WHEN the user clicks the breadcrumb "对话历史" link, THE Dashboard SHALL navigate back to the Tenant_List_View
- THE Tenant_Detail_View SHALL support pagination with configurable page size
- THE Tenant_Detail_View SHALL support filtering by session status and date range
Requirement 5: 会话详情查看(第三层保留)
User Story: 作为管理员,我希望在租户详情视图中点击某个会话后能查看该会话的消息详情,以便审查具体对话内容。
Acceptance Criteria
- WHEN a user clicks on a ChatSession row in the Tenant_Detail_View, THE Dashboard SHALL display the message detail view showing all Conversation records for the selected ChatSession
- THE Dashboard SHALL retain the existing message detail display logic and UI layout
- THE Dashboard SHALL provide a breadcrumb navigation showing "对话历史 > {tenant_id} > {session_title}" in the message detail view
- WHEN the user clicks the breadcrumb "{tenant_id}" link, THE Dashboard SHALL navigate back to the Tenant_Detail_View for the corresponding Tenant
Requirement 6: 会话管理操作
User Story: 作为管理员,我希望在租户详情视图中能对会话执行删除操作,以便维护对话历史数据。
Acceptance Criteria
- WHILE viewing the Tenant_Detail_View, THE Dashboard SHALL provide a delete button for each ChatSession row
- WHEN a user deletes a ChatSession in the Tenant_Detail_View, THE Conversation_API SHALL delete the ChatSession and all associated Conversation records
- WHEN a user deletes a ChatSession, THE Dashboard SHALL refresh the Tenant_Detail_View to reflect the updated data
- WHEN a user deletes all ChatSession records for a Tenant, THE Dashboard SHALL navigate back to the Tenant_List_View and remove the empty Tenant card
- IF a ChatSession deletion fails, THEN THE Dashboard SHALL display an error notification with the failure reason
Requirement 7: 搜索功能适配
User Story: 作为管理员,我希望在租户详情视图中搜索会话时,搜索范围限定在当前租户内,以便精确查找。
Acceptance Criteria
- WHILE viewing the Tenant_Detail_View, THE Dashboard SHALL scope the session search to the currently selected Tenant
- WHEN a search query is submitted in the Tenant_Detail_View, THE Conversation_API SHALL filter search results by the specified
tenant_id - WHEN the search query is cleared, THE Dashboard SHALL restore the full paginated session list for the current Tenant
- THE History_Manager search method SHALL accept an optional
tenant_idparameter to limit search scope
Requirement 8: 统计信息适配
User Story: 作为管理员,我希望对话历史统计面板在租户列表视图时展示全局统计,在租户详情视图时展示当前租户的统计,以便获取准确的上下文信息。
Acceptance Criteria
- WHILE the Tenant_List_View is displayed, THE Dashboard SHALL show global conversation statistics (total sessions across all tenants, total messages, total active sessions)
- WHILE the Tenant_Detail_View is displayed, THE Dashboard SHALL show statistics scoped to the selected Tenant
- WHEN a GET request with query parameter
tenant_idis sent to/api/conversations/analytics, THE Conversation_API SHALL return analytics data filtered by the specified Tenant - WHEN the
tenant_idparameter is omitted from the analytics request, THE Conversation_API SHALL return global analytics across all tenants