Skip to content

Commit f47645c

Browse files
.NET: [Breaking] Allow passing auth token credential to cosmosdb extensions (#3250)
* allow passing token credentials to cosmosdb extensions * Update dotnet/src/Microsoft.Agents.AI.CosmosNoSql/CosmosDBWorkflowExtensions.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update dotnet/src/Microsoft.Agents.AI.CosmosNoSql/CosmosDBWorkflowExtensions.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update dotnet/src/Microsoft.Agents.AI.CosmosNoSql/CosmosDBChatExtensions.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 2cf4980 commit f47645c

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed

dotnet/src/Microsoft.Agents.AI.CosmosNoSql/CosmosDBChatExtensions.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using System;
44
using System.Diagnostics.CodeAnalysis;
55
using System.Threading.Tasks;
6-
using Azure.Identity;
6+
using Azure.Core;
77
using Microsoft.Azure.Cosmos;
88

99
namespace Microsoft.Agents.AI;
@@ -47,23 +47,30 @@ public static ChatClientAgentOptions WithCosmosDBMessageStore(
4747
/// <param name="accountEndpoint">The Cosmos DB account endpoint URI.</param>
4848
/// <param name="databaseId">The identifier of the Cosmos DB database.</param>
4949
/// <param name="containerId">The identifier of the Cosmos DB container.</param>
50+
/// <param name="tokenCredential">The TokenCredential to use for authentication (e.g., DefaultAzureCredential, ManagedIdentityCredential).</param>
5051
/// <returns>The configured <see cref="ChatClientAgentOptions"/>.</returns>
51-
/// <exception cref="ArgumentNullException">Thrown when <paramref name="options"/> is null.</exception>
52+
/// <exception cref="ArgumentNullException">Thrown when <paramref name="options"/> or <paramref name="tokenCredential"/> is null.</exception>
5253
/// <exception cref="ArgumentException">Thrown when any string parameter is null or whitespace.</exception>
5354
[RequiresUnreferencedCode("The CosmosChatMessageStore uses JSON serialization which is incompatible with trimming.")]
5455
[RequiresDynamicCode("The CosmosChatMessageStore uses JSON serialization which is incompatible with NativeAOT.")]
5556
public static ChatClientAgentOptions WithCosmosDBMessageStoreUsingManagedIdentity(
5657
this ChatClientAgentOptions options,
5758
string accountEndpoint,
5859
string databaseId,
59-
string containerId)
60+
string containerId,
61+
TokenCredential tokenCredential)
6062
{
6163
if (options is null)
6264
{
6365
throw new ArgumentNullException(nameof(options));
6466
}
6567

66-
options.ChatMessageStoreFactory = (context, ct) => new ValueTask<ChatMessageStore>(new CosmosChatMessageStore(accountEndpoint, new DefaultAzureCredential(), databaseId, containerId));
68+
if (tokenCredential is null)
69+
{
70+
throw new ArgumentNullException(nameof(tokenCredential));
71+
}
72+
73+
options.ChatMessageStoreFactory = (context, ct) => new ValueTask<ChatMessageStore>(new CosmosChatMessageStore(accountEndpoint, tokenCredential, databaseId, containerId));
6774
return options;
6875
}
6976

dotnet/src/Microsoft.Agents.AI.CosmosNoSql/CosmosDBWorkflowExtensions.cs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
using System;
44
using System.Diagnostics.CodeAnalysis;
5-
using Azure.Identity;
5+
using Azure.Core;
66
using Microsoft.Agents.AI.Workflows.Checkpointing;
77
using Microsoft.Azure.Cosmos;
88

@@ -52,14 +52,17 @@ public static CosmosCheckpointStore CreateCheckpointStore(
5252
/// <param name="accountEndpoint">The Cosmos DB account endpoint URI.</param>
5353
/// <param name="databaseId">The identifier of the Cosmos DB database.</param>
5454
/// <param name="containerId">The identifier of the Cosmos DB container.</param>
55+
/// <param name="tokenCredential">The TokenCredential to use for authentication (e.g., DefaultAzureCredential, ManagedIdentityCredential).</param>
5556
/// <returns>A new instance of <see cref="CosmosCheckpointStore"/>.</returns>
5657
/// <exception cref="ArgumentException">Thrown when any string parameter is null or whitespace.</exception>
58+
/// <exception cref="ArgumentNullException">Thrown when <paramref name="tokenCredential"/> is null.</exception>
5759
[RequiresUnreferencedCode("The CosmosCheckpointStore uses JSON serialization which is incompatible with trimming.")]
5860
[RequiresDynamicCode("The CosmosCheckpointStore uses JSON serialization which is incompatible with NativeAOT.")]
5961
public static CosmosCheckpointStore CreateCheckpointStoreUsingManagedIdentity(
6062
string accountEndpoint,
6163
string databaseId,
62-
string containerId)
64+
string containerId,
65+
TokenCredential tokenCredential)
6366
{
6467
if (string.IsNullOrWhiteSpace(accountEndpoint))
6568
{
@@ -76,7 +79,12 @@ public static CosmosCheckpointStore CreateCheckpointStoreUsingManagedIdentity(
7679
throw new ArgumentException("Cannot be null or whitespace", nameof(containerId));
7780
}
7881

79-
return new CosmosCheckpointStore(accountEndpoint, new DefaultAzureCredential(), databaseId, containerId);
82+
if (tokenCredential is null)
83+
{
84+
throw new ArgumentNullException(nameof(tokenCredential));
85+
}
86+
87+
return new CosmosCheckpointStore(accountEndpoint, tokenCredential, databaseId, containerId);
8088
}
8189

8290
/// <summary>
@@ -154,14 +162,17 @@ public static CosmosCheckpointStore<T> CreateCheckpointStore<T>(
154162
/// <param name="accountEndpoint">The Cosmos DB account endpoint URI.</param>
155163
/// <param name="databaseId">The identifier of the Cosmos DB database.</param>
156164
/// <param name="containerId">The identifier of the Cosmos DB container.</param>
165+
/// <param name="tokenCredential">The TokenCredential to use for authentication (e.g., DefaultAzureCredential, ManagedIdentityCredential).</param>
157166
/// <returns>A new instance of <see cref="CosmosCheckpointStore{T}"/>.</returns>
158167
/// <exception cref="ArgumentException">Thrown when any string parameter is null or whitespace.</exception>
168+
/// <exception cref="ArgumentNullException">Thrown when <paramref name="tokenCredential"/> is null.</exception>
159169
[RequiresUnreferencedCode("The CosmosCheckpointStore uses JSON serialization which is incompatible with trimming.")]
160170
[RequiresDynamicCode("The CosmosCheckpointStore uses JSON serialization which is incompatible with NativeAOT.")]
161171
public static CosmosCheckpointStore<T> CreateCheckpointStoreUsingManagedIdentity<T>(
162172
string accountEndpoint,
163173
string databaseId,
164-
string containerId)
174+
string containerId,
175+
TokenCredential tokenCredential)
165176
{
166177
if (string.IsNullOrWhiteSpace(accountEndpoint))
167178
{
@@ -178,7 +189,12 @@ public static CosmosCheckpointStore<T> CreateCheckpointStoreUsingManagedIdentity
178189
throw new ArgumentException("Cannot be null or whitespace", nameof(containerId));
179190
}
180191

181-
return new CosmosCheckpointStore<T>(accountEndpoint, new DefaultAzureCredential(), databaseId, containerId);
192+
if (tokenCredential is null)
193+
{
194+
throw new ArgumentNullException(nameof(tokenCredential));
195+
}
196+
197+
return new CosmosCheckpointStore<T>(accountEndpoint, tokenCredential, databaseId, containerId);
182198
}
183199

184200
/// <summary>

0 commit comments

Comments
 (0)