Implementation:23blocks OS Ai maestro Use Agent Playback Hook
| Knowledge Sources | |
|---|---|
| Domains | UI, Transcript |
| Last Updated | 2026-10-26 18:00 GMT |
Overview
Concrete tool for managing agent transcript playback state — play, pause, seek, speed, and message loading — provided by AI Maestro's `hooks/useAgentPlayback.ts`.
Description
The useAgentPlayback hook manages conversation transcript playback for an agent. It loads a persisted `PlaybackState` (current message index, isPlaying, speed, totalMessages) from the `/api/agents/<id>/playback` endpoint, exposes transport controls (`start`, `pause`, `toggle`, `seek`, `setSpeed`, `next`, `previous`, `reset`), and writes each control change back to the server. Speed is constrained to `[0.5, 1.0, 1.5, 2.0]` (exported as `PLAYBACK_SPEEDS`). An auto-save timer persists state so playback resumes across reloads, and an `isMountedRef` guard prevents state writes after unmount. The hook initializes a default state on load error so the UI always has a usable value.
Usage
Call this hook from a transcript playback component, passing the agent ID and an optional session ID for cross-session playback. Use the returned control functions to wire UI buttons; read `state.currentMessageIndex` and `state.isPlaying` to render the transport.
Code Reference
Source Location
- Repository: ai-maestro
- File: hooks/useAgentPlayback.ts
- Lines: L1-L366
Signature
export const PLAYBACK_SPEEDS = [0.5, 1.0, 1.5, 2.0] as const
export function useAgentPlayback(
agentId: string,
sessionId?: string
): {
state: PlaybackState | null
loading: boolean
error: Error | null
messages: Array<{ role: 'user' | 'assistant' | 'system'; content: string; timestamp?: number; metadata?: Record<string, any> }>
start: () => void
pause: () => void
toggle: () => void
seek: (position: number) => void
setSpeed: (speed: number) => void
reset: () => void
next: () => void
previous: () => void
}
Import
import { useAgentPlayback, PLAYBACK_SPEEDS } from '@/hooks/useAgentPlayback'
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| agentId | string | Yes | Agent ID whose transcript to play back |
| sessionId | string | No | Restrict playback to a specific session |
Outputs
| Name | Type | Description |
|---|---|---|
| state | null | isPlaying, currentMessageIndex, speed, totalMessages |
| messages | Array | Loaded conversation messages (role, content, timestamp) |
| start/pause/toggle | () => void | Transport controls |
| seek | (position: number) => void | Jump to message index (clamped to total) |
| setSpeed | (speed: number) => void | Set speed (0.5–2.0) |
| next/previous | () => void | Step one message forward/back |
Usage Examples
Transport UI
import { useAgentPlayback, PLAYBACK_SPEEDS } from '@/hooks/useAgentPlayback'
function PlaybackControls({ agentId }: { agentId: string }) {
const { state, toggle, next, previous, setSpeed } = useAgentPlayback(agentId)
if (!state) return <p>Loading…</p>
return (
<div>
<button onClick={previous}>◀</button>
<button onClick={toggle}>{state.isPlaying ? '⏸' : '▶'}</button>
<button onClick={next}>▶</button>
<span>{state.currentMessageIndex + 1} / {state.totalMessages}</span>
<select value={state.speed} onChange={(e) => setSpeed(Number(e.target.value))}>
{PLAYBACK_SPEEDS.map((s) => <option key={s} value={s}>{s}×</option>)}
</select>
</div>
)
}