cadi-scraper 2.0.0

CADI Scraper/Chunker utility for converting source code repos and file data into reusable CADI chunks
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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
# CADI Scraper/Chunker - Implementation Guide

## Overview

The CADI Scraper/Chunker utility converts any source code repository or file data into reusable CADI chunks. It enables users to:

1. **Extract content** from local directories, files, or URLs
2. **Parse and analyze** code with language-specific AST extraction
3. **Create chunks** using multiple strategies (file-based, semantic, hierarchical, etc.)
4. **Extract metadata** automatically (titles, descriptions, licenses, dependencies)
5. **Publish chunks** to authenticated registry servers
6. **Build custom CADI repositories** for personal or organizational use

## Architecture

### Core Modules

```
cadi-scraper/
├── lib.rs                 # Main library exports
├── types.rs              # Core types and configuration
├── config.rs             # Configuration management
├── error.rs              # Error types
├── fetcher.rs            # HTTP/file fetching with rate limiting
├── parser.rs             # Multi-format content parsing
├── chunker.rs            # Semantic and hierarchical chunking
├── metadata.rs           # Metadata extraction
├── transformer.rs        # Language-specific AST extraction
└── scraper.rs            # Main orchestrator
```

### Data Flow

```
Input (Path/URL/Directory)
[Fetcher] → Fetch content with rate limiting
[Parser] → Parse multi-format (code, markdown, YAML, JSON)
[Metadata Extractor] → Auto-extract titles, descriptions, licenses
[Transformer] → Extract AST, compute quality metrics
[Chunker] → Split using selected strategy
[Scraper] → Create manifest and relationships
Output (Chunks + Manifest)
[Publish] → Registry or local storage
```

## Features

### 1. Multiple Input Sources

#### Local Files
```bash
cadi scrape /path/to/file.rs
```

#### Directories
```bash
cadi scrape /path/to/project --strategy semantic
```

#### URLs (Future)
```bash
cadi scrape https://raw.githubusercontent.com/user/repo/main/src/lib.rs
```

#### Git Repositories (Future)
```bash
cadi scrape git@github.com:user/repo.git --branch main
```

### 2. Chunking Strategies

#### By File (Default)
- Each file becomes a single chunk
- Fastest, suitable for small files
- No splitting or overlap

```bash
cadi scrape /path --strategy by-file
```

#### Semantic
- Splits by functions, classes, traits, methods
- Language-aware boundary detection
- Best for understanding code structure

```bash
cadi scrape /path --strategy semantic
```

#### Fixed Size
- Splits content into fixed byte sizes
- Configurable via `--max-chunk-size`
- Useful for uniform processing

```bash
cadi scrape /path --strategy fixed-size --max-chunk-size 102400
```

#### Hierarchical
- Creates parent-child chunk relationships
- File chunk as parent
- Semantic sub-chunks as children

```bash
cadi scrape /path --strategy hierarchical
```

#### By Line Count
- Splits by fixed line count (default 100 lines)
- Simple, predictable chunking

```bash
cadi scrape /path --strategy by-line-count
```

### 3. Language Support

#### Supported Languages
- **Rust** (.rs)
- **TypeScript/JavaScript** (.ts, .tsx, .js, .jsx)
- **Python** (.py)
- **Go** (.go)
- **C/C++** (.c, .h, .cpp)
- **Java** (.java)
- **Markdown** (.md)
- **JSON/YAML/TOML** (structured data)
- **HTML/CSS** (.html, .css)

#### Language-Specific Features

**Rust:**
- Extract functions, structs, traits
- Detect async/await patterns
- Identify macro usage
- Mark unsafe code blocks

**TypeScript/JavaScript:**
- Extract classes, interfaces, functions
- Detect React components
- Find decorators and metadata
- Track imports/exports

**Python:**
- Extract classes and functions
- Identify decorators
- Track imports
- Detect async code

### 4. Metadata Extraction

Automatic extraction of:

| Item | Detection Method |
|------|-----------------|
| **Title** | Markdown heading, JSON name, Cargo.toml package.name |
| **Description** | Markdown after heading, JSON description, Cargo.toml |
| **Keywords** | JSON keywords array |
| **Concepts** | Pattern matching (database, API, UI, testing, etc.) |
| **License** | SPDX detection, JSON license field |
| **Authors** | JSON author/contributors, Cargo.toml authors |
| **Frameworks** | React, Vue, Angular, Express, FastAPI, Django, Rails, etc. |
| **Dependencies** | AST extraction, package.json, Cargo.toml |

Example output:
```json
{
  "chunk_id": "chunk:sha256:abc123...",
  "name": "Hello Function",
  "description": "A simple greeting function",
  "language": "rust",
  "concepts": ["function", "async"],
  "license": "MIT",
  "frameworks": ["tokio"],
  "dependencies": ["tokio", "serde"]
}
```

### 5. API Surface Extraction

Extracts public API:

**From Rust:**
```rust
pub fn function_name() {}
pub struct MyStruct {}
pub trait MyTrait {}
```

**From TypeScript:**
```typescript
export function functionName() {}
export class MyClass {}
export interface MyInterface {}
```

**From Python:**
```python
def function_name():
class ClassName:
```

### 6. Chunk Relationships

Hierarchical chunks track:
- **Parent ID**: Parent chunk in hierarchy
- **Child IDs**: Child chunks
- **Dependencies**: Referenced chunks
- **Concepts**: Semantic tags

Example hierarchy:
```
file-chunk (parent)
├── function-chunk-1 (child)
├── function-chunk-2 (child)
├── class-chunk-1 (child)
│   ├── method-chunk-1 (grandchild)
│   └── method-chunk-2 (grandchild)
└── trait-chunk-1 (child)
```

### 7. Manifest Generation

Creates manifest linking all chunks:

```json
{
  "version": "1.0.0",
  "cadi_type": "manifest",
  "scraped_at": "2026-01-11T12:00:00Z",
  "chunk_count": 15,
  "chunks": [
    {
      "chunk_id": "chunk:sha256:...",
      "name": "MyModule",
      "source": "src/main.rs",
      "language": "rust",
      "concepts": ["async", "http"]
    }
  ],
  "dependency_graph": {
    "chunk:sha256:abc": ["chunk:sha256:def"],
    "chunk:sha256:def": []
  }
}
```

## CLI Usage

### Basic Scraping

```bash
# Scrape a directory with semantic chunking
cadi scrape ./src --strategy semantic --output ./chunks

# Scrape a single file
cadi scrape main.rs --output ./chunks

# Dry run (preview without saving)
cadi scrape ./project --dry-run

# Verbose output
cadi scrape ./project -v
```

### Advanced Options

```bash
# Custom chunk size
cadi scrape ./project \
  --strategy fixed-size \
  --max-chunk-size 102400

# Include/exclude overlap context
cadi scrape ./project \
  --strategy semantic \
  --include-overlap true

# Create hierarchical relationships
cadi scrape ./project \
  --strategy hierarchical \
  --hierarchy true

# Extract API surfaces
cadi scrape ./project \
  --extract-api true

# Detect licenses
cadi scrape ./project \
  --detect-licenses true
```

### Output Formats

```bash
# Table format (default)
cadi scrape ./project --format table

# JSON format
cadi scrape ./project --format json | jq .

# YAML format (future)
cadi scrape ./project --format yaml
```

## Publishing Workflow

### Publishing to Registry

```bash
# Step 1: Scrape repository
cadi scrape ./my-project --output ./chunks

# Step 2: Configure registry and auth
export CADI_REGISTRY_URL="https://registry.example.com"
export CADI_AUTH_TOKEN="your-token"

# Step 3: Publish chunks
cadi publish \
  --registry $CADI_REGISTRY_URL \
  --auth-token $CADI_AUTH_TOKEN \
  --namespace myorg/myproject

# Step 4: Verify in registry
cadi query --name myproject --registry $CADI_REGISTRY_URL
```

### Batch Publishing

```bash
# Publish with deduplication (skip existing chunks)
cadi publish --no-dedup false

# Batch size control
cadi publish --batch-size 10

# Sign chunks during publish
cadi publish --no-sign false
```

## Configuration

### Config File (~/.cadi/scraper.yaml)

```yaml
registry_url: https://registry.example.com
auth_token: YOUR_TOKEN_HERE
namespace: myorg

chunking_strategy: semantic
max_chunk_size: 52428800  # 50MB
include_overlap: true
overlap_size: 500

language_options:
  rust:
    min_semantic_size: 100
    split_by_semantic_boundary: true
    extract_functions: true
    extract_types: true
    extract_classes: true

exclude_patterns:
  - "**/.git"
  - "**/node_modules"
  - "**/target"
  - "**/dist"

create_hierarchy: true
extract_api_surface: true
detect_licenses: true

request_timeout: 30
rate_limit: 10.0  # requests per second
cache_dir: ~/.cadi/scraper-cache
```

### Environment Variables

```bash
# Registry configuration
export CADI_REGISTRY_URL="https://registry.example.com"
export CADI_AUTH_TOKEN="your-token"
export CADI_NAMESPACE="myorg"

# Chunking strategy
export CADI_CHUNKING_STRATEGY="semantic"

# Rate limiting
export CADI_RATE_LIMIT="10"

# Timeout
export CADI_REQUEST_TIMEOUT="30"
```

## Examples

### Example 1: Scrape Todo Suite Project

```bash
cadi scrape ./examples/todo-suite \
  --strategy hierarchical \
  --output ./todo-suite-chunks \
  --extract-api true \
  --detect-licenses true
```

Creates chunks for each component with hierarchy:
- todo-core (functions, types)
- todo-cli (CLI structures)
- todo-web (React components)

### Example 2: Create Custom Organization Repository

```bash
# Scrape all internal projects
for project in internal/*/; do
  cadi scrape "$project" \
    --strategy semantic \
    --output "./org-chunks/$(basename $project)"
done

# Publish to org registry
cadi publish \
  --registry https://registry.myorg.com \
  --namespace myorg
```

### Example 3: Share Open Source Chunks

```bash
# Scrape popular open source projects
cadi scrape https://github.com/tokio-rs/tokio.git \
  --strategy semantic \
  --output ./public-chunks

# Publish to public CADI registry
cadi publish \
  --registry https://registry.cadi.dev \
  --namespace community/tokio
```

## Performance

### Benchmarks (on typical laptop)

| Operation | Time | Notes |
|-----------|------|-------|
| Scrape 100 small files | 2-3s | By-file strategy |
| Scrape 1000 LOC with semantic | 5-10s | Full AST extraction |
| Hierarchical chunking | +15% | Overhead from parent/child tracking |
| Metadata extraction | <1s | Per-chunk |
| Publish 50 chunks | 3-5s | Sequential, 10 req/s |

### Optimization Tips

1. **Use by-file strategy** for large codebases (faster)
2. **Enable deduplication** to skip existing chunks
3. **Batch publishing** with higher batch_size for better throughput
4. **Configure rate_limit** appropriately for your registry
5. **Use hierarchical** only when parent-child relationships needed

## Roadmap

### Phase 1 (MVP - Current)
- ✅ Local file/directory scraping
- ✅ Multi-format parsing (code, markdown, JSON, YAML)
- ✅ Semantic chunking with AST extraction
- ✅ Metadata auto-extraction
- ✅ Batch publishing with authentication
- ✅ CLI command integration

### Phase 2 (Planned)
- 🔄 URL/HTTP scraping with caching
- 🔄 Git repository support
- 🔄 Incremental scraping (track changes)
- 🔄 HTML/Web scraping
- 🔄 PDF parsing
- 🔄 Custom transformer plugins

### Phase 3 (Future)
- 📋 Multi-language support (more languages)
- 📋 Federated scraping (coordinate across registries)
- 📋 Semantic chunking v2 (ML-based boundaries)
- 📋 Compression and delta encoding
- 📋 Browser extension for web scraping

## Troubleshooting

### Common Issues

**Issue: "No chunks to publish"**
```bash
# Solution: Verify chunks were created
ls -la ./chunks
# Check for .json files
```

**Issue: "Authentication failed"**
```bash
# Solution: Verify auth token
echo $CADI_AUTH_TOKEN
# Try with explicit token
cadi publish --auth-token YOUR_TOKEN
```

**Issue: "Chunk already exists at registry"**
```bash
# Solution: Skip deduplication check
cadi publish --no-dedup
```

**Issue: Rate limit exceeded**
```bash
# Solution: Reduce rate limit
export CADI_RATE_LIMIT="5"
cadi scrape ./project
```

## Contributing

Contributions welcome for:
- Additional language support
- New chunking strategies
- Better metadata extraction
- Performance optimizations
- Bug fixes

See main README.md for contribution guidelines.