diff --git a/server/telemetry.mjs b/server/telemetry.mjs index be3a14b..fcb6e6d 100644 --- a/server/telemetry.mjs +++ b/server/telemetry.mjs @@ -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);