133 lines
4.5 KiB
Python
133 lines
4.5 KiB
Python
from typing import Any
|
|
|
|
from app.shopping.service import ShoppingService
|
|
from app.tools._dispatch import NOT_HANDLED, ToolContext
|
|
|
|
TOOL_NAMES = frozenset({
|
|
"list_shopping_lists",
|
|
"create_shopping_list",
|
|
"add_shopping_items",
|
|
"check_shopping_item",
|
|
"remove_shopping_item",
|
|
"delete_shopping_list",
|
|
})
|
|
|
|
TOOL_DEFINITIONS: list[dict[str, Any]] = [
|
|
{
|
|
"type": "function",
|
|
"function": {
|
|
"name": "list_shopping_lists",
|
|
"description": "Все списки покупок с позициями. «Что купить», «покажи списки».",
|
|
"parameters": {"type": "object", "properties": {}, "required": []},
|
|
},
|
|
},
|
|
{
|
|
"type": "function",
|
|
"function": {
|
|
"name": "create_shopping_list",
|
|
"description": "Создать новый список покупок.",
|
|
"parameters": {
|
|
"type": "object",
|
|
"properties": {
|
|
"name": {"type": "string", "description": "Название списка, например «Продукты»"},
|
|
},
|
|
"required": ["name"],
|
|
},
|
|
},
|
|
},
|
|
{
|
|
"type": "function",
|
|
"function": {
|
|
"name": "add_shopping_items",
|
|
"description": "Добавить товары в список. Список создаётся, если не существует.",
|
|
"parameters": {
|
|
"type": "object",
|
|
"properties": {
|
|
"list_name": {"type": "string", "description": "Название списка"},
|
|
"list_id": {"type": "integer"},
|
|
"items": {
|
|
"type": "array",
|
|
"items": {
|
|
"type": "object",
|
|
"properties": {
|
|
"text": {"type": "string"},
|
|
"quantity": {"type": "number"},
|
|
"unit": {"type": "string"},
|
|
},
|
|
"required": ["text"],
|
|
},
|
|
},
|
|
},
|
|
"required": ["items"],
|
|
},
|
|
},
|
|
},
|
|
{
|
|
"type": "function",
|
|
"function": {
|
|
"name": "check_shopping_item",
|
|
"description": "Отметить позицию как купленную (checked=true) или снять отметку (false).",
|
|
"parameters": {
|
|
"type": "object",
|
|
"properties": {
|
|
"item_id": {"type": "integer"},
|
|
"checked": {"type": "boolean"},
|
|
},
|
|
"required": ["item_id", "checked"],
|
|
},
|
|
},
|
|
},
|
|
{
|
|
"type": "function",
|
|
"function": {
|
|
"name": "remove_shopping_item",
|
|
"description": "Удалить позицию из списка по item_id.",
|
|
"parameters": {
|
|
"type": "object",
|
|
"properties": {"item_id": {"type": "integer"}},
|
|
"required": ["item_id"],
|
|
},
|
|
},
|
|
},
|
|
{
|
|
"type": "function",
|
|
"function": {
|
|
"name": "delete_shopping_list",
|
|
"description": "Удалить весь список покупок.",
|
|
"parameters": {
|
|
"type": "object",
|
|
"properties": {"list_id": {"type": "integer"}},
|
|
"required": ["list_id"],
|
|
},
|
|
},
|
|
},
|
|
]
|
|
|
|
|
|
async def execute(name: str, arguments: dict[str, Any], ctx: ToolContext) -> Any:
|
|
if name not in TOOL_NAMES:
|
|
return NOT_HANDLED
|
|
|
|
shopping = ShoppingService(ctx.db, ctx.user_id)
|
|
|
|
if name == "list_shopping_lists":
|
|
return shopping.list_lists(include_items=True)
|
|
if name == "create_shopping_list":
|
|
return shopping.create_list(arguments.get("name", ""))
|
|
if name == "add_shopping_items":
|
|
return shopping.add_items(
|
|
arguments.get("items") or [],
|
|
list_id=arguments.get("list_id"),
|
|
list_name=arguments.get("list_name"),
|
|
)
|
|
if name == "check_shopping_item":
|
|
return shopping.set_item_checked(
|
|
int(arguments["item_id"]),
|
|
bool(arguments.get("checked", True)),
|
|
)
|
|
if name == "remove_shopping_item":
|
|
return shopping.remove_item(int(arguments["item_id"]))
|
|
if name == "delete_shopping_list":
|
|
return shopping.delete_list(int(arguments["list_id"]))
|
|
return NOT_HANDLED
|