Skip to content

Commit 0ff97b3

Browse files
authored
Merge pull request #3 from BinarCode/feature/watch_mode_with_auto_sync
feat: Watch Mode with Auto-Sync
2 parents 1d30697 + 8fc7033 commit 0ff97b3

6 files changed

Lines changed: 474 additions & 4 deletions

File tree

CLAUDE.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,25 @@ aidocs mcp docs/ # Start MCP server for docs directory
3030

3131
# PDF export
3232
aidocs export-pdf docs/page.md
33+
34+
# Watch mode (auto-sync on file changes)
35+
aidocs watch # Watch docs/ and auto-chunk on changes
36+
aidocs watch --with-vectors # Also generate embeddings
3337
```
3438

3539
## Architecture
3640

3741
```
3842
src/aidocs_cli/
3943
├── __init__.py # Version and entry point
40-
├── cli.py # Typer CLI commands (init, check, serve, rag-*, export-pdf)
44+
├── cli.py # Typer CLI commands (init, check, serve, rag-*, export-pdf, watch)
4145
├── installer.py # Copies templates to target project (.claude/commands/, .claude/workflows/)
4246
├── chunker.py # Splits markdown at ## headings for RAG
4347
├── embeddings.py # OpenAI embeddings + SQL generation for pgvector
4448
├── server.py # MkDocs config generation and nav discovery
4549
├── pdf_exporter.py # Markdown→HTML→PDF with Chrome/Playwright
4650
├── mcp_server.py # MCP server exposing docs via tools (list, search, read)
51+
├── watcher.py # File system watcher for auto-sync (watchdog + Rich Live)
4752
└── templates/
4853
├── commands/docs/ # Slash command definitions (*.md)
4954
└── workflows/ # Workflow implementations per command
@@ -78,7 +83,7 @@ Version is defined in two places (keep in sync):
7883

7984
## Dependencies
8085

81-
Core: typer, rich, httpx, mkdocs, mkdocs-material, pyyaml, mcp
86+
Core: typer, rich, httpx, mkdocs, mkdocs-material, pyyaml, mcp, watchdog, python-dotenv
8287

8388
Python 3.11+ required. Build system uses hatchling.
8489

README.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,65 @@ Once configured, Claude Code can use these tools automatically. You can prompt:
489489
| Empty search results | Ensure docs directory has `.md` files |
490490
| Slow searches | Run `aidocs rag` to pre-chunk files |
491491

492+
### `aidocs watch`
493+
494+
Watch documentation directory for changes and automatically re-chunk files and regenerate embeddings.
495+
496+
```bash
497+
aidocs watch # Watch docs/ with auto-embeddings
498+
aidocs watch --with-vectors # Generate also embeddings
499+
aidocs watch --debounce 5 # Wait 5 seconds before processing
500+
aidocs watch docs/users # Watch specific subdirectory
501+
```
502+
503+
**Options:**
504+
| Option | Description |
505+
|--------|-------------|
506+
| `--with-vectors` | Include embedding generation |
507+
| `--debounce, -d` | Seconds to wait after last change (default: 10) |
508+
| `--table, -t` | Target table name for embeddings (default: `doc_embeddings`) |
509+
510+
**What it does:**
511+
1. Monitors the docs directory for `.md` file changes
512+
2. Debounces rapid changes (waits 10 seconds after last edit by default)
513+
3. Re-chunks modified files automatically
514+
4. Generates embeddings if `OPENAI_API_KEY` is set (use `--with-vectors` to enable)
515+
5. Updates manifest and sync state
516+
517+
**Real-time display:**
518+
```
519+
╭─────────────────────────────────────────╮
520+
│ Watching docs/ │
521+
│ │
522+
│ Last update: 14:32:05 │
523+
│ Files: 12 | Chunks: 45 | Embeddings: 45 │
524+
│ │
525+
│ Embeddings: enabled │
526+
│ │
527+
│ Recent: │
528+
│ ✓ users/index.md (3 chunks) │
529+
│ ✓ api/auth.md (5 chunks) │
530+
│ │
531+
│ Press Ctrl+C to stop │
532+
╰─────────────────────────────────────────╯
533+
```
534+
535+
**Use cases:**
536+
- Keep chunks updated while editing documentation
537+
- Auto-sync embeddings during documentation sprints
538+
- Run alongside `aidocs serve` for a complete dev workflow
539+
540+
**Example workflow:**
541+
```bash
542+
# Terminal 1: Watch for changes
543+
aidocs watch
544+
545+
# Terminal 2: Serve documentation
546+
aidocs serve
547+
548+
# Edit docs in your editor - changes auto-sync!
549+
```
550+
492551
## Slash Commands
493552

494553
After running `aidocs init`, these commands are available in Claude Code:

pyproject.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "aidocs"
3-
version = "0.16.0"
3+
version = "0.17.0"
44
description = "AI-powered documentation generator for web applications. Install docs commands into your Claude Code project."
55
readme = "README.md"
66
license = { text = "MIT" }
@@ -26,6 +26,8 @@ dependencies = [
2626
"mkdocs-material>=9.5.0",
2727
"pyyaml>=6.0",
2828
"mcp>=1.0.0",
29+
"watchdog>=4.0.0",
30+
"python-dotenv>=1.0.0",
2931
]
3032

3133
[project.scripts]

src/aidocs_cli/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""AI-powered documentation generator CLI for Claude Code projects."""
22

3-
__version__ = "0.16.0"
3+
__version__ = "0.17.0"
44

55
from .cli import app
66

src/aidocs_cli/cli.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -843,5 +843,68 @@ def mcp_command(
843843
asyncio.run(run_server(target_dir))
844844

845845

846+
@app.command("watch")
847+
def watch(
848+
docs_dir: Optional[str] = typer.Argument(
849+
"docs",
850+
help="Directory containing documentation to watch.",
851+
),
852+
with_vectors: bool = typer.Option(
853+
False,
854+
"--with-vectors",
855+
help="Enable embedding generation (requires OPENAI_API_KEY).",
856+
),
857+
debounce: float = typer.Option(
858+
10.0,
859+
"--debounce",
860+
"-d",
861+
help="Seconds to wait after last change before processing.",
862+
),
863+
table: str = typer.Option(
864+
"doc_embeddings",
865+
"--table",
866+
"-t",
867+
help="Target table name for embeddings.",
868+
),
869+
) -> None:
870+
"""Watch documentation directory and auto-sync on changes.
871+
872+
Monitors the docs directory for markdown file changes and automatically:
873+
- Re-chunks modified files
874+
- Generates embeddings (if --with-vectors and OPENAI_API_KEY is set)
875+
- Updates manifest and sync state
876+
877+
Uses debouncing to batch rapid changes (default: 10 seconds).
878+
879+
Examples:
880+
aidocs watch # Watch docs/, chunk only
881+
aidocs watch --with-vectors # Also generate embeddings
882+
aidocs watch --debounce 5 # Wait 5 seconds before processing
883+
aidocs watch docs/users # Watch specific subdirectory
884+
"""
885+
from .watcher import watch_docs
886+
887+
target_dir = Path(docs_dir)
888+
889+
if not target_dir.exists():
890+
console.print(f"[red]Error: Directory not found: {docs_dir}[/red]")
891+
raise typer.Exit(1)
892+
893+
if not target_dir.is_dir():
894+
console.print(f"[red]Error: Not a directory: {docs_dir}[/red]")
895+
raise typer.Exit(1)
896+
897+
try:
898+
watch_docs(
899+
target_dir,
900+
with_vectors=with_vectors,
901+
debounce_seconds=debounce,
902+
table_name=table,
903+
)
904+
except Exception as e:
905+
console.print(f"[red]Error: {e}[/red]")
906+
raise typer.Exit(1)
907+
908+
846909
if __name__ == "__main__":
847910
app()

0 commit comments

Comments
 (0)