openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
201 lines (200 loc) • 8.26 kB
JavaScript
import { c as normalizeOptionalLowercaseString, l as normalizeOptionalString, m as readNonBlankString } from "./string-coerce-CCIiB4H7.mjs";
import { w as parseStrictFiniteNumber } from "./number-coercion-D6u-4YD8.mjs";
import { n as parseBooleanValue } from "./boolean-2vRuVafy.mjs";
import { c as filterStringRecord } from "./record-coerce-CVPuDqcc.mjs";
import { r as defaultRuntime } from "./runtime-B-SLYP-m.mjs";
import { n as inheritOptionFromParent } from "./command-options-D76J8Wl7.mjs";
import "./string-coerce-runtime-BwAZZDf5.mjs";
import "./number-runtime-W4chuk8B.mjs";
import "./runtime-env-D-Pa5lD6.mjs";
import "./cli-runtime-DeVPX2l7.mjs";
import { a as printBrowserJsonResult, c as runBrowserCliCommand, l as runBrowserCliRequest, n as callBrowserRequest, t as BROWSER_TAB_REFERENCE_HELP } from "./browser-cli-shared-BEDAHNiM.mjs";
import { t as registerBrowserResizeCommand } from "./browser-cli-resize-DTqBbuoW.mjs";
//#region extensions/browser/src/cli/browser-cli-state.ts
function resolveTargetId(e, t) {
return normalizeOptionalString(e) ?? normalizeOptionalString(inheritOptionFromParent(t, "targetId"));
}
function parseFiniteNumberOption(e, t) {
if (e === void 0) return;
const n = parseStrictFiniteNumber(e);
if (n === void 0) throw new Error(`Invalid ${t}: must be a finite number`);
return n;
}
function registerBrowserStateCommands(e, t) {
const n = e.command("cookies").description("Read/write cookies");
const r = e.command("storage").description("Read/write localStorage/sessionStorage");
for (const e of [
"cookies",
"local",
"session"
]) {
const i = e === "cookies";
const a = i ? n : r.command(e).description(`${e}Storage commands`);
const o = i ? "/cookies" : `/storage/${e}`;
const s = i ? "cookie" : `${e}Storage`;
const read = async (e, n, r) => {
const a = t(r);
const s = resolveTargetId(n.targetId, r);
await runBrowserCliRequest({
parent: a,
method: "GET",
path: o,
query: i ? { targetId: s } : {
key: readNonBlankString(e),
targetId: s
},
errorPolicy: "inline",
print: (e) => defaultRuntime.writeJson(i ? e.cookies ?? [] : e.values ?? {})
});
};
if (i) a.option("--target-id <id>", BROWSER_TAB_REFERENCE_HELP).action((e, t) => read(void 0, e, t));
else a.command("get").description(`Get ${e}Storage (all keys or one key)`).argument("[key]", "Key (optional)").option("--target-id <id>", BROWSER_TAB_REFERENCE_HELP).action(read);
const c = a.command("set").description(i ? "Set a cookie (requires --url or domain+path)" : `Set a ${e}Storage key`).argument(i ? "<name>" : "<key>", i ? "Cookie name" : "Key").argument("<value>", i ? "Cookie value" : "Value");
if (i) c.option("--url <url>", "Cookie URL scope (recommended)");
c.option("--target-id <id>", BROWSER_TAB_REFERENCE_HELP).action(async (e, n, r, a) => {
const c = t(a);
const l = resolveTargetId(r.targetId, a);
const u = i ? normalizeOptionalString(r.url) : void 0;
if (i && !u) throw new Error("Missing required --url option for cookies set");
await runBrowserCliRequest({
parent: c,
path: `${o}/set`,
body: i ? {
targetId: l,
cookie: {
name: e,
value: n,
url: u
}
} : {
key: e,
value: n,
targetId: l
},
errorPolicy: "inline",
successMessage: `${s} set: ${e}`
});
});
a.command("clear").description(i ? "Clear all cookies" : `Clear all ${e}Storage keys`).option("--target-id <id>", BROWSER_TAB_REFERENCE_HELP).action(async (e, n) => {
const r = t(n);
const a = resolveTargetId(e.targetId, n);
await runBrowserCliRequest({
parent: r,
path: `${o}/clear`,
body: { targetId: a },
errorPolicy: "inline",
successMessage: `${i ? "cookies" : s} cleared`
});
});
}
const i = e.command("set").description("Browser environment settings");
registerBrowserResizeCommand(i.command("viewport").description("Set viewport size (alias for resize)"), t, true);
const registerParsedSetting = (e) => {
const n = e === "offline";
const r = n ? "on|off" : "dark|light|no-preference|none";
i.command(e).description(n ? "Toggle offline mode" : "Emulate prefers-color-scheme").argument(`<${r}>`, r.replaceAll("|", "/")).option("--target-id <id>", BROWSER_TAB_REFERENCE_HELP).action(async (i, a, o) => {
const s = t(o);
const c = n ? parseBooleanValue(i) : normalizeOptionalLowercaseString(i);
if (c === void 0 || typeof c === "string" && !r.split("|").includes(c)) throw new Error(`Expected ${r}`);
await runBrowserCliRequest({
parent: s,
path: `/set/${e}`,
body: {
[n ? "offline" : "colorScheme"]: c,
targetId: normalizeOptionalString(a.targetId)
},
successMessage: `${n ? "offline" : "media colorScheme"}: ${c}`
});
});
};
registerParsedSetting("offline");
i.command("headers").description("Set extra HTTP headers (JSON object)").argument("[headersJson]", "JSON object of headers (alternative to --headers-json)").option("--headers-json <json>", "JSON object of headers").option("--target-id <id>", BROWSER_TAB_REFERENCE_HELP).action(async (e, n, r) => {
const i = t(r);
await runBrowserCliCommand(async () => {
const t = normalizeOptionalString(n.headersJson) ?? normalizeOptionalString(e);
if (!t) throw new Error("Missing headers JSON (pass --headers-json or positional JSON argument)");
const r = JSON.parse(t);
if (!r || typeof r !== "object" || Array.isArray(r)) throw new Error("Headers JSON must be a JSON object");
const a = filterStringRecord(r) ?? {};
Reflect.deleteProperty(a, "__proto__");
const o = i?.browserProfile;
const s = await callBrowserRequest(i, {
method: "POST",
path: "/set/headers",
query: o ? { profile: o } : void 0,
body: {
headers: a,
targetId: normalizeOptionalString(n.targetId)
}
});
if (printBrowserJsonResult(i, s)) return;
defaultRuntime.log("headers set");
});
});
i.command("credentials").description("Set HTTP basic auth credentials").option("--clear", "Clear credentials", false).argument("[username]", "Username").argument("[password]", "Password").option("--target-id <id>", BROWSER_TAB_REFERENCE_HELP).action(async (e, n, r, i) => {
const a = t(i);
await runBrowserCliRequest({
parent: a,
path: "/set/credentials",
body: {
username: normalizeOptionalString(e),
password: n,
clear: Boolean(r.clear),
targetId: normalizeOptionalString(r.targetId)
},
successMessage: r.clear ? "credentials cleared" : "credentials set"
});
});
i.command("geo").description("Set geolocation (and grant permission)").option("--clear", "Clear geolocation + permissions", false).argument("[latitude]", "Latitude").argument("[longitude]", "Longitude").option("--accuracy <m>", "Accuracy in meters").option("--origin <origin>", "Origin to grant permissions for").option("--target-id <id>", BROWSER_TAB_REFERENCE_HELP).action(async (e, n, r, i) => {
const a = t(i);
const o = parseFiniteNumberOption(e, "latitude");
const s = parseFiniteNumberOption(n, "longitude");
const c = parseFiniteNumberOption(r.accuracy, "--accuracy");
await runBrowserCliRequest({
parent: a,
path: "/set/geolocation",
body: {
latitude: o,
longitude: s,
accuracy: c,
origin: normalizeOptionalString(r.origin),
clear: Boolean(r.clear),
targetId: normalizeOptionalString(r.targetId)
},
successMessage: r.clear ? "geolocation cleared" : "geolocation set"
});
});
registerParsedSetting("media");
for (const [e, n, r, a] of [
[
"timezone",
"Override timezone (CDP)",
"timezoneId",
"Timezone ID (e.g. America/New_York)"
],
[
"locale",
"Override locale (CDP)",
"locale",
"Locale (e.g. en-US)"
],
[
"device",
"Apply a Playwright device descriptor (e.g. \"iPhone 14\")",
"name",
"Device name (Playwright devices)"
]
]) i.command(e).description(n).argument(`<${r}>`, a).option("--target-id <id>", BROWSER_TAB_REFERENCE_HELP).action(async (n, i, a) => {
await runBrowserCliRequest({
parent: t(a),
path: `/set/${e}`,
body: {
[r]: n,
targetId: normalizeOptionalString(i.targetId)
},
successMessage: `${e}: ${n}`
});
});
}
//#endregion
export { registerBrowserStateCommands };