@nemesis-oss/ollama-sdk 1.15.0 → 1.17.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.cts CHANGED
@@ -1,10 +1,10 @@
1
1
  import { T as ThinkingMetadata, M as ModelResponse, R as RequestCancellationOptions, S as ShowRequestOptions, a as ShowResponse, P as PullRequestOptions, b as ProgressResponse, c as PushRequestOptions, C as CreateRequestOptions, D as DeleteRequestOptions, d as StatusResponse, e as CopyRequestOptions, f as PsResponse, V as VersionResponse, g as ToolParameters, h as ThinkValue, i as ToolCall, j as ChatRequestOptions, k as ModelOptions, l as ToolDefinition, F as FormatOption, m as ChatResponse, n as Message, G as GenerateRequestOptions, o as GenerateResponse, E as EmbedRequestOptions, p as EmbedResponse, q as EmbeddingsRequestOptions, r as EmbeddingsResponse, W as WebSearchRequestOptions, s as WebSearchResponse, t as WebFetchRequestOptions, u as WebFetchResponse, U as UsageRequestOptions, B as BalanceRequestOptions, v as PingResult, w as VisionInput } from './types-DbPnfVh-.cjs';
2
2
  export { L as ListResponse, x as Logprob, y as LogprobToken, z as ModelDetails, A as Role, H as ToolCallFunction, I as ToolFunctionDefinition, J as ToolProperty, K as UsageRange, N as UsageScope, O as WebSearchResult, Q as imageStringNeedsResolution, X as resolveImageInput } from './types-DbPnfVh-.cjs';
3
- import { H as HttpClient, F as FetchLike, M as Middleware, L as Logger, R as RequestLifecycleHook, c as OllamaStreamEvent, A as AbortableAsyncIterable, d as OllamaStreamEventType, P as ProgressStreamResult, B as BinaryBody, O as OllamaRuntime, C as ChatStreamResult, G as GenerateStreamResult, e as RawUsageSource, T as TokenUsage } from './runtime-C4RRHqOs.cjs';
4
- export { D as DoneEvent, E as ErrorEvent, f as HttpBody, g as HttpClientOptions, h as HttpRequestOptions, i as LifecycleErrorEvent, j as LifecycleRetryEvent, k as LifecycleStartEvent, l as LifecycleSuccessEvent, m as LogFn, n as MessageEvent, o as MiddlewareContext, N as NANOS_PER_MS, p as NANOS_PER_SECOND, q as NOOP_LOGGER, r as NextFunction, s as OllamaAbortError, t as OllamaAgentMaxIterationsError, u as OllamaAgentMaxToolCallsError, v as OllamaAgentToolLoopError, w as OllamaAuthError, x as OllamaBadGatewayError, y as OllamaClientError, z as OllamaClientErrorOptions, J as OllamaErrorRequestContext, K as OllamaErrorResponseContext, Q as OllamaGenericClientError, U as OllamaIncompatibleModelError, V as OllamaMcpError, W as OllamaModelRoutingError, X as OllamaNetworkError, Y as OllamaNotFoundError, Z as OllamaOverloadedError, _ as OllamaQuotaExceededError, $ as OllamaRateLimitError, a0 as OllamaRequestTooLargeError, a1 as OllamaRequestValidationError, a2 as OllamaResponseValidationError, a3 as OllamaServerError, a4 as OllamaServerVersionUnknownError, a5 as OllamaSkillInvalidError, a6 as OllamaSkillNotFoundError, a7 as OllamaStreamError, a8 as OllamaTimeoutError, a9 as OllamaToolTimeoutError, aa as OllamaToolValidationError, ab as OllamaUnsupportedCapabilityError, ac as RequestContext, ad as RequestLifecycleEvent, ae as ResponseContext, af as SseEvent, ag as ThinkingEvent, ah as TokenEvent, ai as ToolCallEvent, aj as composeMiddleware, ak as createConsoleLogger, al as extractUsage, am as mapError, an as parseSseStream } from './runtime-C4RRHqOs.cjs';
3
+ import { H as HttpClient, F as FetchLike, M as Middleware, L as Logger, R as RequestLifecycleHook, c as OllamaStreamEvent, A as AbortableAsyncIterable, d as OllamaStreamEventType, P as ProgressStreamResult, B as BinaryBody, O as OllamaRuntime, C as ChatStreamResult, G as GenerateStreamResult, e as RawUsageSource, T as TokenUsage } from './runtime-CdZP-N7j.cjs';
4
+ export { D as DoneEvent, E as ErrorEvent, f as HttpBody, g as HttpClientOptions, h as HttpRequestOptions, i as LifecycleErrorEvent, j as LifecycleRetryEvent, k as LifecycleStartEvent, l as LifecycleSuccessEvent, m as LogFn, n as MessageEvent, o as MiddlewareContext, N as NANOS_PER_MS, p as NANOS_PER_SECOND, q as NETWORK_ERROR_CODES, r as NOOP_LOGGER, s as NextFunction, t as OllamaAbortError, u as OllamaAgentMaxIterationsError, v as OllamaAgentMaxToolCallsError, w as OllamaAgentToolLoopError, x as OllamaAuthError, y as OllamaBadGatewayError, z as OllamaClientError, J as OllamaClientErrorOptions, K as OllamaErrorRequestContext, Q as OllamaErrorResponseContext, U as OllamaGenericClientError, V as OllamaIncompatibleModelError, W as OllamaMcpError, X as OllamaModelRoutingError, Y as OllamaNetworkError, Z as OllamaNotFoundError, _ as OllamaOverloadedError, $ as OllamaQuotaExceededError, a0 as OllamaRateLimitError, a1 as OllamaRequestTooLargeError, a2 as OllamaRequestValidationError, a3 as OllamaResponseValidationError, a4 as OllamaServerError, a5 as OllamaServerVersionUnknownError, a6 as OllamaSkillInvalidError, a7 as OllamaSkillNotFoundError, a8 as OllamaStreamError, a9 as OllamaTimeoutError, aa as OllamaToolTimeoutError, ab as OllamaToolValidationError, ac as OllamaUnsupportedCapabilityError, ad as RequestContext, ae as RequestLifecycleEvent, af as ResponseContext, ag as SseEvent, ah as ThinkingEvent, ai as TokenEvent, aj as ToolCallEvent, ak as composeMiddleware, al as createConsoleLogger, am as extractUsage, an as isNetworkLevelError, ao as mapError, ap as parseSseStream } from './runtime-CdZP-N7j.cjs';
5
5
  import { z } from 'zod';
6
- import { R as RequestRunner, S as SystemOneChoiceAnswer, o as SystemOneNoulAnswer, p as SystemOneScoreAnswer, q as SystemOneChoiceQuestion, r as SystemOneNoulQuestion, s as SystemOneScoreQuestion, t as SystemOneRequest$1, u as SystemOneResponse$1, v as SystemOneContent, N as NativeApi, w as OpenAICompatClient, x as AnthropicCompatClient } from './anthropic-UeaVjLeP.cjs';
7
- export { y as AnthropicCacheControl, z as AnthropicContentBlock, B as AnthropicImageContentBlock, C as AnthropicMessage, n as AnthropicMessageStreamEvent, A as AnthropicMessagesRequest, m as AnthropicMessagesResponse, D as AnthropicMessagesStream, E as AnthropicMetadata, F as AnthropicOutputConfig, G as AnthropicRedactedThinkingContentBlock, H as AnthropicSystem, I as AnthropicSystemTextBlock, J as AnthropicTextContentBlock, K as AnthropicThinkingConfig, L as AnthropicThinkingContentBlock, M as AnthropicTool, P as AnthropicToolChoice, Q as AnthropicToolResultContentBlock, T as AnthropicToolUseContentBlock, U as AnthropicUnknownStreamEvent, V as OllamaAnthropicContentBlock, W as OllamaAnthropicMessage, X as OllamaAnthropicMessagesRequest, Y as OllamaAnthropicSystem, Z as OllamaAnthropicSystemTextBlock, _ as OllamaAnthropicTextContentBlock, $ as OllamaAnthropicThinkingConfig, a0 as OllamaAnthropicTool, a1 as OllamaAnthropicToolResultContentBlock, a2 as OllamaOpenAIChatCompletionRequest, a3 as OllamaOpenAIChatContentPart, a4 as OllamaOpenAIChatMessage, a5 as OllamaOpenAICompletionRequest, a6 as OllamaOpenAIEmbeddingRequest, a7 as OllamaOpenAIImageDataUrl, a8 as OllamaOpenAIReasoningEffort, a9 as OllamaOpenAIResponsesRequest, aa as OpenAIChatCompletionChoice, b as OpenAIChatCompletionChunk, ab as OpenAIChatCompletionChunkChoice, ac as OpenAIChatCompletionDelta, O as OpenAIChatCompletionRequest, a as OpenAIChatCompletionResponse, ad as OpenAIChatCompletionStream, ae as OpenAICompletionChoice, e as OpenAICompletionChunk, c as OpenAICompletionRequest, d as OpenAICompletionResponse, af as OpenAICompletionStream, ag as OpenAIContentPart, ah as OpenAIEmbeddingInput, ai as OpenAIEmbeddingItem, f as OpenAIEmbeddingRequest, g as OpenAIEmbeddingResponse, aj as OpenAIFunctionDefinition, ak as OpenAIImageUrlContentPart, h as OpenAIListModelsResponse, al as OpenAIMessage, i as OpenAIModelItem, am as OpenAIReasoningEffort, an as OpenAIResponseFormat, ao as OpenAIResponsesCompletedEvent, ap as OpenAIResponsesContentPartAddedEvent, aq as OpenAIResponsesContentPartDoneEvent, ar as OpenAIResponsesCreatedEvent, as as OpenAIResponsesError, at as OpenAIResponsesEventBase, au as OpenAIResponsesFailedEvent, av as OpenAIResponsesFunctionCallArgumentsDeltaEvent, aw as OpenAIResponsesFunctionCallArgumentsDoneEvent, ax as OpenAIResponsesInProgressEvent, ay as OpenAIResponsesIncompleteDetails, az as OpenAIResponsesIncompleteEvent, aA as OpenAIResponsesOutputContent, aB as OpenAIResponsesOutputFunctionCall, aC as OpenAIResponsesOutputItem, aD as OpenAIResponsesOutputItemAddedEvent, aE as OpenAIResponsesOutputItemDoneEvent, aF as OpenAIResponsesOutputMessage, aG as OpenAIResponsesOutputReasoning, aH as OpenAIResponsesOutputRefusal, aI as OpenAIResponsesOutputTextContent, aJ as OpenAIResponsesOutputTextDeltaEvent, aK as OpenAIResponsesOutputTextDoneEvent, aL as OpenAIResponsesQueuedEvent, aM as OpenAIResponsesReasoningSummaryPartAddedEvent, aN as OpenAIResponsesReasoningSummaryPartDoneEvent, aO as OpenAIResponsesReasoningSummaryTextDeltaEvent, aP as OpenAIResponsesReasoningSummaryTextDoneEvent, aQ as OpenAIResponsesReasoningTextDeltaEvent, aR as OpenAIResponsesReasoningTextDoneEvent, aS as OpenAIResponsesRefusalDeltaEvent, aT as OpenAIResponsesRefusalDoneEvent, j as OpenAIResponsesRequest, k as OpenAIResponsesResponse, aU as OpenAIResponsesStatus, aV as OpenAIResponsesStream, aW as OpenAIResponsesStreamError, l as OpenAIResponsesStreamEvent, aX as OpenAIResponsesUsage, aY as OpenAIStreamOptions, aZ as OpenAITextContentPart, a_ as OpenAITool, a$ as OpenAIToolCall, b0 as OpenAIToolCallDelta, b1 as OpenAIUsage, b2 as SystemOneConfidence, b3 as SystemOneProbabilities } from './anthropic-UeaVjLeP.cjs';
6
+ import { R as RequestRunner, S as SystemOneChoiceAnswer, o as SystemOneNoulAnswer, p as SystemOneScoreAnswer, q as SystemOneChoiceQuestion, r as SystemOneNoulQuestion, s as SystemOneScoreQuestion, t as SystemOneRequest$1, u as SystemOneResponse$1, v as SystemOneContent, N as NativeApi, w as OpenAICompatClient, x as AnthropicCompatClient } from './anthropic-AKbXnWJU.cjs';
7
+ export { y as AnthropicCacheControl, z as AnthropicContentBlock, B as AnthropicImageContentBlock, C as AnthropicMessage, n as AnthropicMessageStreamEvent, A as AnthropicMessagesRequest, m as AnthropicMessagesResponse, D as AnthropicMessagesStream, E as AnthropicMetadata, F as AnthropicOutputConfig, G as AnthropicRedactedThinkingContentBlock, H as AnthropicSystem, I as AnthropicSystemTextBlock, J as AnthropicTextContentBlock, K as AnthropicThinkingConfig, L as AnthropicThinkingContentBlock, M as AnthropicTool, P as AnthropicToolChoice, Q as AnthropicToolResultContentBlock, T as AnthropicToolUseContentBlock, U as AnthropicUnknownStreamEvent, V as OllamaAnthropicContentBlock, W as OllamaAnthropicMessage, X as OllamaAnthropicMessagesRequest, Y as OllamaAnthropicSystem, Z as OllamaAnthropicSystemTextBlock, _ as OllamaAnthropicTextContentBlock, $ as OllamaAnthropicThinkingConfig, a0 as OllamaAnthropicTool, a1 as OllamaAnthropicToolResultContentBlock, a2 as OllamaOpenAIChatCompletionRequest, a3 as OllamaOpenAIChatContentPart, a4 as OllamaOpenAIChatMessage, a5 as OllamaOpenAICompletionRequest, a6 as OllamaOpenAIEmbeddingRequest, a7 as OllamaOpenAIImageDataUrl, a8 as OllamaOpenAIReasoningEffort, a9 as OllamaOpenAIResponsesRequest, aa as OpenAIChatCompletionChoice, b as OpenAIChatCompletionChunk, ab as OpenAIChatCompletionChunkChoice, ac as OpenAIChatCompletionDelta, O as OpenAIChatCompletionRequest, a as OpenAIChatCompletionResponse, ad as OpenAIChatCompletionStream, ae as OpenAICompletionChoice, e as OpenAICompletionChunk, c as OpenAICompletionRequest, d as OpenAICompletionResponse, af as OpenAICompletionStream, ag as OpenAIContentPart, ah as OpenAIEmbeddingInput, ai as OpenAIEmbeddingItem, f as OpenAIEmbeddingRequest, g as OpenAIEmbeddingResponse, aj as OpenAIFunctionDefinition, ak as OpenAIImageUrlContentPart, h as OpenAIListModelsResponse, al as OpenAIMessage, i as OpenAIModelItem, am as OpenAIReasoningEffort, an as OpenAIResponseFormat, ao as OpenAIResponsesCompletedEvent, ap as OpenAIResponsesContentPartAddedEvent, aq as OpenAIResponsesContentPartDoneEvent, ar as OpenAIResponsesCreatedEvent, as as OpenAIResponsesError, at as OpenAIResponsesEventBase, au as OpenAIResponsesFailedEvent, av as OpenAIResponsesFunctionCallArgumentsDeltaEvent, aw as OpenAIResponsesFunctionCallArgumentsDoneEvent, ax as OpenAIResponsesInProgressEvent, ay as OpenAIResponsesIncompleteDetails, az as OpenAIResponsesIncompleteEvent, aA as OpenAIResponsesOutputContent, aB as OpenAIResponsesOutputFunctionCall, aC as OpenAIResponsesOutputItem, aD as OpenAIResponsesOutputItemAddedEvent, aE as OpenAIResponsesOutputItemDoneEvent, aF as OpenAIResponsesOutputMessage, aG as OpenAIResponsesOutputReasoning, aH as OpenAIResponsesOutputRefusal, aI as OpenAIResponsesOutputTextContent, aJ as OpenAIResponsesOutputTextDeltaEvent, aK as OpenAIResponsesOutputTextDoneEvent, aL as OpenAIResponsesQueuedEvent, aM as OpenAIResponsesReasoningSummaryPartAddedEvent, aN as OpenAIResponsesReasoningSummaryPartDoneEvent, aO as OpenAIResponsesReasoningSummaryTextDeltaEvent, aP as OpenAIResponsesReasoningSummaryTextDoneEvent, aQ as OpenAIResponsesReasoningTextDeltaEvent, aR as OpenAIResponsesReasoningTextDoneEvent, aS as OpenAIResponsesRefusalDeltaEvent, aT as OpenAIResponsesRefusalDoneEvent, j as OpenAIResponsesRequest, k as OpenAIResponsesResponse, aU as OpenAIResponsesStatus, aV as OpenAIResponsesStream, aW as OpenAIResponsesStreamError, l as OpenAIResponsesStreamEvent, aX as OpenAIResponsesUsage, aY as OpenAIStreamOptions, aZ as OpenAITextContentPart, a_ as OpenAITool, a$ as OpenAIToolCall, b0 as OpenAIToolCallDelta, b1 as OpenAIUsage, b2 as SystemOneConfidence, b3 as SystemOneProbabilities } from './anthropic-AKbXnWJU.cjs';
8
8
  import { T as ToolHandler, a as Tool, A as AnyTool, b as ToolRegistry, c as ToolExecutionResult } from './compose-DFkmtUOP.cjs';
9
9
  export { d as AppliedSkillResult, e as ApplySkillOptions, P as ParsedFrontmatter, S as Skill, f as SkillFrontmatter, g as SkillSummary, h as ToolExecutionContext, i as ToolExecutionFailure, j as ToolExecutionSuccess, k as ToolRegistryOptions, l as applySkill, p as parseFrontmatter } from './compose-DFkmtUOP.cjs';
10
10
  import { M as McpClientLike, a as McpToolDescriptor, b as McpTask, c as McpCallToolResult } from './types-CI-gM2u5.cjs';
@@ -970,8 +970,8 @@ declare class ModelsClient {
970
970
  * model-not-found (404), or VRAM-exhaustion failures.
971
971
  */
972
972
  pin(model: string): Promise<void>;
973
- createBlob(digest: string, data: BinaryBody): Promise<void>;
974
- checkBlob(digest: string): Promise<boolean>;
973
+ createBlob(digest: string, data: BinaryBody, request?: RequestCancellationOptions): Promise<void>;
974
+ checkBlob(digest: string, request?: RequestCancellationOptions): Promise<boolean>;
975
975
  /**
976
976
  * Computes a blob digest the way Ollama's content-addressed store expects:
977
977
  * SHA-256 over the raw bytes, formatted as `sha256:<64 lowercase hex>`.
@@ -992,17 +992,44 @@ declare class ModelsClient {
992
992
  */
993
993
  createBlobFromData(data: Uint8Array, opts?: {
994
994
  readonly digest?: string | undefined;
995
- readonly signal?: AbortSignal | undefined;
996
- }): Promise<BlobUploadResult>;
995
+ } & RequestCancellationOptions): Promise<BlobUploadResult>;
996
+ /**
997
+ * Streams a blob upload straight from a web `ReadableStream` — the
998
+ * multi-gigabyte-safe counterpart of {@link createBlobFromData}. The body
999
+ * is never materialized in memory: the transport hands the stream to
1000
+ * `fetch` directly (setting `duplex: 'half'`, which Node 18+ requires for
1001
+ * stream bodies), so peak heap usage is a transport-level chunk, not the
1002
+ * blob size.
1003
+ *
1004
+ * The digest must be known up front — it *is* the upload URL
1005
+ * (`/api/blobs/<digest>`), the same content-addressed protocol
1006
+ * `curl -T` performs. Hash first when you don't have it: in-memory bytes
1007
+ * via {@link computeBlobDigest}, files via {@link createBlobFromFile}
1008
+ * (which hashes in a streaming pass). The server verifies the streamed
1009
+ * bytes against the digest and rejects mismatches, so a corrupted or
1010
+ * truncated stream fails the upload instead of poisoning the store.
1011
+ */
1012
+ createBlobFromStream(digest: string, stream: ReadableStream<Uint8Array>, request?: RequestCancellationOptions): Promise<BlobUploadResult>;
997
1013
  /**
998
1014
  * Reads a local file and uploads it as a blob (Node.js only — dynamic
999
1015
  * `node:fs` import, so browser bundles never pull it in). The file name
1000
1016
  * (basename) is what `/api/create`'s `files` map expects as the key, so
1001
1017
  * this returns it alongside the digest.
1018
+ *
1019
+ * Streams both passes in fixed-size chunks — peak heap is O(chunk), not
1020
+ * O(file), so multi-gigabyte GGUFs never enter the Node heap:
1021
+ *
1022
+ * 1. **Hash pass** (skipped when `opts.digest` is provided): the file is
1023
+ * streamed through `node:crypto`'s incremental SHA-256.
1024
+ * 2. **Upload pass**: `node:fs` createReadStream → web ReadableStream →
1025
+ * `fetch` body with `duplex: 'half'` (see {@link createBlobFromStream}).
1026
+ *
1027
+ * A pre-existing blob (HEAD 200) skips the upload pass entirely, same as
1028
+ * {@link createBlobFromData}.
1002
1029
  */
1003
1030
  createBlobFromFile(path: string, opts?: {
1004
- readonly signal?: AbortSignal | undefined;
1005
- }): Promise<BlobUploadResult & {
1031
+ readonly digest?: string | undefined;
1032
+ } & RequestCancellationOptions): Promise<BlobUploadResult & {
1006
1033
  readonly fileName: string;
1007
1034
  }>;
1008
1035
  /**
@@ -2277,8 +2304,8 @@ declare class OllamaClient {
2277
2304
  */
2278
2305
  readonly getContextLength: (request: ContextDiscoveryRequestOptions) => Promise<DiscoveredContextLength>;
2279
2306
  readonly version: () => Promise<VersionResponse>;
2280
- readonly createBlob: (digest: string, data: BinaryBody) => Promise<void>;
2281
- readonly checkBlob: (digest: string) => Promise<boolean>;
2307
+ readonly createBlob: (digest: string, data: BinaryBody, request?: RequestCancellationOptions) => Promise<void>;
2308
+ readonly checkBlob: (digest: string, request?: RequestCancellationOptions) => Promise<boolean>;
2282
2309
  /**
2283
2310
  * Immediately unloads `model` from VRAM. Convenience alias for
2284
2311
  * {@link ModelsClient.unload} — see that method for the full
@@ -2780,6 +2807,140 @@ declare function toDataStream<TChunk, TFinal>(stream: OllamaStream<TChunk, TFina
2780
2807
  */
2781
2808
  declare function toResponse<TChunk, TFinal>(stream: OllamaStream<TChunk, TFinal>, init?: ResponseInit): Response;
2782
2809
 
2810
+ /**
2811
+ * Zero-copy stream observation — the safe alternative to `ReadableStream.tee()`
2812
+ * for multi-sink consumption (audit logging, token counting, telemetry taps).
2813
+ *
2814
+ * ## The `tee()` hazard this exists for
2815
+ *
2816
+ * Developers streaming to a UI while also feeding a logger (or a token
2817
+ * counter, or an OpenTelemetry exporter) typically reach for:
2818
+ *
2819
+ * ```ts
2820
+ * // VULNERABLE PATTERN — do not do this
2821
+ * const [forUi, forLogger] = response.body.tee();
2822
+ * ```
2823
+ *
2824
+ * WHATWG `tee()` branches share a single upstream pull mechanism: a branch
2825
+ * that reads slower than its sibling forces every intermediate chunk to be
2826
+ * retained in that branch's internal queue until it catches up. If the slow
2827
+ * consumer is a logger waiting on a database write — or never drains at all —
2828
+ * the queue grows without bound, and because both branches apply shared
2829
+ * backpressure, the *fast* consumer is throttled by the slowest one. In a
2830
+ * long-lived streaming server this is a slow, silent heap leak.
2831
+ *
2832
+ * ## What `observeStream` does instead
2833
+ *
2834
+ * A single-pass wrapper: chunks flow from the source through the observer
2835
+ * callbacks to the primary consumer with **no second queue and no forked
2836
+ * stream**. The contract is deliberately one-sided in favor of the primary
2837
+ * consumer:
2838
+ *
2839
+ * - **Never blocks.** Observer callbacks run on a serialized microtask
2840
+ * chain, not on the hot path. A slow async observer (a logger waiting on
2841
+ * I/O) delays only its own tail — the primary consumer keeps receiving
2842
+ * chunks at source speed.
2843
+ * - **Never fails.** An observer that throws (sync) or rejects (async) is
2844
+ * routed to `onListenerError` and otherwise discarded. Observer failures
2845
+ * can never poison the primary pipeline.
2846
+ * - **Never leaks via the wrapper.** There is no second branch to
2847
+ * backpressure. The only retained state is one closure per backlogged
2848
+ * observer callback; if your observer can stall *forever*, bound it on
2849
+ * your side (queue, drop policy) — the wrapper will keep enqueueing
2850
+ * otherwise, exactly like any fire-and-forget log sink.
2851
+ * - **Ordered.** Observer callbacks run strictly FIFO, one at a time —
2852
+ * async sinks see chunks in stream order, and `onDone` is enqueued only
2853
+ * after the last chunk's callback settles. (This is intentionally
2854
+ * stricter than a naive `void onChunk(chunk)` fire-and-forget, which
2855
+ * runs concurrent, out-of-order observers and turns a rejected async
2856
+ * callback into an unhandled rejection.)
2857
+ *
2858
+ * ## Lifecycle
2859
+ *
2860
+ * - Source completes naturally → `onDone` (enqueued after the last chunk
2861
+ * callback settles, so a logger has flushed everything it saw).
2862
+ * - Source throws → `onError(err)` immediately, then the error re-throws to
2863
+ * the primary consumer (the observer is transparent, not a circuit
2864
+ * breaker).
2865
+ * - Primary consumer stops early (`break` / `return` / `throw` inside the
2866
+ * `for await` loop) → `onAbandon()` immediately. This is distinct from
2867
+ * completion: the source's iterator-`return()` has already closed the
2868
+ * underlying transport by the time `onAbandon` fires.
2869
+ *
2870
+ * Works over any `AsyncIterable<T>` — the SDK's `OllamaStream` (streaming
2871
+ * `chat`/`generate`), the raw NDJSON chunk iterables, or your own sources.
2872
+ * Note that `OllamaStream` itself is dual-mode but mode-exclusive (`.on()`
2873
+ * listeners cannot be combined with `for await`); `observeStream` is the
2874
+ * supported way to tap a stream you are already iterating.
2875
+ */
2876
+ /**
2877
+ * Lifecycle phase an observer failure came from. `'chunk'` and `'done'`
2878
+ * failures arrive from the serialized callback chain; `'error'` and
2879
+ * `'abandon'` failures come from those terminal hooks themselves.
2880
+ */
2881
+ type StreamObserverPhase = 'chunk' | 'done' | 'error' | 'abandoned';
2882
+ /** Passive, best-effort observer hooks — see the module docs for semantics. */
2883
+ interface StreamObserverCallbacks<T> {
2884
+ /**
2885
+ * Called once per chunk, in stream order, FIFO, never overlapping with a
2886
+ * previous still-running call. May be sync (`void`) or async
2887
+ * (`Promise<void>`); async return values are awaited *on the observer
2888
+ * chain only* — they never delay the primary consumer. Rejections (and
2889
+ * sync throws) route to {@link StreamObserverCallbacks.onListenerError}.
2890
+ */
2891
+ onChunk?: ((chunk: T) => void | Promise<void>) | undefined;
2892
+ /**
2893
+ * Called with the source's error immediately before that error is re-thrown
2894
+ * to the primary consumer. A throwing `onError` hook cannot mask the
2895
+ * original error — it is reported via {@link StreamObserverCallbacks.onListenerError}
2896
+ * and the original error still propagates.
2897
+ */
2898
+ onError?: ((error: unknown) => void) | undefined;
2899
+ /**
2900
+ * Called when the source completes naturally — enqueued after the last
2901
+ * chunk's observer callback settles, so a flushed logger has seen every
2902
+ * chunk before its "done" marker.
2903
+ */
2904
+ onDone?: (() => void | Promise<void>) | undefined;
2905
+ /**
2906
+ * Called when the primary consumer stops early (`break`/`return`/`throw` in
2907
+ * the consuming loop). The source has already been closed via the
2908
+ * iterator-`return()` protocol when this fires. `onDone` does not fire in
2909
+ * this case.
2910
+ */
2911
+ onAbandon?: (() => void) | undefined;
2912
+ /**
2913
+ * The observation failure channel: any observer hook that throws or rejects
2914
+ * is reported here together with the phase it came from, and then
2915
+ * discarded. If this hook itself throws, the failure is swallowed — an
2916
+ * error reporter must never become the next unhandled rejection.
2917
+ */
2918
+ onListenerError?: ((error: unknown, phase: StreamObserverPhase) => void) | undefined;
2919
+ }
2920
+ /**
2921
+ * Wraps an `AsyncIterable` with passive observer hooks without creating a
2922
+ * secondary buffered stream queue — avoiding the memory retention and shared
2923
+ * backpressure penalties of `ReadableStream.tee()`.
2924
+ *
2925
+ * @example
2926
+ * ```ts
2927
+ * const stream = await client.chat({ model: 'llama4', messages, stream: true });
2928
+ * let tokens = 0;
2929
+ * const observed = observeStream(stream, {
2930
+ * onChunk: (event) => {
2931
+ * if (event.type === 'token') tokens += 1;
2932
+ * auditLog.write(event); // slow async sink — never delays the consumer
2933
+ * },
2934
+ * onDone: () => auditLog.flush(),
2935
+ * onListenerError: (err) => telemetry.count('audit_log_failure', err),
2936
+ * });
2937
+ * for await (const event of observed) {
2938
+ * ui.render(event); // primary consumer — full speed, full order
2939
+ * }
2940
+ * ```
2941
+ */
2942
+ declare function observeStream<T>(source: AsyncIterable<T>, callbacks?: StreamObserverCallbacks<T>): AsyncGenerator<T, void, unknown>;
2943
+
2783
2944
  /**
2784
2945
  * Client-side usage budgeting.
2785
2946
  *
@@ -3695,7 +3856,33 @@ interface ContextCheck {
3695
3856
  readonly windowSource: ContextWindowSource;
3696
3857
  /** True when the estimate crossed the warning threshold of the effective window. */
3697
3858
  readonly exceedsThreshold: boolean;
3859
+ /**
3860
+ * Present only when `exceedsThreshold` is `true`: the smallest power-of-two
3861
+ * window with ~30% generation headroom above the estimate (see
3862
+ * {@link recommendContextWindow}). A starting point, not a verdict — check
3863
+ * the model's native ceiling with `client.models.getContextLength()` before
3864
+ * adopting it; a prompt that outgrows the native maximum needs compaction
3865
+ * or history truncation, not a bigger `num_ctx`.
3866
+ */
3867
+ readonly recommendedNumCtx?: number | undefined;
3698
3868
  }
3869
+ /**
3870
+ * Smallest power-of-two context window that comfortably fits
3871
+ * `estimatedTokens` — ~30% headroom for the response, floored at 4096 (the
3872
+ * common modern server default when `num_ctx` is unset).
3873
+ *
3874
+ * Uses an exact doubling loop rather than
3875
+ * `Math.pow(2, Math.ceil(Math.log2(x)))`: floating-point `log2` of exact
3876
+ * powers of two can land marginally above the integer (e.g.
3877
+ * `Math.log2(8) > 3` on some platforms), which rounds a perfect fit *up* a
3878
+ * whole power of two. The loop has no such boundary bugs.
3879
+ *
3880
+ * Non-finite / non-positive inputs return the 4096 floor rather than throwing:
3881
+ * callers use this to *suggest* a window after their own estimate already
3882
+ * flagged a problem, so garbage-in should degrade to a conservative
3883
+ * suggestion, not a second error path.
3884
+ */
3885
+ declare function recommendContextWindow(estimatedTokens: number): number;
3699
3886
  interface ContextCheckOptions {
3700
3887
  /** Client `defaultContextLength`, applied when the request doesn't set `num_ctx`. */
3701
3888
  readonly defaultContextLength?: number | undefined;
@@ -3862,4 +4049,4 @@ declare class ModelAffinityScheduler {
3862
4049
  /** Convenience factory — `createModelAffinityScheduler(client, opts)` reads better in setups. */
3863
4050
  declare function createModelAffinityScheduler(client: ModelAffinityClient | OllamaClient, options?: ModelAffinitySchedulerOptions): ModelAffinityScheduler;
3864
4051
 
3865
- export { ATTR_GEN_AI_OPERATION_NAME, ATTR_GEN_AI_REQUEST_MODEL, ATTR_GEN_AI_RESPONSE_MODEL, ATTR_GEN_AI_SYSTEM, ATTR_GEN_AI_TOOL_CALL_ID, ATTR_GEN_AI_TOOL_NAME, ATTR_GEN_AI_USAGE_INPUT_TOKENS, ATTR_GEN_AI_USAGE_OUTPUT_TOKENS, ATTR_HTTP_REQUEST_METHOD, ATTR_HTTP_RESPONSE_STATUS_CODE, ATTR_OLLAMA_AGENT_ITERATION, ATTR_OLLAMA_AGENT_MAX_ITERATIONS, ATTR_OLLAMA_ENDPOINT_ATTEMPT, ATTR_OLLAMA_ENDPOINT_NAME, ATTR_SERVER_ADDRESS, ATTR_SERVER_PORT, ATTR_URL_FULL, AbortableAsyncIterable, type AffinityRunningModel, Agent, type AgentChatClient, type AgentConfig, type AgentHooks, type AgentResult, type AgentRunInput, type AgentTurn, type AnswerFor, AnthropicCompatClient, AnyTool, type BackoffOptions, BalanceRequestOptions, type BalanceResponse, type BlobUploadResult, ChatRequestOptions, ChatResponse, ChatStreamResult, type ChoiceDecision, type CompactionOptions, type CompareDraftPerformanceOptions, type ContextCheck, type ContextCheckOptions, type ContextDiscoveryRequestOptions, type ContextLengthSignals, type ContextLengthSource, type ContextWindowSource, type ConversationSendOptions, ConversationSession, type ConversationSessionOptions, CopyRequestOptions, type CreateModelFromGgufOptions, CreateRequestOptions, type CumulativeCacheStats, DEFAULT_BACKOFF, DEFAULT_BASE_URL, DEFAULT_CONTEXT_WARNING_THRESHOLD, DEFAULT_EMBED_BATCH_SIZE, DEFAULT_EMBED_CONCURRENCY, DEFAULT_FAILOVER_CODES, DEFAULT_FETCH_BACKOFF_CONFIG, DEFAULT_RETRY_CONFIG, DEFAULT_TIMEOUT_MS, type Decision, type DefineToolOptions, DeleteRequestOptions, type DiscoveredContextLength, type DraftBenchmarkClient, type DraftPerformanceComparison, type DraftRunStats, type EmbedBatchOptions, type EmbedBatchResult, EmbedRequestOptions, EmbedResponse, EmbeddingsRequestOptions, EmbeddingsResponse, type EndpointHealth, type EndpointHealthCheckResult, EndpointRegistry, type EndpointRegistryOptions, FetchLike, type FetchWithBackoffConfig, FormatOption, type FormattedTelemetry, GEN_AI_SYSTEM_OLLAMA, GenerateRequestOptions, GenerateResponse, GenerateStreamResult, type HistorySanitizeOptions, HttpClient, IMAGE_TOKEN_ESTIMATE, type IncludedBalance, KEEP_ALIVE_INDEFINITE, KEEP_ALIVE_UNLOAD, KNOWN_DONE_REASONS, type KeepAlive, type KnownDoneReason, type LegacyBalanceLimit, type LegacyIncludedBalance, type LoadMcpToolsOptions, Logger, MAX_SYSTEM_ONE_IMAGES_BYTES, MAX_SYSTEM_ONE_REQUEST_BYTES, MESSAGE_OVERHEAD_TOKENS, McpBridge, type McpBridgeOptions, McpCallToolResult, McpClientLike, McpTask, McpToolDescriptor, Message, Middleware, type ModelAffinityClient, ModelAffinityRouter, type ModelAffinityRoutingOptions, ModelAffinityScheduler, type ModelAffinitySchedulerOptions, type ModelAffinitySnapshot, type ModelAffinityStats, type ModelCapabilities, ModelOptions, ModelResponse, ModelsClient, type NoulDecision, OLLAMA_CLOUD_BASE_URL, OLLAMA_FALLBACK_CONTEXT_LENGTH, OllamaClient, type OllamaClientConfig, type OllamaCloudFreeTierQuotaBudgets, type OllamaCredentialConfig, type OllamaEndpoint, OllamaStream, OllamaStreamEvent, OllamaStreamEventType, OpenAICompatClient, type OversizedEmbedInput, type ParallelToolCallBehavior, PingResult, ProgressResponse, ProgressStreamResult, PsResponse, PullRequestOptions, PushRequestOptions, QuotaManager, type QuotaManagerOptions, type QuotaWindowConfig, type QuotaWindowStatus, RETRYABLE_STATUS_CODES, type RankResult, type RawOllamaMetrics, RawUsageSource, RequestCancellationOptions, RequestLifecycleHook, RequestRunner, type ResponsesCreateRequest, type ResponsesCreateResponse, ResponsesModule, type ResponsesReasoningEffort, type ResponsesStreamEvent, type ResponsesToolDefinition, type ResponsesUsage, type RetryConfig, type RuntimeMode, type ScoreDecision, type SessionCompactionOptions, type SessionCompactionResult, type SessionTurn, ShowRequestOptions, ShowResponse, type SpanAttributeValue, type SpanAttributes, StatusResponse, type SupportedSchema, type SystemOneAnswer, type SystemOneAnswers, SystemOneChoiceAnswer, SystemOneChoiceQuestion, SystemOneContent, SystemOneNoulAnswer, SystemOneNoulQuestion, type SystemOneQuestion, type SystemOneQuestions, type SystemOneRequest, type SystemOneResponse, SystemOneScoreAnswer, SystemOneScoreQuestion, type SystemOneUsage, ThinkValue, ThinkingMetadata, type TimeoutSignal, TokenUsage, Tool, ToolCall, ToolCallAccumulator, ToolDefinition, ToolExecutionResult, ToolHandler, ToolParameters, ToolRegistry, type TurnCacheStats, type UsageBucket, type UsageMetrics, UsageRequestOptions, type UsageResponse, VersionResponse, VisionInput, WEB_FETCH_TOOL_NAME, WEB_SEARCH_TOOL_NAME, WebFetchRequestOptions, WebFetchResponse, type WebFetchToolParams, WebSearchRequestOptions, WebSearchResponse, type WebSearchToolParams, type WebToolsClient, type WebToolsOptions, addSpanEvent, batchEmbed, calculateBackoff, canonicalToolCallSignature, checkChatContext, checkEndpointHealth, checkGenerateContext, compactConversationHistory, compareDraftPerformance, contextWarningMessage, createDecision, createModelAffinityScheduler, createOllamaCloudFreeTierQuota, createTimeoutSignal, createWebFetchTool, createWebSearchTool, createWebTools, defineTool, detectModelCapabilities, embedBatchOverflowMessage, encodeImage, estimateChatRequestTokens, estimateGenerateRequestTokens, estimateMessageTokens, estimateSystemOneRequestBytes, estimateTokens, extractContextLength as extractNativeContextLength, extractParameterNumCtx, fetchWithBackoff, findOversizedEmbedInputs, findRunningModelContextLength, formatTelemetry, formatWebFetchResult, formatWebSearchResult, inferRuntimeMode, isKeepAliveSugar, isKnownDoneReason, isModelResident, isSameToolCall, listAllMcpTools, listAvailableModels, loadMcpTools, mergeToolCall, mergeToolCallArgumentsString, mergeToolCallArrays, normalizeChatStream, normalizeGenerateStream, normalizeKeepAlive, normalizeProgressStream, parseNdjsonStream, parseStructuredOutput, registerMcpTools, resolveContextLength, resolveCredentialEndpoints, sanitizeHistoryForNextTurn, setSpanError, toDataStream, toResponse, toTextStream, withRetry, withSpan, zodToJsonSchema };
4052
+ export { ATTR_GEN_AI_OPERATION_NAME, ATTR_GEN_AI_REQUEST_MODEL, ATTR_GEN_AI_RESPONSE_MODEL, ATTR_GEN_AI_SYSTEM, ATTR_GEN_AI_TOOL_CALL_ID, ATTR_GEN_AI_TOOL_NAME, ATTR_GEN_AI_USAGE_INPUT_TOKENS, ATTR_GEN_AI_USAGE_OUTPUT_TOKENS, ATTR_HTTP_REQUEST_METHOD, ATTR_HTTP_RESPONSE_STATUS_CODE, ATTR_OLLAMA_AGENT_ITERATION, ATTR_OLLAMA_AGENT_MAX_ITERATIONS, ATTR_OLLAMA_ENDPOINT_ATTEMPT, ATTR_OLLAMA_ENDPOINT_NAME, ATTR_SERVER_ADDRESS, ATTR_SERVER_PORT, ATTR_URL_FULL, AbortableAsyncIterable, type AffinityRunningModel, Agent, type AgentChatClient, type AgentConfig, type AgentHooks, type AgentResult, type AgentRunInput, type AgentTurn, type AnswerFor, AnthropicCompatClient, AnyTool, type BackoffOptions, BalanceRequestOptions, type BalanceResponse, type BlobUploadResult, ChatRequestOptions, ChatResponse, ChatStreamResult, type ChoiceDecision, type CompactionOptions, type CompareDraftPerformanceOptions, type ContextCheck, type ContextCheckOptions, type ContextDiscoveryRequestOptions, type ContextLengthSignals, type ContextLengthSource, type ContextWindowSource, type ConversationSendOptions, ConversationSession, type ConversationSessionOptions, CopyRequestOptions, type CreateModelFromGgufOptions, CreateRequestOptions, type CumulativeCacheStats, DEFAULT_BACKOFF, DEFAULT_BASE_URL, DEFAULT_CONTEXT_WARNING_THRESHOLD, DEFAULT_EMBED_BATCH_SIZE, DEFAULT_EMBED_CONCURRENCY, DEFAULT_FAILOVER_CODES, DEFAULT_FETCH_BACKOFF_CONFIG, DEFAULT_RETRY_CONFIG, DEFAULT_TIMEOUT_MS, type Decision, type DefineToolOptions, DeleteRequestOptions, type DiscoveredContextLength, type DraftBenchmarkClient, type DraftPerformanceComparison, type DraftRunStats, type EmbedBatchOptions, type EmbedBatchResult, EmbedRequestOptions, EmbedResponse, EmbeddingsRequestOptions, EmbeddingsResponse, type EndpointHealth, type EndpointHealthCheckResult, EndpointRegistry, type EndpointRegistryOptions, FetchLike, type FetchWithBackoffConfig, FormatOption, type FormattedTelemetry, GEN_AI_SYSTEM_OLLAMA, GenerateRequestOptions, GenerateResponse, GenerateStreamResult, type HistorySanitizeOptions, HttpClient, IMAGE_TOKEN_ESTIMATE, type IncludedBalance, KEEP_ALIVE_INDEFINITE, KEEP_ALIVE_UNLOAD, KNOWN_DONE_REASONS, type KeepAlive, type KnownDoneReason, type LegacyBalanceLimit, type LegacyIncludedBalance, type LoadMcpToolsOptions, Logger, MAX_SYSTEM_ONE_IMAGES_BYTES, MAX_SYSTEM_ONE_REQUEST_BYTES, MESSAGE_OVERHEAD_TOKENS, McpBridge, type McpBridgeOptions, McpCallToolResult, McpClientLike, McpTask, McpToolDescriptor, Message, Middleware, type ModelAffinityClient, ModelAffinityRouter, type ModelAffinityRoutingOptions, ModelAffinityScheduler, type ModelAffinitySchedulerOptions, type ModelAffinitySnapshot, type ModelAffinityStats, type ModelCapabilities, ModelOptions, ModelResponse, ModelsClient, type NoulDecision, OLLAMA_CLOUD_BASE_URL, OLLAMA_FALLBACK_CONTEXT_LENGTH, OllamaClient, type OllamaClientConfig, type OllamaCloudFreeTierQuotaBudgets, type OllamaCredentialConfig, type OllamaEndpoint, OllamaStream, OllamaStreamEvent, OllamaStreamEventType, OpenAICompatClient, type OversizedEmbedInput, type ParallelToolCallBehavior, PingResult, ProgressResponse, ProgressStreamResult, PsResponse, PullRequestOptions, PushRequestOptions, QuotaManager, type QuotaManagerOptions, type QuotaWindowConfig, type QuotaWindowStatus, RETRYABLE_STATUS_CODES, type RankResult, type RawOllamaMetrics, RawUsageSource, RequestCancellationOptions, RequestLifecycleHook, RequestRunner, type ResponsesCreateRequest, type ResponsesCreateResponse, ResponsesModule, type ResponsesReasoningEffort, type ResponsesStreamEvent, type ResponsesToolDefinition, type ResponsesUsage, type RetryConfig, type RuntimeMode, type ScoreDecision, type SessionCompactionOptions, type SessionCompactionResult, type SessionTurn, ShowRequestOptions, ShowResponse, type SpanAttributeValue, type SpanAttributes, StatusResponse, type StreamObserverCallbacks, type StreamObserverPhase, type SupportedSchema, type SystemOneAnswer, type SystemOneAnswers, SystemOneChoiceAnswer, SystemOneChoiceQuestion, SystemOneContent, SystemOneNoulAnswer, SystemOneNoulQuestion, type SystemOneQuestion, type SystemOneQuestions, type SystemOneRequest, type SystemOneResponse, SystemOneScoreAnswer, SystemOneScoreQuestion, type SystemOneUsage, ThinkValue, ThinkingMetadata, type TimeoutSignal, TokenUsage, Tool, ToolCall, ToolCallAccumulator, ToolDefinition, ToolExecutionResult, ToolHandler, ToolParameters, ToolRegistry, type TurnCacheStats, type UsageBucket, type UsageMetrics, UsageRequestOptions, type UsageResponse, VersionResponse, VisionInput, WEB_FETCH_TOOL_NAME, WEB_SEARCH_TOOL_NAME, WebFetchRequestOptions, WebFetchResponse, type WebFetchToolParams, WebSearchRequestOptions, WebSearchResponse, type WebSearchToolParams, type WebToolsClient, type WebToolsOptions, addSpanEvent, batchEmbed, calculateBackoff, canonicalToolCallSignature, checkChatContext, checkEndpointHealth, checkGenerateContext, compactConversationHistory, compareDraftPerformance, contextWarningMessage, createDecision, createModelAffinityScheduler, createOllamaCloudFreeTierQuota, createTimeoutSignal, createWebFetchTool, createWebSearchTool, createWebTools, defineTool, detectModelCapabilities, embedBatchOverflowMessage, encodeImage, estimateChatRequestTokens, estimateGenerateRequestTokens, estimateMessageTokens, estimateSystemOneRequestBytes, estimateTokens, extractContextLength as extractNativeContextLength, extractParameterNumCtx, fetchWithBackoff, findOversizedEmbedInputs, findRunningModelContextLength, formatTelemetry, formatWebFetchResult, formatWebSearchResult, inferRuntimeMode, isKeepAliveSugar, isKnownDoneReason, isModelResident, isSameToolCall, listAllMcpTools, listAvailableModels, loadMcpTools, mergeToolCall, mergeToolCallArgumentsString, mergeToolCallArrays, normalizeChatStream, normalizeGenerateStream, normalizeKeepAlive, normalizeProgressStream, observeStream, parseNdjsonStream, parseStructuredOutput, recommendContextWindow, registerMcpTools, resolveContextLength, resolveCredentialEndpoints, sanitizeHistoryForNextTurn, setSpanError, toDataStream, toResponse, toTextStream, withRetry, withSpan, zodToJsonSchema };
package/dist/index.d.ts CHANGED
@@ -1,10 +1,10 @@
1
1
  import { T as ThinkingMetadata, M as ModelResponse, R as RequestCancellationOptions, S as ShowRequestOptions, a as ShowResponse, P as PullRequestOptions, b as ProgressResponse, c as PushRequestOptions, C as CreateRequestOptions, D as DeleteRequestOptions, d as StatusResponse, e as CopyRequestOptions, f as PsResponse, V as VersionResponse, g as ToolParameters, h as ThinkValue, i as ToolCall, j as ChatRequestOptions, k as ModelOptions, l as ToolDefinition, F as FormatOption, m as ChatResponse, n as Message, G as GenerateRequestOptions, o as GenerateResponse, E as EmbedRequestOptions, p as EmbedResponse, q as EmbeddingsRequestOptions, r as EmbeddingsResponse, W as WebSearchRequestOptions, s as WebSearchResponse, t as WebFetchRequestOptions, u as WebFetchResponse, U as UsageRequestOptions, B as BalanceRequestOptions, v as PingResult, w as VisionInput } from './types-DbPnfVh-.js';
2
2
  export { L as ListResponse, x as Logprob, y as LogprobToken, z as ModelDetails, A as Role, H as ToolCallFunction, I as ToolFunctionDefinition, J as ToolProperty, K as UsageRange, N as UsageScope, O as WebSearchResult, Q as imageStringNeedsResolution, X as resolveImageInput } from './types-DbPnfVh-.js';
3
- import { H as HttpClient, F as FetchLike, M as Middleware, L as Logger, R as RequestLifecycleHook, c as OllamaStreamEvent, A as AbortableAsyncIterable, d as OllamaStreamEventType, P as ProgressStreamResult, B as BinaryBody, O as OllamaRuntime, C as ChatStreamResult, G as GenerateStreamResult, e as RawUsageSource, T as TokenUsage } from './runtime-BIjr5ME4.js';
4
- export { D as DoneEvent, E as ErrorEvent, f as HttpBody, g as HttpClientOptions, h as HttpRequestOptions, i as LifecycleErrorEvent, j as LifecycleRetryEvent, k as LifecycleStartEvent, l as LifecycleSuccessEvent, m as LogFn, n as MessageEvent, o as MiddlewareContext, N as NANOS_PER_MS, p as NANOS_PER_SECOND, q as NOOP_LOGGER, r as NextFunction, s as OllamaAbortError, t as OllamaAgentMaxIterationsError, u as OllamaAgentMaxToolCallsError, v as OllamaAgentToolLoopError, w as OllamaAuthError, x as OllamaBadGatewayError, y as OllamaClientError, z as OllamaClientErrorOptions, J as OllamaErrorRequestContext, K as OllamaErrorResponseContext, Q as OllamaGenericClientError, U as OllamaIncompatibleModelError, V as OllamaMcpError, W as OllamaModelRoutingError, X as OllamaNetworkError, Y as OllamaNotFoundError, Z as OllamaOverloadedError, _ as OllamaQuotaExceededError, $ as OllamaRateLimitError, a0 as OllamaRequestTooLargeError, a1 as OllamaRequestValidationError, a2 as OllamaResponseValidationError, a3 as OllamaServerError, a4 as OllamaServerVersionUnknownError, a5 as OllamaSkillInvalidError, a6 as OllamaSkillNotFoundError, a7 as OllamaStreamError, a8 as OllamaTimeoutError, a9 as OllamaToolTimeoutError, aa as OllamaToolValidationError, ab as OllamaUnsupportedCapabilityError, ac as RequestContext, ad as RequestLifecycleEvent, ae as ResponseContext, af as SseEvent, ag as ThinkingEvent, ah as TokenEvent, ai as ToolCallEvent, aj as composeMiddleware, ak as createConsoleLogger, al as extractUsage, am as mapError, an as parseSseStream } from './runtime-BIjr5ME4.js';
3
+ import { H as HttpClient, F as FetchLike, M as Middleware, L as Logger, R as RequestLifecycleHook, c as OllamaStreamEvent, A as AbortableAsyncIterable, d as OllamaStreamEventType, P as ProgressStreamResult, B as BinaryBody, O as OllamaRuntime, C as ChatStreamResult, G as GenerateStreamResult, e as RawUsageSource, T as TokenUsage } from './runtime-CFuv7Ifh.js';
4
+ export { D as DoneEvent, E as ErrorEvent, f as HttpBody, g as HttpClientOptions, h as HttpRequestOptions, i as LifecycleErrorEvent, j as LifecycleRetryEvent, k as LifecycleStartEvent, l as LifecycleSuccessEvent, m as LogFn, n as MessageEvent, o as MiddlewareContext, N as NANOS_PER_MS, p as NANOS_PER_SECOND, q as NETWORK_ERROR_CODES, r as NOOP_LOGGER, s as NextFunction, t as OllamaAbortError, u as OllamaAgentMaxIterationsError, v as OllamaAgentMaxToolCallsError, w as OllamaAgentToolLoopError, x as OllamaAuthError, y as OllamaBadGatewayError, z as OllamaClientError, J as OllamaClientErrorOptions, K as OllamaErrorRequestContext, Q as OllamaErrorResponseContext, U as OllamaGenericClientError, V as OllamaIncompatibleModelError, W as OllamaMcpError, X as OllamaModelRoutingError, Y as OllamaNetworkError, Z as OllamaNotFoundError, _ as OllamaOverloadedError, $ as OllamaQuotaExceededError, a0 as OllamaRateLimitError, a1 as OllamaRequestTooLargeError, a2 as OllamaRequestValidationError, a3 as OllamaResponseValidationError, a4 as OllamaServerError, a5 as OllamaServerVersionUnknownError, a6 as OllamaSkillInvalidError, a7 as OllamaSkillNotFoundError, a8 as OllamaStreamError, a9 as OllamaTimeoutError, aa as OllamaToolTimeoutError, ab as OllamaToolValidationError, ac as OllamaUnsupportedCapabilityError, ad as RequestContext, ae as RequestLifecycleEvent, af as ResponseContext, ag as SseEvent, ah as ThinkingEvent, ai as TokenEvent, aj as ToolCallEvent, ak as composeMiddleware, al as createConsoleLogger, am as extractUsage, an as isNetworkLevelError, ao as mapError, ap as parseSseStream } from './runtime-CFuv7Ifh.js';
5
5
  import { z } from 'zod';
6
- import { R as RequestRunner, S as SystemOneChoiceAnswer, o as SystemOneNoulAnswer, p as SystemOneScoreAnswer, q as SystemOneChoiceQuestion, r as SystemOneNoulQuestion, s as SystemOneScoreQuestion, t as SystemOneRequest$1, u as SystemOneResponse$1, v as SystemOneContent, N as NativeApi, w as OpenAICompatClient, x as AnthropicCompatClient } from './anthropic-DrDUfbfQ.js';
7
- export { y as AnthropicCacheControl, z as AnthropicContentBlock, B as AnthropicImageContentBlock, C as AnthropicMessage, n as AnthropicMessageStreamEvent, A as AnthropicMessagesRequest, m as AnthropicMessagesResponse, D as AnthropicMessagesStream, E as AnthropicMetadata, F as AnthropicOutputConfig, G as AnthropicRedactedThinkingContentBlock, H as AnthropicSystem, I as AnthropicSystemTextBlock, J as AnthropicTextContentBlock, K as AnthropicThinkingConfig, L as AnthropicThinkingContentBlock, M as AnthropicTool, P as AnthropicToolChoice, Q as AnthropicToolResultContentBlock, T as AnthropicToolUseContentBlock, U as AnthropicUnknownStreamEvent, V as OllamaAnthropicContentBlock, W as OllamaAnthropicMessage, X as OllamaAnthropicMessagesRequest, Y as OllamaAnthropicSystem, Z as OllamaAnthropicSystemTextBlock, _ as OllamaAnthropicTextContentBlock, $ as OllamaAnthropicThinkingConfig, a0 as OllamaAnthropicTool, a1 as OllamaAnthropicToolResultContentBlock, a2 as OllamaOpenAIChatCompletionRequest, a3 as OllamaOpenAIChatContentPart, a4 as OllamaOpenAIChatMessage, a5 as OllamaOpenAICompletionRequest, a6 as OllamaOpenAIEmbeddingRequest, a7 as OllamaOpenAIImageDataUrl, a8 as OllamaOpenAIReasoningEffort, a9 as OllamaOpenAIResponsesRequest, aa as OpenAIChatCompletionChoice, b as OpenAIChatCompletionChunk, ab as OpenAIChatCompletionChunkChoice, ac as OpenAIChatCompletionDelta, O as OpenAIChatCompletionRequest, a as OpenAIChatCompletionResponse, ad as OpenAIChatCompletionStream, ae as OpenAICompletionChoice, e as OpenAICompletionChunk, c as OpenAICompletionRequest, d as OpenAICompletionResponse, af as OpenAICompletionStream, ag as OpenAIContentPart, ah as OpenAIEmbeddingInput, ai as OpenAIEmbeddingItem, f as OpenAIEmbeddingRequest, g as OpenAIEmbeddingResponse, aj as OpenAIFunctionDefinition, ak as OpenAIImageUrlContentPart, h as OpenAIListModelsResponse, al as OpenAIMessage, i as OpenAIModelItem, am as OpenAIReasoningEffort, an as OpenAIResponseFormat, ao as OpenAIResponsesCompletedEvent, ap as OpenAIResponsesContentPartAddedEvent, aq as OpenAIResponsesContentPartDoneEvent, ar as OpenAIResponsesCreatedEvent, as as OpenAIResponsesError, at as OpenAIResponsesEventBase, au as OpenAIResponsesFailedEvent, av as OpenAIResponsesFunctionCallArgumentsDeltaEvent, aw as OpenAIResponsesFunctionCallArgumentsDoneEvent, ax as OpenAIResponsesInProgressEvent, ay as OpenAIResponsesIncompleteDetails, az as OpenAIResponsesIncompleteEvent, aA as OpenAIResponsesOutputContent, aB as OpenAIResponsesOutputFunctionCall, aC as OpenAIResponsesOutputItem, aD as OpenAIResponsesOutputItemAddedEvent, aE as OpenAIResponsesOutputItemDoneEvent, aF as OpenAIResponsesOutputMessage, aG as OpenAIResponsesOutputReasoning, aH as OpenAIResponsesOutputRefusal, aI as OpenAIResponsesOutputTextContent, aJ as OpenAIResponsesOutputTextDeltaEvent, aK as OpenAIResponsesOutputTextDoneEvent, aL as OpenAIResponsesQueuedEvent, aM as OpenAIResponsesReasoningSummaryPartAddedEvent, aN as OpenAIResponsesReasoningSummaryPartDoneEvent, aO as OpenAIResponsesReasoningSummaryTextDeltaEvent, aP as OpenAIResponsesReasoningSummaryTextDoneEvent, aQ as OpenAIResponsesReasoningTextDeltaEvent, aR as OpenAIResponsesReasoningTextDoneEvent, aS as OpenAIResponsesRefusalDeltaEvent, aT as OpenAIResponsesRefusalDoneEvent, j as OpenAIResponsesRequest, k as OpenAIResponsesResponse, aU as OpenAIResponsesStatus, aV as OpenAIResponsesStream, aW as OpenAIResponsesStreamError, l as OpenAIResponsesStreamEvent, aX as OpenAIResponsesUsage, aY as OpenAIStreamOptions, aZ as OpenAITextContentPart, a_ as OpenAITool, a$ as OpenAIToolCall, b0 as OpenAIToolCallDelta, b1 as OpenAIUsage, b2 as SystemOneConfidence, b3 as SystemOneProbabilities } from './anthropic-DrDUfbfQ.js';
6
+ import { R as RequestRunner, S as SystemOneChoiceAnswer, o as SystemOneNoulAnswer, p as SystemOneScoreAnswer, q as SystemOneChoiceQuestion, r as SystemOneNoulQuestion, s as SystemOneScoreQuestion, t as SystemOneRequest$1, u as SystemOneResponse$1, v as SystemOneContent, N as NativeApi, w as OpenAICompatClient, x as AnthropicCompatClient } from './anthropic-CTv2acbj.js';
7
+ export { y as AnthropicCacheControl, z as AnthropicContentBlock, B as AnthropicImageContentBlock, C as AnthropicMessage, n as AnthropicMessageStreamEvent, A as AnthropicMessagesRequest, m as AnthropicMessagesResponse, D as AnthropicMessagesStream, E as AnthropicMetadata, F as AnthropicOutputConfig, G as AnthropicRedactedThinkingContentBlock, H as AnthropicSystem, I as AnthropicSystemTextBlock, J as AnthropicTextContentBlock, K as AnthropicThinkingConfig, L as AnthropicThinkingContentBlock, M as AnthropicTool, P as AnthropicToolChoice, Q as AnthropicToolResultContentBlock, T as AnthropicToolUseContentBlock, U as AnthropicUnknownStreamEvent, V as OllamaAnthropicContentBlock, W as OllamaAnthropicMessage, X as OllamaAnthropicMessagesRequest, Y as OllamaAnthropicSystem, Z as OllamaAnthropicSystemTextBlock, _ as OllamaAnthropicTextContentBlock, $ as OllamaAnthropicThinkingConfig, a0 as OllamaAnthropicTool, a1 as OllamaAnthropicToolResultContentBlock, a2 as OllamaOpenAIChatCompletionRequest, a3 as OllamaOpenAIChatContentPart, a4 as OllamaOpenAIChatMessage, a5 as OllamaOpenAICompletionRequest, a6 as OllamaOpenAIEmbeddingRequest, a7 as OllamaOpenAIImageDataUrl, a8 as OllamaOpenAIReasoningEffort, a9 as OllamaOpenAIResponsesRequest, aa as OpenAIChatCompletionChoice, b as OpenAIChatCompletionChunk, ab as OpenAIChatCompletionChunkChoice, ac as OpenAIChatCompletionDelta, O as OpenAIChatCompletionRequest, a as OpenAIChatCompletionResponse, ad as OpenAIChatCompletionStream, ae as OpenAICompletionChoice, e as OpenAICompletionChunk, c as OpenAICompletionRequest, d as OpenAICompletionResponse, af as OpenAICompletionStream, ag as OpenAIContentPart, ah as OpenAIEmbeddingInput, ai as OpenAIEmbeddingItem, f as OpenAIEmbeddingRequest, g as OpenAIEmbeddingResponse, aj as OpenAIFunctionDefinition, ak as OpenAIImageUrlContentPart, h as OpenAIListModelsResponse, al as OpenAIMessage, i as OpenAIModelItem, am as OpenAIReasoningEffort, an as OpenAIResponseFormat, ao as OpenAIResponsesCompletedEvent, ap as OpenAIResponsesContentPartAddedEvent, aq as OpenAIResponsesContentPartDoneEvent, ar as OpenAIResponsesCreatedEvent, as as OpenAIResponsesError, at as OpenAIResponsesEventBase, au as OpenAIResponsesFailedEvent, av as OpenAIResponsesFunctionCallArgumentsDeltaEvent, aw as OpenAIResponsesFunctionCallArgumentsDoneEvent, ax as OpenAIResponsesInProgressEvent, ay as OpenAIResponsesIncompleteDetails, az as OpenAIResponsesIncompleteEvent, aA as OpenAIResponsesOutputContent, aB as OpenAIResponsesOutputFunctionCall, aC as OpenAIResponsesOutputItem, aD as OpenAIResponsesOutputItemAddedEvent, aE as OpenAIResponsesOutputItemDoneEvent, aF as OpenAIResponsesOutputMessage, aG as OpenAIResponsesOutputReasoning, aH as OpenAIResponsesOutputRefusal, aI as OpenAIResponsesOutputTextContent, aJ as OpenAIResponsesOutputTextDeltaEvent, aK as OpenAIResponsesOutputTextDoneEvent, aL as OpenAIResponsesQueuedEvent, aM as OpenAIResponsesReasoningSummaryPartAddedEvent, aN as OpenAIResponsesReasoningSummaryPartDoneEvent, aO as OpenAIResponsesReasoningSummaryTextDeltaEvent, aP as OpenAIResponsesReasoningSummaryTextDoneEvent, aQ as OpenAIResponsesReasoningTextDeltaEvent, aR as OpenAIResponsesReasoningTextDoneEvent, aS as OpenAIResponsesRefusalDeltaEvent, aT as OpenAIResponsesRefusalDoneEvent, j as OpenAIResponsesRequest, k as OpenAIResponsesResponse, aU as OpenAIResponsesStatus, aV as OpenAIResponsesStream, aW as OpenAIResponsesStreamError, l as OpenAIResponsesStreamEvent, aX as OpenAIResponsesUsage, aY as OpenAIStreamOptions, aZ as OpenAITextContentPart, a_ as OpenAITool, a$ as OpenAIToolCall, b0 as OpenAIToolCallDelta, b1 as OpenAIUsage, b2 as SystemOneConfidence, b3 as SystemOneProbabilities } from './anthropic-CTv2acbj.js';
8
8
  import { T as ToolHandler, a as Tool, A as AnyTool, b as ToolRegistry, c as ToolExecutionResult } from './compose-CeFbHJeO.js';
9
9
  export { d as AppliedSkillResult, e as ApplySkillOptions, P as ParsedFrontmatter, S as Skill, f as SkillFrontmatter, g as SkillSummary, h as ToolExecutionContext, i as ToolExecutionFailure, j as ToolExecutionSuccess, k as ToolRegistryOptions, l as applySkill, p as parseFrontmatter } from './compose-CeFbHJeO.js';
10
10
  import { M as McpClientLike, a as McpToolDescriptor, b as McpTask, c as McpCallToolResult } from './types-CI-gM2u5.js';
@@ -970,8 +970,8 @@ declare class ModelsClient {
970
970
  * model-not-found (404), or VRAM-exhaustion failures.
971
971
  */
972
972
  pin(model: string): Promise<void>;
973
- createBlob(digest: string, data: BinaryBody): Promise<void>;
974
- checkBlob(digest: string): Promise<boolean>;
973
+ createBlob(digest: string, data: BinaryBody, request?: RequestCancellationOptions): Promise<void>;
974
+ checkBlob(digest: string, request?: RequestCancellationOptions): Promise<boolean>;
975
975
  /**
976
976
  * Computes a blob digest the way Ollama's content-addressed store expects:
977
977
  * SHA-256 over the raw bytes, formatted as `sha256:<64 lowercase hex>`.
@@ -992,17 +992,44 @@ declare class ModelsClient {
992
992
  */
993
993
  createBlobFromData(data: Uint8Array, opts?: {
994
994
  readonly digest?: string | undefined;
995
- readonly signal?: AbortSignal | undefined;
996
- }): Promise<BlobUploadResult>;
995
+ } & RequestCancellationOptions): Promise<BlobUploadResult>;
996
+ /**
997
+ * Streams a blob upload straight from a web `ReadableStream` — the
998
+ * multi-gigabyte-safe counterpart of {@link createBlobFromData}. The body
999
+ * is never materialized in memory: the transport hands the stream to
1000
+ * `fetch` directly (setting `duplex: 'half'`, which Node 18+ requires for
1001
+ * stream bodies), so peak heap usage is a transport-level chunk, not the
1002
+ * blob size.
1003
+ *
1004
+ * The digest must be known up front — it *is* the upload URL
1005
+ * (`/api/blobs/<digest>`), the same content-addressed protocol
1006
+ * `curl -T` performs. Hash first when you don't have it: in-memory bytes
1007
+ * via {@link computeBlobDigest}, files via {@link createBlobFromFile}
1008
+ * (which hashes in a streaming pass). The server verifies the streamed
1009
+ * bytes against the digest and rejects mismatches, so a corrupted or
1010
+ * truncated stream fails the upload instead of poisoning the store.
1011
+ */
1012
+ createBlobFromStream(digest: string, stream: ReadableStream<Uint8Array>, request?: RequestCancellationOptions): Promise<BlobUploadResult>;
997
1013
  /**
998
1014
  * Reads a local file and uploads it as a blob (Node.js only — dynamic
999
1015
  * `node:fs` import, so browser bundles never pull it in). The file name
1000
1016
  * (basename) is what `/api/create`'s `files` map expects as the key, so
1001
1017
  * this returns it alongside the digest.
1018
+ *
1019
+ * Streams both passes in fixed-size chunks — peak heap is O(chunk), not
1020
+ * O(file), so multi-gigabyte GGUFs never enter the Node heap:
1021
+ *
1022
+ * 1. **Hash pass** (skipped when `opts.digest` is provided): the file is
1023
+ * streamed through `node:crypto`'s incremental SHA-256.
1024
+ * 2. **Upload pass**: `node:fs` createReadStream → web ReadableStream →
1025
+ * `fetch` body with `duplex: 'half'` (see {@link createBlobFromStream}).
1026
+ *
1027
+ * A pre-existing blob (HEAD 200) skips the upload pass entirely, same as
1028
+ * {@link createBlobFromData}.
1002
1029
  */
1003
1030
  createBlobFromFile(path: string, opts?: {
1004
- readonly signal?: AbortSignal | undefined;
1005
- }): Promise<BlobUploadResult & {
1031
+ readonly digest?: string | undefined;
1032
+ } & RequestCancellationOptions): Promise<BlobUploadResult & {
1006
1033
  readonly fileName: string;
1007
1034
  }>;
1008
1035
  /**
@@ -2277,8 +2304,8 @@ declare class OllamaClient {
2277
2304
  */
2278
2305
  readonly getContextLength: (request: ContextDiscoveryRequestOptions) => Promise<DiscoveredContextLength>;
2279
2306
  readonly version: () => Promise<VersionResponse>;
2280
- readonly createBlob: (digest: string, data: BinaryBody) => Promise<void>;
2281
- readonly checkBlob: (digest: string) => Promise<boolean>;
2307
+ readonly createBlob: (digest: string, data: BinaryBody, request?: RequestCancellationOptions) => Promise<void>;
2308
+ readonly checkBlob: (digest: string, request?: RequestCancellationOptions) => Promise<boolean>;
2282
2309
  /**
2283
2310
  * Immediately unloads `model` from VRAM. Convenience alias for
2284
2311
  * {@link ModelsClient.unload} — see that method for the full
@@ -2780,6 +2807,140 @@ declare function toDataStream<TChunk, TFinal>(stream: OllamaStream<TChunk, TFina
2780
2807
  */
2781
2808
  declare function toResponse<TChunk, TFinal>(stream: OllamaStream<TChunk, TFinal>, init?: ResponseInit): Response;
2782
2809
 
2810
+ /**
2811
+ * Zero-copy stream observation — the safe alternative to `ReadableStream.tee()`
2812
+ * for multi-sink consumption (audit logging, token counting, telemetry taps).
2813
+ *
2814
+ * ## The `tee()` hazard this exists for
2815
+ *
2816
+ * Developers streaming to a UI while also feeding a logger (or a token
2817
+ * counter, or an OpenTelemetry exporter) typically reach for:
2818
+ *
2819
+ * ```ts
2820
+ * // VULNERABLE PATTERN — do not do this
2821
+ * const [forUi, forLogger] = response.body.tee();
2822
+ * ```
2823
+ *
2824
+ * WHATWG `tee()` branches share a single upstream pull mechanism: a branch
2825
+ * that reads slower than its sibling forces every intermediate chunk to be
2826
+ * retained in that branch's internal queue until it catches up. If the slow
2827
+ * consumer is a logger waiting on a database write — or never drains at all —
2828
+ * the queue grows without bound, and because both branches apply shared
2829
+ * backpressure, the *fast* consumer is throttled by the slowest one. In a
2830
+ * long-lived streaming server this is a slow, silent heap leak.
2831
+ *
2832
+ * ## What `observeStream` does instead
2833
+ *
2834
+ * A single-pass wrapper: chunks flow from the source through the observer
2835
+ * callbacks to the primary consumer with **no second queue and no forked
2836
+ * stream**. The contract is deliberately one-sided in favor of the primary
2837
+ * consumer:
2838
+ *
2839
+ * - **Never blocks.** Observer callbacks run on a serialized microtask
2840
+ * chain, not on the hot path. A slow async observer (a logger waiting on
2841
+ * I/O) delays only its own tail — the primary consumer keeps receiving
2842
+ * chunks at source speed.
2843
+ * - **Never fails.** An observer that throws (sync) or rejects (async) is
2844
+ * routed to `onListenerError` and otherwise discarded. Observer failures
2845
+ * can never poison the primary pipeline.
2846
+ * - **Never leaks via the wrapper.** There is no second branch to
2847
+ * backpressure. The only retained state is one closure per backlogged
2848
+ * observer callback; if your observer can stall *forever*, bound it on
2849
+ * your side (queue, drop policy) — the wrapper will keep enqueueing
2850
+ * otherwise, exactly like any fire-and-forget log sink.
2851
+ * - **Ordered.** Observer callbacks run strictly FIFO, one at a time —
2852
+ * async sinks see chunks in stream order, and `onDone` is enqueued only
2853
+ * after the last chunk's callback settles. (This is intentionally
2854
+ * stricter than a naive `void onChunk(chunk)` fire-and-forget, which
2855
+ * runs concurrent, out-of-order observers and turns a rejected async
2856
+ * callback into an unhandled rejection.)
2857
+ *
2858
+ * ## Lifecycle
2859
+ *
2860
+ * - Source completes naturally → `onDone` (enqueued after the last chunk
2861
+ * callback settles, so a logger has flushed everything it saw).
2862
+ * - Source throws → `onError(err)` immediately, then the error re-throws to
2863
+ * the primary consumer (the observer is transparent, not a circuit
2864
+ * breaker).
2865
+ * - Primary consumer stops early (`break` / `return` / `throw` inside the
2866
+ * `for await` loop) → `onAbandon()` immediately. This is distinct from
2867
+ * completion: the source's iterator-`return()` has already closed the
2868
+ * underlying transport by the time `onAbandon` fires.
2869
+ *
2870
+ * Works over any `AsyncIterable<T>` — the SDK's `OllamaStream` (streaming
2871
+ * `chat`/`generate`), the raw NDJSON chunk iterables, or your own sources.
2872
+ * Note that `OllamaStream` itself is dual-mode but mode-exclusive (`.on()`
2873
+ * listeners cannot be combined with `for await`); `observeStream` is the
2874
+ * supported way to tap a stream you are already iterating.
2875
+ */
2876
+ /**
2877
+ * Lifecycle phase an observer failure came from. `'chunk'` and `'done'`
2878
+ * failures arrive from the serialized callback chain; `'error'` and
2879
+ * `'abandon'` failures come from those terminal hooks themselves.
2880
+ */
2881
+ type StreamObserverPhase = 'chunk' | 'done' | 'error' | 'abandoned';
2882
+ /** Passive, best-effort observer hooks — see the module docs for semantics. */
2883
+ interface StreamObserverCallbacks<T> {
2884
+ /**
2885
+ * Called once per chunk, in stream order, FIFO, never overlapping with a
2886
+ * previous still-running call. May be sync (`void`) or async
2887
+ * (`Promise<void>`); async return values are awaited *on the observer
2888
+ * chain only* — they never delay the primary consumer. Rejections (and
2889
+ * sync throws) route to {@link StreamObserverCallbacks.onListenerError}.
2890
+ */
2891
+ onChunk?: ((chunk: T) => void | Promise<void>) | undefined;
2892
+ /**
2893
+ * Called with the source's error immediately before that error is re-thrown
2894
+ * to the primary consumer. A throwing `onError` hook cannot mask the
2895
+ * original error — it is reported via {@link StreamObserverCallbacks.onListenerError}
2896
+ * and the original error still propagates.
2897
+ */
2898
+ onError?: ((error: unknown) => void) | undefined;
2899
+ /**
2900
+ * Called when the source completes naturally — enqueued after the last
2901
+ * chunk's observer callback settles, so a flushed logger has seen every
2902
+ * chunk before its "done" marker.
2903
+ */
2904
+ onDone?: (() => void | Promise<void>) | undefined;
2905
+ /**
2906
+ * Called when the primary consumer stops early (`break`/`return`/`throw` in
2907
+ * the consuming loop). The source has already been closed via the
2908
+ * iterator-`return()` protocol when this fires. `onDone` does not fire in
2909
+ * this case.
2910
+ */
2911
+ onAbandon?: (() => void) | undefined;
2912
+ /**
2913
+ * The observation failure channel: any observer hook that throws or rejects
2914
+ * is reported here together with the phase it came from, and then
2915
+ * discarded. If this hook itself throws, the failure is swallowed — an
2916
+ * error reporter must never become the next unhandled rejection.
2917
+ */
2918
+ onListenerError?: ((error: unknown, phase: StreamObserverPhase) => void) | undefined;
2919
+ }
2920
+ /**
2921
+ * Wraps an `AsyncIterable` with passive observer hooks without creating a
2922
+ * secondary buffered stream queue — avoiding the memory retention and shared
2923
+ * backpressure penalties of `ReadableStream.tee()`.
2924
+ *
2925
+ * @example
2926
+ * ```ts
2927
+ * const stream = await client.chat({ model: 'llama4', messages, stream: true });
2928
+ * let tokens = 0;
2929
+ * const observed = observeStream(stream, {
2930
+ * onChunk: (event) => {
2931
+ * if (event.type === 'token') tokens += 1;
2932
+ * auditLog.write(event); // slow async sink — never delays the consumer
2933
+ * },
2934
+ * onDone: () => auditLog.flush(),
2935
+ * onListenerError: (err) => telemetry.count('audit_log_failure', err),
2936
+ * });
2937
+ * for await (const event of observed) {
2938
+ * ui.render(event); // primary consumer — full speed, full order
2939
+ * }
2940
+ * ```
2941
+ */
2942
+ declare function observeStream<T>(source: AsyncIterable<T>, callbacks?: StreamObserverCallbacks<T>): AsyncGenerator<T, void, unknown>;
2943
+
2783
2944
  /**
2784
2945
  * Client-side usage budgeting.
2785
2946
  *
@@ -3695,7 +3856,33 @@ interface ContextCheck {
3695
3856
  readonly windowSource: ContextWindowSource;
3696
3857
  /** True when the estimate crossed the warning threshold of the effective window. */
3697
3858
  readonly exceedsThreshold: boolean;
3859
+ /**
3860
+ * Present only when `exceedsThreshold` is `true`: the smallest power-of-two
3861
+ * window with ~30% generation headroom above the estimate (see
3862
+ * {@link recommendContextWindow}). A starting point, not a verdict — check
3863
+ * the model's native ceiling with `client.models.getContextLength()` before
3864
+ * adopting it; a prompt that outgrows the native maximum needs compaction
3865
+ * or history truncation, not a bigger `num_ctx`.
3866
+ */
3867
+ readonly recommendedNumCtx?: number | undefined;
3698
3868
  }
3869
+ /**
3870
+ * Smallest power-of-two context window that comfortably fits
3871
+ * `estimatedTokens` — ~30% headroom for the response, floored at 4096 (the
3872
+ * common modern server default when `num_ctx` is unset).
3873
+ *
3874
+ * Uses an exact doubling loop rather than
3875
+ * `Math.pow(2, Math.ceil(Math.log2(x)))`: floating-point `log2` of exact
3876
+ * powers of two can land marginally above the integer (e.g.
3877
+ * `Math.log2(8) > 3` on some platforms), which rounds a perfect fit *up* a
3878
+ * whole power of two. The loop has no such boundary bugs.
3879
+ *
3880
+ * Non-finite / non-positive inputs return the 4096 floor rather than throwing:
3881
+ * callers use this to *suggest* a window after their own estimate already
3882
+ * flagged a problem, so garbage-in should degrade to a conservative
3883
+ * suggestion, not a second error path.
3884
+ */
3885
+ declare function recommendContextWindow(estimatedTokens: number): number;
3699
3886
  interface ContextCheckOptions {
3700
3887
  /** Client `defaultContextLength`, applied when the request doesn't set `num_ctx`. */
3701
3888
  readonly defaultContextLength?: number | undefined;
@@ -3862,4 +4049,4 @@ declare class ModelAffinityScheduler {
3862
4049
  /** Convenience factory — `createModelAffinityScheduler(client, opts)` reads better in setups. */
3863
4050
  declare function createModelAffinityScheduler(client: ModelAffinityClient | OllamaClient, options?: ModelAffinitySchedulerOptions): ModelAffinityScheduler;
3864
4051
 
3865
- export { ATTR_GEN_AI_OPERATION_NAME, ATTR_GEN_AI_REQUEST_MODEL, ATTR_GEN_AI_RESPONSE_MODEL, ATTR_GEN_AI_SYSTEM, ATTR_GEN_AI_TOOL_CALL_ID, ATTR_GEN_AI_TOOL_NAME, ATTR_GEN_AI_USAGE_INPUT_TOKENS, ATTR_GEN_AI_USAGE_OUTPUT_TOKENS, ATTR_HTTP_REQUEST_METHOD, ATTR_HTTP_RESPONSE_STATUS_CODE, ATTR_OLLAMA_AGENT_ITERATION, ATTR_OLLAMA_AGENT_MAX_ITERATIONS, ATTR_OLLAMA_ENDPOINT_ATTEMPT, ATTR_OLLAMA_ENDPOINT_NAME, ATTR_SERVER_ADDRESS, ATTR_SERVER_PORT, ATTR_URL_FULL, AbortableAsyncIterable, type AffinityRunningModel, Agent, type AgentChatClient, type AgentConfig, type AgentHooks, type AgentResult, type AgentRunInput, type AgentTurn, type AnswerFor, AnthropicCompatClient, AnyTool, type BackoffOptions, BalanceRequestOptions, type BalanceResponse, type BlobUploadResult, ChatRequestOptions, ChatResponse, ChatStreamResult, type ChoiceDecision, type CompactionOptions, type CompareDraftPerformanceOptions, type ContextCheck, type ContextCheckOptions, type ContextDiscoveryRequestOptions, type ContextLengthSignals, type ContextLengthSource, type ContextWindowSource, type ConversationSendOptions, ConversationSession, type ConversationSessionOptions, CopyRequestOptions, type CreateModelFromGgufOptions, CreateRequestOptions, type CumulativeCacheStats, DEFAULT_BACKOFF, DEFAULT_BASE_URL, DEFAULT_CONTEXT_WARNING_THRESHOLD, DEFAULT_EMBED_BATCH_SIZE, DEFAULT_EMBED_CONCURRENCY, DEFAULT_FAILOVER_CODES, DEFAULT_FETCH_BACKOFF_CONFIG, DEFAULT_RETRY_CONFIG, DEFAULT_TIMEOUT_MS, type Decision, type DefineToolOptions, DeleteRequestOptions, type DiscoveredContextLength, type DraftBenchmarkClient, type DraftPerformanceComparison, type DraftRunStats, type EmbedBatchOptions, type EmbedBatchResult, EmbedRequestOptions, EmbedResponse, EmbeddingsRequestOptions, EmbeddingsResponse, type EndpointHealth, type EndpointHealthCheckResult, EndpointRegistry, type EndpointRegistryOptions, FetchLike, type FetchWithBackoffConfig, FormatOption, type FormattedTelemetry, GEN_AI_SYSTEM_OLLAMA, GenerateRequestOptions, GenerateResponse, GenerateStreamResult, type HistorySanitizeOptions, HttpClient, IMAGE_TOKEN_ESTIMATE, type IncludedBalance, KEEP_ALIVE_INDEFINITE, KEEP_ALIVE_UNLOAD, KNOWN_DONE_REASONS, type KeepAlive, type KnownDoneReason, type LegacyBalanceLimit, type LegacyIncludedBalance, type LoadMcpToolsOptions, Logger, MAX_SYSTEM_ONE_IMAGES_BYTES, MAX_SYSTEM_ONE_REQUEST_BYTES, MESSAGE_OVERHEAD_TOKENS, McpBridge, type McpBridgeOptions, McpCallToolResult, McpClientLike, McpTask, McpToolDescriptor, Message, Middleware, type ModelAffinityClient, ModelAffinityRouter, type ModelAffinityRoutingOptions, ModelAffinityScheduler, type ModelAffinitySchedulerOptions, type ModelAffinitySnapshot, type ModelAffinityStats, type ModelCapabilities, ModelOptions, ModelResponse, ModelsClient, type NoulDecision, OLLAMA_CLOUD_BASE_URL, OLLAMA_FALLBACK_CONTEXT_LENGTH, OllamaClient, type OllamaClientConfig, type OllamaCloudFreeTierQuotaBudgets, type OllamaCredentialConfig, type OllamaEndpoint, OllamaStream, OllamaStreamEvent, OllamaStreamEventType, OpenAICompatClient, type OversizedEmbedInput, type ParallelToolCallBehavior, PingResult, ProgressResponse, ProgressStreamResult, PsResponse, PullRequestOptions, PushRequestOptions, QuotaManager, type QuotaManagerOptions, type QuotaWindowConfig, type QuotaWindowStatus, RETRYABLE_STATUS_CODES, type RankResult, type RawOllamaMetrics, RawUsageSource, RequestCancellationOptions, RequestLifecycleHook, RequestRunner, type ResponsesCreateRequest, type ResponsesCreateResponse, ResponsesModule, type ResponsesReasoningEffort, type ResponsesStreamEvent, type ResponsesToolDefinition, type ResponsesUsage, type RetryConfig, type RuntimeMode, type ScoreDecision, type SessionCompactionOptions, type SessionCompactionResult, type SessionTurn, ShowRequestOptions, ShowResponse, type SpanAttributeValue, type SpanAttributes, StatusResponse, type SupportedSchema, type SystemOneAnswer, type SystemOneAnswers, SystemOneChoiceAnswer, SystemOneChoiceQuestion, SystemOneContent, SystemOneNoulAnswer, SystemOneNoulQuestion, type SystemOneQuestion, type SystemOneQuestions, type SystemOneRequest, type SystemOneResponse, SystemOneScoreAnswer, SystemOneScoreQuestion, type SystemOneUsage, ThinkValue, ThinkingMetadata, type TimeoutSignal, TokenUsage, Tool, ToolCall, ToolCallAccumulator, ToolDefinition, ToolExecutionResult, ToolHandler, ToolParameters, ToolRegistry, type TurnCacheStats, type UsageBucket, type UsageMetrics, UsageRequestOptions, type UsageResponse, VersionResponse, VisionInput, WEB_FETCH_TOOL_NAME, WEB_SEARCH_TOOL_NAME, WebFetchRequestOptions, WebFetchResponse, type WebFetchToolParams, WebSearchRequestOptions, WebSearchResponse, type WebSearchToolParams, type WebToolsClient, type WebToolsOptions, addSpanEvent, batchEmbed, calculateBackoff, canonicalToolCallSignature, checkChatContext, checkEndpointHealth, checkGenerateContext, compactConversationHistory, compareDraftPerformance, contextWarningMessage, createDecision, createModelAffinityScheduler, createOllamaCloudFreeTierQuota, createTimeoutSignal, createWebFetchTool, createWebSearchTool, createWebTools, defineTool, detectModelCapabilities, embedBatchOverflowMessage, encodeImage, estimateChatRequestTokens, estimateGenerateRequestTokens, estimateMessageTokens, estimateSystemOneRequestBytes, estimateTokens, extractContextLength as extractNativeContextLength, extractParameterNumCtx, fetchWithBackoff, findOversizedEmbedInputs, findRunningModelContextLength, formatTelemetry, formatWebFetchResult, formatWebSearchResult, inferRuntimeMode, isKeepAliveSugar, isKnownDoneReason, isModelResident, isSameToolCall, listAllMcpTools, listAvailableModels, loadMcpTools, mergeToolCall, mergeToolCallArgumentsString, mergeToolCallArrays, normalizeChatStream, normalizeGenerateStream, normalizeKeepAlive, normalizeProgressStream, parseNdjsonStream, parseStructuredOutput, registerMcpTools, resolveContextLength, resolveCredentialEndpoints, sanitizeHistoryForNextTurn, setSpanError, toDataStream, toResponse, toTextStream, withRetry, withSpan, zodToJsonSchema };
4052
+ export { ATTR_GEN_AI_OPERATION_NAME, ATTR_GEN_AI_REQUEST_MODEL, ATTR_GEN_AI_RESPONSE_MODEL, ATTR_GEN_AI_SYSTEM, ATTR_GEN_AI_TOOL_CALL_ID, ATTR_GEN_AI_TOOL_NAME, ATTR_GEN_AI_USAGE_INPUT_TOKENS, ATTR_GEN_AI_USAGE_OUTPUT_TOKENS, ATTR_HTTP_REQUEST_METHOD, ATTR_HTTP_RESPONSE_STATUS_CODE, ATTR_OLLAMA_AGENT_ITERATION, ATTR_OLLAMA_AGENT_MAX_ITERATIONS, ATTR_OLLAMA_ENDPOINT_ATTEMPT, ATTR_OLLAMA_ENDPOINT_NAME, ATTR_SERVER_ADDRESS, ATTR_SERVER_PORT, ATTR_URL_FULL, AbortableAsyncIterable, type AffinityRunningModel, Agent, type AgentChatClient, type AgentConfig, type AgentHooks, type AgentResult, type AgentRunInput, type AgentTurn, type AnswerFor, AnthropicCompatClient, AnyTool, type BackoffOptions, BalanceRequestOptions, type BalanceResponse, type BlobUploadResult, ChatRequestOptions, ChatResponse, ChatStreamResult, type ChoiceDecision, type CompactionOptions, type CompareDraftPerformanceOptions, type ContextCheck, type ContextCheckOptions, type ContextDiscoveryRequestOptions, type ContextLengthSignals, type ContextLengthSource, type ContextWindowSource, type ConversationSendOptions, ConversationSession, type ConversationSessionOptions, CopyRequestOptions, type CreateModelFromGgufOptions, CreateRequestOptions, type CumulativeCacheStats, DEFAULT_BACKOFF, DEFAULT_BASE_URL, DEFAULT_CONTEXT_WARNING_THRESHOLD, DEFAULT_EMBED_BATCH_SIZE, DEFAULT_EMBED_CONCURRENCY, DEFAULT_FAILOVER_CODES, DEFAULT_FETCH_BACKOFF_CONFIG, DEFAULT_RETRY_CONFIG, DEFAULT_TIMEOUT_MS, type Decision, type DefineToolOptions, DeleteRequestOptions, type DiscoveredContextLength, type DraftBenchmarkClient, type DraftPerformanceComparison, type DraftRunStats, type EmbedBatchOptions, type EmbedBatchResult, EmbedRequestOptions, EmbedResponse, EmbeddingsRequestOptions, EmbeddingsResponse, type EndpointHealth, type EndpointHealthCheckResult, EndpointRegistry, type EndpointRegistryOptions, FetchLike, type FetchWithBackoffConfig, FormatOption, type FormattedTelemetry, GEN_AI_SYSTEM_OLLAMA, GenerateRequestOptions, GenerateResponse, GenerateStreamResult, type HistorySanitizeOptions, HttpClient, IMAGE_TOKEN_ESTIMATE, type IncludedBalance, KEEP_ALIVE_INDEFINITE, KEEP_ALIVE_UNLOAD, KNOWN_DONE_REASONS, type KeepAlive, type KnownDoneReason, type LegacyBalanceLimit, type LegacyIncludedBalance, type LoadMcpToolsOptions, Logger, MAX_SYSTEM_ONE_IMAGES_BYTES, MAX_SYSTEM_ONE_REQUEST_BYTES, MESSAGE_OVERHEAD_TOKENS, McpBridge, type McpBridgeOptions, McpCallToolResult, McpClientLike, McpTask, McpToolDescriptor, Message, Middleware, type ModelAffinityClient, ModelAffinityRouter, type ModelAffinityRoutingOptions, ModelAffinityScheduler, type ModelAffinitySchedulerOptions, type ModelAffinitySnapshot, type ModelAffinityStats, type ModelCapabilities, ModelOptions, ModelResponse, ModelsClient, type NoulDecision, OLLAMA_CLOUD_BASE_URL, OLLAMA_FALLBACK_CONTEXT_LENGTH, OllamaClient, type OllamaClientConfig, type OllamaCloudFreeTierQuotaBudgets, type OllamaCredentialConfig, type OllamaEndpoint, OllamaStream, OllamaStreamEvent, OllamaStreamEventType, OpenAICompatClient, type OversizedEmbedInput, type ParallelToolCallBehavior, PingResult, ProgressResponse, ProgressStreamResult, PsResponse, PullRequestOptions, PushRequestOptions, QuotaManager, type QuotaManagerOptions, type QuotaWindowConfig, type QuotaWindowStatus, RETRYABLE_STATUS_CODES, type RankResult, type RawOllamaMetrics, RawUsageSource, RequestCancellationOptions, RequestLifecycleHook, RequestRunner, type ResponsesCreateRequest, type ResponsesCreateResponse, ResponsesModule, type ResponsesReasoningEffort, type ResponsesStreamEvent, type ResponsesToolDefinition, type ResponsesUsage, type RetryConfig, type RuntimeMode, type ScoreDecision, type SessionCompactionOptions, type SessionCompactionResult, type SessionTurn, ShowRequestOptions, ShowResponse, type SpanAttributeValue, type SpanAttributes, StatusResponse, type StreamObserverCallbacks, type StreamObserverPhase, type SupportedSchema, type SystemOneAnswer, type SystemOneAnswers, SystemOneChoiceAnswer, SystemOneChoiceQuestion, SystemOneContent, SystemOneNoulAnswer, SystemOneNoulQuestion, type SystemOneQuestion, type SystemOneQuestions, type SystemOneRequest, type SystemOneResponse, SystemOneScoreAnswer, SystemOneScoreQuestion, type SystemOneUsage, ThinkValue, ThinkingMetadata, type TimeoutSignal, TokenUsage, Tool, ToolCall, ToolCallAccumulator, ToolDefinition, ToolExecutionResult, ToolHandler, ToolParameters, ToolRegistry, type TurnCacheStats, type UsageBucket, type UsageMetrics, UsageRequestOptions, type UsageResponse, VersionResponse, VisionInput, WEB_FETCH_TOOL_NAME, WEB_SEARCH_TOOL_NAME, WebFetchRequestOptions, WebFetchResponse, type WebFetchToolParams, WebSearchRequestOptions, WebSearchResponse, type WebSearchToolParams, type WebToolsClient, type WebToolsOptions, addSpanEvent, batchEmbed, calculateBackoff, canonicalToolCallSignature, checkChatContext, checkEndpointHealth, checkGenerateContext, compactConversationHistory, compareDraftPerformance, contextWarningMessage, createDecision, createModelAffinityScheduler, createOllamaCloudFreeTierQuota, createTimeoutSignal, createWebFetchTool, createWebSearchTool, createWebTools, defineTool, detectModelCapabilities, embedBatchOverflowMessage, encodeImage, estimateChatRequestTokens, estimateGenerateRequestTokens, estimateMessageTokens, estimateSystemOneRequestBytes, estimateTokens, extractContextLength as extractNativeContextLength, extractParameterNumCtx, fetchWithBackoff, findOversizedEmbedInputs, findRunningModelContextLength, formatTelemetry, formatWebFetchResult, formatWebSearchResult, inferRuntimeMode, isKeepAliveSugar, isKnownDoneReason, isModelResident, isSameToolCall, listAllMcpTools, listAvailableModels, loadMcpTools, mergeToolCall, mergeToolCallArgumentsString, mergeToolCallArrays, normalizeChatStream, normalizeGenerateStream, normalizeKeepAlive, normalizeProgressStream, observeStream, parseNdjsonStream, parseStructuredOutput, recommendContextWindow, registerMcpTools, resolveContextLength, resolveCredentialEndpoints, sanitizeHistoryForNextTurn, setSpanError, toDataStream, toResponse, toTextStream, withRetry, withSpan, zodToJsonSchema };