Code deduplication

This commit is contained in:
Matt Low 2023-11-29 05:10:44 +00:00
parent c64bc370f4
commit e29dbaf2a3

View File

@ -78,6 +78,22 @@ func LLMRequest(messages []Message) ([]Message, error) {
return replies, err return replies, err
} }
func (c *Conversation) GenerateAndSaveReplies(messages []Message) {
replies, err := LLMRequest(messages)
if err != nil {
Fatal("Error fetching LLM response: %v\n", err)
}
for _, reply := range replies {
reply.ConversationID = c.ID
err = store.SaveMessage(&reply)
if err != nil {
Warn("Could not save reply: %v\n", err)
}
}
}
// InputFromArgsOrEditor returns either the provided input from the args slice // InputFromArgsOrEditor returns either the provided input from the args slice
// (joined with spaces), or if len(args) is 0, opens an editor and returns // (joined with spaces), or if len(args) is 0, opens an editor and returns
// whatever input was provided there. placeholder is a string which populates // whatever input was provided there. placeholder is a string which populates
@ -318,24 +334,9 @@ var replyCmd = &cobra.Command{
messages = append(messages, userReply) messages = append(messages, userReply)
RenderConversation(messages, true) RenderConversation(messages, true)
// output an <Assistant> message heading (&Message{Role: MessageRoleAssistant}).RenderTTY()
(&Message{
Role: MessageRoleAssistant,
}).RenderTTY()
replies, err := LLMRequest(messages) conversation.GenerateAndSaveReplies(messages)
if err != nil {
Fatal("Error fetching LLM response: %v\n", err)
}
for _, reply := range replies {
reply.ConversationID = conversation.ID
err = store.SaveMessage(&reply)
if err != nil {
Warn("Could not save reply: %v\n", err)
}
}
}, },
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
compMode := cobra.ShellCompDirectiveNoFileComp compMode := cobra.ShellCompDirectiveNoFileComp
@ -382,25 +383,9 @@ var newCmd = &cobra.Command{
} }
RenderConversation(messages, true) RenderConversation(messages, true)
(&Message{Role: MessageRoleAssistant}).RenderTTY()
// output an <Assistant> message heading conversation.GenerateAndSaveReplies(messages)
(&Message{
Role: MessageRoleAssistant,
}).RenderTTY()
replies, err := LLMRequest(messages)
if err != nil {
Fatal("Error fetching LLM response: %v\n", err)
}
for _, reply := range replies {
reply.ConversationID = conversation.ID
err = store.SaveMessage(&reply)
if err != nil {
Warn("Could not save reply: %v\n", err)
}
}
err = conversation.GenerateTitle() err = conversation.GenerateTitle()
if err != nil { if err != nil {
@ -485,23 +470,9 @@ var retryCmd = &cobra.Command{
messages = messages[:lastUserMessageIndex+1] messages = messages[:lastUserMessageIndex+1]
RenderConversation(messages, true) RenderConversation(messages, true)
(&Message{ (&Message{Role: MessageRoleAssistant}).RenderTTY()
Role: MessageRoleAssistant,
}).RenderTTY()
replies, err := LLMRequest(messages) conversation.GenerateAndSaveReplies(messages)
if err != nil {
Fatal("Error fetching LLM response: %v\n", err)
}
for _, reply := range replies {
reply.ConversationID = conversation.ID
err = store.SaveMessage(&reply)
if err != nil {
Warn("Could not save reply: %v\n", err)
}
}
}, },
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
compMode := cobra.ShellCompDirectiveNoFileComp compMode := cobra.ShellCompDirectiveNoFileComp
@ -536,23 +507,9 @@ var continueCmd = &cobra.Command{
} }
RenderConversation(messages, true) RenderConversation(messages, true)
(&Message{ (&Message{Role: MessageRoleAssistant}).RenderTTY()
Role: MessageRoleAssistant,
}).RenderTTY()
replies, err := LLMRequest(messages) conversation.GenerateAndSaveReplies(messages)
if err != nil {
Fatal("Error fetching LLM response: %v\n", err)
}
for _, reply := range replies {
reply.ConversationID = conversation.ID
err = store.SaveMessage(&reply)
if err != nil {
Warn("Could not save reply: %v\n", err)
}
}
}, },
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
compMode := cobra.ShellCompDirectiveNoFileComp compMode := cobra.ShellCompDirectiveNoFileComp