1- import json
21import asyncio
32import difflib
4-
3+ import json
4+ import re
55from typing import Any , Dict , Literal , Optional , Tuple
66
77import nbformat
88from jupyter_ai .tools .models import Tool , Toolkit
9- from pycrdt import Awareness , Doc , Text , Assoc
109from jupyter_ydoc import YNotebook
10+ from pycrdt import Assoc , Text
1111
12- from ..utils import cell_to_md , get_file_id , get_jupyter_ydoc , notebook_json_to_md , get_global_awareness , collaborative_tool
13- import re
12+ from ..utils import (
13+ cell_to_md ,
14+ get_file_id ,
15+ get_jupyter_ydoc ,
16+ notebook_json_to_md ,
17+ )
1418
1519
1620def _is_uuid_like (value : str ) -> bool :
@@ -69,7 +73,7 @@ async def read_notebook(file_path: str, include_outputs=False) -> str:
6973 notebook_dict = await read_notebook_json (file_path )
7074 notebook_md = notebook_json_to_md (notebook_dict , include_outputs = include_outputs )
7175 return notebook_md
72- except Exception as e :
76+ except Exception :
7377 raise
7478
7579
@@ -90,7 +94,7 @@ async def read_notebook_json(file_path: str) -> Dict[str, Any]:
9094 with open (file_path , "r" , encoding = "utf-8" ) as f :
9195 notebook_dict = json .load (f )
9296 return notebook_dict
93- except Exception as e :
97+ except Exception :
9498 raise
9599
96100
@@ -121,7 +125,7 @@ async def read_cell(file_path: str, cell_id: str, include_outputs: bool = True)
121125 cell , cell_index = await read_cell_json (file_path , resolved_cell_id )
122126 cell_md = cell_to_md (cell , cell_index )
123127 return cell_md
124- except Exception as e :
128+ except Exception :
125129 raise
126130
127131
@@ -157,7 +161,7 @@ async def read_cell_json(file_path: str, cell_id: str) -> Tuple[Dict[str, Any],
157161
158162 raise LookupError (f"No cell found with { cell_id = } " )
159163
160- except Exception as e :
164+ except Exception :
161165 raise
162166
163167
@@ -193,7 +197,7 @@ async def get_cell_id_from_index(file_path: str, cell_index: int) -> str:
193197
194198 return cell_id
195199
196- except Exception as e :
200+ except Exception :
197201 raise
198202
199203
@@ -269,7 +273,7 @@ async def add_cell(
269273 with open (file_path , "w" , encoding = "utf-8" ) as f :
270274 nbformat .write (notebook , f )
271275
272- except Exception as e :
276+ except Exception :
273277 raise
274278
275279
@@ -333,7 +337,7 @@ async def insert_cell(
333337 with open (file_path , "w" , encoding = "utf-8" ) as f :
334338 nbformat .write (notebook , f )
335339
336- except Exception as e :
340+ except Exception :
337341 raise
338342
339343
@@ -381,7 +385,7 @@ async def delete_cell(file_path: str, cell_id: str):
381385 if cell_index is None :
382386 raise ValueError (f"Could not find cell index for { cell_id = } " )
383387
384- except Exception as e :
388+ except Exception :
385389 raise
386390
387391
@@ -784,7 +788,7 @@ async def edit_cell(file_path: str, cell_id: str, content: str) -> None:
784788 else :
785789 raise ValueError (f"Cell with { cell_id = } not found in notebook at { file_path = } " )
786790
787- except Exception as e :
791+ except Exception :
788792 raise
789793
790794
@@ -903,99 +907,11 @@ def _determine_insert_index(cells_count: int, cell_index: Optional[int], add_abo
903907
904908
905909
906-
907- # async def set_persona_awareness(
908- # file_path: str,
909- # username: str,
910- # name: str,
911- # display_name: str,
912- # initials: str,
913- # avatar_url: str = "",
914- # color: str = "var(--jp-collaborator-color1)",
915- # mention_name: str = "",
916- # current: str = "",
917- # documents: list = None
918- # ) -> str:
919- # """Sets user awareness information in the notebook's YDoc.
920-
921- # This function sets both the local and global "user" field in the notebook's
922- # awareness state with the provided user information based on the Jupyter Server user model.
923-
924- # Args:
925- # file_path: The relative path to the notebook file on the filesystem.
926- # username: The username of the user
927- # name: The full name of the user
928- # display_name: The display name for the user
929- # initials: User's initials for avatar display
930- # avatar_url: URL to the user's avatar image (optional)
931- # color: CSS color variable for user identification (optional)
932- # mention_name: The mention name for @-mentions (optional, defaults to @username)
933- # current: Current context/status string (optional)
934- # documents: List of documents the user is working with (optional)
935-
936- # Returns:
937- # Success message or error message
938- # """
939- # try:
940- # print(f"DEBUG: set_user_awareness called with file_path='{file_path}', username='{username}'")
941-
942- # file_id = await get_file_id(file_path)
943- # ydoc = await get_jupyter_ydoc(file_id)
944- # global_awareness = await get_global_awareness()
945-
946- # if not ydoc:
947- # return f"Error: Could not access notebook document for {file_path}. Notebook may not be open."
948-
949- # # Set default mention_name if not provided
950- # if not mention_name:
951- # mention_name = f"@{username}"
952-
953- # # Set default documents list if not provided
954- # if documents is None:
955- # documents = [file_path]
956-
957- # # Create user model based on Jupyter Server user model
958- # user_model = {
959- # "username": username,
960- # "name": name,
961- # "display_name": display_name,
962- # "initials": initials,
963- # "avatar_url": avatar_url,
964- # "color": color,
965- # "mention_name": mention_name
966- # }
967-
968- # print(f"DEBUG: set_user_awareness setting user awareness with model: {user_model}")
969-
970- # # Set the local user field in the notebook's awareness
971- # ydoc.awareness.set_local_state_field("user", user_model)
972-
973- # # Create global awareness state with user, current, and documents fields
974- # global_state = {
975- # "user": user_model,
976- # "current": current,
977- # "documents": documents
978- # }
979-
980- # print(f"DEBUG: set_user_awareness setting global awareness state: {global_state}")
981-
982- # # Set the global awareness state
983- # global_awareness.set_local_state(global_state)
984-
985- # print("DEBUG: set_user_awareness completed successfully")
986- # return f"Successfully set user awareness for {display_name} ({username}) in notebook {file_path} with current='{current}' and {len(documents)} documents"
987-
988- # except Exception as e:
989- # print(f"ERROR: set_user_awareness failed for {file_path}, username={username}: {str(e)}")
990- # return f"Error setting user awareness: {str(e)}"
991-
992-
993910toolkit = Toolkit (
994911 name = "notebook_toolkit" ,
995912 description = "Tools for reading and manipulating Jupyter notebooks." ,
996913)
997914toolkit .add_tool (Tool (callable = read_notebook , read = True ))
998- # toolkit.add_tool(Tool(callable=write_to_cell, read=True, write=True))
999915toolkit .add_tool (Tool (callable = read_cell , read = True ))
1000916toolkit .add_tool (Tool (callable = add_cell , read = True , write = True ))
1001917toolkit .add_tool (Tool (callable = insert_cell , read = True , write = True ))
0 commit comments