先决条件⚠️警告容器环境建议GitHub Copilot 可以执行工具和命令这些操作可能会与你的系统产生交互。为了安全起见强烈建议在容器化环境中运行此示例例如 Docker、Dev Container以避免对你的本地机器造成意外影响。在开始之前请确保你具备以下先决条件已安装 .NET 10 SDK 或更高版本已安装 GitHub Copilot CLI并且可以在你的 PATH 中访问或者你也可以提供自定义路径配置 GitHub Copilot CLI要使用此示例你需要安装 GitHub Copilot CLI。你可以按照以下地址的说明进行安装GitHub Copilot CLI 上手指南在终端里使用 AI 编程助手安装完成后请确保 copilot 命令已经添加到你的 PATH 中或者通过 CopilotClientOptions 配置一个自定义路径。运行示例如果使用默认配置则无需额外的环境变量。该示例将会使用默认配置创建一个 GitHub Copilot 客户端使用 Copilot SDK 创建一个 AI Agent向 Agent 发送一条消息我们让他打开Chrome浏览器查询微软的股票显示返回结果Console.InputEncoding Encoding.UTF8; Console.OutputEncoding new UTF8Encoding(encoderShouldEmitUTF8Identifier: false); static TaskPermissionRequestResult PromptPermission(PermissionRequest request, PermissionInvocation invocation) { Console.WriteLine($\n[请求权限: {request.Kind}]); Console.Write(同意? (y/n): ); string? input Console.ReadLine()?.Trim().ToUpperInvariant(); PermissionRequestResultKind kind input isY or YES ? PermissionRequestResultKind.Approved : PermissionRequestResultKind.DeniedInteractivelyByUser; return Task.FromResult(new PermissionRequestResult { Kind kind }); } awaitusing CopilotClient copilotClient new(); await copilotClient.StartAsync(); SessionConfig sessionConfig new() { OnPermissionRequest PromptPermission, }; AIAgent agent copilotClient.AsAIAgent(sessionConfig, ownsClient: true); bool useStreaming true; string prompt 帮我打开Chrome浏览器查询微软的股票; Console.WriteLine($User: {prompt}\n); if (useStreaming) { awaitforeach (AgentResponseUpdate update in agent.RunStreamingAsync(prompt)) { Console.Write(update); } Console.WriteLine(); } else { AgentResponse response await agent.RunAsync(prompt); Console.WriteLine(response); }运行效果如下高级用法你可以通过提供额外配置来自定义 Agentawait using CopilotClient copilotClient new(); await copilotClient.StartAsync(); // 创建会话配置并指定模型 SessionConfig sessionConfig new() { Model claude-opus-4.5, Streaming false, OnPermissionRequest PermissionHandler.ApproveAll // or a custom handler }; // 使用扩展方法创建带自定义配置的 Agent AIAgent agent copilotClient.AsAIAgent( sessionConfig, ownsClient: true, id: my-copilot-agent, name: My Copilot Assistant, description: 一个由 GitHub Copilot 驱动的智能 AI 助手 ); // 使用 Agent —— 让它帮我们写代码 AgentResponse response await agent.RunAsync(编写一个 .NET 10 的 C# 单文件 Hello World 程序); Console.WriteLine(response);流式响应Streaming Responses如果你希望获取流式输出边生成边返回await foreach (AgentResponseUpdate update in agent.RunStreamingAsync(编写一个计算斐波那契数列的 C# 函数)) { Console.Write(update.Text); }源代码地址https://github.com/bingbing-gui/dotnet-platform/tree/master/src/09-AI-Agent/Agent-Framework/25-GitHubCopilotSDK