openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
130 lines (129 loc) • 4.56 kB
JavaScript
import { l as normalizeOptionalString, p as normalizeStringifiedOptionalString } from "./string-coerce-CCIiB4H7.mjs";
import { r as truncateUtf16Safe } from "./utf16-slice-CuDyEXgS.mjs";
import { i as runUtf8CommandWithTimeout } from "./exec-runner-Br73vG-X.mjs";
import { n as runExec } from "./exec-aRIvS9s4.mjs";
import "./string-coerce-runtime-BwAZZDf5.mjs";
import "./text-utility-runtime-Ds8jsG0J.mjs";
import "./process-runtime-DcBPEA8Y.mjs";
import { n as COGNITIVE_SERVICES_RESOURCE } from "./shared-DiLOhEpP.mjs";
import { execFileSync } from "node:child_process";
//#region extensions/microsoft-foundry/cli.ts
function summarizeAzErrorMessage(e) {
const t = e.trim();
if (!t) return "";
const n = t.replace(/\s+/g, " ");
if (/not recognized|enoent|spawn .* az/i.test(n)) return "Azure CLI (az) is not installed or not on PATH.";
if (/az login/i.test(n) || /please run 'az login'/i.test(n)) return "Azure CLI is not logged in. Run `az login --use-device-code`.";
if (/subscription/i.test(n) && /could not be found|does not exist|no subscriptions/i.test(n)) return "Azure CLI could not find an accessible subscription. Check the selected subscription or tenant access.";
if (/tenant/i.test(n) && /not found|invalid|doesn't exist|does not exist/i.test(n)) return "Azure CLI could not use that tenant. Verify the tenant ID or tenant domain and try `az login --tenant <tenant>`.";
if (/aadsts\d+/i.test(n)) return "Azure login failed for the selected tenant. Re-run `az login --use-device-code` and confirm the tenant is correct.";
return truncateUtf16Safe(n, 300);
}
function buildAzCommandError(e, t, n) {
const r = summarizeAzErrorMessage(`${t ?? ""} ${n ?? ""}`);
return new Error(r ? `${e.message}: ${r}` : e.message);
}
function execAz(e) {
return normalizeOptionalString(execFileSync("az", e, {
encoding: "utf-8",
timeout: 3e4,
shell: process.platform === "win32",
killSignal: "SIGKILL"
})) ?? "";
}
async function execAzAsync(e) {
try {
const { stdout: t } = await runExec("az", e, {
logOutput: false,
timeoutMs: 3e4
});
return normalizeStringifiedOptionalString(t) ?? "";
} catch (e) {
const t = e instanceof Error ? e : new Error(String(e));
const n = e;
throw buildAzCommandError(t, typeof n.stderr === "string" ? n.stderr : "", typeof n.stdout === "string" ? n.stdout : "");
}
}
function isAzCliInstalled() {
try {
execAz([
"version",
"--output",
"none"
]);
return true;
} catch {
return false;
}
}
function getLoggedInAccount() {
try {
return parseAzJson(execAz([
"account",
"show",
"--output",
"json"
]), "account");
} catch {
return null;
}
}
function listSubscriptions() {
try {
return parseAzJson(execAz([
"account",
"list",
"--output",
"json",
"--all"
]), "subscriptions").filter((e) => e.state === "Enabled");
} catch {
return [];
}
}
function parseAzJson(e, t) {
try {
return JSON.parse(e);
} catch {
throw new Error(`Azure CLI returned malformed ${t} JSON.`);
}
}
function buildAccessTokenArgs(e) {
const t = ["account", "get-access-token"];
if (e?.scope) t.push("--scope", e.scope);
else t.push("--resource", COGNITIVE_SERVICES_RESOURCE);
t.push("--output", "json");
if (e?.subscriptionId) t.push("--subscription", e.subscriptionId);
else if (e?.tenantId) t.push("--tenant", e.tenantId);
return t;
}
function getAccessTokenResult(e) {
return parseAzJson(execAz(buildAccessTokenArgs(e)), "access token");
}
async function getAccessTokenResultAsync(e) {
return parseAzJson(await execAzAsync(buildAccessTokenArgs(e)), "access token");
}
const AZ_LOGIN_TIMEOUT_MS = 12e5;
async function azLoginDeviceCodeWithOptions(e) {
const t = [
"login",
"--use-device-code",
...e.tenantId ? ["--tenant", e.tenantId] : [],
...e.allowNoSubscriptions ? ["--allow-no-subscriptions"] : []
];
const n = await runUtf8CommandWithTimeout(["az", ...t], {
timeoutMs: AZ_LOGIN_TIMEOUT_MS,
killProcessTree: true,
outputCapture: "tail",
maxOutputBytes: 8e3,
onOutputChunk: (e, t) => {
process[t].write(e);
}
});
if (n.termination === "timeout") throw new Error("az login timed out after 20 minutes");
if (n.code === 0) return;
const r = normalizeOptionalString(`${n.stderr}${n.stdout}`) ?? "";
throw new Error(r ? `az login exited with code ${n.code}: ${r}` : `az login exited with code ${n.code}`);
}
//#endregion
export { getLoggedInAccount as a, getAccessTokenResultAsync as i, execAz as n, isAzCliInstalled as o, getAccessTokenResult as r, listSubscriptions as s, azLoginDeviceCodeWithOptions as t };