n8n-nodes-openrouter
Version:
n8n node for OpenRouter API integration
250 lines (249 loc) • 11.4 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.OpenRouter = void 0;
const n8n_workflow_1 = require("n8n-workflow");
class OpenRouter {
constructor() {
this.description = {
displayName: 'OpenRouter',
name: 'openRouter',
icon: 'file:openrouter.svg',
group: ['transform'],
version: 1,
subtitle: '={{$parameter["operation"]}}',
description: 'Interact with OpenRouter API',
defaults: {
name: 'OpenRouter',
},
inputs: '={{["main"]}}',
outputs: '={{["main"]}}',
credentials: [
{
name: 'openRouterApi',
required: true,
},
],
properties: [
{
displayName: 'Operation',
name: 'operation',
type: 'options',
noDataExpression: true,
options: [
{
name: 'Chat',
value: 'chat',
description: 'Send a chat message',
action: 'Send a chat message',
},
],
default: 'chat',
},
{
displayName: 'Model Name or ID',
name: 'model',
type: 'options',
noDataExpression: true,
typeOptions: {
loadOptionsMethod: 'getModels',
},
required: true,
default: '',
description: 'Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code/expressions/">expression</a>',
},
{
displayName: 'System Prompt',
name: 'system_prompt',
type: 'string',
typeOptions: {
rows: 4,
},
default: '',
description: 'System message to set the behavior of the assistant',
placeholder: 'You are a helpful assistant...',
},
{
displayName: 'Message',
name: 'message',
type: 'string',
typeOptions: {
rows: 4,
},
default: '',
description: 'The message to send to the chat model',
required: true,
},
{
displayName: 'Temperature',
name: 'temperature',
type: 'number',
default: 0.9,
description: 'What sampling temperature to use',
},
{
displayName: 'Additional Fields',
name: 'additionalFields',
type: 'collection',
placeholder: 'Add Field',
default: {},
options: [
{
displayName: 'Frequency Penalty',
name: 'frequency_penalty',
type: 'number',
default: 0,
description: 'Number between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency.',
},
{
displayName: 'Max Tokens',
name: 'max_tokens',
type: 'number',
default: 1000,
description: 'The maximum number of tokens to generate',
},
{
displayName: 'Presence Penalty',
name: 'presence_penalty',
type: 'number',
default: 0,
description: 'Number between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far.',
},
{
displayName: 'Top P',
name: 'top_p',
type: 'number',
default: 1,
description: 'An alternative to sampling with temperature, called nucleus sampling',
},
],
},
],
};
this.methods = {
loadOptions: {
async getModels() {
const credentials = await this.getCredentials('openRouterApi');
const options = {
url: 'https://openrouter.ai/api/v1/models',
headers: {
Authorization: `Bearer ${credentials.apiKey}`,
'HTTP-Referer': 'https://github.com/MatthewSabia/n8n-nodes-openrouter',
'X-Title': 'n8n OpenRouter Node',
'Content-Type': 'application/json',
},
method: 'GET',
json: true,
};
try {
const response = await this.helpers.request(options);
if (!(response === null || response === void 0 ? void 0 : response.data) || !Array.isArray(response.data)) {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Invalid response format from OpenRouter API');
}
const truncateAndAddPricing = (model) => {
const originalDescription = model.description || '';
const truncatedDescription = originalDescription.slice(0, Math.floor(originalDescription.length / 2));
const pricing = `Price: $${parseFloat(model.pricing.prompt) * 1000000}/1M tokens (prompt), $${parseFloat(model.pricing.completion) * 1000000}/1M tokens (completion)`;
const combinedDescription = `${truncatedDescription} ${pricing}`.trim();
return combinedDescription.length > originalDescription.length
? combinedDescription.slice(0, originalDescription.length - 3) + '...'
: combinedDescription;
};
const models = response.data
.filter((model) => model.id && model.name)
.map((model) => ({
name: model.name,
value: model.id,
description: truncateAndAddPricing(model),
}))
.sort((a, b) => a.name.localeCompare(b.name));
if (models.length === 0) {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'No models found in OpenRouter API response');
}
return models;
}
catch (error) {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Failed to load models: ${error.message}`);
}
},
},
};
}
async execute() {
var _a, _b, _c;
const items = this.getInputData();
const returnData = [];
const credentials = await this.getCredentials('openRouterApi');
if (!(credentials === null || credentials === void 0 ? void 0 : credentials.apiKey)) {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'No valid API key provided');
}
for (let i = 0; i < items.length; i++) {
try {
const operation = this.getNodeParameter('operation', i);
const model = this.getNodeParameter('model', i);
const systemPrompt = this.getNodeParameter('system_prompt', i, '');
const message = this.getNodeParameter('message', i);
const temperature = this.getNodeParameter('temperature', i);
const additionalFields = this.getNodeParameter('additionalFields', i);
if (operation === 'chat') {
const messages = [];
// Add system message if provided
if (systemPrompt) {
messages.push({
role: 'system',
content: systemPrompt,
});
}
// Add user message
messages.push({
role: 'user',
content: message,
});
const requestBody = {
model,
messages,
temperature,
...additionalFields,
};
const options = {
url: 'https://openrouter.ai/api/v1/chat/completions',
headers: {
Authorization: `Bearer ${credentials.apiKey}`,
'HTTP-Referer': 'https://github.com/MatthewSabia/n8n-nodes-openrouter',
'X-Title': 'n8n OpenRouter Node',
'Content-Type': 'application/json',
},
method: 'POST',
body: requestBody,
json: true,
};
const response = await this.helpers.request(options);
if (!((_c = (_b = (_a = response === null || response === void 0 ? void 0 : response.choices) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.message) === null || _c === void 0 ? void 0 : _c.content)) {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Invalid response format from OpenRouter API');
}
const typedResponse = response;
const messageContent = typedResponse.choices[0].message.content.trim();
returnData.push({
json: {
response: messageContent,
},
pairedItem: { item: i },
});
}
}
catch (error) {
if (this.continueOnFail()) {
returnData.push({
json: {
error: error.message,
},
pairedItem: { item: i },
});
continue;
}
throw error;
}
}
return [returnData];
}
}
exports.OpenRouter = OpenRouter;