Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
import { useRef, useState, type RefObject } from "react";
import { t } from "@/lib/i18n";
import { canCompactConversation } from "@/lib/context-warning";
import { newThreadPrompt, threadCommandFromComposer, type ComposerSlashCommand } from "@/lib/composer-commands";
import { visibleMessages, type Action, type Bot } from "@/state/store";
import { announceArchiveNotice } from "./bot-archive";
import { ConfirmDialog } from "./ConfirmDialog";
import { useContextAction } from "./ContextWarning";
export function conversationCommands(started: boolean): ComposerSlashCommand[] {
return [
...(started ? [{ kind: "command" as const, id: "new" as const, label: "/new" as const, description: t("composer.command.newDesc") }] : []),
{ kind: "command", id: "archive", label: "/archive", description: t("composer.command.archiveDesc") },
{ kind: "command", id: "delete", label: "/delete", description: t("composer.command.deleteDesc") },
{ kind: "command", id: "compact", label: "/compact", description: t("contextMenu.compact") },
{ kind: "command", id: "context", label: "/context", description: t("contextMenu.title") },
];
}
export function useComposerThreadCommand({ text, bot, threadId, mainThreadId, threads, title, busy, dispatch, returnFocusRef, clearText, onCleared, threadActions, started }: {
text: string;
bot: Bot | undefined;
threadId: string;
/** Archive, delete, compact, and context need a real conversation. `/new` does not. */
threadActions: boolean;
/** False on a blank thread, where `/new` is hidden and a bare `/new` does nothing. */
started: boolean;
/** The conversation the person set as main, opened after this one leaves the list when it is not archived. */
mainThreadId: string | undefined;
/** The bot's conversations, for the next active one when there is no main to open. */
threads: ReadonlyArray<{ threadId: string; updatedAt?: number; createdAt?: number; archivedAt?: number; routineRunId?: string }> | undefined;
title: string | undefined;
busy: boolean;
dispatch: (action: Action) => void;
returnFocusRef?: RefObject<HTMLTextAreaElement | null>;
clearText: () => void;
onCleared: () => void;
}) {
const botId = bot?.id;
const parsed = threadCommandFromComposer(text);
const newPrompt = parsed === "new" ? newThreadPrompt(text) : "";
// A room has no bot. A bare /new there clears the box. /new plus text stays a normal room message.
const command = parsed !== "new" ? (botId && threadActions ? parsed : null) : (newPrompt && !botId ? null : "new");
const freshPrompt = command === "new" ? newPrompt : "";
const [confirming, setConfirming] = useState(false);
const contextMenu = useRef<{ open: () => void }>(null);
const compact = useContextAction();
const nextThread = () => {
if (!botId) return;
const rest = (threads ?? []).filter((thread) => thread.threadId !== threadId && thread.archivedAt === undefined && thread.routineRunId === undefined);
const target = mainThreadId && mainThreadId !== threadId && rest.some((thread) => thread.threadId === mainThreadId)
? mainThreadId
: rest.sort((a, b) => (b.updatedAt ?? b.createdAt ?? 0) - (a.updatedAt ?? a.createdAt ?? 0))[0]?.threadId;
if (target) dispatch({ type: "switchTask", botId, threadId: target });
};
const run = () => {
if (command === "new") {
onCleared();
if (botId && started) dispatch({ type: "newTask", botId });
return;
}
if (!command || !bot || !botId) return;
if (command === "context") {
clearText();
contextMenu.current?.open();
return;
}
if (command === "compact") {
if (compact.pending) return;
if (busy) {
dispatch({ type: "error", message: t("contextWarning.busy") });
return;
}
clearText();
if (canCompactConversation(bot, visibleMessages(bot))) compact.run(`/api/bots/${bot.id}/compact`, { threadId });
else contextMenu.current?.open();
return;
}
if (busy) {
dispatch({ type: "error", message: t(command === "archive" ? "composer.command.archiveBusy" : "composer.command.deleteBusy") });
return;
}
if (command === "delete") { setConfirming(true); return; }
const archivedThreadId = threadId;
onCleared();
nextThread();
dispatch({ type: "updateTask", botId, threadId, patch: { archivedAt: Date.now() } });
// The sidebar's notice outlives this composer, which the conversation switch remounts.
announceArchiveNotice({
error: false,
text: t("composer.command.archived"),
undo: () => dispatch({ type: "updateTask", botId, threadId: archivedThreadId, patch: { archivedAt: null } }),
});
};
const confirmDelete = () => {
if (!botId) return;
setConfirming(false);
if (busy) { dispatch({ type: "error", message: t("composer.command.deleteBusy") }); return; }
onCleared();
nextThread();
dispatch({ type: "deleteTask", botId, threadId });
};
const dialog = (
<ConfirmDialog
open={confirming}
title={t("task.deleteConfirm")}
body={t("task.deleteBody", { title: title || t("task.untitledConversation") })}
confirmLabel={t("task.deleteAria")}
tone="danger"
onCancel={() => setConfirming(false)}
onConfirm={confirmDelete}
returnFocusRef={returnFocusRef}
/>
);
return { command, freshPrompt, run, dialog, contextMenu };
}