Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
// The automation actions with app events added: pause, run now and delete reach /api/app-triggers for kind "app".
import { useState } from "react";
import { appTriggerApi } from "@/lib/app-triggers";
import type { Automation } from "@/lib/automations";
import { useAutomationActions } from "./actions";
/** What `busy` is while "Pause all" / "Resume all" writes: every switch waits for it. */
export const SET_ALL_BUSY = "toggle:all";
export type AutomationActions = ReturnType<typeof useAllAutomationActions>;
export function useAllAutomationActions() {
const base = useAutomationActions();
const [busy, setBusy] = useState<string | null>(null);
const [error, setError] = useState("");
const attempt = async (key: string, work: () => Promise<unknown>) => {
if (busy || base.busy) return false;
setBusy(key);
setError("");
base.clearError();
try { await work(); return true; }
catch (failure) { setError(failure instanceof Error ? failure.message : String(failure)); return false; }
finally { setBusy(null); }
};
/** Runs a schedule's or another app's own action: an earlier "Pause all" refusal is no longer the latest word. */
const handOn = <T>(action: () => T): T => {
if (!base.busy) setError("");
return action();
};
return {
...base,
busy: busy ?? base.busy,
error: error || base.error,
setError: (message: string) => { setError(message); base.setError(message); },
/** "Pause all" / "Resume all": turns each of `automations` on or off in turn, stopping at the first refusal.
* Gives the ids it changed, so a refusal partway still says which ones it turned. */
setAllEnabled: async (automations: readonly Automation[], enabled: boolean): Promise<string[]> => {
const changed: string[] = [];
await attempt(SET_ALL_BUSY, async () => {
for (const automation of automations) {
if (automation.kind === "app") await appTriggerApi.update(automation.id, { enabled });
else await base.setEnabled(automation, enabled);
changed.push(automation.id);
}
});
return changed;
},
toggle: (automation: Automation) => automation.kind === "app"
? void attempt(`toggle:${automation.id}`, () => appTriggerApi.update(automation.id, { enabled: !automation.enabled }))
: busy ? undefined : handOn(() => base.toggle(automation)),
runNow: (automation: Automation) => automation.kind === "app"
? attempt(`run:${automation.id}`, () => appTriggerApi.test(automation.id))
: handOn(() => base.runNow(automation)),
copyAddress: (webhookId: string, replace?: boolean) => handOn(() => base.copyAddress(webhookId, replace)),
cancelRun: (...args: Parameters<typeof base.cancelRun>) => handOn(() => base.cancelRun(...args)),
remove: async (automation: Automation, question: string): Promise<boolean> => {
if (!window.confirm(question)) return false;
if (automation.kind !== "app") {
const removed = await handOn(() => base.removeConfirmed(automation));
if (removed) setError("");
return removed;
}
return attempt(`delete:${automation.id}`, () => appTriggerApi.remove(automation.id));
},
};
}