Commit 40f5a569 authored by Vũ Hoàng Anh's avatar Vũ Hoàng Anh

Modify : remove filter lack tokens sku

parent 4e045633
......@@ -150,7 +150,7 @@ def _attach_variant_skus(formatted_products: list[dict], raw_products: list[dict
def _resolve_stock_skus(searches: list[SearchItem], products: list[dict]) -> list[str]:
"""Resolve sku_color list from returned flat products, ready for stock tool."""
"""Resolve full stock SKUs from returned flat products, expanding by size when possible."""
resolved: list[str] = []
seen: set[str] = set()
......@@ -172,16 +172,22 @@ def _resolve_stock_skus(searches: list[SearchItem], products: list[dict]) -> lis
for product in candidates:
sku_color = str(product.get("sku_color") or "").strip()
product_color = _normalize_text(product.get("color"))
sizes = [str(size).strip().upper() for size in (product.get("sizes") or []) if str(size).strip()]
expanded_skus = [f"{sku_color}-{size}" for size in sizes] if sku_color and sizes else ([sku_color] if sku_color else [])
if target_color:
if sku_color and (target_color in product_color or product_color in target_color) and sku_color not in seen:
seen.add(sku_color)
resolved.append(sku_color)
if target_color in product_color or product_color in target_color:
for full_sku in expanded_skus:
if full_sku and full_sku not in seen:
seen.add(full_sku)
resolved.append(full_sku)
continue
if sku_color and sku_color not in seen:
seen.add(sku_color)
resolved.append(sku_color)
for full_sku in expanded_skus:
if full_sku and full_sku not in seen:
seen.add(full_sku)
resolved.append(full_sku)
return resolved
......@@ -347,7 +353,6 @@ async def data_retrieval_tool(searches: list[SearchItem]) -> str:
]
allowed_skus = sorted({str(p.get("sku") or "").strip() for p in combined_results if p.get("sku")})
stock_skus = _resolve_stock_skus(searches, combined_results)
output = {
"status": "success",
......@@ -359,7 +364,7 @@ async def data_retrieval_tool(searches: list[SearchItem]) -> str:
),
"search_input": search_inputs,
"results": combined_results,
"stock_skus": stock_skus,
"stock_skus": _resolve_stock_skus(searches, combined_results),
"filter_info": final_info,
}
......
......@@ -14,6 +14,18 @@ def _parse_code_search_input(raw_code: str) -> tuple[str, str | None]:
return internal_ref_code, suffix_code or None
def _build_code_subsequence_pattern(raw_code: str) -> str:
"""
Build LIKE pattern giữ nguyên thứ tự ký tự.
Dùng cho trường hợp user nhập thiếu 1 ký tự ở giữa mã nhưng vẫn muốn match ra full code.
VD: 6ST25W05 -> 6%S%T%2%5%W%0%5%
"""
normalized = str(raw_code or "").strip().upper().replace(" ", "").replace("-", "")
if not normalized:
return "%"
return "%" + "%".join(normalized) + "%"
def _get_price_clauses(params, sql_params: list) -> list[str]:
"""Lọc theo giá (Parameterized)."""
clauses = []
......@@ -129,6 +141,8 @@ async def build_starrocks_query(params, query_vector: list[float] | None = None)
# Chuẩn hóa code user gửi và quy input về internal_ref_code trước khi lấy variants.
normalized_magento_code = str(magento_code).strip().upper().replace(" ", "")
internal_ref_hint, suffix_code = _parse_code_search_input(normalized_magento_code)
internal_ref_loose = _build_code_subsequence_pattern(internal_ref_hint)
magento_code_loose = _build_code_subsequence_pattern(normalized_magento_code)
extra_filters = []
sql_params = [
......@@ -136,10 +150,10 @@ async def build_starrocks_query(params, query_vector: list[float] | None = None)
normalized_magento_code,
normalized_magento_code,
f"{internal_ref_hint}-%",
f"{internal_ref_hint[:-1]}%" if len(internal_ref_hint) > 1 else internal_ref_hint,
f"{internal_ref_hint[:-1]}%-%" if len(internal_ref_hint) > 1 else f"{internal_ref_hint}-%",
f"{normalized_magento_code[:-1]}%" if len(normalized_magento_code) > 1 else normalized_magento_code,
f"{normalized_magento_code[:-1]}%" if len(normalized_magento_code) > 1 else normalized_magento_code,
internal_ref_loose,
internal_ref_loose,
magento_code_loose,
magento_code_loose,
]
# Ưu tiên màu user nói trong message; đây là filter mạnh hơn suffix trong mã.
......@@ -179,10 +193,10 @@ async def build_starrocks_query(params, query_vector: list[float] | None = None)
OR UPPER(magento_ref_code) = %s
OR UPPER(product_color_code) = %s
OR UPPER(product_color_code) LIKE %s
OR UPPER(internal_ref_code) LIKE %s
OR UPPER(product_color_code) LIKE %s
OR UPPER(magento_ref_code) LIKE %s
OR UPPER(product_color_code) LIKE %s
OR REPLACE(UPPER(internal_ref_code), '-', '') LIKE %s
OR REPLACE(UPPER(product_color_code), '-', '') LIKE %s
OR REPLACE(UPPER(magento_ref_code), '-', '') LIKE %s
OR REPLACE(UPPER(product_color_code), '-', '') LIKE %s
)
SELECT
magento_ref_code,
......
from types import SimpleNamespace
import pytest
from agent.tools.product_search_helpers import build_starrocks_query
@pytest.mark.asyncio
async def test_code_search_supports_missing_middle_character_with_loose_pattern():
params = SimpleNamespace(
description="",
product_name=None,
magento_ref_code="6ST25W05",
gender_by_product=None,
age_by_product=None,
master_color=None,
price_min=None,
price_max=None,
discount_min=None,
discount_max=None,
discovery_mode=None,
)
sql, sql_params = await build_starrocks_query(params)
assert "REPLACE(UPPER(internal_ref_code), '-', '') LIKE %s" in sql
assert "REPLACE(UPPER(magento_ref_code), '-', '') LIKE %s" in sql
assert "REPLACE(UPPER(product_color_code), '-', '') LIKE %s" in sql
assert "%6%S%T%2%5%W%0%5%" in sql_params
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment