Skip to content

armgit / raea

Public Imported from GitHub
0

Render Anything Engine: tell your AI what to make. Videos, motion graphics, 3D and games as React and TypeScript, rendered on your own GPU. Rust + Bevy, macOS, Linux and Windows.

raea cloud's box: its shell on Windows (RAEA_CLOUD_BASH, taskkill), for raea.app's local cloud

shinyobjectz13092a5cfce1
diff --git a/sdk/cloud/server/agents.ts b/sdk/cloud/server/agents.ts
index 986e9dae..a32fbed7 100644
--- a/sdk/cloud/server/agents.ts
+++ b/sdk/cloud/server/agents.ts
@@ -8,6 +8,7 @@
// node agents.ts skills the raea skill names for the installed agents (entrypoint.sh)
// node agents.ts list the installed agents, one per line
+import { sh } from "./shell-os.ts"
import { execFileSync } from "node:child_process"
import { createHash } from "node:crypto"
import { chmodSync, existsSync, mkdirSync, readdirSync, readFileSync, rmSync, writeFileSync } from "node:fs"
@@ -121,7 +122,7 @@ export function installed(): Spawn[] {
export function spawnFor(id?: string): Spawn {
const want = id || process.env.RAEA_AGENT || ""
const cmd = process.env.RAEA_AGENT_CMD
- if (cmd && (!want || want === "custom")) return { id: "custom", version: "", command: "/bin/sh", args: ["-c", cmd], env: {} }
+ if (cmd && (!want || want === "custom")) return { id: "custom", version: "", command: sh(), args: ["-c", cmd], env: {} }
const all = installed()
const s = want ? all.find((a) => a.id === want) : all[0]
if (!s) throw new Error(want ? `${want} isn't installed in this image (installed: ${all.map((a) => a.id).join(", ") || "none"})` : "no agent is installed: build with AGENTS=..., or set RAEA_AGENT_CMD")
diff --git a/sdk/cloud/server/main.ts b/sdk/cloud/server/main.ts
index 683df366..06ae1cd3 100644
--- a/sdk/cloud/server/main.ts
+++ b/sdk/cloud/server/main.ts
@@ -116,14 +116,12 @@ async function shell(req: IncomingMessage, res: ServerResponse) {
const dir = resolve(cwd ?? ".")
mkdirSync(dir, { recursive: true })
res.writeHead(200, { "content-type": "application/x-ndjson" })
+ // the shell on any OS: bash and a process group here, RAEA_CLOUD_BASH and taskkill on Windows (shell-os.ts)
+ const { bash, groupOpts, stopTree } = await import("./shell-os.ts")
// in a process group of its own, so a timeout or a caller leaving stops what the command started too
- const child = spawn("bash", ["-c", command], { cwd: dir, env: childEnv(), stdio: ["ignore", "pipe", "pipe"], detached: true })
+ const child = spawn(bash(), ["-c", command], { cwd: dir, env: childEnv(), stdio: ["ignore", "pipe", "pipe"], ...groupOpts() })
const line = (v: unknown) => res.write(JSON.stringify(v) + "\n")
- const stop = () => {
- try {
- if (child.pid && child.exitCode === null) process.kill(-child.pid, "SIGTERM")
- } catch {}
- }
+ const stop = () => stopTree(child)
let timedOut = false
const timer = setTimeout(() => ((timedOut = true), stop()), ms)
child.stdout.setEncoding("utf8").on("data", (out: string) => line({ out }))
diff --git a/sdk/cloud/server/shell-os.ts b/sdk/cloud/server/shell-os.ts
new file mode 100644
index 00000000..a491c729
--- /dev/null
+++ b/sdk/cloud/server/shell-os.ts
@@ -0,0 +1,26 @@
+// The box's shell on any OS (main.ts /v1/shell, agents.ts RAEA_AGENT_CMD). In raea cloud's image it's Linux's bash and
+// /bin/sh; on a person's own computer (raea.app's local cloud, apps/app host/local-cloud/box.ts) it can be Windows,
+// where there's no /bin/sh: RAEA_CLOUD_BASH names a bash (the one beside Git for Windows, or raea's own Git's), and a
+// run is stopped with its children by taskkill, as Windows has no process groups to signal.
+import { spawn, type ChildProcess } from "node:child_process"
+
+const win = process.platform === "win32"
+
+/** the bash that runs a command line: RAEA_CLOUD_BASH, else bash on PATH */
+export const bash = () => process.env.RAEA_CLOUD_BASH || "bash"
+
+/** the POSIX shell an agent's own command line runs in (RAEA_AGENT_CMD): /bin/sh, or that bash on Windows */
+export const sh = () => (win ? bash() : "/bin/sh")
+
+/** spawn options for a run that's stopped with everything it started: its own process group (not on Windows, where a
+ * detached child gets a console window of its own) */
+export const groupOpts = () => (win ? { windowsHide: true } : { detached: true })
+
+/** the run and what it started stopped */
+export function stopTree(child: ChildProcess) {
+ if (!child.pid || child.exitCode !== null) return
+ try {
+ if (win) spawn("taskkill", ["/pid", String(child.pid), "/T", "/F"], { windowsHide: true, stdio: "ignore" })
+ else process.kill(-child.pid, "SIGTERM")
+ } catch {}
+}