mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-07-14 09:51:24 -06:00
fix: send pushName for openai assistant
This commit is contained in:
parent
87a5663da7
commit
9ae5255d5f
@ -1118,7 +1118,7 @@ export class OpenaiService {
|
|||||||
}, debounceTime * 1000);
|
}, debounceTime * 1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async sendOpenai(instance: InstanceDto, remoteJid: string, msg: Message) {
|
public async sendOpenai(instance: InstanceDto, remoteJid: string, pushName: string, msg: Message) {
|
||||||
try {
|
try {
|
||||||
const settings = await this.prismaRepository.openaiSetting.findFirst({
|
const settings = await this.prismaRepository.openaiSetting.findFirst({
|
||||||
where: {
|
where: {
|
||||||
@ -1262,6 +1262,7 @@ export class OpenaiService {
|
|||||||
await this.processOpenaiAssistant(
|
await this.processOpenaiAssistant(
|
||||||
this.waMonitor.waInstances[instance.instanceName],
|
this.waMonitor.waInstances[instance.instanceName],
|
||||||
remoteJid,
|
remoteJid,
|
||||||
|
pushName,
|
||||||
findOpenai,
|
findOpenai,
|
||||||
session,
|
session,
|
||||||
settings,
|
settings,
|
||||||
@ -1285,6 +1286,7 @@ export class OpenaiService {
|
|||||||
await this.processOpenaiAssistant(
|
await this.processOpenaiAssistant(
|
||||||
this.waMonitor.waInstances[instance.instanceName],
|
this.waMonitor.waInstances[instance.instanceName],
|
||||||
remoteJid,
|
remoteJid,
|
||||||
|
pushName,
|
||||||
findOpenai,
|
findOpenai,
|
||||||
session,
|
session,
|
||||||
settings,
|
settings,
|
||||||
@ -1352,6 +1354,7 @@ export class OpenaiService {
|
|||||||
private async initAssistantNewSession(
|
private async initAssistantNewSession(
|
||||||
instance: any,
|
instance: any,
|
||||||
remoteJid: string,
|
remoteJid: string,
|
||||||
|
pushName: string,
|
||||||
openaiBot: OpenaiBot,
|
openaiBot: OpenaiBot,
|
||||||
settings: OpenaiSetting,
|
settings: OpenaiSetting,
|
||||||
session: IntegrationSession,
|
session: IntegrationSession,
|
||||||
@ -1398,7 +1401,13 @@ export class OpenaiService {
|
|||||||
|
|
||||||
await instance.client.sendPresenceUpdate('composing', remoteJid);
|
await instance.client.sendPresenceUpdate('composing', remoteJid);
|
||||||
|
|
||||||
const response = await this.getAIResponse(data.session.sessionId, runAssistant.id, openaiBot.functionUrl, remoteJid);
|
const response = await this.getAIResponse(
|
||||||
|
data.session.sessionId,
|
||||||
|
runAssistant.id,
|
||||||
|
openaiBot.functionUrl,
|
||||||
|
remoteJid,
|
||||||
|
pushName,
|
||||||
|
);
|
||||||
|
|
||||||
await instance.client.sendPresenceUpdate('paused', remoteJid);
|
await instance.client.sendPresenceUpdate('paused', remoteJid);
|
||||||
|
|
||||||
@ -1474,20 +1483,17 @@ export class OpenaiService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async getAIResponse(threadId: string, runId: string, functionUrl: string, remoteJid: string) {
|
private async getAIResponse(
|
||||||
|
threadId: string,
|
||||||
|
runId: string,
|
||||||
|
functionUrl: string,
|
||||||
|
remoteJid: string,
|
||||||
|
pushName: string,
|
||||||
|
) {
|
||||||
const getRun = await this.client.beta.threads.runs.retrieve(threadId, runId);
|
const getRun = await this.client.beta.threads.runs.retrieve(threadId, runId);
|
||||||
let toolCalls;
|
let toolCalls;
|
||||||
switch (getRun.status) {
|
switch (getRun.status) {
|
||||||
case 'requires_action':
|
case 'requires_action':
|
||||||
const contactPushName = await this.prismaRepository.contact.findFirst({
|
|
||||||
where: {
|
|
||||||
remoteJid,
|
|
||||||
},
|
|
||||||
select: {
|
|
||||||
pushName: true,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
.then((contact) => contact.pushName);
|
|
||||||
toolCalls = getRun?.required_action?.submit_tool_outputs?.tool_calls;
|
toolCalls = getRun?.required_action?.submit_tool_outputs?.tool_calls;
|
||||||
|
|
||||||
if (toolCalls) {
|
if (toolCalls) {
|
||||||
@ -1503,7 +1509,7 @@ export class OpenaiService {
|
|||||||
try {
|
try {
|
||||||
const { data } = await axios.post(functionUrl, {
|
const { data } = await axios.post(functionUrl, {
|
||||||
name: functionName,
|
name: functionName,
|
||||||
arguments: { ...functionArgument, remoteJid, contactPushName },
|
arguments: { ...functionArgument, remoteJid, pushName },
|
||||||
});
|
});
|
||||||
|
|
||||||
output = JSON.stringify(data)
|
output = JSON.stringify(data)
|
||||||
@ -1532,13 +1538,13 @@ export class OpenaiService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.getAIResponse(threadId, runId, functionUrl, remoteJid);
|
return this.getAIResponse(threadId, runId, functionUrl, remoteJid, pushName);
|
||||||
case 'queued':
|
case 'queued':
|
||||||
await new Promise((resolve) => setTimeout(resolve, 1000));
|
await new Promise((resolve) => setTimeout(resolve, 1000));
|
||||||
return this.getAIResponse(threadId, runId, functionUrl, remoteJid);
|
return this.getAIResponse(threadId, runId, functionUrl, remoteJid, pushName);
|
||||||
case 'in_progress':
|
case 'in_progress':
|
||||||
await new Promise((resolve) => setTimeout(resolve, 1000));
|
await new Promise((resolve) => setTimeout(resolve, 1000));
|
||||||
return this.getAIResponse(threadId, runId, functionUrl, remoteJid);
|
return this.getAIResponse(threadId, runId, functionUrl, remoteJid, pushName);
|
||||||
case 'completed':
|
case 'completed':
|
||||||
return await this.client.beta.threads.messages.list(threadId, {
|
return await this.client.beta.threads.messages.list(threadId, {
|
||||||
run_id: runId,
|
run_id: runId,
|
||||||
@ -1554,6 +1560,7 @@ export class OpenaiService {
|
|||||||
private async processOpenaiAssistant(
|
private async processOpenaiAssistant(
|
||||||
instance: any,
|
instance: any,
|
||||||
remoteJid: string,
|
remoteJid: string,
|
||||||
|
pushName: string,
|
||||||
openaiBot: OpenaiBot,
|
openaiBot: OpenaiBot,
|
||||||
session: IntegrationSession,
|
session: IntegrationSession,
|
||||||
settings: OpenaiSetting,
|
settings: OpenaiSetting,
|
||||||
@ -1591,13 +1598,13 @@ export class OpenaiService {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
await this.initAssistantNewSession(instance, remoteJid, openaiBot, settings, session, content);
|
await this.initAssistantNewSession(instance, remoteJid, pushName, openaiBot, settings, session, content);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!session) {
|
if (!session) {
|
||||||
await this.initAssistantNewSession(instance, remoteJid, openaiBot, settings, session, content);
|
await this.initAssistantNewSession(instance, remoteJid, pushName, openaiBot, settings, session, content);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1684,28 +1691,29 @@ export class OpenaiService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
await this.client.beta.threads.messages.create(threadId, messageData);
|
await this.client.beta.threads.messages.create(threadId, messageData);
|
||||||
const contactPushName = await this.prismaRepository.contact.findFirst({
|
const contactPushName = await this.prismaRepository.contact
|
||||||
where: {
|
.findFirst({
|
||||||
remoteJid,
|
where: {
|
||||||
},
|
remoteJid,
|
||||||
select: {
|
},
|
||||||
pushName: true,
|
select: {
|
||||||
},
|
pushName: true,
|
||||||
})
|
},
|
||||||
.then((contact) => contact.pushName);
|
})
|
||||||
|
.then((contact) => contact.pushName);
|
||||||
const runAssistant = await this.client.beta.threads.runs.create(threadId, {
|
const runAssistant = await this.client.beta.threads.runs.create(threadId, {
|
||||||
assistant_id: openaiBot.assistantId,
|
assistant_id: openaiBot.assistantId,
|
||||||
additional_instructions: `WhatsappApiInfo:
|
additional_instructions: `WhatsappApiInfo:
|
||||||
Name: ${contactPushName}
|
Name: ${contactPushName}
|
||||||
RemoteJid: ${remoteJid}
|
RemoteJid: ${remoteJid}
|
||||||
`
|
`,
|
||||||
});
|
});
|
||||||
|
|
||||||
await instance.client.presenceSubscribe(remoteJid);
|
await instance.client.presenceSubscribe(remoteJid);
|
||||||
|
|
||||||
await instance.client.sendPresenceUpdate('composing', remoteJid);
|
await instance.client.sendPresenceUpdate('composing', remoteJid);
|
||||||
|
|
||||||
const response = await this.getAIResponse(threadId, runAssistant.id, openaiBot.functionUrl, remoteJid);
|
const response = await this.getAIResponse(threadId, runAssistant.id, openaiBot.functionUrl, remoteJid, pushName);
|
||||||
|
|
||||||
await instance.client.sendPresenceUpdate('paused', remoteJid);
|
await instance.client.sendPresenceUpdate('paused', remoteJid);
|
||||||
|
|
||||||
|
@ -1244,6 +1244,7 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
await this.openaiService.sendOpenai(
|
await this.openaiService.sendOpenai(
|
||||||
{ instanceName: this.instance.name, instanceId: this.instanceId },
|
{ instanceName: this.instance.name, instanceId: this.instanceId },
|
||||||
messageRaw.key.remoteJid,
|
messageRaw.key.remoteJid,
|
||||||
|
messageRaw.pushName,
|
||||||
messageRaw,
|
messageRaw,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -2057,6 +2058,7 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
await this.openaiService.sendOpenai(
|
await this.openaiService.sendOpenai(
|
||||||
{ instanceName: this.instance.name, instanceId: this.instanceId },
|
{ instanceName: this.instance.name, instanceId: this.instanceId },
|
||||||
messageRaw.key.remoteJid,
|
messageRaw.key.remoteJid,
|
||||||
|
messageRaw.pushName,
|
||||||
messageRaw,
|
messageRaw,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -513,6 +513,7 @@ export class BusinessStartupService extends ChannelStartupService {
|
|||||||
await this.openaiService.sendOpenai(
|
await this.openaiService.sendOpenai(
|
||||||
{ instanceName: this.instance.name, instanceId: this.instanceId },
|
{ instanceName: this.instance.name, instanceId: this.instanceId },
|
||||||
messageRaw.key.remoteJid,
|
messageRaw.key.remoteJid,
|
||||||
|
pushName,
|
||||||
messageRaw,
|
messageRaw,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -961,6 +962,7 @@ export class BusinessStartupService extends ChannelStartupService {
|
|||||||
await this.openaiService.sendOpenai(
|
await this.openaiService.sendOpenai(
|
||||||
{ instanceName: this.instance.name, instanceId: this.instanceId },
|
{ instanceName: this.instance.name, instanceId: this.instanceId },
|
||||||
messageRaw.key.remoteJid,
|
messageRaw.key.remoteJid,
|
||||||
|
messageRaw.pushName,
|
||||||
messageRaw,
|
messageRaw,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user