fix: do template substition after parsing prompt YAML

This commit is contained in:
David Sanders
2025-10-20 21:32:06 -07:00
parent 83bb5ca3e8
commit af1c1c29a3
3 changed files with 18 additions and 8 deletions
Generated Vendored
+8 -3
View File
@@ -52572,10 +52572,8 @@ function loadPromptFile(filePath, templateVariables = {}) {
throw new Error(`Prompt file not found: ${filePath}`);
}
const fileContent = fs.readFileSync(filePath, 'utf-8');
// Apply template variable substitution
const processedContent = replaceTemplateVariables(fileContent, templateVariables);
try {
const config = load(processedContent);
const config = load(fileContent);
if (!config.messages || !Array.isArray(config.messages)) {
throw new Error('Prompt file must contain a "messages" array');
}
@@ -52588,6 +52586,13 @@ function loadPromptFile(filePath, templateVariables = {}) {
throw new Error(`Invalid message role: ${message.role}`);
}
}
// Prepare messages by replacing template variables with actual content
config.messages = config.messages.map(msg => {
return {
...msg,
content: replaceTemplateVariables(msg.content, templateVariables),
};
});
return config;
}
catch (error) {
Generated Vendored
+1 -1
View File
File diff suppressed because one or more lines are too long
+9 -4
View File
@@ -101,11 +101,8 @@ export function loadPromptFile(filePath: string, templateVariables: TemplateVari
const fileContent = fs.readFileSync(filePath, 'utf-8')
// Apply template variable substitution
const processedContent = replaceTemplateVariables(fileContent, templateVariables)
try {
const config = yaml.load(processedContent) as PromptConfig
const config = yaml.load(fileContent) as PromptConfig
if (!config.messages || !Array.isArray(config.messages)) {
throw new Error('Prompt file must contain a "messages" array')
@@ -121,6 +118,14 @@ export function loadPromptFile(filePath: string, templateVariables: TemplateVari
}
}
// Prepare messages by replacing template variables with actual content
config.messages = config.messages.map(msg => {
return {
...msg,
content: replaceTemplateVariables(msg.content, templateVariables),
}
})
return config
} catch (error) {
throw new Error(`Failed to parse prompt file: ${error instanceof Error ? error.message : 'Unknown error'}`)