Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
// The desktop app's own count of a day it was used: one `app_active` event per install per local day, sent by main, so
// the count holds whatever its pages do. It is the same event, under the same id, that this computer's page sends, so a
// chart counts one install once a day whichever of the two reached it. It honours the same Usage analytics switch.
import { desktopOs, sendDesktopEvent } from "./launch-analytics.mjs";
const CHECK_MS = 10 * 60_000;
/** The computer's own calendar day, as the app's pages write it. */
export function localDay(date) {
const pad = part => String(part).padStart(2, "0");
return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}`;
}
/**
* `check()` sends the day's event when a window of the app is in view (`visible()`), this install may send
* (`installId()` is its id, null with the switch off or in a run that sends nothing) and the day kept in `day` is not
* today. The day is kept once the event was taken, so a send that failed is tried again at the next check, and a
* restart on the same day sends nothing more. Security: nothing is read, made or sent while no window is in view.
*/
export function createDesktopActive({ fetch, installId, day, visible, version, platform, now = Date.now, today = () => localDay(new Date(now())) }) {
let sending = null;
return {
check() {
if (!visible()) return;
const id = installId();
if (!id) return;
const date = today();
if (sending === date || day.read() === date) return;
sending = date;
const properties = { app_version: version, platform: "desktop", surface: "main", $os: desktopOs(platform) };
void sendDesktopEvent(fetch, { event: "app_active", id, at: now(), properties }).then(taken => {
// Security: a switch turned off while the event was on its way keeps no day.
if (taken && installId()) day.write(date);
if (sending === date) sending = null;
});
},
};
}
/** A window the person can see: shown, not minimized, not put away in the tray. */
export const inView = windows => windows.some(win => !win.isDestroyed() && win.isVisible() && !win.isMinimized());
/**
* This app's count: checked when a window is shown, restored or focused, and every ten minutes, which is how a day that
* changes while the app stays open is counted. A start at login out of sight shows no window, so it counts nothing.
* `windows()`: the app's own windows. `launch`: this launch's analytics, which say whether this install may send.
*/
export function installDesktopActive({ app, session, prefs, windows, launch, setInterval = globalThis.setInterval }) {
const active = createDesktopActive({
fetch: (...args) => session.defaultSession.fetch(...args), installId: () => launch.installId(), visible: () => inView(windows()),
day: { read: () => prefs.activeDay(), write: date => prefs.setActiveDay(date) }, version: app.getVersion(), platform: process.platform,
});
const check = () => { try { active.check(); } catch {} };
app.on("browser-window-focus", check);
app.on("browser-window-created", (_event, win) => { for (const shown of ["show", "restore", "ready-to-show"]) win.on(shown, check); });
setInterval(check, CHECK_MS).unref?.();
return active;
}