| 🛟 Updated. 28d5985 k33g 12h ago | 1 | // The file half of the client: what the agent may ask the editor to read and |
| 2 | // write for it, and how a mentioned file's text is fetched when a prompt is |
| 3 | // sent. The editor's own buffers are consulted first, so the agent sees what |
| 4 | // you can see rather than what was last saved. |
| 5 | |
| 6 | package acp |
| 7 | |
| 8 | import ( |
| 9 | "encoding/json" |
| 10 | "errors" |
| 11 | "os" |
| 12 | |
| 📦 Turbo Core f3ade8d k33g 4h ago | 13 | "rickub.com/turbo-editors/turbo-core/jsonrpc" |
| 🛟 Updated. 28d5985 k33g 12h ago | 14 | ) |
| 15 | |
| 16 | // readFile returns a file's text as the editor sees it — the same view the |
| 17 | // agent gets from fs/read_text_file — falling back to the disk when the editor |
| 18 | // gave no way to ask it. |
| 19 | func (s *Session) readFile(path string) (string, error) { |
| 20 | if s.options.ReadTextFile != nil { |
| 21 | return s.options.ReadTextFile(path) |
| 22 | } |
| 23 | data, err := os.ReadFile(path) |
| 24 | if err != nil { |
| 25 | return "", err |
| 26 | } |
| 27 | return string(data), nil |
| 28 | } |
| 29 | |
| 30 | // readTextFile answers with the text the editor sees, which is the buffer's |
| 31 | // when the file is open and modified. |
| 32 | func (s *Session) readTextFile(raw json.RawMessage) (any, error) { |
| 33 | var params ReadTextFileParams |
| 34 | if err := json.Unmarshal(raw, ¶ms); err != nil { |
| 35 | return nil, &jsonrpc.ResponseError{Code: jsonrpc.CodeInvalidParams, Message: err.Error()} |
| 36 | } |
| 37 | if s.options.ReadTextFile == nil { |
| 38 | return nil, errors.New("this editor cannot read files for an agent") |
| 39 | } |
| 40 | |
| 41 | content, err := s.options.ReadTextFile(params.Path) |
| 42 | if err != nil { |
| 43 | return nil, err |
| 44 | } |
| 45 | return ReadTextFileResult{Content: slice(content, params.Line, params.Limit)}, nil |
| 46 | } |
| 47 | |
| 48 | // slice returns the lines the agent asked for, counting from one as the |
| 49 | // protocol does. Asking for neither gives the whole file. |
| 50 | func slice(content string, line, limit *int) string { |
| 51 | if line == nil && limit == nil { |
| 52 | return content |
| 53 | } |
| 54 | |
| 55 | lines := splitLines(content) |
| 56 | from := 0 |
| 57 | if line != nil && *line > 0 { |
| 58 | from = min(*line-1, len(lines)) |
| 59 | } |
| 60 | to := len(lines) |
| 61 | if limit != nil && *limit >= 0 { |
| 62 | to = min(from+*limit, len(lines)) |
| 63 | } |
| 64 | return joinLines(lines[from:to]) |
| 65 | } |
| 66 | |
| 67 | // writeTextFile puts the agent's text where the editor can show it. |
| 68 | func (s *Session) writeTextFile(raw json.RawMessage) (any, error) { |
| 69 | var params WriteTextFileParams |
| 70 | if err := json.Unmarshal(raw, ¶ms); err != nil { |
| 71 | return nil, &jsonrpc.ResponseError{Code: jsonrpc.CodeInvalidParams, Message: err.Error()} |
| 72 | } |
| 73 | if s.options.WriteTextFile == nil { |
| 74 | return nil, errors.New("this editor cannot write files for an agent") |
| 75 | } |
| 76 | if err := s.options.WriteTextFile(params.Path, params.Content); err != nil { |
| 77 | return nil, err |
| 78 | } |
| 79 | return map[string]any{}, nil |
| 80 | } |