Add --offset flag to edit command
This commit is contained in:
parent
b93ee94233
commit
51ce74ad3a
@ -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))
|
||||
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(
|
||||
cloneCmd,
|
||||
@ -659,34 +660,39 @@ var editCmd = &cobra.Command{
|
||||
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
|
||||
// including the last user message
|
||||
toRemove := []Message{}
|
||||
var lastUserMessage *Message
|
||||
var toEdit *Message
|
||||
for i := len(messages) - 1; i >= 0; i-- {
|
||||
if messages[i].Role == MessageRoleUser {
|
||||
lastUserMessage = &messages[i]
|
||||
if i == desiredIdx {
|
||||
toEdit = &messages[i]
|
||||
}
|
||||
|
||||
toRemove = append(toRemove, messages[i])
|
||||
messages = messages[:i]
|
||||
if lastUserMessage != nil {
|
||||
if toEdit != nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if lastUserMessage == nil {
|
||||
Fatal("No messages left in the conversation, nothing to edit.\n")
|
||||
}
|
||||
|
||||
existingContents := lastUserMessage.OriginalContent
|
||||
existingContents := toEdit.OriginalContent
|
||||
|
||||
newContents := inputFromArgsOrEditor(args[1:], "# Save when finished editing\n", existingContents)
|
||||
if newContents == existingContents {
|
||||
switch newContents {
|
||||
case existingContents:
|
||||
Fatal("No edits were made.\n")
|
||||
}
|
||||
|
||||
if newContents == "" {
|
||||
case "":
|
||||
Fatal("No message was provided.\n")
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user