MCPcopy Index your code
hub / github.com/codehamr/codehamr / ReadFile

Function ReadFile

internal/tools/read.go:14–30  ·  view source on GitHub ↗

ReadFile returns path's contents, truncated to the shared tool-output budget (Truncate). The model gets exact bytes, not a shell-mangled approximation. Per the bash/write/edit convention, filesystem errors come back in the output string, never as a Go error: the model reacts to them like a non-zero

(path string)

Source from the content-addressed store, hash-verified

12// Per the bash/write/edit convention, filesystem errors come back in the output
13// string, never as a Go error: the model reacts to them like a non-zero exit.
14func ReadFile(path string) string {
15 if path == "" {
16 return "(empty path)"
17 }
18 // Refuse non-regular files up front: open(2) on a FIFO blocks forever
19 // waiting for a writer (leaking the tool goroutine past Ctrl+C, which
20 // cancels the turn but can't unblock the read), and an endless device file
21 // (/dev/zero) grows ReadFile's buffer without bound. Stat never blocks.
22 if info, err := os.Stat(path); err == nil && !info.Mode().IsRegular() && !info.IsDir() {
23 return fmt.Sprintf("(read error: %s is not a regular file)", path)
24 }
25 raw, err := os.ReadFile(path)
26 if err != nil {
27 return fmt.Sprintf("(read error: %v)", err)
28 }
29 return chmctx.Truncate(string(raw))
30}
31
32// ReadFileSchema is the OpenAI tool definition for read_file. The description
33// nudges the model toward read_file over `cat` so it stops piping source

Callers 6

TestReadFileHappyFunction · 0.85
TestReadFileEmptyPathFunction · 0.85
TestReadFileMissingFileFunction · 0.85
runRawFunction · 0.85

Calls

no outgoing calls

Tested by 5

TestReadFileHappyFunction · 0.68
TestReadFileEmptyPathFunction · 0.68
TestReadFileMissingFileFunction · 0.68