🦄 refactor: 优化 OpenaiBuilder 类和工具模块代码结构

- 移除 utils/openai-builder.js 中不再使用的 import 语句和方法
- 简化 apps/tools.js 中的下载队列逻辑,直接执行下载任务
- 移除 package.json 中不再使用的依赖项
- 确保代码清晰和功能正常运行,提升代码可维护性
- 优化哔哩哔哩视频下载流程,提高下载效率和稳定性
- 修复潜在的文件路径问题,确保文件操作安全有效
- 移除冗余的上传至小飞机逻辑,简化代码结构
- 优化视频预览功能,提升用户体验和操作便捷性
This commit is contained in:
zhiyu1998 2024-09-19 21:42:45 +08:00
parent b114084276
commit 75d10fbd45
4 changed files with 76 additions and 147 deletions

View File

@ -96,7 +96,7 @@ import { deepSeekChat, llmRead } from "../utils/llm-util.js";
import { getDS } from "../utils/mihoyo.js";
import { OpenaiBuilder } from "../utils/openai-builder.js";
import { redisExistKey, redisGetKey, redisSetKey } from "../utils/redis-util.js";
import { saveTDL, startTDL, uploadTDL } from "../utils/tdl-util.js";
import { saveTDL, startTDL } from "../utils/tdl-util.js";
import Translate from "../utils/trans-strategy.js";
import { mid2id } from "../utils/weibo.js";
import { ytDlpGetTilt, ytDlpHelper } from "../utils/yt-dlp-util.js";
@ -394,11 +394,9 @@ export class tools extends plugin {
// logger.info(resUrl);
const path = `${ this.getCurDownloadPath(e) }/temp.mp4`;
// 加入队列
this.queue.add(async () => {
await this.downloadVideo(resUrl).then(() => {
this.sendVideoToUpload(e, path)
});
})
} else if (urlType === "image") {
// 发送描述
e.reply(`${ this.identifyPrefix } 识别:抖音, ${ item.desc }`);
@ -575,11 +573,8 @@ export class tools extends plugin {
const ep = await this.biliEpInfo(url, e);
// 如果使用了BBDown && 没有填写session 就放开下载
if (this.biliUseBBDown) {
// 加入队列
this.queue.add(async () => {
// 下载文件
await this.biliDownloadStrategy(e, `https://www.bilibili.com/bangumi/play/ep${ ep }`, path);
})
}
return true;
}
@ -613,11 +608,8 @@ export class tools extends plugin {
if (e.msg !== undefined && e.msg.startsWith("音乐")) {
return await this.biliMusic(e, url);
}
// 加入队列
this.queue.add(async () => {
// 下载文件
await this.biliDownloadStrategy(e, url, path);
})
return true;
}
@ -721,6 +713,7 @@ export class tools extends plugin {
* @returns {Promise<void>}
*/
async biliDownloadStrategy(e, url, path) {
return this.queue.add(async () => {
// =================以下是调用BBDown的逻辑=====================
// 下载视频和音频
const tempPath = `${ path }temp`;
@ -758,6 +751,7 @@ export class tools extends plugin {
logger.error('[R插件][哔哩哔哩视频发送]下载错误,具体原因为:', err);
e.reply("解析失败,请重试一下");
}
})
}
/**
@ -1928,10 +1922,8 @@ export class tools extends plugin {
// 处理视频
if (link) {
this.queue.add(async () => {
const filePath = await this.downloadVideo(link);
this.sendVideoToUpload(e, `${ filePath }/temp.mp4`);
});
}
}
}
@ -2100,7 +2092,7 @@ export class tools extends plugin {
* @param isProxy
* @param headers
* @param numThreads
* @returns {Promise<void>}
* @returns {Promise<string>}
*/
async downloadVideo(url, isProxy = false, headers = null, numThreads = this.videoDownloadConcurrency) {
// 构造群信息参数
@ -2129,17 +2121,19 @@ export class tools extends plugin {
target,
groupPath,
}
logger.info(`[R插件][视频下载]:当前队列长度为 ${ this.queue.size + 1 }`);
return await this.queue.add(async () => {
// 如果是用户设置了单线程,则不分片下载
if (numThreads === 1) {
return await this.downloadVideoWithSingleThread(downloadVideoParams);
await this.downloadVideoWithSingleThread(downloadVideoParams);
} else if (numThreads !== 1 && this.biliDownloadMethod === 1) {
return await this.downloadVideoWithAria2(downloadVideoParams, numThreads);
await this.downloadVideoWithAria2(downloadVideoParams, numThreads);
} else if (numThreads !== 1 && this.biliDownloadMethod === 2) {
return await this.downloadVideoUseAxel(downloadVideoParams, numThreads);
await this.downloadVideoUseAxel(downloadVideoParams, numThreads);
} else {
return await this.downloadVideoWithMultiThread(downloadVideoParams, numThreads);
await this.downloadVideoWithMultiThread(downloadVideoParams, numThreads);
}
});
}
/**
@ -2468,14 +2462,8 @@ export class tools extends plugin {
}
const stats = fs.statSync(path);
const videoSize = Math.floor(stats.size / (1024 * 1024));
// 顺便发送一份到小飞机
if (e.msg.startsWith("上传飞机")) {
this.queue.add(async () => {
await uploadTDL(path, this.isOverseasServer(), this.proxyAddr);
e.reply("✈️ 已发送一份到您的小飞机收藏夹了!");
})
} else if (e.msg.startsWith("预览视频")) {
// 预览视频逻辑
if (e.msg.startsWith("预览视频")) {
const keyframesPath = this.getCurDownloadPath(e) + "keyframes";
await mkdirIfNotExists(keyframesPath);
await extractKeyframes(path, keyframesPath);

View File

@ -1,11 +1,11 @@
- {
version: 1.8.2,
version: 1.9.0,
data:
[
优化<span class="cmd">队列下载和GPT</span>功能,
新增<span class="cmd">哔哩哔哩下载分辨率设置</span>功能,
新增<span class="cmd">自定义识别</span>功能,
修正<span class="cmd">油管分辨率降低到720P</span>功能,
新增<span class="cmd">小飞机解析 Beta</span>功能,
支持<span class="cmd">锅巴</span>插件,方便查看和修改配置,
输入<span class="cmd">#R帮助</span>获取插件帮助,
输入<span class="cmd">#R更新</span>更新插件,

View File

@ -6,7 +6,6 @@
"axios": "^1.3.4",
"qrcode": "^1.5.3",
"p-queue": "^8.0.1",
"ws": "^8.17.0",
"openai": "^4.47.1"
"ws": "^8.17.0"
}
}

View File

@ -1,8 +1,4 @@
import { toBase64 } from "./file.js";
// openai库
import OpenAI from 'openai';
// fs
import fs from "node:fs";
import axios from "axios";
export class OpenaiBuilder {
constructor() {
@ -14,7 +10,7 @@ export class OpenaiBuilder {
}
setBaseURL(baseURL) {
this.baseURL = baseURL + "/v1";
this.baseURL = baseURL;
return this;
}
@ -41,16 +37,20 @@ export class OpenaiBuilder {
async build() {
// logger.info(this.baseURL, this.apiKey)
// 创建客户端
this.client = new OpenAI({
this.client = axios.create({
baseURL: this.baseURL,
apiKey: this.apiKey
timeout: 10000,
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer " + this.apiKey
}
});
return this;
}
async kimi(query) {
// 请求Kimi
const completion = await this.client.chat.completions.create({
const completion = await this.client.post("/v1/chat/completions", {
model: "moonshot-v1-8k",
messages: [
{
@ -68,62 +68,4 @@ export class OpenaiBuilder {
"ans": completion.choices[0].message.content
}
}
async kimi_pic(path) {
let file_object = await this.client.files.create({
file: fs.createReadStream(path),
purpose: "file-extract"
})
let file_content = await (await this.client.files.content(file_object.id)).text()
// 请求Kimi
const completion = await this.client.chat.completions.create({
model: "moonshot-v1-8k",
messages: [
{
"role": "system",
"content": file_content,
},
{
role: "user",
content: this.prompt
},
],
});
return {
"model": "月之暗面 Kimi",
"ans": completion.choices[0].message.content
}
}
async openai_pic(path) {
// 转换base64
const pic = await toBase64(path);
const completion = await this.client.chat.completions.create({
model: this.model,
messages: [
{
role: "user",
content: [
{
type: "image_url",
image_url: {
url: pic,
},
},
{
type: "text",
text: this.prompt,
},
],
},
],
use_search: false,
});
return {
"model": "OpenAI",
"ans": completion.choices[0].message.content
}
}
}