{"version":3,"file":"ai-error.mjs","names":[],"sources":["../../../../../../../ai/src/errors/ai-error.ts"],"sourcesContent":["import type { ErrorCategory } from \"./error-category.type\";\nimport type { AIErrorCode } from \"./error-code.type\";\n\n/**\n * Optional constructor payload shared by every `AIError` subclass.\n *\n * `cause` carries the original thrown value (SDK error, runtime crash,\n * validation failure) so downstream logging and debuggers can still\n * reach it. `context` is a free-form diagnostic bag for provider-raw\n * metadata (status codes, request ids, response headers) that shouldn't\n * be promoted to typed fields but is useful in logs and telemetry.\n *\n * **No `category` here.** Category is fixed per subclass via the\n * class-level `static defaultCategory`. Subclasses ARE their\n * category — `RateLimitError` is always `\"rate-limit\"`, never\n * something else at runtime. Direct `new AIError(...)` callers (the\n * one legitimate override case, since the base catch-all has no\n * specific class) receive a separate 4th constructor argument\n * instead, so the override is structurally unreachable from subclass\n * call sites.\n */\nexport type AIErrorOptions = {\n  cause?: unknown;\n  context?: Record<string, unknown>;\n};\n\n/**\n * Base class for every error thrown (or surfaced via `result.error`) by\n * `@warlock.js/ai` and its provider adapter packages.\n *\n * **Role.** The single typed error contract across the AI framework.\n * Every thrown error anywhere in `@warlock.js/ai*` is either an\n * `AIError` itself or one of its subclasses — plain `Error` must never\n * leak out. Consumers branch either on the narrow `error.code` (stable\n * string), on `error.category` (coarse dashboard grouping), or on\n * `instanceof` a specific subclass.\n *\n * **Independence.** Deliberately extends the platform `Error` directly\n * — never `HttpError` from `@warlock.js/core`. The AI framework is a\n * standalone product; coupling its error base to a web framework would\n * force every consumer to pull in the HTTP layer even when they only\n * use AI in a CLI, worker, or test.\n *\n * **Fields.**\n * - `code` — stable machine-readable identifier (see `AIErrorCode`).\n * - `category` — coarse `ErrorCategory` for dashboards / retry policy.\n *   Resolved at construction from the subclass's `static defaultCategory`\n *   (or, for direct `new AIError(...)` calls, from the explicit 4th\n *   constructor argument).\n * - `cause` — optional original thrown value (SDK error, nested error,\n *   raw value). Preserves root cause through re-wrapping.\n * - `context` — optional free-form diagnostic bag (status, requestId,\n *   headers). Consumers treat it as opaque; logs and telemetry read it.\n *\n * **Category override — direct AIError usage only.** Subclasses ARE\n * their category by type; there's no legitimate runtime override at\n * the subclass level. The 4th constructor argument exists ONLY for\n * direct `new AIError(...)` callers, who would otherwise be stuck with\n * the `\"unknown\"` default. Subclasses construct via `super(code,\n * message, options)` and physically cannot reach the override slot\n * through their own typed signatures.\n *\n * @example\n * try {\n *   await agent.execute(\"hello\");\n * } catch (error) {\n *   if (error instanceof AIError) {\n *     console.error(`[${error.code}] (${error.category}) ${error.message}`);\n *   }\n * }\n *\n * @example\n * // Direct AIError construction with explicit category — escape hatch\n * // for call sites that lack a specific subclass.\n * throw new AIError(\"UNEXPECTED\", \"transient glitch\", undefined, \"provider\");\n */\nexport class AIError extends Error {\n  /**\n   * Class-level category for every instance of this error type.\n   * Subclasses redeclare with their own concrete `ErrorCategory` so\n   * `error.category` is correct without per-call wiring. The base\n   * class keeps `\"unknown\"` so untyped direct throws of `AIError`\n   * itself remain honest about their lack of dispatch information\n   * (and can override via the 4th constructor argument).\n   */\n  public static readonly defaultCategory: ErrorCategory = \"unknown\";\n\n  public readonly code: AIErrorCode;\n  public readonly category: ErrorCategory;\n  public readonly context?: Record<string, unknown>;\n\n  public constructor(\n    code: AIErrorCode,\n    message: string,\n    options?: AIErrorOptions,\n    category?: ErrorCategory,\n  ) {\n    super(message);\n\n    this.name = \"AIError\";\n    this.code = code;\n    this.context = options?.context;\n    this.category = category ?? (this.constructor as typeof AIError).defaultCategory;\n\n    if (options?.cause !== undefined) {\n      (this as { cause?: unknown }).cause = options.cause;\n    }\n  }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4EA,IAAa,UAAb,cAA6B,MAAM;;yBASuB;;CAMxD,AAAO,YACL,MACA,SACA,SACA,UACA;EACA,MAAM,OAAO;EAEb,KAAK,OAAO;EACZ,KAAK,OAAO;EACZ,KAAK,UAAU,SAAS;EACxB,KAAK,WAAW,YAAa,KAAK,YAA+B;EAEjE,IAAI,SAAS,UAAU,QACrB,AAAC,KAA6B,QAAQ,QAAQ;CAElD;AACF"}