@juspay/neurolink
Version:
The pipe layer of an AI nervous system: one interface connecting provider neurons to your application, across three inference types — generate, stream and decide. `decide` returns typed, calibrated judgements from a non-generative model (~400ms, ~$0.00002
94 lines (93 loc) • 3.13 kB
JavaScript
/**
* Push-driven async iterable shared by the transcription sockets: socket
* frames are pushed in, `transcribeStream()` pulls them out.
*
* The queue reports its level to whoever feeds it, so a socket can be paused
* when audio arrives faster than the engine takes it and resumed once the
* backlog drains (TCP backpressure, instead of buffering the whole burst).
* Every queue also charges its bytes to one process-wide budget, so a burst
* of uploads across many sockets cannot exhaust the process.
*
* @module server/websocket/frameQueue
*/
/** Queued bytes across every `FrameQueue` of this process. */
let processQueuedBytes = 0;
/** What every queue in the process holds right now, for caps and tests. */
export function totalQueuedFrameBytes() {
return processQueuedBytes;
}
/** Push-driven async iterable: socket frames in, `transcribeStream()` pulls. */
export class FrameQueue {
frames = [];
waiting;
ended = false;
onLevel;
queuedBytes = 0;
/** Called after every change of `queuedBytes`, with the new level. */
setLevelListener(listener) {
this.onLevel = listener;
}
push(frame) {
if (this.ended) {
return;
}
if (this.waiting) {
const resolve = this.waiting;
this.waiting = undefined;
resolve({ value: frame, done: false });
return;
}
this.frames.push(frame);
this.charge(frame.length);
}
/**
* No more frames will arrive; the ones already queued are still handed
* out, so a recording whose `end` frame followed its audio is transcribed
* whole. The consumer's `return()` (see below) is what discards.
*/
end() {
if (this.ended) {
return;
}
this.ended = true;
if (this.waiting) {
const resolve = this.waiting;
this.waiting = undefined;
resolve({ value: undefined, done: true });
}
}
/** The consumer is gone: whatever it did not pull leaves the process budget. */
discard() {
this.end();
if (this.queuedBytes > 0) {
this.charge(-this.queuedBytes);
}
this.frames.length = 0;
}
charge(delta) {
this.queuedBytes += delta;
processQueuedBytes = Math.max(0, processQueuedBytes + delta);
this.onLevel?.(this.queuedBytes);
}
[Symbol.asyncIterator]() {
return {
next: () => {
const frame = this.frames.shift();
if (frame) {
this.charge(-frame.length);
return Promise.resolve({ value: frame, done: false });
}
if (this.ended) {
return Promise.resolve({ value: undefined, done: true });
}
return new Promise((resolve) => {
this.waiting = resolve;
});
},
return: () => {
this.discard();
return Promise.resolve({ value: undefined, done: true });
},
};
}
}