Implementation:23blocks OS Ai maestro Use Agents Hook
| Knowledge Sources | |
|---|---|
| Domains | UI, Agent_Orchestration |
| Last Updated | 2026-10-26 18:00 GMT |
Overview
Concrete tool for fetching and aggregating agents across self and peer hosts with timeout-based fail-fast and cache fallback, provided by AI Maestro's `hooks/useAgents.ts`.
Description
The useAgents hook is the primary client-side data source for the agent fleet. It fetches agents from the self host and all configured peer hosts concurrently, using differentiated timeouts (8s for self, 3s for peers to fail fast). For peer hosts that time out or error, it falls back to a TTL-based client cache (`lib/agent-cache.ts`) so the dashboard remains usable during network partitions. Results are aggregated: system agents (prefixed `_aim-`) are filtered out, agents are sorted online-first then alphabetically, and stats (total, online, offline, orphans, cached) are computed in a single loop. The hook refreshes on a 10-second interval and exposes `refreshAgents` for manual triggers.
Usage
Call this hook at the top level of the dashboard to obtain the agent list, aggregated stats, loading state, and per-host error map. Use `onlineAgents` for selectable agents and `refreshAgents()` after mutations (create/delete/wake).
Code Reference
Source Location
- Repository: ai-maestro
- File: hooks/useAgents.ts
- Lines: L1-L454
Signature
interface UseAgentsResult {
agents: Agent[]
stats: AggregatedStats
loading: boolean
error: string | null
hostErrors: Record<string, Error>
refreshAgents: () => void
onlineAgents: Agent[]
}
export function useAgents(): UseAgentsResult
Import
import { useAgents } from '@/hooks/useAgents'
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| (none) | — | Hook reads host list from useHosts internally |
Outputs
| Name | Type | Description |
|---|---|---|
| agents | Agent[] | Sorted (online-first, alpha) non-system agents from all hosts |
| stats | AggregatedStats | total, online, offline, orphans, newlyRegistered, cached |
| loading | boolean | True during initial fetch |
| error | null | Top-level error message |
| hostErrors | Record<string, Error> | Per-host fetch errors (peers may be cached) |
| refreshAgents | () => void | Trigger an immediate re-fetch |
| onlineAgents | Agent[] | Subset with session.status === 'online' |
Usage Examples
Basic fleet display
import { useAgents } from '@/hooks/useAgents'
function AgentCount() {
const { agents, stats, loading } = useAgents()
if (loading) return <p>Loading fleet…</p>
return (
<p>{stats.online}/{stats.total} agents online ({stats.cached} cached)</p>
)
}
Manual refresh after mutation
const { agents, refreshAgents } = useAgents()
async function handleDelete(id: string) {
await fetch(`/api/agents/${id}`, { method: 'DELETE' })
refreshAgents() // Re-fetch immediately instead of waiting for the 10s poll
}
Related Pages
Implements Principle
- Principle:23blocks_OS_Ai_maestro_Server_Feed_Aggregation
- Principle:23blocks_OS_Ai_maestro_Eventual_Consistency