Skip to content

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

VPS editor: open it from a vps:<alias> server id

milind-sonie683f0a5ab2a
diff --git a/server/routes/bot-computers.ts b/server/routes/bot-computers.ts
index 379267e93b..7e1f4c056b 100644
--- a/server/routes/bot-computers.ts
+++ b/server/routes/bot-computers.ts
@@ -1,6 +1,6 @@
// Polled every few seconds by a devices map, so it reads only what this server already holds: the bots, the
// config, and the last answers other code got. It never looks at a computer, a provider, Docker, SSH or a screen.
-import type { BotComputerKind, BotComputerRow, BotComputersPayload, BotComputerState } from "../../shared/bot-computers.ts";
+import { vpsServerId, type BotComputerKind, type BotComputerRow, type BotComputersPayload, type BotComputerState } from "../../shared/bot-computers.ts";
import { boatIdOf, deadBoatOf } from "../boat.ts";
import { boatHealth } from "../boat-health.ts";
import { mapComputerState, type ComputerFrames } from "../computer-frames.ts";
@@ -104,7 +104,7 @@ export function botComputerRows(deps: BotComputersRouteDeps, visible: ReturnType
return {
kind, computerId: `vps:${alias}/${container}`, name: cfg.vpsServers?.find((server) => server.sshAlias === alias)?.name ?? null,
state: last ? containerState(last.status) : "unknown", stateAt: last?.at ?? null,
- vps: { serverId: `vps:${alias}`, alias, container },
+ vps: { serverId: vpsServerId(alias), alias, container },
};
}
if (kind === "vm") {
diff --git a/shared/bot-computers.test.ts b/shared/bot-computers.test.ts
new file mode 100644
index 0000000000..e6b208a015
--- /dev/null
+++ b/shared/bot-computers.test.ts
@@ -0,0 +1,16 @@
+import { describe, expect, it } from "vitest";
+
+import { vpsAliasOfServerId, vpsServerId } from "./bot-computers.ts";
+
+describe("VPS server ids", () => {
+ it("round-trips an alias through its vps:<alias> server id", () => {
+ expect(vpsServerId("office-vps")).toBe("vps:office-vps");
+ expect(vpsAliasOfServerId(vpsServerId("office-vps"))).toBe("office-vps");
+ });
+
+ it("reads no alias from a container id, another kind's id or a malformed one", () => {
+ for (const id of ["vps:office-vps/openmausbot-vps-bot-abc", "cloud:bot-1", "local", "vps:", "vps:-x", "office-vps"]) {
+ expect(vpsAliasOfServerId(id)).toBeNull();
+ }
+ });
+});
diff --git a/shared/bot-computers.ts b/shared/bot-computers.ts
index 478bd9d40d..a99542d561 100644
--- a/shared/bot-computers.ts
+++ b/shared/bot-computers.ts
@@ -37,3 +37,13 @@ export interface BotComputersPayload {
now: number;
rows: BotComputerRow[];
}
+
+/** A VPS server's device id, as `vps.serverId` and the Devices page carry it. */
+export function vpsServerId(alias: string): string {
+ return `vps:${alias}`;
+}
+
+/** The SSH alias in a `vps:<alias>` server id; null for any other id, a `vps:<alias>/<container>` computer id included. */
+export function vpsAliasOfServerId(id: string): string | null {
+ return /^vps:([A-Za-z0-9][A-Za-z0-9_.-]{0,127})$/.exec(id)?.[1] ?? null;
+}
diff --git a/src/components/keys/KeysPage.dom.test.ts b/src/components/keys/KeysPage.dom.test.ts
index 3ada5101e0..205c9d4df1 100644
--- a/src/components/keys/KeysPage.dom.test.ts
+++ b/src/components/keys/KeysPage.dom.test.ts
@@ -696,6 +696,20 @@ describe("Self-hosted VPS servers", () => {
expect(field("Memory (GB)").value).toBe("");
});
+ it("opens the same editor from a vps:<alias> server id, as the computers list carries it", async () => {
+ const { VpsServerEditor } = await import("./VpsServerEditor");
+ setConfig(status({ sshAlias: "production-vps", servers: [{ name: "Office", sshAlias: "office-vps", cpus: 4 }] }));
+ fx.api.mockImplementation(echo({ sshAlias: "production-vps" }));
+ render(() => createElement(VpsServerEditor, { key: "office", serverId: "vps:office-vps" }));
+ expect(aliases()).toEqual(["office-vps"]);
+ expect(field("Office CPU cores").value).toBe("4");
+ type(field("Office CPU cores"), "3");
+ await answered(() => button(rowFor("office-vps"), "Save").click());
+ expect(lastBody()).toEqual({ vpsServers: [{ name: "Office", sshAlias: "office-vps", cpus: 3 }] });
+ render(() => createElement(VpsServerEditor, { key: "gone", serverId: "vps:office-vps/openmausbot-vps-bot-abc" }));
+ expect(aliases()).toEqual([]);
+ });
+
it("focuses the default server's alias when a link asks for the VPS row", async () => {
setConfig(status({ sshAlias: "production-vps" }));
page();
diff --git a/src/components/keys/VpsServerEditor.tsx b/src/components/keys/VpsServerEditor.tsx
index 54f986a83b..552e71587d 100644
--- a/src/components/keys/VpsServerEditor.tsx
+++ b/src/components/keys/VpsServerEditor.tsx
@@ -3,6 +3,7 @@ import { Loader2 } from "lucide-react";
import { api, type ConfigStatus } from "@/state/store";
import { useStoreDispatch, useStoreSelector } from "@/state/store-select";
import { cn } from "@/lib/cn";
+import { vpsAliasOfServerId } from "../../../shared/bot-computers";
import { t } from "@/lib/i18n";
import { SETTINGS_INLINE_INPUT } from "../SettingsPrimitives";
import { KEY_FIELD } from "./KeyRow";
@@ -80,11 +81,17 @@ export function LimitInput({ label, unit, value, onChange, range, placeholder, d
* that shows a server can open it. Empty limits on a named server use the default server's; the default's use 4 GB
* and 2 cores. `onRemoved` hears the server's notice after a remove, since the editor is gone by then.
*/
-export function VpsServerEditor({ alias, onRemoved }: { alias: string; onRemoved?: (notice: string | null) => void }) {
+export type VpsServerEditorProps = ({ alias: string; serverId?: never } | { serverId: string; alias?: never }) & {
+ onRemoved?: (notice: string | null) => void;
+};
+
+/** Takes the server's SSH alias, or its `vps:<alias>` device id from the computers list or the Devices page. */
+export function VpsServerEditor({ onRemoved, ...which }: VpsServerEditorProps) {
+ const alias = which.alias ?? vpsAliasOfServerId(which.serverId ?? "") ?? "";
const vps = useStoreSelector((s) => s.config?.vps);
const named = vps?.servers ?? NO_SERVERS;
- const isDefault = Boolean(vps?.sshAlias) && vps?.sshAlias === alias;
- const own = isDefault ? undefined : named.find((server) => server.sshAlias === alias);
+ const isDefault = Boolean(alias) && vps?.sshAlias === alias;
+ const own = isDefault || !alias ? undefined : named.find((server) => server.sshAlias === alias);
const saving = useVpsSaving();
const save = useVpsSave();
const savedName = isDefault ? alias : own?.name ?? "";