55 # in editable mode with pip. It is highly recommended to install
66 # the package from a stable release or in editable mode: https://pip.pypa.io/en/stable/topics/local-project-installs/#editable-installs
77 import warnings
8- warnings .warn ("Importing 'jupyterlab_commands_toolkit' outside a proper installation." )
8+
9+ warnings .warn (
10+ "Importing 'jupyterlab_commands_toolkit' outside a proper installation."
11+ )
912 __version__ = "dev"
1013
1114import pathlib
1215
1316from jupyter_server .serverapp import ServerApp
14- from .toolkit import toolkit
1517
16- # Export the AI toolkit for jupyter-ai integration
17- try :
18- from .toolkit import toolkit as ai_toolkit
19- except ImportError :
20- # If jupyter-ai is not available, the AI toolkit won't be available
21- toolkit = None
2218
2319def _jupyter_labextension_paths ():
24- return [{
25- "src" : "labextension" ,
26- "dest" : "jupyterlab-commands-toolkit"
27- }]
20+ return [{"src" : "labextension" , "dest" : "jupyterlab-commands-toolkit" }]
2821
2922
3023def _jupyter_server_extension_points ():
31- return [{
32- "module" : "jupyterlab_commands_toolkit"
33- }]
24+ return [{"module" : "jupyterlab_commands_toolkit" }]
25+
3426
3527def _load_jupyter_server_extension (serverapp : ServerApp ):
36- schema_path = pathlib .Path (__file__ ).parent / "events" / "jupyterlab-command.yml"
37- serverapp .event_logger .register_event_schema (schema_path )
38- serverapp .log .info ("jupyterlab_commands_toolkit extension loaded." )
28+ command_schema_path = (
29+ pathlib .Path (__file__ ).parent / "events" / "jupyterlab-command.yml"
30+ )
31+ serverapp .event_logger .register_event_schema (command_schema_path )
32+
33+ result_schema_path = (
34+ pathlib .Path (__file__ ).parent / "events" / "jupyterlab-command-result.yml"
35+ )
36+ serverapp .event_logger .register_event_schema (result_schema_path )
37+
38+ async def command_result_listener (logger , schema_id : str , data : dict ) -> None :
39+ """
40+ Handle command result events from the frontend.
41+
42+ This listener receives the results of JupyterLab commands that were
43+ executed in the frontend and processes them accordingly.
44+ """
45+ from .tools import handle_command_result
46+
47+ try :
48+ request_id = data .get ("requestId" , "unknown" )
49+ success = data .get ("success" , False )
50+ result = data .get ("result" )
51+ error = data .get ("error" )
52+
53+ serverapp .log .info (
54+ f"Received command result for request { request_id } : success={ success } "
55+ )
56+
57+ if success :
58+ if result is not None :
59+ serverapp .log .debug (f"Command result: { result } " )
60+ else :
61+ serverapp .log .warning (f"Command failed: { error } " )
62+
63+ handle_command_result (data )
64+
65+ except Exception as e :
66+ serverapp .log .error (f"Error processing command result: { e } " )
67+
68+ result_schema_id = (
69+ "https://events.jupyter.org/jupyterlab_command_toolkit/lab_command_result/v1"
70+ )
71+ serverapp .event_logger .add_listener (
72+ schema_id = result_schema_id , listener = command_result_listener
73+ )
74+
75+ serverapp .log .info (
76+ "jupyterlab_commands_toolkit extension loaded with bidirectional event communication."
77+ )
0 commit comments