修复依赖注入问题? (#17)

* 实现缓存装饰器,优化数据获取和存储逻辑

* 重构代码,准备将聊天请求逻辑移入MarshoHandler

* 记录点(

* unfinished

* 🎨 重写基本完毕

* 移除未使用import,添加漏掉的换行

* 修复依赖注入问题?
This commit is contained in:
Akarin~ 2025-02-26 00:47:57 +08:00 committed by GitHub
parent aa53643aae
commit 1cd60252b5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 11 additions and 8 deletions

View File

@ -133,7 +133,7 @@ class MarshoHandler:
# except: # except:
# pass # pass
tool_msg.append(choice.message) tool_msg.append(choice.message)
for tool_call in tool_calls: for tool_call in tool_calls: # type: ignore
try: try:
function_args = json.loads(tool_call.function.arguments) function_args = json.loads(tool_call.function.arguments)
except json.JSONDecodeError: except json.JSONDecodeError:
@ -160,6 +160,7 @@ class MarshoHandler:
bot=self.bot, bot=self.bot,
event=self.event, event=self.event,
matcher=self.matcher, matcher=self.matcher,
state=None,
) )
).call(**function_args) ).call(**function_args)
else: else:
@ -184,7 +185,7 @@ class MarshoHandler:
tools_list: list, tools_list: list,
stream: bool = False, stream: bool = False,
tool_message: Optional[list] = None, tool_message: Optional[list] = None,
) -> Union[Tuple[UserMessage, ChatCompletionMessage], None]: ) -> Optional[Tuple[UserMessage, ChatCompletionMessage]]:
""" """
处理一般聊天 处理一般聊天
""" """

View File

@ -264,7 +264,7 @@ async def marsho(
context.append(context_user.as_dict(), target.id, target.private) context.append(context_user.as_dict(), target.id, target.private)
context.append(context_assistant.to_dict(), target.id, target.private) context.append(context_assistant.to_dict(), target.id, target.private)
else: else:
await UniMessage("没有回答").send() return
except Exception as e: except Exception as e:
await UniMessage(str(e) + suggest_solution(str(e))).send() await UniMessage(str(e) + suggest_solution(str(e))).send()
traceback.print_exc() traceback.print_exc()

View File

@ -72,7 +72,9 @@ class Caller:
# if self.ctx.state is None: # if self.ctx.state is None:
# return False, "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 False, "告诉用户 Rule Denied 规则不匹配"
return True, "" return True, ""
@ -113,9 +115,9 @@ class Caller:
# 检查函数签名,确定依赖注入参数 # 检查函数签名,确定依赖注入参数
sig = inspect.signature(func) sig = inspect.signature(func)
for name, param in sig.parameters.items(): for name, param in sig.parameters.items():
if param.annotation == T_State: # if param.annotation == T_State:
self.di.state = name # self.di.state = name
continue # 防止后续判断T_State子类时报错 # continue # 防止后续判断T_State子类时报错
if issubclass(param.annotation, Event) or isinstance( if issubclass(param.annotation, Event) or isinstance(
param.annotation, Event param.annotation, Event

View File

@ -19,7 +19,7 @@ class SessionContext(BaseModel):
bot: Bot bot: Bot
event: Event event: Event
matcher: Matcher matcher: Matcher
# state: T_State state: T_State | None
caller: Any = None caller: Any = None
class Config: class Config: