From 473bc89d3a850c74f27b6c19c0fcccb8f4691947 Mon Sep 17 00:00:00 2001 From: Kyle Kelley Date: Wed, 20 Nov 2024 13:04:22 -0800 Subject: [PATCH] Allow dynamic use of RunningKernels as trait objects - Modify NativeRunningKernel and RemoteRunningKernel to return Box instead of concrete types - Update Session to handle RunningKernels as trait objects - Implement trait methods for both native and remote kernels This also has remote kernel specs hooked up in a hardcoded way for now. --- crates/repl/src/kernels/native_kernel.rs | 6 ++-- crates/repl/src/kernels/remote_kernels.rs | 6 ++-- crates/repl/src/session.rs | 37 +++++++++++++++++++---- 3 files changed, 37 insertions(+), 12 deletions(-) diff --git a/crates/repl/src/kernels/native_kernel.rs b/crates/repl/src/kernels/native_kernel.rs index a0795bb19d..61f508293e 100644 --- a/crates/repl/src/kernels/native_kernel.rs +++ b/crates/repl/src/kernels/native_kernel.rs @@ -119,7 +119,7 @@ impl NativeRunningKernel { // todo: convert to weak view session: View, cx: &mut WindowContext, - ) -> Task> { + ) -> Task>> { cx.spawn(|cx| async move { let ip = IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)); let ports = peek_ports(ip).await?; @@ -309,7 +309,7 @@ impl NativeRunningKernel { .ok(); }); - anyhow::Ok(Self { + anyhow::Ok(Box::new(Self { process, request_tx, working_directory, @@ -320,7 +320,7 @@ impl NativeRunningKernel { connection_path, execution_state: ExecutionState::Idle, kernel_info: None, - }) + }) as Box) }) } } diff --git a/crates/repl/src/kernels/remote_kernels.rs b/crates/repl/src/kernels/remote_kernels.rs index aa151f6fd2..e2acfeebaf 100644 --- a/crates/repl/src/kernels/remote_kernels.rs +++ b/crates/repl/src/kernels/remote_kernels.rs @@ -42,7 +42,7 @@ impl RemoteRunningKernel { working_directory: std::path::PathBuf, session: View, cx: &mut WindowContext, - ) -> Task> { + ) -> Task>> { let remote_server = RemoteServer { base_url: kernelspec.url, token: kernelspec.token, @@ -90,7 +90,7 @@ impl RemoteRunningKernel { } }); - anyhow::Ok(Self { + anyhow::Ok(Box::new(Self { _routing_task: routing_task, _receiving_task: receiving_task, remote_server, @@ -99,7 +99,7 @@ impl RemoteRunningKernel { // todo(kyle): pull this from the kernel API to start with execution_state: ExecutionState::Idle, kernel_info: None, - }) + }) as Box) }) } } diff --git a/crates/repl/src/session.rs b/crates/repl/src/session.rs index df7ccddaf5..6b16530222 100644 --- a/crates/repl/src/session.rs +++ b/crates/repl/src/session.rs @@ -1,4 +1,5 @@ use crate::components::KernelListItem; +use crate::kernels::{RemoteKernelSpecification, RemoteRunningKernel}; use crate::setup_editor_session_actions; use crate::{ kernels::{Kernel, KernelSpecification, NativeRunningKernel}, @@ -22,8 +23,8 @@ use gpui::{ use language::Point; use project::Fs; use runtimelib::{ - ExecuteRequest, ExecutionState, InterruptRequest, JupyterMessage, JupyterMessageContent, - ShutdownRequest, + ExecuteRequest, ExecutionState, InterruptRequest, JupyterKernelspec, JupyterMessage, + JupyterMessageContent, ShutdownRequest, }; use std::{env::temp_dir, ops::Range, sync::Arc, time::Duration}; use theme::ActiveTheme; @@ -212,6 +213,27 @@ impl Session { }) .ok(); + // Creating a baked in kernel specification to see if remoting is working + let kernel_specification = KernelSpecification::Remote(RemoteKernelSpecification { + name: "todo".to_string(), + url: "http://localhost:8888/".to_string(), + token: std::env::var("JUPYTER_TOKEN").expect("JUPYTER_TOKEN not set"), + kernelspec: JupyterKernelspec { + argv: vec![ + "python".to_string(), + "-m".to_string(), + "ipykernel_launcher".to_string(), + "-f".to_string(), + "{connection_file}".to_string(), + ], + env: None, + display_name: "Python 3 (ipykernel)".to_string(), + language: "python".to_string(), + interrupt_mode: Some("signal".to_string()), + metadata: None, + }, + }); + let mut session = Self { fs, editor, @@ -253,9 +275,12 @@ impl Session { session_view, cx, ), - KernelSpecification::Remote(_remote_kernel_specification) => { - unimplemented!() - } + KernelSpecification::Remote(remote_kernel_specification) => RemoteRunningKernel::new( + remote_kernel_specification, + working_directory, + session_view, + cx, + ), }; let pending_kernel = cx @@ -265,7 +290,7 @@ impl Session { match kernel { Ok(kernel) => { this.update(&mut cx, |session, cx| { - session.kernel(Kernel::RunningKernel(Box::new(kernel)), cx); + session.kernel(Kernel::RunningKernel(kernel), cx); }) .ok(); }