mirror of
https://github.com/LiteyukiStudio/nonebot-plugin-marshoai.git
synced 2025-08-01 03:59:51 +00:00
✨ 支持开发热重载插件,支持独立测试函数
This commit is contained in:
@ -1,9 +1,16 @@
|
||||
from nonebot import require
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
from nonebot import get_driver, logger, require
|
||||
from nonebot.adapters import Bot, Event
|
||||
from nonebot.matcher import Matcher
|
||||
from nonebot.typing import T_State
|
||||
|
||||
from nonebot_plugin_marshoai.plugin.func_call.models import SessionContext
|
||||
from nonebot_plugin_marshoai.plugin.load import reload_plugin
|
||||
|
||||
from .azure import context
|
||||
from .config import config
|
||||
from .plugin.func_call.models import SessionContext
|
||||
|
||||
require("nonebot_plugin_alconna")
|
||||
|
||||
@ -17,8 +24,12 @@ from nonebot_plugin_alconna import (
|
||||
on_alconna,
|
||||
)
|
||||
|
||||
from .observer import *
|
||||
from .plugin import get_plugin, get_plugins
|
||||
from .plugin.func_call.caller import get_function_calls
|
||||
|
||||
driver = get_driver()
|
||||
|
||||
function_call = on_alconna(
|
||||
command=Alconna(
|
||||
"marsho-function-call",
|
||||
@ -66,8 +77,13 @@ async def call_function(
|
||||
):
|
||||
function = get_function_calls().get(function_name)
|
||||
if function is None:
|
||||
await UniMessage(f"未找到函数 {function_name}").send()
|
||||
return
|
||||
for f in get_function_calls().values():
|
||||
if f.short_name == function_name:
|
||||
function = f
|
||||
break
|
||||
else:
|
||||
await UniMessage(f"未找到函数 {function_name}").send()
|
||||
return
|
||||
await UniMessage(
|
||||
str(
|
||||
await function.with_ctx(
|
||||
@ -75,3 +91,37 @@ async def call_function(
|
||||
).call(**{i.split("=", 1)[0]: i.split("=", 1)[1] for i in kwargs})
|
||||
)
|
||||
).send()
|
||||
|
||||
|
||||
@on_file_system_event(
|
||||
(str(Path(__file__).parent / "plugins"), *config.marshoai_plugin_dirs),
|
||||
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
|
||||
else:
|
||||
logger.debug("未找到变动插件")
|
||||
return
|
||||
|
Reference in New Issue
Block a user