{"version":3,"file":"exceptions.mjs","names":[],"sources":["../../../../src/batteries/llm/ollama/exceptions.ts"],"sourcesContent":["/**\n * Battery-scoped exception constructors for the native Ollama `/api/chat` adapter.\n *\n * @module @nhtio/adk/batteries/llm/ollama/exceptions\n *\n * @remarks\n * Battery-scoped exception classes for the native Ollama adapter. These exceptions are owned by\n * the battery (not the ADK core) and are minted via `createException` from `@nhtio/adk/factories`.\n * Re-exported from the battery's barrel. They mirror the OpenAI Chat Completions battery's\n * exception set (same status codes, same fatal/non-fatal split) with two deliberate divergences:\n *\n * - `E_OLLAMA_INVALID_TOOL_CALL_ARGS` — native `/api/chat` returns tool-call `arguments` as a JSON\n *   **object**, not a string, so there is no `JSON.parse` failure path. This fires only when the\n *   `arguments` value is present but is not a plain object (array / null / primitive — defensive\n *   against a non-conformant server or proxy).\n * - `E_OLLAMA_UNSUPPORTED_MEDIA_MODALITY` — native `/api/chat` supports only base64 `images[]`, so\n *   its \"unsupported\" set is wider than the OpenAI battery's (audio, document, and video all fall\n *   through here under `unsupportedMediaPolicy: 'throw'`).\n *\n * Malformed NDJSON lines are NOT a distinct exception — they are swallowed and surfaced via\n * `helpers.log.trace` (matching the OpenAI battery's `sse-parse-failure` policy); only a transport\n * throw mid-stream raises `E_OLLAMA_STREAM_ERROR`.\n */\n\nimport { createException } from '@nhtio/adk/factories'\n\n/**\n * Thrown when the resolved adapter options (constructor, executor overrides, or per-dispatch\n * `stash.ollama`) fail validation against `ollamaOptionsSchema`.\n */\nexport const E_INVALID_OLLAMA_OPTIONS = createException<[string]>(\n  'E_INVALID_OLLAMA_OPTIONS',\n  'Invalid Ollama adapter options: %s',\n  'E_INVALID_OLLAMA_OPTIONS',\n  529,\n  true\n)\n\n/**\n * Thrown when the total token weight of the resolved request exceeds `contextWindow`. Only raised\n * when `tokenEncoding` is non-null. Carries `{ total, contextWindow, tokenEncoding, perBucket }` in\n * the message so middleware can target shedding.\n *\n * @remarks\n * This is the ADK-side token-budget guard and is independent of Ollama's server-side `num_ctx`\n * (KV-cache size) runtime option; the adapter does not auto-sync the two.\n */\nexport const E_OLLAMA_CONTEXT_OVERFLOW = createException<[number, number, string, string]>(\n  'E_OLLAMA_CONTEXT_OVERFLOW',\n  'Ollama request token weight (%d) exceeds context window (%d) under encoding %s. Per-bucket breakdown: %s',\n  'E_OLLAMA_CONTEXT_OVERFLOW',\n  529,\n  true\n)\n\n/**\n * Thrown when the upstream `/api/chat` endpoint returns a non-2xx response. Non-fatal — surfaced\n * via `ctx.nack(...)` so middleware can decide retry / fail.\n */\nexport const E_OLLAMA_HTTP_ERROR = createException<[number, string]>(\n  'E_OLLAMA_HTTP_ERROR',\n  'Ollama HTTP error %d: %s',\n  'E_OLLAMA_HTTP_ERROR',\n  502,\n  false\n)\n\n/**\n * Thrown when the NDJSON stream emits a transport-level failure mid-stream (the reader throws).\n * Non-fatal — surfaced via `ctx.nack(...)`. A single malformed NDJSON line is NOT this error — it\n * is swallowed + logged at `trace`.\n */\nexport const E_OLLAMA_STREAM_ERROR = createException<[string]>(\n  'E_OLLAMA_STREAM_ERROR',\n  'Ollama stream error: %s',\n  'E_OLLAMA_STREAM_ERROR',\n  502,\n  false\n)\n\n/**\n * Thrown when the NDJSON stream goes silent for longer than `streamIdleTimeoutMs`. Non-fatal —\n * surfaced via `ctx.nack(...)` so middleware can recover.\n */\nexport const E_OLLAMA_STREAM_STALLED = createException<[number]>(\n  'E_OLLAMA_STREAM_STALLED',\n  'Ollama stream stalled (no chunk for %dms)',\n  'E_OLLAMA_STREAM_STALLED',\n  504,\n  false\n)\n\n/**\n * Thrown when the initial request handshake (TCP connect, TLS, response headers) does not complete\n * before `requestTimeoutMs`. Non-fatal — surfaced via `ctx.nack(...)`. Eligible for retry on the\n * same footing as a retriable 5xx.\n */\nexport const E_OLLAMA_REQUEST_TIMEOUT = createException<[number]>(\n  'E_OLLAMA_REQUEST_TIMEOUT',\n  'Ollama request timed out after %dms (before response headers)',\n  'E_OLLAMA_REQUEST_TIMEOUT',\n  504,\n  false\n)\n\n/**\n * Raised when a tool-call's `arguments` value emitted by the model is present but is not a JSON\n * object (e.g. an array, `null`, or a primitive).\n *\n * @remarks\n * Non-fatal. Native `/api/chat` delivers `arguments` already parsed as an object, so — unlike the\n * OpenAI battery — there is no JSON-parse failure mode; this fires only on the not-an-object case.\n * The adapter does NOT throw it: it instantiates it inside `executeAndPersistToolCall`, pulls\n * `.message` into a {@link @nhtio/adk!Tokenizable}, and persists a `ToolCall` record with\n * `isError: true` so the model can self-correct on the next iteration.\n *\n * Printf args: `[reasonHeadline, rawArgs]`.\n *   - `reasonHeadline` — short reason such as `'must be a JSON object; received array'`.\n *   - `rawArgs` — `JSON.stringify(arguments)` of what the model sent, echoed back so it can see it.\n */\nexport const E_OLLAMA_INVALID_TOOL_CALL_ARGS = createException<[string, string]>(\n  'E_OLLAMA_INVALID_TOOL_CALL_ARGS',\n  'Tool arguments %s. Raw value: %s',\n  'E_OLLAMA_INVALID_TOOL_CALL_ARGS',\n  422,\n  false\n)\n\n/**\n * Raised when a {@link @nhtio/adk!Media} instance whose modality cannot be natively represented in\n * the Ollama `/api/chat` wire format reaches the adapter under `unsupportedMediaPolicy: 'throw'`.\n *\n * @remarks\n * Native `/api/chat` supports only base64 `images[]` — it has no audio, document, or video\n * representation — so this triggers for every non-image modality (a wider set than the OpenAI\n * battery, which natively supports audio and document blocks). Consumers can opt out of the throw\n * by switching to `'fallback-stash'` or `'synthetic-description'`.\n *\n * Printf args: `[kind, mimeType, filename]`.\n */\nexport const E_OLLAMA_UNSUPPORTED_MEDIA_MODALITY = createException<[string, string, string]>(\n  'E_OLLAMA_UNSUPPORTED_MEDIA_MODALITY',\n  'Ollama /api/chat natively supports only images; media of kind %s (mime=%s, filename=%s) is unsupported. Configure adapter `unsupportedMediaPolicy` to `fallback-stash` or `synthetic-description` to handle this case.',\n  'E_OLLAMA_UNSUPPORTED_MEDIA_MODALITY',\n  422,\n  true\n)\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA8BA,IAAa,2BAA2B,gBACtC,4BACA,sCACA,4BACA,KACA,IACF;;;;;;;;;;AAWA,IAAa,4BAA4B,gBACvC,6BACA,4GACA,6BACA,KACA,IACF;;;;;AAMA,IAAa,sBAAsB,gBACjC,uBACA,4BACA,uBACA,KACA,KACF;;;;;;AAOA,IAAa,wBAAwB,gBACnC,yBACA,2BACA,yBACA,KACA,KACF;;;;;AAMA,IAAa,0BAA0B,gBACrC,2BACA,6CACA,2BACA,KACA,KACF;;;;;;AAOA,IAAa,2BAA2B,gBACtC,4BACA,iEACA,4BACA,KACA,KACF;;;;;;;;;;;;;;;;AAiBA,IAAa,kCAAkC,gBAC7C,mCACA,oCACA,mCACA,KACA,KACF;;;;;;;;;;;;;AAcA,IAAa,sCAAsC,gBACjD,uCACA,0NACA,uCACA,KACA,IACF"}