sink

package
v0.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 20, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsLifecycleEvent

func IsLifecycleEvent(e zenflow.Event) bool

IsLifecycleEvent reports whether an event must be flushed immediately by a coalescing sink (see Buffered). Lifecycle events are step/workflow transitions, resume lifecycle markers, drops, errors, plan-ready, transcript-sealed, and coordinator-inbox (reverse-reply) messages - any signal where user-visible latency matters more than batching. High-frequency delta events (narration, agent_turn, tool_call, agent_inbox_drain, idle/wake, coordinator_message/synthesis, generic message) are NOT lifecycle: they are safe to coalesce within the Buffered window. Exported so consumers (TUI adapter, JSONSink) share one canonical list rather than re-encoding it.

Types

type BufferedSink

type BufferedSink struct {
	// contains filtered or unexported fields
}

BufferedSink is a ProgressSink that batches high-volume delta events (narration, agent_turn, tool_call, output, etc.) within a time window and flushes immediately on lifecycle events (see IsLifecycleEvent). The returned sink implements zenflow.ProgressSink and an additional Close error method that flushes any pending batch - call it when the run ends to avoid losing the final window of deltas. Backpressure: OnEvent/OnOutput block when the internal buffer is full (defaults to 1024 entries); consumers wrapping a slow sink should choose a larger buffer to avoid coupling LLM throughput to UI responsiveness. Goroutine model: a single dispatcher goroutine owns the batch and the timer. Callers submit work via a buffered channel; lifecycle events cause the dispatcher to emit the current batch (in order) followed immediately by the lifecycle event itself.

func (*BufferedSink) Close

func (b *BufferedSink) Close() error

Close drains any pending batch and stops the dispatcher. Safe to call multiple times. Order matters: close(b.done) runs BEFORE the flush dance so concurrent OnEvent/OnOutput callers prefer the wrapped (synchronous) path instead of racing into b.in. The loop also drains any items that snuck into b.in after isFlush was queued (FIFO would otherwise leave them stuck behind the flush sentinel and silently dropped when the loop returns).

func (*BufferedSink) OnEvent

func (b *BufferedSink) OnEvent(ctx context.Context, e zenflow.Event)

OnEvent queues an event. Lifecycle events are flushed immediately (the dispatcher emits any pending batch first to preserve order).

func (*BufferedSink) OnOutput

func (b *BufferedSink) OnOutput(ctx context.Context, o zenflow.Output)

OnOutput queues an output delta for coalescing.

type ClosableProgressSink

type ClosableProgressSink interface {
	zenflow.ProgressSink
	Close() error
}

ClosableProgressSink is a ProgressSink that owns shutdown semantics. Exposed so callers of Buffered can hold the concrete shutdown contract via an exported named interface instead of the anonymous interface{ Close error } type assertion ceremony. defer Close is the canonical shutdown pattern; the interface lets callers store the result without knowing the unexported *BufferedSink type.

func Buffered

func Buffered(wrapped zenflow.ProgressSink, window time.Duration) ClosableProgressSink

Buffered returns a ProgressSink that coalesces delta events within the given window. When window <= 0, defaults to 100ms. The return type is ClosableProgressSink so callers can defer b.Close directly without an anonymous type assertion. The underlying *BufferedSink already exposes Close error; this just surfaces the contract through the static type.

type JSONSink

type JSONSink struct {
	// contains filtered or unexported fields
}

JSONSink writes workflow events as NDJSON (one JSON object per line). It is safe for concurrent use. Construct via JSON(w). The concrete type is exported so callers can inspect Err after the run completes to detect I/O failures the streaming write path silently latched.

func JSON

func JSON(w io.Writer) *JSONSink

JSON returns a ProgressSink that writes NDJSON to the given writer. It is the canonical machine-readable sink for `zenflow flow --json` and any library consumer that wants structured event output.

func (*JSONSink) Err

func (s *JSONSink) Err() error

Err returns the first write error encountered, if any. Consumers should check after workflow completion to detect I/O failures.

func (*JSONSink) OnEvent

func (s *JSONSink) OnEvent(_ context.Context, event zenflow.Event)

OnEvent handles a workflow event by encoding it as a JSON line.

func (*JSONSink) OnOutput

func (s *JSONSink) OnOutput(_ context.Context, output zenflow.Output)

OnOutput handles streaming agent output by encoding it as a JSON line.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL