Skip to content

Commit 9d15544

Browse files
committed
fix: use configured Control UI base path at startup
1 parent 2508fb4 commit 9d15544

8 files changed

Lines changed: 58 additions & 55 deletions

File tree

‎src/gateway/control-ui-contract.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
/** HTTP path for the Control UI bootstrap config payload. */
44
export const CONTROL_UI_BOOTSTRAP_CONFIG_PATH = "/control-ui-config.json";
55

6+
/** Carries the gateway-configured Control UI mount path into browser bootstrap. */
7+
export const CONTROL_UI_BASE_PATH_ATTRIBUTE = "data-openclaw-control-ui-base-path";
8+
69
/** Marks whether the served document CSP permits the terminal WASM runtime. */
710
export const CONTROL_UI_TERMINAL_ENABLED_ATTRIBUTE = "data-openclaw-terminal-enabled";
811

‎src/gateway/control-ui.http.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -923,6 +923,7 @@ describe("handleControlUiHttpRequest", () => {
923923
);
924924
expect(handled).toBe(true);
925925
const body = String(end.mock.calls[0]?.[0] ?? "");
926+
expect(body).toContain('data-openclaw-control-ui-base-path="/openclaw"');
926927
expect(body).toContain('href="/openclaw/manifest.webmanifest"');
927928
expect(body).toContain('href="/openclaw/favicon.svg"');
928929
expect(body).not.toContain('href="/manifest.webmanifest"');

‎src/gateway/control-ui.ts‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import {
3939
} from "./auth-rate-limit.js";
4040
import { authorizeHttpGatewayConnect, type ResolvedGatewayAuth } from "./auth.js";
4141
import {
42+
CONTROL_UI_BASE_PATH_ATTRIBUTE,
4243
CONTROL_UI_BOOTSTRAP_CONFIG_PATH,
4344
CONTROL_UI_TERMINAL_ENABLED_ATTRIBUTE,
4445
type ControlUiBootstrapConfig,
@@ -187,6 +188,15 @@ function rewriteControlUiIndexHtmlPublicAssetHrefs(html: string, basePath: strin
187188
return next;
188189
}
189190

191+
function escapeHtmlAttribute(value: string): string {
192+
return value
193+
.replaceAll("&", "&")
194+
.replaceAll('"', """)
195+
.replaceAll("<", "&lt;")
196+
.replaceAll(">", "&gt;")
197+
.replaceAll("'", "&#39;");
198+
}
199+
190200
type ControlUiAvatarResolution =
191201
| { kind: "none"; reason: string; source?: string | null }
192202
| { kind: "local"; filePath: string; source?: string | null }
@@ -788,12 +798,16 @@ function serveResolvedIndexHtml(
788798
basePath?: string,
789799
allowWasm?: boolean,
790800
) {
791-
const withBasePath = rewriteControlUiIndexHtmlPublicAssetHrefs(body, basePath ?? "");
801+
const normalizedBasePath = normalizeControlUiBasePath(basePath);
802+
const withBasePath = rewriteControlUiIndexHtmlPublicAssetHrefs(body, normalizedBasePath);
803+
const basePathAttribute = normalizedBasePath
804+
? ` ${CONTROL_UI_BASE_PATH_ATTRIBUTE}="${escapeHtmlAttribute(normalizedBasePath)}"`
805+
: "";
792806
// Let the app initialize fail-closed without guessing whether this document
793807
// was served with the terminal's WASM CSP allowance.
794808
const prepared = withBasePath.replace(
795809
/<html\b/i,
796-
`<html ${CONTROL_UI_TERMINAL_ENABLED_ATTRIBUTE}="${allowWasm === true}"`,
810+
`<html${basePathAttribute} ${CONTROL_UI_TERMINAL_ENABLED_ATTRIBUTE}="${allowWasm === true}"`,
797811
);
798812
const hashes = computeInlineScriptHashes(prepared);
799813
// Always set the document CSP here (the index carries inline scripts) so the

‎ui/src/app/bootstrap.ts‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ import type { RouteLocation } from "@openclaw/uirouter";
22
import type { GatewayBrowserClient } from "../api/gateway.ts";
33
import {
44
createApplicationRouter,
5-
inferBasePathFromPathname,
65
locationForRoute,
7-
normalizeBasePath,
86
pathForRoute,
97
routeIdFromPath,
108
startApplicationRouter,
@@ -18,7 +16,7 @@ import { createRuntimeConfigCapability } from "../lib/config/index.ts";
1816
import { createSessionCapability } from "../lib/sessions/index.ts";
1917
import { createWorkboardCapability } from "../lib/workboard/capability.ts";
2018
import { createAgentSelectionCapability } from "./agent-selection.ts";
21-
import { createBrowserHistory } from "./browser.ts";
19+
import { createBrowserHistory, resolveControlUiBasePath } from "./browser.ts";
2220
import { createApplicationConfigCapability } from "./config.ts";
2321
import type {
2422
ApplicationNavigationOptions,
@@ -235,8 +233,8 @@ export function bootstrapApplication(): ApplicationRuntime {
235233
if (startup.changed) {
236234
saveSettings(startup.settings);
237235
}
238-
const basePath = normalizeBasePath(
239-
inferBasePathFromPathname(startup.location.pathname || globalThis.location?.pathname || "/"),
236+
const basePath = resolveControlUiBasePath(
237+
startup.location.pathname || globalThis.location?.pathname || "/",
240238
);
241239
const initialLocation = normalizeInitialApplicationLocation(
242240
startup.location,

‎ui/src/app/browser.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,29 @@
11
import type { RouteLocation, RouterHistory } from "@openclaw/uirouter";
2+
import { CONTROL_UI_BASE_PATH_ATTRIBUTE } from "../../../src/gateway/control-ui-contract.js";
3+
import { inferBasePathFromPathname, normalizeBasePath } from "../app-route-paths.ts";
4+
5+
type WindowWithControlUiBasePath = Window &
6+
typeof globalThis & {
7+
[key: string]: unknown;
8+
};
9+
10+
export function resolveControlUiBasePath(pathname: string): string {
11+
if (typeof window !== "undefined") {
12+
const windowValue = (window as WindowWithControlUiBasePath)[
13+
"__OPENCLAW_CONTROL_UI_BASE_PATH__"
14+
];
15+
if (typeof windowValue === "string") {
16+
return normalizeBasePath(windowValue);
17+
}
18+
}
19+
if (typeof document !== "undefined") {
20+
const documentValue = document.documentElement.getAttribute(CONTROL_UI_BASE_PATH_ATTRIBUTE);
21+
if (documentValue !== null) {
22+
return normalizeBasePath(documentValue);
23+
}
24+
}
25+
return inferBasePathFromPathname(pathname);
26+
}
227

328
function readLocation(): RouteLocation {
429
return {

‎ui/src/app/public-assets.ts‎

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Control UI module implements public assets behavior.
2-
import { inferBasePathFromPathname, normalizeBasePath } from "../app-route-paths.ts";
2+
import { normalizeBasePath } from "../app-route-paths.ts";
3+
import { resolveControlUiBasePath } from "./browser.ts";
34

45
type ControlUiPublicAsset =
56
| "apple-touch-icon.png"
@@ -10,11 +11,6 @@ type ControlUiPublicAsset =
1011
| "sw.js"
1112
| `provider-icons/ProviderIcon-${string}.svg`;
1213

13-
type WindowWithControlUiBasePath = Window &
14-
typeof globalThis & {
15-
[key: string]: unknown;
16-
};
17-
1814
export function controlUiPublicAssetPath(
1915
asset: ControlUiPublicAsset,
2016
basePath: string | null | undefined,
@@ -30,20 +26,9 @@ export function inferControlUiPublicAssetPath(
3026
pathname?: string;
3127
},
3228
): string {
33-
const configured = params?.basePath ?? readConfiguredBasePath();
34-
const inferredBasePath =
35-
configured != null
36-
? configured
37-
: inferBasePathFromPathname(params?.pathname ?? currentPathname());
38-
return controlUiPublicAssetPath(asset, inferredBasePath);
39-
}
40-
41-
function readConfiguredBasePath(): string | null {
42-
if (typeof window === "undefined") {
43-
return null;
44-
}
45-
const value = (window as WindowWithControlUiBasePath)["__OPENCLAW_CONTROL_UI_BASE_PATH__"];
46-
return typeof value === "string" ? value : null;
29+
const basePath =
30+
params?.basePath ?? resolveControlUiBasePath(params?.pathname ?? currentPathname());
31+
return controlUiPublicAssetPath(asset, basePath);
4732
}
4833

4934
function currentPathname(): string {

‎ui/src/app/settings.ts‎

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,6 @@ const LEGACY_TOKEN_SESSION_KEY = "openclaw.control.token.v1";
77
const TOKEN_SESSION_KEY_PREFIX = "openclaw.control.token.v1:";
88
const MAX_SCOPED_SESSION_ENTRIES = 10;
99

10-
type WindowWithControlUiBasePath = Window &
11-
typeof globalThis & {
12-
[key: string]: unknown;
13-
};
14-
1510
function settingsKeyForGateway(gatewayUrl: string): string {
1611
return `${SETTINGS_KEY_PREFIX}${normalizeGatewayTokenScope(gatewayUrl)}`;
1712
}
@@ -37,11 +32,11 @@ import {
3732
normalizeSidebarPinnedRoutes,
3833
type SidebarNavRoute,
3934
} from "../app-navigation.ts";
40-
import { inferBasePathFromPathname, normalizeBasePath } from "../app-route-paths.ts";
4135
import { isSupportedLocale } from "../i18n/index.ts";
4236
import { normalizeOptionalString } from "../lib/string-coerce.ts";
4337
import { getSafeLocalStorage, getSafeSessionStorage } from "../local-storage.ts";
4438
import { normalizeChatSplitLayout, type ChatSplitLayout } from "../pages/chat/split-layout.ts";
39+
import { resolveControlUiBasePath } from "./browser.ts";
4540
import { parseImportedCustomTheme, type ImportedCustomTheme } from "./custom-theme.ts";
4641
import { normalizeGatewayTokenScope } from "./gateway-scope.ts";
4742
import { parseThemeSelection, type ThemeMode, type ThemeName } from "./theme.ts";
@@ -326,14 +321,7 @@ function formatHostWithPort(hostname: string, port: string): string {
326321

327322
function deriveDefaultGatewayUrl(): { pageUrl: string; effectiveUrl: string } {
328323
const proto = location.protocol === "https:" ? "wss" : "ws";
329-
const configured =
330-
typeof window !== "undefined" &&
331-
normalizeOptionalString(
332-
(window as WindowWithControlUiBasePath)["__OPENCLAW_CONTROL_UI_BASE_PATH__"],
333-
);
334-
const basePath = configured
335-
? normalizeBasePath(configured)
336-
: inferBasePathFromPathname(location.pathname);
324+
const basePath = resolveControlUiBasePath(location.pathname);
337325
const pageUrl = `${proto}://${location.host}${basePath}`;
338326
if (!isViteDevPage()) {
339327
return { pageUrl, effectiveUrl: pageUrl };

‎ui/src/components/markdown.ts‎

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,8 @@ import yaml from "highlight.js/lib/languages/yaml";
1818
import MarkdownIt from "markdown-it";
1919
import markdownItTaskLists from "markdown-it-task-lists";
2020
import { stripUnsupportedCitationControlMarkers } from "../../../src/shared/text/citation-control-markers.js";
21-
import {
22-
inferBasePathFromPathname,
23-
normalizeBasePath,
24-
routeIdFromPath,
25-
} from "../app-route-paths.ts";
21+
import { routeIdFromPath } from "../app-route-paths.ts";
22+
import { resolveControlUiBasePath } from "../app/browser.ts";
2623
import { i18n, t } from "../i18n/index.ts";
2724
import { copyToClipboard } from "../lib/clipboard.ts";
2825
import { truncateText } from "../lib/format.ts";
@@ -396,10 +393,6 @@ const APP_RESOURCE_PATH_PREFIXES = [
396393
["plugins", "diffs"],
397394
["plugins", "diffs-language-pack"],
398395
];
399-
type WindowWithControlUiBasePath = Window &
400-
typeof globalThis & {
401-
[key: string]: unknown;
402-
};
403396
const markdownCache = new Map<string, string>();
404397
const TAIL_LINK_BLUR_CLASS = "chat-link-tail-blur";
405398
const FENCE_OPEN_RE = /^[ \t]{0,3}(`{3,}|~{3,})/;
@@ -559,11 +552,7 @@ function currentControlUiBasePath(): string {
559552
if (typeof window === "undefined") {
560553
return "";
561554
}
562-
const configured = (window as WindowWithControlUiBasePath)["__OPENCLAW_CONTROL_UI_BASE_PATH__"];
563-
if (typeof configured === "string") {
564-
return normalizeBasePath(configured);
565-
}
566-
return inferBasePathFromPathname(window.location.pathname);
555+
return resolveControlUiBasePath(window.location.pathname);
567556
}
568557

569558
function pathSegments(pathname: string): string[] {

0 commit comments

Comments
 (0)