|
| 1 | +import { normalizeOptionalString } from "@openclaw/normalization-core/string-coerce"; |
| 2 | +import { registerContextEngineForOwner } from "../context-engine/registry.js"; |
| 3 | +import { |
| 4 | + getDetachedTaskLifecycleRuntimeRegistration, |
| 5 | + registerDetachedTaskLifecycleRuntime, |
| 6 | +} from "../tasks/detached-task-runtime-state.js"; |
| 7 | +import { |
| 8 | + getRegisteredCompactionProvider, |
| 9 | + registerCompactionProvider as registerGlobalCompactionProvider, |
| 10 | +} from "./compaction-provider.js"; |
| 11 | +import { registerRegistryPluginInteractiveHandler } from "./interactive-registry.js"; |
| 12 | +import type { PluginRegistryState } from "./registry-state.js"; |
| 13 | +import type { PluginRecord } from "./registry-types.js"; |
| 14 | +import { defaultSlotIdForKey } from "./slots.js"; |
| 15 | +import type { OpenClawPluginApi, PluginRegistrationMode } from "./types.js"; |
| 16 | + |
| 17 | +export function createCapabilityRegistrars(state: PluginRegistryState) { |
| 18 | + const { registry, pushDiagnostic } = state; |
| 19 | + |
| 20 | + const registerDetachedTaskRuntime = ( |
| 21 | + record: PluginRecord, |
| 22 | + runtime: Parameters<OpenClawPluginApi["registerDetachedTaskRuntime"]>[0], |
| 23 | + ) => { |
| 24 | + const existing = getDetachedTaskLifecycleRuntimeRegistration(); |
| 25 | + if (existing && existing.pluginId !== record.id) { |
| 26 | + pushDiagnostic({ |
| 27 | + level: "error", |
| 28 | + pluginId: record.id, |
| 29 | + source: record.source, |
| 30 | + message: `detached task runtime already registered by ${existing.pluginId}`, |
| 31 | + }); |
| 32 | + return; |
| 33 | + } |
| 34 | + registerDetachedTaskLifecycleRuntime(record.id, runtime); |
| 35 | + }; |
| 36 | + |
| 37 | + const registerInteractiveHandler = ( |
| 38 | + record: PluginRecord, |
| 39 | + registration: Parameters<OpenClawPluginApi["registerInteractiveHandler"]>[0], |
| 40 | + ) => { |
| 41 | + const result = registerRegistryPluginInteractiveHandler(record.id, registration, { |
| 42 | + pluginName: record.name, |
| 43 | + pluginRoot: record.rootDir, |
| 44 | + }); |
| 45 | + if (!result.ok) { |
| 46 | + pushDiagnostic({ |
| 47 | + level: "warn", |
| 48 | + pluginId: record.id, |
| 49 | + source: record.source, |
| 50 | + message: result.error ?? "interactive handler registration failed", |
| 51 | + }); |
| 52 | + return; |
| 53 | + } |
| 54 | + registry.interactiveHandlers.push({ |
| 55 | + ...registration, |
| 56 | + pluginId: record.id, |
| 57 | + pluginName: record.name, |
| 58 | + pluginRoot: record.rootDir, |
| 59 | + }); |
| 60 | + }; |
| 61 | + |
| 62 | + const registerContextEngine = ( |
| 63 | + record: PluginRecord, |
| 64 | + id: Parameters<OpenClawPluginApi["registerContextEngine"]>[0], |
| 65 | + factory: Parameters<OpenClawPluginApi["registerContextEngine"]>[1], |
| 66 | + registrationMode: PluginRegistrationMode, |
| 67 | + ) => { |
| 68 | + const normalizedId = normalizeOptionalString(id) ?? ""; |
| 69 | + if (!normalizedId) { |
| 70 | + pushDiagnostic({ |
| 71 | + level: "error", |
| 72 | + pluginId: record.id, |
| 73 | + source: record.source, |
| 74 | + message: "context engine registration missing id", |
| 75 | + }); |
| 76 | + return; |
| 77 | + } |
| 78 | + if (typeof factory !== "function") { |
| 79 | + pushDiagnostic({ |
| 80 | + level: "error", |
| 81 | + pluginId: record.id, |
| 82 | + source: record.source, |
| 83 | + message: `context engine "${normalizedId}" registration missing factory`, |
| 84 | + }); |
| 85 | + return; |
| 86 | + } |
| 87 | + if (normalizedId === defaultSlotIdForKey("contextEngine")) { |
| 88 | + pushDiagnostic({ |
| 89 | + level: "error", |
| 90 | + pluginId: record.id, |
| 91 | + source: record.source, |
| 92 | + message: `context engine id reserved by core: ${normalizedId}`, |
| 93 | + }); |
| 94 | + return; |
| 95 | + } |
| 96 | + const result = registerContextEngineForOwner(normalizedId, factory, `plugin:${record.id}`, { |
| 97 | + allowSameOwnerRefresh: true, |
| 98 | + lifecycle: registrationMode === "full" ? "runtime" : "readOnlyDiscovery", |
| 99 | + }); |
| 100 | + if (!result.ok) { |
| 101 | + pushDiagnostic({ |
| 102 | + level: "error", |
| 103 | + pluginId: record.id, |
| 104 | + source: record.source, |
| 105 | + message: `context engine already registered: ${normalizedId} (${result.existingOwner})`, |
| 106 | + }); |
| 107 | + return; |
| 108 | + } |
| 109 | + if (!record.contextEngineIds?.includes(normalizedId)) { |
| 110 | + record.contextEngineIds = [...(record.contextEngineIds ?? []), normalizedId]; |
| 111 | + } |
| 112 | + }; |
| 113 | + |
| 114 | + const registerCompactionProvider = ( |
| 115 | + record: PluginRecord, |
| 116 | + provider: Parameters<OpenClawPluginApi["registerCompactionProvider"]>[0], |
| 117 | + ) => { |
| 118 | + const id = normalizeOptionalString( |
| 119 | + (provider as Partial<Parameters<OpenClawPluginApi["registerCompactionProvider"]>[0]> | null) |
| 120 | + ?.id, |
| 121 | + ); |
| 122 | + if (!id) { |
| 123 | + pushDiagnostic({ |
| 124 | + level: "error", |
| 125 | + pluginId: record.id, |
| 126 | + source: record.source, |
| 127 | + message: "compaction provider registration missing id", |
| 128 | + }); |
| 129 | + return; |
| 130 | + } |
| 131 | + if (typeof provider?.summarize !== "function") { |
| 132 | + pushDiagnostic({ |
| 133 | + level: "error", |
| 134 | + pluginId: record.id, |
| 135 | + source: record.source, |
| 136 | + message: `compaction provider "${id}" registration missing summarize`, |
| 137 | + }); |
| 138 | + return; |
| 139 | + } |
| 140 | + const existing = getRegisteredCompactionProvider(id); |
| 141 | + if (existing) { |
| 142 | + const ownerDetail = existing.ownerPluginId ? ` (owner: ${existing.ownerPluginId})` : ""; |
| 143 | + pushDiagnostic({ |
| 144 | + level: "error", |
| 145 | + pluginId: record.id, |
| 146 | + source: record.source, |
| 147 | + message: `compaction provider already registered: ${id}${ownerDetail}`, |
| 148 | + }); |
| 149 | + return; |
| 150 | + } |
| 151 | + registerGlobalCompactionProvider(provider, { ownerPluginId: record.id }); |
| 152 | + }; |
| 153 | + |
| 154 | + return { |
| 155 | + registerDetachedTaskRuntime, |
| 156 | + registerInteractiveHandler, |
| 157 | + registerContextEngine, |
| 158 | + registerCompactionProvider, |
| 159 | + }; |
| 160 | +} |
| 161 | + |
| 162 | +export type CapabilityRegistrars = ReturnType<typeof createCapabilityRegistrars>; |
0 commit comments