Path: redb-route/redb.Route.Llm/Knowledge/KnowledgeComponent.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/Knowledge/KnowledgeComponent.cs
----
using redb.Route.Abstractions;
using redb.Route.Core;
namespace redb.Route.Llm.Knowledge;
///
/// Ingest transport for the knowledge base. Scheme: knowledge.
///
/// To("knowledge://<collection>?chunkChars=1000&overlap=100&embed=true") —
/// takes the inbound exchange body as a document, splits it into chunks, optionally
/// embeds them (when an is registered),
/// and upserts into the . Producer-only.
///
///
///
/// From("file://docs?include=*.md").To("knowledge://handbook");
///
///
///
[EndpointRole(EndpointRole.Producer, Hint = "It ingests: use To(\"knowledge://\").")]
public sealed class KnowledgeComponent : ComponentBase
{
///
public override string Scheme => "knowledge";
///
public override IEndpoint CreateEndpoint(EndpointUri uri)
{
ArgumentNullException.ThrowIfNull(uri);
var options = new KnowledgeEndpointOptions();
options.BindFromUri(uri.RawParameters);
options.Validate();
return new KnowledgeEndpoint(uri, this, options);
}
}
/// Options for a knowledge:// endpoint (URI query auto-binds by name).
public sealed class KnowledgeEndpointOptions : EndpointOptions
{
/// Target chunk size in characters.
public int ChunkChars { get; set; } = 1000;
/// Overlap (chars) between consecutive chunks. Must be in [0, ChunkChars).
public int Overlap { get; set; } = 0;
///
/// Embed chunks on ingest when an is
/// available. Set ?embed=false to force keyword-only ingest (empty vectors).
///
public bool Embed { get; set; } = true;
///
/// Stable document id base — chunk ids are {DocId}#{index}, so a re-ingest
/// of the same document replaces its chunks in place. Falls back to the
/// knowledge.doc.id header, then to "doc".
///
public string? DocId { get; set; }
/// Collection fallback when the URI has no path (knowledge://?collection=...).
public string? Collection { get; set; }
///
public override void Validate()
{
if (ChunkChars < 1)
throw new InvalidOperationException("knowledge://: chunkChars must be >= 1.");
if (Overlap < 0 || Overlap >= ChunkChars)
throw new InvalidOperationException("knowledge://: overlap must be in [0, chunkChars).");
}
}
/// Knowledge ingest endpoint. URI path = collection name.
public sealed class KnowledgeEndpoint : EndpointBase
{
/// Target collection (URI path, or ?collection=).
public string Collection { get; }
/// Creates the endpoint.
public KnowledgeEndpoint(EndpointUri uri, KnowledgeComponent component, KnowledgeEndpointOptions options)
: base(uri, component, options)
{
Collection = !string.IsNullOrWhiteSpace(uri.Path) ? uri.Path : (options.Collection ?? string.Empty);
if (string.IsNullOrWhiteSpace(Collection))
throw new InvalidOperationException(
"knowledge:// requires a collection — use 'knowledge://' or '?collection='.");
}
///
public override IProducer CreateProducer() => new KnowledgeProducer(this, Options);
///
public override IConsumer CreateConsumer(IProcessor processor) =>
throw new NotSupportedException(
"knowledge:// is producer-only (ingest). Use To(\"knowledge://\").");
}