@sema-agent/core
Version:
Stateless, task-oriented AI agent core
46 lines (45 loc) • 2.67 kB
TypeScript
/**
* A place a writer keeps in a {@link PushQueue}: what it puts there is delivered at that place, and every value pushed after the
* place was kept waits behind it until the place is filled. `put` delivers values and keeps the place (more may follow there);
* `fill` delivers values and frees the place. Once filled, the place takes nothing more (a later `put` / `fill` is a no-op);
* filling with nothing frees it.
*/
export interface QueueSlot<T> {
put(values: readonly T[]): void;
fill(values: readonly T[]): void;
}
/**
* A minimal async iterable you can push values into and close. Backpressure-free (buffers).
*
* A writer that must decide later what stands at the present point of the stream keeps a place with {@link reserve}: values pushed
* after it are held behind it (accepted, in push order) until it is filled. `close()` refuses later pushes and later places, and the
* iteration ends once every place kept before the close is filled and everything ahead of the end is delivered — a kept place is
* part of what was pushed before the close, so its owner must fill it (a place that is never filled holds the iteration open).
*/
export declare class PushQueue<T> implements AsyncIterable<T> {
private buffer;
private waiters;
private closed;
/** What waits behind the oldest open place, in order — empty when no place is kept. */
private held;
/** Returns whether the value was ACCEPTED (`false` after close — the push is a silent no-op then).
* Callers that mirror a pushed value to a second reader gate on this so both sides stay in parity
* (#253: a status frame must not reach a side sink the stream itself already refused). A value accepted while a place is kept
* is delivered after that place is filled: a reader mirroring at push sees it earlier than the stream's reader does. */
push(value: T): boolean;
/** Keep a place at the present point of the stream; `undefined` after close (nothing more is accepted). */
reserve(): QueueSlot<T> | undefined;
close(): void;
/**
* Free every kept place with what was put into it so far: the writer that kept them can no longer fill them (the run that
* owned it died before its own settle). Values held behind the places are delivered in order; a later `put` / `fill` on a
* freed place is a no-op.
*/
freePlaces(): void;
/** Deliver what stands ahead of the oldest open place (and what was put into it), then end the iteration if it is closed and
* nothing is held. */
private release;
private deliver;
private endIfDone;
[Symbol.asyncIterator](): AsyncIterator<T>;
}