import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
import tavilyExtension from "../node_modules/@tavily/pi-extension/index.ts";
import { prefixTavilyTool, type TavilyToolDefinition } from "./tavily-tool-prefix.ts";


/** Load Tavily while making the provider explicit in every registered tool name. */
export default function tavilyOverrideExtension(pi: ExtensionAPI): void {
	const prefixedPi = new Proxy(pi, {
		get(target, property) {
			if (property === "registerTool") {
				return (definition: TavilyToolDefinition) =>
					(target.registerTool as (tool: unknown) => void)(prefixTavilyTool(definition));
			}

			const value = Reflect.get(target, property, target);
			return typeof value === "function" ? value.bind(target) : value;
		},
	});

	tavilyExtension(prefixedPi);
}
