Don't export some additional functions, rename slightly

This commit is contained in:
Matt Low 2024-01-03 05:52:41 +00:00
parent becaa5c7c0
commit a25d0d95e8

View File

@ -31,6 +31,7 @@ func init() {
rootCmd.AddCommand( rootCmd.AddCommand(
continueCmd, continueCmd,
editCmd,
lsCmd, lsCmd,
newCmd, newCmd,
promptCmd, promptCmd,
@ -39,7 +40,6 @@ func init() {
retryCmd, retryCmd,
rmCmd, rmCmd,
viewCmd, viewCmd,
editCmd,
) )
} }
@ -47,11 +47,11 @@ func Execute() error {
return rootCmd.Execute() return rootCmd.Execute()
} }
func SystemPrompt() string { func getSystemPrompt() string {
if systemPromptFile != "" { if systemPromptFile != "" {
content, err := FileContents(systemPromptFile) content, err := FileContents(systemPromptFile)
if err != nil { if err != nil {
Fatal("Could not read file contents at %s: %v", systemPromptFile, err) Fatal("Could not read file contents at %s: %v\n", systemPromptFile, err)
} }
return content return content
} }
@ -82,9 +82,9 @@ func fetchAndShowCompletion(messages []Message) ([]Message, error) {
return replies, err return replies, err
} }
// lookupConversationByShortname either returns the conversation found by the // lookupConversation either returns the conversation found by the
// short name or exits the program // short name or exits the program
func lookupConversationByShortname(shortName string) *Conversation { func lookupConversation(shortName string) *Conversation {
c, err := store.ConversationByShortName(shortName) c, err := store.ConversationByShortName(shortName)
if err != nil { if err != nil {
Fatal("Could not lookup conversation: %v\n", err) Fatal("Could not lookup conversation: %v\n", err)
@ -137,11 +137,11 @@ func handleConversationReply(c *Conversation, persist bool, toSend ...Message) {
} }
} }
// 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
// the editor and gets stripped from the final output. // the editor and gets stripped from the final output.
func InputFromArgsOrEditor(args []string, placeholder string, existingMessage string) (message string) { func inputFromArgsOrEditor(args []string, placeholder string, existingMessage string) (message string) {
var err error var err error
if len(args) == 0 { if len(args) == 0 {
message, err = InputFromEditor(placeholder, "message.*.md", existingMessage) message, err = InputFromEditor(placeholder, "message.*.md", existingMessage)
@ -280,7 +280,7 @@ var rmCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
var toRemove []*Conversation var toRemove []*Conversation
for _, shortName := range args { for _, shortName := range args {
conversation := lookupConversationByShortname(shortName) conversation := lookupConversation(shortName)
toRemove = append(toRemove, conversation) toRemove = append(toRemove, conversation)
} }
var errors []error var errors []error
@ -327,7 +327,7 @@ var viewCmd = &cobra.Command{
}, },
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
shortName := args[0] shortName := args[0]
conversation := lookupConversationByShortname(shortName) conversation := lookupConversation(shortName)
messages, err := store.Messages(conversation) messages, err := store.Messages(conversation)
if err != nil { if err != nil {
@ -358,7 +358,7 @@ var renameCmd = &cobra.Command{
}, },
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
shortName := args[0] shortName := args[0]
conversation := lookupConversationByShortname(shortName) conversation := lookupConversation(shortName)
var err error var err error
generate, _ := cmd.Flags().GetBool("generate") generate, _ := cmd.Flags().GetBool("generate")
@ -370,7 +370,7 @@ var renameCmd = &cobra.Command{
} }
} else { } else {
if len(args) < 2 { if len(args) < 2 {
Fatal("Conversation title not provided.") Fatal("Conversation title not provided.\n")
} }
title = strings.Join(args[1:], " ") title = strings.Join(args[1:], " ")
} }
@ -403,9 +403,9 @@ var replyCmd = &cobra.Command{
}, },
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
shortName := args[0] shortName := args[0]
conversation := lookupConversationByShortname(shortName) conversation := lookupConversation(shortName)
reply := InputFromArgsOrEditor(args[1:], "# How would you like to reply?\n", "") reply := inputFromArgsOrEditor(args[1:], "# How would you like to reply?\n", "")
if reply == "" { if reply == "" {
Fatal("No reply was provided.\n") Fatal("No reply was provided.\n")
} }
@ -430,7 +430,7 @@ var newCmd = &cobra.Command{
Short: "Start a new conversation", Short: "Start a new conversation",
Long: `Start a new conversation with the Large Language Model.`, Long: `Start a new conversation with the Large Language Model.`,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
messageContents := InputFromArgsOrEditor(args, "# What would you like to say?\n", "") messageContents := inputFromArgsOrEditor(args, "# What would you like to say?\n", "")
if messageContents == "" { if messageContents == "" {
Fatal("No message was provided.\n") Fatal("No message was provided.\n")
} }
@ -445,7 +445,7 @@ var newCmd = &cobra.Command{
{ {
ConversationID: conversation.ID, ConversationID: conversation.ID,
Role: MessageRoleSystem, Role: MessageRoleSystem,
OriginalContent: SystemPrompt(), OriginalContent: getSystemPrompt(),
}, },
{ {
ConversationID: conversation.ID, ConversationID: conversation.ID,
@ -475,7 +475,7 @@ var promptCmd = &cobra.Command{
Short: "Do a one-shot prompt", Short: "Do a one-shot prompt",
Long: `Prompt the Large Language Model and get a response.`, Long: `Prompt the Large Language Model and get a response.`,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
message := InputFromArgsOrEditor(args, "# What would you like to say?\n", "") message := inputFromArgsOrEditor(args, "# What would you like to say?\n", "")
if message == "" { if message == "" {
Fatal("No message was provided.\n") Fatal("No message was provided.\n")
} }
@ -483,7 +483,7 @@ var promptCmd = &cobra.Command{
messages := []Message{ messages := []Message{
{ {
Role: MessageRoleSystem, Role: MessageRoleSystem,
OriginalContent: SystemPrompt(), OriginalContent: getSystemPrompt(),
}, },
{ {
Role: MessageRoleUser, Role: MessageRoleUser,
@ -511,7 +511,7 @@ var retryCmd = &cobra.Command{
}, },
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
shortName := args[0] shortName := args[0]
conversation := lookupConversationByShortname(shortName) conversation := lookupConversation(shortName)
messages, err := store.Messages(conversation) messages, err := store.Messages(conversation)
if err != nil { if err != nil {
@ -555,7 +555,7 @@ var continueCmd = &cobra.Command{
}, },
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
shortName := args[0] shortName := args[0]
conversation := lookupConversationByShortname(shortName) conversation := lookupConversation(shortName)
handleConversationReply(conversation, true) handleConversationReply(conversation, true)
}, },
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
@ -579,7 +579,7 @@ var editCmd = &cobra.Command{
}, },
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
shortName := args[0] shortName := args[0]
conversation := lookupConversationByShortname(shortName) conversation := lookupConversation(shortName)
messages, err := store.Messages(conversation) messages, err := store.Messages(conversation)
if err != nil { if err != nil {
@ -608,7 +608,7 @@ var editCmd = &cobra.Command{
existingContents := lastUserMessage.OriginalContent 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 { if newContents == existingContents {
Fatal("No edits were made.\n") Fatal("No edits were made.\n")
} }
@ -617,7 +617,7 @@ var editCmd = &cobra.Command{
Fatal("No message was provided.\n") Fatal("No message was provided.\n")
} }
for _, message := range(toRemove) { for _, message := range toRemove {
err = store.DeleteMessage(&message) err = store.DeleteMessage(&message)
if err != nil { if err != nil {
Warn("Could not delete message: %v\n", err) Warn("Could not delete message: %v\n", err)