Skip to content

Commit 6939ab8

Browse files
authored
.Net: Processes fixing sample 1 to emit exit event (#9294)
### Description Fixing processes sample 01 to emit exit event conditionally ### Contribution Checklist <!-- Before submitting this PR, please make sure: --> - [x] The code builds clean without any errors or warnings - [x] The PR follows the [SK Contribution Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md) and the [pre-submission formatting script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts) raises no violations - [x] All unit tests pass, and I have added new tests where possible - [x] I didn't break anyone 😄
1 parent 8b4a149 commit 6939ab8

File tree

4 files changed

+48
-8
lines changed

4 files changed

+48
-8
lines changed

dotnet/samples/GettingStartedWithProcesses/Events/CommonEvents.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ public static class CommonEvents
88
{
99
public static readonly string UserInputReceived = nameof(UserInputReceived);
1010
public static readonly string AssistantResponseGenerated = nameof(AssistantResponseGenerated);
11+
public static readonly string Exit = nameof(Exit);
1112
}

dotnet/samples/GettingStartedWithProcesses/SharedSteps/ScriptedUserInputStep.cs

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,23 +48,42 @@ public override ValueTask ActivateAsync(KernelProcessStepState<UserInputState> s
4848
return ValueTask.CompletedTask;
4949
}
5050

51+
internal string GetNextUserMessage()
52+
{
53+
if (_state != null && _state.CurrentInputIndex >= 0 && _state.CurrentInputIndex < this._state.UserInputs.Count)
54+
{
55+
var userMessage = this._state!.UserInputs[_state.CurrentInputIndex];
56+
_state.CurrentInputIndex++;
57+
58+
Console.ForegroundColor = ConsoleColor.Yellow;
59+
Console.WriteLine($"USER: {userMessage}");
60+
Console.ResetColor();
61+
62+
return userMessage;
63+
}
64+
65+
Console.WriteLine("SCRIPTED_USER_INPUT: No more scripted user messages defined, returning empty string as user message");
66+
return string.Empty;
67+
}
68+
5169
/// <summary>
5270
/// Gets the user input.
71+
/// Could be overridden to customize the output events to be emitted
5372
/// </summary>
5473
/// <param name="context">An instance of <see cref="KernelProcessStepContext"/> which can be
5574
/// used to emit events from within a KernelFunction.</param>
5675
/// <returns>A <see cref="ValueTask"/></returns>
5776
[KernelFunction(Functions.GetUserInput)]
58-
public async ValueTask GetUserInputAsync(KernelProcessStepContext context)
77+
public virtual async ValueTask GetUserInputAsync(KernelProcessStepContext context)
5978
{
60-
var userMessage = _state!.UserInputs[_state.CurrentInputIndex];
61-
_state.CurrentInputIndex++;
62-
63-
Console.ForegroundColor = ConsoleColor.Yellow;
64-
Console.WriteLine($"USER: {userMessage}");
65-
Console.ResetColor();
66-
79+
var userMessage = this.GetNextUserMessage();
6780
// Emit the user input
81+
if (string.IsNullOrEmpty(userMessage))
82+
{
83+
await context.EmitEventAsync(new() { Id = CommonEvents.Exit });
84+
return;
85+
}
86+
6887
await context.EmitEventAsync(new() { Id = CommonEvents.UserInputReceived, Data = userMessage });
6988
}
7089
}

dotnet/samples/GettingStartedWithProcesses/Step01/Step01_Processes.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,22 @@ public override void PopulateUserInputs(UserInputState state)
9595
state.UserInputs.Add("How low is the lowest valley?");
9696
state.UserInputs.Add("How wide is the widest river?");
9797
state.UserInputs.Add("exit");
98+
state.UserInputs.Add("This text will be ignored because exit process condition was already met at this point.");
99+
}
100+
101+
public override async ValueTask GetUserInputAsync(KernelProcessStepContext context)
102+
{
103+
var userMessage = this.GetNextUserMessage();
104+
105+
if (string.Equals(userMessage, "exit", StringComparison.OrdinalIgnoreCase))
106+
{
107+
// exit condition met, emitting exit event
108+
await context.EmitEventAsync(new() { Id = ChatBotEvents.Exit, Data = userMessage });
109+
return;
110+
}
111+
112+
// emitting userInputReceived event
113+
await context.EmitEventAsync(new() { Id = CommonEvents.UserInputReceived, Data = userMessage });
98114
}
99115
}
100116

dotnet/samples/GettingStartedWithProcesses/Step02/Step02_AccountOpening.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ private KernelProcess SetupAccountOpeningProcess<TUserInputStep>() where TUserIn
4747
.OnEvent(CommonEvents.UserInputReceived)
4848
.SendEventTo(new ProcessFunctionTargetBuilder(newCustomerFormStep, CompleteNewCustomerFormStep.Functions.NewAccountProcessUserInfo, "userMessage"));
4949

50+
userInputStep
51+
.OnEvent(CommonEvents.Exit)
52+
.StopProcess();
53+
5054
// When the newCustomerForm step emits needs more details, send message to displayAssistantMessage step
5155
newCustomerFormStep
5256
.OnEvent(AccountOpeningEvents.NewCustomerFormNeedsMoreDetails)

0 commit comments

Comments
 (0)