fix(chat): use Markdown for LLM image context and normalize legacy XML

Chat and agent tool context now emits answer-ready Markdown images instead
of internal <image> XML so copied content renders safely. The frontend also
normalizes legacy XML blocks in persisted conversations during streaming.
This commit is contained in:
wizardchen
2026-07-15 17:30:16 +08:00
committed by lyingbug
parent a729bc1271
commit 951f95903b
9 changed files with 357 additions and 57 deletions
@@ -6,6 +6,7 @@ import {
closeDanglingStreamingEmphasis,
createChatMarkdownRenderer,
markStandaloneStrongParagraphs,
normalizeLegacyImageContextMarkup,
preprocessMathDelimiters,
renderChatMarkdown,
repairFlankingEmphasis,
@@ -45,6 +46,79 @@ test('replaceIncompleteImageWithPlaceholder hides an unfinished streaming image'
)
})
test('normalizeLegacyImageContextMarkup converts copied image XML to Markdown', () => {
const input = [
'before',
'<image url="resource://AbCdEfGhIjKlMnOpQrStUv">',
'<image_caption>目标说话人提取的流程图 [测试]</image_caption>',
'<image_ocr>目标说话人提取</image_ocr>',
'</image>',
'after',
].join('\n')
const output = normalizeLegacyImageContextMarkup(input)
assert.ok(
output.includes('![目标说话人提取的流程图 \\[测试\\]](resource://AbCdEfGhIjKlMnOpQrStUv)'),
)
assert.doesNotMatch(output, /<image|image_caption|image_ocr/)
assert.match(output, /before[\s\S]*after/)
})
test('normalizeLegacyImageContextMarkup keeps original Markdown when present', () => {
const input = [
'<images>',
'<image url="resource://AbCdEfGhIjKlMnOpQrStUv">',
'<image_original>![原图](resource://AbCdEfGhIjKlMnOpQrStUv)</image_original>',
'<image_caption>description</image_caption>',
'</image>',
'</images>',
].join('\n')
assert.equal(
normalizeLegacyImageContextMarkup(input).trim(),
'![原图](resource://AbCdEfGhIjKlMnOpQrStUv)',
)
})
test('normalizeLegacyImageContextMarkup hides an unfinished XML block while streaming', () => {
const prefix = '测试阶段主要流程\n\n'
for (const partial of [
'<ima',
'<image url="resource://AbCdEfGhIjKlMnOpQrStUv">',
'<image url="resource://AbCdEfGhIjKlMnOpQrStUv">\n<image_caption>流程图',
]) {
const output = normalizeLegacyImageContextMarkup(prefix + partial, true)
assert.equal(output, prefix + '<span class="streaming-image-loading"><span class="streaming-image-loading__skeleton"></span></span>')
assert.doesNotMatch(output, /resource:\/\/|image_caption|<ima/)
}
})
test('normalizeLegacyImageContextMarkup preserves literal image XML in code', () => {
const code = '```xml\n<image url="resource://example">\n</image>\n```'
assert.equal(normalizeLegacyImageContextMarkup(code, true), code)
assert.equal(normalizeLegacyImageContextMarkup(code, false), code)
assert.equal(normalizeLegacyImageContextMarkup('ordinary <input', true), 'ordinary <input')
})
test('renderChatMarkdown renders leaked legacy image XML through the safe image renderer', () => {
const renderer = createChatMarkdownRenderer({
imageRenderer: ({ href, text }) => `<img src="${href}" alt="${text}">`,
isValidImageUrl: (href) => href.startsWith('resource://'),
})
const html = renderChatMarkdown(
'<image url="resource://AbCdEfGhIjKlMnOpQrStUv"><image_caption>流程图</image_caption></image>',
{
renderer,
escapeMarkdown: (text) => text,
sanitizeHtml: (value) => value,
streaming: false,
},
)
assert.match(html, /<img src="resource:\/\/AbCdEfGhIjKlMnOpQrStUv" alt="流程图">/)
assert.doesNotMatch(html, /image_caption|&lt;image/)
})
test('stripIncompleteCitationTag hides only an unfinished streaming citation tail', () => {
const prefix = 'Source '
const complete = '<kb doc="2.jpg" chunk_id="3c67efd5-f2ff-4e26-9032-9e44e6861178" />'
+83 -1
View File
@@ -78,6 +78,84 @@ export function replaceIncompleteImageWithPlaceholder(content: string): string {
return content
}
const LEGACY_IMAGE_BLOCK_RE = /<image\b([^>]*)>([\s\S]*?)<\/images?>/gi
const LEGACY_IMAGES_WRAPPER_RE = /<\/?images\b[^>]*>/gi
const LEGACY_IMAGE_ORIGINAL_RE = /<image_original>([\s\S]*?)<\/image_original>/i
const LEGACY_IMAGE_CAPTION_RE = /<image_caption>([\s\S]*?)<\/image_caption>/i
const LEGACY_IMAGE_OCR_RE = /<image_ocr>([\s\S]*?)<\/image_ocr>/i
const COMPLETE_MARKDOWN_CODE_RE = /(```[\s\S]*?```|~~~[\s\S]*?~~~|`[^`\n]*`)/g
function legacyImageField(body: string, pattern: RegExp): string {
const value = body.match(pattern)?.[1] || ''
return value.replace(/<[^>]*>/g, '').replace(/\s+/g, ' ').trim()
}
function escapeMarkdownImageAlt(value: string): string {
return value.replace(/\\/g, '\\\\').replace(/\[/g, '\\[').replace(/\]/g, '\\]')
}
function legacyImageBlockToMarkdown(attributes: string, body: string): string {
const original = body.match(LEGACY_IMAGE_ORIGINAL_RE)?.[1]?.trim()
if (original) return original
const urlMatch = attributes.match(/\burl\s*=\s*(["'])(.*?)\1/i)
const url = urlMatch?.[2]?.trim() || ''
const caption = legacyImageField(body, LEGACY_IMAGE_CAPTION_RE)
const ocr = legacyImageField(body, LEGACY_IMAGE_OCR_RE)
if (!url) return caption || ocr
const alt = escapeMarkdownImageAlt(caption || ocr || 'image')
return `![${alt}](${url})`
}
function normalizeLegacyImageSegment(content: string): string {
return content
.replace(
LEGACY_IMAGE_BLOCK_RE,
(_match, attributes: string, body: string) => legacyImageBlockToMarkdown(attributes, body),
)
.replace(LEGACY_IMAGES_WRAPPER_RE, '')
}
/**
* Normalize the legacy image-context protocol if a model copies it into an
* answer. New chat context is Markdown-native, but persisted conversations and
* older deployments can still contain `<image url="…">` blocks.
*
* Complete blocks become ordinary Markdown images and therefore pass through
* the same URL validation/sanitization as model-authored images. While a block
* is still streaming, its unfinished tail is replaced by the existing image
* skeleton so internal tags, captions, and OCR never flash as prose.
*/
export function normalizeLegacyImageContextMarkup(content: string, streaming = false): string {
if (!content) return content
const hasLegacyImageTag = /<\/?images?\b/i.test(content)
const partialTagPattern = /(^|\n)[ \t]*<i(?:m(?:a(?:g(?:e(?:[ \t][^>\n]*)?)?)?)?)?$/i
const hasPartialStreamingTag = streaming && partialTagPattern.test(content)
if (!hasLegacyImageTag && !hasPartialStreamingTag) return content
const parts = content.split(COMPLETE_MARKDOWN_CODE_RE)
for (let i = 0; i < parts.length; i += 2) {
parts[i] = normalizeLegacyImageSegment(parts[i])
}
if (streaming) {
// The split always leaves normal Markdown in the final even-indexed part,
// so code examples containing literal <image> tags stay untouched.
const tailIndex = parts.length - 1
let tail = parts[tailIndex]
const openingMatches = Array.from(tail.matchAll(/<image(?:\s|>)/gi))
const lastOpening = openingMatches[openingMatches.length - 1]
if (lastOpening?.index !== undefined) {
tail = tail.slice(0, lastOpening.index) + STREAMING_IMAGE_PLACEHOLDER
} else {
// Also hide the few frames before the opening tag name itself is complete.
tail = tail.replace(partialTagPattern, `$1${STREAMING_IMAGE_PLACEHOLDER}`)
}
parts[tailIndex] = tail
}
return parts.join('')
}
/**
* Hide a trailing Markdown horizontal-rule candidate while content is streaming.
*
@@ -353,7 +431,11 @@ export function renderChatMarkdown(rawMarkdown: unknown, options: RenderChatMark
const streamingSafeText = options.streaming
? stripTrailingStreamingListMarker(stripTrailingStreamingHorizontalRule(rawText))
: rawText
const citationSafeText = stripIncompleteCitationTag(streamingSafeText)
const imageContextSafeText = normalizeLegacyImageContextMarkup(
streamingSafeText,
Boolean(options.streaming),
)
const citationSafeText = stripIncompleteCitationTag(imageContextSafeText)
const { text: tagSafe, tags } = preserveCitationTags(citationSafeText)
const imageSafe = replaceIncompleteImageWithPlaceholder(tagSafe)
const mathSafe = preprocessMathDelimiters(imageSafe)