Skip to content

Commit 950ed4b

Browse files
committed
Working version for file_system toolkit
1 parent 288c291 commit 950ed4b

File tree

5 files changed

+37
-41
lines changed

5 files changed

+37
-41
lines changed

jupyter_ai_tools/__init__.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
from .tools import get_git_tools, get_notebook_tools
2-
from jupyter_server_ai_tools.models import ToolSet, Toolkit
3-
41
__version__ = "0.1.2"
52

63

jupyter_ai_tools/toolkits/code_execution.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,5 @@ async def bash(command: str, timeout: Optional[int] = None) -> str:
4545
name="code_execution_toolkit",
4646
description="Tools to execute code in different environments.",
4747
)
48-
toolkit.add(Tool(callable=bash, execute=True))
48+
toolkit.add_tool(Tool(callable=bash, execute=True))
4949

jupyter_ai_tools/toolkits/file_system.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from jupyter_ai.tools.models import Tool, Toolkit
1010

1111

12-
async def read(file_path: str, offset: Optional[int] = None, limit: Optional[int] = None) -> str:
12+
def read(file_path: str, offset: Optional[int] = None, limit: Optional[int] = None) -> str:
1313
"""Reads a file from the local filesystem
1414
1515
Args:
@@ -27,31 +27,31 @@ async def read(file_path: str, offset: Optional[int] = None, limit: Optional[int
2727
if not os.path.isfile(file_path):
2828
return f"Error: Not a file: {file_path}"
2929

30-
content = await _read_file_content(file_path, offset, limit)
30+
content = _read_file_content(file_path, offset, limit)
3131
return content
3232
except Exception as e:
3333
return f"Error: Failed to read file: {str(e)}"
3434

3535

36-
async def _read_file_content(
36+
def _read_file_content(
3737
file_path: str, offset: Optional[int] = None, limit: Optional[int] = None
3838
) -> str:
3939
"""Helper function to read file content in a separate thread"""
4040
with open(file_path, "r", encoding="utf-8") as f:
4141
if offset is not None:
4242
# Skip lines until we reach the offset
4343
for _ in range(offset):
44-
line = await f.readline()
44+
line = f.readline()
4545
if not line:
4646
break
4747

4848
# Read the specified number of lines or all lines if limit is None
4949
if limit is not None:
50-
lines = [await f.readline() for _ in range(limit)]
50+
lines = [f.readline() for _ in range(limit)]
5151
# Filter out None values in case we hit EOF
5252
lines = [line for line in lines if line]
5353
else:
54-
lines = await f.readlines()
54+
lines = f.readlines()
5555

5656
# Add line numbers (starting from offset+1 if offset is provided)
5757
start_line = (offset or 0) + 1
@@ -60,7 +60,6 @@ async def _read_file_content(
6060
return "".join(numbered_lines)
6161

6262

63-
# Question: Should this be async?
6463
def write(file_path: str, content: str) -> str:
6564
"""Writes content to a file on the local filesystem
6665
@@ -89,7 +88,7 @@ def _write_file_content(file_path: str, content: str) -> None:
8988
f.write(content)
9089

9190

92-
async def edit(file_path: str, old_string: str, new_string: str, replace_all: bool = False) -> str:
91+
def edit(file_path: str, old_string: str, new_string: str, replace_all: bool = False) -> str:
9392
"""Performs string replacement in a file
9493
9594
Args:
@@ -114,7 +113,7 @@ async def edit(file_path: str, old_string: str, new_string: str, replace_all: bo
114113

115114
# Check if old_string exists in the file
116115
if old_string not in content:
117-
return f"Error: String to replace not found in file"
116+
return "Error: String to replace not found in file"
118117

119118
# Perform the replacement
120119
if replace_all:
@@ -209,12 +208,13 @@ async def glob(pattern: str, path: Optional[str] = None) -> List[str]:
209208
matching_files = await asyncio.to_thread(_glob_search, search_path, pattern)
210209

211210
if not matching_files:
212-
return []
211+
return "No matching files found"
213212

214213
# Sort files by modification time (most recent first)
215214
matching_files.sort(key=lambda f: os.path.getmtime(f), reverse=True)
216-
217-
return matching_files
215+
matching_files = [str(f) for f in matching_files]
216+
217+
return "\n".join(matching_files)
218218
except Exception as e:
219219
return [f"Error: Failed to perform glob search: {str(e)}"]
220220

@@ -284,7 +284,7 @@ async def grep(
284284
return [f"Error: Failed to perform grep search: {str(e)}"]
285285

286286

287-
async def ls(path: str, ignore: Optional[List[str]] = None) -> List[str]:
287+
async def ls(path: str, ignore: Optional[List[str]] = None) -> str:
288288
"""Lists files and directories in a given path
289289
290290
Args:
@@ -327,7 +327,7 @@ async def ls(path: str, ignore: Optional[List[str]] = None) -> List[str]:
327327
# Sort by type (directories first) and then by name
328328
full_paths.sort(key=lambda p: (0 if os.path.isdir(p) else 1, p.lower()))
329329

330-
return full_paths
330+
return "\n".join(full_paths)
331331
except Exception as e:
332332
return [f"Error: Failed to list directory: {str(e)}"]
333333

@@ -336,11 +336,11 @@ async def ls(path: str, ignore: Optional[List[str]] = None) -> List[str]:
336336
name="file_system_toolkit",
337337
description="Tools to do search, list, read, write and edit operations on files.",
338338
)
339-
toolkit.add(Tool(callable=read, read=True))
340-
toolkit.add(Tool(callable=write, write=True))
341-
toolkit.add(Tool(callable=edit, read=True, write=True))
342-
toolkit.add(Tool(callable=search_and_replace, read=True, write=True))
343-
toolkit.add(Tool(callable=glob, read=True))
344-
toolkit.add(Tool(callable=grep, read=True))
345-
toolkit.add(Tool(callable=ls, read=True))
339+
toolkit.add_tool(Tool(callable=read, read=True))
340+
toolkit.add_tool(Tool(callable=edit, read=True, write=True))
341+
toolkit.add_tool(Tool(callable=write, write=True))
342+
toolkit.add_tool(Tool(callable=search_and_replace, read=True, write=True))
343+
toolkit.add_tool(Tool(callable=glob, read=True))
344+
toolkit.add_tool(Tool(callable=grep, read=True))
345+
toolkit.add_tool(Tool(callable=ls, read=True))
346346

jupyter_ai_tools/toolkits/git.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import json
22
import os
33

4-
from jupyterlab_git.git import Git
54
from jupyter_ai.tools.models import Tool, Toolkit
5+
from jupyterlab_git.git import Git
66

77
git = Git()
88

@@ -169,12 +169,11 @@ async def git_get_repo_root(path: str) -> str:
169169
name="git_toolkit",
170170
description="Tools for working with Git repositories.",
171171
)
172-
toolkit.add(Tool(callable=git_clone, execute=True))
173-
toolkit.add(Tool(callable=git_status, read=True))
174-
toolkit.add(Tool(callable=git_log, read=True))
175-
toolkit.add(Tool(callable=git_pull, execute=True))
176-
toolkit.add(Tool(callable=git_push, execute=True))
177-
toolkit.add(Tool(callable=git_commit, execute=True))
178-
toolkit.add(Tool(callable=git_add, execute=True))
179-
toolkit.add(Tool(callable=git_get_repo_root, read=True))
180-
172+
toolkit.add_tool(Tool(callable=git_clone, execute=True))
173+
toolkit.add_tool(Tool(callable=git_status, read=True))
174+
toolkit.add_tool(Tool(callable=git_log, read=True))
175+
toolkit.add_tool(Tool(callable=git_pull, execute=True))
176+
toolkit.add_tool(Tool(callable=git_push, execute=True))
177+
toolkit.add_tool(Tool(callable=git_commit, execute=True))
178+
toolkit.add_tool(Tool(callable=git_add, execute=True))
179+
toolkit.add_tool(Tool(callable=git_get_repo_root, read=True))

jupyter_ai_tools/toolkits/notebook.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
from typing import Any, Dict, Literal, Tuple
33

44
import nbformat
5-
65
from jupyter_ai.tools.models import Tool, Toolkit
6+
77
from ..utils import cell_to_md, get_file_id, get_jupyter_ydoc, notebook_json_to_md
88

99

@@ -237,8 +237,8 @@ def _determine_insert_index(cells_count: int, cell_index: int, add_above: bool)
237237
name="notebook_toolkit",
238238
description="Tools for reading and manipulating Jupyter notebooks.",
239239
)
240-
toolkit.add(Tool(callable=read_notebook, read=True))
241-
toolkit.add(Tool(callable=read_cell, read=True))
242-
toolkit.add(Tool(callable=add_cell, write=True))
243-
toolkit.add(Tool(callable=delete_cell, delete=True))
244-
toolkit.add(Tool(callable=edit_cell, read=True, write=True))
240+
toolkit.add_tool(Tool(callable=read_notebook, read=True))
241+
toolkit.add_tool(Tool(callable=read_cell, read=True))
242+
toolkit.add_tool(Tool(callable=add_cell, write=True))
243+
toolkit.add_tool(Tool(callable=delete_cell, delete=True))
244+
toolkit.add_tool(Tool(callable=edit_cell, read=True, write=True))

0 commit comments

Comments
 (0)