Skip to content

feat(prompts): support Sprig in templates - #337

Merged
mudler merged 1 commit into
mainfrom
feat/sprig-templates
Oct 31, 2025
Merged

mudler merged 1 commit into
mainfrom
feat/sprig-templates

Conversation

@mudler

@mudler mudler commented Oct 31, 2025

Copy link
Copy Markdown
Owner

This PR adds support to Sprig into prompt templates

Copilot AI review requested due to automatic review settings October 31, 2025 20:55

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR enhances the template system by integrating the Sprig template function library and refactoring template handling. The main goal is to add support for Sprig template functions across the agent's prompt processing system.

  • Switched from html/template to text/template package
  • Added Sprig v3 template function support via new helper functions
  • Applied template processing to message content and system prompts

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
core/agent/templates.go Added templateBase and templateExecute helper functions with Sprig support, updated import from html/template to text/template, refactored renderTemplate to use new helpers
core/agent/agent.go Applied template processing to message content and system prompts using the new template helpers

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread core/agent/agent.go Outdated
Comment on lines +375 to +379
content, err := templateExecute(promptTemplate, struct{}{})
if err != nil {
xlog.Error("Error executing template", "error", err)
}
if !conversation.Exist(content) {

Copilot AI Oct 31, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When template execution fails (line 375-378), the error is logged but content remains empty. The code then proceeds to check conversation.Exist(content) with an empty string on line 379, which may cause unintended behavior. Consider returning early or handling the error to prevent using an empty content value.

Copilot uses AI. Check for mistakes.
Comment thread core/agent/agent.go Outdated
Comment on lines +314 to +323
promptTemplate, err := templateBase("template", content)
if err != nil {
xlog.Error("Error rendering template", "error", err)
continue
}

content, err = templateExecute(promptTemplate, struct{}{})
if err != nil {
xlog.Error("Error executing template", "error", err)
continue

Copilot AI Oct 31, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Template processing is applied to all message content regardless of whether it contains template directives. Consider checking if the content contains template markers (e.g., '{{') before creating and executing templates to avoid unnecessary parsing overhead for non-templated content.

Suggested change
promptTemplate, err := templateBase("template", content)
if err != nil {
xlog.Error("Error rendering template", "error", err)
continue
}
content, err = templateExecute(promptTemplate, struct{}{})
if err != nil {
xlog.Error("Error executing template", "error", err)
continue
if strings.Contains(content, "{{") {
promptTemplate, err := templateBase("template", content)
if err != nil {
xlog.Error("Error rendering template", "error", err)
continue
}
content, err = templateExecute(promptTemplate, struct{}{})
if err != nil {
xlog.Error("Error executing template", "error", err)
continue
}

Copilot uses AI. Check for mistakes.
Comment thread core/agent/agent.go Outdated
continue
}

content, err = templateExecute(promptTemplate, struct{}{})

Copilot AI Oct 31, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An empty struct struct{}{} is passed as template data, which means templates cannot access any dynamic values. Consider defining a proper data structure with relevant context (e.g., agent state, character info, timestamps) to make the template functionality more useful.

Copilot uses AI. Check for mistakes.
Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
@mudler
mudler force-pushed the feat/sprig-templates branch from 22affa1 to e1516dd Compare October 31, 2025 21:44
@mudler
mudler merged commit 02eda4e into main Oct 31, 2025
2 checks passed
@mudler
mudler deleted the feat/sprig-templates branch October 31, 2025 21:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants