# awaescher/ollamasharp architecture

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

- Interactive diagram: https://gitdiagram.com/awaescher/ollamasharp
- Repository: https://github.com/awaescher/ollamasharp

## Overview

##### Project type and purpose
- This is an open-source .NET SDK / client library (“OllamaSharp”) that wraps the Ollama REST API.
- It provides:
- A core HTTP client (`OllamaApiClient`) for all Ollama endpoints (chat, embeddings, model management, etc.)
- Protocol definitions (`OllamaSharp.ModelContextProtocol`) for client/server communication and JSON schema types
- A tools engine with C# source generators to generate client‐side stubs for server tools
- A demo console app showcasing usage patterns
- Integration points with Microsoft.Extensions.AI abstractions

##### File structure
- Top-level directories:
- src/OllamaSharp.ModelContextProtocol – protocol contracts, server classes, JSON schema types
- src/OllamaSharp – the main client library (HttpClient extensions, models, constants, tools, async streaming)
- src/SourceGenerators – a C# source generator for tool support
- demo – a .NET console application demonstrating core scenarios (chat, image chat, model management)
- test & tests – unit and functional tests for both the protocol project and the client library
- Config files: Directory.Build.targets/Packages.props, .github/workflows (CI and docs), docfx.json for API docs

##### README for additional insights
- Confirms full API coverage, real-time streaming, progress reporting, tools engine via source generators
- Mentions integration as `IChatClient`/`IEmbeddingGenerator` in Microsoft.Extensions.AI

##### Be detailed but focused on core elements
- Data flow (REST calls, streaming tokens, progress events).
- Do not overcomplicate: separate protocol, business logic, extensibility, and runtime calls.

## Diagram (Mermaid)

```mermaid
graph TD
    %% Consumer Applications
    subgraph "Consumer Applications"
        Demo["Demo Console App"]:::app
        OtherApp["Other .NET App"]:::app
    end

    %% Core Library Modules
    subgraph "OllamaSharp Core Library"
        Protocol["ModelContextProtocol"]:::library
        HttpClient["HTTP Transport Layer"]:::library
        Models["Models & Converters"]:::library
        Streaming["Async Streaming Support"]:::library
        Tools["Tools Engine"]:::library
        AI["Microsoft.Extensions.AI Integration"]:::library
    end

    %% Compile-Time Components
    SourceGen["Tool Source Generator"]:::compile

    %% External Systems
    subgraph "External Systems"
        Server["Ollama Server (REST API)"]:::external
    end

    %% Testing Projects
    subgraph "Tests"
        UnitTests["Unit Tests"]:::test
        FuncTests["Functional Tests"]:::test
        ProtocolTests["Protocol Project Tests"]:::test
    end

    %% CI & Documentation
    subgraph "CI & Docs"
        CI["CI Workflow"]:::external
        Docs["Docs Workflow"]:::external
        Config["API Docs Config"]:::external
    end

    %% Connections
    Demo -->|calls| HttpClient
    OtherApp -->|calls| HttpClient
    HttpClient -->|HTTP REST| Server
    Server -->|JSON/Stream| HttpClient
    HttpClient -->|uses| Streaming
    Streaming -->|streams to| Demo

    Demo -->|invokes| Tools
    Tools -->|invokes| Server
    SourceGen -->|generates stubs for| Tools

    Demo -->|via AI interfaces| AI
    HttpClient -->|implements| Protocol

    UnitTests -->|tests| HttpClient
    UnitTests -->|tests| Streaming
    FuncTests -->|end-to-end| Server
    FuncTests -->|end-to-end| HttpClient
    ProtocolTests -->|validates| Protocol

    CI -->|runs on push| Config
    Docs -->|uses| Config

    %% Click Events
    click Protocol "https://github.com/awaescher/ollamasharp/blob/main/src/OllamaSharp.ModelContextProtocol"
    click HttpClient "https://github.com/awaescher/ollamasharp/blob/main/src/OllamaSharp/OllamaApiClient.cs"
    click HttpClient "https://github.com/awaescher/ollamasharp/blob/main/src/OllamaSharp/OllamaApiClientExtensions.cs"
    click HttpClient "https://github.com/awaescher/ollamasharp/blob/main/src/OllamaSharp/HttpRequestMessageExtensions.cs"
    click HttpClient "https://github.com/awaescher/ollamasharp/blob/main/src/OllamaSharp/IOllamaApiClient.cs"
    click Models "https://github.com/awaescher/ollamasharp/tree/main/src/OllamaSharp/Models"
    click Models "https://github.com/awaescher/ollamasharp/tree/main/src/OllamaSharp/Models/Chat/Converter"
    click Streaming "https://github.com/awaescher/ollamasharp/tree/main/src/OllamaSharp/AsyncEnumerableExtensions"
    click Tools "https://github.com/awaescher/ollamasharp/tree/main/src/OllamaSharp/Tools"
    click SourceGen "https://github.com/awaescher/ollamasharp/blob/main/src/SourceGenerators/ToolSourceGenerator.cs"
    click AI "https://github.com/awaescher/ollamasharp/tree/main/src/OllamaSharp/MicrosoftAi"
    click Demo "https://github.com/awaescher/ollamasharp/tree/main/demo"
    click UnitTests "https://github.com/awaescher/ollamasharp/blob/main/test/OllamaApiClientTests.cs"
    click UnitTests "https://github.com/awaescher/ollamasharp/blob/main/test/IAsyncEnumerableExtensionTests.cs"
    click FuncTests "https://github.com/awaescher/ollamasharp/tree/main/test/FunctionalTests"
    click ProtocolTests "https://github.com/awaescher/ollamasharp/blob/main/tests/OllamaSharp.ModelContextProtocol.Tests"
    click CI "https://github.com/awaescher/ollamasharp/blob/main/.github/workflows/ci.yml"
    click Docs "https://github.com/awaescher/ollamasharp/blob/main/.github/workflows/docs.yml"
    click Config "https://github.com/awaescher/ollamasharp/blob/main/docfx.json"

    %% Styles
    classDef app fill:#D6EAF8,stroke:#1F618D,color:#1B4F72;
    classDef library fill:#EBF5FB,stroke:#2874A6,color:#1B2631;
    classDef external fill:#FDEDEC,stroke:#C0392B,color:#78281F;
    classDef compile fill:#FEF9E7,stroke:#B7950B,color:#7D6608,stroke-dasharray: 5 5;
    classDef test fill:#E8F8F5,stroke:#17A589,color:#0E6251;
```
