Skip to content
2 min read · 457 words

@nhtio/adk/batteries/generation/local_diffusion/protocol

Pure protocol constants, frame parsing, and byte-safe line framing for local diffusion backends.

Remarks

The line framer is byte-oriented: it retains raw bytes (never a decoded string), enforces the maxLineBytes cap while consuming input (so an unbounded no-newline write cannot balloon memory), discards the remainder of an over-sized physical line up to its next newline before resuming, and decodes each bounded, complete line with a fatal UTF-8 decoder (invalid encoding → a malformed frame, never a silent mis-parse). parseFrame is total — it classifies every line as a typed frame and never throws.

Type Aliases

Type AliasDescription
DoneFrameA parsed request completion frame.
ErrorFrameA parsed request error frame.
ImageFrameA parsed image result frame.
MalformedFrameA prefix-matched frame whose fields or JSON do not satisfy the protocol.
ModelLoadFrameA parsed startup model-loading progress frame.
ParsedFrameThe total, discriminated result of parsing one backend stdout line.
ProgressFrameA parsed request progress frame.
ProtocolConfigThe configurable wire tags used by a local diffusion backend.
ProtocolErrorFrameA stream-level protocol problem, such as an over-sized line.
ReadyFrameA parsed backend-ready frame.
UnknownFrameA frame with a tag this protocol version does not understand.

Variables

VariableDescription
DEFAULT_PROTOCOLThe DiffusionBee-compatible default local diffusion protocol configuration.

Functions

FunctionDescription
buildEditCommandBuild an edit command frame, including its terminating newline.
buildGenerateCommandBuild a generate command frame, including its terminating newline.
buildShutdownCommandBuild a backend shutdown command frame, including its terminating newline.
buildStopCommandBuild a best-effort stop command frame, including its terminating newline.
createFrameReaderCreate an incremental, byte-oriented line reader. Raw bytes are retained (never a decoded string), the maxLineBytes cap is enforced while consuming so an unbounded no-newline write cannot balloon memory, an over-sized physical line reports exactly one protocolError and is then discarded through its next newline (its continuation is NOT re-parsed as a fresh frame), and each bounded, complete line is decoded with a fatal UTF-8 decoder — invalid encoding yields a malformed frame. Malformed protocol lines are reported as malformed rather than thrown.
parseFrameParse one already-split backend stdout line into a typed ParsedFrame. Total by contract — it never throws: malformed input yields a malformed/unknown frame, and a runtime-invalid or hostile config (null, partial, or a throwing accessor) is caught and reported as malformed.