Skip to content

Conversation

@3coins
Copy link
Contributor

@3coins 3coins commented Oct 15, 2025

Summary

Enhances the MessageRouter to support command-specific slash command observers with flexible pattern matching. This allows observers to register for specific commands rather than receiving all slash commands.

Key Changes

  • Updated observe_slash_cmd_msg() to accept a command_pattern parameter for filtering specific commands
  • Supports exact matches ("help"), wildcards ("ai-*"), and regex patterns ("export-(json|csv)")
  • Automatically strips the command from message body and passes clean command name to callbacks
  • Updated callback signature to (room_id: str, command: str, message: Message) where command is the clean command name and message.body contains only the arguments

New Features

  1. Command Filtering: Observers only receive messages for commands matching their registered patterns
  2. Flexible Matching: Three pattern types supported for different use cases
  3. Clean API: Commands passed without leading slash, message bodies contain only arguments
  4. Metadata Preservation: All original message metadata is preserved in the trimmed message

Example Usage

# Exact command match
router.observe_slash_cmd_msg("room1", "help", handle_help)
# Wildcard pattern for command families  
router.observe_slash_cmd_msg("room1", "ai-*", handle_ai_commands)
# Regex pattern for specific variants
router.observe_slash_cmd_msg("room1", r"export-(json|csv)", handle_exports)

Breaking Changes

The observe_slash_cmd_msg() method signature has changed from (room_id, callback) to (room_id, command_pattern, callback) and callback signature changed from (room_id, message) to (room_id, command, message).

Fixes #7

@3coins 3coins added the enhancement New feature or request label Oct 15, 2025
@3coins 3coins marked this pull request as ready for review October 15, 2025 21:35
@3coins
Copy link
Contributor Author

3coins commented Oct 15, 2025

cc @Zsailer @jtpio

Copy link
Contributor

@dlqqq dlqqq left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@3coins Thank you Piyush! The code looks good, appreciate the unit tests that verify this logic.

I left a few comments below, but none of these are blocking since the code works as-is. 👋

Copy link
Contributor

@dlqqq dlqqq left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Looks perfect!

@3coins 3coins merged commit 1e77a19 into jupyter-ai-contrib:main Oct 17, 2025
6 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.

Allow registering observers for specific slash commands

2 participants