From 273006466c0efbc0000537c5ba91d49f8eb8d6e1 Mon Sep 17 00:00:00 2001 From: zhiyu1998 Date: Sun, 1 Dec 2024 15:06:54 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=A6=84=20refactor:=20=E7=A7=BB=E9=99=A4?= =?UTF-8?q?=E5=86=B7=E9=97=A8=E7=9A=84=20linux=20=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/query.js | 128 --------------------------------------------- constants/query.js | 29 ---------- 2 files changed, 157 deletions(-) delete mode 100644 constants/query.js diff --git a/apps/query.js b/apps/query.js index 67db42b..8041ba5 100644 --- a/apps/query.js +++ b/apps/query.js @@ -1,14 +1,7 @@ import axios from "axios"; -import _ from "lodash"; import fetch from "node-fetch"; // 常量 import { CAT_LIMIT, COMMON_USER_AGENT } from "../constants/constant.js"; -import { LINUX_AI_PROMPT, LINUX_QUERY, REDIS_YUNZAI_LINUX } from "../constants/query.js"; -// 配置文件 -import config from "../model/config.js"; -import { OpenaiBuilder } from "../utils/openai-builder.js"; -import { redisExistAndGetKey, redisExistAndInsertObject, redisExistAndUpdateObject } from "../utils/redis-util.js"; -import { textArrayToMakeForward } from "../utils/yunzai-util.js"; export class query extends plugin { @@ -42,23 +35,9 @@ export class query extends plugin { { reg: "^#竹白(.*)", fnc: "zhubaiSearch", - }, - { - reg: "^#(linux|Linux)(.*)", - fnc: "linuxQuery" } ], }); - // 配置文件 - this.toolsConfig = config.getConfig("tools"); - // 视频保存路径 - this.defaultPath = this.toolsConfig.defaultPath; - // ai接口 - this.aiBaseURL = this.toolsConfig.aiBaseURL; - // ai api key - this.aiApiKey = this.toolsConfig.aiApiKey; - // ai模型 - this.aiModel = this.toolsConfig.aiModel; } async doctor(e) { @@ -231,113 +210,6 @@ export class query extends plugin { return true; } - async linuxQuery(e) { - const order = e.msg.replace(/^#([lL])inux/, "").trim(); - // 查询 Redis 中是否存在这个命令如果存在直接返回没有的话就发起网络请求 - const linuxInRedis = await redisExistAndGetKey(REDIS_YUNZAI_LINUX) - let linuxOrderData; - // 判断这个命令是否在缓存里 - const isOrderInRedis = linuxInRedis && Object.keys(linuxInRedis).includes(order); - if (!isOrderInRedis) { - // 没有在缓存里,直接发起网络请求 - const resp = await fetch(LINUX_QUERY.replace("{}", order), { - headers: { - "User-Agent": COMMON_USER_AGENT - } - }); - linuxOrderData = (await resp.json()).data; - // 如果缓存里没有就保存一份到缓存里 - linuxOrderData && await redisExistAndInsertObject(REDIS_YUNZAI_LINUX, { [order]: linuxOrderData }); - } else { - // 在缓存里就取出 - linuxOrderData = linuxInRedis[order]; - } - try { - const builder = await new OpenaiBuilder() - .setBaseURL(this.aiBaseURL) - .setApiKey(this.aiApiKey) - .setModel(this.aiModel) - .setPrompt(LINUX_AI_PROMPT) - .build(); - let aiBuilder; - if (linuxOrderData) { - const { linux, content, link } = linuxOrderData; - // 发送消息 - e.reply(`识别:Linux命令 <${ linux }>\n\n功能:${ content }`); - aiBuilder = await builder.kimi(`能否帮助根据${ link }网站的Linux命令内容返回一些常见的用法,内容简洁明了即可`) - } else { - aiBuilder = await builder.kimi(`我现在需要一个Linux命令去完成:“${ order }”,你能否帮助我查询到相关的一些命令用法和示例,内容简洁明了即可`); - } - // 如果填了写 AI 才总结 - if (this.aiApiKey && this.aiBaseURL) { - const { ans: kimiAns, model } = aiBuilder; - const Msg = await Bot.makeForwardMsg(textArrayToMakeForward(e, [`「R插件 x ${ model }」联合为您总结内容:`, kimiAns])); - await e.reply(Msg); - // 提取AI返回的内容并进行解析 - if (_.isEmpty(linuxInRedis[order]?.content.trim())) { - const parsedData = this.parseAiResponse(order, kimiAns); - await redisExistAndUpdateObject(REDIS_YUNZAI_LINUX, order, parsedData); - e.reply(`已重新学习命令 ${ order } 的用法,当前已经更新功能为:${ parsedData.content }`); - } - } - } catch (err) { - e.reply(`暂时无法查询到当前命令!`); - logger.error(logger.red(`[R插件][linux]: ${ err }`)); - } - return true; - } - - /** - * AI响应解析函数 - * @param order - * @param aiResponse - * @returns {{linux, link: string, content: (*|string)}} - */ - parseAiResponse(order, aiResponse) { - // 检查 aiResponse 是否为有效字符串 - if (!aiResponse || typeof aiResponse !== 'string') { - return { - linux: order, - content: '', - link: `https://www.linuxcool.com/${ order }` // 默认参考链接 - }; - } - - // 初始化保存数据的对象 - let parsedData = { - linux: order, - content: '', - link: `https://www.linuxcool.com/${ order }` // 默认参考链接 - }; - - // 清理多余的换行符,避免意外的分隔问题 - const lines = aiResponse.split('\n').map(line => line.trim()).filter(line => line); - - // 遍历每一行查找命令相关的描述 - lines.forEach(line => { - // 允许命令带有可选的路径,修改正则表达式以适应路径变化 - const match = line.match(/[`'“](.+?)[`'”]\s*[::—-]?\s*(.*)/); - - if (match) { - logger.info(match) - const command = match[1].trim(); // 提取命令部分 - const description = match[2].trim(); // 提取描述部分// 同样忽略路径 - - // 如果命令和参数部分匹配,保存描述 - if (command.includes(order)) { - parsedData.content = description; - } - } - }); - - // 如果没有找到具体的描述内容,则给出默认提示 - if (!parsedData.content) { - parsedData.content = ''; - } - - return parsedData; - } - // 删除标签 removeTag(title) { const titleRex = /<[^>]+>/g; diff --git a/constants/query.js b/constants/query.js deleted file mode 100644 index d9cbc8c..0000000 --- a/constants/query.js +++ /dev/null @@ -1,29 +0,0 @@ -/** - * linux 命令查询 - * @type {string} - */ -export const LINUX_QUERY = "https://api.pearktrue.cn/api/linux/?keyword={}"; - -export const LINUX_AI_PROMPT = "- Role: Linux命令专家\n" + - "- Background: 用户需要从特定的Linux命令网站获取常见命令的用法,希望得到简洁明了的回复。\n" + - "- Profile: 你是一位对Linux命令有深入理解的专家,能够快速从网站中提取关键信息,并以简洁的方式呈现给用户。\n" + - "- Skills: 你具备快速阅读、信息提取、语言组织和表达的能力,能够将复杂的命令用法简化为易于理解的格式。\n" + - "- Goals: 提供一个精确、简洁的Linux命令用法列表,帮助用户快速掌握和使用。\n" + - "- Constrains: 确保回复内容准确无误,避免使用过于技术性的语言,确保用户易于理解。\n" + - "- OutputFormat: 列表形式,每条命令用法简洁明了,不超过两句话。\n" + - "- Workflow:\n" + - " 1. 访问并分析用户提供的网站链接。\n" + - " 2. 提取网站上的Linux命令及其常见用法。\n" + - " 3. 将提取的信息以简洁明了的方式组织成列表。\n" + - " 4. 返回格式为\"[命令]: [命令的解释],例如:ls -l: 列出目录所有内容。\n" + - "- Examples:\n" + - " - 例子1: 命令 'ls' — 列出目录内容。\n" + - " - 例子2: 命令 'cd' — 更改当前目录。\n" + - " - 例子3: 命令 'cp' — 复制文件或目录。\n" + - "- Initialization: 在第一次对话中,请直接输出以下:您好!我是Linux命令专家,我将为您提供简洁明了的Linux命令用法。"; - -/** - * Linux命令缓存 - * @type {string} - */ -export const REDIS_YUNZAI_LINUX = "Yz:rconsole:query:linux";