Class: ClaudeAgentSDK::SubprocessCLITransport

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

Overview

Subprocess transport using Claude Code CLI

Constant Summary collapse

DEFAULT_MAX_BUFFER_SIZE =

1MB buffer limit

1024 * 1024
MINIMUM_CLAUDE_CODE_VERSION =
'2.0.0'

Instance Method Summary collapse

Constructor Details

#initialize(prompt, options) ⇒ SubprocessCLITransport

Returns a new instance of SubprocessCLITransport.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/claude_agent_sdk/subprocess_cli_transport.rb', line 15

def initialize(prompt, options)
  @prompt = prompt
  @is_streaming = !prompt.is_a?(String)
  @options = options
  @cli_path = options.cli_path || find_cli
  @cwd = options.cwd
  @process = nil
  @stdin = nil
  @stdout = nil
  @stderr = nil
  @ready = false
  @exit_error = nil
  @max_buffer_size = options.max_buffer_size || DEFAULT_MAX_BUFFER_SIZE
  @stderr_task = nil
end

Instance Method Details

#build_commandObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/claude_agent_sdk/subprocess_cli_transport.rb', line 60

def build_command
  cmd = [@cli_path, '--output-format', 'stream-json', '--verbose']

  # System prompt handling
  if @options.system_prompt
    if @options.system_prompt.is_a?(String)
      cmd.concat(['--system-prompt', @options.system_prompt])
    elsif @options.system_prompt.is_a?(Hash) &&
          @options.system_prompt[:type] == 'preset' &&
          @options.system_prompt[:append]
      cmd.concat(['--append-system-prompt', @options.system_prompt[:append]])
    end
  end

  cmd.concat(['--allowedTools', @options.allowed_tools.join(',')]) unless @options.allowed_tools.empty?
  cmd.concat(['--max-turns', @options.max_turns.to_s]) if @options.max_turns
  cmd.concat(['--disallowedTools', @options.disallowed_tools.join(',')]) unless @options.disallowed_tools.empty?
  cmd.concat(['--model', @options.model]) if @options.model
  cmd.concat(['--permission-prompt-tool', @options.permission_prompt_tool_name]) if @options.permission_prompt_tool_name
  cmd.concat(['--permission-mode', @options.permission_mode]) if @options.permission_mode
  cmd << '--continue' if @options.continue_conversation
  cmd.concat(['--resume', @options.resume]) if @options.resume
  cmd.concat(['--settings', @options.settings]) if @options.settings

  # Add directories
  @options.add_dirs.each do |dir|
    cmd.concat(['--add-dir', dir.to_s])
  end

  # MCP servers
  if @options.mcp_servers && !@options.mcp_servers.empty?
    if @options.mcp_servers.is_a?(Hash)
      servers_for_cli = {}
      @options.mcp_servers.each do |name, config|
        if config.is_a?(Hash) && config[:type] == 'sdk'
          # For SDK servers, exclude instance field
          sdk_config = config.reject { |k, _| k == :instance }
          servers_for_cli[name] = sdk_config
        else
          servers_for_cli[name] = config
        end
      end
      cmd.concat(['--mcp-config', JSON.generate({ mcpServers: servers_for_cli })]) unless servers_for_cli.empty?
    else
      cmd.concat(['--mcp-config', @options.mcp_servers.to_s])
    end
  end

  cmd << '--include-partial-messages' if @options.include_partial_messages
  cmd << '--fork-session' if @options.fork_session

  # Agents
  if @options.agents
    agents_dict = @options.agents.transform_values do |agent_def|
      {
        description: agent_def.description,
        prompt: agent_def.prompt,
        tools: agent_def.tools,
        model: agent_def.model
      }.compact
    end
    cmd.concat(['--agents', JSON.generate(agents_dict)])
  end

  # Setting sources
  sources_value = @options.setting_sources ? @options.setting_sources.join(',') : ''
  cmd.concat(['--setting-sources', sources_value])

  # Extra args
  @options.extra_args.each do |flag, value|
    if value.nil?
      cmd << "--#{flag}"
    else
      cmd.concat(["--#{flag}", value.to_s])
    end
  end

  # Prompt handling
  if @is_streaming
    cmd.concat(['--input-format', 'stream-json'])
  else
    cmd.concat(['--print', '--', @prompt.to_s])
  end

  cmd
end

#check_claude_versionObject



342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
# File 'lib/claude_agent_sdk/subprocess_cli_transport.rb', line 342

def check_claude_version
  begin
    output = `#{@cli_path} -v 2>&1`.strip
    if match = output.match(/([0-9]+\.[0-9]+\.[0-9]+)/)
      version = match[1]
      version_parts = version.split('.').map(&:to_i)
      min_parts = MINIMUM_CLAUDE_CODE_VERSION.split('.').map(&:to_i)

      if version_parts < min_parts
        warning = "Warning: Claude Code version #{version} is unsupported in the Agent SDK. " \
                  "Minimum required version is #{MINIMUM_CLAUDE_CODE_VERSION}. " \
                  "Some features may not work correctly."
        warn warning
      end
    end
  rescue StandardError
    # Ignore version check errors
  end
end

#closeObject



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/claude_agent_sdk/subprocess_cli_transport.rb', line 222

def close
  @ready = false
  return unless @process

  # Cancel stderr task
  @stderr_task&.stop

  # Close streams
  begin
    @stdin&.close
  rescue StandardError
    # Ignore
  end
  begin
    @stderr&.close
  rescue StandardError
    # Ignore
  end

  # Terminate process
  begin
    @process.terminate
    @process.wait
  rescue StandardError
    # Ignore
  end

  @process = nil
  @stdout = nil
  @stdin = nil
  @stderr = nil
  @exit_error = nil
end

#connectObject



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/claude_agent_sdk/subprocess_cli_transport.rb', line 147

def connect
  return if @process

  check_claude_version

  cmd = build_command

  # Build environment
  process_env = ENV.to_h.merge(@options.env).merge(
    'CLAUDE_CODE_ENTRYPOINT' => 'sdk-rb',
    'CLAUDE_AGENT_SDK_VERSION' => VERSION
  )
  process_env['PWD'] = @cwd.to_s if @cwd

  # Determine stderr handling
  should_pipe_stderr = @options.stderr || @options.extra_args.key?('debug-to-stderr')

  begin
    # Start process
    @process = Async::Process::Child.new(
      *cmd,
      stdin: :pipe,
      stdout: :pipe,
      stderr: should_pipe_stderr ? :pipe : nil,
      chdir: @cwd&.to_s,
      env: process_env
    )

    @stdout = @process.stdout
    @stdin = @process.stdin if @is_streaming

    # Handle stderr if piped
    if should_pipe_stderr && @process.stderr
      @stderr = @process.stderr
      @stderr_task = Async do
        handle_stderr
      rescue StandardError
        # Ignore errors during stderr reading
      end
    end

    # Close stdin for non-streaming mode
    @process.stdin.close unless @is_streaming

    @ready = true
  rescue Errno::ENOENT => e
    # Check if error is from cwd or CLI
    if @cwd && !File.directory?(@cwd.to_s)
      error = CLIConnectionError.new("Working directory does not exist: #{@cwd}")
      @exit_error = error
      raise error
    end
    error = CLINotFoundError.new("Claude Code not found at: #{@cli_path}")
    @exit_error = error
    raise error
  rescue StandardError => e
    error = CLIConnectionError.new("Failed to start Claude Code: #{e}")
    @exit_error = error
    raise error
  end
end

#end_inputObject



272
273
274
275
276
277
278
279
280
281
# File 'lib/claude_agent_sdk/subprocess_cli_transport.rb', line 272

def end_input
  return unless @stdin

  begin
    @stdin.close
  rescue StandardError
    # Ignore
  end
  @stdin = nil
end

#find_cliObject

Raises:



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
# File 'lib/claude_agent_sdk/subprocess_cli_transport.rb', line 31

def find_cli
  # Try which command first
  cli = `which claude 2>/dev/null`.strip
  return cli unless cli.empty?

  # Try common locations
  locations = [
    File.join(Dir.home, '.claude/local/claude'),  # Claude Code default install location
    File.join(Dir.home, '.npm-global/bin/claude'),
    '/usr/local/bin/claude',
    File.join(Dir.home, '.local/bin/claude'),
    File.join(Dir.home, 'node_modules/.bin/claude'),
    File.join(Dir.home, '.yarn/bin/claude')
  ]

  locations.each do |path|
    return path if File.exist?(path) && File.file?(path)
  end

  raise CLINotFoundError.new(
    "Claude Code not found. Install with:\n" \
    "  npm install -g @anthropic-ai/claude-code\n" \
    "\nIf already installed locally, try:\n" \
    '  export PATH="$HOME/node_modules/.bin:$PATH"' \
    "\n\nOr provide the path via ClaudeAgentOptions:\n" \
    "  ClaudeAgentOptions.new(cli_path: '/path/to/claude')"
  )
end

#handle_stderrObject



209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/claude_agent_sdk/subprocess_cli_transport.rb', line 209

def handle_stderr
  return unless @stderr

  @stderr.each_line do |line|
    line_str = line.chomp
    next if line_str.empty?

    @options.stderr&.call(line_str)
  end
rescue StandardError
  # Ignore errors during stderr reading
end

#read_messages(&block) ⇒ Object

Raises:



283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# File 'lib/claude_agent_sdk/subprocess_cli_transport.rb', line 283

def read_messages(&block)
  return enum_for(:read_messages) unless block_given?

  raise CLIConnectionError, 'Not connected' unless @process && @stdout

  json_buffer = ''

  begin
    @stdout.each_line do |line|
      line_str = line.strip
      next if line_str.empty?

      json_lines = line_str.split("\n")

      json_lines.each do |json_line|
        json_line = json_line.strip
        next if json_line.empty?

        json_buffer += json_line

        if json_buffer.bytesize > @max_buffer_size
          buffer_length = json_buffer.bytesize
          json_buffer = ''
          raise CLIJSONDecodeError.new(
            "JSON message exceeded maximum buffer size",
            StandardError.new("Buffer size #{buffer_length} exceeds limit #{@max_buffer_size}")
          )
        end

        begin
          data = JSON.parse(json_buffer, symbolize_names: true)
          json_buffer = ''
          yield data
        rescue JSON::ParserError
          # Continue accumulating
          next
        end
      end
    end
  rescue IOError
    # Stream closed
  rescue StopIteration
    # Client disconnected
  end

  # Check process completion
  status = @process.wait
  returncode = status.exitstatus

  if returncode && returncode != 0
    @exit_error = ProcessError.new(
      "Command failed with exit code #{returncode}",
      exit_code: returncode,
      stderr: 'Check stderr output for details'
    )
    raise @exit_error
  end
end

#ready?Boolean

Returns:

  • (Boolean)


362
363
364
# File 'lib/claude_agent_sdk/subprocess_cli_transport.rb', line 362

def ready?
  @ready
end

#write(data) ⇒ Object

Raises:



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/claude_agent_sdk/subprocess_cli_transport.rb', line 256

def write(data)
  raise CLIConnectionError, 'ProcessTransport is not ready for writing' unless @ready && @stdin
  raise CLIConnectionError, "Cannot write to terminated process" if @process && @process.status

  raise CLIConnectionError, "Cannot write to process that exited with error: #{@exit_error}" if @exit_error

  begin
    @stdin.write(data)
    @stdin.flush
  rescue StandardError => e
    @ready = false
    @exit_error = CLIConnectionError.new("Failed to write to process stdin: #{e}")
    raise @exit_error
  end
end