-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
499 lines (438 loc) · 14.6 KB
/
main.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
488
489
490
491
492
493
494
495
496
497
498
499
package main
import (
"fmt"
"log/slog"
"strconv"
"strings"
"github.com/GiGurra/boa/pkg/boa"
"github.com/gigurra/ai/common"
"github.com/gigurra/ai/config"
"github.com/gigurra/ai/domain"
"github.com/gigurra/ai/providers"
"github.com/gigurra/ai/session"
"github.com/gigurra/ai/util"
"github.com/google/uuid"
"github.com/samber/lo"
"github.com/spf13/cobra"
)
func main() {
cliParams := config.CliParams{}
boa.Wrap{
Use: "ai",
Short: "ai/llm conversation tool, every terminal is a conversation",
ParamEnrich: config.CliParamEnricher,
Params: &cliParams,
Args: cobra.MinimumNArgs(0),
SubCommands: []*cobra.Command{
sessionsCmd(),
sessionCmd(),
statusCmd(),
configCmd(),
historyCmd(),
newOrResetCmd("new"),
newOrResetCmd("reset"),
setSessionCmd(),
deleteSessionCmd(),
renameCmd(),
copyCmd(),
nameAll(),
prepCmd(),
},
Run: func(cmd *cobra.Command, args []string) {
question := strings.Join(args, " ")
if cliParams.Verbose.Value() {
slog.SetLogLoggerLevel(slog.LevelDebug)
}
cfgFilePath, storedCfg := config.LoadCfgFile()
cfg := config.ValidateCfg(cfgFilePath, storedCfg, cliParams)
provider := providers.CreateProvider(cfg)
stdInAttachment, err := util.ReadAllStdIn()
if err != nil {
common.FailAndExit(1, fmt.Sprintf("Failed to read attachment from stdin: %v", err))
}
if stdInAttachment != "" {
if question != "" {
footer := fmt.Sprintf("\n Attached additional info/data: \n %s", stdInAttachment)
question = fmt.Sprintf("%s\n%s", question, footer)
} else {
question = stdInAttachment
}
}
if question == "" {
common.FailAndExit(1, "No data provided")
}
state := session.LoadSession(session.GetSessionID(cliParams.Session.GetOrElse("")))
messageHistory := state.MessageHistory()
newMessage := domain.Message{
SourceType: domain.User,
Content: question,
}
stream := provider.BasicAskStream(domain.Question{
Messages: append(messageHistory, newMessage),
})
inputTokens := 0
outputTokens := 0
accum := strings.Builder{}
for {
res, hasMore := <-stream
if !hasMore {
break // stream done
}
if res.Err != nil {
common.FailAndExit(1, fmt.Sprintf("Failed to receive stream response: %v", res.Err))
}
inputTokens += res.Resp.GetUsage().PromptTokens
outputTokens += res.Resp.GetUsage().CompletionTokens
if len(res.Resp.GetChoices()) == 0 {
continue
}
accum.WriteString(res.Resp.GetChoices()[0].Message.Content)
fmt.Printf("%s", res.Resp.GetChoices()[0].Message.Content)
}
fmt.Printf("\n")
state.InputTokensAccum += inputTokens
state.InputTokens = inputTokens
state.OutputTokensAccum += outputTokens
state.OutputTokens = outputTokens
state.AddMessage(newMessage)
state.AddMessage(domain.Message{
SourceType: domain.Assistant,
Content: accum.String(),
})
session.StoreSession(state)
},
}.ToApp()
}
func sessionsCmd() *cobra.Command {
p := config.CliSubcParams{}
return boa.Wrap{
Use: "sessions",
Short: "List all stored sessions",
Params: &p,
ParamEnrich: config.CliParamEnricher,
Run: func(cmd *cobra.Command, args []string) {
sessions := session.ListSessions()
currentSession := session.GetSessionID(p.Session.GetOrElse(""))
if p.Verbose.Value() {
for _, s := range sessions {
currentSessionSuffix := ""
if s.SessionID == currentSession {
currentSessionSuffix = " [ *current* ]"
}
fmt.Printf("%s (i=%d/%d, o=%d/%d, created %v)%s\n", s.SessionID, s.InputTokens, s.InputTokensAccum, s.OutputTokens, s.OutputTokensAccum, s.CreatedAt.Format("2006-01-02 15:04:05"), currentSessionSuffix)
}
} else {
for _, s := range sessions {
currentSessionSuffix := ""
if s.SessionID == currentSession {
currentSessionSuffix = " [ *current* ]"
}
fmt.Printf("%s%s\n", s.SessionID, currentSessionSuffix)
}
}
},
}.ToCmd()
}
func sessionCmd() *cobra.Command {
p := config.CliSubcParams{}
return boa.Wrap{
Use: "session",
Short: "Print id of current session",
Params: &p,
Run: func(cmd *cobra.Command, args []string) {
sessionId := session.GetSessionID(p.Session.GetOrElse(""))
if p.Verbose.Value() {
s := session.LoadSession(sessionId)
fmt.Printf("%s (i=%d/%d, o=%d/%d, created %v)\n", s.SessionID, s.InputTokens, s.InputTokensAccum, s.OutputTokens, s.OutputTokensAccum, s.CreatedAt.Format("2006-01-02 15:04:05"))
} else {
fmt.Printf("%s\n", sessionId)
}
},
}.ToCmd()
}
func statusCmd() *cobra.Command {
p := config.CliSubcParams{}
return boa.Wrap{
Use: "status",
Short: "Prints info about current session",
Params: &p,
Run: func(cmd *cobra.Command, args []string) {
_, cfgInFile := config.LoadCfgFile()
s := session.LoadSession(session.GetSessionID(p.Session.GetOrElse("")))
provider := p.Provider.GetOrElse(cfgInFile.Provider)
fmt.Printf("current provider: %s\n", provider)
fmt.Printf("current model: %s\n", cfgInFile.Model(provider))
fmt.Printf("config file: %s\n", config.CfgFilePath())
fmt.Printf("storage dir: %s\n", session.Dir())
fmt.Printf("lookup dir: %s\n", session.LookupDir())
fmt.Printf("current session: %s (i=%d/%d, o=%d/%d, created %v)\n", s.SessionID, s.InputTokens, s.InputTokensAccum, s.OutputTokens, s.OutputTokensAccum, s.CreatedAt.Format("2006-01-02 15:04:05"))
fmt.Printf("current session file: %s\n", s.StateFile)
},
}.ToCmd()
}
func configCmd() *cobra.Command {
p := config.CliSubcParams{}
return boa.Wrap{
Use: "config",
Short: "Prints the current configuration",
Params: &p,
Run: func(cmd *cobra.Command, args []string) {
cfgFilePath, storedCfg := config.LoadCfgFile()
cfg := config.ValidateCfg(cfgFilePath, storedCfg, p.ToCliParams())
cfg = cfg.WithoutSecrets()
fmt.Printf("--- %s ---\n%s", cfgFilePath, cfg.ToYaml())
},
}.ToCmd()
}
func historyCmd() *cobra.Command {
type HistoryCmdParams struct {
config.CliSubcParams
Format boa.Required[string] `name:"format" descr:"Output format. Valid options: pretty or yaml" default:"pretty"`
}
p := HistoryCmdParams{}
return boa.Wrap{
Use: "history",
Short: "Prints the conversation history of the current session",
ValidArgsFunc: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return storedSessionIDs(), cobra.ShellCompDirectiveDefault
},
Params: &p,
Run: func(cmd *cobra.Command, args []string) {
state := session.LoadSession(session.GetSessionID(p.Session.GetOrElse("")))
oneMsgPrinted := false
for _, entry := range state.History {
if entry.Type == "message" {
if p.Format.Value() == "pretty" {
fmt.Printf("\n----------------------\n")
fmt.Printf("| %s\n", entry.Message.SourceType)
fmt.Printf("-------------\n")
fmt.Printf("%s\n", entry.Message.Content)
} else if p.Format.Value() == "yaml" {
if oneMsgPrinted {
fmt.Printf("---\n")
}
fmt.Printf("%s", entry.Message.ToYaml())
} else {
common.FailAndExit(1, fmt.Sprintf("Unsupported format: %s", p.Format.Value()))
}
oneMsgPrinted = true
} else {
slog.Warn(fmt.Sprintf("Unsupported entry type: %s", entry.Type))
}
}
},
}.ToCmd()
}
func newOrResetCmd(name string) *cobra.Command {
var p struct {
Session boa.Optional[string] `descr:"Session id" positional:"true" name:"session"`
Verbose boa.Required[bool] `descr:"Verbose output" short:"v" default:"false" name:"verbose"`
}
return boa.Wrap{
Use: name,
Short: "Create a new session",
Params: &p,
Run: func(cmd *cobra.Command, args []string) {
session.NewSession(p.Session.GetOrElse(""))
},
}.ToCmd()
}
func setSessionCmd() *cobra.Command {
var p struct {
Session boa.Required[string] `descr:"Session id" positional:"true"`
Verbose boa.Required[bool] `descr:"Verbose output" short:"v" default:"false" name:"verbose"`
}
return boa.Wrap{
Use: "set",
Short: "Set the ai session",
ValidArgsFunc: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return storedSessionIDs(), cobra.ShellCompDirectiveDefault
},
Params: &p,
Run: func(cmd *cobra.Command, args []string) {
session.SetSession(p.Session.Value())
},
}.ToCmd()
}
func renameCmd() *cobra.Command {
var p struct {
Arg1 boa.Required[string] `descr:"arg1 (new name if 1 arg, from name if 2 args)" positional:"true"`
Arg2 boa.Optional[string] `descr:"arg2 (to name if 2 args)" positional:"true"`
Verbose boa.Required[bool] `descr:"Verbose output" short:"v" default:"false" name:"verbose"`
}
return boa.Wrap{
Use: "rename",
Short: "Rename a session",
Params: &p,
Run: func(cmd *cobra.Command, args []string) {
if p.Arg2.HasValue() {
session.RenameSession(p.Arg1.Value(), *p.Arg2.Value())
} else {
session.RenameSession("", p.Arg1.Value())
}
},
}.ToCmd()
}
func copyCmd() *cobra.Command {
var p struct {
Arg1 boa.Required[string] `descr:"arg1 (new name if 1 arg, from name if 2 args)" positional:"true"`
Arg2 boa.Optional[string] `descr:"arg2 (to name if 2 args)" positional:"true"`
Verbose boa.Required[bool] `descr:"Verbose output" short:"v" default:"false" name:"verbose"`
}
return boa.Wrap{
Use: "copy",
Short: "Copy a session",
Params: &p,
Run: func(cmd *cobra.Command, args []string) {
if p.Arg2.HasValue() {
session.CopySession(p.Arg1.Value(), *p.Arg2.Value())
} else {
session.CopySession("", p.Arg1.Value())
}
},
}.ToCmd()
}
func nameAll() *cobra.Command {
var p struct {
Verbose boa.Required[bool] `descr:"Verbose output" short:"v" default:"false" name:"verbose"`
Yes boa.Required[bool] `descr:"Verbose output" short:"y" default:"false" name:"yes"`
Provider boa.Optional[string] `descr:"AI provider to use" name:"provider" env:"AI_PROVIDER" short:"p"`
}
return boa.Wrap{
Use: "name-all",
Short: "generate names to replace UUID session IDs",
Params: &p,
Run: func(cmd *cobra.Command, args []string) {
sessions := session.ListSessions()
cfgFilePath, storedCfg := config.LoadCfgFile()
cfg := config.ValidateCfg(cfgFilePath, storedCfg, config.CliParams{Provider: p.Provider})
provider := providers.CreateProvider(cfg)
sessionsToRename := lo.Filter(sessions, func(s session.Header, _ int) bool {
return isUUID(s.SessionID)
})
if len(sessionsToRename) == 0 {
fmt.Printf("No UUID sessions to rename\n")
return
}
for _, s := range sessionsToRename {
fmt.Printf("Processing session %s\n", s.SessionID)
if !p.Yes.Value() {
newQuestionTokens := s.InputTokens + s.OutputTokens // the last inputTokenCount and outputTokenCount is what we will send as input to the next question
res := askYesNo(" The total input tokens that will be used to generate the name: " + strconv.Itoa(newQuestionTokens) +
"\n Do you wish to auto-assign a name to it?")
if !res {
fmt.Printf(" Skipping session %s\n", s.SessionID)
continue
}
}
sessionData := session.LoadSession(s.SessionID)
newQuestionMsgs := append(sessionData.MessageHistory(), domain.Message{
SourceType: domain.User,
Content: "Please summarize this conversation in 3 words, concatenated with _ (underscores)",
})
resp, err := provider.BasicAsk(domain.Question{
Messages: newQuestionMsgs,
})
if err != nil {
common.FailAndExit(1, fmt.Sprintf("Failed to ask question: %v", err))
}
if len(resp.GetChoices()) == 0 {
common.FailAndExit(1, "No response from provider")
}
newName := strings.ToLower(resp.GetChoices()[0].Message.Content)
newName = string(lo.Filter([]rune(newName), func(r rune, _ int) bool {
return session.IsAllowedNameChar(r)
}))
newName = strings.TrimSpace(newName)
if newName == "" {
common.FailAndExit(1, "No name returned from provider")
}
fmt.Printf(" ==>>> %s\n", newName)
if !p.Yes.Value() {
res := askYesNo(" Do you wish to assign this name to the session?")
if !res {
fmt.Printf(" Skipping session %s\n", s.SessionID)
continue
}
}
session.RenameSession(s.SessionID, newName)
}
},
}.ToCmd()
}
func deleteSessionCmd() *cobra.Command {
var p struct {
Session boa.Optional[string] `descr:"Session id" positional:"true" name:"session"`
Verbose boa.Required[bool] `descr:"Verbose output" short:"v" default:"false" name:"verbose"`
Yes boa.Required[bool] `descr:"Auto confirm" short:"y" default:"false" name:"yes"`
}
return boa.Wrap{
Use: "delete",
Short: "Delete a session, or the current session if no session id is provided",
ValidArgsFunc: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return storedSessionIDs(), cobra.ShellCompDirectiveDefault
},
Params: &p,
Run: func(cmd *cobra.Command, args []string) {
session.DeleteSession(p.Session.GetOrElse(""), p.Yes.Value())
},
}.ToCmd()
}
func prepCmd() *cobra.Command {
var p struct {
Verbose boa.Required[bool] `descr:"Verbose output" short:"v" default:"false" name:"verbose"`
}
return boa.Wrap{
Use: "prep",
Short: "Add a user message to the current session without sending a question",
Params: &p,
Args: cobra.MinimumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
question := strings.Join(args, " ")
stdInAttachment, err := util.ReadAllStdIn()
if err != nil {
common.FailAndExit(1, fmt.Sprintf("Failed to read attachment from stdin: %v", err))
}
if stdInAttachment != "" {
if question != "" {
footer := fmt.Sprintf("\n Attached additional info/data: \n %s", stdInAttachment)
question = fmt.Sprintf("%s\n%s", question, footer)
} else {
question = stdInAttachment
}
}
if question == "" {
common.FailAndExit(1, "No data provided")
}
state := session.LoadSession(session.GetSessionID(""))
newMessage := domain.Message{
SourceType: domain.User,
Content: question,
}
state.AddMessage(newMessage)
session.StoreSession(state)
if p.Verbose.Value() {
fmt.Printf("Added message to session %s: %s\n", state.SessionID, question)
}
},
}.ToCmd()
}
func storedSessionIDs() []string {
availableSessions := session.ListSessions()
return lo.Map(availableSessions, func(s session.Header, _ int) string {
return s.SessionID
})
}
func askYesNo(question string) bool {
fmt.Printf("%s (y/n) ", question)
var answer string
_, err := fmt.Scanln(&answer)
if err != nil {
common.FailAndExit(1, fmt.Sprintf("Failed to read answer: %v", err))
}
return strings.HasPrefix(strings.ToLower(strings.TrimSpace(answer)), "y")
}
func isUUID(s string) bool {
_, err := uuid.Parse(s)
return err == nil
}