/
CompositeKernel.cs
314 lines (252 loc) · 9.71 KB
/
CompositeKernel.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.CommandLine;
using System.CommandLine.Invocation;
using System.CommandLine.NamingConventionBinder;
using System.CommandLine.Parsing;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.DotNet.Interactive.Commands;
using Microsoft.DotNet.Interactive.Connection;
using Microsoft.DotNet.Interactive.Events;
using Microsoft.DotNet.Interactive.Parsing;
namespace Microsoft.DotNet.Interactive;
public sealed class CompositeKernel :
Kernel,
IEnumerable<Kernel>
{
private readonly KernelCollection _childKernels;
private string _defaultKernelName;
private Command _connectDirective;
private KernelHost _host;
private readonly ConcurrentDictionary<Type, string> _defaultKernelNamesByCommandType = new();
public CompositeKernel(string name = null) : base(name ?? ".NET")
{
KernelInfo.IsComposite = true;
_childKernels = new(this);
}
public string DefaultKernelName
{
get => _defaultKernelName ??
(ChildKernels.Count == 1
? ChildKernels.Single().Name
: null);
set => _defaultKernelName = value;
}
public void Add(Kernel kernel, IEnumerable<string> aliases = null)
{
if (kernel is null)
{
throw new ArgumentNullException(nameof(kernel));
}
if (kernel.ParentKernel is not null)
{
throw new InvalidOperationException($"Kernel \"{kernel.Name}\" already has a parent: \"{kernel.ParentKernel.Name}\".");
}
if (kernel is CompositeKernel)
{
throw new ArgumentException($"{nameof(CompositeKernel)} cannot be added as a child kernel.", nameof(kernel));
}
kernel.ParentKernel = this;
kernel.RootKernel = RootKernel;
kernel.SetScheduler(Scheduler);
if (aliases is not null)
{
kernel.KernelInfo.NameAndAliases.UnionWith(aliases);
}
AddChooseKernelDirective(kernel);
_childKernels.Add(kernel);
RegisterForDisposal(kernel.KernelEvents.Subscribe(PublishEvent));
RegisterForDisposal(kernel);
if (KernelInvocationContext.Current is { } current)
{
var kernelInfoProduced = new KernelInfoProduced(kernel.KernelInfo, current.Command);
current.Publish(kernelInfoProduced);
}
else
{
var kernelInfoProduced = new KernelInfoProduced(kernel.KernelInfo, KernelCommand.None);
PublishEvent(kernelInfoProduced);
}
}
public void SetDefaultTargetKernelNameForCommand(
Type commandType,
string kernelName)
{
_defaultKernelNamesByCommandType[commandType] = kernelName;
}
private void AddChooseKernelDirective(Kernel kernel)
{
var chooseKernelCommand = kernel.ChooseKernelDirective;
foreach (var alias in kernel.KernelInfo.Aliases)
{
chooseKernelCommand.AddAlias($"#!{alias}");
}
AddDirective(chooseKernelCommand);
}
public KernelCollection ChildKernels => _childKernels;
protected override void SetHandlingKernel(KernelCommand command, KernelInvocationContext context)
{
context.HandlingKernel = GetHandlingKernel(command, context);
}
private protected override Kernel GetHandlingKernel(
KernelCommand command,
KernelInvocationContext context)
{
Kernel kernel;
if (command.DestinationUri is not null)
{
if (_childKernels.TryGetByUri(command.DestinationUri, out kernel))
{
return kernel;
}
}
var targetKernelName = command.TargetKernelName;
if (targetKernelName is null)
{
if (CanHandle(command))
{
return this;
}
if (!_defaultKernelNamesByCommandType.TryGetValue(command.GetType(), out targetKernelName))
{
targetKernelName = DefaultKernelName;
}
}
if (targetKernelName is not null)
{
if (_childKernels.TryGetByAlias(targetKernelName, out kernel))
{
return kernel;
}
}
kernel = _childKernels.Count switch
{
0 => null,
1 => _childKernels.Single(),
_ => context?.HandlingKernel
};
if (kernel is null)
{
return this;
}
return kernel;
}
internal override async Task HandleAsync(
KernelCommand command,
KernelInvocationContext context)
{
if (!string.IsNullOrWhiteSpace(command.TargetKernelName) &&
_childKernels.TryGetByAlias(command.TargetKernelName, out var kernel))
{
// route to a subkernel
await kernel.Pipeline.SendAsync(command, context);
}
else
{
await base.HandleAsync(command, context);
}
}
private protected override async Task HandleRequestKernelInfoAsync(
RequestKernelInfo command,
KernelInvocationContext context)
{
context.Publish(new KernelInfoProduced(KernelInfo, command));
foreach (var childKernel in ChildKernels)
{
if (childKernel.SupportsCommand(command))
{
var childCommand = new RequestKernelInfo(childKernel.Name);
childCommand.SetParent(command, true);
childCommand.RoutingSlip.ContinueWith(command.RoutingSlip);
await childKernel.HandleAsync(childCommand, context);
}
}
}
private protected override IEnumerable<Parser> GetDirectiveParsersForCompletion(
DirectiveNode directiveNode,
int requestPosition)
{
var upToCursor =
directiveNode.Text[..requestPosition];
var indexOfPreviousSpace =
upToCursor.LastIndexOf(" ", StringComparison.CurrentCultureIgnoreCase);
var compositeKernelDirectiveParser = SubmissionParser.GetDirectiveParser();
if (indexOfPreviousSpace >= 0 &&
directiveNode is ActionDirectiveNode actionDirectiveNode)
{
// if the first token has been specified, we can narrow down to the specific directive parser that defines this directive
var directiveName = directiveNode.ChildNodesAndTokens[0].Text;
var kernel = this.FindKernelByName(actionDirectiveNode.ParentKernelName) ?? this;
var languageKernelDirectiveParser = kernel.SubmissionParser.GetDirectiveParser();
if (IsDirectiveDefinedIn(languageKernelDirectiveParser))
{
// the directive is defined in the subkernel, so this is the only directive parser we need
yield return languageKernelDirectiveParser;
}
else if (IsDirectiveDefinedIn(compositeKernelDirectiveParser))
{
yield return compositeKernelDirectiveParser;
}
bool IsDirectiveDefinedIn(Parser parser) =>
parser.Configuration.RootCommand.Children.GetByAlias(directiveName) is { };
}
else
{
// otherwise, return all directive parsers from the CompositeKernel as well as subkernels
yield return compositeKernelDirectiveParser;
foreach (var kernel in ChildKernels)
{
yield return kernel.SubmissionParser.GetDirectiveParser();
}
}
}
public IEnumerator<Kernel> GetEnumerator() => _childKernels.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
public void AddKernelConnector(ConnectKernelCommand connectionCommand)
{
if (_connectDirective is null)
{
_connectDirective = new Command(
"#!connect",
"Connects additional subkernels");
AddDirective(_connectDirective);
}
connectionCommand.Handler = CommandHandler.Create<KernelInvocationContext, InvocationContext>(
async (context, commandLineContext) =>
{
var connectedKernels = await connectionCommand.ConnectKernelsAsync(context, commandLineContext);
foreach (var connectedKernel in connectedKernels)
{
Add(connectedKernel);
// todo : here the connector should be used to patch the kernelInfo with the right destination uri for the proxy
var chooseKernelDirective =
Directives.OfType<ChooseKernelDirective>()
.Single(d => d.Kernel == connectedKernel);
if (!string.IsNullOrWhiteSpace(connectionCommand.ConnectedKernelDescription))
{
chooseKernelDirective.Description = connectionCommand.ConnectedKernelDescription;
}
chooseKernelDirective.Description += " (Connected kernel)";
context.Display($"Kernel added: #!{connectedKernel.Name}");
}
});
_connectDirective.Add(connectionCommand);
SubmissionParser.ResetParser();
}
public KernelHost Host => _host;
internal void SetHost(KernelHost host)
{
if (_host is { })
{
throw new InvalidOperationException("Host cannot be changed");
}
_host = host;
KernelInfo.Uri = _host.Uri;
_childKernels.NotifyThatHostWasSet();
}
}