Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
import type { Notification, NotifyKind } from "../shared/notification.ts";
import { personMentionExcerpt } from "../shared/person-mention.ts";
import { plainPreviewText } from "../shared/plain-preview.ts";
import { conversationLanguage, serverLine, type ServerLineKey } from "./server-lines.ts";
export type { Notification, NotifyKind } from "../shared/notification.ts";
const graphemes = new Intl.Segmenter(undefined, { granularity: "grapheme" });
/** Fenced code stays out of a banner, as before; the rest of the Markdown becomes the readable line a lock screen shows. */
export function summarize(text: string, max = 140): string {
const line = plainPreviewText(text.replace(/```[\s\S]*?```/g, " "));
if (line.length <= max) return line;
const cutoff = max - 1;
const boundary = graphemes.segment(line).containing(cutoff)?.index ?? cutoff;
return `${line.slice(0, boundary).trimEnd()}…`;
}
export interface NotifyBot {
id: string;
name: string;
threadId: string;
notifications?: boolean;
}
export function buildSpendNotification(
bot: Pick<NotifyBot, "id" | "name">,
threadId: string,
text: { title: string; body: string },
): Notification {
return { kind: "spend", botId: bot.id, botName: bot.name, threadId, title: text.title, body: summarize(text.body, 200) };
}
/** Something OpenMausBot did on its own about `bot` that the person hears once, in one plain line. */
export function buildNoticeNotification(bot: Pick<NotifyBot, "id" | "name">, threadId: string, text: string): Notification {
return { kind: "notice", botId: bot.id, botName: bot.name, threadId, title: bot.name, body: summarize(text, 240) };
}
const TITLE: Partial<Record<NotifyKind, ServerLineKey>> = {
approval: "server.notify.approval",
question: "server.notify.question",
"routine-failed": "server.notify.routineFailed",
"routine-deferred": "server.notify.routineDeferred",
"turn-failed": "server.notify.turnFailed",
incident: "server.notify.incident",
"delegation-settled": "server.notify.delegationSettled",
mention: "server.notify.mention",
wait: "server.notify.wait",
};
export function buildNotification(
kind: NotifyKind,
bot: Omit<NotifyBot, "threadId"> & { threadId?: string },
threadId: string,
detail: string,
extra?: { avatarUrl?: string; group?: { id: string; name: string }; site?: string; label?: string; app?: string; handoff?: boolean; routine?: boolean },
): Notification | null {
if (bot.notifications === false) return null;
const body = summarize(detail);
const language = conversationLanguage(threadId);
const who = extra?.group ? serverLine(language, "server.notify.inRoom", { bot: bot.name, room: extra.group.name }) : bot.name;
const title = kind === "secret" ? serverLine(language, "server.notify.secret", { who, label: extra?.label ?? "" })
: kind === "sign-in"
? extra?.handoff ? serverLine(language, "server.notify.handoff", { who })
: extra?.app ? serverLine(language, "server.notify.connect", { who, app: extra.app })
: extra?.site ? serverLine(language, "server.notify.signIn", { who, site: extra.site }) : serverLine(language, "server.notify.signInWebsite", { who })
: serverLine(language, TITLE[kind] ?? "server.notify.done", { who });
if ((kind === "done" || kind === "mention") && !body) return null;
const notification: Notification = { kind, botId: bot.id, botName: bot.name, threadId, title, body };
if (extra?.avatarUrl) notification.avatarUrl = extra.avatarUrl;
if (extra?.group) notification.groupId = extra.group.id;
if (extra?.routine && kind === "done") notification.routine = true;
return notification;
}
export function unaskedBotThread(
task: { openedBy?: unknown } | undefined,
path: readonly { role: string; kind: string; at: number; peerAsk?: { incident?: unknown } }[],
assignedAt?: number,
): boolean {
if (!task?.openedBy) return false;
const asked = path.findLast((message) => message.role === "user" && message.kind === "text");
if (!asked || (assignedAt !== undefined && asked.at < assignedAt)) return true;
return Boolean(asked.peerAsk && !asked.peerAsk.incident);
}
type MentionSource = {
activePath(threadId: string): readonly { role: string; kind: string; text?: string; turnId?: string; from?: { botId: string } }[];
bot(id: string): { name: string; hidden?: boolean } | null | undefined;
};
export function buildMentionNotification(
source: MentionSource,
bot: Omit<NotifyBot, "threadId"> & { avatarUrl?: string },
room: { id: string; name: string; dm?: boolean; memberIds: readonly string[] },
turn: { threadId: string; turnId?: string; reply: string; profileName?: string },
): Notification | null {
if (room.dm) return null;
const texts = turn.turnId
? source.activePath(turn.threadId).filter((m) => m.role === "bot" && m.kind === "text" && m.turnId === turn.turnId && m.from?.botId === bot.id).map((m) => m.text ?? "")
: [turn.reply];
const botNames = room.memberIds.flatMap((id) => {
const member = source.bot(id);
return member && !member.hidden ? [member.name] : [];
});
for (const text of texts) {
const excerpt = personMentionExcerpt(text, { profileName: turn.profileName, botNames });
if (excerpt) return buildNotification("mention", { ...bot, threadId: turn.threadId }, turn.threadId, excerpt, { avatarUrl: bot.avatarUrl, group: room });
}
return null;
}