All channels have a bunch of commands like /new /compact etc. I think Matrix does not,…
- User
All channels have a bunch of commands like /new /compact etc. I think Matrix does not, plan to add all those commands in Matrix too.
Assistant
Let me explore the existing channel command implementations and the Matrix channel to understand the gap.
Assistant
Now let me look at the Matrix handler and a simple reference (WhatsApp) to understand the exact code changes needed.
Assistant
Now I have a clear picture. The key insertion point is in
handle_room_messagearound line 319-376 where the text message is dispatched to chat. I need to add a slash command check beforesink.dispatch_to_chat(), similar to WhatsApp/Teams. Let me also check how Matrix sends text responses.Assistant
Now I have the full picture. Let me also check the Telegram help handler to understand what help text looks like, since we should add a local
/helphandler for Matrix too.Assistant
Now let me also check the Telegram
should_intercept_slash_commandto understand the full command interception pattern.Assistant
Good. Now I have a complete understanding. Let me plan the implementation.
The pattern is straightforward — in
handle_room_message, right beforesink.dispatch_to_chat()(line 376), I need to intercept slash commands, dispatch them viasink.dispatch_command(), and send the response back viasend_text(). I'll also handle/helplocally like Telegram does.Let me also check if there's anything in the text message flow between access check and dispatch that I need to be careful about (like the ack reaction — commands should probably still get the ack).
Assistant
Now let me implement the slash command handling. The insertion point is in
handle_room_message, right after the text message kind check and beforesink.dispatch_to_chat(). I need to intercept/prefixed text messages.Looking at the code flow (lines 319-377), the text dispatch happens inside
if let Some(sink) = &event_sink { ... }. The slash command check should go right before thesink.dispatch_to_chat(&body, reply_to, meta).await;call on line 376, after the location coordinate check.Assistant
Now I'll add the slash command interception right before
dispatch_to_chat:Assistant
Now I need to add the
HELP_TEXTconstant. Let me add it near the top of the file, after the existing constant:Assistant
Let me verify the code compiles:
Assistant
Compiles clean. Now let me add tests. Let me look at existing tests in the Matrix handler to follow the same patterns:
Assistant
Now let me see the end of the test file to add new tests:
Assistant
The tests in this file are unit tests for helper functions. The command interception I added is inline in the async handler, so I should add a test for
HELP_TEXTcontent and verify the command detection logic. But actually, the core logic is simple — it just checksbody.strip_prefix('/')and callsdispatch_command. The integration test coverage comes from the gateway'sdispatch_commandtests.Let me add a test for
HELP_TEXTand the command detection pattern:Assistant
All tests pass. Now let me run the format check and clippy:
Assistant
The clippy failure is from CUDA (pre-existing, not related to my change). Let me fix the formatting and run clippy just on the matrix crate:
Assistant
Clean. Let me verify the final state of the changes:
Assistant
Here's a summary of the changes:
crates/matrix/src/handler.rs— two additions:-
HELP_TEXTconstant (line 51): Lists all 12 slash commands with descriptions, matching Telegram's help text but also including/peekand/stop. -
Command interception (line 390): Before
dispatch_to_chat(), checks if a text message starts with/. If it's/help, returns the local help text. For all other commands, delegates tosink.dispatch_command()— the same gateway dispatcher that WhatsApp, Teams, Telegram, Discord, and Slack all use. Sends the response (or error) back to the room viasend_text(). -
Two tests: Verifies
HELP_TEXTmentions all 12 commands and that the slash prefix detection logic works.
The pattern follows the simplest approach used by WhatsApp and Teams (text-based
/prefix detection), which is the natural fit for Matrix.-
Assistant
Ready to commit when you'd like — single file change, 72 lines added to
crates/matrix/src/handler.rs.- User
commit push create a PR
Assistant
PR created: https://github.com/moltis-org/moltis/pull/590
- User
Fix PR comments
Assistant
Valid point —
starts_with("help")would match/helpme,/helper, etc. I'll use the gateway idiom of extracting the command name viasplit_whitespace.Assistant
All 95 tests pass, formatting clean.
Assistant
Pushed. The fix extracts the command name with
split_whitespace().next()before comparing to"help", so/helpmeetc. won't incorrectly match.