How itwhizharsh/haki works
1. Top-level shape
- The repo is split into two runtime domains: HAKI.app in Swift and haki_core_service in Python.
- This is not a single in-process app; it is a two-process local assistant with a hard boundary between UI/device access and AI orchestration.
2. Process boundary and roles
- Swift “Body” (HAKI/) owns macOS-facing capabilities: permissions, audio, screen/capture, notifications, keyboard/menu-bar UI, local storage, and OS automation.
- Python “Mind” (Core/) owns the assistant logic: orchestration, intent routing, dialogue, planning, memory/RAG, learning, and model backend abstraction.
- The split is important for architecture diagrams because most side effects happen in the Swift process, while decisions and reasoning happen in Python.
3. IPC layer
- The two processes communicate over a UNIX domain socket at ~/Library/Application Support/HAKI/haki_core.sock.
- The contract is defined in proto/haki_ipc.proto and implemented with generated gRPC stubs in Core/core/ipc/proto/.
- Swift has its own IPC client (IPCClient.swift, JSONIPCClient.swift), so the transport is a key integration boundary rather than an internal detail.
4. Swift app shell
- The Swift package starts from main.swift and AppDelegate.swift, with CoreProcessManager.swift responsible for launching/managing the Python service lifecycle.
- MenuBarUI.swift and UIState.swift indicate the user experience is menu-bar driven, not a full windowed app.
- HAKI.entitlements and the permission subsystem reflect that this process is the one interacting with macOS security-sensitive APIs.
5. Device and OS-facing subsystems in Swift
- Audio: AudioEngine, VAD, STTService, TTSService, VoiceEngine, and ClauseSegmenter form the voice interaction path.
- Capture: ScreenReader and VisionOCR handle screen text/image capture.
- OS actions: AppleScriptBridge, EventKitBridge, and NotificationManager perform real system-side effects.
- Permissions: PermissionManager and HAKIPermission gate access to macOS capabilities like Accessibility and screen/audio capture.
6. Local persistence and user data in Swift
- AppStore.swift and KeychainStore.swift show the body process also owns durable local state and secrets handling.
- The README’s privacy model matches this: sensitive local data stays on-device, and the Swift side appears to be the trusted boundary for encrypted storage and keychain-backed credentials.
7. Core orchestration in Python
- orchestrator.py and intent_router.py are the main entry points for user-turn processing.
- This layer likely receives events or utterances from the Swift process, routes them to the appropriate subsystem, and manages cancellation/turn control.
- dialogue_manager.py handles clarification and slot-filling behavior when the user request is underspecified.
8. Planning, execution, and safety
- planner.py plus execution_engine.py indicate a two-step flow: convert intent into a command plan, then execute it.
- safety_gate.py is a distinct boundary before execution, which is important for diagramming because it separates “decision” from “allowed action.”
- In the overall architecture, this is the point where AI-generated intent becomes constrained, checked behavior.
9. Memory and learning pipeline
- memory/ is a full subsystem: vault.py, indexer.py, chunker.py, serializer.py, models.py, and store_result.py.
- This suggests a RAG-style pipeline: ingest notes/events, chunk and index them, persist them in a vault, and retrieve them for dialogue/orchestration.
- learning_engine.py sits on top of this, extracting knowledge from conversations and deciding when a conversation has ended or when new memory should be stored.
10. Model abstraction and multimodal AI
- model_provider.py abstracts backend access for STT, LLM, TTS, and embeddings.
- That means the core logic is not tied to one vendor; the provider layer is the integration boundary for local or remote model backends.
- language_engine.py, persona_engine.py, mood_detector.py, and clock.py provide higher-level context signals that shape responses and behavior.
11. How data moves through the system
- Typical flow is: user input / audio / screen context in Swift → IPC → Python orchestration/planning/memory/model calls → plan or response back over IPC → Swift performs UI/audio/OS actions.
- In the opposite direction, Swift can also send sensory context or events into the Core process to support memory, dialogue, or safety decisions.
- This makes the architecture a feedback loop between “capture/action” and “reasoning.”
12. Testing and repository structure
- The Core/tests/ and HAKI/Tests/ trees show each process is tested independently, with additional integration coverage around IPC and memory.
- That separation mirrors the runtime split, so when drawing the architecture, it helps to place tests alongside the subsystem they validate rather than as a single shared layer.
- The .kiro/specs/ folder is design/specification material, not runtime architecture, but it documents the intended behavior of the same two-process system.
Main entry points: orchestrator.py Orchestrator · IPCClient.swift IPC Client · AppDelegate.swift Delegate · PermissionManager.swift Permissions
Components 26
HAKI.app
macOS-facing body process for capture, actions, UI, and local storage
- App Shell · Starts the macOS app and manages the core service lifecycle
HAKI/Sources/HAKI/main.swift - Delegate · Coordinates app startup and runtime wiring
HAKI/Sources/HAKI/AppDelegate.swift - Core Manager · Launches and supervises the Python assistant service
HAKI/Sources/HAKI/CoreProcessManager.swift - Menu Bar · Menu-bar driven user interface and status surface
HAKI/Sources/Subsystems/UI/MenuBarUI.swift - Permissions · Gates macOS accessibility, screen, and audio capabilities
HAKI/Sources/Subsystems/Permissions/PermissionManager.swift - Voice Stack · Audio capture, VAD, STT, TTS, and voice control
HAKI/Sources/Subsystems/Audio/AudioEngine.swift - Capture · Reads screen content and OCR text for assistant context
HAKI/Sources/Subsystems/Capture/ScreenReader.swift - OS Actions · Performs AppleScript, calendar, and notification side effects
HAKI/Sources/Subsystems/OSActions/AppleScriptBridge.swift - Local Store · Owns app state and keychain-backed secrets
HAKI/Sources/Subsystems/Store/AppStore.swift - IPC Client · Swift client for Python service communication
HAKI/Sources/Subsystems/IPC/IPCClient.swift
haki_core_service
Python mind process for orchestration, planning, memory, and model access
- Service Main · Starts the Python core service and server loop
Core/haki_core_service.py - IPC Server · Receives Swift requests and streams responses
Core/core/ipc/server.py - Orchestrator · Coordinates user-turn processing across core subsystems
Core/core/orchestrator/orchestrator.py - Intent Router · Maps requests to specialized assistant behavior
Core/core/orchestrator/intent_router.py - Dialogue · Handles slot-filling and conversational repair
Core/core/dialogue/dialogue_manager.py - Planner · Converts intent into structured action plans
Core/core/planner/planner.py - Safety Gate · Constrains plans before execution
Core/core/execution/safety_gate.py - Executor · Runs approved plans and returns outcomes
Core/core/execution/execution_engine.py - Memory · Coordinates vault, indexing, chunking, and retrieval
Core/core/memory/memory_brain.py - Learning · Captures durable knowledge from conversations
Core/core/learning/learning_engine.py - Model Provider · Abstracts STT, LLM, TTS, and embedding backends
Core/core/model_provider/model_provider.py - Language · Provides higher-level language behavior signals
Core/core/language/language_engine.py - Persona · Shapes assistant voice and persona
Core/core/persona/persona_engine.py - Mood · Infers user mood and conversational tone
Core/core/mood/mood_detector.py - Clock · Supplies time-aware context
Core/core/clock/clock.py
IPC contract
Shared transport and schema boundary between Swift and Python
- IPC Proto · Shared IPC contract and generated stubs
proto/haki_ipc.proto
Connections 30
- App Shell → Delegate: boots · App startup flows into lifecycle coordination
- Delegate → Core Manager: starts core · Delegate asks manager to launch and supervise Python service
- Delegate → Menu Bar: wires UI · Initializes the menu-bar interface
- Delegate → Permissions: checks access · Ensures required macOS permissions are available
- Permissions → Voice Stack: unlocks · Gates audio and speech features
- Permissions → Capture: unlocks · Gates screen capture and OCR
- Local Store → Delegate: state · Provides persisted app state and secrets
- Voice Stack → IPC Client: sends audio · Transmits recognized speech or audio events to core
- Capture → IPC Client: sends context · Sends screen-derived context to core
- OS Actions → IPC Client: reports effects · Receives commands or returns operation results through IPC
- Menu Bar → IPC Client: user turn · User interactions are forwarded to the core process
- IPC Client → IPC Proto: uses schema · Client follows the shared IPC contract
- IPC Server → IPC Proto: implements schema · Server implements the same gRPC contract
- Core Manager → Service Main: launches · Supervisor starts the Python service process
- Service Main → IPC Server: serves · Service exposes the IPC server
- IPC Server → Orchestrator: dispatches · Incoming requests enter the orchestration layer
- Orchestrator → Intent Router: routes · Orchestrator selects the right handling path
- Orchestrator → Dialogue: clarifies · Underspecified turns are repaired through dialogue
- Intent Router → Planner: plans · Intent becomes a structured plan
- Planner → Safety Gate: validates · Plans are checked before execution
- Safety Gate → Executor: approves · Only safe actions proceed to execution
- Orchestrator → Memory: retrieves · Turn processing consults memory and context
- Learning → Memory: stores · Learnt knowledge is written back into memory
- Orchestrator → Learning: learns · Conversation outcomes feed learning
- Orchestrator → Model Provider: invokes models · Uses model backends for reasoning and generation
- Dialogue → Language: shapes reply · Dialogue behavior is informed by language utilities
- Orchestrator → Persona: styles voice · Persona influences the final assistant response
- Orchestrator → Mood: reads mood · Mood signal informs interpretation and tone
- Orchestrator → Clock: reads time · Time context can affect decisions and phrasing
- Executor → IPC Client: returns actions · Approved actions and results flow back to Swift