Skip to content

API Reference parrot.knowledge.bookstore.toolkit

github-actions[bot] edited this page Dec 2, 2026 · 4 revisions

parrot.knowledge.bookstore.toolkit

View source

BookstoreToolkit — the read-only agent surface of the library.

Every public async method auto-becomes a bookstore_* tool via :class:~parrot.tools.toolkit.AbstractToolkit. The surface is deliberately small and funnel-shaped for research:

  1. bookstore_find_books — which books treat X, and where (librarian's sectors first)
  2. bookstore_catalog_search — which book covers X? (cheap, no LLM) 1b. bookstore_related_books / bookstore_communities — expand by the book graph before opening anything (FEAT-533)
  3. bookstore_get_toc — orient inside the chosen book
  4. bookstore_search_book — targeted in-book hybrid search
  5. bookstore_agentic_retrieve — validated, cited research within one book
  6. bookstore_read_section — read only the sections that matter

Ingestion/removal is NOT exposed here — managing the library is the bookstore CLI's job (indexing a book is a minutes-long LLM batch, the wrong shape for a stdio tool call, same reasoning that keeps bookstore relate/export-wiki CLI-only).

Alias AbstractToolkit

Re-exported from parrot.tools.toolkit.AbstractToolkit.

Alias Bookstore

Re-exported from parrot.knowledge.bookstore.library.Bookstore.

Class BookstoreToolkit

Read-only tools over a :class:~parrot.knowledge.bookstore.library.Bookstore.

Parameter Type Default Description
bookstore Bookstore The composed library manager (catalogs + trees).
**kwargs Any {}

Attributes:

Attribute Type Description
read_tools frozenset[str]

Method BookstoreToolkit.find_books

Which books of the library treat a topic, and where — START HERE.

Answers from the librarian's sectors (the library notebook) with direct pointers to the table-of-contents entries; falls back to catalog search when no sector matches.

find_books(query: str, top_k: int = 5) -> dict[str, Any]

Parameter Type Default Description
query str Topic or keywords (e.g. "stoic ethics").
top_k int 5 Maximum sectors or books returned (clamped to 1-50).

Returns: dict[str, Any] — {"source": "sectors", "sectors": [{code, title, books: [{book_id, title, scope, entries}]}]}, or {"source": "catalog_fts", "books": [...], "hint": ...} when no sector matched. Use book_id + entry node_id with bookstore_read_section.

Method BookstoreToolkit.catalog_search

Find which books cover a topic by their catalog cards (lexical fallback of find_books).

Lexical (FTS/BM25) search over the per-book catalog cards (title, authors, topics, summary, table of contents). Cheap — no LLM involved. Use the returned book_id values with bookstore_get_toc / bookstore_search_book.

catalog_search(query: str, top_k: int = 8) -> list[dict[str, Any]]

Parameter Type Default Description
query str Topic or keywords to research (e.g. "index tuning").
top_k int 8 Maximum books returned (clamped to 1-50).

Returns: list[dict[str, Any]] — Compact card dicts, best match first.

Method BookstoreToolkit.list_books

List every book in the library (project + global scopes).

list_books() -> list[dict[str, Any]]

Returns: list[dict[str, Any]] — Compact card dicts, project scope winning id collisions.

Method BookstoreToolkit.get_card

Full catalog card (ficha) for one book.

Includes the rendered table-of-contents digest — useful to decide whether the book is worth opening.

get_card(book_id: str) -> dict[str, Any]

Parameter Type Default Description
book_id str Id from bookstore_catalog_search / bookstore_list_books.

Returns: dict[str, Any]

Method BookstoreToolkit.get_toc

Structured table of contents with node ids and page ranges.

Each entry's node_id is the key for bookstore_read_section.

get_toc(book_id: str) -> dict[str, Any]

Parameter Type Default Description
book_id str The book to open.

Returns: dict[str, Any]

Method BookstoreToolkit.search_book

Search inside ONE book's chapter tree.

Hybrid BM25 + LLM tree-walk when a model is configured, BM25-only otherwise. Prefer this over reading sections blindly.

search_book(book_id: str, query: str, top_k: int = 8) -> list[dict[str, Any]]

Parameter Type Default Description
book_id str The book to search.
query str Natural-language question or keywords.
top_k int 8 Maximum sections returned (clamped to 1-50).

Returns: list[dict[str, Any]] — {node_id, title, summary, score, source} candidates.

Method BookstoreToolkit.agentic_retrieve

Research one book with validated citations; requires PARROT_BOOKSTORE_LLM.

agentic_retrieve(book_id: str, query: str, top_k: int = 5, draft_answer: bool = True, budget_chars: Optional[int] = None, max_reads: Optional[int] = None, timeout_s: Optional[float] = None) -> dict[str, Any]

Parameter Type Default Description
book_id str Stable identifier of the indexed book to research.
query str Research question for the bounded reading loop.
top_k int 5 Number of first-stage candidates to consider.
draft_answer bool True Whether to ask the model for a final answer.
budget_chars Optional[int] None Optional total character budget for evidence reads.
max_reads Optional[int] None Optional maximum number of evidence reads.
timeout_s Optional[float] None Optional total request timeout in seconds.

Returns: dict[str, Any] — Validated evidence and citations decorated with book identity.

Raises:

  • BookstoreError — If indexed segments or a compatible LLM are unavailable.
  • ValueError — If a numeric argument is invalid.

Method BookstoreToolkit.read_section

Read one section's full markdown content.

Cite results as: book title, section title, pp. start-end.

read_section(book_id: str, node_id: str) -> dict[str, Any]

Parameter Type Default Description
book_id str The book.
node_id str Section id from bookstore_get_toc or bookstore_search_book.

Returns: dict[str, Any]

Method BookstoreToolkit.read_pages

Read pages of a book, e.g. pages="5-7,12".

Returns quoted book text split into segments; cite a segment by its address.label (e.g. p. 12 ¶3). When truncated is true, call again with cursor=next_cursor.

read_pages(book_id: str, pages: str, max_chars: int = 12000, cursor: Optional[str] = None) -> dict[str, Any]

Parameter Type Default Description
book_id str The book.
pages str Page ranges, comma separated.
max_chars int 12000 Character budget for this call.
cursor Optional[str] None next_cursor from a previous call.

Returns: dict[str, Any]

Method BookstoreToolkit.read_segments

Read paragraphs (segments) of a book by position [start, end) or one section's own text.

Returns quoted book text; cite a segment by its address.label (e.g. Setup > Install ¶2). When truncated is true, call again with cursor=next_cursor.

read_segments(book_id: str, start: Optional[int] = None, end: Optional[int] = None, node_id: Optional[str] = None, max_chars: int = 12000, cursor: Optional[str] = None, expected_revision: Optional[str] = None) -> dict[str, Any]

Parameter Type Default Description
book_id str The book.
start Optional[int] None First segment position (inclusive).
end Optional[int] None Last segment position (exclusive).
node_id Optional[str] None Section id from bookstore_get_toc; reads only that section's own paragraphs.
max_chars int 12000 Character budget for this call.
cursor Optional[str] None next_cursor from a previous call.
expected_revision Optional[str] None Optional revision that the returned snapshot must match.

Returns: dict[str, Any] — Citable segments from the matching book snapshot.

Raises:

  • RevisionMismatchError — If the returned snapshot has a different revision.
  • BookstoreError — If the segments cannot be read.

Method BookstoreToolkit.outline

Compact outline of a book (or one section): titles, node ids, pages and paragraph counts.

Cheaper than bookstore_get_toc for deciding what to read next.

outline(book_id: str, node_id: Optional[str] = None, max_depth: int = 3) -> dict[str, Any]

Parameter Type Default Description
book_id str The book.
node_id Optional[str] None Outline only this section's subtree.
max_depth int 3 How many levels to include.

Returns: dict[str, Any]

Method BookstoreToolkit.related_books

Books related to book_id through the library graph.

Use this right after bookstore_catalog_search on its best hit, before opening any book — it surfaces the author's other works, sibling works of the same tradition/era, or conceptually adjacent works an agent would otherwise only find by luck. SQL-only, no LLM call, no matter what.

Each item carries rel (same_author, shares_topic, same_tradition, same_genre, same_era, same_language, influenced_by, responds_to, parallels, contrasts_with, same_community), origin (deterministic | llm | community) and, for LLM relations, confidence and a rationale. Cite the origin when a relation drives a claim — e.g. "the library links these as parallels (LLM-inferred, 0.7)" vs "same author (deterministic)".

related_books(book_id: str, rel: Optional[str] = None, depth: int = 1, top_k: int = 10) -> list[dict[str, Any]]

Parameter Type Default Description
book_id str Origin book id.
rel Optional[str] None Restrict to one relation kind.
depth int 1 Hops to walk from book_id (clamped to 1-2).
top_k int 10 Maximum results returned (clamped to 1-50).

Returns: list[dict[str, Any]]

Method BookstoreToolkit.communities

List every detected community (thematic/author/tradition cluster).

Use when a question is thematic or comparative ("what schools of thought does this library cover?") rather than about one book. Members here are compact briefs (book_id, title) — use bookstore_get_community for the full row including inter-community relations. Run bookstore relate --all (CLI-only) first if this returns nothing.

communities() -> list[dict[str, Any]]

Returns: list[dict[str, Any]]

Method BookstoreToolkit.get_community

Full detail for one community, including inter-community relations.

get_community(community_id: str) -> dict[str, Any]

Parameter Type Default Description
community_id str Id from bookstore_communities.

Returns: dict[str, Any]

Method BookstoreToolkit.search

Cross-book research search (catalog shortlist → tree search).

Use for questions spanning the library ("which of my books discusses X and what do they say?"). Costs up to one LLM tree-walk per shortlisted book — keep max_books small.

search(query: str, book_ids: Optional[list[str]] = None, max_books: int = 3, expand_related: bool = False) -> dict[str, Any]

Parameter Type Default Description
query str Natural-language research question.
book_ids Optional[list[str]] None Restrict to these books; omit for an automatic catalog shortlist.
max_books int 3 Cap on books searched (clamped to 1-10).
expand_related bool False When True, widen the shortlist with each shortlisted book's depth-1 related books before searching — still capped by max_books, no extra LLM call.

Returns: dict[str, Any]

Architecture

Clients

Bots & Agents

Memory

Knowledge

Tools & Toolkits

Flows

Experts

Skills

Stores & Loaders

MCP & A2A

Server & APIs

Integrations

Outputs & A2UI

Prompts

Security

Operations

Migration

SDD Tooling

LLM Wiki

Deployment

Decisions

API Reference

Clone this wiki locally