Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
import { closeSync, fstatSync, openSync, readSync, type Stats } from "node:fs";
import { join, resolve } from "node:path";
import type { RuntimeEvent } from "./contracts.ts";
import type { InspectorEntry, InspectorPage, NativeRecord } from "../shared/inspector.ts";
export type { InspectorEntry, InspectorPage, NativeRecord } from "../shared/inspector.ts";
const DEFAULT_LIMIT = 300;
const MAX_LIMIT = 2000;
const READ_CHUNK = 64 * 1024;
export const MAX_TAIL_BYTES = 8 * 1024 * 1024;
interface LineCount {
dev: number;
ino: number;
size: number;
mtimeMs: number;
complete: number;
trailing: boolean;
}
type FileStat = Pick<Stats, "dev" | "ino" | "size" | "mtimeMs">;
const lineCounts = new Map<string, LineCount>();
const LINE_COUNT_CACHE_MAX = 256;
/** Only harness-minted uuids: anything else is not a file to read. */
function assertThreadId(threadId: string) {
if (!/^[\w-]+$/.test(threadId)) throw new Error("invalid thread id");
}
function countLines(fd: number, file: string, stat: FileStat): number {
const previous = lineCounts.get(file);
const appended =
previous &&
previous.dev === stat.dev &&
previous.ino === stat.ino &&
stat.size >= previous.size &&
(stat.size > previous.size || stat.mtimeMs === previous.mtimeMs);
if (appended && stat.size === previous.size) return previous.complete + Number(previous.trailing);
let offset = appended ? previous.size : 0;
let complete = appended ? previous.complete : 0;
let trailing = appended ? previous.trailing : false;
while (offset < stat.size) {
const length = Math.min(READ_CHUNK, stat.size - offset);
const chunk = Buffer.allocUnsafe(length);
const read = readSync(fd, chunk, 0, length, offset);
if (read <= 0) break;
for (let i = 0; i < read; i++) {
if (chunk[i] === 0x0a) {
if (trailing) complete++;
trailing = false;
} else if (chunk[i] !== 0x0d) {
trailing = true;
}
}
offset += read;
}
const next = { dev: stat.dev, ino: stat.ino, size: stat.size, mtimeMs: stat.mtimeMs, complete, trailing };
lineCounts.delete(file);
lineCounts.set(file, next);
while (lineCounts.size > LINE_COUNT_CACHE_MAX) lineCounts.delete(lineCounts.keys().next().value!);
return complete + Number(trailing);
}
type RecordGuard<T> = (value: unknown) => value is T;
function parseRecent<T>(text: string, includeFirst: boolean, limit: number, valid: RecordGuard<T>): T[] {
const lines = text.split("\n");
if (!includeFirst) lines.shift();
const out: T[] = [];
for (const raw of lines) {
if (!raw) continue;
try {
const value: unknown = JSON.parse(raw);
if (valid(value)) out.push(value);
} catch {
}
}
return out.slice(-limit);
}
export function readRecentLines<T>(file: string, limit: number, valid: RecordGuard<T>): { lines: T[]; total: number } {
let fd: number;
try {
fd = openSync(file, "r");
} catch {
return { lines: [], total: 0 };
}
try {
const stat = fstatSync(fd);
const total = countLines(fd, file, stat);
let position = stat.size;
let bytes = Buffer.alloc(0);
let lines: T[] = [];
while (position > 0 && bytes.length < MAX_TAIL_BYTES) {
const remaining = MAX_TAIL_BYTES - bytes.length;
const start = Math.max(0, position - Math.min(READ_CHUNK, remaining));
const length = position - start;
const chunk = Buffer.allocUnsafe(length);
const read = readSync(fd, chunk, 0, length, start);
if (read <= 0) break;
bytes = Buffer.concat([chunk.subarray(0, read), bytes]);
position = start;
const text = bytes.toString("utf8");
if (position === 0 || text.split("\n").length - 1 >= limit) {
lines = parseRecent(text, position === 0, limit, valid);
if (lines.length >= limit || position === 0) break;
}
}
if (lines.length === 0 && bytes.length > 0) {
lines = parseRecent(bytes.toString("utf8"), position === 0, limit, valid);
}
return { lines, total };
} finally {
closeSync(fd);
}
}
const isRecord = (value: unknown): value is Record<string, unknown> => typeof value === "object" && value !== null && !Array.isArray(value);
const stringOrMissing = (value: unknown) => value === undefined || typeof value === "string";
const stringOrNullOrMissing = (value: unknown) => value === undefined || value === null || typeof value === "string";
const numberOrNullOrMissing = (value: unknown) => value === undefined || value === null || typeof value === "number";
const stringsOrMissing = (value: unknown) => value === undefined || (Array.isArray(value) && value.every((item) => typeof item === "string"));
const computerHolderOrMissing = (value: unknown) =>
value === undefined ||
(isRecord(value) && typeof value.name === "string" && stringOrMissing(value.task));
const askQuestionsOrMissing = (value: unknown) =>
value === undefined ||
(Array.isArray(value) &&
value.every(
(question) =>
isRecord(question) &&
typeof question.question === "string" &&
Array.isArray(question.options) &&
question.options.every((option) => isRecord(option) && typeof option.label === "string"),
));
/** Validate persisted records, including legacy relative handoff paths normalized during replay. */
export function isRuntimeEvent(value: unknown): value is RuntimeEvent {
if (
!isRecord(value) ||
typeof value.eventId !== "string" ||
typeof value.provider !== "string" ||
typeof value.threadId !== "string" ||
typeof value.createdAt !== "string" ||
typeof value.type !== "string" ||
!stringOrMissing(value.providerInstanceId) ||
!stringOrMissing(value.turnId) ||
!stringOrMissing(value.itemId) ||
!stringOrMissing(value.requestId)
) return false;
switch (value.type) {
case "session.started":
return (value.sessionId === null || typeof value.sessionId === "string") && stringOrNullOrMissing(value.model);
case "session.exited":
return stringOrMissing(value.reason);
case "turn.started":
return true;
case "turn.retrying":
return (
typeof value.attempt === "number" &&
Number.isInteger(value.attempt) &&
value.attempt >= 1 &&
typeof value.delayMs === "number" &&
Number.isFinite(value.delayMs) &&
value.delayMs >= 0 &&
typeof value.reason === "string"
);
case "turn.completed":
return (
typeof value.ok === "boolean" &&
stringOrNullOrMissing(value.stopReason) &&
numberOrNullOrMissing(value.cost) &&
stringsOrMissing(value.denials) &&
(value.usage === undefined ||
(isRecord(value.usage) && typeof value.usage.input === "number" && typeof value.usage.output === "number"))
);
case "turn.wait_started":
return typeof value.resource === "string" && computerHolderOrMissing(value.holder);
case "cap.exhausted":
return typeof value.handoffPath === "string" && value.handoffPath.trim().length > 0 && value.reason === "cap";
case "turn.wait_ended":
return (
typeof value.resource === "string" &&
computerHolderOrMissing(value.holder) &&
typeof value.waitedMs === "number" &&
Number.isFinite(value.waitedMs) &&
value.waitedMs >= 0 &&
(value.outcome === "acquired" || value.outcome === "gave_up" || value.outcome === "parked" || value.outcome === "stopped")
);
case "item.started":
return (value.itemType === "tool" || value.itemType === "reasoning") && stringOrMissing(value.title);
case "item.updated":
return (value.itemType === "tool" || value.itemType === "reasoning") && numberOrNullOrMissing(value.tokens);
case "item.completed":
return value.itemType === "assistant_text" ? typeof value.text === "string" : value.itemType === "tool" && typeof value.ok === "boolean";
case "content.delta":
return (value.streamKind === "assistant_text" || value.streamKind === "reasoning_text") && typeof value.delta === "string";
case "request.opened":
return (
(value.requestType === "permission" || value.requestType === "question") &&
typeof value.tool === "string" &&
typeof value.summary === "string" &&
stringsOrMissing(value.choices) &&
askQuestionsOrMissing(value.questions) &&
(value.origin === undefined || value.origin === "tool" || value.origin === "output")
);
case "request.resolved":
return (
(value.behavior === "allow" || value.behavior === "deny" || value.behavior === "answer") &&
(value.source === "user" ||
value.source === "auto" ||
value.source === "timeout" ||
value.source === "system" ||
value.source === "unavailable" ||
value.source === "peer")
);
case "thread.token-usage.updated":
return typeof value.input === "number" && typeof value.output === "number";
case "runtime.notice":
return typeof value.message === "string";
case "context.compacted":
return true;
case "context.summarizing":
return typeof value.active === "boolean";
case "runtime.error":
return typeof value.message === "string" &&
(value.setup === undefined || typeof value.setup === "boolean") &&
(value.claudeUpdate === undefined || typeof value.claudeUpdate === "boolean");
default:
return false;
}
}
function isNativeRecord(value: unknown): value is NativeRecord {
return (
isRecord(value) &&
typeof value.at === "string" &&
(value.dir === "in" || value.dir === "out") &&
typeof value.source === "string" &&
Object.hasOwn(value, "msg")
);
}
export function readThreadEvents(input: {
eventsDir: string;
nativeDir: string;
threadId: string;
limit?: number;
}): InspectorPage {
const { eventsDir, nativeDir, threadId } = input;
assertThreadId(threadId);
const requested = input.limit ?? DEFAULT_LIMIT;
const limit = Number.isFinite(requested) ? Math.max(1, Math.min(Math.trunc(requested), MAX_LIMIT)) : DEFAULT_LIMIT;
const runtime = readRecentLines(join(eventsDir, `${threadId}.ndjson`), limit, isRuntimeEvent);
const native = readRecentLines(join(nativeDir, `${threadId}.ndjson`), limit, isNativeRecord);
const merged: InspectorEntry[] = [
...runtime.lines.map((data): InspectorEntry => ({
kind: "runtime",
at: data.createdAt,
data: data.type === "cap.exhausted" ? { ...data, handoffPath: resolve(data.handoffPath) } : data,
})),
...native.lines.map((data): InspectorEntry => ({ kind: "native", at: data.at, data })),
];
merged.sort((a, b) => (a.at < b.at ? -1 : a.at > b.at ? 1 : 0));
return {
entries: merged,
total: { runtime: runtime.total, native: native.total },
};
}