Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
/** A single delta that adds more than this many characters is a burst worth spreading over frames; anything smaller is
* drawn as it arrives, with no frame of its own. */
export const PACE_BURST_CHARS = 96;
/** A burst's backlog closes with this time constant, but never slower than MIN_CHARS_PER_SEC. */
const CATCHUP_MS = 90;
const MIN_CHARS_PER_SEC = 140;
/** A frame this long after the last one means the page was in the background: what is waiting shows at once. */
export const PACE_STALE_FRAME_MS = 250;
const SPACE = /[ \n]/;
/** Characters that belong to the one before them, so a cut must never fall just before one. */
const JOINS_PREVIOUS = /^(?:\p{M}|\u200d)/u;
/** Where the words that streamed so far may be cut: just after their last space or line break (all of it once done). */
export function wordBoundary(text: string, done: boolean): number {
return done ? text.length : Math.max(text.lastIndexOf(" "), text.lastIndexOf("\n")) + 1;
}
/** The first cut at or after `from` that falls just after a space and before a new character, else `goal`. */
function cutFrom(text: string, from: number, goal: number): number {
for (let at = Math.max(1, from); at < goal; at++) {
if (SPACE.test(text[at - 1]!) && !JOINS_PREVIOUS.test(text.slice(at, at + 1))) return at;
}
return goal;
}
/** How much of `text` to show after a frame of `dtMs`, moving from `shown` toward `goal` (a cut `wordBoundary` gave).
* It only ever stops just after a space, so a word, a link or an emoji is never split; with no space ahead it shows all. */
export function nextPacedLength(shown: number, text: string, goal: number, dtMs: number): number {
if (shown >= goal) return goal;
const dt = Math.min(Math.max(dtMs, 4), 64);
const step = Math.max(Math.ceil(((goal - shown) * dt) / CATCHUP_MS), Math.ceil((MIN_CHARS_PER_SEC * dt) / 1000));
return shown + step >= goal ? goal : cutFrom(text, shown + step, goal);
}