2026-02-24 08:10:27 +00:00
|
|
|
"""
|
|
|
|
|
Session monitoring and management endpoints.
|
|
|
|
|
[AC-ASA-07, AC-ASA-09] Session list and detail monitoring.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import logging
|
feat(ai-service): v0.2.0 前后端联调真实对接
实现内容:
- 新增知识库实体模型 (KnowledgeBase, Document, IndexJob)
- 新增 KBService 服务层,支持文档上传、存储、索引任务管理
- 实现知识库管理 API 真实对接 (POST/GET /admin/kb/documents)
- 实现索引任务状态查询 API (GET /admin/kb/index/jobs/{jobId})
- 实现 RAG 实验室真实向量检索 (POST /admin/rag/experiments/run)
- 实现会话监控真实数据库查询 (GET /admin/sessions)
规范更新:
- requirements.md: v0.1.0 -> v0.2.0, 新增 AC-AISVC-21~28
- tasks.md: v0.1.0 -> v0.2.0, 新增 Phase 6 (9个任务)
- openapi.admin.yaml: L0 -> L1, 更新 x-requirements 映射
验收标准: AC-AISVC-21, AC-AISVC-22, AC-AISVC-23, AC-AISVC-24,
AC-AISVC-25, AC-AISVC-26, AC-AISVC-27, AC-AISVC-28
2026-02-24 10:16:29 +00:00
|
|
|
from typing import Annotated, Optional, Sequence
|
2026-02-24 08:10:27 +00:00
|
|
|
from datetime import datetime
|
|
|
|
|
|
|
|
|
|
from fastapi import APIRouter, Depends, Query
|
|
|
|
|
from fastapi.responses import JSONResponse
|
feat(ai-service): v0.2.0 前后端联调真实对接
实现内容:
- 新增知识库实体模型 (KnowledgeBase, Document, IndexJob)
- 新增 KBService 服务层,支持文档上传、存储、索引任务管理
- 实现知识库管理 API 真实对接 (POST/GET /admin/kb/documents)
- 实现索引任务状态查询 API (GET /admin/kb/index/jobs/{jobId})
- 实现 RAG 实验室真实向量检索 (POST /admin/rag/experiments/run)
- 实现会话监控真实数据库查询 (GET /admin/sessions)
规范更新:
- requirements.md: v0.1.0 -> v0.2.0, 新增 AC-AISVC-21~28
- tasks.md: v0.1.0 -> v0.2.0, 新增 Phase 6 (9个任务)
- openapi.admin.yaml: L0 -> L1, 更新 x-requirements 映射
验收标准: AC-AISVC-21, AC-AISVC-22, AC-AISVC-23, AC-AISVC-24,
AC-AISVC-25, AC-AISVC-26, AC-AISVC-27, AC-AISVC-28
2026-02-24 10:16:29 +00:00
|
|
|
from sqlalchemy import select, func
|
|
|
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
|
from sqlmodel import col
|
2026-02-24 08:10:27 +00:00
|
|
|
|
feat(ai-service): v0.2.0 前后端联调真实对接
实现内容:
- 新增知识库实体模型 (KnowledgeBase, Document, IndexJob)
- 新增 KBService 服务层,支持文档上传、存储、索引任务管理
- 实现知识库管理 API 真实对接 (POST/GET /admin/kb/documents)
- 实现索引任务状态查询 API (GET /admin/kb/index/jobs/{jobId})
- 实现 RAG 实验室真实向量检索 (POST /admin/rag/experiments/run)
- 实现会话监控真实数据库查询 (GET /admin/sessions)
规范更新:
- requirements.md: v0.1.0 -> v0.2.0, 新增 AC-AISVC-21~28
- tasks.md: v0.1.0 -> v0.2.0, 新增 Phase 6 (9个任务)
- openapi.admin.yaml: L0 -> L1, 更新 x-requirements 映射
验收标准: AC-AISVC-21, AC-AISVC-22, AC-AISVC-23, AC-AISVC-24,
AC-AISVC-25, AC-AISVC-26, AC-AISVC-27, AC-AISVC-28
2026-02-24 10:16:29 +00:00
|
|
|
from app.core.database import get_session
|
|
|
|
|
from app.core.exceptions import MissingTenantIdException
|
2026-02-24 08:10:27 +00:00
|
|
|
from app.core.tenant import get_tenant_id
|
|
|
|
|
from app.models import ErrorResponse
|
feat(ai-service): v0.2.0 前后端联调真实对接
实现内容:
- 新增知识库实体模型 (KnowledgeBase, Document, IndexJob)
- 新增 KBService 服务层,支持文档上传、存储、索引任务管理
- 实现知识库管理 API 真实对接 (POST/GET /admin/kb/documents)
- 实现索引任务状态查询 API (GET /admin/kb/index/jobs/{jobId})
- 实现 RAG 实验室真实向量检索 (POST /admin/rag/experiments/run)
- 实现会话监控真实数据库查询 (GET /admin/sessions)
规范更新:
- requirements.md: v0.1.0 -> v0.2.0, 新增 AC-AISVC-21~28
- tasks.md: v0.1.0 -> v0.2.0, 新增 Phase 6 (9个任务)
- openapi.admin.yaml: L0 -> L1, 更新 x-requirements 映射
验收标准: AC-AISVC-21, AC-AISVC-22, AC-AISVC-23, AC-AISVC-24,
AC-AISVC-25, AC-AISVC-26, AC-AISVC-27, AC-AISVC-28
2026-02-24 10:16:29 +00:00
|
|
|
from app.models.entities import ChatSession, ChatMessage, SessionStatus
|
2026-02-24 08:10:27 +00:00
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
router = APIRouter(prefix="/admin/sessions", tags=["Session Monitoring"])
|
|
|
|
|
|
|
|
|
|
|
feat(ai-service): v0.2.0 前后端联调真实对接
实现内容:
- 新增知识库实体模型 (KnowledgeBase, Document, IndexJob)
- 新增 KBService 服务层,支持文档上传、存储、索引任务管理
- 实现知识库管理 API 真实对接 (POST/GET /admin/kb/documents)
- 实现索引任务状态查询 API (GET /admin/kb/index/jobs/{jobId})
- 实现 RAG 实验室真实向量检索 (POST /admin/rag/experiments/run)
- 实现会话监控真实数据库查询 (GET /admin/sessions)
规范更新:
- requirements.md: v0.1.0 -> v0.2.0, 新增 AC-AISVC-21~28
- tasks.md: v0.1.0 -> v0.2.0, 新增 Phase 6 (9个任务)
- openapi.admin.yaml: L0 -> L1, 更新 x-requirements 映射
验收标准: AC-AISVC-21, AC-AISVC-22, AC-AISVC-23, AC-AISVC-24,
AC-AISVC-25, AC-AISVC-26, AC-AISVC-27, AC-AISVC-28
2026-02-24 10:16:29 +00:00
|
|
|
def get_current_tenant_id() -> str:
|
|
|
|
|
"""Dependency to get current tenant ID or raise exception."""
|
|
|
|
|
tenant_id = get_tenant_id()
|
|
|
|
|
if not tenant_id:
|
|
|
|
|
raise MissingTenantIdException()
|
|
|
|
|
return tenant_id
|
|
|
|
|
|
|
|
|
|
|
2026-02-24 08:10:27 +00:00
|
|
|
@router.get(
|
|
|
|
|
"",
|
|
|
|
|
operation_id="listSessions",
|
|
|
|
|
summary="Query session list",
|
|
|
|
|
description="[AC-ASA-09] Get list of sessions with pagination and filtering.",
|
|
|
|
|
responses={
|
|
|
|
|
200: {"description": "Session list with pagination"},
|
|
|
|
|
401: {"description": "Unauthorized", "model": ErrorResponse},
|
|
|
|
|
403: {"description": "Forbidden", "model": ErrorResponse},
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
async def list_sessions(
|
feat(ai-service): v0.2.0 前后端联调真实对接
实现内容:
- 新增知识库实体模型 (KnowledgeBase, Document, IndexJob)
- 新增 KBService 服务层,支持文档上传、存储、索引任务管理
- 实现知识库管理 API 真实对接 (POST/GET /admin/kb/documents)
- 实现索引任务状态查询 API (GET /admin/kb/index/jobs/{jobId})
- 实现 RAG 实验室真实向量检索 (POST /admin/rag/experiments/run)
- 实现会话监控真实数据库查询 (GET /admin/sessions)
规范更新:
- requirements.md: v0.1.0 -> v0.2.0, 新增 AC-AISVC-21~28
- tasks.md: v0.1.0 -> v0.2.0, 新增 Phase 6 (9个任务)
- openapi.admin.yaml: L0 -> L1, 更新 x-requirements 映射
验收标准: AC-AISVC-21, AC-AISVC-22, AC-AISVC-23, AC-AISVC-24,
AC-AISVC-25, AC-AISVC-26, AC-AISVC-27, AC-AISVC-28
2026-02-24 10:16:29 +00:00
|
|
|
tenant_id: Annotated[str, Depends(get_current_tenant_id)],
|
|
|
|
|
session: Annotated[AsyncSession, Depends(get_session)],
|
2026-02-24 08:10:27 +00:00
|
|
|
status: Annotated[Optional[str], Query()] = None,
|
|
|
|
|
start_time: Annotated[Optional[str], Query(alias="startTime")] = None,
|
|
|
|
|
end_time: Annotated[Optional[str], Query(alias="endTime")] = None,
|
|
|
|
|
page: int = Query(1, ge=1),
|
|
|
|
|
page_size: int = Query(20, ge=1, le=100),
|
|
|
|
|
) -> JSONResponse:
|
|
|
|
|
"""
|
|
|
|
|
[AC-ASA-09] List sessions with filtering and pagination.
|
|
|
|
|
"""
|
|
|
|
|
logger.info(
|
|
|
|
|
f"[AC-ASA-09] Listing sessions: tenant={tenant_id}, status={status}, "
|
|
|
|
|
f"start_time={start_time}, end_time={end_time}, page={page}, page_size={page_size}"
|
|
|
|
|
)
|
|
|
|
|
|
feat(ai-service): v0.2.0 前后端联调真实对接
实现内容:
- 新增知识库实体模型 (KnowledgeBase, Document, IndexJob)
- 新增 KBService 服务层,支持文档上传、存储、索引任务管理
- 实现知识库管理 API 真实对接 (POST/GET /admin/kb/documents)
- 实现索引任务状态查询 API (GET /admin/kb/index/jobs/{jobId})
- 实现 RAG 实验室真实向量检索 (POST /admin/rag/experiments/run)
- 实现会话监控真实数据库查询 (GET /admin/sessions)
规范更新:
- requirements.md: v0.1.0 -> v0.2.0, 新增 AC-AISVC-21~28
- tasks.md: v0.1.0 -> v0.2.0, 新增 Phase 6 (9个任务)
- openapi.admin.yaml: L0 -> L1, 更新 x-requirements 映射
验收标准: AC-AISVC-21, AC-AISVC-22, AC-AISVC-23, AC-AISVC-24,
AC-AISVC-25, AC-AISVC-26, AC-AISVC-27, AC-AISVC-28
2026-02-24 10:16:29 +00:00
|
|
|
stmt = select(ChatSession).where(ChatSession.tenant_id == tenant_id)
|
|
|
|
|
|
2026-02-24 08:10:27 +00:00
|
|
|
if status:
|
feat(ai-service): v0.2.0 前后端联调真实对接
实现内容:
- 新增知识库实体模型 (KnowledgeBase, Document, IndexJob)
- 新增 KBService 服务层,支持文档上传、存储、索引任务管理
- 实现知识库管理 API 真实对接 (POST/GET /admin/kb/documents)
- 实现索引任务状态查询 API (GET /admin/kb/index/jobs/{jobId})
- 实现 RAG 实验室真实向量检索 (POST /admin/rag/experiments/run)
- 实现会话监控真实数据库查询 (GET /admin/sessions)
规范更新:
- requirements.md: v0.1.0 -> v0.2.0, 新增 AC-AISVC-21~28
- tasks.md: v0.1.0 -> v0.2.0, 新增 Phase 6 (9个任务)
- openapi.admin.yaml: L0 -> L1, 更新 x-requirements 映射
验收标准: AC-AISVC-21, AC-AISVC-22, AC-AISVC-23, AC-AISVC-24,
AC-AISVC-25, AC-AISVC-26, AC-AISVC-27, AC-AISVC-28
2026-02-24 10:16:29 +00:00
|
|
|
stmt = stmt.where(ChatSession.metadata_["status"].as_string() == status)
|
|
|
|
|
|
|
|
|
|
if start_time:
|
|
|
|
|
try:
|
|
|
|
|
start_dt = datetime.fromisoformat(start_time.replace("Z", "+00:00"))
|
|
|
|
|
stmt = stmt.where(ChatSession.created_at >= start_dt)
|
|
|
|
|
except ValueError:
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
if end_time:
|
|
|
|
|
try:
|
|
|
|
|
end_dt = datetime.fromisoformat(end_time.replace("Z", "+00:00"))
|
|
|
|
|
stmt = stmt.where(ChatSession.created_at <= end_dt)
|
|
|
|
|
except ValueError:
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
count_stmt = select(func.count()).select_from(stmt.subquery())
|
|
|
|
|
total_result = await session.execute(count_stmt)
|
|
|
|
|
total = total_result.scalar() or 0
|
|
|
|
|
|
|
|
|
|
stmt = stmt.order_by(col(ChatSession.created_at).desc())
|
|
|
|
|
stmt = stmt.offset((page - 1) * page_size).limit(page_size)
|
|
|
|
|
|
|
|
|
|
result = await session.execute(stmt)
|
|
|
|
|
sessions = result.scalars().all()
|
2026-02-24 08:10:27 +00:00
|
|
|
|
feat(ai-service): v0.2.0 前后端联调真实对接
实现内容:
- 新增知识库实体模型 (KnowledgeBase, Document, IndexJob)
- 新增 KBService 服务层,支持文档上传、存储、索引任务管理
- 实现知识库管理 API 真实对接 (POST/GET /admin/kb/documents)
- 实现索引任务状态查询 API (GET /admin/kb/index/jobs/{jobId})
- 实现 RAG 实验室真实向量检索 (POST /admin/rag/experiments/run)
- 实现会话监控真实数据库查询 (GET /admin/sessions)
规范更新:
- requirements.md: v0.1.0 -> v0.2.0, 新增 AC-AISVC-21~28
- tasks.md: v0.1.0 -> v0.2.0, 新增 Phase 6 (9个任务)
- openapi.admin.yaml: L0 -> L1, 更新 x-requirements 映射
验收标准: AC-AISVC-21, AC-AISVC-22, AC-AISVC-23, AC-AISVC-24,
AC-AISVC-25, AC-AISVC-26, AC-AISVC-27, AC-AISVC-28
2026-02-24 10:16:29 +00:00
|
|
|
session_ids = [s.session_id for s in sessions]
|
|
|
|
|
|
|
|
|
|
if session_ids:
|
|
|
|
|
msg_count_stmt = (
|
|
|
|
|
select(
|
|
|
|
|
ChatMessage.session_id,
|
|
|
|
|
func.count(ChatMessage.id).label("count")
|
|
|
|
|
)
|
|
|
|
|
.where(
|
|
|
|
|
ChatMessage.tenant_id == tenant_id,
|
|
|
|
|
ChatMessage.session_id.in_(session_ids)
|
|
|
|
|
)
|
|
|
|
|
.group_by(ChatMessage.session_id)
|
|
|
|
|
)
|
|
|
|
|
msg_count_result = await session.execute(msg_count_stmt)
|
|
|
|
|
msg_counts = {row.session_id: row.count for row in msg_count_result}
|
|
|
|
|
else:
|
|
|
|
|
msg_counts = {}
|
|
|
|
|
|
|
|
|
|
data = []
|
|
|
|
|
for s in sessions:
|
|
|
|
|
session_status = SessionStatus.ACTIVE.value
|
|
|
|
|
if s.metadata_ and "status" in s.metadata_:
|
|
|
|
|
session_status = s.metadata_["status"]
|
|
|
|
|
|
|
|
|
|
end_time_val = None
|
|
|
|
|
if s.metadata_ and "endTime" in s.metadata_:
|
|
|
|
|
end_time_val = s.metadata_["endTime"]
|
|
|
|
|
|
|
|
|
|
data.append({
|
|
|
|
|
"sessionId": s.session_id,
|
|
|
|
|
"status": session_status,
|
|
|
|
|
"startTime": s.created_at.isoformat() + "Z",
|
|
|
|
|
"endTime": end_time_val,
|
|
|
|
|
"messageCount": msg_counts.get(s.session_id, 0),
|
|
|
|
|
"channelType": s.channel_type,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
total_pages = (total + page_size - 1) // page_size if total > 0 else 0
|
2026-02-24 08:10:27 +00:00
|
|
|
|
|
|
|
|
return JSONResponse(
|
|
|
|
|
content={
|
feat(ai-service): v0.2.0 前后端联调真实对接
实现内容:
- 新增知识库实体模型 (KnowledgeBase, Document, IndexJob)
- 新增 KBService 服务层,支持文档上传、存储、索引任务管理
- 实现知识库管理 API 真实对接 (POST/GET /admin/kb/documents)
- 实现索引任务状态查询 API (GET /admin/kb/index/jobs/{jobId})
- 实现 RAG 实验室真实向量检索 (POST /admin/rag/experiments/run)
- 实现会话监控真实数据库查询 (GET /admin/sessions)
规范更新:
- requirements.md: v0.1.0 -> v0.2.0, 新增 AC-AISVC-21~28
- tasks.md: v0.1.0 -> v0.2.0, 新增 Phase 6 (9个任务)
- openapi.admin.yaml: L0 -> L1, 更新 x-requirements 映射
验收标准: AC-AISVC-21, AC-AISVC-22, AC-AISVC-23, AC-AISVC-24,
AC-AISVC-25, AC-AISVC-26, AC-AISVC-27, AC-AISVC-28
2026-02-24 10:16:29 +00:00
|
|
|
"data": data,
|
2026-02-24 08:10:27 +00:00
|
|
|
"pagination": {
|
|
|
|
|
"page": page,
|
|
|
|
|
"pageSize": page_size,
|
|
|
|
|
"total": total,
|
|
|
|
|
"totalPages": total_pages,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.get(
|
|
|
|
|
"/{session_id}",
|
|
|
|
|
operation_id="getSessionDetail",
|
|
|
|
|
summary="Get session details",
|
|
|
|
|
description="[AC-ASA-07] Get full session details with messages and trace.",
|
|
|
|
|
responses={
|
|
|
|
|
200: {"description": "Full session details with messages and trace"},
|
|
|
|
|
401: {"description": "Unauthorized", "model": ErrorResponse},
|
|
|
|
|
403: {"description": "Forbidden", "model": ErrorResponse},
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
async def get_session_detail(
|
feat(ai-service): v0.2.0 前后端联调真实对接
实现内容:
- 新增知识库实体模型 (KnowledgeBase, Document, IndexJob)
- 新增 KBService 服务层,支持文档上传、存储、索引任务管理
- 实现知识库管理 API 真实对接 (POST/GET /admin/kb/documents)
- 实现索引任务状态查询 API (GET /admin/kb/index/jobs/{jobId})
- 实现 RAG 实验室真实向量检索 (POST /admin/rag/experiments/run)
- 实现会话监控真实数据库查询 (GET /admin/sessions)
规范更新:
- requirements.md: v0.1.0 -> v0.2.0, 新增 AC-AISVC-21~28
- tasks.md: v0.1.0 -> v0.2.0, 新增 Phase 6 (9个任务)
- openapi.admin.yaml: L0 -> L1, 更新 x-requirements 映射
验收标准: AC-AISVC-21, AC-AISVC-22, AC-AISVC-23, AC-AISVC-24,
AC-AISVC-25, AC-AISVC-26, AC-AISVC-27, AC-AISVC-28
2026-02-24 10:16:29 +00:00
|
|
|
tenant_id: Annotated[str, Depends(get_current_tenant_id)],
|
|
|
|
|
session: Annotated[AsyncSession, Depends(get_session)],
|
2026-02-24 08:10:27 +00:00
|
|
|
session_id: str,
|
|
|
|
|
) -> JSONResponse:
|
|
|
|
|
"""
|
|
|
|
|
[AC-ASA-07] Get session detail with messages and trace information.
|
|
|
|
|
"""
|
|
|
|
|
logger.info(
|
|
|
|
|
f"[AC-ASA-07] Getting session detail: tenant={tenant_id}, session_id={session_id}"
|
|
|
|
|
)
|
|
|
|
|
|
feat(ai-service): v0.2.0 前后端联调真实对接
实现内容:
- 新增知识库实体模型 (KnowledgeBase, Document, IndexJob)
- 新增 KBService 服务层,支持文档上传、存储、索引任务管理
- 实现知识库管理 API 真实对接 (POST/GET /admin/kb/documents)
- 实现索引任务状态查询 API (GET /admin/kb/index/jobs/{jobId})
- 实现 RAG 实验室真实向量检索 (POST /admin/rag/experiments/run)
- 实现会话监控真实数据库查询 (GET /admin/sessions)
规范更新:
- requirements.md: v0.1.0 -> v0.2.0, 新增 AC-AISVC-21~28
- tasks.md: v0.1.0 -> v0.2.0, 新增 Phase 6 (9个任务)
- openapi.admin.yaml: L0 -> L1, 更新 x-requirements 映射
验收标准: AC-AISVC-21, AC-AISVC-22, AC-AISVC-23, AC-AISVC-24,
AC-AISVC-25, AC-AISVC-26, AC-AISVC-27, AC-AISVC-28
2026-02-24 10:16:29 +00:00
|
|
|
session_stmt = select(ChatSession).where(
|
|
|
|
|
ChatSession.tenant_id == tenant_id,
|
|
|
|
|
ChatSession.session_id == session_id,
|
|
|
|
|
)
|
|
|
|
|
session_result = await session.execute(session_stmt)
|
|
|
|
|
chat_session = session_result.scalar_one_or_none()
|
|
|
|
|
|
|
|
|
|
if not chat_session:
|
|
|
|
|
return JSONResponse(
|
|
|
|
|
status_code=404,
|
|
|
|
|
content={
|
|
|
|
|
"code": "SESSION_NOT_FOUND",
|
|
|
|
|
"message": f"Session {session_id} not found",
|
2026-02-24 08:10:27 +00:00
|
|
|
},
|
feat(ai-service): v0.2.0 前后端联调真实对接
实现内容:
- 新增知识库实体模型 (KnowledgeBase, Document, IndexJob)
- 新增 KBService 服务层,支持文档上传、存储、索引任务管理
- 实现知识库管理 API 真实对接 (POST/GET /admin/kb/documents)
- 实现索引任务状态查询 API (GET /admin/kb/index/jobs/{jobId})
- 实现 RAG 实验室真实向量检索 (POST /admin/rag/experiments/run)
- 实现会话监控真实数据库查询 (GET /admin/sessions)
规范更新:
- requirements.md: v0.1.0 -> v0.2.0, 新增 AC-AISVC-21~28
- tasks.md: v0.1.0 -> v0.2.0, 新增 Phase 6 (9个任务)
- openapi.admin.yaml: L0 -> L1, 更新 x-requirements 映射
验收标准: AC-AISVC-21, AC-AISVC-22, AC-AISVC-23, AC-AISVC-24,
AC-AISVC-25, AC-AISVC-26, AC-AISVC-27, AC-AISVC-28
2026-02-24 10:16:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
messages_stmt = (
|
|
|
|
|
select(ChatMessage)
|
|
|
|
|
.where(
|
|
|
|
|
ChatMessage.tenant_id == tenant_id,
|
|
|
|
|
ChatMessage.session_id == session_id,
|
|
|
|
|
)
|
|
|
|
|
.order_by(col(ChatMessage.created_at).asc())
|
|
|
|
|
)
|
|
|
|
|
messages_result = await session.execute(messages_stmt)
|
|
|
|
|
messages = messages_result.scalars().all()
|
|
|
|
|
|
|
|
|
|
messages_data = []
|
|
|
|
|
for msg in messages:
|
|
|
|
|
msg_data = {
|
|
|
|
|
"role": msg.role,
|
|
|
|
|
"content": msg.content,
|
|
|
|
|
"timestamp": msg.created_at.isoformat() + "Z",
|
|
|
|
|
}
|
|
|
|
|
messages_data.append(msg_data)
|
|
|
|
|
|
|
|
|
|
trace = _build_trace_info(messages)
|
|
|
|
|
|
|
|
|
|
return JSONResponse(
|
|
|
|
|
content={
|
|
|
|
|
"sessionId": session_id,
|
|
|
|
|
"messages": messages_data,
|
|
|
|
|
"trace": trace,
|
|
|
|
|
"metadata": chat_session.metadata_ or {},
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _build_trace_info(messages: Sequence[ChatMessage]) -> dict:
|
|
|
|
|
"""
|
|
|
|
|
Build trace information from messages.
|
|
|
|
|
This extracts retrieval and tool call information from message metadata.
|
|
|
|
|
"""
|
|
|
|
|
trace = {
|
|
|
|
|
"retrieval": [],
|
|
|
|
|
"tools": [],
|
|
|
|
|
"errors": [],
|
2026-02-24 08:10:27 +00:00
|
|
|
}
|
|
|
|
|
|
feat(ai-service): v0.2.0 前后端联调真实对接
实现内容:
- 新增知识库实体模型 (KnowledgeBase, Document, IndexJob)
- 新增 KBService 服务层,支持文档上传、存储、索引任务管理
- 实现知识库管理 API 真实对接 (POST/GET /admin/kb/documents)
- 实现索引任务状态查询 API (GET /admin/kb/index/jobs/{jobId})
- 实现 RAG 实验室真实向量检索 (POST /admin/rag/experiments/run)
- 实现会话监控真实数据库查询 (GET /admin/sessions)
规范更新:
- requirements.md: v0.1.0 -> v0.2.0, 新增 AC-AISVC-21~28
- tasks.md: v0.1.0 -> v0.2.0, 新增 Phase 6 (9个任务)
- openapi.admin.yaml: L0 -> L1, 更新 x-requirements 映射
验收标准: AC-AISVC-21, AC-AISVC-22, AC-AISVC-23, AC-AISVC-24,
AC-AISVC-25, AC-AISVC-26, AC-AISVC-27, AC-AISVC-28
2026-02-24 10:16:29 +00:00
|
|
|
for msg in messages:
|
|
|
|
|
if msg.role == "assistant":
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
return trace
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.put(
|
|
|
|
|
"/{session_id}/status",
|
|
|
|
|
operation_id="updateSessionStatus",
|
|
|
|
|
summary="Update session status",
|
|
|
|
|
description="[AC-ASA-09] Update session status (active, closed, expired).",
|
|
|
|
|
responses={
|
|
|
|
|
200: {"description": "Session status updated"},
|
|
|
|
|
404: {"description": "Session not found"},
|
|
|
|
|
401: {"description": "Unauthorized", "model": ErrorResponse},
|
|
|
|
|
403: {"description": "Forbidden", "model": ErrorResponse},
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
async def update_session_status(
|
|
|
|
|
tenant_id: Annotated[str, Depends(get_current_tenant_id)],
|
|
|
|
|
db_session: Annotated[AsyncSession, Depends(get_session)],
|
|
|
|
|
session_id: str,
|
|
|
|
|
status: str = Query(..., description="New status: active, closed, expired"),
|
|
|
|
|
) -> JSONResponse:
|
|
|
|
|
"""
|
|
|
|
|
[AC-ASA-09] Update session status.
|
|
|
|
|
"""
|
|
|
|
|
logger.info(
|
|
|
|
|
f"[AC-ASA-09] Updating session status: tenant={tenant_id}, "
|
|
|
|
|
f"session_id={session_id}, status={status}"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
stmt = select(ChatSession).where(
|
|
|
|
|
ChatSession.tenant_id == tenant_id,
|
|
|
|
|
ChatSession.session_id == session_id,
|
|
|
|
|
)
|
|
|
|
|
result = await db_session.execute(stmt)
|
|
|
|
|
chat_session = result.scalar_one_or_none()
|
|
|
|
|
|
|
|
|
|
if not chat_session:
|
|
|
|
|
return JSONResponse(
|
|
|
|
|
status_code=404,
|
|
|
|
|
content={
|
|
|
|
|
"code": "SESSION_NOT_FOUND",
|
|
|
|
|
"message": f"Session {session_id} not found",
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
metadata = chat_session.metadata_ or {}
|
|
|
|
|
metadata["status"] = status
|
|
|
|
|
|
|
|
|
|
if status == SessionStatus.CLOSED.value or status == SessionStatus.EXPIRED.value:
|
|
|
|
|
metadata["endTime"] = datetime.utcnow().isoformat() + "Z"
|
|
|
|
|
|
|
|
|
|
chat_session.metadata_ = metadata
|
|
|
|
|
chat_session.updated_at = datetime.utcnow()
|
|
|
|
|
await db_session.flush()
|
|
|
|
|
|
|
|
|
|
return JSONResponse(
|
|
|
|
|
content={
|
|
|
|
|
"success": True,
|
|
|
|
|
"sessionId": session_id,
|
|
|
|
|
"status": status,
|
|
|
|
|
}
|
|
|
|
|
)
|