惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
WordPress大学
WordPress大学
博客园 - 【当耐特】
Engineering at Meta
Engineering at Meta
IT之家
IT之家
Apple Machine Learning Research
Apple Machine Learning Research
小众软件
小众软件
美团技术团队
S
SegmentFault 最新的问题
The GitHub Blog
The GitHub Blog
Martin Fowler
Martin Fowler
Recorded Future
Recorded Future
H
Help Net Security
aimingoo的专栏
aimingoo的专栏
Y
Y Combinator Blog
博客园_首页
A
About on SuperTechFans
MongoDB | Blog
MongoDB | Blog
阮一峰的网络日志
阮一峰的网络日志
V
Visual Studio Blog
D
DataBreaches.Net
人人都是产品经理
人人都是产品经理
大猫的无限游戏
大猫的无限游戏
B
Blog
The Register - Security
The Register - Security
I
InfoQ
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
雷峰网
雷峰网
Last Week in AI
Last Week in AI
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
月光博客
月光博客
酷 壳 – CoolShell
酷 壳 – CoolShell
Blog — PlanetScale
Blog — PlanetScale
N
Netflix TechBlog - Medium
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
有赞技术团队
有赞技术团队
Stack Overflow Blog
Stack Overflow Blog
Jina AI
Jina AI
Security Archives - TechRepublic
Security Archives - TechRepublic
Hacker News - Newest:
Hacker News - Newest: "LLM"
U
Unit 42
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
W
WeLiveSecurity
Latest news
Latest news
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
博客园 - 叶小钗
L
Lohrmann on Cybersecurity
博客园 - Franky
Recent Commits to openclaw:main
Recent Commits to openclaw:main
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO

博客园_首页

Plist 二进制格式 Milvus 和 PGVector,哪个更好? OpenClaw 已过时?在 VS Code 中运行 Hermes Agent! 第30篇文章:一个大三计科生的自白 Manim如何在数学公式中完美显示中文? Docker 部署 RocketMQ 5 并发编程核心概念辨析 C#事务处理最佳实践:别再让“主表存了、明细丢了”的破事发生 CLI 是什么?为什么大厂突然集体卷命令行? 【从0到1构建一个ClaudeAgent】协作-自主Agent UIImageView 设置图片不生效的原因排查 最小二乘问题详解20:无先验约束下的增量式SFM自由网平差 痞子衡嵌入式:大话双核i.MXRT1180之XIP应用里借助MU实现可靠Flash IAP的方法 AI Chat 封装, SemanticKerne.AiProvider.Unified 已发布 Windows下右键编辑js文件无法打开记事本——在注册表中使用环境变量 在后台服务中使用 Scoped 服务,为什么总是报错? H200 安装驱动并使用sglang启动模型 wireshark 抓包Trap上报告警内容 我用 AI 辅助开发了一系列小工具(2):图片压缩工具 [A Primer On MC and CC] 2.1 Memory Consistency 1 - 指令重排序和 SC 模型 Oracle数据库SCN推进技术详解与实践指南 玩转控件:封装个带图片的Label控件 Claude Code 4.7 真正该升级的不是模型,而是你的工作流 前端小白一句话,AI 帮我做了个颜值拉满的桌面媒体播放器。当代码不再是门槛,一句话编程就是现实。 5. WorkBuddy: 小龙虾的灵魂三件套,让你的小龙虾不只是工具 SQLite 分片方案实战:三种分片策略的深度对比 告别简陋 UI!一款基于 Fluent Design 和基于 WinUI 的开源免费、现代化的 Avalonia UI 控件库 关于二进制排列组合枚举的总结 AI开发-python-LangGraph框架(3-27-LangGraph从零实现大模型智能决策工作流) ElasticSearch主分片和副本分片概念详解 【002】HTTPS 粗解:证书、TLS 握手与对后端配置的影响 Hermes Agent 一周暴涨五万 Star,但我劝你别急着追 明明连接的是Redis的DB0,为什么能查到DB3的数据? 【从0到1构建一个ClaudeAgent】协作-Agent团队 熟悉电子元器件之后,电子小白下一步该怎么走? MAF快速入门(23)通过C#类定义Skills .NET 高级开发 | 手写一个对象映射框架 FastAPI数据库ORM怎么选?我肝了三个Demo后,终于不再纠结了 mysqldump 参数拾遗:在遗忘与铭记之间 C# .NET 周刊|2026年3月5期 Claude code入门 - 陈彦斌 一文学习入门 ThingsBoard 开源物联网平台 GitHub 热门项目 | 2026年04月16日 如何为GIT设置全局勾子,为每次提交追加信息 Number.isFinite和isFinite与isNaN()和Number.isNaN的区别 PortSwigger SQL注入LAB2 推荐一个测试人必备的Skills,从功能到性能全搞定(附详细实操和安装下载方式) 筑基期:掌握Odoo基础核心知识点02(Odoo XML 开发方式详解) GLM模型这么火,咱们用vllm也咧一个呗! 深入理解 AbortController:从底层原理到跨语言设计哲学 字符串学习笔记 多租户系统框架的基础模块设计和分析设计 Apache SeaTunnel Zeta 为什么能做到“又快又稳”? AI开发-python-LangGraph框架(3-26-LangGraph基本概念及第一个简单样例) Vue 3 组件通信,别只会用 Props 和 Emits 了,这几个狠活儿你得看看 ElasticSearch7.X版本配置密码 用Manim实现动态交点计算--从一个动点问题说起 团结引擎+Addressable+Instant Game打包抖音小游戏 function call 实战:让 LLM 自动判断 pod 异常、调用日志工具并完成故障分析 bubseek —— 让 Agent 的足迹,变成团队的洞察 通过 C# 读取并导出 PDF 书签 如何用 GitHub Actions 实现 Steam 自动化发布 【从0到1构建一个ClaudeAgent】并发-后台任务 .NET 高级开发 | 定制 ASP.NET Core 框架 电子小白:什么是运算放大器(运放) zero2Agent:面向大厂面试的 Agent 工程教程,从概念到生产的完整学习路线 堆上的ORW HC32F460 USB CDC通信异常:非对齐访问异常排查 20260413-Hyperbridge 攻击事件:发生在默克尔山上的验证绕过 那些喊着AI 要淘汰你的人,正在靠你的焦虑赚大钱! 深度学习进阶(八)Swin Transformer 最小二乘问题详解19:带先验约束的增量式SFM优化与实现 SnapTranslate 3.0 正式发布:全局划词翻译 + 完整英语学习闭环,一站式搞定查词、记词、复习 工作的意义、工作的困难认知再思考 .NET + AI 进阶实战:基于类的技能开发 - 打造可治理的 Agent 能力模块 【从0到1构建一个ClaudeAgent】规划与协调-技能 上周热点回顾(4.6-4.12) 电子小白的工具三件套:面包板、杜邦线、万能板 单表五亿数据的查询优化 | Mysql、StarRocks 2. WorkBuddy:从“我是谁”到“帮我干活” C# 如何减少代码运行时间:7 个实战技巧 基于HelixToolkit.SharpDX 渲染3D模型 - 笺上知微 从零开始的双臂具身VLA起源及现阶段发展综述 - SkyXZ 记对 xonsh shell 的使用, 脚本编写, 迁移及调优 - pluvium27 受够了Vibe Coding的失控?换个起点,让AI事半功倍 从开始配置漏洞环境到漏洞复现流程 - 難しい 关于10年工作经验的程序员对OpenClaw的实战经验分享以及看法 - 虚无境 Any metadata 的内存布局 C# .NET 周刊|2026年3月2期 - InCerry 我帮你测过了,测试圈排名第二的 Skill 依然很牛逼 Skill Discovery | 无监督技能发现的经典工作总结 - MoonOut 上下文工程是什么?过时了么?一文讲明白! - 一枫说码 开了 TUN 模式还是直连?90% 的人都踩过这个坑 AScript扩展多种脚本语言 - rockey627 AI 学习笔记:Agent 的记忆机制 你能被装进一个文件里吗?——7 万人把同事"蒸馏"成了 AI - 我没有三颗心脏 Claude Code 通关手册(七):给 AI 装上技能包——Skills 完全指南 - 暮色之狐 在浏览器中快速编辑代码:VSCode Web 集成实践 - Newbe36524 蒸馏自己 skill?基于 Deepseek 的蒸馏器,丐版蒸馏方式,简单便捷 - To_Carpe_Diem Spring AI Aliababa和AgentScope,哪个更好? - 苏三说技术
[MAF的Agent管道详解-06]ChatClientAgent对IChatClient和输入输出增强管道的整合
Artech · 2026-06-01 · via 博客园_首页

上面我们介绍了与LLM交互的IChatClient管道、持久化对话消息的ChatHistoryProvider、以及实现输入和输出增强的AIContextProvider,接下来我们来看看ChatClientAgent是如何将它们整合在一起的。

1. ChatClientAgent的构建

如下面的代码片段所示,ChatClientAgent定义了两个构造函数,我们可以指定一个IChatClient对象、一个ChatClientAgentOptions以及可选的ILoggerFactoryIServiceProvider对象来创建一个ChatClientAgent对象。ChatClientAgentOptions承载的配置选项包括Agent的名称、描述、指令、工具集等也可以通过调用另一个构造函数利用对应的参数来提供。

public sealed class ChatClientAgent : AIAgent
{    
    public IChatClient ChatClient { get; }
    public ChatHistoryProvider? ChatHistoryProvider { get; private set; }
    public IReadOnlyList<AIContextProvider>? AIContextProviders { get; }
    protected override string? IdCore { get; }
    public override string? Name { get; }
    public override string? Description { get; }
    public string? Instructions { get; }

    public ChatClientAgent(
        IChatClient chatClient,
        string? instructions = null,
        string? name = null,
        string? description = null,
        IList<AITool>? tools = null,
        ILoggerFactory? loggerFactory = null,
        IServiceProvider? services = null);

    public ChatClientAgent(
        IChatClient chatClient,
        ChatClientAgentOptions? options,
        ILoggerFactory? loggerFactory = null,
        IServiceProvider? services = null)
    ...

1.1 ChatClientAgentOptions

ChatClientAgentChatClient之外的其他属性都会从ChatClientAgentOptions中获取。如下所示的是ChatClientAgentOptions类型的定义:

public sealed class ChatClientAgentOptions
{
    public string? Id { get; set; }
    public string? Name { get; set; }
    public string? Description { get; set; }
    public ChatOptions? ChatOptions { get; set; }
    public ChatHistoryProvider? ChatHistoryProvider { get; set; }
    public IEnumerable<AIContextProvider>? AIContextProviders { get; set; }
    public bool UseProvidedChatClientAsIs { get; set; }
    public bool ClearOnChatHistoryProviderConflict { get; set; } = true;
    public bool WarnOnChatHistoryProviderConflict { get; set; } = true;
    public bool ThrowOnChatHistoryProviderConflict { get; set; } = true;
    public bool RequirePerServiceCallChatHistoryPersistence { get; set; }
}

具体的配置选项包括:

  • Id:Agent的唯一标识符,如果没有提供,系统会自动生成一个GUID字符串作为Id;
  • Name:Agent的名称,用于描述Agent的功能或角色;
  • Description:Agent的描述信息,用于提供更详细的Agent功能说明;
  • ChatOptions:一个ChatOptions对象,调用IChatClient时会使用的选项配置);
  • ChatHistoryProvider:一个ChatHistoryProvider对象,用于管理Agent的对话历史;
  • AIContextProviders:一个AIContextProvider对象的集合,用于提供Agent的上下文定制功能;
  • UseProvidedChatClientAsIs:一个布尔值,指示是否直接使用提供的IChatClient对象而不进行任何包装或增强,默认为false;
  • RequirePerServiceCallChatHistoryPersistence:一个布尔值,指示是否要求每次服务调用都持久化ChatHistoryProvider提供的对话历史,默认为false;

1.2 构建规则

ChatClientAgent在构建过程中会采用如下的规则:

  • 如果指定的ChatClientAgentOptions没有提供ChatHistoryProvider,会默认使用InMemoryChatHistoryProvider作为ChatHistoryProvider
  • 如果指定的ChatClientAgentOptionsUseProvidedChatClientAsIs属性被设置成True,会直接使用提供的IChatClient对象;否则会利用配置选项添加响应的中间件:

默认利用中间件对IChatClient的装饰规则如下:

  • 自动添加一个FunctionInvokingChatClient中间件来支持函数调用功能,正是这个中间件实现ReAct循环;
  • ChatClientAgentOptionsTools属性提供的工具集将会作为FunctionInvokingChatClientAdditionalTools;
  • 如果ChatClientAgentOptionsRequirePerServiceCallChatHistoryPersistence属性被设置成True,会自动添加一个PerServiceCallChatHistoryPersistingChatClient中间件。对于每次Agent调用,这个中间件都会确保对话历史被持久化。与此同时,会屏蔽ChatHistoryProvider针对对话历史的持久化,否则就出现了重复的持久化操作;

如果IChatClient对象已经内置了对话历史管理功能(比如采用OpenAI的Responses API),指定的ChatClientAgentOptions又同时提供了ChatHistoryProvider,此时就会产生冲突,ChatClientAgentOptions提供了三个选项来处理这种冲突:

  • ClearOnChatHistoryProviderConflict:是否直接将ChatClientAgentOptionsChatHistoryProvider属性设置为Null,默认为true
  • WarnOnChatHistoryProviderConflict:是否输出一个警告日志,默认为true
  • ThrowOnChatHistoryProviderConflict:是否抛出异常,默认为true

在如下的演示程序中,我们调用OpenAIClientGetResponsesClient方法来获取一个ResponsesClient对象,并将它转换成一个IChatClient对象来创建ChatClientAgent。在创建ChatClientAgent时,我们提供了一个ChatClientAgentOptions对象,并指定了一个InMemoryChatHistoryProvider作为ChatHistoryProvider

using dotenv.net;
using Microsoft.Agents.AI;
using Microsoft.Extensions.AI;
using OpenAI;
using System.ClientModel;

DotEnv.Load();
var model = Environment.GetEnvironmentVariable("MODEL")!;
var apiKey = Environment.GetEnvironmentVariable("API_KEY")!;
var openAIUrl = Environment.GetEnvironmentVariable("OPENAI_URL")!;
var prompt = "目前的天气是晴天,气温25°C。请给我一些穿衣建议。";

var openAIClient = new OpenAIClient(
    credential: new ApiKeyCredential(key: apiKey),
    options: new OpenAIClientOptions
    {
        Endpoint = new Uri(openAIUrl)
    });

var chatClient = openAIClient.GetResponsesClient()
    .AsIChatClient(defaultModelId:model);
var agent = new ChatClientAgent(
    chatClient:chatClient,
    options: new ChatClientAgentOptions { ChatHistoryProvider = new InMemoryChatHistoryProvider() });
var session = await agent.CreateSessionAsync();
await agent.RunAsync(message: prompt,session:session);

我们调用ChatClientAgent的RunAsync方法来发送一个请求消息,此时会得到如下所示的InvalidOperationException异常,提示ChatClientAgentOptionsChatHistoryProvider属性与IChatClient对象内置的对话历史管理功能产生了冲突。

Alternative Text

2. 会话的创建和序列化

ChatClientAgent创建的Session类型为ChatClientAgentSession,它在基类AgentSession的基础上增加了一个ConversationId属性,用于标识与LLM交互的会话ID。所以ChatClientAgent重写了基类的CreateSessionCoreAsync方法来创建一个ChatClientAgentSession对象,并重写了SerializeSessionCoreAsyncDeserializeSessionCoreAsync方法来支持ChatClientAgentSession对象的序列化和反序列化。它还额外提供了一个CreateSessionAsync方法来创建一个带有指定ConversationIdChatClientAgentSession对象。

public sealed class ChatClientAgent
{
    protected override ValueTask<AgentSession> CreateSessionCoreAsync(CancellationToken cancellationToken = default);    
    public ValueTask<AgentSession> CreateSessionAsync(
        string conversationId,
        CancellationToken cancellationToken = default);
    protected override ValueTask<JsonElement> SerializeSessionCoreAsync(
        AgentSession session,
        JsonSerializerOptions? jsonSerializerOptions = null,
        CancellationToken cancellationToken = default);
    protected override ValueTask<AgentSession> DeserializeSessionCoreAsync(
        JsonElement serializedState,
        JsonSerializerOptions? jsonSerializerOptions = null,
        CancellationToken cancellationToken = default)
}

public sealed class ChatClientAgentSession : AgentSession
{  
    public string? ConversationId{get;set;}
}

3. 执行流程

除了具有专属的AgentSession类型之外,ChatClientAgent还有一个属于直接的AgentRunOptions类型,名为ChatClientAgentRunOptions。它在基类AgentRunOptions的基础上增加了一个ChatOptions属性,返回的ChatOptions将会作为调用IChatClientGetResponseAsyncGetStreamingResponseAsync方法的参数。另一个ChatClientFactory属性返回的Func<IChatClient, IChatClient>委托可以用来为指定的IChatClient对象装饰任意的中间件。

如果基于IChatClient的中间件适用于每次调用,就应该将它置于预先构建好的IChatClient管道中,并利用它来创建ChatClientAgent。如果中间件只适用于某些特殊的调用,ChatClientAgentRunOptionsChatClientFactory赋予我们对它进行动态注册的机会。ChatClientAgent重新定义了一系列重载的RunAsyncRunStreamingAsync方法来接受ChatClientAgentRunOptions参数。

public sealed class ChatClientAgentRunOptions : AgentRunOptions
{
    public ChatOptions? ChatOptions { get; set; }
    public Func<IChatClient, IChatClient>? ChatClientFactory { get; set; }
}

public sealed class ChatClientAgent
{
    public Task<AgentResponse> RunAsync(
        AgentSession? session,
        ChatClientAgentRunOptions? options,
        CancellationToken cancellationToken = default);

    public Task<AgentResponse> RunAsync(
        string message,
        AgentSession? session,
        ChatClientAgentRunOptions? options,
        CancellationToken cancellationToken = default);

    public Task<AgentResponse> RunAsync(
        ChatMessage message,
        AgentSession? session,
        ChatClientAgentRunOptions? options,
        CancellationToken cancellationToken = default);

    public Task<AgentResponse> RunAsync(
        IEnumerable<ChatMessage> messages,
        AgentSession? session,
        ChatClientAgentRunOptions? options,
        CancellationToken cancellationToken = default);
   
    public IAsyncEnumerable<AgentResponseUpdate> RunStreamingAsync(
        AgentSession? session,
        ChatClientAgentRunOptions? options,
        CancellationToken cancellationToken = default);

    public IAsyncEnumerable<AgentResponseUpdate> RunStreamingAsync(
        string message,
        AgentSession? session,
        ChatClientAgentRunOptions? options,
        CancellationToken cancellationToken = default);

    public IAsyncEnumerable<AgentResponseUpdate> RunStreamingAsync(
        ChatMessage message,
        AgentSession? session,
        ChatClientAgentRunOptions? options,
        CancellationToken cancellationToken = default);

    public IAsyncEnumerable<AgentResponseUpdate> RunStreamingAsync(
        IEnumerable<ChatMessage> messages,
        AgentSession? session,
        ChatClientAgentRunOptions? options,
        CancellationToken cancellationToken = default);

执行ChatClientAgent真正的逻辑实现在重写的RunCoreAsyncRunCoreStreamingAsync方法中。

public sealed class ChatClientAgent
{
    protected override async Task<AgentResponse> RunCoreAsync(
        IEnumerable<ChatMessage> messages,
        AgentSession? session = null,
        AgentRunOptions? options = null,
        CancellationToken cancellationToken = default);

    protected override IAsyncEnumerable<AgentResponseUpdate> RunCoreStreamingAsync(
        IEnumerable<ChatMessage> messages,
        AgentSession? session = null,
        AgentRunOptions? options = null,
        CancellationToken cancellationToken = default);
    }
}

我们来具体说说实现在RunCoreAsync方法中的调用流程,我们将整个流程分为准备阶段、调用阶段和收尾阶段。准备阶段流程如下:

  • 如果指定的session为null,则调用CreateSessionAsync方法来创建一个新的ChatClientAgentSession对象;也就是说孤立的单次调用也会绑定一个Session
  • 如果RequirePerServiceCallChatHistoryPersistence选项为false,会利用ChatHistoryProvider加载作为当前对话历史的消息列表,并与指定的消息列表进行合并(如果为true,额外注册的PerServiceCallChatHistoryPersistingChatClient会在每次调用时完成此工作);
  • 根据得到的消息列表和从ChatOptions提取的系统指令和工具集创建AIContext上下文,并基于它调用每个注册的AIContextProviderInvokingAsync方法来定制这个上下文。被定制的系统指令和工具集最终回ChatOptions中,从而实现针对输入的增强;

如果指定的AgentRunOptions参数是一个ChatClientAgentRunOptions对象,并且它的ChatClientFactory属性不为null,会将当前IChatClient作为参数调用这个ChatClientFactory委托对它进行装饰,以得到一个新的IChatClient对象来调用;否则直接使用ChatClientAgentChatClient属性来调用。调用之后进入收尾阶段,如果调用过程没有发生异常,并得到作为响应结果的ChatResponse,则按照如下的流程执行:

  • 如果ChatResponseContinuationToken不为null,意味这得到的并非完整的结果,还有后续;在RequiresPerServiceCallChatHistoryPersistence=False的情况下(如果为True的化,额外注册的PerServiceCallChatHistoryPersistingChatClient会在每次调用时完成此工作),需要将响应返回的ConversationId保存到当前ChatClientAgentSessionConversationId属性上;
  • 如果响应消息的AuthorName属性没有设置,会设置为当前ChatClientAgentName属性;
  • 如果RequirePerServiceCallChatHistoryPersistence选项为false,并且ChatResponseContinuationToken不为null或者chatOptions.AllowBackgroundResponses为True(表明响应尚未结束),此时需要:
    • 将响应消息列表与当前对话历史进行合并后存储到ChatHistoryProvider中;
    • 调用每个注册的AIContextProviderInvokedAsync方法来对结果进行再加工;

如果调用过程中发生了异常,并且RequirePerServiceCallChatHistoryPersistence选项为False,此时需要:

  • 调用每个注册的AIContextProviderInvokedAsync方法来作响应的异常处理;
  • 调用每个注册的AIContextProviderInvokedAsync方法来作响应的异常处理;

对于RunCoreStreamingAsync方法,调用流程与RunCoreAsync方法基本一致,区别在于它会处理来自IChatClient的流式响应,并将每个响应更新(AgentResponseUpdate)逐一返回。

如果Agent执行尚未结束,响应的ChatResponse或者ChatResponseUpdateContiuationToken属性将会返回一个ResponseContinuationToken对象。如果希望得到后续的响应,需要指定一个ChatClientAgentRunOptions对象,并将这个ContinuationToken对象保存到ChatClientAgentRunOptions对象的ContinuationToken属性中。

对于ChatClientAgent来说,这个ContinuationToken是一个ChatClientAgentContinuationToken对象。从如下的代码可以看出,ChatClientAgentContinuationToken是对另一个ResponseContinuationToken对象的包装,并在此基础上增加了InputMessagesResponseUpdates属性来保存之前的输入消息列表和响应更新列表,以便在获取后续响应时继续使用。

internal class ChatClientAgentContinuationToken : ResponseContinuationToken
{
    internal ResponseContinuationToken InnerToken { get; }
    internal IEnumerable<ChatMessage>? InputMessages { get; set; }
    internal IReadOnlyList<ChatResponseUpdate>? ResponseUpdates { get; set; }
    internal ChatClientAgentContinuationToken(ResponseContinuationToken innerToken);
}

4. 结构化输出

让Agent返回指定结构的输出一直是Agent开发中的一个重要需求。ChatClientAgent通过提供一系列重载的RunAsync方法来满足这个需求,这些方法允许调用者指定一个泛型参数T来表示期望的响应结果类型。ChatClientAgent会尝试将LLM的响应内容反序列化成T类型的对象,并将其封装在AgentResponse<T>对象中返回。

public sealed class ChatClientAgent : AIAgent
{  
    public Task<AgentResponse<T>> RunAsync<T>(
        AgentSession? session,
        JsonSerializerOptions? serializerOptions,
        ChatClientAgentRunOptions? options,
        CancellationToken cancellationToken = default);

    public Task<AgentResponse<T>> RunAsync<T>(
        string message, AgentSession? session,
        JsonSerializerOptions? serializerOptions,
        ChatClientAgentRunOptions? options,
        CancellationToken cancellationToken = default);

    public Task<AgentResponse<T>> RunAsync<T>(
        ChatMessage message, AgentSession? session,
        JsonSerializerOptions? serializerOptions,
        ChatClientAgentRunOptions? options,
        CancellationToken cancellationToken = default);

    public Task<AgentResponse<T>> RunAsync<T>(
        IEnumerable<ChatMessage> messages, AgentSession? session,
        JsonSerializerOptions? serializerOptions,
        ChatClientAgentRunOptions? options,
        CancellationToken cancellationToken = default);
}

结构化输出的核心是对AgentRunOptionsResponseFormat属性进行配置,该属性定义了LLM响应内容的期望格式。通过设置ResponseFormat,开发者可以指定响应应如何被序列化和反序列化,从而确保Agent返回的数据结构符合预期。