Matt Low
0384c7cb66
This refactor splits out all conversation concerns into a new `conversation` package. There is now a split between `conversation` and `api`s representation of `Message`, the latter storing the minimum information required for interaction with LLM providers. There is necessary conversation between the two when making LLM calls.
79 lines
2.3 KiB
Go
79 lines
2.3 KiB
Go
package cmd
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"git.mlow.ca/mlow/lmcli/pkg/api"
|
|
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 {
|
|
err := validateGenerationFlags(ctx, cmd)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
shortName := args[0]
|
|
c := cmdutil.LookupConversation(ctx, shortName)
|
|
|
|
messages, err := ctx.Conversations.PathToLeaf(c.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 != api.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.Content, "\n\t ")
|
|
|
|
// Update the original message
|
|
err = ctx.Conversations.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.Conversations.ConversationShortNameCompletions(toComplete), compMode
|
|
},
|
|
}
|
|
applyGenerationFlags(ctx, cmd)
|
|
return cmd
|
|
}
|