turbo-editors/turbo-corepublic Fork 0
v1.0.2
Commits
Clone
git clone https://git.rickub.com/turbo-editors/turbo-core.git
git clone ssh://git@rickub.com/turbo-editors/turbo-core.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

session_files.go · 80 lines · 2.4 KBGo Blame HistoryRaw
🛟 Updated. 28d5985 k33g 18h ago1// 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
6package acp
7
8import (
9 "encoding/json"
10 "errors"
11 "os"
12
📦 Turbo Core f3ade8d k33g 11h ago13 "rickub.com/turbo-editors/turbo-core/jsonrpc"
🛟 Updated. 28d5985 k33g 18h ago14)
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.
19func (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.
32func (s *Session) readTextFile(raw json.RawMessage) (any, error) {
33 var params ReadTextFileParams
34 if err := json.Unmarshal(raw, &params); 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.
50func 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.
68func (s *Session) writeTextFile(raw json.RawMessage) (any, error) {
69 var params WriteTextFileParams
70 if err := json.Unmarshal(raw, &params); 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}