|
1 | 1 | """JupyterLab Commands toolkit for Jupyter AI""" |
2 | | -from jupyter_ai.tools.models import Tool, Toolkit |
3 | | - |
4 | | -from typing import Optional |
5 | | -from .tools import ( |
6 | | - open_document, |
7 | | - open_markdown_file_in_preview_mode, |
8 | | - clear_all_outputs_in_notebook, |
9 | | - show_diff_of_current_notebook, |
10 | | - INSERT_MODE |
11 | | -) |
12 | | - |
13 | | -# Create the toolkit |
14 | | -toolkit = Toolkit( |
15 | | - name="jupyterlab_commands_toolkit", |
16 | | - description="""A comprehensive toolkit for controlling JupyterLab interface and performing notebook operations through AI commands. |
17 | | -
|
18 | | -This toolkit provides programmatic access to JupyterLab's core functionality, enabling AI assistants to: |
19 | | -
|
20 | | -**Document Management:** |
21 | | -- Open files, notebooks, and documents with precise control over layout positioning |
22 | | -- Support for split-pane layouts (top, left, right, bottom) and tab management |
23 | | -- Open markdown files in rendered preview mode for better readability |
24 | | -
|
25 | | -**Notebook Operations:** |
26 | | -- Clear all cell outputs in the active notebook for cleanup and sharing |
27 | | -- Display git diffs for notebooks using nbdime visualization |
28 | | -- Maintain notebook structure while performing operations |
29 | | -
|
30 | | -**Layout Control:** |
31 | | -- Split current workspace into multiple panes |
32 | | -- Merge content with adjacent areas |
33 | | -- Create new tabs before or after current position |
34 | | -- Flexible positioning options for optimal workspace organization |
35 | | -
|
36 | | -**Key Features:** |
37 | | -- Event-driven architecture using JupyterLab's command system |
38 | | -- Seamless integration with Jupyter AI for natural language control |
39 | | -- Support for relative file paths from server root directory |
40 | | -- Comprehensive error handling and user feedback |
41 | | -- Compatible with JupyterLab 4.0+ and modern Jupyter environments |
42 | | -
|
43 | | -Use these tools to programmatically manage your JupyterLab workspace, organize documents, and perform common notebook operations through conversational AI interfaces.""" |
44 | | -) |
45 | | - |
46 | | -# Add tools to the toolkit |
47 | | -toolkit.add_tool(Tool(callable=open_document, read=True)) |
48 | | -toolkit.add_tool(Tool(callable=open_markdown_file_in_preview_mode, read=True)) |
49 | | -toolkit.add_tool(Tool(callable=clear_all_outputs_in_notebook, read=True)) |
50 | | -toolkit.add_tool(Tool(callable=show_diff_of_current_notebook, read=True)) |
| 2 | + |
| 3 | +try: |
| 4 | + from jupyter_ai.tools.models import Tool, Toolkit |
| 5 | + |
| 6 | + from typing import Optional |
| 7 | + from .tools import ( |
| 8 | + open_document, |
| 9 | + open_markdown_file_in_preview_mode, |
| 10 | + clear_all_outputs_in_notebook, |
| 11 | + show_diff_of_current_notebook, |
| 12 | + INSERT_MODE, |
| 13 | + ) |
| 14 | + |
| 15 | + # Create the toolkit |
| 16 | + toolkit = Toolkit( |
| 17 | + name="jupyterlab_commands_toolkit", |
| 18 | + description="""A comprehensive toolkit for controlling JupyterLab interface and performing notebook operations through AI commands. |
| 19 | +
|
| 20 | + This toolkit provides programmatic access to JupyterLab's core functionality, enabling AI assistants to: |
| 21 | +
|
| 22 | + **Document Management:** |
| 23 | + - Open files, notebooks, and documents with precise control over layout positioning |
| 24 | + - Support for split-pane layouts (top, left, right, bottom) and tab management |
| 25 | + - Open markdown files in rendered preview mode for better readability |
| 26 | +
|
| 27 | + **Notebook Operations:** |
| 28 | + - Clear all cell outputs in the active notebook for cleanup and sharing |
| 29 | + - Display git diffs for notebooks using nbdime visualization |
| 30 | + - Maintain notebook structure while performing operations |
| 31 | +
|
| 32 | + **Layout Control:** |
| 33 | + - Split current workspace into multiple panes |
| 34 | + - Merge content with adjacent areas |
| 35 | + - Create new tabs before or after current position |
| 36 | + - Flexible positioning options for optimal workspace organization |
| 37 | +
|
| 38 | + **Key Features:** |
| 39 | + - Event-driven architecture using JupyterLab's command system |
| 40 | + - Seamless integration with Jupyter AI for natural language control |
| 41 | + - Support for relative file paths from server root directory |
| 42 | + - Comprehensive error handling and user feedback |
| 43 | + - Compatible with JupyterLab 4.0+ and modern Jupyter environments |
| 44 | +
|
| 45 | + Use these tools to programmatically manage your JupyterLab workspace, organize documents, and perform common notebook operations through conversational AI interfaces.""", |
| 46 | + ) |
| 47 | + |
| 48 | + # Add tools to the toolkit |
| 49 | + toolkit.add_tool(Tool(callable=open_document, read=True)) |
| 50 | + toolkit.add_tool(Tool(callable=open_markdown_file_in_preview_mode, read=True)) |
| 51 | + toolkit.add_tool(Tool(callable=clear_all_outputs_in_notebook, read=True)) |
| 52 | + toolkit.add_tool(Tool(callable=show_diff_of_current_notebook, read=True)) |
| 53 | +except ImportError: |
| 54 | + # If jupyter-ai is not available, the AI toolkit won't be available |
| 55 | + toolkit = None |
0 commit comments