forked from microsoft/semantic-kernel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDocumentGenerationGrpcClient.cs
74 lines (68 loc) · 3.03 KB
/
DocumentGenerationGrpcClient.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright (c) Microsoft. All rights reserved.
using System.Text.Json;
using Grpc.Net.Client;
using Microsoft.SemanticKernel;
using ProcessWithCloudEvents.Grpc.DocumentationGenerator;
using ProcessWithCloudEvents.Processes;
using ProcessWithCloudEvents.Processes.Models;
namespace ProcessWithCloudEvents.Grpc.Clients;
/// <summary>
/// Client that implements the <see cref="IExternalKernelProcessMessageChannel"/> interface used internally by the SK process
/// to emit events to external systems.<br/>
/// This implementation is an example of a gRPC client that emits events to a gRPC server
/// </summary>
public class DocumentGenerationGrpcClient : IExternalKernelProcessMessageChannel
{
private GrpcChannel? _grpcChannel;
private GrpcDocumentationGeneration.GrpcDocumentationGenerationClient? _grpcClient;
/// <inheritdoc/>
public async ValueTask Initialize()
{
this._grpcChannel = GrpcChannel.ForAddress("http://localhost:58641");
this._grpcClient = new GrpcDocumentationGeneration.GrpcDocumentationGenerationClient(this._grpcChannel);
}
/// <inheritdoc/>
public async ValueTask Uninitialize()
{
if (this._grpcChannel != null)
{
await this._grpcChannel.ShutdownAsync();
}
}
/// <inheritdoc/>
public async Task EmitExternalEventAsync(string externalTopicEvent, KernelProcessProxyMessage eventData)
{
if (this._grpcClient != null)
{
switch (externalTopicEvent)
{
case DocumentGenerationProcess.DocGenerationTopics.RequestUserReview:
var requestDocument = JsonSerializer.Deserialize<DocumentInfo>(eventData.EventData!.ToString()!);
if (requestDocument != null)
{
await this._grpcClient.RequestUserReviewDocumentationFromProcessAsync(new()
{
Title = requestDocument.Title,
AssistantMessage = "Document ready for user revision. Approve or reject document",
Content = requestDocument.Content,
ProcessData = new() { ProcessId = eventData.ProcessId }
});
}
return;
case DocumentGenerationProcess.DocGenerationTopics.PublishDocumentation:
var publishedDocument = JsonSerializer.Deserialize<DocumentInfo>(eventData.EventData!.ToString()!);
if (publishedDocument != null)
{
await this._grpcClient.PublishDocumentationAsync(new()
{
Title = publishedDocument.Title,
AssistantMessage = "Published Document Ready",
Content = publishedDocument.Content,
ProcessData = new() { ProcessId = eventData.ProcessId }
});
}
return;
}
}
}
}