41
apps/web/src/lib/ai/prompts/render.ts
Executable file
41
apps/web/src/lib/ai/prompts/render.ts
Executable file
@@ -0,0 +1,41 @@
|
||||
const PLACEHOLDER_RE = /\{\{(\w+)\}\}/g;
|
||||
|
||||
export function renderTemplate(
|
||||
body: string,
|
||||
data: Record<string, string | number | unknown>
|
||||
): string {
|
||||
return body.replace(PLACEHOLDER_RE, (_, key: string) => {
|
||||
const val = data[key];
|
||||
if (val === undefined || val === null) return `{{${key}}}`;
|
||||
if (typeof val === "object") return JSON.stringify(val);
|
||||
return String(val);
|
||||
});
|
||||
}
|
||||
|
||||
export function findMissingPlaceholders(
|
||||
body: string,
|
||||
data: Record<string, unknown>
|
||||
): string[] {
|
||||
const missing: string[] = [];
|
||||
let match;
|
||||
const re = /\{\{(\w+)\}\}/g;
|
||||
while ((match = re.exec(body)) !== null) {
|
||||
const key = match[1];
|
||||
if (data[key] === undefined && !missing.includes(key)) {
|
||||
missing.push(key);
|
||||
}
|
||||
}
|
||||
return missing;
|
||||
}
|
||||
|
||||
export const SAMPLE_PREVIEW_DATA: Record<string, unknown> = {
|
||||
user_name: "Traveler",
|
||||
context: { level: 5, recentReading: 42, exerciseDays: 3 },
|
||||
topic: "Stoic philosophy",
|
||||
week_summary: "You read 35 pages, prayed 4 days, and exercised twice.",
|
||||
reading_progress: "Currently reading page 120 of 300",
|
||||
exercise_summary: "2 exercise sessions this week",
|
||||
prayer_summary: "4 of 5 morning prayers completed",
|
||||
learning_topics: "Roman history, Latin roots",
|
||||
chronicle_context: "Chapter 3: The Long Road — 45 days on the journey",
|
||||
};
|
||||
Reference in New Issue
Block a user