|
| 1 | +/** Browser tool lifecycle and host-local profile discovery/import actions. */ |
| 2 | +import type { AgentToolResult } from "openclaw/plugin-sdk/agent-core"; |
| 3 | +import type { BrowserProxyRequest } from "./browser-node-proxy.js"; |
| 4 | +import { resolveBrowserBaseUrl } from "./browser-tool.routing.js"; |
| 5 | +import { |
| 6 | + browserDoctor, |
| 7 | + browserImportProfile, |
| 8 | + browserProfiles, |
| 9 | + browserSystemProfiles, |
| 10 | + browserStart, |
| 11 | + browserStatus, |
| 12 | + browserStop, |
| 13 | + jsonResult, |
| 14 | + normalizeOptionalString, |
| 15 | +} from "./browser-tool.runtime.js"; |
| 16 | +import { parseSystemProfileDomains } from "./browser/system-profile-domains.js"; |
| 17 | + |
| 18 | +const unavailableSystemProfiles = (unavailableReason: string) => ({ |
| 19 | + profiles: [], |
| 20 | + unavailableReason, |
| 21 | +}); |
| 22 | + |
| 23 | +/** |
| 24 | + * Read importable system profiles from the host control server. Discovery must |
| 25 | + * match where import runs (host-local), so it never uses a node proxy or the |
| 26 | + * sandbox base URL. Other profile sources remain useful when host discovery |
| 27 | + * is unavailable, so failures become an explicit degradation fact. |
| 28 | + */ |
| 29 | +async function readHostSystemProfiles(params: { |
| 30 | + allowHostControl?: boolean; |
| 31 | + sandboxBridgeUrl?: string; |
| 32 | + timeoutMs?: number; |
| 33 | + signal?: AbortSignal; |
| 34 | +}) { |
| 35 | + if (params.allowHostControl === false) { |
| 36 | + return unavailableSystemProfiles( |
| 37 | + "Host system profile discovery is disabled by sandbox policy; enable host control to discover importable system profiles.", |
| 38 | + ); |
| 39 | + } |
| 40 | + let hostBaseUrl: string | undefined; |
| 41 | + try { |
| 42 | + hostBaseUrl = resolveBrowserBaseUrl({ |
| 43 | + target: "host", |
| 44 | + sandboxBridgeUrl: params.sandboxBridgeUrl, |
| 45 | + allowHostControl: params.allowHostControl, |
| 46 | + }); |
| 47 | + } catch { |
| 48 | + return unavailableSystemProfiles( |
| 49 | + 'Host browser control is unavailable; enable it and retry action=profiles target="host".', |
| 50 | + ); |
| 51 | + } |
| 52 | + try { |
| 53 | + return { |
| 54 | + profiles: await browserSystemProfiles(hostBaseUrl, { |
| 55 | + timeoutMs: params.timeoutMs, |
| 56 | + signal: params.signal, |
| 57 | + }), |
| 58 | + unavailableReason: undefined, |
| 59 | + }; |
| 60 | + } catch { |
| 61 | + params.signal?.throwIfAborted(); |
| 62 | + return unavailableSystemProfiles( |
| 63 | + 'Host system profile discovery failed; retry action=profiles target="host" after host browser control is available.', |
| 64 | + ); |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +export async function executeBrowserLifecycleAction({ |
| 69 | + action, |
| 70 | + input: params, |
| 71 | + baseUrl, |
| 72 | + profile, |
| 73 | + timeoutMs: toolTimeoutMs, |
| 74 | + proxyRequest, |
| 75 | + allowHostControl, |
| 76 | + sandboxBridgeUrl, |
| 77 | + signal, |
| 78 | +}: { |
| 79 | + action: "doctor" | "status" | "start" | "stop" | "profiles" | "importprofile"; |
| 80 | + input: Record<string, unknown>; |
| 81 | + baseUrl?: string; |
| 82 | + profile?: string; |
| 83 | + timeoutMs?: number; |
| 84 | + proxyRequest: BrowserProxyRequest | null; |
| 85 | + allowHostControl?: boolean; |
| 86 | + sandboxBridgeUrl?: string; |
| 87 | + signal?: AbortSignal; |
| 88 | +}): Promise<AgentToolResult<unknown>> { |
| 89 | + const readBrowserStatus = async () => |
| 90 | + proxyRequest |
| 91 | + ? await proxyRequest({ |
| 92 | + method: "GET", |
| 93 | + path: "/", |
| 94 | + profile, |
| 95 | + timeoutMs: toolTimeoutMs, |
| 96 | + }) |
| 97 | + : await browserStatus(baseUrl, { |
| 98 | + profile, |
| 99 | + timeoutMs: toolTimeoutMs, |
| 100 | + signal, |
| 101 | + }); |
| 102 | + switch (action) { |
| 103 | + case "doctor": |
| 104 | + return jsonResult( |
| 105 | + proxyRequest |
| 106 | + ? await proxyRequest({ method: "GET", path: "/doctor", profile }) |
| 107 | + : await browserDoctor(baseUrl, { profile, signal }), |
| 108 | + ); |
| 109 | + case "status": |
| 110 | + return jsonResult(await readBrowserStatus()); |
| 111 | + case "start": |
| 112 | + case "stop": { |
| 113 | + if (proxyRequest) { |
| 114 | + await proxyRequest({ |
| 115 | + method: "POST", |
| 116 | + path: `/${action}`, |
| 117 | + profile, |
| 118 | + timeoutMs: toolTimeoutMs, |
| 119 | + }); |
| 120 | + } else { |
| 121 | + const updateBrowser = action === "start" ? browserStart : browserStop; |
| 122 | + await updateBrowser(baseUrl, { profile, timeoutMs: toolTimeoutMs, signal }); |
| 123 | + } |
| 124 | + return jsonResult(await readBrowserStatus()); |
| 125 | + } |
| 126 | + case "profiles": { |
| 127 | + // Importable system profiles are host-local (import runs on the host), |
| 128 | + // so read them from the host regardless of the profiles action target; |
| 129 | + // never let a node proxy or sandbox describe the wrong Chrome profiles. |
| 130 | + const { profiles: systemProfiles, unavailableReason: systemProfilesUnavailable } = |
| 131 | + await readHostSystemProfiles({ |
| 132 | + allowHostControl, |
| 133 | + sandboxBridgeUrl, |
| 134 | + timeoutMs: toolTimeoutMs, |
| 135 | + signal, |
| 136 | + }); |
| 137 | + if (proxyRequest) { |
| 138 | + const result = await proxyRequest({ |
| 139 | + method: "GET", |
| 140 | + path: "/profiles", |
| 141 | + timeoutMs: toolTimeoutMs, |
| 142 | + }); |
| 143 | + return jsonResult({ |
| 144 | + ...(result && typeof result === "object" ? result : { profiles: result }), |
| 145 | + systemProfiles, |
| 146 | + ...(systemProfilesUnavailable ? { systemProfilesUnavailable } : {}), |
| 147 | + }); |
| 148 | + } |
| 149 | + return jsonResult({ |
| 150 | + profiles: await browserProfiles(baseUrl, { |
| 151 | + timeoutMs: toolTimeoutMs, |
| 152 | + signal, |
| 153 | + }), |
| 154 | + systemProfiles, |
| 155 | + ...(systemProfilesUnavailable ? { systemProfilesUnavailable } : {}), |
| 156 | + }); |
| 157 | + } |
| 158 | + case "importprofile": { |
| 159 | + if (proxyRequest) { |
| 160 | + throw new Error("system profile import must run on the browser host"); |
| 161 | + } |
| 162 | + const domains = parseSystemProfileDomains(params.domains); |
| 163 | + return jsonResult( |
| 164 | + await browserImportProfile(baseUrl, { |
| 165 | + browser: normalizeOptionalString(params.browser) ?? "chrome", |
| 166 | + systemProfile: normalizeOptionalString(params.systemProfile) ?? "Default", |
| 167 | + into: normalizeOptionalString(params.into) ?? "imported", |
| 168 | + domains, |
| 169 | + signal, |
| 170 | + }), |
| 171 | + ); |
| 172 | + } |
| 173 | + default: |
| 174 | + throw new Error(`Unknown action: ${String(action)}`); |
| 175 | + } |
| 176 | +} |
0 commit comments