mirror of https://github.com/openclaw/openclaw.git
fix: share context engine registry across bundled chunks
This commit is contained in:
parent
9914b48c57
commit
fdd7468c20
|
|
@ -198,6 +198,19 @@ describe("Registry tests", () => {
|
|||
expect(getContextEngineFactory("reg-overwrite")).toBe(factory2);
|
||||
expect(getContextEngineFactory("reg-overwrite")).not.toBe(factory1);
|
||||
});
|
||||
|
||||
it("shares registered engines across duplicate module copies", async () => {
|
||||
const registryUrl = new URL("./registry.ts", import.meta.url).href;
|
||||
const suffix = Date.now().toString(36);
|
||||
const first = await import(/* @vite-ignore */ `${registryUrl}?copy=${suffix}-a`);
|
||||
const second = await import(/* @vite-ignore */ `${registryUrl}?copy=${suffix}-b`);
|
||||
|
||||
const engineId = `dup-copy-${suffix}`;
|
||||
const factory = () => new MockContextEngine();
|
||||
first.registerContextEngine(engineId, factory);
|
||||
|
||||
expect(second.getContextEngineFactory(engineId)).toBe(factory);
|
||||
});
|
||||
});
|
||||
|
||||
// ═══════════════════════════════════════════════════════════════════════════
|
||||
|
|
|
|||
|
|
@ -12,27 +12,45 @@ export type ContextEngineFactory = () => ContextEngine | Promise<ContextEngine>;
|
|||
// Registry (module-level singleton)
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
const _engines = new Map<string, ContextEngineFactory>();
|
||||
const CONTEXT_ENGINE_REGISTRY_STATE = Symbol.for("openclaw.contextEngineRegistryState");
|
||||
|
||||
type ContextEngineRegistryState = {
|
||||
engines: Map<string, ContextEngineFactory>;
|
||||
};
|
||||
|
||||
// Keep context-engine registrations process-global so duplicated dist chunks
|
||||
// still share one registry map at runtime.
|
||||
function getContextEngineRegistryState(): ContextEngineRegistryState {
|
||||
const globalState = globalThis as typeof globalThis & {
|
||||
[CONTEXT_ENGINE_REGISTRY_STATE]?: ContextEngineRegistryState;
|
||||
};
|
||||
if (!globalState[CONTEXT_ENGINE_REGISTRY_STATE]) {
|
||||
globalState[CONTEXT_ENGINE_REGISTRY_STATE] = {
|
||||
engines: new Map<string, ContextEngineFactory>(),
|
||||
};
|
||||
}
|
||||
return globalState[CONTEXT_ENGINE_REGISTRY_STATE];
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a context engine implementation under the given id.
|
||||
*/
|
||||
export function registerContextEngine(id: string, factory: ContextEngineFactory): void {
|
||||
_engines.set(id, factory);
|
||||
getContextEngineRegistryState().engines.set(id, factory);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the factory for a registered engine, or undefined.
|
||||
*/
|
||||
export function getContextEngineFactory(id: string): ContextEngineFactory | undefined {
|
||||
return _engines.get(id);
|
||||
return getContextEngineRegistryState().engines.get(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* List all registered engine ids.
|
||||
*/
|
||||
export function listContextEngineIds(): string[] {
|
||||
return [..._engines.keys()];
|
||||
return [...getContextEngineRegistryState().engines.keys()];
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
|
@ -55,7 +73,7 @@ export async function resolveContextEngine(config?: OpenClawConfig): Promise<Con
|
|||
? slotValue.trim()
|
||||
: defaultSlotIdForKey("contextEngine");
|
||||
|
||||
const factory = _engines.get(engineId);
|
||||
const factory = getContextEngineRegistryState().engines.get(engineId);
|
||||
if (!factory) {
|
||||
throw new Error(
|
||||
`Context engine "${engineId}" is not registered. ` +
|
||||
|
|
|
|||
Loading…
Reference in New Issue