Skip to content

Instantly share code, notes, and snippets.

@josue1dario2
Created September 5, 2024 18:18
Show Gist options
  • Save josue1dario2/57c34b7d7964c40415567729707f14fe to your computer and use it in GitHub Desktop.
Save josue1dario2/57c34b7d7964c40415567729707f14fe to your computer and use it in GitHub Desktop.
app.get('/update-all-training-phrases', async (request, response) => {
try {
const { agentId } = request.query;
functions.logger.info('/update-all-training-phrases >> agentId', agentId);
if (isUndefined(agentId) || isEmpty(agentId.trim()))
return response.status(400).send('AgentId es requerido').end();
const entityTypesClient = await handleEntityTypesClient(agentId, response);
if (entityTypesClient.error) {
const { statusCode, message } = entityTypesClient.error;
return response.status(statusCode).send(message).end();
}
const entityTypes = await handleEntityTypes(agentId, entityTypesClient);
if (entityTypes.length === 0) {
return response
.status(400)
.send(`No se encontraron entidades en el agente [${agentId}]`)
.end();
}
const entityTypeMappings = mapEntityTypeArrayToObject(entityTypes);
functions.logger.info(
`/update-all-training-phrases >> [${agentId}] >> Se encontraron [${entityTypes.length}] entidades en el agente`,
);
const intentsClient = await getIntentsClient(agentId);
let intents = await handleIntents(agentId, intentsClient);
if (intents.length === 0) {
return response
.status(400)
.send(`No se encontraron intenciones en el agente [${agentId}] con los parámetros`)
.end();
}
// busco toda la info del intent para obtener las frases de entrenamiento
intents = await getFullIntentInfo(intents, intentsClient, agentId);
const allIntents = await updateIntentsTrainingPhrases(intents, entityTypeMappings);
functions.logger.info(
`/update-all-training-phrases >> [${agentId}] >> Se encontraron [${intents.length}] intenciones en el agente`,
);
await updateIntents(allIntents, agentId, intentsClient);
response.status(200).end();
} catch (error) {
functions.logger.error('/update-all-training-phrases', error.message);
response.status(500).send(errorMessage500).end();
}
});
const handleEntityTypesClient = async (agentId) => {
try {
return await getEntityTypesClient(agentId);
} catch (error) {
if (error.code === 404) {
return {
error: { statusCode: 400, message: `No se encontró un agente con ID [${agentId}]` },
};
} else {
return { error: { statusCode: 500, message: errorMessage500 } };
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment