-
Notifications
You must be signed in to change notification settings - Fork 0
manifest.md
maoxiaoyue edited this page May 15, 2026
·
3 revisions
掃描 HypGo 應用程式的路由、中間件、伺服器設定,產出機器可讀的 YAML/JSON 描述,讓 AI 以最少 token 掌握專案全貌。
AI 協作開發中最大的瓶頸是「理解專案」。傳統做法是讓 AI 逐一閱讀原始碼,消耗大量 token。Manifest 讓框架自動產出結構化的專案描述:
❌ AI 讀 20 個檔案才理解 API 結構(數千 token)
✅ AI 讀 1 個 manifest 就掌握全貌(數百 token)
import ( "os" "github.com/maoxiaoyue/hypgo/pkg/manifest" "github.com/maoxiaoyue/hypgo/pkg/router" "github.com/maoxiaoyue/hypgo/pkg/config" ) // 1. 建立 Collector(從 Router + Config 收集) c := manifest.NewCollector(myRouter, myConfig) // 2. 生成 Manifest m := c.Collect() // 3. 輸出 YAML 到 stdout manifest.WriteYAML(os.Stdout, m) // 4. 或儲存到檔案 manifest.SaveToFile(".hyp/manifest.yaml", m, "yaml") manifest.SaveToFile(".hyp/manifest.json", m, "json")
srv := server.New(cfg, logger) // 註冊路由後... m := srv.Manifest() manifest.WriteYAML(os.Stdout, m)
# 輸出 YAML 到 stdout hyp context # 輸出 JSON 格式 hyp context -f json # 儲存到檔案 hyp context -o .hyp/manifest.yaml hyp context -o .hyp/manifest.json -f json
version: "1.0" framework: HypGo generated_at: 2026年03月26日T20:00:00+08:00 server: addr: ":8080" protocol: http2 tls: true routes: - method: POST path: /api/users handler_names: - controllers.CreateUser summary: "建立使用者" tags: - users input_type: CreateUserRequest output_type: UserResponse responses: 201: "User created" 400: "Invalid input" - method: GET path: /api/users/:id handler_names: - controllers.GetUser summary: "依 ID 取得使用者" tags: - users output_type: UserResponse - method: GET path: /health handler_names: - main.healthHandler database: driver: postgres has_replicas: true
{
"version": "1.0",
"framework": "HypGo",
"server": {
"addr": ":8080",
"protocol": "http2",
"tls": true
},
"routes": [
{
"method": "POST",
"path": "/api/users",
"handler_names": ["controllers.CreateUser"],
"summary": "建立使用者",
"tags": ["users"],
"input_type": "CreateUserRequest",
"output_type": "UserResponse",
"responses": { "201": "User created" }
}
]
}type Manifest struct { Version string // 固定 "1.0" Framework string // 固定 "HypGo" GeneratedAt time.Time // 生成時間 Server ServerInfo // 伺服器配置 Routes []RouteManifest // 所有路由 Middleware []string // 全域中間件 Database *DatabaseInfo // 資料庫配置(可選) }
type RouteManifest struct { // 多協議支援 Protocol string // "rest", "grpc", "bot", "mcp", "websocket", "cli" Command string // 非 REST 命令標識 Platform string // bot 專用平台(telegram, line, discord...) // REST 欄位 Method string // HTTP 方法 Path string // 路由路徑 HandlerNames []string // handler 函式名(透過 runtime 反射) // 共用 metadata Summary string // 路由描述(來自 schema) Description string // 詳細描述(來自 schema) Tags []string // 分類標籤(來自 schema) InputType string // 請求型別名稱(來自 schema) OutputType string // 回應型別名稱(來自 schema) Responses map[int]string // 狀態碼 → 描述(來自 schema) }
Manifest 同時包含 REST 和非 REST 路由,AI 讀一個 manifest 就能理解所有協議的 API。
Collector 從三個來源收集資訊:
| 來源 | 提供資訊 |
|---|---|
Router.Routes() |
Method、Path、HandlerNames |
schema.Global() |
Summary、Tags、InputType、OutputType、Responses |
config.Config |
Server(addr、protocol、TLS)、Database(driver、replicas) |
未使用 Schema() 註冊的路由仍會出現在 manifest 中,但不含 schema metadata。
Router.Routes()
│
├──→ 每個 RouteInfo
│ │
│ └──→ schema.Global().Get(method, path)
│ │
│ └──→ 合併為 RouteManifest
│
├──→ sort by Path + Method
│
config.Config ──→ ServerInfo + DatabaseInfo
│
└──→ 組裝 Manifest
// 寫入 io.Writer manifest.WriteYAML(w, m) // YAML 格式,2 空格縮排 manifest.WriteJSON(w, m) // JSON 格式,2 空格縮排 // 儲存到檔案(自動建立目錄) manifest.SaveToFile(path, m, "yaml") manifest.SaveToFile(path, m, "json")
v0.8.5+Manifest 智慧增強(Layer 2 LLM 增強、llm.yaml配置)為 v0.8.5 新增功能。v0.8.1 的 Manifest 僅含 Layer 1 純 Go 推斷。
Manifest 支援兩層增強機制,自動填充 Summary、Tags、Description 等欄位:
從 handler 名稱和路徑自動推斷,不需任何外部依賴:
// handler 名 → Summary "(*UserController).Create" → "Create User" // 路徑 → Tags "/api/users/:id" → ["users"]
透過 config/llm.yaml 啟用,支援三種模式:
| 模式 | 說明 | 適用場景 |
|---|---|---|
none |
不使用 | X |
ollama |
連接本地 Ollama | 開發環境,零費用 |
api |
OpenAI / Anthropic / Gemini | 正式環境,最高品質 |
rag |
向量資料庫 + embedding + LLM | 大型專案,最精確 |
# 最簡配置 — 連接本地 Ollama mode: ollama ollama: model: llama3
# API 模式 — 使用 OpenAI mode: api api: provider: openai model: gpt-4o-mini api_key: "${OPENAI_API_KEY}" # 支援環境變數
# RAG 模式 — 向量檢索 + LLM 生成 mode: rag rag: embedding_model: nomic-embed-text vector_store: chroma vector_store_url: "http://localhost:8000" collection: my_codebase generator_model: llama3
// 載入 LLM 配置 llmCfg, err := config.LoadLLMConfig("config/llm.yaml") // 建立帶 LLM 增強的 Collector c, err := manifest.NewCollectorWithLLM(myRouter, myConfig, llmCfg) m := c.Collect()
# 自動偵測 config/llm.yaml 或 .hyp/llm.yaml hyp context # 指定配置檔 hyp context --llm config/llm.yaml # 搭配輸出選項 hyp context --llm config/llm.yaml -o .hyp/manifest.yaml -f yaml
mode: none ┃ 零成本 ┃ ★★☆ 基礎推斷
mode: ollama ┃ 零費用 ┃ ★★★ 本地模型生成
mode: api ┃ 按量付費 ┃ ★★★★ 雲端模型生成
mode: rag ┃ 按量付費 ┃ ★★★★★ 結合程式碼上下文
-
api_key支援${ENV_VAR}語法,避免明文存入配置檔 - Manifest 輸出不包含 LLM 配置(不洩漏 API key)
- LLM 回應做 sanitize,防止注入不預期內容
- Ollama 預設只連
localhost,不暴露到外網
cfg := manifest.EnrichConfig{ InferSummary: true, // 從 handler 名推斷 Summary InferTags: true, // 從路徑推斷 Tags IncludeFields: true, // 包含 InputFields/OutputFields LLMEnricher: myLLMEnricher, // LLM 增強器(可選) } c := manifest.NewCollectorWithEnrich(router, config, cfg)
pkg/manifest/
├── manifest.go Manifest、ServerInfo、RouteManifest、DatabaseInfo 型別
├── collector.go Collector:從 Router + Config + Schema Registry 收集
├── enricher.go EnrichConfig、純 Go 推斷邏輯、LLMEnricher 介面
├── llm_enricher.go OllamaEnricher、APIEnricher、RAGEnricher 實作
├── writer.go WriteYAML、WriteJSON、SaveToFile 輸出
├── manifest_test.go Collector + Writer 測試
└── llm_enricher_test.go LLM Enricher 測試(含 httptest 模擬)
pkg/manifest → pkg/router(Routes() 取得路由清單)
→ pkg/config(取得伺服器/資料庫設定)
→ pkg/schema(取得 schema metadata)
→ gopkg.in/yaml.v3(YAML 輸出)
Server.Start() 啟動時自動呼叫 pkg/autosync,將 Manifest 寫入 .hyp/context.yaml,確保 AI 永遠有最新的專案結構:
// Server.Start() 內部自動執行: sync := autosync.New(autosync.Config{Enabled: true}, router, config, logger) sync.SyncSafe() // 失敗不 panic,僅 log warning
特點:
-
原子寫入:temp file +
os.Rename()防止中途損壞 - 安全:不包含密碼、token、DSN 等敏感資訊
- 零配置:Server 啟動即同步,無需手動操作
設計文件
套件
- config — 設定
- context — 請求上下文
- router — 路由器
- server — 伺服器
- middleware — 中介層
- websocket — WebSocket
- hidb — 資料庫 ORM
- hidb/cassandra — Cassandra
- logger — 日誌
- json — JSON 處理
- grpc — gRPC
AI 協作工具鏈
- schema — Schema-first 路由
- manifest — 專案 Manifest
- contract — Contract Testing
- errors — Typed Error Catalog
- migrate — Migration Diff
- scaffold — 智慧 Scaffold
- airules — AI Rules
CLI 命令
- hyp 總覽
- hyp new
- hyp api
- hyp run
- hyp restart
- hyp generate
- hyp migrate
- hyp context
- hyp ai-rules
- hyp chkcomment
- hyp impact
- hyp docker
- hyp health
- hyp version
- hyp difflog
Design Docs
Packages
- config — Configuration
- context — Request Context
- router — Router
- server — Server
- middleware — Middleware
- websocket — WebSocket
- hidb — Database ORM
- hidb/cassandra - Cassandra 5.0
- logger — Logger
- json — JSON
- grpc — gRPC
AI Collaboration Toolchain
- schema — Schema-first Routing
- manifest — Project Manifest
- contract — Contract Testing
- errors — Typed Error Catalog
- migrate — Migration Diff
- scaffold — Smart Scaffold
- airules — AI Rules
CLI Commands