feat: implement dynamic knowledge graph updates and state management services
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
using System.Net.Http.Json;
|
||||
using FluentResults;
|
||||
using NexusReader.Application.Abstractions.Services;
|
||||
using NexusReader.Application.DTOs.AI;
|
||||
|
||||
namespace NexusReader.Web.Client.Services;
|
||||
|
||||
public class WasmKnowledgeService : IKnowledgeService
|
||||
{
|
||||
private readonly HttpClient _httpClient;
|
||||
|
||||
public WasmKnowledgeService(HttpClient httpClient)
|
||||
{
|
||||
_httpClient = httpClient;
|
||||
}
|
||||
|
||||
public async Task<Result<KnowledgePacket>> GetKnowledgeAsync(string text, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
Console.WriteLine($"[WasmKnowledgeService] Calling API for extraction (Text length: {text.Length})...");
|
||||
var response = await _httpClient.PostAsJsonAsync("/api/knowledge", new { text }, cancellationToken);
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
Console.WriteLine("[WasmKnowledgeService] API Response success. Deserializing...");
|
||||
var packet = await response.Content.ReadFromJsonAsync<KnowledgePacket>(cancellationToken: cancellationToken);
|
||||
return packet != null ? Result.Ok(packet) : Result.Fail("Failed to deserialize knowledge packet.");
|
||||
}
|
||||
|
||||
var errorBody = await response.Content.ReadAsStringAsync(cancellationToken);
|
||||
Console.WriteLine($"[WasmKnowledgeService] API Error ({response.StatusCode}): {errorBody}");
|
||||
return Result.Fail($"Server error ({response.StatusCode}): {errorBody}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"[WasmKnowledgeService] Exception: {ex.Message}");
|
||||
return Result.Fail(new Error($"Network or parsing error: {ex.Message}").CausedBy(ex));
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<Result> ClearCacheAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
Console.WriteLine("[WasmKnowledgeService] Requesting cache clear...");
|
||||
var response = await _httpClient.DeleteAsync("/api/knowledge", cancellationToken);
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
Console.WriteLine("[WasmKnowledgeService] Cache cleared successfully.");
|
||||
return Result.Ok();
|
||||
}
|
||||
|
||||
var errorBody = await response.Content.ReadAsStringAsync(cancellationToken);
|
||||
return Result.Fail($"Server error ({response.StatusCode}): {errorBody}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Fail(new Error($"Network error: {ex.Message}").CausedBy(ex));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user