n8n-nodes-base
Version:
Base nodes of n8n
60 lines • 3.42 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.embeddingsErrorPostReceive = exports.searchErrorPostReceive = exports.agentErrorPostReceive = void 0;
exports.sendErrorPostReceive = sendErrorPostReceive;
exports.getAgentModels = getAgentModels;
const n8n_workflow_1 = require("n8n-workflow");
async function sendErrorPostReceive(data, response) {
if (String(response.statusCode).startsWith('4') || String(response.statusCode).startsWith('5')) {
const errorBody = response.body;
const error = (errorBody?.error ?? {});
const errorMessage = typeof error.message === 'string'
? error.message
: (response.statusMessage ?? 'An unexpected issue occurred');
const errorType = typeof error.type === 'string' ? error.type : 'UnknownError';
const itemIndex = typeof error.itemIndex === 'number' ? `[Item ${error.itemIndex}]` : '';
if (errorType === 'invalid_model') {
throw new n8n_workflow_1.NodeApiError(this.getNode(), errorBody, {
message: 'Invalid model',
description: 'The model is not valid. Permitted models can be found in the documentation at https://docs.perplexity.ai/guides/model-cards.',
});
}
// Fallback for other errors
throw new n8n_workflow_1.NodeApiError(this.getNode(), response, {
message: `${errorMessage}${itemIndex ? ' ' + itemIndex : ''}.`,
description: 'Any optional system messages must be sent first, followed by alternating user and assistant messages. For more details, refer to the API documentation: https://docs.perplexity.ai/api-reference/chat-completions',
});
}
return data;
}
const createErrorHandler = (description) => async function (data, { statusCode, body, statusMessage }) {
if (statusCode >= 400 && statusCode <= 599) {
const errorBody = body;
const error = (errorBody?.error ?? errorBody);
const errorMessage = typeof error.message === 'string'
? error.message
: (statusMessage ?? 'An unexpected issue occurred');
throw new n8n_workflow_1.NodeApiError(this.getNode(), errorBody, { message: errorMessage, description });
}
return data;
};
exports.agentErrorPostReceive = createErrorHandler('Refer to the Agent API documentation at https://docs.perplexity.ai/api-reference/agent-post for valid parameters.');
exports.searchErrorPostReceive = createErrorHandler('Refer to the Search API documentation at https://docs.perplexity.ai/api-reference/search-post for valid parameters.');
exports.embeddingsErrorPostReceive = createErrorHandler('Refer to the Embeddings API documentation at https://docs.perplexity.ai/api-reference/embeddings-post for valid parameters.');
async function getAgentModels(filter = '') {
const response = (await this.helpers.requestWithAuthentication.call(this, 'perplexityApi', {
method: 'GET',
url: 'https://api.perplexity.ai/v1/models',
json: true,
}));
const models = response.data ?? [];
const results = models
.map((model) => ({
name: model.id,
value: model.id,
url: 'https://docs.perplexity.ai/docs/agent-api/models',
}))
.filter((item) => !filter || item.name.toLowerCase().includes(filter.toLowerCase()));
return { results };
}
//# sourceMappingURL=GenericFunctions.js.map