48 lines
1.7 KiB
C#
48 lines
1.7 KiB
C#
using MAF1.Agents.FileCity;
|
|
using Microsoft.Agents.AI.Workflows;
|
|
using Microsoft.Extensions.AI;
|
|
|
|
namespace MAF1.Workflows;
|
|
|
|
internal sealed class CityGateExecutor() : ChatProtocolExecutor(
|
|
"CityGate",
|
|
new ChatProtocolExecutorOptions { AutoSendTurnToken = false })
|
|
{
|
|
protected override ProtocolBuilder ConfigureProtocol(ProtocolBuilder builder)
|
|
{
|
|
return base.ConfigureProtocol(builder)
|
|
.SendsMessage<ChatMessage>()
|
|
.SendsMessage<TurnToken>()
|
|
.YieldsOutput<string>();
|
|
}
|
|
|
|
protected override async ValueTask TakeTurnAsync(
|
|
List<ChatMessage> messages,
|
|
IWorkflowContext context,
|
|
bool? emitEvents,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
string text = string.Join(
|
|
Environment.NewLine,
|
|
messages
|
|
.Where(message => !string.IsNullOrWhiteSpace(message.Text))
|
|
.Select(message => message.Text));
|
|
|
|
CityExtraction extraction = FileCityAgent.Parse(text);
|
|
if (!extraction.HasValidCities)
|
|
{
|
|
string reason = string.IsNullOrWhiteSpace(extraction.Reason)
|
|
? "文件里没有可查询的城市。"
|
|
: extraction.Reason;
|
|
await context.YieldOutputAsync(
|
|
$"没有有效城市,工作流结束,不执行 WeatherAgent。{reason}",
|
|
cancellationToken);
|
|
return;
|
|
}
|
|
|
|
string prompt = $"请查询这些城市的天气:{string.Join("、", extraction.Cities)}";
|
|
await context.SendMessageAsync(new ChatMessage(ChatRole.User, prompt), cancellationToken: cancellationToken);
|
|
await context.SendMessageAsync(new TurnToken(emitEvents), cancellationToken: cancellationToken);
|
|
}
|
|
}
|