fix: 6 bug fixes - timezone CST, knowledge_ids mismatch, skill uninstall, dashboard stats, chat knowledge retrieval, skill selection

This commit is contained in:
qyyfw committed 2026-07-03 09:18:04 +08:00
1 parent f7ab61bd9d
commit 8ec808b515
6 files changed
+119 -49

No files matched your search

+43 -19
View File
@@ -4,7 +4,7 @@ from sqlalchemy import and_, desc, asc
from pydantic import BaseModel, Field
from typing import Optional
import uuid
from datetime import datetime
from datetime import datetime, timezone, timedelta
import re
import random
import json
@@ -26,6 +26,14 @@ router = APIRouter()
logger = logging.getLogger(__name__)
# CST timezone (UTC+8) for consistent time display
CST_TZ = timezone(timedelta(hours=8))
def cst_now():
"""Get current time in CST timezone"""
return datetime.now(CST_TZ)
class CreateConversationRequest(BaseModel):
title: str = Field(default="新对话")
@@ -99,8 +107,8 @@ def create_conversation(
status="active",
pinned=False,
knowledge_graph_count=0,
created_at=datetime.now(),
updated_at=datetime.now(),
created_at=cst_now(),
updated_at=cst_now(),
)
db.add(conv)
db.commit()
@@ -141,7 +149,7 @@ def update_conversation(
conv.status = req.status
if req.pinned is not None:
conv.pinned = req.pinned
conv.updated_at = datetime.now()
conv.updated_at = cst_now()
db.commit()
db.refresh(conv)
@@ -166,7 +174,7 @@ def delete_conversation(
# 软删除
conv.status = "archived"
conv.updated_at = datetime.now()
conv.updated_at = cst_now()
db.commit()
return {"code": 0, "message": "删除成功"}
@@ -228,7 +236,7 @@ async def send_message(
content=req.content,
token_used=len(req.content) // 2, # 粗略估算
model_id=req.model_id or conv.model_id,
created_at=datetime.now(),
created_at=cst_now(),
)
db.add(user_msg)
@@ -294,6 +302,22 @@ async def send_message(
if kbs:
kb_names = ", ".join([k.name for k in kbs])
system_parts.append(f"参考知识库: {kb_names}")
# 修复: 实际检索知识库文档内容注入到system prompt
kb_context_parts = []
for kb in kbs:
docs = db.query(KnowledgeDocument).filter(
KnowledgeDocument.knowledge_base_id == kb.id
).order_by(KnowledgeDocument.created_at.desc()).limit(5).all()
for doc in docs:
if doc.content:
# 截取前500字符避免prompt过长
kb_context_parts.append(f"[{kb.name}/{doc.name}]: {doc.content[:500]}")
if kb_context_parts:
kb_context = "\n".join(kb_context_parts)
# 限制总长度避免超出模型token上限
if len(kb_context) > 2000:
kb_context = kb_context[:2000] + "...(内容截断)"
system_parts.append(f"知识库内容参考:\n{kb_context}")
messages.append({"role": "system", "content": "; ".join(system_parts)})
@@ -403,7 +427,7 @@ async def send_message(
content=ai_content,
token_used=ai_token_count,
model_id=req.model_id or conv.model_id,
created_at=datetime.now(),
created_at=cst_now(),
)
db.add(ai_msg)
@@ -418,14 +442,14 @@ async def send_message(
used_skills = [s.skill_name for s in selected_skills]
ai_msg.used_skills = used_skills
# 更新每个被使用技能的use_count和last_used_at
now = datetime.now()
now = cst_now()
for s in selected_skills:
s.use_count = (s.use_count or 0) + 1
s.last_used_at = now
# 更新对话统计
conv.knowledge_graph_count = (conv.knowledge_graph_count or 0) + 1
conv.updated_at = datetime.now()
conv.updated_at = cst_now()
# 记录Token使用
token_usage = TokenUsageLog(
@@ -437,7 +461,7 @@ async def send_message(
prompt_tokens=prompt_tokens or len(req.content) // 2,
completion_tokens=completion_tokens or ai_token_count,
total_tokens=total_tokens or (len(req.content) // 2 + ai_token_count),
created_at=datetime.now(),
created_at=cst_now(),
)
db.add(token_usage)
@@ -454,7 +478,7 @@ async def send_message(
conversation_id=conv_id,
status="discovered",
token_used=ai_token_count,
created_at=datetime.now(),
created_at=cst_now(),
)
db.add(trajectory)
db.flush() # 确保trajectory先写入,再启动pipeline
@@ -511,7 +535,7 @@ def clear_conversation(
).delete()
conv.knowledge_graph_count = 0
conv.updated_at = datetime.now()
conv.updated_at = cst_now()
db.commit()
return {"code": 0, "message": "对话上下文已清空"}
@@ -550,8 +574,8 @@ def convert_to_knowledge_graph(
description=f"从对话「{conv.title}」自动生成的知识图谱",
directory_path="/",
document_count=0,
created_at=datetime.now(),
updated_at=datetime.now(),
created_at=cst_now(),
updated_at=cst_now(),
)
db.add(kb)
db.flush()
@@ -572,8 +596,8 @@ def convert_to_knowledge_graph(
file_size=len(msg.content.encode('utf-8')),
status="completed",
chunk_count=max(1, len(msg.content) // 500 + 1),
created_at=datetime.now(),
updated_at=datetime.now(),
created_at=cst_now(),
updated_at=cst_now(),
)
db.add(doc)
doc_count += 1
@@ -660,11 +684,11 @@ def convert_to_knowledge_graph(
# 更新知识库的知识图谱计数
kb.knowledge_graph_count = len(graph_nodes)
kb.updated_at = datetime.now()
kb.updated_at = cst_now()
# 更新对话的知识图谱计数
conv.knowledge_graph_count = (conv.knowledge_graph_count or 0) + 1
conv.updated_at = datetime.now()
conv.updated_at = cst_now()
db.commit()
@@ -776,6 +800,6 @@ def _execute_skill_tool(db: Session, func_name: str, args: dict, skills_list: li
# 更新skill使用计数
if matched_skill:
matched_skill.use_count = (matched_skill.use_count or 0) + 1
matched_skill.last_used_at = datetime.now()
matched_skill.last_used_at = cst_now()
return result