var workflow = new WorkflowBuilder(startExecutor) .AddFanOutEdge(startExecutor, [amazonExecutor, ebayExecutor, shopeeExecutor]) .AddFanInEdge([amazonExecutor, ebayExecutor, shopeeExecutor], strategyExecutor) .WithOutputFrom(strategyExecutor)
internal class PriceQueryDto
{ public string ProductId { get; private set; } public string ProductName { get; private set; } public string TargetRegion { get; private set; } public PriceQueryDto(string productId, string productName, string targetRegion)
{
ProductId = productId;
ProductName = productName;
internal sealed class PlatformPriceExecutor : Executor<ChatMessage>{ private readonly string _instructions; private readonly IChatClient _chatClient; public PlatformPriceExecutor(string id, IChatClient chatClient, string platformInstructions)
: base(id)
{
_chatClient = chatClient;
_instructions = platformInstructions;
} public override async ValueTask HandleAsync(ChatMessage message, IWorkflowContext context, CancellationToken cancellationToken = default)
{ var messages = new List<ChatMessage> 8x1.xydgdq.cn
{ new(ChatRole.System, _instructions),
message m.yinuo168.cn
}; var response = await _chatClient.GetResponseAsync(messages, cancellationToken: cancellationToken); var replyMessage = new ChatMessage(ChatRole.Assistant, response.Text ?? string.Empty)
{
AuthorName = this.Id
}; await context.SendMessageAsync(replyMessage, cancellationToken: cancellationToken);
internal sealed class PriceQueryStartExecutor() : Executor<PriceQueryDto>(nameof(PriceQueryStartExecutor))
{ public override async ValueTask HandleAsync(PriceQueryDto query, IWorkflowContext context, CancellationToken cancellationToken = default)
{ var userPrompt = $@"商品ID: {query.ProductId}
商品名称: {query.ProductName}
目标区域: {query.TargetRegion}
请查询该商品在你的平台上的当前价格、库存状态和配送信息。"; 3g.xydgdq.cn await context.SendMessageAsync(new ChatMessage(ChatRole.User, userPrompt), cancellationToken: cancellationToken); await context.SendMessageAsync(new TurnToken(emitEvents: true), cancellationToken: cancellationToken);
Console.WriteLine("📡 Fan-out 价格查询广播已发送");
internal sealed class PricingStrategyExecutor : Executor<ChatMessage>{ private readonly List<ChatMessage> _messages = []; private readonly int _targetCount; public PricingStrategyExecutor(int targetCount) : base(nameof(PricingStrategyExecutor))
{
_targetCount = targetCount;
} public override async ValueTask HandleAsync(ChatMessage message, IWorkflowContext context, CancellationToken cancellationToken = default)
{ this._messages.Add(message);
Console.WriteLine($"📊 已收集 {_messages.Count}/{_targetCount} 个平台数据 - 来自 {message.AuthorName}"); if (this._messages.Count == this._targetCount)
{ var platformData = string.Join("\n", this._messages.Select(m => $"• {m.AuthorName}: {m.Text}")); var strategyReport = $@"━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
📊 多平台价格汇总(共 {this._messages.Count} 个平台)yinuo168.cn
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
{platformData}
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
💡 智能定价建议
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
基于以上数据,建议分析竞争对手价格区间,制定差异化定价策略。
考虑因素:库存压力、配送成本、平台佣金率、目标利润率。"; m.tinghaoshu.cn await context.YieldOutputAsync(strategyReport, cancellationToken);
var chatClient = new OpenAIClient( new ApiKeyCredential(openAIProvider.ApiKey), new OpenAIClientOptions { Endpoint = new Uri(openAIProvider.Endpoint) })
.GetChatClient(openAIProvider.ModelId)
.AsIChatClient();jrs.ttLyf.cn
ar amazonExecutor = new PlatformPriceExecutor( "AmazonPriceAgent", z0.xydgdq.cn chatClient, "你是Amazon平台价格查询Agent。返回格式:价格=$XXX,库存状态=充足/紧张,配送说明=Prime会员免运费/标准配送。");var ebayExecutor = new PlatformPriceExecutor( "eBayPriceAgent", chatClient, "你是eBay平台价格查询Agent。返回格式:价格=$XXX,商品状态=全新/二手XX新,运费说明=包邮/买家承担。");var shopeeExecutor = new PlatformPriceExecutor( "ShopeePriceAgent", chatClient, "你是Shopee平台价格查询Agent。返回格式:价格=$XXX(含税),区域=东南亚/台湾,促销信息=满减活动/无。");var startExecutor = new PriceQueryStartExecutor();
var workflow = new WorkflowBuilder(startExecutor)
.AddFanOutEdge(startExecutor, [amazonExecutor, ebayExecutor, shopeeExecutor])
.AddFanInEdge([amazonExecutor, ebayExecutor, shopeeExecutor], strategyExecutor)
.WithOutputFrom(strategyExecutor) czLangrun.com
.Build(); jrs.dLddz.com
Console.OutputEncoding = Encoding.UTF8;
Console.WriteLine("✅ Loop Workflow 构建完成");
await using (var run = await InProcessExecution.StreamAsync(workflow, priceQuery))
{ await foreach (var evt in run.WatchStreamAsync())
{ switch (evt) app.xydgdq.cn
{ case ExecutorInvokedEvent started:
Console.WriteLine($"🚀 {started.ExecutorId} 开始运行"); break; case ExecutorCompletedEvent completed:
Console.WriteLine($"✅ {completed.ExecutorId} 结束运行"); tw3rs.xydgdq.cn break; case WorkflowOutputEvent outputEvent:
Console.WriteLine("🎉 Fan-in 汇总输出:");jrszb.yqswh.cn
Console.WriteLine($"{outputEvent.Data}"); break; case WorkflowErrorEvent errorEvent:
Console.WriteLine("✨ 收到 Workflow Error Event:");
Console.WriteLine($"{errorEvent.Data}"); er4.xydgdq.cn break;
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/70048957/viewspace-3109574/ ,如需转载,请注明出处,否则将追究法律责任。