Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions jupyter_ai_tools/toolkits/file_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

from jupyter_ai.tools.models import Tool, Toolkit

from ..utils import normalize_filepath


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

Expand Down Expand Up @@ -73,6 +76,7 @@ def write(file_path: str, content: str) -> str:
A success message or error message
"""
try:
file_path = normalize_filepath(file_path)
# Ensure the directory exists
directory = os.path.dirname(file_path)
if directory and not os.path.exists(directory):
Expand Down Expand Up @@ -107,6 +111,7 @@ def edit(file_path: str, old_string: str, new_string: str, replace_all: bool = F
A success message or error message
"""
try:
file_path = normalize_filepath(file_path)
if not os.path.exists(file_path):
return f"Error: File not found: {file_path}"

Expand Down Expand Up @@ -159,6 +164,7 @@ async def search_and_replace(
A success message or error message
"""
try:
file_path = normalize_filepath(file_path)
if not os.path.exists(file_path):
return f"Error: File not found: {file_path}"

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

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

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

Expand Down
10 changes: 10 additions & 0 deletions jupyter_ai_tools/toolkits/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from jupyter_ai.tools.models import Tool, Toolkit
from jupyterlab_git.git import Git

from ..utils import normalize_filepath

git = Git()


Expand All @@ -19,6 +21,7 @@ async def git_clone(path: str, url: str) -> str:
Returns:
str: Success or error message.
"""
path = normalize_filepath(path)
res = await git.clone(path, repo_url=url)
if res["code"] == 0:
return f"✅ Cloned repo into {res['path']}"
Expand All @@ -36,6 +39,7 @@ async def git_status(path: str) -> str:
Returns:
str: A JSON-formatted string of status or an error message.
"""
path = normalize_filepath(path)
res = await git.status(path)
if res["code"] == 0:
return f"📋 Status:\n{json.dumps(res, indent=2)}"
Expand All @@ -54,6 +58,7 @@ async def git_log(path: str, history_count: int = 10) -> str:
Returns:
str: A JSON-formatted commit log or error message.
"""
path = normalize_filepath(path)
res = await git.log(path, history_count=history_count)
if res["code"] == 0:
return f"🕓 Recent commits:\n{json.dumps(res, indent=2)}"
Expand All @@ -71,6 +76,7 @@ async def git_pull(path: str) -> str:
Returns:
str: Success or error message.
"""
path = normalize_filepath(path)
res = await git.pull(path)
return (
"✅ Pulled latest changes."
Expand All @@ -91,6 +97,7 @@ async def git_push(path: str, branch: str) -> str:
Returns:
str: Success or error message.
"""
path = normalize_filepath(path)
res = await git.push(remote="origin", branch=branch, path=path)
return (
"✅ Pushed changes."
Expand All @@ -111,6 +118,7 @@ async def git_commit(path: str, message: str) -> str:
Returns:
str: Success or error message.
"""
path = normalize_filepath(path)
res = await git.commit(commit_msg=message, amend=False, path=path)
return (
"✅ Commit successful."
Expand All @@ -132,6 +140,7 @@ async def git_add(path: str, add_all: bool = True, filename: str = "") -> str:
Returns:
str: Success or error message.
"""
path = normalize_filepath(path)
if add_all:
res = await git.add_all(path)
elif filename:
Expand All @@ -158,6 +167,7 @@ async def git_get_repo_root(path: str) -> str:
Returns:
str: The path to the Git repository root or an error message.
"""
path = normalize_filepath(path)
dir_path = os.path.dirname(path)
res = await git.show_top_level(dir_path)
if res["code"] == 0 and res.get("path"):
Expand Down
Loading