# sundynix-agentix · 系统架构图

分层式 AI Agent 平台 — **Monolith First → Microservices (Morph B)** 演进。
共 **5 层 + 1 条 NATS 零拷贝消息总线**。

> 下方 Mermaid 图在 GitHub / VS Code(装 Mermaid 插件) / Typora / Obsidian 中可直接渲染。

```mermaid
flowchart TB

%% ===================== 1. CLIENT =====================
subgraph CLIENT["1 · CLIENT LAYER (Edge Interaction)"]
  direction TB
  subgraph DESKTOP["sundynix-desktop (User Device App)"]
    UI["UI Representation Layer<br/>React 19 + TypeScript"]
    SHAD["shadcn/ui + Tailwind CSS<br/>Styles / Components"]
    RF["React Flow Canvas<br/>Agent Orchestration"]
    DSL["JSON DSL export"]
    WIKI["LLM Wiki Management Panel"]
    WAILS["Wails Local Go Runtime"]
    BIND["Strong Binding TS/Go"]
    FS["Local File System I/O"]
    UI --> SHAD --> RF --> DSL
    RF --> WIKI --> WAILS
    WAILS --> BIND
    WAILS --> FS
  end
end

%% ===================== 2. GATEWAY =====================
subgraph GATEWAY["2 · BUSINESS GATEWAY LAYER"]
  direction TB
  subgraph GW["sundynix-gateway (Gin 微服务 · Monolith First)"]
    GIN["Gin 微服务 / 统一接入层"]
    MAINDB[("MainDB<br/>PgSQL: Users, Billing, DSL")]
    CACHE1[("CacheDB<br/>Session / Rate Limit")]
    CACHE2[("CacheDB<br/>Redis: Session, Rate Limit")]
    BILL["商业化与计费模块"]
    GUARD["Harness: Input/Output Guardrail"]
    PARSER["Task DSL Parser & Assembly"]
    GIN --> MAINDB
    GIN --> CACHE1
    GIN --> CACHE2
    GIN --> BILL
    GIN --> GUARD
    GIN --> PARSER
  end
end

%% ===================== 3. NATS =====================
subgraph NATS["3 · MESSAGE BUS — NATS 零拷贝骨干网"]
  direction LR
  QUEUE["NATS Queue<br/>分布式任务队列<br/>sundynix.tasks.*"]
  STREAM["NATS Stream<br/>零拷贝字节管道<br/>sundynix.streams.*"]
end

%% ===================== 4. DISPATCHER =====================
subgraph DISPATCHER["4 · AI AGENT DISPATCHER LAYER"]
  direction TB
  LLMPOOL[("LLM Pool<br/>vLLM / Ollama 集群")]
  subgraph DISP["sundynix-dispatcher (Go / Eino 集群)"]
    EINO["Eino 图编排引擎"]
    NATSGO["nats.go client"]
    EVAL["Harness: LLM 自动化评测"]
    FUSE["Harness: 熔断降级中心"]
    EINO <--> NATSGO
  end
  LLMPOOL <-->|"EinoCore · Eino 回调机制"| EINO
end

%% ===================== 5. MCP TOOLS =====================
subgraph TOOLS["5 · MICROSERVICE MCP TOOLS LAYER (Morph B)"]
  direction TB
  subgraph MCPGO["sundynix-mcp-go (Go 微服务 · I/O 型)"]
    GGW["MCP Protocol Gateway"]
    SEARCH["LLM Wiki 搜索引擎<br/>Hybrid: Bleve · Milvus Go SDK · Neo4j Go Driver"]
    MILVUS[("Milvus · Vector DB")]
    BLEVE["Bleve · Go Search Index"]
    NEO1[("Neo4j · Knowledge Graph")]
    NEO2[("Neo4j · Graph")]
    UNI["UniOffice · Word/Doc Rendering"]
    EXT["External APIs"]
    GGW --> SEARCH --> MILVUS & BLEVE & NEO1 & NEO2
    GGW --> UNI
    GGW --> EXT
  end
  subgraph MCPPY["sundynix-mcp-py (Python 微服务 · 算法型)"]
    PGW["MCP Protocol Gateway"]
    SANDBOX["Harness: Secure Code Sandbox<br/>gVisor / KataVM · Static Code Guard"]
    MINERU["MinerU · Multimodal Parser (PaddleOCR)"]
    DOCKER["Docker 隔离沙箱 · Code Interpreter"]
    PGW --> SANDBOX --> MINERU --> DOCKER
  end
end

%% ===================== 跨层连线 =====================
DESKTOP <==>|"4 · PHYSICAL COMMUNICATION<br/>HTTPS / WebSocket / SSE<br/>sundynix.streams.task_id (Token Stream)"| GIN
PARSER  ==>|"1 · Publish Task DSL"| QUEUE
STREAM  ==>|"4 · Subscribe Stream"| GIN
QUEUE   <==>|"4 · Subscribe Stream"| NATSGO
STREAM  <==> NATSGO
NATS    ==> MCPGO
NATS    ==> MCPPY

%% ===================== 配色 =====================
classDef client     fill:#fdf2e3,stroke:#e0a85a,color:#1c2b3a;
classDef gateway    fill:#dcecf7,stroke:#5fa9d4,color:#1c2b3a;
classDef nats       fill:#e7f4e1,stroke:#86c06a,color:#1c2b3a;
classDef dispatcher fill:#efe5f8,stroke:#a984cf,color:#1c2b3a;
classDef gotools    fill:#d6e7f3,stroke:#5fa9d4,color:#1c2b3a;
classDef pytools    fill:#fbf3cf,stroke:#d8bf52,color:#1c2b3a;

class CLIENT,DESKTOP,UI,SHAD,RF,DSL,WIKI,WAILS,BIND,FS client;
class GATEWAY,GW,GIN,MAINDB,CACHE1,CACHE2,BILL,GUARD,PARSER gateway;
class NATS,QUEUE,STREAM nats;
class DISPATCHER,DISP,LLMPOOL,EINO,NATSGO,EVAL,FUSE dispatcher;
class TOOLS,MCPGO,GGW,SEARCH,MILVUS,BLEVE,NEO1,NEO2,UNI,EXT gotools;
class MCPPY,PGW,SANDBOX,MINERU,DOCKER pytools;
```

---

## 分层说明

| 层 | 组件 | 技术栈 / 职责 | 配色 |
|---|---|---|---|
| **1. Client** | `sundynix-desktop` | React 19 + TS、shadcn/ui + Tailwind、React Flow 画布（Agent 编排 → 导出 JSON DSL）、LLM Wiki 面板、Wails 本地 Go 运行时、TS/Go 强绑定、本地文件 I/O | 🟧 橙 |
| **2. Gateway** | `sundynix-gateway` | Gin 统一接入层；MainDB(PgSQL: Users/Billing/DSL) + CacheDB(Redis: Session/Rate Limit)；商业化计费；输入/输出 Guardrail；**Task DSL 解析与组装** | 🟦 蓝 |
| **3. 消息总线** | `NATS Server (Go)` | 零拷贝骨干网：Queue(`sundynix.tasks.*` 分布式任务队列) + Stream(`sundynix.streams.*` 零拷贝字节管道) | 🟩 绿 |
| **4. Dispatcher** | `sundynix-dispatcher` | Go/Eino 集群：Eino 图编排引擎 + nats.go client，经 EinoCore/回调驱动 LLM Pool(vLLM/Ollama)；LLM 自动化评测、熔断降级中心 | 🟪 紫 |
| **5a. MCP Go** | `sundynix-mcp-go` | Go I/O 型：MCP 协议网关、Wiki 混合检索(Bleve+Milvus+Neo4j)、UniOffice 文档渲染、外部 API | 🟦 蓝 |
| **5b. MCP Py** | `sundynix-mcp-py` | Python 算法型：MCP 协议网关、安全代码沙箱(gVisor/KataVM)、MinerU 多模态解析(PaddleOCR)、Docker 代码解释器 | 🟨 黄 |

## 核心数据流（图中编号）

1. **Publish Task DSL** — Gateway 解析 DSL 后发布任务到 NATS Queue (`sundynix.tasks.*`)
2. **订阅与编排** — Dispatcher 经 `nats.go` 订阅任务，Eino 图编排引擎驱动 LLM Pool 推理
3. **调用 MCP 工具** — Dispatcher / Gateway 经 NATS 调用第 5 层 Go(I/O 型) + Python(算法型) 工具
4. **Token Stream 回流** — 推理结果以零拷贝流经 `sundynix.streams.task_id` 回 Gateway → 经 SSE/WebSocket 推送给 Client
