chore: import upstream snapshot with attribution
CodeQL / Analyze (csharp) (push) Has been cancelled
CodeQL / Analyze (python) (push) Has been cancelled

This commit is contained in:
wehub-resource-sync
2026-07-13 13:21:23 +08:00
commit b957a53def
5423 changed files with 863745 additions and 0 deletions
@@ -0,0 +1,107 @@
// Copyright (c) Microsoft. All rights reserved.
using System.Text.Json;
using Microsoft.SemanticKernel;
using Step03.Models;
using Step03.Steps;
namespace Step03.Processes;
/// <summary>
/// Sample process that showcases how to create a process with a fan in/fan out behavior and use of existing processes as steps.<br/>
/// Visual reference of this process can be found in the <see href="https://github.com/microsoft/semantic-kernel/blob/main/dotnet/samples/GettingStartedWithProcesses/README.md#fish-and-chips-preparation-process" >diagram</see>
/// </summary>
public static class FishAndChipsProcess
{
public static class ProcessEvents
{
public const string PrepareFishAndChips = nameof(PrepareFishAndChips);
public const string FishAndChipsReady = nameof(FishAndChipsReady);
public const string FishAndChipsIngredientOutOfStock = nameof(FishAndChipsIngredientOutOfStock);
}
public static ProcessBuilder CreateProcess(string processName = "FishAndChipsProcess")
{
var processBuilder = new ProcessBuilder(processName);
var makeFriedFishStep = processBuilder.AddStepFromProcess(FriedFishProcess.CreateProcess());
var makePotatoFriesStep = processBuilder.AddStepFromProcess(PotatoFriesProcess.CreateProcess());
var addCondimentsStep = processBuilder.AddStepFromType<AddFishAndChipsCondimentsStep>();
// An additional step that is the only one that emits an public event in a process can be added to maintain event names unique
var externalStep = processBuilder.AddStepFromType<ExternalFishAndChipsStep>();
processBuilder
.OnInputEvent(ProcessEvents.PrepareFishAndChips)
.SendEventTo(makeFriedFishStep.WhereInputEventIs(FriedFishProcess.ProcessEvents.PrepareFriedFish))
.SendEventTo(makePotatoFriesStep.WhereInputEventIs(PotatoFriesProcess.ProcessEvents.PreparePotatoFries));
makeFriedFishStep
.OnEvent(FriedFishProcess.ProcessEvents.FriedFishReady)
.SendEventTo(new ProcessFunctionTargetBuilder(addCondimentsStep, parameterName: "fishActions"));
makePotatoFriesStep
.OnEvent(PotatoFriesProcess.ProcessEvents.PotatoFriesReady)
.SendEventTo(new ProcessFunctionTargetBuilder(addCondimentsStep, parameterName: "potatoActions"));
addCondimentsStep
.OnEvent(AddFishAndChipsCondimentsStep.OutputEvents.CondimentsAdded)
.SendEventTo(new ProcessFunctionTargetBuilder(externalStep));
return processBuilder;
}
public static ProcessBuilder CreateProcessWithStatefulSteps(string processName = "FishAndChipsWithStatefulStepsProcess")
{
var processBuilder = new ProcessBuilder(processName);
var makeFriedFishStep = processBuilder.AddStepFromProcess(FriedFishProcess.CreateProcessWithStatefulStepsV1());
var makePotatoFriesStep = processBuilder.AddStepFromProcess(PotatoFriesProcess.CreateProcessWithStatefulSteps());
var addCondimentsStep = processBuilder.AddStepFromType<AddFishAndChipsCondimentsStep>();
// An additional step that is the only one that emits an public event in a process can be added to maintain event names unique
var externalStep = processBuilder.AddStepFromType<ExternalFishAndChipsStep>();
processBuilder
.OnInputEvent(ProcessEvents.PrepareFishAndChips)
.SendEventTo(makeFriedFishStep.WhereInputEventIs(FriedFishProcess.ProcessEvents.PrepareFriedFish))
.SendEventTo(makePotatoFriesStep.WhereInputEventIs(PotatoFriesProcess.ProcessEvents.PreparePotatoFries));
makeFriedFishStep
.OnEvent(FriedFishProcess.ProcessEvents.FriedFishReady)
.SendEventTo(new ProcessFunctionTargetBuilder(addCondimentsStep, parameterName: "fishActions"));
makePotatoFriesStep
.OnEvent(PotatoFriesProcess.ProcessEvents.PotatoFriesReady)
.SendEventTo(new ProcessFunctionTargetBuilder(addCondimentsStep, parameterName: "potatoActions"));
addCondimentsStep
.OnEvent(AddFishAndChipsCondimentsStep.OutputEvents.CondimentsAdded)
.SendEventTo(new ProcessFunctionTargetBuilder(externalStep));
return processBuilder;
}
private sealed class AddFishAndChipsCondimentsStep : KernelProcessStep
{
public static class ProcessFunctions
{
public const string AddCondiments = nameof(AddCondiments);
}
public static class OutputEvents
{
public const string CondimentsAdded = nameof(CondimentsAdded);
}
[KernelFunction(ProcessFunctions.AddCondiments)]
public async Task AddCondimentsAsync(KernelProcessStepContext context, List<string> fishActions, List<string> potatoActions)
{
Console.WriteLine($"ADD_CONDIMENTS: Added condiments to Fish & Chips - Fish: {JsonSerializer.Serialize(fishActions)}, Potatoes: {JsonSerializer.Serialize(potatoActions)}");
fishActions.AddRange(potatoActions);
fishActions.Add(FoodIngredients.Condiments.ToFriendlyString());
await context.EmitEventAsync(new() { Id = OutputEvents.CondimentsAdded, Data = fishActions });
}
}
private sealed class ExternalFishAndChipsStep : ExternalStep
{
public ExternalFishAndChipsStep() : base(ProcessEvents.FishAndChipsReady) { }
}
}
@@ -0,0 +1,151 @@
// Copyright (c) Microsoft. All rights reserved.
using Microsoft.SemanticKernel;
using Step03.Models;
using Step03.Steps;
namespace Step03.Processes;
/// <summary>
/// Sample process that showcases how to create a process with sequential steps and use of existing processes as steps.<br/>
/// Visual reference of this process can be found in the <see href="https://github.com/microsoft/semantic-kernel/blob/main/dotnet/samples/GettingStartedWithProcesses/README.md#fish-sandwich-preparation-process" >diagram</see>
/// </summary>
public static class FishSandwichProcess
{
public static class ProcessEvents
{
public const string PrepareFishSandwich = nameof(PrepareFishSandwich);
public const string FishSandwichReady = nameof(FishSandwichReady);
}
public static ProcessBuilder CreateProcess(string processName = "FishSandwichProcess")
{
var processBuilder = new ProcessBuilder(processName);
var makeFriedFishStep = processBuilder.AddStepFromProcess(FriedFishProcess.CreateProcess());
var addBunsStep = processBuilder.AddStepFromType<AddBunsStep>();
var addSpecialSauceStep = processBuilder.AddStepFromType<AddSpecialSauceStep>();
// An additional step that is the only one that emits an public event in a process can be added to maintain event names unique
var externalStep = processBuilder.AddStepFromType<ExternalFriedFishStep>();
processBuilder
.OnInputEvent(ProcessEvents.PrepareFishSandwich)
.SendEventTo(makeFriedFishStep.WhereInputEventIs(FriedFishProcess.ProcessEvents.PrepareFriedFish));
makeFriedFishStep
.OnEvent(FriedFishProcess.ProcessEvents.FriedFishReady)
.SendEventTo(new ProcessFunctionTargetBuilder(addBunsStep));
addBunsStep
.OnEvent(AddBunsStep.OutputEvents.BunsAdded)
.SendEventTo(new ProcessFunctionTargetBuilder(addSpecialSauceStep));
addSpecialSauceStep
.OnEvent(AddSpecialSauceStep.OutputEvents.SpecialSauceAdded)
.SendEventTo(new ProcessFunctionTargetBuilder(externalStep));
return processBuilder;
}
public static ProcessBuilder CreateProcessWithStatefulStepsV1(string processName = "FishSandwichWithStatefulStepsProcess")
{
var processBuilder = new ProcessBuilder(processName) { Version = "FishSandwich.V1" };
var makeFriedFishStep = processBuilder.AddStepFromProcess(FriedFishProcess.CreateProcessWithStatefulStepsV1());
var addBunsStep = processBuilder.AddStepFromType<AddBunsStep>();
var addSpecialSauceStep = processBuilder.AddStepFromType<AddSpecialSauceStep>();
// An additional step that is the only one that emits an public event in a process can be added to maintain event names unique
var externalStep = processBuilder.AddStepFromType<ExternalFriedFishStep>();
processBuilder
.OnInputEvent(ProcessEvents.PrepareFishSandwich)
.SendEventTo(makeFriedFishStep.WhereInputEventIs(FriedFishProcess.ProcessEvents.PrepareFriedFish));
makeFriedFishStep
.OnEvent(FriedFishProcess.ProcessEvents.FriedFishReady)
.SendEventTo(new ProcessFunctionTargetBuilder(addBunsStep));
addBunsStep
.OnEvent(AddBunsStep.OutputEvents.BunsAdded)
.SendEventTo(new ProcessFunctionTargetBuilder(addSpecialSauceStep));
addSpecialSauceStep
.OnEvent(AddSpecialSauceStep.OutputEvents.SpecialSauceAdded)
.SendEventTo(new ProcessFunctionTargetBuilder(externalStep));
return processBuilder;
}
public static ProcessBuilder CreateProcessWithStatefulStepsV2(string processName = "FishSandwichWithStatefulStepsProcess")
{
var processBuilder = new ProcessBuilder(processName) { Version = "FishSandwich.V2" };
var makeFriedFishStep = processBuilder.AddStepFromProcess(FriedFishProcess.CreateProcessWithStatefulStepsV2("FriedFishStep"), aliases: ["FriedFishWithStatefulStepsProcess"]);
var addBunsStep = processBuilder.AddStepFromType<AddBunsStep>();
var addSpecialSauceStep = processBuilder.AddStepFromType<AddSpecialSauceStep>();
// An additional step that is the only one that emits an public event in a process can be added to maintain event names unique
var externalStep = processBuilder.AddStepFromType<ExternalFriedFishStep>();
processBuilder
.OnInputEvent(ProcessEvents.PrepareFishSandwich)
.SendEventTo(makeFriedFishStep.WhereInputEventIs(FriedFishProcess.ProcessEvents.PrepareFriedFish));
makeFriedFishStep
.OnEvent(FriedFishProcess.ProcessEvents.FriedFishReady)
.SendEventTo(new ProcessFunctionTargetBuilder(addBunsStep));
addBunsStep
.OnEvent(AddBunsStep.OutputEvents.BunsAdded)
.SendEventTo(new ProcessFunctionTargetBuilder(addSpecialSauceStep));
addSpecialSauceStep
.OnEvent(AddSpecialSauceStep.OutputEvents.SpecialSauceAdded)
.SendEventTo(new ProcessFunctionTargetBuilder(externalStep));
return processBuilder;
}
private sealed class AddBunsStep : KernelProcessStep
{
public static class ProcessFunctions
{
public const string AddBuns = nameof(AddBuns);
}
public static class OutputEvents
{
public const string BunsAdded = nameof(BunsAdded);
}
[KernelFunction(ProcessFunctions.AddBuns)]
public async Task SliceFoodAsync(KernelProcessStepContext context, List<string> foodActions)
{
Console.WriteLine($"BUNS_ADDED_STEP: Buns added to ingredient {foodActions.First()}");
foodActions.Add(FoodIngredients.Buns.ToFriendlyString());
await context.EmitEventAsync(new() { Id = OutputEvents.BunsAdded, Data = foodActions });
}
}
private sealed class AddSpecialSauceStep : KernelProcessStep
{
public static class ProcessFunctions
{
public const string AddSpecialSauce = nameof(AddSpecialSauce);
}
public static class OutputEvents
{
public const string SpecialSauceAdded = nameof(SpecialSauceAdded);
}
[KernelFunction(ProcessFunctions.AddSpecialSauce)]
public async Task SliceFoodAsync(KernelProcessStepContext context, List<string> foodActions)
{
Console.WriteLine($"SPECIAL_SAUCE_ADDED: Special sauce added to ingredient {foodActions.First()}");
foodActions.Add(FoodIngredients.Sauce.ToFriendlyString());
await context.EmitEventAsync(new() { Id = OutputEvents.SpecialSauceAdded, Data = foodActions, Visibility = KernelProcessEventVisibility.Public });
}
}
private sealed class ExternalFriedFishStep : ExternalStep
{
public ExternalFriedFishStep() : base(ProcessEvents.FishSandwichReady) { }
}
}
@@ -0,0 +1,141 @@
// Copyright (c) Microsoft. All rights reserved.
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Process;
using Step03.Models;
using Step03.Steps;
namespace Step03.Processes;
/// <summary>
/// Sample process that showcases how to create a process with sequential steps and reuse of existing steps.<br/>
/// </summary>
public static class FriedFishProcess
{
public static class ProcessEvents
{
public const string PrepareFriedFish = nameof(PrepareFriedFish);
// When multiple processes use the same final step, the should event marked as public
// so that the step event can be used as the output event of the process too.
// In these samples both fried fish and potato fries end with FryStep success
public const string FriedFishReady = FryFoodStep.OutputEvents.FriedFoodReady;
}
/// <summary>
/// For a visual reference of the FriedFishProcess check this
/// <see href="https://github.com/microsoft/semantic-kernel/blob/main/dotnet/samples/GettingStartedWithProcesses/README.md#fried-fish-preparation-process" >diagram</see>
/// </summary>
/// <param name="processName">name of the process</param>
/// <returns><see cref="ProcessBuilder"/></returns>
public static ProcessBuilder CreateProcess(string processName = "FriedFishProcess")
{
var processBuilder = new ProcessBuilder(processName);
var gatherIngredientsStep = processBuilder.AddStepFromType<GatherFriedFishIngredientsStep>();
var chopStep = processBuilder.AddStepFromType<CutFoodStep>();
var fryStep = processBuilder.AddStepFromType<FryFoodStep>();
processBuilder
.OnInputEvent(ProcessEvents.PrepareFriedFish)
.SendEventTo(new ProcessFunctionTargetBuilder(gatherIngredientsStep));
gatherIngredientsStep
.OnEvent(GatherFriedFishIngredientsStep.OutputEvents.IngredientsGathered)
.SendEventTo(new ProcessFunctionTargetBuilder(chopStep, functionName: CutFoodStep.ProcessStepFunctions.ChopFood));
chopStep
.OnEvent(CutFoodStep.OutputEvents.ChoppingReady)
.SendEventTo(new ProcessFunctionTargetBuilder(fryStep));
fryStep
.OnEvent(FryFoodStep.OutputEvents.FoodRuined)
.SendEventTo(new ProcessFunctionTargetBuilder(gatherIngredientsStep));
return processBuilder;
}
public static ProcessBuilder CreateProcessWithStatefulStepsV1(string processName = "FriedFishWithStatefulStepsProcess")
{
// It is recommended to specify process version in case this process is used as a step by another process
var processBuilder = new ProcessBuilder(processName) { Version = "FriedFishProcess.v1" }; ;
var gatherIngredientsStep = processBuilder.AddStepFromType<GatherFriedFishIngredientsWithStockStep>();
var chopStep = processBuilder.AddStepFromType<CutFoodStep>();
var fryStep = processBuilder.AddStepFromType<FryFoodStep>();
processBuilder
.OnInputEvent(ProcessEvents.PrepareFriedFish)
.SendEventTo(new ProcessFunctionTargetBuilder(gatherIngredientsStep));
gatherIngredientsStep
.OnEvent(GatherFriedFishIngredientsWithStockStep.OutputEvents.IngredientsGathered)
.SendEventTo(new ProcessFunctionTargetBuilder(chopStep, functionName: CutFoodWithSharpeningStep.ProcessStepFunctions.ChopFood));
chopStep
.OnEvent(CutFoodWithSharpeningStep.OutputEvents.ChoppingReady)
.SendEventTo(new ProcessFunctionTargetBuilder(fryStep));
fryStep
.OnEvent(FryFoodStep.OutputEvents.FoodRuined)
.SendEventTo(new ProcessFunctionTargetBuilder(gatherIngredientsStep));
return processBuilder;
}
/// <summary>
/// For a visual reference of the FriedFishProcess with stateful steps check this
/// <see href="https://github.com/microsoft/semantic-kernel/blob/main/dotnet/samples/GettingStartedWithProcesses/README.md#fried-fish-preparation-with-knife-sharpening-and-ingredient-stock-process" >diagram</see>
/// </summary>
/// <param name="processName">name of the process</param>
/// <returns><see cref="ProcessBuilder"/></returns>
public static ProcessBuilder CreateProcessWithStatefulStepsV2(string processName = "FriedFishWithStatefulStepsProcess")
{
// It is recommended to specify process version in case this process is used as a step by another process
var processBuilder = new ProcessBuilder(processName) { Version = "FriedFishProcess.v2" };
var gatherIngredientsStep = processBuilder.AddStepFromType<GatherFriedFishIngredientsWithStockStep>(id: "gatherFishIngredientStep", aliases: ["GatherFriedFishIngredientsWithStockStep"]);
var chopStep = processBuilder.AddStepFromType<CutFoodWithSharpeningStep>(id: "chopFishStep", aliases: ["CutFoodStep"]);
var fryStep = processBuilder.AddStepFromType<FryFoodStep>(id: "fryFishStep", aliases: ["FryFoodStep"]);
processBuilder
.OnInputEvent(ProcessEvents.PrepareFriedFish)
.SendEventTo(new ProcessFunctionTargetBuilder(gatherIngredientsStep));
gatherIngredientsStep
.OnEvent(GatherFriedFishIngredientsWithStockStep.OutputEvents.IngredientsGathered)
.SendEventTo(new ProcessFunctionTargetBuilder(chopStep, functionName: CutFoodWithSharpeningStep.ProcessStepFunctions.ChopFood));
gatherIngredientsStep
.OnEvent(GatherFriedFishIngredientsWithStockStep.OutputEvents.IngredientsOutOfStock)
.StopProcess();
chopStep
.OnEvent(CutFoodWithSharpeningStep.OutputEvents.ChoppingReady)
.SendEventTo(new ProcessFunctionTargetBuilder(fryStep));
chopStep
.OnEvent(CutFoodWithSharpeningStep.OutputEvents.KnifeNeedsSharpening)
.SendEventTo(new ProcessFunctionTargetBuilder(chopStep, functionName: CutFoodWithSharpeningStep.ProcessStepFunctions.SharpenKnife));
chopStep
.OnEvent(CutFoodWithSharpeningStep.OutputEvents.KnifeSharpened)
.SendEventTo(new ProcessFunctionTargetBuilder(chopStep, functionName: CutFoodWithSharpeningStep.ProcessStepFunctions.ChopFood));
fryStep
.OnEvent(FryFoodStep.OutputEvents.FoodRuined)
.SendEventTo(new ProcessFunctionTargetBuilder(gatherIngredientsStep));
return processBuilder;
}
[KernelProcessStepMetadata("GatherFishIngredient.V1")]
private sealed class GatherFriedFishIngredientsStep : GatherIngredientsStep
{
public GatherFriedFishIngredientsStep() : base(FoodIngredients.Fish) { }
}
[KernelProcessStepMetadata("GatherFishIngredient.V2")]
private sealed class GatherFriedFishIngredientsWithStockStep : GatherIngredientsWithStockStep
{
public GatherFriedFishIngredientsWithStockStep() : base(FoodIngredients.Fish) { }
}
}
@@ -0,0 +1,110 @@
// Copyright (c) Microsoft. All rights reserved.
using Microsoft.SemanticKernel;
using Step03.Models;
using Step03.Steps;
namespace Step03.Processes;
/// <summary>
/// Sample process that showcases how to create a process with sequential steps and reuse of existing steps.<br/>
/// </summary>
public static class PotatoFriesProcess
{
public static class ProcessEvents
{
public const string PreparePotatoFries = nameof(PreparePotatoFries);
// When multiple processes use the same final step, the should event marked as public
// so that the step event can be used as the output event of the process too.
// In these samples both fried fish and potato fries end with FryStep success
public const string PotatoFriesReady = nameof(FryFoodStep.OutputEvents.FriedFoodReady);
}
/// <summary>
/// For a visual reference of the PotatoFriesProcess check this
/// <see href="https://github.com/microsoft/semantic-kernel/blob/main/dotnet/samples/GettingStartedWithProcesses/README.md#potato-fries-preparation-process" >diagram</see>
/// </summary>
/// <param name="processName">name of the process</param>
/// <returns><see cref="ProcessBuilder"/></returns>
public static ProcessBuilder CreateProcess(string processName = "PotatoFriesProcess")
{
var processBuilder = new ProcessBuilder(processName);
var gatherIngredientsStep = processBuilder.AddStepFromType<GatherPotatoFriesIngredientsStep>();
var sliceStep = processBuilder.AddStepFromType<CutFoodStep>("sliceStep");
var fryStep = processBuilder.AddStepFromType<FryFoodStep>();
processBuilder
.OnInputEvent(ProcessEvents.PreparePotatoFries)
.SendEventTo(new ProcessFunctionTargetBuilder(gatherIngredientsStep));
gatherIngredientsStep
.OnEvent(GatherPotatoFriesIngredientsStep.OutputEvents.IngredientsGathered)
.SendEventTo(new ProcessFunctionTargetBuilder(sliceStep, functionName: CutFoodStep.ProcessStepFunctions.SliceFood));
sliceStep
.OnEvent(CutFoodStep.OutputEvents.SlicingReady)
.SendEventTo(new ProcessFunctionTargetBuilder(fryStep));
fryStep
.OnEvent(FryFoodStep.OutputEvents.FoodRuined)
.SendEventTo(new ProcessFunctionTargetBuilder(gatherIngredientsStep));
return processBuilder;
}
/// <summary>
/// For a visual reference of the PotatoFriesProcess with stateful steps check this
/// <see href="https://github.com/microsoft/semantic-kernel/blob/main/dotnet/samples/GettingStartedWithProcesses/README.md#potato-fries-preparation-with-knife-sharpening-and-ingredient-stock-process" >diagram</see>
/// </summary>
/// <param name="processName">name of the process</param>
/// <returns><see cref="ProcessBuilder"/></returns>
public static ProcessBuilder CreateProcessWithStatefulSteps(string processName = "PotatoFriesWithStatefulStepsProcess")
{
var processBuilder = new ProcessBuilder(processName);
var gatherIngredientsStep = processBuilder.AddStepFromType<GatherPotatoFriesIngredientsWithStockStep>();
var sliceStep = processBuilder.AddStepFromType<CutFoodWithSharpeningStep>("sliceStep");
var fryStep = processBuilder.AddStepFromType<FryFoodStep>();
processBuilder
.OnInputEvent(ProcessEvents.PreparePotatoFries)
.SendEventTo(new ProcessFunctionTargetBuilder(gatherIngredientsStep));
gatherIngredientsStep
.OnEvent(GatherPotatoFriesIngredientsWithStockStep.OutputEvents.IngredientsGathered)
.SendEventTo(new ProcessFunctionTargetBuilder(sliceStep, functionName: CutFoodWithSharpeningStep.ProcessStepFunctions.SliceFood));
gatherIngredientsStep
.OnEvent(GatherPotatoFriesIngredientsWithStockStep.OutputEvents.IngredientsOutOfStock)
.StopProcess();
sliceStep
.OnEvent(CutFoodWithSharpeningStep.OutputEvents.SlicingReady)
.SendEventTo(new ProcessFunctionTargetBuilder(fryStep));
sliceStep
.OnEvent(CutFoodWithSharpeningStep.OutputEvents.KnifeNeedsSharpening)
.SendEventTo(new ProcessFunctionTargetBuilder(sliceStep, functionName: CutFoodWithSharpeningStep.ProcessStepFunctions.SharpenKnife));
sliceStep
.OnEvent(CutFoodWithSharpeningStep.OutputEvents.KnifeSharpened)
.SendEventTo(new ProcessFunctionTargetBuilder(sliceStep, functionName: CutFoodWithSharpeningStep.ProcessStepFunctions.SliceFood));
fryStep
.OnEvent(FryFoodStep.OutputEvents.FoodRuined)
.SendEventTo(new ProcessFunctionTargetBuilder(gatherIngredientsStep));
return processBuilder;
}
private sealed class GatherPotatoFriesIngredientsStep : GatherIngredientsStep
{
public GatherPotatoFriesIngredientsStep() : base(FoodIngredients.Pototoes) { }
}
private sealed class GatherPotatoFriesIngredientsWithStockStep : GatherIngredientsWithStockStep
{
public GatherPotatoFriesIngredientsWithStockStep() : base(FoodIngredients.Pototoes) { }
}
}
@@ -0,0 +1,142 @@
// Copyright (c) Microsoft. All rights reserved.
using System.Text.Json;
using Microsoft.SemanticKernel;
using Step03.Models;
using Step03.Steps;
namespace Step03.Processes;
/// <summary>
/// Sample process that showcases how to create a selecting fan out process
/// For a visual reference of the FriedFishProcess check this <see href="https://github.com/microsoft/semantic-kernel/blob/main/dotnet/samples/GettingStartedWithProcesses/README.md#single-order-preparation-process" >diagram</see>
/// </summary>
public static class SingleFoodItemProcess
{
public static class ProcessEvents
{
public const string SingleOrderReceived = nameof(SingleOrderReceived);
public const string SingleOrderReady = nameof(SingleOrderReady);
}
public static ProcessBuilder CreateProcess(string processName = "SingleFoodItemProcess")
{
var processBuilder = new ProcessBuilder(processName);
var dispatchOrderStep = processBuilder.AddStepFromType<DispatchSingleOrderStep>();
var makeFriedFishStep = processBuilder.AddStepFromProcess(FriedFishProcess.CreateProcess());
var makePotatoFriesStep = processBuilder.AddStepFromProcess(PotatoFriesProcess.CreateProcess());
var makeFishSandwichStep = processBuilder.AddStepFromProcess(FishSandwichProcess.CreateProcess());
var makeFishAndChipsStep = processBuilder.AddStepFromProcess(FishAndChipsProcess.CreateProcess());
var packOrderStep = processBuilder.AddStepFromType<PackOrderStep>();
var externalStep = processBuilder.AddStepFromType<ExternalSingleOrderStep>();
processBuilder
.OnInputEvent(ProcessEvents.SingleOrderReceived)
.SendEventTo(new ProcessFunctionTargetBuilder(dispatchOrderStep));
dispatchOrderStep
.OnEvent(DispatchSingleOrderStep.OutputEvents.PrepareFriedFish)
.SendEventTo(makeFriedFishStep.WhereInputEventIs(FriedFishProcess.ProcessEvents.PrepareFriedFish));
dispatchOrderStep
.OnEvent(DispatchSingleOrderStep.OutputEvents.PrepareFries)
.SendEventTo(makePotatoFriesStep.WhereInputEventIs(PotatoFriesProcess.ProcessEvents.PreparePotatoFries));
dispatchOrderStep
.OnEvent(DispatchSingleOrderStep.OutputEvents.PrepareFishSandwich)
.SendEventTo(makeFishSandwichStep.WhereInputEventIs(FishSandwichProcess.ProcessEvents.PrepareFishSandwich));
dispatchOrderStep
.OnEvent(DispatchSingleOrderStep.OutputEvents.PrepareFishAndChips)
.SendEventTo(makeFishAndChipsStep.WhereInputEventIs(FishAndChipsProcess.ProcessEvents.PrepareFishAndChips));
makeFriedFishStep
.OnEvent(FriedFishProcess.ProcessEvents.FriedFishReady)
.SendEventTo(new ProcessFunctionTargetBuilder(packOrderStep));
makePotatoFriesStep
.OnEvent(PotatoFriesProcess.ProcessEvents.PotatoFriesReady)
.SendEventTo(new ProcessFunctionTargetBuilder(packOrderStep));
makeFishSandwichStep
.OnEvent(FishSandwichProcess.ProcessEvents.FishSandwichReady)
.SendEventTo(new ProcessFunctionTargetBuilder(packOrderStep));
makeFishAndChipsStep
.OnEvent(FishAndChipsProcess.ProcessEvents.FishAndChipsReady)
.SendEventTo(new ProcessFunctionTargetBuilder(packOrderStep));
packOrderStep
.OnEvent(PackOrderStep.OutputEvents.FoodPacked)
.SendEventTo(new ProcessFunctionTargetBuilder(externalStep));
return processBuilder;
}
private sealed class DispatchSingleOrderStep : KernelProcessStep
{
public static class ProcessFunctions
{
public const string PrepareSingleOrder = nameof(PrepareSingleOrder);
}
public static class OutputEvents
{
public const string PrepareFries = nameof(PrepareFries);
public const string PrepareFriedFish = nameof(PrepareFriedFish);
public const string PrepareFishSandwich = nameof(PrepareFishSandwich);
public const string PrepareFishAndChips = nameof(PrepareFishAndChips);
}
[KernelFunction(ProcessFunctions.PrepareSingleOrder)]
public async Task DispatchSingleOrderAsync(KernelProcessStepContext context, FoodItem foodItem)
{
var foodName = foodItem.ToFriendlyString();
Console.WriteLine($"DISPATCH_SINGLE_ORDER: Dispatching '{foodName}'!");
var foodActions = new List<string>();
switch (foodItem)
{
case FoodItem.PotatoFries:
await context.EmitEventAsync(new() { Id = OutputEvents.PrepareFries, Data = foodActions });
break;
case FoodItem.FriedFish:
await context.EmitEventAsync(new() { Id = OutputEvents.PrepareFriedFish, Data = foodActions });
break;
case FoodItem.FishSandwich:
await context.EmitEventAsync(new() { Id = OutputEvents.PrepareFishSandwich, Data = foodActions });
break;
case FoodItem.FishAndChips:
await context.EmitEventAsync(new() { Id = OutputEvents.PrepareFishAndChips, Data = foodActions });
break;
default:
break;
}
}
}
private sealed class PackOrderStep : KernelProcessStep
{
public static class ProcessFunctions
{
public const string PackFood = nameof(PackFood);
}
public static class OutputEvents
{
public const string FoodPacked = nameof(FoodPacked);
}
[KernelFunction(ProcessFunctions.PackFood)]
public async Task PackFoodAsync(KernelProcessStepContext context, List<string> foodActions)
{
Console.WriteLine($"PACKING_FOOD: Food {foodActions.First()} Packed! - {JsonSerializer.Serialize(foodActions)}");
await context.EmitEventAsync(new() { Id = OutputEvents.FoodPacked });
}
}
private sealed class ExternalSingleOrderStep : ExternalStep
{
public ExternalSingleOrderStep() : base(ProcessEvents.SingleOrderReady) { }
}
}