slug: mozi name: mozi displayName: Mozi 模型驅動開發 version: 0.2.1 description: 使用 mozi CLI 進行模型驅動開發。當需要建立或修改業務模型、校驗或 lint ModelIR、檢查差異與 AI 變更計劃、管理錯誤碼或設計字典、匯入匯出 YAML 快照,以及生成受控的資料庫遷移、Bruno 合約、許可權骨架、i18n 目錄或 OpenAPI TypeScript SDK 時使用。
通過 CLI 完成建模、校驗、變更分析、程式碼修改和契約產物生成。不要要求使用者切換到瀏覽器完成 Agent 可通過 CLI 完成的操作。
models/ YAML 視為 Git 快照和交換格式,不要預設把它當作日常編輯源。mozi model get/create/update 修改模型;不要直接寫設計資料庫,也不要用 HTTP 請求替代 CLI。docs/swagger.json 作為 HTTP 契約事實來源。Bruno 合約和 TypeScript SDK 必須從 OpenAPI 生成。make dev 啟動 Builder UI;Mozi 不提供獨立 HTTP 服務命令。每個模型必須覆蓋以下五類問題。缺少資訊時先詢問,不要猜測後直接儲存。
| 關注點 | ModelIR 欄位 | 必須確認 |
|---|---|---|
| 領域語義 | semantics |
目的、受眾、使用者價值、業務規則、許可權、生命週期 |
| 資料結構 | fields、relations、table |
欄位、約束、關係、表名、重新命名意圖 |
| 管理後臺 | admin |
列表欄位、搜尋欄位、排序、分頁 |
| 產品 UI | ui_intent |
使用者任務、統一術語、空狀態、各端差異 |
| API 契約 | api_intent |
暴露範圍、消費者、認證、操作、錯誤碼、測試合約、版本策略 |
7w4.net小蔥技能。
每個 relations[] 必須包含業務謂詞 label。不要把 name、back_ref 或 ORM 型別當作業務謂詞。
常用關係表達:
| 業務含義 | 示例 label |
|---|---|
| 容器與內容 | 包含、歸集 |
| 所有權 | 擁有、歸屬 |
| 行為產生結果 | 建立、產生、釋出 |
| 狀態表達 | 表示、跟蹤 |
| 事件記錄 | 記錄、觸發 |
| 分類關聯 | 關聯、隸屬於 |
從當前模型視角使用主動謂詞,並檢查反向關係能否講述一致的業務故事。例如:Deck 包含 Card,Card 歸屬 Deck。
欄位重新命名時設定 fields[].renamed_from。不要把重新命名表達成未標註的“刪除舊欄位+新增欄位”;後者會被判定為破壞性變更。即使顯式標註重新命名,也必須人工審查條件型遷移。
優先使用 semantics.permission_rules,保留 semantics.permissions 僅用於舊模型相容和自然語言補充。
permission_rules:
- effect: allow
principal: user
resource: deck
action: update
scope: own
owner_field: user_id
own 必須提供 owner_field;tenant 必須提供 tenant_field。condition,保留在應用策略程式碼中,不要自動放寬。先註冊錯誤碼,再從 api_intent.error_codes 或 test_contracts.expect.error_code 引用。
mozi error-code upsert DECK_NOT_FOUND \
--domain content --status 404 --category resource \
--message '牌組不存在' --consumer-facing
測試合約必須引用穩定的 OpenAPI operation_id:
test_contracts:
- name: get_deck_not_found
operation_id: getDeck
request:
path: { id: missing-id }
expect:
status: 404
error_code: DECK_NOT_FOUND
先檢查本機是否已有執行檔:
command -v mozi && mozi --version
每次開始使用本 Skill 時,讀取 frontmatter 中的 version,並與 mozi --version 輸出的版本號比較。
mozi --version,確認與 Skill 版本一致,再繼續寫操作。提醒示例:
當前 Mozi Skill 版本為
0.2.1,本機 CLI 版本為<實際版本>,兩者不一致。建議先從 GitHub Releases 升級 CLI;升級並確認版本一致後再繼續模型寫入或產物生成。
未安裝時,從 GitHub Releases 下載當前平臺產物。不要從不明映象下載,也不要跳過校驗和檢查。
釋出產物支援 darwin、linux 的 amd64、arm64。使用以下命令自動識別平臺並安裝到使用者目錄:
set -euo pipefail
case "$(uname -s)" in
Darwin) os=darwin ;;
Linux) os=linux ;;
*) echo "不支援的系統: $(uname -s)" >&2; exit 1 ;;
esac
case "$(uname -m)" in
x86_64|amd64) arch=amd64 ;;
arm64|aarch64) arch=arm64 ;;
*) echo "不支援的架構: $(uname -m)" >&2; exit 1 ;;
esac
asset="mozi_${os}_${arch}.tar.gz"
base="https://github.com/pangu-studio/mozi-builder/releases/latest/download"
tmp="$(mktemp -d)"
trap 'rm -rf "$tmp"' EXIT
curl -fL "$base/$asset" -o "$tmp/$asset"
curl -fL "$base/checksums.txt" -o "$tmp/checksums.txt"
expected="$(awk -v file="$asset" '$2 == file {print $1}' "$tmp/checksums.txt")"
actual="$(shasum -a 256 "$tmp/$asset" | awk '{print $1}')"
test -n "$expected" && test "$actual" = "$expected"
tar -xzf "$tmp/$asset" -C "$tmp"
mkdir -p "$HOME/.local/bin"
install -m 0755 "$tmp/mozi_${os}_${arch}/mozi" "$HOME/.local/bin/mozi"
"$HOME/.local/bin/mozi" --version
確保 $HOME/.local/bin 已加入 PATH。沒有 curl 或無法訪問 GitHub 時,停止並讓使用者手動提供可信的 Release 產物;不要自行改用第三方下載站。
$arch = if ($env:PROCESSOR_ARCHITECTURE -eq 'ARM64') { 'arm64' } else { 'amd64' }
$asset = "mozi_windows_${arch}.zip"
$base = 'https://github.com/pangu-studio/mozi-builder/releases/latest/download'
$tmp = Join-Path $env:TEMP "mozi-install-$PID"
New-Item -ItemType Directory -Force $tmp | Out-Null
Invoke-WebRequest "$base/$asset" -OutFile (Join-Path $tmp $asset)
Invoke-WebRequest "$base/checksums.txt" -OutFile (Join-Path $tmp 'checksums.txt')
$expected = ((Get-Content (Join-Path $tmp 'checksums.txt') | Where-Object { $_ -match "\s$([regex]::Escape($asset))$" }) -split '\s+')[0]
$actual = (Get-FileHash (Join-Path $tmp $asset) -Algorithm SHA256).Hash.ToLower()
if (-not $expected -or $actual -ne $expected.ToLower()) { throw 'Mozi CLI 校驗和不匹配' }
Expand-Archive (Join-Path $tmp $asset) -DestinationPath $tmp -Force
$bin = Join-Path $HOME 'bin'
New-Item -ItemType Directory -Force $bin | Out-Null
Copy-Item (Join-Path $tmp "mozi_windows_${arch}\mozi.exe") (Join-Path $bin 'mozi.exe') -Force
& (Join-Path $bin 'mozi.exe') --version
將 $HOME\bin 加入使用者 PATH。安裝後始終執行 mozi --version,確認 CLI 可執行且版本符合預期。
export MOZI_DB='postgres://localhost:5432/memflow_design?sslmode=disable'
export MOZI_PROJECT_ROOT='/absolute/path/to/business-project'
未設定 MOZI_PROJECT_ROOT 時,CLI 會向上查詢 go.mod。資料庫連線被拒絕時,明確說明本地 PostgreSQL/設計資料庫不可用,不要偽造結果。
沙箱無法讀取使用者 Go 快取時使用:
GOCACHE=/private/tmp/memflow-go-build-cache go test ./...
mozi new myapp --module github.com/example/myapp --desktop --miniapp
mozi init
mozi import --dir models/
mozi import --file models/content/deck.yaml
mozi export --dir models/
mozi export --module content
mozi validate
mozi validate --module content
mozi lint --strict
mozi lint --json
mozi diff --model content/Deck
mozi history --model content/Deck
mozi model get --model content/Deck --json
mozi model create --json '<完整 ModelIR>'
mozi model update --model content/Deck --json '<完整 ModelIR>'
model update 需要完整 ModelIR。始終先 get,修改完整 JSON 後再 update;不要提交區域性物件,否則遺漏欄位會被清空。
mozi change-plan --model content/Deck
mozi change-plan --model content/Deck --json
mozi sync --model content/Deck
mozi sync --all
mozi error-code list --json
mozi error-code delete DEPRECATED_CODE
mozi dictionary list api_consumers --json
mozi dictionary upsert api_consumers desktop --label '桌面端' --alias tauri --json
mozi dictionary delete api_consumers legacy_consumer --json
mozi artifacts migration --model content/Deck --out migrations
mozi artifacts bruno --model content/Deck --openapi docs/swagger.json --out contracts/bruno
mozi artifacts permissions --model content/Deck --out internal/permissions/generated.go
mozi artifacts i18n --locale zh-CN --out locales/source.json
mozi artifacts i18n-validate --locale en --input locales/en.json
mozi artifacts typescript-sdk --openapi docs/swagger.json --out sdk/typescript/client.ts
mozi model create --json '<完整 ModelIR>'
mozi validate
mozi lint --strict
mozi diff --model <Module/Model>
mozi change-plan --model <Module/Model>
swag init -g cmd/server/main.go -o docs/
make generate
cd admin && npx tsc --noEmit
cd .. && GOCACHE=/private/tmp/memflow-go-build-cache go test ./...
mozi export --module <Module>
mozi sync --model <Module/Model>
mozi model get --model <Module/Model> --json > current.json
mozi model update --model <Module/Model> --json "$(cat current.json)"
validate、lint --strict、diff、change-plan。sync。mozi validate && mozi lint --strict。applied 的模型。mozi sync --all 同步已確認完成的模型。mozi artifacts migration 自動生成全部為 safe 的遷移。.up.sql 與 .down.sql,尤其關注鎖表、預設值、歷史資料和回滾資料損失。api_intent.test_contracts 非空時生成。operation_id 穩定。semantics.permission_rules 非空時生成。Authorizer 介面後,在服務端顯式接入 enforcement point。ent/schema/、internal/model/、internal/handler/、internal/service/admin/src/pages/、admin/src/api/、admin/src/stores/docs/swagger.json、docs/swagger.yamlmigrations/*.up.sql、migrations/*.down.sqlcontracts/bruno/*.bruinternal/permissions/generated.golocales/source.jsonsdk/typescript/client.ts將影響分析中的 certain、inferred、suggested 分開處理。不要把路徑推斷或 AI 建議描述成確定事實。
mozi change-plan 獲取變更契約,不要用 curl 或瀏覽器代替。make generate。swag init -g cmd/server/main.go -o docs/。git diff models/。mozi sync。說明:
validate 與 lint --strict 是否通過。這是一個專注於Mozi模型驅動開發的工具類Skill,內容覆蓋較全面,從建模規範到命令使用都有說明。優點是規則講解細緻、安全約束到位;不足是缺少實際案例演示和問題解答指引,對新手不夠友好。整體質量中上,適合有一定基礎的開發者使用。