refactor: integrations folder structure

This commit is contained in:
Davidson Gomes
2024-08-20 12:27:04 -03:00
parent 1673132c3e
commit d68d42b984
55 changed files with 99 additions and 87 deletions

View File

@@ -0,0 +1,93 @@
import { InstanceDto } from '@api/dto/instance.dto';
import { OpenaiCredsDto, OpenaiDto, OpenaiIgnoreJidDto } from '@api/integrations/chatbot/openai/dto/openai.dto';
import { OpenaiService } from '@api/integrations/chatbot/openai/services/openai.service';
import { configService, Openai } from '@config/env.config';
import { BadRequestException } from '@exceptions';
export class OpenaiController {
constructor(private readonly openaiService: OpenaiService) {}
public async createOpenaiCreds(instance: InstanceDto, data: OpenaiCredsDto) {
if (!configService.get<Openai>('OPENAI').ENABLED) throw new BadRequestException('Openai is disabled');
return this.openaiService.createCreds(instance, data);
}
public async findOpenaiCreds(instance: InstanceDto) {
if (!configService.get<Openai>('OPENAI').ENABLED) throw new BadRequestException('Openai is disabled');
return this.openaiService.findCreds(instance);
}
public async deleteCreds(instance: InstanceDto, openaiCredsId: string) {
if (!configService.get<Openai>('OPENAI').ENABLED) throw new BadRequestException('Openai is disabled');
return this.openaiService.deleteCreds(instance, openaiCredsId);
}
public async createOpenai(instance: InstanceDto, data: OpenaiDto) {
if (!configService.get<Openai>('OPENAI').ENABLED) throw new BadRequestException('Openai is disabled');
return this.openaiService.create(instance, data);
}
public async findOpenai(instance: InstanceDto) {
if (!configService.get<Openai>('OPENAI').ENABLED) throw new BadRequestException('Openai is disabled');
return this.openaiService.find(instance);
}
public async fetchOpenai(instance: InstanceDto, openaiBotId: string) {
if (!configService.get<Openai>('OPENAI').ENABLED) throw new BadRequestException('Openai is disabled');
return this.openaiService.fetch(instance, openaiBotId);
}
public async updateOpenai(instance: InstanceDto, openaiBotId: string, data: OpenaiDto) {
if (!configService.get<Openai>('OPENAI').ENABLED) throw new BadRequestException('Openai is disabled');
return this.openaiService.update(instance, openaiBotId, data);
}
public async deleteOpenai(instance: InstanceDto, openaiBotId: string) {
if (!configService.get<Openai>('OPENAI').ENABLED) throw new BadRequestException('Openai is disabled');
return this.openaiService.delete(instance, openaiBotId);
}
public async settings(instance: InstanceDto, data: any) {
if (!configService.get<Openai>('OPENAI').ENABLED) throw new BadRequestException('Openai is disabled');
return this.openaiService.setDefaultSettings(instance, data);
}
public async fetchSettings(instance: InstanceDto) {
if (!configService.get<Openai>('OPENAI').ENABLED) throw new BadRequestException('Openai is disabled');
return this.openaiService.fetchDefaultSettings(instance);
}
public async changeStatus(instance: InstanceDto, data: any) {
if (!configService.get<Openai>('OPENAI').ENABLED) throw new BadRequestException('Openai is disabled');
return this.openaiService.changeStatus(instance, data);
}
public async fetchSessions(instance: InstanceDto, openaiBotId: string) {
if (!configService.get<Openai>('OPENAI').ENABLED) throw new BadRequestException('Openai is disabled');
return this.openaiService.fetchSessions(instance, openaiBotId);
}
public async getModels(instance: InstanceDto) {
if (!configService.get<Openai>('OPENAI').ENABLED) throw new BadRequestException('Openai is disabled');
return this.openaiService.getModels(instance);
}
public async ignoreJid(instance: InstanceDto, data: OpenaiIgnoreJidDto) {
if (!configService.get<Openai>('OPENAI').ENABLED) throw new BadRequestException('Openai is disabled');
return this.openaiService.ignoreJid(instance, data);
}
}

View File

@@ -0,0 +1,60 @@
import { TriggerOperator, TriggerType } from '@prisma/client';
export class Session {
remoteJid?: string;
sessionId?: string;
status?: string;
createdAt?: number;
updateAt?: number;
}
export class OpenaiCredsDto {
name: string;
apiKey: string;
}
export class OpenaiDto {
enabled?: boolean;
description?: string;
openaiCredsId: string;
botType?: string;
assistantId?: string;
functionUrl?: string;
model?: string;
systemMessages?: string[];
assistantMessages?: string[];
userMessages?: string[];
maxTokens?: number;
expire?: number;
keywordFinish?: string;
delayMessage?: number;
unknownMessage?: string;
listeningFromMe?: boolean;
stopBotFromMe?: boolean;
keepOpen?: boolean;
debounceTime?: number;
triggerType?: TriggerType;
triggerOperator?: TriggerOperator;
triggerValue?: string;
ignoreJids?: any;
}
export class OpenaiSettingDto {
openaiCredsId?: string;
expire?: number;
keywordFinish?: string;
delayMessage?: number;
unknownMessage?: string;
listeningFromMe?: boolean;
stopBotFromMe?: boolean;
keepOpen?: boolean;
debounceTime?: number;
openaiIdFallback?: string;
ignoreJids?: any;
speechToText?: boolean;
}
export class OpenaiIgnoreJidDto {
remoteJid?: string;
action?: string;
}

View File

@@ -0,0 +1,168 @@
import { RouterBroker } from '@api/abstract/abstract.router';
import { InstanceDto } from '@api/dto/instance.dto';
import {
OpenaiCredsDto,
OpenaiDto,
OpenaiIgnoreJidDto,
OpenaiSettingDto,
} from '@api/integrations/chatbot/openai/dto/openai.dto';
import { HttpStatus } from '@api/routes/index.router';
import { openaiController } from '@api/server.module';
import {
instanceSchema,
openaiCredsSchema,
openaiIgnoreJidSchema,
openaiSchema,
openaiSettingSchema,
openaiStatusSchema,
} from '@validate/validate.schema';
import { RequestHandler, Router } from 'express';
export class OpenaiRouter extends RouterBroker {
constructor(...guards: RequestHandler[]) {
super();
this.router
.post(this.routerPath('creds'), ...guards, async (req, res) => {
const response = await this.dataValidate<OpenaiCredsDto>({
request: req,
schema: openaiCredsSchema,
ClassRef: OpenaiCredsDto,
execute: (instance, data) => openaiController.createOpenaiCreds(instance, data),
});
res.status(HttpStatus.CREATED).json(response);
})
.get(this.routerPath('creds'), ...guards, async (req, res) => {
const response = await this.dataValidate<InstanceDto>({
request: req,
schema: instanceSchema,
ClassRef: InstanceDto,
execute: (instance) => openaiController.findOpenaiCreds(instance),
});
res.status(HttpStatus.OK).json(response);
})
.delete(this.routerPath('creds/:openaiCredsId'), ...guards, async (req, res) => {
const response = await this.dataValidate<InstanceDto>({
request: req,
schema: instanceSchema,
ClassRef: InstanceDto,
execute: (instance) => openaiController.deleteCreds(instance, req.params.openaiCredsId),
});
res.status(HttpStatus.OK).json(response);
})
.post(this.routerPath('create'), ...guards, async (req, res) => {
const response = await this.dataValidate<OpenaiDto>({
request: req,
schema: openaiSchema,
ClassRef: OpenaiDto,
execute: (instance, data) => openaiController.createOpenai(instance, data),
});
res.status(HttpStatus.CREATED).json(response);
})
.get(this.routerPath('find'), ...guards, async (req, res) => {
const response = await this.dataValidate<InstanceDto>({
request: req,
schema: instanceSchema,
ClassRef: InstanceDto,
execute: (instance) => openaiController.findOpenai(instance),
});
res.status(HttpStatus.OK).json(response);
})
.get(this.routerPath('fetch/:openaiBotId'), ...guards, async (req, res) => {
const response = await this.dataValidate<InstanceDto>({
request: req,
schema: instanceSchema,
ClassRef: InstanceDto,
execute: (instance) => openaiController.fetchOpenai(instance, req.params.openaiBotId),
});
res.status(HttpStatus.OK).json(response);
})
.put(this.routerPath('update/:openaiBotId'), ...guards, async (req, res) => {
const response = await this.dataValidate<OpenaiDto>({
request: req,
schema: openaiSchema,
ClassRef: OpenaiDto,
execute: (instance, data) => openaiController.updateOpenai(instance, req.params.openaiBotId, data),
});
res.status(HttpStatus.OK).json(response);
})
.delete(this.routerPath('delete/:openaiBotId'), ...guards, async (req, res) => {
const response = await this.dataValidate<InstanceDto>({
request: req,
schema: instanceSchema,
ClassRef: InstanceDto,
execute: (instance) => openaiController.deleteOpenai(instance, req.params.openaiBotId),
});
res.status(HttpStatus.OK).json(response);
})
.post(this.routerPath('settings'), ...guards, async (req, res) => {
const response = await this.dataValidate<OpenaiSettingDto>({
request: req,
schema: openaiSettingSchema,
ClassRef: OpenaiSettingDto,
execute: (instance, data) => openaiController.settings(instance, data),
});
res.status(HttpStatus.OK).json(response);
})
.get(this.routerPath('fetchSettings'), ...guards, async (req, res) => {
const response = await this.dataValidate<InstanceDto>({
request: req,
schema: instanceSchema,
ClassRef: InstanceDto,
execute: (instance) => openaiController.fetchSettings(instance),
});
res.status(HttpStatus.OK).json(response);
})
.post(this.routerPath('changeStatus'), ...guards, async (req, res) => {
const response = await this.dataValidate<InstanceDto>({
request: req,
schema: openaiStatusSchema,
ClassRef: InstanceDto,
execute: (instance, data) => openaiController.changeStatus(instance, data),
});
res.status(HttpStatus.OK).json(response);
})
.get(this.routerPath('fetchSessions/:openaiBotId'), ...guards, async (req, res) => {
const response = await this.dataValidate<InstanceDto>({
request: req,
schema: instanceSchema,
ClassRef: InstanceDto,
execute: (instance) => openaiController.fetchSessions(instance, req.params.openaiBotId),
});
res.status(HttpStatus.OK).json(response);
})
.post(this.routerPath('ignoreJid'), ...guards, async (req, res) => {
const response = await this.dataValidate<OpenaiIgnoreJidDto>({
request: req,
schema: openaiIgnoreJidSchema,
ClassRef: OpenaiIgnoreJidDto,
execute: (instance, data) => openaiController.ignoreJid(instance, data),
});
res.status(HttpStatus.OK).json(response);
})
.get(this.routerPath('getModels'), ...guards, async (req, res) => {
const response = await this.dataValidate<InstanceDto>({
request: req,
schema: instanceSchema,
ClassRef: InstanceDto,
execute: (instance) => openaiController.getModels(instance),
});
res.status(HttpStatus.OK).json(response);
});
}
public readonly router: Router = Router();
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,129 @@
import { JSONSchema7 } from 'json-schema';
import { v4 } from 'uuid';
const isNotEmpty = (...propertyNames: string[]): JSONSchema7 => {
const properties = {};
propertyNames.forEach(
(property) =>
(properties[property] = {
minLength: 1,
description: `The "${property}" cannot be empty`,
}),
);
return {
if: {
propertyNames: {
enum: [...propertyNames],
},
},
then: { properties },
};
};
export const openaiSchema: JSONSchema7 = {
$id: v4(),
type: 'object',
properties: {
enabled: { type: 'boolean' },
description: { type: 'string' },
openaiCredsId: { type: 'string' },
botType: { type: 'string', enum: ['assistant', 'chatCompletion'] },
assistantId: { type: 'string' },
functionUrl: { type: 'string' },
model: { type: 'string' },
systemMessages: { type: 'array', items: { type: 'string' } },
assistantMessages: { type: 'array', items: { type: 'string' } },
userMessages: { type: 'array', items: { type: 'string' } },
maxTokens: { type: 'integer' },
triggerType: { type: 'string', enum: ['all', 'keyword', 'none', 'advanced'] },
triggerOperator: { type: 'string', enum: ['equals', 'contains', 'startsWith', 'endsWith', 'regex'] },
triggerValue: { type: 'string' },
expire: { type: 'integer' },
keywordFinish: { type: 'string' },
delayMessage: { type: 'integer' },
unknownMessage: { type: 'string' },
listeningFromMe: { type: 'boolean' },
stopBotFromMe: { type: 'boolean' },
keepOpen: { type: 'boolean' },
debounceTime: { type: 'integer' },
ignoreJids: { type: 'array', items: { type: 'string' } },
},
required: ['enabled', 'openaiCredsId', 'botType', 'triggerType'],
...isNotEmpty('enabled', 'openaiCredsId', 'botType', 'triggerType'),
};
export const openaiCredsSchema: JSONSchema7 = {
$id: v4(),
type: 'object',
properties: {
name: { type: 'string' },
apiKey: { type: 'string' },
},
required: ['name', 'apiKey'],
...isNotEmpty('name', 'apiKey'),
};
export const openaiStatusSchema: JSONSchema7 = {
$id: v4(),
type: 'object',
properties: {
remoteJid: { type: 'string' },
status: { type: 'string', enum: ['opened', 'closed', 'paused', 'delete'] },
},
required: ['remoteJid', 'status'],
...isNotEmpty('remoteJid', 'status'),
};
export const openaiSettingSchema: JSONSchema7 = {
$id: v4(),
type: 'object',
properties: {
openaiCredsId: { type: 'string' },
expire: { type: 'integer' },
keywordFinish: { type: 'string' },
delayMessage: { type: 'integer' },
unknownMessage: { type: 'string' },
listeningFromMe: { type: 'boolean' },
stopBotFromMe: { type: 'boolean' },
keepOpen: { type: 'boolean' },
debounceTime: { type: 'integer' },
speechToText: { type: 'boolean' },
ignoreJids: { type: 'array', items: { type: 'string' } },
openaiIdFallback: { type: 'string' },
},
required: [
'openaiCredsId',
'expire',
'keywordFinish',
'delayMessage',
'unknownMessage',
'listeningFromMe',
'stopBotFromMe',
'keepOpen',
'debounceTime',
'ignoreJids',
],
...isNotEmpty(
'openaiCredsId',
'expire',
'keywordFinish',
'delayMessage',
'unknownMessage',
'listeningFromMe',
'stopBotFromMe',
'keepOpen',
'debounceTime',
'ignoreJids',
),
};
export const openaiIgnoreJidSchema: JSONSchema7 = {
$id: v4(),
type: 'object',
properties: {
remoteJid: { type: 'string' },
action: { type: 'string', enum: ['add', 'remove'] },
},
required: ['remoteJid', 'action'],
...isNotEmpty('remoteJid', 'action'),
};