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.
SseAction
Section titled “SseAction”Action returned by an SSE event handler.
SseAction = enum sseContinue, sseStop, sseCancelSseDrive
Section titled “SseDrive”Terminal state returned by an SSE stream driver.
SseDrive = enum sdEnded, ## [DONE] or handle sseStop sdCancelled, sdClosed ## body ended without a terminalWakeWatch
Section titled “WakeWatch”Optional file-descriptor watcher used during streaming.
WakeWatch = objectPendingTool
Section titled “PendingTool”Tool-call fragments accumulated from a stream.
PendingTool = object id*: string name*: string args*: string itemId*: stringStreamAcc
Section titled “StreamAcc”Text, reasoning, and tool fragments accumulated from a stream.
StreamAcc = object text*: string think*: string details*: JsonNode tools*: seq[PendingTool] parsedFinal*: boolProcedures
Section titled “Procedures”initStreamAcc
Section titled “initStreamAcc”Create an empty stream accumulator.
proc initStreamAcc(): StreamAcc {.raises: [], tags: [], forbids: [].}Returns: StreamAcc.
parseOpenAiUsage
Section titled “parseOpenAiUsage”Chat Completions (prompt_tokens) and Responses (input_tokens) usage.
proc parseOpenAiUsage(usage: JsonNode; result: var Usage) {.raises: [], tags: [], forbids: [].}Arguments
Section titled “Arguments”| Name | Type | Default |
|---|---|---|
usage |
JsonNode |
|
result |
var Usage |
popLine
Section titled “popLine”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].
Arguments
Section titled “Arguments”| Name | Type | Default |
|---|---|---|
buf |
var string |
register
Section titled “register”Register a wake file descriptor when one is supplied.
proc register(w: var WakeWatch; wakeFd: cint) {.raises: [], tags: [], forbids: [].}Arguments
Section titled “Arguments”| Name | Type | Default |
|---|---|---|
w |
var WakeWatch |
|
wakeFd |
cint |
unregister
Section titled “unregister”Unregister a previously watched wake file descriptor.
proc unregister(w: var WakeWatch) {.raises: [], tags: [], forbids: [].}Arguments
Section titled “Arguments”| Name | Type | Default |
|---|---|---|
w |
var WakeWatch |
awaitWithWakeAsync
Section titled “awaitWithWakeAsync”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].
Arguments
Section titled “Arguments”| Name | Type | Default |
|---|---|---|
fut |
Future[T] |
|
watch |
ptr WakeWatch |
|
wakeFd |
cint |
|
onEvent |
StreamCallback |
drainBodyStreamAsync
Section titled “drainBodyStreamAsync”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].
Arguments
Section titled “Arguments”| Name | Type | Default |
|---|---|---|
bodyStream |
FutureStream[string] |
forEachSseAsync
Section titled “forEachSseAsync”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].
Arguments
Section titled “Arguments”| Name | Type | Default |
|---|---|---|
bodyStream |
FutureStream[string] |
|
watch |
ptr WakeWatch |
|
wakeFd |
cint |
|
onEvent |
StreamCallback |
|
handle |
proc (data: JsonNode): SseAction {.closure.} |
assembleStream
Section titled “assembleStream”Turn accumulated stream fragments into response content once.
proc assembleStream(acc: StreamAcc; response: var ProviderResponse) {. raises: [], tags: [ReadIOEffect, WriteIOEffect], forbids: [].}Arguments
Section titled “Arguments”| Name | Type | Default |
|---|---|---|
acc |
StreamAcc |
|
response |
var ProviderResponse |