2023-11-05 00:44:06 -06:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-11-05 01:40:55 -06:00
|
|
|
"strings"
|
2023-11-05 00:44:06 -06:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ShowWaitAnimation "draws" an animated ellipses to stdout until something is
|
|
|
|
// received on the signal channel. An empty string sent to the channel to
|
|
|
|
// noftify the caller that the animation has completed (carriage returned).
|
|
|
|
func ShowWaitAnimation(signal chan any) {
|
|
|
|
animationStep := 0
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case _ = <-signal:
|
|
|
|
fmt.Print("\r")
|
|
|
|
signal <- ""
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
modSix := animationStep % 6
|
|
|
|
if modSix == 3 || modSix == 0 {
|
|
|
|
fmt.Print("\r")
|
|
|
|
}
|
|
|
|
if modSix < 3 {
|
|
|
|
fmt.Print(".")
|
|
|
|
} else {
|
|
|
|
fmt.Print(" ")
|
|
|
|
}
|
|
|
|
animationStep++
|
|
|
|
time.Sleep(250 * time.Millisecond)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// HandledDelayedResponse writes a waiting animation (abusing \r) and the
|
2023-11-05 01:40:55 -06:00
|
|
|
// (possibly chunked) content received on the response channel to stdout.
|
|
|
|
// Blocks until the channel is closed.
|
|
|
|
func HandleDelayedResponse(response chan string) string {
|
2023-11-05 00:44:06 -06:00
|
|
|
waitSignal := make(chan any)
|
|
|
|
go ShowWaitAnimation(waitSignal)
|
|
|
|
|
2023-11-05 01:40:55 -06:00
|
|
|
sb := strings.Builder{}
|
|
|
|
|
2023-11-05 00:44:06 -06:00
|
|
|
firstChunk := true
|
|
|
|
for chunk := range response {
|
|
|
|
if firstChunk {
|
2023-11-05 11:19:30 -07:00
|
|
|
// notify wait animation that we've received data
|
2023-11-05 00:44:06 -06:00
|
|
|
waitSignal <- ""
|
2023-11-05 11:19:30 -07:00
|
|
|
// wait for signal that wait animation has completed
|
2023-11-05 00:44:06 -06:00
|
|
|
<-waitSignal
|
|
|
|
firstChunk = false
|
|
|
|
}
|
|
|
|
fmt.Print(chunk)
|
2023-11-05 01:40:55 -06:00
|
|
|
sb.WriteString(chunk)
|
2023-11-05 00:44:06 -06:00
|
|
|
}
|
2023-11-05 01:40:55 -06:00
|
|
|
|
|
|
|
return sb.String()
|
2023-11-05 00:44:06 -06:00
|
|
|
}
|
2023-11-05 01:50:07 -07:00
|
|
|
|
|
|
|
func (m *Message) RenderTTY(paddingDown bool) {
|
|
|
|
var friendlyRole string
|
|
|
|
switch m.Role {
|
|
|
|
case "user":
|
|
|
|
friendlyRole = "You"
|
|
|
|
case "system":
|
|
|
|
friendlyRole = "System"
|
|
|
|
case "assistant":
|
|
|
|
friendlyRole = "Assistant"
|
|
|
|
}
|
|
|
|
fmt.Printf("<%s>\n\n", friendlyRole)
|
|
|
|
if m.OriginalContent != "" {
|
|
|
|
fmt.Print(m.OriginalContent)
|
|
|
|
}
|
|
|
|
if paddingDown {
|
|
|
|
fmt.Print("\n\n")
|
|
|
|
}
|
|
|
|
}
|