Allow message input from either args or editor on all relevant commands

Those (sub-)commands being: `new`, `reply`, and `prompt`
This commit is contained in:
Matt Low 2023-11-20 16:50:56 +00:00
parent b5f066ff34
commit c8a1e3e105
1 changed files with 28 additions and 14 deletions

View File

@ -35,6 +35,23 @@ func Execute() error {
return rootCmd.Execute()
}
// InputFromArgsOrEditor returns either the provided input from the args slice
// (joined with spaces), or if len(args) is 0, opens an editor and returns
// whatever input was provided there. placeholder is a string which populates
// the editor and gets stripped from the final output.
func InputFromArgsOrEditor(args []string, placeholder string) (message string) {
var err error
if len(args) == 0 {
message, err = InputFromEditor(placeholder, "message.*.md")
if err != nil {
Fatal("Failed to get input: %v\n", err)
}
} else {
message = strings.Trim(strings.Join(args, " "), " \t\n")
}
return
}
var rootCmd = &cobra.Command{
Use: "lmcli",
Short: "Interact with Large Language Models",
@ -149,7 +166,7 @@ var lsCmd = &cobra.Command{
}
var rmCmd = &cobra.Command{
Use: "rm [conversation]",
Use: "rm <conversation>",
Short: "Remove a conversation",
Long: `Removes a conversation by its short name.`,
Args: func(cmd *cobra.Command, args []string) error {
@ -183,7 +200,7 @@ var rmCmd = &cobra.Command{
}
var viewCmd = &cobra.Command{
Use: "view [conversation]",
Use: "view <conversation>",
Short: "View messages in a conversation",
Long: `Finds a conversation by its short name and displays its contents.`,
Args: func(cmd *cobra.Command, args []string) error {
@ -217,7 +234,7 @@ var viewCmd = &cobra.Command{
}
var replyCmd = &cobra.Command{
Use: "reply",
Use: "reply <conversation> [message]",
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 {
@ -239,7 +256,7 @@ var replyCmd = &cobra.Command{
Fatal("Could not retrieve messages for conversation: %s\n", conversation.Title)
}
messageContents, err := InputFromEditor("# How would you like to reply?\n", "reply.*.md")
messageContents := InputFromArgsOrEditor(args[1:], "# How would you like to reply?\n")
if messageContents == "" {
Fatal("No reply was provided.\n")
}
@ -294,22 +311,18 @@ var replyCmd = &cobra.Command{
}
var newCmd = &cobra.Command{
Use: "new",
Use: "new [message]",
Short: "Start a new conversation",
Long: `Start a new conversation with the Large Language Model.`,
Run: func(cmd *cobra.Command, args []string) {
messageContents, err := InputFromEditor("# What would you like to say?\n", "message.*.md")
if err != nil {
Fatal("Failed to get input: %v\n", err)
}
messageContents := InputFromArgsOrEditor(args, "# What would you like to say?\n")
if messageContents == "" {
Fatal("No message was provided.\n")
}
// TODO: set title if --title provided, otherwise defer for later(?)
conversation := Conversation{}
err = store.SaveConversation(&conversation)
err := store.SaveConversation(&conversation)
if err != nil {
Fatal("Could not save new conversation: %v\n", err)
}
@ -365,6 +378,7 @@ var newCmd = &cobra.Command{
if err != nil {
Warn("Could not generate title for conversation: %v\n", err)
}
err = store.SaveConversation(&conversation)
if err != nil {
Warn("Could not save conversation after generating title: %v\n", err)
@ -373,12 +387,12 @@ var newCmd = &cobra.Command{
}
var promptCmd = &cobra.Command{
Use: "prompt",
Use: "prompt [message]",
Short: "Do a one-shot prompt",
Long: `Prompt the Large Language Model and get a response.`,
Run: func(cmd *cobra.Command, args []string) {
message := strings.Join(args, " ")
if len(strings.Trim(message, " \t\n")) == 0 {
message := InputFromArgsOrEditor(args, "# What would you like to say?\n")
if message == "" {
Fatal("No message was provided.\n")
}