mirror of
https://github.com/LiteyukiStudio/nonebot-plugin-marshoai.git
synced 2025-09-24 12:26:26 +00:00
🐛 修复 get_message_id 被弃用的问题并简单修缮了代码格式 (#32)
Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com> Co-authored-by: Akarin~ <60691961+Asankilp@users.noreply.github.com>
This commit is contained in:
@ -114,30 +114,38 @@ async def call_function(
|
|||||||
recursive=True,
|
recursive=True,
|
||||||
)
|
)
|
||||||
def on_plugin_file_change(event):
|
def on_plugin_file_change(event):
|
||||||
if event.src_path.endswith(".py"):
|
if not event.src_path.endswith(".py"):
|
||||||
logger.info(f"文件变动: {event.src_path}")
|
return
|
||||||
# 层层向上查找到插件目录
|
|
||||||
dir_list: list[str] = event.src_path.split("/") # type: ignore
|
logger.info(f"文件变动: {event.src_path}")
|
||||||
dir_list[-1] = dir_list[-1].split(".", 1)[0]
|
# 层层向上查找到插件目录
|
||||||
dir_list.reverse()
|
dir_list: list[str] = event.src_path.split("/") # type: ignore
|
||||||
for plugin_name in dir_list:
|
dir_list[-1] = dir_list[-1].split(".", 1)[0]
|
||||||
if plugin := get_plugin(plugin_name):
|
dir_list.reverse()
|
||||||
if plugin.module_path.endswith("__init__.py"):
|
|
||||||
# 包插件
|
for plugin_name in dir_list:
|
||||||
if os.path.dirname(plugin.module_path).replace(
|
if not (plugin := get_plugin(plugin_name)):
|
||||||
"\\", "/"
|
continue
|
||||||
) in event.src_path.replace("\\", "/"):
|
|
||||||
logger.debug(f"找到变动插件: {plugin.name},正在重新加载")
|
if (
|
||||||
reload_plugin(plugin)
|
plugin.module_path
|
||||||
context.reset_all()
|
and plugin.module_path.endswith("__init__.py")
|
||||||
break
|
and os.path.dirname(plugin.module_path).replace("\\", "/")
|
||||||
else:
|
in event.src_path.replace("\\", "/")
|
||||||
# 单文件插件
|
): # 包插件
|
||||||
if plugin.module_path == event.src_path:
|
logger.debug(f"找到变动插件: {plugin.name},正在重新加载")
|
||||||
logger.debug(f"找到变动插件: {plugin.name},正在重新加载")
|
reload_plugin(plugin)
|
||||||
reload_plugin(plugin)
|
context.reset_all()
|
||||||
context.reset_all()
|
break
|
||||||
break
|
|
||||||
else:
|
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
|
||||||
|
@ -22,6 +22,7 @@ from nonebot_plugin_alconna.uniseg import (
|
|||||||
Text,
|
Text,
|
||||||
UniMessage,
|
UniMessage,
|
||||||
UniMsg,
|
UniMsg,
|
||||||
|
get_message_id,
|
||||||
get_target,
|
get_target,
|
||||||
)
|
)
|
||||||
from nonebot_plugin_argot import Argot # type: ignore
|
from nonebot_plugin_argot import Argot # type: ignore
|
||||||
@ -57,7 +58,7 @@ class MarshoHandler:
|
|||||||
self.event: Event = current_event.get()
|
self.event: Event = current_event.get()
|
||||||
# self.state: T_State = current_handler.get().state
|
# self.state: T_State = current_handler.get().state
|
||||||
self.matcher: Matcher = current_matcher.get()
|
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)
|
self.target = get_target(self.event)
|
||||||
|
|
||||||
async def process_user_input(
|
async def process_user_input(
|
||||||
@ -124,7 +125,7 @@ class MarshoHandler:
|
|||||||
|
|
||||||
async def handle_function_call(
|
async def handle_function_call(
|
||||||
self,
|
self,
|
||||||
completion: Union[ChatCompletion],
|
completion: Union[ChatCompletion, AsyncStream[ChatCompletionChunk]],
|
||||||
user_message: Union[str, list],
|
user_message: Union[str, list],
|
||||||
model_name: str,
|
model_name: str,
|
||||||
tools_list: list | None = None,
|
tools_list: list | None = None,
|
||||||
@ -248,7 +249,7 @@ class MarshoHandler:
|
|||||||
Text(await process_completion_to_details(response)),
|
Text(await process_completion_to_details(response)),
|
||||||
command="detail",
|
command="detail",
|
||||||
expired_at=timedelta(minutes=5),
|
expired_at=timedelta(minutes=5),
|
||||||
)
|
) # type:ignore
|
||||||
)
|
)
|
||||||
# send_message.append(
|
# send_message.append(
|
||||||
# Argot(
|
# Argot(
|
||||||
|
@ -121,7 +121,7 @@ async def add_assistantmsg(target: MsgTarget, arg: Message = CommandArg()):
|
|||||||
@praises_cmd.handle()
|
@praises_cmd.handle()
|
||||||
async def praises():
|
async def praises():
|
||||||
# await UniMessage(await tools.call("marshoai-weather.get_weather", {"location":"杭州"})).send()
|
# 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()
|
@contexts_cmd.handle()
|
||||||
|
@ -1,9 +1,8 @@
|
|||||||
import importlib
|
import importlib
|
||||||
|
import importlib.util
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
# import importlib.util
|
|
||||||
import traceback
|
import traceback
|
||||||
|
|
||||||
from nonebot import logger
|
from nonebot import logger
|
||||||
@ -117,10 +116,12 @@ class MarshoTools:
|
|||||||
spec = importlib.util.spec_from_file_location(
|
spec = importlib.util.spec_from_file_location(
|
||||||
package_name, os.path.join(package_path, "__init__.py")
|
package_name, os.path.join(package_path, "__init__.py")
|
||||||
)
|
)
|
||||||
|
if not spec:
|
||||||
|
raise ImportError(f"工具包 {package_name} 未找到")
|
||||||
package = importlib.util.module_from_spec(spec)
|
package = importlib.util.module_from_spec(spec)
|
||||||
self.imported_packages[package_name] = package
|
self.imported_packages[package_name] = package
|
||||||
sys.modules[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}")
|
logger.success(f"成功加载工具包 {package_name}")
|
||||||
except json.JSONDecodeError as e:
|
except json.JSONDecodeError as e:
|
||||||
|
@ -29,7 +29,7 @@ def debounce(wait):
|
|||||||
def wrapper(*args, **kwargs):
|
def wrapper(*args, **kwargs):
|
||||||
nonlocal last_call_time
|
nonlocal last_call_time
|
||||||
current_time = time.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
|
last_call_time = current_time
|
||||||
return func(*args, **kwargs)
|
return func(*args, **kwargs)
|
||||||
|
|
||||||
@ -52,7 +52,7 @@ class CodeModifiedHandler(FileSystemEventHandler):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
@debounce(1)
|
@debounce(1)
|
||||||
def on_modified(self, event):
|
def on_modified(self, event: FileSystemEvent):
|
||||||
raise NotImplementedError("on_modified must be implemented")
|
raise NotImplementedError("on_modified must be implemented")
|
||||||
|
|
||||||
def on_created(self, event):
|
def on_created(self, event):
|
||||||
|
@ -24,9 +24,9 @@ async def process_chat_stream(
|
|||||||
delta = chunk.choices[0].delta
|
delta = chunk.choices[0].delta
|
||||||
if (
|
if (
|
||||||
hasattr(delta, "reasoning_content")
|
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:
|
else:
|
||||||
if not is_answering:
|
if not is_answering:
|
||||||
logger.info(
|
logger.info(
|
||||||
@ -72,6 +72,9 @@ async def process_chat_stream(
|
|||||||
|
|
||||||
|
|
||||||
async def process_completion_to_details(completion: ChatCompletion) -> str:
|
async def process_completion_to_details(completion: ChatCompletion) -> str:
|
||||||
|
if not isinstance(completion, ChatCompletion):
|
||||||
|
return "暂不支持对流式调用用量的获取,或预期之外的输入"
|
||||||
|
|
||||||
usage_text = ""
|
usage_text = ""
|
||||||
usage = completion.usage
|
usage = completion.usage
|
||||||
if usage is None:
|
if usage is None:
|
||||||
|
Reference in New Issue
Block a user