6cab0338d2
- reader.js: local text styling (system panels, dialogue, thoughts, values, sound effects, footnotes, promo hiding) with per-novel feature toggles, usable when pasted offline - indexer/maintain: text and comments describe MiniMax via Headroom instead of GLM; the maintenance Job uses the minimax-headroom Secret - scripts/ni: status, logs, health and a verified reader-only deploy - docs: MiniMax quota routing notes Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
90 lines
3.5 KiB
Python
90 lines
3.5 KiB
Python
"""Pre-reading: a background thread that reads a Kavita novel chapter by chapter, in
|
|
order, and records characters, looks, glossary terms and chapter summaries on the story
|
|
timeline. Cards and recaps are then complete even for chapters that are never illustrated,
|
|
and stay spoiler-free because everything is tied to chapter positions. Text-only MiniMax M2.7
|
|
calls; portraits are still drawn lazily, when a scene or a card needs them."""
|
|
import sys
|
|
import time
|
|
import traceback
|
|
|
|
import kavita
|
|
import llm
|
|
import pipeline
|
|
import store
|
|
from config import MAX_TEXT
|
|
|
|
DELAY = 5 # seconds between chapters: gentle on MiniMax limits
|
|
BACKFILL_DELAY = 0.5 # between mention-recount chapters: leave CPU to the web thread
|
|
RETRY_AFTER = 300
|
|
|
|
|
|
REFRESH_TOTAL = 6 * 3600 # re-check the page count: nightly updates append chapters
|
|
|
|
|
|
def register(novel, source, chapter_path):
|
|
"""Start pre-reading a Kavita novel the first time the reader opens it, and pick up
|
|
chapters appended to the book since."""
|
|
parsed = kavita.parse_chapter_path(chapter_path)
|
|
if not kavita.enabled() or not source.startswith("kavita:") or not parsed:
|
|
return
|
|
state = store.index_status(novel)
|
|
if state and time.time() - state["checked_at"] < REFRESH_TOTAL:
|
|
return
|
|
total = kavita.book_pages(parsed[0])
|
|
if total:
|
|
store.register_index(novel, source, parsed[0], total)
|
|
|
|
|
|
def read_chapter(novel, position, title, text):
|
|
text = text[:MAX_TEXT]
|
|
with store.connect() as conn:
|
|
context = pipeline.roster_text(conn, novel, position, text)
|
|
found = llm.chat(llm.INDEX_SYSTEM, f"Chapter: {title}\n\n{context}\n\nChapter text:\n{text}")
|
|
with store.connect() as conn:
|
|
store.learn(conn, novel, position, found, text)
|
|
|
|
|
|
def backfill_mentions(work):
|
|
"""Chapters pre-read before mention tracking existed: record their mentions from the
|
|
text alone (no model call), a quick pass that catches up with next_position."""
|
|
position = work["mentions_position"]
|
|
got = kavita.chapter(work["book"], position)
|
|
if got:
|
|
with store.connect() as conn:
|
|
store.record_mentions(conn, work["novel"], position, got[1])
|
|
store.advance_mentions(work["novel"], position + 1)
|
|
|
|
|
|
def run():
|
|
while True:
|
|
# The reader's illustrations come first: model calls share one Token Plan.
|
|
if pipeline.busy():
|
|
time.sleep(20)
|
|
continue
|
|
work = store.next_index_work() if kavita.enabled() else None
|
|
if not work:
|
|
time.sleep(60)
|
|
continue
|
|
if work["mentions_position"] < work["next_position"]:
|
|
try:
|
|
backfill_mentions(work)
|
|
time.sleep(BACKFILL_DELAY)
|
|
except Exception as e:
|
|
print(f"mention backfill {work['novel']} failed: {e}", file=sys.stderr)
|
|
time.sleep(RETRY_AFTER)
|
|
continue
|
|
position = work["next_position"]
|
|
try:
|
|
got = kavita.chapter(work["book"], position)
|
|
if got is None:
|
|
store.advance_index(work["novel"], work["total"]) # past the end
|
|
continue
|
|
read_chapter(work["novel"], position, *got)
|
|
store.advance_index(work["novel"], position + 1)
|
|
time.sleep(DELAY)
|
|
except Exception as e:
|
|
print(f"pre-reading {work['novel']} chapter {position} failed: {e}", file=sys.stderr)
|
|
traceback.print_exc()
|
|
store.advance_index(work["novel"], position, str(e)) # retried later, in order
|
|
time.sleep(RETRY_AFTER)
|