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:
@@ -72,17 +72,80 @@ func TestEnrichContentWithImageInfoForChat_SkipsUnmatched(t *testing.T) {
|
||||
content := "\n\n"
|
||||
raw, _ := json.Marshal([]types.ImageInfo{{URL: "u2", OCRText: "two"}})
|
||||
got := EnrichContentWithImageInfoForChat(content, string(raw))
|
||||
if strings.Count(got, "<image url=") != 1 {
|
||||
t.Fatalf("expected one image block, got: %s", got)
|
||||
if strings.Contains(got, "<image") {
|
||||
t.Fatalf("chat context should not contain internal image XML: %s", got)
|
||||
}
|
||||
if !strings.Contains(got, "") {
|
||||
t.Fatalf("unmatched markdown should remain: %s", got)
|
||||
}
|
||||
if !strings.Contains(got, "<image_ocr>two</image_ocr>") {
|
||||
if !strings.Contains(got, "") {
|
||||
t.Fatalf("matched markdown should remain renderable: %s", got)
|
||||
}
|
||||
if !strings.Contains(got, "> **Image text (OCR):** two") {
|
||||
t.Fatalf("matched image should be enriched: %s", got)
|
||||
}
|
||||
if strings.Contains(got, "<image_original>") {
|
||||
t.Fatalf("chat enrich should not duplicate markdown in image_original: %s", got)
|
||||
if strings.Count(got, "\n\nafter"
|
||||
raw, _ := json.Marshal([]types.ImageInfo{
|
||||
{
|
||||
URL: "resource://AbCdEfGhIjKlMnOpQrStUv",
|
||||
Caption: "目标说话人提取流程图",
|
||||
OCRText: "输入\n目标说话人提取\n输出",
|
||||
},
|
||||
})
|
||||
|
||||
got := EnrichContentWithImageInfoForChat(content, string(raw))
|
||||
for _, want := range []string{
|
||||
"",
|
||||
"> **Image caption:** 目标说话人提取流程图",
|
||||
"> **Image text (OCR):** 输入",
|
||||
"> 目标说话人提取",
|
||||
"> 输出",
|
||||
} {
|
||||
if !strings.Contains(got, want) {
|
||||
t.Fatalf("expected %q in enriched Markdown:\n%s", want, got)
|
||||
}
|
||||
}
|
||||
if strings.Contains(got, "<image") {
|
||||
t.Fatalf("chat context should be Markdown-only for image content: %s", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnrichContentWithImageInfoForChat_EnrichesRepeatedImagesOnceEach(t *testing.T) {
|
||||
content := "\n\n"
|
||||
raw, _ := json.Marshal([]types.ImageInfo{{URL: "u1", Caption: "same caption"}})
|
||||
|
||||
got := EnrichContentWithImageInfoForChat(content, string(raw))
|
||||
if strings.Count(got, "") != 2 {
|
||||
t.Fatalf("expected both Markdown images to remain: %s", got)
|
||||
}
|
||||
if strings.Count(got, "> **Image caption:** same caption") != 2 {
|
||||
t.Fatalf("expected each image to be enriched exactly once: %s", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildImageInfoMarkdownWithURL(t *testing.T) {
|
||||
got := BuildImageInfoMarkdownWithURL(
|
||||
"resource://AbCdEfGhIjKlMnOpQrStUv",
|
||||
&types.ImageInfo{Caption: "流程图 [测试]", OCRText: "输入\n输出"},
|
||||
)
|
||||
for _, want := range []string{
|
||||
`![流程图 \[测试\]](resource://AbCdEfGhIjKlMnOpQrStUv)`,
|
||||
"> **Image caption:** 流程图 [测试]",
|
||||
"> **Image text (OCR):** 输入",
|
||||
"> 输出",
|
||||
} {
|
||||
if !strings.Contains(got, want) {
|
||||
t.Fatalf("expected %q in image Markdown:\n%s", want, got)
|
||||
}
|
||||
}
|
||||
if strings.Contains(got, "<image") {
|
||||
t.Fatalf("LLM-facing image context must not use image XML: %s", got)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user