2024-02-21 21:55:38 -07:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"git.mlow.ca/mlow/lmcli/pkg/lmcli"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
cmdutil "git.mlow.ca/mlow/lmcli/pkg/cmd/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
func RenameCmd(ctx *lmcli.Context) *cobra.Command {
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "rename <conversation> [title]",
|
|
|
|
Short: "Rename a conversation",
|
|
|
|
Long: `Renames a conversation, either with the provided title or by generating a new name.`,
|
|
|
|
Args: func(cmd *cobra.Command, args []string) error {
|
|
|
|
argCount := 1
|
|
|
|
if err := cobra.MinimumNArgs(argCount)(cmd, args); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
shortName := args[0]
|
|
|
|
conversation := cmdutil.LookupConversation(ctx, shortName)
|
2024-05-20 12:12:44 -06:00
|
|
|
|
2024-02-21 21:55:38 -07:00
|
|
|
var err error
|
2024-05-20 12:12:44 -06:00
|
|
|
var title string
|
2024-02-21 21:55:38 -07:00
|
|
|
|
|
|
|
generate, _ := cmd.Flags().GetBool("generate")
|
|
|
|
if generate {
|
2024-05-20 12:12:44 -06:00
|
|
|
messages, err := ctx.Store.PathToLeaf(conversation.SelectedRoot)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Could not retrieve conversation messages: %v", err)
|
|
|
|
}
|
|
|
|
title, err = cmdutil.GenerateTitle(ctx, messages)
|
2024-02-21 21:55:38 -07:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Could not generate conversation title: %v", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if len(args) < 2 {
|
|
|
|
return fmt.Errorf("Conversation title not provided.")
|
|
|
|
}
|
|
|
|
title = strings.Join(args[1:], " ")
|
|
|
|
}
|
|
|
|
|
|
|
|
conversation.Title = title
|
2024-05-20 12:12:44 -06:00
|
|
|
err = ctx.Store.UpdateConversation(conversation)
|
2024-02-21 21:55:38 -07:00
|
|
|
if err != nil {
|
2024-05-20 12:12:44 -06:00
|
|
|
lmcli.Warn("Could not update conversation title: %v\n", err)
|
2024-02-21 21:55:38 -07:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
|
|
|
compMode := cobra.ShellCompDirectiveNoFileComp
|
|
|
|
if len(args) != 0 {
|
|
|
|
return nil, compMode
|
|
|
|
}
|
|
|
|
return ctx.Store.ConversationShortNameCompletions(toComplete), compMode
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-03-12 02:03:04 -06:00
|
|
|
cmd.Flags().Bool("generate", false, "Generate a conversation title")
|
|
|
|
|
2024-02-21 21:55:38 -07:00
|
|
|
return cmd
|
|
|
|
}
|