namespace AIStudio.Assistants.VisualBriefing; /// /// Guards parts compiled by AI Studio after the model-controlled contracts have been validated. /// internal static class VisualBriefingCompilerInvariant { private const string USER_MESSAGE = "AI Studio could not assemble this briefing because its own compiler produced an invalid part. This is a defect in AI Studio, not in the model response."; /// /// Fails the build when compiled parts violate the artifact contract. /// /// The stage running the compilation. /// The compiler issue, or an empty string when the parts are valid. /// Thrown when the compiled parts are invalid. internal static void Guard(VisualBriefingBuildStage stage, string compilerIssue) { if (string.IsNullOrEmpty(compilerIssue)) return; throw new VisualBriefingBuildException( VisualBriefingFailureCode.COMPILER_INVARIANT_VIOLATED, stage, USER_MESSAGE, $"Stage={stage}; CompilerIssue={compilerIssue}"); } /// /// Runs a compilation and translates structural failures into a compiler invariant failure. /// /// The compilation result type. /// The stage running the compilation. /// The compilation to run. /// The compilation result. /// Thrown when the compilation fails structurally. internal static T Guard(VisualBriefingBuildStage stage, Func compile) { try { return compile(); } catch (InvalidDataException exception) { throw new VisualBriefingBuildException( VisualBriefingFailureCode.COMPILER_INVARIANT_VIOLATED, stage, USER_MESSAGE, $"Stage={stage}; CompilerIssue={exception.Message}"); } } }