fix: keep style-transfer target aligned with user intent
Some checks failed
Lint / build (20) (push) Has been cancelled
Playwright Tests / test (push) Has been cancelled

This commit is contained in:
Your Name 2026-02-20 17:18:22 +08:00
parent 9623f5365b
commit 84fe9175b5

View File

@ -131,10 +131,10 @@ const EDIT_TEMPLATES: Record<
"风格迁移:将图片转换为{style}风格。要求:保持主体可识别性和基本特征,色彩风格协调统一,整体效果自然美观。",
negative: "主体变形严重,特征丢失,风格不统一,色彩异常,人物不可识别",
extractors: {
style: /(?:转换为|变成|改成|转成)(.+?)(?:风格|$)/,
style: /(?:转换为|转化为|变成|变为|改成|改为|转成)(.+?)(?:风格|样式|效果|$)/,
},
defaults: {
style: "卡通动漫",
style: "目标风格",
},
},
@ -171,6 +171,41 @@ const EDIT_TEMPLATES: Record<
import { generateText } from "ai";
function inferStyleTransferTarget(originalPrompt: string, extracted?: string): string {
const text = (originalPrompt || "").trim();
const normalized = text.toLowerCase();
const cleanCandidate = (value?: string) =>
String(value || "")
.replace(/[\n\r]/g, " ")
.replace(/[,。!?;,.!?]/g, " ")
.replace(/\s+/g, " ")
.trim();
const explicitMatch = text.match(
/(?:转换为|转化为|变成|变为|改成|改为|转成)\s*([^,。!?;,.!?]+)/i
);
const explicitValue = cleanCandidate(explicitMatch?.[1]);
if (explicitValue) return explicitValue;
const extractedValue = cleanCandidate(extracted);
if (extractedValue) return extractedValue;
if (/写实|真实|实拍|照片|摄影|photoreal|realistic/.test(normalized)) {
return "真实写实照片";
}
if (/卡通|动漫|二次元|插画|漫画|anime|cartoon/.test(normalized)) {
return "卡通动漫";
}
if (/电影|cinematic|cinema/.test(normalized)) {
return "电影感";
}
if (/油画|watercolor|水彩|素描|国风|古风/.test(normalized)) {
return "艺术风格";
}
return "目标风格";
}
// 历史消息类型
interface HistoryMessage {
role: "user" | "assistant";
@ -630,6 +665,10 @@ export function optimizePrompt(params: {
for (const [key, regex] of Object.entries(template.extractors)) {
const match = originalPrompt.match(regex as RegExp);
let value = match ? match[1].trim() : template.defaults[key] || "";
if (subIntent === "style_transfer" && key === "style") {
value = inferStyleTransferTarget(originalPrompt, value);
}
// 清理提取的值
value = value.replace(/[,。!?,.!?]/g, "").trim();
@ -662,6 +701,9 @@ export function optimizePrompt(params: {
for (const [key, regex] of Object.entries(template.extractors) as [string, RegExp][]) {
const match = originalPrompt.match(regex);
let value = match ? match[1].trim() : template.defaults[key] || "";
if (subIntent === "style_transfer" && key === "style") {
value = inferStyleTransferTarget(originalPrompt, value);
}
value = value.replace(/[,。!?,.!?]/g, "").trim();
if (!value) {
value = template.defaults[key] || "";