From e2224e82bda990bdf88d329e42973f98afd7ff52 Mon Sep 17 00:00:00 2001 From: Luis Markmann <110569833+lmarkmann@users.noreply.github.com> Date: Sat, 17 Oct 2026 15:49:58 +0100 Subject: [PATCH] feat: printed labels create open orders that take their items from the first seal --- .changeset/label-creates-order.md | 5 +++++ apps/api/src/proofbox_api/__init__.py | 14 +++++++++++++ apps/api/src/proofbox_api/store.py | 21 +++++++++++++++++++- apps/web/src/components/label-panel.tsx | 7 +++++++ apps/web/src/hooks/use-pack-session.ts | 26 +++++++++++++++++++++---- apps/web/src/lib/api.ts | 3 +++ apps/web/src/lib/vision-math.test.ts | 5 +++++ apps/web/src/lib/vision-math.ts | 12 ++++++++++-- 8 files changed, 86 insertions(+), 7 deletions(-) create mode 100644 .changeset/label-creates-order.md diff --git a/.changeset/label-creates-order.md b/.changeset/label-creates-order.md new file mode 100644 index 0000000..70933c9 --- /dev/null +++ b/.changeset/label-creates-order.md @@ -0,0 +1,5 @@ +--- +"web": minor +--- + +Printing a box label creates its order through `POST /orders`. A label printed without items is an open order: YOLOE watches for a broad list of everyday objects, and the first seal writes what it tracked as the order's items, so the proof page and the after-seal check run against what went into the box. diff --git a/apps/api/src/proofbox_api/__init__.py b/apps/api/src/proofbox_api/__init__.py index 276de32..1e9d336 100644 --- a/apps/api/src/proofbox_api/__init__.py +++ b/apps/api/src/proofbox_api/__init__.py @@ -43,6 +43,7 @@ from proofbox_api.cosmos import Cosmos from proofbox_api.packing import ( Check, + Item, Order, Pack, PackSummary, @@ -202,6 +203,13 @@ def list_orders(svc: Svc) -> list[Order]: return svc.store.orders() +@app.post("/orders") +def register_order(svc: Svc, body: Order) -> Order: + """Creates the order a freshly printed label points at; an existing order is kept as it is.""" + svc.store.seed_orders([body]) + return svc.store.order(body.order_id) or body + + @app.get("/orders/{order_id}") def get_order(svc: Svc, order_id: str) -> OrderDetail: order = svc.store.order(order_id) @@ -268,6 +276,12 @@ async def seal_pack( clip = clip_path.read_bytes() counts = count_tracks(points) + if not order.items and counts: + # a label printed without items: what the camera tracked becomes the order + order.items = [Item(name=label, qty=n) for label, n in counts.items()] + await asyncio.to_thread( + svc.store.set_items, order.order_id, order.items + ) expected = ", ".join(f"{item.qty} x {item.name}" for item in order.items) object_key = None errors = [remux_error] if remux_error else [] diff --git a/apps/api/src/proofbox_api/store.py b/apps/api/src/proofbox_api/store.py index 89b957c..20d16b7 100644 --- a/apps/api/src/proofbox_api/store.py +++ b/apps/api/src/proofbox_api/store.py @@ -9,7 +9,7 @@ from ibis import _ from vastdb.table import ITable -from proofbox_api.packing import Check, Order, Pack, TrackPoint +from proofbox_api.packing import Check, Item, Order, Pack, TrackPoint SCHEMA = "proofbox" @@ -142,6 +142,25 @@ def order(self, order_id: str) -> Order | None: rows = self.select("orders", _.order_id == order_id) return order_from_row(rows[0]) if rows else None + def set_items(self, order_id: str, items: list[Item]) -> None: + with self.table("orders") as table: + found = table.select( + columns=["order_id"], + predicate=_.order_id == order_id, + internal_row_id=True, + ).read_all() + encoded = json.dumps([item.model_dump() for item in items]) + table.update( + pa.table( + { + "$row_id": found["$row_id"], + "items": pa.array( + [encoded] * found.num_rows, pa.utf8() + ), + } + ) + ) + def packs(self, order_id: str) -> list[Pack]: rows = self.select("packs", _.order_id == order_id) return sorted(map(pack_from_row, rows), key=lambda pack: pack.sealed_at) diff --git a/apps/web/src/components/label-panel.tsx b/apps/web/src/components/label-panel.tsx index c63983b..1a9b8d4 100644 --- a/apps/web/src/components/label-panel.tsx +++ b/apps/web/src/components/label-panel.tsx @@ -22,6 +22,7 @@ import { Label } from "@/components/ui/label"; import { Skeleton } from "@/components/ui/skeleton"; import type { PrinterStatus } from "@/hooks/use-probe"; import { type Address, addressLines, EMPTY_ADDRESS } from "@/lib/address"; +import { registerOrder } from "@/lib/api"; import { assertNever } from "@/lib/assert-never"; import { type LabelRequest, @@ -280,6 +281,12 @@ export function LabelPanel({ } setJob({ tag: "printing" }); try { + // no items: the first sealed pack fills them in from what the camera tracked + await registerOrder({ + order_id: label.order_id, + customer: address.recipient || "Walk-in", + items: [], + }); await printLabel(label); const printed = { orderId: label.order_id, at: new Date() }; onPrinted(printed); diff --git a/apps/web/src/hooks/use-pack-session.ts b/apps/web/src/hooks/use-pack-session.ts index 051a05f..62e7d52 100644 --- a/apps/web/src/hooks/use-pack-session.ts +++ b/apps/web/src/hooks/use-pack-session.ts @@ -24,6 +24,22 @@ export type PackState = /** One tracked object's lifetime in the clip, in ms from the recording's start */ export type Span = { label: string; track: number; from: number; to: number }; +// what YOLOE looks for when a label was printed without items +const OPEN_ORDER_VOCABULARY = [ + "computer mouse", + "phone", + "cable", + "charger", + "headphones", + "keyboard", + "book", + "bottle", + "cup", + "pen", +]; +// Classes.names in apps/vision +const MAX_CLASSES = 20; + const RECORDING_TYPES = [ "video/mp4;codecs=avc1", "video/mp4", @@ -104,10 +120,12 @@ export function usePackSession( try { const [order, orders] = await Promise.all([getOrder(orderId), listOrders()]); const vocabulary = [ - ...new Set( - [...order.items, ...orders.flatMap((other) => other.items)].map((item) => item.name), - ), - ]; + ...new Set([ + ...order.items.map((item) => item.name), + ...(order.items.length ? [] : OPEN_ORDER_VOCABULARY), + ...orders.flatMap((other) => other.items.map((item) => item.name)), + ]), + ].slice(0, MAX_CLASSES); const stream = recordingStream(); if (!stream) throw new Error("The camera stopped before packing could start."); const { pack_id } = await createPack(orderId); diff --git a/apps/web/src/lib/api.ts b/apps/web/src/lib/api.ts index 9d3b715..9abe505 100644 --- a/apps/web/src/lib/api.ts +++ b/apps/web/src/lib/api.ts @@ -71,6 +71,9 @@ const json = (value: unknown): RequestInit => ({ export const listOrders = () => call("/orders"); +/** Keeps an order that already exists, so a reprint never rewrites what was packed */ +export const registerOrder = (order: Order) => call("/orders", json(order)); + export type RecentPack = PackSummary & { order_id: string }; /** Newest first, so the station history survives a reload. */ diff --git a/apps/web/src/lib/vision-math.test.ts b/apps/web/src/lib/vision-math.test.ts index ab18cd5..410e0b7 100644 --- a/apps/web/src/lib/vision-math.test.ts +++ b/apps/web/src/lib/vision-math.test.ts @@ -55,4 +55,9 @@ describe("reconcile", () => { ["banana", 1, "extra"], ]); }); + + test("an order without items takes everything tracked as packed", () => { + const rows = reconcile([], new Map([["computer mouse", new Set([3, 5])]])); + expect(rows).toEqual([{ name: "computer mouse", qty: 2, seen: 2, status: "done" }]); + }); }); diff --git a/apps/web/src/lib/vision-math.ts b/apps/web/src/lib/vision-math.ts index a50053c..2a1d188 100644 --- a/apps/web/src/lib/vision-math.ts +++ b/apps/web/src/lib/vision-math.ts @@ -44,11 +44,15 @@ export type ChecklistRow = { status: "missing" | "partial" | "done" | "extra"; }; -/** Counts distinct tracks per label against the order; labels the order lacks come last as extras. */ +/** + * Counts distinct tracks per label against the order; labels the order lacks come last as extras. + * An order without items expects nothing, so everything tracked counts as packed. + */ export function reconcile( items: OrderItem[], tracksByLabel: ReadonlyMap>, ): ChecklistRow[] { + const open = items.length === 0; const expected = new Set(items.map((item) => item.name)); const rows: ChecklistRow[] = items.map(({ name, qty }) => { const seen = tracksByLabel.get(name)?.size ?? 0; @@ -61,7 +65,11 @@ export function reconcile( }); for (const [name, tracks] of tracksByLabel) { if (!expected.has(name) && tracks.size > 0) - rows.push({ name, qty: 0, seen: tracks.size, status: "extra" }); + rows.push( + open + ? { name, qty: tracks.size, seen: tracks.size, status: "done" } + : { name, qty: 0, seen: tracks.size, status: "extra" }, + ); } return rows; }