Skip to content

nimgent/providers/stream

This page is generated from the module’s exported API and ## documentation comments.

Shared OpenAI-family runtime: SSE, wake-fd, stream accumulation, usage JSON.

Action returned by an SSE event handler.

SseAction = enum
sseContinue, sseStop, sseCancel

View source

Terminal state returned by an SSE stream driver.

SseDrive = enum
sdEnded, ## [DONE] or handle sseStop
sdCancelled, sdClosed ## body ended without a terminal

View source

Optional file-descriptor watcher used during streaming.

WakeWatch = object

View source

Tool-call fragments accumulated from a stream.

PendingTool = object
id*: string
name*: string
args*: string
itemId*: string

View source

Text, reasoning, and tool fragments accumulated from a stream.

StreamAcc = object
text*: string
think*: string
details*: JsonNode
tools*: seq[PendingTool]
parsedFinal*: bool

View source

Create an empty stream accumulator.

proc initStreamAcc(): StreamAcc {.raises: [], tags: [], forbids: [].}

Returns: StreamAcc.

View source

Chat Completions (prompt_tokens) and Responses (input_tokens) usage.

proc parseOpenAiUsage(usage: JsonNode; result: var Usage) {.raises: [],
tags: [], forbids: [].}
Name Type Default
usage JsonNode
result var Usage

View source

Remove and return one newline-terminated line from a buffer.

proc popLine(buf: var string): tuple[ok: bool, line: string] {.raises: [],
tags: [], forbids: [].}

Returns: tuple[ok: bool, line: string].

Name Type Default
buf var string

View source

Register a wake file descriptor when one is supplied.

proc register(w: var WakeWatch; wakeFd: cint) {.raises: [], tags: [],
forbids: [].}
Name Type Default
w var WakeWatch
wakeFd cint

View source

Unregister a previously watched wake file descriptor.

proc unregister(w: var WakeWatch) {.raises: [], tags: [], forbids: [].}
Name Type Default
w var WakeWatch

View source

Check cancellation even when another input poller has consumed wakeFd.

proc awaitWithWakeAsync[T](fut: Future[T]; watch: ptr WakeWatch; wakeFd: cint;
onEvent: StreamCallback): Future[bool] {.
stackTrace: false.}

Type parameters: T.

Returns: Future[bool].

Name Type Default
fut Future[T]
watch ptr WakeWatch
wakeFd cint
onEvent StreamCallback

View source

Read all remaining chunks from an asynchronous response body.

proc drainBodyStreamAsync(bodyStream: FutureStream[string]): Future[string] {.
stackTrace: false, raises: [Exception, ValueError], tags: [RootEffect],
forbids: [].}

Returns: Future[string].

Name Type Default
bodyStream FutureStream[string]

View source

Parse SSE data lines and pass each JSON event to a handler.

proc forEachSseAsync(bodyStream: FutureStream[string]; watch: ptr WakeWatch;
wakeFd: cint; onEvent: StreamCallback;
handle: proc (data: JsonNode): SseAction {.closure.}): Future[
SseDrive] {.stackTrace: false, raises: [Exception, ValueError, OSError],
tags: [RootEffect, TimeEffect, ReadIOEffect, WriteIOEffect],
forbids: [].}

Returns: Future[SseDrive].

Name Type Default
bodyStream FutureStream[string]
watch ptr WakeWatch
wakeFd cint
onEvent StreamCallback
handle proc (data: JsonNode): SseAction {.closure.}

View source

Turn accumulated stream fragments into response content once.

proc assembleStream(acc: StreamAcc; response: var ProviderResponse) {.
raises: [], tags: [ReadIOEffect, WriteIOEffect], forbids: [].}
Name Type Default
acc StreamAcc
response var ProviderResponse

View source