export { validateYonexusClientConfig, YonexusClientConfigError } from "./core/config.js"; export type { YonexusClientConfig } from "./core/config.js"; export { CLIENT_STATE_VERSION, YonexusClientStateError, YonexusClientStateCorruptionError, createYonexusClientStateStore, loadYonexusClientState, saveYonexusClientState, createInitialClientState, hasClientSecret, hasClientKeyPair, type YonexusClientState, type YonexusClientStateFile, type YonexusClientStateStore } from "./core/state.js"; export { createClientTransport, YonexusClientTransport, type ClientTransport, type ClientTransportOptions, type ClientConnectionState, type ClientMessageHandler, type ClientStateChangeHandler, type ClientErrorHandler } from "./core/transport.js"; export { createYonexusClientRuntime, YonexusClientRuntime, type YonexusClientRuntimeOptions, type YonexusClientRuntimeState, type YonexusClientPhase } from "./core/runtime.js"; export { createClientRuleRegistry, YonexusClientRuleRegistry, ClientRuleRegistryError, type ClientRuleRegistry, type ClientRuleProcessor } from "./core/rules.js"; import path from "node:path"; import { validateYonexusClientConfig } from "./core/config.js"; import { createYonexusClientStateStore } from "./core/state.js"; import { createClientTransport } from "./core/transport.js"; import { createYonexusClientRuntime } from "./core/runtime.js"; import { createClientRuleRegistry } from "./core/rules.js"; export interface YonexusClientPluginManifest { readonly name: "Yonexus.Client"; readonly version: string; readonly description: string; } const manifest: YonexusClientPluginManifest = { name: "Yonexus.Client", version: "0.1.0", description: "Yonexus client plugin for cross-instance OpenClaw communication" }; let _clientStarted = false; export function createYonexusClientPlugin(api: { rootDir: string; pluginConfig: unknown }): void { if (_clientStarted) return; _clientStarted = true; const config = validateYonexusClientConfig(api.pluginConfig); const stateStore = createYonexusClientStateStore(path.join(api.rootDir, "state.json")); const ruleRegistry = createClientRuleRegistry(); const onAuthenticatedCallbacks: Array<() => void> = []; let runtimeRef: ReturnType | null = null; const transport = createClientTransport({ config, onMessage: (msg) => { runtimeRef?.handleMessage(msg).catch((err: unknown) => { console.error("[yonexus-client] message handler error:", err); }); }, onStateChange: (state) => { runtimeRef?.handleTransportStateChange(state); } }); // Expose registry and helpers for other plugins loaded in the same process (globalThis as Record)["__yonexusClient"] = { ruleRegistry, sendRule: (ruleId: string, content: string): boolean => runtimeRef?.sendRuleMessage(ruleId, content) ?? false, submitPairingCode: (code: string): boolean => runtimeRef?.submitPairingCode(code) ?? false, onAuthenticated: onAuthenticatedCallbacks }; const runtime = createYonexusClientRuntime({ config, transport, stateStore, ruleRegistry, onAuthenticated: () => { for (const cb of onAuthenticatedCallbacks) cb(); } }); runtimeRef = 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); }); } export default createYonexusClientPlugin; export { manifest };