@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
45 lines (44 loc) • 1.67 kB
JavaScript
/** Splits a transcript into words on any whitespace. */
export function splitWords(text) {
return text.split(/\s+/).filter(Boolean);
}
/** Length of the shared prefix of two word lists. */
export function commonPrefixLength(a, b) {
let n = 0;
while (n < a.length && n < b.length && a[n] === b[n]) {
n++;
}
return n;
}
export function createLocalAgreement() {
return { prevWords: [], committedWords: [], committedN: 0 };
}
/**
* Folds one pass into the state and returns the split for display. Words
* this pass agrees on with the previous one, beyond what is already
* committed, are appended to the committed list; the words already committed
* are kept as they were shown — they are never re-read from the new pass, so
* a later pass that hears the start differently (or comes back shorter)
* cannot shrink or rewrite them. The tail is whatever this pass has past the
* committed count.
*/
export function updateLocalAgreement(state, text) {
const words = splitWords(text);
const agreedN = Math.max(state.committedN, commonPrefixLength(state.prevWords, words));
if (agreedN > state.committedN) {
state.committedWords.push(...words.slice(state.committedN, agreedN));
state.committedN = state.committedWords.length;
}
state.prevWords = words;
return {
words,
committed: state.committedWords.join(" "),
tail: words.slice(state.committedN).join(" "),
};
}
/** Forgets everything, e.g. when another engine takes the utterance over. */
export function resetLocalAgreement(state) {
state.prevWords = [];
state.committedWords = [];
state.committedN = 0;
}