readInstructionFileFromDir scans a directory for a file matching fileName (case-insensitive) and reads its contents.
(dir, fileName string)
| 239 | // readInstructionFileFromDir scans a directory for a file matching |
| 240 | // fileName (case-insensitive) and reads its contents. |
| 241 | func readInstructionFileFromDir(dir, fileName string) (codersdk.ChatMessagePart, bool) { |
| 242 | dirEntries, err := os.ReadDir(dir) |
| 243 | if err != nil { |
| 244 | return codersdk.ChatMessagePart{}, false |
| 245 | } |
| 246 | |
| 247 | for _, e := range dirEntries { |
| 248 | if e.IsDir() { |
| 249 | continue |
| 250 | } |
| 251 | if strings.EqualFold(strings.TrimSpace(e.Name()), fileName) { |
| 252 | filePath := filepath.Join(dir, e.Name()) |
| 253 | content, truncated, ok := readAndSanitizeFile(filePath, maxInstructionFileBytes) |
| 254 | if !ok { |
| 255 | return codersdk.ChatMessagePart{}, false |
| 256 | } |
| 257 | if content == "" { |
| 258 | return codersdk.ChatMessagePart{}, false |
| 259 | } |
| 260 | return codersdk.ChatMessagePart{ |
| 261 | Type: codersdk.ChatMessagePartTypeContextFile, |
| 262 | ContextFilePath: filePath, |
| 263 | ContextFileContent: content, |
| 264 | ContextFileTruncated: truncated, |
| 265 | }, true |
| 266 | } |
| 267 | } |
| 268 | return codersdk.ChatMessagePart{}, false |
| 269 | } |
| 270 | |
| 271 | // readAndSanitizeFile reads the file at path, capping the read |
| 272 | // at maxBytes to avoid unbounded memory allocation. It sanitizes |
no test coverage detected