# bytonylee/free-router architecture

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

- Interactive diagram: https://gitdiagram.com/bytonylee/free-router
- Repository: https://github.com/bytonylee/free-router

## Overview

#### Purpose and entry points
Free Router is a Bun/Node-distributed TypeScript CLI that discovers and benchmarks free AI models from NVIDIA NIM and OpenRouter, then configures a selected model for local AI clients. The executable entry point is `src/bin/free-router.ts`; it selects interactive TUI mode, help, or the non-interactive `--best` workflow.

#### Model catalog and ranking data
Static model capabilities, support metadata, aliases, and rankings are repository-managed data rather than fetched at runtime. The CLI loads and normalizes this catalog to construct provider/model candidates, tiers, context windows, and target compatibility:
- `data/model-rankings.json`
- `data/model-support.json`
- `src/lib/models.ts`

Catalog maintenance is an explicit pipeline: `scripts/update-models.ts` updates data under the contract in `docs/model-sync-data-contract.md`, and GitHub automation runs it through `.github/workflows/model-catalog-sync.yml`.

#### Configuration and provider boundary
Configuration is persisted in the user home directory (`~/.free-router.json`, intended mode 0600), while environment variables take precedence for API keys. Provider enablement and UI behavior are centralized in:
- `src/lib/config.ts`
- `src/lib/models.ts`

The provider boundary supports NVIDIA NIM and OpenRouter credentials/endpoints. Missing or failing keys remain distinguishable from service/model availability so the UI can direct users to key editing without conflating authentication with ping health.

#### Ping and live metrics pipeline
The CLI probes candidate model endpoints in parallel, records HTTP status and latency, and derives rolling average latency, uptime, and verdict state. This is the core operational data flow used by both the live interface and best-model selection:
- `src/lib/ping.ts`
- `src/lib/models.ts`
- `src/lib/utils.ts`

Interactive mode repeatedly refreshes these metrics; `--best` performs fixed rounds and prints only the selected model ID, making stdout suitable for scripts.

#### Terminal interaction layer
The interactive runtime is an Ink/React terminal application. It owns keyboard-driven filtering, sorting, navigation, refresh cadence, focus-safe redraw behavior, and transitions among workflows:
- `src/tui/ink-harness.ts`
- `src/tui/first-run-app.tsx`
- `src/tui/settings-app.tsx`

`src/tui/target-picker-app.tsx` is the selection handoff boundary, while `src/tui/update-app.tsx` handles the in-app global update/restart path.

#### Target configuration and system effects
After selection, the target picker transforms model/provider metadata into each target’s native configuration, backs up existing files, and may launch OpenCode. Target-specific paths include OpenCode JSON, OpenClaw JSON, and Hermes YAML; plaintext key export is opt-in. The write/launch logic is concentrated in:
- `src/lib/targets.ts`
- `src/tui/target-picker-app.tsx`
- `src/lib/config.ts`

This is the primary side-effect boundary: pings contact external provider APIs; target handoff modifies user configuration files and can spawn OpenCode.

#### Distribution, website, and release infrastructure
The package is distributed through npm-compatible launchers, with compatibility shims for historical command/package names:
- `package.json`
- `publish/frouter-cli-shim/bin.js`
- `publish/legacy-scope-shim/bin.js`

A separate Vite static site renders catalog-derived content and is deployable to Vercel:
- `site/src/main.ts`
- `site/scripts/build-site.mjs`
- `site/vercel.json`

CI, releases, and governance are GitHub Actions-driven, notably `.github/workflows/ci.yml` and `.github/workflows/release.yml`.

Main entry points: [`src/bin/free-router.ts`](https://github.com/bytonylee/free-router/blob/main/src/bin/free-router.ts) (Free Router CLI), [`src/tui/ink-harness.ts`](https://github.com/bytonylee/free-router/blob/main/src/tui/ink-harness.ts) (Ink TUI harness), [`src/lib/ping.ts`](https://github.com/bytonylee/free-router/blob/main/src/lib/ping.ts) (Ping metrics), [`src/lib/models.ts`](https://github.com/bytonylee/free-router/blob/main/src/lib/models.ts) (Model candidates)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_cli["CLI runtime"]
  node_entry["Free Router CLI<br/>TypeScript entrypoint<br/>[free-router.ts]"]
  node_config["Configuration<br/>config boundary<br/>[config.ts]"]
  node_models["Model candidates<br/>catalog normalization<br/>[models.ts]"]
  node_ping["Ping metrics<br/>probe pipeline<br/>[ping.ts]"]
  node_utils["Metric utilities<br/>shared utilities<br/>[utils.ts]"]
  node_ink["Ink TUI harness<br/>terminal runtime<br/>[ink-harness.ts]"]
  node_first_run["First-run flow<br/>TUI workflow<br/>[first-run-app.tsx]"]
  node_settings["Settings flow<br/>TUI workflow<br/>[settings-app.tsx]"]
end

subgraph group_catalog["Model catalog"]
  node_rankings["Model rankings<br/>catalog data"]
  node_support["Model support<br/>catalog data<br/>[model-support.json]"]
  node_catalog_update["Catalog updater<br/>maintenance script<br/>[update-models.ts]"]
  node_catalog_contract["Sync data contract<br/>documentation"]
  node_catalog_sync["Catalog sync action<br/>GitHub workflow"]
end

subgraph group_targets["Target handoff"]
  node_target_picker["Target picker<br/>TUI handoff"]
  node_target_writer["Target writer<br/>system effects<br/>[targets.ts]"]
end

subgraph group_distribution["Distribution and release"]
  node_package["Package manifest<br/>npm package<br/>[package.json]"]
  node_frouter_shim["Frouter compatibility shim<br/>launcher shim<br/>[bin.js]"]
  node_legacy_shim["Legacy scope shim<br/>launcher shim<br/>[bin.js]"]
  node_site["Catalog website<br/>Vite static site<br/>[main.ts]"]
  node_site_build["Site builder<br/>build script<br/>[build-site.mjs]"]
  node_vercel["Vercel deployment<br/>deployment config<br/>[vercel.json]"]
  node_ci["CI workflow<br/>GitHub workflow<br/>[ci.yml]"]
  node_release["Release workflow<br/>GitHub workflow<br/>[release.yml]"]
end

node_entry -->|"interactive mode"| node_ink
node_entry -->|"--best selection"| node_models
node_entry -->|"loads settings"| node_config
node_config -->|"enables providers"| node_models
node_rankings -->|"ranking data"| node_models
node_support -->|"capabilities and aliases"| node_models
node_models -->|"candidate endpoints"| node_ping
node_config -->|"provider credentials"| node_ping
node_utils -->|"rolling calculations"| node_ping
node_ping -->|"live metrics"| node_ink
node_ping -->|"best-model result"| node_entry
node_ink -->|"onboarding"| node_first_run
node_ink -->|"settings"| node_settings
node_ink -->|"selected model"| node_target_picker
node_target_picker -->|"write target config"| node_target_writer
node_config -->|"key export policy"| node_target_writer
node_catalog_contract -.->|"governs updates"| node_catalog_update
node_catalog_update -->|"updates"| node_rankings
node_catalog_update -->|"updates"| node_support
node_catalog_sync -->|"runs"| node_catalog_update
node_package -->|"publishes executable"| node_entry
node_frouter_shim -->|"compatibility launch"| node_entry
node_legacy_shim -->|"compatibility launch"| node_entry
node_rankings -->|"catalog content"| node_site
node_support -->|"catalog content"| node_site
node_site_build -->|"builds"| node_site
node_vercel -->|"deploys"| node_site
node_ci -.->|"validates"| node_package
node_release -->|"publishes"| node_package

click node_entry "https://github.com/bytonylee/free-router/blob/main/src/bin/free-router.ts"
click node_config "https://github.com/bytonylee/free-router/blob/main/src/lib/config.ts"
click node_models "https://github.com/bytonylee/free-router/blob/main/src/lib/models.ts"
click node_ping "https://github.com/bytonylee/free-router/blob/main/src/lib/ping.ts"
click node_utils "https://github.com/bytonylee/free-router/blob/main/src/lib/utils.ts"
click node_ink "https://github.com/bytonylee/free-router/blob/main/src/tui/ink-harness.ts"
click node_first_run "https://github.com/bytonylee/free-router/blob/main/src/tui/first-run-app.tsx"
click node_settings "https://github.com/bytonylee/free-router/blob/main/src/tui/settings-app.tsx"
click node_rankings "https://github.com/bytonylee/free-router/blob/main/data/model-rankings.json"
click node_support "https://github.com/bytonylee/free-router/blob/main/data/model-support.json"
click node_catalog_update "https://github.com/bytonylee/free-router/blob/main/scripts/update-models.ts"
click node_catalog_contract "https://github.com/bytonylee/free-router/blob/main/docs/model-sync-data-contract.md"
click node_catalog_sync "https://github.com/bytonylee/free-router/blob/main/.github/workflows/model-catalog-sync.yml"
click node_target_picker "https://github.com/bytonylee/free-router/blob/main/src/tui/target-picker-app.tsx"
click node_target_writer "https://github.com/bytonylee/free-router/blob/main/src/lib/targets.ts"
click node_package "https://github.com/bytonylee/free-router/blob/main/package.json"
click node_frouter_shim "https://github.com/bytonylee/free-router/blob/main/publish/frouter-cli-shim/bin.js"
click node_legacy_shim "https://github.com/bytonylee/free-router/blob/main/publish/legacy-scope-shim/bin.js"
click node_site "https://github.com/bytonylee/free-router/blob/main/site/src/main.ts"
click node_site_build "https://github.com/bytonylee/free-router/blob/main/site/scripts/build-site.mjs"
click node_vercel "https://github.com/bytonylee/free-router/blob/main/site/vercel.json"
click node_ci "https://github.com/bytonylee/free-router/blob/main/.github/workflows/ci.yml"
click node_release "https://github.com/bytonylee/free-router/blob/main/.github/workflows/release.yml"

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_entry,node_config,node_models,node_ping,node_utils,node_ink,node_first_run,node_settings toneBlue
class node_rankings,node_support,node_catalog_update,node_catalog_contract,node_catalog_sync toneAmber
class node_target_picker,node_target_writer toneMint
class node_package,node_frouter_shim,node_legacy_shim,node_site,node_site_build,node_vercel,node_ci,node_release toneRose
```

## Components

### CLI runtime

Interactive and scripted model selection.

- [Free Router CLI](https://github.com/bytonylee/free-router/blob/main/src/bin/free-router.ts): TypeScript entrypoint · Selects TUI, help, or the scripted best-model workflow. · `src/bin/free-router.ts`
- [Configuration](https://github.com/bytonylee/free-router/blob/main/src/lib/config.ts): config boundary · Reads user config and environment-overridden API keys. · `src/lib/config.ts`
- [Model candidates](https://github.com/bytonylee/free-router/blob/main/src/lib/models.ts): catalog normalization · Builds provider candidates, tiers, context windows, and compatibility. · `src/lib/models.ts`
- [Ping metrics](https://github.com/bytonylee/free-router/blob/main/src/lib/ping.ts): probe pipeline · Parallel endpoint probes derive latency, uptime, and verdicts. · `src/lib/ping.ts`
- [Metric utilities](https://github.com/bytonylee/free-router/blob/main/src/lib/utils.ts): shared utilities · Supports rolling metrics and verdict derivation. · `src/lib/utils.ts`
- [Ink TUI harness](https://github.com/bytonylee/free-router/blob/main/src/tui/ink-harness.ts): terminal runtime · Owns live refresh, keyboard interaction, filtering, and navigation. · `src/tui/ink-harness.ts`
- [First-run flow](https://github.com/bytonylee/free-router/blob/main/src/tui/first-run-app.tsx): TUI workflow · `src/tui/first-run-app.tsx`
- [Settings flow](https://github.com/bytonylee/free-router/blob/main/src/tui/settings-app.tsx): TUI workflow · Edits provider credentials and UI settings. · `src/tui/settings-app.tsx`

### Model catalog

Repository-managed provider and capability data.

- [Model rankings](https://github.com/bytonylee/free-router/blob/main/data/model-rankings.json): catalog data · `data/model-rankings.json`
- [Model support](https://github.com/bytonylee/free-router/blob/main/data/model-support.json): catalog data · `data/model-support.json`
- [Catalog updater](https://github.com/bytonylee/free-router/blob/main/scripts/update-models.ts): maintenance script · Updates managed model data under the sync contract. · `scripts/update-models.ts`
- [Sync data contract](https://github.com/bytonylee/free-router/blob/main/docs/model-sync-data-contract.md): documentation · `docs/model-sync-data-contract.md`
- [Catalog sync action](https://github.com/bytonylee/free-router/blob/main/.github/workflows/model-catalog-sync.yml): GitHub workflow · `.github/workflows/model-catalog-sync.yml`

### Target handoff

Selected models become local client configuration.

- [Target picker](https://github.com/bytonylee/free-router/blob/main/src/tui/target-picker-app.tsx): TUI handoff · Transforms a selection into a target-specific configuration request. · `src/tui/target-picker-app.tsx`
- [Target writer](https://github.com/bytonylee/free-router/blob/main/src/lib/targets.ts): system effects · Backs up and writes OpenCode/OpenClaw JSON or Hermes YAML; may launch OpenCode. · `src/lib/targets.ts`

### Distribution and release

Package launchers, website, and automation.

- [Package manifest](https://github.com/bytonylee/free-router/blob/main/package.json): npm package · Defines distributed CLI package and launchers. · `package.json`
- [Frouter compatibility shim](https://github.com/bytonylee/free-router/blob/main/publish/frouter-cli-shim/bin.js): launcher shim · `publish/frouter-cli-shim/bin.js`
- [Legacy scope shim](https://github.com/bytonylee/free-router/blob/main/publish/legacy-scope-shim/bin.js): launcher shim · `publish/legacy-scope-shim/bin.js`
- [Catalog website](https://github.com/bytonylee/free-router/blob/main/site/src/main.ts): Vite static site · Renders catalog-derived content for Vercel deployment. · `site/src/main.ts`
- [Site builder](https://github.com/bytonylee/free-router/blob/main/site/scripts/build-site.mjs): build script · `site/scripts/build-site.mjs`
- [Vercel deployment](https://github.com/bytonylee/free-router/blob/main/site/vercel.json): deployment config · `site/vercel.json`
- [CI workflow](https://github.com/bytonylee/free-router/blob/main/.github/workflows/ci.yml): GitHub workflow · `.github/workflows/ci.yml`
- [Release workflow](https://github.com/bytonylee/free-router/blob/main/.github/workflows/release.yml): GitHub workflow · `.github/workflows/release.yml`

## Connections

- Free Router CLI → Ink TUI harness: interactive mode
- Free Router CLI → Model candidates: --best selection
- Free Router CLI → Configuration: loads settings
- Configuration → Model candidates: enables providers
- Model rankings → Model candidates: ranking data
- Model support → Model candidates: capabilities and aliases
- Model candidates → Ping metrics: candidate endpoints
- Configuration → Ping metrics: provider credentials
- Metric utilities → Ping metrics: rolling calculations
- Ping metrics → Ink TUI harness: live metrics
- Ping metrics → Free Router CLI: best-model result
- Ink TUI harness → First-run flow: onboarding
- Ink TUI harness → Settings flow: settings
- Ink TUI harness → Target picker: selected model
- Target picker → Target writer: write target config
- Configuration → Target writer: key export policy
- Sync data contract → Catalog updater: governs updates
- Catalog updater → Model rankings: updates
- Catalog updater → Model support: updates
- Catalog sync action → Catalog updater: runs
- Package manifest → Free Router CLI: publishes executable
- Frouter compatibility shim → Free Router CLI: compatibility launch
- Legacy scope shim → Free Router CLI: compatibility launch
- Model rankings → Catalog website: catalog content
- Model support → Catalog website: catalog content
- Site builder → Catalog website: builds
- Vercel deployment → Catalog website: deploys
- CI workflow → Package manifest: validates
- Release workflow → Package manifest: publishes
