41 lines
3.7 KiB
C#

using PlotDirector.Companion.Protocol;
using PlotLine.Models;
namespace PlotLine.Services.NativeCompanion;
public static class NativeChapterMapping
{
public static ManuscriptStructureAnalysisRequest Build(AiChapterRequest request,int maxCharacters=120000)
{
if(request.ChapterTitle==null||request.ChapterTitle.Length>200||request.RequestId==Guid.Empty||request.ManuscriptId==Guid.Empty||request.SessionId==Guid.Empty||request.BookId<=0||request.Revision<0||request.Paragraphs==null||request.Paragraphs.Length is <1 or >10000||request.ChapterStart<0||request.ChapterEnd<=request.ChapterStart)throw new ArgumentException("Invalid chapter identity or range.");
var end=request.ChapterStart;var previousIndex=-1;var characters=0;
foreach(var p in request.Paragraphs)
{
if(p==null||p.Text==null||p.Text.Length>maxCharacters||p.Index<=previousIndex||p.Start!=end||p.End<=p.Start||p.End>request.ChapterEnd)throw new ArgumentException("Invalid chapter paragraph mapping.");
characters+=p.Text.Length;if(characters>maxCharacters)throw new ArgumentException("Chapter exceeds configured analysis size. Split or review its boundaries.");
previousIndex=p.Index;end=p.End;
}
if(end!=request.ChapterEnd)throw new ArgumentException("Incomplete chapter.");
var readable=request.Paragraphs.Where(p=>!p.IsHeading&&!string.IsNullOrWhiteSpace(p.Text.Trim('\a','\r','\n'))).ToArray();
if(readable.Length==0)throw new ArgumentException("This chapter contains no readable paragraphs.");
return new(){BookID=request.BookId,PreviewID=request.RequestId,TemporaryChapterKey=request.RequestId.ToString("N"),ChapterNumber=1,SourceLabel="Native Word chapter",PrivateAnalysis=true,
ChapterText=string.Join("\n\n",readable.Select(p=>p.Text)),
ParagraphMap=readable.Select((p,i)=>new ManuscriptScanParagraphMap{StructuralParagraphOrdinal=i+1,WordParagraphIndex=p.Index,Text=p.Text}).ToArray(),
SourceParagraphs=request.Paragraphs.Select(p=>new ManuscriptScanSourceParagraph{WordParagraphIndex=p.Index,Text=p.Text,IsBlank=string.IsNullOrWhiteSpace(p.Text),IsChapterHeading=p.IsHeading,IncludeInSourceText=!p.IsHeading}).ToArray()};
}
public static AiChapterResult Map(AiChapterRequest request,ManuscriptStructureAnalysisResult result)
{
if(!result.Success)return new(){RequestId=request.RequestId,State="failed",Message="AI could not produce valid scene boundaries. Review the chapter or try again."};
if(result.Scenes.Count>500)throw new ArgumentException("Too many scenes in one chapter.");
var scenes=new List<StructureSuggestion>();
foreach(var scene in result.Scenes)
{
var start=request.Paragraphs.FirstOrDefault(p=>p.Index==scene.WordStartPosition);
var end=request.Paragraphs.FirstOrDefault(p=>p.Index==scene.WordEndPosition);
if(start==null||end==null||end.End<=start.Start)throw new ArgumentException("AI scene could not be mapped to Word.");
string Bound(string? text,int length)=>string.IsNullOrEmpty(text)?"":text.Length<=length?text:text[..length];
scenes.Add(new(){Kind="Scene",Origin="AI",Start=start.Start,End=end.End,Paragraph=start.Index,Title=Bound(scene.Title,200),Reason=Bound(scene.Reason??scene.StructuralSummary,600),Summary=Bound(scene.StructuralSummary,600),Confidence=scene.Confidence?.ToString("0.00",System.Globalization.CultureInfo.InvariantCulture)??"Unspecified"});
}
return new(){RequestId=request.RequestId,State="complete",DocumentRevision=request.Revision,ChapterTitle=request.ChapterTitle,Message="AI suggestions only. Manuscript and existing markers are unchanged.",Scenes=scenes.ToArray()};
}
}