(_ context.Context, execCtx ExecutionContext, input any)
| 68 | } |
| 69 | |
| 70 | func (t *ReadFileTool) Execute(_ context.Context, execCtx ExecutionContext, input any) (string, error) { |
| 71 | in := deref[ReadFileInput](input) |
| 72 | path := resolveToolPath(execCtx, in.FilePath) |
| 73 | data, err := os.ReadFile(path) |
| 74 | if err != nil { |
| 75 | return fmt.Sprintf("Error reading file: %s", err), nil |
| 76 | } |
| 77 | content := decodeToolBytes(data) |
| 78 | lineEndings := detectLineEndings(content) |
| 79 | if lineEndings == "\r\n" { |
| 80 | content = strings.ReplaceAll(content, "\r\n", "\n") |
| 81 | } |
| 82 | updateReadState(execCtx, path, content, lineEndings, in.Offset > 0 || in.Limit != nil) |
| 83 | lines := strings.Split(content, "\n") |
| 84 | start := in.Offset |
| 85 | if start < 0 { |
| 86 | start = 0 |
| 87 | } |
| 88 | end := len(lines) |
| 89 | if in.Limit != nil && *in.Limit != 0 { |
| 90 | end = start + *in.Limit |
| 91 | if end > len(lines) { |
| 92 | end = len(lines) |
| 93 | } |
| 94 | } |
| 95 | if start > len(lines) || end <= start { |
| 96 | return "", nil |
| 97 | } |
| 98 | var out strings.Builder |
| 99 | for i := start; i < end; i++ { |
| 100 | if i > start { |
| 101 | out.WriteString("\n") |
| 102 | } |
| 103 | out.WriteString(fmt.Sprintf("%d\t%s", i+1, lines[i])) |
| 104 | } |
| 105 | return out.String(), nil |
| 106 | } |
| 107 | |
| 108 | type WriteFileInput struct { |
| 109 | FilePath string `json:"file_path" jsonschema_description:"The absolute path to the file to write"` |
nothing calls this directly
no test coverage detected