|
| 1 | +# HAPPE Crate (`@happe`) |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +The `HAPPE` (Host Application Environment) crate implements the core host daemon responsible for managing interactions between the user, the main Large Language Model (LLM), and other system components like the Internal Dialogue App (`IDA`) and MCP Servers. |
| 6 | + |
| 7 | +It acts as the primary execution environment, orchestrating the flow of information and commands based on user requests and LLM responses. |
| 8 | + |
| 9 | +## Core Responsibilities |
| 10 | + |
| 11 | +1. **User/Client Interface:** (Future) Provides the primary interface for user interaction, potentially through various means like APIs, WebSockets, or other front-ends (initially replacing the direct CLI usage). |
| 12 | +2. **LLM Interaction:** Manages the connection and communication with the main LLM, sending constructed prompts and receiving generated responses. |
| 13 | +3. **IDA Communication (IPC Client):** |
| 14 | + * Connects to the `IDA` daemon via Inter-Process Communication (IPC). |
| 15 | + * Sends the user's raw query to `IDA` before contacting the LLM to allow `IDA` to retrieve relevant memories. |
| 16 | + * Receives retrieved memories (or none) from `IDA`. |
| 17 | + * Constructs the final prompt for the LLM by **appending** the received memories to the **original user query**. |
| 18 | + * Asynchronously sends the complete conversation turn (original query, appended memories, LLM response) back to `IDA` for analysis and storage after the LLM interaction is complete. |
| 19 | +4. **MCP Tool Execution (LLM-Initiated):** If the main LLM decides to use an MCP Tool during its response generation, `HAPPE` is responsible for invoking the appropriate MCP Server (likely via the `@mcp` crate or direct MCP client logic) and returning the result to the LLM. |
| 20 | +5. **State Management:** Manages the immediate state required for the current interaction turn. |
| 21 | + |
| 22 | +## Architecture |
| 23 | + |
| 24 | +`HAPPE` is designed as a long-running daemon process. It communicates with `IDA` via IPC. Its primary workflow involves: |
| 25 | + |
| 26 | +1. Receiving a user query. |
| 27 | +2. Querying `IDA` for contextual memories. |
| 28 | +3. Constructing the LLM prompt. |
| 29 | +4. Executing the LLM call. |
| 30 | +5. Handling any MCP tool calls requested by the LLM during generation. |
| 31 | +6. Returning the final response to the user/client. |
| 32 | +7. Asynchronously informing `IDA` about the completed turn for learning/memory storage. |
| 33 | + |
| 34 | +```mermaid |
| 35 | +sequenceDiagram |
| 36 | + participant User/Client |
| 37 | + participant HAPPE (Daemon) |
| 38 | + participant IDA (Daemon via IPC) |
| 39 | + participant MemoryMCPServer |
| 40 | + participant OtherMCPServers |
| 41 | + participant MainLLM |
| 42 | +
|
| 43 | + User/Client->>+HAPPE: Sends Raw Query |
| 44 | + HAPPE->>+IDA: Send Raw Query [IPC Call] |
| 45 | + IDA->>+MemoryMCPServer: retrieve_memories() |
| 46 | + MemoryMCPServer-->>-IDA: Return Memories |
| 47 | + IDA-->>-HAPPE: Send Back Memories [IPC Response] |
| 48 | + HAPPE->>HAPPE: Construct Prompt = Query + Memories |
| 49 | + HAPPE->>+MainLLM: Send Prompt |
| 50 | + alt LLM needs MCP Tool |
| 51 | + MainLLM-->>HAPPE: Request Tool X |
| 52 | + HAPPE->>+OtherMCPServers: Execute Tool X |
| 53 | + OtherMCPServers-->>-HAPPE: Tool Result |
| 54 | + HAPPE-->>MainLLM: Provide Tool Result |
| 55 | + end |
| 56 | + MainLLM-->>-HAPPE: Receive Final LLM Response |
| 57 | + HAPPE-->>User/Client: Display Response |
| 58 | + HAPPE-)+IDA: Send Full Turn [Async IPC Call] |
| 59 | + IDA->>IDA: Process Turn for Storage |
| 60 | + deactivate HAPPE |
| 61 | + activate IDA |
| 62 | + IDA->>+MemoryMCPServer: store_memory() |
| 63 | + MemoryMCPServer-->>-IDA: Ack |
| 64 | + deactivate MemoryMCPServer |
| 65 | + deactivate IDA |
| 66 | +``` |
| 67 | + |
| 68 | +## Usage |
| 69 | + |
| 70 | +This crate builds into the `happe-daemon` executable. Configuration details (LLM endpoints, IDA IPC address, MCP server details) will likely be managed via configuration files or environment variables. |
| 71 | + |
| 72 | +```bash |
| 73 | +# Build |
| 74 | +cargo build --release |
| 75 | + |
| 76 | +# Run (Example) |
| 77 | +./target/release/happe-daemon |
| 78 | +``` |
| 79 | + |
| 80 | +## Dependencies |
| 81 | + |
| 82 | +* `tokio`: For asynchronous runtime. |
| 83 | +* `serde`, `serde_json`: For serialization/deserialization. |
| 84 | +* IPC Crate (e.g., `interprocess`): For communication with IDA. |
| 85 | +* LLM Client Crate: For interacting with the main LLM. |
| 86 | +* (Potentially) `@mcp` or MCP client logic: For handling LLM-initiated tool calls. |
| 87 | +* (Potentially) `@core`: For shared types or utilities. |
0 commit comments