Class: ClaudeAgentSDK::CommandBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/claude_agent_sdk/command_builder.rb

Overview

Builds the CLI argv array from a ClaudeAgentOptions instance.

Constant Summary collapse

EXTRA_ARG_FLAG_REGEXP =
/\A[a-z0-9][a-z0-9-]*\z/
SKILL_NAME_INVALID_CHARS =

Parentheses and commas are delimiters to the --allowedTools tokenizer; control characters (C0, DEL, C1) never appear in a skill directory name. U+FEFF is here rather than in the whitespace checks below because the CLI trims it as whitespace and [[:space:]] does not match it.

/[(),\u0000-\u001F\u007F-\u009F\uFEFF]/
SKILL_NAME_EDGE_WHITESPACE =

Unicode-aware edge whitespace: the CLI and Skill tool trim Unicode whitespace, and Ruby's String#strip is ASCII-only.

/\A[[:space:]]+|[[:space:]]+\z/

Instance Method Summary collapse

Constructor Details

#initialize(cli_path, options) ⇒ CommandBuilder

Returns a new instance of CommandBuilder.



22
23
24
25
# File 'lib/claude_agent_sdk/command_builder.rb', line 22

def initialize(cli_path, options)
  @cli_path = cli_path
  @options = options
end

Instance Method Details

#buildObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/claude_agent_sdk/command_builder.rb', line 27

def build
  cmd = [@cli_path, "--output-format", "stream-json", "--verbose"]

  # skills auto-wires the Skill tool into --allowedTools and defaults
  # --setting-sources; compute both once so the two flags cannot diverge
  # (mirrors Python _apply_skills_defaults).
  effective_allowed_tools, effective_setting_sources = skills_defaults

  append_system_prompt(cmd)
  append_tools(cmd)
  append_allowed_tools(cmd, effective_allowed_tools)
  append_disallowed_tools(cmd)
  append_max_turns(cmd)
  append_model(cmd)
  append_permission(cmd)
  append_session(cmd)
  append_settings(cmd)
  append_budget(cmd)
  append_thinking(cmd)
  append_effort(cmd)
  append_betas(cmd)
  append_output_format(cmd)
  append_additional_dirs(cmd)
  append_mcp_servers(cmd)
  append_boolean_flags(cmd)
  append_plugins(cmd)
  append_setting_sources(cmd, effective_setting_sources)
  append_extra_args(cmd)

  # Always use streaming mode for bidirectional control protocol.
  # Prompts and agents are sent via stdin (initialize + user messages),
  # which avoids OS ARG_MAX limits for large prompts and agent configurations.
  cmd.push("--input-format", "stream-json")

  cmd
end