-
Notifications
You must be signed in to change notification settings - Fork 2.2k
.NET: Add Foundry hosted session and user identity pass-through #7648
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
Open
Roger Barreto (rogerbarreto)
wants to merge
7
commits into
microsoft:main
Choose a base branch
from
rogerbarreto:foundry-session-user-isolation-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0617be7
.NET: Add Foundry session and user identity pass-through
rogerbarreto 9eada87
.NET: Add live ITs for Foundry session and user identity
rogerbarreto 235bb47
.NET: Reject Foundry hosted session switch when sticky
rogerbarreto 9c3d704
.NET: Clear nested user identity and preserve run options
rogerbarreto 5a396cf
.NET: Clarify previous_response_id user binding in docs
rogerbarreto e9190e3
refactor(foundry): clarify hosted agent APIs
rogerbarreto aa203eb
Merge branch 'main' into foundry-session-user-isolation-support
rogerbarreto 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
Some comments aren't visible on the classic Files Changed page.
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
77 changes: 77 additions & 0 deletions
77
dotnet/src/Microsoft.Agents.AI.Foundry/FoundryAgentSessionExtensions.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,77 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| using System.Diagnostics.CodeAnalysis; | ||
| using Microsoft.Agents.AI.Foundry; | ||
| using Microsoft.Shared.DiagnosticIds; | ||
| using Microsoft.Shared.Diagnostics; | ||
|
|
||
| namespace Microsoft.Agents.AI; | ||
|
|
||
| /// <summary> | ||
| /// Foundry-specific extension methods for <see cref="AgentSession"/>. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// <para> | ||
| /// The hosted-agent session id (sandbox / <c>agent_session_id</c>) is stored in | ||
| /// <see cref="AgentSession.StateBag"/> under <see cref="FoundryHostedAgentSessionIdKey"/>. That keeps | ||
| /// Foundry-specific state off the sealed <see cref="ChatClientAgentSession"/> type while still | ||
| /// serializing with the session. | ||
| /// </para> | ||
| /// <para> | ||
| /// This is not <see cref="Extensions.AI.ChatOptions.AdditionalProperties"/>. Per-call | ||
| /// overrides use | ||
| /// <see cref="Extensions.AI.FoundryChatOptionsExtensions.WithFoundryHostedAgentSessionId(Extensions.AI.ChatOptions, string)"/>. | ||
| /// </para> | ||
| /// </remarks> | ||
| [Experimental(DiagnosticIds.Experiments.AIOpenAIRequestPolicies)] | ||
|
rogerbarreto marked this conversation as resolved.
|
||
| public static class FoundryAgentSessionExtensions | ||
|
rogerbarreto marked this conversation as resolved.
|
||
| { | ||
| /// <summary> | ||
| /// Well-known <see cref="AgentSessionStateBag"/> key for the sticky hosted-agent session id. | ||
| /// </summary> | ||
| public const string FoundryHostedAgentSessionIdKey = "Microsoft.Agents.AI.Foundry.HostedAgentSessionId"; | ||
|
|
||
| extension(AgentSession session) | ||
| { | ||
| /// <summary> | ||
| /// Gets the sticky Microsoft Foundry hosted-agent session id associated with this | ||
| /// Agent Framework session. | ||
| /// </summary> | ||
| /// <value> | ||
| /// The Foundry <c>agent_session_id</c>, or <see langword="null"/> when no hosted sandbox | ||
| /// has been pinned or captured yet. | ||
| /// </value> | ||
| /// <remarks> | ||
| /// <para> | ||
| /// This id identifies the Foundry-managed hosted-agent sandbox: its compute, persisted | ||
| /// <c>$HOME</c>, and files. It is separate from | ||
| /// <see cref="ChatClientAgentSession.ConversationId"/>, which identifies conversation | ||
| /// history. | ||
| /// </para> | ||
| /// <para> | ||
| /// Prefer creating or pinning through | ||
| /// <see cref="FoundryAgent.CreateFoundryHostedAgentSessionAsync(string?, string?, System.Threading.CancellationToken)"/>. | ||
| /// The property is populated automatically when Foundry creates a sandbox on first use. | ||
| /// See | ||
| /// <see href="https://learn.microsoft.com/azure/foundry/agents/how-to/manage-hosted-sessions#sessions-versus-conversations">Manage hosted agent sessions</see>. | ||
| /// </para> | ||
| /// </remarks> | ||
| public string? FoundryHostedAgentSessionId | ||
| { | ||
| get | ||
| { | ||
| _ = Throw.IfNull(session); | ||
| return session.StateBag.TryGetValue<string>(FoundryHostedAgentSessionIdKey, out var value) | ||
| ? value | ||
| : null; | ||
| } | ||
|
|
||
| internal set | ||
| { | ||
| _ = Throw.IfNull(session); | ||
| _ = Throw.IfNullOrWhitespace(value); | ||
| session.StateBag.SetValue(FoundryHostedAgentSessionIdKey, value); | ||
| } | ||
| } | ||
| } | ||
| } | ||
143 changes: 143 additions & 0 deletions
143
dotnet/src/Microsoft.Agents.AI.Foundry/FoundryChatOptionsExtensions.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,143 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| using System.Diagnostics.CodeAnalysis; | ||
| using Microsoft.Agents.AI; | ||
| using Microsoft.Agents.AI.Foundry; | ||
| using Microsoft.Shared.DiagnosticIds; | ||
| using Microsoft.Shared.Diagnostics; | ||
|
|
||
| namespace Microsoft.Extensions.AI; | ||
|
|
||
| /// <summary> | ||
| /// Foundry-specific extension methods for <see cref="ChatOptions"/>. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// <para> | ||
| /// Use these helpers to attach per-call Foundry request fields: | ||
| /// <list type="bullet"> | ||
| /// <item><description><see cref="WithFoundryHostedAgentSessionId"/> sends <c>agent_session_id</c> on the Responses body.</description></item> | ||
| /// <item><description><see cref="WithFoundryHostedAgentUserIdentity"/> sends <c>x-ms-user-identity</c> on the request.</description></item> | ||
| /// </list> | ||
| /// </para> | ||
| /// <para> | ||
| /// Hosted-agent session ids supplied via <see cref="WithFoundryHostedAgentSessionId"/> participate in the same | ||
| /// conflict rule as <see cref="ChatOptions.ConversationId"/>: if the <see cref="AgentSession"/> already | ||
| /// holds a different hosted id in its <see cref="AgentSession.StateBag"/>, the run throws | ||
| /// <see cref="System.InvalidOperationException"/>. Prefer pinning at session creation via | ||
| /// <see cref="FoundryAgent.CreateFoundryHostedAgentSessionAsync(string?, string?, System.Threading.CancellationToken)"/>. | ||
| /// </para> | ||
| /// </remarks> | ||
| [Experimental(DiagnosticIds.Experiments.AIOpenAIRequestPolicies)] | ||
| public static class FoundryChatOptionsExtensions | ||
|
rogerbarreto marked this conversation as resolved.
|
||
| { | ||
| /// <summary>HTTP header name for delegated application user identity.</summary> | ||
| public const string FoundryHostedAgentUserIdentityHeaderName = "x-ms-user-identity"; | ||
|
|
||
| /// <summary> | ||
| /// Well-known <see cref="ChatOptions.AdditionalProperties"/> key used to carry a per-call | ||
| /// hosted-agent session id. | ||
| /// </summary> | ||
| internal const string FoundryHostedAgentSessionIdKey = "Microsoft.Agents.AI.Foundry.HostedAgentSessionId"; | ||
|
|
||
| /// <summary> | ||
| /// Well-known <see cref="ChatOptions.AdditionalProperties"/> key used to carry the per-call | ||
| /// user identity value. | ||
| /// </summary> | ||
| internal const string FoundryHostedAgentUserIdentityKey = "Microsoft.Agents.AI.Foundry.UserIdentity"; | ||
|
|
||
| /// <summary> | ||
| /// Attaches a hosted-agent session id to the per-call <paramref name="options"/> carrier. | ||
|
rogerbarreto marked this conversation as resolved.
|
||
| /// </summary> | ||
| /// <remarks> | ||
| /// <para> | ||
| /// Only valid when the run's session has no hosted id yet, or already has this same id. | ||
| /// Prefer | ||
| /// <see cref="FoundryAgent.CreateFoundryHostedAgentSessionAsync(string?, string?, System.Threading.CancellationToken)"/> | ||
| /// to pin at session creation. | ||
| /// </para> | ||
| /// <para> | ||
| /// The value is stored in <see cref="ChatOptions.AdditionalProperties"/>. Replacing that | ||
| /// dictionary after calling this method removes the value; populate or replace the dictionary | ||
| /// first, then call this method. | ||
| /// </para> | ||
| /// </remarks> | ||
| public static ChatOptions WithFoundryHostedAgentSessionId(this ChatOptions options, string hostedSessionId) | ||
| { | ||
| _ = Throw.IfNull(options); | ||
| _ = Throw.IfNullOrWhitespace(hostedSessionId); | ||
|
|
||
| options.AdditionalProperties ??= new AdditionalPropertiesDictionary(); | ||
| options.AdditionalProperties[FoundryHostedAgentSessionIdKey] = hostedSessionId; | ||
| return options; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Attaches a delegated user identity value that will be sent as the | ||
| /// <c>x-ms-user-identity</c> request header. | ||
| /// </summary> | ||
| /// <param name="options">The per-call chat options to mutate.</param> | ||
| /// <param name="userIdentity">Opaque application user identifier. Must be non-empty.</param> | ||
| /// <returns><paramref name="options"/> for fluent chaining.</returns> | ||
| /// <remarks> | ||
| /// <para> | ||
| /// User identity is always request-scoped. It is never stored on <see cref="AgentSession"/>. | ||
| /// </para> | ||
| /// <para> | ||
| /// Per Foundry hosted-agent isolation, a Responses chain created under one user cannot be | ||
| /// continued by another user via <c>previous_response_id</c>, even when both calls share the | ||
| /// same hosted sandbox (<c>agent_session_id</c>). See | ||
| /// <see href="https://learn.microsoft.com/azure/foundry/agents/how-to/multiplex-session-users">Multiplex multiple users in one hosted agent session</see>. | ||
| /// Reusing one <see cref="AgentSession"/> across identities typically reuses that chain, so the | ||
| /// second identity's run fails at the platform (observed as a response not-found error). Prefer | ||
| /// a distinct <see cref="AgentSession"/> per identity; those sessions may still share one hosted | ||
| /// sandbox pin via <see cref="WithFoundryHostedAgentSessionId"/> or | ||
| /// <see cref="FoundryAgent.CreateFoundryHostedAgentSessionAsync(string?, string?, System.Threading.CancellationToken)"/>. | ||
| /// </para> | ||
| /// <para> | ||
| /// The value is stored in <see cref="ChatOptions.AdditionalProperties"/>. Replacing that | ||
| /// dictionary after calling this method removes the value; populate or replace the dictionary | ||
| /// first, then call this method. | ||
| /// </para> | ||
| /// </remarks> | ||
| public static ChatOptions WithFoundryHostedAgentUserIdentity(this ChatOptions options, string userIdentity) | ||
| { | ||
| _ = Throw.IfNull(options); | ||
| _ = Throw.IfNullOrWhitespace(userIdentity); | ||
|
|
||
| options.AdditionalProperties ??= new AdditionalPropertiesDictionary(); | ||
| options.AdditionalProperties[FoundryHostedAgentUserIdentityKey] = userIdentity; | ||
| return options; | ||
| } | ||
|
|
||
| /// <summary>Reads the per-call hosted-agent session id stamped by <see cref="WithFoundryHostedAgentSessionId"/>.</summary> | ||
| internal static string? GetFoundryHostedAgentSessionId(this ChatOptions options) | ||
| { | ||
| if (options.AdditionalProperties is null) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| if (!options.AdditionalProperties.TryGetValue(FoundryHostedAgentSessionIdKey, out var raw)) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| return raw as string; | ||
| } | ||
|
|
||
| /// <summary>Reads the per-call user identity stamped by <see cref="WithFoundryHostedAgentUserIdentity"/>.</summary> | ||
| internal static string? GetFoundryHostedAgentUserIdentity(this ChatOptions options) | ||
| { | ||
| if (options.AdditionalProperties is null) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| if (!options.AdditionalProperties.TryGetValue(FoundryHostedAgentUserIdentityKey, out var raw)) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| return raw as string; | ||
| } | ||
| } | ||
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.