|
| 1 | +// Copyright (c) Microsoft. All rights reserved. |
| 2 | + |
| 3 | +using System.Collections.Concurrent; |
| 4 | +using Dapr.Actors.Client; |
| 5 | +using Grpc.Core; |
| 6 | +using Microsoft.SemanticKernel; |
| 7 | +using Microsoft.VisualStudio.Threading; |
| 8 | +using ProcessWithCloudEvents.Grpc.Contract; |
| 9 | +using ProcessWithCloudEvents.Processes; |
| 10 | + |
| 11 | +namespace ProcessWithCloudEvents.Grpc.Services; |
| 12 | + |
| 13 | +/// <summary> |
| 14 | +/// This gRPC service handles the generation of documents using/invoking a SK Process |
| 15 | +/// </summary> |
| 16 | +public class TeacherStudentInteractionService : GrpcTeacherStudentInteraction.GrpcTeacherStudentInteractionBase |
| 17 | +{ |
| 18 | + private readonly DaprKernelProcessFactory _kernelProcessFactory; |
| 19 | + private readonly ILogger<TeacherStudentInteractionService> _logger; |
| 20 | + private readonly Kernel _kernel; |
| 21 | + private readonly IActorProxyFactory _actorProxyFactory; |
| 22 | + private readonly ConcurrentDictionary<string, ConcurrentBag<IServerStreamWriter<MessageContent>>> _studentMessagesSubscribers; |
| 23 | + /// <summary> |
| 24 | + /// Constructor for the <see cref="DocumentGenerationService"/> |
| 25 | + /// </summary> |
| 26 | + /// <param name="logger"></param> |
| 27 | + /// <param name="kernel"></param> |
| 28 | + /// <param name="actorProxy"></param> |
| 29 | + /// <param name="kernelProcessFactory"></param> |
| 30 | + public TeacherStudentInteractionService(ILogger<TeacherStudentInteractionService> logger, Kernel kernel, IActorProxyFactory actorProxy, DaprKernelProcessFactory kernelProcessFactory) |
| 31 | + { |
| 32 | + this._logger = logger; |
| 33 | + this._kernel = kernel; |
| 34 | + this._actorProxyFactory = actorProxy; |
| 35 | + this._studentMessagesSubscribers = new(); |
| 36 | + this._kernelProcessFactory = kernelProcessFactory; |
| 37 | + } |
| 38 | + |
| 39 | + public override async Task<ProcessDetails> StartProcess(ProcessDetails request, ServerCallContext context) |
| 40 | + { |
| 41 | + var processId = string.IsNullOrEmpty(request.ProcessId) ? Guid.NewGuid().ToString() : request.ProcessId; |
| 42 | + var process = TeacherStudentProcess.CreateProcessBuilder().Build(); |
| 43 | + |
| 44 | + var processContext = await this._kernelProcessFactory.StartAsync(TeacherStudentProcess.Key, processId, new KernelProcessEvent() |
| 45 | + { |
| 46 | + Id = TeacherStudentProcess.ProcessEvents.StartProcess, |
| 47 | + Data = "Give me a welcome message with a brief summary of what you can do", |
| 48 | + }, |
| 49 | + this._actorProxyFactory); |
| 50 | + |
| 51 | + return new ProcessDetails { ProcessId = processId }; |
| 52 | + } |
| 53 | + |
| 54 | + public override async Task ReceiveStudentAgentResponse(ProcessDetails request, IServerStreamWriter<MessageContent> responseStream, ServerCallContext context) |
| 55 | + { |
| 56 | + var subscribers = this._studentMessagesSubscribers.GetOrAdd(request.ProcessId, []); |
| 57 | + subscribers.Add(responseStream); |
| 58 | + |
| 59 | + try |
| 60 | + { |
| 61 | + // Wait until the client disconnects |
| 62 | + await context.CancellationToken.WaitHandle.ToTask(); |
| 63 | + } |
| 64 | + finally |
| 65 | + { |
| 66 | + // Remove the subscriber when client disconnects |
| 67 | +#pragma warning disable CS8600 // Converting null literal or possible null value to non-nullable type. |
| 68 | + subscribers.TryTake(out responseStream); |
| 69 | +#pragma warning restore CS8600 // Converting null literal or possible null value to non-nullable type. |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + public override async Task<MessageContent> PublishStudentAgentResponseFromProcess(MessageContent request, ServerCallContext context) |
| 74 | + { |
| 75 | + if (this._studentMessagesSubscribers.TryGetValue(request.ProcessId, out var subscribers)) |
| 76 | + { |
| 77 | + foreach (var subscriber in subscribers) |
| 78 | + { |
| 79 | + await subscriber.WriteAsync(request).ConfigureAwait(false); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + return request; |
| 84 | + } |
| 85 | + |
| 86 | + public override async Task<MessageContent> RequestStudentAgentResponse(MessageContent request, ServerCallContext context) |
| 87 | + { |
| 88 | + var process = TeacherStudentProcess.CreateProcessBuilder().Build(); |
| 89 | + var processId = request.ProcessId; |
| 90 | + |
| 91 | + var processContext = await this._kernelProcessFactory.StartAsync(DocumentGenerationProcess.Key, processId, new KernelProcessEvent() |
| 92 | + { |
| 93 | + Id = TeacherStudentProcess.ProcessEvents.TeacherAskedQuestion, |
| 94 | + Data = request.Content, |
| 95 | + }, |
| 96 | + this._actorProxyFactory); |
| 97 | + |
| 98 | + return request; |
| 99 | + } |
| 100 | +} |
0 commit comments