Skip to content

Commit

Permalink
Added replay command which replays most recent reply (#21)
Browse files Browse the repository at this point in the history
Examples:
`clai re`
`clai -r replay`

If you quickly want to check the most recent message or replay some reply without 
formatting, this is the command for you
  • Loading branch information
baalimago authored Aug 19, 2024
1 parent d4e117a commit 927aa81
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
22 changes: 22 additions & 0 deletions internal/reply/replay.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package reply

import (
"errors"
"fmt"

"github.com/baalimago/clai/internal/utils"
)

func Replay(raw bool) error {
prevReply, err := Load("")
if err != nil {
return fmt.Errorf("failed to load previous reply: %v", err)
}
amMessages := len(prevReply.Messages)
if amMessages == 0 {
return errors.New("failed to find any recent reply")
}
mostRecentMsg := prevReply.Messages[amMessages-1]
utils.AttemptPrettyPrint(mostRecentMsg, "system", raw)
return nil
}
11 changes: 11 additions & 0 deletions internal/reply/reply.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"io/fs"
"os"
"path"
"time"

Expand Down Expand Up @@ -41,7 +42,17 @@ func SaveAsPreviousQuery(claiConfDir string, msgs []models.Message) error {
}

// Load the prevQuery.json from the claiConfDir/conversations directory
// If claiConfDir is left empty, it will be re-constructed. The technical debt
// is piling up quite fast here
func Load(claiConfDir string) (models.Chat, error) {
if claiConfDir == "" {
confDir, err := os.UserConfigDir()
if err != nil {
return models.Chat{}, fmt.Errorf("failed to find home dir: %v", err)
}
claiConfDir = path.Join(confDir, ".clai")
}

c, err := chat.FromPath(path.Join(claiConfDir, "conversations", "prevQuery.json"))
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
Expand Down
10 changes: 10 additions & 0 deletions internal/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/baalimago/clai/internal/glob"
"github.com/baalimago/clai/internal/models"
"github.com/baalimago/clai/internal/photo"
"github.com/baalimago/clai/internal/reply"
"github.com/baalimago/clai/internal/setup"
"github.com/baalimago/clai/internal/text"
"github.com/baalimago/clai/internal/utils"
Expand All @@ -36,6 +37,7 @@ const (
VERSION
SETUP
CMD
REPLAY
)

var defaultFlags = Configurations{
Expand Down Expand Up @@ -81,6 +83,8 @@ func getModeFromArgs(cmd string) (Mode, error) {
return VERSION, nil
case "cmd":
return CMD, nil
case "replay", "re":
return REPLAY, nil
default:
return HELP, fmt.Errorf("unknown command: '%s'", os.Args[1])
}
Expand Down Expand Up @@ -235,6 +239,12 @@ func Setup(usage string) (models.Querier, error) {
}
os.Exit(0)
return nil, nil
case REPLAY:
err := reply.Replay(flagSet.PrintRaw)
if err != nil {
return nil, fmt.Errorf("failed to replay previous reply: %v", err)
}
os.Exit(0)
default:
return nil, fmt.Errorf("unknown mode: %v", mode)
}
Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Commands:
p|photo <text> Ask the photo model a picture with the requested prompt
g|glob <glob> <text> Query the chat model with the contents of the files found by the glob and the given text
cmd <text> Describe the command you wish to do, then execute the suggested command. It's a bit wonky when used with -re.
re|replay Replay the most recent message.
c|chat n|new <prompt> Create a new chat with the given prompt.
c|chat c|continue <chatID> Continue an existing chat with the given chat ID.
Expand Down

0 comments on commit 927aa81

Please sign in to comment.