mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-19 07:37:27 +00:00
fix: resolve bundled chrome extension assets (#8914) (thanks @kelvinCB)
This commit is contained in:
@@ -23,6 +23,7 @@ Docs: https://docs.openclaw.ai
|
|||||||
|
|
||||||
### Fixes
|
### Fixes
|
||||||
|
|
||||||
|
- CLI: resolve bundled Chrome extension assets by walking up to the nearest assets directory; add resolver and clipboard tests. (#8914) Thanks @kelvinCB.
|
||||||
- Heartbeat: allow explicit accountId routing for multi-account channels. (#8702) Thanks @lsh411.
|
- Heartbeat: allow explicit accountId routing for multi-account channels. (#8702) Thanks @lsh411.
|
||||||
- TUI/Gateway: handle non-streaming finals, refresh history for non-local chat runs, and avoid event gap warnings for targeted tool streams. (#8432) Thanks @gumadeiras.
|
- TUI/Gateway: handle non-streaming finals, refresh history for non-local chat runs, and avoid event gap warnings for targeted tool streams. (#8432) Thanks @gumadeiras.
|
||||||
- Shell completion: auto-detect and migrate slow dynamic patterns to cached files for faster terminal startup; add completion health checks to doctor/update/onboard.
|
- Shell completion: auto-detect and migrate slow dynamic patterns to cached files for faster terminal startup; add completion health checks to doctor/update/onboard.
|
||||||
|
|||||||
@@ -1,20 +1,116 @@
|
|||||||
import fs from "node:fs";
|
import fs from "node:fs";
|
||||||
import os from "node:os";
|
import os from "node:os";
|
||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
import { describe, expect, it } from "vitest";
|
import { describe, expect, it, vi } from "vitest";
|
||||||
import { installChromeExtension } from "./browser-cli-extension";
|
|
||||||
|
|
||||||
describe("browser extension install", () => {
|
const copyToClipboard = vi.fn();
|
||||||
it("installs bundled chrome extension into a state dir", async () => {
|
const runtime = {
|
||||||
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-ext-state-"));
|
log: vi.fn(),
|
||||||
|
error: vi.fn(),
|
||||||
|
exit: vi.fn(),
|
||||||
|
};
|
||||||
|
|
||||||
|
vi.mock("../infra/clipboard.js", () => ({
|
||||||
|
copyToClipboard,
|
||||||
|
}));
|
||||||
|
|
||||||
|
vi.mock("../runtime.js", () => ({
|
||||||
|
defaultRuntime: runtime,
|
||||||
|
}));
|
||||||
|
|
||||||
|
function writeManifest(dir: string) {
|
||||||
|
fs.mkdirSync(dir, { recursive: true });
|
||||||
|
fs.writeFileSync(path.join(dir, "manifest.json"), JSON.stringify({ manifest_version: 3 }));
|
||||||
|
}
|
||||||
|
|
||||||
|
describe("bundled extension resolver", () => {
|
||||||
|
it("walks up to find the assets directory", async () => {
|
||||||
|
const root = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-ext-root-"));
|
||||||
|
const here = path.join(root, "dist", "cli");
|
||||||
|
const assets = path.join(root, "assets", "chrome-extension");
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const result = await installChromeExtension({ stateDir: tmp });
|
writeManifest(assets);
|
||||||
|
fs.mkdirSync(here, { recursive: true });
|
||||||
|
|
||||||
|
const { resolveBundledExtensionRootDir } = await import("./browser-cli-extension.js");
|
||||||
|
expect(resolveBundledExtensionRootDir(here)).toBe(assets);
|
||||||
|
} finally {
|
||||||
|
fs.rmSync(root, { recursive: true, force: true });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
it("prefers the nearest assets directory", async () => {
|
||||||
|
const root = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-ext-root-"));
|
||||||
|
const here = path.join(root, "dist", "cli");
|
||||||
|
const distAssets = path.join(root, "dist", "assets", "chrome-extension");
|
||||||
|
const rootAssets = path.join(root, "assets", "chrome-extension");
|
||||||
|
|
||||||
|
try {
|
||||||
|
writeManifest(distAssets);
|
||||||
|
writeManifest(rootAssets);
|
||||||
|
fs.mkdirSync(here, { recursive: true });
|
||||||
|
|
||||||
|
const { resolveBundledExtensionRootDir } = await import("./browser-cli-extension.js");
|
||||||
|
expect(resolveBundledExtensionRootDir(here)).toBe(distAssets);
|
||||||
|
} finally {
|
||||||
|
fs.rmSync(root, { recursive: true, force: true });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("browser extension install", () => {
|
||||||
|
it("installs into the state dir (never node_modules)", async () => {
|
||||||
|
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-ext-"));
|
||||||
|
|
||||||
|
try {
|
||||||
|
const { installChromeExtension } = await import("./browser-cli-extension.js");
|
||||||
|
const sourceDir = path.resolve(process.cwd(), "assets/chrome-extension");
|
||||||
|
const result = await installChromeExtension({ stateDir: tmp, sourceDir });
|
||||||
|
|
||||||
expect(result.path).toBe(path.join(tmp, "browser", "chrome-extension"));
|
expect(result.path).toBe(path.join(tmp, "browser", "chrome-extension"));
|
||||||
expect(fs.existsSync(path.join(result.path, "manifest.json"))).toBe(true);
|
expect(fs.existsSync(path.join(result.path, "manifest.json"))).toBe(true);
|
||||||
|
expect(result.path.includes("node_modules")).toBe(false);
|
||||||
} finally {
|
} finally {
|
||||||
fs.rmSync(tmp, { recursive: true, force: true });
|
fs.rmSync(tmp, { recursive: true, force: true });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("copies extension path to clipboard", async () => {
|
||||||
|
const prev = process.env.OPENCLAW_STATE_DIR;
|
||||||
|
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-ext-path-"));
|
||||||
|
process.env.OPENCLAW_STATE_DIR = tmp;
|
||||||
|
|
||||||
|
try {
|
||||||
|
copyToClipboard.mockReset();
|
||||||
|
copyToClipboard.mockResolvedValue(true);
|
||||||
|
runtime.log.mockReset();
|
||||||
|
runtime.error.mockReset();
|
||||||
|
runtime.exit.mockReset();
|
||||||
|
|
||||||
|
const dir = path.join(tmp, "browser", "chrome-extension");
|
||||||
|
writeManifest(dir);
|
||||||
|
|
||||||
|
vi.resetModules();
|
||||||
|
const { Command } = await import("commander");
|
||||||
|
const { registerBrowserExtensionCommands } = await import("./browser-cli-extension.js");
|
||||||
|
|
||||||
|
const program = new Command();
|
||||||
|
const browser = program.command("browser").option("--json", false);
|
||||||
|
registerBrowserExtensionCommands(
|
||||||
|
browser,
|
||||||
|
(cmd) => cmd.parent?.opts?.() as { json?: boolean },
|
||||||
|
);
|
||||||
|
|
||||||
|
await program.parseAsync(["browser", "extension", "path"], { from: "user" });
|
||||||
|
|
||||||
|
expect(copyToClipboard).toHaveBeenCalledWith(dir);
|
||||||
|
} finally {
|
||||||
|
if (prev === undefined) {
|
||||||
|
delete process.env.OPENCLAW_STATE_DIR;
|
||||||
|
} else {
|
||||||
|
process.env.OPENCLAW_STATE_DIR = prev;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -12,26 +12,23 @@ import { theme } from "../terminal/theme.js";
|
|||||||
import { shortenHomePath } from "../utils.js";
|
import { shortenHomePath } from "../utils.js";
|
||||||
import { formatCliCommand } from "./command-format.js";
|
import { formatCliCommand } from "./command-format.js";
|
||||||
|
|
||||||
function bundledExtensionRootDir() {
|
export function resolveBundledExtensionRootDir(
|
||||||
const here = path.dirname(fileURLToPath(import.meta.url));
|
here = path.dirname(fileURLToPath(import.meta.url)),
|
||||||
|
) {
|
||||||
// `here` is the directory containing this file.
|
let current = here;
|
||||||
// - In source runs/tests, it's typically `<packageRoot>/src/cli`.
|
while (true) {
|
||||||
// - In transpiled builds, it's typically `<packageRoot>/dist/cli`.
|
const candidate = path.join(current, "assets", "chrome-extension");
|
||||||
// - In bundled builds, it may be `<packageRoot>/dist`.
|
|
||||||
// The bundled extension lives at `<packageRoot>/assets/chrome-extension`.
|
|
||||||
//
|
|
||||||
// Prefer the most common layouts first and fall back if needed.
|
|
||||||
const candidates = [
|
|
||||||
path.resolve(here, "../assets/chrome-extension"),
|
|
||||||
path.resolve(here, "../../assets/chrome-extension"),
|
|
||||||
];
|
|
||||||
for (const candidate of candidates) {
|
|
||||||
if (hasManifest(candidate)) {
|
if (hasManifest(candidate)) {
|
||||||
return candidate;
|
return candidate;
|
||||||
}
|
}
|
||||||
|
const parent = path.dirname(current);
|
||||||
|
if (parent === current) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
current = parent;
|
||||||
}
|
}
|
||||||
return candidates[0];
|
|
||||||
|
return path.resolve(here, "../../assets/chrome-extension");
|
||||||
}
|
}
|
||||||
|
|
||||||
function installedExtensionRootDir() {
|
function installedExtensionRootDir() {
|
||||||
@@ -46,7 +43,7 @@ export async function installChromeExtension(opts?: {
|
|||||||
stateDir?: string;
|
stateDir?: string;
|
||||||
sourceDir?: string;
|
sourceDir?: string;
|
||||||
}): Promise<{ path: string }> {
|
}): Promise<{ path: string }> {
|
||||||
const src = opts?.sourceDir ?? bundledExtensionRootDir();
|
const src = opts?.sourceDir ?? resolveBundledExtensionRootDir();
|
||||||
if (!hasManifest(src)) {
|
if (!hasManifest(src)) {
|
||||||
throw new Error("Bundled Chrome extension is missing. Reinstall OpenClaw and try again.");
|
throw new Error("Bundled Chrome extension is missing. Reinstall OpenClaw and try again.");
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user