Skip to content

Commit 2ec15b6

Browse files
authored
Remove slash command listener, make PersonaManagers accessible (#2)
* remove /refresh-persona handler * make persona managers accessible to other extensions * change path of persona managers dict in settings dict * rename _refresh_personas() to refresh_personas()
1 parent cc06288 commit 2ec15b6

File tree

2 files changed

+20
-40
lines changed

2 files changed

+20
-40
lines changed

jupyter_ai_persona_manager/extension.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,20 @@ class PersonaManagerExtension(ExtensionApp):
4343
def initialize(self, argv: Any = None) -> None:
4444
super().initialize()
4545

46-
self.persona_managers_by_room: dict[str, PersonaManager] = {}
47-
"""Cache of PersonaManager instances, indexed by room ID."""
46+
@property
47+
def persona_managers_by_room(self) -> dict[str, PersonaManager]:
48+
"""
49+
Dictionary of PersonaManager instances indexed by room ID.
50+
51+
This is accessible to other extensions via
52+
`self.settings['jupyter-ai']['persona-managers']`.
53+
"""
54+
if 'jupyter-ai' not in self.settings:
55+
self.settings['jupyter-ai'] = {}
56+
if 'persona-managers' not in self.settings['jupyter-ai']:
57+
self.settings['jupyter-ai']['persona-managers'] = {}
58+
59+
return self.settings['jupyter-ai']['persona-managers']
4860

4961
@property
5062
def event_loop(self) -> AbstractEventLoop:
@@ -114,7 +126,6 @@ def _on_router_chat_init(self, room_id: str, ychat: "YChat") -> None:
114126

115127
# Register persona manager callbacks with router
116128
self.router.observe_chat_msg(room_id, persona_manager.on_chat_message)
117-
self.router.observe_slash_cmd_msg(room_id, persona_manager.on_slash_cmd_message)
118129

119130
def _init_persona_manager(
120131
self, room_id: str, ychat: "YChat"

jupyter_ai_persona_manager/persona_manager.py

Lines changed: 6 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -416,46 +416,15 @@ def _broadcast(
416416
self.event_loop.create_task(persona.process_message(message))
417417
return
418418

419-
def on_slash_cmd_message(self, room_id: str, message: Message):
419+
async def refresh_personas(self):
420420
"""
421-
Routes & handles a message containing a slash command. Returns `True` if
422-
the message specified a valid slash command recognized by
423-
`PersonaManager`, `False` otherwise. Notes:
421+
Method that reloads all persona classes defined locally under
422+
`.jupyter/personas`, and re-initializes each persona class available in
423+
the current chat.
424424
425-
- Each message may have exactly one slash command, which must be
426-
specified by the first word of the message.
427-
428-
- This method will return `True` even if the command was not handled
429-
successfully. `False` is just meant to indicate that the control flow
430-
should return back to `route_message()`. This allows AI personas to
431-
receive custom slash commands that only they recognize.
432-
"""
433-
first_word = get_first_word(message.body)
434-
assert first_word and first_word.startswith("/")
435-
436-
command_id = first_word[1:]
437-
if command_id == "refresh-personas":
438-
self.handle_refresh_personas_command(message)
439-
return True
440-
441-
# If command is unrecognized, log an error
442-
self.log.warning(f"Unrecognized slash command: '/{command_id}'")
443-
return False
444-
445-
def handle_refresh_personas_command(self, _: Message) -> None:
425+
This method is public because it is called by `jupyter_ai_chat_commands`
426+
when the `/refresh-personas` slash command is sent.
446427
"""
447-
Handles the '/refresh-personas' slash command.
448-
449-
TODO: How do we show status/completion in the UI?
450-
"""
451-
self.log.info(
452-
f"Received '/refresh-personas'. Refreshing personas in chat '{self.room_id}'..."
453-
)
454-
455-
# Refresh personas in background task
456-
asyncio.create_task(self._refresh_personas())
457-
458-
async def _refresh_personas(self):
459428
# Shutdown all personas
460429
await self.shutdown_personas()
461430

0 commit comments

Comments
 (0)