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.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=>!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),IncludeInSourceText=true}).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(); 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),Confidence=scene.Confidence?.ToString("0.00",System.Globalization.CultureInfo.InvariantCulture)??"Unspecified"}); } return new(){RequestId=request.RequestId,State="complete",Message="AI suggestions only. Manuscript and existing markers are unchanged.",Scenes=scenes.ToArray()}; } }