blob: e9e01b6d2be5499b8c2a1d2e551dbad9d839626e [file] [log] [blame]
Earl Lee2e463fb2025-04-17 11:22:22 -07001package dockerimg
2
3import (
4 "bytes"
5 "context"
6 "crypto/sha256"
7 "encoding/hex"
8 "encoding/json"
9 "fmt"
10 "io/fs"
11 "maps"
12 "net/http"
13 "slices"
14 "strings"
15 "text/template"
16
17 "sketch.dev/ant"
18)
19
20func hashInitFiles(initFiles map[string]string) string {
21 h := sha256.New()
22 for _, path := range slices.Sorted(maps.Keys(initFiles)) {
23 fmt.Fprintf(h, "%s\n%s\n\n", path, initFiles[path])
24 }
25 return hex.EncodeToString(h.Sum(nil))
26}
27
28// createDockerfile creates a Dockerfile for a git repo.
29// It expects the relevant initFiles to have been provided.
30// If the sketch binary is being executed in a sub-directory of the repository,
31// the relative path is provided on subPathWorkingDir.
32func createDockerfile(ctx context.Context, httpc *http.Client, antURL, antAPIKey string, initFiles map[string]string, subPathWorkingDir string) (string, error) {
33 if subPathWorkingDir == "." {
34 subPathWorkingDir = ""
35 } else if subPathWorkingDir != "" && subPathWorkingDir[0] != '/' {
36 subPathWorkingDir = "/" + subPathWorkingDir
37 }
38 toolCalled := false
39 var dockerfileFROM, dockerfileExtraCmds string
40 runDockerfile := func(ctx context.Context, input json.RawMessage) (string, error) {
41 // TODO: unmarshal straight into a struct
42 var m map[string]any
43 if err := json.Unmarshal(input, &m); err != nil {
44 return "", fmt.Errorf(`input=%[1]v (%[1]T), wanted a map[string]any, got: %w`, input, err)
45 }
46 var ok bool
47 dockerfileFROM, ok = m["from"].(string)
48 if !ok {
49 return "", fmt.Errorf(`input["from"]=%[1]v (%[1]T), wanted a string`, m["path"])
50 }
51 dockerfileExtraCmds, ok = m["extra_cmds"].(string)
52 if !ok {
53 return "", fmt.Errorf(`input["extra_cmds"]=%[1]v (%[1]T), wanted a string`, m["path"])
54 }
55 toolCalled = true
56 return "OK", nil
57 }
58 convo := ant.NewConvo(ctx, antAPIKey)
59 if httpc != nil {
60 convo.HTTPC = httpc
61 }
62 if antURL != "" {
63 convo.URL = antURL
64 }
65 convo.Tools = []*ant.Tool{{
66 Name: "dockerfile",
67 Description: "Helps define a Dockerfile that sets up a dev environment for this project.",
68 Run: runDockerfile,
69 InputSchema: ant.MustSchema(`{
70 "type": "object",
71 "required": ["from", "extra_cmds"],
72 "properties": {
73 "from": {
74 "type": "string",
75 "description": "The alpine base image provided to the dockerfile FROM command"
76 },
77 "extra_cmds": {
78 "type": "string",
79 "description": "Extra commands to add to the dockerfile."
80 }
81 }
82}`),
83 }}
84
85 // TODO: add semgrep, prettier -- they require node/npm/etc which is more complicated than apk
86 // If/when we do this, add them into the list of available tools in bash.go.
87 const dockerfileBase = `FROM {{.From}}
88
89RUN apk add bash git make jq sqlite gcc musl-dev linux-headers npm nodejs go github-cli ripgrep fzf
90
91ENV GOTOOLCHAIN=auto
92ENV GOPATH=/go
93ENV PATH="$GOPATH/bin:$PATH"
94
95RUN go install golang.org/x/tools/cmd/goimports@latest
96RUN go install golang.org/x/tools/gopls@latest
97RUN go install mvdan.cc/gofumpt@latest
98
99{{.ExtraCmds}}
100
101ARG GIT_USER_EMAIL
102ARG GIT_USER_NAME
103
104RUN git config --global user.email "$GIT_USER_EMAIL" && \
105 git config --global user.name "$GIT_USER_NAME"
106
107LABEL sketch_context="{{.InitFilesHash}}"
108COPY . /app
109
110WORKDIR /app{{.SubDir}}
111RUN if [ -f go.mod ]; then go mod download; fi
112
113CMD ["/bin/sketch"]`
114
115 // TODO: it's basically impossible to one-shot a python env. We need an agent loop for that.
116 // Right now the prompt contains a set of half-baked workarounds.
117
118 // If you want to edit the model prompt, run:
119 //
120 // go test ./sketch/dockerimg -httprecord ".*" -rewritewant
121 //
122 // Then look at the changes with:
123 //
124 // git diff sketch/dockerimg/testdata/*.dockerfile
125 //
126 // If the dockerfile changes are a strict improvement, commit all the changes.
127 msg := ant.Message{
128 Role: ant.MessageRoleUser,
129 Content: []ant.Content{{
130 Type: ant.ContentTypeText,
131 Text: `
132Call the dockerfile tool to create a Dockerfile.
133The parameters to dockerfile fill out the From and ExtraCmds
134template variables in the following Go template:
135
136` + "```\n" + dockerfileBase + "\n```" + `
137
138In particular:
139- Assume it is primarily a Go project. For a minimal env, prefer 1.24.2-alpine3.21 as a base image.
140- If any python is needed at all, switch to using a python alpine image as a the base and apk add go.
141 Favor using uv, and use one of these base images, depending on the preferred python version:
142 ghcr.io/astral-sh/uv:python3.13-alpine
143 ghcr.io/astral-sh/uv:python3.12-alpine
144 ghcr.io/astral-sh/uv:python3.11-alpine
145- When using pip to install packages, use: uv pip install --system.
146- Python env setup is challenging and often no required, so any RUN commands involving python tooling should be written to let docker build continue if there is a failure.
147- Include any tools particular to this repository that can be inferred from the given context.
148- Append || true to any apk add commands in case the package does not exist.
149- Do not expose any ports.
150`,
151 }},
152 }
153 if len(initFiles) > 0 {
154 msg.Content[0].Text += "Here is the content of several files from the repository that may be relevant:\n\n"
155 }
156
157 for _, name := range slices.Sorted(maps.Keys(initFiles)) {
158 msg.Content = append(msg.Content, ant.Content{
159 Type: ant.ContentTypeText,
160 Text: fmt.Sprintf("Here is the contents %s:\n<file>\n%s\n</file>\n\n", name, initFiles[name]),
161 })
162 }
163 msg.Content = append(msg.Content, ant.Content{
164 Type: ant.ContentTypeText,
165 Text: "Now call the dockerfile tool.",
166 })
167 res, err := convo.SendMessage(msg)
168 if err != nil {
169 return "", err
170 }
171 if res.StopReason != ant.StopReasonToolUse {
172 return "", fmt.Errorf("expected stop reason %q, got %q", ant.StopReasonToolUse, res.StopReason)
173 }
174 if _, err := convo.ToolResultContents(context.TODO(), res); err != nil {
175 return "", err
176 }
177 if !toolCalled {
178 return "", fmt.Errorf("no dockerfile returned")
179 }
180
181 buf := new(bytes.Buffer)
182 err = template.Must(template.New("dockerfile").Parse(dockerfileBase)).Execute(buf, map[string]string{
183 "From": dockerfileFROM,
184 "ExtraCmds": dockerfileExtraCmds,
185 "InitFilesHash": hashInitFiles(initFiles),
186 "SubDir": subPathWorkingDir,
187 })
188 if err != nil {
189 return "", fmt.Errorf("dockerfile template failed: %w", err)
190 }
191
192 return buf.String(), nil
193}
194
195// For future reference: we can find the current git branch/checkout with: git symbolic-ref -q --short HEAD || git describe --tags --exact-match 2>/dev/null || git rev-parse HEAD
196
197func readInitFiles(fsys fs.FS) (map[string]string, error) {
198 result := make(map[string]string)
199
200 err := fs.WalkDir(fsys, ".", func(path string, d fs.DirEntry, err error) error {
201 if err != nil {
202 return err
203 }
204 if d.IsDir() && (d.Name() == ".git" || d.Name() == "node_modules") {
205 return fs.SkipDir
206 }
207 if !d.Type().IsRegular() {
208 return nil
209 }
210
211 // Case 1: Check for README files
212 // TODO: find README files between the .git root (where we start)
213 // and the dir that sketch was initialized. This needs more info
214 // plumbed to this function.
215 if strings.HasPrefix(strings.ToLower(path), "readme") {
216 content, err := fs.ReadFile(fsys, path)
217 if err != nil {
218 return err
219 }
220 result[path] = string(content)
221 return nil
222 }
223
224 // Case 2: Check for GitHub workflow files
225 if strings.HasPrefix(path, ".github/workflows/") {
226 content, err := fs.ReadFile(fsys, path)
227 if err != nil {
228 return err
229 }
230 result[path] = string(content)
231 return nil
232 }
233
234 return nil
235 })
236 if err != nil {
237 return nil, err
238 }
239 return result, nil
240}