Update file_insert_lines

Renamed `start_line` parameter to `position`
This commit is contained in:
Matt Low 2023-11-26 20:42:31 +00:00
parent 3b20e00330
commit a805c92131

View File

@ -179,16 +179,16 @@ var AvailableTools = map[string]AvailableTool{
Type: "string",
Description: "Path of the file to be modified, relative to the current working directory.",
},
"start_line": {
"position": {
Type: "integer",
Description: `Which line to begin inserting lines at (existing line will be moved to after content).`,
Description: `Which line to insert content *before*.`,
},
"content": {
Type: "string",
Description: `The content to insert.`,
},
},
Required: []string{"path", "start", "content"},
Required: []string{"path", "position", "content"},
},
}},
Impl: func(args map[string]interface{}) (string, error) {
@ -200,14 +200,14 @@ var AvailableTools = map[string]AvailableTool{
if !ok {
return "", fmt.Errorf("Invalid path in function arguments: %v", tmp)
}
var start_line int
tmp, ok = args["start_line"]
var position int
tmp, ok = args["position"]
if ok {
tmp, ok := tmp.(float64)
if !ok {
return "", fmt.Errorf("Invalid start_line in function arguments: %v", tmp)
return "", fmt.Errorf("Invalid position in function arguments: %v", tmp)
}
start_line = int(tmp)
position = int(tmp)
}
var content string
tmp, ok = args["content"]
@ -217,7 +217,7 @@ var AvailableTools = map[string]AvailableTool{
return "", fmt.Errorf("Invalid content in function arguments: %v", tmp)
}
}
return FileInsertLines(path, start_line, content), nil
return FileInsertLines(path, position, content), nil
},
},
"file_replace_lines": {
@ -484,7 +484,7 @@ func WriteFile(path string, content string) string {
return resultToJson(FunctionResult{})
}
func FileInsertLines(path string, startLine int, content string) string {
func FileInsertLines(path string, position int, content string) string {
ok, res := isPathWithinCWD(path)
if !ok {
return resultToJson(*res)
@ -503,15 +503,15 @@ func FileInsertLines(path string, startLine int, content string) string {
data = []byte{}
}
if startLine < 1 {
if position < 1 {
return resultToJson(FunctionResult{Message: "start_line cannot be less than 1"})
}
lines := strings.Split(string(data), "\n")
contentLines := strings.Split(strings.Trim(content, "\n"), "\n")
before := lines[:startLine-1]
after := lines[startLine-1:]
before := lines[:position-1]
after := lines[position-1:]
lines = append(before, append(contentLines, after...)...)
newContent := strings.Join(lines, "\n")