diff --git a/core/chatter.go b/core/chatter.go index f85a0ebe1..9eff5dd11 100644 --- a/core/chatter.go +++ b/core/chatter.go @@ -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) diff --git a/plugins/db/fsdb/patterns.go b/plugins/db/fsdb/patterns.go index 0daa18525..475020311 100644 --- a/plugins/db/fsdb/patterns.go +++ b/plugins/db/fsdb/patterns.go @@ -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 +} \ No newline at end of file