openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
158 lines (157 loc) • 7.71 kB
JavaScript
import { l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs";
import { D as parseStrictPositiveInteger, E as parseStrictNonNegativeInteger } from "./number-coercion-D6u-4YD8.mjs";
import { l as isRecord } from "./record-coerce-CVPuDqcc.mjs";
import { r as defaultRuntime } from "./runtime-B-SLYP-m.mjs";
import { p as shortenHomePath } from "./utils-C8YECn2Q.mjs";
import { t as captureRuntimeConfigAsyncReader } from "./io.runtime-DsJ2dLCQ.mjs";
import { n as inheritOptionFromParent } from "./command-options-D76J8Wl7.mjs";
import "./string-coerce-runtime-BwAZZDf5.mjs";
import "./runtime-config-snapshot-Dy0VFqWf.mjs";
import "./text-utility-runtime-Ds8jsG0J.mjs";
import "./number-runtime-W4chuk8B.mjs";
import "./runtime-env-D-Pa5lD6.mjs";
import "./cli-runtime-DeVPX2l7.mjs";
import { t as writeExternalFileWithinOutputRoot } from "./output-files-Cl2IXnm2.mjs";
import { c as runBrowserCliCommand, i as parseBrowserPositiveIntegerOption, n as callBrowserRequest, t as BROWSER_TAB_REFERENCE_HELP } from "./browser-cli-shared-BEDAHNiM.mjs";
import path from "node:path";
import fs from "node:fs/promises";
//#region extensions/browser/src/cli/browser-cli-inspect.ts
function parseOptionalIntegerOption(e, t, n) {
if (e === void 0) return;
const r = n.min === 0 ? parseStrictNonNegativeInteger(e) : parseStrictPositiveInteger(e);
if (r === void 0 || r < n.min) throw new Error(`Invalid ${t}: must be an integer >= ${n.min}`);
return r;
}
function parseBrowserChoiceOption(e, t, n) {
const r = n.find((t) => t === e);
if (r !== void 0) return r;
throw new Error(`Invalid ${t}: expected ${n.join(" or ")}`);
}
function readPendingDialogs(e) {
const t = isRecord(e) ? e.dialogs : void 0;
const n = isRecord(t) ? t.pending : void 0;
if (!Array.isArray(n)) return [];
return n.flatMap((e) => {
if (!isRecord(e) || typeof e.id !== "string") return [];
return [{
id: e.id,
type: typeof e.type === "string" ? e.type : void 0,
message: typeof e.message === "string" ? e.message : void 0
}];
});
}
function describeDialogBlockedSnapshot(e, t) {
const n = t ? ` --browser-profile=${t}` : "";
const r = [`Snapshot not captured: a pending dialog blocks tab ${e.targetId}.`];
for (const t of readPendingDialogs(e.browserState)) {
const i = t.type ? ` (${t.type})` : "";
const a = t.message ? `: ${JSON.stringify(t.message)}` : "";
r.push(`Dialog ${t.id}${i}${a}`);
r.push(`Dismiss it with: openclaw browser dialog${n} --dismiss --dialog-id ${t.id} --target-id ${e.targetId}`);
}
return r.join("\n");
}
function resolveBrowserInspectTimeout(e, t, n) {
const r = e.getOptionValueSource("timeout") === "cli" ? n : inheritOptionFromParent(e, "timeout", "cli");
if (r === void 0) return { parent: t };
const i = parseBrowserPositiveIntegerOption(r, "--timeout");
return {
parent: {
...t,
timeout: String(i)
},
timeoutMs: i
};
}
function registerBrowserInspectCommands(e, t) {
e.command("screenshot").description("Capture a screenshot (prints the saved path)").argument("[targetId]", BROWSER_TAB_REFERENCE_HELP).option("--full-page", "Capture full scrollable page", false).option("--ref <ref>", "ARIA ref from ai snapshot").option("--element <selector>", "CSS selector for element screenshot").option("--labels", "Overlay role refs on the screenshot (works with --full-page, --ref, and --element)", false).option("--type <png|jpeg>", "Output type (default: png)", "png").option("--timeout <ms>", "Timeout in ms").action(async (e, n, r) => {
const i = t(r);
const a = i?.browserProfile;
const o = parseBrowserChoiceOption(n.type, "--type", ["png", "jpeg"]);
await runBrowserCliCommand(async () => {
const t = resolveBrowserInspectTimeout(r, i, n.timeout);
const s = await callBrowserRequest(t.parent, {
method: "POST",
path: "/screenshot",
query: a ? { profile: a } : void 0,
body: {
targetId: normalizeOptionalString(e),
fullPage: Boolean(n.fullPage),
ref: normalizeOptionalString(n.ref),
element: normalizeOptionalString(n.element),
labels: Boolean(n.labels),
type: o,
...t.timeoutMs === void 0 ? {} : { timeoutMs: t.timeoutMs }
}
});
if (i?.json) {
defaultRuntime.writeJson(s);
return;
}
defaultRuntime.log(shortenHomePath(s.path));
}, "inline");
});
e.command("snapshot").description("Capture a snapshot (default: ai; aria is the accessibility tree)").option("--format <aria|ai>", "Snapshot format (default: ai)", "ai").option("--target-id <id>", BROWSER_TAB_REFERENCE_HELP).option("--limit <n>", "Max nodes (default: 500/800)").option("--mode <efficient>", "Snapshot preset (efficient)").option("--efficient", "Use the efficient snapshot preset", false).option("--interactive", "Role snapshot: interactive elements only", false).option("--compact", "Role snapshot: compact output", false).option("--depth <n>", "Role snapshot: max depth").option("--selector <sel>", "Role snapshot: scope to CSS selector").option("--frame <sel>", "Role snapshot: scope to an iframe selector").option("--labels", "Include label overlay screenshot with annotations", false).option("--urls", "Append discovered link URLs to AI snapshots", false).option("--out <path>", "Write snapshot to a file").option("--timeout <ms>", "Timeout in ms").action(async (e, n) => {
const r = t(n);
const i = r?.browserProfile;
const a = parseBrowserChoiceOption(e.format, "--format", ["aria", "ai"]);
const o = e.mode === void 0 ? void 0 : parseBrowserChoiceOption(e.mode, "--mode", ["efficient"]);
const s = !(n.getOptionValueSource("format") === "cli") && a === "ai" && (await captureRuntimeConfigAsyncReader()()).browser?.snapshotDefaults?.mode === "efficient" ? "efficient" : void 0;
const c = e.efficient === true || o === "efficient" ? "efficient" : s;
const l = parseOptionalIntegerOption(e.limit, "--limit", { min: 1 });
const u = parseOptionalIntegerOption(e.depth, "--depth", { min: 0 });
await runBrowserCliCommand(async () => {
const t = resolveBrowserInspectTimeout(n, r, e.timeout);
const o = {
format: a,
targetId: normalizeOptionalString(e.targetId),
limit: l,
interactive: e.interactive ? true : void 0,
compact: e.compact ? true : void 0,
depth: u,
selector: normalizeOptionalString(e.selector),
frame: normalizeOptionalString(e.frame),
labels: e.labels ? true : void 0,
urls: e.urls ? true : void 0,
mode: c,
profile: i,
...t.timeoutMs === void 0 ? {} : { timeoutMs: t.timeoutMs }
};
const s = await callBrowserRequest(t.parent, {
method: "GET",
path: "/snapshot",
query: o
});
const d = s.format === "ai" ? s.imagePath : void 0;
if (s.blockedByDialog && (e.out || !r?.json)) throw new Error(describeDialogBlockedSnapshot(s, normalizeOptionalString(i)));
if (e.out) {
const t = s.format === "ai" ? s.snapshot : JSON.stringify(s, null, 2);
await writeExternalFileWithinOutputRoot({
path: path.resolve(e.out),
write: async (e) => {
await fs.writeFile(e, t, "utf8");
}
});
}
if (r?.json) {
defaultRuntime.writeJson(e.out ? {
ok: true,
out: e.out,
...d ? { imagePath: d } : {}
} : s);
return;
}
if (e.out) defaultRuntime.log(shortenHomePath(e.out));
else if (s.format === "ai") defaultRuntime.log(s.snapshot);
else defaultRuntime.log(s.nodes.map((e) => {
const t = " ".repeat(Math.min(20, e.depth));
const n = e.name ? ` "${e.name}"` : "";
const r = e.value ? ` = "${e.value}"` : "";
return `${t}- ${e.role}${n}${r} [ref=${e.ref}]`;
}).join("\n"));
if (d) defaultRuntime.log(shortenHomePath(d));
}, "inline");
});
}
//#endregion
export { registerBrowserInspectCommands };