47 lines
1.9 KiB
JavaScript
47 lines
1.9 KiB
JavaScript
const REPEATED_PATTERN = /^(.{1,3})\1+$/u;
|
|
|
|
/**
|
|
* Lightweight intent guard used before calling the generation APIs. It keeps
|
|
* obvious placeholder input (for example, "111" or "aaaa") from being sent
|
|
* to the model while allowing normal Chinese/English product descriptions.
|
|
*/
|
|
export function analyzeProductIntent(values) {
|
|
const text = values
|
|
.map((value) => String(value || "").trim())
|
|
.filter(Boolean)
|
|
.join(" ");
|
|
if (!text) return "请至少填写一项商品信息";
|
|
|
|
const compact = text.replace(/[\s\p{P}\p{S}]+/gu, "");
|
|
if (!compact) return "商品信息不能只有符号,请填写真实的商品内容";
|
|
if (/^\d+$/u.test(compact)) {
|
|
return "商品信息看起来像占位内容,请填写真实的商品名称、品类、功效或卖点";
|
|
}
|
|
if (REPEATED_PATTERN.test(compact)) {
|
|
return "商品信息看起来是重复的占位内容,请补充真实的商品信息";
|
|
}
|
|
if (!/[\u4e00-\u9fffA-Za-z]/u.test(compact)) {
|
|
return "请填写包含文字的真实商品信息";
|
|
}
|
|
return "";
|
|
}
|
|
|
|
export function validateGenerationOptions(form, includeAnswers = true) {
|
|
// 生成偏好均为选填,只有生成条数需要填写并进行数值校验。
|
|
const required = [["question_count", "提问生成条数"]];
|
|
const missing = required
|
|
.filter(([key]) => !String(form[key] ?? "").trim())
|
|
.map(([, label]) => label);
|
|
if (missing.length) return `请填写${missing.join("、")}`;
|
|
if (!Number.isInteger(Number(form.question_count)) || Number(form.question_count) < 1) {
|
|
return "提问生成条数必须是大于 0 的整数";
|
|
}
|
|
if (String(form.claim_questions_per_user ?? "").trim()) {
|
|
const claimCount = Number(form.claim_questions_per_user);
|
|
if (!Number.isInteger(claimCount) || claimCount < 2 || claimCount > 20) {
|
|
return "每个用户可认领的问题数量必须在 2-20 条之间";
|
|
}
|
|
}
|
|
return "";
|
|
}
|