Skip to content

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

2.7 KB
Raw Blame
import { providerMcpTool } from "./provider-tool-allow.ts";

export interface McpImageContent {
  data: string;
  mimeType: string;
}

const asMcpImage = (c: unknown): McpImageContent | null =>
  !!c &&
  typeof c === "object" &&
  (c as { type?: unknown }).type === "image" &&
  typeof (c as McpImageContent).data === "string" &&
  typeof (c as McpImageContent).mimeType === "string"
    ? (c as McpImageContent)
    : null;

// Claude CLI hands MCP images over in Anthropic Messages shape ({type:"image", source:{type:"base64", media_type, data}});
// codex/acp pass the MCP-native {type, data, mimeType} through.
const asAnthropicImage = (c: unknown): McpImageContent | null => {
  const source = (c as { type?: unknown; source?: unknown } | null)?.type === "image"
    ? (c as { source?: unknown }).source
    : null;
  if (!source || typeof source !== "object") return null;
  const { type, media_type, data } = source as { type?: unknown; media_type?: unknown; data?: unknown };
  return type === "base64" && typeof media_type === "string" && typeof data === "string"
    ? { data, mimeType: media_type }
    : null;
};

const asImageBlock = (c: unknown): McpImageContent | null => asMcpImage(c) ?? asAnthropicImage(c);

export function extractMcpImages(value: unknown): McpImageContent[] {
  const arr = Array.isArray(value)
    ? value
    : Array.isArray((value as { content?: unknown } | null)?.content)
      ? (value as { content: unknown[] }).content
      : null;
  return arr ? arr.map(asImageBlock).filter((v): v is McpImageContent => v !== null) : [];
}

const OWN_SCREEN_SERVERS: ReadonlySet<string> = new Set(["computer", "browser", "agents"]);

/** The servers among a turn's mounts that OpenMausBot gave its own computer and browser tools. */
export function ownScreenMounts(mounted: Iterable<string>): Set<string> {
  return new Set([...mounted].filter((name) => OWN_SCREEN_SERVERS.has(name)));
}

/**
 * OpenMausBot's own computer and browser tools, mounted by OpenMausBot for this turn (`own`). The bot reads their
 * screenshots; the chat gets none of them (the bot sends one with attach_file when asked). A same-named server from
 * the engine's own config or the project folder keeps its images.
 */
export function isOwnScreenTool(server: unknown, tool: unknown, own: ReadonlySet<string>): boolean {
  return typeof server === "string" && own.has(server) && (server !== "agents" || tool === "shared_computer");
}

/** The same check for a namespaced tool name, `mcp__<server>__<tool>`. */
export function isOwnScreenToolName(name: unknown, own: ReadonlySet<string>): boolean {
  const parsed = typeof name === "string" ? providerMcpTool(name) : null;
  return !!parsed && isOwnScreenTool(parsed.server, parsed.tool, own);
}