Add nested inventory package tracking

This commit is contained in:
Joren de Graaf
2026-10-26 00:25:00 +01:00
parent 4d76a2bc9c
commit 5217c7f30c
19 changed files with 838 additions and 44 deletions
+42 -4
View File
@@ -105,6 +105,16 @@ class GroceryInput(BaseModel):
notes: str = ''
opened: bool = False
best_before: str | None = None
original_amount: Decimal | None = Field(default=None, gt=0)
unit: Literal['mg', 'g', 'kg', 'ml', 'L', 'pieces', 'packs', 'portions'] | None = None
pieces_per_package: int | None = Field(default=None, ge=1)
remaining_pieces: int | None = Field(default=None, ge=0)
piece_amount: Decimal | None = Field(default=None, gt=0)
inner_packs_per_package: int | None = Field(default=None, ge=1)
pieces_per_inner_pack: int | None = Field(default=None, ge=1)
inner_pack_amount: Decimal | None = Field(default=None, gt=0)
inner_pack_label: str | None = None
piece_label: str | None = None
class InventoryUpdate(BaseModel):
@@ -115,6 +125,16 @@ class InventoryUpdate(BaseModel):
notes: str | None = None
opened: bool | None = None
remaining_amount: Decimal | None = Field(default=None, ge=0)
original_amount: Decimal | None = Field(default=None, gt=0)
unit: Literal['mg', 'g', 'kg', 'ml', 'L', 'pieces', 'packs', 'portions'] | None = None
pieces_per_package: int | None = Field(default=None, ge=1)
remaining_pieces: int | None = Field(default=None, ge=0)
piece_amount: Decimal | None = Field(default=None, gt=0)
inner_packs_per_package: int | None = Field(default=None, ge=1)
pieces_per_inner_pack: int | None = Field(default=None, ge=1)
inner_pack_amount: Decimal | None = Field(default=None, gt=0)
inner_pack_label: str | None = None
piece_label: str | None = None
best_before: str | None = None
@@ -355,7 +375,10 @@ def _entry(entry):
def _inventory(item):
return {key: item.get(key) for key in ('id', 'product_id', 'product_name', 'brand', 'barcode',
'quantity', 'location', 'category', 'opened', 'best_before', 'notes',
'remaining_amount', 'original_amount', 'unit', 'status', 'archived_at')}
'remaining_amount', 'original_amount', 'unit', 'package_count', 'pieces_per_package',
'remaining_pieces', 'piece_amount', 'inner_packs_per_package', 'pieces_per_inner_pack',
'inner_pack_amount', 'inner_pack_label', 'piece_label', 'remaining_full_inner_packs',
'opened_inner_pack_pieces', 'status', 'archived_at')}
def _key(value):
@@ -534,15 +557,30 @@ def create_server() -> MCPServer:
data = await api_request('GET', 'kitchen/items/', params={'search': query, 'limit': min(max(limit, 1), 100)})
return {'count': data['count'], 'items': [_inventory(item) for item in data['results']]}
@server.tool(structured_output=True, description='Add an owned saved food product to the authenticated user’s kitchen inventory. Quantity counts whole items; location and category use TaskNest’s existing names. Reuse idempotency_key on retries.')
@server.tool(structured_output=True, description='Add an owned saved food product to the authenticated user’s kitchen inventory. Quantity counts outer packages. Optional nested fields define inner_packs_per_package × pieces_per_inner_pack; inner_pack_amount is the measured amount per inner pack, from which per-piece weight is estimated. Do not infer measured amounts. Reuse idempotency_key on retries.')
async def inventory_add(product_id: str, quantity: int, location: str, category: str | None = None,
notes: str = '', opened: bool = False, best_before: str | None = None,
original_amount: Decimal | None = None,
unit: Literal['mg', 'g', 'kg', 'ml', 'L', 'pieces', 'packs', 'portions'] | None = None,
pieces_per_package: int | None = None, remaining_pieces: int | None = None,
piece_amount: Decimal | None = None,
inner_packs_per_package: int | None = None, pieces_per_inner_pack: int | None = None,
inner_pack_amount: Decimal | None = None, inner_pack_label: str | None = None,
piece_label: str | None = None,
idempotency_key: str | None = None) -> dict[str, Any]:
key = _key(idempotency_key)
payload = {'product_id': product_id, 'quantity': quantity, 'location': location,
'notes': notes, 'opened': opened}
if category is not None: payload['category'] = category
if best_before is not None: payload['best_before'] = best_before
for name, value in (('original_amount', original_amount), ('unit', unit),
('pieces_per_package', pieces_per_package),
('remaining_pieces', remaining_pieces), ('piece_amount', piece_amount),
('inner_packs_per_package', inner_packs_per_package),
('pieces_per_inner_pack', pieces_per_inner_pack),
('inner_pack_amount', inner_pack_amount), ('inner_pack_label', inner_pack_label),
('piece_label', piece_label)):
if value is not None: payload[name] = str(value) if isinstance(value, Decimal) else value
item = await api_request('POST', 'kitchen/items/', payload=payload, idempotency_key=key)
return {'success': True, 'item': _inventory(item), 'idempotency_key': key}
@@ -555,7 +593,7 @@ def create_server() -> MCPServer:
'create_missing_products': create_missing_products}, idempotency_key=key)
return {**data, 'idempotency_key': key}
@server.tool(structured_output=True, description='Update only the supplied fields of one inventory item belonging to the authenticated user. Changes existing data.')
@server.tool(structured_output=True, description='Update only supplied inventory fields. Quantity is outer-package count. Nested packages can specify inner-pack count, pieces per inner pack, and measured amount per inner pack; estimated piece amounts are derived from those values. Partial deductions by pieces, inner_packs, or matching g/ml are atomic; unsupported conversions return an error.')
async def inventory_update(item_id: str, changes: InventoryUpdate) -> dict[str, Any]:
item = await api_request('PATCH', f'kitchen/items/{item_id}/',
payload=changes.model_dump(mode='json', exclude_unset=True), return_validation=True)
@@ -595,7 +633,7 @@ def create_server() -> MCPServer:
params = {'product_id': product_id, 'query': query, 'location_id': location_id}
return await api_request('GET', 'kitchen/stock/', params={k: v for k, v in params.items() if v is not None})
@server.tool(structured_output=True, description='Preview exact package deductions for one owned product without changing stock. A shortage is reported rather than silently consumed.')
@server.tool(structured_output=True, description='Preview package deductions for one owned product without changing stock. Supports pieces and inner_packs when packaging is configured, plus matching g/ml amounts. Nested per-piece weights are estimates derived from measured inner-pack amounts; unsupported conversions return a clear error.')
async def inventory_preview_consumption(product_id: str, amount: Decimal, unit: str,
package_id: str | None = None) -> dict[str, Any]:
return await api_request('POST', 'kitchen/consumption/preview/', payload={