claude_code 0.0.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/.rspec_status +114 -0
- data/.rubocop.yml +51 -0
- data/.yardopts +7 -0
- data/CHANGELOG.md +37 -0
- data/LICENSE +21 -0
- data/README.md +288 -0
- data/Rakefile +32 -0
- data/USAGE_EXAMPLES.md +428 -0
- data/claude_code.gemspec +47 -0
- data/docs/README.md +254 -0
- data/docs/mcp_integration.md +404 -0
- data/docs/streaming.md +316 -0
- data/examples/authentication_examples.rb +133 -0
- data/examples/basic_usage.rb +73 -0
- data/examples/conversation_resuming.rb +96 -0
- data/examples/irb_helpers.rb +168 -0
- data/examples/jsonl_cli_equivalent.rb +108 -0
- data/examples/mcp_examples.rb +166 -0
- data/examples/model_examples.rb +111 -0
- data/examples/quick_start.rb +75 -0
- data/examples/rails_sidekiq_example.rb +336 -0
- data/examples/streaming_examples.rb +195 -0
- data/examples/streaming_json_input.rb +171 -0
- data/hello.txt +1 -0
- data/lib/claude_code/client.rb +437 -0
- data/lib/claude_code/errors.rb +48 -0
- data/lib/claude_code/types.rb +237 -0
- data/lib/claude_code/version.rb +5 -0
- data/lib/claude_code.rb +265 -0
- metadata +219 -0
@@ -0,0 +1,168 @@
|
|
1
|
+
# IRB helpers for Ruby Claude Code SDK with MCP
|
2
|
+
# Load this file: require_relative 'examples/irb_helpers'
|
3
|
+
|
4
|
+
require_relative '../lib/claude_code'
|
5
|
+
|
6
|
+
# Quick MCP test with Creator Ninja
|
7
|
+
def ninja_test(prompt)
|
8
|
+
ClaudeCode.quick_mcp_query(
|
9
|
+
prompt,
|
10
|
+
server_name: 'ninja',
|
11
|
+
server_url: 'https://mcp-creator-ninja-v1-4-0.mcp.soy/',
|
12
|
+
tools: 'about'
|
13
|
+
).each do |msg|
|
14
|
+
case msg
|
15
|
+
when ClaudeCode::AssistantMessage
|
16
|
+
msg.content.each do |block|
|
17
|
+
case block
|
18
|
+
when ClaudeCode::TextBlock
|
19
|
+
puts "💬 #{block.text}"
|
20
|
+
when ClaudeCode::ToolUseBlock
|
21
|
+
puts "🔧 #{block.name}: #{block.input}"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
when ClaudeCode::ResultMessage
|
25
|
+
puts "💰 $#{format('%.6f', msg.total_cost_usd)}" if msg.total_cost_usd
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
# Generic MCP tester
|
31
|
+
def test_mcp(prompt, server_name, server_url, tools)
|
32
|
+
ClaudeCode.quick_mcp_query(
|
33
|
+
prompt,
|
34
|
+
server_name: server_name,
|
35
|
+
server_url: server_url,
|
36
|
+
tools: tools
|
37
|
+
).each { |msg| puts msg.inspect }
|
38
|
+
end
|
39
|
+
|
40
|
+
# Quick Claude query without MCP
|
41
|
+
def quick_claude(prompt, model: nil)
|
42
|
+
options = ClaudeCode::ClaudeCodeOptions.new(
|
43
|
+
model: model,
|
44
|
+
max_turns: 1
|
45
|
+
)
|
46
|
+
|
47
|
+
ClaudeCode.query(
|
48
|
+
prompt: prompt,
|
49
|
+
options: options,
|
50
|
+
cli_path: "/Users/admin/.claude/local/claude"
|
51
|
+
).each do |msg|
|
52
|
+
if msg.is_a?(ClaudeCode::AssistantMessage)
|
53
|
+
msg.content.each do |block|
|
54
|
+
if block.is_a?(ClaudeCode::TextBlock)
|
55
|
+
puts block.text
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
# Streaming version with timestamps
|
63
|
+
def stream_claude(prompt, model: nil)
|
64
|
+
start_time = Time.now
|
65
|
+
ClaudeCode.stream_query(
|
66
|
+
prompt: prompt,
|
67
|
+
options: ClaudeCode::ClaudeCodeOptions.new(model: model, max_turns: 1),
|
68
|
+
cli_path: "/Users/admin/.claude/local/claude"
|
69
|
+
) do |msg, index|
|
70
|
+
timestamp = Time.now - start_time
|
71
|
+
case msg
|
72
|
+
when ClaudeCode::AssistantMessage
|
73
|
+
msg.content.each do |block|
|
74
|
+
if block.is_a?(ClaudeCode::TextBlock)
|
75
|
+
puts "[#{format('%.2f', timestamp)}s] #{block.text}"
|
76
|
+
end
|
77
|
+
end
|
78
|
+
when ClaudeCode::ResultMessage
|
79
|
+
puts "[#{format('%.2f', timestamp)}s] 💰 $#{format('%.6f', msg.total_cost_usd || 0)}"
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
# Simple auto-streaming
|
85
|
+
def auto_stream(prompt, model: nil)
|
86
|
+
ClaudeCode.stream_query(
|
87
|
+
prompt: prompt,
|
88
|
+
options: ClaudeCode::ClaudeCodeOptions.new(model: model, max_turns: 1),
|
89
|
+
cli_path: "/Users/admin/.claude/local/claude"
|
90
|
+
)
|
91
|
+
end
|
92
|
+
|
93
|
+
# Continue the most recent conversation
|
94
|
+
def continue_chat(prompt = nil)
|
95
|
+
last_session_id = nil
|
96
|
+
|
97
|
+
ClaudeCode.continue_conversation(prompt) do |msg|
|
98
|
+
case msg
|
99
|
+
when ClaudeCode::AssistantMessage
|
100
|
+
msg.content.each do |block|
|
101
|
+
if block.is_a?(ClaudeCode::TextBlock)
|
102
|
+
puts "💬 #{block.text}"
|
103
|
+
end
|
104
|
+
end
|
105
|
+
when ClaudeCode::ResultMessage
|
106
|
+
last_session_id = msg.session_id
|
107
|
+
puts "📋 Session: #{last_session_id}"
|
108
|
+
puts "💰 $#{format('%.6f', msg.total_cost_usd || 0)}"
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
last_session_id
|
113
|
+
end
|
114
|
+
|
115
|
+
# Resume a specific conversation
|
116
|
+
def resume_chat(session_id, prompt = nil)
|
117
|
+
ClaudeCode.resume_conversation(session_id, prompt) do |msg|
|
118
|
+
case msg
|
119
|
+
when ClaudeCode::AssistantMessage
|
120
|
+
msg.content.each do |block|
|
121
|
+
if block.is_a?(ClaudeCode::TextBlock)
|
122
|
+
puts "💬 #{block.text}"
|
123
|
+
end
|
124
|
+
end
|
125
|
+
when ClaudeCode::ResultMessage
|
126
|
+
puts "📋 Session: #{msg.session_id}"
|
127
|
+
puts "💰 $#{format('%.6f', msg.total_cost_usd || 0)}"
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
# Save session ID for later use
|
133
|
+
$last_session_id = nil
|
134
|
+
|
135
|
+
def save_session(session_id)
|
136
|
+
$last_session_id = session_id
|
137
|
+
puts "💾 Saved session ID: #{session_id}"
|
138
|
+
end
|
139
|
+
|
140
|
+
def resume_last(prompt = nil)
|
141
|
+
if $last_session_id
|
142
|
+
resume_chat($last_session_id, prompt)
|
143
|
+
else
|
144
|
+
puts "❌ No saved session ID. Use save_session(id) first."
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
puts "🚀 Ruby Claude Code SDK with MCP, Streaming, and Conversation helpers loaded!"
|
149
|
+
puts
|
150
|
+
puts "Basic commands:"
|
151
|
+
puts " quick_claude('What is Ruby?')"
|
152
|
+
puts " ninja_test('Tell me about yourself')"
|
153
|
+
puts
|
154
|
+
puts "Streaming commands:"
|
155
|
+
puts " stream_claude('Explain Ruby blocks')"
|
156
|
+
puts " auto_stream('Count to 5')"
|
157
|
+
puts
|
158
|
+
puts "Conversation commands:"
|
159
|
+
puts " continue_chat('Follow up question')"
|
160
|
+
puts " resume_chat('session-id', 'New prompt')"
|
161
|
+
puts " save_session('session-id')"
|
162
|
+
puts " resume_last('New prompt')"
|
163
|
+
puts
|
164
|
+
puts "Advanced:"
|
165
|
+
puts " quick_claude('Explain arrays', model: 'sonnet')"
|
166
|
+
puts " test_mcp('prompt', 'server_name', 'server_url', 'tool_name')"
|
167
|
+
puts
|
168
|
+
puts "💡 Remember to set ANTHROPIC_API_KEY environment variable!"
|
@@ -0,0 +1,108 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require_relative '../lib/claude_code'
|
5
|
+
|
6
|
+
# Example: JSONL CLI equivalent showing how the Ruby SDK mirrors CLI functionality
|
7
|
+
|
8
|
+
puts "=== JSONL CLI Equivalent Example ==="
|
9
|
+
|
10
|
+
puts "\nThis example shows how the Ruby SDK implements the same functionality as:"
|
11
|
+
puts 'echo \'{"type":"user","message":{"role":"user","content":[{"type":"text","text":"Explain this code"}]}}\' | claude -p --output-format=stream-json --input-format=stream-json --verbose'
|
12
|
+
|
13
|
+
# Create a single user message in the exact format the CLI expects
|
14
|
+
user_message = {
|
15
|
+
'type' => 'user',
|
16
|
+
'message' => {
|
17
|
+
'role' => 'user',
|
18
|
+
'content' => [
|
19
|
+
{
|
20
|
+
'type' => 'text',
|
21
|
+
'text' => 'Explain this Ruby code: def factorial(n); n <= 1 ? 1 : n * factorial(n - 1); end'
|
22
|
+
}
|
23
|
+
]
|
24
|
+
}
|
25
|
+
}
|
26
|
+
|
27
|
+
puts "\n📝 Sending JSONL message:"
|
28
|
+
puts JSON.pretty_generate(user_message)
|
29
|
+
|
30
|
+
# Configure options to match CLI behavior
|
31
|
+
options = ClaudeCode::ClaudeCodeOptions.new(
|
32
|
+
input_format: 'stream-json',
|
33
|
+
output_format: 'stream-json'
|
34
|
+
)
|
35
|
+
|
36
|
+
puts "\n🚀 Processing with streaming JSON I/O..."
|
37
|
+
|
38
|
+
begin
|
39
|
+
ClaudeCode.stream_json_query([user_message], options: options) do |message|
|
40
|
+
case message
|
41
|
+
when ClaudeCode::SystemMessage
|
42
|
+
puts "🔧 System (#{message.subtype}): #{message.data.keys.join(', ')}"
|
43
|
+
when ClaudeCode::AssistantMessage
|
44
|
+
puts "\n💬 Assistant Response:"
|
45
|
+
message.content.each do |block|
|
46
|
+
case block
|
47
|
+
when ClaudeCode::TextBlock
|
48
|
+
puts block.text
|
49
|
+
when ClaudeCode::ToolUseBlock
|
50
|
+
puts "🔧 Tool Use: #{block.name}"
|
51
|
+
puts "📥 Input: #{block.input}"
|
52
|
+
when ClaudeCode::ToolResultBlock
|
53
|
+
puts "📤 Tool Result: #{block.content}"
|
54
|
+
end
|
55
|
+
end
|
56
|
+
when ClaudeCode::ResultMessage
|
57
|
+
puts "\n📊 Result:"
|
58
|
+
puts " Duration: #{message.duration_ms}ms"
|
59
|
+
puts " API Time: #{message.duration_api_ms}ms" if message.duration_api_ms
|
60
|
+
puts " Cost: $#{format('%.6f', message.total_cost_usd || 0)}"
|
61
|
+
puts " Turns: #{message.num_turns}"
|
62
|
+
puts " Session: #{message.session_id}"
|
63
|
+
puts " Status: #{message.subtype}"
|
64
|
+
end
|
65
|
+
end
|
66
|
+
rescue ClaudeCode::CLINotFoundError => e
|
67
|
+
puts "❌ Claude CLI not found: #{e.message}"
|
68
|
+
puts "\nPlease install Claude Code:"
|
69
|
+
puts " npm install -g @anthropic-ai/claude-code"
|
70
|
+
rescue ClaudeCode::ProcessError => e
|
71
|
+
puts "❌ Process error: #{e.message}"
|
72
|
+
puts "Exit code: #{e.exit_code}" if e.exit_code
|
73
|
+
puts "Stderr: #{e.stderr}" if e.stderr
|
74
|
+
rescue StandardError => e
|
75
|
+
puts "❌ Unexpected error: #{e.message}"
|
76
|
+
puts e.backtrace.first(5)
|
77
|
+
end
|
78
|
+
|
79
|
+
puts "\n" + "="*50
|
80
|
+
puts "JSONL Helper Methods Demonstration"
|
81
|
+
puts "="*50
|
82
|
+
|
83
|
+
puts "\n1. Using JSONLHelpers.create_user_message:"
|
84
|
+
helper_message = ClaudeCode::JSONLHelpers.create_user_message("What is Ruby?")
|
85
|
+
puts JSON.pretty_generate(helper_message)
|
86
|
+
|
87
|
+
puts "\n2. Creating multiple messages:"
|
88
|
+
messages = ClaudeCode::JSONLHelpers.create_conversation(
|
89
|
+
"Hello!",
|
90
|
+
"How are you?",
|
91
|
+
"Tell me about Ruby"
|
92
|
+
)
|
93
|
+
|
94
|
+
puts "Created #{messages.length} messages:"
|
95
|
+
messages.each_with_index do |msg, i|
|
96
|
+
puts "Message #{i + 1}: #{msg['message']['content'][0]['text']}"
|
97
|
+
end
|
98
|
+
|
99
|
+
puts "\n3. Format as JSONL string:"
|
100
|
+
jsonl_string = ClaudeCode::JSONLHelpers.format_messages_as_jsonl(messages)
|
101
|
+
puts jsonl_string
|
102
|
+
|
103
|
+
puts "\n✅ CLI equivalent example completed!"
|
104
|
+
puts "\nTo use this functionality:"
|
105
|
+
puts "1. Set ANTHROPIC_API_KEY environment variable"
|
106
|
+
puts "2. Use ClaudeCode.stream_json_query(messages)"
|
107
|
+
puts "3. Process streaming responses in real-time"
|
108
|
+
puts "4. Handle system, assistant, and result messages appropriately"
|
@@ -0,0 +1,166 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# MCP server configuration examples for Ruby Claude Code SDK
|
3
|
+
|
4
|
+
require_relative '../lib/claude_code'
|
5
|
+
|
6
|
+
def test_mcp_creator_ninja
|
7
|
+
claude_path = "/Users/admin/.claude/local/claude"
|
8
|
+
|
9
|
+
puts "=== Testing MCP Creator Ninja Server ==="
|
10
|
+
puts
|
11
|
+
|
12
|
+
# Method 1: Using the ergonomic mcp_servers parameter
|
13
|
+
puts "1. Using ergonomic mcp_servers parameter:"
|
14
|
+
|
15
|
+
mcp_servers = ClaudeCode.add_mcp_server(
|
16
|
+
"creator_ninja",
|
17
|
+
"https://mcp-creator-ninja-v1-4-0.mcp.soy/"
|
18
|
+
)
|
19
|
+
|
20
|
+
options = ClaudeCode::ClaudeCodeOptions.new(
|
21
|
+
max_turns: 1,
|
22
|
+
allowed_tools: ["mcp__creator_ninja__about"], # Allow the 'about' tool
|
23
|
+
system_prompt: "You are helpful. Use the MCP tools available to you."
|
24
|
+
)
|
25
|
+
|
26
|
+
begin
|
27
|
+
ClaudeCode.query(
|
28
|
+
prompt: "Use the about tool to tell me about the Creator Ninja MCP server",
|
29
|
+
options: options,
|
30
|
+
cli_path: claude_path,
|
31
|
+
mcp_servers: mcp_servers
|
32
|
+
).each do |message|
|
33
|
+
case message
|
34
|
+
when ClaudeCode::AssistantMessage
|
35
|
+
message.content.each do |block|
|
36
|
+
case block
|
37
|
+
when ClaudeCode::TextBlock
|
38
|
+
puts " Response: #{block.text}"
|
39
|
+
when ClaudeCode::ToolUseBlock
|
40
|
+
puts " Tool Used: #{block.name}"
|
41
|
+
puts " Tool Input: #{block.input}"
|
42
|
+
end
|
43
|
+
end
|
44
|
+
when ClaudeCode::ResultMessage
|
45
|
+
puts " Cost: $#{format('%.6f', message.total_cost_usd)}" if message.total_cost_usd
|
46
|
+
end
|
47
|
+
end
|
48
|
+
rescue => e
|
49
|
+
puts " Error: #{e.message}"
|
50
|
+
puts " Backtrace: #{e.backtrace.first(3).join("\n ")}"
|
51
|
+
end
|
52
|
+
|
53
|
+
puts "\n" + "="*60 + "\n"
|
54
|
+
|
55
|
+
# Method 2: Using options directly
|
56
|
+
puts "2. Using ClaudeCodeOptions directly:"
|
57
|
+
|
58
|
+
creator_ninja_server = ClaudeCode::McpHttpServerConfig.new(
|
59
|
+
url: "https://mcp-creator-ninja-v1-4-0.mcp.soy/"
|
60
|
+
)
|
61
|
+
|
62
|
+
options2 = ClaudeCode::ClaudeCodeOptions.new(
|
63
|
+
max_turns: 1,
|
64
|
+
mcp_servers: { "creator_ninja" => creator_ninja_server },
|
65
|
+
allowed_tools: ["mcp__creator_ninja__about"],
|
66
|
+
system_prompt: "You are helpful. Call the about tool to learn about yourself."
|
67
|
+
)
|
68
|
+
|
69
|
+
begin
|
70
|
+
ClaudeCode.query(
|
71
|
+
prompt: "What can you tell me about yourself using the about tool?",
|
72
|
+
options: options2,
|
73
|
+
cli_path: claude_path
|
74
|
+
).each do |message|
|
75
|
+
case message
|
76
|
+
when ClaudeCode::AssistantMessage
|
77
|
+
message.content.each do |block|
|
78
|
+
case block
|
79
|
+
when ClaudeCode::TextBlock
|
80
|
+
puts " Response: #{block.text}"
|
81
|
+
when ClaudeCode::ToolUseBlock
|
82
|
+
puts " Tool Used: #{block.name}"
|
83
|
+
puts " Tool Input: #{block.input}"
|
84
|
+
end
|
85
|
+
end
|
86
|
+
when ClaudeCode::ResultMessage
|
87
|
+
puts " Cost: $#{format('%.6f', message.total_cost_usd)}" if message.total_cost_usd
|
88
|
+
end
|
89
|
+
end
|
90
|
+
rescue => e
|
91
|
+
puts " Error: #{e.message}"
|
92
|
+
puts " Backtrace: #{e.backtrace.first(3).join("\n ")}"
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
def test_ultra_convenient_method
|
97
|
+
puts "=== Ultra-Convenient MCP Method ==="
|
98
|
+
puts
|
99
|
+
|
100
|
+
# The simplest way to use MCP - just specify server and tools
|
101
|
+
ClaudeCode.quick_mcp_query(
|
102
|
+
"Tell me about this MCP server using the about tool",
|
103
|
+
server_name: "ninja",
|
104
|
+
server_url: "https://mcp-creator-ninja-v1-4-0.mcp.soy/",
|
105
|
+
tools: "about" # Can be string or array
|
106
|
+
).each do |message|
|
107
|
+
if message.is_a?(ClaudeCode::AssistantMessage)
|
108
|
+
message.content.each do |block|
|
109
|
+
if block.is_a?(ClaudeCode::TextBlock)
|
110
|
+
puts "Response: #{block.text}"
|
111
|
+
elsif block.is_a?(ClaudeCode::ToolUseBlock)
|
112
|
+
puts "🔧 Tool: #{block.name}"
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
puts "\n" + "="*50 + "\n"
|
119
|
+
end
|
120
|
+
|
121
|
+
def show_mcp_usage_examples
|
122
|
+
puts "=== MCP Server Configuration Examples ==="
|
123
|
+
puts
|
124
|
+
puts "```ruby"
|
125
|
+
puts "# Method 1: Using ergonomic helper"
|
126
|
+
puts "mcp_servers = ClaudeCode.add_mcp_server("
|
127
|
+
puts " 'my_server', 'https://my-mcp-server.com/'"
|
128
|
+
puts ")"
|
129
|
+
puts
|
130
|
+
puts "ClaudeCode.query("
|
131
|
+
puts " prompt: 'Use my_server tools',"
|
132
|
+
puts " mcp_servers: mcp_servers,"
|
133
|
+
puts " options: ClaudeCodeOptions.new("
|
134
|
+
puts " allowed_tools: ['mcp__my_server__my_tool']"
|
135
|
+
puts " )"
|
136
|
+
puts ")"
|
137
|
+
puts
|
138
|
+
puts "# Method 2: Using configuration objects"
|
139
|
+
puts "options = ClaudeCode::ClaudeCodeOptions.new("
|
140
|
+
puts " mcp_servers: {"
|
141
|
+
puts " 'http_server' => ClaudeCode::McpHttpServerConfig.new("
|
142
|
+
puts " url: 'https://api.example.com'"
|
143
|
+
puts " ),"
|
144
|
+
puts " 'stdio_server' => ClaudeCode::McpStdioServerConfig.new("
|
145
|
+
puts " command: 'node',"
|
146
|
+
puts " args: ['my-mcp-server.js']"
|
147
|
+
puts " )"
|
148
|
+
puts " },"
|
149
|
+
puts " allowed_tools: ['mcp__http_server__api_call']"
|
150
|
+
puts ")"
|
151
|
+
puts
|
152
|
+
puts "# Method 3: Multiple servers with helper"
|
153
|
+
puts "servers = {}"
|
154
|
+
puts "servers.merge!(ClaudeCode.add_mcp_server('ninja', 'https://...'))"
|
155
|
+
puts "servers.merge!(ClaudeCode.add_mcp_server('local', 'node server.js'))"
|
156
|
+
puts "```"
|
157
|
+
puts
|
158
|
+
puts "="*60
|
159
|
+
puts
|
160
|
+
end
|
161
|
+
|
162
|
+
if __FILE__ == $0
|
163
|
+
show_mcp_usage_examples
|
164
|
+
test_ultra_convenient_method
|
165
|
+
test_mcp_creator_ninja
|
166
|
+
end
|
@@ -0,0 +1,111 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# Model specification examples for Ruby Claude Code SDK
|
3
|
+
|
4
|
+
require_relative '../lib/claude_code'
|
5
|
+
|
6
|
+
def test_model_specification
|
7
|
+
claude_path = "/Users/admin/.claude/local/claude"
|
8
|
+
|
9
|
+
puts "=== Model Specification Examples ==="
|
10
|
+
puts
|
11
|
+
|
12
|
+
# Example 1: Using model alias
|
13
|
+
puts "1. Using model alias 'sonnet':"
|
14
|
+
options1 = ClaudeCode::ClaudeCodeOptions.new(
|
15
|
+
model: "sonnet",
|
16
|
+
max_turns: 1
|
17
|
+
)
|
18
|
+
|
19
|
+
ClaudeCode.query(
|
20
|
+
prompt: "What's the capital of Japan?",
|
21
|
+
options: options1,
|
22
|
+
cli_path: claude_path
|
23
|
+
).each do |message|
|
24
|
+
if message.is_a?(ClaudeCode::AssistantMessage)
|
25
|
+
message.content.each do |block|
|
26
|
+
if block.is_a?(ClaudeCode::TextBlock)
|
27
|
+
puts " Answer: #{block.text}"
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
puts
|
34
|
+
|
35
|
+
# Example 2: Using full model name
|
36
|
+
puts "2. Using full model name 'claude-sonnet-4-20250514':"
|
37
|
+
options2 = ClaudeCode::ClaudeCodeOptions.new(
|
38
|
+
model: "claude-sonnet-4-20250514",
|
39
|
+
system_prompt: "Be very concise.",
|
40
|
+
max_turns: 1
|
41
|
+
)
|
42
|
+
|
43
|
+
ClaudeCode.query(
|
44
|
+
prompt: "What's 10 + 15?",
|
45
|
+
options: options2,
|
46
|
+
cli_path: claude_path
|
47
|
+
).each do |message|
|
48
|
+
if message.is_a?(ClaudeCode::AssistantMessage)
|
49
|
+
message.content.each do |block|
|
50
|
+
if block.is_a?(ClaudeCode::TextBlock)
|
51
|
+
puts " Answer: #{block.text}"
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
puts
|
58
|
+
|
59
|
+
# Example 3: No model specified (uses default)
|
60
|
+
puts "3. Using default model (no model specified):"
|
61
|
+
options3 = ClaudeCode::ClaudeCodeOptions.new(
|
62
|
+
max_turns: 1
|
63
|
+
)
|
64
|
+
|
65
|
+
ClaudeCode.query(
|
66
|
+
prompt: "What's the largest planet in our solar system?",
|
67
|
+
options: options3,
|
68
|
+
cli_path: claude_path
|
69
|
+
).each do |message|
|
70
|
+
if message.is_a?(ClaudeCode::AssistantMessage)
|
71
|
+
message.content.each do |block|
|
72
|
+
if block.is_a?(ClaudeCode::TextBlock)
|
73
|
+
puts " Answer: #{block.text}"
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def show_usage
|
81
|
+
puts "=== How to Specify Models in Ruby Claude SDK ==="
|
82
|
+
puts
|
83
|
+
puts "```ruby"
|
84
|
+
puts "# Using model alias (recommended)"
|
85
|
+
puts "options = ClaudeCode::ClaudeCodeOptions.new("
|
86
|
+
puts " model: 'sonnet', # or 'haiku', 'opus'"
|
87
|
+
puts " max_turns: 1"
|
88
|
+
puts ")"
|
89
|
+
puts
|
90
|
+
puts "# Using full model name"
|
91
|
+
puts "options = ClaudeCode::ClaudeCodeOptions.new("
|
92
|
+
puts " model: 'claude-sonnet-4-20250514',"
|
93
|
+
puts " system_prompt: 'You are helpful.'"
|
94
|
+
puts ")"
|
95
|
+
puts
|
96
|
+
puts "# Query with model"
|
97
|
+
puts "ClaudeCode.query("
|
98
|
+
puts " prompt: 'Your question here',"
|
99
|
+
puts " options: options,"
|
100
|
+
puts " cli_path: '/path/to/claude'"
|
101
|
+
puts ")"
|
102
|
+
puts "```"
|
103
|
+
puts
|
104
|
+
puts "="*50
|
105
|
+
puts
|
106
|
+
end
|
107
|
+
|
108
|
+
if __FILE__ == $0
|
109
|
+
show_usage
|
110
|
+
test_model_specification
|
111
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require_relative '../lib/claude_code'
|
5
|
+
|
6
|
+
def basic_example
|
7
|
+
puts "=== Basic Example ==="
|
8
|
+
|
9
|
+
ClaudeCode.query(prompt: "What is 2 + 2?").each do |message|
|
10
|
+
if message.is_a?(ClaudeCode::AssistantMessage)
|
11
|
+
message.content.each do |block|
|
12
|
+
if block.is_a?(ClaudeCode::TextBlock)
|
13
|
+
puts "Claude: #{block.text}"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
puts
|
19
|
+
end
|
20
|
+
|
21
|
+
def with_options_example
|
22
|
+
puts "=== With Options Example ==="
|
23
|
+
|
24
|
+
options = ClaudeCode::ClaudeCodeOptions.new(
|
25
|
+
system_prompt: "You are a helpful assistant that explains things simply.",
|
26
|
+
max_turns: 1
|
27
|
+
)
|
28
|
+
|
29
|
+
ClaudeCode.query(
|
30
|
+
prompt: "Explain what Ruby is in one sentence.",
|
31
|
+
options: options
|
32
|
+
).each do |message|
|
33
|
+
if message.is_a?(ClaudeCode::AssistantMessage)
|
34
|
+
message.content.each do |block|
|
35
|
+
if block.is_a?(ClaudeCode::TextBlock)
|
36
|
+
puts "Claude: #{block.text}"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
puts
|
42
|
+
end
|
43
|
+
|
44
|
+
def with_tools_example
|
45
|
+
puts "=== With Tools Example ==="
|
46
|
+
|
47
|
+
options = ClaudeCode::ClaudeCodeOptions.new(
|
48
|
+
allowed_tools: ["Read", "Write"],
|
49
|
+
system_prompt: "You are a helpful file assistant."
|
50
|
+
)
|
51
|
+
|
52
|
+
ClaudeCode.query(
|
53
|
+
prompt: "Create a file called hello.txt with 'Hello, World!' in it",
|
54
|
+
options: options
|
55
|
+
).each do |message|
|
56
|
+
if message.is_a?(ClaudeCode::AssistantMessage)
|
57
|
+
message.content.each do |block|
|
58
|
+
if block.is_a?(ClaudeCode::TextBlock)
|
59
|
+
puts "Claude: #{block.text}"
|
60
|
+
end
|
61
|
+
end
|
62
|
+
elsif message.is_a?(ClaudeCode::ResultMessage) && message.total_cost_usd && message.total_cost_usd > 0
|
63
|
+
puts "\nCost: $#{format('%.4f', message.total_cost_usd)}"
|
64
|
+
end
|
65
|
+
end
|
66
|
+
puts
|
67
|
+
end
|
68
|
+
|
69
|
+
def main
|
70
|
+
basic_example
|
71
|
+
with_options_example
|
72
|
+
with_tools_example
|
73
|
+
end
|
74
|
+
|
75
|
+
main if __FILE__ == $0
|