mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
88ea9d08c7
* Initial plan * Update code for Microsoft.Extensions.AI.Abstractions 10.4.0 breaking changes - Rename FunctionApprovalRequestContent → ToolApprovalRequestContent - Rename FunctionApprovalResponseContent → ToolApprovalResponseContent - Rename UserInputRequestContent → ToolApprovalRequestContent - Rename UserInputResponseContent → ToolApprovalResponseContent - Update .FunctionCall property → .ToolCall with FunctionCallContent casts where needed - Update .Id property → .RequestId on the renamed types - Rename FunctionApprovalRequestEventGenerator → ToolApprovalRequestEventGenerator - Rename FunctionApprovalResponseEventGenerator → ToolApprovalResponseEventGenerator Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Update OpenAI 2.9.1, ME.AI 10.4.0, fix breaking API changes Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com> * Fix remaining ME.AI 10.4.0 breaking changes: MCP approval types, .Output→.Outputs Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com> * Use pattern matching with `when` for ToolApprovalRequestContent/FunctionCallContent Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com> * Update Azure.AI.OpenAI to 2.9.0-beta.1 Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com> * Fix remaining GetResponsesClient(model) build failures for Azure.AI.OpenAI 2.9.0-beta.1 Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com> * Address review feedback: remove redundant type checks in TestRequestAgent.cs and fix error message in AIAgentHostExecutor.cs Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com> * Update Azure.AI.Projects to 2.0.0-beta.2 with namespace migration - Azure.AI.Projects 2.0.0-beta.1 → 2.0.0-beta.2 - Azure.AI.Projects.OpenAI → Azure.AI.Extensions.OpenAI (transitive) - Agent types moved to Azure.AI.Projects.Agents namespace - AgentRecord.Versions.Latest → AgentRecord.GetLatestVersion() - OpenAPIFunctionDefinition → OpenApiFunctionDefinition - BingCustomSearchToolParameters → BingCustomSearchToolOptions - MemorySearchPreviewTool.UpdateDelay → UpdateDelayInSecs - Azure.Identity 1.17.1 → 1.19.0 - Microsoft.Identity.Client.Extensions.Msal 4.78.0 → 4.83.1 Co-authored-by: rogerbarreto <19890735+rogerbarreto@users.noreply.github.com> * Fix remaining type renames for Azure.AI.Projects 2.0.0-beta.2 - BrowserAutomationToolParameters → BrowserAutomationToolOptions - MemoryUpdateOptions.UpdateDelay stays as UpdateDelay (not renamed) - WaitForMemoriesUpdateAsync parameter order: pollingInterval before options - AIProjectAgentsOperations → AgentsClient Co-authored-by: rogerbarreto <19890735+rogerbarreto@users.noreply.github.com> * Fix format errors and OpenTelemetry test for ME.AI 10.4.0 - Remove unused 'using Azure.AI.Extensions.OpenAI' and fix import ordering in Agent_With_AzureAIProject/Program.cs - Update OpenTelemetryAgentTests: gen_ai.tool.definitions is now always emitted regardless of EnableSensitiveData per ME.AI 10.4.0 change (dotnet/extensions#7346). Tool definitions are not considered sensitive. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Fix GetRepoFolder() to work in git worktrees Use 'workflow-samples' directory as repo root marker instead of '.git', which fails in worktrees (.git is a file) and also matches too early when a '.github' folder exists in subdirectories. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Fix formatting: remove unused usings and fix import ordering dotnet format applied across 59 impacted projects. Primarily removes unnecessary 'using Azure.AI.Projects' where Azure.AI.Projects.Agents provides all needed types, and fixes import ordering per editorconfig. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Disable AzureAIAgentsPersistent integration tests (#4769) Azure.AI.Agents.Persistent 1.2.0-beta.9 references McpServerToolApprovalResponseContent which was removed in ME.AI 10.4.0 (renamed to ToolApprovalResponseContent), causing TypeLoadException at runtime. Mark all 6 test classes with IntegrationDisabled trait until Persistent ships a version targeting ME.AI 10.4.0+. Upstream fix: https://github.com/Azure/azure-sdk-for-net/pull/56929 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Add README with compatibility note for AzureAI.Persistent (#4769) Documents that Azure.AI.Agents.Persistent 1.2.0-beta.9 is only compatible with ME.AI ≤10.3.0 and OpenAI ≤2.8.0 due to type renames in ME.AI 10.4.0. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Fix file encoding: restore UTF-8 BOM on Persistent test files Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Mark AzureAI.Persistent as IsPackable=false (#4769) Prevent shipping until Azure.AI.Agents.Persistent targets ME.AI 10.4.0+. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Moving IsPackable after import --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com> Co-authored-by: rogerbarreto <19890735+rogerbarreto@users.noreply.github.com>
192 lines
8.9 KiB
C#
192 lines
8.9 KiB
C#
// Copyright (c) Microsoft. All rights reserved.
|
|
|
|
// This sample shows how to use Computer Use Tool with AI Agents.
|
|
|
|
using Azure.AI.Projects;
|
|
using Azure.AI.Projects.Agents;
|
|
using Azure.Identity;
|
|
using Microsoft.Agents.AI;
|
|
using Microsoft.Extensions.AI;
|
|
using OpenAI.Responses;
|
|
|
|
namespace Demo.ComputerUse;
|
|
|
|
internal sealed class Program
|
|
{
|
|
private static async Task Main(string[] args)
|
|
{
|
|
string endpoint = Environment.GetEnvironmentVariable("AZURE_AI_PROJECT_ENDPOINT") ?? throw new InvalidOperationException("AZURE_AI_PROJECT_ENDPOINT is not set.");
|
|
string deploymentName = Environment.GetEnvironmentVariable("AZURE_AI_MODEL_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.
|
|
// Get a client to create/retrieve/delete server side agents with Azure Foundry Agents.
|
|
AIProjectClient aiProjectClient = new(new Uri(endpoint), new DefaultAzureCredential());
|
|
const string AgentInstructions = @"
|
|
You are a computer automation assistant.
|
|
|
|
Be direct and efficient. When you reach the search results page, read and describe the actual search result titles and descriptions you can see.
|
|
";
|
|
|
|
const string AgentNameMEAI = "ComputerAgent-MEAI";
|
|
const string AgentNameNative = "ComputerAgent-NATIVE";
|
|
|
|
// Option 1 - Using ComputerUseTool + AgentOptions (MEAI + AgentFramework)
|
|
// Create AIAgent directly
|
|
AIAgent agentOption1 = await aiProjectClient.CreateAIAgentAsync(
|
|
name: AgentNameMEAI,
|
|
model: deploymentName,
|
|
instructions: AgentInstructions,
|
|
description: "Computer automation agent with screen interaction capabilities.",
|
|
tools: [
|
|
ResponseTool.CreateComputerTool(ComputerToolEnvironment.Browser, 1026, 769).AsAITool(),
|
|
]);
|
|
|
|
// Option 2 - Using PromptAgentDefinition SDK native type
|
|
// Create the server side agent version
|
|
AIAgent agentOption2 = await aiProjectClient.CreateAIAgentAsync(
|
|
name: AgentNameNative,
|
|
creationOptions: new AgentVersionCreationOptions(
|
|
new PromptAgentDefinition(model: deploymentName)
|
|
{
|
|
Instructions = AgentInstructions,
|
|
Tools = { ResponseTool.CreateComputerTool(
|
|
environment: new ComputerToolEnvironment("windows"),
|
|
displayWidth: 1026,
|
|
displayHeight: 769) }
|
|
})
|
|
);
|
|
|
|
// Either invoke option1 or option2 agent, should have same result
|
|
// Option 1
|
|
await InvokeComputerUseAgentAsync(agentOption1);
|
|
|
|
// Option 2
|
|
//await InvokeComputerUseAgentAsync(agentOption2);
|
|
|
|
// Cleanup by agent name removes the agent version created.
|
|
await aiProjectClient.Agents.DeleteAgentAsync(agentOption1.Name);
|
|
await aiProjectClient.Agents.DeleteAgentAsync(agentOption2.Name);
|
|
}
|
|
|
|
private static async Task InvokeComputerUseAgentAsync(AIAgent agent)
|
|
{
|
|
// Load screenshot assets
|
|
Dictionary<string, byte[]> screenshots = ComputerUseUtil.LoadScreenshotAssets();
|
|
|
|
ChatOptions chatOptions = new();
|
|
CreateResponseOptions responseCreationOptions = new()
|
|
{
|
|
TruncationMode = ResponseTruncationMode.Auto
|
|
};
|
|
chatOptions.RawRepresentationFactory = (_) => responseCreationOptions;
|
|
ChatClientAgentRunOptions runOptions = new(chatOptions)
|
|
{
|
|
AllowBackgroundResponses = true,
|
|
};
|
|
|
|
ChatMessage message = new(ChatRole.User, [
|
|
new TextContent("I need you to help me search for 'OpenAI news'. Please type 'OpenAI news' and submit the search. Once you see search results, the task is complete."),
|
|
new DataContent(new BinaryData(screenshots["browser_search"]), "image/png")
|
|
]);
|
|
|
|
// Initial request with screenshot - start with Bing search page
|
|
Console.WriteLine("Starting computer automation session (initial screenshot: cua_browser_search.png)...");
|
|
|
|
// IMPORTANT: Computer-use with the Azure Agents API differs from the vanilla OpenAI Responses API.
|
|
// The Azure Agents API rejects requests that include previous_response_id alongside
|
|
// computer_call_output items. To work around this, each call uses a fresh session (avoiding
|
|
// previous_response_id) and re-sends the full conversation context as input items instead.
|
|
AgentSession session = await agent.CreateSessionAsync();
|
|
AgentResponse response = await agent.RunAsync(message, session: session, options: runOptions);
|
|
|
|
// Main interaction loop
|
|
const int MaxIterations = 10;
|
|
int iteration = 0;
|
|
// Initialize state machine
|
|
SearchState currentState = SearchState.Initial;
|
|
|
|
while (true)
|
|
{
|
|
// Poll until the response is complete.
|
|
while (response.ContinuationToken is { } token)
|
|
{
|
|
// Wait before polling again.
|
|
await Task.Delay(TimeSpan.FromSeconds(2));
|
|
|
|
// Continue with the token.
|
|
runOptions.ContinuationToken = token;
|
|
|
|
response = await agent.RunAsync(session, runOptions);
|
|
}
|
|
|
|
// Clear the continuation token so the next RunAsync call is a fresh request.
|
|
runOptions.ContinuationToken = null;
|
|
|
|
Console.WriteLine($"Agent response received (ID: {response.ResponseId})");
|
|
|
|
if (iteration >= MaxIterations)
|
|
{
|
|
Console.WriteLine($"\nReached maximum iterations ({MaxIterations}). Stopping.");
|
|
break;
|
|
}
|
|
|
|
iteration++;
|
|
Console.WriteLine($"\n--- Iteration {iteration} ---");
|
|
|
|
// Check for computer calls in the response
|
|
IEnumerable<ComputerCallResponseItem> computerCallResponseItems = response.Messages
|
|
.SelectMany(x => x.Contents)
|
|
.Where(c => c.RawRepresentation is ComputerCallResponseItem and not null)
|
|
.Select(c => (ComputerCallResponseItem)c.RawRepresentation!);
|
|
|
|
ComputerCallResponseItem? firstComputerCall = computerCallResponseItems.FirstOrDefault();
|
|
if (firstComputerCall is null)
|
|
{
|
|
Console.WriteLine("No computer call actions found. Ending interaction.");
|
|
Console.WriteLine($"Final Response: {response}");
|
|
break;
|
|
}
|
|
|
|
// Process the first computer call response
|
|
ComputerCallAction action = firstComputerCall.Action;
|
|
string currentCallId = firstComputerCall.CallId;
|
|
|
|
Console.WriteLine($"Processing computer call (ID: {currentCallId})");
|
|
|
|
// Simulate executing the action and taking a screenshot
|
|
(SearchState CurrentState, byte[] ImageBytes) screenInfo = ComputerUseUtil.HandleComputerActionAndTakeScreenshot(action, currentState, screenshots);
|
|
currentState = screenInfo.CurrentState;
|
|
|
|
Console.WriteLine("Sending action result back to agent...");
|
|
|
|
// Build the follow-up messages with full conversation context.
|
|
// The Azure Agents API rejects previous_response_id when computer_call_output items are
|
|
// present, so we must re-send all prior output items (reasoning, computer_call, etc.)
|
|
// as input items alongside the computer_call_output to maintain conversation continuity.
|
|
List<ChatMessage> followUpMessages = [];
|
|
|
|
// Re-send all response output items as an assistant message so the API has full context
|
|
List<AIContent> priorOutputContents = response.Messages
|
|
.SelectMany(m => m.Contents)
|
|
.ToList();
|
|
followUpMessages.Add(new ChatMessage(ChatRole.Assistant, priorOutputContents));
|
|
|
|
// Add the computer_call_output as a user message
|
|
AIContent callOutput = new()
|
|
{
|
|
RawRepresentation = new ComputerCallOutputResponseItem(
|
|
currentCallId,
|
|
output: ComputerCallOutput.CreateScreenshotOutput(new BinaryData(screenInfo.ImageBytes), "image/png"))
|
|
};
|
|
followUpMessages.Add(new ChatMessage(ChatRole.User, [callOutput]));
|
|
|
|
// Create a fresh session so ConversationId does not carry over a previous_response_id.
|
|
// Without this, the Azure Agents API returns an error when computer_call_output is present.
|
|
session = await agent.CreateSessionAsync();
|
|
response = await agent.RunAsync(followUpMessages, session: session, options: runOptions);
|
|
}
|
|
}
|
|
}
|