Skip to content

Commit

Permalink
.Net: Processes fixing sample 1 to emit exit event (#9294)
Browse files Browse the repository at this point in the history
### 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 😄
  • Loading branch information
esttenorio authored Oct 16, 2024
1 parent 8b4a149 commit 6939ab8
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ public static class CommonEvents
{
public static readonly string UserInputReceived = nameof(UserInputReceived);
public static readonly string AssistantResponseGenerated = nameof(AssistantResponseGenerated);
public static readonly string Exit = nameof(Exit);
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,42 @@ public override ValueTask ActivateAsync(KernelProcessStepState<UserInputState> s
return ValueTask.CompletedTask;
}

internal string GetNextUserMessage()
{
if (_state != null && _state.CurrentInputIndex >= 0 && _state.CurrentInputIndex < this._state.UserInputs.Count)
{
var userMessage = this._state!.UserInputs[_state.CurrentInputIndex];
_state.CurrentInputIndex++;

Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine($"USER: {userMessage}");
Console.ResetColor();

return userMessage;
}

Console.WriteLine("SCRIPTED_USER_INPUT: No more scripted user messages defined, returning empty string as user message");
return string.Empty;
}

/// <summary>
/// Gets the user input.
/// Could be overridden to customize the output events to be emitted
/// </summary>
/// <param name="context">An instance of <see cref="KernelProcessStepContext"/> which can be
/// used to emit events from within a KernelFunction.</param>
/// <returns>A <see cref="ValueTask"/></returns>
[KernelFunction(Functions.GetUserInput)]
public async ValueTask GetUserInputAsync(KernelProcessStepContext context)
public virtual async ValueTask GetUserInputAsync(KernelProcessStepContext context)
{
var userMessage = _state!.UserInputs[_state.CurrentInputIndex];
_state.CurrentInputIndex++;

Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine($"USER: {userMessage}");
Console.ResetColor();

var userMessage = this.GetNextUserMessage();
// Emit the user input
if (string.IsNullOrEmpty(userMessage))
{
await context.EmitEventAsync(new() { Id = CommonEvents.Exit });
return;
}

await context.EmitEventAsync(new() { Id = CommonEvents.UserInputReceived, Data = userMessage });
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,22 @@ public override void PopulateUserInputs(UserInputState state)
state.UserInputs.Add("How low is the lowest valley?");
state.UserInputs.Add("How wide is the widest river?");
state.UserInputs.Add("exit");
state.UserInputs.Add("This text will be ignored because exit process condition was already met at this point.");
}

public override async ValueTask GetUserInputAsync(KernelProcessStepContext context)
{
var userMessage = this.GetNextUserMessage();

if (string.Equals(userMessage, "exit", StringComparison.OrdinalIgnoreCase))
{
// exit condition met, emitting exit event
await context.EmitEventAsync(new() { Id = ChatBotEvents.Exit, Data = userMessage });
return;
}

// emitting userInputReceived event
await context.EmitEventAsync(new() { Id = CommonEvents.UserInputReceived, Data = userMessage });
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ private KernelProcess SetupAccountOpeningProcess<TUserInputStep>() where TUserIn
.OnEvent(CommonEvents.UserInputReceived)
.SendEventTo(new ProcessFunctionTargetBuilder(newCustomerFormStep, CompleteNewCustomerFormStep.Functions.NewAccountProcessUserInfo, "userMessage"));

userInputStep
.OnEvent(CommonEvents.Exit)
.StopProcess();

// When the newCustomerForm step emits needs more details, send message to displayAssistantMessage step
newCustomerFormStep
.OnEvent(AccountOpeningEvents.NewCustomerFormNeedsMoreDetails)
Expand Down

0 comments on commit 6939ab8

Please sign in to comment.