"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.makeUnlockTool = makeUnlockTool;
function makeUnlockTool(authService) {
    async function unlock(params = {}) {
        const hasAssertion = params.signature &&
            params.credentialId &&
            params.authenticatorData &&
            params.clientDataJSON &&
            params.challenge;
        if (!hasAssertion) {
            const challenge = authService.generateChallenge();
            return {
                success: false,
                challenge,
                credentialId: authService.getCredentialId(),
                message: "Sign this challenge with your passkey and call unlock again with the assertion data.",
            };
        }
        const result = await authService.unlock({
            signature: params.signature,
            credentialId: params.credentialId,
            authenticatorData: params.authenticatorData,
            clientDataJSON: params.clientDataJSON,
            challenge: params.challenge,
        });
        if (result.success) {
            const info = authService.getSessionInfo();
            return {
                success: true,
                message: `Worker unlocked successfully. Session expires at ${info.expiresAt}.`,
            };
        }
        return {
            success: false,
            message: `Unlock failed: ${result.reason}`,
        };
    }
    const unlockDefinition = {
        type: "function",
        function: {
            name: "unlock",
            description: "Unlock the worker using a passkey. " +
                "Call with NO parameters to receive a challenge string. " +
                "Then sign the challenge via the browser WebAuthn API (navigator.credentials.get) and call again with the assertion data to unlock. " +
                "All other tools are blocked until the worker is unlocked.",
            parameters: {
                type: "object",
                properties: {
                    signature: {
                        type: "string",
                        description: "base64url-encoded signature from WebAuthn assertion response.signature",
                    },
                    credentialId: {
                        type: "string",
                        description: "base64url-encoded credential ID from WebAuthn assertion",
                    },
                    authenticatorData: {
                        type: "string",
                        description: "base64url-encoded authenticatorData from WebAuthn assertion response.authenticatorData",
                    },
                    clientDataJSON: {
                        type: "string",
                        description: "base64url-encoded clientDataJSON from WebAuthn assertion response.clientDataJSON",
                    },
                    challenge: {
                        type: "string",
                        description: "The challenge string returned by the first unlock() call (base64url)",
                    },
                },
                required: [],
            },
        },
    };
    return { unlock, unlockDefinition };
}
//# sourceMappingURL=unlock.js.map