/** * Intelligence Domain Hooks * * React hooks for accessing intelligence state with Valtio. * Provides reactive access to memory, heartbeat, reflection, and identity. */ import { useSnapshot } from 'valtio'; import { intelligenceStore } from './store'; import type { MemoryEntry, CacheStats } from './types'; // === Memory Hooks === /** * Hook to access memories list */ export function useMemories() { const { memories } = useSnapshot(intelligenceStore); return memories as readonly MemoryEntry[]; } /** * Hook to access memory stats */ export function useMemoryStats() { const { memoryStats } = useSnapshot(intelligenceStore); return memoryStats; } /** * Hook to check if memories are loading */ export function useMemoryLoading(): boolean { const { isMemoryLoading } = useSnapshot(intelligenceStore); return isMemoryLoading; } // === Heartbeat Hooks === /** * Hook to access heartbeat config */ export function useHeartbeatConfig() { const { heartbeatConfig } = useSnapshot(intelligenceStore); return heartbeatConfig; } /** * Hook to access heartbeat history */ export function useHeartbeatHistory() { const { heartbeatHistory } = useSnapshot(intelligenceStore); return heartbeatHistory; } /** * Hook to check if heartbeat is running */ export function useHeartbeatRunning(): boolean { const { isHeartbeatRunning } = useSnapshot(intelligenceStore); return isHeartbeatRunning; } // === Compaction Hooks === /** * Hook to access last compaction result */ export function useLastCompaction() { const { lastCompaction } = useSnapshot(intelligenceStore); return lastCompaction; } /** * Hook to access compaction check */ export function useCompactionCheck() { const { compactionCheck } = useSnapshot(intelligenceStore); return compactionCheck; } // === Reflection Hooks === /** * Hook to access reflection state */ export function useReflectionState() { const { reflectionState } = useSnapshot(intelligenceStore); return reflectionState; } /** * Hook to access last reflection result */ export function useLastReflection() { const { lastReflection } = useSnapshot(intelligenceStore); return lastReflection; } // === Identity Hooks === /** * Hook to access current identity */ export function useIdentity() { const { currentIdentity } = useSnapshot(intelligenceStore); return currentIdentity; } /** * Hook to access pending identity proposals */ export function usePendingProposals() { const { pendingProposals } = useSnapshot(intelligenceStore); return pendingProposals; } // === Cache Hooks === /** * Hook to access cache stats */ export function useCacheStats(): CacheStats { const { cacheStats } = useSnapshot(intelligenceStore); return cacheStats; } // === General Hooks === /** * Hook to check if any intelligence operation is loading */ export function useIntelligenceLoading(): boolean { const { isLoading, isMemoryLoading } = useSnapshot(intelligenceStore); return isLoading || isMemoryLoading; } /** * Hook to access intelligence error */ export function useIntelligenceError(): string | null { const { error } = useSnapshot(intelligenceStore); return error; } /** * Hook to access the full intelligence state snapshot */ export function useIntelligenceState() { return useSnapshot(intelligenceStore); } /** * Hook to access intelligence actions * Returns the store directly for calling actions. * Does not cause re-renders. */ export function useIntelligenceActions() { return intelligenceStore; } // === Convenience Hooks === /** * Hook for memory operations with loading state */ export function useMemoryOperations() { const memories = useMemories(); const isLoading = useMemoryLoading(); const stats = useMemoryStats(); const actions = useIntelligenceActions(); return { memories, isLoading, stats, loadMemories: actions.loadMemories, storeMemory: actions.storeMemory, deleteMemory: actions.deleteMemory, loadStats: actions.loadMemoryStats, }; } /** * Hook for heartbeat operations */ export function useHeartbeatOperations() { const config = useHeartbeatConfig(); const isRunning = useHeartbeatRunning(); const history = useHeartbeatHistory(); const actions = useIntelligenceActions(); return { config, isRunning, history, init: actions.initHeartbeat, start: actions.startHeartbeat, stop: actions.stopHeartbeat, tick: actions.tickHeartbeat, }; } /** * Hook for compaction operations */ export function useCompactionOperations() { const lastCompaction = useLastCompaction(); const check = useCompactionCheck(); const actions = useIntelligenceActions(); return { lastCompaction, check, checkThreshold: actions.checkCompaction, compact: actions.compact, }; } /** * Hook for reflection operations */ export function useReflectionOperations() { const state = useReflectionState(); const lastReflection = useLastReflection(); const actions = useIntelligenceActions(); return { state, lastReflection, recordConversation: actions.recordConversation, shouldReflect: actions.shouldReflect, reflect: actions.reflect, }; } /** * Hook for identity operations */ export function useIdentityOperations() { const identity = useIdentity(); const pendingProposals = usePendingProposals(); const actions = useIntelligenceActions(); return { identity, pendingProposals, loadIdentity: actions.loadIdentity, buildPrompt: actions.buildPrompt, proposeChange: actions.proposeIdentityChange, approveProposal: actions.approveProposal, rejectProposal: actions.rejectProposal, }; }