Compare commits
6 Commits
57b53fc122
...
notify-pro
| Author | SHA1 | Date | |
|---|---|---|---|
| d0b19cf116 | |||
|
|
c3c11c1b27 | ||
| 6b51bc6475 | |||
| 8b26919790 | |||
| 4adb187331 | |||
| 8824e768fb |
8
package-lock.json
generated
8
package-lock.json
generated
@@ -12,10 +12,14 @@
|
|||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/node": "^25.5.2",
|
"@types/node": "^25.5.2",
|
||||||
|
"openclaw": "file:/usr/lib/node_modules/openclaw",
|
||||||
"typescript": "^5.6.3",
|
"typescript": "^5.6.3",
|
||||||
"vitest": "^4.1.3"
|
"vitest": "^4.1.3"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"../../../../../usr/lib/node_modules/openclaw": {
|
||||||
|
"dev": true
|
||||||
|
},
|
||||||
"node_modules/@emnapi/core": {
|
"node_modules/@emnapi/core": {
|
||||||
"version": "1.9.1",
|
"version": "1.9.1",
|
||||||
"resolved": "https://registry.npmjs.org/@emnapi/core/-/core-1.9.1.tgz",
|
"resolved": "https://registry.npmjs.org/@emnapi/core/-/core-1.9.1.tgz",
|
||||||
@@ -914,6 +918,10 @@
|
|||||||
],
|
],
|
||||||
"license": "MIT"
|
"license": "MIT"
|
||||||
},
|
},
|
||||||
|
"node_modules/openclaw": {
|
||||||
|
"resolved": "../../../../../usr/lib/node_modules/openclaw",
|
||||||
|
"link": true
|
||||||
|
},
|
||||||
"node_modules/pathe": {
|
"node_modules/pathe": {
|
||||||
"version": "2.0.3",
|
"version": "2.0.3",
|
||||||
"resolved": "https://registry.npmjs.org/pathe/-/pathe-2.0.3.tgz",
|
"resolved": "https://registry.npmjs.org/pathe/-/pathe-2.0.3.tgz",
|
||||||
|
|||||||
@@ -5,6 +5,9 @@
|
|||||||
"description": "Yonexus.Client OpenClaw plugin scaffold",
|
"description": "Yonexus.Client OpenClaw plugin scaffold",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"main": "dist/plugin/index.js",
|
"main": "dist/plugin/index.js",
|
||||||
|
"openclaw": {
|
||||||
|
"extensions": ["./dist/Yonexus.Client/plugin/index.js"]
|
||||||
|
},
|
||||||
"files": [
|
"files": [
|
||||||
"dist",
|
"dist",
|
||||||
"plugin",
|
"plugin",
|
||||||
@@ -29,6 +32,7 @@
|
|||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/node": "^25.5.2",
|
"@types/node": "^25.5.2",
|
||||||
"typescript": "^5.6.3",
|
"typescript": "^5.6.3",
|
||||||
"vitest": "^4.1.3"
|
"vitest": "^4.1.3",
|
||||||
|
"openclaw": "file:/usr/lib/node_modules/openclaw"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,12 @@
|
|||||||
export interface YonexusClientConfig {
|
export interface YonexusClientConfig {
|
||||||
mainHost: string;
|
mainHost: string;
|
||||||
identifier: string;
|
identifier: string;
|
||||||
notifyBotToken: string;
|
/**
|
||||||
adminUserId: string;
|
* Optional. The client never sends pairing notifications (the server
|
||||||
|
* does); accepted for back-compat but no longer required.
|
||||||
|
*/
|
||||||
|
notifyBotToken?: string;
|
||||||
|
adminUserId?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class YonexusClientConfigError extends Error {
|
export class YonexusClientConfigError extends Error {
|
||||||
@@ -44,15 +48,9 @@ export function validateYonexusClientConfig(raw: unknown): YonexusClientConfig {
|
|||||||
issues.push("identifier is required");
|
issues.push("identifier is required");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Optional (back-compat): the client does not send notifications.
|
||||||
const rawNotifyBotToken = source.notifyBotToken;
|
const rawNotifyBotToken = source.notifyBotToken;
|
||||||
if (!isNonEmptyString(rawNotifyBotToken)) {
|
|
||||||
issues.push("notifyBotToken is required");
|
|
||||||
}
|
|
||||||
|
|
||||||
const rawAdminUserId = source.adminUserId;
|
const rawAdminUserId = source.adminUserId;
|
||||||
if (!isNonEmptyString(rawAdminUserId)) {
|
|
||||||
issues.push("adminUserId is required");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (issues.length > 0) {
|
if (issues.length > 0) {
|
||||||
throw new YonexusClientConfigError(issues);
|
throw new YonexusClientConfigError(issues);
|
||||||
@@ -60,13 +58,15 @@ export function validateYonexusClientConfig(raw: unknown): YonexusClientConfig {
|
|||||||
|
|
||||||
const mainHost = (rawMainHost as string).trim();
|
const mainHost = (rawMainHost as string).trim();
|
||||||
const identifier = (rawIdentifier as string).trim();
|
const identifier = (rawIdentifier as string).trim();
|
||||||
const notifyBotToken = (rawNotifyBotToken as string).trim();
|
|
||||||
const adminUserId = (rawAdminUserId as string).trim();
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
mainHost,
|
mainHost,
|
||||||
identifier,
|
identifier,
|
||||||
notifyBotToken,
|
notifyBotToken: isNonEmptyString(rawNotifyBotToken)
|
||||||
adminUserId
|
? rawNotifyBotToken.trim()
|
||||||
|
: undefined,
|
||||||
|
adminUserId: isNonEmptyString(rawAdminUserId)
|
||||||
|
? rawAdminUserId.trim()
|
||||||
|
: undefined
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ import {
|
|||||||
} from "./state.js";
|
} from "./state.js";
|
||||||
import { generateNonce, signMessage } from "../crypto/keypair.js";
|
import { generateNonce, signMessage } from "../crypto/keypair.js";
|
||||||
import type { ClientConnectionState, ClientTransport } from "./transport.js";
|
import type { ClientConnectionState, ClientTransport } from "./transport.js";
|
||||||
|
import type { ClientRuleRegistry } from "./rules.js";
|
||||||
|
|
||||||
export type YonexusClientPhase =
|
export type YonexusClientPhase =
|
||||||
| "idle"
|
| "idle"
|
||||||
@@ -44,6 +45,8 @@ export interface YonexusClientRuntimeOptions {
|
|||||||
config: YonexusClientConfig;
|
config: YonexusClientConfig;
|
||||||
transport: ClientTransport;
|
transport: ClientTransport;
|
||||||
stateStore: YonexusClientStateStore;
|
stateStore: YonexusClientStateStore;
|
||||||
|
ruleRegistry?: ClientRuleRegistry;
|
||||||
|
onAuthenticated?: () => void;
|
||||||
now?: () => number;
|
now?: () => number;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -118,6 +121,7 @@ export class YonexusClientRuntime {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!isBuiltinMessage(raw)) {
|
if (!isBuiltinMessage(raw)) {
|
||||||
|
this.options.ruleRegistry?.dispatch(raw);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -159,6 +163,7 @@ export class YonexusClientRuntime {
|
|||||||
};
|
};
|
||||||
await this.options.stateStore.save(this.clientState);
|
await this.options.stateStore.save(this.clientState);
|
||||||
this.phase = "authenticated";
|
this.phase = "authenticated";
|
||||||
|
this.options.onAuthenticated?.();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -177,7 +182,17 @@ export class YonexusClientRuntime {
|
|||||||
|
|
||||||
handleTransportStateChange(state: ClientConnectionState): void {
|
handleTransportStateChange(state: ClientConnectionState): void {
|
||||||
if (state === "connected") {
|
if (state === "connected") {
|
||||||
this.sendHello();
|
// Reload state from disk before hello so that any secret written by an
|
||||||
|
// external process (e.g. a pairing script) is picked up on reconnect.
|
||||||
|
this.options.stateStore.load(this.options.config.identifier).then((fresh) => {
|
||||||
|
if (fresh) {
|
||||||
|
this.clientState = { ...this.clientState, ...fresh };
|
||||||
|
}
|
||||||
|
this.sendHello();
|
||||||
|
}).catch(() => {
|
||||||
|
// If reload fails, proceed with in-memory state
|
||||||
|
this.sendHello();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (state === "disconnected") {
|
if (state === "disconnected") {
|
||||||
@@ -259,7 +274,10 @@ export class YonexusClientRuntime {
|
|||||||
adminNotification: payload.adminNotification
|
adminNotification: payload.adminNotification
|
||||||
};
|
};
|
||||||
this.lastPairingFailure = undefined;
|
this.lastPairingFailure = undefined;
|
||||||
this.phase = payload.adminNotification === "sent" ? "waiting_pair_confirm" : "pair_required";
|
// Always wait for the pairing code regardless of notification status.
|
||||||
|
// When adminNotification is "failed", the admin can retrieve the code
|
||||||
|
// via the server CLI command and deliver it through an alternate channel.
|
||||||
|
this.phase = "waiting_pair_confirm";
|
||||||
}
|
}
|
||||||
|
|
||||||
private async handlePairSuccess(envelope: TypedBuiltinEnvelope<"pair_success">): Promise<void> {
|
private async handlePairSuccess(envelope: TypedBuiltinEnvelope<"pair_success">): Promise<void> {
|
||||||
@@ -316,6 +334,12 @@ export class YonexusClientRuntime {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.lastPairingFailure = payload.reason;
|
this.lastPairingFailure = payload.reason;
|
||||||
|
// If the server lost our session (race condition), re-announce via hello
|
||||||
|
// so the server creates a new session and we can retry auth.
|
||||||
|
if (payload.reason === "not_paired" && hasClientSecret(this.clientState)) {
|
||||||
|
this.sendHello();
|
||||||
|
return;
|
||||||
|
}
|
||||||
this.phase = "auth_required";
|
this.phase = "auth_required";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
106
plugin/index.ts
106
plugin/index.ts
@@ -39,31 +39,113 @@ export {
|
|||||||
type ClientRuleProcessor
|
type ClientRuleProcessor
|
||||||
} from "./core/rules.js";
|
} 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";
|
||||||
|
import { createYonexusClientRuntime, type YonexusClientRuntime } from "./core/runtime.js";
|
||||||
|
import { createClientRuleRegistry, YonexusClientRuleRegistry } from "./core/rules.js";
|
||||||
|
|
||||||
|
const _G = globalThis as Record<string, unknown>;
|
||||||
|
const _STARTED_KEY = "_yonexusClientStarted";
|
||||||
|
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 {
|
export interface YonexusClientPluginManifest {
|
||||||
readonly name: "Yonexus.Client";
|
readonly name: "Yonexus.Client";
|
||||||
readonly version: string;
|
readonly version: string;
|
||||||
readonly description: string;
|
readonly description: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface YonexusClientPluginRuntime {
|
|
||||||
readonly hooks: readonly [];
|
|
||||||
readonly commands: readonly [];
|
|
||||||
readonly tools: readonly [];
|
|
||||||
}
|
|
||||||
|
|
||||||
const manifest: YonexusClientPluginManifest = {
|
const manifest: YonexusClientPluginManifest = {
|
||||||
name: "Yonexus.Client",
|
name: "Yonexus.Client",
|
||||||
version: "0.1.0",
|
version: "0.1.0",
|
||||||
description: "Yonexus client plugin for cross-instance OpenClaw communication"
|
description: "Yonexus client plugin for cross-instance OpenClaw communication"
|
||||||
};
|
};
|
||||||
|
|
||||||
export function createYonexusClientPlugin(): YonexusClientPluginRuntime {
|
export function createYonexusClientPlugin(api: OpenClawPluginApi): void {
|
||||||
return {
|
// 1. Ensure shared state survives hot-reload — only initialise when absent
|
||||||
hooks: [],
|
if (!(_G[_REGISTRY_KEY] instanceof YonexusClientRuleRegistry)) {
|
||||||
commands: [],
|
_G[_REGISTRY_KEY] = createClientRuleRegistry();
|
||||||
tools: []
|
}
|
||||||
|
if (!Array.isArray(_G[_CALLBACKS_KEY])) {
|
||||||
|
_G[_CALLBACKS_KEY] = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
const ruleRegistry = _G[_REGISTRY_KEY] as YonexusClientRuleRegistry;
|
||||||
|
const onAuthenticatedCallbacks = _G[_CALLBACKS_KEY] as Array<() => void>;
|
||||||
|
|
||||||
|
// 2. Refresh the cross-plugin API object every call so that sendRule / submitPairingCode
|
||||||
|
// closures always read the live runtime from globalThis.
|
||||||
|
_G["__yonexusClient"] = {
|
||||||
|
ruleRegistry,
|
||||||
|
sendRule: (ruleId: string, content: string): boolean =>
|
||||||
|
(_G[_RUNTIME_KEY] as YonexusClientRuntime | undefined)?.sendRuleMessage(ruleId, content) ?? false,
|
||||||
|
submitPairingCode: (code: string): boolean =>
|
||||||
|
(_G[_RUNTIME_KEY] as YonexusClientRuntime | undefined)?.submitPairingCode(code) ?? false,
|
||||||
|
onAuthenticated: onAuthenticatedCallbacks
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// 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(PLUGIN_DATA_DIR, "state.json"));
|
||||||
|
|
||||||
|
const transport = createClientTransport({
|
||||||
|
config,
|
||||||
|
onMessage: (msg) => {
|
||||||
|
(_G[_RUNTIME_KEY] as YonexusClientRuntime | undefined)?.handleMessage(msg).catch((err: unknown) => {
|
||||||
|
console.error("[yonexus-client] message handler error:", err);
|
||||||
|
});
|
||||||
|
},
|
||||||
|
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);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
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 };
|
export { manifest };
|
||||||
|
|||||||
@@ -1,13 +1,19 @@
|
|||||||
{
|
{
|
||||||
|
"id": "yonexus-client",
|
||||||
"name": "Yonexus.Client",
|
"name": "Yonexus.Client",
|
||||||
"version": "0.1.0",
|
|
||||||
"description": "Yonexus client plugin for cross-instance OpenClaw communication",
|
"description": "Yonexus client plugin for cross-instance OpenClaw communication",
|
||||||
"entry": "dist/plugin/index.js",
|
"activation": {
|
||||||
"permissions": [],
|
"onStartup": true
|
||||||
"config": {
|
},
|
||||||
"mainHost": "",
|
"configSchema": {
|
||||||
"identifier": "",
|
"type": "object",
|
||||||
"notifyBotToken": "",
|
"additionalProperties": false,
|
||||||
"adminUserId": ""
|
"properties": {
|
||||||
|
"mainHost": { "type": "string" },
|
||||||
|
"identifier": { "type": "string" },
|
||||||
|
"notifyBotToken": { "type": "string" },
|
||||||
|
"adminUserId": { "type": "string" }
|
||||||
|
},
|
||||||
|
"required": ["mainHost", "identifier"]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
2
protocol
2
protocol
Submodule protocol updated: 9232aa7c17...2611304084
@@ -29,6 +29,7 @@ if (mode === "install") {
|
|||||||
fs.rmSync(targetDir, { recursive: true, force: true });
|
fs.rmSync(targetDir, { recursive: true, force: true });
|
||||||
fs.cpSync(sourceDist, path.join(targetDir, "dist"), { recursive: true });
|
fs.cpSync(sourceDist, path.join(targetDir, "dist"), { recursive: true });
|
||||||
fs.copyFileSync(path.join(repoRoot, "plugin", "openclaw.plugin.json"), path.join(targetDir, "openclaw.plugin.json"));
|
fs.copyFileSync(path.join(repoRoot, "plugin", "openclaw.plugin.json"), path.join(targetDir, "openclaw.plugin.json"));
|
||||||
|
fs.copyFileSync(path.join(repoRoot, "package.json"), path.join(targetDir, "package.json"));
|
||||||
console.log(`Installed ${pluginName} to ${targetDir}`);
|
console.log(`Installed ${pluginName} to ${targetDir}`);
|
||||||
process.exit(0);
|
process.exit(0);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user