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)
| 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. |
| 14 | func 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 |
no outgoing calls