|
|
|
@@ -39,7 +39,11 @@ export {
|
|
|
|
type ClientRuleProcessor
|
|
|
|
type ClientRuleProcessor
|
|
|
|
} from "./core/rules.js";
|
|
|
|
} from "./core/rules.js";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import os from "node:os";
|
|
|
|
import path from "node:path";
|
|
|
|
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 { validateYonexusClientConfig } from "./core/config.js";
|
|
|
|
import { createYonexusClientStateStore } from "./core/state.js";
|
|
|
|
import { createYonexusClientStateStore } from "./core/state.js";
|
|
|
|
import { createClientTransport } from "./core/transport.js";
|
|
|
|
import { createClientTransport } from "./core/transport.js";
|
|
|
|
@@ -52,6 +56,8 @@ const _RUNTIME_KEY = "_yonexusClientRuntime";
|
|
|
|
const _REGISTRY_KEY = "_yonexusClientRegistry";
|
|
|
|
const _REGISTRY_KEY = "_yonexusClientRegistry";
|
|
|
|
const _CALLBACKS_KEY = "_yonexusClientOnAuthCallbacks";
|
|
|
|
const _CALLBACKS_KEY = "_yonexusClientOnAuthCallbacks";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const PLUGIN_DATA_DIR = path.join(os.homedir(), ".openclaw", "yonexus-client");
|
|
|
|
|
|
|
|
|
|
|
|
export interface YonexusClientPluginManifest {
|
|
|
|
export interface YonexusClientPluginManifest {
|
|
|
|
readonly name: "Yonexus.Client";
|
|
|
|
readonly name: "Yonexus.Client";
|
|
|
|
readonly version: string;
|
|
|
|
readonly version: string;
|
|
|
|
@@ -64,7 +70,7 @@ const manifest: YonexusClientPluginManifest = {
|
|
|
|
description: "Yonexus client plugin for cross-instance OpenClaw communication"
|
|
|
|
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
|
|
|
|
// 1. Ensure shared state survives hot-reload — only initialise when absent
|
|
|
|
if (!(_G[_REGISTRY_KEY] instanceof YonexusClientRuleRegistry)) {
|
|
|
|
if (!(_G[_REGISTRY_KEY] instanceof YonexusClientRuleRegistry)) {
|
|
|
|
_G[_REGISTRY_KEY] = createClientRuleRegistry();
|
|
|
|
_G[_REGISTRY_KEY] = createClientRuleRegistry();
|
|
|
|
@@ -87,48 +93,59 @@ export function createYonexusClientPlugin(api: { rootDir: string; pluginConfig:
|
|
|
|
onAuthenticated: onAuthenticatedCallbacks
|
|
|
|
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
|
|
|
|
if (_G[_STARTED_KEY]) return;
|
|
|
|
// register() inside one-shot CLI subprocesses (e.g. `openclaw completion`).
|
|
|
|
_G[_STARTED_KEY] = true;
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
|
|
|
|
const config = validateYonexusClientConfig(api.pluginConfig);
|
|
|
|
fs.mkdirSync(PLUGIN_DATA_DIR, { recursive: true });
|
|
|
|
const stateStore = createYonexusClientStateStore(path.join(api.rootDir, "state.json"));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const transport = createClientTransport({
|
|
|
|
const config = validateYonexusClientConfig(api.pluginConfig);
|
|
|
|
config,
|
|
|
|
const stateStore = createYonexusClientStateStore(path.join(PLUGIN_DATA_DIR, "state.json"));
|
|
|
|
onMessage: (msg) => {
|
|
|
|
|
|
|
|
(_G[_RUNTIME_KEY] as YonexusClientRuntime | undefined)?.handleMessage(msg).catch((err: unknown) => {
|
|
|
|
const transport = createClientTransport({
|
|
|
|
console.error("[yonexus-client] message handler error:", err);
|
|
|
|
config,
|
|
|
|
});
|
|
|
|
onMessage: (msg) => {
|
|
|
|
},
|
|
|
|
(_G[_RUNTIME_KEY] as YonexusClientRuntime | undefined)?.handleMessage(msg).catch((err: unknown) => {
|
|
|
|
onStateChange: (state) => {
|
|
|
|
console.error("[yonexus-client] message handler error:", err);
|
|
|
|
(_G[_RUNTIME_KEY] as YonexusClientRuntime | undefined)?.handleTransportStateChange(state);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
onStateChange: (state) => {
|
|
|
|
|
|
|
|
(_G[_RUNTIME_KEY] as YonexusClientRuntime | undefined)?.handleTransportStateChange(state);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const runtime = createYonexusClientRuntime({
|
|
|
|
|
|
|
|
config,
|
|
|
|
|
|
|
|
transport,
|
|
|
|
|
|
|
|
stateStore,
|
|
|
|
|
|
|
|
ruleRegistry,
|
|
|
|
|
|
|
|
onAuthenticated: () => {
|
|
|
|
|
|
|
|
for (const cb of onAuthenticatedCallbacks) cb();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
_G[_RUNTIME_KEY] = runtime;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
runtime.start().catch((err: unknown) => {
|
|
|
|
|
|
|
|
console.error("[yonexus-client] failed to start:", err);
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
const runtime = createYonexusClientRuntime({
|
|
|
|
api.on("gateway_stop", () => {
|
|
|
|
config,
|
|
|
|
const runtime = _G[_RUNTIME_KEY] as YonexusClientRuntime | undefined;
|
|
|
|
transport,
|
|
|
|
runtime?.stop().catch((err: unknown) => {
|
|
|
|
stateStore,
|
|
|
|
|
|
|
|
ruleRegistry,
|
|
|
|
|
|
|
|
onAuthenticated: () => {
|
|
|
|
|
|
|
|
for (const cb of onAuthenticatedCallbacks) cb();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
_G[_RUNTIME_KEY] = runtime;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const shutdown = (): void => {
|
|
|
|
|
|
|
|
runtime.stop().catch((err: unknown) => {
|
|
|
|
|
|
|
|
console.error("[yonexus-client] shutdown error:", err);
|
|
|
|
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);
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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 };
|
|
|
|
export { manifest };
|
|
|
|
|