# rockbite/localforge architecture

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

- Interactive diagram: https://gitdiagram.com/rockbite/localforge
- Repository: https://github.com/rockbite/localforge

## Overview

#### Purpose and entry points
Localforge is a local intelligent coding assistant: it serves a browser UI, lets an LLM autonomously inspect and modify a selected workspace, and can run as an Electron desktop app. The CLI entry point is `cli.js`; the application/server composition starts at `src/index.js` and `src/server/index.js`. Electron packaging launches `electron/main.js`, with isolated renderer access via `electron/preload.js`.

#### Application host and UI boundary
The Node server renders EJS views and serves the static browser client. Server-rendered shell and component layout live in `views/main.ejs`, `views/partials/main-panel.ejs`, and `views/partials/right-panel.ejs`. The client is modular vanilla JavaScript: bootstrap/state in `public/js/main.js` and `public/js/state.js`, HTTP access in `public/js/api.js`, and real-time messaging in `public/js/socket.js`. This separates browser interaction from privileged filesystem, model, and process operations on the local backend.

#### Agent orchestration
The core agent service coordinates conversations, model calls, tools, and task progress in `src/services/agent/index.js`. Agent-facing HTTP endpoints are exposed through `src/routes/agentsRoutes.js`; persisted agent definitions/state are handled by `src/db/agentStore.js`. Prompt templates provide role-specific system instructions and transformations, centered on `prompts/main-system.ejs`, with specialist templates such as `prompts/expert-advice.ejs` and `prompts/compressor.ejs`.

#### Model-provider abstraction and accounting
LLM middleware routes requests through a provider adapter layer in `src/middleware/llm.js` and `src/middleware/providers/index.js`. Implementations support hosted and local model backends, including `src/middleware/providers/openai.js`, `src/middleware/providers/anthropic.js`, `src/middleware/providers/aws.bedrock.js`, Gemini, Vertex AI, Hugging Face, and Ollama. Usage/cost tracking is a separate service in `src/services/accounting/index.js`, using model pricing definitions from `src/config/pricing.js`.

#### Autonomous tool execution
The agent acts on the local workspace through a centralized tool registry at `tools/index.js`. File discovery and edits are represented by tools such as `tools/list/GrepTool.js`, `tools/list/EditTool.js`, and `tools/list/GlobTool.js`; command execution is mediated by `tools/list/BashTool.js` and security controls in `tools/utils/bashSecurity.js`. Additional capabilities include web fetch, browser automation under `tools/list/browser`, expert delegation, and task management. This is the critical trust boundary: model-generated actions are translated into local filesystem/process/network operations.

#### Workspace, sessions, and persistence
Project/workspace lifecycle is exposed via `src/routes/projectsRoutes.js`, while session state is managed in `src/services/sessions/index.js` with schemas in `src/services/sessions/schema.js`. General persistence resides in `src/db/store.js`. The UI’s project and workspace controllers—`public/js/ui/projects.js` and `public/js/ui/workspace.js`—surface this state, and `public/js/file-tree.js` presents filesystem structure.

#### Auxiliary capabilities and deployment
Context compression is implemented across `src/logic/compress.js`, `src/routes/compressionRoutes.js`, and `src/services/compressionTracker.js`. The service layer also supports MCP integrations (`src/logic/mcp.js`, `src/services/mcp/index.js`), image handling, task tracking, and update checks. Electron-specific local hosting and release updates are handled by `electron/server.js` and `electron/updater.js`; CI release/build workflows are in `.github/workflows/build-and-release.yml` and `.github/workflows/macos-build.yml`.

Main entry points: [`src/services/agent/index.js`](https://github.com/rockbite/localforge/blob/main/src/services/agent/index.js) (Agent orchestrator), [`src/server/index.js`](https://github.com/rockbite/localforge/blob/main/src/server/index.js) (Server composition), [`public/js/api.js`](https://github.com/rockbite/localforge/blob/main/public/js/api.js) (Client API & sockets), [`src/middleware/llm.js`](https://github.com/rockbite/localforge/blob/main/src/middleware/llm.js) (LLM middleware)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_ui["Browser UI"]
  node_views["EJS application shell<br/>server-rendered UI<br/>[main.ejs]"]
  node_browser["Browser client<br/>vanilla JS client<br/>[main.js]"]
  node_client_api["Client API &amp; sockets<br/>browser transport<br/>[api.js]"]
end

subgraph group_server["Local Server"]
  node_cli{{"CLI entry point<br/>Node CLI<br/>[cli.js]"}}
  node_app["Application host<br/>Node server<br/>[index.js]"]
  node_server["Server composition<br/>HTTP server<br/>[index.js]"]
  node_agent_routes["Agent endpoints<br/>HTTP routes<br/>[agentsRoutes.js]"]
  node_project_routes["Project endpoints<br/>HTTP routes<br/>[projectsRoutes.js]"]
  node_sessions[("Sessions &amp; persistence<br/>local state store<br/>[index.js]")]
end

subgraph group_agent["Agent Runtime"]
  node_agent_service["Agent orchestrator<br/>agent service<br/>[index.js]"]
  node_prompts["Prompt templates<br/>EJS prompts<br/>[main-system.ejs]"]
  node_llm["LLM middleware<br/>model gateway<br/>[llm.js]"]
  node_providers["Model providers<br/>provider adapters<br/>[index.js]"]
  node_accounting["Usage accounting<br/>accounting service<br/>[index.js]"]
  node_tools{{"Tool registry<br/>agent tool boundary<br/>[index.js]"}}
  node_bash_security["Command security<br/>execution controls<br/>[bashSecurity.js]"]
  node_compression["Context compression<br/>context service<br/>[compress.js]"]
end

subgraph group_platform["Desktop &amp; Delivery"]
  node_electron["Electron desktop host<br/>desktop runtime<br/>[main.js]"]
  node_release["Build &amp; release workflow<br/>CI workflow"]
end

node_workspace[("Workspace filesystem<br/>selected local workspace")]

node_cli -->|"starts"| node_app
node_app -->|"composes"| node_server
node_server -->|"renders"| node_views
node_server -->|"serves"| node_browser
node_browser -->|"uses"| node_client_api
node_client_api -->|"agent requests"| node_agent_routes
node_client_api -->|"workspace requests"| node_project_routes
node_agent_routes -->|"invokes"| node_agent_service
node_project_routes -->|"loads and saves"| node_sessions
node_agent_service -->|"renders"| node_prompts
node_agent_service -->|"requests inference"| node_llm
node_llm -->|"selects adapter"| node_providers
node_llm -->|"records usage"| node_accounting
node_agent_service -->|"dispatches actions"| node_tools
node_tools -->|"guards commands"| node_bash_security
node_tools -->|"inspects and modifies"| node_workspace
node_agent_service -->|"persists state"| node_sessions
node_agent_service -->|"compresses context"| node_compression
node_electron -->|"hosts locally"| node_server
node_release -->|"packages"| node_electron

click node_cli "https://github.com/rockbite/localforge/blob/main/cli.js"
click node_app "https://github.com/rockbite/localforge/blob/main/src/index.js"
click node_server "https://github.com/rockbite/localforge/blob/main/src/server/index.js"
click node_views "https://github.com/rockbite/localforge/blob/main/views/main.ejs"
click node_browser "https://github.com/rockbite/localforge/blob/main/public/js/main.js"
click node_client_api "https://github.com/rockbite/localforge/blob/main/public/js/api.js"
click node_agent_routes "https://github.com/rockbite/localforge/blob/main/src/routes/agentsRoutes.js"
click node_project_routes "https://github.com/rockbite/localforge/blob/main/src/routes/projectsRoutes.js"
click node_agent_service "https://github.com/rockbite/localforge/blob/main/src/services/agent/index.js"
click node_prompts "https://github.com/rockbite/localforge/blob/main/prompts/main-system.ejs"
click node_llm "https://github.com/rockbite/localforge/blob/main/src/middleware/llm.js"
click node_providers "https://github.com/rockbite/localforge/blob/main/src/middleware/providers/index.js"
click node_accounting "https://github.com/rockbite/localforge/blob/main/src/services/accounting/index.js"
click node_tools "https://github.com/rockbite/localforge/blob/main/tools/index.js"
click node_bash_security "https://github.com/rockbite/localforge/blob/main/tools/utils/bashSecurity.js"
click node_sessions "https://github.com/rockbite/localforge/blob/main/src/services/sessions/index.js"
click node_compression "https://github.com/rockbite/localforge/blob/main/src/logic/compress.js"
click node_electron "https://github.com/rockbite/localforge/blob/main/electron/main.js"
click node_release "https://github.com/rockbite/localforge/blob/main/.github/workflows/build-and-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_views,node_browser,node_client_api toneBlue
class node_cli,node_app,node_server,node_agent_routes,node_project_routes,node_sessions toneAmber
class node_agent_service,node_prompts,node_llm,node_providers,node_accounting,node_tools,node_bash_security,node_compression toneMint
class node_electron,node_release toneRose
class node_workspace toneNeutral
```

## Components

### Browser UI

Server-rendered shell and modular browser client.

- [EJS application shell](https://github.com/rockbite/localforge/blob/main/views/main.ejs): server-rendered UI · Main layout and panel structure. · `views/main.ejs`
- [Browser client](https://github.com/rockbite/localforge/blob/main/public/js/main.js): vanilla JS client · Bootstraps UI state and user interactions. · `public/js/main.js`
- [Client API & sockets](https://github.com/rockbite/localforge/blob/main/public/js/api.js): browser transport · HTTP and realtime messaging to the local backend. · `public/js/api.js`

### Local Server

HTTP, realtime, and application composition.

- [CLI entry point](https://github.com/rockbite/localforge/blob/main/cli.js): Node CLI · `cli.js`
- [Application host](https://github.com/rockbite/localforge/blob/main/src/index.js): Node server · Composes the local application server. · `src/index.js`
- [Server composition](https://github.com/rockbite/localforge/blob/main/src/server/index.js): HTTP server · Serves views, static client, routes, and realtime services. · `src/server/index.js`
- [Agent endpoints](https://github.com/rockbite/localforge/blob/main/src/routes/agentsRoutes.js): HTTP routes · `src/routes/agentsRoutes.js`
- [Project endpoints](https://github.com/rockbite/localforge/blob/main/src/routes/projectsRoutes.js): HTTP routes · `src/routes/projectsRoutes.js`
- [Sessions & persistence](https://github.com/rockbite/localforge/blob/main/src/services/sessions/index.js): local state store · Session schemas plus general and agent state. · `src/services/sessions/index.js`

### Agent Runtime

Conversation orchestration and autonomous actions.

- [Agent orchestrator](https://github.com/rockbite/localforge/blob/main/src/services/agent/index.js): agent service · Coordinates conversations, prompts, models, tools, and progress. · `src/services/agent/index.js`
- [Prompt templates](https://github.com/rockbite/localforge/blob/main/prompts/main-system.ejs): EJS prompts · Role instructions and context transformations. · `prompts/main-system.ejs`
- [LLM middleware](https://github.com/rockbite/localforge/blob/main/src/middleware/llm.js): model gateway · Routes model requests through provider adapters. · `src/middleware/llm.js`
- [Model providers](https://github.com/rockbite/localforge/blob/main/src/middleware/providers/index.js): provider adapters · Hosted and local backends including OpenAI, Anthropic, Bedrock, and Ollama. · `src/middleware/providers/index.js`
- [Usage accounting](https://github.com/rockbite/localforge/blob/main/src/services/accounting/index.js): accounting service · Tracks model usage and cost from pricing definitions. · `src/services/accounting/index.js`
- [Tool registry](https://github.com/rockbite/localforge/blob/main/tools/index.js): agent tool boundary · Translates model actions into local capabilities. · `tools/index.js`
- [Command security](https://github.com/rockbite/localforge/blob/main/tools/utils/bashSecurity.js): execution controls · Guards Bash tool process execution. · `tools/utils/bashSecurity.js`
- [Context compression](https://github.com/rockbite/localforge/blob/main/src/logic/compress.js): context service · Compresses agent context and tracks compression state. · `src/logic/compress.js`

### Desktop & Delivery

Electron hosting and release automation.

- [Electron desktop host](https://github.com/rockbite/localforge/blob/main/electron/main.js): desktop runtime · Launches local hosting with isolated renderer access. · `electron/main.js`
- [Build & release workflow](https://github.com/rockbite/localforge/blob/main/.github/workflows/build-and-release.yml): CI workflow · `.github/workflows/build-and-release.yml`

### Other components

- **Workspace filesystem**: selected local workspace · Files are discovered, edited, and executed by tools.

## Connections

- CLI entry point → Application host: starts
- Application host → Server composition: composes
- Server composition → EJS application shell: renders
- Server composition → Browser client: serves
- Browser client → Client API & sockets: uses
- Client API & sockets → Agent endpoints: agent requests
- Client API & sockets → Project endpoints: workspace requests
- Agent endpoints → Agent orchestrator: invokes
- Project endpoints → Sessions & persistence: loads and saves
- Agent orchestrator → Prompt templates: renders
- Agent orchestrator → LLM middleware: requests inference
- LLM middleware → Model providers: selects adapter
- LLM middleware → Usage accounting: records usage
- Agent orchestrator → Tool registry: dispatches actions
- Tool registry → Command security: guards commands
- Tool registry → Workspace filesystem: inspects and modifies
- Agent orchestrator → Sessions & persistence: persists state
- Agent orchestrator → Context compression: compresses context
- Electron desktop host → Server composition: hosts locally
- Build & release workflow → Electron desktop host: packages
