Skip to content

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

3.1 KB
Raw Blame
import type { GroupTask as GroupTaskRecord } from "../shared/wire.ts";
import { newId } from "./contracts.ts";
import * as mdb from "./message-db.ts";
import type { BotRecord, Message, TaskRecord } from "./store.ts";
import { buildTask } from "./task-record.ts";
import { titleFromMessage } from "./thread-title.ts";
import type { ApprovalMode } from "../shared/approval-mode.ts";

function forkHistory(path: Message[], sourceMessageId: string | undefined) {
  const anchor = path.findLastIndex((message) => message.kind === "text" && (message.role === "user" || message.role === "bot")
    && (sourceMessageId === undefined || message.id === sourceMessageId));
  if (anchor < 0) return null;
  return { history: structuredClone(path.slice(0, anchor + 1)), messageId: path[anchor]!.id };
}

function registerImportedFork<T extends { threadId: string }>(task: T, history: Message[], messageId: string,
  register: (task: T) => void): void {
  mdb.importThread(task.threadId, history, messageId);
  try {
    register(task);
  } catch (error) {
    try { mdb.deleteThread(task.threadId); } catch (cleanupError) {
      throw new AggregateError([error, cleanupError], "could not roll back the imported fork");
    }
    throw error;
  }
}

export function createGroupFork(source: GroupTaskRecord, path: Message[], sourceMessageId: string | undefined,
  register: (task: GroupTaskRecord) => void): GroupTaskRecord | null {
  const fork = forkHistory(path, sourceMessageId);
  if (!fork) return null;
  const { history, messageId } = fork;
  const createdAt = Date.now();
  const task: GroupTaskRecord = {
    threadId: newId(),
    title: titleFromMessage(history.at(-1)?.text ?? ""),
    createdAt,
    updatedAt: createdAt,
    origin: { threadId: source.threadId, messageId },
    lastReadMessageId: messageId,
    ...(source.pinnedCwd !== undefined ? { pinnedCwd: source.pinnedCwd } : {}),
  };
  registerImportedFork(task, history, messageId, register);
  return task;
}

export function createBotFork(bot: BotRecord, source: TaskRecord, path: Message[], sourceMessageId: string | undefined,
  approval: { mode: ApprovalMode; alwaysAllow: string[] },
  register: (task: TaskRecord) => void): TaskRecord | null {
  const fork = forkHistory(path, sourceMessageId);
  if (!fork) return null;
  const { history, messageId } = fork;
  const task = buildTask(bot, titleFromMessage(history.at(-1)?.text ?? ""), source.projectId, undefined,
    undefined, source.modelSelection, source.cwd);
  // A fork starts with the source's effective permissions, including an Ask mask for an unconfirmed grant.
  task.approvalMode = approval.mode;
  task.autoApprove = approval.mode === "auto";
  task.alwaysAllow = structuredClone(approval.alwaysAllow);
  task.outside = source.outside;
  task.botApi = source.botApi;
  if (source.botApiAsOwner) task.botApiAsOwner = true;
  if (source.surface && source.surfaceSource !== "auto") {
    task.surface = source.surface;
    if (source.surfaceSource) task.surfaceSource = source.surfaceSource;
  }
  task.origin = { threadId: source.threadId, messageId };
  task.lastReadMessageId = messageId;
  registerImportedFork(task, history, messageId, register);
  return task;
}