Skip to content

Commit c0db273

Browse files
committed
Fix custom code
1 parent d66058c commit c0db273

File tree

62 files changed

+439
-450
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+439
-450
lines changed

specification/base/typespec/responses/custom.tsp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,10 @@ union CodeInterpreterContainerConfigurationType {
4646
model CodeInterpreterContainerConfiguration {
4747
@doc("The type of the output.")
4848
type: CodeInterpreterContainerConfigurationType;
49-
}
49+
}
50+
51+
model DeleteResponseResponse {
52+
id: string;
53+
object: "response";
54+
deleted: true;
55+
};

specification/base/typespec/responses/operations.tsp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,7 @@ interface Responses {
5656
@path
5757
@example("resp_677efb5139a88190b512bc3fef8e535d")
5858
response_id: string,
59-
): {
60-
id: string;
61-
object: "response";
62-
deleted: true;
63-
} | ResponseErrorResponse;
59+
): DeleteResponseResponse | ResponseErrorResponse;
6460

6561
@post
6662
@route("{response_id}/cancel")

specification/client/responses.client.tsp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ using TypeSpec.HttpClient.CSharp;
214214
@@dynamicModel(LogProb);
215215
// custom.tsp models
216216
@@dynamicModel(ResponseErrorResponse);
217+
@@dynamicModel(DeleteResponseResponse);
217218
@@dynamicModel(ReasoningItemSummaryPart);
218219
@@dynamicModel(ReasoningItemSummaryTextPart);
219220
@@dynamicModel(CodeInterpreterToolOutput);

src/Custom/Chat/ChatClient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public partial class ChatClient
2323
{
2424
private readonly string _model;
2525
private readonly OpenTelemetrySource _telemetry;
26-
private static readonly InternalChatCompletionStreamOptions s_includeUsageStreamOptions = new(includeUsage: true, additionalBinaryDataProperties: null);
26+
private static readonly InternalChatCompletionStreamOptions s_includeUsageStreamOptions = new(includeUsage: true, patch: default);
2727

2828
// CUSTOM: Added as a convenience.
2929
/// <summary> Initializes a new instance of <see cref="ChatClient"/>. </summary>

src/Custom/Chat/ChatFunctionChoice.Serialization.cs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.ClientModel.Primitives;
33
using System.Collections.Generic;
4+
using System.Text;
45
using System.Text.Json;
56

67
namespace OpenAI.Chat;
@@ -13,6 +14,14 @@ void IJsonModel<ChatFunctionChoice>.Write(Utf8JsonWriter writer, ModelReaderWrit
1314

1415
internal static void SerializeChatFunctionChoice(ChatFunctionChoice instance, Utf8JsonWriter writer, ModelReaderWriterOptions options)
1516
{
17+
#pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
18+
if (instance.Patch.Contains("$"u8))
19+
{
20+
writer.WriteRawValue(instance.Patch.GetJson("$"u8));
21+
return;
22+
}
23+
#pragma warning restore SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
24+
1625
if (instance._isPlainString)
1726
{
1827
writer.WriteStringValue(instance._string);
@@ -22,12 +31,14 @@ internal static void SerializeChatFunctionChoice(ChatFunctionChoice instance, Ut
2231
writer.WriteStartObject();
2332
writer.WritePropertyName("name"u8);
2433
writer.WriteStringValue(instance._function.Name);
25-
writer.WriteSerializedAdditionalRawData(instance._additionalBinaryDataProperties, options);
34+
#pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
35+
instance.Patch.WriteTo(writer);
36+
#pragma warning restore SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
2637
writer.WriteEndObject();
2738
}
2839
}
2940

30-
internal static ChatFunctionChoice DeserializeChatFunctionChoice(JsonElement element, ModelReaderWriterOptions options = null)
41+
internal static ChatFunctionChoice DeserializeChatFunctionChoice(JsonElement element, BinaryData data, ModelReaderWriterOptions options = null)
3142
{
3243
options ??= ModelSerializationExtensions.WireOptions;
3344

@@ -42,8 +53,9 @@ internal static ChatFunctionChoice DeserializeChatFunctionChoice(JsonElement ele
4253
else
4354
{
4455
string name = default;
45-
IDictionary<string, BinaryData> serializedAdditionalRawData = default;
46-
Dictionary<string, BinaryData> rawDataDictionary = new Dictionary<string, BinaryData>();
56+
#pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
57+
JsonPatch patch = new JsonPatch(data is null ? ReadOnlyMemory<byte>.Empty : data.ToMemory());
58+
#pragma warning restore SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
4759
foreach (var property in element.EnumerateObject())
4860
{
4961
if (property.NameEquals("name"u8))
@@ -53,11 +65,10 @@ internal static ChatFunctionChoice DeserializeChatFunctionChoice(JsonElement ele
5365
}
5466
if (true)
5567
{
56-
rawDataDictionary.Add(property.Name, BinaryData.FromString(property.Value.GetRawText()));
68+
patch.Set([.. "$."u8, .. Encoding.UTF8.GetBytes(property.Name)], property.Value.GetUtf8Bytes());
5769
}
5870
}
59-
serializedAdditionalRawData = rawDataDictionary;
60-
return new ChatFunctionChoice(name, serializedAdditionalRawData);
71+
return new ChatFunctionChoice(name, patch);
6172
}
6273
}
6374
}

src/Custom/Chat/ChatFunctionChoice.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.ClientModel.Primitives;
23
using System.Collections.Generic;
34

45
namespace OpenAI.Chat;
@@ -35,16 +36,18 @@ internal ChatFunctionChoice(string predefinedFunctionChoice)
3536
// CUSTOM: Added the function name parameter to the constructor that takes additional data to handle the object representation.
3637
/// <summary> Initializes a new instance of <see cref="ChatFunctionChoice"/>. </summary>
3738
/// <param name="functionName"> The function name. </param>
38-
/// <param name="serializedAdditionalRawData"> Keeps track of any properties unknown to the library. </param>
39-
internal ChatFunctionChoice(string functionName, IDictionary<string, BinaryData> serializedAdditionalRawData)
39+
/// <param name="patch"> Keeps track of any properties unknown to the library. </param>
40+
#pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
41+
internal ChatFunctionChoice(string functionName, in JsonPatch patch)
4042
{
4143
Argument.AssertNotNull(functionName, nameof(functionName));
4244

4345
_function = new(functionName);
4446
_isPlainString = false;
4547

46-
_additionalBinaryDataProperties = serializedAdditionalRawData;
48+
_patch = patch;
4749
}
50+
#pragma warning restore SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
4851

4952
/// <summary>
5053
/// Creates an instance of <see cref="ChatFunctionChoice"/> that specifies the model must call a specific function,
@@ -56,7 +59,7 @@ public static ChatFunctionChoice CreateNamedChoice(string functionName)
5659
{
5760
Argument.AssertNotNull(functionName, nameof(functionName));
5861

59-
return new(functionName, serializedAdditionalRawData: null);
62+
return new(functionName, patch: default);
6063
}
6164

6265
/// <summary>

src/Custom/Chat/ChatMessageContent.Serialization.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ internal static ChatMessageContent DeserializeChatMessageContent(JsonElement ele
4242
List<ChatMessageContentPart> parts = [];
4343
foreach (JsonElement contentPartElement in element.EnumerateArray())
4444
{
45-
parts.Add(ChatMessageContentPart.DeserializeChatMessageContentPart(contentPartElement, options));
45+
parts.Add(ChatMessageContentPart.DeserializeChatMessageContentPart(contentPartElement, contentPartElement.GetUtf8Bytes(), options));
4646
}
4747
return new(parts);
4848
}

src/Custom/Chat/ChatMessageContentPart.Serialization.cs

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System;
33
using System.ClientModel.Primitives;
44
using System.Collections.Generic;
5+
using System.Text;
56
using System.Text.Json;
67

78
namespace OpenAI.Chat;
@@ -14,6 +15,14 @@ void IJsonModel<ChatMessageContentPart>.Write(Utf8JsonWriter writer, ModelReader
1415

1516
internal static void WriteCoreContentPart(ChatMessageContentPart instance, Utf8JsonWriter writer, ModelReaderWriterOptions options)
1617
{
18+
#pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
19+
if (instance.Patch.Contains("$"u8))
20+
{
21+
writer.WriteRawValue(instance.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+
1726
writer.WriteStartObject();
1827
writer.WritePropertyName("type"u8);
1928
writer.WriteStringValue(instance._kind.ToSerialString());
@@ -43,11 +52,13 @@ internal static void WriteCoreContentPart(ChatMessageContentPart instance, Utf8J
4352
writer.WritePropertyName("file"u8);
4453
writer.WriteObjectValue(instance._fileFile, options);
4554
}
46-
writer.WriteSerializedAdditionalRawData(instance._additionalBinaryDataProperties, options);
55+
#pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
56+
instance.Patch.WriteTo(writer);
57+
#pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
4758
writer.WriteEndObject();
4859
}
4960

50-
internal static ChatMessageContentPart DeserializeChatMessageContentPart(JsonElement element, ModelReaderWriterOptions options = null)
61+
internal static ChatMessageContentPart DeserializeChatMessageContentPart(JsonElement element, BinaryData data, ModelReaderWriterOptions options = null)
5162
{
5263
options ??= ModelSerializationExtensions.WireOptions;
5364

@@ -62,8 +73,9 @@ internal static ChatMessageContentPart DeserializeChatMessageContentPart(JsonEle
6273
InternalChatCompletionRequestMessageContentPartImageImageUrl imageUri = default;
6374
InternalChatCompletionRequestMessageContentPartAudioInputAudio inputAudio = default;
6475
InternalChatCompletionRequestMessageContentPartFileFile fileFile = default;
65-
IDictionary<string, BinaryData> serializedAdditionalRawData = default;
66-
Dictionary<string, BinaryData> rawDataDictionary = new Dictionary<string, BinaryData>();
76+
#pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
77+
JsonPatch patch = new JsonPatch(data is null ? ReadOnlyMemory<byte>.Empty : data.ToMemory());
78+
#pragma warning restore SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
6779
foreach (var property in element.EnumerateObject())
6880
{
6981
if (property.NameEquals("type"u8))
@@ -78,7 +90,7 @@ internal static ChatMessageContentPart DeserializeChatMessageContentPart(JsonEle
7890
}
7991
if (property.NameEquals("image_url"u8))
8092
{
81-
imageUri = InternalChatCompletionRequestMessageContentPartImageImageUrl.DeserializeInternalChatCompletionRequestMessageContentPartImageImageUrl(property.Value, options);
93+
imageUri = InternalChatCompletionRequestMessageContentPartImageImageUrl.DeserializeInternalChatCompletionRequestMessageContentPartImageImageUrl(property.Value, property.Value.GetUtf8Bytes(), options);
8294
continue;
8395
}
8496
if (property.NameEquals("refusal"u8))
@@ -89,21 +101,20 @@ internal static ChatMessageContentPart DeserializeChatMessageContentPart(JsonEle
89101
if (property.NameEquals("input_audio"u8))
90102
{
91103
inputAudio = InternalChatCompletionRequestMessageContentPartAudioInputAudio
92-
.DeserializeInternalChatCompletionRequestMessageContentPartAudioInputAudio(property.Value, options);
104+
.DeserializeInternalChatCompletionRequestMessageContentPartAudioInputAudio(property.Value, property.Value.GetUtf8Bytes(), options);
93105
continue;
94106
}
95107
if (property.NameEquals("file"u8))
96108
{
97109
fileFile = InternalChatCompletionRequestMessageContentPartFileFile
98-
.DeserializeInternalChatCompletionRequestMessageContentPartFileFile(property.Value, options);
110+
.DeserializeInternalChatCompletionRequestMessageContentPartFileFile(property.Value, property.Value.GetUtf8Bytes(), options);
99111
continue;
100112
}
101113
if (true)
102114
{
103-
rawDataDictionary.Add(property.Name, BinaryData.FromString(property.Value.GetRawText()));
115+
patch.Set([.. "$."u8, .. Encoding.UTF8.GetBytes(property.Name)], property.Value.GetUtf8Bytes());
104116
}
105117
}
106-
serializedAdditionalRawData = rawDataDictionary;
107-
return new ChatMessageContentPart(kind, text, imageUri, refusal, inputAudio, fileFile, serializedAdditionalRawData);
118+
return new ChatMessageContentPart(kind, text, imageUri, refusal, inputAudio, fileFile, patch);
108119
}
109120
}

src/Custom/Chat/ChatMessageContentPart.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.ClientModel.Primitives;
23
using System.Collections.Generic;
34
using System.Diagnostics.CodeAnalysis;
45

@@ -42,23 +43,25 @@ internal ChatMessageContentPart()
4243
}
4344

4445
// CUSTOM: Added to support deserialization.
46+
#pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
4547
internal ChatMessageContentPart(
4648
ChatMessageContentPartKind kind,
4749
string text = default,
4850
InternalChatCompletionRequestMessageContentPartImageImageUrl imageUri = default,
4951
string refusal = default,
5052
InternalChatCompletionRequestMessageContentPartAudioInputAudio inputAudio = default,
5153
InternalChatCompletionRequestMessageContentPartFileFile fileFile = default,
52-
IDictionary<string, BinaryData> serializedAdditionalRawData = default)
54+
in JsonPatch patch = default)
5355
{
5456
_kind = kind;
5557
_text = text;
5658
_imageUri = imageUri;
5759
_refusal = refusal;
5860
_inputAudio = inputAudio;
5961
_fileFile = fileFile;
60-
_additionalBinaryDataProperties = serializedAdditionalRawData;
62+
_patch = patch;
6163
}
64+
#pragma warning restore SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
6265

6366
/// <summary> The kind of content part. </summary>
6467
public ChatMessageContentPartKind Kind => _kind;

src/Custom/Chat/ChatResponseFormat.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public static ChatResponseFormat CreateJsonSchemaFormat(string jsonSchemaFormatN
9999
jsonSchemaFormatName,
100100
schema: jsonSchema,
101101
jsonSchemaIsStrict,
102-
additionalBinaryDataProperties: null);
102+
patch: default);
103103

104104
return new InternalDotNetChatResponseFormatJsonSchema(internalSchema);
105105
}

0 commit comments

Comments
 (0)