๐Ÿ“ฆ rogpeppe / AI

๐Ÿ“„ main.go ยท 301 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301package main

import (
	"bytes"
	"context"
	"crypto/rand"
	_ "embed"
	"encoding/base64"
	"flag"
	"fmt"
	"iter"
	"os"
	"slices"
	"strings"
	"unicode/utf8"

	"9fans.net/go/acme"
	"github.com/openai/openai-go"
	"github.com/openai/openai-go/responses"
	"github.com/openai/openai-go/shared"
)

//go:generate cue exp gengotypes

//go:embed schema.cue
var schemaCUE string

func main() {
	if err := main1(); err != nil {
		fmt.Fprintf(os.Stderr, "AI: %v\n", err)
		os.Exit(1)
	}
}

type params struct {
	Filename     string
	Instructions string
}

var systemPrompt = `
I'm currently editing a file.

Your task is to produce the new contents of this file following any intent indicated
in the user instructions. Please produce me a JSON result with CUE schema
(indicated by #Reply); see the "schema" part for info on JSON schemas.

I've included other parts indicating aspects of the current edit session.
Each part is in JSON format described by the CUE #Part schema.
`

var (
	flagBig     = flag.Bool("big", false, "allow large files")
	flagModel   = flag.String("m", string(openai.ChatModelGPT4o), "OpenAI model to use")
	flagVerbose = flag.Bool("v", false, "enable verbose output")
)

func main1() error {
	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, `
usage: AI [<prompt> [file...]]

This executes OpenAI with the given instructions on the selection
in the current file.
Any files provided will be attached as context.
`)
		os.Exit(2)
	}
	flag.Parse()

	win, err := acmeCurrentWin()
	if err != nil {
		return err
	}
	defer win.CloseFiles()

	key := os.Getenv("OPENAI_API_KEY")
	if key == "" {
		return fmt.Errorf("no value set for $OPENAI_API_KEY")
	}

	var parts []Part
	parts = append(parts, Part{
		Instructions: "This holds the CUE schema for the JSON reply for you to send me",
		Content:      schemaCUE,
	})

	part, body, err := currentFilePart(win)
	if err != nil {
		return err
	}
	parts = append(parts, part)

	args := flag.Args()
	if len(args) > 0 {
		parts = append(parts, Part{
			Instructions: "This part holds the user instructions.",
			Content:      args[0],
		})
		args = args[1:]
	}
	for _, filename := range args {
		data, err := os.ReadFile(filename)
		if err != nil {
			return err
		}
		if len(data) > 100*1024 && !*flagBig {
			return fmt.Errorf("refusing to send large file (%d bytes); use -big to override", len(data))
		}
		b64 := false
		if !utf8.Valid(data) {
			b64 = true
			data = base64.StdEncoding.AppendEncode(nil, data)
		}
		parts = append(parts, Part{
			Instructions: "this is a file attached by the user",
			Filename:     filename,
			Base64:       b64,
			Content:      string(data),
		})
	}

	// Build the messages
	systemMsg := responses.EasyInputMessageParam{
		Role: responses.EasyInputMessageRoleSystem,
		Content: responses.EasyInputMessageContentUnionParam{
			OfString: openai.Opt(systemPrompt),
		},
	}

	var userContent bytes.Buffer
	for _, p := range parts {
		aiPart, err := p.AsOpenAI()
		if err != nil {
			return fmt.Errorf("cannot marshal part: %v", err)
		}
		userContent.WriteString(aiPart.Text)
		userContent.WriteString("\n")
	}

	userMsg := responses.EasyInputMessageParam{
		Role: responses.EasyInputMessageRoleUser,
		Content: responses.EasyInputMessageContentUnionParam{
			OfString: openai.Opt(userContent.String()),
		},
	}

	// Create the client, relying on OPENAI_API_KEY in env
	client := openai.NewClient()

	ctx := context.Background()
	respIter := streamIter(client.Responses.NewStreaming(ctx, responses.ResponseNewParams{
		Model: responses.ChatModel(*flagModel),
		Input: responses.ResponseNewParamsInputUnion{
			OfInputItemList: responses.ResponseInputParam{
				{OfMessage: &systemMsg},
				{OfMessage: &userMsg},
			},
		},
		Text: responses.ResponseTextConfigParam{
			Format: responses.ResponseFormatTextConfigUnionParam{
				OfJSONObject: &shared.ResponseFormatJSONObjectParam{},
			},
		},
	}))

	var buf bytes.Buffer
	for part, err := range partsIter(respIter, &buf) {
		if err != nil {
			fmt.Printf("bad response:\n%s\n", buf.Bytes())
			return fmt.Errorf("error receiving reply: %v", err)
		}
		var newBody []byte
		switch r := part.AsAny().(type) {
		case *FurtherInstructionNeeded:
			fmt.Printf("further instruction needed: %s\n", r.Message)
			continue
		case *FullContent:
			newBody = body.text
		case *SelectionAppend:
			body.selection = slices.Concat(body.selection, []byte(r.Text))
			newBody = slices.Concat(body.head, body.selection, []byte(r.Text), body.tail)
		case *SelectionInsert:
			body.selection = slices.Concat([]byte(r.Text), body.selection)
			newBody = slices.Concat(body.head, body.selection, body.tail)
		case *SelectionReplace:
			body.selection = []byte(r.Text)
			newBody = slices.Concat(body.head, body.selection, body.tail)
		case *Commentary:
			fmt.Println(r.Text)
			continue
		default:
			return fmt.Errorf("unhandled reply type %T", r)
		}

		body.text = ensureNewline(body.text)
		newBody = ensureNewline(newBody)
		if err := doApply(win, body.text, newBody); err != nil {
			// TODO: ouch: this is racy.
			fmt.Printf("response:\n%s\n", buf.Bytes())
			return fmt.Errorf("cannot apply results to acme window: %v", err)
		}
		body.text = newBody
	}

	return nil
}

func ensureNewline(data []byte) []byte {
	if !bytes.HasSuffix(data, []byte("\n")) {
		data = append(data, '\n')
	}
	return data
}

type bodyInfo struct {
	text      []byte
	head      []byte
	selection []byte
	tail      []byte
}

func currentFilePart(win *acme.Win) (part Part, info *bodyInfo, err error) {
	var buf bytes.Buffer
	if err := copyBody(&buf, win); err != nil {
		return Part{}, nil, fmt.Errorf("cannot copy window body: %v", err)
	}
	body := buf.Bytes()

	_, _, err = win.ReadAddr() // ensure address file is open
	if err != nil {
		return Part{}, nil, fmt.Errorf("cannot read address: %v", err)
	}
	if err := win.Ctl("addr=dot"); err != nil {
		return Part{}, nil, fmt.Errorf("cannot set address: %v", err)
	}
	a0, a1, err := win.ReadAddr()
	if err != nil {
		return Part{}, nil, fmt.Errorf("cannot get dot: %v", err)
	}
	a0b, a1b := runeOffset2ByteOffset(body, a0), runeOffset2ByteOffset(body, a1)

	head := body[:a0b]
	selection := body[a0b:a1b]
	tail := body[a1b:]

	delim := []byte(uniqID())
	hbody := slices.Concat(
		head,
		delim,
		selection,
		delim,
		tail,
	)

	tagBytes, err := win.ReadAll("tag")
	if err != nil {
		return Part{}, nil, fmt.Errorf("cannot read tag: %v", err)
	}
	filename, _, _ := strings.Cut(string(tagBytes), " ")

	part = Part{
		Instructions: fmt.Sprintf("Contents of the file currently being edited. The current selection is surrounded by the delimiter string %q", delim),
		Filename:     filename,
		Content:      string(hbody),
	}
	info = &bodyInfo{
		text:      body,
		head:      head,
		selection: selection,
		tail:      tail,
	}
	return part, info, nil
}

func uniqID() string {
	buf := make([]byte, 16)
	if _, err := rand.Read(buf); err != nil {
		panic(err)
	}
	return fmt.Sprintf("%x", buf)
}

type stream[T any] interface {
	Next() bool
	Current() T
	Err() error
}

func streamIter[T any](s stream[T]) iter.Seq2[T, error] {
	return func(yield func(T, error) bool) {
		for s.Next() {
			if !yield(s.Current(), s.Err()) {
				if s, _ := s.(interface{ Close() }); s != nil {
					s.Close()
				}
				return
			}
		}
	}
}