# tura-ai/tura architecture

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

- Interactive diagram: https://gitdiagram.com/tura-ai/tura
- Repository: https://github.com/tura-ai/tura

## Overview

#### Purpose and entry points
Tura is a local, open-source coding-agent system. It provides an interactive terminal UI, direct and gateway-backed CLI runs, a local HTTP/SSE gateway, and a desktop GUI. User-facing launch paths are `tura`/`tura run` in `apps/tui/src/cli.ts`, gateway binary `crates/gateway/src/bin/gateway.rs`, direct executor `crates/gateway/src/bin/tura_exec.rs`, and Tauri desktop host `apps/tauri/src-tauri/src/main.rs`.

#### Client surfaces
The TUI is a TypeScript terminal client with command parsing, gateway lifecycle/autostart, event-driven reducer state, and terminal rendering: `apps/tui/src/index.ts`, `apps/tui/src/gateway/client.ts`, `apps/tui/src/tui/app.ts`. The GUI is a React/Vite application using a typed gateway SDK; it renders conversation, files, plans, and settings from shared gateway state: `apps/gui/app/src/entry.tsx`, `apps/gui/app/src/app.tsx`, `apps/gui/sdk/gateway/src/client.ts`. Tauri packages the GUI and grants narrowly declared native capabilities, including gateway startup and local file input.

#### Gateway: API and orchestration boundary
The Rust gateway is the clients’ local HTTP/SSE boundary. It defines stable contracts, routes API resources (sessions, prompts, files, providers, agents, commands), streams runtime events, and manages the router/runtime processes: `crates/gateway/src/web/server.rs`, `crates/gateway/src/api/session.rs`, `crates/gateway/src/contracts/event.rs`. It also owns provider auth/OAuth lifecycle at the API boundary: `crates/gateway/src/api/provider/oauth_flow.rs`, `crates/gateway/src/api/provider/auth_scheduler.rs`.

#### Router and worker process control
The router is a local daemon/process supervisor and IPC dispatcher. It resolves registered agents, personas, and tools; starts worker runtimes; scopes managed processes; handles recovery and shutdown; and exposes session-db services: `crates/router/src/main.rs`, `crates/router/src/runtime_dispatch.rs`, `crates/router/src/services/manager.rs`. This is the principal isolation boundary between gateway requests and execution workers.

#### Agent runtime turn loop
The runtime executes session turns as a state machine. It bootstraps or restores sessions, builds bounded context, selects prompt/agent behavior, calls providers, interprets streaming/tool requests, executes tools, checkpoints state, and emits gateway events: `crates/runtime/src/worker.rs`, `crates/runtime/src/turn_loop/mod.rs`, `crates/runtime/src/context/build.rs`. Runtime prompt styles and agent modes are embedded/configured under `crates/runtime/src/prompt_style/` and `agents/src/`.

#### Provider integration
The provider crate normalizes heterogeneous LLM APIs into runtime-facing streaming and response behavior, with provider-specific implementations for OpenAI-compatible services, ChatGPT/Codex, Claude Code, Google, Bedrock, Cohere, MiniMax, Qwen, and xAI: `crates/provider/src/tura_llm.rs`, `crates/provider/src/streaming.rs`, `crates/provider/src/llm/providers/mod.rs`. Credentials are external user configuration; providers are network-service boundaries.

#### Tools, commands, and execution safety
The primary model tool is structured `command_run`, which can represent multi-step shell, patch, planning, and task-status work in one model turn. Tool dispatch enforces command manifests/policies and routes external commands or shell execution: `crates/tools/src/command_run/handler.rs`, `crates/tools/src/runtime/dispatch.rs`, `crates/tools/src/shell_executor/execution.rs`. Built-in media/web commands are separate Rust executables—`commands/read_media/src/main.rs`, `commands/web_discover/src/main.rs`, `commands/generate_media/src/main.rs`—and may invoke local processing or external provider/web services.

#### Persistence and event flow
Session state, messages, logs, checkpoints, task plans, and snapshots flow runtime → router/session-db → gateway → SSE clients. The dedicated session-log service persists queued writes and supports recovery across process lifecycle changes: `crates/session_log/src/service.rs`, `crates/session_log/src/store.rs`, `crates/runtime/src/checkpoint/runtime.rs`. Gateway session stores bridge persisted state to frontend-oriented session APIs: `crates/gateway/src/session/store.rs`, `crates/gateway/src/session/store_frontend.rs`.

Main entry points: [`crates/gateway/src/web/server.rs`](https://github.com/tura-ai/tura/blob/main/crates/gateway/src/web/server.rs) (HTTP/SSE API), [`crates/runtime/src/worker.rs`](https://github.com/tura-ai/tura/blob/main/crates/runtime/src/worker.rs) (Worker runtime), [`crates/runtime/src/turn_loop/mod.rs`](https://github.com/tura-ai/tura/blob/main/crates/runtime/src/turn_loop/mod.rs) (Turn state machine), [`crates/router/src/runtime_dispatch.rs`](https://github.com/tura-ai/tura/blob/main/crates/router/src/runtime_dispatch.rs) (Router daemon)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_clients["Client surfaces"]
  node_tui["Terminal UI &amp; CLI<br/>TypeScript client<br/>[cli.ts]"]
  node_gui["React desktop UI<br/>React/Vite client<br/>[app.tsx]"]
  node_tauri["Tauri desktop host<br/>[main.rs]"]
end

subgraph group_gateway["Gateway &amp; control"]
  node_gateway_bin{{"Gateway binary<br/>Rust entry point<br/>[gateway.rs]"}}
  node_gateway_api["HTTP/SSE API<br/>Rust gateway<br/>[server.rs]"]
  node_gateway_sessions["Frontend session store<br/>gateway state bridge<br/>[store_frontend.rs]"]
  node_oauth["Provider auth lifecycle<br/>OAuth scheduler<br/>[oauth_flow.rs]"]
  node_router["Router daemon<br/>process supervisor"]
end

subgraph group_execution["Agent execution"]
  node_runtime["Worker runtime<br/>Rust worker<br/>[worker.rs]"]
  node_turn_loop["Turn state machine<br/>agent loop<br/>[mod.rs]"]
  node_context["Bounded context builder<br/>context assembly<br/>[build.rs]"]
  node_tools["Tool dispatcher<br/>policy enforcement<br/>[dispatch.rs]"]
  node_command_run["command_run<br/>structured model tool<br/>[handler.rs]"]
  node_shell["Shell executor<br/>local execution<br/>[execution.rs]"]
end

subgraph group_persistence["State &amp; integrations"]
  node_providers["LLM provider adapter<br/>provider normalization<br/>[tura_llm.rs]"]
  node_external_llms(("LLM services<br/>external network services"))
  node_session_db[("Router session DB services<br/>session persistence<br/>[manager.rs]")]
  node_session_log[("Session log store<br/>durable event store<br/>[service.rs]")]
  node_checkpoint[("Runtime checkpoints<br/>recovery state<br/>[runtime.rs]")]
end

node_tauri -->|"hosts"| node_gui
node_tui -->|"autostarts"| node_gateway_bin
node_gateway_bin -->|"serves"| node_gateway_api
node_tui -->|"HTTP/SSE"| node_gateway_api
node_gui -->|"typed SDK"| node_gateway_api
node_gateway_api -->|"session APIs"| node_gateway_sessions
node_gateway_api -->|"provider auth"| node_oauth
node_gateway_api -->|"dispatches work"| node_router
node_router -->|"starts isolated worker"| node_runtime
node_runtime -->|"executes turns"| node_turn_loop
node_turn_loop -->|"builds context"| node_context
node_turn_loop -->|"model streaming"| node_providers
node_providers -->|"network calls"| node_external_llms
node_turn_loop -->|"tool requests"| node_tools
node_tools -->|"structured command"| node_command_run
node_command_run -->|"shell actions"| node_shell
node_runtime -->|"checkpoints"| node_checkpoint
node_runtime -->|"events and logs"| node_session_log
node_router -->|"session services"| node_session_db
node_session_log -->|"recovery state"| node_session_db
node_session_db -->|"persisted sessions"| node_gateway_sessions
node_gateway_api -.->|"SSE events"| node_tui
node_gateway_api -.->|"SSE events"| node_gui

click node_tui "https://github.com/tura-ai/tura/blob/main/apps/tui/src/cli.ts"
click node_gui "https://github.com/tura-ai/tura/blob/main/apps/gui/app/src/app.tsx"
click node_tauri "https://github.com/tura-ai/tura/blob/main/apps/tauri/src-tauri/src/main.rs"
click node_gateway_bin "https://github.com/tura-ai/tura/blob/main/crates/gateway/src/bin/gateway.rs"
click node_gateway_api "https://github.com/tura-ai/tura/blob/main/crates/gateway/src/web/server.rs"
click node_gateway_sessions "https://github.com/tura-ai/tura/blob/main/crates/gateway/src/session/store_frontend.rs"
click node_oauth "https://github.com/tura-ai/tura/blob/main/crates/gateway/src/api/provider/oauth_flow.rs"
click node_router "https://github.com/tura-ai/tura/blob/main/crates/router/src/runtime_dispatch.rs"
click node_runtime "https://github.com/tura-ai/tura/blob/main/crates/runtime/src/worker.rs"
click node_turn_loop "https://github.com/tura-ai/tura/blob/main/crates/runtime/src/turn_loop/mod.rs"
click node_context "https://github.com/tura-ai/tura/blob/main/crates/runtime/src/context/build.rs"
click node_tools "https://github.com/tura-ai/tura/blob/main/crates/tools/src/runtime/dispatch.rs"
click node_command_run "https://github.com/tura-ai/tura/blob/main/crates/tools/src/command_run/handler.rs"
click node_shell "https://github.com/tura-ai/tura/blob/main/crates/tools/src/shell_executor/execution.rs"
click node_providers "https://github.com/tura-ai/tura/blob/main/crates/provider/src/tura_llm.rs"
click node_session_db "https://github.com/tura-ai/tura/blob/main/crates/router/src/services/manager.rs"
click node_session_log "https://github.com/tura-ai/tura/blob/main/crates/session_log/src/service.rs"
click node_checkpoint "https://github.com/tura-ai/tura/blob/main/crates/runtime/src/checkpoint/runtime.rs"

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_tui,node_gui,node_tauri toneBlue
class node_gateway_bin,node_gateway_api,node_gateway_sessions,node_oauth,node_router toneAmber
class node_runtime,node_turn_loop,node_context,node_tools,node_command_run,node_shell toneMint
class node_providers,node_external_llms,node_session_db,node_session_log,node_checkpoint toneRose
```

## Components

### Client surfaces

- [Terminal UI & CLI](https://github.com/tura-ai/tura/blob/main/apps/tui/src/cli.ts): TypeScript client · Interactive UI plus direct and gateway-backed runs. · `apps/tui/src/cli.ts`
- [React desktop UI](https://github.com/tura-ai/tura/blob/main/apps/gui/app/src/app.tsx): React/Vite client · Renders shared session, file, plan, and settings state. · `apps/gui/app/src/app.tsx`
- [Tauri desktop host](https://github.com/tura-ai/tura/blob/main/apps/tauri/src-tauri/src/main.rs): desktop host · Packages the GUI and grants scoped native capabilities. · `apps/tauri/src-tauri/src/main.rs`

### Gateway & control

- [Gateway binary](https://github.com/tura-ai/tura/blob/main/crates/gateway/src/bin/gateway.rs): Rust entry point · Launches the local HTTP/SSE gateway. · `crates/gateway/src/bin/gateway.rs`
- [HTTP/SSE API](https://github.com/tura-ai/tura/blob/main/crates/gateway/src/web/server.rs): Rust gateway · Routes session, prompt, file, provider, agent, and command APIs. · `crates/gateway/src/web/server.rs`
- [Frontend session store](https://github.com/tura-ai/tura/blob/main/crates/gateway/src/session/store_frontend.rs): gateway state bridge · Adapts persisted session state for frontend APIs. · `crates/gateway/src/session/store_frontend.rs`
- [Provider auth lifecycle](https://github.com/tura-ai/tura/blob/main/crates/gateway/src/api/provider/oauth_flow.rs): OAuth scheduler · Owns provider authentication at the API boundary. · `crates/gateway/src/api/provider/oauth_flow.rs`
- [Router daemon](https://github.com/tura-ai/tura/blob/main/crates/router/src/runtime_dispatch.rs): process supervisor · IPC dispatcher and isolation boundary for worker runtimes. · `crates/router/src/runtime_dispatch.rs`

### Agent execution

- [Worker runtime](https://github.com/tura-ai/tura/blob/main/crates/runtime/src/worker.rs): Rust worker · Bootstraps or restores session execution. · `crates/runtime/src/worker.rs`
- [Turn state machine](https://github.com/tura-ai/tura/blob/main/crates/runtime/src/turn_loop/mod.rs): agent loop · Builds context, invokes models, handles streams and checkpoints. · `crates/runtime/src/turn_loop/mod.rs`
- [Bounded context builder](https://github.com/tura-ai/tura/blob/main/crates/runtime/src/context/build.rs): context assembly · `crates/runtime/src/context/build.rs`
- [Tool dispatcher](https://github.com/tura-ai/tura/blob/main/crates/tools/src/runtime/dispatch.rs): policy enforcement · Validates manifests and dispatches model tool requests. · `crates/tools/src/runtime/dispatch.rs`
- [command_run](https://github.com/tura-ai/tura/blob/main/crates/tools/src/command_run/handler.rs): structured model tool · Represents shell, patch, planning, and task-status work. · `crates/tools/src/command_run/handler.rs`
- [Shell executor](https://github.com/tura-ai/tura/blob/main/crates/tools/src/shell_executor/execution.rs): local execution · `crates/tools/src/shell_executor/execution.rs`

### State & integrations

- [LLM provider adapter](https://github.com/tura-ai/tura/blob/main/crates/provider/src/tura_llm.rs): provider normalization · Normalizes heterogeneous streaming LLM APIs. · `crates/provider/src/tura_llm.rs`
- **LLM services**: external network services · OpenAI-compatible, Claude, Google, Bedrock, and other providers.
- [Router session DB services](https://github.com/tura-ai/tura/blob/main/crates/router/src/services/manager.rs): session persistence · Serves session state across managed processes. · `crates/router/src/services/manager.rs`
- [Session log store](https://github.com/tura-ai/tura/blob/main/crates/session_log/src/service.rs): durable event store · Queues writes and supports lifecycle recovery. · `crates/session_log/src/service.rs`
- [Runtime checkpoints](https://github.com/tura-ai/tura/blob/main/crates/runtime/src/checkpoint/runtime.rs): recovery state · `crates/runtime/src/checkpoint/runtime.rs`

## Connections

- Tauri desktop host → React desktop UI: hosts
- Terminal UI & CLI → Gateway binary: autostarts
- Gateway binary → HTTP/SSE API: serves
- Terminal UI & CLI → HTTP/SSE API: HTTP/SSE
- React desktop UI → HTTP/SSE API: typed SDK
- HTTP/SSE API → Frontend session store: session APIs
- HTTP/SSE API → Provider auth lifecycle: provider auth
- HTTP/SSE API → Router daemon: dispatches work
- Router daemon → Worker runtime: starts isolated worker
- Worker runtime → Turn state machine: executes turns
- Turn state machine → Bounded context builder: builds context
- Turn state machine → LLM provider adapter: model streaming
- LLM provider adapter → LLM services: network calls
- Turn state machine → Tool dispatcher: tool requests
- Tool dispatcher → command_run: structured command
- command_run → Shell executor: shell actions
- Worker runtime → Runtime checkpoints: checkpoints
- Worker runtime → Session log store: events and logs
- Router daemon → Router session DB services: session services
- Session log store → Router session DB services: recovery state
- Router session DB services → Frontend session store: persisted sessions
- HTTP/SSE API → Terminal UI & CLI: SSE events
- HTTP/SSE API → React desktop UI: SSE events
