agentmap 0.4.3

CLI tool to prepare codebases for AI agents by generating outlines, memory files, and reading rules
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
# agentmap

[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
[![Rust](https://img.shields.io/badge/rust-1.70%2B-orange.svg)](https://www.rust-lang.org/)

**Chuẩn bị codebase cho AI agents** bằng cách tạo tài liệu có cấu trúc giúp trợ lý AI hiểu và điều hướng code của bạn hiệu quả hơn.

[🇬🇧 English](README.md)

## Công Dụng

agentmap quét codebase và tạo ra **cấu trúc tài liệu phân cấp** theo module:

```
.agentmap/
├── INDEX.md              # L0: Bảng định hướng toàn cục
├── modules/
│   └── {module-slug}/
│       ├── MODULE.md     # L1: Tóm tắt module
│       ├── outline.md    # L1: Bản đồ symbol cho module
│       ├── memory.md     # L1: Warnings/TODOs theo module
│       └── imports.md    # L1: Dependencies cho module
└── files/
    └── {file-slug}.md    # L2: Tài liệu chi tiết cho file phức tạp (tùy chọn)
```

### Content Hierarchy

| Cấp | File | Mục đích | Kích thước |
|-----|------|----------|------------|
| L0 | `INDEX.md` | Bảng định hướng tổng quan theo module | O(modules) |
| L1 | `MODULE.md` | Tóm tắt module, file list, entry points | O(files in module) |
| L1 | `outline.md` | Bản đồ symbol cho file lớn trong module | O(large files) |
| L1 | `memory.md` | Warnings và TODOs theo module | O(markers) |
| L1 | `imports.md` | Phụ thuộc trong module | O(imports) |
| L2 | `files/*.md` | Tài liệu chi tiết cho file phức tạp | O(symbols) |

## Tại Sao Cần?

AI coding assistants gặp khó khăn với codebase lớn vì không thể thấy toàn cảnh. agentmap cung cấp:

- **Điều hướng phân cấp** để AI chỉ load module cần thiết
- **Module detection** để gom file thành các nhóm có ý nghĩa
- **Symbol maps** để biết có gì trong file lớn mà không đọc toàn bộ
- **Scoped context** để docs chỉ chứa thông tin liên quan

## Cài Đặt

### Cài Nhanh (Khuyến nghị)

```bash
curl -fsSL https://raw.githubusercontent.com/nguyenphutrong/agentmap/main/scripts/install.sh | sh
```

### Từ crates.io

```bash
cargo install agentmap
```

### Từ Source

```bash
git clone https://github.com/nguyenphutrong/agentmap
cd agentmap
cargo install --path .
```

### Tải Thủ Công

Tải prebuilt binaries từ [GitHub Releases](https://github.com/nguyenphutrong/agentmap/releases).

## Cách Dùng

### Cơ Bản

```bash
# Tạo docs cho thư mục hiện tại (hierarchical mode - mặc định)
agentmap

# Output ra thư mục tùy chỉnh
agentmap -o docs/ai

# Xem trước mà không ghi file
agentmap --dry-run

# Output chi tiết
agentmap -v
```

### Remote Repositories

```bash
# Phân tích GitHub repo trực tiếp
agentmap github.com/user/repo
agentmap https://github.com/vercel/next.js

# Giới hạn depth cho repo lớn
agentmap --depth 3 github.com/facebook/react
```

### Git Diff Mode

```bash
# Chỉ show các file thay đổi từ branch
agentmap --diff main

# So sánh với commit cụ thể
agentmap --diff HEAD~5
```

### JSON Output

```bash
# Output analysis dưới dạng JSON (cho tooling integration)
agentmap --json > analysis.json

# Kết hợp với flags khác
agentmap --json --depth 2 github.com/user/repo
```

### Options

```
Usage: agentmap [OPTIONS] [PATH]

Arguments:
  [PATH]  Thư mục đích hoặc GitHub URL [default: .]

Options:
  -o, --output <OUTPUT>              Thư mục output [default: .agentmap]
  -t, --threshold <THRESHOLD>        Ngưỡng số dòng cho file "lớn" [default: 500]
  -c, --complex-threshold <COMPLEX>  Ngưỡng symbol cho L2 file docs [default: 30]
  -d, --depth <DEPTH>                Max directory depth (0 = unlimited)
      --diff <REF>                   So sánh với git branch/commit
      --json                         Output JSON ra stdout
      --check                        Kiểm tra docs có stale không (exit 1 nếu cần regenerate)
      --config <FILE>                Đường dẫn config file
      --force                        Force regenerate tất cả modules (bỏ qua cache)
  -i, --ignore <IGNORE>              Patterns bổ sung để bỏ qua
  -l, --lang <LANG>                  Lọc theo ngôn ngữ
      --no-gitignore                 Không tuân theo .gitignore
      --dry-run                      Xem trước mà không ghi file
  -v, --verbose...                   Tăng mức chi tiết (-v, -vv, -vvv)
  -q, --quiet                        Không hiển thị output
  -h, --help                         In help
  -V, --version                      In version

Commands:
  watch   Theo dõi file changes và tự động regenerate docs
  hooks   Quản lý git hooks cho auto-regeneration
  init    Khởi tạo cấu hình agentmap
  update  Cập nhật agentmap lên phiên bản mới nhất
```

## Watch Mode

Giữ docs luôn đồng bộ trong quá trình development:

```bash
# Bắt đầu theo dõi changes (regenerate khi save file)
agentmap watch

# Tuỳ chỉnh debounce delay (mặc định: 300ms)
agentmap watch --debounce 500
```

Watch mode tận dụng hệ thống manifest incremental, nên chỉ modules thay đổi được regenerate.

## Git Hooks

Tự động regenerate docs tại các git events quan trọng:

```bash
# Cài hooks (pre-commit, post-checkout, post-merge)
agentmap hooks install

# Gỡ hooks
agentmap hooks remove

# Bỏ qua hooks tạm thời
AGENTMAP_SKIP=1 git commit -m "quick fix"
```

Các hooks được cài:
- **pre-commit**: Regenerate docs và stage `.agentmap/`
- **post-checkout**: Regenerate sau khi đổi branch (chạy nền)
- **post-merge**: Regenerate sau pull/merge (chạy nền)

## Configuration File

Tạo `agentmap.toml` cho cài đặt riêng của project:

```bash
# Tạo config file mặc định
agentmap init --config

# Dùng config tùy chỉnh
agentmap --config custom.toml
```

Ví dụ `agentmap.toml`:

```toml
output = ".agentmap"
threshold = 500
complex_threshold = 1000
ignore = ["*.test.ts", "fixtures/", "__mocks__/"]

[watch]
debounce_ms = 300
```

CLI flags ghi đè giá trị config file.

## MCP Server

agentmap có thể chạy như MCP server cho các AI tools như Claude Desktop và Cursor:

```bash
# Sử dụng npx (không cần cài đặt)
npx agentmap-cli serve --mcp

# Hoặc nếu đã cài global
agentmap serve --mcp
```

**Các tools có sẵn:**

| Tool | Mô tả |
|------|-------|
| `regenerate` | Regenerate tài liệu |
| `get_module` | Lấy docs module theo slug |
| `check_stale` | Kiểm tra docs có cần cập nhật |
| `get_outline` | Lấy symbol outline cho file |

**Ví dụ MCP config (Claude Desktop, Cursor, etc.):**

```json
{
  "mcpServers": {
    "agentmap": {
      "command": "npx",
      "args": ["agentmap-cli", "serve", "--mcp"]
    }
  }
}
```

Xem [MCP Server Documentation](docs/mcp-server.vi.md) để biết hướng dẫn cài đặt và tích hợp.

## CI Integration

Validate docs freshness trong CI pipelines:

```bash
# Kiểm tra docs có stale không (exit 0 = fresh, exit 1 = stale)
agentmap --check

# Kết hợp với diff mode
agentmap --check --diff main
```

Ví dụ GitHub Actions workflow:

```yaml
name: Check Agentmap
on: [pull_request]

jobs:
  check:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Install agentmap
        run: cargo install agentmap
      - name: Check docs freshness
        run: agentmap --check
```

## Module Detection

agentmap tự động phát hiện module boundary dựa vào quy ước theo ngôn ngữ:

| Ngôn ngữ | Boundary rõ ràng | Ví dụ |
|----------|------------------|-------|
| Rust | `mod.rs`, `lib.rs` | `src/analyze/mod.rs` → module `src-analyze` |
| Python | `__init__.py` | `src/utils/__init__.py` → module `src-utils` |
| JavaScript/TypeScript | `index.{js,ts,tsx}` | `src/components/index.ts` → module `src-components` |
| Any | 5+ source files trong thư mục | `src/helpers/` có 5+ files → implicit module |

### Module Slug Naming

Đường dẫn thư mục được chuyển thành slug bằng dấu gạch ngang:
- `src/analyze/lang``src-analyze-lang`
- `lib/utils``lib-utils`

## Ví Dụ Output

### INDEX.md (L0 Global)

```markdown
# Project

## Reading Protocol

**Start here**, then navigate to specific modules.

1. Read this INDEX for overview
2. Go to relevant `modules/{name}/MODULE.md`
3. Check module's `outline.md` for large files
4. Check module's `memory.md` for warnings

## Entry Points

- `src/main.rs`
- `src/lib.rs`

## Modules

| Module | Type | Files | Warnings | Hub |
| ------ | ---- | ----- | -------- | --- |
| [src]modules/src/MODULE.md | rust | 2 | - |  |
| [src/analyze]modules/src-analyze/MODULE.md | rust | 5 | ⚠️ 2 |  |
| [src/generate]modules/src-generate/MODULE.md | rust | 8 | - | 🔗 |
```

### MODULE.md (L1 Module)

```markdown
# Module: src/analyze

[← Back to INDEX](../../INDEX.md)

**Type:** rust | **Files:** 5

**Entry point:** `src/analyze/mod.rs`

## Files

| File | Lines | Large |
| ---- | ----- | ----- |
| `src/analyze/graph.rs` | 98 |  |
| `src/analyze/parser.rs` | 650 | 📄 |
| `src/analyze/mod.rs` | 10 |  |

## Child Modules

- [src-analyze-lang]../src-analyze-lang/MODULE.md

## Documentation

- [outline.md]outline.md - Symbol maps for large files
- [memory.md]memory.md - Warnings and TODOs
- [imports.md]imports.md - Dependencies
```

### outline.md (L1 Module-Scoped)

```markdown
# Outline: src/analyze

[← MODULE.md](MODULE.md) | [← INDEX.md](../../INDEX.md)

## src/analyze/parser.rs (650 lines)

| Line | Kind | Name | Visibility |
| ---- | ---- | ---- | ---------- |
| 15 | fn | parse_symbols | pub |
| 89 | fn | extract_functions | (private) |
| 156 | struct | ParseResult | pub |
```

### memory.md (L1 Module-Scoped)

```markdown
# Memory: src/analyze

[← MODULE.md]MODULE.md | [← INDEX.md]../../INDEX.md

## ⚠️ Warnings

### 🔴 `WARNING` (src/analyze/parser.rs:42)
> Edge case not handled for nested generics

## 🔧 Technical Debt

### 🟡 `TODO` (src/analyze/graph.rs:128)
> Optimize cycle detection algorithm
```

## Supported Languages

| Language | Symbol Extraction | Import Graph | Memory Markers | Module Detection |
|----------|-------------------|--------------|----------------|------------------|
| Rust | ✅ Functions, structs, enums, traits, impls | ✅ | ✅ | ✅ `mod.rs` |
| Python | ✅ Functions, classes, methods | ✅ | ✅ | ✅ `__init__.py` |
| JavaScript/TypeScript | ✅ Functions, classes, arrow functions | ✅ | ✅ | ✅ `index.{js,ts}` |
| Go | ✅ Functions, structs, interfaces, methods | ✅ | ✅ | ✅ implicit |
| Swift | ✅ Functions, classes, structs, enums, protocols | ✅ | ✅ | ✅ implicit |
| Dart | ✅ Functions, classes, mixins, extensions | ✅ | ✅ | ✅ implicit |
| Ruby | ✅ Methods, classes, modules | ✅ | ✅ | ✅ implicit |
| C# | ✅ Methods, classes, structs, interfaces | ✅ | ✅ | ✅ implicit |
| Java | ✅ Methods, classes, interfaces, enums | ✅ | ✅ | ✅ implicit |

## Memory Markers

agentmap trích xuất các comment patterns sau:

| Pattern | Category | Priority |
|---------|----------|----------|
| `TODO`, `FIXME`, `XXX`, `BUG`, `HACK` | Technical Debt | Medium |
| `WARNING`, `WARN` | Warnings | High |
| `SAFETY`, `INVARIANT` | Safety | High |
| `RULE`, `POLICY` | Business Rules | High |
| `DEPRECATED` | Technical Debt | High |
| `NOTE` | Notes | Low |

## Tích Hợp với AI Tools

### Claude Code / Cursor

Thêm vào AI instructions của project:

```
Trước khi làm việc với codebase này, đọc:
1. .agentmap/INDEX.md - tổng quan và điều hướng module
2. Navigate to module's MODULE.md để biết chi tiết
3. Kiểm tra module's memory.md trước khi edit
4. Consult module's outline.md để điều hướng file lớn
```

### GitHub Copilot

Include `.agentmap/` trong workspace context.

### JSON Integration

Cho programmatic access:

```bash
agentmap --json | jq '.modules[] | {slug, file_count, warning_count}'
```

JSON output gồm:
- `modules[]` - Array module metadata (slug, path, file_count, warning_count, symbol_count, is_hub)
- `files[]` - Tất cả scanned files và metadata
- `memory[]` - Tất cả memory markers và locations
- `entry_points[]` - Detected entry points
- `hub_files[]` - Files được import bởi 3+ others

## Development

```bash
# Chạy tests
cargo test

# Chạy với verbose output
cargo run -- -vv .

# Kiểm tra issues
cargo clippy
```

## License

MIT License - xem [LICENSE](LICENSE) để biết chi tiết.