-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[.Net][AutoGen.DotnetInteractive] add DotnetInteractiveStdioConnector (…
…#3337) * add DotnetInteractiveStdioCOnector * update * update * comment out DotnetInteractive test * add header * update
- Loading branch information
1 parent
972b4ed
commit f49ed29
Showing
9 changed files
with
367 additions
and
186 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 9 additions & 108 deletions
117
dotnet/src/AutoGen.DotnetInteractive/DotnetInteractiveKernelBuilder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,127 +1,28 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// DotnetInteractiveKernelBuilder.cs | ||
|
||
#if NET8_0_OR_GREATER | ||
using AutoGen.DotnetInteractive.Extension; | ||
using Microsoft.DotNet.Interactive; | ||
using Microsoft.DotNet.Interactive.Commands; | ||
using Microsoft.DotNet.Interactive.CSharp; | ||
using Microsoft.DotNet.Interactive.FSharp; | ||
using Microsoft.DotNet.Interactive.Jupyter; | ||
using Microsoft.DotNet.Interactive.PackageManagement; | ||
using Microsoft.DotNet.Interactive.PowerShell; | ||
|
||
namespace AutoGen.DotnetInteractive; | ||
|
||
public class DotnetInteractiveKernelBuilder | ||
public static class DotnetInteractiveKernelBuilder | ||
{ | ||
private readonly CompositeKernel compositeKernel; | ||
|
||
private DotnetInteractiveKernelBuilder() | ||
#if NET8_0_OR_GREATER | ||
public static InProccessDotnetInteractiveKernelBuilder CreateEmptyInProcessKernelBuilder() | ||
{ | ||
this.compositeKernel = new CompositeKernel(); | ||
|
||
// add jupyter connector | ||
this.compositeKernel.AddKernelConnector( | ||
new ConnectJupyterKernelCommand() | ||
.AddConnectionOptions(new JupyterHttpKernelConnectionOptions()) | ||
.AddConnectionOptions(new JupyterLocalKernelConnectionOptions())); | ||
return new InProccessDotnetInteractiveKernelBuilder(); | ||
} | ||
|
||
/// <summary> | ||
/// Create an empty builder. | ||
/// </summary> | ||
/// <returns></returns> | ||
public static DotnetInteractiveKernelBuilder CreateEmptyBuilder() | ||
{ | ||
return new DotnetInteractiveKernelBuilder(); | ||
} | ||
|
||
/// <summary> | ||
/// Create a default builder with C# and F# kernels. | ||
/// </summary> | ||
public static DotnetInteractiveKernelBuilder CreateDefaultBuilder() | ||
public static InProccessDotnetInteractiveKernelBuilder CreateDefaultInProcessKernelBuilder() | ||
{ | ||
return new DotnetInteractiveKernelBuilder() | ||
return new InProccessDotnetInteractiveKernelBuilder() | ||
.AddCSharpKernel() | ||
.AddFSharpKernel(); | ||
} | ||
#endif | ||
|
||
public DotnetInteractiveKernelBuilder AddCSharpKernel(IEnumerable<string>? aliases = null) | ||
{ | ||
aliases ??= ["c#", "C#"]; | ||
// create csharp kernel | ||
var csharpKernel = new CSharpKernel() | ||
.UseNugetDirective((k, resolvedPackageReference) => | ||
{ | ||
|
||
k.AddAssemblyReferences(resolvedPackageReference | ||
.SelectMany(r => r.AssemblyPaths)); | ||
return Task.CompletedTask; | ||
}) | ||
.UseKernelHelpers() | ||
.UseWho() | ||
.UseMathAndLaTeX() | ||
.UseValueSharing(); | ||
|
||
this.AddKernel(csharpKernel, aliases); | ||
|
||
return this; | ||
} | ||
|
||
public DotnetInteractiveKernelBuilder AddFSharpKernel(IEnumerable<string>? aliases = null) | ||
{ | ||
aliases ??= ["f#", "F#"]; | ||
// create fsharp kernel | ||
var fsharpKernel = new FSharpKernel() | ||
.UseDefaultFormatting() | ||
.UseKernelHelpers() | ||
.UseWho() | ||
.UseMathAndLaTeX() | ||
.UseValueSharing(); | ||
|
||
this.AddKernel(fsharpKernel, aliases); | ||
|
||
return this; | ||
} | ||
|
||
public DotnetInteractiveKernelBuilder AddPowershellKernel(IEnumerable<string>? aliases = null) | ||
{ | ||
aliases ??= ["pwsh", "powershell"]; | ||
// create powershell kernel | ||
var powershellKernel = new PowerShellKernel() | ||
.UseProfiles() | ||
.UseValueSharing(); | ||
|
||
this.AddKernel(powershellKernel, aliases); | ||
|
||
return this; | ||
} | ||
|
||
public DotnetInteractiveKernelBuilder AddPythonKernel(string venv, string kernelName = "python", IEnumerable<string>? aliases = null) | ||
{ | ||
aliases ??= [kernelName]; | ||
// create python kernel | ||
var magicCommand = $"#!connect jupyter --kernel-name {kernelName} --kernel-spec {venv}"; | ||
var connectCommand = new SubmitCode(magicCommand); | ||
var result = this.compositeKernel.SendAsync(connectCommand).Result; | ||
|
||
result.ThrowOnCommandFailed(); | ||
|
||
return this; | ||
} | ||
|
||
public CompositeKernel Build() | ||
{ | ||
return this.compositeKernel | ||
.UseDefaultMagicCommands() | ||
.UseImportMagicCommand(); | ||
} | ||
|
||
private DotnetInteractiveKernelBuilder AddKernel(Kernel kernel, IEnumerable<string>? aliases = null) | ||
public static DotnetInteractiveStdioKernelConnector CreateKernelBuilder(string workingDirectory, string kernelName = "root-proxy") | ||
{ | ||
this.compositeKernel.Add(kernel, aliases); | ||
return this; | ||
return new DotnetInteractiveStdioKernelConnector(workingDirectory, kernelName); | ||
} | ||
} | ||
#endif |
86 changes: 86 additions & 0 deletions
86
dotnet/src/AutoGen.DotnetInteractive/DotnetInteractiveStdioKernelConnector.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// DotnetInteractiveStdioKernelConnector.cs | ||
|
||
using AutoGen.DotnetInteractive.Extension; | ||
using Microsoft.DotNet.Interactive; | ||
using Microsoft.DotNet.Interactive.Commands; | ||
using Microsoft.DotNet.Interactive.Connection; | ||
|
||
namespace AutoGen.DotnetInteractive; | ||
|
||
public class DotnetInteractiveStdioKernelConnector | ||
{ | ||
private string workingDirectory; | ||
private InteractiveService interactiveService; | ||
private string kernelName; | ||
private List<SubmitCode> setupCommands = new List<SubmitCode>(); | ||
|
||
internal DotnetInteractiveStdioKernelConnector(string workingDirectory, string kernelName = "root-proxy") | ||
{ | ||
this.workingDirectory = workingDirectory; | ||
this.interactiveService = new InteractiveService(workingDirectory); | ||
this.kernelName = kernelName; | ||
} | ||
|
||
public DotnetInteractiveStdioKernelConnector RestoreDotnetInteractive() | ||
{ | ||
if (this.interactiveService.RestoreDotnetInteractive()) | ||
{ | ||
return this; | ||
} | ||
else | ||
{ | ||
throw new Exception("Failed to restore dotnet interactive tool."); | ||
} | ||
} | ||
|
||
public DotnetInteractiveStdioKernelConnector AddPythonKernel( | ||
string venv, | ||
string kernelName = "python") | ||
{ | ||
var magicCommand = $"#!connect jupyter --kernel-name {kernelName} --kernel-spec {venv}"; | ||
var connectCommand = new SubmitCode(magicCommand); | ||
|
||
this.setupCommands.Add(connectCommand); | ||
|
||
return this; | ||
} | ||
|
||
public async Task<Kernel> BuildAsync(CancellationToken ct = default) | ||
{ | ||
var compositeKernel = new CompositeKernel(); | ||
var url = KernelHost.CreateHostUri(this.kernelName); | ||
var cmd = new string[] | ||
{ | ||
"dotnet", | ||
"tool", | ||
"run", | ||
"dotnet-interactive", | ||
$"[cb-{this.kernelName}]", | ||
"stdio", | ||
//"--default-kernel", | ||
//"csharp", | ||
"--working-dir", | ||
$@"""{workingDirectory}""", | ||
}; | ||
var connector = new StdIoKernelConnector( | ||
cmd, | ||
this.kernelName, | ||
url, | ||
new DirectoryInfo(this.workingDirectory)); | ||
var rootProxyKernel = await connector.CreateRootProxyKernelAsync(); | ||
rootProxyKernel.KernelInfo.SupportedKernelCommands.Add(new(nameof(SubmitCode))); | ||
var dotnetKernel = await connector.CreateProxyKernelAsync(".NET"); | ||
foreach (var setupCommand in this.setupCommands) | ||
{ | ||
var setupCommandResult = await rootProxyKernel.SendAsync(setupCommand, ct); | ||
setupCommandResult.ThrowOnCommandFailed(); | ||
} | ||
|
||
return rootProxyKernel; | ||
} | ||
} |
110 changes: 110 additions & 0 deletions
110
dotnet/src/AutoGen.DotnetInteractive/InProccessDotnetInteractiveKernelBuilder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// InProccessDotnetInteractiveKernelBuilder.cs | ||
|
||
#if NET8_0_OR_GREATER | ||
using AutoGen.DotnetInteractive.Extension; | ||
using Microsoft.DotNet.Interactive; | ||
using Microsoft.DotNet.Interactive.Commands; | ||
using Microsoft.DotNet.Interactive.CSharp; | ||
using Microsoft.DotNet.Interactive.FSharp; | ||
using Microsoft.DotNet.Interactive.Jupyter; | ||
using Microsoft.DotNet.Interactive.PackageManagement; | ||
using Microsoft.DotNet.Interactive.PowerShell; | ||
|
||
namespace AutoGen.DotnetInteractive; | ||
|
||
/// <summary> | ||
/// Build an in-proc dotnet interactive kernel. | ||
/// </summary> | ||
public class InProccessDotnetInteractiveKernelBuilder | ||
{ | ||
private readonly CompositeKernel compositeKernel; | ||
|
||
internal InProccessDotnetInteractiveKernelBuilder() | ||
{ | ||
this.compositeKernel = new CompositeKernel(); | ||
|
||
// add jupyter connector | ||
this.compositeKernel.AddKernelConnector( | ||
new ConnectJupyterKernelCommand() | ||
.AddConnectionOptions(new JupyterHttpKernelConnectionOptions()) | ||
.AddConnectionOptions(new JupyterLocalKernelConnectionOptions())); | ||
} | ||
|
||
public InProccessDotnetInteractiveKernelBuilder AddCSharpKernel(IEnumerable<string>? aliases = null) | ||
{ | ||
aliases ??= ["c#", "C#", "csharp"]; | ||
// create csharp kernel | ||
var csharpKernel = new CSharpKernel() | ||
.UseNugetDirective((k, resolvedPackageReference) => | ||
{ | ||
|
||
k.AddAssemblyReferences(resolvedPackageReference | ||
.SelectMany(r => r.AssemblyPaths)); | ||
return Task.CompletedTask; | ||
}) | ||
.UseKernelHelpers() | ||
.UseWho() | ||
.UseMathAndLaTeX() | ||
.UseValueSharing(); | ||
|
||
this.AddKernel(csharpKernel, aliases); | ||
|
||
return this; | ||
} | ||
|
||
public InProccessDotnetInteractiveKernelBuilder AddFSharpKernel(IEnumerable<string>? aliases = null) | ||
{ | ||
aliases ??= ["f#", "F#", "fsharp"]; | ||
// create fsharp kernel | ||
var fsharpKernel = new FSharpKernel() | ||
.UseDefaultFormatting() | ||
.UseKernelHelpers() | ||
.UseWho() | ||
.UseMathAndLaTeX() | ||
.UseValueSharing(); | ||
|
||
this.AddKernel(fsharpKernel, aliases); | ||
|
||
return this; | ||
} | ||
|
||
public InProccessDotnetInteractiveKernelBuilder AddPowershellKernel(IEnumerable<string>? aliases = null) | ||
{ | ||
aliases ??= ["pwsh", "powershell"]; | ||
// create powershell kernel | ||
var powershellKernel = new PowerShellKernel() | ||
.UseProfiles() | ||
.UseValueSharing(); | ||
|
||
this.AddKernel(powershellKernel, aliases); | ||
|
||
return this; | ||
} | ||
|
||
public InProccessDotnetInteractiveKernelBuilder AddPythonKernel(string venv, string kernelName = "python") | ||
{ | ||
// create python kernel | ||
var magicCommand = $"#!connect jupyter --kernel-name {kernelName} --kernel-spec {venv}"; | ||
var connectCommand = new SubmitCode(magicCommand); | ||
var result = this.compositeKernel.SendAsync(connectCommand).Result; | ||
|
||
result.ThrowOnCommandFailed(); | ||
|
||
return this; | ||
} | ||
|
||
public CompositeKernel Build() | ||
{ | ||
return this.compositeKernel | ||
.UseDefaultMagicCommands() | ||
.UseImportMagicCommand(); | ||
} | ||
|
||
private InProccessDotnetInteractiveKernelBuilder AddKernel(Kernel kernel, IEnumerable<string>? aliases = null) | ||
{ | ||
this.compositeKernel.Add(kernel, aliases); | ||
return this; | ||
} | ||
} | ||
#endif |
Oops, something went wrong.