Discuss with AI: Fix addition of comments like fields to the prompt

This commit is contained in:
Kovid Goyal
2025-12-05 22:00:23 +05:30
parent 0dbdb32599
commit e487da7679
2 changed files with 18 additions and 4 deletions
+14 -4
View File
@@ -14,13 +14,14 @@ from calibre.gui2 import Application, gprefs
from calibre.gui2.llm import ActionData, ConverseWidget, LLMActionsSettingsWidget, LLMSettingsDialogBase, LocalisedResults
from calibre.gui2.ui import get_gui
from calibre.gui2.widgets2 import Dialog
from calibre.library.comments import comments_as_markdown
from calibre.utils.icu import primary_sort_key
from polyglot.binary import from_hex_unicode
def format_book_for_query(book: Metadata, is_first: bool, num_books: int) -> str:
which = '' if num_books < 2 else ('first' if is_first else 'next')
ans = f'The {which} book is: {book.title} by {book.format_authors()}.'
which = '' if num_books < 2 else ('first ' if is_first else 'next ')
ans = f'The {which}book is: {book.title} by {book.format_authors()}.'
left = get_allowed_fields() - {'title', 'authors'}
if 'series' in left:
ans += f' It is in the series: {book.series}.'
@@ -31,9 +32,18 @@ def format_book_for_query(book: Metadata, is_first: bool, num_books: int) -> str
comments = []
fields = []
for field in left:
if book.is_null(field):
continue
m = book.metadata_for_field(field)
if field == 'comments' or m['datatype'] == 'comments':
comments.append(m.get('field'))
if field == 'comments':
comments.append(comments_as_markdown(book.get(field)))
elif m['datatype'] == 'comments':
ctype = m.get('display', {}).get('interpret_as') or 'html'
match ctype:
case 'long-text' | 'short-text' | 'markdown':
comments.append(book.get(field))
case _:
comments.append(comments_as_markdown(book.get(field)))
else:
fields.append(book.format_field(field))
if fields:
+4
View File
@@ -147,6 +147,10 @@ def sanitize_comments_html(html):
return html
def comments_as_markdown(x: str | None) -> str:
return html2text(comments_to_html(x or ''), single_line_break=False)
def find_tests():
import unittest