{"version":3,"file":"errors.mjs","sources":["../../../src/utils/errors.ts"],"sourcesContent":["/**\n * Context-overflow error detection helpers.\n *\n * Provider error messages vary — Anthropic returns \"prompt is too long\",\n * OpenAI returns \"context_length_exceeded\", Bedrock returns \"Input is too\n * long\", Google returns a size-limit phrase. This module centralises the\n * phrase list so the agent graph's emergency-prune retry can classify\n * errors consistently instead of duplicating inline substring matches at\n * each call site.\n *\n * The strict check (`isContextOverflowError`) matches only known phrases.\n * The loose check (`isLikelyContextOverflowError`) also matches a heuristic\n * regex for providers we haven't explicitly catalogued. Both filter out\n * false positives (rate-limit, auth, quota, billing) that might otherwise\n * trigger an unnecessary prune retry.\n */\n\nconst CONTEXT_OVERFLOW_PHRASES = [\n  'request_too_large',\n  'context length exceeded',\n  'maximum context length',\n  'prompt is too long',\n  'exceeds model context window',\n  'exceeds the model',\n  'too large for model',\n  'context_length_exceeded',\n  'max_tokens',\n  'token limit',\n  'input too long',\n  'input is too long',\n  'payload too large',\n  'content_too_large',\n] as const;\n\nconst CONTEXT_OVERFLOW_HINT_RE =\n  /413|too large|too long|context.*exceed|exceed.*context|token.*limit|limit.*token|prompt.*size|size.*limit|maximum.*length|length.*maximum/i;\n\nconst FALSE_POSITIVE_RE =\n  /rate.?limit|too many requests|quota|billing|auth|permission|forbidden/i;\n\n/**\n * Extracts a human-readable error message from an unknown error value.\n * Walks common shapes: strings, Error instances, `{ message }`,\n * `{ error: string }`, `{ error: { message } }`. Falls back to\n * JSON.stringify or String() so callers never have to null-check.\n */\nexport function extractErrorMessage(error: unknown): string {\n  if (error == null) {\n    return '';\n  }\n  if (typeof error === 'string') {\n    return error;\n  }\n  if (error instanceof Error) {\n    return error.message;\n  }\n  if (typeof error === 'object') {\n    const record = error as Record<string, unknown>;\n    if (typeof record.message === 'string') {\n      return record.message;\n    }\n    if (typeof record.error === 'string') {\n      return record.error;\n    }\n    if (\n      typeof record.error === 'object' &&\n      record.error != null &&\n      typeof (record.error as Record<string, unknown>).message === 'string'\n    ) {\n      return (record.error as Record<string, unknown>).message as string;\n    }\n  }\n  try {\n    return JSON.stringify(error);\n  } catch {\n    return String(error);\n  }\n}\n\n/**\n * Strict check: returns true only for known, unambiguous context-overflow\n * phrases. Use when the recovery action is expensive (full prune + retry)\n * and false positives are undesirable.\n */\nexport function isContextOverflowError(errorMessage?: string): boolean {\n  if (!errorMessage) {\n    return false;\n  }\n  const lower = errorMessage.toLowerCase();\n  if (FALSE_POSITIVE_RE.test(lower)) {\n    return false;\n  }\n  return CONTEXT_OVERFLOW_PHRASES.some((phrase) => lower.includes(phrase));\n}\n\n/**\n * Loose check: returns true for known phrases OR heuristic regex matches.\n * Preferred by the graph's emergency-prune retry because the cost of a\n * false positive is one extra retry with a smaller context, while the\n * cost of a false negative is an opaque provider failure surfaced to\n * the user.\n */\nexport function isLikelyContextOverflowError(errorMessage?: string): boolean {\n  if (!errorMessage) {\n    return false;\n  }\n  if (isContextOverflowError(errorMessage)) {\n    return true;\n  }\n  const lower = errorMessage.toLowerCase();\n  if (FALSE_POSITIVE_RE.test(lower)) {\n    return false;\n  }\n  return CONTEXT_OVERFLOW_HINT_RE.test(lower);\n}\n"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;AAeG;AAEH,MAAM,wBAAwB,GAAG;IAC/B,mBAAmB;IACnB,yBAAyB;IACzB,wBAAwB;IACxB,oBAAoB;IACpB,8BAA8B;IAC9B,mBAAmB;IACnB,qBAAqB;IACrB,yBAAyB;IACzB,YAAY;IACZ,aAAa;IACb,gBAAgB;IAChB,mBAAmB;IACnB,mBAAmB;IACnB,mBAAmB;CACX;AAEV,MAAM,wBAAwB,GAC5B,4IAA4I;AAE9I,MAAM,iBAAiB,GACrB,wEAAwE;AAE1E;;;;;AAKG;AACG,SAAU,mBAAmB,CAAC,KAAc,EAAA;AAChD,IAAA,IAAI,KAAK,IAAI,IAAI,EAAE;AACjB,QAAA,OAAO,EAAE;IACX;AACA,IAAA,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE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