{"version":3,"file":"guardrail-violation-error.mjs","names":[],"sources":["../../../../../../../ai/src/errors/guardrail-violation-error.ts"],"sourcesContent":["import { AIError, type AIErrorOptions } from \"./ai-error\";\nimport type { ErrorCategory } from \"./error-category.type\";\n\n/**\n * Phase at which a guardrail rejected the content — `\"input\"` when the\n * violation was detected on the outbound prompt (before the model saw\n * it), `\"output\"` when it was detected on the model's response (before\n * the caller saw it).\n */\nexport type GuardrailPhase = \"input\" | \"output\";\n\n/**\n * Payload for `GuardrailViolationError`. `phase` tells the caller\n * whether the prompt or the response tripped the check; `reason` is\n * the free-form explanation the guardrail middleware produced;\n * `guardrail` names the offending middleware so operators can tune a\n * specific rule without hunting through logs.\n */\nexport type GuardrailViolationErrorOptions = AIErrorOptions & {\n  phase: GuardrailPhase;\n  reason: string;\n  guardrail?: string;\n};\n\n/**\n * A guardrail middleware rejected the prompt or response mid-execution.\n *\n * **Role.** The typed abort surface for `ai.middleware.guardrail`.\n * Consumers branch on `error.phase` to distinguish \"the user asked\n * something disallowed\" (`\"input\"`) from \"the model produced something\n * disallowed\" (`\"output\"`) — the two failure modes have very different\n * product responses (block vs. retry, or surface a policy message vs.\n * re-prompt the model).\n *\n * Thrown from inside the middleware pipeline's `trip.before` / `trip.after`\n * hooks; surfaced to the caller via `result.error` like every other\n * `AIError`.\n *\n * @example\n * if (result.error instanceof GuardrailViolationError) {\n *   if (result.error.phase === \"input\") {\n *     return respondWithPolicyMessage(result.error.reason);\n *   }\n *   return retryWithSanitizedPrompt();\n * }\n */\nexport class GuardrailViolationError extends AIError {\n  public static readonly defaultCategory: ErrorCategory = \"guardrail\";\n\n  public readonly phase: GuardrailPhase;\n  public readonly reason: string;\n  public readonly guardrail?: string;\n\n  public constructor(message: string, options: GuardrailViolationErrorOptions) {\n    super(\"GUARDRAIL_VIOLATION\", message, options);\n\n    this.name = \"GuardrailViolationError\";\n    this.phase = options.phase;\n    this.reason = options.reason;\n    this.guardrail = options.guardrail;\n  }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AA8CA,IAAa,0BAAb,cAA6C,QAAQ;;yBACK;;CAMxD,AAAO,YAAY,SAAiB,SAAyC;EAC3E,MAAM,uBAAuB,SAAS,OAAO;EAE7C,KAAK,OAAO;EACZ,KAAK,QAAQ,QAAQ;EACrB,KAAK,SAAS,QAAQ;EACtB,KAAK,YAAY,QAAQ;CAC3B;AACF"}