docs-automation
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseDocumentation Automation Skill
文档自动化Skill
Automate documentation updates to keep docs in sync with code changes.
当代码发生变更时,自动更新文档以保持文档与代码同步。
When to Use
使用场景
Use this skill when:
- API endpoints are added, modified, or removed
- Functions or classes are significantly changed
- Architecture changes occur
- New features are implemented
- Breaking changes are introduced
在以下场景中使用本Skill:
- 添加、修改或移除API端点时
- 函数或类发生重大变更时
- 架构发生变更时
- 实现新功能时
- 引入破坏性变更时
Capabilities
核心能力
1. Detect Code Changes
1. 代码变更检测
Automatically detect changes that require documentation updates:
- Monitor for API endpoint changes
backend/routers/**/*.py - Monitor for function/class changes
backend/utils/**/*.py - Monitor architecture files for structural changes
- Use git diff to identify what changed
自动检测需要更新文档的代码变更:
- 监控目录下的API端点变更
backend/routers/**/*.py - 监控目录下的函数/类变更
backend/utils/**/*.py - 监控架构文件的结构变更
- 使用git diff识别具体变更内容
2. Generate API Reference
2. API参考文档生成
Automatically generate API reference documentation:
- Extract endpoint information from FastAPI routers
- Parse route decorators (,
@router.get, etc.)@router.post - Extract function docstrings for endpoint descriptions
- Parse request/response models from type hints
- Extract query parameters, path parameters, and request bodies
- Generate MDX documentation files with examples
- Update
.cursor/API_REFERENCE.md - Update
docs/api-reference/endpoint/*.mdx - Update
docs/doc/developer/api/*.mdx
Process:
- Scan for route decorators
backend/routers/**/*.py - Extract endpoint metadata:
- HTTP method (GET, POST, etc.)
- Path pattern
- Function name and docstring
- Parameters (path, query, body)
- Response model
- Tags
- Parse FastAPI models for request/response schemas
- Generate MDX file with:
- Endpoint description from docstring
- Request/response examples
- Parameter documentation
- Error codes
- Update API reference index
Example:
python
@router.post("/v1/conversations", response_model=CreateConversationResponse, tags=['conversations'])
def process_in_progress_conversation(
request: ProcessConversationRequest = None,
uid: str = Depends(auth.get_current_user_uid)
):
"""
Process an in-progress conversation after recording is complete.
...
"""Generates:
mdx
undefined自动生成API参考文档:
- 从FastAPI路由中提取端点信息
- 解析路由装饰器(、
@router.get等)@router.post - 提取函数文档字符串作为端点描述
- 从类型提示中解析请求/响应模型
- 提取查询参数、路径参数和请求体
- 生成带示例的MDX文档文件
- 更新文件
.cursor/API_REFERENCE.md - 更新文件
docs/api-reference/endpoint/*.mdx - 更新文件
docs/doc/developer/api/*.mdx
流程:
- 扫描文件中的路由装饰器
backend/routers/**/*.py - 提取端点元数据:
- HTTP方法(GET、POST等)
- 路径模式
- 函数名称和文档字符串
- 参数(路径、查询、请求体)
- 响应模型
- 标签
- 解析FastAPI模型以获取请求/响应Schema
- 生成包含以下内容的MDX文件:
- 来自文档字符串的端点描述
- 请求/响应示例
- 参数文档
- 错误码
- 更新API参考文档索引
示例:
python
@router.post("/v1/conversations", response_model=CreateConversationResponse, tags=['conversations'])
def process_in_progress_conversation(
request: ProcessConversationRequest = None,
uid: str = Depends(auth.get_current_user_uid)
):
"""
Process an in-progress conversation after recording is complete.
...
"""生成的文档内容:
mdx
undefinedProcess In-Progress Conversation
Process In-Progress Conversation
POST /v1/conversationsProcess an in-progress conversation after recording is complete.
Request Body: ProcessConversationRequest (optional)
Response: CreateConversationResponse
undefinedPOST /v1/conversationsProcess an in-progress conversation after recording is complete.
Request Body: ProcessConversationRequest (optional)
Response: CreateConversationResponse
undefined3. Update Architecture Diagrams
3. 架构图更新
Generate and update architecture diagrams:
- Analyze code structure to generate Mermaid diagrams
- Update with new components
.cursor/ARCHITECTURE.md - Update if data flows change
.cursor/DATA_FLOW.md - Update component documentation files
生成并更新架构图:
- 分析代码结构以生成Mermaid图
- 在中新增组件信息
.cursor/ARCHITECTURE.md - 若数据流发生变更,更新
.cursor/DATA_FLOW.md - 更新组件文档文件
4. Sync Documentation
4. 文档同步
Keep documentation synchronized:
- Sync between internal docs and
.cursor/external docsdocs/ - Ensure consistency across documentation locations
- Update cross-references and links
- Validate documentation structure
保持文档同步:
- 同步内部文档与
.cursor/外部文档docs/ - 确保不同位置的文档内容一致
- 更新交叉引用与链接
- 验证文档结构
Workflow
工作流程
- Detect Changes: Analyze git diff or file changes
- Identify Impact: Determine which documentation needs updating
- Generate Updates: Create or update relevant documentation files
- Validate: Check documentation for completeness and accuracy
- Sync: Ensure all documentation locations are in sync
- 变更检测:分析git diff或文件变更
- 影响评估:确定需要更新的文档内容
- 生成更新内容:创建或更新相关文档文件
- 验证:检查文档的完整性与准确性
- 同步:确保所有位置的文档保持一致
Usage Examples
使用示例
Automatic API Documentation
自动生成API文档
When a new endpoint is added to :
backend/routers/conversations.py- Detect the new route decorator using AST parsing
- Extract endpoint details:
- Method from decorator (→ POST)
@router.post - Path from decorator argument
- Function docstring for description
- Parameters from function signature
- Response model from argument
response_model
- Method from decorator (
- Parse request/response models:
- Extract field names and types
- Generate JSON examples
- Document required vs optional fields
- Generate MDX documentation file:
- Create
docs/api-reference/endpoint/{endpoint_name}.mdx - Include description, parameters, examples
- Add to API reference index
- Create
- Update with new endpoint
.cursor/API_REFERENCE.md - Validate documentation format and links
当在中添加新端点时:
backend/routers/conversations.py- 使用AST解析检测新的路由装饰器
- 提取端点详细信息:
- 从装饰器中获取HTTP方法(→ POST)
@router.post - 从装饰器参数中获取路径
- 从函数文档字符串获取描述
- 从函数签名中获取参数
- 从参数中获取响应模型
response_model
- 从装饰器中获取HTTP方法(
- 解析请求/响应模型:
- 提取字段名称与类型
- 生成JSON示例
- 记录必填与可选字段
- 生成MDX文档文件:
- 创建文件
docs/api-reference/endpoint/{endpoint_name}.mdx - 包含描述、参数与示例
- 添加至API参考文档索引
- 创建
- 在中添加新端点信息
.cursor/API_REFERENCE.md - 验证文档格式与链接
Parsing FastAPI Routers
FastAPI路由解析
Implementation approach:
python
import ast
from typing import List, Dict
def parse_fastapi_router(file_path: str) -> List[Dict]:
"""Parse FastAPI router file and extract endpoint information."""
with open(file_path) as f:
tree = ast.parse(f.read())
endpoints = []
for node in ast.walk(tree):
if isinstance(node, ast.FunctionDef):
# Check for route decorators
for decorator in node.decorator_list:
if isinstance(decorator, ast.Call):
# Extract @router.post("/path", ...)
method = get_decorator_method(decorator)
path = get_decorator_path(decorator)
response_model = get_response_model(decorator)
endpoints.append({
'method': method,
'path': path,
'function_name': node.name,
'docstring': ast.get_docstring(node),
'parameters': parse_parameters(node),
'response_model': response_model,
})
return endpoints实现方式:
python
import ast
from typing import List, Dict
def parse_fastapi_router(file_path: str) -> List[Dict]:
"""Parse FastAPI router file and extract endpoint information."""
with open(file_path) as f:
tree = ast.parse(f.read())
endpoints = []
for node in ast.walk(tree):
if isinstance(node, ast.FunctionDef):
# Check for route decorators
for decorator in node.decorator_list:
if isinstance(decorator, ast.Call):
# Extract @router.post("/path", ...)
method = get_decorator_method(decorator)
path = get_decorator_path(decorator)
response_model = get_response_model(decorator)
endpoints.append({
'method': method,
'path': path,
'function_name': node.name,
'docstring': ast.get_docstring(node),
'parameters': parse_parameters(node),
'response_model': response_model,
})
return endpointsArchitecture Update
架构更新
When a new module is added:
- Detect new module structure
- Update architecture documentation
- Generate/update Mermaid diagrams
- Update component references
当添加新模块时:
- 检测新模块结构
- 更新架构文档
- 生成/更新Mermaid图
- 更新组件引用
Related Resources
相关资源
Rules
规则文档
- - Documentation standards
.cursor/rules/documentation-standards.mdc - - Auto-documentation rules
.cursor/rules/auto-documentation.mdc - - API patterns
.cursor/rules/backend-api-patterns.mdc
- - 文档规范
.cursor/rules/documentation-standards.mdc - - 自动文档生成规则
.cursor/rules/auto-documentation.mdc - - API设计模式
.cursor/rules/backend-api-patterns.mdc
Subagents
子Agent
- - Documentation generation subagent
.cursor/agents/docs-generator.md
- - 文档生成子Agent
.cursor/agents/docs-generator.md
Commands
命令
- - Trigger automatic documentation update
/auto-docs - - Update API reference documentation from FastAPI routers
/update-api-docs - - Generate or update documentation
/docs
- - 触发文档自动更新
/auto-docs - - 从FastAPI路由更新API参考文档
/update-api-docs - - 生成或更新文档
/docs
Implementation Notes
实现说明
FastAPI Router Parsing
FastAPI路由解析
To auto-generate API docs:
- Parse Router Files: Use AST to parse Python files and extract route decorators
- Extract Metadata: Get method, path, parameters, response models from decorators
- Parse Docstrings: Extract endpoint descriptions from function docstrings
- Generate Examples: Create request/response examples from Pydantic models
- Generate MDX: Create MDX files following documentation standards
- Update Index: Add new endpoints to API reference index
要自动生成API文档:
- 解析路由文件:使用AST解析Python文件并提取路由装饰器
- 提取元数据:从装饰器中获取方法、路径、参数与响应模型
- 解析文档字符串:从函数文档字符串中提取端点描述
- 生成示例:从Pydantic模型生成请求/响应示例
- 生成MDX文件:按照文档规范创建MDX文件
- 更新索引:将新端点添加至API参考文档索引
Tools and Libraries
工具与依赖库
- AST: Python's Abstract Syntax Tree for parsing Python code
- Pydantic: Extract model schemas for request/response examples
- FastAPI: Use FastAPI's OpenAPI schema generation capabilities
- MDX: Generate MDX files with proper frontmatter and formatting
- AST:Python的抽象语法树,用于解析Python代码
- Pydantic:提取模型Schema以生成请求/响应示例
- FastAPI:利用FastAPI的OpenAPI Schema生成能力
- MDX:生成包含正确前置内容与格式的MDX文件
Automation Triggers
自动化触发方式
- Git Hooks: Run on commit if router files changed
- CI/CD: Run in CI pipeline to validate docs are up to date
- Manual: Use command when needed
/update-api-docs
- Git钩子:当路由文件变更时,在提交时触发
- CI/CD:在CI流水线中运行,验证文档是否为最新版本
- 手动触发:必要时使用命令
/update-api-docs