PlotDirector/PlotLine/Services/Context/ContextIntentPalette.cs
2026-12-02 21:05:56 +00:00

73 lines
4.3 KiB
C#

using System.Data;
using Dapper;
using PlotLine.Data;
namespace PlotLine.Services.Context;
public sealed record IntentPaletteQuery(int BookID, int ChapterID, int? SceneID = null,
bool WiderStory = false, string Search = "");
public sealed record IntentPaletteScope(int ProjectID, int BookID, int ChapterID, int? SceneID);
public sealed record IntentPaletteSource(int SceneID, string Provenance);
public sealed record IntentPaletteEntry(string Key, string Name, string Kind, bool IsCandidate,
bool InScope, bool PossiblyAmbiguous, IReadOnlyList<string> Aliases,
IReadOnlyList<IntentPaletteSource> Sources);
public sealed record IntentPaletteFrame(IntentPaletteScope Scope,
IReadOnlyList<IntentPaletteEntry> Entries, bool HasMore);
// The database authorises the exact chapter/optional scene before any catalog is
// returned. Reads neither prepare candidates nor move the manuscript or focus.
public sealed class ContextIntentPalette(ISqlConnectionFactory connections, ICurrentUserService user)
{
public const int PageSize = 200;
public sealed record Row(string EntityKey, string Name, string Kind, bool IsCandidate,
bool InScope, bool PossiblyAmbiguous);
public sealed record AliasRow(string EntityKey, string Alias);
public sealed record SourceRow(string EntityKey, int SceneID, string Provenance);
public async Task<IntentPaletteFrame> Read(IntentPaletteQuery query)
{
Validate(query);
var author = user.UserId ?? throw new UnauthorizedAccessException();
using var db = connections.CreateConnection();
using var result = await db.QueryMultipleAsync("dbo.ContextIntent_Palette",
new { UserID = author, query.BookID, query.ChapterID, query.SceneID, query.WiderStory, Search = query.Search.Trim() },
commandType: CommandType.StoredProcedure);
var scope = await result.ReadSingleAsync<IntentPaletteScope>();
var rows = (await result.ReadAsync<Row>()).ToArray();
var aliases = (await result.ReadAsync<AliasRow>()).ToArray();
var sources = (await result.ReadAsync<SourceRow>()).ToArray();
if (scope.BookID != query.BookID || scope.ChapterID != query.ChapterID || scope.SceneID != query.SceneID)
throw new InvalidOperationException("The palette returned a different Intent scope.");
return Compose(scope, rows, aliases, sources);
}
public static void Validate(IntentPaletteQuery query)
{
if (query.BookID <= 0 || query.ChapterID <= 0 || query.SceneID <= 0 || query.Search is null || query.Search.Length > 200)
throw new ArgumentException("Choose a saved chapter and optional scene; search with at most 200 characters.");
}
// One identity can have many recorded/planned appearances and aliases. Labels
// never merge identities, including pending candidates with proposed targets.
public static IntentPaletteFrame Compose(IntentPaletteScope scope, IReadOnlyList<Row> rows,
IReadOnlyList<AliasRow> aliases, IReadOnlyList<SourceRow> sources)
{
var identities = rows.GroupBy(r => r.EntityKey, StringComparer.Ordinal).ToArray();
var byAlias = aliases.ToLookup(a => a.EntityKey, StringComparer.Ordinal);
var bySource = sources.ToLookup(s => s.EntityKey, StringComparer.Ordinal);
var entries = identities.Take(PageSize).Select(group =>
{
var row = group.First();
if (group.Any(r => r.Name != row.Name || r.Kind != row.Kind || r.IsCandidate != row.IsCandidate))
throw new InvalidOperationException("Conflicting Story Element identity metadata.");
return new IntentPaletteEntry(row.EntityKey, row.Name, row.Kind, row.IsCandidate,
group.Any(r => r.InScope), group.Any(r => r.PossiblyAmbiguous),
Array.AsReadOnly(byAlias[row.EntityKey].Select(a => a.Alias.Trim()).Prepend(row.Name)
.Where(a => !string.IsNullOrWhiteSpace(a)).Distinct(StringComparer.OrdinalIgnoreCase).ToArray()),
Array.AsReadOnly(bySource[row.EntityKey].Select(s => new IntentPaletteSource(s.SceneID, s.Provenance))
.Distinct().OrderBy(s => s.SceneID).ThenBy(s => s.Provenance, StringComparer.Ordinal).ToArray()));
}).ToArray();
return new(scope, Array.AsReadOnly(entries), identities.Length > PageSize);
}
}