Skip to content

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

1.5 KB
Raw Blame
import { extractMcpImages, isOwnScreenToolName } from "../../mcp-tool-images.ts";
import { toolDetailPreview } from "../../tool-summary.ts";

export type AcpToolEndEvent = { type: "item.completed" } & (
  | { itemType: "tool"; itemId: string | undefined; ok: boolean; output?: string }
  | { itemType: "assistant_image"; data: string; fromTool: true }
);

/**
 * The events that end a finished tool item: its completion, then any images its result carries. An item already in
 * `ended` gets none, and an item that ends here joins it: ACP v1 lets an update name a tool call that already finished.
 * OpenMausBot's own computer/browser calls (named by this update or listed in `own.ids` when they started) add no images.
 */
export function acpToolEndEvents(
  update: { status?: unknown; rawOutput?: unknown; content?: unknown; title?: unknown },
  itemId: string | undefined,
  ended: Set<string>,
  own: { ids: Set<string>; servers: ReadonlySet<string> },
): AcpToolEndEvent[] {
  if (itemId !== undefined) {
    if (ended.has(itemId)) return [];
    ended.add(itemId);
  }
  return [
    { type: "item.completed", itemType: "tool", itemId, ok: update.status !== "failed", output: toolDetailPreview(update.rawOutput ?? update.content) },
    ...((itemId !== undefined && own.ids.delete(itemId)) || isOwnScreenToolName(update.title, own.servers) ? [] : extractMcpImages(update.content ?? update.rawOutput)).map((img): AcpToolEndEvent => ({ type: "item.completed", itemType: "assistant_image", data: img.data, fromTool: true })),
  ];
}