UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

110 lines (109 loc) • 3.77 kB
import { t as formatErrorMessage } from "./errors-BKzCeTYb.mjs"; import { t as mutateConfigFile } from "./mutate-DhAOi0-2.mjs"; import "./security-runtime-C-zE_Zy_.mjs"; import { i as resolveBrowserConfig, n as getOwnBrowserProfile } from "./config-ccOD-Wrr.mjs"; import { a as BrowserConflictError, p as BrowserValidationError, u as BrowserResourceExhaustedError } from "./errors-C9QQGqVs.mjs"; import { n as getUsedPorts, t as allocateCdpPort } from "./profiles-DzbsR8ma.mjs"; import "./config-mutation-CGDddIEg.mjs"; import { n as assertCdpEndpointAllowed } from "./cdp.helpers-Bxe-G5Kr.mjs"; import { isDeepStrictEqual } from "node:util"; //#region extensions/browser/src/browser/config-mutations.ts async function persistBrowserControlCredential(e) { await mutateConfigFile({ afterWrite: { mode: "auto" }, mutate: (t) => { t.gateway = { ...t.gateway, auth: { ...t.gateway?.auth, [e.kind]: e.value } }; } }); } async function createBrowserProfileConfig(e) { return (await mutateConfigFile({ afterWrite: { mode: "auto" }, mutate: async (t) => { const n = t.gateway?.port !== void 0; const r = resolveBrowserConfig({ ...e.resolved, ...t.browser, profiles: t.browser?.profiles ?? e.resolved.profiles }, t); const i = resolveBrowserConfig(t.browser, t); const a = n ? i : r; const o = t.browser?.profiles ?? {}; if (getOwnBrowserProfile(o, e.name) || getOwnBrowserProfile(a.profiles, e.name)) throw new BrowserConflictError(`profile "${e.name}" already exists`); let s; if (e.parsedCdpUrl) { try { await assertCdpEndpointAllowed(e.parsedCdpUrl, r.ssrfPolicy); } catch (e) { throw new BrowserValidationError(formatErrorMessage(e)); } s = { cdpUrl: e.parsedCdpUrl, ...e.driver ? { driver: e.driver } : {}, ...e.driver === "existing-session" ? { attachOnly: true } : {} }; } else if (e.driver === "existing-session") s = { driver: e.driver, attachOnly: true, ...e.userDataDir ? { userDataDir: e.userDataDir } : {} }; else { const t = getUsedPorts(a.profiles); const r = n ? i : e.resolved; const o = await allocateCdpPort(t, { start: r.cdpPortRangeStart, end: r.cdpPortRangeEnd }); if (o === null) throw new BrowserResourceExhaustedError("no available CDP ports in range"); s = { cdpPort: o, ...e.driver ? { driver: e.driver } : {} }; } t.browser = { ...t.browser, profiles: { ...t.browser?.profiles, [e.name]: s } }; return s; } })).result; } /** Delete the exact persisted browser profile definition captured by the caller. */ async function deleteBrowserProfileConfig(e) { await mutateConfigFile({ afterWrite: { mode: "auto" }, mutate: (t) => { if (t.browser?.defaultProfile === e.name) throw new BrowserValidationError(`cannot delete the default profile "${e.name}"; change browser.defaultProfile first`); const n = getOwnBrowserProfile(t.browser?.profiles, e.name); if (!isDeepStrictEqual(n, e.expected)) throw new BrowserConflictError(`profile "${e.name}" changed while deletion was pending; retry the delete request`); const { [e.name]: r, ...i } = t.browser?.profiles ?? {}; t.browser = { ...t.browser, profiles: i }; } }); } async function setDefaultBrowserProfile(e) { await mutateConfigFile({ afterWrite: { mode: "auto" }, mutate: (t) => { if (!getOwnBrowserProfile(t.browser?.profiles, e)) throw new BrowserValidationError(`profile "${e}" does not exist`); t.browser = { ...t.browser, defaultProfile: e }; } }); } //#endregion export { setDefaultBrowserProfile as i, deleteBrowserProfileConfig as n, persistBrowserControlCredential as r, createBrowserProfileConfig as t };