Add a item to the similar books menu to ask ai for what to read next

This commit is contained in:
Kovid Goyal 2025-12-02 06:10:52 +05:30
parent 93cf30d837
commit 1b95f02f2a
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 28 additions and 5 deletions

View File

@ -24,18 +24,34 @@ class SimilarBooksAction(InterfaceAction):
def genesis(self):
m = self.qaction.menu()
for text, icon, target, shortcut in [
(_('Books by same author'), 'user_profile.png', 'authors', 'Alt+A'),
(_('Books in this series'), 'books_in_series.png', 'series',
'Alt+Shift+S'),
(_('Books by this publisher'), 'publisher.png', 'publisher', 'Alt+P'),
(_('Books with the same tags'), 'tags.png', 'tags', 'Alt+T'),]:
(_('Books by same author'), 'user_profile.png', 'authors', 'Alt+A'),
(_('Books in this series'), 'books_in_series.png', 'series',
'Alt+Shift+S'),
(_('Books by this publisher'), 'publisher.png', 'publisher', 'Alt+P'),
(_('Books with the same tags'), 'tags.png', 'tags', 'Alt+T'),
]:
ac = self.create_action(spec=(text, icon, None, shortcut),
attr=target)
ac.setObjectName(target)
m.addAction(ac)
connect_lambda(ac.triggered, self, lambda self: self.show_similar_books(self.gui.sender().objectName()))
ac = self.create_action(spec=(_('Ask AI for what to read next'), 'ai.png', None, 'Ctrl+Shift+A'), attr='ai')
ac.setObjectName('ai')
m.addAction(ac)
ac.triggered.connect(self.ask_ai)
self.qaction.setMenu(m)
def ask_ai(self):
idx = self.gui.library_view.currentIndex()
if not idx.isValid():
return
db = idx.model().db
mi = db.new_api.get_metadata(db.id(idx.row()))
from calibre.gui2.dialogs.llm_book import LLMBookDialog, read_next_action
d = LLMBookDialog([mi], parent=self.gui)
d.llm.activate_action(read_next_action())
d.exec()
def show_similar_books(self, typ, *args):
idx = self.gui.library_view.currentIndex()
if not idx.isValid():

View File

@ -85,6 +85,13 @@ def default_actions() -> tuple[Action, ...]:
)
def read_next_action() -> Action:
for ac in default_actions():
if ac.name == 'read_next':
return ac
raise KeyError('No read next action could be found')
def current_actions(include_disabled=False) -> Iterator[Action]:
p = gprefs.get('llm_book_quick_actions') or {}
return Action.unserialize(p, default_actions(), include_disabled)