@adonis-agora/agent
Version:
Governed, durable-backed AI agent (chat + tool-calling + governance) for AdonisJS — part of the Agora ecosystem.
60 lines • 1.8 kB
JavaScript
/**
* The default {@link TokenStreamSink}: buffers typed frames per run in-process so a reconnecting
* subscriber replays everything so far, then follows live until the run ends. Good for a single
* replica; a multi-replica deployment needs a shared (Redis) sink so any pod can serve any run's
* stream. Identical (frame-for-frame) to the `InMemoryTokenStreamSink` shipped from `./testing`.
*/
export class InProcessTokenStreamSink {
runs = new Map();
buffer(runId) {
let buf = this.runs.get(runId);
if (buf === undefined) {
buf = { chunks: [], ended: false, notify: new Set() };
this.runs.set(runId, buf);
}
return buf;
}
wake(buf) {
for (const resolve of buf.notify) {
resolve();
}
buf.notify.clear();
}
open(runId) {
const buf = this.buffer(runId);
return {
write: (frame) => {
buf.chunks.push(frame);
this.wake(buf);
},
end: () => {
buf.ended = true;
this.wake(buf);
},
};
}
async *subscribe(runId) {
const buf = this.buffer(runId);
let index = 0;
while (true) {
while (index < buf.chunks.length) {
const chunk = buf.chunks[index];
index += 1;
if (chunk !== undefined) {
yield chunk;
}
}
if (buf.ended) {
return;
}
await new Promise((resolve) => buf.notify.add(resolve));
}
}
has(runId) {
return this.runs.has(runId);
}
close(runId) {
this.runs.delete(runId);
}
}
//# sourceMappingURL=in-process-sink.js.map