mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-12-20 04:12:23 -06:00
chore: equations and adjustments for the new manager
This commit is contained in:
@@ -19,6 +19,12 @@ export class OpenaiController {
|
||||
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');
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ export class Session {
|
||||
}
|
||||
|
||||
export class OpenaiCredsDto {
|
||||
name: string;
|
||||
apiKey: string;
|
||||
}
|
||||
|
||||
|
||||
@@ -38,6 +38,16 @@ export class OpenaiRouter extends RouterBroker {
|
||||
|
||||
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,
|
||||
|
||||
@@ -32,10 +32,12 @@ export class OpenaiService {
|
||||
.then((instance) => instance.id);
|
||||
|
||||
if (!data.apiKey) throw new Error('API Key is required');
|
||||
if (!data.name) throw new Error('Name is required');
|
||||
|
||||
try {
|
||||
const creds = await this.prismaRepository.openaiCreds.create({
|
||||
data: {
|
||||
name: data.name,
|
||||
apiKey: data.apiKey,
|
||||
instanceId: instanceId,
|
||||
},
|
||||
@@ -69,6 +71,43 @@ export class OpenaiService {
|
||||
return creds;
|
||||
}
|
||||
|
||||
public async deleteCreds(instance: InstanceDto, openaiCredsId: string) {
|
||||
const instanceId = await this.prismaRepository.instance
|
||||
.findFirst({
|
||||
where: {
|
||||
name: instance.instanceName,
|
||||
},
|
||||
})
|
||||
.then((instance) => instance.id);
|
||||
|
||||
const creds = await this.prismaRepository.openaiCreds.findFirst({
|
||||
where: {
|
||||
id: openaiCredsId,
|
||||
},
|
||||
});
|
||||
|
||||
if (!creds) {
|
||||
throw new Error('Openai Creds not found');
|
||||
}
|
||||
|
||||
if (creds.instanceId !== instanceId) {
|
||||
throw new Error('Openai Creds not found');
|
||||
}
|
||||
|
||||
try {
|
||||
await this.prismaRepository.openaiCreds.delete({
|
||||
where: {
|
||||
id: openaiCredsId,
|
||||
},
|
||||
});
|
||||
|
||||
return { openaiCreds: { id: openaiCredsId } };
|
||||
} catch (error) {
|
||||
this.logger.error(error);
|
||||
throw new Error('Error deleting openai creds');
|
||||
}
|
||||
}
|
||||
|
||||
public async create(instance: InstanceDto, data: OpenaiDto) {
|
||||
const instanceId = await this.prismaRepository.instance
|
||||
.findFirst({
|
||||
@@ -98,9 +137,9 @@ export class OpenaiService {
|
||||
|
||||
if (!data.openaiCredsId) data.openaiCredsId = defaultSettingCheck?.openaiCredsId || null;
|
||||
if (!data.expire) data.expire = defaultSettingCheck?.expire || 0;
|
||||
if (!data.keywordFinish) data.keywordFinish = defaultSettingCheck?.keywordFinish || '#SAIR';
|
||||
if (!data.keywordFinish) data.keywordFinish = defaultSettingCheck?.keywordFinish || '';
|
||||
if (!data.delayMessage) data.delayMessage = defaultSettingCheck?.delayMessage || 1000;
|
||||
if (!data.unknownMessage) data.unknownMessage = defaultSettingCheck?.unknownMessage || 'Desculpe, não entendi';
|
||||
if (!data.unknownMessage) data.unknownMessage = defaultSettingCheck?.unknownMessage || '';
|
||||
if (!data.listeningFromMe) data.listeningFromMe = defaultSettingCheck?.listeningFromMe || false;
|
||||
if (!data.stopBotFromMe) data.stopBotFromMe = defaultSettingCheck?.stopBotFromMe || false;
|
||||
if (!data.keepOpen) data.keepOpen = defaultSettingCheck?.keepOpen || false;
|
||||
@@ -149,6 +188,7 @@ export class OpenaiService {
|
||||
whereDuplication = {
|
||||
...whereDuplication,
|
||||
assistantId: data.assistantId,
|
||||
botType: data.botType,
|
||||
};
|
||||
} else if (data.botType === 'chatCompletion') {
|
||||
if (!data.model) throw new Error('Model is required');
|
||||
@@ -158,6 +198,7 @@ export class OpenaiService {
|
||||
...whereDuplication,
|
||||
model: data.model,
|
||||
maxTokens: data.maxTokens,
|
||||
botType: data.botType,
|
||||
};
|
||||
} else {
|
||||
throw new Error('Bot type is required');
|
||||
|
||||
@@ -54,10 +54,11 @@ export const openaiCredsSchema: JSONSchema7 = {
|
||||
$id: v4(),
|
||||
type: 'object',
|
||||
properties: {
|
||||
name: { type: 'string' },
|
||||
apiKey: { type: 'string' },
|
||||
},
|
||||
required: ['apiKey'],
|
||||
...isNotEmpty('apiKey'),
|
||||
required: ['name', 'apiKey'],
|
||||
...isNotEmpty('name', 'apiKey'),
|
||||
};
|
||||
|
||||
export const openaiStatusSchema: JSONSchema7 = {
|
||||
|
||||
@@ -557,6 +557,7 @@ export class TypebotService {
|
||||
return await this.prismaRepository.typebotSession.findMany({
|
||||
where: {
|
||||
remoteJid: remoteJid,
|
||||
instanceId: instanceId,
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -583,6 +584,7 @@ export class TypebotService {
|
||||
await this.prismaRepository.typebotSession.deleteMany({
|
||||
where: {
|
||||
remoteJid: remoteJid,
|
||||
instanceId: instanceId,
|
||||
},
|
||||
});
|
||||
|
||||
@@ -714,6 +716,7 @@ export class TypebotService {
|
||||
where: {
|
||||
url: url,
|
||||
typebot: typebot,
|
||||
instanceId: instance.instanceId,
|
||||
},
|
||||
});
|
||||
|
||||
@@ -738,6 +741,7 @@ export class TypebotService {
|
||||
await this.prismaRepository.typebotSession.deleteMany({
|
||||
where: {
|
||||
remoteJid: remoteJid,
|
||||
instanceId: instance.instanceId,
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user