fix(search): preserve chunk types in tool results (#4402)
parent
8ddb245d1b
commit
2fac6381f7
|
|
@ -1073,8 +1073,10 @@ func (t *SearchKnowledgeTool) formatOutput(
|
|||
tag = "faq"
|
||||
idAttr = fmt.Sprintf("faq_id=\"%s\" index=\"%d\"", xmlEscape(result.ID), result.ChunkIndex)
|
||||
}
|
||||
fmt.Fprintf(&ob, "<%s rank=\"%d\" %s knowledge_base_id=\"%s\" knowledge_title=\"%s\" score=\"%.3f\">\n",
|
||||
tag, i+1, idAttr, xmlEscape(result.KnowledgeBaseID), xmlEscape(result.KnowledgeTitle), result.Score)
|
||||
fmt.Fprintf(&ob, "<%s rank=\"%d\" %s knowledge_base_id=\"%s\" "+
|
||||
"knowledge_title=\"%s\" chunk_type=\"%s\" score=\"%.3f\">\n",
|
||||
tag, i+1, idAttr, xmlEscape(result.KnowledgeBaseID),
|
||||
xmlEscape(result.KnowledgeTitle), xmlEscape(result.ChunkType), result.Score)
|
||||
if snippet != "" {
|
||||
fmt.Fprintf(&ob, "<match_snippet>%s</match_snippet>\n", xmlEscape(snippet))
|
||||
}
|
||||
|
|
@ -1098,6 +1100,7 @@ func (t *SearchKnowledgeTool) formatOutput(
|
|||
row := map[string]interface{}{
|
||||
"result_index": i + 1,
|
||||
"content": result.Content,
|
||||
"chunk_type": result.ChunkType,
|
||||
"knowledge_id": result.KnowledgeID,
|
||||
"knowledge_base_id": result.KnowledgeBaseID,
|
||||
"knowledge_title": result.KnowledgeTitle,
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ package tools
|
|||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"encoding/xml"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
|
@ -365,3 +366,34 @@ func TestDeduplicateResultsKeepsSiblingsImagesAndBestScore(t *testing.T) {
|
|||
t.Fatalf("dedup = %v (first %s)", got, out[0].ID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFormatOutputPreservesChunkType(t *testing.T) {
|
||||
for _, chunkType := range []string{
|
||||
"text", "summary", "image_ocr", "image_caption", "image_vector", "", `custom"<&`,
|
||||
} {
|
||||
t.Run(chunkType, func(t *testing.T) {
|
||||
tool := &SearchKnowledgeTool{}
|
||||
res := tool.formatOutput(context.Background(), []*searchResultWithMeta{{
|
||||
SearchResult: &types.SearchResult{
|
||||
ID: "chunk-1", KnowledgeID: "doc-1", Content: "evidence", ChunkType: chunkType,
|
||||
},
|
||||
KnowledgeBaseID: "kb-1",
|
||||
}}, []string{"kb-1"}, "evidence", SearchModeHybrid)
|
||||
rows := res.Data["results"].([]map[string]interface{})
|
||||
if got, ok := rows[0]["chunk_type"]; !ok || got != chunkType {
|
||||
t.Fatalf("chunk_type = %#v, present = %v; want %q", got, ok, chunkType)
|
||||
}
|
||||
var rendered struct {
|
||||
Chunks []struct {
|
||||
ChunkType string `xml:"chunk_type,attr"`
|
||||
} `xml:"chunk"`
|
||||
}
|
||||
if err := xml.Unmarshal([]byte(res.Output), &rendered); err != nil {
|
||||
t.Fatalf("invalid text output: %v", err)
|
||||
}
|
||||
if len(rendered.Chunks) != 1 || rendered.Chunks[0].ChunkType != chunkType {
|
||||
t.Fatalf("text output lost chunk type %q: %s", chunkType, res.Output)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -53,6 +53,8 @@ MCP 管理页支持按服务名称和卡片显示的用途说明搜索。搜索
|
|||
| Wiki | `wiki_search`、`wiki_read_page`、`wiki_index` | 只对开启了 Wiki 的知识库生效;`wiki_search` 的 `query` 保持原有的正则语义(大小写不敏感),不是合法正则的文本按字面匹配;`regex=false` 强制字面匹配,`regex=true` 要求合法正则 |
|
||||
| 写入 | `add_document`、`update_document`、`delete_document` | 默认关闭;支持 Markdown 文本或 URL 导入 |
|
||||
|
||||
`search_knowledge` 的结构化结果 `results[].chunk_type` 和文本结果的 `chunk_type` 属性保留命中片段的原始类型,例如 `text`、`summary`、`image_ocr`、`image_caption` 或 `image_vector`。空字符串表示类型未知,不能据此认定为原文;客户端可据类型决定是否补读文档正文。
|
||||
|
||||
现有 MCP 工具的返回结果会复用 REST/IM 的资源链接转换:正文、Wiki 内容以及结构化数据中的图片引用,都会在权限校验后转换为可直接访问的 HTTP(S) 链接,不需要新增工具或修改端点工具清单。`ask` 的 `references[].images` 保留图片 URL、caption 和 OCR 文本,文本引用列表也包含图片链接,避免正文摘要截断后丢失图片信息。
|
||||
|
||||
本地或私有存储请配置外部客户端可访问的 `APP_EXTERNAL_URL`,并确保反向代理转发 `/r/`。`resource://` 图片沿用有时效的 `/r/<token>` 链接;其他存储地址使用对应存储服务生成的 HTTP(S) URL。一次工具调用中同一资源只转换一次,返回链接不写回文档或会话记录。无法生成可访问链接时保留原引用,不中断文本结果。
|
||||
|
|
|
|||
Loading…
Reference in New Issue