-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathstreaming_input_example.rb
More file actions
executable file
·204 lines (179 loc) · 5.49 KB
/
streaming_input_example.rb
File metadata and controls
executable file
·204 lines (179 loc) · 5.49 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
51
52
53
54
55
56
57
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
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
146
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
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'bundler/setup'
require 'claude_agent_sdk'
# Example 1: Streaming input from an array of messages
def example_array_streaming
puts "=" * 80
puts "Example 1: Streaming Input from Array"
puts "=" * 80
puts
messages = [
"Hello! I'm going to ask you a few questions.",
"What is 2 + 2?",
"What is the capital of France?",
"Thank you for your answers!"
]
# Create a streaming enumerator from the array
stream = ClaudeAgentSDK::Streaming.from_array(messages)
# Configure options
options = ClaudeAgentSDK::ClaudeAgentOptions.new(
max_turns: 10
)
# Query with streaming input
ClaudeAgentSDK.query(prompt: stream, options: options) do |message|
case message
when ClaudeAgentSDK::AssistantMessage
puts "\n[Assistant]:"
message.content.each do |block|
case block
when ClaudeAgentSDK::TextBlock
puts block.text
when ClaudeAgentSDK::ThinkingBlock
puts "[Thinking: #{block.thinking[0..100]}...]" if block.thinking
end
end
when ClaudeAgentSDK::UserMessage
content = message.content.is_a?(String) ? message.content : message.content.first&.text
puts "\n[User]: #{content}"
when ClaudeAgentSDK::ResultMessage
puts "\n[Result]:"
puts " Turns: #{message.num_turns}"
puts " Cost: $#{message.total_cost_usd}" if message.total_cost_usd
end
end
end
# Example 2: Streaming input with dynamic generation
def example_dynamic_streaming
puts "\n\n"
puts "=" * 80
puts "Example 2: Dynamic Streaming Input"
puts "=" * 80
puts
# Create a stream that generates messages dynamically
stream = Enumerator.new do |yielder|
# First message
yielder << ClaudeAgentSDK::Streaming.user_message(
"I'm going to send you a series of math problems."
)
# Generate math problems dynamically
3.times do |i|
a = rand(1..10)
b = rand(1..10)
yielder << ClaudeAgentSDK::Streaming.user_message(
"Problem #{i + 1}: What is #{a} + #{b}?"
)
end
# Final message
yielder << ClaudeAgentSDK::Streaming.user_message(
"Great! Thank you for solving these problems."
)
end
options = ClaudeAgentSDK::ClaudeAgentOptions.new(
max_turns: 15
)
ClaudeAgentSDK.query(prompt: stream, options: options) do |message|
case message
when ClaudeAgentSDK::AssistantMessage
message.content.each do |block|
if block.is_a?(ClaudeAgentSDK::TextBlock)
puts "[Assistant]: #{block.text}"
end
end
when ClaudeAgentSDK::ResultMessage
puts "\n[Completed in #{message.num_turns} turns]"
end
end
end
# Example 3: Streaming with session management
def example_session_streaming
puts "\n\n"
puts "=" * 80
puts "Example 3: Streaming with Session IDs"
puts "=" * 80
puts
# Create streams for different sessions
stream = Enumerator.new do |yielder|
# Session 1: Math questions
yielder << ClaudeAgentSDK::Streaming.user_message(
"What is 5 + 3?",
session_id: 'math'
)
yielder << ClaudeAgentSDK::Streaming.user_message(
"What is 10 * 2?",
session_id: 'math'
)
# Session 2: General questions
yielder << ClaudeAgentSDK::Streaming.user_message(
"What is the capital of Japan?",
session_id: 'geography'
)
end
options = ClaudeAgentSDK::ClaudeAgentOptions.new(
max_turns: 10
)
ClaudeAgentSDK.query(prompt: stream, options: options) do |message|
case message
when ClaudeAgentSDK::AssistantMessage
session = message.instance_variable_get(:@parent_tool_use_id) || 'default'
message.content.each do |block|
if block.is_a?(ClaudeAgentSDK::TextBlock)
puts "[Session #{session}] Assistant: #{block.text[0..80]}..."
end
end
when ClaudeAgentSDK::ResultMessage
puts "\n[All sessions completed]"
end
end
end
# Example 4: Custom enumerator with delays
def example_timed_streaming
puts "\n\n"
puts "=" * 80
puts "Example 4: Streaming with Delays (simulating user input)"
puts "=" * 80
puts
# Create a stream that simulates delayed user input
stream = Enumerator.new do |yielder|
messages = [
"Let's have a conversation.",
"Tell me a short joke.",
"That's funny! Tell me another one."
]
messages.each_with_index do |msg, i|
yielder << ClaudeAgentSDK::Streaming.user_message(msg)
puts "[Sent message #{i + 1}]"
sleep 0.1 if i < messages.length - 1 # Small delay between messages
end
end
options = ClaudeAgentSDK::ClaudeAgentOptions.new(
max_turns: 10
)
ClaudeAgentSDK.query(prompt: stream, options: options) do |message|
if message.is_a?(ClaudeAgentSDK::AssistantMessage)
message.content.each do |block|
puts "\n[Assistant]: #{block.text}" if block.is_a?(ClaudeAgentSDK::TextBlock)
end
end
end
end
# Run examples
if __FILE__ == $PROGRAM_NAME
begin
puts "Claude Agent SDK - Streaming Input Examples"
puts "==========================================="
puts
# Uncomment the examples you want to run:
example_array_streaming
# example_dynamic_streaming
# example_session_streaming
# example_timed_streaming
puts "\n\nAll examples completed successfully!"
rescue ClaudeAgentSDK::CLINotFoundError => e
puts "Error: #{e.message}"
puts "\nPlease install Claude Code to run these examples."
rescue StandardError => e
puts "Error: #{e.class} - #{e.message}"
puts e.backtrace.first(5).join("\n")
end
end