Skip to content

Commit ca23912

Browse files
committed
feat: add briefs to index.md entries and read from frontmatter
1 parent a172c43 commit ca23912

2 files changed

Lines changed: 66 additions & 13 deletions

File tree

openkb/agent/compiler.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -439,8 +439,19 @@ def _backlink_concepts(wiki_dir: Path, doc_name: str, concept_slugs: list[str])
439439
path.write_text(text, encoding="utf-8")
440440

441441

442-
def _update_index(wiki_dir: Path, doc_name: str, concept_names: list[str]) -> None:
443-
"""Append document and concept entries to index.md."""
442+
def _update_index(
443+
wiki_dir: Path, doc_name: str, concept_names: list[str],
444+
doc_brief: str = "", concept_briefs: dict[str, str] | None = None,
445+
) -> None:
446+
"""Append document and concept entries to index.md.
447+
448+
When ``doc_brief`` or entries in ``concept_briefs`` are provided, entries
449+
are written as ``- [[link]] — brief text``. Existing entries are detected
450+
by the link part only, so updating a brief on a re-compile works correctly.
451+
"""
452+
if concept_briefs is None:
453+
concept_briefs = {}
454+
444455
index_path = wiki_dir / "index.md"
445456
if not index_path.exists():
446457
index_path.write_text(
@@ -450,14 +461,20 @@ def _update_index(wiki_dir: Path, doc_name: str, concept_names: list[str]) -> No
450461

451462
text = index_path.read_text(encoding="utf-8")
452463

453-
doc_entry = f"- [[summaries/{doc_name}]]"
454-
if doc_entry not in text:
464+
doc_link = f"[[summaries/{doc_name}]]"
465+
if doc_link not in text:
466+
doc_entry = f"- {doc_link}"
467+
if doc_brief:
468+
doc_entry += f" — {doc_brief}"
455469
if "## Documents" in text:
456470
text = text.replace("## Documents\n", f"## Documents\n{doc_entry}\n", 1)
457471

458472
for name in concept_names:
459-
concept_entry = f"- [[concepts/{name}]]"
460-
if concept_entry not in text:
473+
concept_link = f"[[concepts/{name}]]"
474+
if concept_link not in text:
475+
concept_entry = f"- {concept_link}"
476+
if name in concept_briefs:
477+
concept_entry += f" — {concept_briefs[name]}"
461478
if "## Concepts" in text:
462479
text = text.replace("## Concepts\n", f"## Concepts\n{concept_entry}\n", 1)
463480

tests/test_compiler.py

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -149,30 +149,44 @@ def test_update_concept_appends_source(self, tmp_path):
149149

150150

151151
class TestUpdateIndex:
152-
def test_appends_entries(self, tmp_path):
152+
def test_appends_entries_with_briefs(self, tmp_path):
153153
wiki = tmp_path / "wiki"
154154
wiki.mkdir()
155155
(wiki / "index.md").write_text(
156156
"# Index\n\n## Documents\n\n## Concepts\n\n## Explorations\n",
157157
encoding="utf-8",
158158
)
159-
_update_index(wiki, "my-doc", ["attention", "transformer"])
159+
_update_index(wiki, "my-doc", ["attention", "transformer"],
160+
doc_brief="Introduces transformers",
161+
concept_briefs={"attention": "Focus mechanism", "transformer": "NN architecture"})
160162
text = (wiki / "index.md").read_text()
161-
assert "[[summaries/my-doc]]" in text
162-
assert "[[concepts/attention]]" in text
163-
assert "[[concepts/transformer]]" in text
163+
assert "[[summaries/my-doc]] — Introduces transformers" in text
164+
assert "[[concepts/attention]] — Focus mechanism" in text
165+
assert "[[concepts/transformer]] — NN architecture" in text
164166

165167
def test_no_duplicates(self, tmp_path):
166168
wiki = tmp_path / "wiki"
167169
wiki.mkdir()
168170
(wiki / "index.md").write_text(
169-
"# Index\n\n## Documents\n- [[summaries/my-doc]]\n\n## Concepts\n",
171+
"# Index\n\n## Documents\n- [[summaries/my-doc]] — Old brief\n\n## Concepts\n",
170172
encoding="utf-8",
171173
)
172-
_update_index(wiki, "my-doc", [])
174+
_update_index(wiki, "my-doc", [], doc_brief="New brief")
173175
text = (wiki / "index.md").read_text()
174176
assert text.count("[[summaries/my-doc]]") == 1
175177

178+
def test_backwards_compat_no_briefs(self, tmp_path):
179+
wiki = tmp_path / "wiki"
180+
wiki.mkdir()
181+
(wiki / "index.md").write_text(
182+
"# Index\n\n## Documents\n\n## Concepts\n\n## Explorations\n",
183+
encoding="utf-8",
184+
)
185+
_update_index(wiki, "my-doc", ["attention"])
186+
text = (wiki / "index.md").read_text()
187+
assert "[[summaries/my-doc]]" in text
188+
assert "[[concepts/attention]]" in text
189+
176190

177191
class TestReadWikiContext:
178192
def test_empty_wiki(self, tmp_path):
@@ -257,6 +271,28 @@ def test_sorted_alphabetically(self, tmp_path):
257271
slugs = [line.split(":")[0].lstrip("- ") for line in lines]
258272
assert slugs == ["apple", "mango", "zebra"]
259273

274+
def test_reads_brief_from_frontmatter(self, tmp_path):
275+
wiki = tmp_path / "wiki"
276+
concepts = wiki / "concepts"
277+
concepts.mkdir(parents=True)
278+
(concepts / "attention.md").write_text(
279+
"---\nsources: [paper.pdf]\nbrief: Selective focus mechanism\n---\n\n# Attention\n\nLong content...",
280+
encoding="utf-8",
281+
)
282+
result = _read_concept_briefs(wiki)
283+
assert "- attention: Selective focus mechanism" in result
284+
285+
def test_falls_back_to_body_truncation(self, tmp_path):
286+
wiki = tmp_path / "wiki"
287+
concepts = wiki / "concepts"
288+
concepts.mkdir(parents=True)
289+
(concepts / "old.md").write_text(
290+
"---\nsources: [paper.pdf]\n---\n\nOld concept without brief field.",
291+
encoding="utf-8",
292+
)
293+
result = _read_concept_briefs(wiki)
294+
assert "- old: Old concept without brief field." in result
295+
260296

261297
class TestBacklinkSummary:
262298
def test_adds_missing_concept_links(self, tmp_path):

0 commit comments

Comments
 (0)