diff --git a/src/lib/server/quote-assistant-service.test.ts b/src/lib/server/quote-assistant-service.test.ts index b5ff37b..d779f8b 100644 --- a/src/lib/server/quote-assistant-service.test.ts +++ b/src/lib/server/quote-assistant-service.test.ts @@ -290,6 +290,13 @@ describe("fillQuote", () => { expect(populateIntel).toHaveBeenCalledWith("a", "quote_items", { write: true, anchor: { stockItemId: "stk1", sku: "HB-1050" }, + posPick: { + stock_item_id: "stk1", + match: "exact", + differs: [], + reason: "same bolt", + model: "MiniMax-M3", + }, }); }); diff --git a/src/lib/server/quote-assistant-service.ts b/src/lib/server/quote-assistant-service.ts index 32b2720..0c1e2b3 100644 --- a/src/lib/server/quote-assistant-service.ts +++ b/src/lib/server/quote-assistant-service.ts @@ -145,12 +145,28 @@ async function populateIntelAfterFill( nowFn: () => Date, ): Promise { const aiFill = patch.ai_fill as - | { stock_item_id?: string | null; stock_item_sku?: string | null } + | { + stock_item_id?: string | null; + stock_item_sku?: string | null; + reason?: string; + model?: string; + } | undefined; const anchor = { stockItemId: aiFill?.stock_item_id || undefined, sku: aiFill?.stock_item_sku || undefined, }; + // The line was filled from a POS row the AI picked — record it, so the + // price check says "this is the item the AI picked" and not "similar". + const posPick = aiFill?.stock_item_id + ? { + stock_item_id: aiFill.stock_item_id, + match: "exact" as const, + differs: [] as string[], + reason: aiFill.reason || "", + model: aiFill.model || "", + } + : undefined; if (nowFn().getTime() >= deadlineMs) { console.warn( `[quote-assistant] time budget spent — skipping item intel for line ${lineId}`, @@ -158,7 +174,11 @@ async function populateIntelAfterFill( return; } try { - await populateItemIntel(lineId, "quote_items", { write: true, anchor }); + await populateItemIntel(lineId, "quote_items", { + write: true, + anchor, + ...(posPick ? { posPick } : {}), + }); } catch (e) { console.error(`[quote-assistant] item intel failed for line ${lineId}:`, e); }