openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
108 lines (107 loc) • 4.52 kB
JavaScript
import { D as parseStrictPositiveInteger, E as parseStrictNonNegativeInteger, a as addTimerTimeoutGraceMs } from "./number-coercion-D6u-4YD8.mjs";
import { r as defaultRuntime } from "./runtime-B-SLYP-m.mjs";
import { t as danger } from "./globals-CgepmiqE.mjs";
import { n as callGatewayFromCli } from "./gateway-rpc-CxUN2f1a.mjs";
import "./number-runtime-W4chuk8B.mjs";
import { t as normalizeBrowserTimerDelayMs } from "./timer-delay-vTpJN_mI.mjs";
import { i as BROWSER_ACTION_TRANSPORT_SLACK_MS } from "./act-policy-fWdzY2YP.mjs";
import "./runtime-env-D-Pa5lD6.mjs";
import "./gateway-runtime-Du0olVch.mjs";
import { n as runCommandWithRuntime } from "./cli-utils-BPmaaZlG.mjs";
import "./cli-runtime-DeVPX2l7.mjs";
import { r as BROWSER_REQUEST_GATEWAY_SCOPES, t as BROWSER_REQUEST_GATEWAY_METHOD } from "./browser-gateway-contract-WuzRejvY.mjs";
import { n as resolveBrowserProxyTimeouts } from "./browser-proxy-timeouts-mqURld-y.mjs";
//#region extensions/browser/src/cli/browser-cli-shared.ts
const BROWSER_TAB_REFERENCE_HELP = "Tab reference: suggested target id, tab id, label, raw target id, or unique raw prefix";
/** Adds gateway slack to a Browser action timeout so route work can finish cleanly. */
function withBrowserActionTimeoutSlack(e) {
return addTimerTimeoutGraceMs(e ?? 2e4, 5e3) ?? 1;
}
async function runBrowserCliCommand(e, t = "runtime") {
const reportError = (e) => {
defaultRuntime.error(danger(String(e)));
defaultRuntime.exit(1);
};
if (t === "runtime") {
await runCommandWithRuntime(defaultRuntime, e, reportError);
return;
}
try {
await e();
} catch (e) {
reportError(e);
}
}
/** Execute a scoped request with the command family's existing error and output policy. */
async function runBrowserCliRequest(e) {
const action = async () => {
const t = e.profile === null ? void 0 : e.profile ?? e.parent.browserProfile;
const n = await callBrowserRequest(e.parent, {
method: e.method ?? "POST",
path: e.path,
query: resolveBrowserProfileQuery(t, e.query),
body: e.body
}, { timeoutMs: e.timeoutMs });
if (e.parent.json) defaultRuntime.writeJson(e.json ? e.json(n) : n);
else if (e.print) e.print(n);
else if (e.successMessage !== void 0) defaultRuntime.log(typeof e.successMessage === "function" ? e.successMessage(n) : e.successMessage);
};
await runBrowserCliCommand(action, e.parent.json ? "runtime" : e.errorPolicy);
}
function printBrowserJsonResult(e, t) {
if (!e?.json) return false;
defaultRuntime.writeJson(t);
return true;
}
function printBrowserList(e, t, n, r) {
if (e.length === 0) {
defaultRuntime.log(t);
return;
}
if (r !== void 0) defaultRuntime.log(r);
defaultRuntime.log(e.map(n).join("\n"));
}
function resolveBrowserProfileQuery(e, t) {
const n = {
...e ? { profile: e } : {},
...t
};
return Object.keys(n).length > 0 ? n : void 0;
}
function normalizeQuery(e) {
if (!e) return;
const t = {};
for (const [n, r] of Object.entries(e)) {
if (r === void 0) continue;
t[n] = String(r);
}
return Object.keys(t).length ? t : void 0;
}
function parseBrowserPositiveIntegerOption(e, t) {
const n = parseStrictPositiveInteger(e);
if (n === void 0) throw new Error(`${t} must be a positive integer.`);
return n;
}
function parseBrowserNonNegativeIntegerOption(e, t) {
const n = parseStrictNonNegativeInteger(e);
if (n === void 0) throw new Error(`${t} must be a non-negative integer.`);
return n;
}
async function callBrowserRequest(e, t, n) {
const r = typeof n?.timeoutMs === "number" && Number.isFinite(n.timeoutMs) ? normalizeBrowserTimerDelayMs(n.timeoutMs) : typeof e.timeout === "string" ? normalizeBrowserTimerDelayMs(parseBrowserPositiveIntegerOption(e.timeout, "--timeout")) : void 0;
const i = r === void 0 ? void 0 : resolveBrowserProxyTimeouts(r);
const a = await callGatewayFromCli(BROWSER_REQUEST_GATEWAY_METHOD, {
...e,
timeout: i ? String(i.gatewayTimeoutMs) : e.timeout
}, {
method: t.method,
path: t.path,
query: normalizeQuery(t.query),
body: t.body,
timeoutMs: i?.proxyTimeoutMs
}, { scopes: [...BROWSER_REQUEST_GATEWAY_SCOPES] });
if (a === void 0) throw new Error("Unexpected browser.request response");
return a;
}
//#endregion
export { printBrowserJsonResult as a, runBrowserCliCommand as c, parseBrowserPositiveIntegerOption as i, runBrowserCliRequest as l, callBrowserRequest as n, printBrowserList as o, parseBrowserNonNegativeIntegerOption as r, resolveBrowserProfileQuery as s, BROWSER_TAB_REFERENCE_HELP as t, withBrowserActionTimeoutSlack as u };