WFDASH-WEBAPP/src/lib/server
Dave 47d58db0c9 feat(mcp): add price_list.add_items tool for agent-driven catalogue upsert
Add a new MCP tool that lets the agent read a supplier PDF, quote PDF,
or pasted supplier email with its native vision, extract the per-line
fields, and commit them to the price_list catalogue in one round-trip.

Why: the only pre-existing path was operator upload via /pricelist ->
n8n ingest -> raw_price_list -> AI extraction -> price_list. That flow
is async, human-in-the-loop, and gates on n8n being healthy. This
tool gives the agent a synchronous shortcut for ad-hoc catalogue
updates -- paste a PDF, get the catalogue updated, no async hops.

The agent does the OCR; the tool does the commit. No LLM work moves
into the MCP server.

Behaviour:

- Dedupe by (sku, supplier) -- exact match. Different suppliers with
  the same SKU get separate rows.
- Upsert policy: cost_price overwrites (latest price wins).
  Description, selling_price, unit, category, subcategory, brand,
  markup, stock_qty, search_text, sku_normalised also patched in
  place on update.
- sku_normalised auto-computed (uppercase, strip non-alphanumeric --
  same canonical form price-list-match.ts uses).
- search_text auto-computed (lowercased, space-separated concat of
  sku + description + brand + supplier + category + subcategory +
  synonyms).
- Skipped items returned with per-item reason (missing_sku,
  missing_supplier, invalid_cost_price, pb_write_failed). Never
  silent.
- Bulk cap 500 items per call; larger lists rejected with
  too_many_items reason.
- source_file_id + source_label supported so new rows can be
  traced back to a raw_price_list upload (optional).
- Supplier stored verbatim on price_list.supplier (free-text field,
  no FK resolution -- same as the live schema).

Files:

- src/lib/server/price-list-service.ts (new) -- addPriceListItems +
  buildSearchText + computeSellingPrice helpers
- src/lib/server/price-list-service.test.ts (new) -- 17 tests
  covering validation, upsert, dedupe filter shape, normalisation
- src/app/api/mcp/route.ts -- new tool definition + dispatch block
- src/app/api/mcp/route.test.ts -- 2 dispatch tests + tool-list
  assertion update
- .agents/skills/wfdash-giellie/SKILL.md -- new section "8. Price-
  list catalogue" documenting input/output shapes + failure modes

Tests:

- 134/134 pass across the affected files
- 1775/1778 in the full suite (3 pre-existing failures unrelated
  to this change: pocketbase-admin.test.ts PB mock mismatch,
  quotes/items/[id]/attachments/upload/route.test.ts timeout,
  supplier-form.test.tsx race -- all reproducible on main)
- npm run build clean
2026-08-10 12:45:05 +02:00
..
agent-auth.test.ts feat(item-history): add getItemHistory service with 5 PB queries 2026-07-29 14:51:58 +02:00
agent-auth.ts feat(item-history): add getItemHistory service with 5 PB queries 2026-07-29 14:51:58 +02:00
ai-settings-service.ts feat: add AI quote processing pipeline and admin settings UI 2026-08-09 16:17:29 +02:00
ai-system-prompt.ts feat: add AI quote processing pipeline and admin settings UI 2026-08-09 16:17:29 +02:00
attachment-extractor.ts feat: add AI quote processing pipeline and admin settings UI 2026-08-09 16:17:29 +02:00
browser-session.ts feat(orders): show resolved sales rep name on order list and sprint 2026-08-04 08:50:54 +02:00
buyer-tasks-service.test.ts fix(buyer-tasks): surface PO email_payload + attachments on group for resend modal prefill 2026-08-06 18:01:58 +02:00
buyer-tasks-service.ts fix(buyer-tasks): surface PO email_payload + attachments on group for resend modal prefill 2026-08-06 18:01:58 +02:00
collection-confirm-service.test.ts feat(stock-purchase): split warehouse confirm from accountant booking 2026-08-06 23:01:15 +02:00
collection-confirm-service.ts feat(stock-purchase): split warehouse confirm from accountant booking 2026-08-06 23:01:15 +02:00
collection-confirm-upload.test.ts feat: add new order, invoice, and dashboard management tools 2026-08-05 23:00:54 +02:00
dashboard-service.test.ts feat: add new order, invoice, and dashboard management tools 2026-08-05 23:00:54 +02:00
dashboard-service.ts feat: add new order, invoice, and dashboard management tools 2026-08-05 23:00:54 +02:00
deliveries-service.test.ts feat(item-history): add getItemHistory service with 5 PB queries 2026-07-29 14:51:58 +02:00
deliveries-service.ts feat(item-history): add getItemHistory service with 5 PB queries 2026-07-29 14:51:58 +02:00
delivery-notes-service.test.ts feat(item-history): add getItemHistory service with 5 PB queries 2026-07-29 14:51:58 +02:00
delivery-notes-service.ts feat(item-history): add getItemHistory service with 5 PB queries 2026-07-29 14:51:58 +02:00
driver-stops.ts feat(item-history): add getItemHistory service with 5 PB queries 2026-07-29 14:51:58 +02:00
item-history-service.test.ts feat(item-history): wire description-fuzzy matcher into getItemHistory 2026-07-29 17:43:59 +02:00
item-history-service.ts feat(context): implement getOrderItemContext (5 PB reads, fuzzy fallback) 2026-08-07 02:34:45 +02:00
management-service.ts feat(item-history): add getItemHistory service with 5 PB queries 2026-07-29 14:51:58 +02:00
order-completion.test.ts feat(item-history): add getItemHistory service with 5 PB queries 2026-07-29 14:51:58 +02:00
order-completion.ts feat(item-history): add getItemHistory service with 5 PB queries 2026-07-29 14:51:58 +02:00
order-item-context-service.test.ts test(context): getOrderItemContext unit tests (null, quote, stock low/out, invoiced) 2026-08-07 02:42:57 +02:00
order-item-context-service.ts chore(context): lint + build adjustments 2026-08-07 03:04:28 +02:00
order-item-margin-approval-service.test.ts fix: resolve PO bug, add sku, improve webhooks 2026-08-06 13:13:57 +02:00
order-item-margin-approval-service.ts fix: resolve PO bug, add sku, improve webhooks 2026-08-06 13:13:57 +02:00
order-item-status.test.ts feat(item-history): add getItemHistory service with 5 PB queries 2026-07-29 14:51:58 +02:00
order-item-status.ts feat(order-items): below-22% margin approval workflow 2026-08-04 00:58:33 +02:00
order-items-service.test.ts feat: collection-map updates, todays-stops, quote/approval rework, buyer-tasks, e2e 2026-08-03 09:33:45 +02:00
order-items-service.ts feat: collection-map updates, todays-stops, quote/approval rework, buyer-tasks, e2e 2026-08-03 09:33:45 +02:00
order-quote-match.test.ts feat(item-history): add getItemHistory service with 5 PB queries 2026-07-29 14:51:58 +02:00
order-quote-match.ts feat(item-history): add getItemHistory service with 5 PB queries 2026-07-29 14:51:58 +02:00
orders-service-review.test.ts feat(orders): show From + Subject in review modal Source of truth header 2026-08-05 16:09:53 +02:00
orders-service.test.ts feat: add new order, invoice, and dashboard management tools 2026-08-05 23:00:54 +02:00
orders-service.ts feat: add new order, invoice, and dashboard management tools 2026-08-05 23:00:54 +02:00
po-email-template.ts feat(item-history): add getItemHistory service with 5 PB queries 2026-07-29 14:51:58 +02:00
po-number.ts fix(buyer-tasks): generate PO succeeds + supplier stripe + PO badge 2026-07-30 15:00:18 +02:00
pocketbase-admin.test.ts feat: add new order, invoice, and dashboard management tools 2026-08-05 23:00:54 +02:00
pocketbase-admin.ts feat: add AI quote processing pipeline and admin settings UI 2026-08-09 16:17:29 +02:00
price-list-match.test.ts feat(item-history): add getItemHistory service with 5 PB queries 2026-07-29 14:51:58 +02:00
price-list-match.ts feat(item-history): add getItemHistory service with 5 PB queries 2026-07-29 14:51:58 +02:00
price-list-service.test.ts feat(mcp): add price_list.add_items tool for agent-driven catalogue upsert 2026-08-10 12:45:05 +02:00
price-list-service.ts feat(mcp): add price_list.add_items tool for agent-driven catalogue upsert 2026-08-10 12:45:05 +02:00
purchase-orders-service.test.ts feat(po): resend purchase order from sent / mail_failed / sending statuses 2026-08-04 20:05:52 +02:00
purchase-orders-service.ts feat(po): resend purchase order from sent / mail_failed / sending statuses 2026-08-04 20:05:52 +02:00
quote-email-template.ts feat: collection-map updates, todays-stops, quote/approval rework, buyer-tasks, e2e 2026-08-03 09:33:45 +02:00
quote-items-insights-service.test.ts feat: add AI quote processing pipeline and admin settings UI 2026-08-09 16:17:29 +02:00
quote-items-insights-service.ts feat: add AI quote processing pipeline and admin settings UI 2026-08-09 16:17:29 +02:00
quote-items-service.ts feat: add AI quote processing pipeline and admin settings UI 2026-08-09 16:17:29 +02:00
quotes-ai-processor.ts feat: add AI quote processing pipeline and admin settings UI 2026-08-09 16:17:29 +02:00
quotes-from-email-service.ts feat: add AI quote processing pipeline and admin settings UI 2026-08-09 16:17:29 +02:00
quotes-service.test.ts feat: add new order, invoice, and dashboard management tools 2026-08-05 23:00:54 +02:00
quotes-service.ts feat: add AI quote processing pipeline and admin settings UI 2026-08-09 16:17:29 +02:00
stock-booking-service.test.ts feat: add split views, stock picking slips, and invoice viewer 2026-08-09 23:16:55 +02:00
stock-booking-service.ts feat: add split views, stock picking slips, and invoice viewer 2026-08-09 23:16:55 +02:00
stock-cost-service.ts feat: unify buyer task flows by supporting both customer orders and stock purchases in mark-ready API and UI, and fix PDF table layout responsiveness. 2026-08-03 18:52:30 +02:00
stock-items-service.test.ts feat: add split-invoice stock PO workflow and supporting tooling 2026-08-07 00:53:29 +02:00
stock-items-service.ts feat: add split-invoice stock PO workflow and supporting tooling 2026-08-07 00:53:29 +02:00
stock-out.ts feat(item-history): add getItemHistory service with 5 PB queries 2026-07-29 14:51:58 +02:00
stock-purchase-invoice-matching-service.test.ts feat(mcp): expand stock purchase invoice matching service with 5 phases 2026-08-09 20:23:36 +02:00
stock-purchase-invoice-matching-service.ts feat: add split views, stock picking slips, and invoice viewer 2026-08-09 23:16:55 +02:00
stock-purchase-items-service.test.ts fix(stock-purchase-items): drop dead drv_collected_at write to unblock build 2026-08-07 01:39:03 +02:00
stock-purchase-items-service.ts fix(stock-purchase-items): drop dead drv_collected_at write to unblock build 2026-08-07 01:39:03 +02:00
stock-purchases-migration.test.ts feat(item-history): add getItemHistory service with 5 PB queries 2026-07-29 14:51:58 +02:00
stock-purchases-service.attachments.test.ts feat(stock-purchases): add additional_attachments service helpers 2026-08-06 23:20:26 +02:00
stock-purchases-service.test.ts feat: add split views, stock picking slips, and invoice viewer 2026-08-09 23:16:55 +02:00
stock-purchases-service.ts feat: add split views, stock picking slips, and invoice viewer 2026-08-09 23:16:55 +02:00
stock-quick-draft-service.test.ts feat(stock-quick-draft): add updateQuickDraftLine service 2026-07-31 13:38:59 +02:00
stock-quick-draft-service.ts feat(stock-quick-draft): add updateQuickDraftLine service 2026-07-31 13:38:59 +02:00
stock-surplus.test.ts feat(item-history): add getItemHistory service with 5 PB queries 2026-07-29 14:51:58 +02:00
stock-surplus.ts feat(item-history): add getItemHistory service with 5 PB queries 2026-07-29 14:51:58 +02:00