添加内置工具插件,支持获取已加载插件列表和读取本地文件内容功能

This commit is contained in:
2024-12-17 02:34:59 +08:00
parent d8ac06419b
commit 1e58944edc
10 changed files with 116 additions and 8 deletions

View File

@ -0,0 +1,12 @@
from nonebot_plugin_marshoai.plugin import PluginMetadata
from .file_io import *
from .manager import *
from .network import *
__marsho_meta__ = PluginMetadata(
name="内置增强组件",
version="0.0.1",
description="内置工具插件",
author="MarshoTeam of LiteyukiStudio",
)

View File

@ -0,0 +1,23 @@
import aiofiles # type: ignore
from nonebot.permission import SUPERUSER
from nonebot_plugin_marshoai.plugin import String, on_function_call
@on_function_call(description="获取设备上本地文件内容").params(
fp=String(description="文件路径")
).permission(SUPERUSER)
async def read_file(fp: str) -> str:
"""获取设备上本地文件内容
Args:
fp (str): 文件路径
Returns:
str: 文件内容
"""
try:
async with aiofiles.open(fp, "r", encoding="utf-8") as f:
return await f.read()
except Exception as e:
return "读取出错: " + str(e)

View File

@ -0,0 +1,18 @@
from nonebot_plugin_marshoai.plugin import get_plugins, on_function_call
@on_function_call(description="获取已加载的插件列表")
def get_marsho_plugins() -> str:
"""获取已加载的插件列表
Returns:
str: 插件列表
"""
reply = "加载的插件列表"
for p in get_plugins().values():
if p.metadata:
reply += f"名称: {p.metadata.name},描述: {p.metadata.description}\n"
else:
reply += f"名称: {p.name},描述: 暂无\n"
return reply

View File

@ -0,0 +1,48 @@
import time
from httpx import AsyncClient
from newspaper import Article
from nonebot import logger
from nonebot_plugin_marshoai.plugin.func_call.caller import on_function_call
from nonebot_plugin_marshoai.plugin.func_call.params import String
headers = {
"User-Agent": "Firefox/90.0 (Windows NT 10.0; Win64; x64; rv:90.0) Gecko/20100101 Firefox/90.0"
}
@on_function_call(
description="使用网页链接(url)获取网页内容摘要,可以让AI上网查询资料"
).params(
url=String(description="网页链接"),
typ=String(description="获取类型,摘要还是内容", enum=["摘要", "内容"]),
)
async def get_web_content(url: str, typ: str) -> str:
"""使用网页链接获取网页内容摘要
为什么要获取摘要不然token超限了
Args:
url (str): _description_
Returns:
str: _description_
"""
async with AsyncClient(headers=headers) as client:
try:
response = await client.get(url)
t1 = time.time()
article = Article(url)
article.set_html(response.text)
article.parse()
t2 = time.time()
logger.debug(f"获取网页内容耗时: {t2 - t1}")
if typ == "摘要":
return f"标题: {article.title}\n作者: {article.authors}\n发布日期: {article.publish_date}"
elif typ == "内容":
return f"标题: {article.title}\n作者: {article.authors}\n发布日期: {article.publish_date}\n摘要: {article.summary}\n正文: {article.text}"
except Exception as e:
logger.error(f"marsho builtin: 获取网页内容失败: {e}")
return "获取网页内容失败:" + str(e)
return "未能获取到有效的网页内容"