107 lines
3.5 KiB
TypeScript
107 lines
3.5 KiB
TypeScript
import type { PostRequestBody } from "../schema";
|
|
|
|
export function sanitizeAgentOutputSettingsSection(params: {
|
|
message?: string;
|
|
userImageParams?: PostRequestBody["imageParams"];
|
|
hasUserCustomImageParams: boolean;
|
|
}): string | undefined {
|
|
const { message, userImageParams, hasUserCustomImageParams } = params;
|
|
if (!message?.trim()) return message;
|
|
|
|
const outputSectionRegex =
|
|
/(?:###\s*输出设置|【\s*输出设置\s*】)[\s\S]*?(?=\n(?:###\s*|【\s*[^】]+\s*】)|$)/;
|
|
if (!outputSectionRegex.test(message)) return message;
|
|
|
|
if (!hasUserCustomImageParams) {
|
|
return message.replace(
|
|
outputSectionRegex,
|
|
"【输出设置】\n- 参数:按用户面板当前设置执行(未自动改写)"
|
|
);
|
|
}
|
|
|
|
const lines: string[] = [];
|
|
const aspectRatio =
|
|
typeof userImageParams?.aspectRatio === "string" ? userImageParams.aspectRatio.trim() : "";
|
|
const imageSize =
|
|
typeof userImageParams?.imageSize === "string" ? userImageParams.imageSize.trim() : "";
|
|
const imageCount =
|
|
typeof userImageParams?.imageCount === "number" ? userImageParams.imageCount : undefined;
|
|
|
|
if (aspectRatio && aspectRatio !== "auto") {
|
|
lines.push(`- 宽高比:${aspectRatio}(来自用户面板设置)`);
|
|
}
|
|
if (imageSize && imageSize.toUpperCase() !== "2K") {
|
|
lines.push(`- 清晰度档位:${imageSize.toUpperCase()}(来自用户面板设置)`);
|
|
}
|
|
if (typeof imageCount === "number" && Number.isFinite(imageCount) && imageCount !== 1) {
|
|
lines.push(`- 出图数量:${Math.max(1, Math.round(imageCount))} 张(来自用户面板设置)`);
|
|
}
|
|
lines.push("- 其余参数:保持用户面板当前设置");
|
|
|
|
return message.replace(outputSectionRegex, `【输出设置】\n${lines.join("\n")}`);
|
|
}
|
|
|
|
export function normalizeAgentReportHeadingLine(line: string): string {
|
|
const trimmed = line.trim();
|
|
if (!trimmed) return line;
|
|
|
|
const markdownHeading = trimmed.match(/^#{1,6}\s*(.+?)\s*$/);
|
|
const plainTitle = markdownHeading?.[1]?.trim() || trimmed;
|
|
const normalizedTitle = plainTitle
|
|
.replace(/^[^\u4e00-\u9fa5A-Za-z0-9]+/, "")
|
|
.trim();
|
|
|
|
const knownTitles = [
|
|
"需求理解",
|
|
"图片分析",
|
|
"执行计划",
|
|
"优化后提示词",
|
|
"输出设置",
|
|
"脚本规划",
|
|
"分镜规划",
|
|
"执行说明",
|
|
"视频生成完成",
|
|
];
|
|
|
|
if (knownTitles.some((title) => normalizedTitle === title)) {
|
|
return `【${normalizedTitle}】`;
|
|
}
|
|
|
|
if (markdownHeading) {
|
|
return `【${normalizedTitle || plainTitle}】`;
|
|
}
|
|
return line;
|
|
}
|
|
|
|
export function stripAgentFriendlyMediaEmbeds(message: string): string {
|
|
if (!message) return message;
|
|
|
|
const withoutInlineMedia = message
|
|
.replace(/!\[[^\]]*]\((https?:\/\/[^\s)]+)\)/gi, "")
|
|
.replace(/<video[\s\S]*?<\/video>/gi, "")
|
|
.replace(/<img[^>]*>/gi, "");
|
|
|
|
const cleanedLines = withoutInlineMedia
|
|
.split("\n")
|
|
.filter((line) => {
|
|
const trimmed = line.trim();
|
|
if (!trimmed) return true;
|
|
if (/^https?:\/\/\S+$/i.test(trimmed)) return false;
|
|
if (/^\[[^\]]*]\(https?:\/\/[^\s)]+\)$/i.test(trimmed)) return false;
|
|
return true;
|
|
})
|
|
.join("\n");
|
|
|
|
return cleanedLines.replace(/\n{3,}/g, "\n\n").trim();
|
|
}
|
|
|
|
export function normalizeAgentReportForDisplay(message?: string): string | undefined {
|
|
if (!message?.trim()) return message;
|
|
const stripped = stripAgentFriendlyMediaEmbeds(message);
|
|
if (!stripped) return undefined;
|
|
return stripped
|
|
.split("\n")
|
|
.map((line) => normalizeAgentReportHeadingLine(line))
|
|
.join("\n");
|
|
}
|