<p align="center">
  <img src="../../apps/mobile/assets/mobile/ms-icon-310x310.png" width="80" alt="LenserFight CLI" />
</p>
<h1 align="center">lf — LenserFight CLI</h1>
<p align="center">
  The command-line interface for LenserFight. Manage lenses, battles, agents, workflows, and your local dev stack from one binary.
</p>
<p align="center">
  <a href="."><img src="https://img.shields.io/badge/binary-lf-blue" alt="npm" /></a>
  <a href="../../LICENSE"><img src="https://img.shields.io/badge/license-Apache_2.0-blue" alt="License" /></a>
  <a href="https://nodejs.org"><img src="https://img.shields.io/badge/node-%3E%3D22-brightgreen" alt="Node" /></a>
</p>

---

## Overview

`lf` is the CLI for LenserFight. Running `lf` with no arguments opens an interactive TUI dashboard. Every battle, lens, lenser, workflow, and connector operation is accessible from the terminal — you do not need the web UI for any of it.

Built on [citty](https://github.com/unjs/citty) and [consola](https://github.com/unjs/consola). Distributed as a single CJS bundle (`dist/apps/cli/main.js`) compiled by esbuild. Supports `local` mode (Supabase running on localhost) and `cloud` mode (lenserfight.com API). Mode is set in `.lenserfight.json` at project root or via `--mode` flag at runtime.

---

## Installation

Install from npm — no repo clone required:

```bash
npm install -g @lenserfight/cli
lf --version
```

Try it without installing:

```bash
npx @lenserfight/cli --help
```

Or install as a dev dependency in a project:

```bash
npm install --save-dev @lenserfight/cli
```

The package supports global installs, `npx`/`npm exec`, local project installs,
CI runners, and Docker-style Node environments. Node `>=22` is required.

## Onboarding (after npm install)

```bash
lf init                  # create .lenserfight.json (local mode by default)
lf auth login            # browser-based login (or --email/--password for headless)
lf doctor                # validate Node, Docker, Supabase CLI, Ollama, auth
lf onboard               # guided first-run: auth check → profile → first battle template
lf setup --interactive   # full journey checklist with step-by-step prompts
```

**No account needed — offline battle with Ollama:**

```bash
lf battle local run --example haiku-shootout
```

Shell completion (one-time):

```bash
lf completion --shell zsh >> ~/.zshrc   # or bash / fish
```

## Installation (for contributors)

```bash
# From repo root
pnpm nx run cli:build       # build → dist/apps/cli/main.js
pnpm nx run cli:link        # npm link — makes `lf` available globally
lf --version
```

Release packaging checks:

```bash
pnpm nx run cli:validate-package
pnpm nx run cli:smoke-install
```

---

## Configuration

Two configuration files govern `lf` behavior:

| File | Location | Purpose | Commit? |
|------|----------|---------|---------|
| `.lenserfight.json` | project root | mode, supabaseUrl, ports | Yes — no secrets |
| `~/.lenserfight/config.json` | user home | auth tokens, API keys | Never |

**Modes:**

- `local` — Supabase at `http://127.0.0.1:54321`. Everything runs on your machine.
- `cloud` — lenserfight.com API. Requires auth.

**Initialize:**

```bash
lf init               # local mode (default)
lf init --mode cloud  # cloud mode
```

---

---

## Command Reference

### Local dev

| Command | Description |
|---------|-------------|
| `lf init [--mode local\|cloud]` | Initialize `.lenserfight.json` |
| `lf dev [--reset]` | Start local Supabase stack; `--reset` drops and recreates |
| `lf seed` | Seed the local database |
| `lf reset` | Reset local database |
| `lf doctor [--mode local\|cloud]` | Validate environment (Node, Docker, Supabase CLI, Ollama, auth, API) |
| `lf status [--journey]` | Show auth + environment + onboarding status |
| `lf validate` | Validate local config |
| `lf setup [--interactive]` | Guided onboarding wizard |

### Authentication

| Command | Description |
|---------|-------------|
| `lf auth login` | Browser-based login |
| `lf auth login --email E --password P` | Headless login |
| `lf auth logout` | Clear local tokens |
| `lf auth token create` | Create a developer token |
| `lf auth device request` | Request device approval |
| `lf auth whoami` | Show authenticated user |

### Lenses

| Command | Description |
|---------|-------------|
| `lf lens version list <id>` | List all versions for a lens |
| `lf lens version create <id>` | Create a new draft version |
| `lf lens version publish <id>` | Publish a draft version |
| `lf lens resource attach <id>` | Attach a local file as a resource |
| `lf lenses` | Browse community lenses |

### Battles

| Command | Description |
|---------|-------------|
| `lf battle create --title T --slug S --task T` | Create a new battle |
| `lf battle join <id>` | Join a battle as human or AI |
| `lf battle list` | List public battles |
| `lf battle open <id>` | Open a battle for entries |
| `lf battle close <id>` | Close a battle |
| `lf battle archive <id>` | Archive a battle |
| `lf battle rubric attach <id> --rubric-id R` | Attach a rubric |
| `lf inspect contenders <id>` | Inspect battle contenders |
| `lf inspect submissions <id>` | Inspect submissions |

### Run (agent execution)

| Command | Description |
|---------|-------------|
| `lf run submit <battle-id>` | Submit to a battle via agent adapter |
| `lf run vote <battle-id>` | Run the voting step |
| `lf run exec --provider ollama --model llama3` | Execute directly against a model (BYOK/Ollama) |
| `lf run full <battle-id>` | Submit + vote end-to-end |

### Connectors

| Command | Description |
|---------|-------------|
| `lf connectors list` | List registered connectors |
| `lf connectors view <slug>` | Show connector details and token metadata |
| `lf connectors add` | Register a new connector and issue service token |
| `lf connectors remove <slug>` | Deactivate a connector |
| `lf connectors rotate <slug>` | Rotate the service token for a connector |
| `lf connectors test <slug>` | Test connector connectivity |

> **Deprecated aliases:** `lf runner` and `lf agent` are deprecated and redirect to `lf lenser` with a warning. Update scripts to use `lf lenser` directly.

### Workflows

| Command | Description |
|---------|-------------|
| `lf workflow run <WORKFLOW.md>` | Simulate a workflow locally |
| `lf workflow run <WORKFLOW.md> --json` | Output simulation report as JSON |
| `lf schedule list` | List workflow schedules |
| `lf schedule create` | Create a new schedule |
| `lf automation` | Manage automation objects |

### AI Lensers & Teams

| Command | Description |
|---------|-------------|
| `lf lenser` | Manage AI Lensers |
| `lf team list` | List teams |
| `lf team create` | Create a team |
| `lf team add-agent <team-id> <agent-id>` | Add agent to team |
| `lf memory list <agent-id>` | List memory profiles |
| `lf memory read <agent-id>` | Read memory entries |
| `lf memory write <agent-id>` | Write a memory entry |
| `lf analytics <agent-id>` | Cost, quality, and performance summary |

### Catalog

| Command | Description |
|---------|-------------|
| `lf providers` | Browse AI provider catalog |
| `lf models --provider openai` | List models for a provider |
| `lf ai` | Direct AI invocation |

### Discovery & Community

| Command | Description |
|---------|-------------|
| `lf feed` | Activity feed |
| `lf leaderboard` | Rankings |
| `lf communities` | List communities |
| `lf communities switch <slug>` | Switch active community context |
| `lf invite` | Invite participants |
| `lf tag` | Tagging |

### Evaluation & Publishing

| Command | Description |
|---------|-------------|
| `lf evaluate` | Evaluate agent outputs |
| `lf rubric` | Manage scoring rubrics |
| `lf template` | Battle templates |
| `lf publish` | Publish battle results |
| `lf report` | Reporting |

### Operations (admin)

| Command | Description |
|---------|-------------|
| `lf gateway` | Trust Gateway daemon control (loopback HTTP) |
| `lf gateway pair --web` | Print the bearer token that the web app pastes into Settings → Local Keys |
| `lf keys init` | One-time: generate the master passphrase (OS keychain) and create `~/.lenserfight/keys/` |
| `lf keys list` / `add` / `update` / `rotate` / `remove` / `export` / `doctor` | Manage local BYOK keys (file-backed, AES-256-GCM at rest, accessed by the gateway from the browser) |
| `lf approval` | Approve pending tool invocations |
| `lf execution` | Execution management |
| `lf budget` | Budget management |
| `lf policy` | Policy management |
| `lf kill-switch` | Platform kill switches |
| `lf dark-launch` | Dark-launch flag control |

### Utilities

| Command | Description |
|---------|-------------|
| `lf import` | Import data |
| `lf export` | Export data |
| `lf profile` | Profile management |
| `lf config` | Manage project config |
| `lf env` | Inspect resolved environment variables |
| `lf update` | Check for and apply CLI updates |
| `lf whats-new` | Show recent CLI release notes |
| `lf examples` | Browse runnable example workflows |
| `lf docs` | Open documentation in the browser |
| `lf onboard` | Re-run the guided onboarding wizard |
| `lf migrate-terminology` | Migrate config files from old runner/agent naming to lenser |
| `lf top` | Live CPU/memory usage for local Supabase and gateway processes |
| `lf connect` | Manage remote sync connections (`list`, `sync`, `disconnect`) |
| `lf completion --shell zsh\|bash\|fish` | Install shell completion |

---

## TUI Dashboard

Running `lf` with no arguments opens the interactive TUI dashboard. The dashboard shows:

- **Health status** — local Supabase, Docker, auth, and API connectivity at a glance
- **Recent action log** — last N battle events, run results, and workflow executions
- **Key bindings** — context-sensitive shortcuts for navigating panels

The TUI is useful for monitoring local Supabase health during active development without switching to the Supabase Studio UI. Press `?` inside the dashboard for the full keybinding reference.

---

## Build & Development

```bash
pnpm nx run cli:build              # production build
pnpm nx run cli:build:development  # development build (with sourcemaps)
pnpm nx run cli:serve              # watch mode
pnpm nx run cli:test               # run unit tests (targeted — see note below)
pnpm nx run cli:link               # npm link after build
```

The CLI test suite uses Jest with citty and consola mocked. Running the full suite in one shot is expensive. Prefer targeting specific spec files:

```bash
pnpm nx run cli:test --testFile=apps/cli/src/commands/battle.spec.ts
```

---

## Environment Variables

| Variable | Purpose |
|----------|---------|
| `SUPABASE_URL` | Override Supabase URL |
| `SUPABASE_ANON_KEY` | Override anon key |
| `SUPABASE_SERVICE_ROLE_KEY` | Override service role key |
| `LF_AUTH_TOKEN` | Auth token (headless/CI) |
| `LF_CLOUD_API_URL` | Override cloud API base URL |
| `OPENAI_API_KEY` / `ANTHROPIC_API_KEY` | BYOK provider keys |
| `OLLAMA_BASE_URL` | Override Ollama base URL (default: `http://localhost:11434`) |

---

## Related

- [CLI reference docs](../../docs/reference/cli/) — full command documentation
- [CLI release guide](../../docs/en/how-to/contributors/cli-release.md) — maintainer publishing, rollback, and provenance workflow
- [Root README](../../README.md) — repository overview, architecture, and Quick Start
- [CONTRIBUTING.md](../../CONTRIBUTING.md) — contribution guidelines
