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 |
暴露范围、消费者、认证、操作、错误码、测试合约、版本策略 |
每个 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,内容覆盖较全面,从建模规范到命令使用都有说明。优点是规则讲解细致、安全约束到位;不足是缺少实际案例演示和问题解答指引,对新手不够友好。整体质量中上,适合有一定基础的开发者使用。