namespace AIStudio.Tools; /// /// The outcome of processing one content stream event: either content to append, or a reported /// failure. /// /// /// Content and error are kept apart on purpose. A reported failure must never be appended as /// content, because that would hand the failure to the AI as if it were part of the document. /// /// The content to append, or null when this event carries none. /// The reported failure, or null when the event was processed successfully. /// What the runtime filtered out of the content, or null when it filtered nothing. /// The number of tokens of the content, or null when it is unknown. /// Where the content came from: its page, its sheet and row, or its line. public readonly record struct ContentStreamProcessedEvent(string? Content, ContentStreamErrorDetails? Error, ContentStreamPromptInjectionDetails? PromptInjection = null, int? TokenCount = null, SourceLocation Location = default) { /// /// An event which neither produced content nor reported a failure. /// public static readonly ContentStreamProcessedEvent NOTHING = new(null, null); /// /// An event which produced content, with the token count and the location of that very content. /// /// /// The count travels with the content because a reader may hold content back across several /// events: pairing it with the count of the event which released it would size it by the /// wrong text. The location travels along for the same reason, and so that whoever indexes the /// content is told where it came from instead of having to read it back out of the text. /// /// The content to append. /// The number of tokens of that content, or null when it is unknown. /// Where that content came from, or nothing when the runtime stated nothing. public static ContentStreamProcessedEvent FromContent(string? content, int? tokenCount = null, SourceLocation location = default) => new(content, null, TokenCount: tokenCount, Location: location); public static ContentStreamProcessedEvent FromError(ContentStreamErrorDetails? error) => new(null, error); /// /// An event reporting that suspicious passages were filtered out of the content. /// /// /// Carries no content and no error: the content was delivered by the events before it, and /// filtering is a notice rather than a failure. /// public static ContentStreamProcessedEvent FromPromptInjection(ContentStreamPromptInjectionDetails? promptInjection) => new(null, null, promptInjection); }