From 69372176347a667715b181a23f65bd2de3199ca0 Mon Sep 17 00:00:00 2001 From: Your Name Date: Tue, 2 Jun 2026 13:18:39 +0800 Subject: [PATCH] fix: resolve detail reference sku to spu --- .../agent-workflow/reference-search/route.ts | 78 +++++++++++++++---- 1 file changed, 63 insertions(+), 15 deletions(-) diff --git a/app/api/agent-workflow/reference-search/route.ts b/app/api/agent-workflow/reference-search/route.ts index b3c29f651..89fc2384c 100644 --- a/app/api/agent-workflow/reference-search/route.ts +++ b/app/api/agent-workflow/reference-search/route.ts @@ -236,7 +236,35 @@ function getGoodsInfoBrandName(data: Record) { ); } -async function fetchGoodsBrandByKeyword(params: { +function getGoodsInfoMatchedSpu(data: Record) { + return ( + sanitizeText(data["spu"], 64) || + sanitizeText(data["SPU"], 64) || + sanitizeText(data["productSpu"], 64) || + sanitizeText(data["ProductSpu"], 64) || + sanitizeText(data["商品SPU"], 64) || + sanitizeText(data["商品spu"], 64) || + sanitizeText(data["款号"], 64) || + sanitizeText(data["商品款号"], 64) || + null + ); +} + +function getGoodsInfoMatchedSku(data: Record) { + return ( + sanitizeText(data["sku"], 64) || + sanitizeText(data["SKU"], 64) || + sanitizeText(data["skuId"], 64) || + sanitizeText(data["SkuId"], 64) || + sanitizeText(data["skuoid"], 64) || + sanitizeText(data["SKUOID"], 64) || + sanitizeText(data["商品SKU"], 64) || + sanitizeText(data["商品sku"], 64) || + null + ); +} + +async function fetchGoodsLookupByKeyword(params: { keyword: string; config: ReturnType; }) { @@ -270,13 +298,20 @@ async function fetchGoodsBrandByKeyword(params: { }); const result = (await response.json().catch(() => ({}))) as ExternalGoodsInfoResponse; if (response.ok && result.success === true && result.data) { - const brandName = getGoodsInfoBrandName(result.data); - if (brandName) { - return brandName; + const data = result.data; + const brandName = getGoodsInfoBrandName(data); + const matchedSpu = getGoodsInfoMatchedSpu(data); + const matchedSku = getGoodsInfoMatchedSku(data); + if (brandName || matchedSpu || matchedSku) { + return { + brandName, + matchedSpu, + matchedSku, + }; } } } catch (error) { - console.warn("[Agent Workflow Reference Search] Goods brand query failed:", { + console.warn("[Agent Workflow Reference Search] Goods info lookup failed:", { requestPath, error, }); @@ -910,15 +945,25 @@ export async function GET(request: NextRequest) { const materialQueryType = queryType as AgentWorkflowMaterialQueryType; const requestedBrandName = String(parsed.brandName || "").trim(); - const verifiedGoodsBrandName = requestedBrandName - ? await fetchGoodsBrandByKeyword({ - keyword: normalizedKeyword, - config: goodsInfoConfig, - }).catch((error) => { - console.warn("[Agent Workflow Reference Search] Goods brand lookup failed:", error); - return null; - }) - : null; + const goodsLookup = await fetchGoodsLookupByKeyword({ + keyword: normalizedKeyword, + config: goodsInfoConfig, + }).catch((error) => { + console.warn("[Agent Workflow Reference Search] Goods info lookup failed:", error); + return null; + }); + const verifiedGoodsBrandName = goodsLookup?.brandName || null; + const resolvedMaterialKeyword = + materialQueryType === "spu" && goodsLookup?.matchedSpu + ? goodsLookup.matchedSpu.trim().toUpperCase() + : normalizedKeyword; + if (materialQueryType === "spu" && goodsLookup?.matchedSpu) { + console.info("[Agent Workflow Reference Search] Resolved SKU input to SPU:", { + keyword: normalizedKeyword, + resolvedSpu: resolvedMaterialKeyword, + matchedSku: goodsLookup.matchedSku || null, + }); + } if ( requestedBrandName && verifiedGoodsBrandName && @@ -953,7 +998,7 @@ export async function GET(request: NextRequest) { for (const currentQueryType of queryTypesToTry) { const attempt = await requestTaskDetailAttempt({ - keyword: normalizedKeyword, + keyword: resolvedMaterialKeyword, queryType: currentQueryType, target: parsed.target, requestedBrandName, @@ -964,6 +1009,7 @@ export async function GET(request: NextRequest) { if (requestedBrandName) { console.info("[Agent Workflow Reference Search] Brand gate:", { keyword: normalizedKeyword, + resolvedKeyword: resolvedMaterialKeyword, queryType: currentQueryType, target: parsed.target, requestedBrandName, @@ -1048,6 +1094,7 @@ export async function GET(request: NextRequest) { if (selectedAttempt.queryType !== materialQueryType) { console.info("[Agent Workflow Reference Search] Query type fallback succeeded:", { keyword: normalizedKeyword, + resolvedKeyword: resolvedMaterialKeyword, requestedQueryType: materialQueryType, resolvedQueryType: selectedAttempt.queryType, target: parsed.target, @@ -1065,6 +1112,7 @@ export async function GET(request: NextRequest) { success: true, data: { query: normalizedKeyword, + resolvedQuery: resolvedMaterialKeyword, queryType: selectedAttempt.queryType, target: parsed.target, matchedSpu: String(selectedAttempt.upstreamResult.data?.spu || "").trim() || null,