Read a file, chunk it, write chunk index to knowledge/embeddings/. Args: filepath: Path to the file to index category: Category label (e.g. "docs", "flask", "skill:superpowers") Returns: List of chunk dicts (empty on failure)
(filepath: str, category: str = "docs")
| 67 | |
| 68 | |
| 69 | def index_file(filepath: str, category: str = "docs") -> list: |
| 70 | """ |
| 71 | Read a file, chunk it, write chunk index to knowledge/embeddings/. |
| 72 | |
| 73 | Args: |
| 74 | filepath: Path to the file to index |
| 75 | category: Category label (e.g. "docs", "flask", "skill:superpowers") |
| 76 | |
| 77 | Returns: |
| 78 | List of chunk dicts (empty on failure) |
| 79 | """ |
| 80 | path = Path(filepath) |
| 81 | if not path.exists() or not path.is_file(): |
| 82 | return [] |
| 83 | |
| 84 | # Skip binary files |
| 85 | try: |
| 86 | text = path.read_text(encoding="utf-8", errors="ignore") |
| 87 | except Exception: |
| 88 | return [] |
| 89 | |
| 90 | if not text.strip(): |
| 91 | return [] |
| 92 | |
| 93 | chunks = chunk_text(text) |
| 94 | |
| 95 | # Tag each chunk with source metadata |
| 96 | for chunk in chunks: |
| 97 | chunk["source"] = str(path.resolve()) |
| 98 | chunk["category"] = category |
| 99 | chunk["filename"] = path.name |
| 100 | |
| 101 | # Write chunk index to knowledge/embeddings/<stem>.chunks.json |
| 102 | embed_dir = KB_ROOT / "embeddings" |
| 103 | embed_dir.mkdir(parents=True, exist_ok=True) |
| 104 | |
| 105 | # Use a sanitised stem to avoid collisions (replace / with _) |
| 106 | safe_stem = path.stem.replace("/", "_").replace("\\", "_") |
| 107 | # If multiple files have the same stem, include a hash of the full path |
| 108 | path_hash = hashlib.md5(str(path.resolve()).encode()).hexdigest()[:6] |
| 109 | index_path = embed_dir / f"{safe_stem}_{path_hash}.chunks.json" |
| 110 | |
| 111 | try: |
| 112 | with open(index_path, "w", encoding="utf-8") as f: |
| 113 | json.dump(chunks, f, indent=2) |
| 114 | except Exception: |
| 115 | return [] |
| 116 | |
| 117 | return chunks |
| 118 | |
| 119 | |
| 120 | def index_directory( |
no test coverage detected