Path: redb-route/redb.Route.Llm/Engine/Storage/IPromptTemplateRegistry.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route.Llm/Engine/Storage/IPromptTemplateRegistry.cs
----
using System.Collections.Concurrent;
using redb.Route.Abstractions;
namespace redb.Route.Llm.Engine.Storage;
///
/// Resolves a named prompt template into the actual system / user text the
/// engine should send to the provider. Versioning is required so that a
/// recorded eval run can be replayed against the exact template that produced it.
///
/// The optional exchange parameter on every method carries the route
/// pipeline's current exchange; REDB-backed implementations resolve a
/// per-exchange through
/// IRouteContext.GetRedbService(name, exchange). In-memory
/// implementations ignore it.
/// Without an exchange a redb-backed implementation opens a redb scope of its own for the call. That scope joins an
/// ambient (a , a
/// route's .Transacted()) as every redb connection does, but not a transaction begun on the context's shared
/// IRedbService (ExecuteAtomicAsync, BeginTransaction), which it does not see: pass the exchange,
/// or use a TransactionScope, when the call must commit or roll back with that work. In-memory implementations
/// take part in no transaction.
///
///
public interface IPromptTemplateRegistry
{
///
/// Returns the requested template. When is null,
/// the latest version is returned.
///
Task GetAsync(string name, string? version = null, IExchange? exchange = null, CancellationToken ct = default);
/// Registers or updates a template version.
Task SetAsync(PromptTemplate template, IExchange? exchange = null, CancellationToken ct = default);
/// Enumerates every version of every template — used for tooling.
Task> ListAsync(IExchange? exchange = null, CancellationToken ct = default);
}
/// A versioned prompt template.
public sealed class PromptTemplate
{
/// Stable template name (free of versioning).
public required string Name { get; init; }
/// Version string (semver, hash, or monotonically-increasing tag).
public required string Version { get; init; }
/// Template body — placeholder substitution is the caller's responsibility.
public required string Body { get; init; }
/// Optional human description.
public string? Description { get; init; }
/// Wall-clock timestamp the version was registered.
public DateTime CreatedAtUtc { get; init; } = DateTime.UtcNow;
}
/// In-memory registry — registered templates live only for the process lifetime.
public sealed class InMemoryPromptTemplateRegistry : IPromptTemplateRegistry
{
private readonly ConcurrentDictionary _templates = new(StringComparer.Ordinal);
///
public Task GetAsync(string name, string? version = null, IExchange? exchange = null, CancellationToken ct = default)
{
if (version is not null)
{
return Task.FromResult(_templates.TryGetValue(Key(name, version), out var exact) ? exact : null);
}
var latest = _templates.Values
.Where(t => string.Equals(t.Name, name, StringComparison.Ordinal))
.OrderByDescending(t => t.CreatedAtUtc)
.FirstOrDefault();
return Task.FromResult(latest);
}
///
public Task SetAsync(PromptTemplate template, IExchange? exchange = null, CancellationToken ct = default)
{
_templates[Key(template.Name, template.Version)] = template;
return Task.CompletedTask;
}
///
public Task> ListAsync(IExchange? exchange = null, CancellationToken ct = default) =>
Task.FromResult>(_templates.Values.ToArray());
private static string Key(string name, string version) => $"{name}@{version}";
}