forked from openclaw/openclaw
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli-registry-loader.ts
More file actions
292 lines (272 loc) · 8.81 KB
/
Copy pathcli-registry-loader.ts
File metadata and controls
292 lines (272 loc) · 8.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import { collectUniqueCommandDescriptors } from "../cli/program/command-descriptor-utils.js";
import type { OpenClawConfig } from "../config/types.openclaw.js";
import { normalizeLowercaseStringOrEmpty } from "../shared/string-coerce.js";
import { uniqueStrings } from "../shared/string-normalization.js";
import { resolveManifestActivationPluginIds } from "./activation-planner.js";
import { createPluginCliGatewayNodesRuntime } from "./cli-gateway-nodes-runtime.js";
import type { PluginLoadOptions } from "./loader.js";
import { loadOpenClawPluginCliRegistry, loadOpenClawPlugins } from "./loader.js";
import { createEmptyPluginRegistry } from "./registry-empty.js";
import type { PluginRegistry } from "./registry.js";
import {
buildPluginRuntimeLoadOptions,
createPluginRuntimeLoaderLogger,
resolvePluginRuntimeLoadContext,
type PluginRuntimeLoadContext,
} from "./runtime/load-context.js";
import type {
OpenClawPluginCliCommandDescriptor,
OpenClawPluginCliContext,
PluginLogger,
} from "./types.js";
export type PluginCliLoaderOptions = Pick<PluginLoadOptions, "pluginSdkResolution">;
export type PluginCliPublicLoadParams = {
cfg?: OpenClawConfig;
env?: NodeJS.ProcessEnv;
loaderOptions?: PluginCliLoaderOptions;
logger?: PluginLogger;
primaryCommand?: string;
};
export type PluginCliLoadContext = PluginRuntimeLoadContext;
export type PluginCliRegistryLoadResult = PluginCliLoadContext & {
registry: PluginRegistry;
};
export type PluginCliCommandGroupEntry = {
pluginId: string;
parentPath: readonly string[];
placeholders: readonly OpenClawPluginCliCommandDescriptor[];
names: readonly string[];
register: (program: OpenClawPluginCliContext["program"]) => Promise<void>;
};
export function createPluginCliLogger(): PluginLogger {
return createPluginRuntimeLoaderLogger();
}
function resolvePluginCliLogger(logger?: PluginLogger): PluginLogger {
return logger ?? createPluginCliLogger();
}
function buildPluginCliLoaderParams(
context: PluginCliLoadContext,
params?: { primaryCommand?: string },
loaderOptions?: PluginCliLoaderOptions,
) {
const onlyPluginIds = resolvePrimaryCommandManifestPluginIds(context, params?.primaryCommand);
return buildPluginRuntimeLoadOptions(context, {
...loaderOptions,
...(onlyPluginIds && onlyPluginIds.length > 0 ? { onlyPluginIds } : {}),
});
}
function normalizePluginCliRootName(value: string | undefined): string {
return normalizeLowercaseStringOrEmpty(value);
}
function resolvePrimaryCommandManifestPluginIds(
context: PluginCliLoadContext,
primaryCommand: string | undefined,
): string[] | undefined {
const normalizedPrimary = normalizePluginCliRootName(primaryCommand);
if (!normalizedPrimary) {
return undefined;
}
return resolveManifestActivationPluginIds({
trigger: {
kind: "command",
command: normalizedPrimary,
},
config: context.activationSourceConfig,
workspaceDir: context.workspaceDir,
env: context.env,
});
}
function listPluginCliRootOwnerIds(registry: PluginRegistry, primaryCommand: string): string[] {
const normalizedPrimary = normalizePluginCliRootName(primaryCommand);
if (!normalizedPrimary) {
return [];
}
return uniqueStrings(
registry.cliRegistrars
.filter((entry) => {
const parentPath = entry.parentPath ?? [];
const roots =
parentPath.length > 0
? [parentPath[0]]
: [...entry.commands, ...entry.descriptors.map((descriptor) => descriptor.name)];
return roots.includes(normalizedPrimary);
})
.map((entry) => entry.pluginId),
);
}
async function resolvePrimaryCommandPluginIds(
context: PluginCliLoadContext,
primaryCommand: string | undefined,
loaderOptions?: PluginCliLoaderOptions,
): Promise<string[] | undefined> {
const normalizedPrimary = normalizePluginCliRootName(primaryCommand);
if (!normalizedPrimary) {
return undefined;
}
const manifestPluginIds = resolvePrimaryCommandManifestPluginIds(context, normalizedPrimary);
if (manifestPluginIds && manifestPluginIds.length > 0) {
return manifestPluginIds;
}
const { registry } = await loadPluginCliMetadataRegistryWithContext(
context,
{ primaryCommand: normalizedPrimary },
loaderOptions,
);
return listPluginCliRootOwnerIds(registry, normalizedPrimary);
}
export function resolvePluginCliLoadContext(params: {
cfg?: OpenClawConfig;
env?: NodeJS.ProcessEnv;
logger: PluginLogger;
}): PluginCliLoadContext {
return resolvePluginRuntimeLoadContext({
config: params.cfg,
env: params.env,
logger: params.logger,
});
}
export async function loadPluginCliMetadataRegistryWithContext(
context: PluginCliLoadContext,
params?: { primaryCommand?: string },
loaderOptions?: PluginCliLoaderOptions,
): Promise<PluginCliRegistryLoadResult> {
return {
...context,
registry: await loadOpenClawPluginCliRegistry(
buildPluginCliLoaderParams(context, params, loaderOptions),
),
};
}
export async function loadPluginCliCommandRegistryWithContext(params: {
context: PluginCliLoadContext;
primaryCommand?: string;
loaderOptions?: PluginCliLoaderOptions;
}): Promise<PluginCliRegistryLoadResult> {
let onlyPluginIds: string[] | undefined;
try {
onlyPluginIds = await resolvePrimaryCommandPluginIds(
params.context,
params.primaryCommand,
params.loaderOptions,
);
} catch {
onlyPluginIds = resolvePrimaryCommandManifestPluginIds(params.context, params.primaryCommand);
}
if (onlyPluginIds && onlyPluginIds.length === 0) {
return {
...params.context,
registry: createEmptyPluginRegistry(),
};
}
return {
...params.context,
registry: loadOpenClawPlugins(
buildPluginRuntimeLoadOptions(params.context, {
...params.loaderOptions,
...(onlyPluginIds && onlyPluginIds.length > 0 ? { onlyPluginIds } : {}),
activate: false,
cache: false,
runtimeOptions: {
nodes: createPluginCliGatewayNodesRuntime(),
},
}),
),
};
}
function buildPluginCliCommandGroupEntries(params: {
registry: PluginRegistry;
config: OpenClawConfig;
workspaceDir: string | undefined;
logger: PluginLogger;
}): PluginCliCommandGroupEntry[] {
return params.registry.cliRegistrars.map((entry) => ({
pluginId: entry.pluginId,
parentPath: entry.parentPath ?? [],
placeholders: entry.descriptors,
names: entry.commands,
register: async (program) => {
await entry.register({
program,
parentPath: entry.parentPath ?? [],
config: params.config,
workspaceDir: params.workspaceDir,
logger: params.logger,
});
},
}));
}
export async function loadPluginCliDescriptors(
params: PluginCliPublicLoadParams,
): Promise<OpenClawPluginCliCommandDescriptor[]> {
try {
const logger = resolvePluginCliLogger(params.logger);
const context = resolvePluginCliLoadContext({
cfg: params.cfg,
env: params.env,
logger,
});
const { registry } = await loadPluginCliMetadataRegistryWithContext(
context,
{ primaryCommand: params.primaryCommand },
params.loaderOptions,
);
return collectUniqueCommandDescriptors(
registry.cliRegistrars
.filter((entry) => (entry.parentPath ?? []).length === 0)
.map((entry) => entry.descriptors),
);
} catch {
return [];
}
}
export async function loadPluginCliRegistrationEntries(params: {
cfg?: OpenClawConfig;
env?: NodeJS.ProcessEnv;
loaderOptions?: PluginCliLoaderOptions;
logger?: PluginLogger;
primaryCommand?: string;
}): Promise<PluginCliCommandGroupEntry[]> {
const resolvedLogger = resolvePluginCliLogger(params.logger);
const context = resolvePluginCliLoadContext({
cfg: params.cfg,
env: params.env,
logger: resolvedLogger,
});
const { config, workspaceDir, logger, registry } = await loadPluginCliCommandRegistryWithContext({
context,
primaryCommand: params.primaryCommand,
loaderOptions: params.loaderOptions,
});
return buildPluginCliCommandGroupEntries({
registry,
config,
workspaceDir,
logger,
});
}
export async function resolvePluginCliRootOwnerIds(
params: PluginCliPublicLoadParams,
): Promise<string[] | null> {
const primaryCommand = normalizePluginCliRootName(params.primaryCommand);
if (!primaryCommand) {
return null;
}
const logger = resolvePluginCliLogger(params.logger);
const context = resolvePluginCliLoadContext({
cfg: params.cfg,
env: params.env,
logger,
});
return (
(await resolvePrimaryCommandPluginIds(context, primaryCommand, params.loaderOptions)) ?? null
);
}
export async function loadPluginCliRegistrationEntriesWithDefaults(
params: PluginCliPublicLoadParams,
): Promise<PluginCliCommandGroupEntry[]> {
const logger = resolvePluginCliLogger(params.logger);
return loadPluginCliRegistrationEntries({
...params,
logger,
});
}