fix: return actionable error for malformed page ranges in local tool

The local get_page_content agent tool only converted DocumentNotFoundError
into a JSON error; a malformed page spec ("all", "5-") let parse_pages'
ValueError surface as the agent SDK's generic non-fatal tool-failure text
("An error occurred... invalid literal for int()"), which the model can't
act on. Catch (ValueError, AttributeError) and return the same actionable
"Invalid pages format: ... Use '5-7', '3,8', or '12'" message the legacy
retrieval tool already gives, so the model can retry with a valid range.
This commit is contained in:
mountain 2026-07-09 19:22:04 +08:00
parent 91e016d2e4
commit 56fc3bf7bb
2 changed files with 20 additions and 0 deletions

View file

@ -306,6 +306,14 @@ class LocalBackend:
result = backend.get_page_content(col_name, doc_id, pages)
except DocumentNotFoundError:
return json.dumps({"error": f"doc_id '{doc_id}' not found."})
except (ValueError, AttributeError) as e:
# A malformed page spec ("all", "5-") is a recoverable bad tool
# argument: hand the model an actionable error it can correct
# (mirroring the legacy retrieval tool) rather than letting the
# ValueError surface as the agent SDK's generic tool-failure text.
return json.dumps({
"error": f"Invalid pages format: {pages!r}. Use '5-7', '3,8', or '12'. Error: {e}"
})
return json.dumps(result, ensure_ascii=False)
tools = [get_document, get_document_structure, get_page_content]