Skip to content

Commit d32d6a5

Browse files
committed
Credenciais default openai
1 parent 205ea81 commit d32d6a5

File tree

1 file changed

+62
-1
lines changed

1 file changed

+62
-1
lines changed

src/api/integrations/chatbot/openai/controllers/openai.controller.ts

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,9 @@ export class OpenaiController extends BaseChatbotController<OpenaiBot, OpenaiDto
267267
},
268268
});
269269

270+
// Automatically set this credential as default in settings
271+
await this.setCredentialAsDefault(instanceId, creds.id);
272+
270273
return creds;
271274
} catch (error) {
272275
this.logger.error(error);
@@ -297,6 +300,64 @@ export class OpenaiController extends BaseChatbotController<OpenaiBot, OpenaiDto
297300
return creds;
298301
}
299302

303+
private async setCredentialAsDefault(instanceId: string, credentialId: string) {
304+
try {
305+
// Check if settings already exist for this instance
306+
const existingSettings = await this.settingsRepository.findFirst({
307+
where: {
308+
instanceId: instanceId,
309+
},
310+
});
311+
312+
if (existingSettings) {
313+
// Update existing settings to use the new credential as default
314+
await this.settingsRepository.update({
315+
where: {
316+
id: existingSettings.id,
317+
},
318+
data: {
319+
OpenaiCreds: {
320+
connect: {
321+
id: credentialId,
322+
},
323+
},
324+
},
325+
});
326+
} else {
327+
// Create new settings with the credential as default
328+
await this.settingsRepository.create({
329+
data: {
330+
expire: 0,
331+
keywordFinish: null,
332+
delayMessage: 1000,
333+
unknownMessage: 'Sorry, I dont understand',
334+
listeningFromMe: true,
335+
stopBotFromMe: true,
336+
keepOpen: false,
337+
debounceTime: 1,
338+
ignoreJids: [],
339+
splitMessages: false,
340+
timePerChar: 50,
341+
speechToText: true,
342+
Instance: {
343+
connect: {
344+
id: instanceId,
345+
},
346+
},
347+
OpenaiCreds: {
348+
connect: {
349+
id: credentialId,
350+
},
351+
},
352+
},
353+
});
354+
}
355+
} catch (error) {
356+
this.logger.error(`Error setting credential as default: ${error}`);
357+
// Don't throw here to avoid breaking the credential creation flow
358+
}
359+
}
360+
300361
public async deleteCreds(instance: InstanceDto, openaiCredsId: string) {
301362
if (!this.integrationEnabled) throw new BadRequestException('Openai is disabled');
302363

@@ -379,7 +440,7 @@ export class OpenaiController extends BaseChatbotController<OpenaiBot, OpenaiDto
379440
},
380441
}
381442
: undefined,
382-
speechToText: data.speechToText,
443+
speechToText: data.speechToText !== undefined ? data.speechToText : true,
383444
};
384445

385446
if (existingSettings) {

0 commit comments

Comments
 (0)