69 lines
4.9 KiB
C#
69 lines
4.9 KiB
C#
using System.Text.Json;
|
|
using PlotLine.Data;
|
|
using PlotLine.Models;
|
|
using PlotLine.ViewModels;
|
|
|
|
namespace PlotLine.Services;
|
|
|
|
public interface IStoryIntelligenceExceptionReviewService
|
|
{
|
|
Task AssessPreparedAsync(int bookId, string module);
|
|
Task ReassessAsync(OnboardingStoryIntelligenceBatch batch, string module);
|
|
Task<IReadOnlyList<StoryIntelligenceReviewSummary>> SummaryAsync(int bookId);
|
|
Task<IReadOnlyList<StoryIntelligenceReviewCandidateRow>> ListAsync(int bookId, string module);
|
|
}
|
|
|
|
public sealed class StoryIntelligenceExceptionReviewService(
|
|
IStoryIntelligenceReviewCandidateRepository repository,
|
|
IStoryIntelligenceKnowledgeImportService knowledge,
|
|
IStoryIntelligenceRelationshipImportService relationships,
|
|
IStoryIntelligenceAssetImportService assets) : IStoryIntelligenceExceptionReviewService
|
|
{
|
|
public static readonly JsonSerializerOptions JsonOptions = new(JsonSerializerDefaults.Web);
|
|
public Task<IReadOnlyList<StoryIntelligenceReviewSummary>> SummaryAsync(int bookId) => repository.GetReviewSummaryAsync(bookId);
|
|
public Task<IReadOnlyList<StoryIntelligenceReviewCandidateRow>> ListAsync(int bookId, string module) => repository.ListAllAsync(bookId, module);
|
|
public Task AssessPreparedAsync(int bookId, string module) => AssessAsync(bookId, module, new Dictionary<string, string>());
|
|
|
|
// Explicit POST only: recover richer evidence from stored scene JSON, never call an AI client.
|
|
// Match stable keys in place. Unmatched older proposals stay visible and optional, never guessed or deleted.
|
|
public async Task ReassessAsync(OnboardingStoryIntelligenceBatch batch, string module)
|
|
{
|
|
Dictionary<string, string> payloads = module switch
|
|
{
|
|
StoryIntelligenceReviewModules.Knowledge => (await knowledge.BuildReviewAsync(batch)).Candidates.ToDictionary(x => x.Key, x => JsonSerializer.Serialize(x, JsonOptions)),
|
|
StoryIntelligenceReviewModules.Relationships => (await relationships.BuildReviewAsync(batch)).Candidates.ToDictionary(x => x.Key, x => JsonSerializer.Serialize(x, JsonOptions)),
|
|
StoryIntelligenceReviewModules.Assets => (await assets.BuildReviewAsync(batch)).Candidates.ToDictionary(x => x.Key, x => JsonSerializer.Serialize(x, JsonOptions)),
|
|
_ => throw new ArgumentException("Unsupported review category.", nameof(module))
|
|
};
|
|
await AssessAsync(batch.BookID, module, payloads);
|
|
}
|
|
|
|
private async Task AssessAsync(int bookId, string module, IReadOnlyDictionary<string, string> refreshed)
|
|
{
|
|
if (module is not (StoryIntelligenceReviewModules.Knowledge or StoryIntelligenceReviewModules.Relationships or StoryIntelligenceReviewModules.Assets)) return;
|
|
var rows = await repository.ListAllAsync(bookId, module);
|
|
string Payload(StoryIntelligenceReviewCandidateRow row) => row.DecisionStatus == "Pending" && refreshed.TryGetValue(row.StableKey, out var json) ? json : row.CandidatePayloadJson;
|
|
// Ignored proposals are not evidence against a later observation. Reviewed proposals may
|
|
// have been corrected in canonical editors; do not reinterpret their original AI state.
|
|
var contextRows = rows.Where(x => x.DecisionStatus is "Pending" or "AutoAccepted").ToList();
|
|
var knowledgeContext = module == StoryIntelligenceReviewModules.Knowledge
|
|
? contextRows.Select(x => JsonSerializer.Deserialize<StoryIntelligenceKnowledgeReviewCandidateViewModel>(Payload(x), JsonOptions)!).ToList() : [];
|
|
var relationshipContext = module == StoryIntelligenceReviewModules.Relationships
|
|
? contextRows.Select(x => JsonSerializer.Deserialize<StoryIntelligenceRelationshipReviewCandidateViewModel>(Payload(x), JsonOptions)!).ToList() : [];
|
|
// Classify the whole snapshot before accepting any item; conflicts cannot disappear as rows are accepted.
|
|
var assessments = rows.Where(x => x.DecisionStatus == "Pending").Select(row =>
|
|
{
|
|
var payload = Payload(row);
|
|
var assessment = module switch
|
|
{
|
|
StoryIntelligenceReviewModules.Knowledge => StoryIntelligenceReviewPolicy.Knowledge(JsonSerializer.Deserialize<StoryIntelligenceKnowledgeReviewCandidateViewModel>(payload, JsonOptions)!, knowledgeContext),
|
|
StoryIntelligenceReviewModules.Relationships => StoryIntelligenceReviewPolicy.Relationship(JsonSerializer.Deserialize<StoryIntelligenceRelationshipReviewCandidateViewModel>(payload, JsonOptions)!, relationshipContext),
|
|
_ => StoryIntelligenceReviewPolicy.Asset(JsonSerializer.Deserialize<StoryIntelligenceAssetReviewCandidateViewModel>(payload, JsonOptions)!)
|
|
};
|
|
return (Row: row, Payload: payload, Assessment: assessment);
|
|
}).ToList();
|
|
foreach (var item in assessments)
|
|
await repository.AssessAsync(item.Row.ReviewCandidateID, item.Row.ReviewCandidateGenerationID, item.Payload, JsonSerializer.Serialize(item.Assessment, JsonOptions));
|
|
}
|
|
}
|