n8n-nodes-tavilyapi
Version:
n8n Community Nodes for Tavily API integration
274 lines (271 loc) • 11.6 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TavilySearch = void 0;
const n8n_workflow_1 = require("n8n-workflow");
const tavilyApi_utils_1 = require("./tavilyApi.utils");
const ALLOWED_TIME_RANGES = ['', 'day', 'week', 'month', 'year', 'd', 'w', 'm', 'y'];
class TavilySearch {
constructor() {
this.description = {
displayName: 'Tavily Search',
name: 'tavilySearch',
icon: 'file:tavily.svg',
group: ['transform'],
version: 1,
description: 'Execute a search query using the Tavily Search endpoint',
defaults: {
name: 'Tavily Search',
},
inputs: ['main'],
outputs: ['main'],
credentials: [
{
name: 'tavilyApi',
required: true,
},
],
properties: [
{
displayName: 'Query',
name: 'query',
type: 'string',
default: '',
required: true,
description: 'The search query to execute with Tavily',
},
{
displayName: 'Topic',
name: 'topic',
type: 'options',
options: [
{ name: 'General', value: 'general' },
{ name: 'News', value: 'news' },
],
default: 'general',
description: 'The category of the search',
},
{
displayName: 'Search Depth',
name: 'searchDepth',
type: 'options',
options: [
{ name: 'Basic', value: 'basic' },
{ name: 'Advanced', value: 'advanced' },
],
default: 'basic',
description: 'Depth of the search (basic=1 credit, advanced=2 credits)',
},
{
displayName: 'Max Results',
name: 'maxResults',
type: 'number',
typeOptions: {
minValue: 0,
maxValue: 20,
},
default: 5,
description: 'Maximum number of search results to return (0-20)',
},
{
displayName: 'Time Range',
name: 'timeRange',
type: 'options',
options: [
{ name: 'None', value: '' },
{ name: 'Day', value: 'day' },
{ name: 'Week', value: 'week' },
{ name: 'Month', value: 'month' },
{ name: 'Year', value: 'year' },
{ name: 'd', value: 'd' },
{ name: 'w', value: 'w' },
{ name: 'm', value: 'm' },
{ name: 'y', value: 'y' },
],
default: '',
description: 'Time range filter (relative to current date)',
},
{
displayName: 'Days (News Only)',
name: 'days',
type: 'number',
typeOptions: {
minValue: 0,
},
default: 3,
description: 'Number of days back from the current date to include (for topic=news)',
displayOptions: {
show: {
topic: ['news'],
},
},
},
{
displayName: 'Include Answer',
name: 'includeAnswer',
type: 'options',
options: [
{ name: 'No', value: 'false' },
{ name: 'Basic', value: 'basic' },
{ name: 'Advanced', value: 'advanced' },
],
default: 'false',
description: 'Include an LLM-generated answer (basic or advanced)',
},
{
displayName: 'Include Raw Content',
name: 'includeRawContent',
type: 'boolean',
default: false,
description: 'Include cleaned and parsed HTML content of each search result',
},
{
displayName: 'Include Images',
name: 'includeImages',
type: 'boolean',
default: false,
description: 'Perform an image search and include images in the response',
},
{
displayName: 'Include Image Descriptions',
name: 'includeImageDescriptions',
type: 'boolean',
displayOptions: {
show: {
includeImages: [true],
},
},
default: false,
description: 'When including images, also add a descriptive text for each image',
},
{
displayName: 'Include Domains',
name: 'includeDomains',
type: 'string',
typeOptions: {
multipleValues: true,
multipleValueButtonText: 'Add Domain',
},
default: [],
description: 'Domains to specifically include in the search results',
},
{
displayName: 'Exclude Domains',
name: 'excludeDomains',
type: 'string',
typeOptions: {
multipleValues: true,
multipleValueButtonText: 'Add Domain',
},
default: [],
description: 'Domains to specifically exclude from the search results',
},
{
displayName: 'API Documentation',
name: 'apiDocumentationNotice',
type: 'notice',
default: '',
description: `### Tavily Search API
**Endpoint**: POST /search
**Body Parameters**:
- query (required)
- topic (general|news)
- search_depth (basic|advanced)
- max_results (0-20)
- time_range (day|week|month|year|d|w|m|y)
- days (>=0, news only)
- include_answer (false|basic|advanced)
- include_raw_content (boolean)
- include_images (boolean)
- include_image_descriptions (boolean)
- include_domains (string[])
- exclude_domains (string[])
**Response**:
- query
- answer (optional)
- images (optional)
- results
- response_time
`,
},
],
};
}
async execute() {
const items = this.getInputData();
const returnData = [];
const credentials = await this.getCredentials('tavilyApi');
if (!credentials?.apiKey) {
throw new n8n_workflow_1.NodeApiError(this.getNode(), { message: 'Missing Tavily API key in credentials.' });
}
const apiKey = credentials.apiKey;
for (let i = 0; i < items.length; i++) {
try {
const query = this.getNodeParameter('query', i);
const topic = this.getNodeParameter('topic', i);
const searchDepth = this.getNodeParameter('searchDepth', i);
const maxResults = this.getNodeParameter('maxResults', i);
const timeRange = this.getNodeParameter('timeRange', i);
const includeAnswer = this.getNodeParameter('includeAnswer', i);
const includeRawContent = this.getNodeParameter('includeRawContent', i);
const includeImages = this.getNodeParameter('includeImages', i);
let includeImageDescriptions = false;
if (includeImages) {
includeImageDescriptions = this.getNodeParameter('includeImageDescriptions', i);
}
const includeDomains = this.getNodeParameter('includeDomains', i, []);
const excludeDomains = this.getNodeParameter('excludeDomains', i, []);
// Validate parameters
if (!query?.trim()) {
throw new n8n_workflow_1.NodeApiError(this.getNode(), { message: 'Query parameter cannot be empty.' });
}
if (maxResults < 0 || maxResults > 20) {
throw new n8n_workflow_1.NodeApiError(this.getNode(), { message: 'Max Results must be between 0 and 20.' });
}
if (!ALLOWED_TIME_RANGES.includes(timeRange)) {
throw new n8n_workflow_1.NodeApiError(this.getNode(), {
message: `Invalid timeRange value. Allowed: ${ALLOWED_TIME_RANGES.join(', ')}`,
});
}
const body = {
query,
topic,
search_depth: searchDepth,
max_results: maxResults,
include_raw_content: includeRawContent,
include_images: includeImages,
include_image_descriptions: includeImageDescriptions,
};
if (timeRange) {
body.time_range = timeRange;
}
// Only get and add days parameter when topic is news
if (topic === 'news') {
const days = this.getNodeParameter('days', i);
if (days < 0) {
throw new n8n_workflow_1.NodeApiError(this.getNode(), { message: 'Days must be >= 0 when topic is "news".' });
}
body.days = days;
}
if (includeAnswer !== 'false') {
body.include_answer = includeAnswer;
}
if (Array.isArray(includeDomains) && includeDomains.length > 0) {
body.include_domains = includeDomains;
}
if (Array.isArray(excludeDomains) && excludeDomains.length > 0) {
body.exclude_domains = excludeDomains;
}
const result = await tavilyApi_utils_1.tavilyApiRequest.call(this, 'POST', '/search', body, apiKey);
returnData.push({ json: result });
}
catch (error) {
if (error instanceof n8n_workflow_1.NodeApiError) {
throw error;
}
throw new n8n_workflow_1.NodeApiError(this.getNode(), { message: String(error) });
}
}
return [returnData];
}
}
exports.TavilySearch = TavilySearch;