import { defineAction } from "@agent-native/core"; import { getRequestUserEmail, todayInTimezone, } from "@agent-native/core/server"; import { z } from "zod"; import { db, schema } from "../server/db/index.js"; export default defineAction({ description: "Log/add/record a meal entry with calories and optional macros. This action writes the meal row to the database and returns the saved row; after it succeeds, do not call db-schema, db-query, db-exec, docs-search, web-request/fetch, raw HTTP, or action HTTP endpoints to verify or insert the same meal.", schema: z.object({ name: z.string().min(1).describe("Meal name"), calories: z.coerce.number().optional().describe("Calories"), protein: z.coerce.number().optional().describe("Protein in grams"), carbs: z.coerce.number().optional().describe("Carbs in grams"), fat: z.coerce.number().optional().describe("Fat in grams"), date: z .string() .optional() .describe("Date in YYYY-MM-DD format (defaults to today)"), }), run: async (args) => { const ownerEmail = getRequestUserEmail(); if (!ownerEmail) throw new Error("no authenticated user"); const date = args.date || todayInTimezone(); const result = await db() .insert(schema.meals) .values({ owner_email: ownerEmail, name: args.name, calories: args.calories || 0, protein: args.protein ?? null, carbs: args.carbs ?? null, fat: args.fat ?? null, date: String(date).split("T")[0], image_url: null, notes: null, }) .returning(); return result[0]; }, });