优化 build_chat_stream_impression 方法,增加对现有 ChatStream 记录的查询,改进平台信息获取逻辑

This commit is contained in:
Windpicker-owo
2025-11-13 23:08:05 +08:00
parent 55ac2b55f8
commit e455bbcfb0

View File

@@ -258,11 +258,22 @@ class RelationshipFetcher:
try: try:
import time import time
from src.common.database.api.crud import CRUDBase
from src.common.database.api.specialized import get_or_create_chat_stream from src.common.database.api.specialized import get_or_create_chat_stream
from src.common.database.core.models import ChatStreams
# 先尝试通过stream_id直接查询现有记录
crud = CRUDBase(ChatStreams)
existing_stream = await crud.get_by(stream_id=stream_id)
platform = "unknown"
if existing_stream:
# 从现有记录获取platform
platform = getattr(existing_stream, 'platform', 'unknown') or "unknown"
logger.debug(f"从现有ChatStream获取到platform: {platform}, stream_id: {stream_id}")
else:
logger.debug(f"未找到现有ChatStream记录使用默认platform: unknown, stream_id: {stream_id}")
# 使用优化后的API带缓存
# 从stream_id解析platform或使用默认值
platform = stream_id.split("_")[0] if "_" in stream_id else "unknown"
current_time = time.time() current_time = time.time()
stream, _ = await get_or_create_chat_stream( stream, _ = await get_or_create_chat_stream(
@@ -286,6 +297,7 @@ class RelationshipFetcher:
"stream_impression_text": stream.__dict__.get("stream_impression_text"), "stream_impression_text": stream.__dict__.get("stream_impression_text"),
"stream_chat_style": stream.__dict__.get("stream_chat_style"), "stream_chat_style": stream.__dict__.get("stream_chat_style"),
"stream_topic_keywords": stream.__dict__.get("stream_topic_keywords"), "stream_topic_keywords": stream.__dict__.get("stream_topic_keywords"),
"stream_interest_score": stream.__dict__.get("stream_interest_score"),
} }
except Exception as e: except Exception as e:
logger.warning(f"访问stream对象属性失败: {e}") logger.warning(f"访问stream对象属性失败: {e}")