fix: exact md page selection + restore CHATGPT_API_KEY alias

Address PR #272 review:

- get_md_page_content / retrieve._get_md_page_content returned every node
  whose line_num fell in [min(pages), max(pages)], so a non-contiguous spec
  like "5,100" over-fetched everything in between. Match the exact requested
  line numbers instead, mirroring the PDF path. (Same bug as #280.)
- Restore the CHATGPT_API_KEY -> OPENAI_API_KEY backward-compat alias dropped
  when pageindex/utils.py became a re-export shim; users with only
  CHATGPT_API_KEY set would otherwise fail auth after upgrading. It now runs
  in __init__.py right after load_dotenv.

Adds regression tests for both.

Claude-Session: https://claude.ai/code/session_01Kx5DgKbhK1N8autqXH8SmS
This commit is contained in:
mountain 2026-07-08 17:33:41 +08:00
parent 2d46d68052
commit 2a69c76b7f
5 changed files with 91 additions and 6 deletions

View file

@ -56,16 +56,19 @@ def _get_pdf_page_content(doc_info: dict, page_nums: list[int]) -> list[dict]:
def _get_md_page_content(doc_info: dict, page_nums: list[int]) -> list[dict]:
"""
For Markdown documents, 'pages' are line numbers.
Find nodes whose line_num falls within [min(page_nums), max(page_nums)] and return their text.
Return only the nodes whose line_num is one of ``page_nums`` (exact match),
not the whole [min(page_nums), max(page_nums)] range.
"""
min_line, max_line = min(page_nums), max(page_nums)
if not page_nums:
return []
wanted = set(page_nums)
results = []
seen = set()
def _traverse(nodes):
for node in nodes:
ln = node.get('line_num')
if ln and min_line <= ln <= max_line and ln not in seen:
if ln in wanted and ln not in seen:
seen.add(ln)
results.append({'page': ln, 'content': node.get('text', '')})
if node.get('nodes'):