Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

2.3 KB
Raw Blame
import { hostScope, parseJournalEntry, type JournalCapability, type JournalScope } from "../saved-steps/journal.ts";
import type { JournalEntry } from "../saved-steps/driver-wire.ts";
import { COMPUTER_JOURNAL_PATH } from "../saved-steps/journal-tap.ts";
import type { ComputerPlace } from "../../shared/skill-burger/format.ts";
import { PASS, type RouteHandler } from "./table.ts";

/** The Skill Burger's own record of a turn's desktop calls (server/skill-burger/desk.ts). */
export interface DeskJournalSink {
  driverPlace(capability: JournalCapability, asked?: string | null): ComputerPlace | null;
  record(capability: JournalCapability, entry: JournalEntry, place: ComputerPlace): void;
}

export function createComputerJournalRoutes(deps: {
  capability: JournalCapability;
  readBody(): Promise<unknown>;
  record(scope: JournalScope, entry: JournalEntry): void;
  enabled(): boolean;
  desk?: DeskJournalSink;
}): RouteHandler {
  // Known open risk: the bearer reaches the engine with the bridge's environment, so an engine that reads it can add
  // entries. The gate-check count and the saveability rules bound what a forged entry can make a replay do; a Skill
  // Burger built from forged entries still needs the chat's own rows to match them call for call, and is shown before it
  // is saved.
  return async ({ res, url, path, method, json }) => {
    if (path !== COMPUTER_JOURNAL_PATH || method !== "POST") return PASS;
    const deskOnly = url.searchParams.get("for") === "desk";
    const scope = deskOnly ? null : hostScope(deps.capability);
    const place = deps.desk?.driverPlace(deps.capability, url.searchParams.get("place")) ?? null;
    if (!scope && !place) return json(res, 403, { error: "only a host computer turn journals" });
    const savedSteps = scope !== null && deps.enabled();
    if (!place && !savedSteps) return json(res, 200, { ok: true, recorded: false });
    const entry = parseJournalEntry(await deps.readBody());
    if (!entry) return savedSteps ? json(res, 400, { error: "malformed journal entry" }) : json(res, 200, { ok: true, recorded: false });
    if (place) deps.desk!.record(deps.capability, entry, place);
    if (!savedSteps) return json(res, 200, { ok: true, recorded: false });
    deps.record(scope, entry);
    return json(res, 200, { ok: true });
  };
}