# mdrideout/junjo architecture

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

- Interactive diagram: https://gitdiagram.com/mdrideout/junjo
- Repository: https://github.com/mdrideout/junjo

## Overview

##### Project type and purpose
- This is primarily a Python library/framework (“Junjo”) for constructing, executing, and instrumenting AI workflows as directed graphs.
- It is not itself a deployed application but provides core modules (graph, node, workflow, telemetry, visualization) plus example applications (AI chat full-stack and a base sample workflow).

##### File structure
- **src/junjo/** contains the core engine:
- app.py, workflow.py – orchestration entrypoints
- graph.py, node.py, edge.py, condition.py, state.py, store.py – graph construction and execution primitives
- react_flow/ – schema definitions for React Flow integration
- graphviz/ – utilities to render Graph objects via Graphviz
- telemetry/ – hook manager, schemas, junjo-server client, proto definitions, OTEL exporter
- util.py, run_concurrent.py – helper functions
- **examples/** holds two references:
- ai_chat/ – full-stack example with a FastAPI backend structured by junjo workflows and a React/Vite frontend
- base/ – minimal Python CLI example showing junjo workflows and subflows
- **docs/** holds Sphinx architecture and usage docs.

##### README for additional insights
- Junjo’s design goals: TDD, telemetry (OpenTelemetry), concurrent execution (asyncio), type safety (Pydantic).
- Optional companion junjo-server for telemetry visualization.
- Graphviz rendering support (broken currently).

##### Main components
a. Core Engine Modules (src/junjo)
- Graph Builder (graph.py)
- Node & Edge abstractions (node.py, edge.py, condition.py)
- Workflow Executor (workflow.py, run_concurrent.py)
- State & Store management (state.py, store.py)
- Utilities (util.py)
b. Telemetry Subsystem
- Hook Manager (telemetry/hook_manager.py)
- OTEL-compatible schemas & exporter (telemetry/otel_schema.py, junjo_server_otel_exporter.py)
- junjo-server gRPC client & proto (telemetry/junjo_server/client.py + proto/*.proto)
c. Visualization
- Graphviz Renderer (graphviz/utils.py)
- React Flow Schema (react_flow/schemas.py) for UI integration
d. Example Integrations
- ai_chat Backend: FastAPI app, database models, routes, LLM services, workflows
- ai_chat Frontend: React + Vite, API hooks, UI components
- base CLI: simple sample_workflow graph executed from main.py

##### Relationships and interactions
- **Workflow Definition** → uses Graph Builder to assemble nodes/edges.
- **Workflow Executor** → takes a Graph, schedules nodes (asyncio), updates State & Store.
- **Telemetry Hook Manager** → intercepts node/workflow lifecycle events, emits OTEL spans.
- **OTEL Exporter** → forwards telemetry to any OTEL backend or junjo-server via gRPC.
- **Graphviz Renderer** → consumes Graph → outputs image for offline visualization.
- **React Flow Schema** → serializes Graph → consumed by React Flow UI in frontend.
- **Examples** → import core modules to define workflows → integrate in a web service or CLI.

##### Architectural patterns & design principles
- Directed Graph (DAG) for workflow orchestration
- Dependency Injection via Hook Manager for telemetry
- Async/await concurrency model for parallel node execution
- Separation of Concerns: core engine vs. telemetry vs. visualization vs. integration
- Extensibility: users supply their own LLM functions, DB calls, business logic in nodes

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Core Engine Layer
    subgraph "Core Engine" 
        direction TB
        GB["Graph Builder"]:::core
        NA["Node Abstraction"]:::core
        EA["Edge Abstraction"]:::core
        CA["Condition Abstraction"]:::core
        WE["Workflow Executor"]:::core
        CR["Concurrent Runner"]:::core
        SM["State Management"]:::core
        STM["Store Management"]:::core
        UF["Utility Functions"]:::core
    end

    %% Telemetry Subsystem
    subgraph "Telemetry Subsystem (Optional)" 
        direction TB
        HM["Telemetry Hook Manager"]:::tele
        HS["Telemetry Hook Schema"]:::tele
        OS["OTEL Schema Definitions"]:::tele
        EX["junjo-server OTEL Exporter"]:::tele
        GC["gRPC Client"]:::tele
        PD["Proto Definitions"]:::tele
    end

    %% Visualization Subsystem
    subgraph "Visualization (Optional)" 
        direction TB
        GV["Graphviz Renderer"]:::vis
        RF["React Flow Schema"]:::vis
    end

    %% Examples
    subgraph "Examples" 
        direction TB
        AIB["AI Chat Example Backend"]:::ex
        AIF["AI Chat Example Frontend"]:::ex
        CLI["Base CLI Example"]:::ex
    end

    %% External Services & Stores
    subgraph "External & Services"
        direction TB
        DB[(Database)]:::store
        LLM[(LLM Service)]:::external
        OTEL[(OTEL Collector)]:::external
        JSERV[(junjo-server)]:::external
    end

    %% Core Relationships
    WE -->|"schedules via"| CR
    CR -->|"updates"| SM
    CR -->|"persists"| STM
    WE -->|"executes graph built by"| GB
    GB -->|"uses abstractions"| NA
    GB -->|"uses abstractions"| EA
    GB -->|"uses abstractions"| CA
    WE -->|"utilizes helpers"| UF

    %% Telemetry Relationships
    WE -->|intercepts| HM
    HM -->|"defines hooks via"| HS
    HM -->|"exports spans via"| OS
    HM -->|"calls exporter"| EX
    EX -->|gRPC| GC
    GC -->|proto| PD
    EX ---|optional export| OTEL
    EX ---|optional export| JSERV

    %% Visualization Relationships
    GB ---|generates graph| GV
    GB ---|serializes graph| RF

    %% Examples Relationships
    AIB -->|"imports core"| GB
    AIB -->|"imports core"| WE
    AIB -->|"connects to"| DB
    AIB -->|"calls"| LLM
    AIF -->|"calls API from"| AIB
    AIF ---|renders graph via| RF
    CLI -->|"imports core"| GB
    CLI -->|"runs via"| WE

    %% Legend
    subgraph "Legend"
        direction TB
        C1["Core = blue"]:::core
        C2["Telemetry = green"]:::tele
        C3["Visualization = orange"]:::vis
        C4["Examples = grey"]:::ex
        C5["Store = purple"]:::store
        C6["External = cloud"]:::external
    end

    %% Click Events
    click GB "https://github.com/mdrideout/junjo/blob/main/src/junjo/graph.py"
    click NA "https://github.com/mdrideout/junjo/blob/main/src/junjo/node.py"
    click EA "https://github.com/mdrideout/junjo/blob/main/src/junjo/edge.py"
    click CA "https://github.com/mdrideout/junjo/blob/main/src/junjo/condition.py"
    click WE "https://github.com/mdrideout/junjo/blob/main/src/junjo/workflow.py"
    click CR "https://github.com/mdrideout/junjo/blob/main/src/junjo/run_concurrent.py"
    click SM "https://github.com/mdrideout/junjo/blob/main/src/junjo/state.py"
    click STM "https://github.com/mdrideout/junjo/blob/main/src/junjo/store.py"
    click UF "https://github.com/mdrideout/junjo/blob/main/src/junjo/util.py"
    click HM "https://github.com/mdrideout/junjo/blob/main/src/junjo/telemetry/hook_manager.py"
    click HS "https://github.com/mdrideout/junjo/blob/main/src/junjo/telemetry/hook_schema.py"
    click OS "https://github.com/mdrideout/junjo/blob/main/src/junjo/telemetry/otel_schema.py"
    click EX "https://github.com/mdrideout/junjo/blob/main/src/junjo/telemetry/junjo_server_otel_exporter.py"
    click GC "https://github.com/mdrideout/junjo/blob/main/src/junjo/telemetry/junjo_server/client.py"
    click PD "https://github.com/mdrideout/junjo/blob/main/src/junjo/telemetry/junjo_server/proto/workflow_log.proto"
    click GV "https://github.com/mdrideout/junjo/blob/main/src/junjo/graphviz/utils.py"
    click RF "https://github.com/mdrideout/junjo/blob/main/src/junjo/react_flow/schemas.py"
    click AIB "https://github.com/mdrideout/junjo/tree/main/examples/ai_chat/backend/src/app"
    click AIF "https://github.com/mdrideout/junjo/tree/main/examples/ai_chat/frontend/src"
    click CLI "https://github.com/mdrideout/junjo/tree/main/examples/base/src/base"

    %% Styles
    classDef core fill:#D6EAF8,stroke:#21618C,stroke-width:1px
    classDef tele fill:#D5F5E3,stroke:#196F3D,stroke-width:1px
    classDef vis fill:#FCF3CF,stroke:#B9770E,stroke-width:1px
    classDef ex fill:#E5E8E8,stroke:#566573,stroke-width:1px
    classDef store fill:#E8DAEF,stroke:#6C3483,stroke-width:1px
    classDef external fill:#FDEDEC,stroke:#922B21,stroke-width:1px,stroke-dasharray: 5 5
```
