// Copyright (c) Microsoft. All rights reserved.
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using System.Threading.Tasks;
using Azure.AI.AgentServer.Responses;
using Azure.AI.AgentServer.Responses.Models;
using Microsoft.Shared.DiagnosticIds;
namespace Microsoft.Agents.AI.Foundry.Hosting;
///
/// Default implementation that maps the platform-injected
/// x-agent-user-isolation-key and x-agent-chat-isolation-key headers from
/// into a .
///
///
/// This is the implementation used in production Foundry hosted environments. When running locally
/// outside the platform, both isolation keys are , which causes
/// to return . The hosting layer treats a null
/// result as a configuration error and surfaces it as a 500 from the request. Local development
/// should register an alternate implementation
/// that provides fallback values for the missing headers.
///
[Experimental(DiagnosticIds.Experiments.AIOpenAIResponses)]
internal sealed class PlatformHostedSessionIsolationKeyProvider : HostedSessionIsolationKeyProvider
{
///
public override ValueTask GetKeysAsync(
ResponseContext context,
CreateResponse request,
CancellationToken cancellationToken)
{
var userKey = context?.Isolation?.UserIsolationKey;
var chatKey = context?.Isolation?.ChatIsolationKey;
if (string.IsNullOrWhiteSpace(userKey) || string.IsNullOrWhiteSpace(chatKey))
{
return new ValueTask((HostedSessionContext?)null);
}
return new ValueTask(new HostedSessionContext(userKey!, chatKey!));
}
}