Render only captured prompt templates
This commit is contained in:
@@ -42,6 +42,8 @@ Ordinary preparation selects the full prompt definition and construction-time
|
||||
hash. It retains its early request-ID check before direct-session
|
||||
normalization. Referenced message content and static template syntax were
|
||||
captured when the engine was built.
|
||||
Rendering uses those captured parses, cloning them per call to bind request
|
||||
input helpers; it does not parse templates during execution.
|
||||
|
||||
`InspectPrompt` selects captured metadata directly from the repository. It
|
||||
returns owned metadata without
|
||||
|
||||
@@ -165,6 +165,10 @@ inspection tests. Renderer-related failures may be deferred through Stage 6.
|
||||
|
||||
## Stage 5: Keep Only Captured Rendering
|
||||
|
||||
**Completed:** Rendering now requires captured templates and no longer parses
|
||||
messages or sessions per call. Go 1.27.3 `go build ./...` passed; renderer and
|
||||
use-case test migrations and their checks are deferred to the next stage.
|
||||
|
||||
**Finding:** 3. **Files:** `internal/prompt/go_renderer.go`, template helpers, engine assembly.
|
||||
|
||||
1. Remove `NewGoRenderer` and the nil-template lookup mode. Keep
|
||||
|
||||
@@ -24,10 +24,6 @@ type goRenderer struct {
|
||||
templates TemplateLookup
|
||||
}
|
||||
|
||||
func NewGoRenderer() Renderer {
|
||||
return &goRenderer{}
|
||||
}
|
||||
|
||||
// NewCompiledRenderer renders definitions using templates captured with the
|
||||
// configuration snapshot. Per-call clones bind request-specific input helpers.
|
||||
func NewCompiledRenderer(templates TemplateLookup) Renderer {
|
||||
@@ -56,13 +52,12 @@ func (r *goRenderer) Render(ctx context.Context, definition *domain.PromptDefini
|
||||
funcs := template.FuncMap{
|
||||
"input": resolver.resolve,
|
||||
}
|
||||
var compiled CompiledTemplates
|
||||
if r.templates != nil {
|
||||
var found bool
|
||||
compiled, found = r.templates.GetCompiledTemplates(definition.ID)
|
||||
if !found || len(compiled.Messages) != len(definition.Templates) {
|
||||
return nil, fmt.Errorf("%w: captured templates are unavailable", ErrRenderFailure)
|
||||
}
|
||||
if r.templates == nil {
|
||||
return nil, fmt.Errorf("%w: captured templates are unavailable", ErrRenderFailure)
|
||||
}
|
||||
compiled, found := r.templates.GetCompiledTemplates(definition.ID)
|
||||
if !found || len(compiled.Messages) != len(definition.Templates) {
|
||||
return nil, fmt.Errorf("%w: captured templates are unavailable", ErrRenderFailure)
|
||||
}
|
||||
|
||||
if err := ctx.Err(); err != nil {
|
||||
@@ -70,15 +65,11 @@ func (r *goRenderer) Render(ctx context.Context, definition *domain.PromptDefini
|
||||
}
|
||||
var sessionID string
|
||||
var err error
|
||||
if r.templates != nil {
|
||||
if definition.SessionID != "" {
|
||||
if compiled.Session == nil {
|
||||
return nil, fmt.Errorf("%w: captured session template is unavailable", ErrRenderFailure)
|
||||
}
|
||||
sessionID, err = renderCompiledSessionID(ctx, compiled.Session, funcs, vars)
|
||||
if definition.SessionID != "" {
|
||||
if compiled.Session == nil {
|
||||
return nil, fmt.Errorf("%w: captured session template is unavailable", ErrRenderFailure)
|
||||
}
|
||||
} else {
|
||||
sessionID, err = renderSessionID(ctx, definition.SessionID, funcs, vars)
|
||||
sessionID, err = renderCompiledSessionID(ctx, compiled.Session, funcs, vars)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -102,18 +93,12 @@ func (r *goRenderer) Render(ctx context.Context, definition *domain.PromptDefini
|
||||
if err := ctx.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var tmpl *template.Template
|
||||
var parseErr error
|
||||
if r.templates != nil {
|
||||
if compiled.Messages[i] == nil {
|
||||
return nil, fmt.Errorf("%w: captured message template is unavailable", ErrRenderFailure)
|
||||
}
|
||||
tmpl, parseErr = compiled.Messages[i].Clone()
|
||||
if parseErr == nil {
|
||||
tmpl.Funcs(funcs)
|
||||
}
|
||||
} else {
|
||||
tmpl, parseErr = parseTemplate(fmt.Sprintf("msg_%d", i), tmplMsg.Content, funcs)
|
||||
if compiled.Messages[i] == nil {
|
||||
return nil, fmt.Errorf("%w: captured message template is unavailable", ErrRenderFailure)
|
||||
}
|
||||
tmpl, parseErr := compiled.Messages[i].Clone()
|
||||
if parseErr == nil {
|
||||
tmpl.Funcs(funcs)
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return nil, err
|
||||
@@ -201,20 +186,6 @@ func (r *artifactTextResolver) resolve(name string) (string, error) {
|
||||
return text, nil
|
||||
}
|
||||
|
||||
func renderSessionID(ctx context.Context, raw string, funcs template.FuncMap, vars map[string]string) (string, error) {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return "", err
|
||||
}
|
||||
tmpl, parseErr := parseTemplate("session_id", raw, funcs)
|
||||
if err := ctx.Err(); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if parseErr != nil {
|
||||
return "", fmt.Errorf("%w: session_id: %v", ErrInvalidTemplate, parseErr)
|
||||
}
|
||||
return executeSessionTemplate(ctx, tmpl, vars)
|
||||
}
|
||||
|
||||
func renderCompiledSessionID(ctx context.Context, compiled *template.Template, funcs template.FuncMap, vars map[string]string) (string, error) {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return "", err
|
||||
|
||||
Reference in New Issue
Block a user