Some checks failed
App Prod-Parity E2E Dispatch / dispatch (push) Failing after 10s
CD Storybook UI / build (push) Failing after 3m25s
CD Storybook UI / dispatch (push) Has been skipped
CD deploy main / website-changed-files (push) Successful in 18m8s
CI Agent Skills / changed-files-check (push) Successful in 18m9s
CI Create App E2E minimal / changed-files-check (push) Successful in 24s
CI Create App / changed-files-check (push) Successful in 26s
CI Docs / changed-files-check (push) Successful in 26s
CI Emails / changed-files-check (push) Successful in 28s
CI Example App Hello World / changed-files-check (push) Successful in 23s
CI Front / changed-files-check (push) Has been skipped
CI Example App Postcard / changed-files-check (push) Successful in 27s
CI UI / changed-files-check (push) Has been skipped
CI Utils Test / changed-files-check (push) Successful in 45s
CI Twenty Apps / discover (push) Successful in 1m3s
CD deploy main / deploy-main (push) Failing after 3s
CI Agent Skills / agent-skills-validate (push) Has been skipped
CI Create App / create-app-test (lint) (push) Has been skipped
CI Emails / emails-test (push) Has been skipped
CI Front / front-task (lingui:compile) (push) Has been skipped
CI Front / front-task (lint) (push) Has been skipped
CI Example App Postcard / example-app-postcard (push) Has been skipped
CI UI / ui-task (lint) (push) Has been skipped
CI UI / ui-task (pack-size) (push) Has been skipped
CI UI / ui-task (size) (push) Has been skipped
CI UI / ui-task (test) (push) Has been skipped
CI UI / ui-task (typecheck) (push) Has been skipped
CI Utils Test / utils-test (push) Has been skipped
CI Twenty Apps / ${{ matrix.app.name }} (push) Has been skipped
CI Emails / ci-emails-status-check (push) Successful in 2s
CI Example App Hello World / ci-example-app-hello-world-status-check (push) Successful in 2s
CI Example App Postcard / ci-example-app-postcard-status-check (push) Successful in 3s
CI UI / ui-sb-test (push) Has been skipped
CI Utils Test / ci-utils-test-status-check (push) Successful in 2s
CI Twenty Apps / ci-twenty-apps-status-check (push) Successful in 2s
CI UI / ci-ui-status-check (push) Successful in 2s
CI Desktop / desktop (push) Failing after 1m8s
Push docs to Crowdin / Push documentation to Crowdin (push) Failing after 55s
Push translations to Crowdin / Extract and upload translations (push) Failing after 5s
Push website translations to Crowdin / Extract and upload website translations (push) Failing after 5s
CI Create App / create-app-test (test) (push) Has been skipped
CI Create App / create-app-test (typecheck) (push) Has been skipped
CI Docs / docs-lint (push) Has been skipped
CI Example App Hello World / example-app-hello-world (push) Has been skipped
CI Front / front-task (lingui:extract) (push) Has been skipped
CI Front / front-task (test) (push) Has been skipped
CI Front / front-task (typecheck) (push) Has been skipped
CI UI / ui-sb-build (push) Failing after 1m32s
CI Twenty Apps / ${{ matrix.app.name }} (${{ matrix.server-source }}) (push) Has been skipped
CI Agent Skills / agent-skills-publish (push) Has been skipped
CI Create App / ci-create-app-status-check (push) Successful in 2s
CI Agent Skills / agent-skills-remote-source (push) Has been skipped
CI Create App E2E minimal / create-app-e2e-minimal (push) Has been cancelled
CI E2E Main / e2e-test (push) Has been cancelled
CI Front / front-sb-build (push) Has been cancelled
CI Front / front-build (push) Has been cancelled
CI Create App E2E minimal / ci-create-app-e2e-minimal-status-check (push) Has been cancelled
CI E2E Main / ci-e2e-main-status-check (push) Has been cancelled
CI E2E Main / notify-main-ci-failure (push) Has been cancelled
CI E2E Main / qa-scout-comment (push) Has been cancelled
CI Front / front-sb-test (push) Has been cancelled
CI Front / front-sb-screenshots (push) Has been cancelled
CI Front / ci-front-status-check (push) Has been cancelled
DPA Sub-Processor Sync / sync-subprocessors (push) Failing after 3s
AI Catalog Sync / sync-catalog (push) Failing after 3s
119 lines
3.8 KiB
TypeScript
119 lines
3.8 KiB
TypeScript
import Exa from 'exa-js';
|
|
import { chargeCredits } from 'twenty-sdk/billing';
|
|
import { defineLogicFunction } from 'twenty-sdk/define';
|
|
|
|
import { DEFAULT_NUM_RESULTS } from './constants/default-num-results.constant';
|
|
import { exaWebSearchInputSchema } from './schemas/exa-web-search-input.schema';
|
|
import { type ExaWebSearchInput } from './types/exa-web-search-input.type';
|
|
|
|
// Number of sentences surfaced per result — keeps the snippet compact
|
|
// enough for an LLM to read many results without blowing the context.
|
|
const HIGHLIGHT_NUM_SENTENCES = 5;
|
|
|
|
// Inner bound — the runtime's `timeoutSeconds: 30` is the outer kill
|
|
// switch; this one ensures we return a clean error on a slow Exa response.
|
|
const EXA_SEARCH_TIMEOUT_MS = 25_000;
|
|
|
|
// Exa auto-search pricing (2025): $0.007 covers the first 10 results,
|
|
// $0.001 per additional result. Twenty charges in micro-credits where
|
|
// 1 USD = 1_000_000 micro-credits (DOLLAR_TO_CREDIT_MULTIPLIER).
|
|
const MICRO_CREDITS_PER_DOLLAR = 1_000_000;
|
|
const EXA_BASE_COST_DOLLARS = 0.007;
|
|
const EXA_COST_PER_ADDITIONAL_RESULT_DOLLARS = 0.001;
|
|
|
|
type ExaSearchResult = {
|
|
title: string;
|
|
url: string;
|
|
snippet: string;
|
|
};
|
|
|
|
type HandlerResult = {
|
|
success: boolean;
|
|
message: string;
|
|
result?: ExaSearchResult[];
|
|
error?: string;
|
|
};
|
|
|
|
const computeMicroCredits = (numResults: number): number => {
|
|
const additional = Math.max(0, numResults - DEFAULT_NUM_RESULTS);
|
|
const dollars =
|
|
EXA_BASE_COST_DOLLARS + additional * EXA_COST_PER_ADDITIONAL_RESULT_DOLLARS;
|
|
|
|
return Math.round(dollars * MICRO_CREDITS_PER_DOLLAR);
|
|
};
|
|
|
|
const handler = async (
|
|
parameters: ExaWebSearchInput,
|
|
): Promise<HandlerResult> => {
|
|
const apiKey = process.env.EXA_API_KEY;
|
|
|
|
if (!apiKey) {
|
|
return {
|
|
success: false,
|
|
message: 'Exa is not configured',
|
|
error:
|
|
'EXA_API_KEY is not set. The server admin must provide an Exa API key for this tool to work.',
|
|
};
|
|
}
|
|
|
|
const query = parameters.query;
|
|
const numResults = parameters.numResults ?? DEFAULT_NUM_RESULTS;
|
|
const category = parameters.category;
|
|
|
|
try {
|
|
const exa = new Exa(apiKey);
|
|
|
|
// exa-js has no built-in abort — race it manually.
|
|
const response = await Promise.race([
|
|
exa.searchAndContents(query, {
|
|
type: 'auto',
|
|
numResults,
|
|
category,
|
|
highlights: { numSentences: HIGHLIGHT_NUM_SENTENCES },
|
|
}),
|
|
new Promise<never>((_, reject) =>
|
|
setTimeout(
|
|
() => reject(new Error('Exa search timed out')),
|
|
EXA_SEARCH_TIMEOUT_MS,
|
|
),
|
|
),
|
|
]);
|
|
|
|
const results: ExaSearchResult[] = response.results.map((result) => ({
|
|
title: result.title ?? '',
|
|
url: result.url,
|
|
snippet: result.highlights?.join('\n') ?? '',
|
|
}));
|
|
|
|
await chargeCredits({
|
|
creditsUsedMicro: computeMicroCredits(results.length),
|
|
operationType: 'WEB_SEARCH',
|
|
resourceContext: 'exa',
|
|
});
|
|
|
|
return {
|
|
success: true,
|
|
message: `Found ${results.length} results for "${query}"${category ? ` (category: ${category})` : ''}`,
|
|
result: results,
|
|
};
|
|
} catch (error) {
|
|
return {
|
|
success: false,
|
|
message: `Web search failed for "${query}"`,
|
|
error: error instanceof Error ? error.message : 'Web search failed',
|
|
};
|
|
}
|
|
};
|
|
|
|
export default defineLogicFunction({
|
|
universalIdentifier: '4c6f9b2a-5d8e-4c2a-af18-3e0b9c6a7e4f',
|
|
name: 'exa_web_search',
|
|
description:
|
|
'Structured web search powered by Exa. Returns entity-aware results with category filtering (companies, people, research papers, news, and other content types). Prefer this when the query benefits from structured data or a specific category. For general real-time web browsing, prefer the native `web_search` tool when it is available.',
|
|
timeoutSeconds: 30,
|
|
toolTriggerSettings: {
|
|
inputSchema: exaWebSearchInputSchema,
|
|
},
|
|
handler,
|
|
});
|