
!function(){try{var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:{},n=(new e.Error).stack;n&&(e._sentryDebugIds=e._sentryDebugIds||{},e._sentryDebugIds[n]="fdaace00-9bc6-5f64-93fb-a464e2a594fb")}catch(e){}}();
import { z } from 'zod';
import log from '@apify/log';
import { ALLOWED_DOC_DOMAINS, HelperTools, TOOL_STATUS } from '../../const.js';
import { fetchApifyDocsCache } from '../../state.js';
import { compileSchema } from '../../utils/ajv.js';
import { logHttpError } from '../../utils/logging.js';
import { buildMCPResponse } from '../../utils/mcp.js';
import { fetchApifyDocsToolOutputSchema } from '../structured_output_schemas.js';
const fetchApifyDocsToolArgsSchema = z.object({
    url: z.string()
        .min(1)
        .describe(`URL of the Apify documentation page to fetch. This should be the full URL, including the protocol (e.g., https://docs.apify.com/).`),
});
const fetchApifyDocsToolInputSchema = z.toJSONSchema(fetchApifyDocsToolArgsSchema);
function buildFetchErrorMessage(url, detail) {
    return `Failed to fetch the documentation page at "${url}". ${detail} \
Please verify the URL is correct and accessible. \
You can search for available documentation pages using the ${HelperTools.DOCS_SEARCH} tool.`;
}
export const fetchApifyDocsTool = Object.freeze({
    type: 'internal',
    name: HelperTools.DOCS_FETCH,
    description: `Fetch the full content of an Apify or Crawlee documentation page by its URL.
Use this after finding a relevant page with the ${HelperTools.DOCS_SEARCH} tool.

USAGE:
- Use when you need the complete content of a specific docs page for detailed answers.

USAGE EXAMPLES:
- user_input: Fetch https://docs.apify.com/platform/actors/running#builds
- user_input: Fetch https://docs.apify.com/academy
- user_input: Fetch https://crawlee.dev/docs/guides/basic-concepts`,
    inputSchema: fetchApifyDocsToolInputSchema,
    outputSchema: fetchApifyDocsToolOutputSchema,
    ajvValidate: compileSchema(fetchApifyDocsToolInputSchema),
    annotations: {
        title: 'Fetch Apify docs',
        readOnlyHint: true,
        destructiveHint: false,
        idempotentHint: true,
        openWorldHint: false,
    },
    call: async (toolArgs) => {
        const { args } = toolArgs;
        const parsed = fetchApifyDocsToolArgsSchema.parse(args);
        const url = parsed.url.trim();
        // Allow URLs from Apify and Crawlee documentation
        const isAllowedDomain = ALLOWED_DOC_DOMAINS.some((domain) => url.startsWith(domain));
        if (!isAllowedDomain) {
            log.softFail(`[fetch-apify-docs] Invalid URL domain: ${url}`);
            return buildMCPResponse({
                texts: [`Invalid URL: "${url}". \
Only documentation URLs from Apify and Crawlee are allowed \
(starting with ${ALLOWED_DOC_DOMAINS.map((d) => `"${d}"`).join(' or ')}). \
Please provide a valid documentation URL. \
You can find documentation URLs using the ${HelperTools.DOCS_SEARCH} tool.`],
                isError: true,
                toolStatus: TOOL_STATUS.SOFT_FAIL,
            });
        }
        // Cache by URL without fragment to avoid fetching the same page multiple times
        const urlWithoutFragment = url.split('#')[0];
        let markdown = fetchApifyDocsCache.get(urlWithoutFragment);
        if (!markdown) {
            // Use the .md variant of the URL to get Markdown directly
            const mdUrl = `${urlWithoutFragment}.md`;
            try {
                const response = await fetch(mdUrl);
                if (!response.ok) {
                    const error = Object.assign(new Error(`HTTP ${response.status} ${response.statusText}`), {
                        statusCode: response.status,
                    });
                    logHttpError(error, 'Failed to fetch the documentation page', { url: mdUrl, statusText: response.statusText });
                    const isUserError = response.status >= 400 && response.status < 500;
                    return buildMCPResponse({
                        texts: [buildFetchErrorMessage(url, `HTTP Status: ${response.status} ${response.statusText}.`)],
                        isError: true,
                        toolStatus: isUserError ? TOOL_STATUS.SOFT_FAIL : TOOL_STATUS.FAILED,
                    });
                }
                markdown = await response.text();
                fetchApifyDocsCache.set(urlWithoutFragment, markdown);
            }
            catch (error) {
                logHttpError(error, 'Failed to fetch the documentation page', { url: mdUrl });
                return buildMCPResponse({
                    texts: [buildFetchErrorMessage(url, `Error: ${error instanceof Error ? error.message : String(error)}.`)],
                    isError: true,
                    toolStatus: TOOL_STATUS.SOFT_FAIL,
                });
            }
        }
        return buildMCPResponse({ texts: [`Fetched content from ${url}:\n\n${markdown}`], structuredContent: { url, content: markdown } });
    },
});
//# sourceMappingURL=fetch_apify_docs.js.map
//# debugId=fdaace00-9bc6-5f64-93fb-a464e2a594fb
