-
Notifications
You must be signed in to change notification settings - Fork 1.2k
.NET: CosmosDB Actor State Storage #262
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
3a3ef59
Implement CosmosDB actor state storage.
adityamandaleeka 16156e4
Fix.
adityamandaleeka b524cfe
Minor fixes.
adityamandaleeka bd5fdab
Fixes.
adityamandaleeka 5e07e92
Make CosmosDB initialization be lazy.
adityamandaleeka 38bdc24
Remove unnecessary read from write path.
adityamandaleeka d397840
Throw on empty writes.
adityamandaleeka 2e218bc
Add arg validation for read.
adityamandaleeka 4219965
Add CosmosIdSanitizer.
adityamandaleeka d7e1c9c
Fix.
adityamandaleeka 9ac4fec
Fix.
adityamandaleeka 54721c0
Simplify doc IDs.
adityamandaleeka 28d7092
Update comment.
adityamandaleeka 013372c
fb
adityamandaleeka 7d2c986
Make LazyCosmosContainer internal and add tests.
adityamandaleeka db2499a
Make test constants public and remove IVT.
adityamandaleeka fc7937f
Use source generated JSON context for future nativeAOT support.
adityamandaleeka 1465666
Re-add dropped comments.
adityamandaleeka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
dotnet/src/Microsoft.Extensions.AI.Agents.Runtime.Storage.CosmosDB/ActorDocuments.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| using System; | ||
| using System.Text.Json; | ||
|
|
||
| namespace Microsoft.Extensions.AI.Agents.Runtime.Storage.CosmosDB; | ||
|
|
||
| /// <summary> | ||
| /// Root document for each actor that provides actor-level ETag semantics. | ||
| /// Every write operation updates this document to ensure a single ETag represents | ||
| /// the entire actor's state for optimistic concurrency control. | ||
| /// This document contains no actor state data. It only serves to track last modified | ||
| /// time and provide a single ETag for the actor's state. | ||
| /// | ||
| /// Example structure: | ||
| /// { | ||
| /// "id": "rootdoc", // Root document ID (constant per actor partition) | ||
| /// "actorId": "actor-123", // Partition key (actor ID) | ||
| /// "lastModified": "2024-...", // Timestamp | ||
| /// } | ||
| /// </summary> | ||
| public sealed class ActorRootDocument | ||
| { | ||
| /// <summary> | ||
| /// The document ID. | ||
| /// </summary> | ||
| public string Id { get; set; } = default!; | ||
|
|
||
| /// <summary> | ||
| /// The actor ID. | ||
| /// </summary> | ||
| public string ActorId { get; set; } = default!; | ||
|
|
||
| /// <summary> | ||
| /// The last modified timestamp. | ||
| /// </summary> | ||
| public DateTimeOffset LastModified { get; set; } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Actor state document that represents a single key-value pair in the actor's state. | ||
| /// Document Structure (one per actor key): | ||
| /// { | ||
| /// "id": "state_sanitizedkey", // Unique document ID for the state entry | ||
| /// "actorId": "actor-123", // Partition key (actor ID) | ||
| /// "key": "foo", // Logical key for the state entry | ||
| /// "value": { "bar": 42, "baz": "hello" } // Arbitrary JsonElement payload | ||
| /// } | ||
| /// </summary> | ||
| public sealed class ActorStateDocument | ||
| { | ||
| /// <summary> | ||
| /// The document ID. | ||
| /// </summary> | ||
| public string Id { get; set; } = default!; | ||
|
|
||
| /// <summary> | ||
| /// The actor ID. | ||
| /// </summary> | ||
| public string ActorId { get; set; } = default!; | ||
|
|
||
| /// <summary> | ||
| /// The logical key for the state entry. | ||
| /// </summary> | ||
| public string Key { get; set; } = default!; | ||
|
|
||
| /// <summary> | ||
| /// The value payload. | ||
| /// </summary> | ||
| public JsonElement Value { get; set; } = default!; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Projection class for Cosmos DB queries to retrieve keys. | ||
| /// </summary> | ||
| public sealed class KeyProjection | ||
| { | ||
| /// <summary> | ||
| /// The key value. | ||
| /// </summary> | ||
| public string Key { get; set; } = default!; | ||
| } |
19 changes: 19 additions & 0 deletions
19
...rc/Microsoft.Extensions.AI.Agents.Runtime.Storage.CosmosDB/CosmosActorStateJsonContext.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| using System.Text.Json; | ||
| using System.Text.Json.Serialization; | ||
|
|
||
| namespace Microsoft.Extensions.AI.Agents.Runtime.Storage.CosmosDB; | ||
|
|
||
| /// <summary> | ||
| /// Source-generated JSON type information for Cosmos DB actor state documents. | ||
| /// </summary> | ||
| [JsonSourceGenerationOptions( | ||
| JsonSerializerDefaults.Web, | ||
| UseStringEnumConverter = true, | ||
| DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, | ||
| WriteIndented = false)] | ||
| [JsonSerializable(typeof(ActorStateDocument))] | ||
| [JsonSerializable(typeof(ActorRootDocument))] | ||
| [JsonSerializable(typeof(KeyProjection))] | ||
| internal sealed partial class CosmosActorStateJsonContext : JsonSerializerContext; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.