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
//! Group and multi-agent conversation API endpoints.
use crate::client::LettaClient;
use crate::error::LettaResult;
use crate::types::{
CreateMessagesRequest, Group, GroupCreate, GroupUpdate, GroupsListRequest, LettaId,
LettaResponse, MessageCreate,
};
use crate::{LettaError, LettaMessageUnion, MessageStream, StreamingEvent};
use eventsource_stream::Eventsource;
use futures::stream::StreamExt;
use reqwest::header::HeaderMap;
/// Group API operations.
#[derive(Debug)]
pub struct GroupApi<'a> {
client: &'a LettaClient,
}
impl<'a> GroupApi<'a> {
/// Create a new group API instance.
pub fn new(client: &'a LettaClient) -> Self {
Self { client }
}
/// List all groups.
///
/// # Arguments
///
/// * `params` - Optional query parameters for filtering and pagination
///
/// # Errors
///
/// Returns a [crate::error::LettaError] if the request fails or if the response cannot be parsed.
pub async fn list(&self, params: Option<GroupsListRequest>) -> LettaResult<Vec<Group>> {
self.client
.get_with_query("v1/groups", ¶ms.unwrap_or_default())
.await
}
/// Create a new group.
///
/// # Arguments
///
/// * `request` - The group creation request
///
/// # Errors
///
/// Returns a [crate::error::LettaError] if the request fails or if the response cannot be parsed.
pub async fn create(&self, request: GroupCreate) -> LettaResult<Group> {
self.client.post("v1/groups", &request).await
}
/// Get a specific group.
///
/// # Arguments
///
/// * `group_id` - The ID of the group to retrieve
///
/// # Errors
///
/// Returns a [crate::error::LettaError] if the request fails or if the response cannot be parsed.
pub async fn get(&self, group_id: &LettaId) -> LettaResult<Group> {
self.client.get(&format!("v1/groups/{}", group_id)).await
}
/// Update a group.
///
/// # Arguments
///
/// * `group_id` - The ID of the group to update
/// * `request` - The group update request
///
/// # Errors
///
/// Returns a [crate::error::LettaError] if the request fails or if the response cannot be parsed.
pub async fn update(&self, group_id: &LettaId, request: GroupUpdate) -> LettaResult<Group> {
self.client
.patch(&format!("v1/groups/{}", group_id), &request)
.await
}
/// Delete a group.
///
/// # Arguments
///
/// * `group_id` - The ID of the group to delete
///
/// # Errors
///
/// Returns a [crate::error::LettaError] if the request fails or if the response cannot be parsed.
pub async fn delete(&self, group_id: &LettaId) -> LettaResult<String> {
self.client.delete(&format!("v1/groups/{}", group_id)).await
}
/// Send a message to a group.
///
/// # Arguments
///
/// * `group_id` - The ID of the group to send the message to
/// * `messages` - The messages to send
///
/// # Errors
///
/// Returns a [crate::error::LettaError] if the request fails or if the response cannot be parsed.
pub async fn send_message(
&self,
group_id: &LettaId,
messages: Vec<MessageCreate>,
) -> LettaResult<LettaResponse> {
let request = CreateMessagesRequest {
messages,
..Default::default()
};
self.client
.post(&format!("v1/groups/{}/messages", group_id), &request)
.await
}
/// Process a user message and return the group’s responses. This endpoint accepts a message from a user and processes it through agents in the group based on the specified pattern. It will stream the steps of the response always, and stream the tokens if ‘stream_tokens’ is set to True.
///
/// This method uses Server-Sent Events (SSE) to stream the response, allowing
/// for real-time updates as the agent processes the messages.
///
/// # Arguments
///
/// * `group_id` - The ID of the agent group to send messages to
/// * `request` - The message creation request with messages and options
/// * `stream_tokens` - Whether to stream individual tokens (true) or complete messages (false)
///
/// # Returns
///
/// A stream of [`StreamingEvent`] items that can be consumed asynchronously.
///
/// # Errors
///
/// Returns a [crate::error::LettaError] if the request fails or if the response cannot be parsed.
pub async fn send_message_streaming(
&self,
group_id: &LettaId,
request: CreateMessagesRequest,
stream_tokens: bool,
) -> LettaResult<MessageStream> {
// Build the URL with streaming endpoint
let url = self
.client
.base_url()
.join(&format!("v1/groups/{}/messages/stream", group_id))?;
// Add query parameter for token streaming
let url = if stream_tokens {
url::Url::parse_with_params(url.as_str(), &[("stream_tokens", "true")])?
} else {
url
};
// Build headers
let mut headers = HeaderMap::new();
self.client.auth().apply_to_headers(&mut headers)?;
headers.insert("Content-Type", "application/json".parse().unwrap());
headers.insert("Accept", "text/event-stream".parse().unwrap());
// Send the request
let response = self
.client
.http()
.post(url)
.headers(headers)
.json(&request)
.send()
.await?;
// Check for HTTP errors
if !response.status().is_success() {
let status = response.status().as_u16();
let body = response.text().await?;
return Err(LettaError::from_response(status, body));
}
// Create the event stream
let stream = response
.bytes_stream()
.eventsource()
.filter_map(|result| async move {
match result {
Ok(event) => {
// Skip events without data
if event.data.is_empty() || event.data == "[DONE]" {
return None;
}
// Parse the event data
match serde_json::from_str::<StreamingEvent>(&event.data) {
Ok(parsed) => Some(Ok(parsed)),
Err(e) => {
// Skip parsing errors (like the Python SDK)
eprintln!("Failed to parse SSE event: {}", e);
None
}
}
}
Err(e) => Some(Err(LettaError::streaming(format!(
"SSE stream error: {}",
e
)))),
}
});
Ok(Box::pin(stream))
}
/// Update a message.
///
/// # Arguments
///
/// * `group_id` - The ID of the agent that owns the message
/// * `message_id` - The ID of the message to update
/// * `request` - The update request with the new message content
///
/// # Returns
///
/// The updated message as a [`LettaMessageUnion`].
///
/// # Errors
///
/// Returns a [crate::error::LettaError] if the request fails or if the response cannot be parsed.
pub async fn update_message(
&self,
group_id: &LettaId,
message_id: &LettaId,
request: crate::types::UpdateMessageRequest,
) -> LettaResult<LettaMessageUnion> {
self.client
.patch(
&format!("v1/groups/{}/messages/{}", group_id, message_id),
&request,
)
.await
}
/// Reset a group's message history.
///
/// # Arguments
///
/// * `group_id` - The ID of the agent whose messages to reset
/// * `add_default_initial_messages` - Whether to add default initial messages
///
/// # Errors
///
/// Returns a [crate::error::LettaError] if the request fails or if the response cannot be parsed.
pub async fn reset(
&self,
group_id: &LettaId,
add_default_initial_messages: Option<bool>,
) -> LettaResult<crate::types::AgentState> {
let mut body = serde_json::Map::new();
if let Some(add_default) = add_default_initial_messages {
body.insert(
"add_default_initial_messages".to_string(),
serde_json::Value::Bool(add_default),
);
}
self.client
.patch(&format!("v1/agents/{}/reset-messages", group_id), &body)
.await
}
/// Retrieve message history for an agent group
///
/// # Arguments
///
/// * `group_id` - The ID of the group to send the message to
/// * `messages` - The messages to send
///
/// # Errors
///
/// Returns a [crate::error::LettaError] if the request fails or if the response cannot be parsed.
pub async fn list_messages(
&self,
group_id: &LettaId,
messages: Vec<MessageCreate>,
) -> LettaResult<LettaResponse> {
let request = CreateMessagesRequest {
messages,
..Default::default()
};
self.client
.get_with_query(&format!("v1/groups/{}/messages", group_id), &request)
.await
}
}
/// Convenience methods for group operations.
impl LettaClient {
/// Get the group API for this client.
pub fn groups(&self) -> GroupApi {
GroupApi::new(self)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::client::ClientConfig;
#[test]
fn test_group_api_creation() {
let config = ClientConfig::new("http://localhost:8283").unwrap();
let client = LettaClient::new(config).unwrap();
let _api = GroupApi::new(&client);
}
}