Skip to content

Commit a0208c3

Browse files
committed
WIP: Toolkits for Jupyter
1 parent 89ca3af commit a0208c3

File tree

10 files changed

+891
-225
lines changed

10 files changed

+891
-225
lines changed

jupyter_ai_tools/__init__.py

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from .extension import get_git_tools, get_notebook_tools
1+
from .tools import get_git_tools, get_notebook_tools
22
from jupyter_server_ai_tools.models import ToolSet, Toolkit
33

44
__version__ = "0.1.2"
@@ -7,23 +7,5 @@
77
def _jupyter_server_extension_points():
88
return [{"module": "jupyter_ai_tools"}]
99

10-
1110
def _load_jupyter_server_extension(serverapp):
12-
serverapp.log.info("✅ jupyter_ai_tools extension loaded.")
13-
14-
async def _start_jupyter_server_extension(serverapp):
15-
registry = serverapp.web_app.settings["toolkit_registry"]
16-
if registry:
17-
notebook_tools = ToolSet(get_notebook_tools())
18-
registry.register_toolkit(
19-
Toolkit(
20-
name="notebook_toolkit", tools=notebook_tools
21-
)
22-
)
23-
24-
git_tools = ToolSet(get_git_tools())
25-
registry.register_toolkit(
26-
Toolkit(
27-
name="git_toolkit", tools=git_tools
28-
)
29-
)
11+
serverapp.log.info("✅ jupyter_ai_tools extension loaded.")

jupyter_ai_tools/extension.py

Lines changed: 0 additions & 26 deletions
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""Toolkits for Jupyter"""
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
"""Tools that provide code execution features"""
2+
3+
4+
import asyncio
5+
import shlex
6+
from typing import Optional
7+
8+
from jupyter_ai.tools.models import Tool, Toolkit
9+
10+
11+
async def bash(command: str, timeout: Optional[int] = None) -> str:
12+
"""Executes a bash command and returns the result
13+
14+
Args:
15+
command: The bash command to execute
16+
timeout: Optional timeout in seconds
17+
18+
Returns:
19+
The command output (stdout and stderr combined)
20+
"""
21+
22+
proc = await asyncio.create_subprocess_exec(
23+
*shlex.split(command),
24+
stdout=asyncio.subprocess.PIPE,
25+
stderr=asyncio.subprocess.PIPE,
26+
)
27+
28+
try:
29+
stdout, stderr = await asyncio.wait_for(proc.communicate(), timeout)
30+
output = stdout.decode("utf-8")
31+
error = stderr.decode("utf-8")
32+
33+
if proc.returncode != 0:
34+
if error:
35+
return f"Error: {error}"
36+
return f"Command failed with exit code {proc.returncode}"
37+
38+
return output if output else "Command executed successfully with no output."
39+
except asyncio.TimeoutError:
40+
proc.kill()
41+
return f"Command timed out after {timeout} seconds"
42+
43+
44+
toolkit = Toolkit(
45+
name="code_execution_toolkit",
46+
description="Tools to execute code in different environments.",
47+
)
48+
toolkit.add(Tool(callable=bash, execute=True))
49+

0 commit comments

Comments
 (0)