这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion MLS.Agent/CommandLine/CommandLineParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -375,12 +375,13 @@ Command Jupyter()
.Trace()
.Handle(delivery));
})
.AddTransient<IKernel>(c => new CompositeKernel
.AddSingleton<IKernel>(c => new CompositeKernel
{
new CSharpKernel()
.UseDefaultRendering()
.UseNugetDirective()
.UseExtendDirective()
.UseKernelHelpers()
})
.AddSingleton(c => new JupyterRequestContextHandler(
c.GetRequiredService<PackageRegistry>(),
Expand Down
23 changes: 11 additions & 12 deletions Microsoft.DotNet.Interactive.Jupyter/ExecuteRequestHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,13 @@ protected override void OnKernelEvent(IKernelEvent @event)
switch (@event)
{
case ValueProduced valueProduced:
OnValueProduced(valueProduced, InFlightRequests);
OnValueProduced(valueProduced);
break;
case CodeSubmissionEvaluated codeSubmissionEvaluated:
OnCodeSubmissionEvaluated(codeSubmissionEvaluated, InFlightRequests);
OnCodeSubmissionEvaluated(codeSubmissionEvaluated);
break;
case CodeSubmissionEvaluationFailed codeSubmissionEvaluationFailed:
OnCodeSubmissionEvaluatedFailed(codeSubmissionEvaluationFailed, InFlightRequests);
OnCodeSubmissionEvaluatedFailed(codeSubmissionEvaluationFailed);
break;
case CodeSubmissionReceived _:
case IncompleteCodeSubmissionReceived _:
Expand All @@ -105,9 +105,9 @@ protected override void OnKernelEvent(IKernelEvent @event)
}
}

private static void OnCodeSubmissionEvaluatedFailed(CodeSubmissionEvaluationFailed codeSubmissionEvaluationFailed, ConcurrentDictionary<IKernelCommand, InflightRequest> openRequests)
private void OnCodeSubmissionEvaluatedFailed(CodeSubmissionEvaluationFailed codeSubmissionEvaluationFailed)
{
openRequests.TryRemove(codeSubmissionEvaluationFailed.Command, out var openRequest);
InFlightRequests.TryRemove(codeSubmissionEvaluationFailed.Command, out var openRequest);

var errorContent = new Error(
eName: "Unhandled Exception",
Expand Down Expand Up @@ -144,11 +144,10 @@ private static void OnCodeSubmissionEvaluatedFailed(CodeSubmissionEvaluationFail
openRequest.Dispose();
}

private static void OnValueProduced(
ValueProduced valueProduced,
ConcurrentDictionary<IKernelCommand, InflightRequest> openRequests)
private void OnValueProduced(ValueProduced valueProduced)
{
openRequests.TryGetValue(valueProduced.Command, out var openRequest);
var openRequest = InFlightRequests.Values.SingleOrDefault();

if (openRequest == null)
{
return;
Expand Down Expand Up @@ -203,10 +202,10 @@ private static void OnValueProduced(
}
}

private static void OnCodeSubmissionEvaluated(CodeSubmissionEvaluated codeSubmissionEvaluated,
ConcurrentDictionary<IKernelCommand, InflightRequest> openRequests)
private void OnCodeSubmissionEvaluated(CodeSubmissionEvaluated codeSubmissionEvaluated)
{
openRequests.TryRemove(codeSubmissionEvaluated.Command, out var openRequest);
InFlightRequests.TryRemove(codeSubmissionEvaluated.Command, out var openRequest);

// reply ok
var executeReplyPayload = new ExecuteReplyOk(executionCount: openRequest.ExecutionCount);

Expand Down
17 changes: 17 additions & 0 deletions Microsoft.DotNet.Interactive/Commands/AddNugetPackage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// 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;

namespace Microsoft.DotNet.Interactive.Commands
{
public class AddNugetPackage : KernelCommandBase
{
public AddNugetPackage(NugetPackageReference packageReference)
{
PackageReference = packageReference ?? throw new ArgumentNullException(nameof(packageReference));
}

public NugetPackageReference PackageReference { get; }
}
}
3 changes: 3 additions & 0 deletions Microsoft.DotNet.Interactive/Commands/IKernelCommand.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
// 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.Threading.Tasks;

namespace Microsoft.DotNet.Interactive.Commands
{
public interface IKernelCommand
{
Task InvokeAsync(KernelInvocationContext context);
}
}
8 changes: 8 additions & 0 deletions Microsoft.DotNet.Interactive/Commands/KernelCommandBase.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
// 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.Threading.Tasks;

namespace Microsoft.DotNet.Interactive.Commands
{
public abstract class KernelCommandBase : IKernelCommand
{
public KernelCommandInvocation Handler { get; set; }

public async Task InvokeAsync(KernelInvocationContext context)
{
await Handler(context);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@

using System;
using System.IO;
using Microsoft.DotNet.Interactive.Commands;

namespace Microsoft.DotNet.Interactive
namespace Microsoft.DotNet.Interactive.Commands
{
public class LoadExtension : KernelCommandBase
{
Expand Down
2 changes: 2 additions & 0 deletions Microsoft.DotNet.Interactive/Commands/SubmitCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,7 @@ public SubmitCode(
public string Code { get; set; }

public string TargetKernelName { get; set; }

public override string ToString() => $"{base.ToString()}: {Code.TruncateForDisplay()}";
}
}
6 changes: 3 additions & 3 deletions Microsoft.DotNet.Interactive/CompositeKernel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public CompositeKernel()
};

chooseKernelCommand.Handler =
CommandHandler.Create<string, KernelPipelineContext>((kernelName, context) =>
CommandHandler.Create<string, KernelInvocationContext>((kernelName, context) =>
{
DefaultKernel = this.Single(k => k.Name == kernelName);
});
Expand Down Expand Up @@ -56,7 +56,7 @@ public void Add(IKernel kernel)

protected override void SetKernel(
IKernelCommand command,
KernelPipelineContext context)
KernelInvocationContext context)
{
if (context.Kernel == null)
{
Expand All @@ -73,7 +73,7 @@ protected override void SetKernel(

protected internal override async Task HandleAsync(
IKernelCommand command,
KernelPipelineContext context)
KernelInvocationContext context)
{
var kernel = context.Kernel;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ public CodeSubmissionEvaluated(SubmitCode command) : base(command)
}

public string Code => ((SubmitCode) Command).Code;

public override string ToString() => $"{base.ToString()}: {Code.TruncateForDisplay()}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@ public CodeSubmissionEvaluationFailed(
: message;
}

public string Code => ((SubmitCode)Command).Code;

public Exception Exception { get; }

public string Message { get; }

public override string ToString() => $"{base.ToString()}: {Code}";
}
}
4 changes: 4 additions & 0 deletions Microsoft.DotNet.Interactive/Events/CodeSubmissionReceived.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ public CodeSubmissionReceived(string value, SubmitCode submitCode) : base(submit
Value = value ?? throw new ArgumentNullException(nameof(value));
}

public string Code => ((SubmitCode)Command).Code;

public string Value { get; }

public override string ToString() => $"{base.ToString()}: {Value.TruncateForDisplay()}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,9 @@ public class CompleteCodeSubmissionReceived : KernelEventBase
public CompleteCodeSubmissionReceived(SubmitCode submitCode) : base(submitCode)
{
}

public string Code => ((SubmitCode)Command).Code;

public override string ToString() => $"{base.ToString()}: {Code.TruncateForDisplay()}";
}
}
11 changes: 6 additions & 5 deletions Microsoft.DotNet.Interactive/Events/KernelEventBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ namespace Microsoft.DotNet.Interactive.Events
{
public abstract class KernelEventBase : IKernelEvent
{
protected KernelEventBase(IKernelCommand command)
protected KernelEventBase(IKernelCommand command = null)
{
Command = command ?? throw new ArgumentNullException(nameof(command));
Command = command ?? KernelInvocationContext.Current?.Command;
}

protected KernelEventBase()
public IKernelCommand Command { get; }

public override string ToString()
{
return $"{GetType().Name}";
}

public IKernelCommand Command { get; }
}
}
27 changes: 27 additions & 0 deletions Microsoft.DotNet.Interactive/Kernel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// 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.Threading.Tasks;
using Microsoft.DotNet.Interactive.Commands;
using Microsoft.DotNet.Interactive.Rendering;

namespace Microsoft.DotNet.Interactive
{
public static class Kernel
{
public static void Display(
object value,
string mimeType = HtmlFormatter.MimeType)
{
var formatted = new FormattedValue(
mimeType,
value.ToDisplayString(mimeType));

var kernel = KernelInvocationContext.Current.Kernel;

Task.Run(() =>
kernel.SendAsync(new DisplayValue(formatted)))
.Wait();
}
}
}
Loading