From 927aa81670c7cdb4db3a5ba54d32c17dcedcb08a Mon Sep 17 00:00:00 2001 From: Lorentz Kinde Date: Mon, 19 Aug 2024 20:15:51 +0300 Subject: [PATCH] Added replay command which replays most recent reply (#21) 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 --- internal/reply/replay.go | 22 ++++++++++++++++++++++ internal/reply/reply.go | 11 +++++++++++ internal/setup.go | 10 ++++++++++ main.go | 1 + 4 files changed, 44 insertions(+) create mode 100644 internal/reply/replay.go diff --git a/internal/reply/replay.go b/internal/reply/replay.go new file mode 100644 index 0000000..3dfdb8b --- /dev/null +++ b/internal/reply/replay.go @@ -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 +} diff --git a/internal/reply/reply.go b/internal/reply/reply.go index dc3b3eb..7df1ef4 100644 --- a/internal/reply/reply.go +++ b/internal/reply/reply.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "io/fs" + "os" "path" "time" @@ -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) { diff --git a/internal/setup.go b/internal/setup.go index 281ec27..99e9ce6 100644 --- a/internal/setup.go +++ b/internal/setup.go @@ -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" @@ -36,6 +37,7 @@ const ( VERSION SETUP CMD + REPLAY ) var defaultFlags = Configurations{ @@ -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]) } @@ -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) } diff --git a/main.go b/main.go index 1eeb7fa..c04819e 100644 --- a/main.go +++ b/main.go @@ -42,6 +42,7 @@ Commands: p|photo Ask the photo model a picture with the requested prompt g|glob Query the chat model with the contents of the files found by the glob and the given text cmd 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 Create a new chat with the given prompt. c|chat c|continue Continue an existing chat with the given chat ID.