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:
@@ -0,0 +1,46 @@
|
||||
package tools
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
)
|
||||
|
||||
func TestLLMToolOutputsUseMarkdownImages(t *testing.T) {
|
||||
imageInfo, err := json.Marshal([]types.ImageInfo{
|
||||
{
|
||||
URL: "resource://AbCdEfGhIjKlMnOpQrStUv",
|
||||
Caption: "目标说话人提取流程图",
|
||||
OCRText: "输入\n输出",
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
chunk := &types.Chunk{
|
||||
ID: "chunk-1",
|
||||
ChunkIndex: 0,
|
||||
ChunkType: types.ChunkTypeText,
|
||||
Content: "测试阶段主要流程",
|
||||
ImageInfo: string(imageInfo),
|
||||
}
|
||||
|
||||
listOutput := (&ListKnowledgeChunksTool{}).buildOutput("knowledge-1", "测试文档", 1, 1, []*types.Chunk{chunk})
|
||||
wikiOutput := enrichChunkContent(chunk)
|
||||
for name, output := range map[string]string{
|
||||
"list_knowledge_chunks": listOutput,
|
||||
"wiki_read_source_doc": wikiOutput,
|
||||
} {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
if !strings.Contains(output, "") {
|
||||
t.Fatalf("expected Markdown image in tool output:\n%s", output)
|
||||
}
|
||||
if strings.Contains(output, "<image") || strings.Contains(output, "<caption>") || strings.Contains(output, "<ocr_text>") {
|
||||
t.Fatalf("tool output leaked legacy image XML:\n%s", output)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1247,14 +1247,10 @@ func (t *KnowledgeSearchTool) formatOutput(
|
||||
var imageInfos []types.ImageInfo
|
||||
if err := json.Unmarshal([]byte(result.ImageInfo), &imageInfos); err == nil && len(imageInfos) > 0 {
|
||||
for _, img := range imageInfos {
|
||||
ob.WriteString(fmt.Sprintf("<image url=\"%s\">\n", xmlEscape(img.URL)))
|
||||
if img.Caption != "" {
|
||||
ob.WriteString(fmt.Sprintf("<image_caption>%s</image_caption>\n", xmlEscape(img.Caption)))
|
||||
if imageMarkdown := searchutil.BuildImageInfoMarkdownWithURL(img.URL, &img); imageMarkdown != "" {
|
||||
ob.WriteString(imageMarkdown)
|
||||
ob.WriteString("\n")
|
||||
}
|
||||
if img.OCRText != "" {
|
||||
ob.WriteString(fmt.Sprintf("<image_ocr>%s</image_ocr>\n", xmlEscape(img.OCRText)))
|
||||
}
|
||||
ob.WriteString("</image>\n")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -412,7 +412,7 @@ func (t *ListKnowledgeChunksTool) buildOutput(
|
||||
for _, c := range chunks {
|
||||
if c.ChunkType == types.ChunkTypeFAQ {
|
||||
writeFAQEntryXML(&b, c)
|
||||
writeChunkImagesXML(&b, c)
|
||||
writeChunkImagesMarkdown(&b, c)
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -424,7 +424,7 @@ func (t *ListKnowledgeChunksTool) buildOutput(
|
||||
c.ID, c.ChunkIndex, c.ChunkType)
|
||||
}
|
||||
fmt.Fprintf(&b, "<content>%s</content>\n", summarizeContent(c.Content))
|
||||
writeChunkImagesXML(&b, c)
|
||||
writeChunkImagesMarkdown(&b, c)
|
||||
b.WriteString("</chunk>\n")
|
||||
}
|
||||
|
||||
@@ -436,7 +436,7 @@ func (t *ListKnowledgeChunksTool) buildOutput(
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func writeChunkImagesXML(b *strings.Builder, c *types.Chunk) {
|
||||
func writeChunkImagesMarkdown(b *strings.Builder, c *types.Chunk) {
|
||||
if c == nil || c.ImageInfo == "" {
|
||||
return
|
||||
}
|
||||
@@ -445,18 +445,10 @@ func writeChunkImagesXML(b *strings.Builder, c *types.Chunk) {
|
||||
return
|
||||
}
|
||||
for _, img := range imageInfos {
|
||||
if img.URL != "" {
|
||||
fmt.Fprintf(b, "<image url=\"%s\">\n", img.URL)
|
||||
} else {
|
||||
b.WriteString("<image>\n")
|
||||
if imageMarkdown := searchutil.BuildImageInfoMarkdownWithURL(img.URL, &img); imageMarkdown != "" {
|
||||
b.WriteString(imageMarkdown)
|
||||
b.WriteString("\n")
|
||||
}
|
||||
if img.Caption != "" {
|
||||
fmt.Fprintf(b, "<image_caption>%s</image_caption>\n", img.Caption)
|
||||
}
|
||||
if img.OCRText != "" {
|
||||
fmt.Fprintf(b, "<image_ocr>%s</image_ocr>\n", img.OCRText)
|
||||
}
|
||||
b.WriteString("</image>\n")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -96,15 +96,9 @@ func enrichChunkContent(c *types.Chunk) string {
|
||||
if err := json.Unmarshal([]byte(c.ImageInfo), &imgInfos); err == nil && len(imgInfos) > 0 {
|
||||
var imgBuilder strings.Builder
|
||||
for _, img := range imgInfos {
|
||||
if img.URL != "" {
|
||||
imgBuilder.WriteString(fmt.Sprintf("\n<image url=\"%s\">\n", img.URL))
|
||||
if img.Caption != "" {
|
||||
imgBuilder.WriteString(fmt.Sprintf("<caption>%s</caption>\n", img.Caption))
|
||||
}
|
||||
if img.OCRText != "" {
|
||||
imgBuilder.WriteString(fmt.Sprintf("<ocr_text>%s</ocr_text>\n", img.OCRText))
|
||||
}
|
||||
imgBuilder.WriteString("</image>")
|
||||
if imageMarkdown := searchutil.BuildImageInfoMarkdownWithURL(img.URL, &img); imageMarkdown != "" {
|
||||
imgBuilder.WriteString("\n")
|
||||
imgBuilder.WriteString(imageMarkdown)
|
||||
}
|
||||
}
|
||||
content += imgBuilder.String()
|
||||
@@ -163,7 +157,7 @@ func (t *wikiReadSourceDocTool) Execute(ctx context.Context, args json.RawMessag
|
||||
pageSize := 100
|
||||
page := 1
|
||||
if hasRange {
|
||||
page = (params.StartChunkIndex - 1) / pageSize + 1
|
||||
page = (params.StartChunkIndex-1)/pageSize + 1
|
||||
}
|
||||
|
||||
var chunksOutput strings.Builder
|
||||
@@ -293,7 +287,7 @@ func (t *wikiReadSourceDocTool) Execute(ctx context.Context, args json.RawMessag
|
||||
}
|
||||
|
||||
sb.WriteString(fmt.Sprintf("<total_chunks>%d</total_chunks>\n</metadata>\n", totalChunks))
|
||||
|
||||
|
||||
if matchCount > 0 {
|
||||
sb.WriteString(fmt.Sprintf("<chunks count=\"%d\">\n", matchCount))
|
||||
sb.WriteString(chunksOutput.String())
|
||||
|
||||
Reference in New Issue
Block a user