MingaClaudeBridge.LineBuffer (minga_claude_bridge v0.1.0)

Copy Markdown View Source

Joins Port line fragments into complete lines.

A Port opened with {:line, max} delivers each output line as one or more {:noeol, chunk} fragments followed by one {:eol, chunk} fragment. push/2 accepts one fragment at a time and returns a complete line when it receives {:eol, chunk}. Lines never include the newline.

The buffer enforces a byte cap on a single line (default 33554432 bytes, 32 MiB). When the bytes of the current line exceed the cap, push/2 returns {:error, :line_too_long, buffer} once and drops the fragments it holds. The buffer then discards the remaining fragments of that line up to and including its {:eol, chunk}, and starts the next line clean. This keeps the tail of an oversized line from being reported as a separate, truncated line.

The module is pure and holds no payload outside the returned struct.

Summary

Types

One fragment as delivered by a {:line, max} Port.

Phase of the line in progress.

Result of pushing one fragment.

t()

Functions

Returns the number of bytes held for the line in progress.

Returns an empty buffer.

Adds one Port fragment to the buffer.

Types

fragment()

@type fragment() :: {:eol | :noeol, binary()}

One fragment as delivered by a {:line, max} Port.

phase()

@type phase() :: {:collecting, [binary()], non_neg_integer()} | :discarding

Phase of the line in progress.

{:collecting, chunks, size} holds the reversed fragments of the current line and their total byte size. :discarding drops fragments until the end of an oversized line.

push_result()

@type push_result() ::
  {:line, binary(), t()} | {:more, t()} | {:error, :line_too_long, t()}

Result of pushing one fragment.

t()

@type t() :: %MingaClaudeBridge.LineBuffer{max_bytes: pos_integer(), phase: phase()}

Functions

buffered_bytes(line_buffer)

@spec buffered_bytes(t()) :: non_neg_integer()

Returns the number of bytes held for the line in progress.

new(opts \\ [])

@spec new(keyword()) :: t()

Returns an empty buffer.

Options:

  • :max_bytes - the largest line in bytes, excluding the newline. Defaults to 32 MiB.

push(buffer, arg)

@spec push(t(), fragment()) :: push_result()

Adds one Port fragment to the buffer.

Returns {:line, line, buffer} when the fragment completes a line, {:more, buffer} when the line continues, and {:error, :line_too_long, buffer} when the line exceeds the cap.