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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
use crate::client::Anthropic;
use crate::http::streaming::{StreamConfig, StreamRequestBuilder};
use crate::streaming::MessageStream;
use crate::types::errors::{AnthropicError, Result};
use crate::types::messages::*;
/// Messages API resource for interacting with Claude
pub struct MessagesResource<'a> {
client: &'a Anthropic,
}
impl<'a> MessagesResource<'a> {
/// Create a new Messages resource
pub fn new(client: &'a Anthropic) -> Self {
Self { client }
}
/// Create a message with Claude
///
/// Send a structured list of input messages with text and/or image content,
/// and Claude will generate the next message in the conversation.
///
/// # Example
///
/// ```rust,no_run
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// use agentik_sdk::{Anthropic, types::MessageCreateBuilder};
///
/// let client = Anthropic::from_env()?;
///
/// let message = client.messages().create(
/// MessageCreateBuilder::new("claude-3-5-sonnet-latest", 1024)
/// .user("Hello, Claude!")
/// .build()
/// ).await?;
///
/// println!("Claude responded: {:?}", message.content);
/// # Ok(())
/// # }
/// ```
pub async fn create(&self, params: MessageCreateParams) -> Result<Message> {
let url = self.client.http_client().build_url("/v1/messages");
let request = self
.client
.http_client()
.post(&url)
.json(¶ms)
.build()
.map_err(|e| AnthropicError::Connection {
message: e.to_string(),
})?;
let response = self.client.http_client().send(request).await?;
// Extract request ID from headers
let request_id = self.client.http_client().extract_request_id(&response);
let status = response.status().as_u16();
let body = response.text().await.map_err(|e| AnthropicError::from_status(status, format!(
"failed to read response body: {e}"
)))?;
let mut message: Message = serde_json::from_str(&body)
.map_err(|e| AnthropicError::from_status(status, format!(
"failed to parse response as JSON: {e}, body: {}",
body.chars().take(500).collect::<String>()
)))?;
message.request_id = request_id;
Ok(message)
}
/// Create a streaming message with Claude
///
/// Send a message request and receive a real-time stream of the response.
/// This allows you to process Claude's response as it's being generated.
///
/// # Example
///
/// ```ignore
/// use agentik_sdk::{Anthropic, MessageCreateBuilder};
/// use futures::StreamExt;
///
/// let client = Anthropic::from_env()?;
///
/// let stream = client.messages().create_stream(
/// MessageCreateBuilder::new("claude-3-5-sonnet-latest", 1024)
/// .user("Write a story about AI")
/// .stream(true)
/// .build()
/// ).await?;
///
/// // Option 1: Use callbacks
/// let final_message = stream
/// .on_text(|delta, _| print!("{}", delta))
/// .on_error(|error| eprintln!("Error: {}", error))
/// .final_message().await?;
///
/// // Option 2: Manual iteration
/// while let Some(event) = stream.next().await {
/// // Process each event as needed
/// }
/// ```
pub async fn create_stream(&self, mut params: MessageCreateParams) -> Result<MessageStream> {
// Ensure streaming is enabled
params.stream = Some(true);
// Create authorization header - use Bearer for most cases including custom gateways
let auth_header = format!("Bearer {}", self.client.config().api_key);
// Build the streaming request with proper authentication
let stream_builder = StreamRequestBuilder::new(
self.client.http_client().client().clone(),
self.client.config().base_url.clone(),
)
.header("Authorization", &auth_header)
.header("Content-Type", "application/json")
.header("anthropic-version", "2023-06-01")
.config(StreamConfig::default());
// Make the streaming request to get the real HTTP stream
let http_stream = stream_builder.post_stream("v1/messages", ¶ms).await?;
// Create MessageStream that processes the real HTTP stream events
let message_stream = MessageStream::from_http_stream(http_stream)?;
Ok(message_stream)
}
/// Create a streaming message using the builder pattern
///
/// This is a convenience method that provides an ergonomic API for creating streaming messages.
///
/// # Example
///
/// ```ignore
/// use agentik_sdk::Anthropic;
///
/// let client = Anthropic::from_env()?;
///
/// let final_message = client.messages()
/// .create_with_builder("claude-3-5-sonnet-latest", 1024)
/// .user("Write a poem about the ocean")
/// .system("You are a creative poet.")
/// .temperature(0.8)
/// .stream()
/// .await?
/// .on_text(|delta, _| print!("{}", delta))
/// .final_message()
/// .await?;
/// ```
pub async fn stream(&self, params: MessageCreateParams) -> Result<MessageStream> {
self.create_stream(params).await
}
/// Create a message using the builder pattern
///
/// This is a convenience method that provides an ergonomic API for creating messages.
///
/// # Example
///
/// ```rust,no_run
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// use agentik_sdk::Anthropic;
///
/// let client = Anthropic::from_env()?;
///
/// let message = client.messages()
/// .create_with_builder("claude-3-5-sonnet-latest", 1024)
/// .user("What is the capital of France?")
/// .system("You are a helpful geography assistant.")
/// .temperature(0.3)
/// .send()
/// .await?;
///
/// println!("Response: {:?}", message.content);
/// # Ok(())
/// # }
/// ```
pub fn create_with_builder(
&'a self,
model: impl Into<String>,
max_tokens: u32,
) -> MessageCreateBuilderWithClient<'a> {
MessageCreateBuilderWithClient {
resource: self,
builder: MessageCreateBuilder::new(model, max_tokens),
}
}
}
/// A message builder with a client reference for sending requests
pub struct MessageCreateBuilderWithClient<'a> {
resource: &'a MessagesResource<'a>,
builder: MessageCreateBuilder,
}
impl<'a> MessageCreateBuilderWithClient<'a> {
/// Add a message to the conversation
pub fn message(mut self, role: Role, content: impl Into<MessageContent>) -> Self {
self.builder = self.builder.message(role, content);
self
}
/// Add a user message
pub fn user(mut self, content: impl Into<MessageContent>) -> Self {
self.builder = self.builder.user(content);
self
}
/// Add an assistant message
pub fn assistant(mut self, content: impl Into<MessageContent>) -> Self {
self.builder = self.builder.assistant(content);
self
}
/// Set the system prompt
pub fn system(mut self, system: impl Into<String>) -> Self {
self.builder = self.builder.system(system);
self
}
/// Set the temperature
pub fn temperature(mut self, temperature: f32) -> Self {
self.builder = self.builder.temperature(temperature);
self
}
/// Set top_p
pub fn top_p(mut self, top_p: f32) -> Self {
self.builder = self.builder.top_p(top_p);
self
}
/// Set top_k
pub fn top_k(mut self, top_k: u32) -> Self {
self.builder = self.builder.top_k(top_k);
self
}
/// Set custom stop sequences
pub fn stop_sequences(mut self, stop_sequences: Vec<String>) -> Self {
self.builder = self.builder.stop_sequences(stop_sequences);
self
}
/// Enable streaming
pub fn stream(mut self, stream: bool) -> Self {
self.builder = self.builder.stream(stream);
self
}
/// Send the message request
pub async fn send(self) -> Result<Message> {
self.resource.create(self.builder.build()).await
}
/// Send the message request as a stream
///
/// This enables streaming mode and returns a MessageStream for real-time processing.
///
/// # Example
///
/// ```ignore
/// let stream = client.messages()
/// .create_with_builder("claude-3-5-sonnet-latest", 1024)
/// .user("Tell me a story")
/// .stream_send()
/// .await?;
///
/// let final_message = stream
/// .on_text(|delta, _| print!("{}", delta))
/// .final_message()
/// .await?;
/// ```
pub async fn stream_send(self) -> Result<MessageStream> {
let params = self.builder.stream(true).build();
self.resource.create_stream(params).await
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::types::messages::{ContentBlockParam, MessageContent};
#[test]
fn test_message_create_params_serialization() {
let params = MessageCreateBuilder::new("claude-3-5-sonnet-latest", 1024)
.user("Hello, world!")
.system("You are helpful")
.temperature(0.7)
.build();
let json = serde_json::to_value(¶ms).unwrap();
assert_eq!(json["model"], "claude-3-5-sonnet-latest");
assert_eq!(json["max_tokens"], 1024);
assert_eq!(json["messages"].as_array().unwrap().len(), 1);
assert_eq!(json["system"], "You are helpful");
// Handle floating point precision by checking if the value is close to 0.7
let temperature = json["temperature"].as_f64().unwrap();
assert!(
(temperature - 0.7).abs() < 0.001,
"Temperature should be close to 0.7, got {}",
temperature
);
}
#[test]
fn test_complex_message_content() {
let content = MessageContent::Blocks(vec![
ContentBlockParam::text("Here's an image:"),
ContentBlockParam::image_base64("image/jpeg", "base64data"),
]);
let params = MessageCreateBuilder::new("claude-3-5-sonnet-latest", 1024)
.user(content)
.build();
let json = serde_json::to_value(¶ms).unwrap();
let message_content = &json["messages"][0]["content"];
assert!(message_content.is_array());
assert_eq!(message_content.as_array().unwrap().len(), 2);
assert_eq!(message_content[0]["type"], "text");
assert_eq!(message_content[1]["type"], "image");
}
#[test]
fn test_multi_message_conversation() {
let params = MessageCreateBuilder::new("claude-3-5-sonnet-latest", 1024)
.user("Hello!")
.assistant("Hi there! How can I help you?")
.user("What's the weather like?")
.build();
assert_eq!(params.messages.len(), 3);
assert_eq!(params.messages[0].role, Role::User);
assert_eq!(params.messages[1].role, Role::Assistant);
assert_eq!(params.messages[2].role, Role::User);
}
}