mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-23 23:22:32 +00:00
refactor(deadcode): remove orphaned core helpers
This commit is contained in:
@@ -1,71 +0,0 @@
|
||||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
import { pathToFileURL } from "node:url";
|
||||
import { afterEach, describe, expect, it } from "vitest";
|
||||
import { cleanupTrackedTempDirs, makeTrackedTempDir } from "../test-helpers/fs-fixtures.js";
|
||||
import { loadSiblingRuntimeModuleSync } from "./local-runtime-module.js";
|
||||
|
||||
const tempDirs: string[] = [];
|
||||
|
||||
function createTempDir(): string {
|
||||
return makeTrackedTempDir("openclaw-local-runtime-module", tempDirs);
|
||||
}
|
||||
|
||||
function writeFile(filePath: string, content: string): void {
|
||||
fs.mkdirSync(path.dirname(filePath), { recursive: true });
|
||||
fs.writeFileSync(filePath, content);
|
||||
}
|
||||
|
||||
afterEach(() => {
|
||||
cleanupTrackedTempDirs(tempDirs);
|
||||
});
|
||||
|
||||
describe("loadSiblingRuntimeModuleSync", () => {
|
||||
it("loads a sibling runtime module from the caller directory", () => {
|
||||
const root = createTempDir();
|
||||
const moduleUrl = pathToFileURL(
|
||||
path.join(root, "src", "plugins", "runtime", "runtime.js"),
|
||||
).href;
|
||||
|
||||
writeFile(
|
||||
path.join(root, "src", "plugins", "runtime", "runtime.contract.js"),
|
||||
"module.exports = { runtimeLine: { source: 'sibling' } };",
|
||||
);
|
||||
|
||||
const loaded = loadSiblingRuntimeModuleSync<{ runtimeLine: { source: string } }>({
|
||||
moduleUrl,
|
||||
relativeBase: "./runtime.contract",
|
||||
});
|
||||
|
||||
expect(loaded.runtimeLine.source).toBe("sibling");
|
||||
});
|
||||
|
||||
it("falls back to the built plugins/runtime dist layout", () => {
|
||||
const root = createTempDir();
|
||||
const moduleUrl = pathToFileURL(path.join(root, "dist", "runtime-9DLN_Ai5.js")).href;
|
||||
|
||||
writeFile(
|
||||
path.join(root, "dist", "plugins", "runtime", "runtime.contract.js"),
|
||||
"module.exports = { runtimeLine: { source: 'dist-runtime' } };",
|
||||
);
|
||||
|
||||
const loaded = loadSiblingRuntimeModuleSync<{ runtimeLine: { source: string } }>({
|
||||
moduleUrl,
|
||||
relativeBase: "./runtime.contract",
|
||||
});
|
||||
|
||||
expect(loaded.runtimeLine.source).toBe("dist-runtime");
|
||||
});
|
||||
|
||||
it("throws when no candidate runtime module exists", () => {
|
||||
const root = createTempDir();
|
||||
const moduleUrl = pathToFileURL(path.join(root, "dist", "runtime-9DLN_Ai5.js")).href;
|
||||
|
||||
expect(() =>
|
||||
loadSiblingRuntimeModuleSync({
|
||||
moduleUrl,
|
||||
relativeBase: "./runtime.contract",
|
||||
}),
|
||||
).toThrow("Unable to resolve runtime module ./runtime.contract");
|
||||
});
|
||||
});
|
||||
@@ -1,56 +0,0 @@
|
||||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { createJiti } from "jiti";
|
||||
import {
|
||||
buildPluginLoaderAliasMap,
|
||||
buildPluginLoaderJitiOptions,
|
||||
shouldPreferNativeJiti,
|
||||
} from "../sdk-alias.js";
|
||||
|
||||
const RUNTIME_MODULE_EXTENSIONS = [".js", ".ts", ".mjs", ".mts", ".cjs", ".cts"] as const;
|
||||
const jitiLoaders = new Map<string, ReturnType<typeof createJiti>>();
|
||||
|
||||
function resolveSiblingRuntimeModulePath(moduleUrl: string, relativeBase: string): string {
|
||||
const baseDir = path.dirname(fileURLToPath(moduleUrl));
|
||||
const baseName = relativeBase.replace(/^\.\//, "");
|
||||
const candidateDirs = [baseDir, path.resolve(baseDir, "plugins", "runtime")];
|
||||
|
||||
for (const dir of candidateDirs) {
|
||||
for (const ext of RUNTIME_MODULE_EXTENSIONS) {
|
||||
const candidate = path.resolve(dir, `${baseName}${ext}`);
|
||||
if (fs.existsSync(candidate)) {
|
||||
return candidate;
|
||||
}
|
||||
}
|
||||
}
|
||||
throw new Error(`Unable to resolve runtime module ${relativeBase} from ${moduleUrl}`);
|
||||
}
|
||||
|
||||
function getJiti(modulePath: string, moduleUrl: string) {
|
||||
const tryNative = shouldPreferNativeJiti(modulePath);
|
||||
const aliasMap = buildPluginLoaderAliasMap(modulePath, process.argv[1], moduleUrl);
|
||||
const cacheKey = JSON.stringify({
|
||||
tryNative,
|
||||
aliasMap: Object.entries(aliasMap).toSorted(([left], [right]) => left.localeCompare(right)),
|
||||
moduleUrl,
|
||||
});
|
||||
const cached = jitiLoaders.get(cacheKey);
|
||||
if (cached) {
|
||||
return cached;
|
||||
}
|
||||
const loader = createJiti(moduleUrl, {
|
||||
...buildPluginLoaderJitiOptions(aliasMap),
|
||||
tryNative,
|
||||
});
|
||||
jitiLoaders.set(cacheKey, loader);
|
||||
return loader;
|
||||
}
|
||||
|
||||
export function loadSiblingRuntimeModuleSync<T>(params: {
|
||||
moduleUrl: string;
|
||||
relativeBase: string;
|
||||
}): T {
|
||||
const modulePath = resolveSiblingRuntimeModulePath(params.moduleUrl, params.relativeBase);
|
||||
return getJiti(modulePath, params.moduleUrl)(modulePath) as T;
|
||||
}
|
||||
@@ -1,43 +0,0 @@
|
||||
import { afterEach, describe, it, vi } from "vitest";
|
||||
import { createTypingLease } from "./typing-lease.js";
|
||||
import {
|
||||
expectDefaultTypingLeaseInterval,
|
||||
registerSharedTypingLeaseTests,
|
||||
} from "./typing-lease.test-support.js";
|
||||
|
||||
const TEST_TYPING_INTERVAL_MS = 2_000;
|
||||
const TEST_TYPING_DEFAULT_INTERVAL_MS = 4_000;
|
||||
|
||||
function buildTypingLeaseParams(
|
||||
pulse: (params: { target: string; lane?: string }) => Promise<unknown>,
|
||||
) {
|
||||
return {
|
||||
defaultIntervalMs: TEST_TYPING_DEFAULT_INTERVAL_MS,
|
||||
errorLabel: "test",
|
||||
intervalMs: TEST_TYPING_INTERVAL_MS,
|
||||
pulse,
|
||||
pulseArgs: {
|
||||
target: "target-1",
|
||||
lane: "answer",
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
describe("createTypingLease", () => {
|
||||
afterEach(() => {
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
registerSharedTypingLeaseTests({
|
||||
createLease: createTypingLease,
|
||||
buildParams: buildTypingLeaseParams,
|
||||
});
|
||||
|
||||
it("falls back to the default interval for non-finite values", async () => {
|
||||
await expectDefaultTypingLeaseInterval({
|
||||
createLease: createTypingLease,
|
||||
buildParams: buildTypingLeaseParams,
|
||||
defaultIntervalMs: TEST_TYPING_DEFAULT_INTERVAL_MS,
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,56 +0,0 @@
|
||||
import { logWarn } from "../../logger.js";
|
||||
|
||||
export type TypingLease = {
|
||||
refresh: () => Promise<void>;
|
||||
stop: () => void;
|
||||
};
|
||||
|
||||
type CreateTypingLeaseParams<TPulseArgs> = {
|
||||
defaultIntervalMs: number;
|
||||
errorLabel: string;
|
||||
intervalMs?: number;
|
||||
pulse: (params: TPulseArgs) => Promise<unknown>;
|
||||
pulseArgs: TPulseArgs;
|
||||
};
|
||||
|
||||
export async function createTypingLease<TPulseArgs>(
|
||||
params: CreateTypingLeaseParams<TPulseArgs>,
|
||||
): Promise<TypingLease> {
|
||||
const intervalMs =
|
||||
typeof params.intervalMs === "number" && Number.isFinite(params.intervalMs)
|
||||
? Math.max(1_000, Math.floor(params.intervalMs))
|
||||
: params.defaultIntervalMs;
|
||||
|
||||
let stopped = false;
|
||||
let timer: ReturnType<typeof setInterval> | null = null;
|
||||
|
||||
const pulse = async () => {
|
||||
if (stopped) {
|
||||
return;
|
||||
}
|
||||
await params.pulse(params.pulseArgs);
|
||||
};
|
||||
|
||||
await pulse();
|
||||
|
||||
timer = setInterval(() => {
|
||||
// Background lease refreshes must never escape as unhandled rejections.
|
||||
void pulse().catch((err) => {
|
||||
logWarn(`plugins: ${params.errorLabel} typing pulse failed: ${String(err)}`);
|
||||
});
|
||||
}, intervalMs);
|
||||
timer.unref?.();
|
||||
|
||||
return {
|
||||
refresh: async () => {
|
||||
await pulse();
|
||||
},
|
||||
stop: () => {
|
||||
stopped = true;
|
||||
if (timer) {
|
||||
clearInterval(timer);
|
||||
timer = null;
|
||||
}
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user