Skip to content

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

2.9 KB
Raw Blame
// MCP tool-result content, converted once for every adapter that renders a server's result to a
// model. A new engine adapter imports this instead of copying a per-driver switch, so a content
// kind added by the spec reaches every adapter at the same time.
//
// The mapping follows pi's own MCP extension (`@earendil-works/pi-mcp` `toLlmContent`): a model
// reads text and images, so the other kinds keep an explicit placeholder rather than being dropped
// silently — a `show_voice_note`-style tool whose audio would otherwise vanish leaves a line the
// model and the log can see. The raw block still rides in the tool result's `structuredContent` and
// `_meta`, which a host can use to attach the audio itself.

/** pi's model-facing content: text and base64 images. */
export type PiToolContent = { type: "text"; text: string } | { type: "image"; data: string; mimeType: string };

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

function oneContentBlock(block: unknown): PiToolContent {
  if (!isRecord(block)) return { type: "text", text: "[unsupported MCP content]" };
  const type = typeof block.type === "string" ? block.type : "";
  switch (type) {
    case "text":
      return { type: "text", text: String(block.text ?? "") };
    case "image":
      return typeof block.data === "string" && typeof block.mimeType === "string"
        ? { type: "image", data: block.data, mimeType: block.mimeType }
        : { type: "text", text: "[unsupported MCP content image]" };
    case "audio":
      // pi's model content has no audio; keep a placeholder so the block is visible, never silent.
      return { type: "text", text: `[audio ${String(block.mimeType ?? "unknown type")} omitted]` };
    case "resource_link":
      return { type: "text", text: `${String(block.name ?? "resource")}: ${String(block.uri ?? "")}` };
    case "resource": {
      const resource = isRecord(block.resource) ? block.resource : undefined;
      if (resource && typeof resource.text === "string") return { type: "text", text: resource.text };
      if (resource && typeof resource.blob === "string" && String(resource.mimeType ?? "").startsWith("image/")) {
        return { type: "image", data: resource.blob, mimeType: String(resource.mimeType) };
      }
      const uri = resource && typeof resource.uri === "string" ? resource.uri : "";
      const mime = resource && typeof resource.mimeType === "string" ? resource.mimeType : "unknown type";
      return { type: "text", text: `[binary resource ${uri} (${mime}) omitted]` };
    }
    default:
      return { type: "text", text: `[unsupported MCP content ${type || "unknown"}]` };
  }
}

/** The content blocks of an MCP `tools/call` result as pi's model content, one output per input. */
export function mcpContentToPiContent(content: unknown): PiToolContent[] {
  return (Array.isArray(content) ? content : []).map(oneContentBlock);
}