Allow dynamic use of RunningKernels as trait objects

- Modify NativeRunningKernel and RemoteRunningKernel to return
  Box<dyn RunningKernel> 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.
This commit is contained in:
Kyle Kelley 2024-11-20 13:04:22 -08:00
parent 118e7a66b3
commit 473bc89d3a
3 changed files with 37 additions and 12 deletions

View file

@ -119,7 +119,7 @@ impl NativeRunningKernel {
// todo: convert to weak view
session: View<Session>,
cx: &mut WindowContext,
) -> Task<Result<Self>> {
) -> Task<Result<Box<dyn RunningKernel>>> {
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<dyn RunningKernel>)
})
}
}

View file

@ -42,7 +42,7 @@ impl RemoteRunningKernel {
working_directory: std::path::PathBuf,
session: View<Session>,
cx: &mut WindowContext,
) -> Task<anyhow::Result<Self>> {
) -> Task<Result<Box<dyn RunningKernel>>> {
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<dyn RunningKernel>)
})
}
}

View file

@ -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();
}