diff --git a/app/(chat)/api/chat/route.ts b/app/(chat)/api/chat/route.ts index f08a38fa5..89fa8bac1 100644 --- a/app/(chat)/api/chat/route.ts +++ b/app/(chat)/api/chat/route.ts @@ -827,9 +827,33 @@ function normalizeAgentReportHeadingLine(line: string): string { return line; } +function stripAgentFriendlyMediaEmbeds(message: string): string { + if (!message) return message; + + const withoutInlineMedia = message + .replace(/!\[[^\]]*]\((https?:\/\/[^\s)]+)\)/gi, "") + .replace(//gi, "") + .replace(/]*>/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(); +} + function normalizeAgentReportForDisplay(message?: string): string | undefined { if (!message?.trim()) return message; - return message + const stripped = stripAgentFriendlyMediaEmbeds(message); + if (!stripped) return undefined; + return stripped .split("\n") .map((line) => normalizeAgentReportHeadingLine(line)) .join("\n");