openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
90 lines (89 loc) • 3.23 kB
JavaScript
import { a as extractErrorCode } from "./error-coercion-W9BtigRm.mjs";
import { r as truncateUtf16Safe } from "./utf16-slice-D_ngcYKd.mjs";
import { s as raceWithTimeout } from "./src-D1BHGNTj.mjs";
import { n as runExec } from "./exec-DdJWnONN.mjs";
import { s as spawnCommand } from "./exec-spawn-rD1j0ZHJ.mjs";
//#region src/gateway/server-methods/open-path.ts
const OPEN_PATH_TIMEOUT_MS = 5e3;
const XDG_OPEN_STARTUP_OBSERVATION_MS = 5e3;
const XDG_OPEN_STDERR_MAX_CHARS = 4096;
function resolveOpenPathCommand(targetPath, platform = process.platform) {
if (platform === "win32") return {
command: "powershell.exe",
args: [
"-NoProfile",
"-NonInteractive",
"-Command",
`Start-Process -FilePath '${targetPath.replaceAll("'", "''")}'`
]
};
return {
command: platform === "darwin" ? "open" : "xdg-open",
args: [targetPath]
};
}
async function observeXdgOpenStartup(command) {
const child = spawnCommand([command.command, ...command.args], {
buffer: false,
cleanup: false,
detached: true,
reject: true,
stdio: [
"ignore",
"ignore",
"pipe"
]
});
child.nodeChildProcess.unref();
let stderrText = "";
const stderr = child.stderr;
stderr?.setEncoding("utf8");
const onStderr = (chunk) => {
if (stderrText.length >= XDG_OPEN_STDERR_MAX_CHARS) return;
stderrText += truncateUtf16Safe(String(chunk), XDG_OPEN_STDERR_MAX_CHARS - stderrText.length);
};
stderr?.on("data", onStderr);
const releaseStderr = (childSettled) => {
stderr?.off("data", onStderr);
if (childSettled) {
stderr?.destroy();
return;
}
stderr?.resume();
stderr?.unref?.();
};
await raceWithTimeout(() => child.then(() => releaseStderr(true), (error) => {
releaseStderr(true);
const commandError = error instanceof Error ? error : new Error(String(error));
const diagnostic = stderrText.trim();
throw diagnostic ? new Error(`${commandError.message}: ${diagnostic}`, { cause: commandError }) : commandError;
}), XDG_OPEN_STARTUP_OBSERVATION_MS, () => releaseStderr(false));
}
async function execOpenPath(command, platform = process.platform) {
if (platform === "linux") {
await observeXdgOpenStartup(command);
return;
}
await runExec(command.command, command.args, {
logOutput: false,
timeoutMs: OPEN_PATH_TIMEOUT_MS
});
}
function formatOpenPathError(error) {
if (typeof error === "object" && error && "message" in error && typeof error.message === "string") return error.message;
return String(error);
}
function isHeadlessOpenPathError(error, command, platform = process.platform) {
if (platform === "linux" && command.command === "xdg-open" && extractErrorCode(error) === "ENOENT") return true;
const message = formatOpenPathError(error);
return message.includes("xdg-open") && message.includes("no method available");
}
function sanitizePathForLog(targetPath) {
const sanitized = Array.from(targetPath, (char) => {
const code = char.charCodeAt(0);
return code < 32 || code === 127 ? "?" : char;
}).join("");
return sanitized.length > 120 ? `${truncateUtf16Safe(sanitized, 117)}...` : sanitized;
}
//#endregion
export { sanitizePathForLog as a, resolveOpenPathCommand as i, formatOpenPathError as n, isHeadlessOpenPathError as r, execOpenPath as t };