2024-02-21 21:55:38 -07:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2024-06-12 02:35:07 -06:00
|
|
|
"git.mlow.ca/mlow/lmcli/pkg/api"
|
2024-02-21 21:55:38 -07:00
|
|
|
cmdutil "git.mlow.ca/mlow/lmcli/pkg/cmd/util"
|
|
|
|
"git.mlow.ca/mlow/lmcli/pkg/lmcli"
|
|
|
|
"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 {
|
2024-06-22 22:47:47 -06:00
|
|
|
err := validateGenerationFlags(ctx, cmd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-02-21 21:55:38 -07:00
|
|
|
shortName := args[0]
|
2024-10-19 20:38:42 -06:00
|
|
|
c := cmdutil.LookupConversation(ctx, shortName)
|
2024-02-21 21:55:38 -07:00
|
|
|
|
2024-10-19 20:38:42 -06:00
|
|
|
messages, err := ctx.Conversations.PathToLeaf(c.SelectedRoot)
|
2024-02-21 21:55:38 -07:00
|
|
|
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]
|
2024-06-12 02:35:07 -06:00
|
|
|
if lastMessage.Role != api.MessageRoleAssistant {
|
2024-02-21 21:55:38 -07:00
|
|
|
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
|
2024-05-07 01:11:04 -06:00
|
|
|
continuedOutput, err := cmdutil.Prompt(ctx, messages, nil)
|
2024-02-21 21:55:38 -07:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error fetching LLM response: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append the new response to the original message
|
2024-06-12 02:35:07 -06:00
|
|
|
lastMessage.Content += strings.TrimRight(continuedOutput.Content, "\n\t ")
|
2024-02-21 21:55:38 -07:00
|
|
|
|
|
|
|
// Update the original message
|
2024-10-19 20:38:42 -06:00
|
|
|
err = ctx.Conversations.UpdateMessage(lastMessage)
|
2024-02-21 21:55:38 -07:00
|
|
|
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
|
|
|
|
}
|
2024-10-19 20:38:42 -06:00
|
|
|
return ctx.Conversations.ConversationShortNameCompletions(toComplete), compMode
|
2024-02-21 21:55:38 -07:00
|
|
|
},
|
|
|
|
}
|
2024-06-22 22:47:47 -06:00
|
|
|
applyGenerationFlags(ctx, cmd)
|
2024-02-21 21:55:38 -07:00
|
|
|
return cmd
|
|
|
|
}
|