# hxtanishq/rag-based-function-execution-api architecture

> Architecture diagram of the GitHub repository hxtanishq/rag-based-function-execution-api, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-22). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/hxtanishq/rag-based-function-execution-api
- Repository: https://github.com/hxtanishq/rag-based-function-execution-api

## Overview

##### Project Type and Purpose
- Full-stack “automation orchestration” service with three interfaces (CLI, Web, REST API)
- Translates natural-language prompts into system calls via semantic search & function registry

##### Core Architectural Components
A. Presentation Layer (Interfaces)
- CLI Interface (cli.py)
- Streamlit Web Interface (web.py)
- REST API Server (FastAPI: api/routes.py, api/main.py)
B. Application Layer (Orchestration)
- Prompt Controller / API Handlers
- Session Manager (utils/session_manager.py) – maintains conversational context
- Error Handler (utils/error_handler.py) – central error logging & recovery
C. Domain Layer (Business Logic)
- Semantic Function Discovery
– Embedding/search module (could live in registry or automation_functions)
- Function Registry (registry/function_registry.py) – metadata & lookup of available functions
- Automation Functions (automation_functions/system_functions.py) – concrete system tasks
D. Infrastructure Layer
- OS/External Shell – actual execution environment for system commands
- Logging (function_execution.log) – persistent log of all executions & errors
- Requirements & Deployment (requirements.txt, uvicorn, streamlit)

##### Relationships & Data Flows
1. User enters prompt in CLI or Web UI, or issues HTTP POST to the API.
2. Interface forwards the prompt + optional params to the Prompt Controller.
3. Controller invokes Session Manager to retrieve or create context.
4. Controller calls into the Semantic Search / Function Registry to pick the best‐matching function.
5. Controller dispatches to the chosen Automation Function.
6. Automation Function executes on the OS or shell, returns result.
7. Error Handler intercepts any exceptions, logs them.
8. Controller logs the call in function_execution.log and returns the output (or error) back to the UI layer.

##### Highlighted Architectural Patterns & Design Principles
- Layered Architecture: clear separation of presentation, application, domain, infrastructure
- Plugin/Registry Pattern: functions are registered centrally and discovered at runtime
- Single Responsibility: each module (session_manager, error_handler, registry, system_functions) has a well-defined role
- Extensibility: adding new automation functions only requires updating the registry and function folder

##### Checklist for the Principal Software Engineer
[ ] All major components (UI, Controller, Registry, Functions, OS, Logs) drawn as distinct nodes
[ ] Zoomed-in inset (optional) for the orchestration core, showing session_manager + error_handler

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Presentation Layer
    subgraph "Presentation Layer"
        CLI["CLI Interface<br/>cli.py"]:::interfaces
        WEB["Streamlit Web Interface<br/>web.py"]:::interfaces
        API["REST API Server<br/>api/main.py"]:::interfaces
    end

    %% Application Layer
    subgraph "Application Layer"
        PC["Prompt Controller<br/>api/routes.py"]:::orchestrator
        SM["Session Manager<br/>utils/session_manager.py"]:::orchestrator
        EH["Error Handler<br/>utils/error_handler.py"]:::orchestrator
    end

    %% Domain Layer
    subgraph "Domain Layer"
        DISC["Semantic Search/Function Discovery"]:::domain
        FR["Function Registry<br/>registry/function_registry.py"]:::domain
        SF["Automation Functions<br/>automation_functions/system_functions.py"]:::domain
    end

    %% Infrastructure Layer
    subgraph "Infrastructure Layer"
        OS["OS/External Shell"]:::infra
        LOG["Execution Log<br/>function_execution.log"]:::infra
        REQ["Requirements & Deployment<br/>requirements.txt"]:::infra
    end

    %% Flows
    CLI -->|"prompt + params"| PC
    WEB -->|"prompt + params"| PC
    API -->|"POST prompt + params"| PC

    PC -->|"get/create context"| SM
    PC -->|"discover function"| DISC
    DISC -->|"lookup metadata"| FR
    PC -->|"dispatch"| SF
    SF -->|"exec command"| OS
    OS -->|"result"| SF
    SF -->|"result"| PC

    %% Error and Logging
    PC -->|"log call"| LOG
    SF -->|"<error>"| EH
    PC -->|"<error>"| EH
    EH -->|"write error"| LOG

    PC -->|"output or error"| CLI
    PC -->|"output or error"| WEB
    PC -->|"output or error"| API

    %% Click Events
    click CLI "https://github.com/hxtanishq/rag-based-function-execution-api/blob/main/cli.py"
    click WEB "https://github.com/hxtanishq/rag-based-function-execution-api/blob/main/web.py"
    click API "https://github.com/hxtanishq/rag-based-function-execution-api/blob/main/api/main.py"
    click PC "https://github.com/hxtanishq/rag-based-function-execution-api/blob/main/api/routes.py"
    click SM "https://github.com/hxtanishq/rag-based-function-execution-api/blob/main/utils/session_manager.py"
    click EH "https://github.com/hxtanishq/rag-based-function-execution-api/blob/main/utils/error_handler.py"
    click FR "https://github.com/hxtanishq/rag-based-function-execution-api/blob/main/registry/function_registry.py"
    click SF "https://github.com/hxtanishq/rag-based-function-execution-api/blob/main/automation_functions/system_functions.py"
    click LOG "https://github.com/hxtanishq/rag-based-function-execution-api/blob/main/function_execution.log"
    click REQ "https://github.com/hxtanishq/rag-based-function-execution-api/blob/main/requirements.txt"

    %% Styles
    classDef interfaces fill:#ADD8E6,stroke:#333,stroke-width:1px
    classDef orchestrator fill:#90EE90,stroke:#333,stroke-width:1px
    classDef domain fill:#FFA500,stroke:#333,stroke-width:1px
    classDef infra fill:#D3D3D3,stroke:#333,stroke-width:1px
```
