Skip to content

Commit 72a14c3

Browse files
committed
GetResponseInputItems
1 parent b91fb1d commit 72a14c3

File tree

4 files changed

+329
-0
lines changed

4 files changed

+329
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
namespace OpenAI.Responses;
2+
3+
public struct GetResponseInputItemsOptions
4+
{
5+
public GetResponseInputItemsOptions(string responseId)
6+
{
7+
ResponseId = responseId;
8+
}
9+
10+
public string ResponseId { get; set; }
11+
12+
public string After { get; set; }
13+
14+
public string Before { get; set; }
15+
16+
public int? Limit { get; set; }
17+
18+
public string Order { get; set; }
19+
20+
}

src/Custom/Responses/OpenAIResponseClient.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,26 @@ public virtual ClientResult<OpenAIResponse> CancelResponse(string responseId, Ca
411411
return ClientResult.FromValue(convenienceResult, protocolResult.GetRawResponse());
412412
}
413413

414+
public virtual ClientResult<ResponseItemList> GetResponseInputItems(GetResponseInputItemsOptions options = default, CancellationToken cancellationToken = default)
415+
{
416+
Argument.AssertNotNull(options, nameof(options));
417+
Argument.AssertNotNullOrEmpty(options.ResponseId, nameof(options.ResponseId));
418+
419+
PipelineMessage message = CreateGetResponseInputItemsRequest(options.ResponseId, options.Limit, options.After, options.Order, options.Before, cancellationToken.ToRequestOptions());
420+
ClientResult result = ClientResult.FromResponse(Pipeline.ProcessMessage(message, cancellationToken.ToRequestOptions()));
421+
return ClientResult.FromValue((ResponseItemList)result, result.GetRawResponse());
422+
}
423+
424+
public virtual async Task<ClientResult> GetResponseInputItemsAsync(GetResponseInputItemsOptions options = default, CancellationToken cancellationToken = default)
425+
{
426+
Argument.AssertNotNull(options, nameof(options));
427+
Argument.AssertNotNullOrEmpty(options.ResponseId, nameof(options.ResponseId));
428+
429+
PipelineMessage message = CreateGetResponseInputItemsRequest(options.ResponseId, options.Limit, options.After, options.Order, options.Before, cancellationToken.ToRequestOptions());
430+
ClientResult result = ClientResult.FromResponse(await Pipeline.ProcessMessageAsync(message, cancellationToken.ToRequestOptions()).ConfigureAwait(false));
431+
return ClientResult.FromValue((ResponseItemList)result, result.GetRawResponse());
432+
}
433+
414434
internal virtual ResponseCreationOptions CreatePerCallOptions(ResponseCreationOptions userOptions, IEnumerable<ResponseItem> inputItems, bool stream = false)
415435
{
416436
ResponseCreationOptions copiedOptions = userOptions is null
Lines changed: 238 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,238 @@
1+
using System;
2+
using System.ClientModel;
3+
using System.ClientModel.Primitives;
4+
using System.Collections.Generic;
5+
using System.Text;
6+
using System.Text.Json;
7+
8+
namespace OpenAI.Responses
9+
{
10+
public partial class ResponseItemList : IJsonModel<ResponseItemList>
11+
{
12+
internal ResponseItemList() : this(null, null, default, null, null, default)
13+
{
14+
}
15+
16+
void IJsonModel<ResponseItemList>.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options)
17+
{
18+
#pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
19+
if (Patch.Contains("$"u8))
20+
{
21+
writer.WriteRawValue(Patch.GetJson("$"u8));
22+
return;
23+
}
24+
#pragma warning restore SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
25+
26+
writer.WriteStartObject();
27+
JsonModelWriteCore(writer, options);
28+
writer.WriteEndObject();
29+
}
30+
31+
protected virtual void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options)
32+
{
33+
string format = options.Format == "W" ? ((IPersistableModel<ResponseItemList>)this).GetFormatFromOptions(options) : options.Format;
34+
if (format != "J")
35+
{
36+
throw new FormatException($"The model {nameof(ResponseItemList)} does not support writing '{format}' format.");
37+
}
38+
#pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
39+
if (!Patch.Contains("$.object"u8))
40+
{
41+
writer.WritePropertyName("object"u8);
42+
writer.WriteStringValue(Object);
43+
}
44+
if (Patch.Contains("$.data"u8))
45+
{
46+
if (!Patch.IsRemoved("$.data"u8))
47+
{
48+
writer.WritePropertyName("data"u8);
49+
writer.WriteRawValue(Patch.GetJson("$.data"u8));
50+
}
51+
}
52+
else
53+
{
54+
writer.WritePropertyName("data"u8);
55+
writer.WriteStartArray();
56+
for (int i = 0; i < Data.Count; i++)
57+
{
58+
if (Data[i].Patch.IsRemoved("$"u8))
59+
{
60+
continue;
61+
}
62+
writer.WriteObjectValue(Data[i], options);
63+
}
64+
Patch.WriteTo(writer, "$.data"u8);
65+
writer.WriteEndArray();
66+
}
67+
if (!Patch.Contains("$.has_more"u8))
68+
{
69+
writer.WritePropertyName("has_more"u8);
70+
writer.WriteBooleanValue(HasMore);
71+
}
72+
if (!Patch.Contains("$.first_id"u8))
73+
{
74+
writer.WritePropertyName("first_id"u8);
75+
writer.WriteStringValue(FirstId);
76+
}
77+
if (!Patch.Contains("$.last_id"u8))
78+
{
79+
writer.WritePropertyName("last_id"u8);
80+
writer.WriteStringValue(LastId);
81+
}
82+
83+
Patch.WriteTo(writer);
84+
#pragma warning restore SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
85+
}
86+
87+
ResponseItemList IJsonModel<ResponseItemList>.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) => JsonModelCreateCore(ref reader, options);
88+
89+
protected virtual ResponseItemList JsonModelCreateCore(ref Utf8JsonReader reader, ModelReaderWriterOptions options)
90+
{
91+
string format = options.Format == "W" ? ((IPersistableModel<ResponseItemList>)this).GetFormatFromOptions(options) : options.Format;
92+
if (format != "J")
93+
{
94+
throw new FormatException($"The model {nameof(ResponseItemList)} does not support reading '{format}' format.");
95+
}
96+
using JsonDocument document = JsonDocument.ParseValue(ref reader);
97+
return DeserializeResponseItemList(document.RootElement, null, options);
98+
}
99+
100+
internal static ResponseItemList DeserializeResponseItemList(JsonElement element, BinaryData data, ModelReaderWriterOptions options)
101+
{
102+
if (element.ValueKind == JsonValueKind.Null)
103+
{
104+
return null;
105+
}
106+
string @object = default;
107+
IList<ResponseItem> data0 = default;
108+
bool hasMore = default;
109+
string firstId = default;
110+
string lastId = default;
111+
#pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
112+
JsonPatch patch = new JsonPatch(data is null ? ReadOnlyMemory<byte>.Empty : data.ToMemory());
113+
#pragma warning restore SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
114+
foreach (var prop in element.EnumerateObject())
115+
{
116+
if (prop.NameEquals("object"u8))
117+
{
118+
@object = prop.Value.GetString();
119+
continue;
120+
}
121+
if (prop.NameEquals("data"u8))
122+
{
123+
List<ResponseItem> array = new List<ResponseItem>();
124+
foreach (var item in prop.Value.EnumerateArray())
125+
{
126+
array.Add(ResponseItem.DeserializeResponseItem(item, item.GetUtf8Bytes(), options));
127+
}
128+
data0 = array;
129+
continue;
130+
}
131+
if (prop.NameEquals("has_more"u8))
132+
{
133+
hasMore = prop.Value.GetBoolean();
134+
continue;
135+
}
136+
if (prop.NameEquals("first_id"u8))
137+
{
138+
firstId = prop.Value.GetString();
139+
continue;
140+
}
141+
if (prop.NameEquals("last_id"u8))
142+
{
143+
lastId = prop.Value.GetString();
144+
continue;
145+
}
146+
patch.Set([.. "$."u8, .. Encoding.UTF8.GetBytes(prop.Name)], prop.Value.GetUtf8Bytes());
147+
}
148+
return new ResponseItemList(
149+
@object,
150+
data0,
151+
hasMore,
152+
firstId,
153+
lastId,
154+
patch);
155+
}
156+
157+
BinaryData IPersistableModel<ResponseItemList>.Write(ModelReaderWriterOptions options) => PersistableModelWriteCore(options);
158+
159+
protected virtual BinaryData PersistableModelWriteCore(ModelReaderWriterOptions options)
160+
{
161+
string format = options.Format == "W" ? ((IPersistableModel<ResponseItemList>)this).GetFormatFromOptions(options) : options.Format;
162+
switch (format)
163+
{
164+
case "J":
165+
return ModelReaderWriter.Write(this, options, OpenAIContext.Default);
166+
default:
167+
throw new FormatException($"The model {nameof(ResponseItemList)} does not support writing '{options.Format}' format.");
168+
}
169+
}
170+
171+
ResponseItemList IPersistableModel<ResponseItemList>.Create(BinaryData data, ModelReaderWriterOptions options) => PersistableModelCreateCore(data, options);
172+
173+
protected virtual ResponseItemList PersistableModelCreateCore(BinaryData data, ModelReaderWriterOptions options)
174+
{
175+
string format = options.Format == "W" ? ((IPersistableModel<ResponseItemList>)this).GetFormatFromOptions(options) : options.Format;
176+
switch (format)
177+
{
178+
case "J":
179+
using (JsonDocument document = JsonDocument.Parse(data))
180+
{
181+
return DeserializeResponseItemList(document.RootElement, data, options);
182+
}
183+
default:
184+
throw new FormatException($"The model {nameof(ResponseItemList)} does not support reading '{options.Format}' format.");
185+
}
186+
}
187+
188+
string IPersistableModel<ResponseItemList>.GetFormatFromOptions(ModelReaderWriterOptions options) => "J";
189+
190+
public static explicit operator ResponseItemList(ClientResult result)
191+
{
192+
using PipelineResponse response = result.GetRawResponse();
193+
BinaryData data = response.Content;
194+
using JsonDocument document = JsonDocument.Parse(data);
195+
return DeserializeResponseItemList(document.RootElement, data, ModelSerializationExtensions.WireOptions);
196+
}
197+
198+
#pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
199+
private bool PropagateGet(ReadOnlySpan<byte> jsonPath, out JsonPatch.EncodedValue value)
200+
{
201+
ReadOnlySpan<byte> local = jsonPath.SliceToStartOfPropertyName();
202+
value = default;
203+
204+
if (local.StartsWith("data"u8))
205+
{
206+
int propertyLength = "data"u8.Length;
207+
ReadOnlySpan<byte> currentSlice = local.Slice(propertyLength);
208+
if (!currentSlice.TryGetIndex(out int index, out int bytesConsumed))
209+
{
210+
return false;
211+
}
212+
return Data[index].Patch.TryGetEncodedValue([.. "$"u8, .. currentSlice.Slice(bytesConsumed)], out value);
213+
}
214+
return false;
215+
}
216+
#pragma warning restore SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
217+
218+
#pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
219+
private bool PropagateSet(ReadOnlySpan<byte> jsonPath, JsonPatch.EncodedValue value)
220+
{
221+
ReadOnlySpan<byte> local = jsonPath.SliceToStartOfPropertyName();
222+
223+
if (local.StartsWith("data"u8))
224+
{
225+
int propertyLength = "data"u8.Length;
226+
ReadOnlySpan<byte> currentSlice = local.Slice(propertyLength);
227+
if (!currentSlice.TryGetIndex(out int index, out int bytesConsumed))
228+
{
229+
return false;
230+
}
231+
Data[index].Patch.Set([.. "$"u8, .. currentSlice.Slice(bytesConsumed)], value);
232+
return true;
233+
}
234+
return false;
235+
}
236+
#pragma warning restore SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
237+
}
238+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System.ClientModel.Primitives;
2+
using System.Collections.Generic;
3+
using System.ComponentModel;
4+
using System.Diagnostics.CodeAnalysis;
5+
using System.Linq;
6+
7+
namespace OpenAI.Responses
8+
{
9+
[Experimental("OPENAI001")]
10+
public partial class ResponseItemList
11+
{
12+
[Experimental("SCME0001")]
13+
private JsonPatch _patch;
14+
15+
internal ResponseItemList(IEnumerable<ResponseItem> data, bool hasMore, string firstId, string lastId)
16+
{
17+
Data = data.ToList();
18+
HasMore = hasMore;
19+
FirstId = firstId;
20+
LastId = lastId;
21+
}
22+
23+
#pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
24+
internal ResponseItemList(string @object, IList<ResponseItem> data, bool hasMore, string firstId, string lastId, in JsonPatch patch)
25+
{
26+
// Plugin customization: ensure initialization of collections
27+
Object = @object;
28+
Data = data ?? new ChangeTrackingList<ResponseItem>();
29+
HasMore = hasMore;
30+
FirstId = firstId;
31+
LastId = lastId;
32+
_patch = patch;
33+
_patch.SetPropagators(PropagateSet, PropagateGet);
34+
}
35+
#pragma warning restore SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
36+
37+
[EditorBrowsable(EditorBrowsableState.Never)]
38+
[Experimental("SCME0001")]
39+
public ref JsonPatch Patch => ref _patch;
40+
41+
public string Object { get; } = "list";
42+
43+
public IList<ResponseItem> Data { get; }
44+
45+
public bool HasMore { get; }
46+
47+
public string FirstId { get; }
48+
49+
public string LastId { get; }
50+
}
51+
}

0 commit comments

Comments
 (0)