import { z } from "zod";
import { tool } from "@anthropic-ai/claude-agent-sdk";
import { getAgentState } from "../agent/state.js";
import { generateToken } from "../obs/token.js";
// Unicode em-dash for the reason description (avoid escape sequences in template strings)
const EM_DASH = "\u2014";
export const remoteDesktopTool = tool("remote_desktop", "Generate a temporary remote desktop link so the user can view and control this machine's desktop "
    + "directly from a browser (H.264 desktop streaming, requires Chrome or Edge). "
    + "Use this proactively whenever the task requires human intervention: identity verification, "
    + "login with a verification code, CAPTCHA solving, face-ID / biometric prompts, "
    + "or any step where the user needs to see and interact with the screen.", {
    ttl_minutes: z
        .number()
        .int()
        .min(5)
        .max(480)
        .default(30)
        .describe("How long the link stays valid in minutes (default: 30, max: 480)"),
    reason: z
        .string()
        .describe(`Why remote access is needed ${EM_DASH} shown in the notification message so the user understands the context`),
}, 
// eslint-disable-next-line @typescript-eslint/require-await
async (args) => {
    let tunnelUrl;
    try {
        tunnelUrl = getAgentState().consoleUrl;
    }
    catch {
        // AgentState not yet initialized
    }
    if (!tunnelUrl) {
        return {
            content: [
                {
                    type: "text",
                    text: "Remote desktop is unavailable: the Cloudflare Tunnel URL is not set. "
                        + "Make sure the obs server is running and the tunnel has been established.",
                },
            ],
            isError: true,
        };
    }
    const { token, expiresAt } = generateToken(args.ttl_minutes, args.reason);
    // Strip any trailing path segments (e.g. /obs, /console) from the tunnel URL
    // so the VNC link starts at the root.
    const baseUrl = tunnelUrl.replace(/\/(obs|console)(\/.*)?$/, "").replace(/\/$/, "");
    const url = `${baseUrl}/rdp/${token}`;
    return {
        content: [
            {
                type: "text",
                text: [
                    `Remote desktop link (valid for ${args.ttl_minutes} min):`,
                    url,
                    `Expires: ${expiresAt}`,
                    `Reason: ${args.reason}`,
                ].join("\n"),
            },
        ],
    };
});
//# sourceMappingURL=remote-desktop.js.map