初始提交
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user