# kingfs/godify architecture

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

- Interactive diagram: https://gitdiagram.com/kingfs/godify
- Repository: https://github.com/kingfs/godify

## Overview

##### Project type and purpose
- This is a Go-based SDK (client library) for the Dify AI platform.
- It provides a unified, type-safe interface to consume multiple Dify API surfaces (Web, Service, Console, Files, MCP).
- It’s not a standalone backend or frontend app but an integration library that your own Go code will import to interact with Dify’s services.

##### High-level file-structure analysis
- Top-level modules correspond 1:1 with Dify API groups:
- console/        – Console API client
- service/        – Service API client
- web/            – Web API client
- files/          – Files API client
- mcp/            – Model Context Protocol client
- dataset/        – Dataset-related helper
- Common infrastructure packages:
- config/         – global config (base URL, tokens, retries)
- client/         – low-level HTTP request builder, SSE support
- errors/         – unified error types and parsing
- metrics/        – instrumentation hooks
- models/         – request/response type definitions
- Examples & tests live under examples/ and test/ to exercise each API surface.
- Build and CI via Makefile and .github/workflows.

##### Readme insights
- Emphasizes four API groups + two “professional” endpoints (Files & MCP).
- Supports streaming (SSE) and retry logic.
- Uses bearer-token, API key, session cookies.
- 100% type-safe, extensive docs, and examples.

- Your client code (the “consumer”)
- Dify Go-SDK library, broken down into:
– Configuration manager (config/)
– HTTP Core (client/, SSE)
– Error handler (errors/)
– Metrics emitter (metrics/)
– Sub-clients (console/, service/, web/, files/, mcp/, dataset/)
– Model definitions (models/)
- External Dify AI backend, showing logical API servers:
– Console API Server
– Service API Server
– Web API Server
– Files API Server
– MCP API Server
b. Relationships & interactions
- At runtime, user code calls dify.NewXClient() → SDK configures HTTP client.
- Sub-clients invoke HTTP Core with proper base-URL & auth header.
- HTTP Core sends REST or SSE requests to the respective API server.
- Responses funnel back through metrics and error handlers, then into models for user consumption.
c. Architectural patterns & design principles
- Layered architecture:
– Presentation (your app) → SDK façade → Core HTTP layer → External API
- Separation of concerns: sub-client per API surface, shared transport/config.
- Retry & error-wrap middleware in the HTTP core.
- Streaming via SSE component.
d. Key technologies/frameworks
- Go modules (go.mod)
- net/http and Server-Sent-Events (SSE)
- Context for cancellation
- Makefile for build/test/meta-commands
- CI pipelines (.github/workflows)

## Diagram (Mermaid)

```mermaid
flowchart TD
    App["Your Application"]:::app
    App -->|"import & calls"| Facade["SDK Facade\ndify.go"]:::sdk
    click Facade "https://github.com/kingfs/godify/blob/master/dify.go"

    subgraph "Go-SDK (Internal)"
        direction TB
        Facade
        Config["Configuration Manager"]:::sdk
        click Config "https://github.com/kingfs/godify/blob/master/config/config.go"
        ClientCore["HTTP Core"]:::sdk
        click ClientCore "https://github.com/kingfs/godify/blob/master/client/client.go"
        SSE["SSE Support"]:::sse
        click SSE "https://github.com/kingfs/godify/blob/master/client/sse.go"
        Errors["Error Handler"]:::sdk
        click Errors "https://github.com/kingfs/godify/blob/master/errors/errors.go"
        Metrics["Metrics Emitter"]:::sdk
        click Metrics "https://github.com/kingfs/godify/blob/master/metrics/metrics.go"
        Models["Model Definitions"]:::sdk
        click Models "https://github.com/kingfs/godify/tree/master/models/"
        subgraph "Sub-Clients"
            direction TB
            ConsoleClient["Console API Client"]:::sdk
            click ConsoleClient "https://github.com/kingfs/godify/blob/master/console/client.go"
            ServiceClient["Service API Client"]:::sdk
            click ServiceClient "https://github.com/kingfs/godify/blob/master/service/client.go"
            WebClient["Web API Client"]:::sdk
            click WebClient "https://github.com/kingfs/godify/blob/master/web/client.go"
            FilesClient["Files API Client"]:::sdk
            click FilesClient "https://github.com/kingfs/godify/blob/master/files/client.go"
            MCPClient["MCP API Client"]:::sdk
            click MCPClient "https://github.com/kingfs/godify/blob/master/mcp/client.go"
            DatasetClient["Dataset Helper"]:::sdk
            click DatasetClient "https://github.com/kingfs/godify/blob/master/dataset/client.go"
        end
    end

    Facade --> Config
    Facade --> ClientCore
    Facade --> Errors
    Facade --> Metrics
    Facade --> Models
    Facade --> ConsoleClient
    Facade --> ServiceClient
    Facade --> WebClient
    Facade --> FilesClient
    Facade --> MCPClient
    Facade --> DatasetClient

    ConsoleClient -->|"REST/JSON"| ConsoleAPI["Console API Server"]:::external
    ServiceClient -->|"REST/JSON"| ServiceAPI["Service API Server"]:::external
    WebClient -->|"REST/JSON"| WebAPI["Web API Server"]:::external
    FilesClient -->|"REST/JSON"| FilesAPI["Files API Server"]:::external
    MCPClient -->|"REST/JSON"| MCPAPI["MCP API Server"]:::external

    SSE -->|"handles streaming"| SSEStream["SSE Stream"]:::sse
    SSEStream --> WebAPI
    SSEStream --> MCPAPI

    classDef app fill:#FFA500,stroke:#333,stroke-width:1px
    classDef sdk fill:#ADD8E6,stroke:#333,stroke-width:1px
    classDef external fill:#98FB98,stroke:#333,stroke-width:1px
    classDef sse fill:#E0FFFF,stroke:#333,stroke-width:1px
```
