feat: reorganize to standard OpenClaw plugin structure
New structure:
├── package.json # Root package
├── README.md # Documentation
├── plugin/ # OpenClaw plugin
│ ├── openclaw.plugin.json # Plugin manifest
│ ├── index.ts # Plugin entry (TypeScript)
│ ├── package.json
│ └── tsconfig.json
├── server/ # Telemetry sidecar
│ └── telemetry.mjs
├── skills/ # OpenClaw skills
└── scripts/
└── install.mjs # Installation script
Matches PaddedCell project structure.
Provides install.mjs with build/install/configure/uninstall commands.
This commit is contained in:
178
plugin/index.ts
Normal file
178
plugin/index.ts
Normal file
@@ -0,0 +1,178 @@
|
||||
/**
|
||||
* HarborForge Monitor Plugin for OpenClaw
|
||||
*
|
||||
* Manages sidecar lifecycle and provides monitor-related tools.
|
||||
*/
|
||||
import { spawn } from 'child_process';
|
||||
import { fileURLToPath } from 'url';
|
||||
import { dirname, join } from 'path';
|
||||
import { existsSync } from 'fs';
|
||||
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = dirname(__filename);
|
||||
|
||||
interface PluginConfig {
|
||||
enabled?: boolean;
|
||||
backendUrl?: string;
|
||||
identifier?: string;
|
||||
challengeUuid?: string;
|
||||
reportIntervalSec?: number;
|
||||
httpFallbackIntervalSec?: number;
|
||||
logLevel?: 'debug' | 'info' | 'warn' | 'error';
|
||||
}
|
||||
|
||||
interface PluginAPI {
|
||||
logger: {
|
||||
info: (...args: any[]) => void;
|
||||
error: (...args: any[]) => void;
|
||||
debug: (...args: any[]) => void;
|
||||
warn: (...args: any[]) => void;
|
||||
};
|
||||
version?: string;
|
||||
isRunning?: () => boolean;
|
||||
on: (event: string, handler: () => void) => void;
|
||||
registerTool: (factory: (ctx: any) => any) => void;
|
||||
}
|
||||
|
||||
export default function register(api: PluginAPI, config: PluginConfig) {
|
||||
const logger = api.logger || {
|
||||
info: (...args: any[]) => console.log('[HF-Monitor]', ...args),
|
||||
error: (...args: any[]) => console.error('[HF-Monitor]', ...args),
|
||||
debug: (...args: any[]) => console.debug('[HF-Monitor]', ...args),
|
||||
warn: (...args: any[]) => console.warn('[HF-Monitor]', ...args),
|
||||
};
|
||||
|
||||
if (!config?.enabled) {
|
||||
logger.info('HarborForge Monitor plugin disabled');
|
||||
return;
|
||||
}
|
||||
|
||||
if (!config.challengeUuid) {
|
||||
logger.error('Missing required config: challengeUuid');
|
||||
logger.error('Please register server in HarborForge Monitor first');
|
||||
return;
|
||||
}
|
||||
|
||||
const serverPath = join(__dirname, '..', 'server', 'telemetry.mjs');
|
||||
|
||||
if (!existsSync(serverPath)) {
|
||||
logger.error('Telemetry server not found:', serverPath);
|
||||
return;
|
||||
}
|
||||
|
||||
let sidecar: ReturnType<typeof spawn> | null = null;
|
||||
|
||||
function startSidecar() {
|
||||
if (sidecar) {
|
||||
logger.debug('Sidecar already running');
|
||||
return;
|
||||
}
|
||||
|
||||
logger.info('Starting HarborForge Monitor telemetry server...');
|
||||
|
||||
const env = {
|
||||
...process.env,
|
||||
HF_MONITOR_BACKEND_URL: config.backendUrl || 'https://monitor.hangman-lab.top',
|
||||
HF_MONITOR_IDENTIFIER: config.identifier || '',
|
||||
HF_MONITOR_CHALLENGE_UUID: config.challengeUuid,
|
||||
HF_MONITOR_REPORT_INTERVAL: String(config.reportIntervalSec || 30),
|
||||
HF_MONITOR_HTTP_FALLBACK_INTERVAL: String(config.httpFallbackIntervalSec || 60),
|
||||
HF_MONITOR_LOG_LEVEL: config.logLevel || 'info',
|
||||
OPENCLAW_PATH: process.env.OPENCLAW_PATH || join(process.env.HOME || '/root', '.openclaw'),
|
||||
OPENCLAW_VERSION: api.version || 'unknown',
|
||||
};
|
||||
|
||||
sidecar = spawn('node', [serverPath], {
|
||||
env,
|
||||
detached: false,
|
||||
stdio: ['ignore', 'pipe', 'pipe']
|
||||
});
|
||||
|
||||
sidecar.stdout?.on('data', (data: Buffer) => {
|
||||
logger.info('[telemetry]', data.toString().trim());
|
||||
});
|
||||
|
||||
sidecar.stderr?.on('data', (data: Buffer) => {
|
||||
logger.error('[telemetry]', data.toString().trim());
|
||||
});
|
||||
|
||||
sidecar.on('exit', (code, signal) => {
|
||||
logger.info(`Telemetry server exited (code: ${code}, signal: ${signal})`);
|
||||
sidecar = null;
|
||||
});
|
||||
|
||||
sidecar.on('error', (err: Error) => {
|
||||
logger.error('Failed to start telemetry server:', err.message);
|
||||
sidecar = null;
|
||||
});
|
||||
|
||||
logger.info('Telemetry server started with PID:', sidecar.pid);
|
||||
}
|
||||
|
||||
function stopSidecar() {
|
||||
if (!sidecar) {
|
||||
logger.debug('Telemetry server not running');
|
||||
return;
|
||||
}
|
||||
|
||||
logger.info('Stopping HarborForge Monitor telemetry server...');
|
||||
sidecar.kill('SIGTERM');
|
||||
|
||||
const timeout = setTimeout(() => {
|
||||
if (sidecar && !sidecar.killed) {
|
||||
logger.warn('Telemetry server did not exit gracefully, forcing kill');
|
||||
sidecar.kill('SIGKILL');
|
||||
}
|
||||
}, 5000);
|
||||
|
||||
sidecar.on('exit', () => {
|
||||
clearTimeout(timeout);
|
||||
});
|
||||
}
|
||||
|
||||
// Hook into Gateway lifecycle
|
||||
api.on('gateway:start', () => {
|
||||
logger.info('Gateway starting, starting telemetry server...');
|
||||
startSidecar();
|
||||
});
|
||||
|
||||
api.on('gateway:stop', () => {
|
||||
logger.info('Gateway stopping, stopping telemetry server...');
|
||||
stopSidecar();
|
||||
});
|
||||
|
||||
// Handle process signals
|
||||
process.on('SIGTERM', stopSidecar);
|
||||
process.on('SIGINT', stopSidecar);
|
||||
|
||||
// Start immediately if Gateway is already running
|
||||
if (api.isRunning?.()) {
|
||||
startSidecar();
|
||||
} else {
|
||||
setTimeout(() => startSidecar(), 1000);
|
||||
}
|
||||
|
||||
// Register status tool
|
||||
api.registerTool(() => ({
|
||||
name: 'harborforge_monitor_status',
|
||||
description: 'Get HarborForge Monitor plugin status',
|
||||
parameters: {
|
||||
type: 'object',
|
||||
properties: {}
|
||||
},
|
||||
async execute() {
|
||||
return {
|
||||
enabled: true,
|
||||
sidecarRunning: sidecar !== null && sidecar.exitCode === null,
|
||||
pid: sidecar?.pid || null,
|
||||
config: {
|
||||
backendUrl: config.backendUrl,
|
||||
identifier: config.identifier || 'auto-detected',
|
||||
reportIntervalSec: config.reportIntervalSec
|
||||
}
|
||||
};
|
||||
}
|
||||
}));
|
||||
|
||||
logger.info('HarborForge Monitor plugin registered');
|
||||
}
|
||||
48
plugin/openclaw.plugin.json
Normal file
48
plugin/openclaw.plugin.json
Normal file
@@ -0,0 +1,48 @@
|
||||
{
|
||||
"id": "harborforge-monitor",
|
||||
"name": "HarborForge Monitor",
|
||||
"version": "0.1.0",
|
||||
"description": "Server monitoring plugin for HarborForge - streams telemetry to Monitor",
|
||||
"entry": "./index.js",
|
||||
"configSchema": {
|
||||
"type": "object",
|
||||
"additionalProperties": false,
|
||||
"properties": {
|
||||
"enabled": {
|
||||
"type": "boolean",
|
||||
"default": true,
|
||||
"description": "Enable the monitor plugin"
|
||||
},
|
||||
"backendUrl": {
|
||||
"type": "string",
|
||||
"default": "https://monitor.hangman-lab.top",
|
||||
"description": "HarborForge Monitor backend URL"
|
||||
},
|
||||
"identifier": {
|
||||
"type": "string",
|
||||
"description": "Server identifier (auto-detected from hostname if not set)"
|
||||
},
|
||||
"challengeUuid": {
|
||||
"type": "string",
|
||||
"description": "Registration challenge UUID from Monitor"
|
||||
},
|
||||
"reportIntervalSec": {
|
||||
"type": "number",
|
||||
"default": 30,
|
||||
"description": "How often to report metrics (seconds)"
|
||||
},
|
||||
"httpFallbackIntervalSec": {
|
||||
"type": "number",
|
||||
"default": 60,
|
||||
"description": "HTTP heartbeat interval when WS unavailable"
|
||||
},
|
||||
"logLevel": {
|
||||
"type": "string",
|
||||
"enum": ["debug", "info", "warn", "error"],
|
||||
"default": "info",
|
||||
"description": "Logging level"
|
||||
}
|
||||
},
|
||||
"required": ["challengeUuid"]
|
||||
}
|
||||
}
|
||||
15
plugin/package.json
Normal file
15
plugin/package.json
Normal file
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"name": "harborforge-monitor-plugin",
|
||||
"version": "0.1.0",
|
||||
"description": "OpenClaw plugin for HarborForge Monitor",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"build": "tsc",
|
||||
"watch": "tsc --watch"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^20.0.0",
|
||||
"typescript": "^5.0.0"
|
||||
},
|
||||
"license": "MIT"
|
||||
}
|
||||
17
plugin/tsconfig.json
Normal file
17
plugin/tsconfig.json
Normal file
@@ -0,0 +1,17 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "ES2022",
|
||||
"module": "NodeNext",
|
||||
"moduleResolution": "NodeNext",
|
||||
"esModuleInterop": true,
|
||||
"strict": true,
|
||||
"skipLibCheck": true,
|
||||
"outDir": "./",
|
||||
"rootDir": "./",
|
||||
"declaration": true,
|
||||
"declarationMap": true,
|
||||
"sourceMap": true
|
||||
},
|
||||
"include": ["*.ts"],
|
||||
"exclude": ["node_modules"]
|
||||
}
|
||||
Reference in New Issue
Block a user