# anthay/eliza architecture

> Architecture diagram of the GitHub repository anthay/eliza, 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/anthay/eliza
- Repository: https://github.com/anthay/eliza

## Overview

##### Project type and purpose
- This is an open-source recreation in C++ of Joseph Weizenbaum’s 1966 ELIZA chatbot.
- It’s delivered primarily as a console application (with an optional serial‐I/O mode) that reads “script” files (S-expression pattern rules) and responds to user input.
- There is also an alternate JavaScript/HTML version for a web demo.

##### File structure
- src/
– eliza.cpp : the complete ELIZA engine (parser, pattern matcher, response generator)
– serial_io.h + posix_serial_io.cpp / win_serial_io.cpp : platform-specific I/O abstraction
– ymatch.cpp : helper for pattern matching
– eliza.html : standalone JS/HTML port
- scripts/ : various ELIZA script files (DOCTOR, Turing-machine variants, etc.)
- doc/ : design notes, transcriptions, serial-I/O documentation, historical commentary
- scripts.md : index of available sample scripts
- Top-level build instructions in README.md

##### README for additional insights
- No external services or databases—everything is local and file-based.
- Build targets:
– POSIX: clang++ -std=c++20 -pedantic
– Windows: cl /EHsc /W4 /std:c++20
- Extensibility point: swap in any “.txt” script with S-expressions to change bot behavior.

##### Main components and their interactions
a. User Interface Layer
– Console I/O or serial-port interface (via serial_io abstraction)
– Web UI (eliza.html + JS engine)
b. Core ELIZA Engine
– Script Loader / S-expression Parser
– Pattern Matcher (ymatch.cpp)
– Response Generator / Substitution Logic
c. Script Repository
– DOCTOR and other “.txt” files under scripts/
d. Build Toolchain
– C++20 compiler (clang++ or cl.exe)
e. Optional External Dependency
– Serial device (ASR-33 teletype) for the serial I/O mode

##### Architectural patterns and design principles
- Layered architecture: UI → Engine → Data (script files)
- Abstraction of I/O so core logic is platform-agnostic
- Plugin-style extensibility: new scripts simply drop into scripts/
- Single-responsibility modules (parser vs. matcher vs. serial driver)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% UI Layer
    subgraph "UI Layer"
        style UI_Layer fill:none,stroke:none
        ConsoleIO["Console I/O"]:::ui
        SerialIO["Serial I/O Abstraction"]:::ui
        PosixDriver["POSIX Serial I/O Driver\n(posix_serial_io.cpp)"]:::ui
        WinDriver["Windows Serial I/O Driver\n(win_serial_io.cpp)"]:::ui
        WebUI["Web UI (eliza.html + JS)"]:::ui
    end

    %% Core Engine Layer
    subgraph "Core Engine" 
        style Core_Engine fill:none,stroke:none
        ELIZAEngine["ELIZA Engine\n(parser & response gen)"]:::core
        PatternMatcher["Pattern Matcher\n(ymatch.cpp)"]:::core
        ScriptLoader["Script Loader\n(S-expression parser)"]:::core
    end

    %% Script Store Layer
    subgraph "Script Store"
        style Script_Store fill:none,stroke:none
        ScriptFiles["ELIZA Script Files\n(cylinder)"]:::data
        ScriptsIndex["Scripts Index\n(cylinder)"]:::data
    end

    %% Build System Layer
    subgraph "Build System"
        style Build_System fill:none,stroke:none
        BuildSystem["Build Instructions\n(README.md)"]:::build
        License["Project License\n(LICENSE)"]:::build
    end

    %% Documentation Layer
    subgraph "Documentation"
        style Documentation fill:none,stroke:none
        SerialIODocs["Serial I/O Design Notes\n(doc/serial_io.md)"]:::data
        GenDocs["General Design Notes\n(doc/)"]:::data
    end

    %% Connections
    ConsoleIO -->|"user input"| ELIZAEngine
    SerialIO -->|"user input"| ELIZAEngine
    PosixDriver -->|"serial data"| SerialIO
    WinDriver -->|"serial data"| SerialIO
    WebUI -->|"user input"| ELIZAEngine

    ELIZAEngine -->|"calls matcher"| PatternMatcher
    ELIZAEngine -->|"calls loader"| ScriptLoader
    PatternMatcher -->|"loads patterns"| ScriptFiles
    ScriptLoader -->|"reads scripts"| ScriptsIndex
    ELIZAEngine -->|"generate response"| ConsoleIO
    ELIZAEngine -->|"generate response"| WebUI

    BuildSystem -->|"compile"| ELIZAEngine
    BuildSystem -->|"compile"| ConsoleIO
    BuildSystem -->|"compile"| SerialIO
    BuildSystem -->|"compile"| WebUI

    SerialIODocs --> SerialIO
    GenDocs --> ELIZAEngine

    %% Click Events
    click SerialIO "https://github.com/anthay/eliza/blob/master/src/serial_io.h"
    click PosixDriver "https://github.com/anthay/eliza/blob/master/src/posix_serial_io.cpp"
    click WinDriver "https://github.com/anthay/eliza/blob/master/src/win_serial_io.cpp"
    click WebUI "https://github.com/anthay/eliza/blob/master/src/eliza.html"
    click ELIZAEngine "https://github.com/anthay/eliza/blob/master/src/eliza.cpp"
    click PatternMatcher "https://github.com/anthay/eliza/blob/master/src/ymatch.cpp"
    click ScriptFiles "https://github.com/anthay/eliza/tree/master/scripts/"
    click ScriptsIndex "https://github.com/anthay/eliza/blob/master/scripts/scripts.md"
    click SerialIODocs "https://github.com/anthay/eliza/blob/master/doc/serial_io.md"
    click GenDocs "https://github.com/anthay/eliza/tree/master/doc/"
    click BuildSystem "https://github.com/anthay/eliza/blob/master/README.md"
    click License "https://github.com/anthay/eliza/tree/master/LICENSE"

    %% Styles
    classDef ui fill:#ccffcc,stroke:#006600,color:#003300;
    classDef core fill:#cce0ff,stroke:#004080,color:#002040;
    classDef data fill:#ffe0b3,stroke:#cc6600,color:#663300;
    classDef build fill:#e0e0e0,stroke:#666666,color:#333333;
```
