Documentation
¶
Overview ¶
Package eventmerge joins the streaming delta events a runtime emits (one per model token or per tool output write) into fewer, larger deltas.
Only consecutive deltas that extend the same message or tool call are joined, so the merged stream carries exactly the same content, in the same order, as the original one. Consumers that concatenate deltas see no difference other than the chunk boundaries.
Index ¶
Constants ¶
const DefaultWindow = 50 * time.Millisecond
DefaultWindow is the coalescing window used when none is configured. It matches the TUI's event throttle.
Variables ¶
This section is empty.
Functions ¶
func Coalesce ¶
func Coalesce(ctx context.Context, in <-chan runtime.Event, window time.Duration) <-chan runtime.Event
Coalesce forwards the events of in, merging streaming deltas (see Merge) so at most one batch of them is emitted per window.
- A delta arriving after a quiet spell of at least window is forwarded immediately, so the first token is never delayed.
- Any other event first flushes the pending deltas, so the merged stream keeps the original order.
- Pending deltas are flushed early once they exceed maxPendingBytes, and when in is closed.
A window <= 0 disables coalescing and returns in itself.
The returned channel is closed once in is closed. Coalesce always drains in to the end, discarding events once ctx is done, so the producer never blocks on an abandoned consumer.
func Merge ¶
func Merge[E any](events []E) []E
Merge joins each run of consecutive deltas that extend the same message or tool call into a single event. Any other value passes through unchanged and ends the current run. A delta that has nothing to merge with is returned as-is, not copied.
Each run is built with a single strings.Builder, so merging N chunks costs O(N) rather than the O(N^2) of repeated string concatenation.
Types ¶
This section is empty.