67 lines
2.2 KiB
C#
67 lines
2.2 KiB
C#
using Alchegos.HCI.Components;
|
|
using Alchegos.HCI.Filters;
|
|
using Alchegos.HCI.Models;
|
|
using Alchegos.HCI.Services;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
// Add services to the container.
|
|
builder.Services.AddRazorComponents()
|
|
.AddInteractiveServerComponents();
|
|
builder.Services.AddHttpClient();
|
|
builder.Services.AddSingleton<IChatSessionService, InMemoryChatSessionService>();
|
|
|
|
var app = builder.Build();
|
|
|
|
// Configure the HTTP request pipeline.
|
|
if (!app.Environment.IsDevelopment())
|
|
{
|
|
app.UseExceptionHandler("/Error", createScopeForErrors: true);
|
|
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
|
|
app.UseHsts();
|
|
}
|
|
|
|
app.UseHttpsRedirection();
|
|
|
|
|
|
app.UseAntiforgery();
|
|
|
|
app.MapStaticAssets();
|
|
|
|
app.MapPost("/api/chatwebhook", async (
|
|
[FromBody] WebhookPayload payload,
|
|
IChatSessionService chatSessionService,
|
|
ILogger<Program> logger) =>
|
|
{
|
|
if (string.IsNullOrEmpty(payload.SessionId))
|
|
{
|
|
logger.LogWarning("Received webhook payload without sessionId.");
|
|
return Results.BadRequest("sessionId is required.");
|
|
}
|
|
|
|
if (payload.Action == "close_session")
|
|
{
|
|
logger.LogInformation("Webhook request received to close session: {SessionId}", payload.SessionId);
|
|
await chatSessionService.TriggerSessionClosed(payload.SessionId);
|
|
return Results.Ok($"Session close requested for {payload.SessionId}");
|
|
}
|
|
else if (!string.IsNullOrEmpty(payload.Output))
|
|
{
|
|
logger.LogInformation("Webhook request received with output for session: {SessionId}", payload.SessionId);
|
|
await chatSessionService.TriggerMessageReceived(payload.SessionId, payload.Output);
|
|
return Results.Ok($"Output received for {payload.SessionId}");
|
|
}
|
|
else
|
|
{
|
|
logger.LogWarning("Received webhook payload for session {SessionId} with neither 'output' nor 'action':'close_session'.", payload.SessionId);
|
|
return Results.BadRequest("Payload must contain either 'output' or 'action':'close_session'.");
|
|
}
|
|
})
|
|
.WithTags("Chat Webhook")
|
|
.AddEndpointFilter<WebhookAuthFilter>();
|
|
|
|
app.MapRazorComponents<App>()
|
|
.AddInteractiveServerRenderMode();
|
|
|
|
app.Run(); |