This commit is contained in:
vastxie
2025-03-04 17:36:53 +08:00
parent cf7dc1d1e7
commit 1e9b9f7ba4
649 changed files with 23183 additions and 1925 deletions

View File

@@ -0,0 +1,44 @@
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var AiPptService_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.AiPptService = void 0;
const common_1 = require("@nestjs/common");
const chatLog_service_1 = require("../chatLog/chatLog.service");
const globalConfig_service_1 = require("../globalConfig/globalConfig.service");
const models_service_1 = require("../models/models.service");
const upload_service_1 = require("../upload/upload.service");
const openaiChat_service_1 = require("./openaiChat.service");
let AiPptService = AiPptService_1 = class AiPptService {
constructor(uploadService, globalConfigService, chatLogService, openAIChatService, modelsService) {
this.uploadService = uploadService;
this.globalConfigService = globalConfigService;
this.chatLogService = chatLogService;
this.openAIChatService = openAIChatService;
this.modelsService = modelsService;
this.logger = new common_1.Logger(AiPptService_1.name);
}
async aiPPT(inputs) {
return;
}
async pptCover(inputs) {
return;
}
};
AiPptService = AiPptService_1 = __decorate([
(0, common_1.Injectable)(),
__metadata("design:paramtypes", [upload_service_1.UploadService,
globalConfig_service_1.GlobalConfigService,
chatLog_service_1.ChatLogService,
openaiChat_service_1.OpenAIChatService,
models_service_1.ModelsService])
], AiPptService);
exports.AiPptService = AiPptService;

View File

@@ -0,0 +1,214 @@
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CogVideoService = void 0;
const common_1 = require("@nestjs/common");
const axios_1 = require("axios");
const chatLog_service_1 = require("../chatLog/chatLog.service");
const globalConfig_service_1 = require("../globalConfig/globalConfig.service");
const upload_service_1 = require("../upload/upload.service");
let CogVideoService = class CogVideoService {
constructor(chatLogService, globalConfigService, uploadService) {
this.chatLogService = chatLogService;
this.globalConfigService = globalConfigService;
this.uploadService = uploadService;
}
async cogVideo(inputs) {
var _a, _b, _c;
const { apiKey, proxyUrl, fileInfo, prompt, timeout, assistantLogId } = inputs;
let result = {
text: '',
fileInfo: '',
taskId: '',
taskData: '',
status: 2,
};
let response = null;
let url = '';
let payloadJson = {};
const headers = { Authorization: `Bearer ${apiKey}` };
url = `${proxyUrl}/cogvideox/v4/videos/generations`;
payloadJson = {
model: 'cogvideox',
prompt: prompt,
};
if (fileInfo) {
payloadJson['image_url'] = fileInfo;
}
common_1.Logger.log(`正在准备发送请求到 ${url}payload: ${JSON.stringify(payloadJson)}, headers: ${JSON.stringify(headers)}`, 'CogService');
try {
response = await axios_1.default.post(url, payloadJson, { headers });
common_1.Logger.debug(`任务提交结果,状态码: ${response.status}, 状态消息: ${response.statusText}, 数据: ${JSON.stringify(response.data)}`);
}
catch (error) {
common_1.Logger.error(`任务提交失败: ${error.message}`, 'CogService');
throw new Error('任务提交失败');
}
if ((_a = response === null || response === void 0 ? void 0 : response.data) === null || _a === void 0 ? void 0 : _a.id) {
result.taskId = (_b = response === null || response === void 0 ? void 0 : response.data) === null || _b === void 0 ? void 0 : _b.id;
common_1.Logger.log(`任务提交成功, 任务ID: ${(_c = response === null || response === void 0 ? void 0 : response.data) === null || _c === void 0 ? void 0 : _c.id}`, 'CogService');
}
else {
throw new Error('未能获取结果数据, 即将重试');
}
try {
await this.pollCogVideoResult({
proxyUrl,
apiKey,
taskId: response.data.id,
timeout,
prompt,
onSuccess: async (data) => {
try {
await this.chatLogService.updateChatLog(assistantLogId, {
videoUrl: data === null || data === void 0 ? void 0 : data.videoUrl,
audioUrl: data === null || data === void 0 ? void 0 : data.audioUrl,
fileInfo: data === null || data === void 0 ? void 0 : data.fileInfo,
answer: (data === null || data === void 0 ? void 0 : data.answer) || prompt,
progress: '100%',
status: 3,
taskId: data === null || data === void 0 ? void 0 : data.taskId,
taskData: data === null || data === void 0 ? void 0 : data.taskData,
});
common_1.Logger.log('视频任务已完成', 'CogService');
}
catch (error) {
common_1.Logger.error(`更新日志失败: ${error.message}`, 'CogService');
}
},
onGenerating: async (data) => {
try {
await this.chatLogService.updateChatLog(assistantLogId, {
videoUrl: data === null || data === void 0 ? void 0 : data.videoUrl,
audioUrl: data === null || data === void 0 ? void 0 : data.audioUrl,
fileInfo: data === null || data === void 0 ? void 0 : data.fileInfo,
answer: (data === null || data === void 0 ? void 0 : data.answer) || '视频生成中...',
progress: data === null || data === void 0 ? void 0 : data.progress,
status: data.status,
});
common_1.Logger.log('视频生成中...', 'CogService');
}
catch (error) {
common_1.Logger.error(`更新日志失败: ${error.message}`, 'CogService');
}
},
onFailure: async (data) => {
try {
await this.chatLogService.updateChatLog(assistantLogId, {
answer: '视频生成失败',
status: data.status,
});
common_1.Logger.log('生成失败', 'Lum aService');
}
catch (error) {
common_1.Logger.error(`更新日志失败: ${error.message}`, 'CogService');
}
},
});
}
catch (error) {
common_1.Logger.error('查询生成结果时发生错误:', error.message, 'CogService');
throw new Error('查询生成结果时发生错误');
}
return result;
}
async pollCogVideoResult(inputs) {
const { proxyUrl, apiKey, taskId, timeout, onSuccess, onFailure, onGenerating, prompt, action, } = inputs;
let result = {
videoUrl: '',
audioUrl: '',
fileInfo: '',
drawId: '',
taskData: '',
status: 2,
progress: 0,
answer: '',
};
const headers = { Authorization: `Bearer ${apiKey}` };
const url = `${proxyUrl}/cogvideox/v4/async-result/${taskId}`;
const startTime = Date.now();
const totalDuration = 300000;
const POLL_INTERVAL = 5000;
let retryCount = 0;
try {
while (Date.now() - startTime < timeout) {
await new Promise((resolve) => setTimeout(resolve, POLL_INTERVAL));
try {
const res = await axios_1.default.get(url, { headers });
const interval = setInterval(() => {
const elapsed = Date.now() - startTime;
let percentage = Math.floor((elapsed / totalDuration) * 100);
if (percentage >= 99)
percentage = 99;
result.answer = `视频生成中 ${percentage}%`;
}, 1000);
const responses = res.data;
common_1.Logger.debug(`轮询结果: ${JSON.stringify(responses)}`, 'CogService');
if (responses.task_status === 'SUCCESS') {
result.taskId = responses.request_id;
result.taskData = JSON.stringify(responses);
common_1.Logger.log('视频生成成功', 'CogService');
result.fileInfo = responses.video_result[0].url;
common_1.Logger.log(result.fileInfo, 'CogService');
try {
const localStorageStatus = await this.globalConfigService.getConfigs([
'localStorageStatus',
]);
if (Number(localStorageStatus)) {
const now = new Date();
const year = now.getFullYear();
const month = String(now.getMonth() + 1).padStart(2, '0');
const day = String(now.getDate()).padStart(2, '0');
const currentDate = `${year}${month}/${day}`;
result.fileInfo = await this.uploadService.uploadFileFromUrl({
url: responses.video_result[0].url,
dir: `video/cog/${currentDate}`,
});
}
}
catch (error) {
common_1.Logger.error(`上传文件失败: ${error.message}`, 'CogService');
}
result.answer = `提示词: "${prompt}"`;
onSuccess(result);
clearInterval(interval);
return;
}
else {
onGenerating(result);
}
if (result.progress) {
}
}
catch (error) {
retryCount++;
common_1.Logger.error(`轮询失败,重试次数: ${retryCount}`, 'CogService');
}
}
common_1.Logger.error('轮询超时,请稍后再试!', 'CogService');
result.status = 4;
onFailure(result);
throw new Error('查询超时,请稍后再试!');
}
catch (error) {
common_1.Logger.error(`轮询过程中发生错误: ${error}`, 'CogService');
result.status = 5;
onFailure(result);
}
}
};
CogVideoService = __decorate([
(0, common_1.Injectable)(),
__metadata("design:paramtypes", [chatLog_service_1.ChatLogService,
globalConfig_service_1.GlobalConfigService,
upload_service_1.UploadService])
], CogVideoService);
exports.CogVideoService = CogVideoService;

View File

@@ -0,0 +1,148 @@
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var FluxDrawService_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.FluxDrawService = void 0;
const common_1 = require("@nestjs/common");
const axios_1 = require("axios");
const chatLog_service_1 = require("../chatLog/chatLog.service");
const globalConfig_service_1 = require("../globalConfig/globalConfig.service");
const upload_service_1 = require("../upload/upload.service");
const openaiChat_service_1 = require("./openaiChat.service");
let FluxDrawService = FluxDrawService_1 = class FluxDrawService {
constructor(uploadService, globalConfigService, chatLogService, openAIChatService) {
this.uploadService = uploadService;
this.globalConfigService = globalConfigService;
this.chatLogService = chatLogService;
this.openAIChatService = openAIChatService;
this.logger = new common_1.Logger(FluxDrawService_1.name);
}
async fluxDraw(inputs, buildMessageFromParentMessageId) {
var _a, _b, _c, _d;
common_1.Logger.log('开始提交 Flux 绘图任务 ', 'DrawService');
const { apiKey, model, proxyUrl, prompt, extraParam, timeout, onSuccess, onFailure, groupId, } = inputs;
const isDalleChat = await this.globalConfigService.getConfigs([
'isDalleChat',
]);
let drawPrompt;
if (isDalleChat === '1') {
try {
common_1.Logger.log('已开启连续绘画模式', 'FluxDraw');
const { messagesHistory } = await buildMessageFromParentMessageId(`参考上文,结合我的需求,给出绘画描述。我的需求是:${prompt}`, {
groupId,
systemMessage: '你是一个绘画提示词生成工具,请根据用户的要求,结合上下文,用一段文字,描述用户需要的绘画需求,不用包含任何礼貌性的寒暄,只需要场景的描述,可以适当联想',
maxModelTokens: 8000,
maxRounds: 5,
fileInfo: '',
}, this.chatLogService);
drawPrompt = await this.openAIChatService.chatFree(prompt, undefined, messagesHistory);
}
catch (error) {
console.error('调用chatFree失败', error);
drawPrompt = prompt;
}
}
else {
drawPrompt = prompt;
}
const size = (extraParam === null || extraParam === void 0 ? void 0 : extraParam.size) || '1024x1024';
let result = { answer: '', fileInfo: '', status: 2 };
try {
const options = {
method: 'POST',
url: `${proxyUrl}/v1/images/generations`,
timeout: timeout,
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${apiKey}`,
},
data: {
model: model,
prompt: drawPrompt,
size,
},
};
common_1.Logger.log(`正在准备发送请求到 ${options.url}payload: ${JSON.stringify(options.data)}, headers: ${JSON.stringify(options.headers)}`, 'FluxDrawService');
const response = await (0, axios_1.default)(options);
common_1.Logger.debug(`请求成功${JSON.stringify(response.data.data[0])}`);
common_1.Logger.debug(`请求状态${JSON.stringify(response.status)}`);
const url = response.data.data[0].url;
try {
common_1.Logger.log(`------> 开始上传图片!!!`, 'DrawService');
const now = new Date();
const year = now.getFullYear();
const month = String(now.getMonth() + 1).padStart(2, '0');
const day = String(now.getDate()).padStart(2, '0');
const currentDate = `${year}${month}/${day}`;
result.fileInfo = await this.uploadService.uploadFileFromUrl({
url: url,
dir: `images/dalle/${currentDate}`,
});
common_1.Logger.log(`图片上传成功URL: ${result.fileInfo}`, 'DrawService');
}
catch (error) {
common_1.Logger.error(`上传图片过程中出现错误: ${error}`, 'DrawService');
}
let revised_prompt_cn;
try {
revised_prompt_cn = await this.openAIChatService.chatFree(`根据提示词{${drawPrompt}}, 模拟AI绘画机器人的语气用中文回复告诉用户已经画好了`);
}
catch (error) {
revised_prompt_cn = `${prompt} 绘制成功`;
common_1.Logger.error('翻译失败: ', error);
}
result.answer = revised_prompt_cn;
result.status = 3;
onSuccess(result);
return;
}
catch (error) {
result.status = 5;
onFailure(result);
const status = ((_a = error === null || error === void 0 ? void 0 : error.response) === null || _a === void 0 ? void 0 : _a.status) || 500;
console.log('draw error: ', JSON.stringify(error), status);
const message = (_d = (_c = (_b = error === null || error === void 0 ? void 0 : error.response) === null || _b === void 0 ? void 0 : _b.data) === null || _c === void 0 ? void 0 : _c.error) === null || _d === void 0 ? void 0 : _d.message;
if (status === 429) {
result.text = '当前请求已过载、请稍等会儿再试试吧!';
return result;
}
if (status === 400 &&
message.includes('This request has been blocked by our content filters')) {
result.text = '您的请求已被系统拒绝。您的提示可能存在一些非法的文本。';
return result;
}
if (status === 400 &&
message.includes('Billing hard limit has been reached')) {
result.text =
'当前模型key已被封禁、已冻结当前调用Key、尝试重新对话试试吧';
return result;
}
if (status === 500) {
result.text = '绘制图片失败,请检查你的提示词是否有非法描述!';
return result;
}
if (status === 401) {
result.text = '绘制图片失败,此次绘画被拒绝了!';
return result;
}
result.text = '绘制图片失败,请稍后试试吧!';
return result;
}
}
};
FluxDrawService = FluxDrawService_1 = __decorate([
(0, common_1.Injectable)(),
__metadata("design:paramtypes", [upload_service_1.UploadService,
globalConfig_service_1.GlobalConfigService,
chatLog_service_1.ChatLogService,
openaiChat_service_1.OpenAIChatService])
], FluxDrawService);
exports.FluxDrawService = FluxDrawService;

View File

@@ -0,0 +1,213 @@
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.LumaVideoService = void 0;
const common_1 = require("@nestjs/common");
const axios_1 = require("axios");
const chatLog_service_1 = require("../chatLog/chatLog.service");
const globalConfig_service_1 = require("../globalConfig/globalConfig.service");
const upload_service_1 = require("../upload/upload.service");
let LumaVideoService = class LumaVideoService {
constructor(chatLogService, globalConfigService, uploadService) {
this.chatLogService = chatLogService;
this.globalConfigService = globalConfigService;
this.uploadService = uploadService;
}
async lumaVideo(inputs) {
var _a, _b, _c;
const { apiKey, proxyUrl, fileInfo, prompt, timeout, assistantLogId, extraParam, } = inputs;
let result = {
text: '',
fileInfo: '',
taskId: '',
taskData: '',
status: 2,
};
let response = null;
let url = '';
let payloadJson = {};
const headers = { Authorization: `Bearer ${apiKey}` };
url = `${proxyUrl}/luma/generations/`;
const aspectRatio = extraParam.size || '16:9';
payloadJson = {
user_prompt: prompt,
aspect_ratio: aspectRatio,
expand_prompt: true,
};
if (fileInfo) {
payloadJson['image_url'] = fileInfo;
}
common_1.Logger.log(`正在准备发送请求到 ${url}payload: ${JSON.stringify(payloadJson)}, headers: ${JSON.stringify(headers)}`, 'LumaService');
try {
response = await axios_1.default.post(url, payloadJson, { headers });
}
catch (error) {
common_1.Logger.error(`任务提交失败: ${error.message}`, 'LumaService');
throw new Error('任务提交失败');
}
if ((_a = response === null || response === void 0 ? void 0 : response.data) === null || _a === void 0 ? void 0 : _a.id) {
result.taskId = (_b = response === null || response === void 0 ? void 0 : response.data) === null || _b === void 0 ? void 0 : _b.id;
common_1.Logger.log(`任务提交成功, 任务ID: ${(_c = response === null || response === void 0 ? void 0 : response.data) === null || _c === void 0 ? void 0 : _c.id}`, 'LumaService');
}
else {
throw new Error('未能获取结果数据, 即将重试');
}
try {
await this.pollLumaVideoResult({
proxyUrl,
apiKey,
taskId: response.data.id,
timeout,
prompt,
onSuccess: async (data) => {
try {
await this.chatLogService.updateChatLog(assistantLogId, {
videoUrl: data === null || data === void 0 ? void 0 : data.videoUrl,
audioUrl: data === null || data === void 0 ? void 0 : data.audioUrl,
fileInfo: data === null || data === void 0 ? void 0 : data.fileInfo,
answer: (data === null || data === void 0 ? void 0 : data.answer) || prompt,
progress: '100%',
status: 3,
taskId: data === null || data === void 0 ? void 0 : data.taskId,
taskData: data === null || data === void 0 ? void 0 : data.taskData,
});
common_1.Logger.log('视频任务已完成', 'LumaService');
}
catch (error) {
common_1.Logger.error(`更新日志失败: ${error.message}`, 'LumaService');
}
},
onGenerating: async (data) => {
try {
await this.chatLogService.updateChatLog(assistantLogId, {
videoUrl: data === null || data === void 0 ? void 0 : data.videoUrl,
audioUrl: data === null || data === void 0 ? void 0 : data.audioUrl,
fileInfo: data === null || data === void 0 ? void 0 : data.fileInfo,
answer: (data === null || data === void 0 ? void 0 : data.answer) || '视频生成中...',
progress: data === null || data === void 0 ? void 0 : data.progress,
status: data.status,
});
common_1.Logger.log('视频生成中...', 'LumaService');
}
catch (error) {
common_1.Logger.error(`更新日志失败: ${error.message}`, 'LumaService');
}
},
onFailure: async (data) => {
try {
await this.chatLogService.updateChatLog(assistantLogId, {
answer: '视频生成失败',
status: data.status,
});
common_1.Logger.log('生成失败', 'Lum aService');
}
catch (error) {
common_1.Logger.error(`更新日志失败: ${error.message}`, 'LumaService');
}
},
});
}
catch (error) {
common_1.Logger.error('查询生成结果时发生错误:', error.message, 'LumaService');
throw new Error('查询生成结果时发生错误');
}
return result;
}
async pollLumaVideoResult(inputs) {
const { proxyUrl, apiKey, taskId, timeout, onSuccess, onFailure, onGenerating, action, } = inputs;
let result = {
videoUrl: '',
audioUrl: '',
fileInfo: '',
drawId: '',
taskData: '',
status: 2,
progress: 0,
answer: '',
};
const headers = { Authorization: `Bearer ${apiKey}` };
const url = `${proxyUrl}/luma/generations/${taskId}`;
const startTime = Date.now();
const totalDuration = 300000;
const POLL_INTERVAL = 5000;
let retryCount = 0;
try {
while (Date.now() - startTime < timeout) {
await new Promise((resolve) => setTimeout(resolve, POLL_INTERVAL));
try {
const res = await axios_1.default.get(url, { headers });
const interval = setInterval(() => {
const elapsed = Date.now() - startTime;
let percentage = Math.floor((elapsed / totalDuration) * 100);
if (percentage >= 99)
percentage = 99;
result.answer = `视频生成中 ${percentage}%`;
}, 1000);
const responses = res.data;
common_1.Logger.debug(`轮询结果: ${JSON.stringify(responses)}`, 'LumaService');
if (responses.state === 'completed') {
result.taskId = responses.id;
result.taskData = JSON.stringify(responses);
result.fileInfo = responses.video.url;
try {
const localStorageStatus = await this.globalConfigService.getConfigs([
'localStorageStatus',
]);
if (Number(localStorageStatus)) {
const now = new Date();
const year = now.getFullYear();
const month = String(now.getMonth() + 1).padStart(2, '0');
const day = String(now.getDate()).padStart(2, '0');
const currentDate = `${year}${month}/${day}`;
result.fileInfo = await this.uploadService.uploadFileFromUrl({
url: responses.video.download_url,
dir: `video/luma/${currentDate}`,
});
}
}
catch (error) {
common_1.Logger.error(`上传文件失败: ${error.message}`, 'LumaService');
}
result.answer = `提示词: "${responses.prompt}"`;
onSuccess(result);
clearInterval(interval);
return;
}
else {
onGenerating(result);
}
if (result.progress) {
}
}
catch (error) {
retryCount++;
common_1.Logger.error(`轮询失败,重试次数: ${retryCount}`, 'LumaService');
}
}
common_1.Logger.error('轮询超时,请稍后再试!', 'LumaService');
result.status = 4;
onFailure(result);
throw new Error('查询超时,请稍后再试!');
}
catch (error) {
common_1.Logger.error(`轮询过程中发生错误: ${error}`, 'LumaService');
result.status = 5;
onFailure(result);
}
}
};
LumaVideoService = __decorate([
(0, common_1.Injectable)(),
__metadata("design:paramtypes", [chatLog_service_1.ChatLogService,
globalConfig_service_1.GlobalConfigService,
upload_service_1.UploadService])
], LumaVideoService);
exports.LumaVideoService = LumaVideoService;

View File

@@ -0,0 +1,237 @@
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.MidjourneyService = void 0;
const common_1 = require("@nestjs/common");
const axios_1 = require("axios");
const chatLog_service_1 = require("../chatLog/chatLog.service");
const globalConfig_service_1 = require("../globalConfig/globalConfig.service");
const upload_service_1 = require("../upload/upload.service");
let MidjourneyService = class MidjourneyService {
constructor(uploadService, globalConfigService, chatLogService) {
this.uploadService = uploadService;
this.globalConfigService = globalConfigService;
this.chatLogService = chatLogService;
}
async midjourneyDraw(inputs) {
var _a, _b, _c, _d;
const { id, apiKey, proxyUrl, action, drawId, prompt, usePrompt, customId, timeout, fileInfo, assistantLogId, } = inputs;
let result = {
text: '',
fileInfo: '',
drawId: '',
customId: '',
status: 2,
};
let response;
let retryCount = 0;
let url = '';
const headers = { 'mj-api-secret': apiKey };
common_1.Logger.debug(`当前任务类型: ${action}`, 'MidjourneyService');
while (retryCount < 3) {
let payloadJson = {};
try {
if (action === 'IMAGINE') {
url = `${proxyUrl}/mj/submit/imagine`;
payloadJson = { prompt: usePrompt };
}
else if (action === 'DESCRIBE') {
url = `${proxyUrl}/mj/submit/describe`;
if (fileInfo) {
const response = await fetch(fileInfo);
const blob = await response.blob();
const buffer = Buffer.from(await blob.arrayBuffer());
const base64String = buffer.toString('base64');
payloadJson = { base64: `data:image/png;base64,${base64String}` };
}
else {
return;
}
}
else if (action === 'PICREADER') {
url = `${proxyUrl}/mj/submit/action`;
payloadJson = { taskId: drawId, customId: customId };
response = await axios_1.default.post(url, payloadJson, { headers });
if ((response === null || response === void 0 ? void 0 : response.status) === 200 && ((_a = response === null || response === void 0 ? void 0 : response.data) === null || _a === void 0 ? void 0 : _a.result)) {
url = `${proxyUrl}/mj/submit/modal`;
payloadJson = { taskId: (_b = response === null || response === void 0 ? void 0 : response.data) === null || _b === void 0 ? void 0 : _b.result };
}
}
else {
url = `${proxyUrl}/mj/submit/action`;
payloadJson = { taskId: drawId, customId: customId };
}
common_1.Logger.log(`正在准备发送请求到 ${url}payload: ${JSON.stringify(payloadJson)}, headers: ${JSON.stringify(headers)}`, 'MidjourneyService');
response = await axios_1.default.post(url, payloadJson, { headers });
if ((response === null || response === void 0 ? void 0 : response.status) === 200 && ((_c = response === null || response === void 0 ? void 0 : response.data) === null || _c === void 0 ? void 0 : _c.result)) {
common_1.Logger.debug(`收到响应: ${JSON.stringify(response.data)}`, 'MidjourneyService');
result.drawId = (_d = response === null || response === void 0 ? void 0 : response.data) === null || _d === void 0 ? void 0 : _d.result;
result.state = 2;
result.answer = '绘画任务提交成功';
common_1.Logger.log(`绘画任务提交成功, 绘画ID: ${response.data.result}`, 'MidjourneyService');
break;
}
else {
throw new Error('未能获取结果数据, 即将重试');
}
}
catch (error) {
retryCount++;
if (retryCount >= 3) {
result.answer = '任务提交失败,请检查提示词后重试';
result.status = 5;
common_1.Logger.log(`绘画任务提交失败, 请检查后台配置或者稍后重试! ${error}`, 'MidjourneyService');
}
}
}
this.pollMjDrawingResult({
proxyUrl,
apiKey,
drawId: result.drawId,
timeout,
prompt,
onSuccess: async (data) => {
await this.chatLogService.updateChatLog(assistantLogId, {
fileInfo: data === null || data === void 0 ? void 0 : data.fileInfo,
answer: (data === null || data === void 0 ? void 0 : data.answer) || prompt,
progress: '100%',
status: 3,
drawId: data === null || data === void 0 ? void 0 : data.drawId,
customId: data === null || data === void 0 ? void 0 : data.customId,
});
common_1.Logger.log('绘图成功!', 'MidjourneyService');
},
onDrawing: async (data) => {
await this.chatLogService.updateChatLog(assistantLogId, {
answer: (data === null || data === void 0 ? void 0 : data.answer) || '绘制中',
progress: data === null || data === void 0 ? void 0 : data.progress,
status: 2,
});
common_1.Logger.log(`绘制中!绘制进度${data === null || data === void 0 ? void 0 : data.progress}`, 'MidjourneyService');
},
onFailure: async (data) => {
await this.chatLogService.updateChatLog(assistantLogId, {
answer: '绘图失败',
status: data.status,
});
common_1.Logger.log('绘图失败', 'MidjourneyService');
},
}).catch((error) => {
common_1.Logger.error('查询绘图结果时发生错误:', error, 'MidjourneyService');
});
return result;
}
async pollMjDrawingResult(inputs) {
const { proxyUrl, apiKey, drawId, timeout, onSuccess, prompt, onFailure, onDrawing, } = inputs;
const { mjNotSaveImg, mjProxyImgUrl, mjNotUseProxy } = await this.globalConfigService.getConfigs([
'mjNotSaveImg',
'mjProxyImgUrl',
'mjNotUseProxy',
]);
let result = {
fileInfo: '',
drawId: '',
customId: '',
status: 2,
progress: 0,
answer: '',
};
const startTime = Date.now();
const POLL_INTERVAL = 5000;
let retryCount = 0;
try {
while (Date.now() - startTime < timeout) {
await new Promise((resolve) => setTimeout(resolve, POLL_INTERVAL));
try {
const headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'mj-api-secret': apiKey,
};
const url = `${proxyUrl}/mj/task/${drawId}/fetch`;
const res = await axios_1.default.get(url, { headers });
const responses = res.data;
common_1.Logger.debug(`查询结果: ${JSON.stringify(responses)}`, 'MidjourneyService');
if (responses.status === 'SUCCESS') {
common_1.Logger.log(`绘制成功, 获取到的URL: ${responses.imageUrl}`, 'MidjourneyService');
let processedUrl = responses.imageUrl;
const shouldReplaceUrl = mjNotUseProxy === '0' && mjProxyImgUrl;
let logMessage = '';
if (shouldReplaceUrl) {
const newUrlBase = new URL(mjProxyImgUrl);
const parsedUrl = new URL(responses.imageUrl);
parsedUrl.protocol = newUrlBase.protocol;
parsedUrl.hostname = newUrlBase.hostname;
parsedUrl.port = newUrlBase.port ? newUrlBase.port : '';
processedUrl = parsedUrl.toString();
logMessage = `使用代理替换后的 URL: ${processedUrl}`;
common_1.Logger.log(logMessage, 'MidjourneyService');
}
if (mjNotSaveImg !== '1') {
try {
common_1.Logger.log(`------> 开始上传图片!!!`, 'MidjourneyService');
const now = new Date();
const year = now.getFullYear();
const month = String(now.getMonth() + 1).padStart(2, '0');
const day = String(now.getDate()).padStart(2, '0');
const currentDate = `${year}${month}/${day}`;
processedUrl = await this.uploadService.uploadFileFromUrl({
url: processedUrl,
dir: `images/midjourney/${currentDate}`,
});
logMessage = `上传成功 URL: ${processedUrl}`;
}
catch (uploadError) {
common_1.Logger.error('存储图片失败,使用原始/代理图片链接');
logMessage = `存储图片失败,使用原始/代理图片链接 ${processedUrl}`;
}
common_1.Logger.log(logMessage, 'MidjourneyService');
}
else {
logMessage = `不保存图片,使用 URL: ${processedUrl}`;
common_1.Logger.log(logMessage, 'MidjourneyService');
}
result.fileInfo = processedUrl;
result.drawId = responses.id;
result.customId = JSON.stringify(responses.buttons);
result.answer = `${prompt}\n${responses.finalPrompt || responses.properties.finalPrompt || ''}`;
onSuccess(result);
return;
}
result.progress = responses === null || responses === void 0 ? void 0 : responses.progress;
result.answer = `当前绘制进度 ${responses === null || responses === void 0 ? void 0 : responses.progress}`;
if (result.progress) {
onDrawing(result);
}
}
catch (error) {
retryCount++;
common_1.Logger.error(`轮询过程中发生错误: ${error}`, 'MidjourneyService');
}
}
common_1.Logger.error(`超过 ${startTime / 1000} s 未完成绘画, 请稍后再试! MidjourneyService`);
result.status = 4;
onFailure(result);
throw new common_1.HttpException('绘画超时,请稍后再试!', common_1.HttpStatus.BAD_REQUEST);
}
catch (error) {
common_1.Logger.error(`绘画失败: ${error} MidjourneyService`);
result.status = 5;
onFailure(result);
}
}
};
MidjourneyService = __decorate([
(0, common_1.Injectable)(),
__metadata("design:paramtypes", [upload_service_1.UploadService,
globalConfig_service_1.GlobalConfigService,
chatLog_service_1.ChatLogService])
], MidjourneyService);
exports.MidjourneyService = MidjourneyService;

View File

@@ -0,0 +1,128 @@
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.netSearchService = void 0;
const common_1 = require("@nestjs/common");
const globalConfig_service_1 = require("../globalConfig/globalConfig.service");
let netSearchService = class netSearchService {
constructor(globalConfigService) {
this.globalConfigService = globalConfigService;
}
async webSearchPro(prompt) {
var _a, _b, _c, _d, _e, _f;
try {
const { pluginUrl, pluginKey } = await this.globalConfigService.getConfigs(['pluginUrl', 'pluginKey']);
if (!pluginUrl || !pluginKey) {
common_1.Logger.warn('搜索插件配置缺失');
return { searchResults: [] };
}
const keys = pluginKey.split(',').filter((key) => key.trim());
const selectedKey = keys[Math.floor(Math.random() * keys.length)];
const isBochaiApi = pluginUrl.includes('bochaai.com');
const isBigModelApi = pluginUrl.includes('bigmodel.cn');
const isTavilyApi = pluginUrl.includes('tavily.com');
common_1.Logger.log(`[搜索] API类型: ${isBochaiApi
? 'Bochai'
: isBigModelApi
? 'BigModel'
: isTavilyApi
? 'Tavily'
: '未知'}`);
common_1.Logger.log(`[搜索] 请求URL: ${pluginUrl}`);
common_1.Logger.log(`[搜索] 搜索关键词: ${prompt}`);
const requestBody = isBochaiApi
? {
query: prompt,
freshness: 'oneWeek',
summary: true,
}
: isTavilyApi
? {
query: prompt,
search_depth: 'basic',
include_answer: false,
include_raw_content: false,
include_images: false,
}
: {
tool: 'web-search-pro',
stream: false,
messages: [{ role: 'user', content: prompt }],
};
common_1.Logger.log(`[搜索] 请求参数: ${JSON.stringify(requestBody, null, 2)}`);
const response = await fetch(pluginUrl, {
method: 'POST',
headers: {
Authorization: selectedKey,
'Content-Type': 'application/json',
},
body: JSON.stringify(requestBody),
});
if (!response.ok) {
common_1.Logger.error(`[搜索] 接口返回错误: ${response.status}`);
return { searchResults: [] };
}
const apiResult = await response.json();
common_1.Logger.log(`[搜索] 原始返回数据: ${JSON.stringify(apiResult, null, 2)}`);
let searchResults = [];
if (isBochaiApi) {
if ((apiResult === null || apiResult === void 0 ? void 0 : apiResult.code) === 200 && ((_b = (_a = apiResult === null || apiResult === void 0 ? void 0 : apiResult.data) === null || _a === void 0 ? void 0 : _a.webPages) === null || _b === void 0 ? void 0 : _b.value)) {
searchResults = apiResult.data.webPages.value.map((item) => ({
title: (item === null || item === void 0 ? void 0 : item.name) || '',
link: (item === null || item === void 0 ? void 0 : item.url) || '',
content: (item === null || item === void 0 ? void 0 : item.summary) || '',
icon: (item === null || item === void 0 ? void 0 : item.siteIcon) || '',
media: (item === null || item === void 0 ? void 0 : item.siteName) || '',
}));
}
}
else if (isBigModelApi) {
if (((_f = (_e = (_d = (_c = apiResult === null || apiResult === void 0 ? void 0 : apiResult.choices) === null || _c === void 0 ? void 0 : _c[0]) === null || _d === void 0 ? void 0 : _d.message) === null || _e === void 0 ? void 0 : _e.tool_calls) === null || _f === void 0 ? void 0 : _f.length) > 0) {
for (const toolCall of apiResult.choices[0].message.tool_calls) {
if (Array.isArray(toolCall.search_result)) {
searchResults = toolCall.search_result.map((item) => ({
title: (item === null || item === void 0 ? void 0 : item.title) || '',
link: (item === null || item === void 0 ? void 0 : item.link) || '',
content: (item === null || item === void 0 ? void 0 : item.content) || '',
icon: (item === null || item === void 0 ? void 0 : item.icon) || '',
media: (item === null || item === void 0 ? void 0 : item.media) || '',
}));
break;
}
}
}
}
else if (isTavilyApi) {
if (Array.isArray(apiResult === null || apiResult === void 0 ? void 0 : apiResult.results)) {
searchResults = apiResult.results.map((item) => ({
title: (item === null || item === void 0 ? void 0 : item.title) || '',
link: (item === null || item === void 0 ? void 0 : item.url) || '',
content: (item === null || item === void 0 ? void 0 : item.content) || '',
icon: '',
media: '',
}));
}
}
const formattedResult = searchResults.map((item, index) => (Object.assign({ resultIndex: index + 1 }, item)));
common_1.Logger.log(`[搜索] 格式化后的结果: ${JSON.stringify(formattedResult, null, 2)}`);
return { searchResults: formattedResult };
}
catch (fetchError) {
common_1.Logger.error('[搜索] 调用接口出错:', fetchError);
return { searchResults: [] };
}
}
};
netSearchService = __decorate([
(0, common_1.Injectable)(),
__metadata("design:paramtypes", [globalConfig_service_1.GlobalConfigService])
], netSearchService);
exports.netSearchService = netSearchService;

View File

@@ -0,0 +1,180 @@
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.OpenAIChatService = void 0;
const utils_1 = require("../../common/utils");
const common_1 = require("@nestjs/common");
const axios_1 = require("axios");
const globalConfig_service_1 = require("../globalConfig/globalConfig.service");
let OpenAIChatService = class OpenAIChatService {
constructor(globalConfigService) {
this.globalConfigService = globalConfigService;
}
async openAIChat(messagesHistory, inputs) {
const { onFailure, onProgress, apiKey, model, proxyUrl, modelName, timeout, chatId, isFileUpload, modelAvatar, temperature, abortController, } = inputs;
let result = {
text: '',
model: '',
modelName: modelName,
chatId: chatId,
answer: '',
errMsg: '',
modelAvatar: modelAvatar,
};
const data = Object.assign({ model, messages: messagesHistory }, (isFileUpload === 2 && { max_tokens: 2048 }));
data.stream = true;
data.temperature = temperature;
const options = {
method: 'POST',
url: `${proxyUrl}/v1/chat/completions`,
responseType: 'stream',
timeout: timeout,
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${apiKey}`,
},
data: data,
};
const sanitizedOptions = await this.sanitizeOptionsForLogging(options);
console.log('请求配置:', JSON.stringify(sanitizedOptions, null, 2), 'ChatService');
console.log('请求配置:', JSON.stringify(sanitizedOptions, null, 2), 'ChatService');
try {
const response = await (0, axios_1.default)(options);
const stream = response.data;
let buffer = '';
await new Promise((resolve, reject) => {
stream.on('data', (chunk) => {
buffer += chunk.toString();
let lines = buffer.split('\n');
buffer = lines.pop();
lines.forEach((line) => {
var _a, _b;
if (line.trim() === 'data: [DONE]') {
console.log('处理结束信号 [DONE]');
resolve(result);
return;
}
if (line.startsWith('data: ')) {
try {
const cleanedLine = line.slice(6).trim();
if (cleanedLine) {
const jsonLine = JSON.parse(cleanedLine);
const content = ((_b = (_a = jsonLine.choices[0]) === null || _a === void 0 ? void 0 : _a.delta) === null || _b === void 0 ? void 0 : _b.content) || '';
result.answer += content;
onProgress === null || onProgress === void 0 ? void 0 : onProgress({ text: content, answer: result.answer });
}
}
catch (error) {
console.error('Error parsing line:', line, error);
}
}
});
});
stream.on('end', () => {
resolve(result);
});
stream.on('error', (error) => {
reject(error);
});
abortController.signal.addEventListener('abort', () => {
resolve(result);
});
});
return result;
}
catch (error) {
result.errMsg = (0, utils_1.handleError)(error);
common_1.Logger.error(result.errMsg);
onFailure(result);
return result;
}
}
async sanitizeOptionsForLogging(options) {
const sanitizedOptions = JSON.parse(JSON.stringify(options));
if (sanitizedOptions.headers && sanitizedOptions.headers.Authorization) {
const authHeader = sanitizedOptions.headers.Authorization;
if (authHeader.startsWith('Bearer ')) {
const token = authHeader.slice(7);
if (token.length > 10) {
sanitizedOptions.headers.Authorization = `Bearer ${token.slice(0, 5)}****${token.slice(-4)}`;
}
}
}
if (sanitizedOptions.data &&
sanitizedOptions.data.messages &&
Array.isArray(sanitizedOptions.data.messages)) {
sanitizedOptions.data.messages = sanitizedOptions.data.messages.map((message) => {
if (message.content && Array.isArray(message.content)) {
message.content = message.content.map((content) => {
if (content.type === 'image_url' &&
content.image_url &&
content.image_url.url) {
content.image_url.url = 'data:image/***;base64 ... ...';
}
return content;
});
}
return message;
});
}
return sanitizedOptions;
}
async chatFree(prompt, systemMessage, messagesHistory) {
const { openaiBaseUrl = '', openaiBaseKey = '', openaiBaseModel, } = await this.globalConfigService.getConfigs([
'openaiBaseKey',
'openaiBaseUrl',
'openaiBaseModel',
]);
const key = openaiBaseKey;
const proxyUrl = openaiBaseUrl;
let requestData = [];
if (systemMessage) {
requestData.push({
role: 'system',
content: systemMessage,
});
}
if (messagesHistory && messagesHistory.length > 0) {
requestData = requestData.concat(messagesHistory);
}
else {
requestData.push({
role: 'user',
content: prompt,
});
}
const options = {
method: 'POST',
url: `${proxyUrl}/v1/chat/completions`,
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${key}`,
},
data: {
model: openaiBaseModel || 'gpt-3.5-turbo-0125',
messages: requestData,
},
};
try {
const response = await (0, axios_1.default)(options);
common_1.Logger.log(`全局模型调用成功, 返回结果: ${response === null || response === void 0 ? void 0 : response.data.choices[0].message.content}`, 'ChatService');
return response === null || response === void 0 ? void 0 : response.data.choices[0].message.content;
}
catch (error) {
console.log('error: ', error);
}
}
};
OpenAIChatService = __decorate([
(0, common_1.Injectable)(),
__metadata("design:paramtypes", [globalConfig_service_1.GlobalConfigService])
], OpenAIChatService);
exports.OpenAIChatService = OpenAIChatService;

View File

@@ -0,0 +1,145 @@
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var OpenAIDrawService_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.OpenAIDrawService = void 0;
const common_1 = require("@nestjs/common");
const axios_1 = require("axios");
const chatLog_service_1 = require("../chatLog/chatLog.service");
const globalConfig_service_1 = require("../globalConfig/globalConfig.service");
const upload_service_1 = require("../upload/upload.service");
const openaiChat_service_1 = require("./openaiChat.service");
let OpenAIDrawService = OpenAIDrawService_1 = class OpenAIDrawService {
constructor(uploadService, globalConfigService, chatLogService, openAIChatService) {
this.uploadService = uploadService;
this.globalConfigService = globalConfigService;
this.chatLogService = chatLogService;
this.openAIChatService = openAIChatService;
this.logger = new common_1.Logger(OpenAIDrawService_1.name);
}
async dalleDraw(inputs, buildMessageFromParentMessageId) {
var _a, _b, _c, _d;
common_1.Logger.log('开始提交 Dalle 绘图任务 ', 'DrawService');
const { apiKey, model, proxyUrl, prompt, extraParam, timeout, onSuccess, onFailure, groupId, } = inputs;
const isDalleChat = await this.globalConfigService.getConfigs([
'isDalleChat',
]);
let drawPrompt;
if (isDalleChat === '1') {
try {
common_1.Logger.log('已开启连续绘画模式', 'DalleDraw');
const { messagesHistory } = await buildMessageFromParentMessageId(`参考上文,结合我的需求,给出绘画描述。我的需求是:${prompt}`, {
groupId,
systemMessage: '你是一个绘画提示词生成工具,请根据用户的要求,结合上下文,用一段文字,描述用户需要的绘画需求,不用包含任何礼貌性的寒暄,只需要场景的描述,可以适当联想',
maxModelTokens: 8000,
maxRounds: 5,
fileInfo: '',
}, this.chatLogService);
drawPrompt = await this.openAIChatService.chatFree(prompt, undefined, messagesHistory);
}
catch (error) {
console.error('调用chatFree失败', error);
drawPrompt = prompt;
}
}
else {
drawPrompt = prompt;
}
const size = (extraParam === null || extraParam === void 0 ? void 0 : extraParam.size) || '1024x1024';
let result = { answer: '', fileInfo: '', status: 2 };
try {
const options = {
method: 'POST',
url: `${proxyUrl}/v1/images/generations`,
timeout: timeout,
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${apiKey}`,
},
data: {
model: model,
prompt: drawPrompt,
size,
},
};
const response = await (0, axios_1.default)(options);
const url = response.data.data[0].url;
try {
common_1.Logger.log(`------> 开始上传图片!!!`, 'DrawService');
const now = new Date();
const year = now.getFullYear();
const month = String(now.getMonth() + 1).padStart(2, '0');
const day = String(now.getDate()).padStart(2, '0');
const currentDate = `${year}${month}/${day}`;
result.fileInfo = await this.uploadService.uploadFileFromUrl({
url: url,
dir: `images/dalle/${currentDate}`,
});
common_1.Logger.log(`图片上传成功URL: ${result.fileInfo}`, 'DrawService');
}
catch (error) {
common_1.Logger.error(`上传图片过程中出现错误: ${error}`, 'DrawService');
}
let revised_prompt_cn;
try {
revised_prompt_cn = await this.openAIChatService.chatFree(`根据提示词{${response.data.data[0].revised_prompt}}, 模拟AI绘画机器人的语气用中文回复告诉用户已经画好了`);
}
catch (error) {
revised_prompt_cn = `${prompt} 绘制成功`;
common_1.Logger.error('翻译失败: ', error);
}
result.answer = revised_prompt_cn;
result.status = 3;
onSuccess(result);
return;
}
catch (error) {
result.status = 5;
onFailure(result);
const status = ((_a = error === null || error === void 0 ? void 0 : error.response) === null || _a === void 0 ? void 0 : _a.status) || 500;
console.log('draw error: ', JSON.stringify(error), status);
const message = (_d = (_c = (_b = error === null || error === void 0 ? void 0 : error.response) === null || _b === void 0 ? void 0 : _b.data) === null || _c === void 0 ? void 0 : _c.error) === null || _d === void 0 ? void 0 : _d.message;
if (status === 429) {
result.text = '当前请求已过载、请稍等会儿再试试吧!';
return result;
}
if (status === 400 &&
message.includes('This request has been blocked by our content filters')) {
result.text = '您的请求已被系统拒绝。您的提示可能存在一些非法的文本。';
return result;
}
if (status === 400 &&
message.includes('Billing hard limit has been reached')) {
result.text =
'当前模型key已被封禁、已冻结当前调用Key、尝试重新对话试试吧';
return result;
}
if (status === 500) {
result.text = '绘制图片失败,请检查你的提示词是否有非法描述!';
return result;
}
if (status === 401) {
result.text = '绘制图片失败,此次绘画被拒绝了!';
return result;
}
result.text = '绘制图片失败,请稍后试试吧!';
return result;
}
}
};
OpenAIDrawService = OpenAIDrawService_1 = __decorate([
(0, common_1.Injectable)(),
__metadata("design:paramtypes", [upload_service_1.UploadService,
globalConfig_service_1.GlobalConfigService,
chatLog_service_1.ChatLogService,
openaiChat_service_1.OpenAIChatService])
], OpenAIDrawService);
exports.OpenAIDrawService = OpenAIDrawService;

View File

@@ -0,0 +1,104 @@
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var StableDiffusionService_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.StableDiffusionService = void 0;
const common_1 = require("@nestjs/common");
const axios_1 = require("axios");
const globalConfig_service_1 = require("../globalConfig/globalConfig.service");
const upload_service_1 = require("../upload/upload.service");
let StableDiffusionService = StableDiffusionService_1 = class StableDiffusionService {
constructor(uploadService, globalConfigService) {
this.uploadService = uploadService;
this.globalConfigService = globalConfigService;
this.logger = new common_1.Logger(StableDiffusionService_1.name);
}
async sdxl(inputs) {
const { onSuccess, onFailure, apiKey, model, proxyUrl, modelName, timeout, chatId, prompt, } = inputs;
let result = {
answer: '',
model: model,
modelName: modelName,
chatId: chatId,
fileInfo: '',
status: 2,
};
console.log('开始处理', { model, modelName, prompt });
const options = {
method: 'POST',
url: `${proxyUrl}/v1/chat/completions`,
timeout: timeout,
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${apiKey}`,
},
data: {
model,
messages: [{ role: 'user', content: prompt }],
},
};
try {
const response = await (0, axios_1.default)(options);
console.log('API响应接收', response.data);
if (response.data.choices && response.data.choices.length > 0) {
const choice = response.data.choices[0];
const content = choice.message.content;
console.log('处理内容', content);
const regex = /\]\((https?:\/\/[^\)]+)\)/;
const match = content.match(regex);
if (match && match[1]) {
result.fileInfo = match[1];
try {
const localStorageStatus = await this.globalConfigService.getConfigs(['localStorageStatus']);
if (Number(localStorageStatus)) {
const now = new Date();
const year = now.getFullYear();
const month = String(now.getMonth() + 1).padStart(2, '0');
const day = String(now.getDate()).padStart(2, '0');
const currentDate = `${year}${month}/${day}`;
result.fileInfo = await this.uploadService.uploadFileFromUrl({
url: result.fileInfo,
dir: `images/stable-diffusion/${currentDate}`,
});
}
}
catch (error) {
common_1.Logger.error(`上传文件失败: ${error.message}`, 'StableDiffusionService');
}
console.log('找到链接', match[1]);
}
else {
console.log('没有找到链接');
}
result.answer = `${prompt} 绘制成功`;
if (result.fileInfo) {
onSuccess(result);
return;
}
else {
onFailure('No link found.');
}
}
else {
onFailure('No choices returned.');
}
}
catch (error) {
common_1.Logger.error('服务器错误,请求失败:', error);
}
}
};
StableDiffusionService = StableDiffusionService_1 = __decorate([
(0, common_1.Injectable)(),
__metadata("design:paramtypes", [upload_service_1.UploadService,
globalConfig_service_1.GlobalConfigService])
], StableDiffusionService);
exports.StableDiffusionService = StableDiffusionService;

View File

@@ -0,0 +1,325 @@
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SunoService = void 0;
const common_1 = require("@nestjs/common");
const axios_1 = require("axios");
const chatLog_service_1 = require("../chatLog/chatLog.service");
const globalConfig_service_1 = require("../globalConfig/globalConfig.service");
const upload_service_1 = require("../upload/upload.service");
let SunoService = class SunoService {
constructor(chatLogService, uploadService, globalConfigService) {
this.chatLogService = chatLogService;
this.uploadService = uploadService;
this.globalConfigService = globalConfigService;
}
async suno(inputs) {
var _a, _b, _c;
const { apiKey, proxyUrl, action, prompt, timeout, assistantLogId, taskData, extraParam, } = inputs;
common_1.Logger.debug(`SunoService: ${JSON.stringify(inputs)}`, 'SunoService');
let result = {
text: '',
fileInfo: '',
taskId: '',
taskData: '',
status: 2,
};
common_1.Logger.log('开始生成音乐', 'SunoService');
let response = null;
let url = '';
let payloadJson = {};
const headers = { Authorization: `Bearer ${apiKey}` };
if (action === 'LYRICS') {
url = `${proxyUrl}/task/suno/v1/submit/lyrics`;
payloadJson = { prompt: prompt };
}
if (action === 'MUSIC') {
url = `${proxyUrl}/task/suno/v1/submit/music`;
try {
payloadJson = JSON.parse(taskData);
}
catch (error) {
common_1.Logger.error(`解析taskData失败: ${error.message}`, 'SunoService');
throw new Error('taskData格式错误');
}
}
common_1.Logger.log(`正在准备发送请求到 ${url}payload: ${JSON.stringify(payloadJson)}, headers: ${JSON.stringify(headers)}`, 'SunoService');
try {
response = await axios_1.default.post(url, payloadJson, { headers });
common_1.Logger.debug(`任务提交结果,状态码: ${response.status}, 状态消息: ${response.statusText}, 数据: ${JSON.stringify(response.data)}`);
}
catch (error) {
common_1.Logger.error(`任务提交失败: ${error.message}`, 'SunoService');
throw new Error('任务提交失败');
}
if ((_a = response === null || response === void 0 ? void 0 : response.data) === null || _a === void 0 ? void 0 : _a.data) {
result.taskId = (_b = response === null || response === void 0 ? void 0 : response.data) === null || _b === void 0 ? void 0 : _b.data;
common_1.Logger.log(`任务提交成功, 任务ID: ${(_c = response === null || response === void 0 ? void 0 : response.data) === null || _c === void 0 ? void 0 : _c.data}`, 'SunoService');
}
else {
throw new Error('未能获取结果数据, 即将重试');
}
try {
await this.pollSunoMusicResult({
proxyUrl,
apiKey,
taskId: response.data.data,
timeout,
prompt,
action,
onSuccess: async (data) => {
try {
await this.chatLogService.updateChatLog(assistantLogId, {
videoUrl: data === null || data === void 0 ? void 0 : data.videoUrl,
audioUrl: data === null || data === void 0 ? void 0 : data.audioUrl,
fileInfo: data === null || data === void 0 ? void 0 : data.fileInfo,
answer: (data === null || data === void 0 ? void 0 : data.answer) || prompt,
progress: '100%',
status: 3,
taskId: data === null || data === void 0 ? void 0 : data.taskId,
taskData: data === null || data === void 0 ? void 0 : data.taskData,
});
common_1.Logger.log('音乐任务已完成', 'SunoService');
}
catch (error) {
common_1.Logger.error(`更新日志失败: ${error.message}`, 'SunoService');
}
},
onAudioSuccess: async (data) => {
try {
await this.chatLogService.updateChatLog(assistantLogId, {
videoUrl: data === null || data === void 0 ? void 0 : data.videoUrl,
audioUrl: data === null || data === void 0 ? void 0 : data.audioUrl,
fileInfo: data === null || data === void 0 ? void 0 : data.fileInfo,
answer: (data === null || data === void 0 ? void 0 : data.answer) || prompt,
progress: data === null || data === void 0 ? void 0 : data.progress,
status: data.status,
taskId: data === null || data === void 0 ? void 0 : data.taskId,
taskData: data === null || data === void 0 ? void 0 : data.taskData,
});
common_1.Logger.log('音频生成成功,等待视频生成...', 'SunoService');
}
catch (error) {
common_1.Logger.error(`更新日志失败: ${error.message}`, 'SunoService');
}
},
onGenerating: async (data) => {
try {
await this.chatLogService.updateChatLog(assistantLogId, {
videoUrl: data === null || data === void 0 ? void 0 : data.videoUrl,
audioUrl: data === null || data === void 0 ? void 0 : data.audioUrl,
fileInfo: data === null || data === void 0 ? void 0 : data.fileInfo,
answer: (data === null || data === void 0 ? void 0 : data.answer) || '音乐生成中...',
progress: data === null || data === void 0 ? void 0 : data.progress,
status: data.status,
});
common_1.Logger.log('音乐生成中...', 'SunoService');
}
catch (error) {
common_1.Logger.error(`更新日志失败: ${error.message}`, 'SunoService');
}
},
onFailure: async (data) => {
try {
await this.chatLogService.updateChatLog(assistantLogId, {
answer: '音乐生成失败',
status: data.status,
});
common_1.Logger.log('生成失败', 'SunoService');
}
catch (error) {
common_1.Logger.error(`更新日志失败: ${error.message}`, 'SunoService');
}
},
});
}
catch (error) {
common_1.Logger.error('查询生成结果时发生错误:', error.message, 'SunoService');
throw new Error('查询生成结果时发生错误');
}
return result;
}
async pollSunoMusicResult(inputs) {
const { proxyUrl, apiKey, taskId, timeout, onSuccess, onAudioSuccess, onFailure, onGenerating, action, } = inputs;
let result = {
videoUrl: '',
audioUrl: '',
fileInfo: '',
drawId: '',
taskData: '',
status: 2,
progress: 0,
answer: '',
};
const headers = { Authorization: `Bearer ${apiKey}` };
const url = `${proxyUrl}/task/suno/v1/fetch/${taskId}`;
const startTime = Date.now();
const POLL_INTERVAL = 5000;
let retryCount = 0;
try {
while (Date.now() - startTime < timeout) {
await new Promise((resolve) => setTimeout(resolve, POLL_INTERVAL));
try {
const res = await axios_1.default.get(url, { headers });
const responses = res.data.data;
common_1.Logger.debug(`轮询结果: ${JSON.stringify(responses)}`, 'SunoService');
if (action === 'LYRICS') {
if (responses.status === 'SUCCESS') {
result.taskId = responses.data.id;
result.taskData = JSON.stringify(responses.data);
result.answer = responses.data.text;
onSuccess(result);
return;
}
result.progress = responses === null || responses === void 0 ? void 0 : responses.progress;
result.answer = `歌词生成中`;
if (result.progress) {
onGenerating(result);
}
}
if (action === 'MUSIC') {
if (responses.data) {
const data = responses.data;
result.taskData = JSON.stringify(data);
if (Array.isArray(data)) {
const validAudioUrls = data
.map((item) => item.audio_url)
.filter((url) => url);
const validVideoUrls = data
.map((item) => item.video_url)
.filter((url) => url);
const validImageUrls = data
.map((item) => item.image_url)
.filter((url) => url);
const titles = data.map((item) => item.title);
const firstTitle = titles.length > 0 ? titles[0] : '音乐已生成';
if (responses.status === 'SUCCESS') {
let audioUrls = [];
let videoUrls = [];
let imageUrls = [];
try {
const localStorageStatus = await this.globalConfigService.getConfigs([
'localStorageStatus',
]);
if (Number(localStorageStatus)) {
const now = new Date();
const year = now.getFullYear();
const month = String(now.getMonth() + 1).padStart(2, '0');
const day = String(now.getDate()).padStart(2, '0');
const currentDate = `${year}${month}/${day}`;
for (const url of validAudioUrls) {
try {
const uploadedUrl = await this.uploadService.uploadFileFromUrl({
url: url,
dir: `audio/suno-music/${currentDate}`,
});
audioUrls.push(uploadedUrl);
}
catch (error) {
common_1.Logger.error(`上传音频文件失败: ${error.message}`, 'SunoService');
audioUrls.push(url);
}
}
for (const url of validVideoUrls) {
try {
const uploadedUrl = await this.uploadService.uploadFileFromUrl({
url: url,
dir: `video/suno-music/${currentDate}`,
});
videoUrls.push(uploadedUrl);
}
catch (error) {
common_1.Logger.error(`上传视频文件失败: ${error.message}`, 'SunoService');
videoUrls.push(url);
}
}
for (const url of validImageUrls) {
try {
const uploadedUrl = await this.uploadService.uploadFileFromUrl({
url: url,
dir: `images/suno-music/${currentDate}`,
});
imageUrls.push(uploadedUrl);
}
catch (error) {
common_1.Logger.error(`上传图片文件失败: ${error.message}`, 'SunoService');
imageUrls.push(url);
}
}
}
else {
audioUrls = validAudioUrls;
videoUrls = validVideoUrls;
imageUrls = validImageUrls;
}
}
catch (error) {
common_1.Logger.error(`获取配置失败: ${error.message}`, 'LumaService');
audioUrls = validAudioUrls;
videoUrls = validVideoUrls;
imageUrls = validImageUrls;
}
result.audioUrl = audioUrls.join(',');
result.videoUrl = videoUrls.join(',');
result.fileInfo = imageUrls.join(',');
if (validAudioUrls.length === 2) {
result.status = 3;
result.answer = firstTitle;
}
else {
result.status = 2;
result.progress = responses === null || responses === void 0 ? void 0 : responses.progress;
result.answer = `当前生成进度 ${responses === null || responses === void 0 ? void 0 : responses.progress}`;
}
common_1.Logger.debug(`音乐生成成功: ${JSON.stringify(data)}`, 'SunoService');
onSuccess(result);
return;
}
else {
result.audioUrl = validAudioUrls.join(',');
result.videoUrl = validVideoUrls.join(',');
result.fileInfo = validImageUrls.join(',');
result.status = 2;
result.progress = responses === null || responses === void 0 ? void 0 : responses.progress;
result.answer = firstTitle;
onAudioSuccess(result);
}
}
}
if (!result.audioUrl && result.progress && result.status === 2) {
onGenerating(result);
}
}
}
catch (error) {
retryCount++;
common_1.Logger.error(`轮询失败,重试次数: ${retryCount}`, 'SunoService');
}
}
common_1.Logger.error('轮询超时,请稍后再试!', 'SunoService');
result.status = 4;
onFailure(result);
throw new Error('查询超时,请稍后再试!');
}
catch (error) {
common_1.Logger.error(`轮询过程中发生错误: ${error}`, 'SunoService');
result.status = 5;
onFailure(result);
}
}
};
SunoService = __decorate([
(0, common_1.Injectable)(),
__metadata("design:paramtypes", [chatLog_service_1.ChatLogService,
upload_service_1.UploadService,
globalConfig_service_1.GlobalConfigService])
], SunoService);
exports.SunoService = SunoService;