feat: stabilize HarborForge monitor sidecar plugin #2

Merged
hzhang merged 10 commits from feat/telemetry-sidecar-v2 into main 2026-03-20 09:18:37 +00:00
Showing only changes of commit 14ed887ce3 - Show all commits

View File

@@ -185,8 +185,59 @@ async function collectOpenclawStatus() {
/**
* Get list of OpenClaw agents from local state
*/
function extractJsonPrefix(text) {
const trimmed = text.trim();
if (!trimmed) return null;
const startsWith = trimmed[0];
if (startsWith !== '[' && startsWith !== '{') return null;
let depth = 0;
let inString = false;
let escape = false;
for (let i = 0; i < trimmed.length; i += 1) {
const ch = trimmed[i];
if (escape) {
escape = false;
continue;
}
if (ch === '\\') {
escape = true;
continue;
}
if (ch === '"') {
inString = !inString;
continue;
}
if (inString) continue;
if (ch === '[' || ch === '{') depth += 1;
if (ch === ']' || ch === '}') depth -= 1;
if (depth === 0) {
return trimmed.slice(0, i + 1);
}
}
return null;
}
async function getOpenclawAgents() {
try {
try {
const { stdout } = await execAsync('openclaw agents list --json 2>/dev/null');
const jsonPrefix = extractJsonPrefix(stdout);
if (jsonPrefix) {
const agents = JSON.parse(jsonPrefix);
if (Array.isArray(agents)) {
return agents.map((agent) => ({
id: agent.id,
name: agent.name || agent.id,
status: agent.isDefault ? 'default' : 'configured',
}));
}
}
} catch (err) {
log.debug('Failed to get agents from `openclaw agents list --json`:', err.message);
}
const agentConfigPath = `${CONFIG.openclawPath}/agents.json`;
try {
await access(agentConfigPath, constants.R_OK);