Files
openclaw/extensions/browser/src/sdk-config.ts
2026-05-01 18:17:29 +01:00

98 lines
2.9 KiB
TypeScript

import { normalizeOptionalLowercaseString } from "openclaw/plugin-sdk/text-runtime";
export {
getRuntimeConfig,
getRuntimeConfigSnapshot,
getRuntimeConfigSourceSnapshot,
} from "openclaw/plugin-sdk/runtime-config-snapshot";
export { replaceConfigFile } from "openclaw/plugin-sdk/config-mutation";
export {
type BrowserConfig,
type BrowserProfileConfig,
type OpenClawConfig,
} from "openclaw/plugin-sdk/config-types";
export {
normalizePluginsConfig,
resolveEffectiveEnableState,
} from "openclaw/plugin-sdk/plugin-config-runtime";
export { resolveGatewayPort } from "openclaw/plugin-sdk/core";
export {
CONFIG_DIR,
escapeRegExp,
resolveUserPath,
shortenHomePath,
} from "openclaw/plugin-sdk/text-runtime";
type PortRange = { start: number; end: number };
const DEFAULT_BROWSER_CDP_PORT_RANGE_START = 18800;
const DEFAULT_BROWSER_CDP_PORT_RANGE_END = 18899;
const DEFAULT_BROWSER_CDP_PORT_RANGE_SPAN =
DEFAULT_BROWSER_CDP_PORT_RANGE_END - DEFAULT_BROWSER_CDP_PORT_RANGE_START;
export const DEFAULT_BROWSER_CONTROL_PORT = 18791;
function isValidPort(port: number): boolean {
return Number.isFinite(port) && port > 0 && port <= 65535;
}
function clampPort(port: number, fallback: number): number {
return isValidPort(port) ? port : fallback;
}
function derivePort(base: number, offset: number, fallback: number): number {
return clampPort(base + offset, fallback);
}
export function deriveDefaultBrowserControlPort(gatewayPort: number): number {
return derivePort(gatewayPort, 2, DEFAULT_BROWSER_CONTROL_PORT);
}
export function deriveDefaultBrowserCdpPortRange(browserControlPort: number): PortRange {
const start = derivePort(browserControlPort, 9, DEFAULT_BROWSER_CDP_PORT_RANGE_START);
const end = start + DEFAULT_BROWSER_CDP_PORT_RANGE_SPAN;
if (end <= 65535) {
return { start, end };
}
return {
start: DEFAULT_BROWSER_CDP_PORT_RANGE_START,
end: DEFAULT_BROWSER_CDP_PORT_RANGE_END,
};
}
type BooleanParseOptions = {
truthy?: string[];
falsy?: string[];
};
const DEFAULT_TRUTHY = ["true", "1", "yes", "on"] as const;
const DEFAULT_FALSY = ["false", "0", "no", "off"] as const;
const DEFAULT_TRUTHY_SET = new Set<string>(DEFAULT_TRUTHY);
const DEFAULT_FALSY_SET = new Set<string>(DEFAULT_FALSY);
export function parseBooleanValue(
value: unknown,
options: BooleanParseOptions = {},
): boolean | undefined {
if (typeof value === "boolean") {
return value;
}
if (typeof value !== "string") {
return undefined;
}
const normalized = normalizeOptionalLowercaseString(value);
if (!normalized) {
return undefined;
}
const truthy = options.truthy ?? DEFAULT_TRUTHY;
const falsy = options.falsy ?? DEFAULT_FALSY;
const truthySet = truthy === DEFAULT_TRUTHY ? DEFAULT_TRUTHY_SET : new Set(truthy);
const falsySet = falsy === DEFAULT_FALSY ? DEFAULT_FALSY_SET : new Set(falsy);
if (truthySet.has(normalized)) {
return true;
}
if (falsySet.has(normalized)) {
return false;
}
return undefined;
}