Skip to content

Commit

Permalink
.Net: Process Framework: Simplify Event Emission in Process Steps (#9560
Browse files Browse the repository at this point in the history
)

### Motivation and Context
Fixes: #9510

Process Framework: Simplify Event Emission in Process Steps
I would like to propose a simplification of the event emission mechanism
within the Process Framework, specifically in process steps. Currently,
emitting events involves multiple method calls and boilerplate code,
which can make the codebase harder to read and maintain.

### Description
I created an overload of EmitEventAsync with simplified signature to
reduce developer cognitive load, updated some tests to verify on build.

If decided to update the signature completely (replace it) I can update
all the examples and tests.

### 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
joslat authored Nov 5, 2024
1 parent 6fee23c commit 3332079
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public async ValueTask DoItAsync(KernelProcessStepContext context)
{
Console.WriteLine("##### AStep ran.");
await Task.Delay(TimeSpan.FromSeconds(1));
await context.EmitEventAsync(new() { Id = CommonEvents.AStepDone, Data = "I did A" });
await context.EmitEventAsync(CommonEvents.AStepDone, "I did A");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,25 @@ public ValueTask EmitEventAsync(KernelProcessEvent processEvent)
{
return this._stepMessageChannel.EmitEventAsync(processEvent);
}

/// <summary>
/// Emit an event from the current step with a simplified method signature.
/// </summary>
/// <param name="eventId"></param>
/// <param name="data"></param>
/// <param name="visibility"></param>
/// <returns></returns>
public ValueTask EmitEventAsync(
string eventId,
object? data = null,
KernelProcessEventVisibility visibility = KernelProcessEventVisibility.Internal)
{
return this._stepMessageChannel.EmitEventAsync(
new KernelProcessEvent
{
Id = eventId,
Data = data,
Visibility = visibility
});
}
}
4 changes: 2 additions & 2 deletions dotnet/src/IntegrationTests/Processes/ProcessCycleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public static class Functions
public async ValueTask PrintWelcomeMessageAsync(KernelProcessStepContext context)
{
await context.EmitEventAsync(new() { Id = CommonEvents.StartARequested, Data = "Get Going A" });
await context.EmitEventAsync(new() { Id = CommonEvents.StartBRequested, Data = "Get Going B" });
await context.EmitEventAsync(CommonEvents.StartBRequested, "Get Going B", KernelProcessEventVisibility.Internal);
}
}

Expand All @@ -124,7 +124,7 @@ private sealed class BStep : KernelProcessStep
public async ValueTask DoItAsync(KernelProcessStepContext context)
{
await Task.Delay(TimeSpan.FromSeconds(2));
await context.EmitEventAsync(new() { Id = CommonEvents.BStepDone, Data = "I did B" });
await context.EmitEventAsync(CommonEvents.BStepDone, "I did B");
}
}

Expand Down

0 comments on commit 3332079

Please sign in to comment.