Skip to content

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

10.3 KB
Raw Blame
import { APP_LIMITS, ROOM_VIEW_ONLY, type AppPayload } from "../../shared/app.ts";
import { APP_RPC_METHODS, PAGE_RPC_METHODS, pageMethodUse, pageUseRefusal, RPC_HOST_REFUSED, type AppRpcMethod, type PageRpcMethod } from "../../shared/app-frame/bridge.ts";
import { webOrigin } from "../../shared/app-frame/csp.ts";
import { appRow, createChatApps, rowRate, type ChatApps } from "../chat-apps.ts";
import type { ConnectorGate } from "../connector-gate.ts";
import type { DataEngine } from "../data/types.ts";
import { messageFileDisposition } from "../message-file.ts";
import type { RequestAuth } from "../request-auth.ts";
import type { BotRecord, Message, Store } from "../store.ts";
import { refusalBody, userError } from "../user-errors.ts";
import { pageRpc } from "./bot-page-rpc.ts";
import { PASS, type RouteHandler } from "./table.ts";

export { createChatApps };
export { botApproval, createChatAppsMcp } from "../chat-apps-mcp.ts";
export { createConnectorGate } from "../connector-gate.ts";
export { createConnectorGateApps } from "../host/connector-gate-host.ts";

export interface McpRpcRequest {
  auth: RequestAuth;
  bot: BotRecord;
  threadId: string;
  row: Message & { app: AppPayload };
  method: AppRpcMethod;
  params: Record<string, unknown>;
}

/** The JSON-RPC `result`, or {error: {code, message}, signIn?, blocked?}. */
export interface McpRpcAnswer {
  status: number;
  body: Record<string, unknown>;
}

export interface ChatAppRouteDeps {
  store: Pick<Store, "bot" | "botByThread" | "groupByThread" | "messagesFor" | "patchMessage" | "taskByThread">;
  apps: ChatApps;
  /** On a Cloud home, a guest changes only conversations it started. */
  threadRefusal?(auth: RequestAuth, threadId: string): string | null;
  /** Whether this person may see the conversation now: asked when its app is fetched and again for every file of a folder
   * page they load, so leaving a room, or losing sight of its bot, closes its pages to them. */
  threadVisible?(auth: RequestAuth, threadId: string): boolean;
  mcpRpc?(request: McpRpcRequest): Promise<McpRpcAnswer>;
  /** The bots' databases, which a bot's page that declared `data` reads. */
  data?: Pick<DataEngine, "unavailable" | "forBot">;
  /** True when the person switched this bot's Data tools off. */
  dataOff?(botId: string): boolean;
  /** The connector tools a bot's page declared, called with the viewer's own connections. */
  connectors?: {
    gate: ConnectorGate;
    /** Whose own connections the viewer's calls use, where each person connects their own; null elsewhere. */
    person(auth: RequestAuth): string | null;
    /** May this viewer have the bot act: a viewer who may only read its conversations may not. */
    mayAct(auth: RequestAuth, botId: string): boolean;
    /** Would a turn of this bot in this conversation run a change without asking. */
    fullAccess(botId: string, threadId: string): boolean;
    /** A conversation whose turns are confined (a guest's, a read-and-draft run's): its pages call no connected apps. */
    confined?(threadId: string): boolean;
  };
}

const DOCUMENT = /^\/api\/threads\/([\w-]+)\/messages\/([\w-]+)\/app(\/download)?$/;
const APP_ACTION = /^\/api\/bots\/([\w-]+)\/apps\/([\w-]+)\/(rpc|context)$/;

function isRecord(value: unknown): value is Record<string, unknown> {
  return Boolean(value) && typeof value === "object" && !Array.isArray(value);
}

export function createChatAppRoutes(deps: ChatAppRouteDeps): RouteHandler {
  const { store, apps } = deps;
  const contextRate = rowRate(120, 60_000);
  const pageRate = rowRate(APP_LIMITS.rpcPerMinute, 60_000);

  return async ({ req, res, url, path, method, auth, json, readBody }) => {
    let m = DOCUMENT.exec(path);
    if (m && method === "GET") {
      const threadId = m[1]!;
      // An unknown thread answers before the store materialises it.
      if (!store.botByThread(threadId) && !store.groupByThread(threadId)) return json(res, 404, { error: "no such conversation" });
      const visible = deps.threadVisible;
      if (visible && !visible(auth, threadId)) return json(res, 404, { error: "no such conversation" });
      if (!appRow(store, threadId, m[2]!)) return json(res, 404, { error: "no app on that message" });
      const asked = url.searchParams.get("rev");
      if (asked !== null && !/^\d{1,6}$/.test(asked)) return json(res, 400, { error: "rev must be a version number" });
      if (m[3]) {
        const page = apps.publishablePage(threadId, m[2]!, asked === null ? undefined : Number(asked));
        if (!page.ok) return json(res, page.status, refusalBody(page));
        if (page.files.length > 1) return json(res, 409, userError("serverError.page.downloadHasFiles"));
        const body = page.files[0]!.data;
        // Security: a bot's page is untrusted HTML; it leaves only as opaque bytes to save, never to render.
        res.writeHead(200, {
          "content-type": "application/octet-stream",
          "content-length": String(body.byteLength),
          "content-disposition": messageFileDisposition(`${page.title || "page"}.html`),
          "content-security-policy": "sandbox",
          "x-content-type-options": "nosniff",
          "cache-control": "private, no-store",
        });
        res.end(body);
        return;
      }
      // A folder page's files load from wherever this client reaches the app; it names that origin itself.
      const origin = webOrigin(url.searchParams.get("origin")) ?? webOrigin(url.origin) ?? undefined;
      const viewer = { key: auth.kind === "session" ? `session:${auth.session.id}` : auth.kind, allowed: () => !visible || visible(auth, threadId) };
      const doc = apps.appDocument(threadId, m[2]!, asked === null ? undefined : Number(asked), origin, viewer);
      if (!doc) return json(res, 404, userError(asked === null ? "serverError.page.notStored" : "serverError.page.versionGone"));
      // `can` and `blocked` change with sign-in and settings: never cached.
      res.setHeader("cache-control", "private, no-store");
      return json(res, 200, doc);
    }

    m = APP_ACTION.exec(path);
    if (!m || method !== "POST") return PASS;
    const [, botId, messageId, action] = m as unknown as [string, string, string, "rpc" | "context"];
    const body = await readBody(req, action === "rpc" ? APP_LIMITS.rpcArgumentsBytes + 16_384 : 64_000);
    if (!isRecord(body) || typeof body.threadId !== "string" || !/^[\w-]+$/.test(body.threadId)) {
      return json(res, 400, { error: "threadId is required" });
    }
    const threadId = body.threadId;
    const bot = store.bot(botId);
    if (!bot) return json(res, 404, { error: "no such bot" });
    // A bot page's own data and state calls; an MCP app's row never makes one.
    const pageCall = (row: { app: AppPayload } | undefined) => action === "rpc" && row?.app.source === "html" && typeof body.method === "string" &&
      (PAGE_RPC_METHODS as readonly string[]).includes(body.method);
    if (store.groupByThread(threadId)) {
      return json(res, 409, pageCall(appRow(store, threadId, messageId))
        ? { error: { code: RPC_HOST_REFUSED, message: pageUseRefusal(pageMethodUse(body.method as PageRpcMethod), true) } } : { error: ROOM_VIEW_ONLY });
    }
    if (!store.taskByThread(bot.id, threadId)) return json(res, 404, { error: "no such conversation" });
    const refused = deps.threadRefusal?.(auth, threadId);
    if (refused) return json(res, 403, { error: refused });
    const row = appRow(store, threadId, messageId);
    if (!row || (row.from && row.from.botId !== bot.id)) return json(res, 404, { error: "That app is not in this conversation." });
    const pageMethod = pageCall(row);
    // A page's data and state are a read of the conversation: only for whoever may see it.
    if (pageMethod && deps.threadVisible && !deps.threadVisible(auth, threadId)) return json(res, 404, { error: "no such conversation" });

    if (action === "context") {
      if (!contextRate.take(`${threadId}:${row.id}`)) return json(res, 429, { error: "Too many context updates from this app." });
      const result = apps.setAppContext({ threadId, messageId: row.id, content: body.content, structuredContent: body.structuredContent });
      return result.ok ? json(res, 200, { ok: true }) : json(res, result.status, { error: result.error });
    }

    if (pageMethod) {
      if (body.params !== undefined && !isRecord(body.params)) return json(res, 400, { error: "params must be an object" });
      const connectorCall = pageMethodUse(body.method as PageRpcMethod) === "connectors";
      const linked = deps.connectors;
      if (connectorCall && linked && !linked.mayAct(auth, bot.id)) {
        return json(res, 403, { error: { code: RPC_HOST_REFUSED, message: `Only someone who can chat with ${bot.name} can use this page's connected apps.` } });
      }
      if (connectorCall && linked?.confined?.(threadId)) {
        return json(res, 403, { error: { code: RPC_HOST_REFUSED, message: "In this conversation, pages cannot use connected apps." } });
      }
      if (!pageRate.take(`${threadId}:${row.id}`)) return json(res, 429, { error: { code: -32000, message: "Too many calls from this page. Wait a moment." } });
      const answer = await pageRpc({
        apps, ...(deps.data ? { data: deps.data } : {}), ...(deps.dataOff ? { dataOff: deps.dataOff } : {}), ...(linked ? { connectors: linked.gate } : {}),
      }, {
        botId: bot.id, threadId, messageId: row.id, method: body.method as PageRpcMethod, params: isRecord(body.params) ? body.params : {},
        ...(connectorCall && linked ? { viewer: { person: linked.person(auth), fullAccess: () => linked.fullAccess(bot.id, threadId) } } : {}),
      });
      return json(res, answer.status, answer.body);
    }
    if (row.app.source !== "mcp") return json(res, 404, { error: "That app has no server to call." });
    if (typeof body.method !== "string" || !(APP_RPC_METHODS as readonly string[]).includes(body.method)) {
      return json(res, 400, { error: `method must be one of ${APP_RPC_METHODS.join(", ")}` });
    }
    if (body.params !== undefined && !isRecord(body.params)) return json(res, 400, { error: "params must be an object" });
    if (!deps.mcpRpc) return json(res, 404, { error: "This app cannot reach its server on this computer yet." });
    const answer = await deps.mcpRpc({
      auth, bot, threadId, row,
      method: body.method as McpRpcRequest["method"],
      params: isRecord(body.params) ? body.params : {},
    });
    return json(res, answer.status, answer.body);
  };
}