chore: import upstream snapshot with attribution
dotnet-build-and-test / dotnet-test-functions (push) Has been cancelled
dotnet-build-and-test / paths-filter (push) Has been cancelled
dotnet-build-and-test / dotnet-build (Debug, windows-latest, net9.0) (push) Has been cancelled
dotnet-build-and-test / dotnet-build (Release, ubuntu-latest, net10.0) (push) Has been cancelled
dotnet-build-and-test / dotnet-build (Release, ubuntu-latest, net8.0) (push) Has been cancelled
dotnet-build-and-test / dotnet-build (Release, windows-latest, net472) (push) Has been cancelled
dotnet-build-and-test / dotnet-test (Release, integration, true, ubuntu-latest, net10.0) (push) Has been cancelled
dotnet-build-and-test / dotnet-test (Release, integration, true, windows-latest, net472) (push) Has been cancelled
dotnet-build-and-test / dotnet-foundry-hosted-it (push) Has been cancelled
dotnet-build-and-test / dotnet-build-and-test-check (push) Has been cancelled
dotnet-build-and-test / Integration Test Report (push) Has been cancelled
CodeQL / Analyze (csharp) (push) Has been cancelled
CodeQL / Analyze (python) (push) Has been cancelled
dotnet-build-and-test / dotnet-test-functions (push) Has been cancelled
dotnet-build-and-test / paths-filter (push) Has been cancelled
dotnet-build-and-test / dotnet-build (Debug, windows-latest, net9.0) (push) Has been cancelled
dotnet-build-and-test / dotnet-build (Release, ubuntu-latest, net10.0) (push) Has been cancelled
dotnet-build-and-test / dotnet-build (Release, ubuntu-latest, net8.0) (push) Has been cancelled
dotnet-build-and-test / dotnet-build (Release, windows-latest, net472) (push) Has been cancelled
dotnet-build-and-test / dotnet-test (Release, integration, true, ubuntu-latest, net10.0) (push) Has been cancelled
dotnet-build-and-test / dotnet-test (Release, integration, true, windows-latest, net472) (push) Has been cancelled
dotnet-build-and-test / dotnet-foundry-hosted-it (push) Has been cancelled
dotnet-build-and-test / dotnet-build-and-test-check (push) Has been cancelled
dotnet-build-and-test / Integration Test Report (push) Has been cancelled
CodeQL / Analyze (csharp) (push) Has been cancelled
CodeQL / Analyze (python) (push) Has been cancelled
This commit is contained in:
+33
@@ -0,0 +1,33 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFrameworks>net10.0</TargetFrameworks>
|
||||
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<NoWarn>$(NoWarn);OPENAICUA001;MEAI001</NoWarn>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Azure.Identity" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Console" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\..\..\..\src\Microsoft.Agents.AI.Foundry\Microsoft.Agents.AI.Foundry.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Update="Assets\cua_browser_search.jpg">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</None>
|
||||
<None Update="Assets\cua_search_results.jpg">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</None>
|
||||
<None Update="Assets\cua_search_typed.jpg">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
BIN
Binary file not shown.
|
After Width: | Height: | Size: 402 KiB |
BIN
Binary file not shown.
|
After Width: | Height: | Size: 85 KiB |
BIN
Binary file not shown.
|
After Width: | Height: | Size: 357 KiB |
+93
@@ -0,0 +1,93 @@
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
using Microsoft.Extensions.AI;
|
||||
using OpenAI.Responses;
|
||||
|
||||
namespace Demo.ComputerUse;
|
||||
|
||||
/// <summary>
|
||||
/// Enum for tracking the state of the simulated web search flow.
|
||||
/// </summary>
|
||||
internal enum SearchState
|
||||
{
|
||||
Initial, // Browser search page
|
||||
Typed, // Text entered in search box
|
||||
PressedEnter // Enter key pressed, transitioning to results
|
||||
}
|
||||
|
||||
internal static class ComputerUseUtil
|
||||
{
|
||||
internal static async Task<Dictionary<string, string>> UploadScreenshotAssetsAsync(IHostedFileClient fileClient)
|
||||
{
|
||||
string assetsDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Assets");
|
||||
|
||||
(string key, string fileName)[] files =
|
||||
[
|
||||
("browser_search", "cua_browser_search.jpg"),
|
||||
("search_typed", "cua_search_typed.jpg"),
|
||||
("search_results", "cua_search_results.jpg")
|
||||
];
|
||||
|
||||
Dictionary<string, string> screenshots = [];
|
||||
|
||||
foreach (var (key, fileName) in files)
|
||||
{
|
||||
HostedFileContent result = await fileClient.UploadAsync(
|
||||
Path.Combine(assetsDir, fileName), new HostedFileClientOptions() { Purpose = "assistants" });
|
||||
screenshots[key] = result.FileId;
|
||||
}
|
||||
|
||||
return screenshots;
|
||||
}
|
||||
|
||||
internal static async Task EnsureDeleteScreenshotAssetsAsync(IHostedFileClient fileClient, Dictionary<string, string> screenshots)
|
||||
{
|
||||
foreach (var (_, fileId) in screenshots)
|
||||
{
|
||||
try
|
||||
{
|
||||
await fileClient.DeleteAsync(fileId);
|
||||
}
|
||||
catch
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Simulates executing a computer action by advancing the state
|
||||
/// and returning the screenshot file ID for the new state.
|
||||
/// </summary>
|
||||
internal static async Task<(SearchState State, string FileId)> GetScreenshotAsync(
|
||||
ComputerCallAction action,
|
||||
SearchState currentState,
|
||||
Dictionary<string, string> screenshots)
|
||||
{
|
||||
if (action.Kind == ComputerCallActionKind.Wait)
|
||||
{
|
||||
await Task.Delay(TimeSpan.FromSeconds(5));
|
||||
}
|
||||
|
||||
SearchState nextState = action.Kind switch
|
||||
{
|
||||
ComputerCallActionKind.Click when currentState == SearchState.Typed => SearchState.PressedEnter,
|
||||
ComputerCallActionKind.Type when action.TypeText is not null => SearchState.Typed,
|
||||
ComputerCallActionKind.KeyPress when IsEnterKey(action) => SearchState.PressedEnter,
|
||||
_ => currentState
|
||||
};
|
||||
|
||||
string imageKey = nextState switch
|
||||
{
|
||||
SearchState.PressedEnter => "search_results",
|
||||
SearchState.Typed => "search_typed",
|
||||
_ => "browser_search"
|
||||
};
|
||||
|
||||
return (nextState, screenshots[imageKey]);
|
||||
}
|
||||
|
||||
private static bool IsEnterKey(ComputerCallAction action) =>
|
||||
action.KeyPressKeyCodes is not null &&
|
||||
(action.KeyPressKeyCodes.Contains("Return", StringComparer.OrdinalIgnoreCase) ||
|
||||
action.KeyPressKeyCodes.Contains("Enter", StringComparer.OrdinalIgnoreCase));
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
// This sample shows how to use the Computer Use tool with AIProjectClient.AsAIAgent(...).
|
||||
|
||||
using Azure.AI.Projects;
|
||||
using Azure.Identity;
|
||||
using Demo.ComputerUse;
|
||||
using Microsoft.Agents.AI;
|
||||
using Microsoft.Agents.AI.Foundry;
|
||||
using Microsoft.Extensions.AI;
|
||||
using OpenAI.Responses;
|
||||
|
||||
string endpoint = Environment.GetEnvironmentVariable("FOUNDRY_PROJECT_ENDPOINT") ?? throw new InvalidOperationException("FOUNDRY_PROJECT_ENDPOINT is not set.");
|
||||
string deploymentName = Environment.GetEnvironmentVariable("AZURE_AI_COMPUTER_USE_DEPLOYMENT_NAME") ?? "computer-use-preview";
|
||||
|
||||
// WARNING: DefaultAzureCredential is convenient for development but requires careful consideration in production.
|
||||
// In production, consider using a specific credential (e.g., ManagedIdentityCredential) to avoid
|
||||
// latency issues, unintended credential probing, and potential security risks from fallback mechanisms.
|
||||
AIProjectClient projectClient = new(new Uri(endpoint), new DefaultAzureCredential());
|
||||
using IHostedFileClient fileClient = projectClient.GetProjectOpenAIClient().AsIHostedFileClient();
|
||||
|
||||
AIAgent agent = projectClient.AsAIAgent(
|
||||
model: deploymentName,
|
||||
name: "ComputerAgent",
|
||||
instructions: "You are a computer automation assistant.",
|
||||
tools: [FoundryAITool.CreateComputerTool(ComputerToolEnvironment.Browser, 1026, 769)]);
|
||||
|
||||
Dictionary<string, string> screenshots = [];
|
||||
|
||||
try
|
||||
{
|
||||
// Upload pre-captured screenshots that simulate browser state transitions.
|
||||
screenshots = await ComputerUseUtil.UploadScreenshotAssetsAsync(fileClient);
|
||||
|
||||
// Enable auto-truncation for the Responses API.
|
||||
ChatClientAgentRunOptions runOptions = new()
|
||||
{
|
||||
ChatOptions = new ChatOptions
|
||||
{
|
||||
RawRepresentationFactory = (_) => new CreateResponseOptions() { TruncationMode = ResponseTruncationMode.Auto },
|
||||
}
|
||||
};
|
||||
|
||||
// Send the initial request with a screenshot of the browser.
|
||||
ChatMessage message = new(ChatRole.User, [
|
||||
new TextContent("Search for 'OpenAI news'. Type it and submit. Once you see results, the task is complete."),
|
||||
new AIContent() { RawRepresentation = ResponseContentPart.CreateInputImagePart(imageFileId: screenshots["browser_search"], imageDetailLevel: ResponseImageDetailLevel.High) }
|
||||
]);
|
||||
|
||||
Console.WriteLine("Starting computer use session...");
|
||||
|
||||
AgentSession session = await agent.CreateSessionAsync();
|
||||
AgentResponse response = await agent.RunAsync(message, session: session, options: runOptions);
|
||||
|
||||
SearchState currentState = SearchState.Initial;
|
||||
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
// Find the next computer call action.
|
||||
ComputerCallResponseItem? computerCall = response.Messages
|
||||
.SelectMany(m => m.Contents)
|
||||
.Select(c => c.RawRepresentation as ComputerCallResponseItem)
|
||||
.FirstOrDefault(item => item is not null);
|
||||
|
||||
if (computerCall is null)
|
||||
{
|
||||
if (currentState == SearchState.PressedEnter)
|
||||
{
|
||||
Console.WriteLine("No more computer actions. Done.");
|
||||
Console.WriteLine(response);
|
||||
break;
|
||||
}
|
||||
|
||||
// Check if the agent is asking for confirmation to proceed, and if so, respond affirmatively.
|
||||
TextContent? textContent = response.Messages
|
||||
.Where(m => m.Role == ChatRole.Assistant)
|
||||
.SelectMany(m => m.Contents.OfType<TextContent>())
|
||||
.FirstOrDefault();
|
||||
|
||||
if (textContent?.Text is { } text && (
|
||||
text.Contains("Would you like me") ||
|
||||
text.Contains("Should I") ||
|
||||
text.Contains("proceed") ||
|
||||
text.Contains('?')))
|
||||
{
|
||||
response = await agent.RunAsync("Please proceed.", session, runOptions);
|
||||
continue;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
Console.WriteLine($"[{i + 1}] Action: {computerCall!.Action.Kind}");
|
||||
|
||||
// Simulate the action and get the resulting screenshot.
|
||||
(currentState, string fileId) = await ComputerUseUtil.GetScreenshotAsync(computerCall.Action, currentState, screenshots);
|
||||
|
||||
// Send the screenshot back as the computer call output.
|
||||
AIContent callOutput = new()
|
||||
{
|
||||
RawRepresentation = new ComputerCallOutputResponseItem(
|
||||
computerCall.CallId,
|
||||
output: ComputerCallOutput.CreateScreenshotOutput(screenshotImageFileId: fileId))
|
||||
};
|
||||
|
||||
response = await agent.RunAsync([new ChatMessage(ChatRole.User, [callOutput])], session: session, options: runOptions);
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
await ComputerUseUtil.EnsureDeleteScreenshotAssetsAsync(fileClient, screenshots);
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
# Computer Use with the Responses API
|
||||
|
||||
This sample shows how to use the Computer Use tool with `AIProjectClient.AsAIAgent(...)`.
|
||||
|
||||
## What this sample demonstrates
|
||||
|
||||
- Using `FoundryAITool.CreateComputerTool()` to add computer use capabilities
|
||||
- Processing computer call actions (click, type, key press)
|
||||
- Managing the computer use interaction loop with screenshots
|
||||
|
||||
For more information, see [Use the computer tool](https://learn.microsoft.com/en-us/azure/foundry/agents/how-to/tools/computer-use?pivots=csharp).
|
||||
|
||||
## How the simulation works
|
||||
|
||||
In a real computer use scenario, the model controls a virtual keyboard and mouse to interact with a live browser — typing text, clicking buttons, and pressing keys. The host application captures a screenshot after each action and sends it back to the model so it can decide what to do next.
|
||||
|
||||
**This sample does not connect to a real browser.** Instead, it intercepts the model's actions and returns pre-captured screenshots as if the actions were actually performed. No real typing, clicking, or key presses happen — the sample fakes the environment so you can explore the computer use protocol without any browser automation setup.
|
||||
|
||||
### State transitions
|
||||
|
||||
The model receives a screenshot as input, analyzes it, and responds with a computer action as output. The sample maps each action to a new state and returns the corresponding screenshot:
|
||||
|
||||
| Step | Model Action | What Happens | Screenshot Sent Back to Model |
|
||||
|------|-----------------|-------------------------------------------|--------------------------------------------------------------|
|
||||
| 1 | | Session starts with the user prompt | `cua_browser_search.jpg` — empty search page |
|
||||
| 2 | Click | Model clicks the search box to focus it | `cua_browser_search.jpg` — same page |
|
||||
| 3 | Type | Model types the search query into the box | `cua_search_typed.jpg` — search text visible in the box |
|
||||
| 3a | *(text response)* | Model may ask for confirmation instead of acting | `cua_search_typed.jpg` — same page |
|
||||
| 4 | KeyPress Enter | Model presses Enter to submit the search | `cua_search_results.jpg` — search results page |
|
||||
|
||||
### Interaction loop
|
||||
|
||||
1. The user prompt and the initial screenshot (`cua_browser_search.jpg` — an empty search page) are sent to the model as input.
|
||||
2. The model analyzes the screenshot and responds with a computer action (e.g., click on the search box to focus it, then type search text, then press Enter).
|
||||
3. The sample intercepts the action, advances the state, and sends back the next pre-captured screenshot as if the action was performed on a real browser.
|
||||
4. Steps 2–3 repeat until the model stops requesting actions or the iteration limit is reached.
|
||||
|
||||
## Prerequisites
|
||||
|
||||
- .NET 10 SDK or later
|
||||
- Microsoft Foundry service endpoint and deployment configured
|
||||
- An authenticated Azure identity (for example, sign in with `az login`)
|
||||
|
||||
Set the following environment variables:
|
||||
|
||||
```powershell
|
||||
$env:FOUNDRY_PROJECT_ENDPOINT="https://your-foundry-service.services.ai.azure.com/api/projects/your-foundry-project"
|
||||
$env:AZURE_AI_COMPUTER_USE_DEPLOYMENT_NAME="computer-use-preview"
|
||||
```
|
||||
|
||||
## Run the sample
|
||||
|
||||
```powershell
|
||||
dotnet run
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user