Generated the JSON contracts of providers, the runtime, and services at compile time (#1150)

This commit is contained in:
Thorsten Sommer authored and GitHub committed 2026-11-03 07:59:30 +01:00
1 parent e954881fda
commit 24e3c22db3
140 files changed
+1415 -292

No files matched your search

@@ -5,12 +5,12 @@ namespace AIStudio.Provider.OpenAI;
public sealed record AssistantToolCallMessage : IMessageBase
{
public string Role { get; init; } = "assistant";
public string Role { get; set; } = "assistant";
public JsonElement? Content { get; init; }
public JsonElement? Content { get; set; }
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string? ReasoningContent { get; init; }
public string? ReasoningContent { get; set; }
public IList<ChatCompletionToolCall> ToolCalls { get; init; } = [];
public IList<ChatCompletionToolCall> ToolCalls { get; set; } = [];
}
@@ -14,15 +14,25 @@ public record ChatCompletionAPIRequest(
bool Stream
)
{
//
// The properties of the parameters, declared again to be settable. The source generation of System.Text.Json
// sets every init-only property while it creates the object, also one missing in the JSON, so its value from
// the parameterless constructor would get lost. Keep them first and in the order of the parameters, which is
// the order they get written in:
//
public string Model { get; set; } = Model;
public IList<IMessageBase> Messages { get; set; } = Messages;
public bool Stream { get; set; } = Stream;
public ChatCompletionAPIRequest() : this(string.Empty, [], true)
{
}
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public IList<object>? Tools { get; init; }
public IList<object>? Tools { get; set; }
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public bool? ParallelToolCalls { get; init; }
public bool? ParallelToolCalls { get; set; }
/// <summary>
/// Asks a streamed request to end with what it cost.
@@ -35,7 +45,10 @@ public record ChatCompletionAPIRequest(
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public ChatCompletionStreamOptions? StreamOptions => this.Stream ? ChatCompletionStreamOptions.INCLUDE_USAGE : null;
// Attention: The "required" modifier is not supported for [JsonExtensionData].
//
// Attention: The "required" modifier is not supported for [JsonExtensionData], and neither is init: the source
// generation of System.Text.Json refuses to write the request at all then.
//
[JsonExtensionData]
public IDictionary<string, object> AdditionalApiParameters { get; init; } = new Dictionary<string, object>();
public IDictionary<string, object> AdditionalApiParameters { get; set; } = new Dictionary<string, object>();
}
@@ -7,6 +7,14 @@ namespace AIStudio.Provider.OpenAI;
/// <param name="Delta">The delta text of the choice.</param>
public record ChatCompletionChoice(int Index, ChatCompletionDelta Delta)
{
//
// The properties of the parameters, declared again to be settable. The source generation of System.Text.Json
// sets every init-only property while it creates the object, also one missing in the JSON, so its value from
// the parameterless constructor would get lost. Keep them first and in the order of the parameters:
//
public int Index { get; set; } = Index;
public ChatCompletionDelta Delta { get; set; } = Delta;
public ChatCompletionChoice() : this(0, new())
{
}
@@ -9,7 +9,7 @@ namespace AIStudio.Provider.OpenAI;
public sealed record ChatCompletionDelta
{
[JsonPropertyName("content")]
public JsonElement? RawContent { get; init; }
public JsonElement? RawContent { get; set; }
[JsonIgnore]
public string Content => ChatCompletionContent.GetText(this.RawContent) ?? string.Empty;
@@ -11,6 +11,19 @@ namespace AIStudio.Provider.OpenAI;
/// <param name="Choices">The choices made by the AI.</param>
public record ChatCompletionDeltaStreamLine(string Id, string Object, uint Created, string Model, string SystemFingerprint, IList<ChatCompletionChoice> Choices) : IResponseStreamLine
{
//
// The properties of the parameters, declared again to be settable. The source generation of System.Text.Json
// sets every init-only property while it creates the object, also one missing in the JSON, so its value from
// the parameterless constructor would get lost, e.g., the empty list of choices of the line which carries the
// usage. Keep them first and in the order of the parameters:
//
public string Id { get; set; } = Id;
public string Object { get; set; } = Object;
public uint Created { get; set; } = Created;
public string Model { get; set; } = Model;
public string SystemFingerprint { get; set; } = SystemFingerprint;
public IList<ChatCompletionChoice> Choices { get; set; } = Choices;
public ChatCompletionDeltaStreamLine() : this(string.Empty, string.Empty, 0, string.Empty, string.Empty, [])
{
}
@@ -25,7 +38,7 @@ public record ChatCompletionDeltaStreamLine(string Id, string Object, uint Creat
/// line of its own which carries no choices -- which is why the usage is read apart from the
/// content rather than next to it.
/// </remarks>
public ChatCompletionUsage? Usage { get; init; }
public ChatCompletionUsage? Usage { get; set; }
/// <inheritdoc />
public bool ContainsContent() => this.Choices.Count > 0;
@@ -0,0 +1,17 @@
using System.Text.Json;
namespace AIStudio.Provider.OpenAI;
/// <summary>
/// The function a tool of a Chat Completions compatible API offers to the model.
/// </summary>
public sealed record ChatCompletionFunctionDeclaration
{
public string Name { get; set; } = string.Empty;
public string Description { get; set; } = string.Empty;
public JsonElement Parameters { get; set; }
public bool Strict { get; set; }
}
@@ -0,0 +1,11 @@
namespace AIStudio.Provider.OpenAI;
/// <summary>
/// The function tool definition shape expected by Chat Completions compatible APIs, with the function nested.
/// </summary>
public sealed record ChatCompletionFunctionTool
{
public string Type { get; set; } = "function";
public ChatCompletionFunctionDeclaration Function { get; set; } = new();
}
@@ -16,7 +16,7 @@ public sealed record ChatCompletionStreamDelta
/// The content as it arrived: a string for most providers, a list of parts for some.
/// </summary>
[JsonPropertyName("content")]
public JsonElement? RawContent { get; init; }
public JsonElement? RawContent { get; set; }
/// <summary>
/// The text of this fragment, whichever shape it arrived in.
@@ -27,10 +27,10 @@ public sealed record ChatCompletionStreamDelta
/// <summary>
/// The reasoning text some providers stream next to the answer.
/// </summary>
public string? ReasoningContent { get; init; }
public string? ReasoningContent { get; set; }
/// <summary>
/// The fragments of the tool calls the model is asking for.
/// </summary>
public IList<ChatCompletionToolCallDelta?>? ToolCalls { get; init; }
public IList<ChatCompletionToolCallDelta?>? ToolCalls { get; set; }
}
@@ -5,12 +5,12 @@ namespace AIStudio.Provider.OpenAI;
public sealed record ChatCompletionToolCall
{
public string? Id { get; init; }
public string? Id { get; set; }
public string? Type { get; init; } = "function";
public string? Type { get; set; } = "function";
public ChatCompletionToolFunction? Function { get; init; }
public ChatCompletionToolFunction? Function { get; set; }
[JsonExtensionData]
public IDictionary<string, JsonElement> AdditionalMetadata { get; init; } = new Dictionary<string, JsonElement>();
public IDictionary<string, JsonElement> AdditionalMetadata { get; set; } = new Dictionary<string, JsonElement>();
}
@@ -117,7 +117,7 @@ public sealed class ChatCompletionToolCallAccumulator(Func<ServerSentEvent, ILis
// No text means no content field, the way a round which only calls a tool arrives
// when it is not streamed. Some providers reject an empty string in its place.
//
RawContent = answer.Length is 0 ? null : JsonSerializer.SerializeToElement(answer),
RawContent = answer.Length is 0 ? null : JsonSerializer.SerializeToElement(answer, ProviderJsonOptions.OPTIONS),
ReasoningContent = this.reasoning.Length is 0 ? null : this.reasoning.ToString(),
ToolCalls = this.toolCalls.Count is 0
? null
@@ -2,7 +2,7 @@ namespace AIStudio.Provider.OpenAI;
public sealed record ChatCompletionToolFunction
{
public string? Name { get; init; }
public string? Name { get; set; }
public string? Arguments { get; init; }
public string? Arguments { get; set; }
}
@@ -21,5 +21,5 @@ public sealed record ChatCompletionToolStreamLine(string? Id, IList<ChatCompleti
/// ChatCompletionAPIRequest asks for it, the requests of the tool rounds included. Not a
/// positional parameter, because nobody but the serializer ever builds this line.
/// </remarks>
public ChatCompletionUsage? Usage { get; init; }
public ChatCompletionUsage? Usage { get; set; }
}
@@ -17,7 +17,7 @@ public sealed record ChatCompletionUsage
/// <summary>
/// What everything sent to the model cost.
/// </summary>
public int? PromptTokens { get; init; }
public int? PromptTokens { get; set; }
/// <summary>
/// States what this block reports, as far as it can be believed.
@@ -8,5 +8,5 @@ public interface ISubContent
/// <summary>
/// The type of the sub-content.
/// </summary>
public SubContentType Type { get; init; }
public SubContentType Type { get; set; }
}
@@ -15,5 +15,5 @@ public interface ISubContentImageUrl
/// <summary>
/// The URL or base64-encoded data URI of the image.
/// </summary>
public string Url { get; init; }
public string Url { get; set; }
}
@@ -7,6 +7,15 @@ namespace AIStudio.Provider.OpenAI;
/// <param name="Role">The role of the message.</param>
public record MultimodalMessage(List<ISubContent> Content, string Role) : IMessage<List<ISubContent>>
{
//
// The properties of the parameters, declared again to be settable. The source generation of System.Text.Json
// sets every init-only property while it creates the object, also one missing in the JSON, so its value from
// the parameterless constructor would get lost. Keep them first and in the order of the parameters, which is
// the order they get written in:
//
public List<ISubContent> Content { get; set; } = Content;
public string Role { get; set; } = Role;
public MultimodalMessage() : this([], string.Empty)
{
}
@@ -59,7 +59,7 @@ public static class OpenAIStrictToolSchema
// schema stays stable across requests, which prompt caching depends on.
//
schema["required"] = new JsonArray([..properties.Select(property => JsonValue.Create(property.Key))]);
return JsonSerializer.Deserialize<JsonElement>(schema.ToJsonString());
return JsonSerializer.Deserialize<JsonElement>(schema.ToJsonString(), ProviderJsonOptions.OPTIONS);
}
private static void AllowNullType(JsonObject property)
@@ -17,11 +17,26 @@ public record ResponsesAPIRequest(
bool Store,
IList<object> Tools)
{
//
// The properties of the parameters, declared again to be settable. The source generation of System.Text.Json
// sets every init-only property while it creates the object, also one missing in the JSON, so its value from
// the parameterless constructor would get lost. Keep them first and in the order of the parameters, which is
// the order they get written in:
//
public string Model { get; set; } = Model;
public IList<object> Input { get; set; } = Input;
public bool Stream { get; set; } = Stream;
public bool Store { get; set; } = Store;
public IList<object> Tools { get; set; } = Tools;
public ResponsesAPIRequest() : this(string.Empty, [], true, false, [])
{
}
// Attention: The "required" modifier is not supported for [JsonExtensionData].
//
// Attention: The "required" modifier is not supported for [JsonExtensionData], and neither is init: the source
// generation of System.Text.Json refuses to write the request at all then.
//
[JsonExtensionData]
public IDictionary<string, object> AdditionalApiParameters { get; init; } = new Dictionary<string, object>();
public IDictionary<string, object> AdditionalApiParameters { get; set; } = new Dictionary<string, object>();
}
@@ -5,9 +5,9 @@ namespace AIStudio.Provider.OpenAI;
/// </summary>
public sealed record ResponsesFunctionCallOutputItem
{
public string Type { get; init; } = "function_call_output";
public string Type { get; set; } = "function_call_output";
public string CallId { get; init; } = string.Empty;
public string CallId { get; set; } = string.Empty;
public string Output { get; init; } = string.Empty;
public string Output { get; set; } = string.Empty;
}
@@ -7,13 +7,13 @@ namespace AIStudio.Provider.OpenAI;
/// </summary>
public sealed record ResponsesFunctionTool
{
public string Type { get; init; } = "function";
public string Type { get; set; } = "function";
public string Name { get; init; } = string.Empty;
public string Name { get; set; } = string.Empty;
public string Description { get; init; } = string.Empty;
public string Description { get; set; } = string.Empty;
public JsonElement Parameters { get; init; }
public JsonElement Parameters { get; set; }
public bool Strict { get; init; }
public bool Strict { get; set; }
}
@@ -17,18 +17,18 @@ public sealed record ResponsesResponse
/// </remarks>
private static readonly HashSet<string> OUTPUT_ITEMS_LEAVING_THE_INPUT_ALONE = ["message", "reasoning", "function_call"];
public string Id { get; init; } = string.Empty;
public string Id { get; set; } = string.Empty;
public string Model { get; init; } = string.Empty;
public string Model { get; set; } = string.Empty;
public string? OutputText { get; init; }
public string? OutputText { get; set; }
public IList<JsonElement> Output { get; init; } = [];
public IList<JsonElement> Output { get; set; } = [];
/// <summary>
/// What OpenAI says the response cost. Only the completed response carries it.
/// </summary>
public ResponsesUsage? Usage { get; init; }
public ResponsesUsage? Usage { get; set; }
/// <summary>
/// States what the request of this response carried, as far as it can be believed.
@@ -17,7 +17,7 @@ public sealed record ResponsesUsage
/// <summary>
/// What everything sent to the model cost, the cached part included.
/// </summary>
public int? InputTokens { get; init; }
public int? InputTokens { get; set; }
/// <summary>
/// States what this block reports, as far as it can be believed.
@@ -5,6 +5,15 @@ namespace AIStudio.Provider.OpenAI;
/// </summary>
public record SubContentImageUrl(SubContentType Type, string ImageUrl) : ISubContent
{
//
// The properties of the parameters, declared again to be settable. The source generation of System.Text.Json
// sets every init-only property while it creates the object, also one missing in the JSON, so its value from
// the parameterless constructor would get lost. Keep them first and in the order of the parameters, which is
// the order they get written in:
//
public SubContentType Type { get; set; } = Type;
public string ImageUrl { get; set; } = ImageUrl;
public SubContentImageUrl() : this(SubContentType.IMAGE_URL, string.Empty)
{
}
@@ -13,5 +13,5 @@ namespace AIStudio.Provider.OpenAI;
public record SubContentImageUrlData : ISubContentImageUrl
{
/// <inheritdoc />
public string Url { get; init; } = string.Empty;
public string Url { get; set; } = string.Empty;
}
@@ -12,6 +12,15 @@ namespace AIStudio.Provider.OpenAI;
/// </remarks>
public record SubContentImageUrlNested(SubContentType Type, ISubContentImageUrl ImageUrl) : ISubContent
{
//
// The properties of the parameters, declared again to be settable. The source generation of System.Text.Json
// sets every init-only property while it creates the object, also one missing in the JSON, so its value from
// the parameterless constructor would get lost. Keep them first and in the order of the parameters, which is
// the order they get written in:
//
public SubContentType Type { get; set; } = Type;
public ISubContentImageUrl ImageUrl { get; set; } = ImageUrl;
public SubContentImageUrlNested() : this(SubContentType.IMAGE_URL, new SubContentImageUrlData())
{
}
@@ -8,6 +8,15 @@ namespace AIStudio.Provider.OpenAI;
/// </remarks>
public record SubContentInputImage(SubContentType Type, string ImageUrl) : ISubContent
{
//
// The properties of the parameters, declared again to be settable. The source generation of System.Text.Json
// sets every init-only property while it creates the object, also one missing in the JSON, so its value from
// the parameterless constructor would get lost. Keep them first and in the order of the parameters, which is
// the order they get written in:
//
public SubContentType Type { get; set; } = Type;
public string ImageUrl { get; set; } = ImageUrl;
public SubContentInputImage() : this(SubContentType.INPUT_IMAGE, string.Empty)
{
}
@@ -8,6 +8,15 @@ namespace AIStudio.Provider.OpenAI;
/// </remarks>
public record SubContentInputText(SubContentType Type, string Text) : ISubContent
{
//
// The properties of the parameters, declared again to be settable. The source generation of System.Text.Json
// sets every init-only property while it creates the object, also one missing in the JSON, so its value from
// the parameterless constructor would get lost. Keep them first and in the order of the parameters, which is
// the order they get written in:
//
public SubContentType Type { get; set; } = Type;
public string Text { get; set; } = Text;
public SubContentInputText() : this(SubContentType.INPUT_TEXT, string.Empty)
{
}
@@ -5,6 +5,15 @@ namespace AIStudio.Provider.OpenAI;
/// </summary>
public record SubContentText(SubContentType Type, string Text) : ISubContent
{
//
// The properties of the parameters, declared again to be settable. The source generation of System.Text.Json
// sets every init-only property while it creates the object, also one missing in the JSON, so its value from
// the parameterless constructor would get lost. Keep them first and in the order of the parameters, which is
// the order they get written in:
//
public SubContentType Type { get; set; } = Type;
public string Text { get; set; } = Text;
public SubContentText() : this(SubContentType.TEXT, string.Empty)
{
}
@@ -7,6 +7,15 @@ namespace AIStudio.Provider.OpenAI;
/// <param name="Role">The role of the message.</param>
public record TextMessage(string Content, string Role) : IMessage<string>
{
//
// The properties of the parameters, declared again to be settable. The source generation of System.Text.Json
// sets every init-only property while it creates the object, also one missing in the JSON, so its value from
// the parameterless constructor would get lost. Keep them first and in the order of the parameters, which is
// the order they get written in:
//
public string Content { get; set; } = Content;
public string Role { get; set; } = Role;
public TextMessage() : this(string.Empty, string.Empty)
{
}
@@ -2,9 +2,9 @@ namespace AIStudio.Provider.OpenAI;
public sealed record ToolResultMessage : IMessage<string>
{
public string Role { get; init; } = "tool";
public string Role { get; set; } = "tool";
public string Content { get; init; } = string.Empty;
public string Content { get; set; } = string.Empty;
public string ToolCallId { get; init; } = string.Empty;
public string ToolCallId { get; set; } = string.Empty;
}