From 1cd60252b5bc30f392430802d0b5d4f6a90d6306 Mon Sep 17 00:00:00 2001 From: Akarin~ <60691961+Asankilp@users.noreply.github.com> Date: Wed, 26 Feb 2025 00:47:57 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=BE=9D=E8=B5=96=E6=B3=A8?= =?UTF-8?q?=E5=85=A5=E9=97=AE=E9=A2=98=EF=BC=9F=20(#17)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 实现缓存装饰器,优化数据获取和存储逻辑 * 重构代码,准备将聊天请求逻辑移入MarshoHandler * 记录点( * unfinished * 🎨 重写基本完毕 * 移除未使用import,添加漏掉的换行 * 修复依赖注入问题? --- nonebot_plugin_marshoai/handler.py | 5 +++-- nonebot_plugin_marshoai/marsho.py | 2 +- nonebot_plugin_marshoai/plugin/func_call/caller.py | 10 ++++++---- nonebot_plugin_marshoai/plugin/func_call/models.py | 2 +- 4 files changed, 11 insertions(+), 8 deletions(-) diff --git a/nonebot_plugin_marshoai/handler.py b/nonebot_plugin_marshoai/handler.py index e67a96c5..cccd02fa 100644 --- a/nonebot_plugin_marshoai/handler.py +++ b/nonebot_plugin_marshoai/handler.py @@ -133,7 +133,7 @@ class MarshoHandler: # except: # pass tool_msg.append(choice.message) - for tool_call in tool_calls: + for tool_call in tool_calls: # type: ignore try: function_args = json.loads(tool_call.function.arguments) except json.JSONDecodeError: @@ -160,6 +160,7 @@ class MarshoHandler: bot=self.bot, event=self.event, matcher=self.matcher, + state=None, ) ).call(**function_args) else: @@ -184,7 +185,7 @@ class MarshoHandler: tools_list: list, stream: bool = False, tool_message: Optional[list] = None, - ) -> Union[Tuple[UserMessage, ChatCompletionMessage], None]: + ) -> Optional[Tuple[UserMessage, ChatCompletionMessage]]: """ 处理一般聊天 """ diff --git a/nonebot_plugin_marshoai/marsho.py b/nonebot_plugin_marshoai/marsho.py index a922cc80..17255b17 100644 --- a/nonebot_plugin_marshoai/marsho.py +++ b/nonebot_plugin_marshoai/marsho.py @@ -264,7 +264,7 @@ async def marsho( context.append(context_user.as_dict(), target.id, target.private) context.append(context_assistant.to_dict(), target.id, target.private) else: - await UniMessage("没有回答").send() + return except Exception as e: await UniMessage(str(e) + suggest_solution(str(e))).send() traceback.print_exc() diff --git a/nonebot_plugin_marshoai/plugin/func_call/caller.py b/nonebot_plugin_marshoai/plugin/func_call/caller.py index 04ab747c..26681ece 100644 --- a/nonebot_plugin_marshoai/plugin/func_call/caller.py +++ b/nonebot_plugin_marshoai/plugin/func_call/caller.py @@ -72,7 +72,9 @@ class Caller: # if self.ctx.state is None: # return False, "State is None" - if self._rule and not await self._rule(self.ctx.bot, self.ctx.event): + if self._rule and not await self._rule( + self.ctx.bot, self.ctx.event, self.ctx.state + ): return False, "告诉用户 Rule Denied 规则不匹配" return True, "" @@ -113,9 +115,9 @@ class Caller: # 检查函数签名,确定依赖注入参数 sig = inspect.signature(func) for name, param in sig.parameters.items(): - if param.annotation == T_State: - self.di.state = name - continue # 防止后续判断T_State子类时报错 + # if param.annotation == T_State: + # self.di.state = name + # continue # 防止后续判断T_State子类时报错 if issubclass(param.annotation, Event) or isinstance( param.annotation, Event diff --git a/nonebot_plugin_marshoai/plugin/func_call/models.py b/nonebot_plugin_marshoai/plugin/func_call/models.py index 3eeefe05..1069d027 100644 --- a/nonebot_plugin_marshoai/plugin/func_call/models.py +++ b/nonebot_plugin_marshoai/plugin/func_call/models.py @@ -19,7 +19,7 @@ class SessionContext(BaseModel): bot: Bot event: Event matcher: Matcher - # state: T_State + state: T_State | None caller: Any = None class Config: