Files
hapi/server
weishu f81e585eef refactor(daemon): use mtime-based version detection instead of string comparison
Replace CLI version string comparison with file modification time (mtime) based detection. This provides a more reliable way to detect when the CLI binary has been updated, especially for bun-compiled executables where package.json may not be accessible.

Changes:
- Add getInstalledCliMtimeMs() utility to check CLI binary or package.json mtime
- Store startedWithCliMtimeMs in daemon state on startup
- Use mtime comparison in version check loop for daemon auto-restart
- Move version flag handling to early CLI startup before daemon initialization
- Fix machine active state merging to prefer newer activeAt timestamp

This improves daemon restart behavior when CLI is updated via package managers.
2025-12-23 13:14:15 +08:00
..

hapi-server

Telegram bot + HTTP API + realtime updates for hapi.

What it does

  • Telegram bot for notifications and the Mini App entrypoint.
  • HTTP API for sessions, messages, permissions, machines, and files.
  • Server-Sent Events stream for live updates in the web app.
  • Socket.IO channel for CLI connections.
  • Serves the web app from web/dist or embedded assets in the single binary.
  • Persists state in SQLite.

Typical deployment flow

  1. Configure env vars.
  2. Expose the server to the internet (HTTPS) if you need Telegram Mini App access.
  3. Run the server.
  4. Point the CLI to the server and open the web app.

Configuration

Required:

  • CLI_API_TOKEN - shared secret used by CLI and web login.

Optional (Telegram):

  • TELEGRAM_BOT_TOKEN - token from @BotFather.
  • ALLOWED_CHAT_IDS - comma-separated chat IDs allowed to use the bot.
  • WEBAPP_URL - public HTTPS URL for Telegram Mini App access.

Optional:

  • WEBAPP_PORT - HTTP port (default: 3006).
  • CORS_ORIGINS - comma-separated origins, or *.
  • HAPI_HOME - data directory (default: ~/.hapi).
  • DB_PATH - SQLite database path.

Running

Binary (single executable):

export TELEGRAM_BOT_TOKEN="..."
export ALLOWED_CHAT_IDS="12345678"
export CLI_API_TOKEN="shared-secret"
export WEBAPP_URL="https://your-domain.example"

hapi server

If you only need web + CLI, you can omit TELEGRAM_BOT_TOKEN and ALLOWED_CHAT_IDS. To enable Telegram, set TELEGRAM_BOT_TOKEN and WEBAPP_URL, start the server, send /start to the bot to get your chat ID, set ALLOWED_CHAT_IDS, and restart the server.

From source:

bun install
bun run dev:server

Or inside server/:

bun run start

Build for deployment

From the repo root:

bun run build:server
bun run build:web

The server build output is server/dist/index.js, and the web assets are in web/dist.

Networking notes

  • Telegram Mini Apps require HTTPS and a public URL. If the server has no public IP, use Cloudflare Tunnel or Tailscale and set WEBAPP_URL to the HTTPS endpoint.
  • If the web app is hosted on a different origin, set CORS_ORIGINS accordingly.

Architecture overview

The server is the hub for direct-connect mode. It accepts CLI connections over Socket.IO, exposes HTTP endpoints for the web UI, and publishes live updates over SSE. A Telegram bot provides notifications and a Mini App entrypoint. Session and machine state are stored in a local SQLite database.

Security model

Access is controlled by:

  • Telegram chat ID allowlist (when Telegram is enabled).
  • CLI_API_TOKEN shared secret for CLI and browser access.

Transport security depends on HTTPS in front of the server.