Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.
Go agentagenticaichatbotdsh-pluginembeddingsevaluationgenerative-aigolangknowledge-basellmmulti-tenantollamaopenaiquestion-answeringragrerankingsemantic-searchvector-searchwiki
fix(agent): show matches in long file search lines (#4774)
ydflow04bd71e5a008
diff --git a/internal/agent/tools/read_file_query_snippet_test.go b/internal/agent/tools/read_file_query_snippet_test.gonew file mode 100644index 000000000..c7a79791d--- /dev/null+++ b/internal/agent/tools/read_file_query_snippet_test.go@@ -0,0 +1,55 @@+package tools++import (+ "encoding/json"+ "regexp"+ "strings"+ "testing"+ "unicode/utf8"++ "github.com/stretchr/testify/require"+)++func TestReadFileQueryShowsMatchInLongLine(t *testing.T) {+ for _, tc := range []struct {+ name, prefix, query, match string+ regex bool+ }{+ {name: "literal ASCII", prefix: strings.Repeat("x", 500), query: "needle", match: "NEEDLE"},+ {name: "Unicode prefix", prefix: strings.Repeat("章节", 300), query: "needle", match: "NEEDLE"},+ {name: "regex", prefix: strings.Repeat("x", 500), query: `ERR-[0-9]+`, match: "ERR-123", regex: true},+ } {+ t.Run(tc.name, func(t *testing.T) {+ reader := NewReadFileTool(nil).WithArtifacts(queryLimitFileSource(tc.prefix + tc.match + " tail\n"))+ args, err := json.Marshal(ReadFileInput{Path: "resource://example", Query: tc.query, Regex: tc.regex})+ require.NoError(t, err)+ result, err := reader.Execute(t.Context(), args)+ require.NoError(t, err)+ require.True(t, result.Success, result.Error)+ require.Equal(t, 1, result.Data["match_count"])+ require.Contains(t, result.Output, tc.match)+ })+ }+}++func TestReadFileMatchSnippetBoundsAndShortLines(t *testing.T) {+ matcher := regexp.MustCompile(`(?i)needle`)+ for _, tc := range []struct {+ name, line string+ }{+ {name: "short", line: "a NEEDLE b"},+ {name: "start", line: "NEEDLE" + strings.Repeat("a", 500)},+ {name: "middle", line: strings.Repeat("章", 500) + "NEEDLE" + strings.Repeat("节", 500)},+ {name: "end", line: strings.Repeat("章", 500) + "NEEDLE"},+ } {+ t.Run(tc.name, func(t *testing.T) {+ got := readFileMatchSnippet(tc.line, []*regexp.Regexp{matcher})+ require.True(t, utf8.ValidString(got))+ require.Contains(t, got, "NEEDLE")+ require.LessOrEqual(t, utf8.RuneCountInString(got), readFileMatchLineRunes+2)+ if tc.name == "short" {+ require.Equal(t, tc.line, got)+ }+ })+ }+}diff --git a/internal/agent/tools/workspace_reader.go b/internal/agent/tools/workspace_reader.goindex 5db2c2210..7a8bdf4ec 100644--- a/internal/agent/tools/workspace_reader.go+++ b/internal/agent/tools/workspace_reader.go@@ -289,10 +289,7 @@ func grepFilePage(if !matchesAll(matchers, lines[i]) {continue}- line := lines[i]- if runes := []rune(line); len(runes) > readFileMatchLineRunes {- line = string(runes[:readFileMatchLineRunes]) + "…"- }+ line := readFileMatchSnippet(lines[i], matchers)entry := fmt.Sprintf("%d: %s\n", i+1, line)cost := utf8.RuneCountInString(entry)if matches >= maxMatches || (matches > 0 && used+cost > budget) {@@ -327,6 +324,37 @@ func grepFilePage(return &types.ToolResult{Success: true, Output: b.String(), Data: resultData}}+// readFileMatchSnippet keeps an actual match visible when a long line is+// shortened. Regexp offsets are bytes; convert them before slicing runes.+func readFileMatchSnippet(line string, matchers []*regexp.Regexp) string {+ runes := []rune(line)+ if len(runes) <= readFileMatchLineRunes {+ return line+ }+ first := len(line)+ end := first+ for _, matcher := range matchers {+ if loc := matcher.FindStringIndex(line); loc != nil && loc[0] < first {+ first, end = loc[0], loc[1]+ }+ }+ startRune, endRune := utf8.RuneCountInString(line[:first]), utf8.RuneCountInString(line[:end])+ start := 0+ if endRune > readFileMatchLineRunes {+ matchLength := min(endRune-startRune, readFileMatchLineRunes)+ start = max(0, startRune-(readFileMatchLineRunes-matchLength)/2)+ start = min(start, len(runes)-readFileMatchLineRunes)+ }+ snippet := string(runes[start : start+readFileMatchLineRunes])+ if start > 0 {+ snippet = "…" + snippet+ }+ if start+readFileMatchLineRunes < len(runes) {+ snippet += "…"+ }+ return snippet+}+// renderFilePage is shared by workspace and skill resources. Content appears// only once, in Output, and pagination obeys the registry's output budget.func renderFilePage(ctx context.Context, input ReadFileInput, data []byte, sessionID, clean, rootDir string) *types.ToolResult {