fix: replace fake random agent states with real gateway session data#61
fix: replace fake random agent states with real gateway session data#61openclawuser4158 wants to merge 1 commit into
Conversation
- Replace random hash-based fake agent states in loadOfficePresenceSnapshot() - Query OpenClaw gateway /tools/invoke sessions_list to get real session states - Map gateway session status to office agent states: - running → working - error/failed → error - done/idle/other → idle - Read gateway auth token dynamically from openclaw.json (no hardcoded creds) - Handle missing tokens gracefully by falling back to idle state - Make loadOfficePresenceSnapshot() async to support HTTP calls - Update /api/office/presence route to handle async function - Maintain existing function signature and types for compatibility
iamlukethedev
left a comment
There was a problem hiding this comment.
Two correctness issues block this for me:
- The new
meetingdetection appears to attribute subagent sessions to the wrong agent. - The gateway config lookup is narrower than the rest of the app and will miss env/settings-backed gateway setups.
I’d also like to see coverage added for the new gateway-backed presence path, since there are currently no tests around this behavior.
| // Count child sessions per agent (subagent activity implies "meeting"/collaboration) | ||
| const childCountByAgent = new Map<string, number>(); | ||
| sessions.forEach(session => { | ||
| if (session.agentId && session.kind === "subagent") { |
There was a problem hiding this comment.
I think this misattributes subagent activity. extractAgentIdFromSessionKey() only parses the owner encoded in the session key (agent:{agentId}:{sessionType}), so for a subagent session it will give you the subagent's own id, not the parent/main agent you want to mark as meeting. As written, childCountByAgent is likely counting children against themselves, which means the collaboration signal won't land on the parent agent as intended.
| const agentList = readConfigAgentList(config); | ||
|
|
||
| // Get gateway configuration for API access | ||
| const gateway = config?.gateway as Record<string, unknown> | undefined; |
There was a problem hiding this comment.
This only reads gateway connection details from openclaw.json, but the rest of the app also supports env/settings-backed gateway defaults. In those setups, the office UI can connect successfully while this endpoint still forces everyone to idle, which is a behavior regression from the broader gateway configuration model already in the repo.
Problem
The office presence system in
src/lib/office/presence.tsgenerates fake agent states using a seeded random hash that changes every 2 seconds. Agents randomly flip between "working", "idle", "meeting", and "error" with no connection to actual gateway runtime state. This makes the 3D office visualization misleading — agents appear busy or errored when they're actually idle, and idle when they're actually working.Solution
Rewrites
loadOfficePresenceSnapshot()to query the real OpenClaw gateway for active session data via the/tools/invokeendpoint (sessions_listtool).Changes
src/lib/office/presence.ts: Replaced random state generation with actual gateway session queriesopenclaw.json(no hardcoded credentials)running→working,error/failed→error, else →idlemeetingstate (collaboration)idleasyncto support HTTP callssrc/app/api/office/presence/route.ts: Addedawaitfor the now-async functionArchitecture note
The existing codebase queries sessions via the WebSocket RPC client (
client.call("sessions.list", ...)). This change uses the HTTP/tools/invokeendpoint instead, since the presence snapshot is loaded server-side in a Next.js API route without an active WebSocket connection. Both surfaces return the same data.Testing
npm run typecheck✅npm run lint✅npm run test— 893 passed, 2 failed (pre-existing on main, not introduced by this PR)Security
127.0.0.1)