@adonis-agora/agent
Version:
Governed, durable-backed AI agent (chat + tool-calling + governance) for AdonisJS — part of the Agora ecosystem.
62 lines • 3.79 kB
TypeScript
import type { RedisStreamClient } from './redis-stream-client.js';
import type { SinkWriter, StreamFrame, TokenStreamSink } from './spi/token-stream-sink.js';
export interface RedisTokenStreamSinkOptions {
/** Key/channel namespace. Defaults to `agent:stream`. Keys are `${keyPrefix}:${runId}:...`. */
keyPrefix?: string;
/**
* TTL (seconds) applied to a run's `chunks`/`state` keys so they self-expire — the framework never
* calls {@link RedisTokenStreamSink.close}, so without this the replay buffer would accumulate in
* Redis forever. The TTL is a sliding window refreshed on every write, so a long run stays alive and
* a crashed run (that never `end`s) still expires. Defaults to 3600 (1h). Set `0` to disable and
* retain keys until `close()`/manual cleanup (the pre-TTL behaviour).
*/
ttlSeconds?: number;
}
/**
* A multi-replica {@link TokenStreamSink} over Redis — the shared-transport counterpart to the default
* {@link import('./in-process-sink.js').InProcessTokenStreamSink}. The replica running the model turn
* `open`s the writer and appends each token chunk to a Redis LIST (`${keyPrefix}:${runId}:chunks`) so
* ANY replica can replay the whole stream so far (the same late-subscriber/reconnect guarantee the
* in-process sink gives, but shared across pods), and PUBLISHes to a per-run channel
* (`${keyPrefix}:${runId}`) to wake live subscribers. The run's terminal state lives in a marker key
* (`${keyPrefix}:${runId}:state`), so an SSE handler on ANOTHER replica that subscribes after the run
* finished still sees the ending and closes the stream.
*
* Consecutive deltas written within 50 ms — answer text, reasoning, one tool call's argument text —
* are pushed as ONE frame holding their concatenation, and each push is one round trip when the
* client offers {@link RedisStreamClient.append}. Every reader appends those deltas, so the text a
* client renders is unchanged; the list is the run's stream, so every replica numbers its SSE events
* the same. A process that dies within that window loses the gathered delta from the LIVE stream
* (the persisted message is unaffected).
*
* A subscriber reads the terminal marker once when it attaches and otherwise waits for the `end`
* message. It ends when the run's keys expired under it, and starts over from the first element when
* the list was started again (a run parked past its TTL that then wrote again). Each element carries
* a write id unique to it, which is how a restarted list is told apart from the one being followed;
* subscribers strip it, so readers get exactly the frames written.
*
* The host supplies a {@link RedisStreamClient} adapter over its own Redis driver; `subscribe` needs a
* connection in subscriber mode (see the client docs). Wire it with `defineConfig({ sink:
* tokenSinks.redis({...}) })`, which builds the adapter over `@adonisjs/redis` lazily.
*/
export declare class RedisTokenStreamSink implements TokenStreamSink {
private readonly client;
private readonly keyPrefix;
private readonly ttlSeconds;
private readonly writes;
constructor(client: RedisStreamClient, options?: RedisTokenStreamSinkOptions);
/** Refresh the sliding TTL on `key` (no-op when TTL is disabled or the client can't `expire`). */
private touchTtl;
private chunksKey;
private stateKey;
private channel;
/** Push one frame and wake the subscribers, or (`null`) mark the run ended. */
private append;
open(runId: string): SinkWriter;
subscribe(runId: string): AsyncIterable<StreamFrame>;
private follow;
has(runId: string): Promise<boolean>;
close(runId: string): Promise<void>;
private hasEnded;
}
//# sourceMappingURL=redis-token-stream-sink.d.ts.map