Class: ClaudeSDK::Internal::InternalClient
- Inherits:
-
Object
- Object
- ClaudeSDK::Internal::InternalClient
- Defined in:
- lib/claude_sdk/internal/client.rb
Overview
Internal client implementation for processing queries through transport
This client handles the communication with Claude Code CLI via the transport layer and parses the received messages into Ruby objects.
Instance Method Summary collapse
-
#initialize ⇒ InternalClient
constructor
Initialize the internal client.
-
#parse_content_blocks(blocks) ⇒ Array<ContentBlock::Text, ContentBlock::ToolUse, ContentBlock::ToolResult>
private
Parse content blocks from assistant message.
-
#parse_message(data) ⇒ Messages::User, ...
private
Parse message from CLI output.
-
#process_query(prompt:, options:) {|Messages::User, Messages::Assistant, Messages::System, Messages::Result| ... } ⇒ Enumerator
Process a query through transport.
Constructor Details
#initialize ⇒ InternalClient
Initialize the internal client
22 23 24 |
# File 'lib/claude_sdk/internal/client.rb', line 22 def initialize # Currently no initialization needed end |
Instance Method Details
#parse_content_blocks(blocks) ⇒ Array<ContentBlock::Text, ContentBlock::ToolUse, ContentBlock::ToolResult> (private)
Parse content blocks from assistant message
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 |
# File 'lib/claude_sdk/internal/client.rb', line 95 def parse_content_blocks(blocks) blocks.map do |block| case block["type"] when "text" ContentBlock::Text.new( text: block["text"], ) when "tool_use" ContentBlock::ToolUse.new( id: block["id"], name: block["name"], input: block["input"], ) when "tool_result" ContentBlock::ToolResult.new( tool_use_id: block["tool_use_id"], content: block["content"], is_error: block["is_error"], ) end end.compact end |
#parse_message(data) ⇒ Messages::User, ... (private)
Parse message from CLI output
58 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 |
# File 'lib/claude_sdk/internal/client.rb', line 58 def (data) case data["type"] when "user" Messages::User.new( content: data.dig("message", "content"), ) when "assistant" content_blocks = parse_content_blocks(data.dig("message", "content") || []) Messages::Assistant.new(content: content_blocks) when "system" Messages::System.new( subtype: data["subtype"], data: data, ) when "result" Messages::Result.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"], ) end end |
#process_query(prompt:, options:) {|Messages::User, Messages::Assistant, Messages::System, Messages::Result| ... } ⇒ Enumerator
Process a query through transport
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/claude_sdk/internal/client.rb', line 32 def process_query(prompt:, options:) return enum_for(:process_query, prompt: prompt, options: ) unless block_given? transport = SubprocessCLI.new( prompt: prompt, options: , ) begin transport.connect transport. do |data| = (data) yield if end ensure transport.disconnect end end |