Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add file-based pattern support #1142

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 26 additions & 9 deletions core/chatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,19 +98,36 @@ func (o *Chatter) BuildSession(request *common.ChatRequest, raw bool) (session *
contextContent = ctx.Content
}



var patternContent string
var pattern *fsdb.Pattern
if request.PatternName != "" {
var pattern *fsdb.Pattern
if pattern, err = o.db.Patterns.GetApplyVariables(request.PatternName, request.PatternVariables); err != nil {
err = fmt.Errorf("could not find pattern %s: %v", request.PatternName, err)
return
}

if pattern.Pattern != "" {
patternContent = pattern.Pattern
}
pathStr := request.PatternName
// First determine if this looks like a file path at all
isFilePath := strings.HasPrefix(pathStr, "\\") ||
strings.HasPrefix(pathStr, "/") ||
strings.HasPrefix(pathStr, "~") ||
strings.HasPrefix(pathStr, ".")
if isFilePath {
// Use the new file-based pattern method
if pattern, err = o.db.Patterns.GetFromFile(pathStr, request.PatternVariables); err != nil {
err = fmt.Errorf("could not read pattern file %s: %v", pathStr, err)
return
}
} else {
// Existing database lookup
if pattern, err = o.db.Patterns.GetApplyVariables(request.PatternName, request.PatternVariables); err != nil {
err = fmt.Errorf("could not find pattern %s: %v", request.PatternName, err)
return
}
}
if pattern.Pattern != "" {
patternContent = pattern.Pattern
}
}


systemMessage := strings.TrimSpace(contextContent) + strings.TrimSpace(patternContent)
if request.Language != "" {
systemMessage = fmt.Sprintf("%s. Please use the language '%s' for the output.", systemMessage, request.Language)
Expand Down
31 changes: 31 additions & 0 deletions plugins/db/fsdb/patterns.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,34 @@ type Pattern struct {
Description string
Pattern string
}

// GetFromFile reads a pattern from a file path and applies variables if provided
// this provides an ad-hoc way to use a pattern
func (o *PatternsEntity) GetFromFile(pathStr string, variables map[string]string) (ret *Pattern, err error) {
// Handle home directory expansion
if strings.HasPrefix(pathStr, "~/") {
var homedir string
if homedir, err = os.UserHomeDir(); err != nil {
return nil, fmt.Errorf("could not get home directory: %v", err)
}
pathStr = filepath.Join(homedir, pathStr[2:])
}


var content []byte
if content, err = os.ReadFile(pathStr); err != nil {
return nil, fmt.Errorf("could not read pattern file %s: %v", pathStr, err)
}

ret = &Pattern{
Name: pathStr,
Pattern: string(content),
}

if variables != nil && len(variables) > 0 {
for variableName, value := range variables {
ret.Pattern = strings.ReplaceAll(ret.Pattern, variableName, value)
}
}
return
}
Loading