Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

5.3 KB
Raw Blame
import { createAction, Property } from '@activepieces/pieces-framework';
import { AuthenticationType, httpClient, HttpMethod } from '@activepieces/pieces-common';
import { tavilyAuth } from '../auth';
import { searchOutputSchema } from '../output-schemas';

export const searchAction = createAction({
  name: 'search',
  outputSchema: searchOutputSchema,
  classification: 'SEARCH',
  displayName: 'Search',
  description: 'Search for data based on a query.',
  audience: 'both',
  aiMetadata: { description: 'Run a web search through the Tavily search engine for a natural-language query and return relevant results; tune scope with search depth, a general vs. news topic, recency windows, and domain include/exclude lists, and optionally include images, raw page content, or an LLM-generated answer summary. Use to look up current or external information from the web. Read-only and idempotent: the same query returns equivalent results without side effects, though live web data may shift over time.', idempotent: true },
  auth: tavilyAuth,
  props: {
    query: Property.LongText({
      displayName: 'Search Query',
      description: 'The search query you want to execute with Tavily.',
      required: true,
    }),
    search_depth: Property.StaticDropdown({
      displayName: 'Search Depth',
      description: 'The depth of the search. It can be "basic" or "advanced".',
      required: false,
      defaultValue: 'basic',
      options: {
        options: [
          { label: 'Basic', value: 'basic' },
          { label: 'Advanced', value: 'advanced' },
        ],
      },
    }),
    topic: Property.StaticDropdown({
      displayName: 'Topic',
      description: 'The category of the search. This will determine which of our agents will be used for the search.',
      required: false,
      defaultValue: 'general',
      options: {
        options: [
          { label: 'General', value: 'general' },
          { label: 'News', value: 'news' },
        ],
      },
    }),
    days: Property.Number({
      displayName: 'Days',
      description: 'The number of days back from the current date to include in the search results. Only available when using the "news" search topic. Default is 3.',
      required: false,
      defaultValue: 3,
    }),
    time_range: Property.StaticDropdown({
      displayName: 'Time Range',
      description: 'The time range back from the current date to include in the search results.',
      required: false,
      options: {
        options: [
          { label: 'Day', value: 'day' },
          { label: 'Week', value: 'week' },
          { label: 'Month', value: 'month' },
          { label: 'Year', value: 'year' },
          { label: 'Day (Short)', value: 'd' },
          { label: 'Week (Short)', value: 'w' },
          { label: 'Month (Short)', value: 'm' },
          { label: 'Year (Short)', value: 'y' },
        ],
      },
    }),
    max_results: Property.Number({
      displayName: 'Maximum Results',
      description: 'The maximum number of search results to return.',
      required: false,
      defaultValue: 5,
    }),
    include_images: Property.Checkbox({
      displayName: 'Include Images',
      description: 'Include a list of query-related images in the response.',
      required: false,
      defaultValue: false,
    }),
    include_image_descriptions: Property.Checkbox({
      displayName: 'Include Image Descriptions',
      description: 'When include_images is set to True, this option adds descriptive text for each image.',
      required: false,
      defaultValue: false,
    }),
    include_answer: Property.Checkbox({
      displayName: 'Include Answer',
      description: 'Include a short answer to original query, generated by an LLM based on Tavily\'s search results.',
      required: false,
      defaultValue: false,
    }),
    include_raw_content: Property.Checkbox({
      displayName: 'Include Raw Content',
      description: 'Include the cleaned and parsed HTML content of each search result.',
      required: false,
      defaultValue: false,
    }),
    include_domains: Property.Array({
      displayName: 'Include Domains',
      description: 'A list of domains to specifically include in the search results.',
      required: false,
    }),
    exclude_domains: Property.Array({
      displayName: 'Exclude Domains',
      description: 'A list of domains to specifically exclude from the search results.',
      required: false,
    }),
  },
  async run({ auth, propsValue }) {
    const response = await httpClient.sendRequest({
      method: HttpMethod.POST,
      url: 'https://api.tavily.com/search',
      authentication: {
        type: AuthenticationType.BEARER_TOKEN,
        token: auth.secret_text
      },
      headers: {
        'Content-Type': 'application/json',
      },
      body: {
        query: propsValue.query,
        search_depth: propsValue.search_depth,
        topic: propsValue.topic,
        days: propsValue.days,
        time_range: propsValue.time_range,
        max_results: propsValue.max_results,
        include_images: propsValue.include_images,
        include_image_descriptions: propsValue.include_image_descriptions,
        include_answer: propsValue.include_answer,
        include_raw_content: propsValue.include_raw_content,
        include_domains: propsValue.include_domains,
        exclude_domains: propsValue.exclude_domains,
      },
    });

    return response.body;
  },
});