# lilimozi/openhanako architecture

> Architecture diagram of the GitHub repository lilimozi/openhanako, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-15). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/lilimozi/openhanako
- Repository: https://github.com/lilimozi/openhanako

## Overview

#### 1. Product and Runtime Shape
HanaAgent is a local-first, multi-agent personal AI assistant: desktop GUI, server-first CLI, mobile/LAN clients, external messaging bridges, tools, memory, scheduling, and plugins. It is primarily an Electron application that starts or connects to a separate Node.js server process; clients consume server APIs and WebSockets.

Primary entry points:
- Electron main/preload: `desktop/main.cjs`, `desktop/preload.cjs`
- React renderer: `desktop/src/main.tsx`, `desktop/src/react/App.tsx`
- Server variants: `server/main-full.ts`, `server/main-open.ts`, `server/index.ts`
- CLI: `cli/entry.ts`
- Background hub: `hub/index.ts`

#### 2. Server Composition and Transport Boundary
The Hono-based server is the application boundary for desktop, mobile PWA, LAN clients, plugins, and CLI. Composition roots distinguish full versus open/server artifacts; route modules expose chat, sessions, resources, models, bridges, plugins, and settings. WebSocket protocol carries streaming chat and resource/event updates.

- `server/composition/full-root.ts`
- `server/composition/open-root.ts`
- `server/routes/chat.ts`
- `server/ws-protocol.ts`

Request security is centralized around transport context, principals, route authorization, and audit logging rather than delegated to UI clients:
- `server/http/boundary.ts`
- `server/http/route-security.ts`
- `core/server-auth.ts`

#### 3. Agent Engine, Sessions, and Model Execution
`core/engine.ts` is the orchestration facade over managers for agents, sessions, models, configuration, skills, channels, bridges, and plugins. Agents execute through the Pi SDK and the internal LLM/provider layer. Session coordination owns loading, turn lifecycle, model isolation, compaction, tool snapshots, and teardown; persisted conversation data includes JSONL and a session manifest layer.

- `core/engine.ts`
- `core/agent-manager.ts`
- `core/session-coordinator.ts`
- `core/llm-client.ts`
- `lib/llm/provider-client.ts`
- `core/session-manifest/store.ts`

Flow: a client submits a turn → server chat route/engine resolves agent and session → policy/context/tools are assembled → provider/Pi runtime streams execution → session state and stream events are persisted and published back over WebSocket.

#### 4. Persistence, Memory, and Workspace Resources
User state is rooted under `HANA_HOME`; better-sqlite3 (WAL) is the material database runtime, alongside session files/manifests and controlled filesystem storage. Data-epoch migration/restore coordinates schema evolution. The memory subsystem derives facts, summaries, pinned and compiled memory, and scheduled “dream” revisions from sessions.

- `shared/persistence/store-registry.ts`
- `core/data-epoch-coordinator.ts`
- `lib/memory/compile.ts`
- `lib/memory/fact-store.ts`
- `lib/resource-io/resource-io.ts`
- `core/session-jsonl-file.ts`

Resource I/O is an authority boundary: local filesystem, mounts, URLs, and session files are represented through providers and access policy rather than arbitrary path access.
- `lib/resource-io/resource-access-policy.ts`
- `lib/resource-io/providers/local-fs-provider.ts`

#### 5. Tools, Sandboxing, and Computer Control
Tools span files, terminal, browser, web search/read, automation, media, subagents, and computer use. Invocation permissions and capability policy gate tool exposure. Execution is isolated with application path guards plus platform-specific OS sandbox implementations: macOS Seatbelt, Linux Bubblewrap, and Windows restricted-token/helper paths.

- `core/tool-catalog.ts`
- `lib/permission/tool-invocation-permission.ts`
- `lib/sandbox/index.ts`
- `lib/sandbox/path-guard.ts`
- `core/computer-use/computer-host.ts`
- `lib/terminal/terminal-session-manager.ts`

#### 6. Background Coordination and External Integrations
The hub is independent of the foreground chat turn and runs scheduling, heartbeats, channel routing, direct-message routing, and agent execution dispatch. Bridge adapters connect agents to Telegram, Feishu, QQ, WeChat, and DingTalk, preserving platform/media capabilities and session identity. MCP supports stdio and HTTP/JSON-RPC connectors with OAuth.

- `hub/scheduler.ts`
- `hub/channel-router.ts`
- `lib/bridge/bridge-manager.ts`
- `lib/bridge/telegram-adapter.ts`
- `core/mcp/manager.ts`

#### 7. Desktop, Plugin, and Distribution Surfaces
Electron hosts React 19/Zustand UI surfaces for chat, workspace (“desk”), settings, onboarding, quick chat, and browser viewer. The plugin system can contribute tools, routes, providers, UI surfaces, skills, and lifecycle behavior, with restricted/full-access capability levels. Built-in plugins ship under `plugins/`; SDK/runtime packages define the extension contract.

- `desktop/src/react/bootstrap.ts`
- `desktop/src/react/services/server-connection.ts`
- `core/plugin-manager.ts`
- `packages/plugin-sdk/src/index.ts`
- `packages/plugin-runtime/src/index.ts`

Build scripts package server/runtime dependencies and native computer-use helpers for macOS and Windows:
- `scripts/build-server.mjs`
- `scripts/build-computer-use-helper.mjs`

Main entry points: [`core/engine.ts`](https://github.com/lilimozi/openhanako/blob/main/core/engine.ts) (Agent engine), [`server/composition/full-root.ts`](https://github.com/lilimozi/openhanako/blob/main/server/composition/full-root.ts) (Server composition), [`core/tool-catalog.ts`](https://github.com/lilimozi/openhanako/blob/main/core/tool-catalog.ts) (Tool catalog), [`core/session-coordinator.ts`](https://github.com/lilimozi/openhanako/blob/main/core/session-coordinator.ts) (Session coordinator)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_clients["Client Surfaces"]
  node_electron["Electron host<br/>desktop runtime<br/>[main.cjs]"]
  node_renderer["React desktop UI<br/>renderer<br/>[App.tsx]"]
  node_cli["Server-first CLI<br/>[entry.ts]"]
end

subgraph group_server["Server Boundary"]
  node_server_root["Server composition<br/>Hono composition root<br/>[full-root.ts]"]
  node_chat_route["Chat API<br/>route module<br/>[chat.ts]"]
  node_transport_security["Transport security<br/>authorization boundary<br/>[boundary.ts]"]
  node_websocket["Streaming protocol<br/>WebSocket protocol<br/>[ws-protocol.ts]"]
end

subgraph group_agent["Agent Runtime"]
  node_engine["Agent engine<br/>orchestration facade<br/>[engine.ts]"]
  node_sessions["Session coordinator<br/>turn lifecycle"]
  node_model_execution["Model execution<br/>LLM and Pi runtime<br/>[llm-client.ts]"]
end

subgraph group_data["State and Resources"]
  node_persistence[("Persistent state<br/>SQLite and session storage<br/>[store-registry.ts]")]
  node_memory["Memory compiler<br/>memory subsystem<br/>[compile.ts]"]
  node_resources["Resource I/O policy<br/>resource authority boundary"]
end

subgraph group_automation["Automation and Integrations"]
  node_tool_catalog["Tool catalog<br/>capability registry<br/>[tool-catalog.ts]"]
  node_sandbox["Sandboxed tool execution<br/>OS sandbox boundary<br/>[index.ts]"]
  node_computer_terminal["Computer and terminal control<br/>execution services<br/>[computer-host.ts]"]
  node_hub["Background hub<br/>background coordinator<br/>[index.ts]"]
  node_bridges["Messaging bridges<br/>platform adapters<br/>[bridge-manager.ts]"]
end

subgraph group_extensions["Extensions and Distribution"]
  node_plugins["Plugin manager<br/>extension host<br/>[plugin-manager.ts]"]
  node_plugin_sdk["Plugin SDK<br/>extension contract<br/>[index.ts]"]
  node_build["Distribution build<br/>packaging scripts<br/>[build-server.mjs]"]
end

node_electron -->|"hosts"| node_renderer
node_electron -->|"starts or connects"| node_server_root
node_renderer -->|"submits turns"| node_chat_route
node_cli -->|"consumes APIs"| node_server_root
node_server_root -->|"applies"| node_transport_security
node_transport_security -->|"authorizes"| node_chat_route
node_server_root -->|"exposes"| node_websocket
node_chat_route -->|"dispatches turns"| node_engine
node_engine -->|"coordinates"| node_sessions
node_engine -->|"invokes"| node_model_execution
node_engine -->|"assembles tools"| node_tool_catalog
node_sessions -->|"persists sessions"| node_persistence
node_sessions -->|"supplies conversation context"| node_memory
node_memory -->|"stores derived memory"| node_persistence
node_tool_catalog -->|"gates execution"| node_sandbox
node_sandbox -->|"isolates"| node_computer_terminal
node_tool_catalog -->|"uses controlled resources"| node_resources
node_engine -->|"publishes streams"| node_websocket
node_hub -->|"dispatches agent work"| node_engine
node_hub -->|"routes channels"| node_bridges
node_bridges -->|"delivers external messages"| node_engine
node_plugins -.->|"contributes routes and providers"| node_server_root
node_plugins -.->|"contributes tools"| node_tool_catalog
node_plugin_sdk -.->|"defines contract"| node_plugins
node_build -->|"packages desktop runtime"| node_electron

click node_electron "https://github.com/lilimozi/openhanako/blob/main/desktop/main.cjs"
click node_renderer "https://github.com/lilimozi/openhanako/blob/main/desktop/src/react/App.tsx"
click node_cli "https://github.com/lilimozi/openhanako/blob/main/cli/entry.ts"
click node_server_root "https://github.com/lilimozi/openhanako/blob/main/server/composition/full-root.ts"
click node_chat_route "https://github.com/lilimozi/openhanako/blob/main/server/routes/chat.ts"
click node_transport_security "https://github.com/lilimozi/openhanako/blob/main/server/http/boundary.ts"
click node_websocket "https://github.com/lilimozi/openhanako/blob/main/server/ws-protocol.ts"
click node_engine "https://github.com/lilimozi/openhanako/blob/main/core/engine.ts"
click node_sessions "https://github.com/lilimozi/openhanako/blob/main/core/session-coordinator.ts"
click node_model_execution "https://github.com/lilimozi/openhanako/blob/main/core/llm-client.ts"
click node_tool_catalog "https://github.com/lilimozi/openhanako/blob/main/core/tool-catalog.ts"
click node_sandbox "https://github.com/lilimozi/openhanako/blob/main/lib/sandbox/index.ts"
click node_computer_terminal "https://github.com/lilimozi/openhanako/blob/main/core/computer-use/computer-host.ts"
click node_hub "https://github.com/lilimozi/openhanako/blob/main/hub/index.ts"
click node_bridges "https://github.com/lilimozi/openhanako/blob/main/lib/bridge/bridge-manager.ts"
click node_persistence "https://github.com/lilimozi/openhanako/blob/main/shared/persistence/store-registry.ts"
click node_memory "https://github.com/lilimozi/openhanako/blob/main/lib/memory/compile.ts"
click node_resources "https://github.com/lilimozi/openhanako/blob/main/lib/resource-io/resource-access-policy.ts"
click node_plugins "https://github.com/lilimozi/openhanako/blob/main/core/plugin-manager.ts"
click node_plugin_sdk "https://github.com/lilimozi/openhanako/blob/main/packages/plugin-sdk/src/index.ts"
click node_build "https://github.com/lilimozi/openhanako/blob/main/scripts/build-server.mjs"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_electron,node_renderer,node_cli toneBlue
class node_server_root,node_chat_route,node_transport_security,node_websocket toneAmber
class node_engine,node_sessions,node_model_execution toneMint
class node_persistence,node_memory,node_resources toneRose
class node_tool_catalog,node_sandbox,node_computer_terminal,node_hub,node_bridges toneIndigo
class node_plugins,node_plugin_sdk,node_build toneTeal
```

## Components

### Client Surfaces

- [Electron host](https://github.com/lilimozi/openhanako/blob/main/desktop/main.cjs): desktop runtime · Starts or connects to the separate server process. · `desktop/main.cjs`
- [React desktop UI](https://github.com/lilimozi/openhanako/blob/main/desktop/src/react/App.tsx): renderer · `desktop/src/react/App.tsx`
- [Server-first CLI](https://github.com/lilimozi/openhanako/blob/main/cli/entry.ts): CLI · `cli/entry.ts`

### Server Boundary

- [Server composition](https://github.com/lilimozi/openhanako/blob/main/server/composition/full-root.ts): Hono composition root · Full and open artifacts compose the application boundary. · `server/composition/full-root.ts`
- [Chat API](https://github.com/lilimozi/openhanako/blob/main/server/routes/chat.ts): route module · `server/routes/chat.ts`
- [Transport security](https://github.com/lilimozi/openhanako/blob/main/server/http/boundary.ts): authorization boundary · Builds transport context, principals, authorization, and audit records. · `server/http/boundary.ts`
- [Streaming protocol](https://github.com/lilimozi/openhanako/blob/main/server/ws-protocol.ts): WebSocket protocol · Carries chat streams and resource/event updates. · `server/ws-protocol.ts`

### Agent Runtime

- [Agent engine](https://github.com/lilimozi/openhanako/blob/main/core/engine.ts): orchestration facade · Coordinates agents, sessions, models, skills, channels, bridges, and plugins. · `core/engine.ts`
- [Session coordinator](https://github.com/lilimozi/openhanako/blob/main/core/session-coordinator.ts): turn lifecycle · Owns loading, isolation, compaction, tool snapshots, and teardown. · `core/session-coordinator.ts`
- [Model execution](https://github.com/lilimozi/openhanako/blob/main/core/llm-client.ts): LLM and Pi runtime · Executes provider-backed agent turns with streaming. · `core/llm-client.ts`

### State and Resources

- [Persistent state](https://github.com/lilimozi/openhanako/blob/main/shared/persistence/store-registry.ts): SQLite and session storage · HANA_HOME contains WAL-backed state, JSONL conversations, and manifests. · `shared/persistence/store-registry.ts`
- [Memory compiler](https://github.com/lilimozi/openhanako/blob/main/lib/memory/compile.ts): memory subsystem · Derives facts, summaries, pinned, compiled, and dream-revised memory. · `lib/memory/compile.ts`
- [Resource I/O policy](https://github.com/lilimozi/openhanako/blob/main/lib/resource-io/resource-access-policy.ts): resource authority boundary · Mediates filesystem, mounts, URLs, and session-file access. · `lib/resource-io/resource-access-policy.ts`

### Automation and Integrations

- [Tool catalog](https://github.com/lilimozi/openhanako/blob/main/core/tool-catalog.ts): capability registry · `core/tool-catalog.ts`
- [Sandboxed tool execution](https://github.com/lilimozi/openhanako/blob/main/lib/sandbox/index.ts): OS sandbox boundary · Uses path guards and platform-specific isolation. · `lib/sandbox/index.ts`
- [Computer and terminal control](https://github.com/lilimozi/openhanako/blob/main/core/computer-use/computer-host.ts): execution services · `core/computer-use/computer-host.ts`
- [Background hub](https://github.com/lilimozi/openhanako/blob/main/hub/index.ts): background coordinator · Runs independent scheduling, heartbeats, routing, and dispatch. · `hub/index.ts`
- [Messaging bridges](https://github.com/lilimozi/openhanako/blob/main/lib/bridge/bridge-manager.ts): platform adapters · Connects external messaging platforms while preserving identity and media capabilities. · `lib/bridge/bridge-manager.ts`

### Extensions and Distribution

- [Plugin manager](https://github.com/lilimozi/openhanako/blob/main/core/plugin-manager.ts): extension host · Loads capability-scoped tools, routes, providers, UI, skills, and lifecycle behavior. · `core/plugin-manager.ts`
- [Plugin SDK](https://github.com/lilimozi/openhanako/blob/main/packages/plugin-sdk/src/index.ts): extension contract · `packages/plugin-sdk/src/index.ts`
- [Distribution build](https://github.com/lilimozi/openhanako/blob/main/scripts/build-server.mjs): packaging scripts · Packages server dependencies and native computer-use helpers. · `scripts/build-server.mjs`

## Connections

- Electron host → React desktop UI: hosts
- Electron host → Server composition: starts or connects
- React desktop UI → Chat API: submits turns
- Server-first CLI → Server composition: consumes APIs
- Server composition → Transport security: applies
- Transport security → Chat API: authorizes
- Server composition → Streaming protocol: exposes
- Chat API → Agent engine: dispatches turns
- Agent engine → Session coordinator: coordinates
- Agent engine → Model execution: invokes
- Agent engine → Tool catalog: assembles tools
- Session coordinator → Persistent state: persists sessions
- Session coordinator → Memory compiler: supplies conversation context
- Memory compiler → Persistent state: stores derived memory
- Tool catalog → Sandboxed tool execution: gates execution
- Sandboxed tool execution → Computer and terminal control: isolates
- Tool catalog → Resource I/O policy: uses controlled resources
- Agent engine → Streaming protocol: publishes streams
- Background hub → Agent engine: dispatches agent work
- Background hub → Messaging bridges: routes channels
- Messaging bridges → Agent engine: delivers external messages
- Plugin manager → Server composition: contributes routes and providers
- Plugin manager → Tool catalog: contributes tools
- Plugin SDK → Plugin manager: defines contract
- Distribution build → Electron host: packages desktop runtime
