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,7 +1,77 @@
|
||||
from nonebot import 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
|
||||
|
||||
require("nonebot_plugin_alconna")
|
||||
|
||||
from nonebot_plugin_alconna import Alconna, on_alconna
|
||||
from nonebot.permission import SUPERUSER
|
||||
from nonebot_plugin_alconna import (
|
||||
Alconna,
|
||||
Args,
|
||||
MultiVar,
|
||||
Subcommand,
|
||||
UniMessage,
|
||||
on_alconna,
|
||||
)
|
||||
|
||||
function_call = on_alconna("marshocall")
|
||||
from .plugin.func_call.caller import get_function_calls
|
||||
|
||||
function_call = on_alconna(
|
||||
command=Alconna(
|
||||
"marsho-function-call",
|
||||
Subcommand(
|
||||
"call",
|
||||
Args["function_name", str]["kwargs", MultiVar(str), []],
|
||||
alias={"c"},
|
||||
),
|
||||
Subcommand(
|
||||
"list",
|
||||
alias={"l"},
|
||||
),
|
||||
Subcommand("info", Args["function_name", str], alias={"i"}),
|
||||
),
|
||||
aliases={"mfc"},
|
||||
permission=SUPERUSER,
|
||||
)
|
||||
|
||||
|
||||
@function_call.assign("list")
|
||||
async def list_functions():
|
||||
reply = "共有如下可调用函数:\n"
|
||||
for function in get_function_calls().values():
|
||||
reply += f"- {function.name}({function.description}))\n"
|
||||
await UniMessage(reply).send()
|
||||
|
||||
|
||||
@function_call.assign("info")
|
||||
async def function_info(function_name: str):
|
||||
function = get_function_calls().get(function_name)
|
||||
if function is None:
|
||||
await UniMessage(f"未找到函数 {function_name}").send()
|
||||
return
|
||||
await UniMessage(str(function)).send()
|
||||
|
||||
|
||||
@function_call.assign("call")
|
||||
async def call_function(
|
||||
function_name: str,
|
||||
kwargs: list[str],
|
||||
event: Event,
|
||||
bot: Bot,
|
||||
matcher: Matcher,
|
||||
state: T_State,
|
||||
):
|
||||
function = get_function_calls().get(function_name)
|
||||
if function is None:
|
||||
await UniMessage(f"未找到函数 {function_name}").send()
|
||||
return
|
||||
await UniMessage(
|
||||
str(
|
||||
await function.with_ctx(
|
||||
SessionContext(event=event, bot=bot, matcher=matcher, state=state)
|
||||
).call(**{i.split("=", 1)[0]: i.split("=", 1)[1] for i in kwargs})
|
||||
)
|
||||
).send()
|
||||
|
Reference in New Issue
Block a user