Skip to content

Commit 07037d7

Browse files
kzuCopilotCopilot
authored
.NET: Allow extending agent run options via additional properties (microsoft#1872)
* Allow extending agent run options via additional properties This mirrors the M.E.AI model in ChatOptions.AdditionalProperties which is very useful when building functionality pipelines. Fixes microsoft#1815 * Expand XML documentation Co-authored-by: Copilot <[email protected]> * Add AdditionalProperties tests to AgentRunOptions Co-authored-by: kzu <[email protected]> --------- Co-authored-by: Copilot <[email protected]> Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: kzu <[email protected]>
1 parent 20c42f5 commit 07037d7

2 files changed

Lines changed: 40 additions & 2 deletions

File tree

‎dotnet/src/Microsoft.Agents.AI.Abstractions/AgentRunOptions.cs‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Microsoft. All rights reserved.
22

33
using System;
4+
using Microsoft.Extensions.AI;
45
using Microsoft.Shared.Diagnostics;
56

67
namespace Microsoft.Agents.AI;
@@ -32,6 +33,7 @@ public AgentRunOptions(AgentRunOptions options)
3233
_ = Throw.IfNull(options);
3334
this.ContinuationToken = options.ContinuationToken;
3435
this.AllowBackgroundResponses = options.AllowBackgroundResponses;
36+
this.AdditionalProperties = options.AdditionalProperties?.Clone();
3537
}
3638

3739
/// <summary>
@@ -74,4 +76,18 @@ public AgentRunOptions(AgentRunOptions options)
7476
/// </para>
7577
/// </remarks>
7678
public bool? AllowBackgroundResponses { get; set; }
79+
80+
/// <summary>
81+
/// Gets or sets additional properties associated with these options.
82+
/// </summary>
83+
/// <value>
84+
/// An <see cref="AdditionalPropertiesDictionary"/> containing custom properties,
85+
/// or <see langword="null"/> if no additional properties are present.
86+
/// </value>
87+
/// <remarks>
88+
/// Additional properties provide a way to include custom metadata or provider-specific
89+
/// information that doesn't fit into the standard options schema. This is useful for
90+
/// preserving implementation-specific details or extending the options with custom data.
91+
/// </remarks>
92+
public AdditionalPropertiesDictionary? AdditionalProperties { get; set; }
7793
}

‎dotnet/tests/Microsoft.Agents.AI.Abstractions.UnitTests/AgentRunOptionsTests.cs‎

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@ public void CloningConstructorCopiesProperties()
1818
var options = new AgentRunOptions
1919
{
2020
ContinuationToken = new object(),
21-
AllowBackgroundResponses = true
21+
AllowBackgroundResponses = true,
22+
AdditionalProperties = new AdditionalPropertiesDictionary
23+
{
24+
["key1"] = "value1",
25+
["key2"] = 42
26+
}
2227
};
2328

2429
// Act
@@ -28,6 +33,10 @@ public void CloningConstructorCopiesProperties()
2833
Assert.NotNull(clone);
2934
Assert.Same(options.ContinuationToken, clone.ContinuationToken);
3035
Assert.Equal(options.AllowBackgroundResponses, clone.AllowBackgroundResponses);
36+
Assert.NotNull(clone.AdditionalProperties);
37+
Assert.NotSame(options.AdditionalProperties, clone.AdditionalProperties);
38+
Assert.Equal("value1", clone.AdditionalProperties["key1"]);
39+
Assert.Equal(42, clone.AdditionalProperties["key2"]);
3140
}
3241

3342
[Fact]
@@ -42,7 +51,12 @@ public void JsonSerializationRoundtrips()
4251
var options = new AgentRunOptions
4352
{
4453
ContinuationToken = ResponseContinuationToken.FromBytes(new byte[] { 1, 2, 3 }),
45-
AllowBackgroundResponses = true
54+
AllowBackgroundResponses = true,
55+
AdditionalProperties = new AdditionalPropertiesDictionary
56+
{
57+
["key1"] = "value1",
58+
["key2"] = 42
59+
}
4660
};
4761

4862
// Act
@@ -54,5 +68,13 @@ public void JsonSerializationRoundtrips()
5468
Assert.NotNull(deserialized);
5569
Assert.Equivalent(ResponseContinuationToken.FromBytes(new byte[] { 1, 2, 3 }), deserialized!.ContinuationToken);
5670
Assert.Equal(options.AllowBackgroundResponses, deserialized.AllowBackgroundResponses);
71+
Assert.NotNull(deserialized.AdditionalProperties);
72+
Assert.Equal(2, deserialized.AdditionalProperties.Count);
73+
Assert.True(deserialized.AdditionalProperties.TryGetValue("key1", out object? value1));
74+
Assert.IsType<JsonElement>(value1);
75+
Assert.Equal("value1", ((JsonElement)value1!).GetString());
76+
Assert.True(deserialized.AdditionalProperties.TryGetValue("key2", out object? value2));
77+
Assert.IsType<JsonElement>(value2);
78+
Assert.Equal(42, ((JsonElement)value2!).GetInt32());
5779
}
5880
}

0 commit comments

Comments
 (0)