/**
 * NodeLLM Error Hierarchy
 *
 * **Stability Contract**: These error types and their semantics are part of the
 * public API and will not change without a major version bump.
 *
 * - Error names are stable
 * - Error codes are stable
 * - Error semantics (when they're thrown) are stable
 * - New errors may be added (non-breaking)
 * - Existing error meanings will not change
 *
 * @see ARCHITECTURE.md for error contract details
 */
/**
 * Base class for all NodeLLM errors
 */
export class LLMError extends Error {
    code;
    constructor(message, code) {
        super(message);
        this.code = code;
        this.name = this.constructor.name;
        Error.captureStackTrace(this, this.constructor);
    }
}
/**
 * Errors occurring during API calls to providers
 */
export class APIError extends LLMError {
    status;
    body;
    provider;
    model;
    constructor(message, status, body, provider, model) {
        super(message, "API_ERROR");
        this.status = status;
        this.body = body;
        this.provider = provider;
        this.model = model;
    }
}
/**
 * 400 - Invalid request parameters
 */
export class BadRequestError extends APIError {
    constructor(message, body, provider, model) {
        super(message, 400, body, provider, model);
        this.name = "BadRequestError";
    }
}
/**
 * 400 - Specifically for context window/token limit issues
 */
export class ContextWindowExceededError extends BadRequestError {
    constructor(message, body, provider, model) {
        super(message, body, provider, model);
        this.name = "ContextWindowExceededError";
    }
}
/**
 * 401 - Invalid or missing API key
 */
export class UnauthorizedError extends APIError {
    constructor(message, body, provider) {
        super(message, 401, body, provider);
        this.name = "UnauthorizedError";
    }
}
/**
 * 402 - Payment required (billing issues)
 */
export class PaymentRequiredError extends APIError {
    constructor(message, body, provider) {
        super(message, 402, body, provider);
        this.name = "PaymentRequiredError";
    }
}
/**
 * 403 - Permission denied
 */
export class ForbiddenError extends APIError {
    constructor(message, body, provider) {
        super(message, 403, body, provider);
        this.name = "ForbiddenError";
    }
}
/**
 * 401/403 - API key or permission issues
 * @deprecated Use UnauthorizedError (401) or ForbiddenError (403) for granular handling
 */
export class AuthenticationError extends APIError {
    constructor(message, status, body, provider) {
        super(message, status, body, provider);
        this.name = "AuthenticationError";
    }
}
/**
 * 429 - Rate limit exceeded
 */
export class RateLimitError extends APIError {
    constructor(message, body, provider, model) {
        super(message, 429, body, provider, model);
        this.name = "RateLimitError";
    }
}
/**
 * 429 - Out of credits or monthly quota exceeded
 */
export class InsufficientQuotaError extends RateLimitError {
    constructor(message, body, provider, model) {
        super(message, body, provider, model);
        this.name = "InsufficientQuotaError";
    }
}
/**
 * 500+ - Provider server error
 */
export class ServerError extends APIError {
    constructor(message, status, body, provider, model) {
        super(message, status, body, provider, model);
        this.name = "ServerError";
    }
}
/**
 * 502/503/529 - Service overloaded/unavailable
 */
export class ServiceUnavailableError extends ServerError {
    constructor(message, status, body, provider, model) {
        super(message, status, body, provider, model);
        this.name = "ServiceUnavailableError";
    }
}
/**
 * Misconfiguration (e.g. missing API key)
 */
export class ConfigurationError extends LLMError {
    constructor(message) {
        super(message, "CONFIGURATION_ERROR");
    }
}
/**
 * Requested model or provider not found
 */
export class NotFoundError extends APIError {
    constructor(message, status = 404, body = null, provider, model) {
        super(message, status, body, provider, model);
        this.name = "NotFoundError";
    }
}
/**
 * Specifically when the requested model ID is invalid/unknown
 */
export class InvalidModelError extends NotFoundError {
    constructor(message, body, provider, model) {
        super(message, 404, body, provider, model);
        this.name = "InvalidModelError";
    }
}
/**
 * Model does not support requested capability
 */
export class CapabilityError extends LLMError {
    constructor(message) {
        super(message, "CAPABILITY_ERROR");
    }
}
/**
 * Thrown when LLM provider is not configured
 */
export class ProviderNotConfiguredError extends LLMError {
    constructor() {
        super("LLM provider not configured", "PROVIDER_NOT_CONFIGURED");
    }
}
/**
 * Thrown when a provider doesn't support a requested feature
 */
export class UnsupportedFeatureError extends LLMError {
    provider;
    feature;
    constructor(provider, feature) {
        super(`${provider} does not support ${feature}`, "UNSUPPORTED_FEATURE");
        this.provider = provider;
        this.feature = feature;
    }
}
/**
 * Thrown when a model doesn't support a requested capability
 */
export class ModelCapabilityError extends LLMError {
    model;
    capability;
    constructor(model, capability) {
        super(`Model ${model} does not support ${capability}`, "MODEL_CAPABILITY_ERROR");
        this.model = model;
        this.capability = capability;
    }
}
/**
 * Thrown when a tool execution fails
 */
export class ToolError extends LLMError {
    toolName;
    fatal;
    constructor(message, toolName, fatal = false) {
        super(message, "TOOL_ERROR");
        this.toolName = toolName;
        this.fatal = fatal;
        this.name = "ToolError";
    }
}
