fix: resolve detail reference sku to spu

This commit is contained in:
Your Name 2026-06-02 13:18:39 +08:00
parent 84f105d4c8
commit 6937217634

View File

@ -236,7 +236,35 @@ function getGoodsInfoBrandName(data: Record<string, unknown>) {
);
}
async function fetchGoodsBrandByKeyword(params: {
function getGoodsInfoMatchedSpu(data: Record<string, unknown>) {
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<string, unknown>) {
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<typeof normalizeAgentWorkflowGoodsInfoApiConfig>;
}) {
@ -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,