Skip to content

Commit 9a43922

Browse files
committed
Updated README
1 parent 649b775 commit 9a43922

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

README.md

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ router = self.serverapp.web_app.settings.get("jupyter-ai", {}).get("router")
1818
def on_new_chat(room_id: str, ychat: YChat):
1919
print(f"New chat connected: {room_id}")
2020

21-
def on_slash_command(room_id: str, message: Message):
22-
print(f"Slash command in {room_id}: {message.body}")
21+
def on_slash_command(room_id: str, command: str, message: Message):
22+
print(f"Slash command '{command}' in {room_id}: {message.body}")
2323

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

2727
# Register the callbacks
2828
router.observe_chat_init(on_new_chat)
29-
router.observe_slash_cmd_msg("room-id", on_slash_command)
29+
router.observe_slash_cmd_msg("room-id", "help", on_slash_command) # Only /help commands
3030
router.observe_chat_msg("room-id", on_regular_message)
3131
```
3232

@@ -40,9 +40,30 @@ router.observe_chat_msg("room-id", on_regular_message)
4040
### Available Methods
4141

4242
- `observe_chat_init(callback)` - Called when new chat sessions are initialized with `(room_id, ychat)`
43-
- `observe_slash_cmd_msg(room_id, callback)` - Called for messages starting with `/` in a specific room
43+
- `observe_slash_cmd_msg(room_id, command_pattern, callback)` - Called for specific slash commands matching the pattern in a specific room
4444
- `observe_chat_msg(room_id, callback)` - Called for regular (non-slash) messages in a specific room
4545

46+
### Command Pattern Matching
47+
48+
The `observe_slash_cmd_msg` method supports flexible command pattern matching:
49+
50+
```python
51+
# Exact match: Only matches "/help"
52+
router.observe_slash_cmd_msg("room-id", "help", callback)
53+
54+
# Wildcard match: Matches "/ai-generate", "/ai-review", etc.
55+
router.observe_slash_cmd_msg("room-id", "ai-*", callback)
56+
57+
# Regex pattern: Matches "/export-json", "/export-csv", "/export-xml"
58+
router.observe_slash_cmd_msg("room-id", r"export-(json|csv|xml)", callback)
59+
```
60+
61+
**Callback signature**: `callback(room_id: str, command: str, message: Message)`
62+
63+
- `room_id`: The chat room identifier
64+
- `command`: The matched command without the leading slash (e.g., "help", "ai-generate")
65+
- `message`: Message object with the command removed from the body (only arguments remain)
66+
4667
## Install
4768

4869
To install the extension, execute:

0 commit comments

Comments
 (0)