Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
// Two looks a bot can change for the person through app_settings, in either mode, with no row of their own beyond the
// usage one: how a plan's usage shows beside the model (today's bars, or one ring), and the order of the composer's end
// buttons. Both default to what the app always drew; only the usage row is shown, and only in Advanced mode.
import { useSyncExternalStore, type CSSProperties } from "react";
import { COMPOSER_BUTTONS, USAGE_INDICATORS } from "../../shared/app-settings-ids";
import { windowPrefChanged } from "./window-pref-changes";
export type UsageIndicator = (typeof USAGE_INDICATORS)[number];
export type ComposerButton = (typeof COMPOSER_BUTTONS)[number];
const USAGE_KEY = "omb-usage-indicator";
const ORDER_KEY = "omb-composer-order";
const listeners = new Set<() => void>();
function storage(): Storage | undefined {
try {
return typeof localStorage === "undefined" ? undefined : localStorage;
} catch {
return undefined;
}
}
function read(key: string): string | null {
try {
return storage()?.getItem(key) ?? null;
} catch {
return null;
}
}
function write(key: string, value: string | null): void {
try {
if (value === null) storage()?.removeItem(key);
else storage()?.setItem(key, value);
} catch {
// Storage can be full or blocked; the choice still holds until the window reloads.
}
cache = undefined;
for (const listener of listeners) listener();
windowPrefChanged();
}
export function readUsageIndicator(): UsageIndicator {
const stored = read(USAGE_KEY);
return (USAGE_INDICATORS as readonly string[]).includes(stored ?? "") ? stored as UsageIndicator : "bars";
}
export function setUsageIndicator(choice: UsageIndicator): void {
write(USAGE_KEY, choice === "bars" ? null : choice);
}
/** The order a bot set, each known button once; anything else reads as today's order (an empty list). */
export function readComposerOrder(): ComposerButton[] {
try {
const stored: unknown = JSON.parse(read(ORDER_KEY) ?? "[]");
if (!Array.isArray(stored)) return [];
const known = stored.filter((name): name is ComposerButton => (COMPOSER_BUTTONS as readonly unknown[]).includes(name));
return new Set(known).size === known.length ? known : [];
} catch {
return [];
}
}
export function setComposerOrder(order: readonly ComposerButton[]): void {
write(ORDER_KEY, order.length ? JSON.stringify(order) : null);
}
/** CSS `order` for each end button, read by the stylesheet (`[data-composer-actions]`): the named buttons first, in the
* order given, then the rest in today's order. Undefined for today's order. */
export function composerOrderStyle(order: readonly ComposerButton[]): CSSProperties | undefined {
if (!order.length) return undefined;
const ranked = [...order, ...COMPOSER_BUTTONS.filter((name) => !order.includes(name))];
return Object.fromEntries(ranked.map((name, index) => [`--composer-order-${name}`, index])) as CSSProperties;
}
let cache: { usage: UsageIndicator; order: ComposerButton[] } | undefined;
function snapshot() {
cache ??= { usage: readUsageIndicator(), order: readComposerOrder() };
return cache;
}
const DEFAULTS = { usage: "bars" as UsageIndicator, order: [] as ComposerButton[] };
function subscribe(listener: () => void): () => void {
listeners.add(listener);
const onStorage = (event: StorageEvent) => {
if (event.key !== USAGE_KEY && event.key !== ORDER_KEY) return;
cache = undefined;
listener();
};
window.addEventListener("storage", onStorage);
return () => {
listeners.delete(listener);
window.removeEventListener("storage", onStorage);
};
}
export function useComposerLayout(): { usage: UsageIndicator; order: ComposerButton[] } {
return useSyncExternalStore(subscribe, snapshot, () => DEFAULTS);
}