Files
promptkit/internal/prompt/template.go

44 lines
1.4 KiB
Go

package prompt
import "text/template"
// CompiledTemplates is the immutable parsed template set for one captured
// prompt. Callers must not mutate its templates after handing it to a renderer.
type CompiledTemplates struct {
Session *template.Template
Messages []*template.Template
}
// InputMetadata is the captured declaration metadata for one prompt input.
// Nil metadata pointers mean the corresponding field was absent or null.
type InputMetadata struct {
Name string
Required bool
ContentType *string
Description *string
}
// RequirementSnapshot provides the captured syntax and declarations needed
// for requirement inspection without reopening prompt resources.
type RequirementSnapshot struct {
Templates CompiledTemplates
Inputs []InputMetadata
}
// TemplateLookup provides parsed templates for a captured prompt identity.
type TemplateLookup interface {
GetCompiledTemplates(id string) (CompiledTemplates, bool)
}
// CompileTemplate checks static syntax with the same grammar and function
// names as rendering, without evaluating request inputs or variables.
func CompileTemplate(name, raw string) (*template.Template, error) {
return parseTemplate(name, raw, template.FuncMap{
"input": func(string) (string, error) { return "", nil },
})
}
func parseTemplate(name, raw string, funcs template.FuncMap) (*template.Template, error) {
return template.New(name).Funcs(funcs).Option("missingkey=error").Parse(raw)
}