| import type { OpenClawConfig } from "../config/config.js"; |
| import { defaultSlotIdForKey } from "../plugins/slots.js"; |
| import type { ContextEngine } from "./types.js"; |
|
|
| |
| |
| |
| |
| export type ContextEngineFactory = () => ContextEngine | Promise<ContextEngine>; |
|
|
| |
| |
| |
|
|
| const CONTEXT_ENGINE_REGISTRY_STATE = Symbol.for("openclaw.contextEngineRegistryState"); |
|
|
| type ContextEngineRegistryState = { |
| engines: Map<string, ContextEngineFactory>; |
| }; |
|
|
| |
| |
| 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]; |
| } |
|
|
| |
| |
| |
| export function registerContextEngine(id: string, factory: ContextEngineFactory): void { |
| getContextEngineRegistryState().engines.set(id, factory); |
| } |
|
|
| |
| |
| |
| export function getContextEngineFactory(id: string): ContextEngineFactory | undefined { |
| return getContextEngineRegistryState().engines.get(id); |
| } |
|
|
| |
| |
| |
| export function listContextEngineIds(): string[] { |
| return [...getContextEngineRegistryState().engines.keys()]; |
| } |
|
|
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| export async function resolveContextEngine(config?: OpenClawConfig): Promise<ContextEngine> { |
| const slotValue = config?.plugins?.slots?.contextEngine; |
| const engineId = |
| typeof slotValue === "string" && slotValue.trim() |
| ? slotValue.trim() |
| : defaultSlotIdForKey("contextEngine"); |
|
|
| const factory = getContextEngineRegistryState().engines.get(engineId); |
| if (!factory) { |
| throw new Error( |
| `Context engine "${engineId}" is not registered. ` + |
| `Available engines: ${listContextEngineIds().join(", ") || "(none)"}`, |
| ); |
| } |
|
|
| return factory(); |
| } |
|
|