Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

Restart the VPS idle window when the desktop viewer closes

Linb24f730e5943
diff --git a/server/index.ts b/server/index.ts
index 145b7c95d6..c08cd0fe3f 100644
--- a/server/index.ts
+++ b/server/index.ts
@@ -14944,7 +14944,7 @@ const handleRequest = async (req: IncomingMessage, res: ServerResponse) => {
return json(res, 415, { error: "content-type must be application/json" });
}
const closedViewers = auth.kind === "session" ? desktopViewer.closeForOwner(auth.session.id, bot.id) : 0;
- if (bot.cloudBackend === "vps") return json(res, 200, vps.closeVpsDesktopTunnel(bot.id));
+ if (bot.cloudBackend === "vps") return json(res, 200, vps.closeVpsDesktopTunnel(bot.id, () => touchVpsIdle(bot)));
return json(res, 200, { closed: closedViewers > 0 });
}
m = path.match(/^\/api\/bots\/([\w-]+)\/computer\/(provision|join|sleep|exec|screenshot|remove)$/);
diff --git a/server/vps-computer.test.ts b/server/vps-computer.test.ts
index 3edf5d4532..9cc3cd4b1a 100644
--- a/server/vps-computer.test.ts
+++ b/server/vps-computer.test.ts
@@ -1,3 +1,4 @@
+import { ComputerIdleTimer } from "./computer-idle.ts";
import { describe, expect, it, vi } from "vitest";
import { EventEmitter } from "node:events";
import { execFileSync } from "node:child_process";
@@ -1358,6 +1359,22 @@ describe("remote desktop tunnel ownership", () => {
expect(child.kill).toHaveBeenCalledTimes(1);
} finally { closeAllVpsDesktopTunnels(); vi.useRealTimers(); }
});
+ it("waits a full idle window after a short desktop view closes", async () => {
+ await openFixture();
+ vi.useFakeTimers();
+ const stop = vi.fn(async () => {});
+ const timer = new ComputerIdleTimer(1000, () => Boolean(vpsDesktopConnection(BOT_ID)), stop);
+ try {
+ timer.touch();
+ await vi.advanceTimersByTimeAsync(400);
+ expect(closeVpsDesktopTunnel(BOT_ID, () => timer.touch())).toEqual({ closed: true });
+ await vi.advanceTimersByTimeAsync(999);
+ expect(stop).not.toHaveBeenCalled();
+ await vi.advanceTimersByTimeAsync(1);
+ expect(stop).toHaveBeenCalledTimes(1);
+ } finally { timer.cancel(); closeAllVpsDesktopTunnels(); vi.useRealTimers(); }
+ });
+
it("keeps remote tabs connected when the native viewer closes", async () => {
const { child, runner } = await openFixture();
try {
diff --git a/server/vps-computer.ts b/server/vps-computer.ts
index 2119b97956..5deb21e730 100644
--- a/server/vps-computer.ts
+++ b/server/vps-computer.ts
@@ -240,14 +240,16 @@ function stopDesktopTunnel(botId: string, expected?: DesktopTunnel): boolean {
return true;
}
-export function closeVpsDesktopTunnel(botId: string) {
+export function closeVpsDesktopTunnel(botId: string, onClosed?: () => void) {
const tunnel = desktopTunnels.get(botId);
if (tunnel?.viewers) {
tunnel.localViewer = false;
clearTimeout(tunnel.expiry);
return { closed: false };
}
- return { closed: stopDesktopTunnel(botId) };
+ const closed = stopDesktopTunnel(botId);
+ if (closed) onClosed?.();
+ return { closed };
}
export function closeAllVpsDesktopTunnels(): void {