diff --git a/cli/flags.go b/cli/flags.go index 54389cb0..d3f12bf7 100644 --- a/cli/flags.go +++ b/cli/flags.go @@ -15,7 +15,7 @@ import ( // Flags create flags struct. the users flags go into this, this will be passed to the chat struct in cli type Flags struct { Pattern string `short:"p" long:"pattern" description:"Choose a pattern from the available patterns" default:""` - PatternVariables map[string]string `short:"v" long:"variable" description:"Values for pattern variables, e.g. -v=$name:John -v=$age:30"` + PatternVariables map[string]string `short:"v" long:"variable" description:"Values for pattern variables, e.g. -v=#role:expert -v=#points:30"` Context string `short:"C" long:"context" description:"Choose a context from the available contexts" default:""` Session string `long:"session" description:"Choose a session from the available sessions"` Setup bool `short:"S" long:"setup" description:"Run setup for all reconfigurable parts of fabric"` @@ -143,6 +143,6 @@ func (o *Flags) AppendMessage(message string) { } func (o *Flags) IsChatRequest() (ret bool) { - ret = o.Message != "" || o.Session != "" + ret = (o.Message != "" || o.Context != "") && (o.Session != "" || o.Pattern != "") return } diff --git a/core/fabric.go b/core/fabric.go index 139b8688..5fa384d6 100644 --- a/core/fabric.go +++ b/core/fabric.go @@ -17,6 +17,7 @@ import ( "github.com/danielmiessler/fabric/plugins/ai/openrouter" "github.com/danielmiessler/fabric/plugins/ai/siliconcloud" "github.com/danielmiessler/fabric/plugins/tools/jina" + "github.com/danielmiessler/fabric/plugins/tools/lang" "github.com/danielmiessler/fabric/plugins/tools/youtube" "github.com/pkg/errors" "os" @@ -49,6 +50,7 @@ func NewFabricBase(db *fs.Db) (ret *Fabric) { VendorsAll: NewVendorsManager(), PatternsLoader: NewPatternsLoader(db.Patterns), YouTube: youtube.NewYouTube(), + Language: lang.NewLanguage(), Jina: jina.NewClient(), } @@ -75,6 +77,7 @@ type Fabric struct { VendorsAll *VendorsManager *PatternsLoader *youtube.YouTube + *lang.Language Jina *jina.Client Db *fs.Db @@ -101,6 +104,7 @@ func (o *Fabric) SaveEnvFile() (err error) { o.YouTube.SetupFillEnvFileContent(&envFileContent) o.Jina.SetupFillEnvFileContent(&envFileContent) + o.Language.SetupFillEnvFileContent(&envFileContent) err = o.Db.SaveEnv(envFileContent.String()) return @@ -125,6 +129,10 @@ func (o *Fabric) Setup() (err error) { return } + if err = o.Language.SetupOrSkip(); err != nil { + return + } + err = o.SaveEnvFile() return @@ -179,7 +187,7 @@ func (o *Fabric) SetupVendors() (err error) { } func (o *Fabric) SetupVendor(vendorName string) (err error) { - if err = o.VendorsAll.SetupVendor(vendorName); err != nil { + if err = o.VendorsAll.SetupVendor(vendorName, o.Vendors); err != nil { return } err = o.SaveEnvFile() @@ -200,6 +208,7 @@ func (o *Fabric) configure() (err error) { //YouTube and Jina are not mandatory, so ignore not configured error _ = o.YouTube.Configure() _ = o.Jina.Configure() + _ = o.Language.Configure() return } diff --git a/core/vendors.go b/core/vendors.go index 60f5e5b3..68c24dc1 100644 --- a/core/vendors.go +++ b/core/vendors.go @@ -89,26 +89,29 @@ func (o *VendorsManager) Setup() (ret map[string]ai.Vendor, err error) { ret = map[string]ai.Vendor{} for _, vendor := range o.Vendors { fmt.Println() - if vendorErr := vendor.Setup(); vendorErr == nil { - fmt.Printf("[%v] configured\n", vendor.GetName()) - ret[vendor.GetName()] = vendor - } else { - fmt.Printf("[%v] skipped\n", vendor.GetName()) - } + o.setupVendorTo(vendor, ret) } return } -func (o *VendorsManager) SetupVendor(vendorName string) (err error) { +func (o *VendorsManager) setupVendorTo(vendor ai.Vendor, configuredVendors map[string]ai.Vendor) { + if vendorErr := vendor.Setup(); vendorErr == nil { + fmt.Printf("[%v] configured\n", vendor.GetName()) + configuredVendors[vendor.GetName()] = vendor + } else { + delete(configuredVendors, vendor.GetName()) + fmt.Printf("[%v] skipped\n", vendor.GetName()) + } + return +} + +func (o *VendorsManager) SetupVendor(vendorName string, configuredVendors map[string]ai.Vendor) (err error) { vendor := o.FindByName(vendorName) if vendor == nil { err = fmt.Errorf("vendor %s not found", vendorName) return } - err = vendor.Setup() - if err != nil { - return - } + o.setupVendorTo(vendor, configuredVendors) return } diff --git a/plugins/tools/lang/language.go b/plugins/tools/lang/language.go new file mode 100644 index 00000000..48d76e8e --- /dev/null +++ b/plugins/tools/lang/language.go @@ -0,0 +1,41 @@ +package lang + +import ( + "github.com/danielmiessler/fabric/common" + "golang.org/x/text/language" +) + +func NewLanguage() (ret *Language) { + + label := "Language" + ret = &Language{} + + ret.Configurable = &common.Configurable{ + Label: label, + EnvNamePrefix: common.BuildEnvVariablePrefix(label), + ConfigureCustom: ret.configure, + } + + ret.DefaultLanguage = ret.Configurable.AddSetupQuestionCustom("Output", false, + "Enter your default want output lang (for example: zh_CN)") + + return +} + +type Language struct { + *common.Configurable + DefaultLanguage *common.SetupQuestion +} + +func (o *Language) configure() error { + if o.DefaultLanguage.Value != "" { + langTag, err := language.Parse(o.DefaultLanguage.Value) + if err == nil { + o.DefaultLanguage.Value = langTag.String() + } else { + o.DefaultLanguage.Value = "" + } + } + + return nil +}