-
Notifications
You must be signed in to change notification settings - Fork 48
Handle Azure Open AI v1 response endpoint change and update README.md #324
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
+275
−25
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d565a1b
Handle Azure OpenAI v1 responses endpoint
depfeife-msft 8917907
Remove unnecessary change
depfeife-msft 041b54e
Potential fix for pull request finding
denzelpfeifer 831d47d
Potential fix for pull request finding
denzelpfeifer a547697
Potential fix for pull request finding
denzelpfeifer 81bb3cd
fix: address W365 token review comments
depfeife-msft e5b86d8
fix: avoid printing CUA token
depfeife-msft 7743799
docs: remove obsolete token output note
depfeife-msft df02a8f
Merge branch 'main' into users/depfeife/azure-openai-v1-responses
desmarest 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
31 changes: 31 additions & 0 deletions
31
dotnet/w365-computer-use/sample-agent/ComputerUse/AzureOpenAIModelProviderOptions.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,31 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| namespace W365ComputerUseSample.ComputerUse; | ||
|
|
||
| internal sealed class AzureOpenAIModelProviderOptions | ||
| { | ||
| public required string Url { get; init; } | ||
|
|
||
| public required string ModelName { get; init; } | ||
|
|
||
| public static AzureOpenAIModelProviderOptions FromConfiguration(IConfiguration configuration) | ||
| { | ||
| var endpoint = configuration["AIServices:AzureOpenAI:Endpoint"] | ||
| ?? throw new InvalidOperationException("AIServices:AzureOpenAI:Endpoint is required."); | ||
|
|
||
| var configuredModelName = configuration["AIServices:AzureOpenAI:ModelName"]; | ||
| var deploymentName = configuration["AIServices:AzureOpenAI:DeploymentName"]; | ||
| var modelName = !string.IsNullOrWhiteSpace(configuredModelName) | ||
| ? configuredModelName | ||
| : !string.IsNullOrWhiteSpace(deploymentName) | ||
| ? deploymentName | ||
| : "computer-use-preview"; | ||
|
|
||
| return new AzureOpenAIModelProviderOptions | ||
| { | ||
| ModelName = modelName, | ||
| Url = $"{endpoint.TrimEnd('/')}/openai/v1/responses", | ||
| }; | ||
| } | ||
| } |
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
192 changes: 192 additions & 0 deletions
192
dotnet/w365-computer-use/sample-agent/scripts/Get-CuaAgentUserToken.ps1
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,192 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT License. | ||
|
|
||
| <# | ||
| .SYNOPSIS | ||
| Acquires a CUA user access token for an agent using the Entra user_fic flow. | ||
|
|
||
| .DESCRIPTION | ||
| Requests an application token, exchanges it for an agent identity token, and then | ||
| requests a CUA user token from Microsoft Entra ID. By default, the script assigns | ||
| the final CUA access token to $env:BEARER_TOKEN in the current PowerShell | ||
| process. Use -ShowOid | ||
| to decode the final token payload and write the oid claim to the information | ||
| stream. | ||
|
|
||
| .EXAMPLE | ||
| .\Get-CuaAgentUserToken.ps1 -TenantId "contoso.onmicrosoft.com" -AgentBlueprintClientId "00000000-0000-0000-0000-000000000000" -AgentBlueprintClientSecret "<agent-blueprint-client-secret>" -AgentClientId "11111111-1111-1111-1111-111111111111" -AgentUsername "user@contoso.com" | ||
|
|
||
| Assigns the final CUA access token to $env:BEARER_TOKEN in the current | ||
| PowerShell process. | ||
|
|
||
| .EXAMPLE | ||
| .\Get-CuaAgentUserToken.ps1 -TenantId "contoso.onmicrosoft.com" -AgentBlueprintClientId "00000000-0000-0000-0000-000000000000" -AgentBlueprintClientSecret "<agent-blueprint-client-secret>" -AgentClientId "11111111-1111-1111-1111-111111111111" -AgentUsername "user@contoso.com" -SetBearerToken -InformationAction Continue | ||
|
|
||
| Assigns the final CUA access token to $env:BEARER_TOKEN in the current | ||
| PowerShell process. | ||
|
|
||
| .EXAMPLE | ||
| .\Get-CuaAgentUserToken.ps1 -TenantId "contoso.onmicrosoft.com" -AgentBlueprintClientId "00000000-0000-0000-0000-000000000000" -AgentBlueprintClientSecret "<agent-blueprint-client-secret>" -AgentClientId "11111111-1111-1111-1111-111111111111" -AgentUsername "user@contoso.com" -ShowOid -InformationAction Continue | ||
|
|
||
| Assigns the final CUA access token to $env:BEARER_TOKEN and writes the token | ||
| oid claim to the information stream. | ||
| #> | ||
| [CmdletBinding()] | ||
| param( | ||
| [Parameter(Mandatory = $true)] | ||
| [string]$TenantId, | ||
| [Parameter(Mandatory = $true)] | ||
| [string]$AgentBlueprintClientId, | ||
| [Parameter(Mandatory = $true)] | ||
| [string]$AgentBlueprintClientSecret, | ||
| [Parameter(Mandatory = $true)] | ||
| [string]$AgentClientId, | ||
| [Parameter(Mandatory = $true)] | ||
| [string]$AgentUsername, | ||
| [string]$AuthorityHost = "https://login.microsoftonline.com", | ||
| [string]$Scope = "da81128c-e5b5-4f9e-8d89-50d906f107c5/.default", | ||
| [switch]$SetBearerToken = $true, | ||
| [switch]$ShowOid | ||
| ) | ||
|
|
||
| $ErrorActionPreference = "Stop" | ||
|
|
||
| function Assert-RequiredParameter { | ||
| param( | ||
| [Parameter(Mandatory = $true)] | ||
| [string]$Name, | ||
|
|
||
| [AllowNull()] | ||
| [string]$Value | ||
| ) | ||
|
|
||
| if ([string]::IsNullOrWhiteSpace($Value)) { | ||
| throw "Parameter validation failed: -$Name is required." | ||
| } | ||
| } | ||
|
|
||
| function Get-AccessTokenFromResponse { | ||
| param( | ||
| [Parameter(Mandatory = $true)] | ||
| [object]$Response, | ||
|
|
||
| [Parameter(Mandatory = $true)] | ||
| [string]$StepLabel | ||
| ) | ||
|
|
||
| if ($null -eq $Response -or [string]::IsNullOrWhiteSpace($Response.access_token)) { | ||
| throw "$StepLabel failed: token response missing required field 'access_token'." | ||
| } | ||
|
|
||
| return $Response.access_token | ||
| } | ||
|
|
||
| function ConvertFrom-Base64Url { | ||
| param( | ||
| [Parameter(Mandatory = $true)] | ||
| [string]$Value | ||
| ) | ||
|
|
||
| $base64 = $Value.Replace("-", "+").Replace("_", "/") | ||
| $padding = (4 - ($base64.Length % 4)) % 4 | ||
| if ($padding -gt 0) { | ||
| $base64 = $base64 + ("=" * $padding) | ||
| } | ||
|
|
||
| $bytes = [Convert]::FromBase64String($base64) | ||
| return [System.Text.Encoding]::UTF8.GetString($bytes) | ||
| } | ||
|
|
||
| function Write-OidInformation { | ||
| param( | ||
| [Parameter(Mandatory = $true)] | ||
| [string]$AccessToken | ||
| ) | ||
|
|
||
| try { | ||
| $segments = $AccessToken.Split(".") | ||
| if ($segments.Count -ne 3) { | ||
| Write-Warning "ShowOid decode failed: access token is not a valid JWT (expected 3 segments)." | ||
| return | ||
| } | ||
|
|
||
| $payloadJson = ConvertFrom-Base64Url -Value $segments[1] | ||
| $claims = $payloadJson | ConvertFrom-Json | ||
| if ([string]::IsNullOrWhiteSpace($claims.oid)) { | ||
| Write-Warning "ShowOid decode completed but JWT payload did not contain an 'oid' claim." | ||
| return | ||
| } | ||
|
|
||
| Write-Information $claims.oid | ||
| } | ||
| catch { | ||
| Write-Warning "ShowOid decode failed: $($_.Exception.Message)" | ||
| } | ||
| } | ||
|
|
||
| Assert-RequiredParameter -Name "TenantId" -Value $TenantId | ||
| Assert-RequiredParameter -Name "AgentBlueprintClientId" -Value $AgentBlueprintClientId | ||
| Assert-RequiredParameter -Name "AgentBlueprintClientSecret" -Value $AgentBlueprintClientSecret | ||
| Assert-RequiredParameter -Name "AgentClientId" -Value $AgentClientId | ||
| Assert-RequiredParameter -Name "AgentUsername" -Value $AgentUsername | ||
| Assert-RequiredParameter -Name "AuthorityHost" -Value $AuthorityHost | ||
| Assert-RequiredParameter -Name "Scope" -Value $Scope | ||
|
|
||
| $tokenUrl = "$($AuthorityHost.TrimEnd('/'))/$TenantId/oauth2/v2.0/token" | ||
| $clientAssertionType = "urn:ietf:params:oauth:client-assertion-type:jwt-bearer" | ||
|
|
||
| try { | ||
| $applicationTokenResponse = Invoke-RestMethod -Method Post -Uri $tokenUrl -ContentType "application/x-www-form-urlencoded" -Body @{ | ||
| client_id = $AgentBlueprintClientId | ||
| scope = "api://AzureADTokenExchange/.default" | ||
| grant_type = "client_credentials" | ||
| client_secret = $AgentBlueprintClientSecret | ||
| fmi_path = $AgentClientId | ||
| } | ||
| $applicationToken = Get-AccessTokenFromResponse -Response $applicationTokenResponse -StepLabel "Application token request" | ||
| } | ||
| catch { | ||
| throw "Application token request failed: $($_.Exception.Message)" | ||
| } | ||
|
|
||
| try { | ||
| $agentIdentityTokenResponse = Invoke-RestMethod -Method Post -Uri $tokenUrl -ContentType "application/x-www-form-urlencoded" -Body @{ | ||
| client_id = $AgentClientId | ||
| scope = "api://AzureADTokenExchange/.default" | ||
| grant_type = "client_credentials" | ||
| client_assertion_type = $clientAssertionType | ||
| client_assertion = $applicationToken | ||
| } | ||
| $agentIdentityToken = Get-AccessTokenFromResponse -Response $agentIdentityTokenResponse -StepLabel "Agent identity token request" | ||
| } | ||
| catch { | ||
| throw "Agent identity token request failed: $($_.Exception.Message)" | ||
| } | ||
|
|
||
| try { | ||
| $cuaTokenResponse = Invoke-RestMethod -Method Post -Uri $tokenUrl -ContentType "application/x-www-form-urlencoded" -Body @{ | ||
| client_id = $AgentClientId | ||
| scope = $Scope | ||
| grant_type = "user_fic" | ||
| client_assertion_type = $clientAssertionType | ||
| client_assertion = $applicationToken | ||
| username = $AgentUsername | ||
| user_federated_identity_credential = $agentIdentityToken | ||
| } | ||
| $cuaAccessToken = Get-AccessTokenFromResponse -Response $cuaTokenResponse -StepLabel "CUA token request" | ||
| } | ||
| catch { | ||
| throw "CUA token request failed: $($_.Exception.Message)" | ||
| } | ||
|
|
||
| if ($ShowOid) { | ||
| Write-OidInformation -AccessToken $cuaAccessToken | ||
| } | ||
|
|
||
| if ($SetBearerToken) { | ||
| $env:BEARER_TOKEN = $cuaAccessToken | ||
| Write-Information "Set `$env:BEARER_TOKEN for the current PowerShell process." | ||
|
denzelpfeifer marked this conversation as resolved.
|
||
| } | ||
| else { | ||
| Write-Information "Token acquired. `$env:BEARER_TOKEN was not set because -SetBearerToken was false." | ||
| } | ||
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.