|
| 1 | +import type { OpenClawConfig } from "openclaw/plugin-sdk/config-runtime"; |
| 2 | +import { normalizeResolvedSecretInputString } from "openclaw/plugin-sdk/config-runtime"; |
| 3 | +import { normalizeSecretInput } from "openclaw/plugin-sdk/provider-auth"; |
| 4 | + |
| 5 | +export const DEFAULT_TAVILY_BASE_URL = "https://api.tavily.com"; |
| 6 | +export const DEFAULT_TAVILY_SEARCH_TIMEOUT_SECONDS = 30; |
| 7 | +export const DEFAULT_TAVILY_EXTRACT_TIMEOUT_SECONDS = 60; |
| 8 | + |
| 9 | +type WebSearchConfig = NonNullable<OpenClawConfig["tools"]>["web"] extends infer Web |
| 10 | + ? Web extends { search?: infer Search } |
| 11 | + ? Search |
| 12 | + : undefined |
| 13 | + : undefined; |
| 14 | + |
| 15 | +type TavilySearchConfig = |
| 16 | + | { |
| 17 | + apiKey?: unknown; |
| 18 | + baseUrl?: string; |
| 19 | + } |
| 20 | + | undefined; |
| 21 | + |
| 22 | +function resolveSearchConfig(cfg?: OpenClawConfig): WebSearchConfig { |
| 23 | + const search = cfg?.tools?.web?.search; |
| 24 | + if (!search || typeof search !== "object") { |
| 25 | + return undefined; |
| 26 | + } |
| 27 | + return search as WebSearchConfig; |
| 28 | +} |
| 29 | + |
| 30 | +export function resolveTavilySearchConfig(cfg?: OpenClawConfig): TavilySearchConfig { |
| 31 | + const search = resolveSearchConfig(cfg); |
| 32 | + if (!search || typeof search !== "object") { |
| 33 | + return undefined; |
| 34 | + } |
| 35 | + const tavily = "tavily" in search ? search.tavily : undefined; |
| 36 | + if (!tavily || typeof tavily !== "object") { |
| 37 | + return undefined; |
| 38 | + } |
| 39 | + return tavily as TavilySearchConfig; |
| 40 | +} |
| 41 | + |
| 42 | +function normalizeConfiguredSecret(value: unknown, path: string): string | undefined { |
| 43 | + return normalizeSecretInput( |
| 44 | + normalizeResolvedSecretInputString({ |
| 45 | + value, |
| 46 | + path, |
| 47 | + }), |
| 48 | + ); |
| 49 | +} |
| 50 | + |
| 51 | +export function resolveTavilyApiKey(cfg?: OpenClawConfig): string | undefined { |
| 52 | + const search = resolveTavilySearchConfig(cfg); |
| 53 | + return ( |
| 54 | + normalizeConfiguredSecret(search?.apiKey, "tools.web.search.tavily.apiKey") || |
| 55 | + normalizeSecretInput(process.env.TAVILY_API_KEY) || |
| 56 | + undefined |
| 57 | + ); |
| 58 | +} |
| 59 | + |
| 60 | +export function resolveTavilyBaseUrl(cfg?: OpenClawConfig): string { |
| 61 | + const search = resolveTavilySearchConfig(cfg); |
| 62 | + const configured = |
| 63 | + (typeof search?.baseUrl === "string" ? search.baseUrl.trim() : "") || |
| 64 | + normalizeSecretInput(process.env.TAVILY_BASE_URL) || |
| 65 | + ""; |
| 66 | + return configured || DEFAULT_TAVILY_BASE_URL; |
| 67 | +} |
| 68 | + |
| 69 | +export function resolveTavilySearchTimeoutSeconds(override?: number): number { |
| 70 | + if (typeof override === "number" && Number.isFinite(override) && override > 0) { |
| 71 | + return Math.floor(override); |
| 72 | + } |
| 73 | + return DEFAULT_TAVILY_SEARCH_TIMEOUT_SECONDS; |
| 74 | +} |
| 75 | + |
| 76 | +export function resolveTavilyExtractTimeoutSeconds(override?: number): number { |
| 77 | + if (typeof override === "number" && Number.isFinite(override) && override > 0) { |
| 78 | + return Math.floor(override); |
| 79 | + } |
| 80 | + return DEFAULT_TAVILY_EXTRACT_TIMEOUT_SECONDS; |
| 81 | +} |
0 commit comments