MCPcopy Create free account
hub / github.com/AI45Lab/Code / register_command

Method register_command

sdk/node/src/lib.rs:4820–4855  ·  view source on GitHub ↗
(
        &self,
        name: String,
        description: String,
        handler: napi::JsFunction,
    )

Source from the content-addressed store, hash-verified

4818 ts_args_type = "name: string, description: string, handler: (args: string, ctx: CommandContext) => string"
4819 )]
4820 pub fn register_command(
4821 &self,
4822 name: String,
4823 description: String,
4824 handler: napi::JsFunction,
4825 ) -> napi::Result<()> {
4826 use napi::threadsafe_function::ThreadSafeCallContext;
4827
4828 // Create a threadsafe function that calls the JS handler
4829 let tsfn: napi::threadsafe_function::ThreadsafeFunction<
4830 (String, RustCommandContext),
4831 napi::threadsafe_function::ErrorStrategy::Fatal,
4832 > = handler.create_threadsafe_function(
4833 0,
4834 |ctx: ThreadSafeCallContext<(String, RustCommandContext)>| {
4835 // Extract the values
4836 let args = ctx.value.0;
4837 let cmd_ctx = ctx.value.1;
4838
4839 // Convert to JS values
4840 let args_str = ctx.env.create_string(&args)?;
4841 let ctx_obj = js_command_context_to_object(&ctx.env, &cmd_ctx)?;
4842
4843 // Return the arguments that will be passed to the JS function
4844 Ok(vec![args_str.into_unknown(), ctx_obj.into_unknown()])
4845 },
4846 )?;
4847
4848 let cmd = Arc::new(JsSlashCommand {
4849 name,
4850 description,
4851 handler: Arc::new(tsfn),
4852 });
4853 self.inner.clone().register_command(cmd);
4854 Ok(())
4855 }
4856
4857 /// List all registered slash commands.
4858 ///

Callers

nothing calls this directly

Calls 2

cloneMethod · 0.45

Tested by

no test coverage detected