| | | 1 | | using System.Runtime.CompilerServices; |
| | | 2 | | using System.Text; |
| | | 3 | | using System.Threading.Channels; |
| | | 4 | | using Elsa.AI.Abstractions.Contracts; |
| | | 5 | | using Elsa.AI.Abstractions.Models; |
| | | 6 | | using Elsa.AI.Copilot.Options; |
| | | 7 | | using GitHub.Copilot; |
| | | 8 | | using Microsoft.Extensions.AI; |
| | | 9 | | using Microsoft.Extensions.Logging; |
| | | 10 | | using Microsoft.Extensions.Options; |
| | | 11 | | |
| | | 12 | | namespace Elsa.AI.Copilot.Adapters; |
| | | 13 | | |
| | 0 | 14 | | public class CopilotProvider( |
| | 0 | 15 | | IOptions<CopilotOptions> options, |
| | 0 | 16 | | CopilotSessionEventMapper eventMapper, |
| | 0 | 17 | | ILogger<CopilotProvider> logger) : IAIProvider |
| | | 18 | | { |
| | 0 | 19 | | public string Name => options.Value.ProviderName ?? "copilot"; |
| | | 20 | | |
| | | 21 | | public ValueTask<AISessionHandle> CreateSessionAsync(CreateAISessionRequest request, CancellationToken cancellationT |
| | | 22 | | { |
| | 0 | 23 | | var providerName = request.ProviderConfiguration?.Name ?? options.Value.ProviderName ?? "copilot"; |
| | | 24 | | |
| | 0 | 25 | | return ValueTask.FromResult(new AISessionHandle |
| | 0 | 26 | | { |
| | 0 | 27 | | Id = request.ConversationId, |
| | 0 | 28 | | ProviderSessionId = $"{providerName}:{request.ConversationId}" |
| | 0 | 29 | | }); |
| | | 30 | | } |
| | | 31 | | |
| | | 32 | | public async IAsyncEnumerable<AIProviderEvent> ExecuteTurnAsync( |
| | | 33 | | AITurnRequest request, |
| | | 34 | | IAIProviderToolInvoker toolInvoker, |
| | | 35 | | [EnumeratorCancellation] CancellationToken cancellationToken = default) |
| | | 36 | | { |
| | 0 | 37 | | var copilotOptions = options.Value; |
| | 0 | 38 | | await using var client = CreateClient(copilotOptions); |
| | 0 | 39 | | await client.StartAsync(cancellationToken); |
| | | 40 | | |
| | 0 | 41 | | await using var session = await CreateOrResumeSessionAsync(client, request, toolInvoker, cancellationToken); |
| | 0 | 42 | | var events = Channel.CreateUnbounded<AIProviderEvent>(new UnboundedChannelOptions |
| | 0 | 43 | | { |
| | 0 | 44 | | SingleReader = true, |
| | 0 | 45 | | SingleWriter = false |
| | 0 | 46 | | }); |
| | | 47 | | |
| | 0 | 48 | | using var subscription = session.On<SessionEvent>(sessionEvent => |
| | 0 | 49 | | { |
| | 0 | 50 | | foreach (var providerEvent in eventMapper.Map(sessionEvent)) |
| | 0 | 51 | | events.Writer.TryWrite(providerEvent); |
| | 0 | 52 | | |
| | 0 | 53 | | if (sessionEvent is SessionIdleEvent or SessionErrorEvent) |
| | 0 | 54 | | events.Writer.TryComplete(); |
| | 0 | 55 | | }); |
| | | 56 | | |
| | | 57 | | try |
| | | 58 | | { |
| | 0 | 59 | | await session.SendAsync(new MessageOptions |
| | 0 | 60 | | { |
| | 0 | 61 | | Prompt = BuildPrompt(request), |
| | 0 | 62 | | DisplayPrompt = request.Message |
| | 0 | 63 | | }, cancellationToken); |
| | 0 | 64 | | } |
| | 0 | 65 | | catch (Exception e) when (e is not OperationCanceledException) |
| | | 66 | | { |
| | 0 | 67 | | events.Writer.TryComplete(e); |
| | 0 | 68 | | } |
| | | 69 | | |
| | 0 | 70 | | await foreach (var providerEvent in events.Reader.ReadAllAsync(cancellationToken)) |
| | 0 | 71 | | yield return providerEvent; |
| | 0 | 72 | | } |
| | | 73 | | |
| | | 74 | | private CopilotClient CreateClient(CopilotOptions copilotOptions) |
| | | 75 | | { |
| | 0 | 76 | | var clientOptions = new CopilotClientOptions |
| | 0 | 77 | | { |
| | 0 | 78 | | Connection = CreateRuntimeConnection(copilotOptions), |
| | 0 | 79 | | WorkingDirectory = copilotOptions.WorkingDirectory, |
| | 0 | 80 | | BaseDirectory = copilotOptions.BaseDirectory, |
| | 0 | 81 | | GitHubToken = copilotOptions.GitHubToken, |
| | 0 | 82 | | UseLoggedInUser = copilotOptions.UseLoggedInUser, |
| | 0 | 83 | | Logger = logger |
| | 0 | 84 | | }; |
| | | 85 | | |
| | 0 | 86 | | return new CopilotClient(clientOptions); |
| | | 87 | | } |
| | | 88 | | |
| | | 89 | | private static RuntimeConnection? CreateRuntimeConnection(CopilotOptions copilotOptions) |
| | | 90 | | { |
| | 0 | 91 | | if (!string.IsNullOrWhiteSpace(copilotOptions.RuntimeUrl)) |
| | 0 | 92 | | return RuntimeConnection.ForUri(copilotOptions.RuntimeUrl, copilotOptions.ConnectionToken); |
| | | 93 | | |
| | 0 | 94 | | if (!string.IsNullOrWhiteSpace(copilotOptions.RuntimePath) || copilotOptions.RuntimeArguments.Count > 0) |
| | 0 | 95 | | return RuntimeConnection.ForStdio(copilotOptions.RuntimePath, copilotOptions.RuntimeArguments.ToList()); |
| | | 96 | | |
| | 0 | 97 | | return null; |
| | | 98 | | } |
| | | 99 | | |
| | | 100 | | private async Task<CopilotSession> CreateOrResumeSessionAsync(CopilotClient client, AITurnRequest request, IAIProvid |
| | | 101 | | { |
| | 0 | 102 | | var providerSessionId = NormalizeSessionId(request.ProviderSessionId) ?? request.ConversationId; |
| | 0 | 103 | | var resumeConfig = ConfigureSession(new ResumeSessionConfig |
| | 0 | 104 | | { |
| | 0 | 105 | | ContinuePendingWork = true, |
| | 0 | 106 | | SuppressResumeEvent = true |
| | 0 | 107 | | }, request, toolInvoker); |
| | | 108 | | |
| | | 109 | | try |
| | | 110 | | { |
| | 0 | 111 | | return await client.ResumeSessionAsync(providerSessionId, resumeConfig, cancellationToken); |
| | | 112 | | } |
| | 0 | 113 | | catch (Exception e) when (e is not OperationCanceledException) |
| | | 114 | | { |
| | 0 | 115 | | logger.LogDebug(e, "Copilot session {ProviderSessionId} could not be resumed; creating a new session.", prov |
| | 0 | 116 | | } |
| | | 117 | | |
| | 0 | 118 | | var createConfig = ConfigureSession(new SessionConfig |
| | 0 | 119 | | { |
| | 0 | 120 | | SessionId = providerSessionId |
| | 0 | 121 | | }, request, toolInvoker); |
| | | 122 | | |
| | 0 | 123 | | return await client.CreateSessionAsync(createConfig, cancellationToken); |
| | 0 | 124 | | } |
| | | 125 | | |
| | | 126 | | private T ConfigureSession<T>(T config, AITurnRequest request, IAIProviderToolInvoker toolInvoker) where T : Session |
| | | 127 | | { |
| | 0 | 128 | | var copilotOptions = options.Value; |
| | 0 | 129 | | var providerConfiguration = request.ProviderConfiguration; |
| | 0 | 130 | | var model = providerConfiguration?.Model ?? copilotOptions.Model; |
| | | 131 | | |
| | 0 | 132 | | config.ClientName = "Elsa Weaver"; |
| | 0 | 133 | | config.Model = model; |
| | 0 | 134 | | config.ReasoningEffort = copilotOptions.ReasoningEffort; |
| | 0 | 135 | | config.Streaming = copilotOptions.EnableStreaming; |
| | 0 | 136 | | config.IncludeSubAgentStreamingEvents = copilotOptions.IncludeSubAgentStreamingEvents; |
| | 0 | 137 | | config.Tools = CreateTools(request.Tools, toolInvoker); |
| | 0 | 138 | | config.AvailableTools = request.Tools.Select(x => x.Name).Where(x => !string.IsNullOrWhiteSpace(x)).ToList(); |
| | 0 | 139 | | config.OnPermissionRequest = PermissionHandler.ApproveAll; |
| | | 140 | | |
| | 0 | 141 | | if (!string.IsNullOrWhiteSpace(providerConfiguration?.Endpoint)) |
| | 0 | 142 | | config.Provider = new ProviderConfig |
| | 0 | 143 | | { |
| | 0 | 144 | | Type = providerConfiguration.Provider, |
| | 0 | 145 | | BaseUrl = providerConfiguration.Endpoint, |
| | 0 | 146 | | ModelId = model |
| | 0 | 147 | | }; |
| | | 148 | | |
| | 0 | 149 | | return config; |
| | | 150 | | } |
| | | 151 | | |
| | | 152 | | private static ICollection<AIFunctionDeclaration> CreateTools(IReadOnlyCollection<AIToolDefinition> tools, IAIProvid |
| | 0 | 153 | | tools |
| | 0 | 154 | | .Where(x => !string.IsNullOrWhiteSpace(x.Name)) |
| | 0 | 155 | | .Select(x => (AIFunctionDeclaration)new ElsaCopilotToolFunction(x, toolInvoker)) |
| | 0 | 156 | | .ToList(); |
| | | 157 | | |
| | | 158 | | private static string BuildPrompt(AITurnRequest request) |
| | | 159 | | { |
| | 0 | 160 | | if (request.Context.Count == 0) |
| | 0 | 161 | | return request.Message; |
| | | 162 | | |
| | 0 | 163 | | var prompt = new StringBuilder(); |
| | 0 | 164 | | prompt.AppendLine(request.Message); |
| | 0 | 165 | | prompt.AppendLine(); |
| | 0 | 166 | | prompt.AppendLine("Elsa context references resolved by the server:"); |
| | | 167 | | |
| | 0 | 168 | | foreach (var context in request.Context) |
| | | 169 | | { |
| | 0 | 170 | | prompt.AppendLine(); |
| | 0 | 171 | | prompt.AppendLine($"- Kind: {context.Kind}"); |
| | 0 | 172 | | prompt.AppendLine($" ReferenceId: {context.ReferenceId}"); |
| | 0 | 173 | | if (!string.IsNullOrWhiteSpace(context.Summary)) |
| | 0 | 174 | | prompt.AppendLine($" Summary: {context.Summary}"); |
| | 0 | 175 | | if (context.Data.Count > 0) |
| | 0 | 176 | | prompt.AppendLine($" Data: {context.Data}"); |
| | | 177 | | } |
| | | 178 | | |
| | 0 | 179 | | return prompt.ToString(); |
| | | 180 | | } |
| | | 181 | | |
| | | 182 | | private static string? NormalizeSessionId(string? providerSessionId) |
| | | 183 | | { |
| | 0 | 184 | | if (string.IsNullOrWhiteSpace(providerSessionId)) |
| | 0 | 185 | | return null; |
| | | 186 | | |
| | 0 | 187 | | var separatorIndex = providerSessionId.IndexOf(':', StringComparison.Ordinal); |
| | 0 | 188 | | return separatorIndex < 0 ? providerSessionId : providerSessionId[(separatorIndex + 1)..]; |
| | | 189 | | } |
| | | 190 | | } |