()
| 175 | |
| 176 | |
| 177 | def build_state() -> dict[str, Any]: |
| 178 | chapters = afml_list_chapters()["chapters"] |
| 179 | chapter_counts = {item["chapter"]: item["chunk_count"] for item in chapters} |
| 180 | module_map = load_module_map() |
| 181 | |
| 182 | chapter_nodes: list[dict[str, Any]] = [] |
| 183 | for blueprint in BLUEPRINT: |
| 184 | chapter_search = afml_search(query=blueprint.query, chapter=blueprint.chapter, top_k=5, include_text=False) |
| 185 | chapter_hit_raw = pick_hit_in_chapter(chapter_search["results"], blueprint.chapter) |
| 186 | chapter_hit = summarize_hit(chapter_hit_raw) if chapter_hit_raw else {} |
| 187 | |
| 188 | module_nodes: list[dict[str, Any]] = [] |
| 189 | for module in blueprint.modules: |
| 190 | slug = module_map.get(module) |
| 191 | if slug is None: |
| 192 | slug = module.replace("::", "-").replace("_", "-") |
| 193 | |
| 194 | module_query = MODULE_QUERIES.get(module, module.replace("_", " ")) |
| 195 | module_search = afml_search(query=module_query, chapter=blueprint.chapter, top_k=5, include_text=False) |
| 196 | module_hit_raw = pick_hit_in_chapter(module_search["results"], blueprint.chapter) |
| 197 | module_hit = summarize_hit(module_hit_raw) if module_hit_raw else {} |
| 198 | |
| 199 | section_id = f"{blueprint.chapter.lower().replace(' ', '-')}-{module}" |
| 200 | module_nodes.append( |
| 201 | { |
| 202 | "id": section_id, |
| 203 | "module": module, |
| 204 | "slug": slug, |
| 205 | "status": "pending", |
| 206 | "afml_anchor": module_hit, |
| 207 | } |
| 208 | ) |
| 209 | |
| 210 | prompt_body = chapter_prompt(blueprint.chapter, module, slug, module_query, chapter_hit, module_hit) |
| 211 | PROMPTS_DIR.mkdir(parents=True, exist_ok=True) |
| 212 | (PROMPTS_DIR / f"{section_id}.md").write_text(prompt_body, encoding="utf-8") |
| 213 | |
| 214 | chapter_nodes.append( |
| 215 | { |
| 216 | "chapter": blueprint.chapter, |
| 217 | "theme": blueprint.theme, |
| 218 | "query": blueprint.query, |
| 219 | "chunk_count": chapter_counts.get(blueprint.chapter, 0), |
| 220 | "status": "pending", |
| 221 | "afml_anchor": chapter_hit, |
| 222 | "sections": module_nodes, |
| 223 | } |
| 224 | ) |
| 225 | |
| 226 | return { |
| 227 | "generated_at": now_iso(), |
| 228 | "updated_at": now_iso(), |
| 229 | "state_version": 1, |
| 230 | "overview": { |
| 231 | "astro_module_data": str(MODULE_DOCS_PATH.relative_to(REPO_ROOT)), |
| 232 | "module_page": "docs-site/src/pages/module/[slug].astro", |
| 233 | "subjects_page": "docs-site/src/pages/modules.astro", |
| 234 | }, |
no test coverage detected