Skip to content

Conversation

@3coins
Copy link
Contributor

@3coins 3coins commented Oct 1, 2025

This extension provides the foundational message routing functionality for Jupyter AI. It automatically detects new chat sessions and routes messages to registered callbacks based on message type (slash commands vs regular messages). Extensions can register callbacks to handle specific chat events without needing to manage chat lifecycle directly.

Usage

Basic MessageRouter Setup

# The router is available in other extensions via settings
router = self.serverapp.web_app.settings.get("jupyter-ai", {}).get("router")

# Register callbacks for different event types
def on_new_chat(room_id: str, ychat: YChat):
    print(f"New chat connected: {room_id}")

def on_slash_command(room_id: str, message: Message):
    print(f"Slash command in {room_id}: {message.body}")

def on_regular_message(room_id: str, message: Message):`
    print(f"Regular message in {room_id}: {message.body}")

# Register the callbacks
router.observe_chat_init(on_new_chat)
router.observe_slash_cmd_msg("room-id", on_slash_command)
router.observe_chat_msg("room-id", on_regular_message)

Message Flow

  1. Router detects new chats - Automatically listens for chat room initialization events
  2. Router connects chats - Establishes observers on YChat message streams
  3. Router routes messages - Calls appropriate callbacks based on message type (slash vs regular)
  4. Extensions respond - Your callbacks receive room_id and message data

Available Methods

  • observe_chat_init(callback) - Called when new chat sessions are initialized with (room_id, ychat)
  • observe_slash_cmd_msg(room_id, callback) - Called for messages starting with / in a specific room
  • observe_chat_msg(room_id, callback) - Called for regular (non-slash) messages in a specific room

@3coins 3coins added the enhancement New feature or request label Oct 2, 2025
@3coins 3coins changed the title WIP: Message router Core message routing layer for Jupyter AI Oct 2, 2025
@3coins 3coins marked this pull request as ready for review October 3, 2025 01:37
@3coins 3coins merged commit fcdd104 into jupyter-ai-contrib:main Oct 3, 2025
4 of 7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant