const TAVILY_TOOL_PREFIX = "tavily_";

export type TavilyToolDefinition = {
	name: string;
	label: string;
	description: string;
	promptSnippet?: string;
	promptGuidelines?: string[];
	[key: string]: unknown;
};

function rewriteToolReferences(text: string): string {
	return text.replace(/\bweb_search\b/g, "tavily_web_search").replace(/\bweb_fetch\b/g, "tavily_web_fetch");
}

export function prefixTavilyTool<T extends TavilyToolDefinition>(definition: T): T {
	return {
		...definition,
		name: `${TAVILY_TOOL_PREFIX}${definition.name}`,
		label: definition.label.startsWith("Tavily ") ? definition.label : `Tavily ${definition.label}`,
		description: rewriteToolReferences(definition.description),
		promptSnippet: definition.promptSnippet ? rewriteToolReferences(definition.promptSnippet) : undefined,
		promptGuidelines: definition.promptGuidelines?.map(rewriteToolReferences),
	} as T;
}
