-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathproviders.go
More file actions
147 lines (128 loc) · 4.09 KB
/
Copy pathproviders.go
File metadata and controls
147 lines (128 loc) · 4.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package unstruct
import (
"fmt"
"io/fs"
"path/filepath"
"strings"
"github.com/tyler-sommer/stick"
)
// → StickPromptProvider is fs-agnostic
type StickPromptProvider struct {
env *stick.Env
templates map[string]string
vars map[string]interface{} // Template variables
}
// → Option pattern keeps the constructor flexible
type Option func(*StickPromptProvider) error
// WithFS loads every *.twig* file found under dir in the supplied FS.
func WithFS[F fs.FS](fsys F, dir string) Option {
return func(p *StickPromptProvider) error {
return fs.WalkDir(fsys, dir, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() || !strings.HasSuffix(path, ".twig") {
return nil
}
content, readErr := fs.ReadFile(fsys, path)
if readErr != nil {
return fmt.Errorf("read %s: %w", path, readErr)
}
tag := strings.TrimSuffix(filepath.Base(path), ".twig")
p.templates[tag] = string(content)
return nil
})
}
}
// WithTemplates lets you inject an in-memory map.
func WithTemplates(m map[string]string) Option {
return func(p *StickPromptProvider) error {
for k, v := range m {
p.templates[k] = v
}
return nil
}
}
// WithVar adds a variable that will be available in all templates
func WithVar(key string, value interface{}) Option {
return func(p *StickPromptProvider) error {
if p.vars == nil {
p.vars = make(map[string]interface{})
}
p.vars[key] = value
return nil
}
}
// NewStickPromptProvider builds a provider from any combination of options.
func NewStickPromptProvider(opts ...Option) (*StickPromptProvider, error) {
p := &StickPromptProvider{
env: stick.New(nil),
templates: make(map[string]string),
vars: make(map[string]interface{}),
}
for _, opt := range opts {
if err := opt(p); err != nil {
return nil, err
}
}
return p, nil
}
// AddTemplate updates or inserts one template.
func (p *StickPromptProvider) AddTemplate(tag, tpl string) { p.templates[tag] = tpl }
// GetPrompt renders the template for the given tag.
func (p *StickPromptProvider) GetPrompt(tag string, version int) (string, error) {
tpl, ok := p.templates[tag]
if !ok {
return "", fmt.Errorf("template %q not found", tag)
}
// Prepare template context with default variables plus custom ones
templateCtx := make(map[string]stick.Value)
templateCtx["version"] = version
templateCtx["tag"] = tag
templateCtx["Version"] = version // Capitalized version for consistency
templateCtx["Tag"] = tag // Capitalized version for consistency
// Add custom variables
for k, v := range p.vars {
templateCtx[k] = v
}
var out strings.Builder
if err := p.env.Execute(tpl, &out, templateCtx); err != nil {
return "", fmt.Errorf("execute %q: %w", tag, err)
}
return out.String(), nil
}
// GetPromptWithContext renders the template with additional context variables.
func (p *StickPromptProvider) GetPromptWithContext(tag string, version int, keys []string, document string) (string, error) {
tpl, ok := p.templates[tag]
if !ok {
return "", fmt.Errorf("template %q not found", tag)
}
// Prepare template context with default variables plus custom ones
templateCtx := make(map[string]stick.Value)
templateCtx["version"] = version
templateCtx["tag"] = tag
templateCtx["Version"] = version // Capitalized version for consistency
templateCtx["Tag"] = tag // Capitalized version for consistency
templateCtx["keys"] = keys
templateCtx["Keys"] = keys
templateCtx["KeyList"] = strings.Join(keys, ", ") // Comma-separated string of keys
templateCtx["document"] = document
templateCtx["Document"] = document
// Add custom variables
for k, v := range p.vars {
templateCtx[k] = v
}
var out strings.Builder
if err := p.env.Execute(tpl, &out, templateCtx); err != nil {
return "", fmt.Errorf("execute %q: %w", tag, err)
}
return out.String(), nil
}
// → SimplePromptProvider stays untouched
type SimplePromptProvider map[string]string
func (s SimplePromptProvider) GetPrompt(tag string, version int) (string, error) {
if tpl, ok := s[tag]; ok {
return tpl, nil
}
return "", fmt.Errorf("prompt %q not found", tag)
}