# lhuanyu/ollamamessage architecture

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

- Interactive diagram: https://gitdiagram.com/lhuanyu/ollamamessage
- Repository: https://github.com/lhuanyu/ollamamessage

## Overview

##### Main Components
- User Interface (UI) Layer: Represented by the Class/View directory, which includes various views (e.g., Chat UI, DialogueList, MessageList, and settings screens) that form the iMessage-like chat interface.
- ViewModel Layer: Found in the Class/ViewModel directory. This layer handles state management (for conversations and dialogue sessions) and acts as the intermediary between the UI and the underlying data/service layers.
- API / Service Layer: Located under Class/API. It contains models (like AIProvider, Message, and Prompt) that define data structures and the Ollama service integration (OllamaModel.swift and OllamaService.swift) for managing network communications with the Ollama service.
- Utility Components: In the Class/Utility folder, various helper extensions and effects (like DateExtensions, KeyboardPublisher, PulseEffect, and Splash) support common functionality.
- Persistence/Data Modeling: With the presence of an .xcdatamodeld directory and Persistence.swift, there is a persistence layer (likely using Core Data) that stores message history and conversation data.

##### Relationships and Interactions
- The User Interface Layer interacts directly with the ViewModel layer. UI components (views) trigger events (user interactions) that are propagated to the ViewModels, which in turn fetch or update data.
- The ViewModel layer communicates with the API/Service layer to send/receive data from the Ollama service. Display logic (e.g., message bubbles, quick replies) is adjusted based on responses and state changes.
- The Persistence layer is used by the ViewModel (or dedicated data manager) to persist conversation history, ensuring that previous messages are available when needed.
- The Utility components are used across the application to ensure consistent behavior, such as date formatting or managing keyboard events.

##### Architectural Patterns and Technologies
- Separation of concerns is present by clearly dividing UI, business logic (ViewModel), and service/integration layers.
- The project has an MVVM (Model-View-ViewModel) pattern, enhancing testability and maintainability by strictly separating view logic from business logic.
- Integration with external services is encapsulated in the API layer, with models standardized for handling chat elements (Message, Prompt, AIProvider).
- The use of Core Data (inferred from the .xcdatamodeld files and Persistence.swift) manages local data persistence.
- Xcode project structure and asset catalogs indicate the use of modern iOS/macOS development practices with Swift, along with unit and UI tests for quality assurance.

##### Essential Elements
- Clear labels – each component (UI, ViewModel, API/Service, Persistence, Utility) should have a title.
- A block that represents the underlying database or Core Data integration for message history persistence.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% UI Layer
    subgraph "User Interface (UI) Layer"
        UI1("Chat UI"):::ui
        UI2("Dialogue List"):::ui
        UI3("Message List"):::ui
        UI4("Settings"):::ui
    end

    %% ViewModel Layer
    subgraph "ViewModel Layer"
        VM("ViewModel"):::viewModel
    end

    %% API / Service Layer
    subgraph "API / Service Layer"
        API("API Service"):::api
    end

    %% Persistence Layer
    subgraph "Persistence Layer"
        PS("Persistence.swift"):::persistence
        DM("Data Model (.xcdatamodeld)"):::persistence
    end

    %% Utility Components
    subgraph "Utility Components"
        UT1("Date Extensions"):::utility
        UT2("Keyboard Publisher"):::utility
        UT3("Pulse Effect"):::utility
        UT4("Splash"):::utility
    end

    %% Resources & Assets
    subgraph "Resources & Assets"
        R1("Resource Directory"):::resources
        A1("Assets Catalog (Assets.xcassets)"):::resources
    end

    %% External Service
    ES("Ollama External Service"):::external

    %% Connections between Layers
    %% UI Layer to ViewModel Layer
    UI1 -->|"triggers update"| VM
    UI2 --> VM
    UI3 --> VM
    UI4 --> VM

    %% ViewModel Layer to API / Service Layer
    VM -->|"fetch/send data"| API

    %% API calls External Ollama Service
    API -->|"request"| ES
    ES -->|"response"| API

    %% ViewModel Layer interacts with Persistence Layer
    VM -->|"store/retrieve"| PS
    VM -->|"store/retrieve"| DM

    %% UI Layer uses Resources & Assets
    UI1 -->|"uses assets"| R1
    R1 -->|"provides assets"| UI1

    %% Utility Components support both UI and ViewModel
    UT1 -.->|"support"| UI1
    UT1 -.->|"support"| VM

    %% Classes Style Definitions
    classDef ui fill:#ffcccc,stroke:#800000,stroke-width:2px;
    classDef viewModel fill:#ccffcc,stroke:#006600,stroke-width:2px;
    classDef api fill:#cce5ff,stroke:#004085,stroke-width:2px;
    classDef persistence fill:#fff2cc,stroke:#bf8f00,stroke-width:2px;
    classDef utility fill:#e2e2e2,stroke:#555555,stroke-width:2px;
    classDef resources fill:#d9ead3,stroke:#38761d,stroke-width:2px;
    classDef external fill:#fce5cd,stroke:#b45f06,stroke-width:2px;

    %% Click Events for Components
    click UI1 "https://github.com/lhuanyu/ollamamessage/tree/main/OllamaMessage/Class/View"
    click VM "https://github.com/lhuanyu/ollamamessage/tree/main/OllamaMessage/Class/ViewModel"
    click API "https://github.com/lhuanyu/ollamamessage/tree/main/OllamaMessage/Class/API"
    click UT1 "https://github.com/lhuanyu/ollamamessage/tree/main/OllamaMessage/Class/Utility"
    click PS "https://github.com/lhuanyu/ollamamessage/blob/main/OllamaMessage/Persistence.swift"
    click DM "https://github.com/lhuanyu/ollamamessage/blob/main/OllamaMessage/OllamaMessage.xcdatamodeld"
    click R1 "https://github.com/lhuanyu/ollamamessage/tree/main/OllamaMessage/Resource"
    click A1 "https://github.com/lhuanyu/ollamamessage/blob/main/OllamaMessage/Resource/Assets.xcassets"
```
