MCPcopy Index your code
hub / github.com/coder/coder / readInstructionFileFromDir

Function readInstructionFileFromDir

agent/agentcontextconfig/api.go:241–269  ·  view source on GitHub ↗

readInstructionFileFromDir scans a directory for a file matching fileName (case-insensitive) and reads its contents.

(dir, fileName string)

Source from the content-addressed store, hash-verified

239// readInstructionFileFromDir scans a directory for a file matching
240// fileName (case-insensitive) and reads its contents.
241func 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

Callers 3

ResolveFunction · 0.85
ContextPartsFromDirFunction · 0.85
readInstructionFilesFunction · 0.85

Calls 3

readAndSanitizeFileFunction · 0.85
ReadDirMethod · 0.80
NameMethod · 0.65

Tested by

no test coverage detected