Skip to content

Commit 28bdb7b

Browse files
chore(internal): codegen related update
1 parent 0ef1ad5 commit 28bdb7b

Some content is hidden

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

44 files changed

+718
-312
lines changed

src/Swarms/JsonConverters.cs

Lines changed: 0 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Reflection;
53
using System.Text.Json;
64
using System.Text.Json.Serialization;
75

@@ -48,61 +46,3 @@ public override void Write(Utf8JsonWriter writer, TEnum value, JsonSerializerOpt
4846
JsonSerializer.Serialize(writer, value.Raw(), options);
4947
}
5048
}
51-
52-
sealed class UnionConverter<T> : JsonConverter<T>
53-
where T : class
54-
{
55-
readonly List<Type> _variantTypes = Enumerable.ToList(
56-
Enumerable.Where(
57-
Assembly.GetExecutingAssembly().GetTypes(),
58-
type => type.BaseType == typeof(T)
59-
)
60-
);
61-
62-
public override T? Read(
63-
ref Utf8JsonReader reader,
64-
Type _typeToConvert,
65-
JsonSerializerOptions options
66-
)
67-
{
68-
List<JsonException> exceptions = [];
69-
foreach (var variantType in _variantTypes)
70-
{
71-
try
72-
{
73-
return JsonSerializer.Deserialize(ref reader, variantType, options) as T;
74-
}
75-
catch (JsonException e)
76-
{
77-
exceptions.Add(e);
78-
}
79-
}
80-
throw new AggregateException(exceptions);
81-
}
82-
83-
public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
84-
{
85-
var variantType =
86-
_variantTypes.Find(type => type == value.GetType())
87-
?? throw new ArgumentOutOfRangeException(value.GetType().Name);
88-
JsonSerializer.Serialize(writer, value, variantType, options);
89-
}
90-
}
91-
92-
sealed class VariantConverter<TVariant, TValue> : JsonConverter<TVariant>
93-
where TVariant : IVariant<TVariant, TValue>
94-
{
95-
public override TVariant Read(
96-
ref Utf8JsonReader reader,
97-
Type _typeToConvert,
98-
JsonSerializerOptions options
99-
)
100-
{
101-
return TVariant.From(JsonSerializer.Deserialize<TValue>(ref reader, options)!);
102-
}
103-
104-
public override void Write(Utf8JsonWriter writer, TVariant value, JsonSerializerOptions options)
105-
{
106-
JsonSerializer.Serialize(writer, value.Value, options);
107-
}
108-
}

src/Swarms/Models/Agent/AgentCompletion.cs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
using System.Text.Json;
44
using System.Text.Json.Serialization;
55
using AgentCompletionProperties = Swarms.Models.Agent.AgentCompletionProperties;
6+
using Swarms = Swarms;
67

78
namespace Swarms.Models.Agent;
89

9-
[JsonConverter(typeof(ModelConverter<AgentCompletion>))]
10-
public sealed record class AgentCompletion : ModelBase, IFromRaw<AgentCompletion>
10+
[JsonConverter(typeof(Swarms::ModelConverter<AgentCompletion>))]
11+
public sealed record class AgentCompletion : Swarms::ModelBase, Swarms::IFromRaw<AgentCompletion>
1112
{
1213
/// <summary>
1314
/// The configuration of the agent to be completed.
@@ -19,7 +20,10 @@ public AgentSpec? AgentConfig
1920
if (!this.Properties.TryGetValue("agent_config", out JsonElement element))
2021
return null;
2122

22-
return JsonSerializer.Deserialize<AgentSpec?>(element, ModelBase.SerializerOptions);
23+
return JsonSerializer.Deserialize<AgentSpec?>(
24+
element,
25+
Swarms::ModelBase.SerializerOptions
26+
);
2327
}
2428
set { this.Properties["agent_config"] = JsonSerializer.SerializeToElement(value); }
2529
}
@@ -37,7 +41,7 @@ public AgentSpec? AgentConfig
3741

3842
return JsonSerializer.Deserialize<AgentCompletionProperties::History?>(
3943
element,
40-
ModelBase.SerializerOptions
44+
Swarms::ModelBase.SerializerOptions
4145
);
4246
}
4347
set { this.Properties["history"] = JsonSerializer.SerializeToElement(value); }
@@ -53,7 +57,10 @@ public string? Img
5357
if (!this.Properties.TryGetValue("img", out JsonElement element))
5458
return null;
5559

56-
return JsonSerializer.Deserialize<string?>(element, ModelBase.SerializerOptions);
60+
return JsonSerializer.Deserialize<string?>(
61+
element,
62+
Swarms::ModelBase.SerializerOptions
63+
);
5764
}
5865
set { this.Properties["img"] = JsonSerializer.SerializeToElement(value); }
5966
}
@@ -68,7 +75,10 @@ public List<string>? Imgs
6875
if (!this.Properties.TryGetValue("imgs", out JsonElement element))
6976
return null;
7077

71-
return JsonSerializer.Deserialize<List<string>?>(element, ModelBase.SerializerOptions);
78+
return JsonSerializer.Deserialize<List<string>?>(
79+
element,
80+
Swarms::ModelBase.SerializerOptions
81+
);
7282
}
7383
set { this.Properties["imgs"] = JsonSerializer.SerializeToElement(value); }
7484
}
@@ -83,7 +93,7 @@ public bool? Stream
8393
if (!this.Properties.TryGetValue("stream", out JsonElement element))
8494
return null;
8595

86-
return JsonSerializer.Deserialize<bool?>(element, ModelBase.SerializerOptions);
96+
return JsonSerializer.Deserialize<bool?>(element, Swarms::ModelBase.SerializerOptions);
8797
}
8898
set { this.Properties["stream"] = JsonSerializer.SerializeToElement(value); }
8999
}
@@ -98,7 +108,10 @@ public string? Task
98108
if (!this.Properties.TryGetValue("task", out JsonElement element))
99109
return null;
100110

101-
return JsonSerializer.Deserialize<string?>(element, ModelBase.SerializerOptions);
111+
return JsonSerializer.Deserialize<string?>(
112+
element,
113+
Swarms::ModelBase.SerializerOptions
114+
);
102115
}
103116
set { this.Properties["task"] = JsonSerializer.SerializeToElement(value); }
104117
}

src/Swarms/Models/Agent/AgentCompletionProperties/History.cs

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Collections.Generic;
23
using System.Text.Json;
34
using System.Text.Json.Serialization;
@@ -9,7 +10,7 @@ namespace Swarms.Models.Agent.AgentCompletionProperties;
910
/// The history of the agent's previous tasks and responses. Can be either a dictionary
1011
/// or a list of message objects.
1112
/// </summary>
12-
[JsonConverter(typeof(UnionConverter<History>))]
13+
[JsonConverter(typeof(HistoryConverter))]
1314
public abstract record class History
1415
{
1516
internal History() { }
@@ -22,3 +23,61 @@ public static implicit operator History(List<Dictionary<string, string>> value)
2223

2324
public abstract void Validate();
2425
}
26+
27+
sealed class HistoryConverter : JsonConverter<History?>
28+
{
29+
public override History? Read(
30+
ref Utf8JsonReader reader,
31+
Type _typeToConvert,
32+
JsonSerializerOptions options
33+
)
34+
{
35+
List<JsonException> exceptions = [];
36+
37+
try
38+
{
39+
var deserialized = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(
40+
ref reader,
41+
options
42+
);
43+
if (deserialized != null)
44+
{
45+
return new HistoryVariants::JsonElements(deserialized);
46+
}
47+
}
48+
catch (JsonException e)
49+
{
50+
exceptions.Add(e);
51+
}
52+
53+
try
54+
{
55+
var deserialized = JsonSerializer.Deserialize<List<Dictionary<string, string>>>(
56+
ref reader,
57+
options
58+
);
59+
if (deserialized != null)
60+
{
61+
return new HistoryVariants::Strings(deserialized);
62+
}
63+
}
64+
catch (JsonException e)
65+
{
66+
exceptions.Add(e);
67+
}
68+
69+
throw new AggregateException(exceptions);
70+
}
71+
72+
public override void Write(Utf8JsonWriter writer, History? value, JsonSerializerOptions options)
73+
{
74+
object? variant = value switch
75+
{
76+
null => null,
77+
HistoryVariants::JsonElements(var jsonElements) => jsonElements,
78+
HistoryVariants::Strings(var strings) => strings,
79+
_ => throw new ArgumentOutOfRangeException(nameof(value)),
80+
};
81+
JsonSerializer.Serialize(writer, variant, options);
82+
}
83+
}

src/Swarms/Models/Agent/AgentCompletionProperties/HistoryVariants/All.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
using System.Collections.Generic;
22
using System.Text.Json;
3-
using System.Text.Json.Serialization;
43

54
namespace Swarms.Models.Agent.AgentCompletionProperties.HistoryVariants;
65

7-
[JsonConverter(typeof(VariantConverter<JsonElements, Dictionary<string, JsonElement>>))]
86
public sealed record class JsonElements(Dictionary<string, JsonElement> Value)
97
: History,
108
IVariant<JsonElements, Dictionary<string, JsonElement>>
@@ -17,7 +15,6 @@ public static JsonElements From(Dictionary<string, JsonElement> value)
1715
public override void Validate() { }
1816
}
1917

20-
[JsonConverter(typeof(VariantConverter<Strings, List<Dictionary<string, string>>>))]
2118
public sealed record class Strings(List<Dictionary<string, string>> Value)
2219
: History,
2320
IVariant<Strings, List<Dictionary<string, string>>>

src/Swarms/Models/Agent/AgentRunParams.cs

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44
using System.Text;
55
using System.Text.Json;
66
using AgentRunParamsProperties = Swarms.Models.Agent.AgentRunParamsProperties;
7+
using Swarms = Swarms;
78

89
namespace Swarms.Models.Agent;
910

1011
/// <summary>
1112
/// Run an agent with the specified task.
1213
/// </summary>
13-
public sealed record class AgentRunParams : ParamsBase
14+
public sealed record class AgentRunParams : Swarms::ParamsBase
1415
{
1516
public Dictionary<string, JsonElement> BodyProperties { get; set; } = [];
1617

@@ -24,7 +25,10 @@ public AgentSpec? AgentConfig
2425
if (!this.BodyProperties.TryGetValue("agent_config", out JsonElement element))
2526
return null;
2627

27-
return JsonSerializer.Deserialize<AgentSpec?>(element, ModelBase.SerializerOptions);
28+
return JsonSerializer.Deserialize<AgentSpec?>(
29+
element,
30+
Swarms::ModelBase.SerializerOptions
31+
);
2832
}
2933
set { this.BodyProperties["agent_config"] = JsonSerializer.SerializeToElement(value); }
3034
}
@@ -42,7 +46,7 @@ public AgentSpec? AgentConfig
4246

4347
return JsonSerializer.Deserialize<AgentRunParamsProperties::History?>(
4448
element,
45-
ModelBase.SerializerOptions
49+
Swarms::ModelBase.SerializerOptions
4650
);
4751
}
4852
set { this.BodyProperties["history"] = JsonSerializer.SerializeToElement(value); }
@@ -58,7 +62,10 @@ public string? Img
5862
if (!this.BodyProperties.TryGetValue("img", out JsonElement element))
5963
return null;
6064

61-
return JsonSerializer.Deserialize<string?>(element, ModelBase.SerializerOptions);
65+
return JsonSerializer.Deserialize<string?>(
66+
element,
67+
Swarms::ModelBase.SerializerOptions
68+
);
6269
}
6370
set { this.BodyProperties["img"] = JsonSerializer.SerializeToElement(value); }
6471
}
@@ -73,7 +80,10 @@ public List<string>? Imgs
7380
if (!this.BodyProperties.TryGetValue("imgs", out JsonElement element))
7481
return null;
7582

76-
return JsonSerializer.Deserialize<List<string>?>(element, ModelBase.SerializerOptions);
83+
return JsonSerializer.Deserialize<List<string>?>(
84+
element,
85+
Swarms::ModelBase.SerializerOptions
86+
);
7787
}
7888
set { this.BodyProperties["imgs"] = JsonSerializer.SerializeToElement(value); }
7989
}
@@ -88,7 +98,7 @@ public bool? Stream
8898
if (!this.BodyProperties.TryGetValue("stream", out JsonElement element))
8999
return null;
90100

91-
return JsonSerializer.Deserialize<bool?>(element, ModelBase.SerializerOptions);
101+
return JsonSerializer.Deserialize<bool?>(element, Swarms::ModelBase.SerializerOptions);
92102
}
93103
set { this.BodyProperties["stream"] = JsonSerializer.SerializeToElement(value); }
94104
}
@@ -103,12 +113,15 @@ public string? Task
103113
if (!this.BodyProperties.TryGetValue("task", out JsonElement element))
104114
return null;
105115

106-
return JsonSerializer.Deserialize<string?>(element, ModelBase.SerializerOptions);
116+
return JsonSerializer.Deserialize<string?>(
117+
element,
118+
Swarms::ModelBase.SerializerOptions
119+
);
107120
}
108121
set { this.BodyProperties["task"] = JsonSerializer.SerializeToElement(value); }
109122
}
110123

111-
public override Uri Url(ISwarmsClientClient client)
124+
public override Uri Url(Swarms::ISwarmsClientClient client)
112125
{
113126
return new UriBuilder(client.BaseUrl.ToString().TrimEnd('/') + "/v1/agent/completions")
114127
{
@@ -125,12 +138,12 @@ public StringContent BodyContent()
125138
);
126139
}
127140

128-
public void AddHeadersToRequest(HttpRequestMessage request, ISwarmsClientClient client)
141+
public void AddHeadersToRequest(HttpRequestMessage request, Swarms::ISwarmsClientClient client)
129142
{
130-
ParamsBase.AddDefaultHeaders(request, client);
143+
Swarms::ParamsBase.AddDefaultHeaders(request, client);
131144
foreach (var item in this.HeaderProperties)
132145
{
133-
ParamsBase.AddHeaderElementToRequest(request, item.Key, item.Value);
146+
Swarms::ParamsBase.AddHeaderElementToRequest(request, item.Key, item.Value);
134147
}
135148
}
136149
}

0 commit comments

Comments
 (0)