This commit is contained in:
DrSmoothl
2026-02-21 16:29:30 +08:00
parent eaef7f0e98
commit 6378bb2052
20 changed files with 57 additions and 69 deletions

View File

@@ -6,8 +6,6 @@ from src.common.logger import get_logger
from src.llm_models.utils_model import LLMRequest
from src.config.config import model_config
from src.chat.knowledge import qa_manager
from src.chat.utils.chat_message_builder import build_readable_messages
from src.chat.brain_chat.PFC.observation_info import dict_to_database_message
logger = get_logger("knowledge_fetcher")
@@ -48,13 +46,7 @@ class KnowledgeFetcher:
Returns:
Tuple[str, str]: (获取的知识, 知识来源)
"""
db_messages = [dict_to_database_message(m) for m in chat_history]
chat_history_text = build_readable_messages(
db_messages,
replace_bot_name=True,
timestamp_mode="relative",
read_mark=0.0,
)
_ = chat_history
# NOTE: Hippocampus memory system was redesigned in v0.12.2
# The old get_memory_from_text API no longer exists

View File

@@ -519,7 +519,7 @@ def _build_readable_messages_internal(
output_lines: List[str] = []
prev_timestamp: Optional[float] = None
for timestamp, name, content, is_action in detailed_message:
for timestamp, name, content, _is_action in detailed_message:
# 检查是否需要插入长时间间隔提示
if long_time_notice and prev_timestamp is not None:
time_diff = timestamp - prev_timestamp

View File

@@ -86,8 +86,8 @@ def init_dream_tools(chat_id: str) -> None:
finish_maintenance = make_finish_maintenance(chat_id)
search_jargon = make_search_jargon(chat_id)
delete_jargon = make_delete_jargon(chat_id)
update_jargon = make_update_jargon(chat_id)
_delete_jargon = make_delete_jargon(chat_id)
_update_jargon = make_update_jargon(chat_id)
_dream_tool_registry.register_tool(
DreamTool(

View File

@@ -34,7 +34,7 @@ def _cleanup_stale_not_found_thinking_back() -> None:
try:
with get_db_session() as session:
statement = select(ThinkingQuestion).where(
(ThinkingQuestion.found_answer == False)
col(ThinkingQuestion.found_answer).is_(False)
& (ThinkingQuestion.updated_timestamp < datetime.fromtimestamp(threshold_time))
)
records = session.exec(statement).all()
@@ -786,8 +786,7 @@ def _get_recent_query_history(chat_id: str, time_window_seconds: float = 600.0)
str: 格式化的查询历史字符串
"""
try:
current_time = time.time()
start_time = current_time - time_window_seconds
_current_time = time.time()
with get_db_session() as session:
statement = (
@@ -838,15 +837,14 @@ def _get_recent_found_answers(chat_id: str, time_window_seconds: float = 600.0)
List[str]: 格式化的答案列表,每个元素格式为 "问题xxx\n答案xxx"
"""
try:
current_time = time.time()
start_time = current_time - time_window_seconds
_current_time = time.time()
# 查询最近时间窗口内已找到答案的记录,按更新时间倒序
with get_db_session() as session:
statement = (
select(ThinkingQuestion)
.where(col(ThinkingQuestion.context) == chat_id)
.where(col(ThinkingQuestion.found_answer) == True)
.where(col(ThinkingQuestion.found_answer))
.where(col(ThinkingQuestion.answer).is_not(None))
.where(col(ThinkingQuestion.answer) != "")
.order_by(col(ThinkingQuestion.updated_timestamp).desc())

View File

@@ -218,7 +218,7 @@ async def get_log_detail(chat_id: str, filename: str):
data = json.load(f)
return PlanLogDetail(**data)
except Exception as e:
raise HTTPException(status_code=500, detail=f"读取日志失败: {str(e)}")
raise HTTPException(status_code=500, detail=f"读取日志失败: {str(e)}") from e
# ========== 兼容旧接口 ==========

View File

@@ -232,7 +232,7 @@ async def get_reply_log_detail(chat_id: str, filename: str):
success=data.get("success", True),
)
except Exception as e:
raise HTTPException(status_code=500, detail=f"读取日志失败: {str(e)}")
raise HTTPException(status_code=500, detail=f"读取日志失败: {str(e)}") from e
# ========== 兼容接口 ==========

View File

@@ -333,7 +333,7 @@ async def get_social_network(year: int = 2025) -> SocialNetworkData:
statement = select(func.count()).where(
col(Messages.timestamp) >= datetime.fromtimestamp(start_ts),
col(Messages.timestamp) <= datetime.fromtimestamp(end_ts),
col(Messages.is_at) == True,
col(Messages.is_at),
)
data.at_count = int(session.exec(statement).first() or 0)
@@ -342,7 +342,7 @@ async def get_social_network(year: int = 2025) -> SocialNetworkData:
statement = select(func.count()).where(
col(Messages.timestamp) >= datetime.fromtimestamp(start_ts),
col(Messages.timestamp) <= datetime.fromtimestamp(end_ts),
col(Messages.is_mentioned) == True,
col(Messages.is_mentioned),
)
data.mentioned_count = int(session.exec(statement).first() or 0)
@@ -552,7 +552,7 @@ async def get_expression_vibe(year: int = 2025) -> ExpressionVibeData:
# 1. 表情包之王 - 使用次数最多的表情包
with get_db_session() as session:
statement = (
select(Images).where(col(Images.is_registered) == True).order_by(desc(col(Images.query_count))).limit(5)
select(Images).where(col(Images.is_registered)).order_by(desc(col(Images.query_count))).limit(5)
)
top_emojis = session.exec(statement).all()
if top_emojis:
@@ -636,7 +636,7 @@ async def get_expression_vibe(year: int = 2025) -> ExpressionVibeData:
statement = select(func.count()).where(
col(Messages.timestamp) >= datetime.fromtimestamp(start_ts),
col(Messages.timestamp) <= datetime.fromtimestamp(end_ts),
col(Messages.is_picture) == True,
col(Messages.is_picture),
)
data.image_processed_count = int(session.exec(statement).first() or 0)
@@ -781,12 +781,12 @@ async def get_achievements(year: int = 2025) -> AchievementData:
# 1. 新学到的黑话数量
# Jargon 表没有时间字段,统计全部已确认的黑话
with get_db_session() as session:
statement = select(func.count()).where(col(Jargon.is_jargon) == True)
statement = select(func.count()).where(col(Jargon.is_jargon))
data.new_jargon_count = int(session.exec(statement).first() or 0)
# 2. 代表性黑话示例
with get_db_session() as session:
statement = select(Jargon).where(col(Jargon.is_jargon) == True).order_by(desc(col(Jargon.count))).limit(5)
statement = select(Jargon).where(col(Jargon.is_jargon)).order_by(desc(col(Jargon.count))).limit(5)
jargon_samples = session.exec(statement).all()
data.sample_jargons = [
{

View File

@@ -532,7 +532,7 @@ async def get_emoji_stats(maibot_session: Optional[str] = Cookie(None), authoriz
.select_from(Images)
.where(
col(Images.image_type) == ImageType.EMOJI,
col(Images.is_registered) == True,
col(Images.is_registered),
)
)
banned_statement = (
@@ -540,7 +540,7 @@ async def get_emoji_stats(maibot_session: Optional[str] = Cookie(None), authoriz
.select_from(Images)
.where(
col(Images.image_type) == ImageType.EMOJI,
col(Images.is_banned) == True,
col(Images.is_banned),
)
)
@@ -1283,7 +1283,7 @@ async def preheat_thumbnail_cache(
select(Images)
.where(
col(Images.image_type) == ImageType.EMOJI,
col(Images.is_banned) == False,
col(Images.is_banned).is_(False),
)
.order_by(col(Images.query_count).desc())
.limit(limit * 2)

View File

@@ -315,15 +315,15 @@ async def get_jargon_stats():
total = session.exec(select(fn.count()).select_from(Jargon)).one()
confirmed_jargon = session.exec(
select(fn.count()).select_from(Jargon).where(col(Jargon.is_jargon) == True)
select(fn.count()).select_from(Jargon).where(col(Jargon.is_jargon))
).one()
confirmed_not_jargon = session.exec(
select(fn.count()).select_from(Jargon).where(col(Jargon.is_jargon) == False)
select(fn.count()).select_from(Jargon).where(col(Jargon.is_jargon).is_(False))
).one()
pending = session.exec(select(fn.count()).select_from(Jargon).where(col(Jargon.is_jargon).is_(None))).one()
complete_count = session.exec(
select(fn.count()).select_from(Jargon).where(col(Jargon.is_complete) == True)
select(fn.count()).select_from(Jargon).where(col(Jargon.is_complete))
).one()
chat_count = session.exec(

View File

@@ -370,7 +370,7 @@ async def get_person_stats(maibot_session: Optional[str] = Cookie(None), authori
with get_db_session() as session:
total = len(session.exec(select(PersonInfo.id)).all())
known = len(session.exec(select(PersonInfo.id).where(col(PersonInfo.is_known) == True)).all())
known = len(session.exec(select(PersonInfo.id).where(col(PersonInfo.is_known))).all())
unknown = total - known
# 按平台统计

View File

@@ -1762,7 +1762,7 @@ async def update_plugin_config_raw(
try:
tomlkit.loads(request.config)
except Exception as e:
raise HTTPException(status_code=400, detail=f"TOML 格式错误: {str(e)}")
raise HTTPException(status_code=400, detail=f"TOML 格式错误: {str(e)}") from e
# 备份旧配置
import shutil