mirror of
https://github.com/vastxie/99AI.git
synced 2025-11-10 18:53:46 +08:00
NineAI 2.4.2
This commit is contained in:
51
dist/modules/queue/dto/mjDraw.dto.js
vendored
Normal file
51
dist/modules/queue/dto/mjDraw.dto.js
vendored
Normal file
@@ -0,0 +1,51 @@
|
||||
"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.MjDrawDto = void 0;
|
||||
const class_validator_1 = require("class-validator");
|
||||
const swagger_1 = require("@nestjs/swagger");
|
||||
class MjDrawDto {
|
||||
}
|
||||
__decorate([
|
||||
(0, swagger_1.ApiProperty)({
|
||||
example: 'close-up polaroid photo, of a little joyful cute panda, in the forest, sun rays coming, photographic, sharp focus, depth of field, soft lighting, heigh quality, 24mm, Nikon Z FX',
|
||||
description: '绘画提示词!',
|
||||
required: true,
|
||||
}),
|
||||
(0, class_validator_1.IsOptional)(),
|
||||
__metadata("design:type", String)
|
||||
], MjDrawDto.prototype, "prompt", void 0);
|
||||
__decorate([
|
||||
(0, swagger_1.ApiProperty)({ example: '--ar 16:9 --c 0', description: '除了prompt的额外参数' }),
|
||||
(0, class_validator_1.IsOptional)(),
|
||||
__metadata("design:type", String)
|
||||
], MjDrawDto.prototype, "extraParam", void 0);
|
||||
__decorate([
|
||||
(0, swagger_1.ApiProperty)({ example: 'https://xsdasdasd.com', description: '垫图图片地址' }),
|
||||
(0, class_validator_1.IsOptional)(),
|
||||
__metadata("design:type", String)
|
||||
], MjDrawDto.prototype, "imgUrl", void 0);
|
||||
__decorate([
|
||||
(0, swagger_1.ApiProperty)({ example: 1, description: '绘画动作 绘图、放大、变换、图生图' }),
|
||||
(0, class_validator_1.IsOptional)(),
|
||||
__metadata("design:type", Number)
|
||||
], MjDrawDto.prototype, "action", void 0);
|
||||
__decorate([
|
||||
(0, swagger_1.ApiProperty)({ example: 1, description: '变体或者放大的序号' }),
|
||||
(0, class_validator_1.IsOptional)(),
|
||||
__metadata("design:type", Number)
|
||||
], MjDrawDto.prototype, "orderId", void 0);
|
||||
__decorate([
|
||||
(0, swagger_1.ApiProperty)({ example: 1, description: '绘画的DBID' }),
|
||||
(0, class_validator_1.IsOptional)(),
|
||||
__metadata("design:type", Number)
|
||||
], MjDrawDto.prototype, "drawId", void 0);
|
||||
exports.MjDrawDto = MjDrawDto;
|
||||
55
dist/modules/queue/queue.controller.js
vendored
Normal file
55
dist/modules/queue/queue.controller.js
vendored
Normal file
@@ -0,0 +1,55 @@
|
||||
"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 __param = (this && this.__param) || function (paramIndex, decorator) {
|
||||
return function (target, key) { decorator(target, key, paramIndex); }
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.QueueController = void 0;
|
||||
const queue_service_1 = require("./queue.service");
|
||||
const common_1 = require("@nestjs/common");
|
||||
const swagger_1 = require("@nestjs/swagger");
|
||||
const mjDraw_dto_1 = require("./dto/mjDraw.dto");
|
||||
const jwtAuth_guard_1 = require("../../common/auth/jwtAuth.guard");
|
||||
let QueueController = class QueueController {
|
||||
constructor(queueService) {
|
||||
this.queueService = queueService;
|
||||
}
|
||||
async mjDraw(body, req) {
|
||||
return await this.queueService.addMjDrawQueue(body, req);
|
||||
}
|
||||
async getQueue() {
|
||||
return await this.queueService.getQueue();
|
||||
}
|
||||
};
|
||||
__decorate([
|
||||
(0, common_1.Post)('addMjDrawQueue'),
|
||||
(0, swagger_1.ApiOperation)({ summary: '提交绘制图片任务' }),
|
||||
(0, common_1.UseGuards)(jwtAuth_guard_1.JwtAuthGuard),
|
||||
(0, swagger_1.ApiBearerAuth)(),
|
||||
__param(0, (0, common_1.Body)()),
|
||||
__param(1, (0, common_1.Req)()),
|
||||
__metadata("design:type", Function),
|
||||
__metadata("design:paramtypes", [mjDraw_dto_1.MjDrawDto, Object]),
|
||||
__metadata("design:returntype", Promise)
|
||||
], QueueController.prototype, "mjDraw", null);
|
||||
__decorate([
|
||||
(0, common_1.Get)('getQueue'),
|
||||
(0, swagger_1.ApiOperation)({ summary: '查询任务队列' }),
|
||||
__metadata("design:type", Function),
|
||||
__metadata("design:paramtypes", []),
|
||||
__metadata("design:returntype", Promise)
|
||||
], QueueController.prototype, "getQueue", null);
|
||||
QueueController = __decorate([
|
||||
(0, swagger_1.ApiTags)('Queue'),
|
||||
(0, common_1.Controller)('queue'),
|
||||
__metadata("design:paramtypes", [queue_service_1.QueueService])
|
||||
], QueueController);
|
||||
exports.QueueController = QueueController;
|
||||
38
dist/modules/queue/queue.module.js
vendored
Normal file
38
dist/modules/queue/queue.module.js
vendored
Normal file
@@ -0,0 +1,38 @@
|
||||
"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;
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.QueueModule = void 0;
|
||||
const common_1 = require("@nestjs/common");
|
||||
const queue_controller_1 = require("./queue.controller");
|
||||
const queue_service_1 = require("./queue.service");
|
||||
const bull_1 = require("@nestjs/bull");
|
||||
const queue_process_1 = require("./queue.process");
|
||||
let QueueModule = class QueueModule {
|
||||
};
|
||||
QueueModule = __decorate([
|
||||
(0, common_1.Module)({
|
||||
imports: [
|
||||
bull_1.BullModule.registerQueueAsync({
|
||||
name: 'MJDRAW',
|
||||
useFactory: () => {
|
||||
const config = {
|
||||
port: +process.env.REDIS_PORT,
|
||||
host: process.env.REDIS_HOST,
|
||||
};
|
||||
process.env.REDIS_PASSWORD && (config.password = process.env.REDIS_PASSWORD);
|
||||
return {
|
||||
redis: config,
|
||||
};
|
||||
},
|
||||
}),
|
||||
],
|
||||
controllers: [queue_controller_1.QueueController],
|
||||
providers: [queue_service_1.QueueService, queue_process_1.QueueProcessor],
|
||||
})
|
||||
], QueueModule);
|
||||
exports.QueueModule = QueueModule;
|
||||
119
dist/modules/queue/queue.process.js
vendored
Normal file
119
dist/modules/queue/queue.process.js
vendored
Normal file
@@ -0,0 +1,119 @@
|
||||
"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 QueueProcessor_1;
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.QueueProcessor = void 0;
|
||||
const bull_1 = require("@nestjs/bull");
|
||||
const common_1 = require("@nestjs/common");
|
||||
const midjourney_service_1 = require("../midjourney/midjourney.service");
|
||||
let QueueProcessor = QueueProcessor_1 = class QueueProcessor {
|
||||
constructor(midjourneyService) {
|
||||
this.midjourneyService = midjourneyService;
|
||||
this.logger = new common_1.Logger(QueueProcessor_1.name);
|
||||
}
|
||||
async handleJob(job) {
|
||||
const res = await this.midjourneyService.draw(job.data, job.id);
|
||||
return res;
|
||||
}
|
||||
onQueueActive(job) {
|
||||
}
|
||||
onQueueError(error) {
|
||||
console.log('队列发生错误', error);
|
||||
}
|
||||
onQueueProgress(job, progress) {
|
||||
console.log('队列任务的一个回调用于通知当前进度', job.id, progress);
|
||||
}
|
||||
onQueueCompleted(job, result) {
|
||||
}
|
||||
onQueueFailed(job, err) {
|
||||
common_1.Logger.error(`Queue failed: ${err.message}: 绘画失败 ${job.id}`, 'QueueProcessor');
|
||||
this.midjourneyService.drawFailed(job.data);
|
||||
}
|
||||
onQueuePaused() {
|
||||
console.log('队列暂停的时候调用');
|
||||
}
|
||||
onQueueResumed() {
|
||||
console.log('队列恢复的时候调用');
|
||||
}
|
||||
onQueueCleaned(jobs, type) {
|
||||
common_1.Logger.log(`Queue cleaned: ${jobs.length} jobs of type ${type} were cleaned.`, 'QueueProcessor');
|
||||
}
|
||||
onQueueDrained() {
|
||||
}
|
||||
};
|
||||
__decorate([
|
||||
(0, bull_1.Process)({
|
||||
name: 'mjDraw',
|
||||
concurrency: process.env.CONCURRENCY ? +process.env.CONCURRENCY : 3,
|
||||
}),
|
||||
__metadata("design:type", Function),
|
||||
__metadata("design:paramtypes", [Object]),
|
||||
__metadata("design:returntype", Promise)
|
||||
], QueueProcessor.prototype, "handleJob", null);
|
||||
__decorate([
|
||||
(0, bull_1.OnQueueActive)(),
|
||||
__metadata("design:type", Function),
|
||||
__metadata("design:paramtypes", [Object]),
|
||||
__metadata("design:returntype", void 0)
|
||||
], QueueProcessor.prototype, "onQueueActive", null);
|
||||
__decorate([
|
||||
(0, bull_1.OnQueueError)(),
|
||||
__metadata("design:type", Function),
|
||||
__metadata("design:paramtypes", [Error]),
|
||||
__metadata("design:returntype", void 0)
|
||||
], QueueProcessor.prototype, "onQueueError", null);
|
||||
__decorate([
|
||||
(0, bull_1.OnQueueProgress)(),
|
||||
__metadata("design:type", Function),
|
||||
__metadata("design:paramtypes", [Object, Number]),
|
||||
__metadata("design:returntype", void 0)
|
||||
], QueueProcessor.prototype, "onQueueProgress", null);
|
||||
__decorate([
|
||||
(0, bull_1.OnQueueCompleted)(),
|
||||
__metadata("design:type", Function),
|
||||
__metadata("design:paramtypes", [Object, Object]),
|
||||
__metadata("design:returntype", void 0)
|
||||
], QueueProcessor.prototype, "onQueueCompleted", null);
|
||||
__decorate([
|
||||
(0, bull_1.OnQueueFailed)(),
|
||||
__metadata("design:type", Function),
|
||||
__metadata("design:paramtypes", [Object, Error]),
|
||||
__metadata("design:returntype", void 0)
|
||||
], QueueProcessor.prototype, "onQueueFailed", null);
|
||||
__decorate([
|
||||
(0, bull_1.OnQueuePaused)(),
|
||||
__metadata("design:type", Function),
|
||||
__metadata("design:paramtypes", []),
|
||||
__metadata("design:returntype", void 0)
|
||||
], QueueProcessor.prototype, "onQueuePaused", null);
|
||||
__decorate([
|
||||
(0, bull_1.OnQueueResumed)(),
|
||||
__metadata("design:type", Function),
|
||||
__metadata("design:paramtypes", []),
|
||||
__metadata("design:returntype", void 0)
|
||||
], QueueProcessor.prototype, "onQueueResumed", null);
|
||||
__decorate([
|
||||
(0, bull_1.OnQueueCleaned)(),
|
||||
__metadata("design:type", Function),
|
||||
__metadata("design:paramtypes", [Array, String]),
|
||||
__metadata("design:returntype", void 0)
|
||||
], QueueProcessor.prototype, "onQueueCleaned", null);
|
||||
__decorate([
|
||||
(0, bull_1.OnQueueDrained)(),
|
||||
__metadata("design:type", Function),
|
||||
__metadata("design:paramtypes", []),
|
||||
__metadata("design:returntype", void 0)
|
||||
], QueueProcessor.prototype, "onQueueDrained", null);
|
||||
QueueProcessor = QueueProcessor_1 = __decorate([
|
||||
(0, bull_1.Processor)('MJDRAW'),
|
||||
__metadata("design:paramtypes", [midjourney_service_1.MidjourneyService])
|
||||
], QueueProcessor);
|
||||
exports.QueueProcessor = QueueProcessor;
|
||||
115
dist/modules/queue/queue.service.js
vendored
Normal file
115
dist/modules/queue/queue.service.js
vendored
Normal file
@@ -0,0 +1,115 @@
|
||||
"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 __param = (this && this.__param) || function (paramIndex, decorator) {
|
||||
return function (target, key) { decorator(target, key, paramIndex); }
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.QueueService = void 0;
|
||||
const common_1 = require("@nestjs/common");
|
||||
const bull_1 = require("@nestjs/bull");
|
||||
const utils_1 = require("../../common/utils");
|
||||
const midjourney_service_1 = require("../midjourney/midjourney.service");
|
||||
const midjourney_constant_1 = require("../../common/constants/midjourney.constant");
|
||||
const userBalance_service_1 = require("../userBalance/userBalance.service");
|
||||
const globalConfig_service_1 = require("../globalConfig/globalConfig.service");
|
||||
let QueueService = class QueueService {
|
||||
constructor(mjDrawQueue, midjourneyService, userBalanceService, globalConfigService) {
|
||||
this.mjDrawQueue = mjDrawQueue;
|
||||
this.midjourneyService = midjourneyService;
|
||||
this.userBalanceService = userBalanceService;
|
||||
this.globalConfigService = globalConfigService;
|
||||
this.jobIds = [];
|
||||
}
|
||||
async onApplicationBootstrap() {
|
||||
await this.mjDrawQueue.clean(0, 'active');
|
||||
await this.midjourneyService.cleanQueue();
|
||||
}
|
||||
async addMjDrawQueue(body, req) {
|
||||
const { prompt, imgUrl, extraParam, orderId, action = 1, drawId } = body;
|
||||
await this.midjourneyService.checkLimit(req);
|
||||
await this.userBalanceService.validateBalance(req, 'mjDraw', action === 2 ? 1 : 4);
|
||||
if (action === midjourney_constant_1.MidjourneyActionEnum.DRAW || action === midjourney_constant_1.MidjourneyActionEnum.GENERATE) {
|
||||
const randomDrawId = `${(0, utils_1.createRandomUid)()}`;
|
||||
const params = Object.assign(Object.assign({}, body), { userId: req.user.id, randomDrawId });
|
||||
const res = await this.midjourneyService.addDrawQueue(params);
|
||||
const timeout = (await this.globalConfigService.getConfigs(['mjTimeoutMs'])) || 200000;
|
||||
const job = await this.mjDrawQueue.add('mjDraw', { id: res.id, action: imgUrl ? 4 : 1, userId: req.user.id }, { delay: 1000, timeout: +timeout });
|
||||
this.jobIds.push(job.id);
|
||||
await this.userBalanceService.deductFromBalance(req.user.id, 'mjDraw', 4, 4);
|
||||
return true;
|
||||
}
|
||||
if (!drawId || !orderId) {
|
||||
throw new common_1.HttpException('缺少必要参数!', common_1.HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
if (action === midjourney_constant_1.MidjourneyActionEnum.UPSCALE) {
|
||||
const actionDetail = await this.midjourneyService.getDrawActionDetail(action, drawId, orderId);
|
||||
const { custom_id } = actionDetail;
|
||||
await this.midjourneyService.checkIsUpscale(custom_id);
|
||||
const params = Object.assign(Object.assign(Object.assign({}, body), { userId: req.user.id }), actionDetail);
|
||||
const res = await this.midjourneyService.addDrawQueue(params);
|
||||
const timeout = (await this.globalConfigService.getConfigs(['mjTimeoutMs'])) || 200000;
|
||||
const job = await this.mjDrawQueue.add('mjDraw', { id: res.id, action, userId: req.user.id }, { delay: 1000, timeout: +timeout });
|
||||
await this.userBalanceService.deductFromBalance(req.user.id, 'mjDraw', 1, 1);
|
||||
this.jobIds.push(job.id);
|
||||
return;
|
||||
}
|
||||
if (action === midjourney_constant_1.MidjourneyActionEnum.VARIATION) {
|
||||
const actionDetail = await this.midjourneyService.getDrawActionDetail(action, drawId, orderId);
|
||||
const params = Object.assign(Object.assign(Object.assign({}, body), { userId: req.user.id }), actionDetail);
|
||||
const res = await this.midjourneyService.addDrawQueue(params);
|
||||
const timeout = (await this.globalConfigService.getConfigs(['mjTimeoutMs'])) || 200000;
|
||||
const job = await this.mjDrawQueue.add('mjDraw', { id: res.id, action, userId: req.user.id }, { delay: 1000, timeout: +timeout });
|
||||
this.jobIds.push(job.id);
|
||||
await this.userBalanceService.deductFromBalance(req.user.id, 'mjDraw', 4, 4);
|
||||
return;
|
||||
}
|
||||
if (action === midjourney_constant_1.MidjourneyActionEnum.REGENERATE) {
|
||||
const actionDetail = await this.midjourneyService.getDrawActionDetail(action, drawId, orderId);
|
||||
const params = Object.assign(Object.assign(Object.assign({}, body), { userId: req.user.id }), actionDetail);
|
||||
const res = await this.midjourneyService.addDrawQueue(params);
|
||||
const timeout = (await this.globalConfigService.getConfigs(['mjTimeoutMs'])) || 200000;
|
||||
const job = await this.mjDrawQueue.add('mjDraw', { id: res.id, action, userId: req.user.id }, { delay: 1000, timeout: +timeout });
|
||||
this.jobIds.push(job.id);
|
||||
await this.userBalanceService.deductFromBalance(req.user.id, 'mjDraw', 4, 4);
|
||||
return;
|
||||
}
|
||||
if (action === midjourney_constant_1.MidjourneyActionEnum.VARY) {
|
||||
const actionDetail = await this.midjourneyService.getDrawActionDetail(action, drawId, orderId);
|
||||
const params = Object.assign(Object.assign(Object.assign({}, body), { userId: req.user.id }), actionDetail);
|
||||
const res = await this.midjourneyService.addDrawQueue(params);
|
||||
const timeout = (await this.globalConfigService.getConfigs(['mjTimeoutMs'])) || 200000;
|
||||
const job = await this.mjDrawQueue.add('mjDraw', { id: res.id, action, userId: req.user.id }, { delay: 1000, timeout: +timeout });
|
||||
this.jobIds.push(job.id);
|
||||
await this.userBalanceService.deductFromBalance(req.user.id, 'mjDraw', 4, 4);
|
||||
return;
|
||||
}
|
||||
if (action === midjourney_constant_1.MidjourneyActionEnum.ZOOM) {
|
||||
const actionDetail = await this.midjourneyService.getDrawActionDetail(action, drawId, orderId);
|
||||
const params = Object.assign(Object.assign(Object.assign({}, body), { userId: req.user.id }), actionDetail);
|
||||
const res = await this.midjourneyService.addDrawQueue(params);
|
||||
const timeout = (await this.globalConfigService.getConfigs(['mjTimeoutMs'])) || 200000;
|
||||
const job = await this.mjDrawQueue.add('mjDraw', { id: res.id, action, userId: req.user.id }, { delay: 1000, timeout: +timeout });
|
||||
this.jobIds.push(job.id);
|
||||
await this.userBalanceService.deductFromBalance(req.user.id, 'mjDraw', 4, 4);
|
||||
return;
|
||||
}
|
||||
}
|
||||
async getQueue() {
|
||||
return { jobIds: this.jobIds };
|
||||
}
|
||||
};
|
||||
QueueService = __decorate([
|
||||
__param(0, (0, bull_1.InjectQueue)('MJDRAW')),
|
||||
__metadata("design:paramtypes", [Object, midjourney_service_1.MidjourneyService,
|
||||
userBalance_service_1.UserBalanceService,
|
||||
globalConfig_service_1.GlobalConfigService])
|
||||
], QueueService);
|
||||
exports.QueueService = QueueService;
|
||||
Reference in New Issue
Block a user