This commit is contained in:
Chris Wanstrath 2025-09-30 22:49:01 -07:00
parent 6701c31a3a
commit 0ba73b6dac
3 changed files with 18 additions and 10 deletions

View File

@ -57,11 +57,11 @@ function retryConnection() {
connected = false
if (retries >= MAX_RETRIES) {
addErrorMessage(`!! Failed to reconnect ${retries} times. Server is down.`)
addErrorMessage(`Failed to reconnect ${retries} times. Server is down.`)
if (ws) ws.onclose = () => { }
return
}
retries++
addErrorMessage(`!! Connection lost. Retrying...`)
addErrorMessage(`Connection lost. Retrying...`)
setTimeout(startConnection, 2000)
}

View File

@ -12,7 +12,7 @@ import { transpile, isFile, tilde } from "./utils"
import { serveApp } from "./webapp"
import { initDNS } from "./dns"
import { commands, commandPath, loadCommandModule } from "./commands"
import { runInSession, processExecOutput } from "./shell"
import { runCommandFn } from "./shell"
import { send, addWebsocket, removeWebsocket, closeWebsockets } from "./websocket"
import { Layout } from "./html/layout"
@ -98,12 +98,9 @@ app.on(["GET", "POST"], ["/cmd/:name"], async c => {
try {
const mod = await loadCommandModule(cmd)
if (!mod || !mod[method])
return c.json({ error: `No ${method} export in ${cmd}` }, 500)
return c.json({ status: "error", output: `No ${method} export in ${cmd}` }, 500)
return c.json(await runInSession(sessionId, async () => {
const [status, output] = processExecOutput(await mod[method](c))
return { status, output }
}))
return c.json(await runCommandFn(sessionId, async () => mod[method](c)))
} catch (e: any) {
return c.json({ status: "error", output: e.message || e.toString() }, 500)
}

View File

@ -29,9 +29,20 @@ export async function runCommand(sessionId: string, taskId: string, input: strin
return { status, output }
}
export async function runInSession(sessionId: string, fn: () => Promise<any>) {
export async function runCommandFn(sessionId: string, fn: () => Promise<CommandResult>) {
let status: "ok" | "error" = "ok"
let output: CommandOutput = ""
const state = getState(sessionId)
return await ALS.run(state, async () => await fn())
try {
const execOutput = await ALS.run(state, async () => await fn())
;[status, output] = processExecOutput(execOutput)
} catch (err) {
status = "error"
output = errorMessage(err)
}
return { status, output }
}
async function exec(cmd: string, args: string[]): Promise<["ok" | "error", CommandOutput]> {