@adonis-agora/agent
Version:
Governed, durable-backed AI agent (chat + tool-calling + governance) for AdonisJS — part of the Agora ecosystem.
58 lines • 1.58 kB
JavaScript
/**
* In-memory `TokenStreamSink`. Buffers typed frames per run so a late subscriber (reconnect)
* replays everything emitted so far, then follows live until the run ends.
*/
export class InMemoryTokenStreamSink {
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-memory-sink.js.map