import { f as readNumberParam, g as readStringParam, l as jsonResult } from "../../common-CrIVS4tE.js";
import { t as definePluginEntry } from "../../plugin-entry-ieJOYYXv.js";
import "../../provider-web-search-alnKLiFd.js";
import { n as runTavilyExtract, r as runTavilySearch } from "../../tavily-client-Bm5CwCaD.js";
import { t as createTavilyWebSearchProvider } from "../../tavily-search-provider-g5x6V4Wf.js";
import { Type } from "typebox";
//#region extensions/tavily/src/tavily-tool-schema.ts
function optionalStringEnum(values, options = {}) {
	return Type.Optional(Type.Unsafe({
		type: "string",
		enum: [...values],
		...options
	}));
}
//#endregion
//#region extensions/tavily/src/tavily-extract-tool.ts
const TavilyExtractToolSchema = Type.Object({
	urls: Type.Array(Type.String(), {
		description: "One or more URLs to extract content from (max 20).",
		minItems: 1,
		maxItems: 20
	}),
	query: Type.Optional(Type.String({ description: "Rerank extracted chunks by relevance to this query." })),
	extract_depth: optionalStringEnum(["basic", "advanced"], { description: "\"basic\" (default) or \"advanced\" (for JS-heavy pages)." }),
	chunks_per_source: Type.Optional(Type.Number({
		description: "Chunks per URL (1-5, requires query).",
		minimum: 1,
		maximum: 5
	})),
	include_images: Type.Optional(Type.Boolean({ description: "Include image URLs in extraction results." }))
}, { additionalProperties: false });
function createTavilyExtractTool(api) {
	return {
		name: "tavily_extract",
		label: "Tavily Extract",
		description: "Extract clean content from one or more URLs using Tavily. Handles JS-rendered pages. Supports query-focused chunking.",
		parameters: TavilyExtractToolSchema,
		execute: async (_toolCallId, rawParams) => {
			const urls = Array.isArray(rawParams.urls) ? rawParams.urls.filter(Boolean) : [];
			if (urls.length === 0) throw new Error("tavily_extract requires at least one URL.");
			const query = readStringParam(rawParams, "query") || void 0;
			const extractDepth = readStringParam(rawParams, "extract_depth") || void 0;
			const chunksPerSource = readNumberParam(rawParams, "chunks_per_source", { integer: true });
			if (chunksPerSource !== void 0 && !query) throw new Error("tavily_extract requires query when chunks_per_source is set.");
			const includeImages = rawParams.include_images === true;
			return jsonResult(await runTavilyExtract({
				cfg: api.config,
				urls,
				query,
				extractDepth,
				chunksPerSource,
				includeImages
			}));
		}
	};
}
//#endregion
//#region extensions/tavily/src/tavily-search-tool.ts
const TavilySearchToolSchema = Type.Object({
	query: Type.String({ description: "Search query string." }),
	search_depth: optionalStringEnum(["basic", "advanced"], { description: "Search depth: \"basic\" (default, faster) or \"advanced\" (more thorough)." }),
	topic: optionalStringEnum([
		"general",
		"news",
		"finance"
	], { description: "Search topic: \"general\" (default), \"news\", or \"finance\"." }),
	max_results: Type.Optional(Type.Number({
		description: "Number of results to return (1-20).",
		minimum: 1,
		maximum: 20
	})),
	include_answer: Type.Optional(Type.Boolean({ description: "Include an AI-generated answer summary (default: false)." })),
	time_range: optionalStringEnum([
		"day",
		"week",
		"month",
		"year"
	], { description: "Filter results by recency: 'day', 'week', 'month', or 'year'." }),
	include_domains: Type.Optional(Type.Array(Type.String(), { description: "Only include results from these domains." })),
	exclude_domains: Type.Optional(Type.Array(Type.String(), { description: "Exclude results from these domains." }))
}, { additionalProperties: false });
function createTavilySearchTool(api) {
	return {
		name: "tavily_search",
		label: "Tavily Search",
		description: "Search the web using Tavily Search API. Supports search depth, topic filtering, domain filters, time ranges, and AI answer summaries.",
		parameters: TavilySearchToolSchema,
		execute: async (_toolCallId, rawParams) => {
			const query = readStringParam(rawParams, "query", { required: true });
			const searchDepth = readStringParam(rawParams, "search_depth") || void 0;
			const topic = readStringParam(rawParams, "topic") || void 0;
			const maxResults = readNumberParam(rawParams, "max_results", { integer: true });
			const includeAnswer = rawParams.include_answer === true;
			const timeRange = readStringParam(rawParams, "time_range") || void 0;
			const includeDomains = Array.isArray(rawParams.include_domains) ? rawParams.include_domains.filter(Boolean) : void 0;
			const excludeDomains = Array.isArray(rawParams.exclude_domains) ? rawParams.exclude_domains.filter(Boolean) : void 0;
			return jsonResult(await runTavilySearch({
				cfg: api.config,
				query,
				searchDepth,
				topic,
				maxResults,
				includeAnswer,
				timeRange,
				includeDomains: includeDomains?.length ? includeDomains : void 0,
				excludeDomains: excludeDomains?.length ? excludeDomains : void 0
			}));
		}
	};
}
//#endregion
//#region extensions/tavily/index.ts
var tavily_default = definePluginEntry({
	id: "tavily",
	name: "Tavily Plugin",
	description: "Bundled Tavily search and extract plugin",
	register(api) {
		api.registerWebSearchProvider(createTavilyWebSearchProvider());
		api.registerTool(createTavilySearchTool(api));
		api.registerTool(createTavilyExtractTool(api));
	}
});
//#endregion
export { tavily_default as default };
