/**
 * request_credential tool — parks via ToolExecutor, waits for the client
 * to collect and store the credential via SFAE, then resumes with status.
 *
 * The agent never sees the actual credential value.
 */
import { Type } from '@sinclair/typebox';
import { REQUEST_CREDENTIAL } from '../../tool-names.js';
const schema = Type.Object({
    domain: Type.String({ description: "The service domain (e.g. 'github.com')" }),
    credential_type: Type.String({ description: 'One of: ACCESS_TOKEN, API_KEY, PASSWORD' }),
    url: Type.Optional(Type.String({
        description: "Help link where user can create the credential (e.g. 'https://github.com/settings/tokens')"
    })),
    message: Type.Optional(Type.String({ description: 'Explanation shown to the user about why the credential is needed' }))
});
export function createRequestCredentialTool(toolExecutor) {
    return {
        name: REQUEST_CREDENTIAL,
        label: 'Request Credential',
        isReadOnly: true,
        description: 'Request a credential (API key, access token, or password) from the user. ' +
            'Use this when you need authentication for a service but no stored credential exists. ' +
            'The user will be prompted to enter the credential securely in the UI. ' +
            'You will never see the actual credential value — it is stored securely and ' +
            'resolved at request time.',
        parameters: schema,
        async execute(toolCallId, { domain, credential_type, url, message }) {
            const { promise, notify } = toolExecutor.submit(toolCallId, REQUEST_CREDENTIAL, {
                domain,
                credential_type,
                ...(url ? { url } : {}),
                ...(message ? { message } : {})
            });
            notify();
            const result = await promise;
            if (result.status === 'error') {
                return {
                    content: [{ type: 'text', text: result.error ?? 'Credential request failed' }],
                    details: { status: 'error' }
                };
            }
            const data = (result.data ?? {});
            const status = data.status ?? 'unknown';
            if (status === 'success') {
                const resDomain = data.domain ?? domain;
                const resType = data.credential_type ?? credential_type;
                return {
                    content: [{ type: 'text', text: `Credential stored: ${resDomain} ${resType}` }],
                    details: { status: 'success', domain: resDomain, credential_type: resType }
                };
            }
            if (status === 'cancelled') {
                return {
                    content: [{ type: 'text', text: 'User cancelled the credential request.' }],
                    details: { status: 'cancelled' }
                };
            }
            return {
                content: [{ type: 'text', text: `Credential request failed: ${status}` }],
                details: { status }
            };
        }
    };
}
//# sourceMappingURL=request-credential.js.map