feat: implement print session and context

This commit is contained in:
Eugen Eisler
2024-10-05 00:57:11 +02:00
parent a6eeff2c91
commit 9452d6bd2a
8 changed files with 127 additions and 70 deletions

View File

@@ -13,7 +13,7 @@ import (
)
// Cli Controls the cli. It takes in the flags and runs the appropriate functions
func Cli(version string) (message string, err error) {
func Cli(version string) (err error) {
var currentFlags *Flags
if currentFlags, err = Init(); err != nil {
return
@@ -55,7 +55,6 @@ func Cli(version string) (message string, err error) {
}
}
// if the update patterns flag is set, run the update patterns function
if currentFlags.UpdatePatterns {
err = fabric.PopulateDB()
return
@@ -66,7 +65,6 @@ func Cli(version string) (message string, err error) {
return
}
// if the latest patterns flag is set, run the latest patterns function
if currentFlags.LatestPatterns != "0" {
var parsedToInt int
if parsedToInt, err = strconv.Atoi(currentFlags.LatestPatterns); err != nil {
@@ -79,42 +77,46 @@ func Cli(version string) (message string, err error) {
return
}
// if the list patterns flag is set, run the list all patterns function
if currentFlags.ListPatterns {
err = fabricDb.Patterns.ListNames()
return
}
// if the list all models flag is set, run the list all models function
if currentFlags.ListAllModels {
fabric.GetModels().Print()
return
}
// if the list all contexts flag is set, run the list all contexts function
if currentFlags.ListAllContexts {
err = fabricDb.Contexts.ListNames()
return
}
// if the list all sessions flag is set, run the list all sessions function
if currentFlags.ListAllSessions {
err = fabricDb.Sessions.ListNames()
return
}
// if the wipe context flag is set, run the wipe context function
if currentFlags.WipeContext != "" {
err = fabricDb.Contexts.Delete(currentFlags.WipeContext)
return
}
// if the wipe session flag is set, run the wipe session function
if currentFlags.WipeSession != "" {
err = fabricDb.Sessions.Delete(currentFlags.WipeSession)
return
}
if currentFlags.PrintSession != "" {
err = fabricDb.Sessions.PrintSession(currentFlags.PrintSession)
return
}
if currentFlags.PrintContext != "" {
err = fabricDb.Contexts.PrintContext(currentFlags.PrintContext)
return
}
if currentFlags.HtmlReadability {
if msg, cleanErr := converter.HtmlReadability(currentFlags.Message); cleanErr != nil {
fmt.Println("use original input, because can't apply html readability", err)
@@ -151,8 +153,6 @@ func Cli(version string) (message string, err error) {
return
}
// fmt.Println(transcript)
currentFlags.AppendMessage(transcript)
}
@@ -164,12 +164,10 @@ func Cli(version string) (message string, err error) {
commentsString := strings.Join(comments, "\n")
// fmt.Println(commentsString)
currentFlags.AppendMessage(commentsString)
}
if currentFlags.Pattern == "" {
if !currentFlags.IsChatRequest() {
// if the pattern flag is not set, we wanted only to grab the transcript or comments
fmt.Println(currentFlags.Message)
return
@@ -179,27 +177,25 @@ func Cli(version string) (message string, err error) {
if (currentFlags.ScrapeURL != "" || currentFlags.ScrapeQuestion != "") && fabric.Jina.IsConfigured() {
// Check if the scrape_url flag is set and call ScrapeURL
if currentFlags.ScrapeURL != "" {
if message, err = fabric.Jina.ScrapeURL(currentFlags.ScrapeURL); err != nil {
var website string
if website, err = fabric.Jina.ScrapeURL(currentFlags.ScrapeURL); err != nil {
return
}
//fmt.Println(message)
currentFlags.AppendMessage(message)
currentFlags.AppendMessage(website)
}
// Check if the scrape_question flag is set and call ScrapeQuestion
if currentFlags.ScrapeQuestion != "" {
if message, err = fabric.Jina.ScrapeQuestion(currentFlags.ScrapeQuestion); err != nil {
var website string
if website, err = fabric.Jina.ScrapeQuestion(currentFlags.ScrapeQuestion); err != nil {
return
}
//fmt.Println(message)
currentFlags.AppendMessage(message)
currentFlags.AppendMessage(website)
}
if currentFlags.Pattern == "" {
if !currentFlags.IsChatRequest() {
// if the pattern flag is not set, we wanted only to grab the url or get the answer to the question
fmt.Println(currentFlags.Message)
return
@@ -211,24 +207,33 @@ func Cli(version string) (message string, err error) {
return
}
if message, err = chatter.Send(currentFlags.BuildChatRequest(), currentFlags.BuildChatOptions()); err != nil {
var session *db.Session
if session, err = chatter.Send(currentFlags.BuildChatRequest(), currentFlags.BuildChatOptions()); err != nil {
return
}
result := session.GetLastMessage().Content
if !currentFlags.Stream {
fmt.Println(message)
// print the result if it was not streamed already
fmt.Println(result)
}
// if the copy flag is set, copy the message to the clipboard
if currentFlags.Copy {
if err = fabric.CopyToClipboard(message); err != nil {
if err = fabric.CopyToClipboard(result); err != nil {
return
}
}
// if the output flag is set, create an output file
if currentFlags.Output != "" {
err = fabric.CreateOutputFile(message, currentFlags.Output)
if currentFlags.OutputPrompt {
sessionAsString := session.String()
err = fabric.CreateOutputFile(sessionAsString, currentFlags.Output)
} else {
err = fabric.CreateOutputFile(result, currentFlags.Output)
}
}
return
}

View File

@@ -15,10 +15,9 @@ func TestCli(t *testing.T) {
defer func() { os.Args = originalArgs }()
os.Args = []string{os.Args[0]}
message, err := Cli("test")
err := Cli("test")
assert.Error(t, err)
assert.Equal(t, core.NoSessionPatternUserMessages, err.Error())
assert.Empty(t, message)
}
func TestSetup(t *testing.T) {

View File

@@ -36,6 +36,7 @@ type Flags struct {
Copy bool `short:"c" long:"copy" description:"Copy to clipboard"`
Model string `short:"m" long:"model" description:"Choose model"`
Output string `short:"o" long:"output" description:"Output to file" default:""`
OutputPrompt bool `long:"output-prompt" description:"Output used prompt before the result"`
LatestPatterns string `short:"n" long:"latest" description:"Number of latest patterns to list" default:"0"`
ChangeDefaultModel bool `short:"d" long:"changeDefaultModel" description:"Change default model"`
YouTube string `short:"y" long:"youtube" description:"YouTube video \"URL\" to grab transcript, comments from it and send to chat"`
@@ -47,6 +48,8 @@ type Flags struct {
Seed int `short:"e" long:"seed" description:"Seed to be used for LMM generation"`
WipeContext string `short:"w" long:"wipecontext" description:"Wipe context"`
WipeSession string `short:"W" long:"wipesession" description:"Wipe session"`
PrintContext string `long:"printcontext" description:"Print context"`
PrintSession string `long:"printsession" description:"Print session"`
HtmlReadability bool `long:"readability" description:"Convert HTML input into a clean, readable view"`
DryRun bool `long:"dry-run" description:"Show what would be sent to the model without actually sending it"`
Version bool `long:"version" description:"Print current version"`
@@ -135,3 +138,8 @@ func (o *Flags) AppendMessage(message string) {
}
return
}
func (o *Flags) IsChatRequest() (ret bool) {
ret = o.Message != "" || o.Session != ""
return
}