From 90d85e676d306566e7ebf1646ed2c6e998781c85 Mon Sep 17 00:00:00 2001 From: Matt Low Date: Tue, 14 Nov 2023 02:09:09 +0000 Subject: [PATCH] Implement `lmcli reply` --- pkg/cli/cmd.go | 70 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 69 insertions(+), 1 deletion(-) diff --git a/pkg/cli/cmd.go b/pkg/cli/cmd.go index da61849..7bd8504 100644 --- a/pkg/cli/cmd.go +++ b/pkg/cli/cmd.go @@ -201,8 +201,75 @@ var replyCmd = &cobra.Command{ Use: "reply", Short: "Send a reply to a conversation", Long: `Sends a reply to conversation and writes the response to stdout.`, + Args: func(cmd *cobra.Command, args []string) error { + argCount := 1 + if err := cobra.MinimumNArgs(argCount)(cmd, args); err != nil { + return err + } + return nil + }, Run: func(cmd *cobra.Command, args []string) { - fmt.Println("Replying to a conversation...") + shortName := args[0] + conversation, err := store.ConversationByShortName(shortName) + if conversation.ID == 0 { + Fatal("Conversation not found with short name: %s\n", shortName) + } + + messages, err := store.Messages(conversation) + if err != nil { + Fatal("Could not retrieve messages for conversation: %s\n", conversation.Title) + } + + messageContents, err := InputFromEditor("# How would you like to reply?\n", "reply.*.md") + + userReply := Message{ + ConversationID: conversation.ID, + Role: "user", + OriginalContent: messageContents, + } + + err = store.SaveMessage(&userReply) + if err != nil { + Warn("Could not save your reply: %v\n", err) + } + + messages = append(messages, userReply) + for _, message := range messages { + message.RenderTTY(true) + } + + assistantReply := Message{ + ConversationID: conversation.ID, + Role: "assistant", + } + assistantReply.RenderTTY(false) + + receiver := make(chan string) + response := make(chan string) + go func() { + response <- HandleDelayedResponse(receiver) + }() + + err = CreateChatCompletionStream(config.OpenAI.DefaultModel, messages, MAX_TOKENS, receiver) + if err != nil { + Fatal("%v\n", err) + } + + assistantReply.OriginalContent = <-response + + err = store.SaveMessage(&assistantReply) + if err != nil { + Fatal("Could not save assistant reply: %v\n", err) + } + + fmt.Println() + }, + ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + compMode := cobra.ShellCompDirectiveNoFileComp + if len(args) != 0 { + return nil, compMode + } + return store.ConversationShortNameCompletions(toComplete), compMode }, } @@ -326,6 +393,7 @@ func NewRootCmd() *cobra.Command { lsCmd, newCmd, promptCmd, + replyCmd, rmCmd, viewCmd, )