MCPcopy Create free account
hub / github.com/53AI/53AIHub / executeEditFile

Function executeEditFile

api/service/tools/executor.go:1074–1162  ·  view source on GitHub ↗
(ctx context.Context, args map[string]interface{})

Source from the content-addressed store, hash-verified

1072}
1073
1074func executeEditFile(ctx context.Context, args map[string]interface{}) (*ToolResult, error) {
1075 path, ok := args["path"].(string)
1076 if !ok || strings.TrimSpace(path) == "" {
1077 return nil, fmt.Errorf("missing path argument")
1078 }
1079 path, err := normalizeSandboxWorkspacePath(path)
1080 if err != nil {
1081 return nil, err
1082 }
1083 oldString, ok := args["old_string"].(string)
1084 if !ok || oldString == "" {
1085 return nil, fmt.Errorf("missing old_string argument")
1086 }
1087 newString, ok := args["new_string"].(string)
1088 if !ok {
1089 return nil, fmt.Errorf("missing new_string argument")
1090 }
1091 replaceAll := false
1092 if v, exists := args["replace_all"]; exists {
1093 replaceAll = parseBoolValue(v)
1094 }
1095
1096 sessionID := resolveSandboxSessionID(ctx, args)
1097 cwd := resolveSandboxCWD(ctx, args)
1098 if _, err := normalizeSandboxRelativePath(cwd); err != nil {
1099 return nil, err
1100 }
1101 if err := ensureSandboxSessionSeeded(ctx, sessionID, cwd); err != nil {
1102 return nil, err
1103 }
1104
1105 readResp, err := getSandboxClient().ReadFile(ctx, sandboxclient.FileReadRequest{
1106 Path: path,
1107 SessionID: sessionID,
1108 Cwd: cwd,
1109 })
1110 if err != nil {
1111 return nil, wrapSandboxServiceError(err)
1112 }
1113
1114 content := readResp.Content
1115 matchCount := strings.Count(content, oldString)
1116 if matchCount == 0 {
1117 return nil, fmt.Errorf("old_string not found in file")
1118 }
1119 if !replaceAll && matchCount > 1 {
1120 return nil, fmt.Errorf("old_string found %d times, set replace_all=true or provide a more specific match", matchCount)
1121 }
1122
1123 newContent := content
1124 replacements := 1
1125 if replaceAll {
1126 newContent = strings.ReplaceAll(content, oldString, newString)
1127 replacements = matchCount
1128 } else {
1129 newContent = strings.Replace(content, oldString, newString, 1)
1130 }
1131

Callers 2

ExecuteToolWithResultFunction · 0.85
ExecuteToolStreamFunction · 0.85

Calls 15

parseBoolValueFunction · 0.85
resolveSandboxSessionIDFunction · 0.85
resolveSandboxCWDFunction · 0.85
getSandboxClientFunction · 0.85
wrapSandboxServiceErrorFunction · 0.85
ErrorfMethod · 0.80

Tested by

no test coverage detected