feat(r): standalone register_slash_command() - #274
Open
kaipingyang wants to merge 1 commit into
Open
Conversation
Factor the slash-command registration machinery out of chat_server() into an exported register_slash_command(id, ...) plus an internal slash_commands_registry() that both paths share. This fills the existing TODO in chat_app.R for a standalone entry point that works outside chat_server()'s returned environment, so callers don't have to thread the return value and can add the official palette to a chat_ui() driven by fully custom server logic. The registry lives in session$userData keyed by the namespaced id (the same idiom as chat_restore.R); the dispatch + sync observers are set up once, lazily. chat_server()'s $slash_command() becomes a thin wrapper over the same code. Adds standalone tests alongside the existing chat_server slash tests.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes the standing
TODOinchat_app.Rfor a standaloneregister_slash_command(). See #273 for motivation and the proposed API.What this does
Factors the slash-command registration machinery out of
chat_server()into:register_slash_command(id, name, description, handler, ..., echo = NULL, force = FALSE, session = getDefaultReactiveDomain())— exported. A free function you can call from anywhere with just the chatidandsession; no need to threadchat_server()'s return value, and it works with a fully customchat_ui()+chat_append()loop (nochat_server()required).slash_commands_registry(id, session)— internal. Get-or-create the per-(session, id)command registry, wiring the dispatch + sync observers exactly once. State lives insession$userDatakeyed by the namespaced id, matching thechat_restore.Ridiom.chat_server()'s$slash_command()is now a thin wrapper overregister_slash_command(), so both paths share one implementation. This is a pure refactor — no behavior change to the existingchat_server()API.Semantics (unchanged from
$slash_command())handler: 0-/1-arg function, orNULLfor client-side handling via the cancelableshiny:chat-slash-commandevent. A 1-arg handler receives aContentSlashCommand.echodefaults toTRUEwhen a handler is supplied,FALSEotherwise.forceoverwrites an existing command; returns an unregister function.chat_append()/chat_clear()(idfirst,sessionlast).Test plan
chat_serverslash tests still pass (updated the 3 that reached into the old closure to read the registry from its new location).register_slash_command()tests: 0-/1-arg dispatch,ContentSlashCommandpayload, validation,echodefaults,force, unregister, single-registry-per-(session, id).devtools::test(), 516 tests, 0 failures).R CMD check: 0 errors, 0 warnings (remaining NOTES are pre-existing and unrelated).chat_server()and standaloneregister_slash_command(), including inside a Shiny module, the 0-arg path, the client-sideNULL-handler +preventDefault()path, and thecontent@text→chat_append()pattern.Open question
I kept the unregister-closure return (R idiom). If you'd rather have a
remove_slash_command()for parity with Python'sChat.remove_slash_command(), I'm happy to add it.