# Installation

This guide covers installing `@edgeai/edge-client-ts` on Linux, macOS, and
Windows for **end users** (running the client) and for **contributors**
(working on the package itself).

---

## End-user install

### 1. Prerequisites

- **Node.js >= 20** (we use `crypto.randomUUID`, native `fetch`, modern ESM).
  Check with:
  ```bash
  node --version   # must print v20.x or newer
  ```
  If you need to install or upgrade Node, use [nvm](https://github.com/nvm-sh/nvm)
  (Linux/macOS) or [fnm](https://github.com/Schniz/fnm) (cross-platform).

- A **C/C++ toolchain** is only required if `node-pty` does not ship a
  prebuilt binary for your platform. Most users on x86_64 / arm64 Linux,
  x86_64 / arm64 macOS, and x86_64 Windows do **not** need it.

### 2. Install via npm

```bash
npm install -g @edgeai/edge-client-ts
```

Verify the binary is on `PATH`:

```bash
ec-ts --help
```

### 3. Configure secrets (NEVER commit them)

Set the fleet shared secret and any optional pins via environment
variables. **Do not paste these into shell history** — use a secrets
manager or a `.env` file with restrictive permissions.

```bash
# Linux/macOS — current shell only
export EDGE_AUTH_TOKEN="<your-fleet-shared-secret>"

# Optional: pin the relay's TLS certificate (extract via the openssl
# one-liner in the upstream docs)
# export EDGE_RELAY_FINGERPRINT="<sha256:base64>"

# Optional: enable offline buffer encryption (32 bytes = 64 hex chars)
# export EDGE_BUFFER_KEY="<64 hex chars>"
```

Persist across shells by adding the exports to `~/.bashrc`,
`~/.zshrc`, or `~/.config/fish/config.fish`.

### 4. Enroll against your relay

After creating an operator in the Edge AI Agent dashboard, copy the
**per-operator API key** (format `opk_<base64>`) and enroll:

```bash
ec-ts enroll --key "<your-operator-api-key>"
```

This persists the credential to the **OS keychain** (Keychain on macOS,
Credential Manager on Windows, libsecret/Secret Service on Linux). The
plain key never touches a config file.

### 5. Run the daemon

Foreground (foreground is the default; useful for development):

```bash
ec-ts run
```

As a system service (auto-start on boot):

```bash
ec-ts run --install-service
# Uninstall later with:
ec-ts run --uninstall-service
```

The service manager installs:

| OS | Service unit |
|----|--------------|
| Linux | `~/.config/systemd/user/ec-ts.service` |
| macOS | `~/Library/LaunchAgents/com.edgeai.ec-ts.plist` |
| Windows | `\LeastPrivilege` scheduled task |

---

## Contributor install

### 1. Clone the monorepo

```bash
git clone https://github.com/baphuongna/edge-ai-agent.git
cd edge-ai-agent/edge-client-ts
```

### 2. Install dependencies

```bash
npm install
```

### 3. Verify

```bash
npm run typecheck    # tsc --noEmit
npm test             # vitest run
npm run build        # compile to dist/
```

### 4. End-to-end smoke (optional)

The smoke test connects a real daemon to a local `ws-service` and
verifies register → heartbeat → ack → reconnect. See
[`e2e-smoke/README.md`](../e2e-smoke/README.md).

```bash
npm run smoke
```

---

## Upgrading

```bash
npm install -g @edgeai/edge-client-ts@latest
```

Configuration and OS-keychain credentials are preserved across upgrades.
To downgrade, pin the version:

```bash
npm install -g @edgeai/edge-client-ts@1.0.0
```

---

## Uninstall

```bash
# Remove the daemon + service
ec-ts run --uninstall-service

# Remove the package
npm uninstall -g @edgeai/edge-client-ts

# Optional: wipe the per-user config directory
rm -rf ~/.config/edge-ai-agent     # Linux
rm -rf ~/Library/Application\ Support/edge-ai-agent   # macOS
# Windows: %APPDATA%\edge-ai-agent
```

OS-keychain entries live in your platform's standard credential store and
are removed with your platform's credential manager (do not forget to
revoke the operator API key in the dashboard as well).