Crestapps core templates

From CrestApps/CrestApps.AgentSkills · View interactive version

Crestapps core templates is an AI agent skill from the GitHub repository CrestApps/CrestApps.AgentSkills. Skill for the general-purpose Liquid template engine in CrestApps.Core.Templates. Covers AddTemplating and TemplateOptions (code templates, discovery paths), AddTemplatesFromAssembly for embedded templates, the Templates and Templates/Prompts folder conventions and the Kind front matter, Markdown fr

How to use this skill

Skill file

CrestApps.Core Templates

You are a CrestApps.Core expert. Generate code for CrestApps.Core.Templates discovery, Liquid rendering and composition.

Guidelines

builder.Services.Configure<Fluid.TemplateOptionsBuilder>(options => options
    .ConfigureFilters(filters => filters.AddFilter("shout", (input, arguments, context) =>
        Fluid.Values.StringValue.Create(input.ToStringValue(context).ToUpperInvariant()))));

Use ConfigureMemberAccess(strategy => ...) only for custom member accessors.

Registration

using CrestApps.Core.Templates.Extensions;

builder.Services
    .AddTemplating(options => options
        .AddDiscoveryPath(builder.Environment.ContentRootPath)
        .AddTemplate(
            "welcome",
            "Hello {{ customer }}!",
            metadata => metadata.Title = "Welcome"))
    .AddTemplatesFromAssembly(typeof(Program).Assembly, source: "MyApp");

| Source | Template id | Kind |

|---|---|---|

| AddTemplate(id, content, ...) | the given id | set Template.Kind yourself |

| {discovery path}/Templates/*.md (not recursive) | file name without extension | from Kind front matter |

| {discovery path}/Templates/Prompts/*.md and one level of subfolders | file name; a subfolder name becomes FeatureId | SystemPrompt unless front matter sets it |

| Embedded resources under Templates/ via AddTemplatesFromAssembly(...) | path after Templates.; a Prompts. prefix is dropped | SystemPrompt for Prompts/, otherwise front matter |

Mark the Markdown files as EmbeddedResource in the .csproj for AddTemplatesFromAssembly(...).

Template File Format

---
Title: Order Follow-up
Description: Asks a customer to confirm a delivery window.
Category: Support
IsListable: true
Kind: SystemPrompt
Parameters:
  - customer_name: The customer's display name
  - order_number: The order being discussed
---
You are writing to {{ customer_name | default: "the customer" }} about order {{ order_number }}.
{% if delivery_window %}
Offer the delivery window {{ delivery_window }}.
{% endif %}

Title, Description, Category, IsListable (default true) and Parameters map to TemplateMetadata; Kind maps to Template.Kind; any other key lands in TemplateMetadata.AdditionalProperties. A missing Title is derived from the id.

Render and Compose

using CrestApps.Core.Templates;
using CrestApps.Core.Templates.Services;

namespace MyApp.Messaging;

public sealed class FollowUpMessageService
{
    private readonly ITemplateService _templates;

    public FollowUpMessageService(ITemplateService templates)
    {
        _templates = templates;
    }

    public Task<string> RenderAsync(string customer, string order, CancellationToken cancellationToken = default)
    {
        return new TemplateBuilder()
            .WithSeparator("\n\n")
            .Append("Keep the reply under 120 words.")
            .AppendTemplate("order-follow-up", new Dictionary<string, object>
            {
                ["customer_name"] = customer,
                ["order_number"] = order,
            })
            .BuildAsync(_templates, cancellationToken);
    }
}

Store Templates in a Database

A provider whose templates change at runtime implements IDynamicTemplateProvider (a marker over ITemplateProvider). DefaultTemplateService reads static providers first, so a stored template never replaces a shipped template with the same id. Use Aliases to keep old identifiers resolvable; GetAsync matches an id first and an alias second.

using CrestApps.Core.Services;
using CrestApps.Core.Templates.Models;
using CrestApps.Core.Templates.Parsing;
using CrestApps.Core.Templates.Providers;

namespace MyApp.Templates;

public sealed class StoredTemplateProvider : IDynamicTemplateProvider
{
    private readonly ICatalog<StoredTemplate> _catalog;
    private readonly ITemplateParser _parser;

    public StoredTemplateProvider(ICatalog<StoredTemplate> catalog, IEnumerable<ITemplateParser> parsers)
    {
        _catalog = catalog;
        _parser = parsers.First(parser => parser.SupportedExtensions.Contains(".md"));
    }

    public async Task<IReadOnlyList<Template>> GetTemplatesAsync(CancellationToken cancellationToken = default)
    {
        var records = await _catalog.GetAllAsync(cancellationToken);

        return records
            .Select(record =>
            {
                var parsed = _parser.Parse(record.Markdown);

                return new Template
                {
                    Id = record.Name,
                    Aliases = [$"stored:{record.ItemId}"],
                    Metadata = parsed.Metadata,
                    Content = parsed.Body,
                    Source = "Database",
                };
            })
            .ToArray();
    }
}
builder.Services.AddScoped<ITemplateProvider, StoredTemplateProvider>();

StoredTemplate is any CatalogItem with Name and Markdown properties registered through a store package. Register dynamic providers as scoped, since ITemplateService is scoped and reads them in every scope. A custom template service that caches for longer than a scope should override GetStaticTemplatesAsync and cache only what it returns.

Upgrading Within 2.0 Previews

Upgrading from 1.x

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.