From 86bdbf28177b8013cae547aa5cf63087015a84f1 Mon Sep 17 00:00:00 2001 From: Zach Sailer Date: Tue, 29 Jul 2025 10:43:24 -0700 Subject: [PATCH] Update to new toolkit API --- README.md | 51 +++++++++++++++++++++++++ jupyterlab_commands_toolkit/__init__.py | 11 +++++- jupyterlab_commands_toolkit/toolkit.py | 50 ++++++++++++++++++++++++ jupyterlab_commands_toolkit/tools.py | 19 +-------- pyproject.toml | 3 +- 5 files changed, 113 insertions(+), 21 deletions(-) create mode 100644 jupyterlab_commands_toolkit/toolkit.py diff --git a/README.md b/README.md index 07cb92d..6102c25 100644 --- a/README.md +++ b/README.md @@ -8,9 +8,17 @@ This extension is composed of a Python package named `jupyterlab_commands_toolki for the server extension and a NPM package named `jupyterlab-commands-toolkit` for the frontend extension. +## Features + +- **Document Management**: Open documents in JupyterLab with various layout modes +- **Markdown Preview**: Open markdown files in rendered preview mode +- **Notebook Operations**: Clear outputs and show diffs for notebooks +- **Jupyter AI Integration**: Tools available for use with Jupyter AI + ## Requirements - JupyterLab >= 4.0.0 +- jupyter-ai (for AI toolkit functionality) ## Install @@ -20,6 +28,49 @@ To install the extension, execute: pip install jupyterlab_commands_toolkit ``` +## Usage + +### With Jupyter AI + +The extension provides a toolkit for Jupyter AI with the following tools: + +1. **open_document_tool**: Open documents in JupyterLab with various layout modes +2. **open_markdown_preview_tool**: Open markdown files in rendered preview mode +3. **clear_notebook_outputs_tool**: Clear all outputs in the active notebook +4. **show_notebook_diff_tool**: Show git diff for the current notebook using nbdime + +```python +# Access the AI toolkit +from jupyterlab_commands_toolkit import ai_toolkit + +# The toolkit is automatically available to Jupyter AI when installed +``` + +### Direct Usage + +You can also use the commands directly: + +```python +from jupyterlab_commands_toolkit.tools import ( + open_document, + open_markdown_file_in_preview_mode, + clear_all_outputs_in_notebook, + show_diff_of_current_notebook +) + +# Open a document +open_document("notebook.ipynb", mode="split-right") + +# Open markdown in preview +open_markdown_file_in_preview_mode("README.md") + +# Clear notebook outputs +clear_all_outputs_in_notebook(True) + +# Show notebook diff +show_diff_of_current_notebook(True) +``` + ## Uninstall To remove the extension, execute: diff --git a/jupyterlab_commands_toolkit/__init__.py b/jupyterlab_commands_toolkit/__init__.py index ac4a1b1..e6032d5 100644 --- a/jupyterlab_commands_toolkit/__init__.py +++ b/jupyterlab_commands_toolkit/__init__.py @@ -9,9 +9,16 @@ __version__ = "dev" import pathlib -from jupyter_server_ai_tools.models import ToolSet, Toolkit + from jupyter_server.serverapp import ServerApp -from .tools import TOOLS +from .toolkit import toolkit + +# Export the AI toolkit for jupyter-ai integration +try: + from .toolkit import toolkit as ai_toolkit +except ImportError: + # If jupyter-ai is not available, the AI toolkit won't be available + toolkit = None def _jupyter_labextension_paths(): return [{ diff --git a/jupyterlab_commands_toolkit/toolkit.py b/jupyterlab_commands_toolkit/toolkit.py new file mode 100644 index 0000000..16e10b5 --- /dev/null +++ b/jupyterlab_commands_toolkit/toolkit.py @@ -0,0 +1,50 @@ +"""JupyterLab Commands toolkit for Jupyter AI""" +from jupyter_ai.tools.models import Tool, Toolkit + +from typing import Optional +from .tools import ( + open_document, + open_markdown_file_in_preview_mode, + clear_all_outputs_in_notebook, + show_diff_of_current_notebook, + INSERT_MODE +) + +# Create the toolkit +toolkit = Toolkit( + name="jupyterlab_commands_toolkit", + description="""A comprehensive toolkit for controlling JupyterLab interface and performing notebook operations through AI commands. + +This toolkit provides programmatic access to JupyterLab's core functionality, enabling AI assistants to: + +**Document Management:** +- Open files, notebooks, and documents with precise control over layout positioning +- Support for split-pane layouts (top, left, right, bottom) and tab management +- Open markdown files in rendered preview mode for better readability + +**Notebook Operations:** +- Clear all cell outputs in the active notebook for cleanup and sharing +- Display git diffs for notebooks using nbdime visualization +- Maintain notebook structure while performing operations + +**Layout Control:** +- Split current workspace into multiple panes +- Merge content with adjacent areas +- Create new tabs before or after current position +- Flexible positioning options for optimal workspace organization + +**Key Features:** +- Event-driven architecture using JupyterLab's command system +- Seamless integration with Jupyter AI for natural language control +- Support for relative file paths from server root directory +- Comprehensive error handling and user feedback +- Compatible with JupyterLab 4.0+ and modern Jupyter environments + +Use these tools to programmatically manage your JupyterLab workspace, organize documents, and perform common notebook operations through conversational AI interfaces.""" +) + +# Add tools to the toolkit +toolkit.add_tool(Tool(callable=open_document, read=True)) +toolkit.add_tool(Tool(callable=open_markdown_file_in_preview_mode, read=True)) +toolkit.add_tool(Tool(callable=clear_all_outputs_in_notebook, read=True)) +toolkit.add_tool(Tool(callable=show_diff_of_current_notebook, read=True)) diff --git a/jupyterlab_commands_toolkit/tools.py b/jupyterlab_commands_toolkit/tools.py index 1287947..1bb07a8 100644 --- a/jupyterlab_commands_toolkit/tools.py +++ b/jupyterlab_commands_toolkit/tools.py @@ -1,10 +1,5 @@ from typing import Literal, Optional from jupyter_server.serverapp import ServerApp -from jupyter_server_ai_tools.models import Tool -from jupyter_server.base.call_context import CallContext - -handler = CallContext.get(CallContext.JUPYTER_HANDLER) - def emit(data): @@ -20,7 +15,6 @@ def emit(data): INSERT_MODE = Literal['split-top', 'split-left', 'split-right', 'split-bottom', 'merge-top', 'merge-left', 'merge-right', 'merge-bottom', 'tab-before', 'tab-after'] - def open_document(relative_path: str, mode: Optional[INSERT_MODE] = None) -> None: """ Open a document in JupyterLab. @@ -188,15 +182,4 @@ def show_diff_of_current_notebook(run: bool) -> None: emit({ "name": "nbdime:diff-git", "args": {} - }) - - - -TOOLS = { - Tool(callable=open_document, read=True), - Tool(callable=open_markdown_file_in_preview_mode, read=True), - Tool(callable=clear_all_outputs_in_notebook, read=True), - Tool(callable=show_diff_of_current_notebook, read=True), -} - - + }) \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml index 6028578..b8083e1 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -24,7 +24,8 @@ classifiers = [ ] dependencies = [ "jupyter_server>=2.4.0,<3", - "jupyterlab-eventlistener" + "jupyterlab-eventlistener", + "jupyter-ai" ] dynamic = ["version", "description", "authors", "urls", "keywords"]