Add gateway overrides and quick sandbox deploy

This commit is contained in:
mmckeen-nv
2026-05-04 08:21:49 -07:00
parent bc0cbac970
commit 819e3e5f7d
20 changed files with 747 additions and 145 deletions
+62 -1
View File
@@ -1,4 +1,4 @@
import { existsSync, readdirSync, statSync } from "node:fs"
import { existsSync, readFileSync, readdirSync, statSync } from "node:fs"
import path from "node:path"
const HOME = process.env.HOME || ""
@@ -107,11 +107,72 @@ function pathEntries() {
export const HOST_PATH = unique(pathEntries()).join(":")
function nonEmptyEnv(name: string) {
const value = process.env[name]?.trim()
return value || undefined
}
function gatewayUrlFromHost(host: string, port: string) {
if (/^https?:\/\//i.test(host)) return host
if (host.includes(":")) return `http://${host}`
return `http://${host}:${port}`
}
function stringConfigValue(value: unknown) {
if (typeof value === "string") return value.trim() || undefined
if (typeof value === "number" && Number.isFinite(value)) return String(value)
return undefined
}
function readOpenShellGatewayAddressConfig() {
const configDir = OPENSHELL_XDG_CONFIG_HOME ? path.join(OPENSHELL_XDG_CONFIG_HOME, "openshell") : ""
const candidates = unique([
process.env.OPENSHELL_GATEWAY_CONFIG,
configDir ? path.join(configDir, "gateway.json") : undefined,
configDir ? path.join(configDir, "config.json") : undefined,
])
for (const candidate of candidates) {
if (!existsSync(candidate)) continue
try {
const parsed = JSON.parse(readFileSync(candidate, "utf8"))
const gateway = parsed?.gateway && typeof parsed.gateway === "object" ? parsed.gateway : {}
const host = stringConfigValue(parsed?.gatewayHost) || stringConfigValue(gateway.host)
const port = stringConfigValue(parsed?.gatewayPort) || stringConfigValue(gateway.port)
const url = stringConfigValue(parsed?.gatewayUrl) || stringConfigValue(gateway.url)
if (host || port || url) return { host, port, url }
} catch {
// Ignore malformed optional config; explicit env vars remain authoritative.
}
}
return {}
}
export function openshellGatewayAddressEnv() {
const config = readOpenShellGatewayAddressConfig()
const host = nonEmptyEnv("OPENSHELL_GATEWAY_HOST") || config.host
const configuredPort = nonEmptyEnv("OPENSHELL_GATEWAY_PORT") || config.port
const port = configuredPort || "8080"
const explicitUrl = nonEmptyEnv("OPENSHELL_GATEWAY_URL") || config.url
return {
...(host ? { OPENSHELL_GATEWAY_HOST: host } : {}),
...(configuredPort || host || explicitUrl ? { OPENSHELL_GATEWAY_PORT: port } : {}),
...(explicitUrl
? { OPENSHELL_GATEWAY_URL: explicitUrl }
: host
? { OPENSHELL_GATEWAY_URL: gatewayUrlFromHost(host, port) }
: {}),
}
}
export function hostCommandEnv(extra: Record<string, string | undefined> = {}) {
return {
...process.env,
...(OPENSHELL_HOME ? { HOME: OPENSHELL_HOME } : {}),
...(OPENSHELL_XDG_CONFIG_HOME ? { XDG_CONFIG_HOME: OPENSHELL_XDG_CONFIG_HOME } : {}),
...openshellGatewayAddressEnv(),
PATH: HOST_PATH,
NO_COLOR: "1",
CLICOLOR: "0",
+6 -1
View File
@@ -8,6 +8,10 @@ const execFileAsync = promisify(execFile)
const LOCAL_HOSTNAMES = new Set(["localhost", "127.0.0.1", "0.0.0.0", "host.docker.internal", "host.openshell.internal"])
const FALLBACK_SANDBOX_PROXY_ORIGIN = "http://10.200.0.1:3128"
function shouldUseProxyWrappedBrokerUrl() {
return /^(1|true|yes|on)$/i.test(process.env.OPENSHELL_CONTROL_MCP_BROKER_PROXY_URL || "")
}
async function discoverOpenShellDockerGateway() {
const container = process.env.OPEN_SHELL_CONTAINER || "openshell-cluster-nemoclaw"
const { stdout } = await execFileAsync("docker", ["inspect", container], {
@@ -67,10 +71,11 @@ export async function brokerBaseUrlForSandbox(
}
const hostBrokerUrl = `${publicOrigin.toString().replace(/\/+$/, "")}/api/mcp/broker`
if (!shouldUseProxyWrappedBrokerUrl()) return hostBrokerUrl
const proxyOrigin = sandbox
? await discoverSandboxProxyOrigin(sandbox.id, sandbox.name).catch(() => null)
: null
if (proxyOrigin) return `${proxyOrigin}/${hostBrokerUrl}`
if (LOCAL_HOSTNAMES.has(new URL(origin).hostname)) return `${FALLBACK_SANDBOX_PROXY_ORIGIN}/${hostBrokerUrl}`
return hostBrokerUrl
+4 -5
View File
@@ -1,6 +1,6 @@
import { execFile, spawn } from "node:child_process"
import { promisify } from "node:util"
import { HOST_PATH, OPENCLAW_BIN, OPENSHELL_BIN } from "./hostCommands"
import { HOST_PATH, OPENCLAW_BIN, OPENSHELL_BIN, hostCommandEnv } from "./hostCommands"
import { getDefaultOpenClawInstance, getOpenClawDashboardPortForSandbox, resolveOpenClawInstance } from "./openclawInstances"
const execFileAsync = promisify(execFile)
@@ -44,13 +44,12 @@ export type DashboardProbe = {
export async function execOpenShell(args: string[]) {
const { stdout, stderr } = await execFileAsync(OPENSHELL_BIN, args, {
env: {
...process.env,
PATH: HOST_PATH,
env: hostCommandEnv({
OPENSHELL_GATEWAY,
NO_COLOR: "1",
CLICOLOR: "0",
CLICOLOR_FORCE: "0",
},
}),
})
return { stdout, stderr }
}
+24
View File
@@ -61,6 +61,30 @@ export async function buildSandboxMcpBrokerHandoff(
token,
"```",
"",
"## Quick Check",
"",
"Run this inside the sandbox to verify that you are using the current broker token without pasting it into your shell history:",
"",
"```sh",
"node - <<'NODE'",
"const fs = require('fs')",
`const url = ${JSON.stringify(`${brokerBaseUrl}/mcp`)}`,
"const manifest = fs.readFileSync('/sandbox/openshell_control_mcp.md', 'utf8')",
"const token = manifest.match(/```\\n(osmcp_[^\\n]+)\\n```/)?.[1]",
"if (!token) throw new Error('MCP broker token not found in /sandbox/openshell_control_mcp.md')",
"const payload = { jsonrpc: '2.0', id: 1, method: 'tools/list', params: {} }",
"fetch(url, {",
" method: 'POST',",
" headers: { 'content-type': 'application/json', authorization: `Bearer ${token}` },",
" body: JSON.stringify(payload),",
"})",
" .then(async (response) => {",
" const body = await response.json()",
" console.log(JSON.stringify({ status: response.status, tools: body.result?.tools?.map((tool) => tool.name) || [], error: body.error || null }, null, 2))",
" })",
"NODE",
"```",
"",
"## Rules",
"",
"- Do not assume any MCP server exists unless the broker returns it as available.",
+47 -9
View File
@@ -75,12 +75,40 @@ function parseNetworkRules(output: string): SandboxNetworkRule[] {
.filter((rule) => rule.chunkId)
}
function hostPortForUrl(parsed: URL) {
const port = parsed.port || (parsed.protocol === "https:" ? "443" : "80")
return `${parsed.hostname}:${port}`
}
function addEndpointCandidate(candidates: Set<string>, parsed: URL) {
const hostPort = hostPortForUrl(parsed)
candidates.add(hostPort)
if (parsed.hostname === "host.docker.internal") {
const port = parsed.port || (parsed.protocol === "https:" ? "443" : "80")
candidates.add(`0.0.0.0:${port}`)
}
}
function extractProxiedTargetUrl(brokerBaseUrl: string) {
try {
const parsed = new URL(brokerBaseUrl)
const rawPath = parsed.pathname.replace(/^\/+/, "")
const decodedPath = decodeURIComponent(rawPath)
if (!/^https?:\/\//i.test(decodedPath)) return null
return new URL(decodedPath)
} catch {
return null
}
}
function brokerEndpointCandidates(brokerBaseUrl: string) {
const parsed = new URL(brokerBaseUrl)
const port = parsed.port || (parsed.protocol === "https:" ? "443" : "80")
const hostPort = `${parsed.hostname}:${port}`
const candidates = new Set([hostPort])
if (parsed.hostname === "host.docker.internal") candidates.add(`0.0.0.0:${port}`)
const candidates = new Set<string>()
addEndpointCandidate(candidates, parsed)
const proxiedTarget = extractProxiedTargetUrl(brokerBaseUrl)
if (proxiedTarget) addEndpointCandidate(candidates, proxiedTarget)
return candidates
}
@@ -153,13 +181,23 @@ async function probeBrokerEndpoint(sandboxName: string, brokerBaseUrl: string) {
}
const script = [
"node",
"--input-type=module",
"-e",
`
fetch(${JSON.stringify(mcpUrl)}, {
method: "POST",
headers: { "content-type": "application/json" },
body: ${JSON.stringify(JSON.stringify(payload))},
}).catch(() => {});
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), 8000);
try {
await fetch(${JSON.stringify(mcpUrl)}, {
method: "POST",
headers: { "content-type": "application/json" },
body: ${JSON.stringify(JSON.stringify(payload))},
signal: controller.signal,
});
} catch {
// The probe only exists to let OpenShell create a policy chunk.
} finally {
clearTimeout(timeout);
}
`,
]
await runOpenShell(["sandbox", "exec", "-n", sandboxName, "--", ...script], 15000).catch(() => null)