feat(llm): introduce request-local source reference aliases
Replace raw chunk/document/KB IDs in LLM context with short cN/dN/bN/wN aliases, decode tool arguments on the way in, and expand compact <ref/> citations to the public <kb>/<web> format before user-facing output.
This commit is contained in:
@@ -31,7 +31,7 @@ If neither query nor range is provided, it returns the beginning of the document
|
||||
"properties": {
|
||||
"knowledge_id": {
|
||||
"type": "string",
|
||||
"description": "The ID of the source document to read"
|
||||
"description": "The short dN source document ID from the <sources> block"
|
||||
},
|
||||
"query": {
|
||||
"type": "string",
|
||||
@@ -161,12 +161,27 @@ func (t *wikiReadSourceDocTool) Execute(ctx context.Context, args json.RawMessag
|
||||
}
|
||||
|
||||
var chunksOutput strings.Builder
|
||||
formattedChunks := make([]map[string]interface{}, 0)
|
||||
totalChunks := int64(0)
|
||||
reachedMax := false
|
||||
|
||||
var prevChunk *types.Chunk
|
||||
var forceOutputNext bool
|
||||
outputtedIndices := make(map[int]bool)
|
||||
appendFormattedChunk := func(chunk *types.Chunk, content string) {
|
||||
if chunk == nil {
|
||||
return
|
||||
}
|
||||
formattedChunks = append(formattedChunks, map[string]interface{}{
|
||||
"chunk_id": chunk.ID,
|
||||
"chunk_index": chunk.ChunkIndex,
|
||||
"chunk_type": chunk.ChunkType,
|
||||
"content": content,
|
||||
"knowledge_id": knowledgeID,
|
||||
"knowledge_base": knowledge.KnowledgeBaseID,
|
||||
"knowledge_title": knowledge.Title,
|
||||
})
|
||||
}
|
||||
|
||||
for {
|
||||
pagination := &types.Pagination{
|
||||
@@ -213,6 +228,7 @@ func (t *wikiReadSourceDocTool) Execute(ctx context.Context, args json.RawMessag
|
||||
break
|
||||
}
|
||||
fmt.Fprintf(&chunksOutput, "<chunk index=\"%d\" type=\"range\">\n%s\n</chunk>\n", chunkNum, chunkContent)
|
||||
appendFormattedChunk(c, chunkContent)
|
||||
matchCount++
|
||||
continue
|
||||
}
|
||||
@@ -232,6 +248,7 @@ func (t *wikiReadSourceDocTool) Execute(ctx context.Context, args json.RawMessag
|
||||
if prevChunk != nil && !outputtedIndices[prevChunk.ChunkIndex] {
|
||||
prevContent := enrichChunkContent(prevChunk)
|
||||
fmt.Fprintf(&chunksOutput, "<chunk index=\"%d\" type=\"context_before\">\n%s\n</chunk>\n", prevChunk.ChunkIndex+1, prevContent)
|
||||
appendFormattedChunk(prevChunk, prevContent)
|
||||
outputtedIndices[prevChunk.ChunkIndex] = true
|
||||
}
|
||||
}
|
||||
@@ -242,6 +259,7 @@ func (t *wikiReadSourceDocTool) Execute(ctx context.Context, args json.RawMessag
|
||||
matchAttr = ` type="match"`
|
||||
}
|
||||
fmt.Fprintf(&chunksOutput, "<chunk index=\"%d\"%s>\n%s\n</chunk>\n", c.ChunkIndex+1, matchAttr, chunkContent)
|
||||
appendFormattedChunk(c, chunkContent)
|
||||
outputtedIndices[c.ChunkIndex] = true
|
||||
}
|
||||
|
||||
@@ -251,6 +269,7 @@ func (t *wikiReadSourceDocTool) Execute(ctx context.Context, args json.RawMessag
|
||||
} else if forceOutputNext {
|
||||
if !outputtedIndices[c.ChunkIndex] {
|
||||
fmt.Fprintf(&chunksOutput, "<chunk index=\"%d\" type=\"context_after\">\n%s\n</chunk>\n", c.ChunkIndex+1, chunkContent)
|
||||
appendFormattedChunk(c, chunkContent)
|
||||
outputtedIndices[c.ChunkIndex] = true
|
||||
}
|
||||
forceOutputNext = false
|
||||
@@ -312,5 +331,16 @@ func (t *wikiReadSourceDocTool) Execute(ctx context.Context, args json.RawMessag
|
||||
|
||||
sb.WriteString("</source_document>")
|
||||
|
||||
return &types.ToolResult{Success: true, Output: sb.String()}, nil
|
||||
return &types.ToolResult{
|
||||
Success: true,
|
||||
Output: sb.String(),
|
||||
Data: map[string]interface{}{
|
||||
"display_type": "knowledge_chunks_list",
|
||||
"knowledge_id": knowledgeID,
|
||||
"knowledge_title": knowledge.Title,
|
||||
"total_chunks": totalChunks,
|
||||
"fetched_chunks": len(formattedChunks),
|
||||
"chunks": formattedChunks,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user