fix: open agent file links that carry line-range selectors (#1412)
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
|
||||
"github.com/kandev/kandev/internal/agentctl/server/adapter/transport/shared"
|
||||
"github.com/kandev/kandev/internal/agentctl/types/streams"
|
||||
"github.com/kandev/kandev/internal/common/readselector"
|
||||
)
|
||||
|
||||
// Tool operation type constants.
|
||||
@@ -313,9 +314,24 @@ func updateModifyFileInput(mf *streams.ModifyFilePayload, supplemental, inputMap
|
||||
}
|
||||
}
|
||||
|
||||
// updateReadFileInput fills a ReadFile payload from a tool_call_update: it sets
|
||||
// FilePath (when still empty) to the selector-stripped path and populates
|
||||
// Offset/Limit from the parsed line range when those fields are unset.
|
||||
func updateReadFileInput(rf *streams.ReadFilePayload, supplemental, inputMap map[string]any) {
|
||||
if path := pathFromArgs(supplemental, inputMap); path != "" && rf.FilePath == "" {
|
||||
rf.FilePath = path
|
||||
// Parse the selector on every update — a later tool_call_update can carry the
|
||||
// line range (e.g. "main.go:50+150") even when an earlier frame already set
|
||||
// FilePath, so range metadata must not be gated on FilePath being empty.
|
||||
if path := pathFromArgs(supplemental, inputMap); path != "" {
|
||||
clean, startLine, lineCount := readselector.Split(path)
|
||||
if rf.FilePath == "" {
|
||||
rf.FilePath = clean
|
||||
}
|
||||
if rf.Offset == 0 {
|
||||
rf.Offset = startLine
|
||||
}
|
||||
if rf.Limit == 0 {
|
||||
rf.Limit = lineCount
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -383,13 +399,11 @@ func (n *Normalizer) normalizeEdit(args map[string]any) *streams.NormalizedPaylo
|
||||
Type: streams.MutationPatch,
|
||||
}
|
||||
|
||||
// Add line numbers if available
|
||||
if startLine, ok := rawInput["old_str_start_line_number_1"].(float64); ok {
|
||||
mutation.StartLine = int(startLine)
|
||||
}
|
||||
if endLine, ok := rawInput["old_str_end_line_number_1"].(float64); ok {
|
||||
mutation.EndLine = int(endLine)
|
||||
}
|
||||
// Line numbers vary by agent: Claude's str-replace editor uses
|
||||
// old_str_*_line_number_1, OMP uses startLine/endLine. Accept both
|
||||
// (plus snake_case) so edit links can navigate to the changed lines.
|
||||
mutation.StartLine = firstInt(rawInput, "old_str_start_line_number_1", "startLine", "start_line")
|
||||
mutation.EndLine = firstInt(rawInput, "old_str_end_line_number_1", "endLine", "end_line")
|
||||
|
||||
// Generate unified diff when at least one string is provided
|
||||
if oldStr != "" || newStr != "" {
|
||||
@@ -403,6 +417,18 @@ func (n *Normalizer) normalizeEdit(args map[string]any) *streams.NormalizedPaylo
|
||||
return streams.NewModifyFile(path, mutations)
|
||||
}
|
||||
|
||||
// firstInt returns the first key whose value is a non-zero int, handling JSON
|
||||
// numbers (decoded as float64). Line/column numbers are 1-based, so a missing
|
||||
// or zero value is treated as absent.
|
||||
func firstInt(m map[string]any, keys ...string) int {
|
||||
for _, k := range keys {
|
||||
if v := shared.GetInt(m, k); v != 0 {
|
||||
return v
|
||||
}
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// normalizeRead converts ACP read tool data.
|
||||
// If rawInput.type is "directory", this becomes a code search (file listing) operation.
|
||||
func (n *Normalizer) normalizeRead(args map[string]any) *streams.NormalizedPayload {
|
||||
@@ -412,13 +438,17 @@ func (n *Normalizer) normalizeRead(args map[string]any) *streams.NormalizedPaylo
|
||||
}
|
||||
|
||||
path := pathFromArgs(args, rawInput)
|
||||
// OMP's read tool embeds a line/range/mode selector in the path
|
||||
// (e.g. "foo.go:43-94"); strip it so the file link stays openable and
|
||||
// carry the parsed range via offset/limit. No-op for other agents.
|
||||
path, startLine, lineCount := readselector.Split(path)
|
||||
|
||||
// Check if this is a directory read - treat as code search (file listing)
|
||||
if readType := shared.GetString(rawInput, "type"); readType == readTypeDirectory {
|
||||
return streams.NewCodeSearch("", "", path, "")
|
||||
}
|
||||
|
||||
return streams.NewReadFile(path, 0, 0)
|
||||
return streams.NewReadFile(path, startLine, lineCount)
|
||||
}
|
||||
|
||||
// normalizeExecute converts ACP execute/bash tool data.
|
||||
|
||||
@@ -393,6 +393,40 @@ func TestNormalizerEdit(t *testing.T) {
|
||||
t.Errorf("expected FilePath 'src/main.go', got %q", result.ModifyFile().FilePath)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("extracts omp startLine/endLine into mutation", func(t *testing.T) {
|
||||
result := normalizer.NormalizeToolCall("edit", map[string]any{
|
||||
"kind": "edit",
|
||||
"raw_input": map[string]any{
|
||||
"path": "main.go",
|
||||
"old_str_1": "a",
|
||||
"new_str_1": "b",
|
||||
"startLine": float64(43),
|
||||
"endLine": float64(94),
|
||||
},
|
||||
})
|
||||
m := result.ModifyFile().Mutations[0]
|
||||
if m.StartLine != 43 || m.EndLine != 94 {
|
||||
t.Errorf("expected StartLine=43 EndLine=94, got %d/%d", m.StartLine, m.EndLine)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("still extracts claude old_str line numbers", func(t *testing.T) {
|
||||
result := normalizer.NormalizeToolCall("edit", map[string]any{
|
||||
"kind": "edit",
|
||||
"raw_input": map[string]any{
|
||||
"path": "main.go",
|
||||
"old_str_1": "a",
|
||||
"new_str_1": "b",
|
||||
"old_str_start_line_number_1": float64(10),
|
||||
"old_str_end_line_number_1": float64(12),
|
||||
},
|
||||
})
|
||||
m := result.ModifyFile().Mutations[0]
|
||||
if m.StartLine != 10 || m.EndLine != 12 {
|
||||
t.Errorf("expected StartLine=10 EndLine=12, got %d/%d", m.StartLine, m.EndLine)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// TestNormalizerRead tests the normalizer's read handling.
|
||||
@@ -453,6 +487,59 @@ func TestNormalizerRead(t *testing.T) {
|
||||
t.Errorf("expected Path '.', got %q", result.CodeSearch().Path)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("strips omp line selector from path and records range", func(t *testing.T) {
|
||||
args := map[string]any{
|
||||
"kind": "read",
|
||||
"raw_input": map[string]any{
|
||||
"path": "apps/backend/internal/sentry/handlers.go:43-94",
|
||||
},
|
||||
}
|
||||
result := normalizer.NormalizeToolCall("read", args)
|
||||
rf := result.ReadFile()
|
||||
if rf == nil {
|
||||
t.Fatal("expected ReadFile to be set")
|
||||
}
|
||||
if rf.FilePath != "apps/backend/internal/sentry/handlers.go" {
|
||||
t.Errorf("expected stripped FilePath, got %q", rf.FilePath)
|
||||
}
|
||||
if rf.Offset != 43 || rf.Limit != 52 {
|
||||
t.Errorf("expected Offset=43 Limit=52, got Offset=%d Limit=%d", rf.Offset, rf.Limit)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("strips omp selector arriving on a tool_call_update", func(t *testing.T) {
|
||||
payload := normalizer.NormalizeToolCall("read", map[string]any{"kind": "read"})
|
||||
normalizer.UpdatePayloadInput(payload, map[string]any{"path": "main.go:50+150"}, nil)
|
||||
rf := payload.ReadFile()
|
||||
if rf == nil {
|
||||
t.Fatal("expected ReadFile to be set")
|
||||
}
|
||||
if rf.FilePath != "main.go" {
|
||||
t.Errorf("expected stripped FilePath 'main.go', got %q", rf.FilePath)
|
||||
}
|
||||
if rf.Offset != 50 || rf.Limit != 150 {
|
||||
t.Errorf("expected Offset=50 Limit=150, got Offset=%d Limit=%d", rf.Offset, rf.Limit)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("populates range from update even when FilePath already set", func(t *testing.T) {
|
||||
payload := normalizer.NormalizeToolCall("read", map[string]any{
|
||||
"kind": "read",
|
||||
"raw_input": map[string]any{"path": "main.go"},
|
||||
})
|
||||
if payload.ReadFile().FilePath != "main.go" {
|
||||
t.Fatalf("expected initial FilePath 'main.go', got %q", payload.ReadFile().FilePath)
|
||||
}
|
||||
normalizer.UpdatePayloadInput(payload, map[string]any{"path": "main.go:50+150"}, nil)
|
||||
rf := payload.ReadFile()
|
||||
if rf.FilePath != "main.go" {
|
||||
t.Errorf("expected FilePath unchanged 'main.go', got %q", rf.FilePath)
|
||||
}
|
||||
if rf.Offset != 50 || rf.Limit != 150 {
|
||||
t.Errorf("expected Offset=50 Limit=150 from update, got Offset=%d Limit=%d", rf.Offset, rf.Limit)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// TestNormalizerResult tests the normalizer's result handling.
|
||||
|
||||
Reference in New Issue
Block a user