Skip to content

Commit 87e75d2

Browse files
authored
Merge pull request #10 from Zsailer/more-tools-2
Add create_notebook function to notebook toolkit
2 parents 6b5f6cf + 07e9f54 commit 87e75d2

File tree

7 files changed

+698
-378
lines changed

7 files changed

+698
-378
lines changed

jupyter_ai_tools/toolkits/file_system.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
from jupyter_ai.tools.models import Tool, Toolkit
1010

11+
from ..utils import normalize_filepath
12+
1113

1214
def read(file_path: str, offset: Optional[int] = None, limit: Optional[int] = None) -> str:
1315
"""Reads a file from the local filesystem
@@ -21,6 +23,7 @@ def read(file_path: str, offset: Optional[int] = None, limit: Optional[int] = No
2123
The contents of the file, potentially with line numbers
2224
"""
2325
try:
26+
file_path = normalize_filepath(file_path)
2427
if not os.path.exists(file_path):
2528
return f"Error: File not found: {file_path}"
2629

@@ -73,6 +76,7 @@ def write(file_path: str, content: str) -> str:
7376
A success message or error message
7477
"""
7578
try:
79+
file_path = normalize_filepath(file_path)
7680
# Ensure the directory exists
7781
directory = os.path.dirname(file_path)
7882
if directory and not os.path.exists(directory):
@@ -107,6 +111,7 @@ def edit(file_path: str, old_string: str, new_string: str, replace_all: bool = F
107111
A success message or error message
108112
"""
109113
try:
114+
file_path = normalize_filepath(file_path)
110115
if not os.path.exists(file_path):
111116
return f"Error: File not found: {file_path}"
112117

@@ -159,6 +164,7 @@ async def search_and_replace(
159164
A success message or error message
160165
"""
161166
try:
167+
file_path = normalize_filepath(file_path)
162168
if not os.path.exists(file_path):
163169
return f"Error: File not found: {file_path}"
164170

@@ -212,7 +218,7 @@ async def glob(pattern: str, path: Optional[str] = None) -> str:
212218
A list of matching file paths sorted by modification time
213219
"""
214220
try:
215-
search_path = path or os.getcwd()
221+
search_path = normalize_filepath(path) if path else os.getcwd()
216222
if not os.path.exists(search_path):
217223
return f"Error: Path not found: {search_path}"
218224

@@ -260,7 +266,7 @@ async def grep(
260266
A list of file paths with at least one match
261267
"""
262268
try:
263-
search_path = path or os.getcwd()
269+
search_path = normalize_filepath(path) if path else os.getcwd()
264270
if not os.path.exists(search_path):
265271
return [f"Error: Path not found: {search_path}"]
266272

@@ -312,6 +318,7 @@ async def ls(path: str, ignore: Optional[List[str]] = None) -> str:
312318
A list of files and directories in the given path
313319
"""
314320
try:
321+
path = normalize_filepath(path)
315322
if not os.path.exists(path):
316323
return f"Error: Path not found: {path}"
317324

jupyter_ai_tools/toolkits/git.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
from jupyter_ai.tools.models import Tool, Toolkit
55
from jupyterlab_git.git import Git
66

7+
from ..utils import normalize_filepath
8+
79
git = Git()
810

911

@@ -19,6 +21,7 @@ async def git_clone(path: str, url: str) -> str:
1921
Returns:
2022
str: Success or error message.
2123
"""
24+
path = normalize_filepath(path)
2225
res = await git.clone(path, repo_url=url)
2326
if res["code"] == 0:
2427
return f"✅ Cloned repo into {res['path']}"
@@ -36,6 +39,7 @@ async def git_status(path: str) -> str:
3639
Returns:
3740
str: A JSON-formatted string of status or an error message.
3841
"""
42+
path = normalize_filepath(path)
3943
res = await git.status(path)
4044
if res["code"] == 0:
4145
return f"📋 Status:\n{json.dumps(res, indent=2)}"
@@ -54,6 +58,7 @@ async def git_log(path: str, history_count: int = 10) -> str:
5458
Returns:
5559
str: A JSON-formatted commit log or error message.
5660
"""
61+
path = normalize_filepath(path)
5762
res = await git.log(path, history_count=history_count)
5863
if res["code"] == 0:
5964
return f"🕓 Recent commits:\n{json.dumps(res, indent=2)}"
@@ -71,6 +76,7 @@ async def git_pull(path: str) -> str:
7176
Returns:
7277
str: Success or error message.
7378
"""
79+
path = normalize_filepath(path)
7480
res = await git.pull(path)
7581
return (
7682
"✅ Pulled latest changes."
@@ -91,6 +97,7 @@ async def git_push(path: str, branch: str) -> str:
9197
Returns:
9298
str: Success or error message.
9399
"""
100+
path = normalize_filepath(path)
94101
res = await git.push(remote="origin", branch=branch, path=path)
95102
return (
96103
"✅ Pushed changes."
@@ -111,6 +118,7 @@ async def git_commit(path: str, message: str) -> str:
111118
Returns:
112119
str: Success or error message.
113120
"""
121+
path = normalize_filepath(path)
114122
res = await git.commit(commit_msg=message, amend=False, path=path)
115123
return (
116124
"✅ Commit successful."
@@ -132,6 +140,7 @@ async def git_add(path: str, add_all: bool = True, filename: str = "") -> str:
132140
Returns:
133141
str: Success or error message.
134142
"""
143+
path = normalize_filepath(path)
135144
if add_all:
136145
res = await git.add_all(path)
137146
elif filename:
@@ -158,6 +167,7 @@ async def git_get_repo_root(path: str) -> str:
158167
Returns:
159168
str: The path to the Git repository root or an error message.
160169
"""
170+
path = normalize_filepath(path)
161171
dir_path = os.path.dirname(path)
162172
res = await git.show_top_level(dir_path)
163173
if res["code"] == 0 and res.get("path"):

0 commit comments

Comments
 (0)