# retoor <retoor@molodetz.nl>

from __future__ import annotations

from .spec import Action, Param

FETCH_ACTIONS: tuple[Action, ...] = (
    Action(
        name="fetch_url",
        method="LOCAL",
        path="",
        summary="Fetch a web page and return its readable text content",
        description=(
            "Retrieves any http(s) URL as a real browser would (stealth headers) and returns "
            "the page title and readable text with links preserved, safely truncated to fit "
            "the context window. Use it to read external pages, then summarize or create posts, "
            "gists, or articles from the content. Private and loopback addresses are refused."
        ),
        handler="fetch",
        requires_auth=False,
        read_only=True,
        params=(
            Param(
                name="url",
                location="body",
                description="The page URL to fetch (https is assumed if no scheme is given).",
                required=True,
            ),
            Param(
                name="max_chars",
                location="body",
                description="Optional cap on returned characters; clamped to a context-safe maximum.",
                type="integer",
            ),
        ),
    ),
    Action(
        name="http_request",
        method="LOCAL",
        path="",
        summary="Make an arbitrary HTTP request (GET/POST/PUT/PATCH/DELETE) to any external API",
        description=(
            "The general HTTP client for external services and REST/JSON APIs. Send any method with "
            "custom headers and a request body, and read the full response (status, headers, content). "
            "Use the 'json' argument to send a JSON body (Content-Type is set automatically), 'form' to "
            "send url-encoded form fields, or 'body' to send a raw string. The response body is returned "
            "verbatim for JSON, or as readable text for HTML, capped to fit the context window; non-2xx "
            "responses are returned (not raised) so you can inspect API errors. Private and loopback "
            "addresses are refused. Call this DIRECTLY to talk to an API; never wrap it in a user-defined "
            "tool or eval that calls itself. Prefer fetch_url for simply reading a web page."
        ),
        handler="fetch",
        requires_auth=True,
        params=(
            Param(
                name="url",
                location="body",
                description="The request URL (https is assumed if no scheme is given).",
                required=True,
            ),
            Param(
                name="method",
                location="body",
                description="HTTP method: GET, POST, PUT, PATCH, DELETE, HEAD, or OPTIONS. Defaults to GET.",
            ),
            Param(
                name="headers",
                location="body",
                description="Optional request headers as a JSON object of string key/value pairs.",
                type="object",
            ),
            Param(
                name="json",
                location="body",
                description="Optional JSON request body as an object or array; sets Content-Type to application/json.",
                type="object",
            ),
            Param(
                name="form",
                location="body",
                description="Optional url-encoded form body as a JSON object of string key/value pairs.",
                type="object",
            ),
            Param(
                name="body",
                location="body",
                description="Optional raw string request body; set a matching Content-Type header yourself.",
            ),
            Param(
                name="max_chars",
                location="body",
                description="Optional cap on returned response characters; clamped to a context-safe maximum.",
                type="integer",
            ),
        ),
    ),
)
