Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/label-creates-order.md
Original file line number Diff line number Diff line change
@@ -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.
14 changes: 14 additions & 0 deletions apps/api/src/proofbox_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
from proofbox_api.cosmos import Cosmos
from proofbox_api.packing import (
Check,
Item,
Order,
Pack,
PackSummary,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 []
Expand Down
21 changes: 20 additions & 1 deletion apps/api/src/proofbox_api/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions apps/web/src/components/label-panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down
26 changes: 22 additions & 4 deletions apps/web/src/hooks/use-pack-session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions apps/web/src/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ const json = (value: unknown): RequestInit => ({

export const listOrders = () => call<Order[]>("/orders");

/** Keeps an order that already exists, so a reprint never rewrites what was packed */
export const registerOrder = (order: Order) => call<Order>("/orders", json(order));

export type RecentPack = PackSummary & { order_id: string };

/** Newest first, so the station history survives a reload. */
Expand Down
5 changes: 5 additions & 0 deletions apps/web/src/lib/vision-math.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" }]);
});
});
12 changes: 10 additions & 2 deletions apps/web/src/lib/vision-math.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, ReadonlySet<number>>,
): 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;
Expand All @@ -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;
}
Loading