feat: add support for pattern variables

This commit is contained in:
Eugen Eisler 2024-09-03 15:13:03 +02:00
parent a921b77f5a
commit d6552f5811
4 changed files with 48 additions and 36 deletions

View File

@ -14,6 +14,7 @@ import (
// Flags create flags struct. the users flags go into this, this will be passed to the chat struct in cli // Flags create flags struct. the users flags go into this, this will be passed to the chat struct in cli
type Flags struct { type Flags struct {
Pattern string `short:"p" long:"pattern" description:"Choose a pattern" default:""` Pattern string `short:"p" long:"pattern" description:"Choose a pattern" default:""`
PatternVariables map[string]string `short:"v" long:"variable" description:"Values for pattern variables"`
Context string `short:"C" long:"context" description:"Choose a context" default:""` Context string `short:"C" long:"context" description:"Choose a context" default:""`
Session string `long:"session" description:"Choose a session"` Session string `long:"session" description:"Choose a session"`
Setup bool `short:"S" long:"setup" description:"Run setup"` Setup bool `short:"S" long:"setup" description:"Run setup"`
@ -101,6 +102,7 @@ func (o *Flags) BuildChatRequest() (ret *common.ChatRequest) {
ContextName: o.Context, ContextName: o.Context,
SessionName: o.Session, SessionName: o.Session,
PatternName: o.Pattern, PatternName: o.Pattern,
PatternVariables: o.PatternVariables,
Message: o.Message, Message: o.Message,
} }
return return

View File

@ -9,6 +9,7 @@ type ChatRequest struct {
ContextName string ContextName string
SessionName string SessionName string
PatternName string PatternName string
PatternVariables map[string]string
Message string Message string
} }

View File

@ -82,7 +82,7 @@ func (o *Chatter) NewChat(request *common.ChatRequest) (ret *Chat, err error) {
if request.PatternName != "" { if request.PatternName != "" {
var pattern *db.Pattern var pattern *db.Pattern
if pattern, err = o.db.Patterns.GetPattern(request.PatternName); err != nil { if pattern, err = o.db.Patterns.GetPattern(request.PatternName, request.PatternVariables); err != nil {
err = fmt.Errorf("could not find pattern %s: %v", request.PatternName, err) err = fmt.Errorf("could not find pattern %s: %v", request.PatternName, err)
return return
} }

View File

@ -14,16 +14,25 @@ type Patterns struct {
} }
// GetPattern finds a pattern by name and returns the pattern as an entry or an error // GetPattern finds a pattern by name and returns the pattern as an entry or an error
func (o *Patterns) GetPattern(name string) (ret *Pattern, err error) { func (o *Patterns) GetPattern(name string, variables map[string]string) (ret *Pattern, err error) {
patternPath := filepath.Join(o.Dir, name, o.SystemPatternFile) patternPath := filepath.Join(o.Dir, name, o.SystemPatternFile)
var pattern []byte var pattern []byte
if pattern, err = os.ReadFile(patternPath); err != nil { if pattern, err = os.ReadFile(patternPath); err != nil {
return return
} }
patternStr := string(pattern)
if variables != nil && len(variables) > 0 {
for variableName, value := range variables {
patternStr = strings.ReplaceAll(patternStr, variableName, value)
}
}
ret = &Pattern{ ret = &Pattern{
Name: name, Name: name,
Pattern: string(pattern), Pattern: patternStr,
} }
return return
} }