AI Agent 项目重构(一)系统分析与知识提取
这个 Agent 并不是直接把旧 Django 模型丢给大模型去“分析”,而是先经过项目扫描、模型提取、目标架构映射、语义分析、关系图构建、领域归纳以及新项目结构学习,最后才进入代码生成阶段。也就是说,它先完成“理解旧系统”和“学习新系统”,再执行 Django 6 模型草稿生成。旧系统完全没有按照 django apps 的方式创建应用,models 全堆在 db/models.py、domai
构建一个 AI Agent:自动分析 Django 旧项目并提取系统结构
一、背景
重构原因:
- 旧系统完全没有按照 django apps 的方式创建应用
- models 全堆在 db/models.py
- domain 混乱
- app 不清晰
- service 层没有
- serializer / view 混在一起
1. 旧信息目录结构
旧系统完全没有按照 django apps 的方式创建应用,models 全堆在 db/models.py、domain 混乱、app 不清晰、service 层没有、serializer / view 混在一起。
下面目录省略了很多 py 文件,总的目录结构没变。
.
├── api
│ ├── __init__.py
│ ├── controllers
│ │ ├── __init__.py
│ ├── models.py
│ ├── tests.py
│ ├── urls.py
│ └── views.py
├── billing
│ ├── __init__.py
│ ├── billing.py
│ ├── client.py
│ ├── datasender.py
│ ├── models.py
│ ├── tests.py
│ └── views.py
├── db
│ ├── __init__.py
│ ├── actionlog.py
│ ├── apis.py
│ ├── auth.py
│ ├── config.py
│ └── volume.py
├── manage.py
├── test1
│ ├── __init__.py
│ ├── config.py
│ ├── settings.py
│ ├── urls.py
│ ├── util.py
│ └── wsgi.py
├── requirements
├── sysadmin
│ ├── __init__.py
│ ├── controllers
│ │ ├── __init__.py
│ │ ├── user.py
│ ├── forms.py
│ ├── models.py
│ ├── tests.py
│ ├── urls.py
│ ├── views.py
│ └── widgets.py
├── templates
│ ├── 404.html
│ ├── email
│ │ ├── auto_shutdown.tpl
│ ├── layout
│ │ ├── jsandcss.html
│ │ ├── master.html
│ │ └── sidebar.html
│ ├── sysadmin
│ │ ├── actionlog
│ │ │ └── list.html
│ │ │ ├── iptable.html
│ │ │ ├── join.html
2. 新系统目录结构
新项目使用 django_rest_framework 给前端提供 api。
根据功能进行分类,比如管理员信息,权限,权限组等放在 core 目录下。
还有其他很多 app 比如 compute,network 等省略。
.
├── apps
│ ├── __init__.py
│ └── core
│ ├── __init__.py
│ ├── apps.py
│ ├── models
│ │ ├── __init__.py
│ │ ├── user.py
│ │ └── permission.py
│ ├── rbac
│ ├── serializers
│ ├── services
│ ├── tests.py
│ ├── urls
│ ├── views
│ └── views.py
├── manage.py
├── re_nexus
│ ├── __init__.py
│ ├── asgi.py
│ ├── config.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
└── README.md
3. Agent 目录结构
.
├── agent
│ └── __init__.py
├── analyzers
│ ├── __init__.py
├── config.py
├── llm
│ ├── __init__.py
├── main.py
├── requirements.txt
├── tasks
│ ├── __init__.py
├── tools
│ ├── __init__.py
└── workspace
二、Agent 整体架构
项目代码
这个 Agent 并不是直接把旧 Django 模型丢给大模型去“分析”,而是先经过项目扫描、模型提取、目标架构映射、语义分析、关系图构建、领域归纳以及新项目结构学习,最后才进入代码生成阶段。也就是说,它先完成“理解旧系统”和“学习新系统”,再执行 Django 6 模型草稿生成。
Legacy Django Project
│
▼
┌──────────────────────┐
│ 1. Project Scan │
│ - 文件扫描 │
│ - module 识别 │
│ - settings / urls │
└──────────────────────┘
│
▼
┌──────────────────────┐
│ 2. Model Analysis │
│ - Model │
│ - Fields │
│ - FK / M2M │
│ - Constants / Choices│
└──────────────────────┘
│
▼
┌──────────────────────┐
│ 3. Model Mapping │
│ - 旧模型 → 新 app │
│ - 旧模型 → 新 domain │
└──────────────────────┘
│
▼
┌──────────────────────┐
│ 4. LLM Review │
│ - 复核 mapping │
│ - 修正规则误差 │
└──────────────────────┘
│
▼
┌──────────────────────┐
│ 5. Final Merge │
│ - 合并规则 + LLM │
│ - 生成 final mapping │
└──────────────────────┘
│
├───────────────────────────────┐
│ │
▼ ▼
┌──────────────────────┐ ┌──────────────────────┐
│ 6. Semantic Analysis │ │ 9. Project Structure │
│ - user/customer语义 │ │ - apps │
│ - operator/customer │ │ - models │
│ - 字段业务角色 │ │ - serializers │
└──────────────────────┘ │ - services / views │
│ └──────────────────────┘
▼ │
┌──────────────────────┐ │
│ 7. Relationship Graph│ │
│ - 模型关系图 │ │
│ - 跨模型依赖 │ │
└──────────────────────┘ │
│ │
▼ │
┌──────────────────────┐ │
│ 8. Domain Graph │ │
│ - compute │ │
│ - asset │ │
│ - network │ │
│ - core │ │
└──────────────────────┘ │
└───────────────┬───────────────┘
▼
┌────────────────────────┐
│ 10. AI Model Generator │
│ - Model Context │
│ - Reference Selection │
│ - Relationship Graph │
│ - Field Semantics │
│ - Project Rules │
└────────────────────────┘
│
▼
New Django 6 Model Draft
目标:
让 AI 从 代码 → 数据模型 → 关系 → 领域 → 架构 逐步理解系统。
三、步骤分析
1. Project Scan 扫描旧项目结构
建立项目整体轮廓
获取:
- Python 文件
- templates
- modules
- settings
- urls
python main.py scan
输出:
project_scan.json
2.Model Analysis 分析 Django models
把代码解析成结构化数据模型
提取:
- models
- fields
- foreign key
- many-to-many
- constants
- default values
python main.py analyze-models
输出:
db_models_analysis.json
3. Model Mapping 将旧模型映射到新架构
决定模型在新架构中的位置
python main.py map-db-to-target
输入:
db_models_analysis.json
target_blueprint.json
输出:
db_to_target_mapping.json
例如:
VirtualServer → compute.virtual_server
IPAddress → network.ipaddress
Region → asset.region
4. LLM Mapping Review 使用大模型复核 mapping
防止规则 mapping 错误
LLM 会检查:
- app 是否正确
- domain 是否合理
- mapping 是否合理
python main.py llm-review-db-mapping
输出:
db_to_target_mapping_review.json
5. Final Mapping Merge 合并规则 + LLM review
这是后续所有步骤的核心输入。
python main.py merge-final-mapping
输出:
db_to_target_mapping_final.json
6. Field Semantic Analysis 分析字段语义
解决 legacy 字段语义混乱
python main.py analyze-semantics
解决问题:
user
customer
admin
owner
例如:
VirtualServer.user → customer
输出:
field_semantics.json
7. Relationship Graph 构建模型关系图
理解数据模型结构
python main.py analyze-relationship-graph
输入:
db_models_analysis
final_mapping
field_semantics
输出:
relationship_graph.json
示例:
VirtualServer
├── Image
├── Customer
├── PhysicalServer
└── IPAddress
8. Domain Graph 从模型关系图推导业务领域
理解业务领域结构
python main.py analyze-domain-graph
例如:
compute
└── VirtualServer
network
└── IPAddress
asset
└── PhysicalServer
输出:
domain_graph.json
9. New Project Structure Learning 分析新项目架构
让 AI 学习新项目架构
python main.py analyze-project-structure
提取:
apps
models
services
serializers
views
输出:
project_structure_graph.json
10. AI Model Generation 生成 Django6 模型
AI 自动生成新架构代码
python main.py generate-django6-model-draft
输入:
model_context
relationship_graph
field_semantics
reference_models
project_structure
输出:
workspace/drafts/apps/.../models/*.py
更多推荐


所有评论(0)