openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
51 lines (50 loc) • 2 kB
JavaScript
import { r as truncateUtf16Safe } from "./utf16-slice-D_ngcYKd.mjs";
import { n as runExec } from "./exec-DdJWnONN.mjs";
import fs from "node:fs/promises";
import os from "node:os";
//#region src/infra/host-account-name.ts
let cachedName;
async function readAccountCommand(command, args) {
try {
const { stdout } = await runExec(command, args, {
timeoutMs: 1e3,
maxBuffer: 16384,
logOutput: false
});
return stdout;
} catch {
return null;
}
}
async function readHostAccountName() {
if (process.platform !== "darwin" && process.platform !== "linux") return null;
const { username } = os.userInfo();
const normalizeName = (value) => {
const name = value?.trim();
return name && name.toLowerCase() !== username.toLowerCase() ? truncateUtf16Safe(name, 256) : null;
};
if (process.platform === "darwin") {
const fullName = normalizeName(await readAccountCommand("/usr/bin/id", ["-F"]));
if (fullName) return fullName;
return normalizeName((await readAccountCommand("/usr/bin/dscl", [
".",
"-read",
`/Users/${username}`,
"RealName"
]))?.replace(/^RealName:\s*/u, ""));
}
const fields = (await readAccountCommand("getent", ["passwd", username]) ?? await fs.readFile("/etc/passwd", "utf8")).split("\n").find((line) => line.split(":", 1)[0] === username)?.split(":");
return normalizeName(fields?.[4]?.split(",", 1)[0]);
}
/** Best-effort human name for the gateway host account; never seeds a login name. */
function resolveHostAccountName() {
return cachedName ??= readHostAccountName().catch(() => null);
}
//#endregion
//#region src/gateway/gateway-owner-profile.ts
/** Owner attribution never supplies a verified login or changes operator authority. */
function shouldUseGatewayOwnerProfile(params) {
return params.role === "operator" && !params.authenticatedUserId && (!params.rolesConfigured || params.authMethod === "token" || params.authMethod === "password");
}
//#endregion
export { resolveHostAccountName as n, shouldUseGatewayOwnerProfile as t };