// Copyright (c) Microsoft. All rights reserved.
using Microsoft.SemanticKernel;
using ProcessWithCloudEvents.Processes.Steps;
namespace ProcessWithCloudEvents.Processes;
///
/// Components related to the SK Process for generating documentation
///
public static class DocumentGenerationProcess
{
///
/// SK Process events emitted by
///
public static class DocGenerationEvents
{
///
/// Event to start the document generation process
///
public const string StartDocumentGeneration = nameof(StartDocumentGeneration);
///
/// Event emitted when the user rejects the document
///
public const string UserRejectedDocument = nameof(UserRejectedDocument);
///
/// Event emitted when the user approves the document
///
public const string UserApprovedDocument = nameof(UserApprovedDocument);
}
///
/// SK Process topics emitted by
/// Topics are used to emit events to external systems
///
public static class DocGenerationTopics
{
///
/// Request user review document generation topic
///
public const string RequestUserReview = nameof(RequestUserReview);
///
/// Publish documentat generated topic
///
public const string PublishDocumentation = nameof(PublishDocumentation);
}
///
/// Creates a process builder for the Document Generation SK Process
///
/// name of the SK Process
/// instance of
public static ProcessBuilder CreateProcessBuilder(string processName = "DocumentationGeneration")
{
// Create the process builder
ProcessBuilder processBuilder = new(processName);
// Add the steps
var infoGatheringStep = processBuilder.AddStepFromType();
var docsGenerationStep = processBuilder.AddStepFromType();
var docsProofreadStep = processBuilder.AddStepFromType();
var docsPublishStep = processBuilder.AddStepFromType();
var proxyStep = processBuilder.AddProxyStep(id: processName, [DocGenerationTopics.RequestUserReview, DocGenerationTopics.PublishDocumentation]);
// Orchestrate the external input events
processBuilder
.OnInputEvent(DocGenerationEvents.StartDocumentGeneration)
.SendEventTo(new(infoGatheringStep));
processBuilder
.OnInputEvent(DocGenerationEvents.UserRejectedDocument)
.SendEventTo(new(docsGenerationStep, functionName: GenerateDocumentationStep.ProcessFunctions.ApplySuggestions));
processBuilder
.OnInputEvent(DocGenerationEvents.UserApprovedDocument)
.SendEventTo(new(docsPublishStep, parameterName: "userApproval"));
// Hooking up the rest of the process steps
infoGatheringStep
.OnFunctionResult()
.SendEventTo(new ProcessFunctionTargetBuilder(docsGenerationStep, functionName: GenerateDocumentationStep.ProcessFunctions.GenerateDocs));
docsGenerationStep
.OnEvent(GenerateDocumentationStep.OutputEvents.DocumentationGenerated)
.SendEventTo(new ProcessFunctionTargetBuilder(docsProofreadStep));
docsProofreadStep
.OnEvent(ProofReadDocumentationStep.OutputEvents.DocumentationRejected)
.SendEventTo(new ProcessFunctionTargetBuilder(docsGenerationStep, functionName: GenerateDocumentationStep.ProcessFunctions.ApplySuggestions));
// When the proofreader approves the documentation, send it to the 'docs' parameter of the docsPublishStep
// Additionally, the generated document is emitted externally for user approval using the pre-configured proxyStep
docsProofreadStep
.OnEvent(ProofReadDocumentationStep.OutputEvents.DocumentationApproved)
.EmitExternalEvent(proxyStep, DocGenerationTopics.RequestUserReview)
.SendEventTo(new ProcessFunctionTargetBuilder(docsPublishStep, parameterName: "document"));
// When event is approved by user, it gets published externally too
docsPublishStep
.OnFunctionResult()
.EmitExternalEvent(proxyStep, DocGenerationTopics.PublishDocumentation);
return processBuilder;
}
}