import { Type } from "@mariozechner/pi-ai"; import type { ExtensionAPI } from "@mariozechner/pi-coding-agent"; import open from "open"; export default function (pi: ExtensionAPI) { pi.registerTool({ name: "show_user_in_browser", label: "Show User in Browser", description: "Open a URL in the user's default browser. Use this when the user asks to be shown something in a browser, wants to view a webpage, or explicitly requests to open a URL in their browser.", parameters: Type.Object({ url: Type.String({ description: "The URL to open in the browser", }), }), async execute(_toolCallId, params, _signal, _onUpdate, _ctx) { const { url } = params as { url: string }; // Validate URL format let validatedUrl: URL; try { validatedUrl = new URL(url); } catch { throw new Error(`Invalid URL: ${url}`); } // Only allow http and https protocols for security if ( validatedUrl.protocol !== "http:" && validatedUrl.protocol !== "https:" ) { throw new Error( `Unsupported protocol: ${validatedUrl.protocol}. Only http: and https: are allowed.`, ); } // Open the URL in the default browser await open(url); return { content: [{ type: "text", text: `Opened ${url} in your browser.` }], details: { url: validatedUrl.href }, }; }, }); }