Class: RubynCode::CLI::Commands::CustomCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/rubyn_code/cli/commands/custom_command.rb

Overview

A slash command defined by a user markdown file (loaded by CustomLoader). Unlike the built-in commands — which are registered as classes — this is a ready instance: the Registry dispatches it directly.

Executing it renders the template (argument / bash substitution) and sends the result to the agent as a normal prompt.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, description:, body:, source: nil, argument_hint: nil, allowed_tools: nil, model: nil) ⇒ CustomCommand

rubocop:disable Metrics/ParameterLists -- explicit kwargs document the frontmatter surface



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/rubyn_code/cli/commands/custom_command.rb', line 26

def initialize(name:, description:, body:, # rubocop:disable Metrics/ParameterLists -- explicit kwargs document the frontmatter surface
               source: nil,
               argument_hint: nil, allowed_tools: nil, model: nil)
  @name = name
  @description = description
  @template = CommandTemplate.new(body)
  @argument_hint = argument_hint
  @allowed_tools = allowed_tools
  @model = model
  @source = source
end

Instance Attribute Details

#allowed_toolsArray<String>? (readonly)

Returns tool names the command restricts to.

Returns:

  • (Array<String>, nil)

    tool names the command restricts to



20
21
22
# File 'lib/rubyn_code/cli/commands/custom_command.rb', line 20

def allowed_tools
  @allowed_tools
end

#argument_hintString? (readonly)

Returns argument hint shown next to the name in /help.

Returns:

  • (String, nil)

    argument hint shown next to the name in /help



18
19
20
# File 'lib/rubyn_code/cli/commands/custom_command.rb', line 18

def argument_hint
  @argument_hint
end

#descriptionString (readonly)

Returns one-line description for /help.

Returns:

  • (String)

    one-line description for /help



16
17
18
# File 'lib/rubyn_code/cli/commands/custom_command.rb', line 16

def description
  @description
end

#modelString? (readonly)

Returns model override for this prompt.

Returns:

  • (String, nil)

    model override for this prompt



22
23
24
# File 'lib/rubyn_code/cli/commands/custom_command.rb', line 22

def model
  @model
end

#nameString (readonly)

Returns command name without the leading slash.

Returns:

  • (String)

    command name without the leading slash



14
15
16
# File 'lib/rubyn_code/cli/commands/custom_command.rb', line 14

def name
  @name
end

#sourceString? (readonly)

Returns originating file path.

Returns:

  • (String, nil)

    originating file path



24
25
26
# File 'lib/rubyn_code/cli/commands/custom_command.rb', line 24

def source
  @source
end

Instance Method Details

#aliasesObject



39
# File 'lib/rubyn_code/cli/commands/custom_command.rb', line 39

def aliases = [].freeze

#all_namesObject



41
# File 'lib/rubyn_code/cli/commands/custom_command.rb', line 41

def all_names = [command_name].freeze

#command_nameObject



38
# File 'lib/rubyn_code/cli/commands/custom_command.rb', line 38

def command_name = "/#{@name}"

#execute(args, ctx) ⇒ nil

Parameters:

Returns:

  • (nil)


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/rubyn_code/cli/commands/custom_command.rb', line 64

def execute(args, ctx)
  rendered = @template.render(args)
  if restricts_tools?
    ctx.with_allowed_tools(@allowed_tools) do
      ctx.with_optional_model(@model) do
        ctx.send_message(rendered)
      end
    end
  elsif overrides_model?
    ctx.with_optional_model(@model) do
      ctx.send_message(rendered)
    end
  else
    ctx.send_message(rendered)
  end
  nil
end

#help_labelObject

Render the help label, including [hint] when frontmatter provides one. The argument_hint is expected to be the placeholder text already wrapped in [ ] (e.g. "[env]") — we don't add another set of brackets.



46
47
48
49
# File 'lib/rubyn_code/cli/commands/custom_command.rb', line 46

def help_label
  hint = @argument_hint.to_s.strip
  hint.empty? ? description : "#{description}  #{hint}"
end

#hidden?Boolean

Returns:

  • (Boolean)


40
# File 'lib/rubyn_code/cli/commands/custom_command.rb', line 40

def hidden? = false

#overrides_model?Boolean

Returns true if the frontmatter overrode the model.

Returns:

  • (Boolean)

    true if the frontmatter overrode the model



57
58
59
# File 'lib/rubyn_code/cli/commands/custom_command.rb', line 57

def overrides_model?
  !@model.to_s.strip.empty?
end

#restricts_tools?Boolean

Returns true if the frontmatter restricted the tool set.

Returns:

  • (Boolean)

    true if the frontmatter restricted the tool set



52
53
54
# File 'lib/rubyn_code/cli/commands/custom_command.rb', line 52

def restricts_tools?
  !@allowed_tools.nil? && !@allowed_tools.empty?
end