# dnyaneshs1/ucode architecture

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

- Interactive diagram: https://gitdiagram.com/dnyaneshs1/ucode
- Repository: https://github.com/dnyaneshs1/ucode

## Overview

#### Purpose and Entry Points
Opti-Prompt is a local Python multi-turn LLM gateway that reduces token use through classification, prompt optimization, caching, and provider failover. It exposes two user-facing entry points:
- CLI: `app/main.py`
- Streamlit UI: `app/streamlit_app.py`

Both orchestrate the same core conversation and generation services; API credentials and operational limits are environment-driven via `.env.example`.

#### Request Orchestration and Provider Boundary
The central LLM execution layer is `core/gemini_wrapper_v2.py`. It receives a prompt plus conversation context, selects a provider/model, enforces generation limits, and handles failures/quota exhaustion by continuing through the fallback chain.
- `core/provider_registry.py` defines available provider configuration/order.
- `core/gemini_wrapper_v2.py` implements provider-specific invocation and context-aware continuation.
- `.env.example` supplies keys for Gemini, Groq, HuggingFace, GitHub Models, and OpenRouter.

External inference services are Gemini (primary), Groq, HuggingFace, GitHub Models, and OpenRouter. This is an API-client application, not a self-hosted LLM runtime.

#### Conversation State and Context Window
Session lifecycle and multi-turn state are owned by `core/session_manager.py`. Conversation history is persisted locally and constrained to a sliding context window before provider calls. `core/context_manager.py` supports preservation/transfer of context, especially when a provider returns only a partial response and execution falls back.
- `core/session_manager.py`
- `core/context_manager.py`
- `app/streamlit_app.py`

The UI maintains its Streamlit session state while durable conversation records are managed by the core session layer.

#### Optimization Pipeline
Before live generation, the system determines likely response complexity and uses it to choose output-token limits. `core/complexity_classifier_module.py` loads and applies the bundled SetFit classifier; its limits are data-driven.
- `core/complexity_classifier_module.py`
- `classifier/complexity_token_limits.json`
- `classifier/complexity_classifier/model.safetensors`

The classifier artifact and tokenizer/config files under `classifier/complexity_classifier/` are shipped with the repository, making complexity decisions local rather than dependent on an external classification API.

#### Cache Layers
Caching is a first-class pre-provider boundary in `core/semantic_cache.py`. The documented flow checks an exact SHA256 prompt cache first, then searches semantic similarity before calling a remote model. Semantic matching relies on the bundled sentence-transformer embedder.
- `core/semantic_cache.py`
- `embedder/model.safetensors`
- `embedder/config.json`

Cache hits avoid or reduce new provider-token consumption; live responses can be stored for later exact or semantically similar requests.

#### Artifacts, Evaluation, and Observability
Generated code/content can be extracted and made available as local artifacts by `core/artifact_manager.py`. Runtime usage, cache state, complexity class, token use, latency, and provider outcomes are recorded through `core/runtime_logger.py`; `core/evaluation_module.py` provides an evaluation boundary around response/system behavior.
- `core/artifact_manager.py`
- `core/runtime_logger.py`
- `core/evaluation_module.py`

Operational data is local filesystem state (sessions, caches, artifacts, CSV usage reporting), rather than a database or deployed observability service.

#### End-to-End Control Flow
A user submits a turn through `app/main.py` or `app/streamlit_app.py`; prior session history is loaded; the prompt is classified and capped; exact/semantic caches are checked; and only a miss reaches `core/gemini_wrapper_v2.py`. The wrapper calls the registered primary provider, captures partial context if needed, and traverses fallback providers on quota/error. The final response updates session history, caches, artifacts where applicable, and runtime/usage logs.

Main entry points: [`core/gemini_wrapper_v2.py`](https://github.com/dnyaneshs1/ucode/blob/main/core/gemini_wrapper_v2.py) (LLM wrapper), [`core/session_manager.py`](https://github.com/dnyaneshs1/ucode/blob/main/core/session_manager.py) (Session manager), [`core/semantic_cache.py`](https://github.com/dnyaneshs1/ucode/blob/main/core/semantic_cache.py) (Semantic cache), [`core/complexity_classifier_module.py`](https://github.com/dnyaneshs1/ucode/blob/main/core/complexity_classifier_module.py) (Complexity classifier)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_entry["User Entry Points"]
  node_cli["CLI<br/>Python entry point<br/>[main.py]"]
  node_ui["Streamlit UI<br/>Python entry point<br/>[streamlit_app.py]"]
end

subgraph group_core["Core Request Pipeline"]
  node_session[("Session manager<br/>conversation state<br/>[session_manager.py]")]
  node_context["Context manager<br/>continuation context<br/>[context_manager.py]"]
  node_classifier["Complexity classifier<br/>local SetFit classifier"]
  node_cache[("Semantic cache<br/>cache boundary<br/>[semantic_cache.py]")]
  node_wrapper{{"LLM wrapper<br/>generation orchestrator"}}
  node_registry["Provider registry<br/>provider routing config"]
  node_artifacts["Artifact manager<br/>local artifact output"]
  node_runtime_log["Runtime logger<br/>usage telemetry<br/>[runtime_logger.py]"]
  node_evaluation["Evaluation module<br/>response evaluation"]
end

subgraph group_local["Local Models &amp; State"]
  node_token_limits["Complexity token limits<br/>limit data"]
  node_classifier_model["Classifier model<br/>bundled model artifact<br/>[model.safetensors]"]
  node_embedder["Sentence embedder<br/>bundled embedding model<br/>[model.safetensors]"]
  node_local_state[("Local filesystem state<br/>durable local storage")]
  node_environment["Environment settings<br/>credentials and limits<br/>[.env.example]"]
end

subgraph group_providers["Inference Providers"]
  node_provider_apis{{"Remote LLM APIs<br/>external inference"}}
end

node_cli -->|"submits turn"| node_session
node_ui -->|"submits turn"| node_session
node_session -->|"prompt with history"| node_classifier
node_session -->|"provides context"| node_context
node_session -->|"loads and saves history"| node_local_state
node_classifier -->|"selects cap"| node_token_limits
node_classifier -->|"runs locally"| node_classifier_model
node_classifier -->|"classified request"| node_cache
node_cache -->|"semantic lookup"| node_embedder
node_cache -->|"cache miss"| node_wrapper
node_cache -->|"reads and stores entries"| node_local_state
node_wrapper -->|"selects provider chain"| node_registry
node_wrapper -->|"primary and fallback calls"| node_provider_apis
node_provider_apis -->|"response or quota/error"| node_wrapper
node_wrapper -.->|"partial-response transfer"| node_context
node_environment -.->|"API keys and limits"| node_wrapper
node_wrapper -->|"final response"| node_session
node_wrapper -->|"stores live response"| node_cache
node_wrapper -->|"generated content"| node_artifacts
node_wrapper -->|"execution outcome"| node_runtime_log
node_wrapper -->|"response behavior"| node_evaluation
node_artifacts -->|"writes artifacts"| node_local_state
node_runtime_log -->|"writes CSV usage"| node_local_state

click node_cli "https://github.com/dnyaneshs1/ucode/blob/main/app/main.py"
click node_ui "https://github.com/dnyaneshs1/ucode/blob/main/app/streamlit_app.py"
click node_session "https://github.com/dnyaneshs1/ucode/blob/main/core/session_manager.py"
click node_context "https://github.com/dnyaneshs1/ucode/blob/main/core/context_manager.py"
click node_classifier "https://github.com/dnyaneshs1/ucode/blob/main/core/complexity_classifier_module.py"
click node_token_limits "https://github.com/dnyaneshs1/ucode/blob/main/classifier/complexity_token_limits.json"
click node_classifier_model "https://github.com/dnyaneshs1/ucode/blob/main/classifier/complexity_classifier/model.safetensors"
click node_cache "https://github.com/dnyaneshs1/ucode/blob/main/core/semantic_cache.py"
click node_embedder "https://github.com/dnyaneshs1/ucode/blob/main/embedder/model.safetensors"
click node_wrapper "https://github.com/dnyaneshs1/ucode/blob/main/core/gemini_wrapper_v2.py"
click node_registry "https://github.com/dnyaneshs1/ucode/blob/main/core/provider_registry.py"
click node_artifacts "https://github.com/dnyaneshs1/ucode/blob/main/core/artifact_manager.py"
click node_runtime_log "https://github.com/dnyaneshs1/ucode/blob/main/core/runtime_logger.py"
click node_evaluation "https://github.com/dnyaneshs1/ucode/blob/main/core/evaluation_module.py"
click node_environment "https://github.com/dnyaneshs1/ucode/blob/main/.env.example"

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_cli,node_ui toneBlue
class node_session,node_context,node_classifier,node_cache,node_wrapper,node_registry,node_artifacts,node_runtime_log,node_evaluation toneAmber
class node_token_limits,node_classifier_model,node_embedder,node_local_state,node_environment toneMint
class node_provider_apis toneRose
```

## Components

### User Entry Points

- [CLI](https://github.com/dnyaneshs1/ucode/blob/main/app/main.py): Python entry point · `app/main.py`
- [Streamlit UI](https://github.com/dnyaneshs1/ucode/blob/main/app/streamlit_app.py): Python entry point · Maintains transient Streamlit session state. · `app/streamlit_app.py`

### Core Request Pipeline

- [Session manager](https://github.com/dnyaneshs1/ucode/blob/main/core/session_manager.py): conversation state · Persists history and applies the sliding context window. · `core/session_manager.py`
- [Context manager](https://github.com/dnyaneshs1/ucode/blob/main/core/context_manager.py): continuation context · Preserves context across partial responses and failover. · `core/context_manager.py`
- [Complexity classifier](https://github.com/dnyaneshs1/ucode/blob/main/core/complexity_classifier_module.py): local SetFit classifier · Classifies requests before generation. · `core/complexity_classifier_module.py`
- [Semantic cache](https://github.com/dnyaneshs1/ucode/blob/main/core/semantic_cache.py): cache boundary · Checks exact SHA256 prompts, then semantic similarity. · `core/semantic_cache.py`
- [LLM wrapper](https://github.com/dnyaneshs1/ucode/blob/main/core/gemini_wrapper_v2.py): generation orchestrator · Invokes providers, enforces limits, and continues through failover. · `core/gemini_wrapper_v2.py`
- [Provider registry](https://github.com/dnyaneshs1/ucode/blob/main/core/provider_registry.py): provider routing config · Defines provider configuration and fallback order. · `core/provider_registry.py`
- [Artifact manager](https://github.com/dnyaneshs1/ucode/blob/main/core/artifact_manager.py): local artifact output · Extracts generated code and content as local artifacts. · `core/artifact_manager.py`
- [Runtime logger](https://github.com/dnyaneshs1/ucode/blob/main/core/runtime_logger.py): usage telemetry · Records usage, cache, complexity, latency, and provider outcomes. · `core/runtime_logger.py`
- [Evaluation module](https://github.com/dnyaneshs1/ucode/blob/main/core/evaluation_module.py): response evaluation · `core/evaluation_module.py`

### Local Models & State

- [Complexity token limits](https://github.com/dnyaneshs1/ucode/blob/main/classifier/complexity_token_limits.json): limit data · `classifier/complexity_token_limits.json`
- [Classifier model](https://github.com/dnyaneshs1/ucode/blob/main/classifier/complexity_classifier/model.safetensors): bundled model artifact · `classifier/complexity_classifier/model.safetensors`
- [Sentence embedder](https://github.com/dnyaneshs1/ucode/blob/main/embedder/model.safetensors): bundled embedding model · `embedder/model.safetensors`
- **Local filesystem state**: durable local storage · Stores sessions, caches, artifacts, and CSV usage reports.
- [Environment settings](https://github.com/dnyaneshs1/ucode/blob/main/.env.example): credentials and limits · Supplies provider API keys and operational limits. · `.env.example`

### Inference Providers

- **Remote LLM APIs**: external inference · Gemini primary; Groq, HuggingFace, GitHub Models, and OpenRouter fallback.

## Connections

- CLI → Session manager: submits turn
- Streamlit UI → Session manager: submits turn
- Session manager → Complexity classifier: prompt with history
- Session manager → Context manager: provides context
- Session manager → Local filesystem state: loads and saves history
- Complexity classifier → Complexity token limits: selects cap
- Complexity classifier → Classifier model: runs locally
- Complexity classifier → Semantic cache: classified request
- Semantic cache → Sentence embedder: semantic lookup
- Semantic cache → LLM wrapper: cache miss
- Semantic cache → Local filesystem state: reads and stores entries
- LLM wrapper → Provider registry: selects provider chain
- LLM wrapper → Remote LLM APIs: primary and fallback calls
- Remote LLM APIs → LLM wrapper: response or quota/error
- LLM wrapper → Context manager: partial-response transfer
- Environment settings → LLM wrapper: API keys and limits
- LLM wrapper → Session manager: final response
- LLM wrapper → Semantic cache: stores live response
- LLM wrapper → Artifact manager: generated content
- LLM wrapper → Runtime logger: execution outcome
- LLM wrapper → Evaluation module: response behavior
- Artifact manager → Local filesystem state: writes artifacts
- Runtime logger → Local filesystem state: writes CSV usage
