Repository navigation
Expand file tree
/
Copy pathutils.py
More file actions
64 lines (48 loc) · 2.3 KB
/
Copy pathutils.py
File metadata and controls
64 lines (48 loc) · 2.3 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import re
from pathlib import Path
import config
OUTPUT_DIR = Path(__file__).parent / "output"
USER_PROFILE_PATH = Path(__file__).parent / "docs" / "user.md"
def _strip_noise(text: str) -> str:
"""Remove <img> tags and mapview blocks; preserve frontmatter so ratings and metadata reach the LLM."""
text = re.sub(r'<img\b[^>]*/?>(?:\s*<br\s*/?>)?', '', text)
text = re.sub(r'```mapview.*?```', '', text, flags=re.DOTALL)
return text.strip()
def clean_content(text: str) -> str:
"""Full strip: frontmatter + noise. Used only for embeddings — raw YAML fields degrade vector quality."""
text = re.sub(r'^---\n.*?\n---\n?', '', text, count=1, flags=re.DOTALL)
return _strip_noise(text)
def collect_md_files(folder: str) -> list[str]:
"""Recursively collect all .md files under folder, excluding *-summary output files."""
base = Path(folder)
if not base.exists():
raise FileNotFoundError(f"Folder not found: {folder}")
return sorted(
str(p) for p in base.rglob("*.md")
if not p.stem.endswith("-summary")
)
def read_file(path: str) -> str:
"""Read a note, stripping visual noise but keeping frontmatter (which contains ratings and metadata)."""
return _strip_noise(Path(path).read_text(encoding="utf-8", errors="replace"))
def query_llm(prompt: str) -> str:
"""Send a single prompt to the LLM and return the response with <think> blocks removed."""
response = config.client.chat(
model=config.MODEL_NAME,
messages=[{"role": "user", "content": prompt}]
)
raw = response.message.content.strip()
result = re.sub(r"<think>.*?</think>", "", raw, flags=re.DOTALL).strip()
config.dbg(f"Response ({len(result)} chars): {result[:200].replace(chr(10), ' ')} ...")
return result
def read_user_profile() -> str:
"""Load user.md if it exists; returns an empty string silently if the file is missing."""
if USER_PROFILE_PATH.exists():
return USER_PROFILE_PATH.read_text(encoding="utf-8")
return ""
def write_output(filename: str, content: str) -> None:
"""Write content to the output directory and print it to stdout."""
OUTPUT_DIR.mkdir(exist_ok=True)
out_path = OUTPUT_DIR / filename
out_path.write_text(content, encoding="utf-8")
print(content)
config.log(f"Output written to {out_path}")