Add --offset flag to edit command

This commit is contained in:
Matt Low 2024-01-09 18:10:05 +00:00
parent b93ee94233
commit 51ce74ad3a

View File

@ -34,6 +34,7 @@ func init() {
listCmd.Flags().Bool("all", false, fmt.Sprintf("Show all conversations, by default only the last %d are shown", LS_LIMIT)) listCmd.Flags().Bool("all", false, fmt.Sprintf("Show all conversations, by default only the last %d are shown", LS_LIMIT))
renameCmd.Flags().Bool("generate", false, "Generate a conversation title") renameCmd.Flags().Bool("generate", false, "Generate a conversation title")
editCmd.Flags().Int("offset", 1, "Offset from the last reply to edit (Default: edit your last message, assuming there's an assistant reply)")
rootCmd.AddCommand( rootCmd.AddCommand(
cloneCmd, cloneCmd,
@ -659,34 +660,39 @@ var editCmd = &cobra.Command{
Fatal("Could not retrieve messages for conversation: %s\n", conversation.Title) Fatal("Could not retrieve messages for conversation: %s\n", conversation.Title)
} }
offset, _ := cmd.Flags().GetInt("offset")
if offset < 0 {
offset = -offset
}
if offset > len(messages) - 1 {
Fatal("Offset %d is before the start of the conversation\n", offset)
}
desiredIdx := len(messages) - 1 - offset
// walk backwards through the conversation deleting messages until and // walk backwards through the conversation deleting messages until and
// including the last user message // including the last user message
toRemove := []Message{} toRemove := []Message{}
var lastUserMessage *Message var toEdit *Message
for i := len(messages) - 1; i >= 0; i-- { for i := len(messages) - 1; i >= 0; i-- {
if messages[i].Role == MessageRoleUser { if i == desiredIdx {
lastUserMessage = &messages[i] toEdit = &messages[i]
} }
toRemove = append(toRemove, messages[i]) toRemove = append(toRemove, messages[i])
messages = messages[:i] messages = messages[:i]
if lastUserMessage != nil { if toEdit != nil {
break break
} }
} }
if lastUserMessage == nil { existingContents := toEdit.OriginalContent
Fatal("No messages left in the conversation, nothing to edit.\n")
}
existingContents := lastUserMessage.OriginalContent
newContents := inputFromArgsOrEditor(args[1:], "# Save when finished editing\n", existingContents) newContents := inputFromArgsOrEditor(args[1:], "# Save when finished editing\n", existingContents)
if newContents == existingContents { switch newContents {
case existingContents:
Fatal("No edits were made.\n") Fatal("No edits were made.\n")
} case "":
if newContents == "" {
Fatal("No message was provided.\n") Fatal("No message was provided.\n")
} }