Files
dagda/meal_tracker/tasks.py
T
2026-11-20 16:35:19 +00:00

58 lines
2.4 KiB
Python

# meal_tracker/tasks.py
from core.base import TaskModule
from .models import Meal, MealItem, Food
class MealTracker(TaskModule):
MODULE_NAME = "meal_tracker"
DESCRIPTION = "Log meals with food items and nutritional tracking"
TOOLS = [
{
"type": "function",
"function": {
"name": "log_meal",
"description": "Log a meal entry with food, quantity, and macros",
"parameters": {
"type": "object",
"properties": {
"date": {"type": "string", "format": "date"},
"items": {
"type": "array",
"items": {
"type": "object",
"properties": {
"food_name": {"type": "string"},
"quantity_grams": {"type": "integer"},
"estimated_calories": {"type": "integer"},
"estimated_protein": {"type": "number"},
"estimated_fat": {"type": "number"},
"estimated_carbs": {"type": "number"}
},
"required": ["food_name", "quantity_grams"]
}
}
},
"required": ["date", "items"]
}
}
}
]
@classmethod
def execute_tool(cls, tool_name: str, arguments: dict) -> dict:
if tool_name == "log_meal":
meal = Meal.objects.create(date=arguments["date"])
for item_data in arguments["items"]:
food = Food.objects.create(
name=item_data["food_name"],
estimated_calories=item_data.get("estimated_calories"),
estimated_protein=item_data.get("estimated_protein"),
estimated_fat=item_data.get("estimated_fat"),
estimated_carbs=item_data.get("estimated_carbs")
)
MealItem.objects.create(meal=meal, food=food, quantity_grams=item_data["quantity_grams"])
return {"status": "success",
"message": f"Logged {len(arguments['items'])} food items for {arguments['date']}"}
raise ValueError(f"Unknown tool: {tool_name}")