Path: redb-route/tests/redb.Route.Tests.Llm/TestHelpers/EchoToolRoute.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/tests/redb.Route.Tests.Llm/TestHelpers/EchoToolRoute.cs
----
using redb.Route.Configuration;
using redb.Route.Llm.Extensions;
namespace redb.Route.Tests.Llm.TestHelpers;
///
/// Test helper that mounts a direct: route with .AsLlmTool(...).
/// The route's processor captures every input passed by the agent engine and
/// returns a fixed JSON reply, letting tests assert that:
///
/// - The model called the tool (input list non-empty).
/// - The model passed the right arguments (input contains expected keys).
/// - The agent surfaced the tool result back to the model (final reply mentions the payload).
///
///
/// The descriptor lands in via the DSL,
/// so the agent picks it up by name from ?tools=... or
/// .
///
///
public sealed class EchoToolRoute : RouteBuilder
{
private readonly string _toolName;
private readonly string _description;
private readonly string _inputSchema;
private readonly Func _replyFactory;
private readonly ToolSideEffect _sideEffect;
private readonly ToolCachingPolicy _caching;
private readonly IReadOnlyList _requiredClaims;
private readonly List _capturedInputs = new();
private readonly List> _capturedHeaders = new();
private readonly List> _capturedProperties = new();
private readonly List _capturedServiceProviders = new();
private readonly List _capturedRouteIds = new();
/// Inputs (raw JSON) the agent has passed to this tool, in call order.
public IReadOnlyList CapturedInputs => _capturedInputs;
/// Header snapshot of every tool exchange, in call order.
public IReadOnlyList> CapturedHeaders => _capturedHeaders;
/// Exchange-property snapshot of every tool exchange, in call order.
public IReadOnlyList> CapturedProperties => _capturedProperties;
/// DI scope provider seen by every tool exchange — reference-compared against the agent exchange's.
public IReadOnlyList CapturedServiceProviders => _capturedServiceProviders;
/// Route id seen by every tool exchange, in call order.
public IReadOnlyList CapturedRouteIds => _capturedRouteIds;
/// The endpoint URI the descriptor dispatches to (direct:tool-{name}).
public string EndpointUri => $"direct:tool-{_toolName}";
///
/// Creates a tool route that returns for every call, with the
/// same optional safety metadata as the delegate overload.
///
public EchoToolRoute(string toolName, string description, string inputSchema, string replyJson,
ToolSideEffect sideEffect = ToolSideEffect.ReadOnly,
ToolCachingPolicy caching = ToolCachingPolicy.None,
params string[] requiredClaims)
: this(toolName, description, inputSchema, _ => replyJson, sideEffect, caching, requiredClaims) { }
///
/// Creates a tool route whose reply depends on the input JSON. The trailing optional
/// parameters declare the tool's safety metadata — governance tests use them to express
/// claim requirements, side-effect class and caching policy without a second helper type.
///
/// Tool name exposed to the model.
/// Human-readable description.
/// JSON Schema for the input arguments.
/// Builds the reply JSON from the raw input JSON.
/// Side-effect class (default ).
/// Caching policy (default ).
/// Claims the calling principal must carry (default: none).
public EchoToolRoute(string toolName, string description, string inputSchema, Func replyFactory,
ToolSideEffect sideEffect = ToolSideEffect.ReadOnly,
ToolCachingPolicy caching = ToolCachingPolicy.None,
params string[] requiredClaims)
{
_toolName = toolName;
_description = description;
_inputSchema = inputSchema;
_replyFactory = replyFactory;
_sideEffect = sideEffect;
_caching = caching;
_requiredClaims = requiredClaims ?? [];
}
///
protected override void Configure()
{
var tool = From(EndpointUri)
.AsLlmTool(_toolName)
.Description(_description)
.Input(_inputSchema)
.SideEffect(_sideEffect)
.Cost(ToolCostClass.Cheap);
if (_caching != ToolCachingPolicy.None) tool = tool.Caching(_caching);
foreach (var claim in _requiredClaims) tool = tool.RequireClaim(claim);
tool.Then()
.Process(e =>
{
var input = e.In.Body switch
{
string s => s,
null => "{}",
var x => x.ToString() ?? "{}"
};
lock (_capturedInputs)
{
_capturedInputs.Add(input);
_capturedHeaders.Add(new Dictionary(e.In.Headers, StringComparer.OrdinalIgnoreCase));
_capturedProperties.Add(new Dictionary(e.Properties, StringComparer.OrdinalIgnoreCase));
_capturedServiceProviders.Add(e.ServiceProvider);
_capturedRouteIds.Add(e.RouteId);
}
e.Out ??= e.In.Clone();
e.Out.Body = _replyFactory(input);
e.Out.Headers["Content-Type"] = "application/json";
});
}
}