Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 | 'use client' /** * Push-based construction notifier — replaces polling in useEuclidVoice. * * Three notification methods with different debounce strategies: * - notifyConstruction: immediate, cancels pending tool timer * - notifyToolState: 300ms debounce * - notifyDragEnd: 600ms debounce with screenshot * * Each call serializes current state and pushes to the active voice session * and injects construction events into the shared chat message history. */ import { useCallback, useRef } from 'react' import type { ConstructionState, ActiveTool, CompassPhase, StraightedgePhase, ExtendPhase, MacroPhase, PropositionStep, } from '../types' import type { ProofFact } from '../engine/facts' import type { UseVoiceCallReturn } from '@/lib/voice/types' import type { ChatMessage } from '@/lib/character/types' import { generateId } from '@/lib/character/useCharacterChat' import type { ConstructionEventBus } from './ConstructionEventBus' import { captureScreenshot } from '@/lib/character/captureScreenshot' import { serializeConstructionGraph, serializeProofFacts, serializeToolState, type ToolStateInfo, } from './serializeProofState' export interface ConstructionEvent { /** Human-readable description of what the user did */ action: string /** true = voice Euclid responds, false = silent update */ shouldPrompt: boolean /** When true, replace trailing event messages in chat instead of appending. * Use for rapid-fire events (e.g. topology changes during drag) to avoid spam. */ collapseInChat?: boolean /** When true, signals a full construction reset (new canvas). */ reset?: boolean } export interface NotifierLogEntry { timestamp: number type: 'construction' | 'tool' | 'drag' action: string delivered: boolean } const MAX_LOG_ENTRIES = 20 export interface ConstructionNotifier { notifyConstruction(event: ConstructionEvent): void notifyToolState(): void notifyDragEnd(pointLabel?: string): void /** Recent push log for debug panel */ recentEvents: NotifierLogEntry[] } interface UseConstructionNotifierOptions { canvasRef: React.RefObject<HTMLCanvasElement | null> constructionRef: React.RefObject<ConstructionState> proofFactsRef: React.RefObject<ProofFact[]> currentStepRef: React.RefObject<number> steps: PropositionStep[] isComplete: boolean activeToolRef: React.RefObject<ActiveTool> compassPhaseRef: React.RefObject<CompassPhase> straightedgePhaseRef: React.RefObject<StraightedgePhase> extendPhaseRef: React.RefObject<ExtendPhase> macroPhaseRef: React.RefObject<MacroPhase> dragPointIdRef: React.RefObject<string | null> voiceCallRef: React.RefObject<UseVoiceCallReturn | null> /** Inject a construction event into the shared chat message history */ addMessage: (msg: ChatMessage) => void /** Replace trailing events with one message (or remove them if null) */ setTrailingEvent: (msg: ChatMessage | null) => void /** Optional event bus — emits construction events for external subscribers */ eventBus?: ConstructionEventBus } export function useConstructionNotifier( options: UseConstructionNotifierOptions ): React.MutableRefObject<ConstructionNotifier> { const { canvasRef, constructionRef, proofFactsRef, currentStepRef, steps, isComplete, activeToolRef, compassPhaseRef, straightedgePhaseRef, extendPhaseRef, macroPhaseRef, dragPointIdRef, voiceCallRef, addMessage, setTrailingEvent, eventBus, } = options const toolTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null) const dragTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null) const logRef = useRef<NotifierLogEntry[]>([]) const readToolState = useCallback( (): ToolStateInfo => ({ activeTool: activeToolRef.current ?? 'compass', compassPhase: compassPhaseRef.current ?? { tag: 'idle' }, straightedgePhase: straightedgePhaseRef.current ?? { tag: 'idle' }, extendPhase: extendPhaseRef.current ?? { tag: 'idle' }, macroPhase: macroPhaseRef.current ?? { tag: 'idle' }, dragPointId: dragPointIdRef.current ?? null, }), [ activeToolRef, compassPhaseRef, straightedgePhaseRef, extendPhaseRef, macroPhaseRef, dragPointIdRef, ] ) const addLogEntry = useCallback( (type: NotifierLogEntry['type'], action: string, delivered: boolean) => { logRef.current = [ ...logRef.current.slice(-(MAX_LOG_ENTRIES - 1)), { timestamp: Date.now(), type, action, delivered }, ] }, [] ) const sendToVoice = useCallback( ( action: string, shouldPrompt: boolean, /** Pass a pre-captured screenshot string, true to capture now, or false/null to skip */ screenshotOrFlag: string | boolean | null ) => { const vc = voiceCallRef.current console.log( '[notifier] sendToVoice: vc=%s, state=%s, action=%s', vc ? 'exists' : 'null', vc?.state ?? 'N/A', action ) if (!vc || vc.state !== 'active') return false const emptyState = { elements: [], nextLabelIndex: 0, nextColorIndex: 0 } as ConstructionState const state = constructionRef.current ?? emptyState const facts = proofFactsRef.current ?? [] const step = currentStepRef.current ?? 0 const toolInfo = readToolState() const graph = serializeConstructionGraph(state) const factsText = serializeProofFacts(facts) const toolText = serializeToolState(toolInfo, state, step, steps, isComplete) // Reuse pre-captured screenshot or capture fresh if true was passed const screenshot = typeof screenshotOrFlag === 'string' ? screenshotOrFlag : screenshotOrFlag && canvasRef.current ? captureScreenshot(canvasRef.current) : null // For prompted updates, include the full state dump so the model has // complete context when generating a response. For silent updates, // send only a compact note — repeated full dumps overwhelm the model // and it loses track of what's current vs. stale. let text: string if (shouldPrompt) { text = `[CONSTRUCTION CHANGED: ${action} — acknowledge briefly what the student did and guide them on what to do next. Keep it to 1-2 sentences.]\n\n${toolText}\n\n${graph}\n\n=== Proven Facts ===\n${factsText}` } else { text = `[SILENT STATE UPDATE — do not speak. ${action}]` } vc.sendContextUpdate(text, screenshot, shouldPrompt) return true }, [ voiceCallRef, constructionRef, proofFactsRef, currentStepRef, steps, isComplete, canvasRef, readToolState, ] ) const notifyConstruction = useCallback( (event: ConstructionEvent) => { // Cancel any pending tool timer — construction is more important if (toolTimerRef.current) { clearTimeout(toolTimerRef.current) toolTimerRef.current = null } // Capture screenshot for both chat and voice const screenshot = canvasRef.current ? captureScreenshot(canvasRef.current) : null // Inject event into shared chat history so the LLM sees it at the right temporal position. // collapseInChat replaces trailing events (for rapid-fire drag topology changes). const msg: ChatMessage = { id: generateId(), role: 'user', content: event.action, timestamp: Date.now(), isEvent: true, imageDataUrl: screenshot ?? undefined, } if (event.collapseInChat) { setTrailingEvent(msg) } else { addMessage(msg) } // Send to voice immediately (reuse captured screenshot) const delivered = sendToVoice(event.action, event.shouldPrompt, screenshot) addLogEntry('construction', event.action, delivered) // Emit to event bus for external subscribers (heckler, proof ledger, etc.) eventBus?.emit(event) console.log('[notifier] construction: %s (delivered=%s)', event.action, delivered) }, [sendToVoice, addMessage, setTrailingEvent, addLogEntry, canvasRef, eventBus] ) const notifyToolState = useCallback(() => { if (toolTimerRef.current) clearTimeout(toolTimerRef.current) toolTimerRef.current = setTimeout(() => { toolTimerRef.current = null const delivered = sendToVoice('tool state change', false, false) addLogEntry('tool', 'tool state change', delivered) console.log('[notifier] tool state (delivered=%s)', delivered) }, 300) }, [sendToVoice, addLogEntry]) const notifyDragEnd = useCallback( (pointLabel?: string) => { if (dragTimerRef.current) clearTimeout(dragTimerRef.current) dragTimerRef.current = setTimeout(() => { dragTimerRef.current = null const action = pointLabel ? `Dragged point ${pointLabel} to a new position` : 'Dragged a point to a new position' const screenshot = canvasRef.current ? captureScreenshot(canvasRef.current) : null addMessage({ id: generateId(), role: 'user', content: action, timestamp: Date.now(), isEvent: true, imageDataUrl: screenshot ?? undefined, }) const delivered = sendToVoice(action, true, screenshot) addLogEntry('drag', 'drag end', delivered) console.log('[notifier] drag end (delivered=%s)', delivered) }, 600) }, [sendToVoice, addMessage, addLogEntry, canvasRef] ) const notifierRef = useRef<ConstructionNotifier>({ notifyConstruction, notifyToolState, notifyDragEnd, recentEvents: logRef.current, }) // Keep the ref up to date notifierRef.current = { notifyConstruction, notifyToolState, notifyDragEnd, recentEvents: logRef.current, } return notifierRef } |