-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.go
85 lines (69 loc) · 1.77 KB
/
template.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
package llmflow
import (
"bytes"
"context"
"encoding/json"
"fmt"
"text/template"
"github.com/RussellLuo/orchestrator"
)
const TypeTemplate = "template"
func init() {
MustRegisterTemplate(orchestrator.GlobalRegistry)
}
func MustRegisterTemplate(r *orchestrator.Registry) {
r.MustRegister(&orchestrator.TaskFactory{
Type: TypeTemplate,
New: func() orchestrator.Task { return new(Template) },
})
}
// Template is a leaf task that is used to render a template by applying given arguments.
type Template struct {
orchestrator.TaskHeader
Input struct {
Template string `json:"template"`
Args orchestrator.Expr[map[string]any] `json:"args"`
} `json:"input"`
}
func (t *Template) String() string {
return fmt.Sprintf("%s(name:%s)", t.Type, t.Name)
}
func (t *Template) Execute(ctx context.Context, input orchestrator.Input) (output orchestrator.Output, err error) {
if err := t.Input.Args.Evaluate(input); err != nil {
return nil, err
}
tmpl, err := template.New("").Funcs(map[string]any{
"jsonUnmarshal": func(data string) (any, error) {
var v any
if err := json.Unmarshal([]byte(data), &v); err != nil {
return nil, err
}
return v, nil
},
}).Parse(t.Input.Template)
if err != nil {
return nil, err
}
args := t.Input.Args.Value
var buf bytes.Buffer
if err := tmpl.Execute(&buf, args); err != nil {
return nil, err
}
result := buf.String()
return orchestrator.Output{
"result": result,
}, nil
}
type TemplateBuilder struct {
task *Template
}
func NewTemplate(name string) *TemplateBuilder {
task := &Template{
TaskHeader: orchestrator.TaskHeader{
Name: name,
Type: TypeTemplate,
},
}
return &TemplateBuilder{task: task}
}
func (b *TemplateBuilder) Build() orchestrator.Task { return b.task }