From e28e7e08e841745bed7a73972cd07fbe9eceb37d Mon Sep 17 00:00:00 2001 From: UnCLAS-Prommer Date: Tue, 12 Aug 2025 17:08:35 +0800 Subject: [PATCH 1/2] =?UTF-8?q?more=20typing=20fix=E5=92=8C=E9=98=B2?= =?UTF-8?q?=E7=82=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/llm_models/model_client/openai_client.py | 8 ++++---- src/plugin_system/apis/person_api.py | 4 ++-- src/plugin_system/apis/send_api.py | 10 ++++++---- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/llm_models/model_client/openai_client.py b/src/llm_models/model_client/openai_client.py index 6902889c5..c580899ad 100644 --- a/src/llm_models/model_client/openai_client.py +++ b/src/llm_models/model_client/openai_client.py @@ -271,14 +271,14 @@ async def _default_stream_response_handler( _insure_buffer_closed() raise ReqAbortException("请求被外部信号中断") # 空 choices / usage-only 帧的防御 - if not getattr(event, "choices", None) or len(event.choices) == 0: - if getattr(event, "usage", None): + if not hasattr(event, "choices") or not event.choices: + if hasattr(event, "usage") and event.usage: _usage_record = ( event.usage.prompt_tokens or 0, event.usage.completion_tokens or 0, event.usage.total_tokens or 0, ) - continue # 跳过本帧,避免访问 choices[0] + continue # 跳过本帧,避免访问 choices[0] delta = event.choices[0].delta # 获取当前块的delta内容 if hasattr(delta, "reasoning_content") and delta.reasoning_content: # type: ignore @@ -479,7 +479,7 @@ class OpenaiClient(BaseClient): req_task.cancel() raise ReqAbortException("请求被外部信号中断") await asyncio.sleep(0.1) # 等待0.5秒后再次检查任务&中断信号量状态 - + # logger.info(f"OpenAI请求时间: {model_info.model_identifier} {time.time() - start_time} \n{messages}") resp, usage_record = async_response_parser(req_task.result()) diff --git a/src/plugin_system/apis/person_api.py b/src/plugin_system/apis/person_api.py index c81e4747e..ed904003a 100644 --- a/src/plugin_system/apis/person_api.py +++ b/src/plugin_system/apis/person_api.py @@ -19,7 +19,7 @@ logger = get_logger("person_api") # ============================================================================= -def get_person_id(platform: str, user_id: int) -> str: +def get_person_id(platform: str, user_id: int | str) -> str: """根据平台和用户ID获取person_id Args: @@ -33,7 +33,7 @@ def get_person_id(platform: str, user_id: int) -> str: person_id = person_api.get_person_id("qq", 123456) """ try: - return Person(platform=platform, user_id=user_id).person_id + return Person(platform=platform, user_id=str(user_id)).person_id except Exception as e: logger.error(f"[PersonAPI] 获取person_id失败: platform={platform}, user_id={user_id}, error={e}") return "" diff --git a/src/plugin_system/apis/send_api.py b/src/plugin_system/apis/send_api.py index c96679f30..870c979f7 100644 --- a/src/plugin_system/apis/send_api.py +++ b/src/plugin_system/apis/send_api.py @@ -98,10 +98,12 @@ async def _send_to_target( if reply_message: anchor_message = message_dict_to_message_recv(reply_message) - anchor_message.update_chat_stream(target_stream) - reply_to_platform_id = ( - f"{anchor_message.message_info.platform}:{anchor_message.message_info.user_info.user_id}" - ) + if anchor_message: + anchor_message.update_chat_stream(target_stream) + assert anchor_message.message_info.user_info, "用户信息缺失" + reply_to_platform_id = ( + f"{anchor_message.message_info.platform}:{anchor_message.message_info.user_info.user_id}" + ) else: reply_to_platform_id = "" anchor_message = None From 9c412cd9bc9a81f0ca3dc5e2644256d356d6cef0 Mon Sep 17 00:00:00 2001 From: UnCLAS-Prommer Date: Tue, 12 Aug 2025 17:18:49 +0800 Subject: [PATCH 2/2] typing fix --- src/person_info/relationship_manager.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/person_info/relationship_manager.py b/src/person_info/relationship_manager.py index 9f95ba85c..0b84e04fb 100644 --- a/src/person_info/relationship_manager.py +++ b/src/person_info/relationship_manager.py @@ -342,7 +342,7 @@ class RelationshipManager: """ person = Person(person_id=person_id) person_name = person.person_name - nickname = person.nickname + # nickname = person.nickname know_times: float = person.know_times user_messages = bot_engaged_messages @@ -358,11 +358,13 @@ class RelationshipManager: if msg.get("user_id") == "system": continue try: - if not is_person_known(user_id=msg.get("user_id"), platform=msg.get("chat_info_platform")): - continue - msg_person = Person(user_id=msg.get("user_id"), platform=msg.get("chat_info_platform")) + user_id = msg.get("user_id") + platform = msg.get("chat_info_platform") + assert isinstance(user_id, str) and isinstance(platform, str) + if is_person_known(user_id=user_id, platform=platform): + msg_person = Person(user_id=user_id, platform=platform) except Exception as e: - logger.error(f"初始化Person失败: {msg}") + logger.error(f"初始化Person失败: {msg}, 出现错误: {e}") traceback.print_exc() continue # 跳过机器人自己