Skip to content

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

1.8 KB
Raw Blame
// "@Chief how much did we spend today?" in Wes's one-to-one chat: Wes asks Chief (coordinate_bots consult), and Chief's
// answer shows in that chat as Chief's own reply, under Chief's name, instead of a folded teammate row Wes must repeat.
import type { HandoffView } from "./coordinator.ts";
import type { WireBot } from "../shared/wire.ts";
import type { Message } from "./store.ts";
import { redactSecretsInText } from "./redact.ts";

/** The reply row a teammate's finished consult leaves in the person's chat when the person tagged that teammate there,
 * or null when it is any other hand-off: rooms, work, notices, nested asks, failures and untagged teammates keep their
 * teammate row. */
export function taggedAnswerRow(
  child: Pick<HandoffView, "id" | "botId" | "mode" | "status" | "result">,
  parent: Pick<HandoffView, "botId" | "groupId" | "parentId">,
  bot: Pick<WireBot, "id" | "name" | "color"> | null | undefined,
  tagged: (botId: string) => boolean,
  requestMessageId?: string,
): Omit<Message, "id" | "at"> | null {
  if (!bot || parent.groupId || parent.parentId || child.botId === parent.botId || child.mode !== "consult" || child.status !== "completed") return null;
  const text = tagged(child.botId) ? redactSecretsInText(child.result.trim()) : "";
  return text ? { role: "bot", kind: "text", text, from: { botId: bot.id, name: bot.name, color: bot.color }, roomRequest: { id: child.id, phase: "result" },
    ...(requestMessageId ? { requestMessageId } : {}) } : null;
}

/** The line the chat's own bot reads when it is told the people it tagged: their answer is already in the chat. */
export const TAGGED_ANSWER_PROMPT = " When you ask a tagged teammate a question with consult, its answer shows in this chat under its own name: do not repeat it; add only what it left out, or nothing.";