Skip to content

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

12.3 KB
Raw Blame
// One tool-free ACP exchange for memory upkeep: a fresh process and session, no MCP servers, an empty
// folder. It fails closed: any agent request, or any tool call it reports, ends the call.
import type { ModelCatalog, TextGenerationOptions, TextGenerationUsage } from "../../contracts.ts";
import type { BoxSpawnOptions, killCliTree, spawnCli } from "../../procs.ts";
import { acpModelOption } from "./config-options.ts";
import type { AcpSupport } from "./core.ts";

export const BACKGROUND_TEXT_TIMEOUT_MS = 120_000;
export const BACKGROUND_TEXT_MAX_CHARS = 200_000;
const MAX_STDOUT_BYTES = 4_000_000;

/** Resolves a one-shot turn model for drivers that accept it outside `selectModel` (for example `-m` and `session/set_model`). */
export function oneShotTurnModel(
  support: Pick<AcpSupport, "oneShotModel" | "selectModel">,
  env: Record<string, string | undefined>,
  options: TextGenerationOptions,
  models: ModelCatalog,
): string | undefined {
  const requested = options.model ?? options.fallback?.model;
  if (!support.oneShotModel || support.selectModel || !requested) return undefined;
  try {
    return support.oneShotModel(requested, env, models) || undefined;
  } catch {
    return undefined;
  }
}

/** Sets a one-shot's env for the bot's own model, not the engine's default (a local model server may hold only the
 * model its chats run on), and returns that model for the session to pick. */
export function applyOneShotEnv(
  support: Pick<AcpSupport, "selectModel" | "resolveTurnModel" | "applyTurnEnv">,
  env: Record<string, string | undefined>,
  options: TextGenerationOptions,
  models: ModelCatalog,
  ctx: { fullAuto: false; cwd: string; threadId: string; mcpServers: []; model?: string },
): AcpOneShotInput["model"] {
  const selection = support.selectModel;
  const requestedModel = selection || ctx.model ? options.model ?? options.fallback?.model : undefined;
  const model = selection
    ? (requestedModel && requestedModel !== selection.agentDefault
      ? resolveOneShotModel(support, requestedModel, env, models)
      : undefined)
    : ctx.model;
  support.applyTurnEnv?.(env, { ...ctx, ...(model ? { model, requestedModel } : {}) });
  return model && selection ? { configId: selection.configId, id: model } : undefined;
}

/** A model the engine can no longer route (e.g. a deleted Qwen provider entry) leaves the session's default to run, as an
 * unoffered one does. */
function resolveOneShotModel(
  support: Pick<AcpSupport, "resolveTurnModel">,
  requestedModel: string,
  env: Record<string, string | undefined>,
  models: ModelCatalog,
): string | undefined {
  try {
    return support.resolveTurnModel?.(requestedModel, env, models) ?? requestedModel;
  } catch {
    return undefined;
  }
}

export interface AcpOneShotInput {
  displayName: string;
  spawn: typeof spawnCli;
  kill: typeof killCliTree;
  /** Security: while the bot box is on, the engine runs in it, as a bot's turn does, so nothing it loads from a HOME a
   * bot can write runs outside the box. */
  box?: BoxSpawnOptions["box"];
  command: string;
  argv: string[];
  env: Record<string, string | undefined>;
  cwd: string;
  prompt: string;
  /** The model usage is booked to when the session names none. */
  defaultModel: string;
  /** Picked through this session config option when the session offers it; otherwise the session's own model runs. */
  model?: { configId: string; id: string } | undefined;
  /** Model applied outside `selectModel` (e.g. via argv or `configure`), used for usage attribution once configured. */
  configuredModel?: string;
  pickAuthMethod: (methods: Array<{ id?: string }>) => string | null;
  authMeta?: Record<string, unknown>;
  authRequired: boolean | "if-requested";
  loginNote: string;
  /** Whether a session/new refusal asks for a sign-in; it then fails with `loginNote`. */
  signInRefused?: (error: unknown) => boolean;
  clientCapabilitiesMeta?: Record<string, unknown>;
  configure?: (ctx: { request: (method: string, params: unknown, timeoutMs?: number) => Promise<any>; sessionId: string; session: any }) => Promise<void>;
  options?: TextGenerationOptions;
  timeoutMs?: number;
}

const REFUSAL = "OpenMausBot background text allows no tools, files, terminals or approvals.";

export function runAcpOneShot(input: AcpOneShotInput): Promise<string> {
  const { signal, onUsage } = input.options ?? {};
  const name = input.displayName;
  if (signal?.aborted) return Promise.reject(new Error(`${name} background call aborted`));
  return new Promise<string>((resolve, reject) => {
    const child = input.spawn(input.command, input.argv, { cwd: input.cwd, env: input.env, stdio: ["pipe", "pipe", "pipe"], botWork: true, ...(input.box ? { box: input.box } : {}) });
    let settled = false;
    let buffer = "";
    let received = 0;
    let output = "";
    let sessionId: string | null = null;
    let model: string | undefined;
    let usage: TextGenerationUsage | undefined;
    let nextId = 1;
    const pending = new Map<number, { resolve: (value: any) => void; reject: (error: Error) => void }>();

    const finish = async (error?: Error) => {
      if (settled) return;
      settled = true;
      clearTimeout(timer);
      signal?.removeEventListener("abort", abort);
      for (const waiter of pending.values()) waiter.reject(error ?? new Error(`${name} background call closed`));
      pending.clear();
      // Windows keeps the session folder locked until the process exits.
      await input.kill(child).catch(() => undefined);
      if (!error && usage) {
        try {
          onUsage?.(usage);
        } catch {
          error = new Error(`${name} usage callback failed`);
        }
      }
      if (error) reject(error);
      else if (!output.trim()) reject(new Error(`${name} background call returned no text`));
      else resolve(output.trim());
    };
    const abort = () => finish(new Error(`${name} background call aborted`));
    const timer = setTimeout(() => finish(new Error(`${name} background call timed out`)), input.timeoutMs ?? input.options?.timeoutMs ?? BACKGROUND_TEXT_TIMEOUT_MS);
    timer.unref?.();

    const send = (message: unknown) => {
      if (settled) return;
      try {
        child.stdin.write(`${JSON.stringify(message)}\n`);
      } catch {}
    };
    const request = (method: string, params: unknown): Promise<any> => new Promise((ok, fail) => {
      if (settled) return fail(new Error(`${name} background call closed`));
      const id = nextId++;
      pending.set(id, { resolve: ok, reject: fail });
      send({ jsonrpc: "2.0", id, method, params });
    });

    const onMessage = (msg: any) => {
      if (msg?.id !== undefined && msg.method === undefined) {
        const waiter = pending.get(msg.id);
        pending.delete(msg.id);
        if (!waiter) return;
        if (msg.error) waiter.reject(Object.assign(new Error(`${name} background ${typeof msg.error?.message === "string" ? msg.error.message.slice(0, 200) : "request failed"}`), { code: msg.error?.code, data: msg.error?.data }));
        else waiter.resolve(msg.result);
        return;
      }
      if (msg?.id !== undefined && typeof msg.method === "string") {
        send(msg.method === "session/request_permission"
          ? { jsonrpc: "2.0", id: msg.id, result: { outcome: { outcome: "cancelled" } } }
          : { jsonrpc: "2.0", id: msg.id, error: { code: -32601, message: REFUSAL } });
        finish(new Error(`${name} background call asked for ${msg.method}, which it may not use`));
        return;
      }
      if (msg?.method !== "session/update") return;
      if (sessionId !== null && msg.params?.sessionId !== undefined && msg.params.sessionId !== sessionId) return;
      const update = msg.params?.update;
      const kind = update?.sessionUpdate;
      if (kind === "tool_call" || kind === "tool_call_update") {
        finish(new Error(`${name} background call tried to use a tool`));
        return;
      }
      if (kind === "agent_message_chunk") {
        const text = update?.content?.text;
        if (typeof text !== "string") return;
        output += text;
        if (output.length > BACKGROUND_TEXT_MAX_CHARS) finish(new Error(`${name} background answer exceeded its limit`));
      }
    };

    child.stdout.setEncoding("utf8");
    child.stderr.resume();
    child.on("error", (error) => finish(error instanceof Error ? error : new Error(String(error))));
    child.on("close", (code) => finish(new Error(`${name} background process exited ${code ?? "on a signal"}`)));
    child.stdout.on("data", (chunk: string) => {
      if (settled) return;
      received += chunk.length;
      if (received > MAX_STDOUT_BYTES) return finish(new Error(`${name} background output exceeded its limit`));
      buffer += chunk;
      let newline: number;
      while (!settled && (newline = buffer.indexOf("\n")) !== -1) {
        const line = buffer.slice(0, newline);
        buffer = buffer.slice(newline + 1);
        if (!line.trim()) continue;
        let msg: unknown;
        try {
          msg = JSON.parse(line);
        } catch {
          // agents print banners and logs on stdout; only JSON lines count
          continue;
        }
        onMessage(msg);
      }
    });
    signal?.addEventListener("abort", abort, { once: true });

    (async () => {
      const init = await request("initialize", {
        protocolVersion: 1,
        clientInfo: { name: "openmausbot_memory", version: "1" },
        clientCapabilities: {
          fs: { readTextFile: false, writeTextFile: false },
          terminal: false,
          ...(input.clientCapabilitiesMeta ? { _meta: input.clientCapabilitiesMeta } : {}),
        },
      });
      const methods: Array<{ id?: string }> = Array.isArray(init?.authMethods) ? init.authMethods : [];
      const methodId = input.pickAuthMethod(methods);
      if (methodId) {
        try {
          await request("authenticate", { methodId, ...(input.authMeta ? { _meta: input.authMeta } : {}) });
        } catch {
          if (input.authRequired) throw new Error(input.loginNote);
        }
      } else if (input.authRequired === true) {
        throw new Error(input.loginNote);
      }
      const session = await request("session/new", { cwd: input.cwd, mcpServers: [] }).catch((error: unknown) => { throw input.signInRefused?.(error) ? new Error(input.loginNote) : error; });
      if (typeof session?.sessionId !== "string") throw new Error(`${name} background session/new returned no sessionId`);
      sessionId = session.sessionId;
      model = typeof session?.models?.currentModelId === "string" ? session.models.currentModelId
        : typeof init?._meta?.modelState?.currentModelId === "string" ? init._meta.modelState.currentModelId : undefined;
      const wanted = input.model;
      const offered = wanted ? acpModelOption(session, wanted.configId) : undefined;
      if (wanted && offered?.configId && offered.options.some(({ id }) => id === wanted.id)) {
        if (offered.currentValue !== wanted.id) {
          const reply = await request("session/set_config_option", { sessionId, configId: offered.configId, value: wanted.id });
          // Some engines confirm a switch with an empty reply; one that names another model did not switch.
          const now = acpModelOption(reply, offered.configId)?.currentValue;
          if (now !== undefined && now !== wanted.id) throw new Error(`${name} background call did not switch to ${wanted.id} (still ${now})`);
        }
        model = wanted.id;
      }
      await input.configure?.({ request: (method, params) => request(method, params), sessionId: session.sessionId, session });
      if (!input.model && input.configuredModel) model = input.configuredModel;
      const result = await request("session/prompt", { sessionId, prompt: [{ type: "text", text: input.prompt }] });
      const reported = result?.usage ?? result?._meta ?? {};
      const count = (value: unknown) => (typeof value === "number" && Number.isFinite(value) && value >= 0 ? value : undefined);
      if (count(reported.inputTokens) !== undefined || count(reported.outputTokens) !== undefined) {
        usage = { model: model ?? input.defaultModel, input: count(reported.inputTokens), output: count(reported.outputTokens) };
      }
      if (result?.stopReason !== "end_turn") throw new Error(`${name} background call stopped: ${String(result?.stopReason ?? "unknown")}`);
      finish();
    })().catch((error: unknown) => finish(error instanceof Error ? error : new Error(String(error))));
  });
}