fix(documents): preserve complete PDF pages across reads

This commit is contained in:
takiAA authored and Xubin Ren committed 2026-10-18 16:52:47 +08:00
1 parent 42276f4884
commit 5f6e8a6baa
3 files changed
+77 -3

No files matched your search

+5
View File
@@ -446,6 +446,11 @@ class ReadFileTool(_FsTool):
return f"(PDF has no extractable text: {fp})"
result = extraction.text
if extraction.truncated:
result += (
f"\n\n(Page {extraction.end_page + 1} was truncated; "
"the remaining text on this page is not shown.)"
)
if extraction.end_page < extraction.total_pages - 1:
next_start = extraction.end_page + 2
next_end = min(extraction.end_page + 1 + self._MAX_PDF_PAGES, extraction.total_pages)
+10 -2
View File
@@ -77,6 +77,7 @@ class PdfExtraction:
total_pages: int
start_page: int
end_page: int
truncated: bool = False
@dataclass(frozen=True, slots=True)
@@ -485,10 +486,17 @@ def extract_pdf_pages(
f"{_MAX_PDF_CONTENT_STREAM_SIZE // (1024 * 1024)} MB limit"
)
text = (page.extract_text() or "").strip()
if text and not collector.add(f"--- Page {index + 1} ---\n{text}", separator="\n\n"):
if not text:
continue
page_text = f"--- Page {index + 1} ---\n{text}"
if collector.parts and collector.length + 2 + len(page_text) > max_chars:
# Leave this page for the next read instead of returning only its prefix.
end = index - 1
break
if not collector.add(page_text, separator="\n\n"):
end = index
break
return PdfExtraction(collector.render(), total_pages, start, end)
return PdfExtraction(collector.render(), total_pages, start, end, truncated=collector.truncated)
def _parse_pdf_page_range(pages: str | None, total_pages: int) -> tuple[int, int]:
+62 -1
View File
@@ -212,9 +212,70 @@ class TestReadDedupSessionIsolation:
class TestReadPdf:
@pytest.fixture()
def tool(self, tmp_path):
def tool(self, tmp_path, monkeypatch):
monkeypatch.setattr("nanobot.config.loader._current_config_path", tmp_path / "config.json")
return ReadFileTool(workspace=tmp_path)
@pytest.mark.asyncio
@pytest.mark.parametrize("page_count", [2, 3])
async def test_pdf_character_limit_preserves_pages_across_continuation(
self, tool, tmp_path, monkeypatch, page_count,
):
import re
fitz = pytest.importorskip("pymupdf")
pdf_path = tmp_path / "report.pdf"
doc = fitz.open()
markers = []
for number in range(1, page_count + 1):
marker = f"END_OF_PAGE_{number}"
markers.append(marker)
page = doc.new_page()
page.insert_text((72, 72), "Report details " * 4 + "\n" + marker)
doc.save(pdf_path)
doc.close()
monkeypatch.setattr(tool, "_MAX_CHARS", 130)
outputs = []
pages = None
for _ in range(page_count):
result = await tool.execute(path=str(pdf_path), pages=pages)
outputs.append(result)
continuation = re.search(r"Use pages='([^']+)'", result)
if continuation is None:
break
pages = continuation[1]
combined = "\n".join(outputs)
for marker in markers:
assert combined.count(marker) == 1
assert "truncated" not in combined
assert "Use pages=" not in outputs[-1]
@pytest.mark.asyncio
async def test_pdf_oversized_page_reports_unreturned_text(
self, tool, tmp_path, monkeypatch,
):
fitz = pytest.importorskip("pymupdf")
pdf_path = tmp_path / "dense.pdf"
doc = fitz.open()
page = doc.new_page()
page.insert_text((72, 72), "\n".join(["Dense page content " * 3] * 8))
page = doc.new_page()
page.insert_text((72, 72), "Second page complete")
doc.save(pdf_path)
doc.close()
monkeypatch.setattr(tool, "_MAX_CHARS", 130)
result = await tool.execute(path=str(pdf_path))
assert "Page 1 was truncated" in result
assert "remaining text on this page is not shown" in result
assert "Use pages='2-2'" in result
second = await tool.execute(path=str(pdf_path), pages="2")
assert "Second page complete" in second
assert "truncated" not in second
@pytest.mark.asyncio
async def test_pdf_returns_text_content(self, tool, tmp_path):
fitz = pytest.importorskip("fitz")