From 14a7b83b4ba84ffcee39e9921ae9bfcdc349653f Mon Sep 17 00:00:00 2001 From: mike Date: Tue, 22 Sep 2026 14:06:22 +0800 Subject: [PATCH] Update micUploadRoutes.js via upload script - 2026-09-22 14:06:09 --- micUploadRoutes.js | 120 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 94 insertions(+), 26 deletions(-) diff --git a/micUploadRoutes.js b/micUploadRoutes.js index 9d0b5a7..2073790 100644 --- a/micUploadRoutes.js +++ b/micUploadRoutes.js @@ -27,7 +27,7 @@ const crypto = require('crypto'); const WebSocket = require('ws'); const { MicBrowserSession } = require('./micBrowserSession'); -const { buildAttributeList, sanitizeValue, parsePackageDimensionsCm, hasNonAscii } = require('./micFieldMapping'); +const { buildAttributeList, sanitizeValue, sanitizeLabel, parsePackageDimensionsCm, hasNonAscii } = require('./micFieldMapping'); // 标题/关键词规则来自MIC官方说明(2026-09抓取): // 标题 https://service.made-in-china.com/help/promotionskill/product/3243.htm @@ -190,13 +190,62 @@ async function generateKeywords(ctx, title, coreNoun, model, log) { // "三进单出(可调单进单出)"这种)没法靠替换表覆盖——这里兜底用AI把attrList里 // 净化后仍然残留非ASCII字符的值整句翻译成英文。一次性把所有需要翻译的值打包 // 成一个请求,而不是每条都单独调用,省时间也省token。 +// 翻译缓存:属性名(label)几乎每个品类都会反复出现同样的字段名,缓存命中率很高, +// 完全自动存取;属性值(value)大概率每个产品数值都不一样,只在"整句一模一样" +// 才命中缓存——不做"从一句话里拆出某个词对应哪段英文"这种自动拆分,拆错了会把 +// 错误映射存进词典、之后所有产品跟着错,比不缓存还糟。词级别的替换("负载"→ +// "load"这种)走 micFieldMapping.js 里 UNIT_WORD_REPLACEMENTS 那张表,人工发现 +// 一个加一条。 +function loadTranslationCache(ctx) { + const data = ctx.readData(); + const cache = data['crm:micTranslationCache']; + if (cache && typeof cache === 'object') { + return { labels: cache.labels || {}, values: cache.values || {} }; + } + return { labels: {}, values: {} }; +} + +function saveTranslationCache(ctx, cache) { + const data = ctx.readData(); + data['crm:micTranslationCache'] = cache; + ctx.writeData(data); +} + async function translateResidualChineseFields(ctx, attrList, log) { - const indices = []; - const toTranslate = []; + // 属性名(name)和属性值(value)都可能含中文——CATEGORY_FIELD_MAP里的字段name + // 我自己写的都是英文,但extraSpecs(AI从PDF/图片OCR导入的原始数据)连 + // label本身经常都是中文,之前只查了value,漏了name,就是这次"ECO模式效率" + // 那条漏翻的原因。 + const items = []; attrList.forEach((item, i) => { - if (hasNonAscii(item.value)) { indices.push(i); toTranslate.push(item.value); } + const needName = hasNonAscii(item.name); + const needValue = hasNonAscii(item.value); + if (needName || needValue) items.push({ idx: i, name: item.name, value: item.value, needName, needValue }); }); - if (toTranslate.length === 0) return attrList; + if (items.length === 0) return attrList; + + const cache = loadTranslationCache(ctx); + const result = attrList.map((item) => ({ ...item })); + const stillNeeded = []; // 缓存没命中的,才需要问AI + let cacheHits = 0; + + for (const it of items) { + const cachedName = it.needName ? cache.labels[it.name] : null; + const cachedValue = it.needValue ? cache.values[it.value] : null; + const nameReady = !it.needName || cachedName; + const valueReady = !it.needValue || cachedValue; + if (nameReady && valueReady) { + result[it.idx] = { + name: cachedName || result[it.idx].name, + value: cachedValue || result[it.idx].value, + }; + cacheHits++; + } else { + stillNeeded.push(it); + } + } + if (cacheHits > 0) log(`词典缓存命中 ${cacheHits} 条,不用问AI`); + if (stillNeeded.length === 0) return result; let apiKey = ''; try { @@ -205,40 +254,59 @@ async function translateResidualChineseFields(ctx, attrList, log) { } catch (e) { /* 走兜底 */ } if (!apiKey) { - log(`有 ${toTranslate.length} 个属性值仍含中文,但没配置DeepSeek Key无法自动翻译,需要人工检查后再提交`, 'WARNING'); - return attrList; + log(`有 ${stillNeeded.length} 个属性名/值仍含中文,但没配置DeepSeek Key无法自动翻译,需要人工检查后再提交`, 'WARNING'); + return result; } - const prompt = `Translate each of these product spec values (from a power equipment / UPS / battery datasheet) ` - + `from Chinese (or mixed Chinese/English) into concise, natural English suitable for a B2B product listing. ` - + `Keep numbers, units, model codes, and any English words unchanged — only translate the Chinese text. ` - + `Return ONLY a JSON array of strings, same length and same order as the input, no explanation.\n\n` - + `Values: ${JSON.stringify(toTranslate)}`; + // 按 idx 做key(对象而不是数组),AI漏翻/多翻某一条时按key对应,不会因为 + // 数量对不上就把已经翻对的其他条目也一起丢掉。 + const payload = {}; + stillNeeded.forEach((it) => { payload[it.idx] = { name: it.name, value: it.value }; }); + + const prompt = `Translate the "name" and "value" fields below (from a power equipment / UPS / battery product ` + + `datasheet, some in Chinese or mixed Chinese/English) into concise, natural English suitable for a B2B ` + + `product listing attribute. Keep numbers, units, model codes, and existing English words unchanged — only ` + + `translate the Chinese text. If a "name" or "value" is already fully in English, return it unchanged. ` + + `Return ONLY a JSON object with the exact same keys as the input, each value shaped {"name": "...", "value": "..."}, nothing else.\n\n` + + `Input: ${JSON.stringify(payload)}`; try { - const content = await ctx.callDeepSeekWithRetry(apiKey, [{ role: 'user', content: prompt }], { temperature: 0.3, maxTokens: 600 }, 2); + const content = await ctx.callDeepSeekWithRetry(apiKey, [{ role: 'user', content: prompt }], { temperature: 0.3, maxTokens: 1500 }, 2); let raw = String(content).trim(); if (raw.startsWith('```')) { raw = raw.split('```')[1]; if (raw.startsWith('json')) raw = raw.slice(4); } const translated = JSON.parse(raw.trim()); - if (!Array.isArray(translated) || translated.length !== toTranslate.length) { - throw new Error(`AI返回的翻译数量(${Array.isArray(translated) ? translated.length : '非数组'})跟请求数量(${toTranslate.length})对不上`); - } - const result = attrList.map((item) => ({ ...item })); - indices.forEach((idx, j) => { - const newVal = sanitizeValue(translated[j]); // 翻译结果再跑一遍已知符号净化 - log(`AI翻译属性值: "${attrList[idx].value}" -> "${newVal}"`); - if (hasNonAscii(newVal)) { - log(` ⚠️ 翻译后仍含非ASCII字符,需要人工检查这一条: ${result[idx].name}`, 'WARNING'); + if (typeof translated !== 'object' || translated === null) throw new Error('AI返回的不是对象'); + + let translatedCount = 0; + let cacheDirty = false; + for (const it of stillNeeded) { + const t = translated[it.idx] ?? translated[String(it.idx)]; + if (!t) { + log(`⚠️ "${it.name}" 没在AI翻译结果里,保留原文,需要人工检查`, 'WARNING'); + continue; } - result[idx] = { ...result[idx], value: newVal }; - }); + const newName = it.needName ? sanitizeLabel(String(t.name ?? it.name)) : result[it.idx].name; + const newValue = it.needValue ? sanitizeValue(String(t.value ?? it.value)) : result[it.idx].value; + log(`AI翻译属性: "${it.name}: ${it.value}" -> "${newName}: ${newValue}"`); + if (hasNonAscii(newName) || hasNonAscii(newValue)) { + log(` ⚠️ 翻译后仍含非ASCII字符,需要人工检查这一条: ${newName}`, 'WARNING'); + } else { + // 翻对了(且已经是纯ASCII)才收录进词典,避免把半吊子的翻译存进去 + if (it.needName && !cache.labels[it.name]) { cache.labels[it.name] = newName; cacheDirty = true; } + if (it.needValue && !cache.values[it.value]) { cache.values[it.value] = newValue; cacheDirty = true; } + } + result[it.idx] = { name: newName, value: newValue }; + translatedCount++; + } + if (cacheDirty) saveTranslationCache(ctx, cache); + log(`AI翻译属性名/值完成 ${translatedCount}/${stillNeeded.length} 条${translatedCount < stillNeeded.length ? ',剩下的保留原文,人工检查一下' : ''}`); return result; } catch (e) { - log(`AI翻译残留中文属性值失败,保留原文,需要人工检查后再提交: ${e.message}`, 'WARNING'); - return attrList; + log(`AI翻译残留中文属性失败,保留原文,需要人工检查后再提交: ${e.message}`, 'WARNING'); + return result; } }