
!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]="6cf28b09-8c4e-5ba9-b95e-79122ede4294")}catch(e){}}();
import { ErrorCode } from '@modelcontextprotocol/sdk/types.js';
import log from '@apify/log';
/**
 * Safely extract HTTP status code from errors.
 * Checks both `statusCode` and `code` properties for compatibility.
 */
export function getHttpStatusCode(error) {
    if (typeof error !== 'object' || error === null) {
        return undefined;
    }
    // Check for statusCode property (used by apify-client)
    if ('statusCode' in error) {
        const { statusCode } = error;
        if (typeof statusCode === 'number' && statusCode >= 100 && statusCode < 600) {
            return statusCode;
        }
    }
    // Check for code property (used by some error types)
    if ('code' in error) {
        const { code } = error;
        if (typeof code === 'number' && code >= 100 && code < 600) {
            return code;
        }
    }
    return undefined;
}
/**
 * Client/caller faults and transient transport conditions that shouldn't trigger error alerts.
 * Anything else in the JSON-RPC reserved range (-32768..-32000) is treated as a server fault.
 */
const SOFT_MCP_ERROR_CODES = new Set([
    ErrorCode.ParseError,
    ErrorCode.InvalidRequest,
    ErrorCode.MethodNotFound,
    ErrorCode.InvalidParams,
    ErrorCode.ConnectionClosed,
    ErrorCode.RequestTimeout,
]);
/**
 * Extract a JSON-RPC error code from an `McpError`-shaped object.
 * Returns `undefined` if the `code` field is absent or outside the JSON-RPC reserved range.
 */
function getMcpErrorCode(error) {
    if (typeof error !== 'object' || error === null || !('code' in error))
        return undefined;
    const { code } = error;
    if (typeof code === 'number' && code >= -32768 && code <= -32000)
        return code;
    return undefined;
}
/**
 * Logs HTTP or MCP errors at the appropriate level:
 * - Client errors (HTTP < 500, or JSON-RPC client/transient codes) → softFail (no stack).
 * - Server errors (HTTP >= 500, or JSON-RPC server codes) → exception (with stack).
 * - Anything unclassifiable → error.
 *
 * @param error - The error object
 * @param message - The log message
 * @param data - Additional data to include in the log
 */
export function logHttpError(error, message, data) {
    const statusCode = getHttpStatusCode(error);
    const rawErrorMessage = error instanceof Error ? error.message : String(error);
    // Mezmo (logDNA) promotes log entries to error level when message/keys contain "error".
    // Sanitize for softFail paths (see CONTRIBUTING.md § Logging → Mezmo promotion rule).
    const softErrMessage = rawErrorMessage.replace(/ error:/gi, ' failure:');
    if (statusCode !== undefined && statusCode < 500) {
        // HTTP client errors (< 500) - softFail without stack trace
        log.softFail(message, { errMessage: softErrMessage, statusCode, ...data });
        return;
    }
    if (statusCode !== undefined && statusCode >= 500) {
        // HTTP server errors (>= 500) - exception with full error (includes stack trace)
        const errorObj = error instanceof Error ? error : new Error(String(error));
        log.exception(errorObj, message, { statusCode, ...data });
        return;
    }
    const mcpErrorCode = getMcpErrorCode(error);
    if (mcpErrorCode !== undefined) {
        if (SOFT_MCP_ERROR_CODES.has(mcpErrorCode)) {
            log.softFail(message, { errMessage: softErrMessage, mcpErrorCode, ...data });
        }
        else {
            const errorObj = error instanceof Error ? error : new Error(String(error));
            log.exception(errorObj, message, { mcpErrorCode, ...data });
        }
        return;
    }
    // No status code available - log as error
    log.error(message, { error, ...data });
}
const SKYFIRE_PAY_ID_KEY = 'skyfire-pay-id';
const REDACTED_VALUE = '[REDACTED]';
const isPlainRecord = (value) => {
    return typeof value === 'object' && value !== null && !Array.isArray(value);
};
/**
 * Sanitizes tool call parameters by redacting the skyfire-pay-id.
 * Used for logging to avoid exposing the Skyfire payment token.
 *
 * @param params - The parameters object to sanitize
 * @returns A new object with skyfire-pay-id replaced with '[REDACTED]'
 */
export function redactSkyfirePayId(params) {
    if (!isPlainRecord(params) || !(SKYFIRE_PAY_ID_KEY in params)) {
        return params;
    }
    if (params[SKYFIRE_PAY_ID_KEY] === REDACTED_VALUE) {
        return params;
    }
    return { ...params, [SKYFIRE_PAY_ID_KEY]: REDACTED_VALUE };
}
//# sourceMappingURL=logging.js.map
//# debugId=6cf28b09-8c4e-5ba9-b95e-79122ede4294
