| 148 | } |
| 149 | |
| 150 | func (code *codeTools) execute(ctx context.Context, name string, args map[string]any) (Observation, error) { |
| 151 | side, _ := args["side"].(string) |
| 152 | if side == "" { |
| 153 | side = "new" |
| 154 | } |
| 155 | observation := Observation{ToolName: name, Arguments: args, SourceKind: "code", Side: side, Revision: code.comparison.Head, Locations: []Location{}} |
| 156 | if side == "old" { |
| 157 | observation.Revision = code.comparison.Old |
| 158 | } |
| 159 | if name == "file_read_diff" { |
| 160 | observation.Side = "" |
| 161 | observation.Revision = "" |
| 162 | } |
| 163 | if name == "file_read" { |
| 164 | filePath, _ := args["file_path"].(string) |
| 165 | if !code.allowed[side][filePath] { |
| 166 | observation.ErrorClass = "unavailable" |
| 167 | return observation, fmt.Errorf("file is unavailable in the authorized %s scope", side) |
| 168 | } |
| 169 | } |
| 170 | if name == "file_find" { |
| 171 | if err := ctx.Err(); err != nil { |
| 172 | return observation, err |
| 173 | } |
| 174 | paths := slices.Sorted(maps.Keys(code.allowed[side])) |
| 175 | query, _ := args["query_name"].(string) |
| 176 | sensitive, _ := args["case_sensitive"].(bool) |
| 177 | matches, err := tool.MatchFileNames(ctx, paths, query, sensitive, 101, func(filePath string) bool { return code.allowed[side][filePath] }) |
| 178 | if err != nil { |
| 179 | return observation, err |
| 180 | } |
| 181 | if len(matches) > 100 { |
| 182 | observation.Truncated = true |
| 183 | observation.Continuation = "Narrow query_name to see more authorized files." |
| 184 | matches = matches[:100] |
| 185 | } |
| 186 | observation.ReturnedContent = strings.Join(matches, "\n") |
| 187 | if len(matches) == 0 { |
| 188 | observation.ReturnedContent = "No authorized files returned." |
| 189 | } |
| 190 | return observation, nil |
| 191 | } |
| 192 | provider, ok := code.registry.Get(name) |
| 193 | if !ok { |
| 194 | return observation, fmt.Errorf("tool is unavailable") |
| 195 | } |
| 196 | var content string |
| 197 | var err error |
| 198 | if name == "code_search" { |
| 199 | content, err = code.search(ctx, provider, args, side) |
| 200 | } else { |
| 201 | content, err = provider.Execute(ctx, args) |
| 202 | } |
| 203 | observation.ReturnedContent = content |
| 204 | if err != nil { |
| 205 | return observation, err |
| 206 | } |
| 207 | if !utf8.ValidString(content) { |