-
Notifications
You must be signed in to change notification settings - Fork 4.1k
.Net: Process Cloud Events - Publish Events Abstractions #10477
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
esttenorio
merged 30 commits into
microsoft:main
from
esttenorio:estenori/cloudEventsPublishAbstraction
Mar 5, 2025
Merged
Changes from 26 commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
c775597
moving event data to a separate class, adding plumbing to capture sou…
esttenorio 79562b7
new abstractions of proxyStep related files
esttenorio 1584e57
LocalRuntime related plumbing of proxyStep - unhooking IExternal from…
esttenorio 3fd8f53
updating existing cloud event uts - working for localRuntime
esttenorio a89d5af
fixing spelling typos
esttenorio a893e10
cloud event abstractions working with dapr runtime
esttenorio 55687fd
addressing comments
esttenorio 0c6c030
addressing more comments -> proxybuilder related
esttenorio 4e04bf0
adding component UTs related to proxyBuilder and KernelProcessProxy
esttenorio 55d3277
fixing spelling typo
esttenorio e715c01
moving shared testing components to shared utilities + adding local r…
esttenorio bb4dd3b
adding more uts
esttenorio 752798d
removing class external messaging interface from processbuilder in th…
esttenorio 2424c0e
fixing pipeline build errors
esttenorio 1d43e3b
removing emit internal event from proxy
esttenorio 5592a6e
fixing #pragma warning restore CA2211 warning
esttenorio e2bdd7a
removing verify
esttenorio f7af833
fixing ut
esttenorio 407b9c9
Merge branch 'main' into estenori/cloudEventsPublishAbstraction
esttenorio 90e885e
Merge branch 'main' into estenori/cloudEventsPublishAbstraction
esttenorio 279387d
fixing uts
esttenorio ab44b2a
addressing comment
esttenorio 432d79c
more integration tests + moving steps to common steps
esttenorio eef446c
fixing test formatting error
esttenorio d123edb
fixing localproxy tests
esttenorio 9c67dfc
fixing formatting
esttenorio adfec30
addressing comments
esttenorio 7ec9643
missing comment fix
esttenorio 1846b80
fixing error
esttenorio 5425c18
missing change after renaming
esttenorio File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
dotnet/src/Experimental/Process.Abstractions/KernelProcessProxy.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
using System.Collections.Generic; | ||
using Microsoft.SemanticKernel.Process; | ||
using Microsoft.SemanticKernel.Process.Models; | ||
|
||
namespace Microsoft.SemanticKernel; | ||
|
||
/// <summary> | ||
/// A serializable representation of a ProcessProxy. | ||
/// </summary> | ||
public sealed record KernelProcessProxy : KernelProcessStepInfo | ||
esttenorio marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
/// <summary> | ||
/// Proxy metadata used for linking specific SK events to external events and viceversa | ||
/// </summary> | ||
public KernelProcessProxyStateMetadata? ProxyMetadata { get; init; } | ||
|
||
/// <summary> | ||
/// Creates a new instance of the <see cref="KernelProcess"/> class. | ||
/// </summary> | ||
/// <param name="state">The process state.</param> | ||
/// <param name="edges">The edges for the map.</param> | ||
public KernelProcessProxy(KernelProcessStepState state, Dictionary<string, List<KernelProcessEdge>> edges) | ||
: base(typeof(KernelProxyStep), state, edges) | ||
{ | ||
Verify.NotNullOrWhiteSpace(state.Name, $"{nameof(state)}.{nameof(KernelProcessStepState.Name)}"); | ||
Verify.NotNullOrWhiteSpace(state.Id, $"{nameof(state)}.{nameof(KernelProcessStepState.Id)}"); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
dotnet/src/Experimental/Process.Abstractions/KernelProcessProxyMessage.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
using System.Runtime.Serialization; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace Microsoft.SemanticKernel; | ||
|
||
/// <summary> | ||
/// A serializable representation of an internal message used in a process runtime received by proxy steps. | ||
/// </summary> | ||
/// <remarks> | ||
/// Initializes a new instance of the <see cref="KernelProcessProxyMessage"/> class. | ||
/// </remarks> | ||
[DataContract] | ||
public record KernelProcessProxyMessage | ||
{ | ||
/// <summary> | ||
/// Id of the SK process that emits the external event | ||
/// </summary> | ||
[DataMember] | ||
[JsonPropertyName("processId")] | ||
public string? ProcessId { get; init; } | ||
|
||
/// <summary> | ||
/// Name of the SK process that triggers sending the event externally | ||
/// </summary> | ||
[DataMember] | ||
[JsonPropertyName("triggerEventId")] | ||
public string? TriggerEventId { get; init; } | ||
|
||
/// <summary> | ||
/// Topic name used for publishing process event data externally | ||
/// </summary> | ||
[DataMember] | ||
[JsonPropertyName("externalTopicName")] | ||
public string? ExternalTopicName { get; init; } | ||
/// <summary> | ||
/// Event name used for publishing process event as another process event with a different event name | ||
/// </summary> | ||
[DataMember] | ||
[JsonPropertyName("proxyEventName")] | ||
public string? ProxyEventName { get; init; } | ||
/// <summary> | ||
/// Data to be emitted | ||
/// </summary> | ||
[DataMember] | ||
[JsonPropertyName("eventData")] | ||
public object? EventData { get; init; } | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
dotnet/src/Experimental/Process.Abstractions/KernelProxyStep.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using System.Threading.Tasks; | ||
|
||
namespace Microsoft.SemanticKernel.Process; | ||
|
||
/// <summary> | ||
/// Internal SK KernelProcessStep preconfigured to be used when emitting SK events outside of the SK Process Framework or inside with a different event name | ||
/// </summary> | ||
public sealed class KernelProxyStep : KernelProcessStep | ||
{ | ||
/// <summary> | ||
/// SK Function names in this SK Step as entry points | ||
/// </summary> | ||
public static class Functions | ||
{ | ||
/// <summary> | ||
/// Function name used to emit events externally | ||
/// </summary> | ||
public const string EmitExternalEvent = nameof(EmitExternalEvent); | ||
} | ||
|
||
/// <summary> | ||
/// Step function used to emit events externally | ||
/// </summary> | ||
/// <param name="context">instance of <see cref="KernelProcessStepContext"/></param> | ||
/// <param name="proxyEvent">event data passed to proxy step</param> | ||
/// <returns></returns> | ||
[KernelFunction(Functions.EmitExternalEvent)] | ||
public Task EmitExternalEventAsync(KernelProcessStepContext context, KernelProcessProxyMessage proxyEvent) | ||
{ | ||
Verify.NotNull(proxyEvent.ExternalTopicName, nameof(proxyEvent.ExternalTopicName)); | ||
return context.EmitExternalEventAsync(proxyEvent.ExternalTopicName, proxyEvent); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
dotnet/src/Experimental/Process.Abstractions/Models/KernelProcessProxyEventMetadata.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
using System.Runtime.Serialization; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace Microsoft.SemanticKernel.Process.Models; | ||
|
||
/// <summary> | ||
/// Process state used for State Persistence serialization | ||
/// </summary> | ||
public sealed record class KernelProcessProxyEventMetadata | ||
{ | ||
/// <summary> | ||
/// Name of the topic to be emitted externally | ||
/// </summary> | ||
[DataMember] | ||
[JsonPropertyName("topicName")] | ||
public string TopicName { get; set; } = string.Empty; | ||
|
||
/// <summary> | ||
/// Internal id used to identify the SK event | ||
/// </summary> | ||
[DataMember] | ||
[JsonPropertyName("eventId")] | ||
public string EventId { get; set; } = string.Empty; | ||
} |
26 changes: 26 additions & 0 deletions
26
dotnet/src/Experimental/Process.Abstractions/Models/KernelProcessProxyStateMetadata.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
using System.Collections.Generic; | ||
using System.Runtime.Serialization; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace Microsoft.SemanticKernel.Process.Models; | ||
|
||
/// <summary> | ||
/// Process state used for State Persistence serialization | ||
/// </summary> | ||
public sealed record class KernelProcessProxyStateMetadata : KernelProcessStepStateMetadata | ||
{ | ||
/// <summary> | ||
/// List of publish topics that can be used by the SK process | ||
/// </summary> | ||
[DataMember] | ||
[JsonPropertyName("publishTopics")] | ||
public List<string> PublishTopics { get; set; } = []; | ||
|
||
/// <summary> | ||
/// Map that stores which process events trigger external topic to be published and internal metadata information | ||
/// </summary> | ||
[DataMember] | ||
[JsonPropertyName("eventMetadata")] | ||
public Dictionary<string, KernelProcessProxyEventMetadata> EventMetadata { get; set; } = []; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
dotnet/src/Experimental/Process.Core/Internal/ProcessEventData.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
namespace Microsoft.SemanticKernel.Process.Internal; | ||
internal class ProcessEventData | ||
{ | ||
/// <summary> | ||
/// SK Process Event Id, id assigned during runtime | ||
/// </summary> | ||
public string EventId { get; init; } = string.Empty; | ||
|
||
/// <summary> | ||
/// SK Process Event Name, human readable, defined when creating the process builder | ||
/// </summary> | ||
public string EventName { get; init; } = string.Empty; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
dotnet/src/Experimental/Process.Core/ProcessProxyBuilder.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Microsoft.SemanticKernel.Process; | ||
using Microsoft.SemanticKernel.Process.Internal; | ||
using Microsoft.SemanticKernel.Process.Models; | ||
|
||
namespace Microsoft.SemanticKernel; | ||
|
||
/// <summary> | ||
/// Provides functionality to allow emitting external messages from within the SK | ||
/// process. | ||
/// </summary> | ||
public sealed class ProcessProxyBuilder : ProcessStepBuilder<KernelProxyStep> | ||
esttenorio marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="ProcessProxyBuilder"/> class. | ||
/// </summary> | ||
internal ProcessProxyBuilder(IReadOnlyList<string> externalTopics, string name) | ||
: base(name) | ||
{ | ||
if (externalTopics.Count == 0) | ||
{ | ||
throw new ArgumentException("No topic names registered"); | ||
} | ||
|
||
this.ExternalTopicUsage = externalTopics.ToDictionary(topic => topic, topic => false); | ||
if (this.ExternalTopicUsage.Count < externalTopics.Count) | ||
{ | ||
throw new ArgumentException("Topic names registered must be different"); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Version of the map-step, used when saving the state of the step. | ||
esttenorio marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// </summary> | ||
public string Version { get; init; } = "v1"; | ||
|
||
internal Dictionary<string, bool> ExternalTopicUsage { get; } | ||
|
||
// For supporting multiple step edges getting linked to the same external topic, current implementation needs to be updated | ||
// to instead have a list of potential edges in case event names in different steps have same name | ||
internal Dictionary<string, KernelProcessProxyEventMetadata> EventMetadata { get; } = []; | ||
esttenorio marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
internal ProcessFunctionTargetBuilder GetExternalFunctionTargetBuilder() | ||
{ | ||
return new ProcessFunctionTargetBuilder(this, functionName: KernelProxyStep.Functions.EmitExternalEvent, parameterName: "proxyEvent"); | ||
} | ||
|
||
internal void LinkTopicToStepEdgeInfo(string topicName, ProcessStepBuilder sourceStep, ProcessEventData eventData) | ||
{ | ||
if (!this.ExternalTopicUsage.TryGetValue(topicName, out bool usedTopic)) | ||
{ | ||
throw new InvalidOperationException($"Topic name {topicName} is not registered as proxy publish event, register first before using"); | ||
} | ||
|
||
if (usedTopic) | ||
{ | ||
throw new InvalidOperationException($"Topic name {topicName} is is already linked to another step edge"); | ||
} | ||
|
||
this.EventMetadata[eventData.EventName] = new() { EventId = eventData.EventId, TopicName = topicName }; | ||
this.ExternalTopicUsage[topicName] = true; | ||
esttenorio marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
/// <inheritdoc/> | ||
internal override KernelProcessStepInfo BuildStep(KernelProcessStepStateMetadata? stateMetadata = null) | ||
{ | ||
if (this.ExternalTopicUsage.All(topic => !topic.Value)) | ||
{ | ||
throw new InvalidOperationException("Proxy step does not have linked steps to it, link step edges to proxy or remove proxy step"); | ||
} | ||
|
||
KernelProcessProxyStateMetadata proxyMetadata = new() | ||
{ | ||
Name = this.Name, | ||
Id = this.Id, | ||
EventMetadata = this.EventMetadata, | ||
PublishTopics = this.ExternalTopicUsage.ToList().Select(topic => topic.Key).ToList(), | ||
}; | ||
|
||
// Build the edges first | ||
var builtEdges = this.Edges.ToDictionary(kvp => kvp.Key, kvp => kvp.Value.Select(e => e.Build()).ToList()); | ||
|
||
KernelProcessStepState state = new(this.Name, this.Version, this.Id); | ||
|
||
return new KernelProcessProxy(state, builtEdges) | ||
{ | ||
ProxyMetadata = proxyMetadata | ||
}; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.