fix: gate runtime startup behind gateway_start; migrate to current plugin SDK
Lifecycle:
- Move runtime.start() and shutdown handlers out of register() into
api.on("gateway_start", ...) and api.on("gateway_stop", ...). register()
runs in every CLI subprocess that loads plugins (e.g. `openclaw completion`,
`openclaw doctor`); without this gate the runtime would open a network
connection / bind a listener every time those one-shot commands ran.
- Drop process.once("SIGTERM"/"SIGINT") in favour of the gateway_stop hook,
which is the documented way for plugins to react to shutdown.
- Stop relying on the non-standard `api.rootDir` field (not present on the
current OpenClawPluginApi); compute the per-plugin data directory as
~/.openclaw/yonexus-client and ensure it exists before use.
Plugin SDK convention update:
- Wrap default export with definePluginEntry({ id, name, description, register })
per the current openclaw plugin authoring contract.
- Re-type the register function to accept OpenClawPluginApi instead of the
hand-crafted { rootDir, pluginConfig, ... } shape.
- Use focused subpath imports openclaw/plugin-sdk/plugin-entry and
openclaw/plugin-sdk/core.
- Add openclaw as a devDependency (file:/usr/lib/node_modules/openclaw) so
tsc resolves the SDK type subpaths at build time.
- Modernize openclaw.plugin.json: drop version/entry/permissions, add
activation.onStartup so gateway_start fires for this plugin at boot.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -32,6 +32,7 @@
|
||||
"devDependencies": {
|
||||
"@types/node": "^25.5.2",
|
||||
"typescript": "^5.6.3",
|
||||
"vitest": "^4.1.3"
|
||||
"vitest": "^4.1.3",
|
||||
"openclaw": "file:/usr/lib/node_modules/openclaw"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,7 +39,11 @@ export {
|
||||
type ClientRuleProcessor
|
||||
} from "./core/rules.js";
|
||||
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import fs from "node:fs";
|
||||
import { definePluginEntry } from "openclaw/plugin-sdk/plugin-entry";
|
||||
import type { OpenClawPluginApi } from "openclaw/plugin-sdk/core";
|
||||
import { validateYonexusClientConfig } from "./core/config.js";
|
||||
import { createYonexusClientStateStore } from "./core/state.js";
|
||||
import { createClientTransport } from "./core/transport.js";
|
||||
@@ -52,6 +56,8 @@ const _RUNTIME_KEY = "_yonexusClientRuntime";
|
||||
const _REGISTRY_KEY = "_yonexusClientRegistry";
|
||||
const _CALLBACKS_KEY = "_yonexusClientOnAuthCallbacks";
|
||||
|
||||
const PLUGIN_DATA_DIR = path.join(os.homedir(), ".openclaw", "yonexus-client");
|
||||
|
||||
export interface YonexusClientPluginManifest {
|
||||
readonly name: "Yonexus.Client";
|
||||
readonly version: string;
|
||||
@@ -64,7 +70,7 @@ const manifest: YonexusClientPluginManifest = {
|
||||
description: "Yonexus client plugin for cross-instance OpenClaw communication"
|
||||
};
|
||||
|
||||
export function createYonexusClientPlugin(api: { rootDir: string; pluginConfig: unknown }): void {
|
||||
export function createYonexusClientPlugin(api: OpenClawPluginApi): void {
|
||||
// 1. Ensure shared state survives hot-reload — only initialise when absent
|
||||
if (!(_G[_REGISTRY_KEY] instanceof YonexusClientRuleRegistry)) {
|
||||
_G[_REGISTRY_KEY] = createClientRuleRegistry();
|
||||
@@ -87,12 +93,18 @@ export function createYonexusClientPlugin(api: { rootDir: string; pluginConfig:
|
||||
onAuthenticated: onAuthenticatedCallbacks
|
||||
};
|
||||
|
||||
// 3. Start the runtime only once — the globalThis flag survives hot-reload
|
||||
// 3. Runtime startup — only fire when the gateway boots, not eagerly during
|
||||
// register() inside one-shot CLI subprocesses (e.g. `openclaw completion`).
|
||||
// Without this gate, every CLI invocation that loads plugins would open
|
||||
// a WebSocket to the Yonexus server.
|
||||
api.on("gateway_start", () => {
|
||||
if (_G[_STARTED_KEY]) return;
|
||||
_G[_STARTED_KEY] = true;
|
||||
|
||||
fs.mkdirSync(PLUGIN_DATA_DIR, { recursive: true });
|
||||
|
||||
const config = validateYonexusClientConfig(api.pluginConfig);
|
||||
const stateStore = createYonexusClientStateStore(path.join(api.rootDir, "state.json"));
|
||||
const stateStore = createYonexusClientStateStore(path.join(PLUGIN_DATA_DIR, "state.json"));
|
||||
|
||||
const transport = createClientTransport({
|
||||
config,
|
||||
@@ -117,18 +129,23 @@ export function createYonexusClientPlugin(api: { rootDir: string; pluginConfig:
|
||||
});
|
||||
_G[_RUNTIME_KEY] = runtime;
|
||||
|
||||
const shutdown = (): void => {
|
||||
runtime.stop().catch((err: unknown) => {
|
||||
console.error("[yonexus-client] shutdown error:", err);
|
||||
});
|
||||
};
|
||||
process.once("SIGTERM", shutdown);
|
||||
process.once("SIGINT", shutdown);
|
||||
|
||||
runtime.start().catch((err: unknown) => {
|
||||
console.error("[yonexus-client] failed to start:", err);
|
||||
});
|
||||
});
|
||||
|
||||
api.on("gateway_stop", () => {
|
||||
const runtime = _G[_RUNTIME_KEY] as YonexusClientRuntime | undefined;
|
||||
runtime?.stop().catch((err: unknown) => {
|
||||
console.error("[yonexus-client] shutdown error:", err);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export default createYonexusClientPlugin;
|
||||
export default definePluginEntry({
|
||||
id: "yonexus-client",
|
||||
name: "Yonexus.Client",
|
||||
description: "Yonexus client plugin for cross-instance OpenClaw communication",
|
||||
register: createYonexusClientPlugin,
|
||||
});
|
||||
export { manifest };
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
{
|
||||
"id": "yonexus-client",
|
||||
"name": "Yonexus.Client",
|
||||
"version": "0.1.0",
|
||||
"description": "Yonexus client plugin for cross-instance OpenClaw communication",
|
||||
"entry": "./dist/Yonexus.Client/plugin/index.js",
|
||||
"permissions": [],
|
||||
"activation": {
|
||||
"onStartup": true
|
||||
},
|
||||
"configSchema": {
|
||||
"type": "object",
|
||||
"additionalProperties": false,
|
||||
|
||||
Reference in New Issue
Block a user