Compare commits

..

No commits in common. "db788760a3cb386560b3f8be2a5339a191e00091" and "239ded18f357d01780a6db175fa8e2af8fe7eb2e" have entirely different histories.

View File

@ -17,28 +17,20 @@ var (
systemPromptFile string systemPromptFile string
) )
const (
// Limit to number of conversations shown with `ls`, without --all
LS_LIMIT int = 25
)
func init() { func init() {
inputCmds := []*cobra.Command{newCmd, promptCmd, replyCmd, retryCmd, continueCmd, editCmd} inputCmds := []*cobra.Command{newCmd, promptCmd, replyCmd, retryCmd, continueCmd, editCmd}
for _, cmd := range inputCmds { for _, cmd := range inputCmds {
cmd.Flags().IntVar(&maxTokens, "length", *config.OpenAI.DefaultMaxLength, "Maximum response tokens") cmd.Flags().IntVar(&maxTokens, "length", *config.OpenAI.DefaultMaxLength, "Max response length in tokens")
cmd.Flags().StringVar(&model, "model", *config.OpenAI.DefaultModel, "Which model to use model") cmd.Flags().StringVar(&model, "model", *config.OpenAI.DefaultModel, "The language model to use")
cmd.Flags().StringVar(&systemPrompt, "system-prompt", *config.ModelDefaults.SystemPrompt, "System prompt") cmd.Flags().StringVar(&systemPrompt, "system-prompt", *config.ModelDefaults.SystemPrompt, "The system prompt to use.")
cmd.Flags().StringVar(&systemPromptFile, "system-prompt-file", "", "A path to a file containing the system prompt") cmd.Flags().StringVar(&systemPromptFile, "system-prompt-file", "", "A path to a file whose contents are used as the system prompt.")
cmd.MarkFlagsMutuallyExclusive("system-prompt", "system-prompt-file") cmd.MarkFlagsMutuallyExclusive("system-prompt", "system-prompt-file")
} }
lsCmd.Flags().Bool("all", false, fmt.Sprintf("Show all conversations, by default only the last %d are shown", LS_LIMIT)) renameCmd.Flags().Bool("generate", false, "Use the LLM to generate the conversation title.")
renameCmd.Flags().Bool("generate", false, "Generate a conversation title")
rootCmd.AddCommand( rootCmd.AddCommand(
cloneCmd,
continueCmd, continueCmd,
editCmd,
lsCmd, lsCmd,
newCmd, newCmd,
promptCmd, promptCmd,
@ -47,6 +39,7 @@ func init() {
retryCmd, retryCmd,
rmCmd, rmCmd,
viewCmd, viewCmd,
editCmd,
) )
} }
@ -54,11 +47,11 @@ func Execute() error {
return rootCmd.Execute() return rootCmd.Execute()
} }
func getSystemPrompt() string { func SystemPrompt() 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\n", systemPromptFile, err) Fatal("Could not read file contents at %s: %v", systemPromptFile, err)
} }
return content return content
} }
@ -89,9 +82,9 @@ func fetchAndShowCompletion(messages []Message) ([]Message, error) {
return replies, err return replies, err
} }
// lookupConversation either returns the conversation found by the // lookupConversationByShortname either returns the conversation found by the
// short name or exits the program // short name or exits the program
func lookupConversation(shortName string) *Conversation { func lookupConversationByShortname(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)
@ -102,17 +95,6 @@ func lookupConversation(shortName string) *Conversation {
return c return c
} }
func lookupConversationE(shortName string) (*Conversation, error) {
c, err := store.ConversationByShortName(shortName)
if err != nil {
return nil, fmt.Errorf("Could not lookup conversation: %v", err)
}
if c.ID == 0 {
return nil, fmt.Errorf("Conversation not found with short name: %s", shortName)
}
return c, nil
}
// handleConversationReply handles sending messages to an existing // handleConversationReply handles sending messages to an existing
// conversation, optionally persisting them. It displays the entire // conversation, optionally persisting them. It displays the entire
// conversation before // conversation before
@ -155,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)
@ -173,19 +155,17 @@ func inputFromArgsOrEditor(args []string, placeholder string, existingMessage st
} }
var rootCmd = &cobra.Command{ var rootCmd = &cobra.Command{
Use: "lmcli <command> [flags]", Use: "lmcli",
Long: `lmcli - Large Language Model CLI`, Long: `lmcli - command-line interface with Large Language Models.`,
SilenceErrors: true,
SilenceUsage: true,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
cmd.Usage() // execute `lm ls` by default
}, },
} }
var lsCmd = &cobra.Command{ var lsCmd = &cobra.Command{
Use: "ls", Use: "ls",
Short: "List conversations", Short: "List existing conversations",
Long: `List conversations in order of recent activity`, Long: `List all existing conversations in descending order of recent activity.`,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
conversations, err := store.Conversations() conversations, err := store.Conversations()
if err != nil { if err != nil {
@ -193,10 +173,25 @@ var lsCmd = &cobra.Command{
return return
} }
type Category struct { // Example output
name string // $ lmcli ls
cutoff time.Duration // last hour:
} // 98sg - 12 minutes ago - Project discussion
// last day:
// tj3l - 10 hours ago - Deep learning concepts
// last week:
// bwfm - 2 days ago - Machine learning study
// 8n3h - 3 days ago - Weekend plans
// f3n7 - 6 days ago - CLI development
// last month:
// 5hn2 - 8 days ago - Book club discussion
// b7ze - 20 days ago - Gardening tips and tricks
// last 6 months:
// 3jn2 - 30 days ago - Web development best practices
// 43jk - 2 months ago - Longboard maintenance
// g8d9 - 3 months ago - History book club
// 4lk3 - 4 months ago - Local events and meetups
// 43jn - 6 months ago - Mobile photography techniques
type ConversationLine struct { type ConversationLine struct {
timeSinceReply time.Duration timeSinceReply time.Duration
@ -204,41 +199,43 @@ var lsCmd = &cobra.Command{
} }
now := time.Now() now := time.Now()
categories := []string{
midnight := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()) "recent",
monthStart := time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, now.Location()) "last hour",
dayOfWeek := int(now.Weekday()) "last 6 hours",
categories := []Category{ "last day",
{"today", now.Sub(midnight)}, "last week",
{"yesterday", now.Sub(midnight.AddDate(0, 0, -1))}, "last month",
{"this week", now.Sub(midnight.AddDate(0, 0, -dayOfWeek))}, "last 6 months",
{"last week", now.Sub(midnight.AddDate(0, 0, -(dayOfWeek + 7)))}, "older",
{"this month", now.Sub(monthStart)},
{"last month", now.Sub(monthStart.AddDate(0, -1, 0))},
{"2 months ago", now.Sub(monthStart.AddDate(0, -2, 0))},
{"3 months ago", now.Sub(monthStart.AddDate(0, -3, 0))},
{"4 months ago", now.Sub(monthStart.AddDate(0, -4, 0))},
{"5 months ago", now.Sub(monthStart.AddDate(0, -5, 0))},
{"older", now.Sub(time.Time{})},
} }
categorized := map[string][]ConversationLine{} categorized := map[string][]ConversationLine{}
all, _ := cmd.Flags().GetBool("all")
for _, conversation := range conversations { for _, conversation := range conversations {
lastMessage, err := store.LastMessage(&conversation) lastMessage, err := store.LastMessage(&conversation)
if lastMessage == nil || err != nil { if lastMessage == nil || err != nil {
continue continue
} }
messageAge := now.Sub(lastMessage.CreatedAt) messageAge := now.Sub(lastMessage.CreatedAt)
var category string var category string
for _, c := range categories { switch {
if messageAge < c.cutoff { case messageAge <= 10*time.Minute:
category = c.name category = "recent"
break case messageAge <= time.Hour:
} category = "last hour"
case messageAge <= 6*time.Hour:
category = "last 6 hours"
case messageAge <= 24*time.Hour:
category = "last day"
case messageAge <= 7*24*time.Hour:
category = "last week"
case messageAge <= 30*24*time.Hour:
category = "last month"
case messageAge <= 6*30*24*time.Hour: // Approximate as 6 months
category = "last 6 months"
default:
category = "older"
} }
formatted := fmt.Sprintf( formatted := fmt.Sprintf(
@ -247,34 +244,23 @@ var lsCmd = &cobra.Command{
humanTimeElapsedSince(messageAge), humanTimeElapsedSince(messageAge),
conversation.Title, conversation.Title,
) )
categorized[category] = append( categorized[category] = append(
categorized[category], categorized[category],
ConversationLine{messageAge, formatted}, ConversationLine{messageAge, formatted},
) )
} }
var conversationsPrinted int
outer:
for _, category := range categories { for _, category := range categories {
conversations, ok := categorized[category.name] conversations, ok := categorized[category]
if !ok { if !ok {
continue continue
} }
slices.SortFunc(conversations, func(a, b ConversationLine) int { slices.SortFunc(conversations, func(a, b ConversationLine) int {
return int(a.timeSinceReply - b.timeSinceReply) return int(a.timeSinceReply - b.timeSinceReply)
}) })
fmt.Printf("%s:\n", category)
fmt.Printf("%s:\n", category.name)
for _, conv := range conversations { for _, conv := range conversations {
if conversationsPrinted >= LS_LIMIT && !all {
fmt.Printf("%d remaining message(s), use --all to view.\n", len(conversations)-conversationsPrinted)
break outer
}
fmt.Printf(" %s\n", conv.formatted) fmt.Printf(" %s\n", conv.formatted)
conversationsPrinted++
} }
} }
}, },
@ -294,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 := lookupConversation(shortName) conversation := lookupConversationByShortname(shortName)
toRemove = append(toRemove, conversation) toRemove = append(toRemove, conversation)
} }
var errors []error var errors []error
@ -328,65 +314,6 @@ var rmCmd = &cobra.Command{
}, },
} }
var cloneCmd = &cobra.Command{
Use: "clone <conversation>",
Short: "Clone conversations",
Long: `Clones the provided conversation.`,
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 {
shortName := args[0]
toClone, err := lookupConversationE(shortName)
if err != nil {
return err
}
messagesToCopy, err := store.Messages(toClone)
if err != nil {
return fmt.Errorf("Could not retrieve messages for conversation: %s", toClone.ShortName.String)
}
clone := &Conversation{
Title: toClone.Title + " - Clone",
}
if err := store.SaveConversation(clone); err != nil {
return fmt.Errorf("Cloud not create clone: %s", err)
}
var errors []error
messageCnt := 0
for _, message := range messagesToCopy {
newMessage := message
newMessage.ConversationID = clone.ID
newMessage.ID = 0
if err := store.SaveMessage(&newMessage); err != nil {
errors = append(errors, err)
} else {
messageCnt++
}
}
if len(errors) > 0 {
return fmt.Errorf("Messages failed to be cloned: %v", errors)
}
fmt.Printf("Cloned %d messages to: %s\n", messageCnt, clone.Title)
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 store.ConversationShortNameCompletions(toComplete), compMode
},
}
var viewCmd = &cobra.Command{ var viewCmd = &cobra.Command{
Use: "view <conversation>", Use: "view <conversation>",
Short: "View messages in a conversation", Short: "View messages in a conversation",
@ -400,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 := lookupConversation(shortName) conversation := lookupConversationByShortname(shortName)
messages, err := store.Messages(conversation) messages, err := store.Messages(conversation)
if err != nil { if err != nil {
@ -431,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 := lookupConversation(shortName) conversation := lookupConversationByShortname(shortName)
var err error var err error
generate, _ := cmd.Flags().GetBool("generate") generate, _ := cmd.Flags().GetBool("generate")
@ -443,7 +370,7 @@ var renameCmd = &cobra.Command{
} }
} else { } else {
if len(args) < 2 { if len(args) < 2 {
Fatal("Conversation title not provided.\n") Fatal("Conversation title not provided.")
} }
title = strings.Join(args[1:], " ") title = strings.Join(args[1:], " ")
} }
@ -465,7 +392,7 @@ var renameCmd = &cobra.Command{
var replyCmd = &cobra.Command{ var replyCmd = &cobra.Command{
Use: "reply <conversation> [message]", Use: "reply <conversation> [message]",
Short: "Reply to a conversation", Short: "Send a reply to a conversation",
Long: `Sends a reply to conversation and writes the response to stdout.`, Long: `Sends a reply to conversation and writes the response to stdout.`,
Args: func(cmd *cobra.Command, args []string) error { Args: func(cmd *cobra.Command, args []string) error {
argCount := 1 argCount := 1
@ -476,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 := lookupConversation(shortName) conversation := lookupConversationByShortname(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")
} }
@ -503,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")
} }
@ -518,7 +445,7 @@ var newCmd = &cobra.Command{
{ {
ConversationID: conversation.ID, ConversationID: conversation.ID,
Role: MessageRoleSystem, Role: MessageRoleSystem,
OriginalContent: getSystemPrompt(), OriginalContent: SystemPrompt(),
}, },
{ {
ConversationID: conversation.ID, ConversationID: conversation.ID,
@ -548,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")
} }
@ -556,7 +483,7 @@ var promptCmd = &cobra.Command{
messages := []Message{ messages := []Message{
{ {
Role: MessageRoleSystem, Role: MessageRoleSystem,
OriginalContent: getSystemPrompt(), OriginalContent: SystemPrompt(),
}, },
{ {
Role: MessageRoleUser, Role: MessageRoleUser,
@ -573,7 +500,7 @@ var promptCmd = &cobra.Command{
var retryCmd = &cobra.Command{ var retryCmd = &cobra.Command{
Use: "retry <conversation>", Use: "retry <conversation>",
Short: "Retry the last user reply in a conversation", Short: "Retries the last conversation prompt.",
Long: `Re-prompt the conversation up to the last user response. Can be used to regenerate the last assistant reply, or simply generate one if an error occurred.`, Long: `Re-prompt the conversation up to the last user response. Can be used to regenerate the last assistant reply, or simply generate one if an error occurred.`,
Args: func(cmd *cobra.Command, args []string) error { Args: func(cmd *cobra.Command, args []string) error {
argCount := 1 argCount := 1
@ -584,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 := lookupConversation(shortName) conversation := lookupConversationByShortname(shortName)
messages, err := store.Messages(conversation) messages, err := store.Messages(conversation)
if err != nil { if err != nil {
@ -617,7 +544,7 @@ var retryCmd = &cobra.Command{
var continueCmd = &cobra.Command{ var continueCmd = &cobra.Command{
Use: "continue <conversation>", Use: "continue <conversation>",
Short: "Continue a conversation from the last message", Short: "Continues where the previous prompt left off.",
Long: `Re-prompt the conversation with all existing prompts. Useful if a reply was cut short.`, Long: `Re-prompt the conversation with all existing prompts. Useful if a reply was cut short.`,
Args: func(cmd *cobra.Command, args []string) error { Args: func(cmd *cobra.Command, args []string) error {
argCount := 1 argCount := 1
@ -628,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 := lookupConversation(shortName) conversation := lookupConversationByShortname(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) {
@ -642,7 +569,7 @@ var continueCmd = &cobra.Command{
var editCmd = &cobra.Command{ var editCmd = &cobra.Command{
Use: "edit <conversation>", Use: "edit <conversation>",
Short: "Edit the last user reply in a conversation", Short: "Edit the last user message in a conversation.",
Args: func(cmd *cobra.Command, args []string) error { Args: func(cmd *cobra.Command, args []string) error {
argCount := 1 argCount := 1
if err := cobra.MinimumNArgs(argCount)(cmd, args); err != nil { if err := cobra.MinimumNArgs(argCount)(cmd, args); err != nil {
@ -652,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 := lookupConversation(shortName) conversation := lookupConversationByShortname(shortName)
messages, err := store.Messages(conversation) messages, err := store.Messages(conversation)
if err != nil { if err != nil {
@ -681,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")
} }
@ -690,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)