# graniet/kheish architecture

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

- Interactive diagram: https://gitdiagram.com/graniet/kheish
- Repository: https://github.com/graniet/kheish

## Overview

##### Main Components

a) Core Agent System
- Four main roles (Proposer, Reviewer, Validator, Formatter) as sub-components

b) Module System
- Following modules in a separate section:
- FileSystem (fs_module)
- Shell (sh_module)
- HTTP (http_module)
- RAG (rag_module)
- SSH (ssh_module)
- Memory (mem_module)
- Vector Store

c) LLM Integration Layer
- Different LLM providers:
- OpenAI
- Anthropic
- Deepseek
- Ollama
- Embeddings service (OpenAI embedder)

d) Database Layer
- Tables identified in migrations:
- Tasks
- Task Outputs
- Task Events

e) API Layer
- REST API endpoints
- Error handling
- Route management

##### Component Relationships

- Task input → Core Agent
- Core Agent ↔ LLM Providers
- Core Agent ↔ Modules
- Core Agent ↔ Database
- API ↔ Core Agent
- RAG Module ↔ Vector Store
- Core Agent ↔ Role Switching

##### Specific Elements

a) Task Processing Flow:
1. YAML Configuration Input
2. Agent Initialization
3. Role-based Processing
4. Module Integration
5. Result Generation

b) Data Stores:
- Vector Store for RAG
- PostgreSQL Database
- File System Storage

##### Important Architectural Patterns to Highlight

- Repository Pattern (shown in db folder structure)
- Module Pattern (for extensibility)
- Event-driven Architecture (task_events)
- Role-based Processing Pipeline

##### Additional Details

- Version/environment information
- Configuration flow
- Error handling paths

##### Technical Stack Indicators

- Rust (Primary Language)
- PostgreSQL (Database)
- Vector Store (RAG)
- REST API
- Multiple LLM Provider Support

This system design should emphasize Kheish's unique capability as an intelligent agent that can switch between roles and request modules on demand, rather than just being a simple orchestrator.

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph API["API Layer"]
        Routes["Routes"]:::api
        Handlers["Handlers"]:::api
        ErrorHandling["Error Handling"]:::api
        Server["Server"]:::api
    end

    subgraph CoreAgent["Core Agent System"]
        AgentManager["Agent Manager"]:::agent
        subgraph Roles["Agent Roles"]
            Proposer["Proposer"]:::role
            Reviewer["Reviewer"]:::role
            Validator["Validator"]:::role
            Formatter["Formatter"]:::role
        end
    end

    subgraph Modules["Module System"]
        ModuleManager["Module Manager"]:::module
        FSModule["FileSystem Module"]:::module
        ShellModule["Shell Module"]:::module
        HTTPModule["HTTP Module"]:::module
        RAGModule["RAG Module"]:::module
        SSHModule["SSH Module"]:::module
        MemModule["Memory Module"]:::module
        VectorStore["Vector Store"]:::module
    end

    subgraph LLM["LLM Integration Layer"]
        LLMClient["LLM Client"]:::llm
        OpenAI["OpenAI Provider"]:::llm
        Anthropic["Anthropic Provider"]:::llm
        Deepseek["Deepseek Provider"]:::llm
        Ollama["Ollama Provider"]:::llm
        Embedder["OpenAI Embedder"]:::llm
    end

    subgraph Worker["Worker System"]
        Execution["Execution"]:::worker
        ModuleRequests["Module Requests"]:::worker
        TaskCompletion["Task Completion"]:::worker
        Outcomes["Outcomes"]:::worker
    end

    subgraph DB["Database Layer"]
        Models["Database Models"]:::db
        TaskRepo["Task Repository"]:::db
        EventRepo["Task Event Repository"]:::db
        Schema["Schema"]:::db
        Migrations["Migrations"]:::db
    end

    subgraph Core["Core Task Processing"]
        TaskManagement["Task Management"]:::core
        TaskContext["Task Context"]:::core
        TaskState["Task State"]:::core
        TaskGeneration["Task Generation"]:::core
        Workflow["Workflow"]:::core
    end

    API --> CoreAgent
    CoreAgent --> LLM
    CoreAgent --> Modules
    CoreAgent --> Worker
    Worker --> Modules
    Worker --> DB
    RAGModule --> VectorStore
    Core --> Worker
    Core --> DB
    
    %% Click events for component mapping
    click Routes "https://github.com/graniet/kheish/blob/main/src/api/routes.rs"
    click Handlers "https://github.com/graniet/kheish/blob/main/src/api/handlers.rs"
    click ErrorHandling "https://github.com/graniet/kheish/blob/main/src/api/errors.rs"
    click Server "https://github.com/graniet/kheish/blob/main/src/api/server.rs"
    click AgentManager "https://github.com/graniet/kheish/blob/main/src/agents/agent_manager.rs"
    click Proposer "https://github.com/graniet/kheish/blob/main/src/agents/proposer.rs"
    click Reviewer "https://github.com/graniet/kheish/blob/main/src/agents/reviewer.rs"
    click Validator "https://github.com/graniet/kheish/blob/main/src/agents/validator.rs"
    click Formatter "https://github.com/graniet/kheish/blob/main/src/agents/formatter.rs"
    click ModuleManager "https://github.com/graniet/kheish/blob/main/src/modules/module_manager.rs"
    click FSModule "https://github.com/graniet/kheish/blob/main/src/modules/fs_module.rs"
    click ShellModule "https://github.com/graniet/kheish/blob/main/src/modules/sh_module.rs"
    click HTTPModule "https://github.com/graniet/kheish/blob/main/src/modules/http_module.rs"
    click RAGModule "https://github.com/graniet/kheish/blob/main/src/modules/rag_module.rs"
    click SSHModule "https://github.com/graniet/kheish/blob/main/src/modules/ssh_module.rs"
    click MemModule "https://github.com/graniet/kheish/blob/main/src/modules/mem_module.rs"
    click VectorStore "https://github.com/graniet/kheish/blob/main/src/modules/vector_store.rs"
    click LLMClient "https://github.com/graniet/kheish/blob/main/src/llm/llm_client.rs"
    click OpenAI "https://github.com/graniet/kheish/blob/main/src/llm/providers/openai.rs"
    click Anthropic "https://github.com/graniet/kheish/blob/main/src/llm/providers/anthropic.rs"
    click Deepseek "https://github.com/graniet/kheish/blob/main/src/llm/providers/deepseek.rs"
    click Ollama "https://github.com/graniet/kheish/blob/main/src/llm/providers/ollama.rs"
    click Embedder "https://github.com/graniet/kheish/blob/main/src/llm/embedders/openai_embedder.rs"
    click Models "https://github.com/graniet/kheish/blob/main/src/db/models.rs"
    click TaskRepo "https://github.com/graniet/kheish/blob/main/src/db/task_repository.rs"
    click EventRepo "https://github.com/graniet/kheish/blob/main/src/db/task_event_repository.rs"
    click Schema "https://github.com/graniet/kheish/blob/main/src/schema.rs"
    click Migrations "https://github.com/graniet/kheish/tree/main/migrations/"
    click TaskManagement "https://github.com/graniet/kheish/blob/main/src/core/task.rs"
    click TaskContext "https://github.com/graniet/kheish/blob/main/src/core/task_context.rs"
    click TaskState "https://github.com/graniet/kheish/blob/main/src/core/task_state.rs"
    click TaskGeneration "https://github.com/graniet/kheish/blob/main/src/core/task_generation.rs"
    click Workflow "https://github.com/graniet/kheish/blob/main/src/core/workflow.rs"
    click Execution "https://github.com/graniet/kheish/blob/main/src/core/worker/execution.rs"
    click ModuleRequests "https://github.com/graniet/kheish/blob/main/src/core/worker/module_request.rs"
    click TaskCompletion "https://github.com/graniet/kheish/blob/main/src/core/worker/task_completion.rs"
    click Outcomes "https://github.com/graniet/kheish/blob/main/src/core/worker/standard_outcome.rs"

    %% Styling
    classDef api fill:#FFE873
    classDef agent fill:#4A90E2
    classDef role fill:#5CB3FF
    classDef module fill:#90EE90
    classDef llm fill:#FFA500
    classDef db fill:#DDA0DD
    classDef worker fill:#20B2AA
    classDef core fill:#CD853F

    %% Legend
    subgraph Legend
        API_L["API Layer"]:::api
        Agent_L["Agent System"]:::agent
        Module_L["Modules"]:::module
        LLM_L["LLM Integration"]:::llm
        DB_L["Database"]:::db
        Worker_L["Worker System"]:::worker
        Core_L["Core Processing"]:::core
    end
```
