File size: 1,195 Bytes
40c0886
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Knowledge base loader.

Provides a simple API for loading the markdown knowledge entries that ship
with the package. Entries are stored as ``.md`` files in
``unity_agent/knowledge/entries/``.
"""

from __future__ import annotations

from importlib import resources
from pathlib import Path
from typing import Dict, List, Optional


def entries_dir() -> Path:
    """Return the absolute path to the knowledge entries directory."""
    return Path(__file__).resolve().parent / "entries"


def list_entries() -> List[str]:
    """Return a list of knowledge entry slugs (file names without .md)."""
    d = entries_dir()
    if not d.exists():
        return []
    return sorted(p.stem for p in d.glob("*.md"))


def load_entry(slug: str) -> Optional[str]:
    """Return the markdown body of a knowledge entry, or None if missing."""
    p = entries_dir() / f"{slug}.md"
    if not p.exists():
        return None
    return p.read_text(encoding="utf-8")


def load_all() -> Dict[str, str]:
    """Return a dict mapping slug -> markdown body for every entry."""
    return {slug: load_entry(slug) for slug in list_entries()}


__all__ = ["entries_dir", "list_entries", "load_entry", "load_all"]