Skip to content

Commit 3332079

Browse files
authored
.Net: Process Framework: Simplify Event Emission in Process Steps (#9560)
### 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 😄
1 parent 6fee23c commit 3332079

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

dotnet/samples/Demos/ProcessWithDapr/Controllers/ProcessController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public async ValueTask DoItAsync(KernelProcessStepContext context)
120120
{
121121
Console.WriteLine("##### AStep ran.");
122122
await Task.Delay(TimeSpan.FromSeconds(1));
123-
await context.EmitEventAsync(new() { Id = CommonEvents.AStepDone, Data = "I did A" });
123+
await context.EmitEventAsync(CommonEvents.AStepDone, "I did A");
124124
}
125125
}
126126

dotnet/src/Experimental/Process.Abstractions/KernelProcessStepContext.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,25 @@ public ValueTask EmitEventAsync(KernelProcessEvent processEvent)
2929
{
3030
return this._stepMessageChannel.EmitEventAsync(processEvent);
3131
}
32+
33+
/// <summary>
34+
/// Emit an event from the current step with a simplified method signature.
35+
/// </summary>
36+
/// <param name="eventId"></param>
37+
/// <param name="data"></param>
38+
/// <param name="visibility"></param>
39+
/// <returns></returns>
40+
public ValueTask EmitEventAsync(
41+
string eventId,
42+
object? data = null,
43+
KernelProcessEventVisibility visibility = KernelProcessEventVisibility.Internal)
44+
{
45+
return this._stepMessageChannel.EmitEventAsync(
46+
new KernelProcessEvent
47+
{
48+
Id = eventId,
49+
Data = data,
50+
Visibility = visibility
51+
});
52+
}
3253
}

dotnet/src/IntegrationTests/Processes/ProcessCycleTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public static class Functions
9898
public async ValueTask PrintWelcomeMessageAsync(KernelProcessStepContext context)
9999
{
100100
await context.EmitEventAsync(new() { Id = CommonEvents.StartARequested, Data = "Get Going A" });
101-
await context.EmitEventAsync(new() { Id = CommonEvents.StartBRequested, Data = "Get Going B" });
101+
await context.EmitEventAsync(CommonEvents.StartBRequested, "Get Going B", KernelProcessEventVisibility.Internal);
102102
}
103103
}
104104

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

0 commit comments

Comments
 (0)