AI-Studio/app/MindWork AI Studio/Provider/OpenAI/ResponsesResponse.cs
Peer Schütt 0b2ce886c5 Key fixes include:
Prevented OS credentials from reaching public hosts or cross-origin redirects.
Preserved Responses API reasoning items across tool rounds.
Fixed nullable tool-call handling and omitted unsupported null request properties.
Propagated cancellation correctly.
Removed tool argument values from logs and tool results from persisted traces.
Added settings validation, clearable optional enums, managed-confidence validation, and safe fallback behavior.
Added tool-definition schema and duplicate validation.
Fixed documentation JSON, changelog regressions, typos, trailing whitespace, and unused Anthropic code.
2026-07-15 11:31:11 +02:00

63 lines
2.2 KiB
C#

using System.Text.Json;
namespace AIStudio.Provider.OpenAI;
/// <summary>
/// Non-streaming OpenAI Responses API result used during local tool execution.
/// </summary>
public sealed record ResponsesResponse
{
public string Id { get; init; } = string.Empty;
public string Model { get; init; } = string.Empty;
public string? OutputText { get; init; }
public IList<JsonElement> Output { get; init; } = [];
public IReadOnlyList<ResponsesFunctionCallItem> GetFunctionCalls() => this.Output
.Where(x => ReadString(x, "type").Equals("function_call", StringComparison.Ordinal))
.Select(x => new ResponsesFunctionCallItem
{
Type = ReadString(x, "type"),
CallId = ReadString(x, "call_id"),
Name = ReadString(x, "name"),
Arguments = ReadString(x, "arguments"),
})
.Where(x => !string.IsNullOrWhiteSpace(x.CallId) && !string.IsNullOrWhiteSpace(x.Name))
.ToList();
public string GetTextOutput()
{
if (!string.IsNullOrWhiteSpace(this.OutputText))
return this.OutputText;
return string.Concat(this.Output
.Where(x => ReadString(x, "type").Equals("message", StringComparison.Ordinal))
.SelectMany(ReadContentItems)
.Where(x => ReadString(x, "type").Equals("output_text", StringComparison.Ordinal))
.Select(x => ReadString(x, "text")));
}
private static IEnumerable<JsonElement> ReadContentItems(JsonElement outputItem)
{
if (outputItem.ValueKind is not JsonValueKind.Object ||
!outputItem.TryGetProperty("content", out var content) ||
content.ValueKind is not JsonValueKind.Array)
yield break;
foreach (var contentItem in content.EnumerateArray())
yield return contentItem;
}
private static string ReadString(JsonElement item, string propertyName)
{
if (item.ValueKind is not JsonValueKind.Object ||
!item.TryGetProperty(propertyName, out var property) ||
property.ValueKind is not JsonValueKind.String)
return string.Empty;
return property.GetString() ?? string.Empty;
}
}