import z from 'zod';
const parameters = z.object({
    sourceUrl: z.string().describe('The URL to fetch (http or https only).'),
});
export const fetchURLTool = {
    name: 'sys:fetch_url',
    description: `
  Fetch a file, resource or content from a remote HTTP(S) URL. Returns the content as utf-8 text with metadata (content type, encoding, etc).
  `,
    parameters,
    needsApproval: true,
    async execute(rawArgs) {
        if (!window.network?.fetchURL) {
            throw new Error('network bridge is not available');
        }
        const args = parameters.parse(rawArgs);
        const result = await window.network.fetchURL(args.sourceUrl);
        if (result.error) {
            return `Error fetching URL: ${result.error}`;
        }
        const { body, ...metadata } = result;
        const previewLimit = 500;
        const bodyPreview = body.length > previewLimit
            ? `${body.slice(0, previewLimit)}…`
            : body;
        return JSON.stringify({
            ...metadata,
            bodyLength: body.length,
            bodyPreview,
        }, null, 2);
    },
};
