c4-gen 1.5.2 → 1.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/generator.js +365 -201
package/package.json
CHANGED
package/src/generator.js
CHANGED
|
@@ -122,10 +122,11 @@ export async function generateC4Documentation({ sourcePath, outputPath, apiKey,
|
|
|
122
122
|
}
|
|
123
123
|
|
|
124
124
|
async function generateContextDiagram(model, codeStructure) {
|
|
125
|
-
//
|
|
126
|
-
const filesList = codeStructure.files.slice(0,
|
|
125
|
+
// Tăng context để phân tích chi tiết hơn
|
|
126
|
+
const filesList = codeStructure.files.slice(0, 50).map(f => f.path).join('\n'); // Tăng từ 30 lên 50
|
|
127
127
|
const structureSummary = summarizeStructure(codeStructure.structure);
|
|
128
|
-
const sampleCode = truncateText(codeStructure.sampleCode,
|
|
128
|
+
const sampleCode = truncateText(codeStructure.sampleCode, 5000); // Tăng từ 3000 lên 5000
|
|
129
|
+
const filesByFolder = groupFilesByFolder(codeStructure.files);
|
|
129
130
|
|
|
130
131
|
const prompt = `
|
|
131
132
|
Phân tích source code sau và tạo C4 LEVEL 1: CONTEXT DIAGRAM (System Overview):
|
|
@@ -134,7 +135,10 @@ Phân tích source code sau và tạo C4 LEVEL 1: CONTEXT DIAGRAM (System Overvi
|
|
|
134
135
|
- Total Files: ${codeStructure.files.length}
|
|
135
136
|
- Project Structure: ${structureSummary}
|
|
136
137
|
|
|
137
|
-
📁
|
|
138
|
+
📁 FILES BY FOLDER:
|
|
139
|
+
${filesByFolder}
|
|
140
|
+
|
|
141
|
+
📁 KEY FILES (50 first files):
|
|
138
142
|
${filesList}
|
|
139
143
|
|
|
140
144
|
💻 SAMPLE CODE:
|
|
@@ -144,89 +148,127 @@ ${sampleCode}
|
|
|
144
148
|
${formatDependencies(codeStructure.dependencies)}
|
|
145
149
|
|
|
146
150
|
---
|
|
147
|
-
🎯 HƯỚNG DẪN PHÂN TÍCH:
|
|
148
|
-
|
|
149
|
-
QUAN TRỌNG:
|
|
150
|
-
|
|
151
|
-
1. BUSINESS CONTEXT:
|
|
152
|
-
- Đọc package.json name/description
|
|
153
|
-
- Phân tích folder structure
|
|
154
|
-
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
-
|
|
162
|
-
-
|
|
163
|
-
-
|
|
151
|
+
🎯 HƯỚNG DẪN PHÂN TÍCH CHI TIẾT:
|
|
152
|
+
|
|
153
|
+
QUAN TRỌNG: Phải phân tích TOÀN DIỆN và CHI TIẾT, KHÔNG tự nghĩ ra hoặc dùng ví dụ.
|
|
154
|
+
|
|
155
|
+
1. BUSINESS CONTEXT - PHÂN TÍCH SÂU:
|
|
156
|
+
- Đọc package.json name/description để hiểu domain
|
|
157
|
+
- Phân tích folder structure: pos/ → Point of Sale, ecommerce/ → E-commerce, etc.
|
|
158
|
+
- Scan routes/controllers để hiểu business features
|
|
159
|
+
- Identify domain entities từ models/schemas
|
|
160
|
+
- Phát hiện business flows từ service layer
|
|
161
|
+
|
|
162
|
+
2. STAKEHOLDERS - TÌM TẤT CẢ:
|
|
163
|
+
- Scan ALL files chứa "role", "permission", "user", "admin"
|
|
164
|
+
- Đọc authentication/authorization code
|
|
165
|
+
- Tìm trong migrations/seeds cho user roles
|
|
166
|
+
- Check middleware cho role-based access
|
|
167
|
+
- List ĐẦY ĐỦ mọi loại người dùng hệ thống
|
|
164
168
|
|
|
165
|
-
|
|
166
|
-
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
169
|
+
3. EXTERNAL SYSTEMS - LIỆT KÊ TẤT CẢ:
|
|
170
|
+
- Scan TOÀN BỘ dependencies cho third-party services:
|
|
171
|
+
* Payment: stripe, paypal, vnpay, momo
|
|
172
|
+
* Email: nodemailer, sendgrid, mailgun
|
|
173
|
+
* SMS: twilio, nexmo
|
|
174
|
+
* Storage: aws-s3, cloudinary, firebase-storage
|
|
175
|
+
* Auth: passport, jwt, oauth
|
|
176
|
+
* Analytics: google-analytics, mixpanel
|
|
177
|
+
* Monitoring: sentry, newrelic
|
|
178
|
+
* Search: elasticsearch, algolia
|
|
179
|
+
* Cache: redis, memcached
|
|
180
|
+
- Tìm API integrations trong code (axios calls, fetch to external URLs)
|
|
181
|
+
- Check config files cho external service endpoints
|
|
182
|
+
- PHẢI liệt kê TẤT CẢ integrations tìm được
|
|
183
|
+
|
|
184
|
+
4. USERS - PHÂN LOẠI CHI TIẾT:
|
|
185
|
+
- Scan routes để identify user journeys:
|
|
186
|
+
* /api/admin/* → Admin users
|
|
187
|
+
* /api/customer/* → Customers
|
|
188
|
+
* /api/vendor/* → Vendors
|
|
189
|
+
* /api/staff/* → Staff members
|
|
190
|
+
- Phân tích permissions/roles cho từng loại user
|
|
191
|
+
- List ĐẦY ĐỦ interactions của từng user type
|
|
192
|
+
- Identify access channels (Web App, Mobile App, API)
|
|
193
|
+
|
|
194
|
+
5. COMMUNICATION PROTOCOLS:
|
|
195
|
+
- HTTP/REST: express, fastify, koa
|
|
196
|
+
- WebSocket: socket.io, ws
|
|
197
|
+
- GraphQL: apollo, graphql
|
|
198
|
+
- gRPC: @grpc/grpc-js
|
|
199
|
+
- Message Queue: amqplib (RabbitMQ), kafkajs (Kafka)
|
|
200
|
+
- Database protocols: postgres, mysql, mongodb
|
|
201
|
+
|
|
202
|
+
6. TECHNOLOGIES - LIỆT KÊ ĐẦY ĐỦ:
|
|
203
|
+
- Frontend: React, Vue, Angular, Next.js, Nuxt, Svelte...
|
|
204
|
+
- Backend: Node.js, Python, Java, Go, .NET...
|
|
205
|
+
- Database: PostgreSQL, MySQL, MongoDB, Redis, Elasticsearch...
|
|
206
|
+
- Infrastructure: Docker, Kubernetes, AWS, GCP, Azure...
|
|
207
|
+
|
|
208
|
+
Hãy trả về JSON với format CHI TIẾT:
|
|
178
209
|
{
|
|
179
|
-
"systemName": "Tên từ package.json
|
|
180
|
-
"description": "Mô tả dựa trên phân tích code
|
|
181
|
-
"businessContext": "Domain nghiệp vụ
|
|
182
|
-
"purpose": "Mục đích
|
|
210
|
+
"systemName": "Tên CHÍNH XÁC từ package.json",
|
|
211
|
+
"description": "Mô tả ĐẦY ĐỦ dựa trên phân tích code (3-5 câu)",
|
|
212
|
+
"businessContext": "Domain nghiệp vụ CHI TIẾT (VD: Restaurant POS system with inventory management, order processing, and payment integration)",
|
|
213
|
+
"purpose": "Mục đích CỤTH (liệt kê 3-5 mục đích chính)",
|
|
183
214
|
"stakeholders": [
|
|
184
215
|
{
|
|
185
|
-
"role": "Vai trò
|
|
186
|
-
"description": "Mô tả
|
|
187
|
-
"concerns": "Concerns
|
|
216
|
+
"role": "Vai trò CHÍNH XÁC từ code",
|
|
217
|
+
"description": "Mô tả CHI TIẾT responsibilities",
|
|
218
|
+
"concerns": "Concerns CỤ THỂ (security, performance, usability...)"
|
|
188
219
|
}
|
|
189
220
|
],
|
|
190
221
|
"users": [
|
|
191
222
|
{
|
|
192
|
-
"name": "User type
|
|
193
|
-
"type": "Internal/External
|
|
194
|
-
"description": "Mô tả
|
|
223
|
+
"name": "User type TỪ CODE (Admin, Customer, Vendor, Staff...)",
|
|
224
|
+
"type": "Internal/External",
|
|
225
|
+
"description": "Mô tả ĐẦY ĐỦ role và responsibilities",
|
|
195
226
|
"interactions": [
|
|
196
|
-
"
|
|
197
|
-
"VÍ DỤ: Nếu có /api/products → 'Quản lý sản phẩm'"
|
|
227
|
+
"LIST TẤT CẢ interactions từ routes (Manage products, Process orders, View reports...)"
|
|
198
228
|
],
|
|
199
|
-
"access": "
|
|
229
|
+
"access": "Access method CHI TIẾT (Web Dashboard, Mobile App, REST API...)"
|
|
200
230
|
}
|
|
201
231
|
],
|
|
202
232
|
"externalSystems": [
|
|
203
233
|
{
|
|
204
|
-
"name": "
|
|
205
|
-
"category": "Category
|
|
206
|
-
"description": "Mô tả service",
|
|
207
|
-
"purpose": "Purpose
|
|
208
|
-
"protocol": "Protocol
|
|
209
|
-
"direction": "Inbound/Outbound
|
|
210
|
-
"dataFlow": "Data type
|
|
234
|
+
"name": "Service name CHÍNH XÁC từ dependencies",
|
|
235
|
+
"category": "Category CỤ THỂ (Payment Gateway, Email Service, Cloud Storage...)",
|
|
236
|
+
"description": "Mô tả CHI TIẾT service và version",
|
|
237
|
+
"purpose": "Purpose CỤ THỂ (Process payments, Send notifications...)",
|
|
238
|
+
"protocol": "Protocol (HTTPS/REST, SMTP, S3...)",
|
|
239
|
+
"direction": "Inbound/Outbound/Bidirectional",
|
|
240
|
+
"dataFlow": "Data type CỤ THỂ (Payment data, Email content, File uploads...)"
|
|
211
241
|
}
|
|
212
242
|
],
|
|
213
|
-
"architectureStyle": "
|
|
214
|
-
"deploymentModel": "
|
|
243
|
+
"architectureStyle": "Architecture CHI TIẾT (Monolithic with modular structure, Microservices...)",
|
|
244
|
+
"deploymentModel": "Deployment CỤ THỂ (Docker containers, Kubernetes, Serverless...)",
|
|
215
245
|
"technologies": {
|
|
216
|
-
"frontend": ["
|
|
217
|
-
"backend": ["
|
|
218
|
-
"database": ["
|
|
219
|
-
"infrastructure": ["
|
|
246
|
+
"frontend": ["TẤT CẢ frontend tech + versions"],
|
|
247
|
+
"backend": ["TẤT CẢ backend tech + versions"],
|
|
248
|
+
"database": ["TẤT CẢ databases + versions"],
|
|
249
|
+
"infrastructure": ["TẤT CẢ infrastructure tools"],
|
|
250
|
+
"devOps": ["CI/CD tools, monitoring..."],
|
|
251
|
+
"security": ["Auth methods, encryption..."]
|
|
220
252
|
},
|
|
221
253
|
"keyPrinciples": [
|
|
222
|
-
"
|
|
254
|
+
"LIST principles từ code (RESTful API, Microservices, DDD, CQRS...)"
|
|
255
|
+
],
|
|
256
|
+
"businessFlows": [
|
|
257
|
+
"KEY business flows từ code (Order Processing, Inventory Management...)"
|
|
223
258
|
]
|
|
224
259
|
}
|
|
225
260
|
|
|
261
|
+
YÊU CẦU ĐẦY ĐỦ:
|
|
262
|
+
✅ Stakeholders: PHẢI có ít nhất 3-5 stakeholder types
|
|
263
|
+
✅ Users: PHẢI liệt kê TẤT CẢ user types tìm được
|
|
264
|
+
✅ External Systems: PHẢI liệt kê TẤT CẢ dependencies/integrations
|
|
265
|
+
✅ Technologies: PHẢI đầy đủ tất cả tech stack
|
|
266
|
+
✅ Business Flows: PHẢI liệt kê các flows chính
|
|
267
|
+
|
|
226
268
|
LƯU Ý:
|
|
227
|
-
- Nếu KHÔNG TÌM THẤY
|
|
228
|
-
-
|
|
229
|
-
-
|
|
269
|
+
- Nếu KHÔNG TÌM THẤY → để mảng rỗng [], KHÔNG bịa
|
|
270
|
+
- PHẢI CHI TIẾT và ĐẦY ĐỦ
|
|
271
|
+
- Ưu tiên QUALITY over QUANTITY nhưng KHÔNG bỏ sót
|
|
230
272
|
`;
|
|
231
273
|
|
|
232
274
|
checkPromptSize(prompt);
|
|
@@ -240,8 +282,9 @@ LƯU Ý:
|
|
|
240
282
|
async function generateContainerDiagram(model, codeStructure) {
|
|
241
283
|
// Tăng context để AI hiểu rõ hơn
|
|
242
284
|
const filesByType = groupFilesByType(codeStructure.files);
|
|
285
|
+
const filesByFolder = groupFilesByFolder(codeStructure.files);
|
|
243
286
|
const structureSummary = summarizeStructure(codeStructure.structure);
|
|
244
|
-
const sampleCode = truncateText(codeStructure.sampleCode,
|
|
287
|
+
const sampleCode = truncateText(codeStructure.sampleCode, 8000); // Tăng từ 5000 lên 8000
|
|
245
288
|
|
|
246
289
|
const prompt = `
|
|
247
290
|
Phân tích source code và tạo C4 LEVEL 2: CONTAINER DIAGRAM (System Components):
|
|
@@ -252,112 +295,165 @@ ${structureSummary}
|
|
|
252
295
|
📁 FILES BY TYPE:
|
|
253
296
|
${filesByType}
|
|
254
297
|
|
|
255
|
-
|
|
298
|
+
� FILES BY FOLDER:
|
|
299
|
+
${filesByFolder}
|
|
300
|
+
|
|
301
|
+
�📦 DEPENDENCIES:
|
|
256
302
|
${formatDependencies(codeStructure.dependencies)}
|
|
257
303
|
|
|
258
304
|
💻 SAMPLE CODE:
|
|
259
305
|
${sampleCode}
|
|
260
306
|
|
|
261
307
|
---
|
|
262
|
-
🎯 HƯỚNG DẪN PHÂN TÍCH:
|
|
263
|
-
|
|
264
|
-
QUAN TRỌNG: Chỉ dựa trên CODE và DEPENDENCIES thực tế,
|
|
265
|
-
|
|
266
|
-
1. IDENTIFY CONTAINERS
|
|
267
|
-
- Frontend:
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
-
|
|
272
|
-
|
|
308
|
+
🎯 HƯỚNG DẪN PHÂN TÍCH CHI TIẾT:
|
|
309
|
+
|
|
310
|
+
QUAN TRỌNG: Chỉ dựa trên CODE và DEPENDENCIES thực tế, PHẢI CHI TIẾT và ĐẦY ĐỦ.
|
|
311
|
+
|
|
312
|
+
1. IDENTIFY ALL CONTAINERS - LIỆT KÊ TẤT CẢ:
|
|
313
|
+
- Frontend Containers:
|
|
314
|
+
* React/Vue/Angular → Web Application
|
|
315
|
+
* React Native → Mobile App
|
|
316
|
+
* Next.js → SSR Application
|
|
317
|
+
- Backend Containers:
|
|
318
|
+
* Express/Fastify/NestJS → API Server
|
|
319
|
+
* GraphQL Server → GraphQL API
|
|
320
|
+
* WebSocket Server → Realtime Service
|
|
321
|
+
- Data Containers:
|
|
322
|
+
* PostgreSQL/MySQL → Primary Database
|
|
323
|
+
* MongoDB → Document Store
|
|
324
|
+
* Redis → Cache Layer
|
|
325
|
+
* Elasticsearch → Search Engine
|
|
326
|
+
- Infrastructure Containers:
|
|
327
|
+
* Nginx → Reverse Proxy
|
|
328
|
+
* RabbitMQ/Kafka → Message Broker
|
|
329
|
+
* Worker processes → Background Jobs
|
|
330
|
+
|
|
331
|
+
2. TECHNOLOGY STACK - CHÍNH XÁC:
|
|
332
|
+
- Đọc package.json cho EXACT versions
|
|
333
|
+
- VD: "express": "^4.18.2" → "Express.js 4.18.2"
|
|
334
|
+
- List ALL major dependencies với versions
|
|
273
335
|
|
|
274
|
-
|
|
275
|
-
-
|
|
276
|
-
-
|
|
336
|
+
3. CONTAINER SPECIFICATIONS:
|
|
337
|
+
- Ports: Scan code cho app.listen(), PORT, env variables
|
|
338
|
+
- Environment: Development, Staging, Production configs
|
|
339
|
+
- Scaling: Replicas từ docker-compose hoặc k8s
|
|
340
|
+
- Resources: Memory/CPU limits từ deployment configs
|
|
341
|
+
- Health checks: Tìm /health, /ping endpoints
|
|
277
342
|
|
|
278
|
-
|
|
279
|
-
-
|
|
280
|
-
|
|
281
|
-
|
|
343
|
+
4. COMMUNICATION PATTERNS:
|
|
344
|
+
- Synchronous:
|
|
345
|
+
* HTTP/REST → express routes
|
|
346
|
+
* GraphQL → apollo-server
|
|
347
|
+
* gRPC → @grpc/grpc-js
|
|
348
|
+
- Asynchronous:
|
|
349
|
+
* Message Queue → amqplib, kafkajs
|
|
350
|
+
* WebSocket → socket.io, ws
|
|
351
|
+
* Event Bus → eventemitter2
|
|
352
|
+
- Caching:
|
|
353
|
+
* Redis → ioredis
|
|
354
|
+
* In-memory → node-cache
|
|
282
355
|
|
|
283
|
-
|
|
284
|
-
-
|
|
285
|
-
-
|
|
286
|
-
-
|
|
287
|
-
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
-
|
|
292
|
-
-
|
|
293
|
-
-
|
|
294
|
-
|
|
295
|
-
|
|
356
|
+
5. CONTAINER INTERACTIONS - CHI TIẾT:
|
|
357
|
+
- API Server ↔ Database: Query patterns, connection pooling
|
|
358
|
+
- API Server ↔ Cache: Cache strategies (read-through, write-through)
|
|
359
|
+
- API Server ↔ Message Queue: Pub/Sub patterns
|
|
360
|
+
- Frontend ↔ API Server: REST endpoints, WebSocket connections
|
|
361
|
+
- Workers ↔ Queue: Job processing patterns
|
|
362
|
+
|
|
363
|
+
6. DATA STORES - LIỆT KÊ TẤT CẢ:
|
|
364
|
+
- Primary DB: Main data storage với schema
|
|
365
|
+
- Cache: Redis cho session, caching
|
|
366
|
+
- Search: Elasticsearch cho full-text search
|
|
367
|
+
- File Storage: S3, Cloudinary cho uploads
|
|
368
|
+
- Analytics: Time-series DB nếu có
|
|
369
|
+
|
|
370
|
+
Hãy trả về JSON CHI TIẾT:
|
|
296
371
|
{
|
|
297
|
-
"architectureStyle": "
|
|
372
|
+
"architectureStyle": "Architecture CỤ THỂ (Monolithic, Microservices, Modular Monolith...)",
|
|
298
373
|
"containers": [
|
|
299
374
|
{
|
|
300
|
-
"name": "Tên
|
|
301
|
-
"type": "Type
|
|
302
|
-
"technology": "
|
|
303
|
-
"description": "Mô tả
|
|
375
|
+
"name": "Tên CHÍNH XÁC (API Server, Web Dashboard, Mobile App, PostgreSQL DB...)",
|
|
376
|
+
"type": "Type CỤ THỂ (Web Application/API Server/Database/Cache/Message Queue...)",
|
|
377
|
+
"technology": "Tech + version CHÍNH XÁC (Express.js 4.18.2, React 18.2.0, PostgreSQL 14.5...)",
|
|
378
|
+
"description": "Mô tả ĐẦY ĐỦ responsibilities và purpose",
|
|
304
379
|
"responsibilities": [
|
|
305
|
-
"
|
|
306
|
-
"Nếu Backend→Business logic, API endpoints..."
|
|
380
|
+
"LIST TẤT CẢ responsibilities từ code (Handle API requests, Process payments, Manage inventory...)"
|
|
307
381
|
],
|
|
308
|
-
"port": "Port
|
|
309
|
-
"replicas": "
|
|
382
|
+
"port": "Port CHÍNH XÁC hoặc 'Not specified'",
|
|
383
|
+
"replicas": "Số instances (1, 3, auto-scale...)",
|
|
310
384
|
"resources": {
|
|
311
|
-
"memory": "
|
|
312
|
-
"cpu": "
|
|
385
|
+
"memory": "Memory limit (512MB, 1GB, 2GB...)",
|
|
386
|
+
"cpu": "CPU limit (0.5, 1.0, 2.0...)"
|
|
313
387
|
},
|
|
314
|
-
"storage": "
|
|
315
|
-
"protocols": ["
|
|
316
|
-
"dependencies": ["
|
|
388
|
+
"storage": "Storage type và size",
|
|
389
|
+
"protocols": ["TẤT CẢ protocols (HTTP/1.1, HTTP/2, WebSocket, gRPC...)"],
|
|
390
|
+
"dependencies": ["TẤT CẢ runtime dependencies"],
|
|
317
391
|
"interactions": [
|
|
318
392
|
{
|
|
319
|
-
"to": "
|
|
320
|
-
"description": "Mô tả
|
|
321
|
-
"protocol": "Protocol
|
|
322
|
-
"direction": "
|
|
323
|
-
"dataExchanged": "Data type
|
|
324
|
-
"isAsync": "true
|
|
393
|
+
"to": "Target container CHÍNH XÁC",
|
|
394
|
+
"description": "Mô tả CHI TIẾT interaction",
|
|
395
|
+
"protocol": "Protocol CỤ THỂ",
|
|
396
|
+
"direction": "Inbound/Outbound/Bidirectional",
|
|
397
|
+
"dataExchanged": "Data type CHI TIẾT",
|
|
398
|
+
"isAsync": "true/false",
|
|
399
|
+
"frequency": "Request frequency (high/medium/low)"
|
|
325
400
|
}
|
|
326
401
|
],
|
|
327
402
|
"endpoints": [
|
|
328
|
-
"
|
|
329
|
-
]
|
|
403
|
+
"TẤT CẢ endpoints/routes phục vụ"
|
|
404
|
+
],
|
|
405
|
+
"healthCheck": "Health check endpoint",
|
|
406
|
+
"logging": "Logging strategy",
|
|
407
|
+
"monitoring": "Monitoring tools"
|
|
330
408
|
}
|
|
331
409
|
],
|
|
332
410
|
"dataStores": [
|
|
333
411
|
{
|
|
334
|
-
"name": "
|
|
335
|
-
"type": "Type
|
|
336
|
-
"technology": "
|
|
337
|
-
"purpose": "Purpose
|
|
338
|
-
"dataModel": "
|
|
339
|
-
"port": "Port
|
|
412
|
+
"name": "Store name CHÍNH XÁC",
|
|
413
|
+
"type": "Type CỤ THỂ (Relational/NoSQL/Cache/Search/File Storage...)",
|
|
414
|
+
"technology": "Tech + version CHÍNH XÁC",
|
|
415
|
+
"purpose": "Purpose CHI TIẾT",
|
|
416
|
+
"dataModel": "Data model description",
|
|
417
|
+
"port": "Port hoặc connection string",
|
|
418
|
+
"replication": "Replication strategy",
|
|
419
|
+
"backup": "Backup strategy"
|
|
340
420
|
}
|
|
341
421
|
],
|
|
342
422
|
"messagingLayer": {
|
|
343
|
-
"technology": "Technology
|
|
344
|
-
"queues": ["
|
|
345
|
-
"
|
|
423
|
+
"technology": "Technology CHÍNH XÁC (RabbitMQ 3.11, Kafka 3.3...)",
|
|
424
|
+
"queues": ["TẤT CẢ queue names"],
|
|
425
|
+
"exchanges": ["Exchange names nếu có"],
|
|
426
|
+
"purpose": "Purpose CHI TIẾT",
|
|
427
|
+
"patterns": ["Pub/Sub, Work Queue, RPC..."]
|
|
346
428
|
},
|
|
347
429
|
"infrastructureComponents": [
|
|
348
430
|
{
|
|
349
|
-
"name": "Component
|
|
350
|
-
"technology": "Technology
|
|
351
|
-
"purpose": "Purpose
|
|
431
|
+
"name": "Component name (Nginx, API Gateway, Load Balancer...)",
|
|
432
|
+
"technology": "Technology + version",
|
|
433
|
+
"purpose": "Purpose CHI TIẾT",
|
|
434
|
+
"configuration": "Key configurations"
|
|
352
435
|
}
|
|
353
|
-
]
|
|
436
|
+
],
|
|
437
|
+
"networkingAndSecurity": {
|
|
438
|
+
"loadBalancing": "Load balancing strategy",
|
|
439
|
+
"ssl": "SSL/TLS configuration",
|
|
440
|
+
"authentication": "Auth methods",
|
|
441
|
+
"rateLimit": "Rate limiting strategy"
|
|
442
|
+
}
|
|
354
443
|
}
|
|
355
444
|
|
|
445
|
+
YÊU CẦU ĐẦY ĐỦ:
|
|
446
|
+
✅ Containers: PHẢI liệt kê TẤT CẢ containers (API, DB, Cache, Queue...)
|
|
447
|
+
✅ Technology: PHẢI có version CHÍNH XÁC từ package.json
|
|
448
|
+
✅ Interactions: PHẢI mô tả CHI TIẾT mọi connection
|
|
449
|
+
✅ Endpoints: PHẢI list routes/APIs của container
|
|
450
|
+
✅ Data Stores: PHẢI liệt kê TẤT CẢ storage systems
|
|
451
|
+
|
|
356
452
|
LƯU Ý:
|
|
357
|
-
- Nếu KHÔNG có Frontend
|
|
358
|
-
- Nếu KHÔNG có
|
|
359
|
-
-
|
|
360
|
-
-
|
|
453
|
+
- Nếu KHÔNG có Frontend → không tạo Frontend container
|
|
454
|
+
- Nếu KHÔNG có Queue → messagingLayer null
|
|
455
|
+
- CHỈ những gì CÓ TRONG CODE
|
|
456
|
+
- PHẢI CHI TIẾT và ĐẦY ĐỦ
|
|
361
457
|
`;
|
|
362
458
|
|
|
363
459
|
checkPromptSize(prompt);
|
|
@@ -371,8 +467,9 @@ LƯU Ý:
|
|
|
371
467
|
async function generateComponentDiagram(model, codeStructure) {
|
|
372
468
|
// Tăng context để đọc nhiều files hơn
|
|
373
469
|
const filesByFolder = groupFilesByFolder(codeStructure.files);
|
|
470
|
+
const filesByType = groupFilesByType(codeStructure.files);
|
|
374
471
|
const structureSummary = summarizeStructure(codeStructure.structure);
|
|
375
|
-
const sampleCode = truncateText(codeStructure.sampleCode,
|
|
472
|
+
const sampleCode = truncateText(codeStructure.sampleCode, 10000); // Tăng từ 8000 lên 10000
|
|
376
473
|
|
|
377
474
|
const prompt = `
|
|
378
475
|
Phân tích source code và tạo C4 LEVEL 3: COMPONENT DIAGRAM (Internal Structure):
|
|
@@ -383,119 +480,186 @@ ${structureSummary}
|
|
|
383
480
|
📁 FILES BY FOLDER (${codeStructure.files.length} total):
|
|
384
481
|
${filesByFolder}
|
|
385
482
|
|
|
386
|
-
|
|
483
|
+
� FILES BY TYPE:
|
|
484
|
+
${filesByType}
|
|
485
|
+
|
|
486
|
+
�📦 DEPENDENCIES:
|
|
387
487
|
${formatDependencies(codeStructure.dependencies)}
|
|
388
488
|
|
|
389
489
|
💻 SAMPLE CODE:
|
|
390
490
|
${sampleCode}
|
|
391
491
|
|
|
392
492
|
---
|
|
393
|
-
🎯 HƯỚNG DẪN PHÂN TÍCH:
|
|
394
|
-
|
|
395
|
-
QUAN TRỌNG: Chỉ liệt kê components TÌM ĐƯỢC trong code,
|
|
396
|
-
|
|
397
|
-
1. SCAN FILES ĐỂ TÌM COMPONENTS:
|
|
398
|
-
- Controllers:
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
-
|
|
402
|
-
|
|
493
|
+
🎯 HƯỚNG DẪN PHÂN TÍCH CHI TIẾT:
|
|
494
|
+
|
|
495
|
+
QUAN TRỌNG: Chỉ liệt kê components TÌM ĐƯỢC trong code, PHẢI ĐẦY ĐỦ và CHI TIẾT.
|
|
496
|
+
|
|
497
|
+
1. SCAN ALL FILES ĐỂ TÌM TẤT CẢ COMPONENTS:
|
|
498
|
+
- Controllers:
|
|
499
|
+
* Scan ALL files matching: *Controller.js, *controller.ts, routes/*.js
|
|
500
|
+
* Extract class names và route handlers
|
|
501
|
+
- Services:
|
|
502
|
+
* Scan ALL files matching: *Service.js, *service.ts, services/*.js
|
|
503
|
+
* Identify business logic components
|
|
504
|
+
- Repositories/DAOs:
|
|
505
|
+
* Scan ALL files matching: *Repository.js, *Repo.js, *DAO.js, models/*.js
|
|
506
|
+
* Map database access layers
|
|
507
|
+
- Middleware:
|
|
508
|
+
* List ALL files in middleware/, middlewares/
|
|
509
|
+
* Identify auth, validation, logging middleware
|
|
510
|
+
- Utilities:
|
|
511
|
+
* List ALL files in utils/, helpers/, lib/
|
|
512
|
+
* Categorize by function
|
|
403
513
|
|
|
404
|
-
2.
|
|
405
|
-
- Presentation:
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
-
|
|
412
|
-
|
|
413
|
-
|
|
514
|
+
2. IDENTIFY ARCHITECTURE LAYERS - CHI TIẾT:
|
|
515
|
+
- Presentation Layer:
|
|
516
|
+
* Routes, Controllers, API handlers
|
|
517
|
+
* Input validation, request mapping
|
|
518
|
+
- Business Logic Layer:
|
|
519
|
+
* Services, Use cases, Domain logic
|
|
520
|
+
* Business rules, calculations
|
|
521
|
+
- Data Access Layer:
|
|
522
|
+
* Repositories, ORMs, Query builders
|
|
523
|
+
* Database operations, caching
|
|
524
|
+
- Infrastructure Layer:
|
|
525
|
+
* Config, External integrations
|
|
526
|
+
* Logging, monitoring, error handling
|
|
527
|
+
- Cross-Cutting Concerns:
|
|
528
|
+
* Authentication, Authorization
|
|
529
|
+
* Logging, Caching, Error handling
|
|
530
|
+
|
|
531
|
+
3. API ENDPOINTS - SCAN TẤT CẢ ROUTES:
|
|
532
|
+
- Đọc TOÀN BỘ route files
|
|
533
|
+
- Extract:
|
|
534
|
+
* Method: GET, POST, PUT, DELETE, PATCH
|
|
535
|
+
* Path: /api/users, /api/products/:id
|
|
536
|
+
* Handler: Controller method name
|
|
537
|
+
* Middleware: Auth, validation middleware
|
|
538
|
+
- PHẢI liệt kê ĐẦY ĐỦ TẤT CẢ endpoints
|
|
414
539
|
|
|
415
|
-
4. COMPONENT DEPENDENCIES
|
|
416
|
-
-
|
|
417
|
-
-
|
|
540
|
+
4. COMPONENT DEPENDENCIES - MAP TOÀN BỘ:
|
|
541
|
+
- Scan imports/require trong MỌI file
|
|
542
|
+
- Build dependency graph:
|
|
543
|
+
* Controller → Service
|
|
544
|
+
* Service → Repository
|
|
545
|
+
* Repository → Database
|
|
546
|
+
- Identify circular dependencies
|
|
418
547
|
|
|
419
|
-
5. DESIGN PATTERNS:
|
|
420
|
-
-
|
|
421
|
-
-
|
|
422
|
-
-
|
|
423
|
-
|
|
424
|
-
|
|
548
|
+
5. DESIGN PATTERNS - PHÁT HIỆN:
|
|
549
|
+
- MVC: Controller + Model + View/Routes
|
|
550
|
+
- Repository: Data access abstraction
|
|
551
|
+
- Service Layer: Business logic separation
|
|
552
|
+
- Dependency Injection: IoC containers
|
|
553
|
+
- Factory: Object creation patterns
|
|
554
|
+
- Singleton: Shared instances
|
|
555
|
+
- Observer: Event emitters
|
|
556
|
+
- Strategy: Pluggable algorithms
|
|
557
|
+
|
|
558
|
+
6. MIDDLEWARE CHAIN - LIỆT KÊ:
|
|
559
|
+
- Global middleware
|
|
560
|
+
- Route-specific middleware
|
|
561
|
+
- Error handling middleware
|
|
562
|
+
- Execution order
|
|
563
|
+
|
|
564
|
+
Hãy trả về JSON CHI TIẾT:
|
|
425
565
|
{
|
|
426
566
|
"architectureLayers": [
|
|
427
567
|
{
|
|
428
|
-
"name": "
|
|
429
|
-
"purpose": "Purpose
|
|
430
|
-
"components": ["
|
|
568
|
+
"name": "Layer name CỤ THỂ (Presentation, Business Logic, Data Access, Infrastructure)",
|
|
569
|
+
"purpose": "Purpose CHI TIẾT",
|
|
570
|
+
"components": ["TẤT CẢ component names trong layer"],
|
|
571
|
+
"responsibilities": ["Chi tiết responsibilities"],
|
|
572
|
+
"patterns": ["Design patterns used"]
|
|
431
573
|
}
|
|
432
574
|
],
|
|
433
575
|
"components": [
|
|
434
576
|
{
|
|
435
|
-
"name": "
|
|
436
|
-
"type": "Type
|
|
437
|
-
"layer": "Layer
|
|
438
|
-
"technology": "Technology
|
|
439
|
-
"description": "Mô tả
|
|
577
|
+
"name": "Component name CHÍNH XÁC từ filename",
|
|
578
|
+
"type": "Type CỤ THỂ (Controller/Service/Repository/Middleware/Utility...)",
|
|
579
|
+
"layer": "Layer CỤ THỂ",
|
|
580
|
+
"technology": "Technology stack (Express, Sequelize, TypeORM...)",
|
|
581
|
+
"description": "Mô tả ĐẦY ĐỦ purpose và functionality",
|
|
440
582
|
"responsibilities": [
|
|
441
|
-
"
|
|
583
|
+
"LIST TẤT CẢ responsibilities từ methods"
|
|
442
584
|
],
|
|
443
585
|
"files": [
|
|
444
|
-
"
|
|
586
|
+
"TẤT CẢ file paths liên quan"
|
|
445
587
|
],
|
|
446
588
|
"apiEndpoints": [
|
|
447
589
|
{
|
|
448
|
-
"method": "HTTP method
|
|
449
|
-
"path": "Path CHÍNH XÁC từ route
|
|
450
|
-
"description": "Description
|
|
451
|
-
"
|
|
452
|
-
"
|
|
590
|
+
"method": "HTTP method CHÍNH XÁC",
|
|
591
|
+
"path": "Path CHÍNH XÁC từ route",
|
|
592
|
+
"description": "Description CHI TIẾT",
|
|
593
|
+
"handler": "Handler function name",
|
|
594
|
+
"authentication": "Auth requirement (Required/Optional/None)",
|
|
595
|
+
"authorization": "Role/Permission requirements",
|
|
596
|
+
"validation": "Input validation rules",
|
|
597
|
+
"requestBody": "Request body schema",
|
|
598
|
+
"responseType": "Response type"
|
|
453
599
|
}
|
|
454
600
|
],
|
|
455
601
|
"dependencies": [
|
|
456
602
|
{
|
|
457
603
|
"component": "Component name TỪ IMPORTS",
|
|
458
|
-
"purpose": "Purpose
|
|
459
|
-
"type": "
|
|
604
|
+
"purpose": "Purpose CHI TIẾT",
|
|
605
|
+
"type": "Dependency type (Import/Injection/Composition)"
|
|
460
606
|
}
|
|
461
607
|
],
|
|
462
|
-
"designPatterns": ["Patterns PHÁT HIỆN từ code structure"],
|
|
463
608
|
"methods": [
|
|
464
609
|
{
|
|
465
610
|
"name": "Method name TỪ CODE",
|
|
466
|
-
"purpose": "Purpose
|
|
467
|
-
"parameters": ["
|
|
468
|
-
"returnType": "Return type
|
|
611
|
+
"purpose": "Purpose CHI TIẾT",
|
|
612
|
+
"parameters": ["TẤT CẢ params với types"],
|
|
613
|
+
"returnType": "Return type CHÍNH XÁC",
|
|
614
|
+
"async": "true/false",
|
|
615
|
+
"visibility": "public/private/protected"
|
|
469
616
|
}
|
|
470
|
-
]
|
|
617
|
+
],
|
|
618
|
+
"designPatterns": ["Patterns PHÁT HIỆN được"]
|
|
471
619
|
}
|
|
472
620
|
],
|
|
473
621
|
"middlewareChain": [
|
|
474
622
|
{
|
|
475
623
|
"name": "Middleware name TỪ FILES",
|
|
476
|
-
"order": "
|
|
477
|
-
"purpose": "Purpose
|
|
478
|
-
"appliesTo": "Routes
|
|
624
|
+
"order": "Execution order number",
|
|
625
|
+
"purpose": "Purpose CHI TIẾT",
|
|
626
|
+
"appliesTo": "Routes áp dụng (Global/Specific routes)",
|
|
627
|
+
"type": "Middleware type (Auth/Validation/Logging/ErrorHandling...)"
|
|
479
628
|
}
|
|
480
629
|
],
|
|
481
630
|
"dataFlow": {
|
|
482
|
-
"requestFlow": "Flow
|
|
483
|
-
"responseFlow": "Response flow
|
|
631
|
+
"requestFlow": "Flow CHI TIẾT (Client → Route → Middleware → Controller → Service → Repository → Database)",
|
|
632
|
+
"responseFlow": "Response flow (Database → Repository → Service → Controller → Response)",
|
|
633
|
+
"errorFlow": "Error handling flow"
|
|
484
634
|
},
|
|
485
635
|
"crossCuttingConcerns": [
|
|
486
636
|
{
|
|
487
|
-
"name": "Concern
|
|
488
|
-
"implementation": "Implementation
|
|
489
|
-
"components": ["
|
|
637
|
+
"name": "Concern name (Logging/Caching/ErrorHandling/Security/Monitoring...)",
|
|
638
|
+
"implementation": "Implementation CHI TIẾT",
|
|
639
|
+
"components": ["TẤT CẢ components liên quan"],
|
|
640
|
+
"strategy": "Strategy used"
|
|
490
641
|
}
|
|
491
|
-
]
|
|
642
|
+
],
|
|
643
|
+
"securityMeasures": {
|
|
644
|
+
"authentication": "Auth methods CHI TIẾT",
|
|
645
|
+
"authorization": "Authorization strategy",
|
|
646
|
+
"inputValidation": "Validation approach",
|
|
647
|
+
"encryption": "Encryption methods"
|
|
648
|
+
}
|
|
492
649
|
}
|
|
493
650
|
|
|
651
|
+
YÊU CẦU ĐẦY ĐỦ:
|
|
652
|
+
✅ Components: PHẢI liệt kê TẤT CẢ components từ files scan
|
|
653
|
+
✅ API Endpoints: PHẢI có TẤT CẢ routes (50+, 100+, 200+...)
|
|
654
|
+
✅ Dependencies: PHẢI map TẤT CẢ imports/dependencies
|
|
655
|
+
✅ Middleware: PHẢI liệt kê TẤT CẢ middleware và order
|
|
656
|
+
✅ Layers: PHẢI phân loại components vào layers
|
|
657
|
+
|
|
494
658
|
LƯU Ý:
|
|
495
|
-
- Nếu KHÔNG TÌM THẤY controllers → components array
|
|
496
|
-
- Nếu KHÔNG có routes → apiEndpoints
|
|
659
|
+
- Nếu KHÔNG TÌM THẤY controllers → components array rỗng cho layer đó
|
|
660
|
+
- Nếu KHÔNG có routes → apiEndpoints rỗng []
|
|
497
661
|
- CHỈ liệt kê những gì CÓ TRONG CODE
|
|
498
|
-
-
|
|
662
|
+
- PHẢI CHI TIẾT và ĐẦY ĐỦ
|
|
499
663
|
`;
|
|
500
664
|
|
|
501
665
|
const result = await model.generateContent(prompt);
|