feat(output): enhance output formatting to XML for various tools

- Updated the formatKnowledgeBaseList function to return knowledge base information as XML, improving data structure for integration.
- Modified the formatOutput function in GrepChunksTool to format search results as XML, enhancing readability and consistency.
- Adjusted the formatOutput method in KnowledgeSearchTool to output search results in XML format, providing a structured response.
- Refactored the buildOutput function in ListKnowledgeChunksTool to generate XML output, improving the clarity of chunk information.

These changes standardize output formats across multiple tools, facilitating better data handling and integration.
This commit is contained in:
wizardchen
2026-04-16 23:25:25 +08:00
committed by lyingbug
parent c5fc05f3d0
commit d546406a15
4 changed files with 161 additions and 207 deletions
+33 -38
View File
@@ -105,79 +105,74 @@ func AvailablePlaceholders() []PlaceholderDefinition {
return result return result
} }
// formatKnowledgeBaseList formats knowledge base information for the prompt // formatKnowledgeBaseList formats knowledge base information as XML for the prompt
func formatKnowledgeBaseList(kbInfos []*KnowledgeBaseInfo) string { func formatKnowledgeBaseList(kbInfos []*KnowledgeBaseInfo) string {
if len(kbInfos) == 0 { if len(kbInfos) == 0 {
return "None" return "<knowledge_bases />"
} }
var builder strings.Builder var b strings.Builder
builder.WriteString("\nThe following knowledge bases have been selected by the user for this conversation. ") b.WriteString("<knowledge_bases>\n")
builder.WriteString("You should search within these knowledge bases to find relevant information.\n\n") for _, kb := range kbInfos {
for i, kb := range kbInfos {
// Display knowledge base name and ID
builder.WriteString(fmt.Sprintf("%d. **%s** (knowledge_base_id: `%s`)\n", i+1, kb.Name, kb.ID))
// Display knowledge base type
kbType := kb.Type kbType := kb.Type
if kbType == "" { if kbType == "" {
kbType = "document" // Default type kbType = "document"
} }
builder.WriteString(fmt.Sprintf(" - Type: %s\n", kbType)) b.WriteString(fmt.Sprintf("<knowledge_base id=\"%s\" name=\"%s\" type=\"%s\" doc_count=\"%d\">\n",
kb.ID, kb.Name, kbType, kb.DocCount))
if kb.Description != "" { if kb.Description != "" {
builder.WriteString(fmt.Sprintf(" - Description: %s\n", kb.Description)) b.WriteString(fmt.Sprintf("<description>%s</description>\n", kb.Description))
} }
builder.WriteString(fmt.Sprintf(" - Document count: %d\n", kb.DocCount))
// Display recent documents if available
// For FAQ type knowledge bases, adjust the display format
if len(kb.RecentDocs) > 0 { if len(kb.RecentDocs) > 0 {
if kbType == "faq" { if kbType == "faq" {
// FAQ knowledge base: show Q&A pairs in a more compact format b.WriteString("<faq_entries>\n")
builder.WriteString(" - Recent FAQ entries:\n\n")
builder.WriteString(" | # | Question | Answers | Chunk ID | Knowledge ID | Created At |\n")
builder.WriteString(" |---|-------------------|---------|----------|--------------|------------|\n")
for j, doc := range kb.RecentDocs { for j, doc := range kb.RecentDocs {
if j >= 10 { // Limit to 10 documents if j >= 10 {
break break
} }
question := doc.FAQStandardQuestion question := doc.FAQStandardQuestion
if question == "" { if question == "" {
question = doc.FileName question = doc.FileName
} }
answers := "-" b.WriteString(fmt.Sprintf("<faq chunk_id=\"%s\" knowledge_id=\"%s\" created_at=\"%s\">\n",
doc.ChunkID, doc.KnowledgeID, doc.CreatedAt))
b.WriteString(fmt.Sprintf("<question>%s</question>\n", question))
if len(doc.FAQAnswers) > 0 { if len(doc.FAQAnswers) > 0 {
answers = strings.Join(doc.FAQAnswers, " | ") for _, ans := range doc.FAQAnswers {
b.WriteString(fmt.Sprintf("<answer>%s</answer>\n", ans))
}
} }
builder.WriteString(fmt.Sprintf(" | %d | %s | %s | `%s` | `%s` | %s |\n", b.WriteString("</faq>\n")
j+1, question, answers, doc.ChunkID, doc.KnowledgeID, doc.CreatedAt))
} }
b.WriteString("</faq_entries>\n")
} else { } else {
// Document knowledge base: show documents in standard format b.WriteString("<recent_documents>\n")
builder.WriteString(" - Recently added documents:\n\n")
builder.WriteString(" | # | Document Name | Type | Created At | Knowledge ID | File Size | Summary |\n")
builder.WriteString(" |---|---------------|------|------------|--------------|----------|---------|\n")
for j, doc := range kb.RecentDocs { for j, doc := range kb.RecentDocs {
if j >= 10 { // Limit to 10 documents if j >= 10 {
break break
} }
docName := doc.Title docName := doc.Title
if docName == "" { if docName == "" {
docName = doc.FileName docName = doc.FileName
} }
// Format file size
fileSize := formatFileSize(doc.FileSize) fileSize := formatFileSize(doc.FileSize)
summary := formatDocSummary(doc.Description, 120) b.WriteString(fmt.Sprintf("<document knowledge_id=\"%s\" type=\"%s\" file_size=\"%s\" created_at=\"%s\">\n",
builder.WriteString(fmt.Sprintf(" | %d | %s | %s | %s | `%s` | %s | %s |\n", doc.KnowledgeID, doc.Type, fileSize, doc.CreatedAt))
j+1, docName, doc.Type, doc.CreatedAt, doc.KnowledgeID, fileSize, summary)) b.WriteString(fmt.Sprintf("<name>%s</name>\n", docName))
if doc.Description != "" {
summary := formatDocSummary(doc.Description, 120)
b.WriteString(fmt.Sprintf("<summary>%s</summary>\n", summary))
}
b.WriteString("</document>\n")
} }
b.WriteString("</recent_documents>\n")
} }
builder.WriteString("\n")
} }
builder.WriteString("\n") b.WriteString("</knowledge_base>\n")
} }
return builder.String() b.WriteString("</knowledge_bases>")
return b.String()
} }
// renderPromptPlaceholders renders placeholders in the prompt template // renderPromptPlaceholders renders placeholders in the prompt template
+19 -28
View File
@@ -374,7 +374,7 @@ func (t *GrepChunksTool) searchChunks(
return results, int64(len(results)), nil return results, int64(len(results)), nil
} }
// formatOutput formats the search results for display (grep-style output) // formatOutput formats the search results as XML
func (t *GrepChunksTool) formatOutput( func (t *GrepChunksTool) formatOutput(
ctx context.Context, ctx context.Context,
results []knowledgeAggregation, results []knowledgeAggregation,
@@ -382,46 +382,37 @@ func (t *GrepChunksTool) formatOutput(
patterns []string, patterns []string,
countOnly bool, countOnly bool,
) string { ) string {
var output strings.Builder var b strings.Builder
// If count_only mode, just return the count
if countOnly { if countOnly {
output.WriteString(fmt.Sprintf("%d\n", totalCount)) b.WriteString(fmt.Sprintf("<grep_results count=\"%d\" />\n", totalCount))
return output.String() return b.String()
} }
// Show search info b.WriteString(fmt.Sprintf("<grep_results match_count=\"%d\">\n", len(results)))
if len(patterns) == 1 { for _, p := range patterns {
output.WriteString(fmt.Sprintf("Pattern: '%s' (case-insensitive)\n", patterns[0])) b.WriteString(fmt.Sprintf("<pattern>%s</pattern>\n", p))
} else {
output.WriteString(fmt.Sprintf("Patterns (%d): %v (case-insensitive, OR logic)\n", len(patterns), patterns))
} }
output.WriteString(fmt.Sprintf("Matches: %d knowledge item(s)\n\n", len(results)))
if len(results) == 0 { if len(results) == 0 {
output.WriteString("No matches found.\n") b.WriteString("</grep_results>")
return output.String() return b.String()
} }
for idx, result := range results { for _, result := range results {
var patternSummaries []string b.WriteString(fmt.Sprintf("<match knowledge_id=\"%s\" title=\"%s\" chunk_hits=\"%d\" chunk_total=\"%d\">\n",
result.KnowledgeID, result.KnowledgeTitle, result.ChunkHitCount, result.TotalChunkCount))
for _, pattern := range patterns { for _, pattern := range patterns {
count := result.PatternCounts[pattern] count := result.PatternCounts[pattern]
patternSummaries = append(patternSummaries, fmt.Sprintf("%s=%d", pattern, count)) if count > 0 {
b.WriteString(fmt.Sprintf("<pattern_hit pattern=\"%s\" count=\"%d\" />\n", pattern, count))
}
} }
b.WriteString("</match>\n")
output.WriteString(
fmt.Sprintf("%d) knowledge_id=%s | title=%s | chunk_hits=%d | chunk_total=%d | pattern_hits=[%s]\n",
idx+1,
result.KnowledgeID,
result.KnowledgeTitle,
result.ChunkHitCount,
result.TotalChunkCount,
strings.Join(patternSummaries, ", "),
),
)
} }
return output.String()
b.WriteString("</grep_results>")
return b.String()
} }
type knowledgeAggregation struct { type knowledgeAggregation struct {
+68 -106
View File
@@ -395,6 +395,21 @@ func (t *KnowledgeSearchTool) Execute(ctx context.Context, args json.RawMessage)
} }
} }
// Enrich image info for search results (lazy-loaded from child image chunks)
if t.chunkService != nil && len(deduplicatedResults) > 0 {
byTenant := make(map[uint64][]*types.SearchResult)
for _, r := range deduplicatedResults {
tid := t.searchTargets.GetTenantIDForKB(r.KnowledgeBaseID)
if tid == 0 {
continue
}
byTenant[tid] = append(byTenant[tid], r.SearchResult)
}
for tid, batch := range byTenant {
searchutil.EnrichSearchResultsImageInfo(ctx, t.chunkService.GetRepository(), tid, batch)
}
}
// Build output // Build output
logger.Infof(ctx, "[Tool][KnowledgeSearch] Formatting output with %d final results", len(deduplicatedResults)) logger.Infof(ctx, "[Tool][KnowledgeSearch] Formatting output with %d final results", len(deduplicatedResults))
result, err := t.formatOutput(ctx, deduplicatedResults, kbIDs, queries) result, err := t.formatOutput(ctx, deduplicatedResults, kbIDs, queries)
@@ -1065,144 +1080,106 @@ func (t *KnowledgeSearchTool) formatOutput(
}, nil }, nil
} }
// Build output header
output := "=== Search Results ===\n"
output += fmt.Sprintf("Found %d relevant results", len(results))
output += "\n\n"
// Count results by KB // Count results by KB
kbCounts := make(map[string]int) kbCounts := make(map[string]int)
for _, r := range results { for _, r := range results {
kbCounts[r.KnowledgeID]++ kbCounts[r.KnowledgeID]++
} }
output += "Knowledge Base Coverage:\n" // Format individual results as XML
for kbID, count := range kbCounts { var ob strings.Builder
output += fmt.Sprintf(" - %s: %d results\n", kbID, count) ob.WriteString(fmt.Sprintf("<search_results count=\"%d\">\n", len(results)))
}
output += "\n=== Detailed Results ===\n\n"
// Format individual results
formattedResults := make([]map[string]interface{}, 0, len(results)) formattedResults := make([]map[string]interface{}, 0, len(results))
currentKB := ""
faqMetadataCache := make(map[string]*types.FAQChunkMetadata) faqMetadataCache := make(map[string]*types.FAQChunkMetadata)
// Track chunks per knowledge for statistics knowledgeChunkMap := make(map[string]map[int]bool)
knowledgeChunkMap := make(map[string]map[int]bool) // knowledge_id -> set of chunk_index knowledgeTotalMap := make(map[string]int64)
knowledgeTotalMap := make(map[string]int64) // knowledge_id -> total chunks knowledgeTitleMap := make(map[string]string)
knowledgeTitleMap := make(map[string]string) // knowledge_id -> title
for i, result := range results { for i, result := range results {
var faqMeta *types.FAQChunkMetadata var faqMeta *types.FAQChunkMetadata
if result.KnowledgeBaseType == types.KnowledgeBaseTypeFAQ { if result.KnowledgeBaseType == types.KnowledgeBaseTypeFAQ {
meta, err := t.getFAQMetadata(ctx, result.ID, faqMetadataCache) meta, err := t.getFAQMetadata(ctx, result.ID, faqMetadataCache)
if err != nil { if err != nil {
logger.Warnf( logger.Warnf(ctx, "[Tool][KnowledgeSearch] Failed to load FAQ metadata for chunk %s: %v", result.ID, err)
ctx,
"[Tool][KnowledgeSearch] Failed to load FAQ metadata for chunk %s: %v",
result.ID,
err,
)
} else { } else {
faqMeta = meta faqMeta = meta
} }
} }
// Track chunk indices per knowledge
if knowledgeChunkMap[result.KnowledgeID] == nil { if knowledgeChunkMap[result.KnowledgeID] == nil {
knowledgeChunkMap[result.KnowledgeID] = make(map[int]bool) knowledgeChunkMap[result.KnowledgeID] = make(map[int]bool)
} }
knowledgeChunkMap[result.KnowledgeID][result.ChunkIndex] = true knowledgeChunkMap[result.KnowledgeID][result.ChunkIndex] = true
knowledgeTitleMap[result.KnowledgeID] = result.KnowledgeTitle knowledgeTitleMap[result.KnowledgeID] = result.KnowledgeTitle
// Group by knowledge base // Cache total chunk count per knowledge
if result.KnowledgeID != currentKB { if _, exists := knowledgeTotalMap[result.KnowledgeID]; !exists {
currentKB = result.KnowledgeID effectiveTenantID := t.searchTargets.GetTenantIDForKB(result.KnowledgeBaseID)
if i > 0 { if effectiveTenantID == 0 {
output += "\n" logger.Warnf(ctx, "[Tool][KnowledgeSearch] KB %s not found in searchTargets, skipping chunk count", result.KnowledgeBaseID)
} knowledgeTotalMap[result.KnowledgeID] = 0
output += fmt.Sprintf("[Source Document: %s]\n", result.KnowledgeTitle) } else {
_, total, err := t.chunkService.GetRepository().ListPagedChunksByKnowledgeID(ctx,
// Get total chunk count for this knowledge (cache it) effectiveTenantID, result.KnowledgeID,
// Use KB's tenant_id from searchTargets to support cross-tenant shared KB &types.Pagination{Page: 1, PageSize: 1},
if _, exists := knowledgeTotalMap[result.KnowledgeID]; !exists { []types.ChunkType{types.ChunkTypeText}, "", "", "", "", "",
// Get tenant_id from searchTargets using the KB ID from the result )
effectiveTenantID := t.searchTargets.GetTenantIDForKB(result.KnowledgeBaseID) if err != nil {
if effectiveTenantID == 0 { logger.Warnf(ctx, "[Tool][KnowledgeSearch] Failed to get total chunks for knowledge %s: %v", result.KnowledgeID, err)
logger.Warnf(ctx, "[Tool][KnowledgeSearch] KB %s not found in searchTargets, skipping chunk count", result.KnowledgeBaseID)
knowledgeTotalMap[result.KnowledgeID] = 0 knowledgeTotalMap[result.KnowledgeID] = 0
} else { } else {
_, total, err := t.chunkService.GetRepository().ListPagedChunksByKnowledgeID(ctx, knowledgeTotalMap[result.KnowledgeID] = total
effectiveTenantID, result.KnowledgeID,
&types.Pagination{Page: 1, PageSize: 1},
[]types.ChunkType{types.ChunkTypeText}, "", "", "", "", "",
)
if err != nil {
logger.Warnf(
ctx,
"[Tool][KnowledgeSearch] Failed to get total chunks for knowledge %s: %v",
result.KnowledgeID,
err,
)
knowledgeTotalMap[result.KnowledgeID] = 0
} else {
knowledgeTotalMap[result.KnowledgeID] = total
}
} }
} }
} }
// relevanceLevel := GetRelevanceLevel(result.Score) ob.WriteString(fmt.Sprintf("<result id=\"%d\" chunk_id=\"%s\" chunk_index=\"%d\" knowledge_id=\"%s\" source=\"%s\">\n",
output += fmt.Sprintf("\nResult #%d:\n", i+1) i+1, result.ID, result.ChunkIndex, result.KnowledgeID, result.KnowledgeTitle))
output += fmt.Sprintf( ob.WriteString(fmt.Sprintf("<content>%s</content>\n", result.Content))
" [chunk_id: %s][chunk_index: %d]\nContent: %s\n",
result.ID,
result.ChunkIndex,
result.Content,
)
// 解析并输出关联的图片信息
if result.ImageInfo != "" { if result.ImageInfo != "" {
var imageInfos []types.ImageInfo var imageInfos []types.ImageInfo
if err := json.Unmarshal([]byte(result.ImageInfo), &imageInfos); err == nil && len(imageInfos) > 0 { if err := json.Unmarshal([]byte(result.ImageInfo), &imageInfos); err == nil && len(imageInfos) > 0 {
output += fmt.Sprintf(" Related Images (%d):\n", len(imageInfos)) for _, img := range imageInfos {
for imgIdx, img := range imageInfos { ob.WriteString(fmt.Sprintf("<image url=\"%s\">\n", img.URL))
output += fmt.Sprintf(" Image %d:\n", imgIdx+1)
if img.URL != "" {
output += fmt.Sprintf(" URL: %s\n", img.URL)
}
if img.Caption != "" { if img.Caption != "" {
output += fmt.Sprintf(" Caption: %s\n", img.Caption) ob.WriteString(fmt.Sprintf("<image_caption>%s</image_caption>\n", img.Caption))
} }
if img.OCRText != "" { if img.OCRText != "" {
output += fmt.Sprintf(" OCR Text: %s\n", img.OCRText) ob.WriteString(fmt.Sprintf("<image_ocr>%s</image_ocr>\n", img.OCRText))
} }
ob.WriteString("</image>\n")
} }
} }
} }
if faqMeta != nil { if faqMeta != nil {
ob.WriteString("<faq>\n")
if faqMeta.StandardQuestion != "" { if faqMeta.StandardQuestion != "" {
output += fmt.Sprintf(" FAQ Standard Question: %s\n", faqMeta.StandardQuestion) ob.WriteString(fmt.Sprintf("<question>%s</question>\n", faqMeta.StandardQuestion))
} }
if len(faqMeta.SimilarQuestions) > 0 { if len(faqMeta.SimilarQuestions) > 0 {
output += fmt.Sprintf(" FAQ Similar Questions: %s\n", strings.Join(faqMeta.SimilarQuestions, "; ")) for _, sq := range faqMeta.SimilarQuestions {
} ob.WriteString(fmt.Sprintf("<similar_question>%s</similar_question>\n", sq))
if len(faqMeta.Answers) > 0 {
output += " FAQ Answers:\n"
for ansIdx, ans := range faqMeta.Answers {
output += fmt.Sprintf(" Answer Choice %d: %s\n", ansIdx+1, ans)
} }
} }
if len(faqMeta.Answers) > 0 {
for _, ans := range faqMeta.Answers {
ob.WriteString(fmt.Sprintf("<answer>%s</answer>\n", ans))
}
}
ob.WriteString("</faq>\n")
} }
ob.WriteString("</result>\n")
formattedResults = append(formattedResults, map[string]interface{}{ formattedResults = append(formattedResults, map[string]interface{}{
"result_index": i + 1, "result_index": i + 1,
"chunk_id": result.ID, "chunk_id": result.ID,
"content": result.Content, "content": result.Content,
// "score": result.Score,
// "relevance_level": relevanceLevel,
"knowledge_id": result.KnowledgeID, "knowledge_id": result.KnowledgeID,
"knowledge_title": result.KnowledgeTitle, "knowledge_title": result.KnowledgeTitle,
"match_type": result.MatchType, "match_type": result.MatchType,
@@ -1213,11 +1190,9 @@ func (t *KnowledgeSearchTool) formatOutput(
last := formattedResults[len(formattedResults)-1] last := formattedResults[len(formattedResults)-1]
// 添加图片信息到结构化数据
if result.ImageInfo != "" { if result.ImageInfo != "" {
var imageInfos []types.ImageInfo var imageInfos []types.ImageInfo
if err := json.Unmarshal([]byte(result.ImageInfo), &imageInfos); err == nil && len(imageInfos) > 0 { if err := json.Unmarshal([]byte(result.ImageInfo), &imageInfos); err == nil && len(imageInfos) > 0 {
// 构建简化的图片信息列表
imageList := make([]map[string]string, 0, len(imageInfos)) imageList := make([]map[string]string, 0, len(imageInfos))
for _, img := range imageInfos { for _, img := range imageInfos {
imgData := make(map[string]string) imgData := make(map[string]string)
@@ -1253,36 +1228,23 @@ func (t *KnowledgeSearchTool) formatOutput(
} }
} }
// Add statistics and recommendations for each knowledge // Retrieval statistics
output += "\n=== Retrieval Statistics ===\n\n" ob.WriteString("<retrieval_statistics>\n")
for knowledgeID, retrievedChunks := range knowledgeChunkMap { for knowledgeID, retrievedChunks := range knowledgeChunkMap {
totalChunks := knowledgeTotalMap[knowledgeID] totalChunks := knowledgeTotalMap[knowledgeID]
retrievedCount := len(retrievedChunks) retrievedCount := len(retrievedChunks)
title := knowledgeTitleMap[knowledgeID] title := knowledgeTitleMap[knowledgeID]
if totalChunks > 0 { if totalChunks > 0 {
percentage := float64(retrievedCount) / float64(totalChunks) * 100
remaining := totalChunks - int64(retrievedCount) remaining := totalChunks - int64(retrievedCount)
percentage := float64(retrievedCount) / float64(totalChunks) * 100
output += fmt.Sprintf("Document: %s (%s)\n", title, knowledgeID) ob.WriteString(fmt.Sprintf("<document_stat knowledge_id=\"%s\" title=\"%s\" total_chunks=\"%d\" retrieved=\"%d\" remaining=\"%d\" coverage=\"%.1f%%\" />\n",
output += fmt.Sprintf(" Total Chunks: %d\n", totalChunks) knowledgeID, title, totalChunks, retrievedCount, remaining, percentage))
output += fmt.Sprintf(" Retrieved: %d (%.1f%%)\n", retrievedCount, percentage)
output += fmt.Sprintf(" Remaining: %d\n", remaining)
} }
} }
ob.WriteString("</retrieval_statistics>\n")
ob.WriteString("</search_results>")
// // Add usage guidance output := ob.String()
// output += "\n\n=== Usage Guidelines ===\n"
// output += "- High relevance (>=0.8): directly usable for answering\n"
// output += "- Medium relevance (0.6-0.8): use as supplementary reference\n"
// output += "- Low relevance (<0.6): use with caution, may not be accurate\n"
// if totalBeforeFilter > len(results) {
// output += "- Results below threshold have been automatically filtered\n"
// }
// output += "- Full content is already included in search results above\n"
// output += "- Results are deduplicated across knowledge bases and sorted by relevance\n"
// output += "- Use list_knowledge_chunks to expand context if needed\n"
data := map[string]interface{}{ data := map[string]interface{}{
"knowledge_base_ids": kbsToSearch, "knowledge_base_ids": kbsToSearch,
+41 -35
View File
@@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"strings" "strings"
"github.com/Tencent/WeKnora/internal/searchutil"
"github.com/Tencent/WeKnora/internal/types" "github.com/Tencent/WeKnora/internal/types"
"github.com/Tencent/WeKnora/internal/types/interfaces" "github.com/Tencent/WeKnora/internal/types/interfaces"
) )
@@ -160,6 +161,22 @@ func (t *ListKnowledgeChunksTool) Execute(ctx context.Context, args json.RawMess
totalChunks := total totalChunks := total
fetched := len(chunks) fetched := len(chunks)
// Enrich image info from child image chunks (lazy loading)
if fetched > 0 {
chunkIDs := make([]string, 0, fetched)
for _, c := range chunks {
chunkIDs = append(chunkIDs, c.ID)
}
infoMap := searchutil.CollectImageInfoByChunkIDs(ctx, t.chunkService.GetRepository(), effectiveTenantID, chunkIDs)
for _, c := range chunks {
if c.ImageInfo == "" {
if merged, ok := infoMap[c.ID]; ok {
c.ImageInfo = merged
}
}
}
}
knowledgeTitle := t.lookupKnowledgeTitle(ctx, knowledgeID) knowledgeTitle := t.lookupKnowledgeTitle(ctx, knowledgeID)
output := t.buildOutput(knowledgeID, knowledgeTitle, totalChunks, fetched, chunks) output := t.buildOutput(knowledgeID, knowledgeTitle, totalChunks, fetched, chunks)
@@ -236,7 +253,7 @@ func (t *ListKnowledgeChunksTool) lookupKnowledgeTitle(ctx context.Context, know
return strings.TrimSpace(knowledge.Title) return strings.TrimSpace(knowledge.Title)
} }
// buildOutput builds the output for the list knowledge chunks tool // buildOutput builds the output as XML for the list knowledge chunks tool
func (t *ListKnowledgeChunksTool) buildOutput( func (t *ListKnowledgeChunksTool) buildOutput(
knowledgeID string, knowledgeID string,
knowledgeTitle string, knowledgeTitle string,
@@ -244,65 +261,54 @@ func (t *ListKnowledgeChunksTool) buildOutput(
fetched int, fetched int,
chunks []*types.Chunk, chunks []*types.Chunk,
) string { ) string {
builder := &strings.Builder{} var b strings.Builder
builder.WriteString("=== Knowledge Document Chunks ===\n\n")
titleAttr := ""
if knowledgeTitle != "" { if knowledgeTitle != "" {
fmt.Fprintf(builder, "Document: %s (%s)\n", knowledgeTitle, knowledgeID) titleAttr = fmt.Sprintf(" title=\"%s\"", knowledgeTitle)
} else {
fmt.Fprintf(builder, "Document ID: %s\n", knowledgeID)
} }
fmt.Fprintf(builder, "Total chunks: %d\n", total) fmt.Fprintf(&b, "<knowledge_chunks knowledge_id=\"%s\"%s total=\"%d\" fetched=\"%d\">\n",
knowledgeID, titleAttr, total, fetched)
if fetched == 0 { if fetched == 0 {
builder.WriteString("No chunks found. Please confirm the document has been parsed.\n") b.WriteString("</knowledge_chunks>")
if total > 0 { return b.String()
builder.WriteString("Document exists but the current page is empty. Please check pagination parameters.\n")
}
return builder.String()
} }
fmt.Fprintf(
builder,
"Fetched: %d chunks, range: %d - %d\n\n",
fetched,
chunks[0].ChunkIndex,
chunks[len(chunks)-1].ChunkIndex,
)
builder.WriteString("=== Chunk Content Preview ===\n\n") for _, c := range chunks {
for idx, c := range chunks { fmt.Fprintf(&b, "<chunk chunk_id=\"%s\" chunk_index=\"%d\" type=\"%s\">\n",
fmt.Fprintf(builder, "Chunk #%d (Index %d)\n", idx+1, c.ChunkIndex+1) c.ID, c.ChunkIndex, c.ChunkType)
fmt.Fprintf(builder, " chunk_id: %s\n", c.ID) fmt.Fprintf(&b, "<content>%s</content>\n", summarizeContent(c.Content))
fmt.Fprintf(builder, " Type: %s\n", c.ChunkType)
fmt.Fprintf(builder, " Content: %s\n", summarizeContent(c.Content))
// Output associated image information
if c.ImageInfo != "" { if c.ImageInfo != "" {
var imageInfos []types.ImageInfo var imageInfos []types.ImageInfo
if err := json.Unmarshal([]byte(c.ImageInfo), &imageInfos); err == nil && len(imageInfos) > 0 { if err := json.Unmarshal([]byte(c.ImageInfo), &imageInfos); err == nil && len(imageInfos) > 0 {
fmt.Fprintf(builder, " Associated images (%d):\n", len(imageInfos)) for _, img := range imageInfos {
for imgIdx, img := range imageInfos {
fmt.Fprintf(builder, " Image %d:\n", imgIdx+1)
if img.URL != "" { if img.URL != "" {
fmt.Fprintf(builder, " URL: %s\n", img.URL) fmt.Fprintf(&b, "<image url=\"%s\">\n", img.URL)
} else {
b.WriteString("<image>\n")
} }
if img.Caption != "" { if img.Caption != "" {
fmt.Fprintf(builder, " Caption: %s\n", img.Caption) fmt.Fprintf(&b, "<image_caption>%s</image_caption>\n", img.Caption)
} }
if img.OCRText != "" { if img.OCRText != "" {
fmt.Fprintf(builder, " OCR Text: %s\n", img.OCRText) fmt.Fprintf(&b, "<image_ocr>%s</image_ocr>\n", img.OCRText)
} }
b.WriteString("</image>\n")
} }
} }
} }
builder.WriteString("\n")
b.WriteString("</chunk>\n")
} }
if int64(fetched) < total { if int64(fetched) < total {
builder.WriteString("Note: The document has more chunks. Adjust offset or make multiple calls to retrieve all content.\n") fmt.Fprintf(&b, "<pagination remaining=\"%d\" />\n", int64(total)-int64(fetched))
} }
return builder.String() b.WriteString("</knowledge_chunks>")
return b.String()
} }
// summarizeContent summarizes the content of a chunk // summarizeContent summarizes the content of a chunk