← 返回
未分类

Openapi Contract Driven

OpenAPI Contract-Driven Development workflow for multi-agent teams. Solves the systemic defect of "frontend invents endpoints, backend forgets to implement"...
面向多智能体团队的OpenAPI契约驱动开发工作流,解决“前端自行发明接口,后端忘记实现”的系统性缺陷。
kmpanda011172 kmpanda011172 来源
未分类 clawhub v1.1.0 1 版本 100000 Key: 无需
★ 0
Stars
📥 97
下载
💾 0
安装
1
版本
#agent-workflow#api-design#code-generation#contract-testing#latest#openapi

概述

OpenAPI Contract-Driven Development

一句话:让前端 agent 连犯错的机会都没有。

解决的问题

pm-agent 漏需求 → 架构漏设计 → 前端自造端点 → 后端没实现 → 用户看到 500

          ↓ 本 Skill 在此切断
          
OpenAPI YAML 是唯一契约 → 代码生成 → 前端只能调生成的函数 → 编译期暴露缺口

核心原理

防线机制拦截什么
---------------------
① 契约层check-api-rules.sh 7条规则operationId缺失、ApiResponse未包裹、ErrorResponse缺失
② 代码生成apiClient.ts 从前端唯一入口手写 fetch/axios、自造端点
③ 运行时ops-agent 逐端口 curl路由错误、服务宕机、端口不匹配
④ 设计审计coordinator 前后端期望对照架构漏设计、需求未覆盖

快速开始

1. 安装

openclaw skills install openapi-contract-driven

# 可选:openapi-typescript(TypeScript 类型生成)
npm install -D openapi-typescript

2. 从模板生成项目 OpenAPI 规范

cp references/openapi-template.yaml standards/{project}-openapi.yaml
# 编辑 YAML,定义你的端点

3. 运行规则检查

bash references/check-api-rules.sh standards/{project}-openapi.yaml
# 期望:✅ 6/7 通过

4. 自动生成前端 API Client

# 🆕 v1.1:一条命令从 YAML 生成完整 apiClient.ts
bash references/generate-api-client.sh standards/{project}-openapi.yaml \
  > frontend/src/api/generated/apiClient.ts

不用再手写端点函数。generate-api-client.sh 从 YAML 自动提取所有 path + method → operationId,生成类型安全的 export const xxx = { ... }

5. 接入 CI 自动检查

# 🆕 v1.1:复制 GitHub Actions 工作流
cp references/ci-github-actions.yml .github/workflows/openapi-check.yml

之后每次 PR 都会自动运行 check-api-rules.sh,契约违规直接阻断合并。

6. 配置 Agent 团队

references/agent-templates/ 下的规则片段合并到对应角色的 Agent 配置中:

模板文件合并到
-----------------
architecture-agent.mdarch-agent / saas-arch-agent 的 AGENTS.md
backend-agent.mdbackend-agent / legal-backend-agent 的 AGENTS.md
frontend-agent.mdfrontend-agent / legal-frontend-agent 的 AGENTS.md
ops-agent.mdops-agent / saas-ops-agent 的 AGENTS.md
coordinator.md小雨(main agent)的 coordinator-spawn-template.md

存量项目?

→ 看 references/migration-guide.md,四阶段渐进式迁移,不改现有代码一行业务逻辑。

工作流

                    ┌─────────────────────────────┐
                    │  pm-agent: 产品需求           │
                    └──────────────┬──────────────┘
                                   ▼
                    ┌─────────────────────────────┐
                    │  arch-agent: 架构设计         │
                    │  → 输出 OpenAPI YAML (强制)   │
                    └──────────────┬──────────────┘
                                   ▼
                    ┌─────────────────────────────┐
                    │  coordinator: 对照审计         │
                    │  前端期望 vs YAML 定义         │
                    └──────────────┬──────────────┘
                           ┌───────┴───────┐
                           ▼               ▼
              ┌──────────────────┐ ┌──────────────────┐
              │ frontend-agent    │ │ backend-agent     │
              │ 只 import 生成的   │ │ 对照 YAML 实现    │
              │ apiClient         │ │ Controller        │
              └────────┬─────────┘ └────────┬─────────┘
                       │                    │
                       └────────┬───────────┘
                                ▼
                    ┌─────────────────────────────┐
                    │  ops-agent: 部署后验证        │
                    │  ① check-api-rules.sh        │
                    │  ② curl 逐端口逐端点          │
                    └─────────────────────────────┘

7 条核心规则

.spectral.yaml 规则集(不与 Spectral CLI 耦合,bash 脚本可独立运行):

  1. operationId 完整性 — 前端代码生成强依赖(error)
  2. ApiResponse 强制 wrapper — 防止「有的返回 T,有的返回 {data:T}」(error)
  3. ErrorResponse 标准模型 — 前端不用猜 error shape(warn)
  4. 分页参数统一 — PageParam + SizeParam + SortParam(warn)
  5. health 端点 — 每个服务必须暴露(warn)
  6. 日期格式显式标注format: date-time(warn)
  7. BearerAuth securityScheme — 统一认证声明(error)

文件清单

openapi-contract-driven/
├── SKILL.md
├── README.md
└── references/
    ├── openapi-template.yaml          新项目起始模板
    ├── .spectral.yaml                  7 条核心规则
    ├── check-api-rules.sh              规则检查脚本(bash only)
    ├── generate-api-client.sh          🆕 YAML → TypeScript 自动生成
    ├── apiClient-template.ts           前端 apiClient 手动模板
    ├── ci-github-actions.yml           🆕 GitHub Actions CI 模板
    ├── migration-guide.md              🆕 存量项目迁移指南
    └── agent-templates/
        ├── architecture-agent.md
        ├── backend-agent.md
        ├── frontend-agent.md
        ├── ops-agent.md
        └── coordinator.md

跨项目验证

已在两个项目上跑通:

项目端点规则检查apiClient
--------------------------------
LexGuard(法律合同审查)306/7 ✅11KB
寓居智联(资产管理)466/7 ✅17KB

🆕 v1.1 更新日志

新增/修复说明
:------:------
generate-api-client.sh从 YAML 自动生成 TypeScript apiClient,682行纯 bash/awk,零依赖
ci-github-actions.ymlPR 时自动运行契约检查,违规阻断合并
migration-guide.md存量项目四阶段渐进迁移,含 MVP 最小可行路径
check-api-rules.sh 修复不再因 YAML 缩进风格差异误报;$ref 和注释引用同时匹配
英文 description触达国际 DevOps 用户
路径修正scripts/references/

参考资料

版本历史

共 1 个版本

  • v1.1.0 当前
    2026-06-09 19:00

安全检测

腾讯云安全 (Keen)

队列中

腾讯云安全 (Sanbu)

队列中

🔗 相关推荐

ai-agent

Agent Browser

rez0
用于 AI 代理的浏览器自动化 CLI。当用户需要与网站交互(包括浏览页面、填写表单、点击按钮、截图等)时使用。
★ 842 📥 320,130
ai-agent

Self-Improving + Proactive Agent

ivangdavila
自我反思+自我批评+自我学习+自组织记忆。智能体评估自身工作、发现错误并持续改进。
★ 1,403 📥 323,993
ai-agent

Find Skills

guipi888
场景驱动+关键词双模式技能发现工具。当用户用自然语言描述场景/需求(如"我想做一个海报""帮我分析股票"),或明确说"安装技能/find skills/找个skill"时,自动从官方内置、本地已安装、SkillHub、虾评、GitHub、C
★ 1,486 📥 548,859