feat: Enhance agent retrieval strategies and regex handling

- Updated the agent system prompt to emphasize the use of regex for chunk searches, improving retrieval accuracy and efficiency.
- Introduced a new `Capabilities` field in the `KnowledgeBaseInfo` struct to define the retrieval surfaces available for each knowledge base, guiding the agent's strategy selection.
- Enhanced the `grep_chunks` tool to support regex queries, allowing for more flexible and powerful text pattern matching in knowledge base chunks.
- Improved JSON handling in the `RepairJSON` function to address invalid escape sequences, particularly for regex patterns, ensuring robust parsing and error handling.
- Updated documentation and comments across various files to clarify usage and expectations for regex and knowledge base capabilities.

These changes significantly enhance the agent's ability to retrieve relevant information while ensuring the integrity of input data handling.
This commit is contained in:
wizardchen
2026-04-21 19:19:57 +08:00
parent 4022f134bd
commit 37a517612d
9 changed files with 688 additions and 328 deletions
+63
View File
@@ -11,6 +11,8 @@ import (
// - Trailing commas before closing brackets
// - Single quotes instead of double quotes
// - Unquoted keys
// - Invalid backslash escapes inside strings (e.g. regex patterns like
// "C\+\+", "\d+", "\.log$") where the LLM forgot to double-escape.
//
// Returns the repaired JSON string. If repair is not possible,
// returns the original string unchanged (caller should handle parse errors).
@@ -30,6 +32,10 @@ func RepairJSON(s string) string {
}
}
// Fix invalid backslash escapes before anything else, because unbalanced
// strings confuse the comma/bracket trackers below.
s = fixInvalidEscapes(s)
// Fix trailing commas: ,} or ,]
s = fixTrailingCommas(s)
@@ -39,6 +45,63 @@ func RepairJSON(s string) string {
return s
}
// fixInvalidEscapes turns invalid JSON string escapes into literal backslash
// sequences. JSON only allows the escape letters \" \\ \/ \b \f \n \r \t \u;
// any other backslash inside a string (e.g. \+ \d \w \. \| \( \)) is a parse
// error. LLMs frequently emit these when they meant to pass a regex pattern
// without double-escaping — rewriting "\+" to "\\+" recovers the LLM's intent
// (the unmarshalled Go string becomes "\+", which is what the regex engine
// expects) and is idempotent on already-valid JSON.
func fixInvalidEscapes(s string) string {
var out strings.Builder
out.Grow(len(s) + 8)
runes := []rune(s)
inString := false
for i := 0; i < len(runes); i++ {
r := runes[i]
if !inString {
out.WriteRune(r)
if r == '"' {
inString = true
}
continue
}
if r == '"' {
out.WriteRune(r)
inString = false
continue
}
if r != '\\' {
out.WriteRune(r)
continue
}
// Found a backslash inside a string. Peek at the next rune to decide
// whether this is a valid JSON escape.
if i+1 >= len(runes) {
// Dangling backslash at EOF — escape it so we don't leave the
// string in a "pending escape" state for downstream parsers.
out.WriteString(`\\`)
continue
}
next := runes[i+1]
switch next {
case '"', '\\', '/', 'b', 'f', 'n', 'r', 't', 'u':
// Valid JSON escape — pass through unchanged.
out.WriteRune(r)
out.WriteRune(next)
i++
default:
// Invalid escape — most likely a regex metachar that the LLM
// forgot to double-escape. Emit a literal backslash + the next
// rune, so the final Go string preserves the \X sequence.
out.WriteString(`\\`)
out.WriteRune(next)
i++
}
}
return out.String()
}
// fixTrailingCommas removes trailing commas before closing brackets/braces.
func fixTrailingCommas(s string) string {
// Simple state machine to handle strings