Class: ClaudeCode::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/claude_code/client.rb

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



9
10
11
# File 'lib/claude_code/client.rb', line 9

def initialize
  # Client setup
end

Instance Method Details

#build_environmentObject (private)



50
51
52
53
54
55
56
57
# File 'lib/claude_code/client.rb', line 50

def build_environment
  env = ENV.to_h
  env['CLAUDE_CODE_ENTRYPOINT'] = 'sdk-ruby'
  env['ANTHROPIC_API_KEY'] = ENV['ANTHROPIC_API_KEY'] if ENV['ANTHROPIC_API_KEY']
  env['CLAUDE_CODE_USE_BEDROCK'] = ENV['CLAUDE_CODE_USE_BEDROCK'] if ENV['CLAUDE_CODE_USE_BEDROCK']
  env['CLAUDE_CODE_USE_VERTEX'] = ENV['CLAUDE_CODE_USE_VERTEX'] if ENV['CLAUDE_CODE_USE_VERTEX']
  env
end

#parse_message(data) ⇒ Object (private)



59
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
# File 'lib/claude_code/client.rb', line 59

def parse_message(data)
  case data['type']
  when 'user'
    UserMessage.new(data.dig('message', 'content'))
  when 'assistant'
    content_blocks = []
    message_content = data.dig('message', 'content') || []
    message_content.each do |block|
      case block['type']
      when 'text'
        content_blocks << TextBlock.new(block['text'])
      when 'tool_use'
        content_blocks << ToolUseBlock.new(
          id: block['id'],
          name: block['name'],
          input: block['input']
        )
      when 'tool_result'
        content_blocks << ToolResultBlock.new(
          tool_use_id: block['tool_use_id'],
          content: block['content'],
          is_error: block['is_error']
        )
      end
    end
    AssistantMessage.new(content_blocks)
  when 'system'
    SystemMessage.new(
      subtype: data['subtype'],
      data: data
    )
  when 'result'
    ResultMessage.new(
      subtype: data['subtype'],
      duration_ms: data['duration_ms'],
      duration_api_ms: data['duration_api_ms'],
      is_error: data['is_error'],
      num_turns: data['num_turns'],
      session_id: data['session_id'],
      total_cost_usd: data['total_cost_usd'],
      usage: data['usage'],
      result: data['result']
    )
  else
    nil
  end
end

#process_query(options:, prompt: nil, messages: nil, cli_path: nil, mcp_servers: {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/claude_code/client.rb', line 13

def process_query(options:, prompt: nil, messages: nil, cli_path: nil, mcp_servers: {})
  if messages
    # Handle streaming JSON input
    transport = SubprocessCLITransport.new(prompt: '', options: options, cli_path: cli_path)
    transport.connect

    # Send messages
    transport.send_messages(messages)

    # Return enumerator for responses
    return Enumerator.new do |yielder|
      transport.receive_messages do |data|
        message = parse_message(data)
        yielder << message if message
      end
    ensure
      transport.disconnect
    end
  end

  transport = SubprocessCLITransport.new(prompt: prompt, options: options, cli_path: cli_path)

  transport.connect

  # Return lazy enumerator that streams messages as they arrive
  Enumerator.new do |yielder|
    transport.receive_messages do |data|
      message = parse_message(data)
      yielder << message if message
    end
  ensure
    transport.disconnect
  end
end