29 lines
4.3 KiB
C#
29 lines
4.3 KiB
C#
using System.Diagnostics;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.Caching.Memory;
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
|
using Microsoft.Extensions.Options;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.Extensions.FileProviders;
|
|
using PlotLine.Models;
|
|
using PlotLine.Services;
|
|
internal static class ParagraphLiveAiTests
|
|
{
|
|
private sealed class Host:IWebHostEnvironment{public string WebRootPath{get;set;}="";public IFileProvider WebRootFileProvider{get;set;}=new NullFileProvider();public string ApplicationName{get;set;}="PlotLine";public IFileProvider ContentRootFileProvider{get;set;}=new NullFileProvider();public string ContentRootPath{get;set;}="/srv/repos/PlotDirector/PlotLine";public string EnvironmentName{get;set;}="Development";}
|
|
public static async Task Run()
|
|
{
|
|
// Explicit live test only: synthetic prose, existing Development settings, no manuscript/user data.
|
|
var info=new ProcessStartInfo("sudo"){RedirectStandardOutput=true,RedirectStandardError=true};foreach(var value in new[]{"-n","cat","/etc/plotdirector/plotdirector.env"})info.ArgumentList.Add(value);using var process=Process.Start(info)!;var text=await process.StandardOutput.ReadToEndAsync();await process.WaitForExitAsync();if(process.ExitCode!=0)throw new Exception("Development settings unavailable.");
|
|
var values=text.Split('\n').Where(l=>!l.StartsWith('#')&&l.Contains('=')).Select(l=>l.Split('=',2)).ToDictionary(v=>v[0].Replace("__",":"),v=>(string?)v[1].Trim().Trim('\'', '"'));
|
|
async Task<string> Read(string path){var read=new ProcessStartInfo("sudo"){RedirectStandardOutput=true,RedirectStandardError=true};foreach(var arg in new[]{"-n","cat",path})read.ArgumentList.Add(arg);using var child=Process.Start(read)!;var content=await child.StandardOutput.ReadToEndAsync();await child.WaitForExitAsync();if(child.ExitCode!=0)throw new Exception("Development configuration read failed.");return content;}
|
|
var configuration=new ConfigurationBuilder().AddJsonStream(new MemoryStream(System.Text.Encoding.UTF8.GetBytes(await Read("/srv/apps/plotdirector-dev/current/appsettings.json")))).AddJsonStream(new MemoryStream(System.Text.Encoding.UTF8.GetBytes(await Read("/srv/apps/plotdirector-dev/current/appsettings.Development.json")))).AddInMemoryCollection(values).Build();
|
|
var options=new StoryIntelligenceOptions();configuration.GetSection("OpenAI").Bind(options);configuration.GetSection("StoryIntelligence").Bind(options);if(string.IsNullOrWhiteSpace(options.ApiKey))throw new Exception("No Development AI credential configured.");
|
|
using var http=new HttpClient{Timeout=TimeSpan.FromMinutes(4)};using var cache=new MemoryCache(new MemoryCacheOptions());
|
|
var service=new ManuscriptStructureAnalysisService(new StoryPromptRepository(new Host(),cache,NullLogger<StoryPromptRepository>.Instance),new StoryPromptVersionService(),new StoryIntelligenceClient(http,Options.Create(options),NullLogger<StoryIntelligenceClient>.Instance,new StoryIntelligenceRequestLimiter(Options.Create(options))),new ChapterStructureValidator(),NullLogger<ManuscriptStructureAnalysisService>.Instance);
|
|
string[] paragraphs=["At dawn, Mara waited at the station. The porter handed her an unopened letter. She read the address and boarded the train.","She sat beside the window as the train left. The letter remained sealed in her coat pocket.","Three days later, on a distant island, Tomas extinguished the lighthouse lamp. He checked the silent radio and watched the storm approach.","He locked the lighthouse door and walked towards the empty harbour."];
|
|
var result=await service.AnalyseChapterAsync(new(){PrivateAnalysis=true,ConfidenceContractVersion=2,TemporaryChapterKey="synthetic-live",ChapterText=string.Join("\n\n",paragraphs),ParagraphMap=paragraphs.Select((p,i)=>new ManuscriptScanParagraphMap{StructuralParagraphOrdinal=i+1,WordParagraphIndex=i+1,Text=p}).ToArray(),SourceParagraphs=paragraphs.Select((p,i)=>new ManuscriptScanSourceParagraph{WordParagraphIndex=i+1,Text=p,IncludeInSourceText=true}).ToArray()});
|
|
if(!result.Success)throw new Exception("Live strict integer chapter analysis failed; no author source was submitted or changed.");
|
|
Console.WriteLine($"PASS live Development AI: schema2 integer chapter contract accepted, {result.Scenes.Count} scene proposals mapped within four submitted synthetic paragraphs");
|
|
}
|
|
}
|