What Templating System Generates Structured AI Responses in Lemon AI?
Lemon AI implements a lightweight, custom templating layer in src/utils/template.js that uses curly-brace placeholder syntax rather than external engines like Jinja, Handlebars, or Mustache.
The Lemon AI templating system provides a fast, zero-dependency solution for generating dynamic prompts and structured AI payloads. By leveraging simple string replacement with {variable} syntax, the system enables rapid template rendering across knowledge feedback, coding assistance, and agent planning modules.
How the Custom Templating System Works
The core implementation resides in src/utils/template.js and operates through three primary mechanisms: variable extraction, string replacement, and file-based template loading.
Variable Extraction with extractTemplateVariables
The system identifies dynamic placeholders using a regular expression that scans for curly-brace-wrapped keys. The extractTemplateVariables function parses template strings to determine which data keys must be supplied at runtime.
// src/utils/template.js (simplified)
const extractTemplateVariables = (template) => {
const regex = /\{([^}]+)\}/g;
const vars = new Set();
let match;
while ((match = regex.exec(template))) vars.add(match[1]);
return [...vars];
};
Template Rendering with resolveTemplate
The resolveTemplate function executes the actual substitution, replacing each {placeholder} with corresponding values from a data object. If a key is missing, the original placeholder text is preserved.
const resolveTemplate = async (template, data = {}) => {
return template.replace(/\{([^}]+)\}/g, (_, key) =>
key in data ? data[key] : `{${key}}`
);
};
File-Based Template Storage
Templates are stored as plain text files under src/template/ and loaded via the loadTemplate function. This separation of concerns allows non-developers to modify prompt structures without touching application logic.
Implementation Examples Across Lemon AI
The templating system is utilized throughout the codebase to construct context-aware AI prompts for different functional domains.
Knowledge Feedback Generation
In src/knowledge/feedback.js, the system loads knowledge.txt templates to generate contextual feedback prompts. The template receives dynamic values such as user queries and domain context to produce targeted AI responses.
const { resolveTemplate, loadTemplate } = require("@src/utils/template");
const tmpl = await loadTemplate("knowledge.txt");
const filled = await resolveTemplate(tmpl, {
user_query: "How does photosynthesis work?",
context: "Botanical basics",
});
Coding Assistance Prompts
The src/editor/coding.js module leverages the templating system to construct coding assistance prompts. It reads local template files and injects dynamic code context including full source code, selected snippets, and specific requirements.
const { resolveTemplate } = require("@src/utils/template");
const fs = require("fs");
const path = require("path");
const templatePath = path.join(__dirname, "template.txt");
const template = fs.readFileSync(templatePath, "utf-8");
const opts = { full_code, selection, requirement };
const prompt = await resolveTemplate(template, opts);
Agent Planning Prompts
In src/agent/prompt/plan.js, the templating system generates planning prompts for the AI agent. This enables dynamic construction of multi-step reasoning prompts based on current task context and available tools.
Summary
- Lemon AI uses a custom templating system located in
src/utils/template.jsrather than external libraries like Jinja or Handlebars. - Curly-brace syntax (
{variable}) defines placeholders that are replaced at runtime via regex-based string substitution. - Three core functions power the system:
extractTemplateVariablesfor parsing,resolveTemplatefor rendering, andloadTemplatefor file operations. - File-based templates stored in
src/template/enable separation of prompt content from application logic. - Zero-dependency implementation ensures fast performance and reduced supply chain risk across knowledge, coding, and agent modules.
Frequently Asked Questions
Does Lemon AI use Jinja2 or Handlebars for templating?
No, Lemon AI does not use Jinja2, Handlebars, Mustache, or any external templating engine. According to the source code in src/utils/template.js, Lemon AI implements a lightweight, custom templating layer that uses simple regex-based string replacement for curly-brace placeholders.
How does variable substitution work in Lemon AI templates?
Variable substitution occurs through the resolveTemplate function, which uses the regular expression /\{([^}]+)\}/g to find all {variable} patterns in the template string. It then replaces each match with the corresponding value from the provided data object. If a variable is not found in the data object, the original placeholder text is preserved.
Where are template files stored in the Lemon AI repository?
Template files are stored as plain text files under the src/template/ directory. The loadTemplate function in src/utils/template.js handles reading these files from disk, after which the loaded string is processed by resolveTemplate to substitute dynamic values before being sent to the AI model.
Can I use conditionals or loops in Lemon AI templates?
No, the custom templating system in Lemon AI does not support advanced features like conditionals, loops, or filters. It performs only simple variable substitution using curly-brace syntax. For complex logic, the codebase handles transformations in JavaScript before passing the final data object to resolveTemplate.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →