Custom Bot Adapters And Source Generation
HPD bot source generation can produce transport-neutral dispatch, dependency-injection registration, and ASP.NET webhook mapping code for custom adapters. This page documents the verified shape and caveats for adapter authors. It does not document platform-specific setup.
Add both packages before using the generated adapter surface:
dotnet add package HPD-Agent.Bots.Abstractions --version 1.0.0
dotnet add package HPD-Agent.Bots.AspNetCore --version 1.0.0The generated registry references the ASP.NET Core bridge even when a custom host drives the adapter through a non-HTTP transport.
Adapter Attribute
Mark an adapter with [HpdBot("name")]:
using HPD.Agent.Bots;
[HpdBot("contoso")]
[HpdStreaming(StreamingStrategy.PostAndEdit)]
public sealed partial class ContosoBot
{
}
public sealed class ContosoBotConfig
{
public string? AgentId { get; set; }
}The {ClassName}Config type is required by the generated registration. For an adapter class named ContosoBot, it must be ContosoBotConfig in the same namespace. The source generator emits a registration class for the adapter name. For a bot named contoso, the generated API shape includes methods named:
services.AddContosoBot(...);
endpoints.MapContosoWebhook(...);Generated dispatch makes the partial adapter implement IBotAdapter:
Task<BotAdapterResponse> HandleAsync(
BotInboundEnvelope envelope,
CancellationToken cancellationToken = default);MapContosoWebhook(...) is the ASP.NET bridge around that method. Non-ASP.NET hosts can build a BotInboundEnvelope themselves and call HandleAsync(...) directly.
Generated registration configures adapter options, named streaming options when [HpdStreaming] is present, the bot singleton, and PlatformSessionMapper. Socket adapters can also add the socket hosted service when the expected socket configuration property is present.
Bot Event Handlers
Use [HpdBotEventHandler("event-type")] to mark methods that handle extracted platform events:
public sealed record ContosoMessage(string Text);
[HpdBotEventHandler("message.created")]
private Task<BotAdapterResponse> HandleMessageAsync(
BotRequestContext context,
ContosoMessage payload)
{
return Task.FromResult(BotAdapterResponse.Ok());
}Handlers must be private or internal, accept (BotRequestContext, Payload), and return Task<BotAdapterResponse>.
Use [HpdHttpMethods(...)] to declare allowed HTTP methods for the generated webhook route. Without it, the generated mapping defaults to POST.
[HpdHttpMethods("GET", "POST")]
public sealed partial class ContosoBot
{
}Pre-Dispatch And Body Extraction
[HpdBotPreDispatch] marks adapter code that should run before handler dispatch. Platform adapters use this stage for work such as signature checks, verification challenges, or request normalization.
[HpdBotPreDispatch]
private Task<BotAdapterResponse?> VerifyAsync(
BotRequestContext ctx,
byte[] bodyBytes)
{
if (ctx.Header("x-contoso-signature") is null)
return Task.FromResult<BotAdapterResponse?>(BotAdapterResponse.Status(401));
return Task.FromResult<BotAdapterResponse?>(null);
}[HpdBotEnvelopeExtractor] marks adapter code that extracts the payload or event envelope from the incoming request body.
[HpdBotEnvelopeExtractor]
private (string? eventType, byte[] dispatchBytes) ExtractEnvelope(
BotRequestContext ctx,
byte[] bodyBytes)
{
return (ctx.Header("x-contoso-event"), bodyBytes);
}Keep platform-specific auth and verification code in the adapter package, not in generic app code.
Generated dispatch currently expects a hand-written JSON context named for the adapter, such as ContosoBotJsonContext, with payload types registered through JsonSerializable attributes.
The pre-1.0 [HpdBotPayload] marker is internal and is not part of the custom-adapter contract yet. It does not generate JSON metadata.
BotRequestContext intentionally exposes request data through neutral helpers such as Header(...), QueryValue(...), Method, Path, and CancellationToken. Use those in generated hooks instead of HttpContext so the same adapter can be driven by webhooks, polling, socket workers, or tests.
Thread Ids
Use [ThreadId] on the type that formats and parses platform thread keys:
[ThreadId("contoso:{WorkspaceId}:{ConversationId}")]
public partial record ContosoThreadId(
string WorkspaceId,
string ConversationId);The platform key produced by this type is what PlatformSessionMapper uses to find or create the HPD session and thread.
Generated Registry And Mapping
The generator emits Add{Name}Bot(...), Map{Name}Webhook(...), and neutral IBotAdapter dispatch for generated adapters. It can also emit assembly-local registry entries such as BotRegistry.g.cs.
MapHPDBots() is a convenience mapper that enumerates registered IBotRegistryProvider services. Generated BotRegistry.g.cs alone is not sufficient unless a generated or hand-written provider is also registered by Add{Name}Bot(...) or package infrastructure. For custom generated adapters, individual Map{Name}Webhook(...) remains the source-confirmed mapping path.
MapHPDBots() is in HPD.Agent.Bots.AspNetCore.EndpointMapping; include:
using HPD.Agent.Bots.AspNetCore.EndpointMapping;
app.MapHPDBots();Package-Level Extensions
Concrete platform packages often provide hand-written partial service extensions that call the generated overload and then register platform infrastructure such as:
- secret resolvers
- HTTP clients
- platform API clients
- formatting services
- gateway or socket services
- polling services
- SDK endpoint integration
- registry providers
For package consumers, prefer the package-provided Add...Bot(...) overload rather than calling a bare generated overload unless the package docs say otherwise.
Diagnostics
The source generator currently reports diagnostics in the HPDA001 through HPDA011 range for adapter visibility, shape, and duplicate-handler errors. HPDA004 and HPDA006 relate to internal pre-1.0 markers and are not supported custom-adapter features. Treat the remaining diagnostics as source-generator feedback: fix the adapter shape, method visibility, attribute placement, or duplicate declaration before expecting generated registration to appear.
Permission Handler Caveat
[HpdPermissionHandler] is internal and is not an implemented generator-routed permission workflow. Current source inspection shows the generator recognizes duplicate permission-handler declarations, but the response-routing flow described by the earlier attribute comments is not implemented.
Use platform adapter code and current runtime behavior for permission responses until that API is clarified.