-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathclient_example.rb
More file actions
50 lines (42 loc) · 1.33 KB
/
client_example.rb
File metadata and controls
50 lines (42 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
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
47
48
49
50
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'bundler/setup'
require 'claude_agent_sdk'
require 'async'
# Example: Interactive client usage
Async do
client = ClaudeAgentSDK::Client.new
begin
puts "Connecting to Claude..."
client.connect
puts "Connected!\n"
# First query
puts "=== Query 1: What is Ruby? ==="
client.query("What is Ruby programming language in one sentence?")
client.receive_response do |msg|
if msg.is_a?(ClaudeAgentSDK::AssistantMessage)
msg.content.each do |block|
puts block.text if block.is_a?(ClaudeAgentSDK::TextBlock)
end
elsif msg.is_a?(ClaudeAgentSDK::ResultMessage)
puts "\nCost: $#{msg.total_cost_usd}\n" if msg.total_cost_usd
end
end
# Second query
puts "\n=== Query 2: What is Python? ==="
client.query("What is Python programming language in one sentence?")
client.receive_response do |msg|
if msg.is_a?(ClaudeAgentSDK::AssistantMessage)
msg.content.each do |block|
puts block.text if block.is_a?(ClaudeAgentSDK::TextBlock)
end
elsif msg.is_a?(ClaudeAgentSDK::ResultMessage)
puts "\nCost: $#{msg.total_cost_usd}\n" if msg.total_cost_usd
end
end
ensure
puts "\nDisconnecting..."
client.disconnect
puts "Disconnected!"
end
end.wait