Files
openclaw/ui/vite.config.ts
Alix-007 f5dd33c975 fix(control-ui): make Control UI bootstrap config endpoint base-path-relative (#66946) (#91305)
* fix(control-ui): make bootstrap config endpoint base-path-relative (#66946)

CONTROL_UI_BOOTSTRAP_CONFIG_PATH embedded a hard-coded /__openclaw prefix
instead of being base-path-relative. When the Control UI is served under
/__openclaw__/, both the gateway and the browser loader compose
${basePath}${CONTROL_UI_BOOTSTRAP_CONFIG_PATH}, producing the doubled
/__openclaw__/__openclaw/control-ui-config.json URL that 404s.

Make the constant base-path-relative (/control-ui-config.json) so the
composed URL is correct under any base path, align the Vite dev stub and
the docs, and add gateway.controlUi.basePath "/__openclaw__" coverage.

* fix(control-ui): serve bootstrap config at default __openclaw__ entry (#66946)

The reporter runs the default deployment (no gateway.controlUi.basePath),
so the Control UI SPA is mounted under the default /__openclaw__/ namespace.
A browser opening that entry infers basePath="/__openclaw__" from the URL
(inferBasePathFromPathname) and fetches /__openclaw__/control-ui-config.json,
but an empty-base-path gateway only served the bare /control-ui-config.json,
so the default-entry bootstrap request 404'd and chat never finished loading.

Make handleControlUiHttpRequest also accept the default-namespace alias
/__openclaw__/control-ui-config.json when no base path is configured. The
alias is derived from the existing CONTROL_UI_NAMESPACE_PREFIX mount constant
and is purely additive: the bare /control-ui-config.json endpoint and the
configured-base-path endpoint are both preserved (no route removed).

Add gateway HTTP coverage for the real default-entry scenario (empty base
path + /__openclaw__/... request) that fails without the alias, alongside the
configured-base-path, bare-path compatibility, and doubled-path 404 cases.

* fix(control-ui): preserve legacy bootstrap endpoint as compat alias (#66946)

Current main and v2026.6.1 serve and document the single-underscore
/__openclaw/control-ui-config.json bootstrap endpoint under an empty
base path (that literal was CONTROL_UI_BOOTSTRAP_CONFIG_PATH before the
path was made base-path-relative). Making the constant relative dropped
that match, so older bundles and clients hitting the documented endpoint
would 404 after upgrading.

Accept the legacy single-underscore path as an empty-base-path
compatibility alias in matchesControlUiBootstrapConfigPath, derived from
the legacy /__openclaw namespace joined with the canonical config
constant (so it tracks any filename rename) and named
LEGACY_BOOTSTRAP_CONFIG_PATH with a comment. The canonical
/control-ui-config.json and the default-namespace
/__openclaw__/control-ui-config.json aliases are unchanged; only this
path is added. The doubled /__openclaw__/__openclaw/... path still 404s.

Add a focused regression that the legacy endpoint returns config under an
empty base path; it 404s without the alias (verified non-vacuous).

* fix(control-ui): preserve legacy bootstrap route under configured base path (#66946)

The previous revision preserved the single-underscore
/__openclaw/control-ui-config.json bootstrap endpoint only under an empty
base path. A deployment with a configured gateway.controlUi.basePath
(e.g. /x) served and documented that endpoint at
${basePath}/__openclaw/control-ui-config.json before this PR made the
config path base-path-relative, so configured-base-path users, older
bundles, and clients that still request it would 404 after upgrading.

Extend matchesControlUiBootstrapConfigPath so the legacy single-underscore
suffix is accepted under every base path, not just the empty one. The
matcher now checks the canonical and legacy suffixes uniformly as
${basePath}${CONTROL_UI_BOOTSTRAP_CONFIG_PATH} and
${basePath}${LEGACY_BOOTSTRAP_CONFIG_PATH} for both the empty and
configured cases, reusing the existing LEGACY_BOOTSTRAP_CONFIG_PATH
constant (no new hard-coded literal). The default-namespace
/__openclaw__/control-ui-config.json alias stays empty-base-path-only
(it is the path the inferred default entry requests when no base path is
configured). All three empty-base-path behaviors are unchanged; the
doubled /__openclaw__/__openclaw/... path still 404s under both an empty
and a configured base path.

Add a focused regression that the configured-base-path legacy endpoint
returns the bootstrap config; it 404s without the alias (verified
non-vacuous). No CHANGELOG.md change.

* fix(ui): mount config stub under vite base

* fix(ui): preserve default config stub route

---------

Co-authored-by: Vincent Koc <vincentkoc@ieee.org>
2026-06-11 08:49:09 +09:00

292 lines
9.0 KiB
TypeScript

// Control UI config module wires vite behavior.
import { execFileSync } from "node:child_process";
import fs from "node:fs";
import { createRequire } from "node:module";
import path from "node:path";
import { fileURLToPath } from "node:url";
import type { Plugin, UserConfig } from "vite";
import { controlUiManualChunk } from "./config/control-ui-chunking.ts";
const here = path.dirname(fileURLToPath(import.meta.url));
const repoRoot = path.resolve(here, "..");
const outDir = path.resolve(here, "../dist/control-ui");
const require = createRequire(import.meta.url);
const json5EsmPath = require.resolve("json5/dist/index.mjs");
type ControlUiViteAlias = {
find: string | RegExp;
replacement: string;
};
const commonJsOptimizeDeps = [
"highlight.js/lib/core",
"highlight.js/lib/languages/bash",
"highlight.js/lib/languages/cpp",
"highlight.js/lib/languages/css",
"highlight.js/lib/languages/diff",
"highlight.js/lib/languages/go",
"highlight.js/lib/languages/java",
"highlight.js/lib/languages/javascript",
"highlight.js/lib/languages/json",
"highlight.js/lib/languages/markdown",
"highlight.js/lib/languages/python",
"highlight.js/lib/languages/rust",
"highlight.js/lib/languages/typescript",
"highlight.js/lib/languages/xml",
"highlight.js/lib/languages/yaml",
] as const;
function normalizeBase(input: string): string {
const trimmed = input.trim();
if (!trimmed) {
return "/";
}
if (trimmed === "./") {
return "./";
}
if (trimmed.endsWith("/")) {
return trimmed;
}
return `${trimmed}/`;
}
function normalizeBuildId(input: string): string {
const normalized = input.trim().replace(/[^a-zA-Z0-9._-]+/g, "-");
return normalized.slice(0, 96) || "dev";
}
function readPackageVersion(): string {
try {
const raw = fs.readFileSync(path.join(repoRoot, "package.json"), "utf8");
const parsed = JSON.parse(raw) as { version?: unknown };
return typeof parsed.version === "string" && parsed.version.trim()
? parsed.version.trim()
: "dev";
} catch {
return "dev";
}
}
function readGitShortSha(): string | null {
try {
const raw = execFileSync("git", ["-C", repoRoot, "rev-parse", "--short=12", "HEAD"], {
encoding: "utf8",
stdio: ["ignore", "pipe", "ignore"],
});
return raw.trim() || null;
} catch {
return null;
}
}
function resolveControlUiBuildId(): string {
const explicit =
process.env.OPENCLAW_CONTROL_UI_BUILD_ID?.trim() || process.env.OPENCLAW_VERSION?.trim();
if (explicit) {
return normalizeBuildId(explicit);
}
const version = readPackageVersion();
const gitSha = readGitShortSha();
return normalizeBuildId(gitSha ? `${version}-${gitSha}` : version);
}
function escapeRegExp(input: string): string {
return input.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}
function sortTsconfigPathEntries(entries: Array<[string, unknown]>): Array<[string, unknown]> {
return entries.toSorted(([left], [right]) => {
const leftPrefixLength = left.includes("*") ? left.indexOf("*") : left.length;
const rightPrefixLength = right.includes("*") ? right.indexOf("*") : right.length;
if (leftPrefixLength !== rightPrefixLength) {
return rightPrefixLength - leftPrefixLength;
}
return right.length - left.length || left.localeCompare(right);
});
}
function resolveTsconfigTargetPath(target: string): string {
return path.resolve(repoRoot, target.replace(/^\.\//, ""));
}
function resolveTsconfigPathAlias(key: string, target: string): ControlUiViteAlias | null {
const keyWildcardIndex = key.indexOf("*");
const targetWildcardIndex = target.indexOf("*");
if (keyWildcardIndex === -1 || targetWildcardIndex === -1) {
if (keyWildcardIndex !== -1 || targetWildcardIndex !== -1) {
return null;
}
return {
find: key,
replacement: resolveTsconfigTargetPath(target),
};
}
if (
key.slice(keyWildcardIndex + 1).includes("*") ||
target.slice(targetWildcardIndex + 1).includes("*")
) {
return null;
}
const prefix = key.slice(0, keyWildcardIndex);
const suffix = key.slice(keyWildcardIndex + 1);
return {
find: new RegExp(`^${escapeRegExp(prefix)}(.+)${escapeRegExp(suffix)}$`),
replacement: resolveTsconfigTargetPath(target).replace("*", "$1"),
};
}
function sourcePackageAlias(packageId: string, subpath?: string): ControlUiViteAlias {
return {
find: `@openclaw/${packageId}${subpath ? `/${subpath}` : ""}`,
replacement: path.join(
repoRoot,
"packages",
packageId,
"src",
...(subpath ? subpath.split("/") : ["index"]).map((part, index, parts) =>
index === parts.length - 1 ? `${part}.ts` : part,
),
),
};
}
export function resolveSourcePackageAliasesForVite(): ControlUiViteAlias[] {
return [
sourcePackageAlias("normalization-core", "number-coercion"),
sourcePackageAlias("normalization-core", "record-coerce"),
sourcePackageAlias("normalization-core", "string-coerce"),
sourcePackageAlias("normalization-core", "string-normalization"),
sourcePackageAlias("normalization-core"),
];
}
export function resolveTsconfigPathAliasesForVite(): ControlUiViteAlias[] {
const raw = fs.readFileSync(path.join(repoRoot, "tsconfig.json"), "utf8");
const parsed = JSON.parse(raw) as {
compilerOptions?: { paths?: Record<string, unknown> };
};
const paths = parsed.compilerOptions?.paths;
if (!paths) {
return [];
}
return sortTsconfigPathEntries(Object.entries(paths)).flatMap(([key, targets]) => {
if (!Array.isArray(targets) || typeof targets[0] !== "string") {
return [];
}
const alias = resolveTsconfigPathAlias(key, targets[0]);
return alias ? [alias] : [];
});
}
function normalizeViteImporterPath(importer: string): string {
return path.normalize(importer.replace(/[?#].*$/u, ""));
}
export function controlUiBrowserOnlySharedModuleAliases(): Plugin {
const browserRedactPath = path.join(here, "src/ui/browser-redact.ts");
const sharedRedactImporters = new Set([
path.join(repoRoot, "src/agents/tool-display-common.ts"),
path.join(repoRoot, "src/agents/tool-display-exec.ts"),
path.join(repoRoot, "src/agents/tool-display.ts"),
]);
return {
name: "control-ui-browser-only-shared-module-aliases",
enforce: "pre",
resolveId(source, importer) {
if (
source === "../logging/redact.js" &&
importer &&
sharedRedactImporters.has(normalizeViteImporterPath(importer))
) {
return browserRedactPath;
}
return null;
},
};
}
function controlUiServiceWorkerBuildIdPlugin(buildId: string): Plugin {
return {
name: "control-ui-service-worker-build-id",
apply: "build",
closeBundle() {
const swPath = path.join(outDir, "sw.js");
const publicSwPath = path.join(here, "public/sw.js");
const source = fs.readFileSync(fs.existsSync(swPath) ? swPath : publicSwPath, "utf8");
const placeholder = '"__OPENCLAW_CONTROL_UI_BUILD_ID__"';
const updated = source.replace(placeholder, JSON.stringify(buildId));
if (updated === source) {
throw new Error(`Control UI service worker build id placeholder missing in ${swPath}`);
}
fs.mkdirSync(outDir, { recursive: true });
fs.writeFileSync(swPath, updated);
},
};
}
export default function controlUiViteConfig(): UserConfig {
const envBase = process.env.OPENCLAW_CONTROL_UI_BASE_PATH?.trim();
const base = envBase ? normalizeBase(envBase) : "./";
const bootstrapConfigPath = base === "./" ? "/control-ui-config.json" : `${base}control-ui-config.json`;
const controlUiBuildId = resolveControlUiBuildId();
return {
base,
define: {
OPENCLAW_CONTROL_UI_BUILD_ID: JSON.stringify(controlUiBuildId),
},
publicDir: path.resolve(here, "public"),
optimizeDeps: {
include: [
"ipaddr.js",
"lit/directives/repeat.js",
"markdown-it-task-lists",
...commonJsOptimizeDeps,
],
},
resolve: {
alias: [
{ find: "json5", replacement: json5EsmPath },
...resolveSourcePackageAliasesForVite(),
...resolveTsconfigPathAliasesForVite(),
],
},
build: {
outDir,
emptyOutDir: true,
sourcemap: true,
rollupOptions: {
output: {
manualChunks: controlUiManualChunk,
},
},
// Keep CI/onboard logs clean; the app chunk is split into stable runtime buckets above.
chunkSizeWarningLimit: 1024,
},
server: {
host: true,
port: 5173,
strictPort: true,
},
plugins: [
controlUiBrowserOnlySharedModuleAliases(),
controlUiServiceWorkerBuildIdPlugin(controlUiBuildId),
{
name: "control-ui-dev-stubs",
configureServer(server) {
server.middlewares.use(bootstrapConfigPath, (_req, res) => {
res.setHeader("Content-Type", "application/json");
res.end(
JSON.stringify({
basePath: "/",
assistantName: "",
assistantAvatar: "",
}),
);
});
},
},
],
};
}