@@ -20,16 +20,38 @@ export async function fetchJoinInfo(): Promise<{ url: string; token: string }> {
2020 if ( invoke ) {
2121 const res = await invoke ( "get_join_token" , [ ] ) ;
2222 return res . data [ "application/json" ] ;
23- } else if ( ( window as any ) . jupyterFetchJoinToken ) {
24- return await ( window as any ) . jupyterFetchJoinToken ( ) ;
25- } else if ( import . meta. env . MODE === "development" ) {
26- // use env variables
23+ }
24+
25+
26+ // This requires that JupyterLab was started with --LabApp.expose_app_in_browser,
27+ if ( ( window as any ) . jupyterapp && ( window as any ) . jupyterapp . shell ) {
28+ const currentWidget = ( window as any ) . jupyterapp . shell . currentWidget ;
29+ if ( currentWidget && currentWidget . context ?. sessionContext ) {
30+ const session = currentWidget . context . sessionContext . session ;
31+ if ( session && session . kernel ) {
32+ try {
33+ const comm = session . kernel . createComm ( "get_join_token_comm" ) ;
34+ comm . open ( ) ;
35+ comm . send ( { request : "token" } ) ;
36+ return new Promise ( ( resolve ) => {
37+ comm . onMsg = ( msg : any ) => {
38+ resolve ( msg . content . data ) ;
39+ } ;
40+ } ) ;
41+ } catch ( error ) {
42+ throw new Error ( "Error creating comm channel: " + error ) ;
43+ }
44+ }
45+ }
46+ }
47+
48+ if ( import . meta. env . MODE === "development" ) {
2749 const url = import . meta. env . VITE_LIVEKIT_URL ;
2850 const token = import . meta. env . VITE_LIVEKIT_TOKEN ;
29- return { url : url , token : token } ;
30- } else {
31- throw new Error ( "No Colab or Jupyter kernel function available" ) ;
51+ return { url, token } ;
3252 }
53+
54+ throw new Error ( "No suitable kernel connection available" ) ;
3355}
3456
3557
0 commit comments