add: hci
This commit is contained in:
26
Program.cs
26
Program.cs
@@ -2,6 +2,7 @@ using Alchegos.HCI.Components;
|
||||
using Alchegos.HCI.Filters;
|
||||
using Alchegos.HCI.Models;
|
||||
using Alchegos.HCI.Services;
|
||||
using Microsoft.AspNetCore.Authentication;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
@@ -11,7 +12,14 @@ builder.Services.AddRazorComponents()
|
||||
.AddInteractiveServerComponents();
|
||||
builder.Services.AddHttpClient();
|
||||
builder.Services.AddSingleton<IChatSessionService, InMemoryChatSessionService>();
|
||||
|
||||
builder.Services.AddSingleton<IAuthenticationService, AuthenticationService>();
|
||||
builder.Services.AddCors(options =>
|
||||
{
|
||||
options.AddPolicy(name: "n8n", policy =>
|
||||
{
|
||||
policy.WithOrigins("http://n8n:5678");
|
||||
});
|
||||
});
|
||||
var app = builder.Build();
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
@@ -37,25 +45,33 @@ app.MapPost("/api/chatwebhook", async (
|
||||
if (string.IsNullOrEmpty(payload.SessionId))
|
||||
{
|
||||
logger.LogWarning("Received webhook payload without sessionId.");
|
||||
return Results.BadRequest("sessionId is required.");
|
||||
var errors = new Dictionary<string, string[]> {
|
||||
{ nameof(payload.SessionId), ["sessionId is required."] }
|
||||
};
|
||||
return Results.ValidationProblem(errors,
|
||||
title: "One or more validation errors occurred.",
|
||||
statusCode: StatusCodes.Status400BadRequest);
|
||||
}
|
||||
|
||||
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}");
|
||||
return Results.Ok(new { message = $"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}");
|
||||
return Results.Ok(new { message = $"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'.");
|
||||
return Results.Problem(
|
||||
title: "Invalid payload content.",
|
||||
detail: "Payload must contain either a non-empty 'output' field or an 'action' field set to 'close_session'.",
|
||||
statusCode: StatusCodes.Status400BadRequest);
|
||||
}
|
||||
})
|
||||
.WithTags("Chat Webhook")
|
||||
|
||||
Reference in New Issue
Block a user