Removed file_remove_lines in favor of a generalized file_replace_lines
This commit is contained in:
parent
8e262c4839
commit
3b20e00330
@ -72,9 +72,7 @@ Result is returned as JSON in the following format:
|
||||
|
||||
FILE_INSERT_LINES = `Insert lines into a file.`
|
||||
|
||||
FILE_REPLACE_LINES = `Replace an (inclusive) range of lines within a file.`
|
||||
|
||||
FILE_REMOVE_LINES = `Remove an (inclusive) range of lines from a file.`
|
||||
FILE_REPLACE_LINES = `Replace or remove a range of lines within a file.`
|
||||
)
|
||||
|
||||
var AvailableTools = map[string]AvailableTool{
|
||||
@ -235,18 +233,18 @@ var AvailableTools = map[string]AvailableTool{
|
||||
},
|
||||
"start_line": {
|
||||
Type: "integer",
|
||||
Description: `Line number which specifies the start of the replacement range (inclusive, this line will be replaced)`,
|
||||
Description: `Line number which specifies the start of the replacement range (inclusive).`,
|
||||
},
|
||||
"end_line": {
|
||||
Type: "integer",
|
||||
Description: `Line number which specifies the end of the replacement range (inclusive, this line will be replaced). If unset, will be set to the end of the file.`,
|
||||
Description: `Line number which specifies the end of the replacement range (inclusive). If unset, range extends to end of file.`,
|
||||
},
|
||||
"content": {
|
||||
Type: "string",
|
||||
Description: `Content which will replace specified range.`,
|
||||
Description: `Content to replace specified range. Omit to remove the specified range.`,
|
||||
},
|
||||
},
|
||||
Required: []string{"path", "start_line", "content"},
|
||||
Required: []string{"path", "start_line"},
|
||||
},
|
||||
}},
|
||||
Impl: func(args map[string]interface{}) (string, error) {
|
||||
@ -288,60 +286,6 @@ var AvailableTools = map[string]AvailableTool{
|
||||
return FileReplaceLines(path, start_line, end_line, content), nil
|
||||
},
|
||||
},
|
||||
"file_remove_lines": {
|
||||
Tool: openai.Tool{Type: "function", Function: openai.FunctionDefinition{
|
||||
Name: "file_remove_lines",
|
||||
Description: FILE_REMOVE_LINES,
|
||||
Parameters: FunctionParameters{
|
||||
Type: "object",
|
||||
Properties: map[string]FunctionParameter{
|
||||
"path": {
|
||||
Type: "string",
|
||||
Description: "Path of the file to be modified, relative to the current working directory.",
|
||||
},
|
||||
"start_line": {
|
||||
Type: "integer",
|
||||
Description: `Line number which specifies the start of the removal range (inclusive, this line will be removed)`,
|
||||
},
|
||||
"end_line": {
|
||||
Type: "integer",
|
||||
Description: `Line number which specifies the end of the removal range (inclusive, this line will be removed).`,
|
||||
},
|
||||
},
|
||||
Required: []string{"path", "start_line", "end_line"},
|
||||
},
|
||||
}},
|
||||
Impl: func(args map[string]interface{}) (string, error) {
|
||||
tmp, ok := args["path"]
|
||||
if !ok {
|
||||
return "", fmt.Errorf("path parameter to write_file was not included.")
|
||||
}
|
||||
path, ok := tmp.(string)
|
||||
if !ok {
|
||||
return "", fmt.Errorf("Invalid path in function arguments: %v", tmp)
|
||||
}
|
||||
var start_line int
|
||||
tmp, ok = args["start_line"]
|
||||
if ok {
|
||||
tmp, ok := tmp.(float64)
|
||||
if !ok {
|
||||
return "", fmt.Errorf("Invalid start_line in function arguments: %v", tmp)
|
||||
}
|
||||
start_line = int(tmp)
|
||||
}
|
||||
var end_line int
|
||||
tmp, ok = args["end_line"]
|
||||
if ok {
|
||||
tmp, ok := tmp.(float64)
|
||||
if !ok {
|
||||
return "", fmt.Errorf("Invalid end_line in function arguments: %v", tmp)
|
||||
}
|
||||
end_line = int(tmp)
|
||||
}
|
||||
|
||||
return FileRemoveLines(path, start_line, end_line), nil
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
func resultToJson(result FunctionResult) string {
|
||||
@ -626,44 +570,3 @@ func FileReplaceLines(path string, startLine int, endLine int, content string) s
|
||||
return resultToJson(FunctionResult{Result: newContent})
|
||||
|
||||
}
|
||||
|
||||
func FileRemoveLines(path string, startLine int, endLine int) string {
|
||||
ok, res := isPathWithinCWD(path)
|
||||
if !ok {
|
||||
return resultToJson(*res)
|
||||
}
|
||||
|
||||
// Read the existing file's content
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
if !os.IsNotExist(err) {
|
||||
return resultToJson(FunctionResult{Message: fmt.Sprintf("Could not read path: %s", err.Error())})
|
||||
}
|
||||
_, err = os.Create(path)
|
||||
if err != nil {
|
||||
return resultToJson(FunctionResult{Message: fmt.Sprintf("Could not create new file: %s", err.Error())})
|
||||
}
|
||||
data = []byte{}
|
||||
}
|
||||
|
||||
if startLine < 1 {
|
||||
return resultToJson(FunctionResult{Message: "start_line cannot be less than 1"})
|
||||
}
|
||||
|
||||
lines := strings.Split(string(data), "\n")
|
||||
|
||||
if endLine == 0 || endLine > len(lines) {
|
||||
endLine = len(lines)
|
||||
}
|
||||
|
||||
lines = append(lines[:startLine-1], lines[endLine:]...)
|
||||
newContent := strings.Join(lines, "\n")
|
||||
|
||||
// Join the lines and write back to the file
|
||||
err = os.WriteFile(path, []byte(newContent), 0644)
|
||||
if err != nil {
|
||||
return resultToJson(FunctionResult{Message: fmt.Sprintf("Could not write to path: %s", err.Error())})
|
||||
}
|
||||
|
||||
return resultToJson(FunctionResult{Result: newContent})
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user