From 6050fd1f2072b5a51e5c1cde96c69b6c1b22d0f9 Mon Sep 17 00:00:00 2001 From: Muika Date: Sat, 16 Aug 2025 17:02:32 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20=E4=BF=AE=E5=A4=8D=20get=5Fmessa?= =?UTF-8?q?ge=5Fid=20=E8=A2=AB=E5=BC=83=E7=94=A8=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98=E5=B9=B6=E7=AE=80=E5=8D=95=E4=BF=AE=E7=BC=AE=E4=BA=86?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E6=A0=BC=E5=BC=8F=20(#32)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com> Co-authored-by: Akarin~ <60691961+Asankilp@users.noreply.github.com> --- nonebot_plugin_marshoai/dev.py | 60 ++++++++++++---------- nonebot_plugin_marshoai/handler.py | 7 +-- nonebot_plugin_marshoai/marsho.py | 2 +- nonebot_plugin_marshoai/models.py | 7 +-- nonebot_plugin_marshoai/observer.py | 4 +- nonebot_plugin_marshoai/utils/processor.py | 7 ++- 6 files changed, 50 insertions(+), 37 deletions(-) diff --git a/nonebot_plugin_marshoai/dev.py b/nonebot_plugin_marshoai/dev.py index 956fc30..9c80beb 100644 --- a/nonebot_plugin_marshoai/dev.py +++ b/nonebot_plugin_marshoai/dev.py @@ -114,30 +114,38 @@ async def call_function( recursive=True, ) def on_plugin_file_change(event): - if event.src_path.endswith(".py"): - logger.info(f"文件变动: {event.src_path}") - # 层层向上查找到插件目录 - dir_list: list[str] = event.src_path.split("/") # type: ignore - dir_list[-1] = dir_list[-1].split(".", 1)[0] - dir_list.reverse() - for plugin_name in dir_list: - if plugin := get_plugin(plugin_name): - if plugin.module_path.endswith("__init__.py"): - # 包插件 - if os.path.dirname(plugin.module_path).replace( - "\\", "/" - ) in event.src_path.replace("\\", "/"): - logger.debug(f"找到变动插件: {plugin.name},正在重新加载") - reload_plugin(plugin) - context.reset_all() - break - else: - # 单文件插件 - if plugin.module_path == event.src_path: - logger.debug(f"找到变动插件: {plugin.name},正在重新加载") - reload_plugin(plugin) - context.reset_all() - break + if not event.src_path.endswith(".py"): + return + + logger.info(f"文件变动: {event.src_path}") + # 层层向上查找到插件目录 + dir_list: list[str] = event.src_path.split("/") # type: ignore + dir_list[-1] = dir_list[-1].split(".", 1)[0] + dir_list.reverse() + + for plugin_name in dir_list: + if not (plugin := get_plugin(plugin_name)): + continue + + if ( + plugin.module_path + and plugin.module_path.endswith("__init__.py") + and os.path.dirname(plugin.module_path).replace("\\", "/") + in event.src_path.replace("\\", "/") + ): # 包插件 + logger.debug(f"找到变动插件: {plugin.name},正在重新加载") + reload_plugin(plugin) + context.reset_all() + break else: - logger.debug("未找到变动插件") - return + # 单文件插件 + if plugin.module_path != event.src_path: + continue + + logger.debug(f"找到变动插件: {plugin.name},正在重新加载") + reload_plugin(plugin) + context.reset_all() + break + else: + logger.debug("未找到变动插件") + return diff --git a/nonebot_plugin_marshoai/handler.py b/nonebot_plugin_marshoai/handler.py index a47e409..f7e742b 100644 --- a/nonebot_plugin_marshoai/handler.py +++ b/nonebot_plugin_marshoai/handler.py @@ -22,6 +22,7 @@ from nonebot_plugin_alconna.uniseg import ( Text, UniMessage, UniMsg, + get_message_id, get_target, ) from nonebot_plugin_argot import Argot # type: ignore @@ -57,7 +58,7 @@ class MarshoHandler: self.event: Event = current_event.get() # self.state: T_State = current_handler.get().state self.matcher: Matcher = current_matcher.get() - self.message_id: str = UniMessage.get_message_id(self.event) + self.message_id: str = get_message_id(self.event) self.target = get_target(self.event) async def process_user_input( @@ -124,7 +125,7 @@ class MarshoHandler: async def handle_function_call( self, - completion: Union[ChatCompletion], + completion: Union[ChatCompletion, AsyncStream[ChatCompletionChunk]], user_message: Union[str, list], model_name: str, tools_list: list | None = None, @@ -248,7 +249,7 @@ class MarshoHandler: Text(await process_completion_to_details(response)), command="detail", expired_at=timedelta(minutes=5), - ) + ) # type:ignore ) # send_message.append( # Argot( diff --git a/nonebot_plugin_marshoai/marsho.py b/nonebot_plugin_marshoai/marsho.py index 241d4ee..d1e25f8 100644 --- a/nonebot_plugin_marshoai/marsho.py +++ b/nonebot_plugin_marshoai/marsho.py @@ -121,7 +121,7 @@ async def add_assistantmsg(target: MsgTarget, arg: Message = CommandArg()): @praises_cmd.handle() async def praises(): # await UniMessage(await tools.call("marshoai-weather.get_weather", {"location":"杭州"})).send() - await praises_cmd.finish(build_praises()) + await praises_cmd.finish(await build_praises()) @contexts_cmd.handle() diff --git a/nonebot_plugin_marshoai/models.py b/nonebot_plugin_marshoai/models.py index 59398c6..1634f65 100755 --- a/nonebot_plugin_marshoai/models.py +++ b/nonebot_plugin_marshoai/models.py @@ -1,9 +1,8 @@ import importlib +import importlib.util import json import os import sys - -# import importlib.util import traceback from nonebot import logger @@ -117,10 +116,12 @@ class MarshoTools: spec = importlib.util.spec_from_file_location( package_name, os.path.join(package_path, "__init__.py") ) + if not spec: + raise ImportError(f"工具包 {package_name} 未找到") package = importlib.util.module_from_spec(spec) self.imported_packages[package_name] = package sys.modules[package_name] = package - spec.loader.exec_module(package) + spec.loader.exec_module(package) # type:ignore logger.success(f"成功加载工具包 {package_name}") except json.JSONDecodeError as e: diff --git a/nonebot_plugin_marshoai/observer.py b/nonebot_plugin_marshoai/observer.py index 8d7573a..f1fb998 100755 --- a/nonebot_plugin_marshoai/observer.py +++ b/nonebot_plugin_marshoai/observer.py @@ -29,7 +29,7 @@ def debounce(wait): def wrapper(*args, **kwargs): nonlocal last_call_time current_time = time.time() - if (current_time - last_call_time) > wait: + if last_call_time is None or (current_time - last_call_time) > wait: last_call_time = current_time return func(*args, **kwargs) @@ -52,7 +52,7 @@ class CodeModifiedHandler(FileSystemEventHandler): """ @debounce(1) - def on_modified(self, event): + def on_modified(self, event: FileSystemEvent): raise NotImplementedError("on_modified must be implemented") def on_created(self, event): diff --git a/nonebot_plugin_marshoai/utils/processor.py b/nonebot_plugin_marshoai/utils/processor.py index 8d49835..317c8a7 100644 --- a/nonebot_plugin_marshoai/utils/processor.py +++ b/nonebot_plugin_marshoai/utils/processor.py @@ -24,9 +24,9 @@ async def process_chat_stream( delta = chunk.choices[0].delta if ( hasattr(delta, "reasoning_content") - and delta.reasoning_content is not None + and delta.reasoning_content is not None # type:ignore ): - reasoning_contents += delta.reasoning_content + reasoning_contents += delta.reasoning_content # type:ignore else: if not is_answering: logger.info( @@ -72,6 +72,9 @@ async def process_chat_stream( async def process_completion_to_details(completion: ChatCompletion) -> str: + if not isinstance(completion, ChatCompletion): + return "暂不支持对流式调用用量的获取,或预期之外的输入" + usage_text = "" usage = completion.usage if usage is None: