-
Notifications
You must be signed in to change notification settings - Fork 4
/
run.go
487 lines (425 loc) · 11.3 KB
/
run.go
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
package tui
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"os/signal"
"path/filepath"
"slices"
"sort"
"strings"
"time"
"atomicgo.dev/cursor"
"github.com/fatih/color"
"github.com/gptscript-ai/go-gptscript"
"github.com/pterm/pterm"
"golang.org/x/exp/maps"
)
const ToolCallHeader = "<tool call>"
type RunOptions struct {
AppName string
Eval []gptscript.ToolDef
TrustedRepoPrefixes []string
DisableCache bool
CredentialOverrides []string
Input string
CacheDir string
SubTool string
ChatState string
SaveChatStateFile string
Workspace string
UserStartConversation *bool
Location string
EventLog string
LoadMessage string
ForceSequential bool
Client *gptscript.GPTScript
ClientOpts *gptscript.GlobalOptions
deleteWorkspaceOn bool
}
func first[T comparable](in ...T) (result T) {
for _, i := range in {
if i != result {
return i
}
}
return
}
func complete(opts ...RunOptions) (result RunOptions, closeClient func(), _ error) {
closeClient = func() {}
for _, opt := range opts {
result.TrustedRepoPrefixes = append(result.TrustedRepoPrefixes, opt.TrustedRepoPrefixes...)
result.DisableCache = first(opt.DisableCache, result.DisableCache)
result.CredentialOverrides = append(result.CredentialOverrides, opt.CredentialOverrides...)
result.CacheDir = first(opt.CacheDir, result.CacheDir)
result.SubTool = first(opt.SubTool, result.SubTool)
result.Workspace = first(opt.Workspace, result.Workspace)
result.SaveChatStateFile = first(opt.SaveChatStateFile, result.SaveChatStateFile)
result.ChatState = first(opt.ChatState, result.ChatState)
result.Eval = append(result.Eval, opt.Eval...)
result.AppName = first(opt.AppName, result.AppName)
result.UserStartConversation = first(opt.UserStartConversation, result.UserStartConversation)
result.Location = first(opt.Location, result.Location)
result.EventLog = first(opt.EventLog, result.EventLog)
result.LoadMessage = first(opt.LoadMessage, result.LoadMessage)
result.ForceSequential = first(opt.ForceSequential, result.ForceSequential)
result.Client = first(opt.Client, result.Client)
result.ClientOpts = first(opt.ClientOpts, result.ClientOpts)
}
if result.AppName == "" {
result.AppName = "gptscript-tui"
}
if result.Workspace == "" {
var err error
result.Workspace, err = os.MkdirTemp("", fmt.Sprintf("%s-workspace-*", result.AppName))
if err != nil {
return result, closeClient, err
}
result.deleteWorkspaceOn = true
} else if !filepath.IsAbs(result.Workspace) {
var err error
result.Workspace, err = filepath.Abs(result.Workspace)
if err != nil {
return result, closeClient, err
}
}
if err := os.MkdirAll(result.Workspace, 0700); err != nil {
return result, closeClient, err
}
if result.Client != nil && result.ClientOpts != nil {
return result, closeClient, fmt.Errorf("only one of Client or ClientOpts may be set, not both")
}
if result.Client == nil {
var (
opts gptscript.GlobalOptions
err error
)
if result.ClientOpts != nil {
opts = *result.ClientOpts
}
result.Client, err = gptscript.NewGPTScript(opts)
if err != nil {
return result, closeClient, err
}
closeClient = result.Client.Close
}
return
}
func Run(ctx context.Context, tool string, opts ...RunOptions) error {
var (
opt, closeClient, err = complete(opts...)
input = opt.Input
localCtx, cancel = signal.NotifyContext(ctx, os.Interrupt)
eventOut io.Writer
)
defer cancel()
defer cursor.Show()
if err != nil {
return err
}
defer closeClient()
if opt.deleteWorkspaceOn {
defer os.RemoveAll(opt.Workspace)
}
startCtx, started := context.WithCancel(ctx)
defer started()
go func() {
select {
case <-startCtx.Done():
return
case <-time.After(time.Second):
}
if opt.LoadMessage != "" {
fmt.Printf(opt.LoadMessage)
}
}()
client := opt.Client
confirm, err := NewConfirm(opt.AppName, client, opt.TrustedRepoPrefixes...)
if err != nil {
return err
}
ui, err := newDisplay(tool)
if err != nil {
return err
}
defer ui.Close()
if opt.UserStartConversation == nil {
tools := opt.Eval
if len(tools) == 0 {
nodes, err := client.Parse(ctx, tool)
if err != nil {
return err
}
for _, node := range nodes {
if node.ToolNode != nil {
tools = append(tools, node.ToolNode.Tool.ToolDef)
}
}
}
if len(tools) > 0 {
if tools[0].Chat && tools[0].Instructions == "" {
opt.UserStartConversation = &[]bool{true}[0]
}
}
}
firstInput := opt.Input
if firstInput == "" && opt.UserStartConversation != nil && *opt.UserStartConversation {
var ok bool
firstInput, ok = ui.Prompt("")
if !ok {
return nil
}
}
if firstInput == "" && opt.ChatState != "" {
var ok bool
firstInput, ok = ui.Prompt("Resuming conversation")
if !ok {
return nil
}
}
var run *gptscript.Run
runOpt := gptscript.Options{
GlobalOptions: gptscript.GlobalOptions{
CacheDir: opt.CacheDir,
},
Confirm: true,
Prompt: true,
IncludeEvents: true,
DisableCache: opt.DisableCache,
CredentialOverrides: opt.CredentialOverrides,
Input: firstInput,
SubTool: opt.SubTool,
Workspace: opt.Workspace,
ChatState: opt.ChatState,
Location: opt.Location,
ForceSequential: opt.ForceSequential,
}
if len(opt.Eval) == 0 {
run, err = client.Run(localCtx, tool, runOpt)
if err != nil {
return err
}
defer run.Close()
} else {
run, err = client.Evaluate(localCtx, runOpt, opt.Eval...)
if err != nil {
return err
}
defer run.Close()
}
if opt.EventLog != "" {
f, err := os.OpenFile(opt.EventLog, os.O_APPEND|os.O_CREATE|os.O_RDWR, 0644)
if err != nil {
return err
}
defer f.Close()
eventOut = f
}
for {
text := func() string { return "" }
for event := range run.Events() {
started()
if eventOut != nil && (event.Call == nil || event.Call.Type != gptscript.EventTypeCallProgress) {
if err := json.NewEncoder(eventOut).Encode(map[string]any{
"time": time.Now(),
"event": event,
}); err != nil {
return err
}
}
if event.Call != nil {
text = render(input, run)
ui.Progress(text)
}
if ok, err := confirm.HandlePrompt(localCtx, event, ui.Ask); !ok {
return nil
} else if err != nil && localCtx.Err() == nil {
return err
}
if ok, err := confirm.HandleConfirm(localCtx, event, ui.AskYesNo); !ok {
return nil
} else if err != nil && localCtx.Err() == nil {
return err
}
}
if errors.Is(localCtx.Err(), context.Canceled) {
text = interrupted
}
ui.Finished(text())
if opt.SaveChatStateFile != "" {
if run.State() == gptscript.Finished {
_ = os.Remove(opt.SaveChatStateFile)
} else {
_ = os.WriteFile(opt.SaveChatStateFile, []byte(run.ChatState()), 0600)
}
}
for {
if run != nil && run.State().IsTerminal() {
if errors.Is(localCtx.Err(), context.Canceled) {
} else if run.Err() != nil {
fmt.Println(color.RedString("%v", run.Err()))
} else {
return nil
}
}
// reset interrupt
cancel()
localCtx, cancel = signal.NotifyContext(ctx, os.Interrupt)
line, ok := ui.Prompt(getCurrentToolName(run))
if !ok {
return nil
}
input = line
ui.Progress(render(input, nil))
run, err = run.NextChat(localCtx, input)
if err != nil {
fmt.Println(color.RedString("%v", run.Err()))
run = nil
continue
}
break
}
}
}
func splitAtTerm(line string, width int) string {
var (
buf = &strings.Builder{}
offset int
)
for _, field := range strings.Fields(line) {
if offset == 0 {
buf.WriteString(field)
offset = len(field)
} else if (len(field) + 1 + offset) > width {
buf.WriteString("\n")
buf.WriteString(field)
offset = len(field)
} else {
buf.WriteString(" ")
buf.WriteString(field)
offset += len(field) + 1
}
}
return buf.String()
}
func renderDeferred(input string, parent *gptscript.CallFrame, calls map[string]gptscript.CallFrame) string {
buf := &strings.Builder{}
if input != "" {
buf.WriteString(color.GreenString(splitAtTerm("> "+input+"\n", pterm.GetTerminalWidth())))
buf.WriteString("\n")
}
if parent != nil {
printCall(buf, calls, *parent, nil)
}
return buf.String()
}
func render(input string, run *gptscript.Run) func() string {
var (
content string
parent *gptscript.CallFrame
calls map[string]gptscript.CallFrame
)
if run != nil {
call, ok := run.ParentCallFrame()
if ok {
parent = &call
calls = run.Calls()
}
}
return func() string {
if content != "" {
return content
}
content = renderDeferred(input, parent, calls)
return content
}
}
func printToolCall(out *strings.Builder, toolCall string) {
// The intention here is to only print the string while it's still being generated, if it's complete
// then there's no reason to because we are waiting on something else at that point and it's status should
// be displayed
lines := strings.Split(toolCall, "\n")
buf := &strings.Builder{}
for _, line := range lines {
name, args, ok := strings.Cut(strings.TrimPrefix(line, ToolCallHeader), " -> ")
if !ok {
continue
}
width := pterm.GetTerminalWidth() - 33
if len(args) > width {
args = fmt.Sprintf("%s %s...(%d)", name, args[:width], len(args[width:]))
} else {
args = fmt.Sprintf("%s %s", name, args)
}
if buf.Len() > 0 {
buf.WriteString("\n")
}
buf.WriteString(strings.TrimSpace(args))
}
if buf.Len() > 0 {
out.WriteString("\n")
out.WriteString(BoxStyle.Render("Call Arguments:\n\n" + buf.String()))
}
}
func printCall(buf *strings.Builder, calls map[string]gptscript.CallFrame, call gptscript.CallFrame, stack []string) {
if slices.Contains(stack, call.ID) {
return
}
if call.DisplayText != "" {
s, err := MarkdownRender.Render(call.DisplayText)
if err == nil {
buf.WriteString(s)
}
}
// Here we try to print the status of credential/context tools that are taking a while to do things.
if len(call.Output) == 0 {
for _, child := range calls {
if child.ID == call.ID {
continue
}
if child.ParentID == call.ID {
if len(child.Output) > 0 && child.End.IsZero() && time.Since(child.Start) > 1000*time.Millisecond {
printCall(buf, calls, child, append(stack, call.ID))
}
}
}
}
for _, output := range call.Output {
content, toolCall, _ := strings.Cut(output.Content, ToolCallHeader)
if content != "" {
if strings.HasPrefix(call.Tool.Instructions, "#!") {
buf.WriteString(BoxStyle.Render(strings.TrimSpace(content)))
} else {
s, err := MarkdownRender.Render(content)
if err == nil {
buf.WriteString(s)
} else {
buf.WriteString(content)
}
}
}
if toolCall != "" {
printToolCall(buf, ToolCallHeader+toolCall)
}
keys := maps.Keys(output.SubCalls)
sort.Slice(keys, func(i, j int) bool {
return calls[keys[i]].Start.Before(calls[keys[j]].Start)
})
for _, key := range keys {
printCall(buf, calls, calls[key], append(stack, call.ID))
}
}
}
func getCurrentToolName(run *gptscript.Run) string {
toolName := run.RespondingTool().Name
if toolName == "" {
return ""
}
return "@" + toolName
}
func interrupted() string {
return "Interrupted\n\n"
}