Matt Low
db465f1bf0
Updated the behaviour of commands: - `lmcli edit` - no longer deletes messages past the edited message - `lmcli retry` - creates a branch from the previous user message
74 lines
2.2 KiB
Go
74 lines
2.2 KiB
Go
package cmd
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
cmdutil "git.mlow.ca/mlow/lmcli/pkg/cmd/util"
|
|
"git.mlow.ca/mlow/lmcli/pkg/lmcli"
|
|
"git.mlow.ca/mlow/lmcli/pkg/lmcli/model"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
func ContinueCmd(ctx *lmcli.Context) *cobra.Command {
|
|
cmd := &cobra.Command{
|
|
Use: "continue <conversation>",
|
|
Short: "Continue a conversation from the last message",
|
|
Long: `Re-prompt the conversation with all existing prompts. Useful if a reply was cut short.`,
|
|
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)
|
|
|
|
messages, err := ctx.Store.PathToLeaf(conversation.SelectedRoot)
|
|
if err != nil {
|
|
return fmt.Errorf("could not retrieve conversation messages: %v", err)
|
|
}
|
|
|
|
if len(messages) < 2 {
|
|
return fmt.Errorf("conversation expected to have at least 2 messages")
|
|
}
|
|
|
|
lastMessage := &messages[len(messages)-1]
|
|
if lastMessage.Role != model.MessageRoleAssistant {
|
|
return fmt.Errorf("the last message in the conversation is not an assistant message")
|
|
}
|
|
|
|
// Output the contents of the last message so far
|
|
fmt.Print(lastMessage.Content)
|
|
|
|
// Submit the LLM request, allowing it to continue the last message
|
|
continuedOutput, err := cmdutil.Prompt(ctx, messages, nil)
|
|
if err != nil {
|
|
return fmt.Errorf("error fetching LLM response: %v", err)
|
|
}
|
|
|
|
// Append the new response to the original message
|
|
lastMessage.Content += strings.TrimRight(continuedOutput, "\n\t ")
|
|
|
|
// Update the original message
|
|
err = ctx.Store.UpdateMessage(lastMessage)
|
|
if err != nil {
|
|
return fmt.Errorf("could not update the last message: %v", err)
|
|
}
|
|
|
|
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
|
|
},
|
|
}
|
|
applyPromptFlags(ctx, cmd)
|
|
return cmd
|
|
}
|