mirror of
https://github.com/openclaw/openclaw.git
synced 2026-03-26 17:32:16 +00:00
55 lines
1.5 KiB
TypeScript
55 lines
1.5 KiB
TypeScript
import type { OpenClawConfig } from "../config/config.js";
|
|
import {
|
|
startExtensionHostServices,
|
|
type ExtensionHostServicesHandle,
|
|
} from "../extension-host/service-lifecycle.js";
|
|
import type { PluginRegistry } from "./registry.js";
|
|
export type PluginServicesHandle = ExtensionHostServicesHandle;
|
|
|
|
export async function startPluginServices(params: {
|
|
registry: PluginRegistry;
|
|
config: OpenClawConfig;
|
|
workspaceDir?: string;
|
|
}): Promise<PluginServicesHandle> {
|
|
const running: Array<{
|
|
id: string;
|
|
stop?: () => void | Promise<void>;
|
|
}> = [];
|
|
const serviceContext = createServiceContext({
|
|
config: params.config,
|
|
workspaceDir: params.workspaceDir,
|
|
});
|
|
|
|
for (const entry of params.registry.services) {
|
|
const service = entry.service;
|
|
try {
|
|
await service.start(serviceContext);
|
|
running.push({
|
|
id: service.id,
|
|
stop: service.stop ? () => service.stop?.(serviceContext) : undefined,
|
|
});
|
|
} catch (err) {
|
|
const error = err as Error;
|
|
const stack = error?.stack?.trim();
|
|
log.error(
|
|
`plugin service failed (${service.id}, plugin=${entry.pluginId}, root=${entry.rootDir ?? "unknown"}): ${error?.message ?? String(err)}${stack ? `\n${stack}` : ""}`,
|
|
);
|
|
}
|
|
}
|
|
|
|
return {
|
|
stop: async () => {
|
|
for (const entry of running.toReversed()) {
|
|
if (!entry.stop) {
|
|
continue;
|
|
}
|
|
try {
|
|
await entry.stop();
|
|
} catch (err) {
|
|
log.warn(`plugin service stop failed (${entry.id}): ${String(err)}`);
|
|
}
|
|
}
|
|
},
|
|
};
|
|
}
|