Compile a long (PageIndex) document's concepts and index. The summary page is already written by the indexer. This function generates concept pages and updates the index.
(
doc_name: str,
summary_path: Path,
doc_id: str,
kb_dir: Path,
model: str,
doc_description: str = "",
max_concurrency: int = DEFAULT_COMPILE_CONCURRENCY,
)
| 2255 | |
| 2256 | |
| 2257 | async def compile_long_doc( |
| 2258 | doc_name: str, |
| 2259 | summary_path: Path, |
| 2260 | doc_id: str, |
| 2261 | kb_dir: Path, |
| 2262 | model: str, |
| 2263 | doc_description: str = "", |
| 2264 | max_concurrency: int = DEFAULT_COMPILE_CONCURRENCY, |
| 2265 | ) -> None: |
| 2266 | """Compile a long (PageIndex) document's concepts and index. |
| 2267 | |
| 2268 | The summary page is already written by the indexer. This function |
| 2269 | generates concept pages and updates the index. |
| 2270 | """ |
| 2271 | from openkb.config import load_config |
| 2272 | |
| 2273 | openkb_dir = kb_dir / ".openkb" |
| 2274 | config = load_config(openkb_dir / "config.yaml") |
| 2275 | language: str = config.get("language", "en") |
| 2276 | entity_types = resolve_entity_types(config) |
| 2277 | |
| 2278 | wiki_dir = kb_dir / "wiki" |
| 2279 | schema_md = get_agents_md(wiki_dir) |
| 2280 | summary_content = summary_path.read_text(encoding="utf-8") |
| 2281 | |
| 2282 | # Backfill OKF fields on the indexer-written summary. Idempotent: set |
| 2283 | # description before type so that when both keys are missing the prepends |
| 2284 | # leave `type` first (canonical order); only rewrite when content changed. |
| 2285 | fm_parts = frontmatter.split(summary_content) |
| 2286 | if fm_parts is not None: |
| 2287 | fm_block, body = fm_parts |
| 2288 | if doc_description: |
| 2289 | fm_block = _set_fm_line(fm_block, "description", doc_description) |
| 2290 | fm_block = _set_fm_line(fm_block, "type", "Summary") |
| 2291 | updated = fm_block + body |
| 2292 | if updated != summary_content: |
| 2293 | summary_content = updated |
| 2294 | atomic_write_text(summary_path, summary_content) |
| 2295 | |
| 2296 | # Base context A. cache_control marker on the doc message creates a |
| 2297 | # cache breakpoint covering (system + doc) for every concept call. |
| 2298 | system_msg = { |
| 2299 | "role": "system", |
| 2300 | "content": _SYSTEM_TEMPLATE.format( |
| 2301 | schema_md=schema_md, |
| 2302 | language=language, |
| 2303 | ), |
| 2304 | } |
| 2305 | doc_msg = { |
| 2306 | "role": "user", |
| 2307 | "content": _cached_text( |
| 2308 | _LONG_DOC_SUMMARY_USER.format( |
| 2309 | doc_name=doc_name, |
| 2310 | doc_id=doc_id, |
| 2311 | content=summary_content, |
| 2312 | ) |
| 2313 | ), |
| 2314 | } |