Skip to content

Commit a172c43

Browse files
committed
feat: store brief in frontmatter of summary and concept pages
1 parent b6ce04e commit a172c43

2 files changed

Lines changed: 75 additions & 15 deletions

File tree

openkb/agent/compiler.py

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,9 @@ def _read_wiki_context(wiki_dir: Path) -> tuple[str, list[str]]:
243243
def _read_concept_briefs(wiki_dir: Path) -> str:
244244
"""Read existing concept pages and return compact one-line summaries.
245245
246-
For each concept, skips YAML frontmatter, takes the first 150 chars of the
247-
body (newlines collapsed to spaces), and formats as ``- {slug}: {brief}``.
246+
For each concept, reads the ``brief:`` field from YAML frontmatter if
247+
present; otherwise falls back to truncating the first 150 chars of the body
248+
(newlines collapsed to spaces). Formats each as ``- {slug}: {brief}``.
248249
249250
Returns "(none yet)" if the concepts directory is missing or empty.
250251
"""
@@ -259,16 +260,23 @@ def _read_concept_briefs(wiki_dir: Path) -> str:
259260
lines: list[str] = []
260261
for path in md_files:
261262
text = path.read_text(encoding="utf-8")
262-
# Strip YAML frontmatter if present
263+
brief = ""
264+
body = text
263265
if text.startswith("---"):
264266
end = text.find("---", 3)
265267
if end != -1:
266-
text = text[end + 3:]
267-
body = text.strip().replace("\n", " ")
268-
brief = body[:150]
269-
lines.append(f"- {path.stem}: {brief}")
268+
fm = text[:end + 3]
269+
body = text[end + 3:]
270+
for line in fm.split("\n"):
271+
if line.startswith("brief:"):
272+
brief = line[len("brief:"):].strip()
273+
break
274+
if not brief:
275+
brief = body.strip().replace("\n", " ")[:150]
276+
if brief:
277+
lines.append(f"- {path.stem}: {brief}")
270278

271-
return "\n".join(lines)
279+
return "\n".join(lines) or "(none yet)"
272280

273281

274282
def _find_source_filename(doc_name: str, kb_dir: Path) -> str:
@@ -281,11 +289,14 @@ def _find_source_filename(doc_name: str, kb_dir: Path) -> str:
281289
return f"{doc_name}.pdf"
282290

283291

284-
def _write_summary(wiki_dir: Path, doc_name: str, source_file: str, summary: str) -> None:
292+
def _write_summary(wiki_dir: Path, doc_name: str, source_file: str, summary: str, brief: str = "") -> None:
285293
"""Write summary page with frontmatter."""
286294
summaries_dir = wiki_dir / "summaries"
287295
summaries_dir.mkdir(parents=True, exist_ok=True)
288-
frontmatter = f"---\nsources: [{source_file}]\n---\n\n"
296+
fm_lines = [f"sources: [{source_file}]"]
297+
if brief:
298+
fm_lines.append(f"brief: {brief}")
299+
frontmatter = "---\n" + "\n".join(fm_lines) + "\n---\n\n"
289300
(summaries_dir / f"{doc_name}.md").write_text(frontmatter + summary, encoding="utf-8")
290301

291302

@@ -298,7 +309,7 @@ def _sanitize_concept_name(name: str) -> str:
298309
return sanitized or "unnamed-concept"
299310

300311

301-
def _write_concept(wiki_dir: Path, name: str, content: str, source_file: str, is_update: bool) -> None:
312+
def _write_concept(wiki_dir: Path, name: str, content: str, source_file: str, is_update: bool, brief: str = "") -> None:
302313
"""Write or update a concept page, managing the sources frontmatter."""
303314
concepts_dir = wiki_dir / "concepts"
304315
concepts_dir.mkdir(parents=True, exist_ok=True)
@@ -324,9 +335,22 @@ def _write_concept(wiki_dir: Path, name: str, content: str, source_file: str, is
324335
else:
325336
existing = f"---\nsources: [{source_file}]\n---\n\n" + existing
326337
existing += f"\n\n{content}"
338+
if brief and existing.startswith("---"):
339+
end = existing.find("---", 3)
340+
if end != -1:
341+
fm = existing[:end + 3]
342+
body = existing[end + 3:]
343+
if "brief:" in fm:
344+
fm = re.sub(r"brief:.*", f"brief: {brief}", fm)
345+
else:
346+
fm = fm.replace("---\n", f"---\nbrief: {brief}\n", 1)
347+
existing = fm + body
327348
path.write_text(existing, encoding="utf-8")
328349
else:
329-
frontmatter = f"---\nsources: [{source_file}]\n---\n\n"
350+
fm_lines = [f"sources: [{source_file}]"]
351+
if brief:
352+
fm_lines.append(f"brief: {brief}")
353+
frontmatter = "---\n" + "\n".join(fm_lines) + "\n---\n\n"
330354
path.write_text(frontmatter + content, encoding="utf-8")
331355

332356

tests/test_compiler.py

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,25 +78,61 @@ class TestWriteSummary:
7878
def test_writes_with_frontmatter(self, tmp_path):
7979
wiki = tmp_path / "wiki"
8080
wiki.mkdir()
81-
_write_summary(wiki, "my-doc", "my-doc.pdf", "# Summary\n\nContent here.")
81+
_write_summary(wiki, "my-doc", "my-doc.pdf", "# Summary\n\nContent here.", brief="Introduces transformers")
8282
path = wiki / "summaries" / "my-doc.md"
8383
assert path.exists()
8484
text = path.read_text()
8585
assert "sources: [my-doc.pdf]" in text
86+
assert "brief: Introduces transformers" in text
8687
assert "# Summary" in text
8788

89+
def test_writes_without_brief(self, tmp_path):
90+
wiki = tmp_path / "wiki"
91+
wiki.mkdir()
92+
_write_summary(wiki, "my-doc", "my-doc.pdf", "# Summary\n\nContent here.")
93+
path = wiki / "summaries" / "my-doc.md"
94+
text = path.read_text()
95+
assert "sources: [my-doc.pdf]" in text
96+
assert "brief:" not in text
97+
8898

8999
class TestWriteConcept:
90-
def test_new_concept(self, tmp_path):
100+
def test_new_concept_with_brief(self, tmp_path):
91101
wiki = tmp_path / "wiki"
92102
wiki.mkdir()
93-
_write_concept(wiki, "attention", "# Attention\n\nDetails.", "paper.pdf", False)
103+
_write_concept(wiki, "attention", "# Attention\n\nDetails.", "paper.pdf", False, brief="Mechanism for selective focus")
94104
path = wiki / "concepts" / "attention.md"
95105
assert path.exists()
96106
text = path.read_text()
97107
assert "sources: [paper.pdf]" in text
108+
assert "brief: Mechanism for selective focus" in text
98109
assert "# Attention" in text
99110

111+
def test_new_concept_without_brief(self, tmp_path):
112+
wiki = tmp_path / "wiki"
113+
wiki.mkdir()
114+
_write_concept(wiki, "attention", "# Attention\n\nDetails.", "paper.pdf", False)
115+
path = wiki / "concepts" / "attention.md"
116+
assert path.exists()
117+
text = path.read_text()
118+
assert "sources: [paper.pdf]" in text
119+
assert "brief:" not in text
120+
121+
def test_update_concept_updates_brief(self, tmp_path):
122+
wiki = tmp_path / "wiki"
123+
concepts = wiki / "concepts"
124+
concepts.mkdir(parents=True)
125+
(concepts / "attention.md").write_text(
126+
"---\nsources: [paper1.pdf]\nbrief: Old brief\n---\n\n# Attention\n\nOld content.",
127+
encoding="utf-8",
128+
)
129+
_write_concept(wiki, "attention", "New info.", "paper2.pdf", True, brief="Updated brief")
130+
text = (concepts / "attention.md").read_text()
131+
assert "paper2.pdf" in text
132+
assert "paper1.pdf" in text
133+
assert "brief: Updated brief" in text
134+
assert "Old brief" not in text
135+
100136
def test_update_concept_appends_source(self, tmp_path):
101137
wiki = tmp_path / "wiki"
102138
concepts = wiki / "concepts"

0 commit comments

Comments
 (0)