refactor(plugin): read config via api.pluginConfig and api.config
- Mirror dirigent plugin registration pattern - Read base config from api.pluginConfig - Read live config from api.config.plugins.entries.harborforge-monitor.config - Support gateway_start/gateway_stop lifecycle only - Compile nested plugin/core files
This commit is contained in:
39
plugin/core/live-config.ts
Normal file
39
plugin/core/live-config.ts
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
export interface HarborForgeMonitorConfig {
|
||||||
|
enabled?: boolean;
|
||||||
|
backendUrl?: string;
|
||||||
|
identifier?: string;
|
||||||
|
apiKey?: string;
|
||||||
|
reportIntervalSec?: number;
|
||||||
|
httpFallbackIntervalSec?: number;
|
||||||
|
logLevel?: 'debug' | 'info' | 'warn' | 'error';
|
||||||
|
}
|
||||||
|
|
||||||
|
interface OpenClawPluginApiLike {
|
||||||
|
config?: Record<string, unknown>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getLivePluginConfig(
|
||||||
|
api: OpenClawPluginApiLike,
|
||||||
|
fallback: HarborForgeMonitorConfig
|
||||||
|
): HarborForgeMonitorConfig {
|
||||||
|
const root = (api.config as Record<string, unknown>) || {};
|
||||||
|
const plugins = (root.plugins as Record<string, unknown>) || {};
|
||||||
|
const entries = (plugins.entries as Record<string, unknown>) || {};
|
||||||
|
const entry = (entries['harborforge-monitor'] as Record<string, unknown>) || {};
|
||||||
|
const cfg = (entry.config as Record<string, unknown>) || {};
|
||||||
|
|
||||||
|
if (Object.keys(cfg).length > 0 || Object.keys(entry).length > 0) {
|
||||||
|
return {
|
||||||
|
...fallback,
|
||||||
|
...cfg,
|
||||||
|
enabled:
|
||||||
|
typeof cfg.enabled === 'boolean'
|
||||||
|
? cfg.enabled
|
||||||
|
: typeof entry.enabled === 'boolean'
|
||||||
|
? entry.enabled
|
||||||
|
: fallback.enabled,
|
||||||
|
} as HarborForgeMonitorConfig;
|
||||||
|
}
|
||||||
|
|
||||||
|
return fallback;
|
||||||
|
}
|
||||||
300
plugin/index.ts
300
plugin/index.ts
@@ -6,21 +6,7 @@
|
|||||||
import { spawn } from 'child_process';
|
import { spawn } from 'child_process';
|
||||||
import { join } from 'path';
|
import { join } from 'path';
|
||||||
import { existsSync } from 'fs';
|
import { existsSync } from 'fs';
|
||||||
|
import { getLivePluginConfig, type HarborForgeMonitorConfig } from './core/live-config';
|
||||||
interface PluginConfig {
|
|
||||||
enabled?: boolean;
|
|
||||||
backendUrl?: string;
|
|
||||||
identifier?: string;
|
|
||||||
apiKey?: string;
|
|
||||||
reportIntervalSec?: number;
|
|
||||||
httpFallbackIntervalSec?: number;
|
|
||||||
logLevel?: 'debug' | 'info' | 'warn' | 'error';
|
|
||||||
}
|
|
||||||
|
|
||||||
interface PluginEntryLike {
|
|
||||||
enabled?: boolean;
|
|
||||||
config?: PluginConfig;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface PluginAPI {
|
interface PluginAPI {
|
||||||
logger: {
|
logger: {
|
||||||
@@ -30,159 +16,169 @@ interface PluginAPI {
|
|||||||
warn: (...args: any[]) => void;
|
warn: (...args: any[]) => void;
|
||||||
};
|
};
|
||||||
version?: string;
|
version?: string;
|
||||||
isRunning?: () => boolean;
|
config?: Record<string, unknown>;
|
||||||
|
pluginConfig?: Record<string, unknown>;
|
||||||
on: (event: string, handler: () => void) => void;
|
on: (event: string, handler: () => void) => void;
|
||||||
registerTool: (factory: (ctx: any) => any) => void;
|
registerTool: (factory: (ctx: any) => any) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function register(api: PluginAPI, rawConfig: PluginConfig | PluginEntryLike | undefined) {
|
export default {
|
||||||
const logger = api.logger || {
|
id: 'harborforge-monitor',
|
||||||
info: (...args: any[]) => console.log('[HF-Monitor]', ...args),
|
name: 'HarborForge Monitor',
|
||||||
error: (...args: any[]) => console.error('[HF-Monitor]', ...args),
|
register(api: PluginAPI) {
|
||||||
debug: (...args: any[]) => console.debug('[HF-Monitor]', ...args),
|
const logger = api.logger || {
|
||||||
warn: (...args: any[]) => console.warn('[HF-Monitor]', ...args),
|
info: (...args: any[]) => console.log('[HF-Monitor]', ...args),
|
||||||
};
|
error: (...args: any[]) => console.error('[HF-Monitor]', ...args),
|
||||||
|
debug: (...args: any[]) => console.debug('[HF-Monitor]', ...args),
|
||||||
const entryLike = rawConfig as PluginEntryLike | undefined;
|
warn: (...args: any[]) => console.warn('[HF-Monitor]', ...args),
|
||||||
const config: PluginConfig = entryLike?.config
|
|
||||||
? {
|
|
||||||
...entryLike.config,
|
|
||||||
enabled: entryLike.config.enabled ?? entryLike.enabled,
|
|
||||||
}
|
|
||||||
: ((rawConfig as PluginConfig | undefined) ?? {});
|
|
||||||
|
|
||||||
const enabled = config.enabled !== false;
|
|
||||||
|
|
||||||
logger.info('HarborForge Monitor plugin config resolved', {
|
|
||||||
enabled,
|
|
||||||
hasApiKey: Boolean(config.apiKey),
|
|
||||||
backendUrl: config.backendUrl ?? null,
|
|
||||||
identifier: config.identifier ?? null,
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!enabled) {
|
|
||||||
logger.info('HarborForge Monitor plugin disabled');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!config.apiKey) {
|
|
||||||
logger.warn('Missing config: apiKey');
|
|
||||||
logger.warn('API authentication will fail. Generate apiKey from HarborForge Monitor admin.');
|
|
||||||
}
|
|
||||||
|
|
||||||
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_API_KEY: config.apiKey || '',
|
|
||||||
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], {
|
const baseConfig: HarborForgeMonitorConfig = {
|
||||||
env,
|
enabled: true,
|
||||||
detached: false,
|
backendUrl: 'https://monitor.hangman-lab.top',
|
||||||
stdio: ['ignore', 'pipe', 'pipe']
|
identifier: '',
|
||||||
});
|
reportIntervalSec: 30,
|
||||||
|
httpFallbackIntervalSec: 60,
|
||||||
|
logLevel: 'info',
|
||||||
|
...(api.pluginConfig || {}),
|
||||||
|
};
|
||||||
|
|
||||||
sidecar.stdout?.on('data', (data: Buffer) => {
|
const serverPath = join(__dirname, 'server', 'telemetry.mjs');
|
||||||
logger.info('[telemetry]', data.toString().trim());
|
let sidecar: ReturnType<typeof spawn> | null = null;
|
||||||
});
|
|
||||||
|
|
||||||
sidecar.stderr?.on('data', (data: Buffer) => {
|
function resolveConfig() {
|
||||||
logger.error('[telemetry]', data.toString().trim());
|
return getLivePluginConfig(api, baseConfig);
|
||||||
});
|
|
||||||
|
|
||||||
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...');
|
function startSidecar() {
|
||||||
sidecar.kill('SIGTERM');
|
const live = resolveConfig();
|
||||||
|
const enabled = live.enabled !== false;
|
||||||
|
|
||||||
const timeout = setTimeout(() => {
|
logger.info('HarborForge Monitor plugin config resolved', {
|
||||||
if (sidecar && !sidecar.killed) {
|
enabled,
|
||||||
logger.warn('Telemetry server did not exit gracefully, forcing kill');
|
hasApiKey: Boolean(live.apiKey),
|
||||||
sidecar.kill('SIGKILL');
|
backendUrl: live.backendUrl ?? null,
|
||||||
|
identifier: live.identifier ?? null,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!enabled) {
|
||||||
|
logger.info('HarborForge Monitor plugin disabled');
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}, 5000);
|
|
||||||
|
|
||||||
sidecar.on('exit', () => {
|
if (sidecar) {
|
||||||
clearTimeout(timeout);
|
logger.debug('Sidecar already running');
|
||||||
});
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Hook into Gateway lifecycle
|
if (!live.apiKey) {
|
||||||
api.on('gateway_start', () => {
|
logger.warn('Missing config: apiKey');
|
||||||
logger.info('Gateway starting, starting telemetry server...');
|
logger.warn('API authentication will fail. Generate apiKey from HarborForge Monitor admin.');
|
||||||
startSidecar();
|
}
|
||||||
});
|
|
||||||
|
|
||||||
api.on('gateway_stop', () => {
|
if (!existsSync(serverPath)) {
|
||||||
logger.info('Gateway stopping, stopping telemetry server...');
|
logger.error('Telemetry server not found:', serverPath);
|
||||||
stopSidecar();
|
return;
|
||||||
});
|
}
|
||||||
|
|
||||||
// Handle process signals
|
logger.info('Starting HarborForge Monitor telemetry server...');
|
||||||
process.on('SIGTERM', stopSidecar);
|
|
||||||
process.on('SIGINT', stopSidecar);
|
|
||||||
|
|
||||||
// Register status tool
|
const env = {
|
||||||
api.registerTool(() => ({
|
...process.env,
|
||||||
name: 'harborforge_monitor_status',
|
HF_MONITOR_BACKEND_URL: live.backendUrl || 'https://monitor.hangman-lab.top',
|
||||||
description: 'Get HarborForge Monitor plugin status',
|
HF_MONITOR_IDENTIFIER: live.identifier || '',
|
||||||
parameters: {
|
HF_MONITOR_API_KEY: live.apiKey || '',
|
||||||
type: 'object',
|
HF_MONITOR_REPORT_INTERVAL: String(live.reportIntervalSec || 30),
|
||||||
properties: {}
|
HF_MONITOR_HTTP_FALLBACK_INTERVAL: String(live.httpFallbackIntervalSec || 60),
|
||||||
},
|
HF_MONITOR_LOG_LEVEL: live.logLevel || 'info',
|
||||||
async execute() {
|
OPENCLAW_PATH: process.env.OPENCLAW_PATH || join(process.env.HOME || '/root', '.openclaw'),
|
||||||
return {
|
OPENCLAW_VERSION: api.version || 'unknown',
|
||||||
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');
|
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);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
api.on('gateway_start', () => {
|
||||||
|
logger.info('gateway_start received, starting telemetry server...');
|
||||||
|
startSidecar();
|
||||||
|
});
|
||||||
|
|
||||||
|
api.on('gateway_stop', () => {
|
||||||
|
logger.info('gateway_stop received, stopping telemetry server...');
|
||||||
|
stopSidecar();
|
||||||
|
});
|
||||||
|
|
||||||
|
process.on('SIGTERM', stopSidecar);
|
||||||
|
process.on('SIGINT', stopSidecar);
|
||||||
|
|
||||||
|
api.registerTool(() => ({
|
||||||
|
name: 'harborforge_monitor_status',
|
||||||
|
description: 'Get HarborForge Monitor plugin status',
|
||||||
|
parameters: {
|
||||||
|
type: 'object',
|
||||||
|
properties: {},
|
||||||
|
},
|
||||||
|
async execute() {
|
||||||
|
const live = resolveConfig();
|
||||||
|
return {
|
||||||
|
enabled: live.enabled !== false,
|
||||||
|
sidecarRunning: sidecar !== null && sidecar.exitCode === null,
|
||||||
|
pid: sidecar?.pid || null,
|
||||||
|
config: {
|
||||||
|
backendUrl: live.backendUrl,
|
||||||
|
identifier: live.identifier || 'auto-detected',
|
||||||
|
reportIntervalSec: live.reportIntervalSec,
|
||||||
|
hasApiKey: Boolean(live.apiKey),
|
||||||
|
},
|
||||||
|
};
|
||||||
|
},
|
||||||
|
}));
|
||||||
|
|
||||||
|
logger.info('HarborForge Monitor plugin registered');
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|||||||
@@ -12,6 +12,6 @@
|
|||||||
"declarationMap": true,
|
"declarationMap": true,
|
||||||
"sourceMap": true
|
"sourceMap": true
|
||||||
},
|
},
|
||||||
"include": ["*.ts"],
|
"include": ["**/*.ts"],
|
||||||
"exclude": ["node_modules"]
|
"exclude": ["node_modules"]
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user