48 lines
1.7 KiB
Python
48 lines
1.7 KiB
Python
from typing import Any
|
|
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.shopping.service import ShoppingService
|
|
|
|
MAX_LISTS_IN_CONTEXT = 8
|
|
MAX_ITEMS_PER_LIST = 12
|
|
|
|
|
|
def get_shopping_snapshot(db: Session) -> dict[str, Any]:
|
|
return ShoppingService(db).snapshot()
|
|
|
|
|
|
def format_shopping_context(snapshot: dict[str, Any]) -> str:
|
|
lines = ["[Списки покупок]"]
|
|
lists = snapshot.get("lists") or []
|
|
|
|
if not lists:
|
|
lines.append("Списков пока нет. create_shopping_list или add_shopping_items.")
|
|
return "\n".join(lines)
|
|
|
|
lines.append(
|
|
f"Всего списков: {snapshot.get('list_count', len(lists))}, "
|
|
f"неотмеченных позиций: {snapshot.get('unchecked_items', 0)}."
|
|
)
|
|
lines.append("Для изменений вызывай tools: list_shopping_lists, add_shopping_items, check_shopping_item.")
|
|
|
|
for lst in lists[:MAX_LISTS_IN_CONTEXT]:
|
|
items = lst.get("items") or []
|
|
unchecked = [i for i in items if not i.get("checked")]
|
|
preview = unchecked[:MAX_ITEMS_PER_LIST]
|
|
parts = []
|
|
for item in preview:
|
|
qty = item.get("quantity")
|
|
unit = (item.get("unit") or "").strip()
|
|
label = item["text"]
|
|
if qty is not None:
|
|
label = f"{label} ({qty}{' ' + unit if unit else ''})"
|
|
parts.append(f"#{item['id']} {label}")
|
|
tail = f" +{len(unchecked) - len(preview)} ещё" if len(unchecked) > len(preview) else ""
|
|
if parts:
|
|
lines.append(f"- «{lst['name']}» (#{lst['id']}): {', '.join(parts)}{tail}")
|
|
else:
|
|
lines.append(f"- «{lst['name']}» (#{lst['id']}): всё отмечено или пусто")
|
|
|
|
return "\n".join(lines)
|