forked from YaoApp/yao
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpression.go
195 lines (159 loc) · 3.64 KB
/
expression.go
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package pipe
import (
"fmt"
"regexp"
"strings"
"github.com/expr-lang/expr"
"github.com/expr-lang/expr/vm"
"github.com/yaoapp/kun/log"
)
// If set the map value, should keep the space at the end of the statement
var stmtRe = regexp.MustCompile(`\{\{([\s\S]*?)\}\}`)
var options = []expr.Option{
expr.AllowUndefinedVariables(),
}
// New create a new expression
func (data Data) New(stmt string) (*vm.Program, error) {
stmt = stmtRe.ReplaceAllStringFunc(stmt, func(stmt string) string {
matches := stmtRe.FindStringSubmatch(stmt)
if len(matches) > 0 {
stmt = strings.ReplaceAll(stmt, matches[0], matches[1])
}
return stmt
})
stmt = strings.TrimSpace(stmt)
// ' => ' " => "
stmt = strings.ReplaceAll(stmt, "'", "'")
stmt = strings.ReplaceAll(stmt, """, "\"")
return expr.Compile(stmt, append([]expr.Option{expr.Env(data)}, options...)...)
}
// Exec exec statement for the template
func (data Data) Exec(stmt string) (interface{}, error) {
program, err := data.New(stmt)
if err != nil {
log.Warn("pipe: %s %s", stmt, err)
return nil, nil
}
v, err := expr.Run(program, data)
if err != nil {
log.Warn("pipe: %s %s", stmt, err)
return nil, nil
}
return v, nil
}
// ExecString exec statement for the template
func (data Data) ExecString(stmt string) (string, error) {
res, err := data.Exec(stmt)
if err != nil {
return "", nil
}
if res == nil {
return "", nil
}
if v, ok := res.(string); ok {
return v, nil
}
return fmt.Sprintf("%v", res), nil
}
// IsExpression check if the statement is an expression
func IsExpression(stmt string) bool {
return stmtRe.MatchString(stmt)
}
func (data Data) replace(value any) (any, error) {
switch v := value.(type) {
case string:
return data.replaceAny(v)
case []any:
return data.replaceArray(v)
case map[string]any:
return data.replaceMap(v)
case Input:
return data.replaceArray(v)
}
return value, nil
}
func (data Data) replacePrompts(prompts []Prompt) ([]Prompt, error) {
newPrompts := []Prompt{}
for _, prompt := range prompts {
content, err := data.replaceString(prompt.Content)
if err != nil {
return nil, err
}
role, err := data.replaceString(prompt.Role)
if err != nil {
return nil, err
}
prompt.Role = role
prompt.Content = content
newPrompts = append(newPrompts, prompt)
}
return newPrompts, nil
}
func (data Data) replaceAny(value string) (any, error) {
if !IsExpression(value) {
return value, nil
}
v, err := data.Exec(value)
if err != nil {
return "", err
}
return v, nil
}
// replaceString replace the string
func (data Data) replaceString(value string) (string, error) {
if !IsExpression(value) {
return value, nil
}
v, err := data.ExecString(value)
if err != nil {
return "", err
}
return v, nil
}
func (data Data) replaceMap(value map[string]any) (map[string]any, error) {
newValue := map[string]any{}
if value == nil {
return newValue, nil
}
for k, v := range value {
res, err := data.replace(v)
if err != nil {
return nil, err
}
newValue[k] = res
}
return newValue, nil
}
func (data Data) replaceArray(value []any) ([]any, error) {
newValue := []any{}
if value == nil {
return newValue, nil
}
for _, v := range value {
res, err := data.replace(v)
if err != nil {
return nil, err
}
newValue = append(newValue, res)
}
return newValue, nil
}
func (data Data) replaceInput(value Input) (Input, error) {
return data.replaceArray(value)
}
func anyToInput(v any) Input {
switch v := v.(type) {
case Input:
return v
case []any:
return v
case []string:
input := Input{}
for _, s := range v {
input = append(input, s)
}
return input
default:
return Input{v}
}
}