letta 0.1.2

A robust Rust client for the Letta REST API
Documentation
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
//! # letta
//!
//! A Rust client library for the [Letta](https://letta.com) REST API, providing idiomatic Rust bindings
//! for building stateful AI agents with persistent memory and context.
//!
//! Unlike the Letta-provided TypeScript and Python libraries, this was not generated from the OpenAPI spec,
//! but implemented by hand (with substantial LLM assistance). As such it exposes things in slightly different,
//! mildly opinionated ways, and includes a number of Rust-oriented affordances.
//!
//! ## Features
//!
//! - **Pagination**: Automatic cursor-based pagination with `PaginatedStream`
//! - **Type Safety**: Comprehensive type definitions for all API requests/responses
//! - **Flexible Configuration**: Support for cloud and local deployments
//! - **Rich Error Handling**: Detailed error types
//! - **Well Tested**: Extensive test coverage with integration tests
//!
//! ## Installation
//!
//! Add this to your `Cargo.toml`:
//!
//! ```toml
//! [dependencies]
//! letta = "0.1.2"
//! ```
//!
//! ### CLI Installation
//!
//! The letta crate includes an optional CLI tool for interacting with Letta servers:
//!
//! ```bash
//! # Install from crates.io
//! cargo install letta --features cli
//!
//! # Or build from source
//! git clone https://github.com/orual/letta-rs
//! cd letta-rs
//! cargo install --path . --features cli
//! ```
//!
//! After installation, the `letta` command will be available in your PATH.
//!
//! ### CLI Configuration
//!
//! Set your API key (for cloud deployments):
//! ```bash
//! export LETTA_API_KEY=your-api-key
//! ```
//!
//! Or specify the base URL for local servers:
//! ```bash
//! export LETTA_BASE_URL=http://localhost:8283
//! ```
//!
//! ### CLI Usage Examples
//!
//! ```bash
//! # Check server health
//! letta health
//!
//! # List all agents
//! letta agent list
//!
//! # Create a new agent
//! letta agent create -n "My Assistant" -m letta/letta-free
//!
//! # Send a message to an agent (with streaming)
//! letta message send -a <agent-id> "Hello, how are you?"
//!
//! # View agent memory
//! letta memory view -a <agent-id>
//!
//! # Upload a document to a source
//! letta sources create -n "docs" -e letta/letta-free
//! letta sources files upload <source-id> -f document.pdf
//!
//! # Get help for any command
//! letta --help
//! letta agent --help
//! ```
//!
//! The CLI supports multiple output formats:
//! - `--output summary` (default) - Human-readable format
//! - `--output json` - JSON output for scripting
//! - `--output pretty` - Pretty-printed JSON
//!
//! ## Compatibility
//!
//! | letta client | letta server |
//! |--------------|--------------|
//! | 0.1.2        | 0.8.8        |
//! | 0.1.0-0.1.1  | 0.8.x        |
//!
//! ## Quick Start
//!
//! ```rust,no_run
//! use letta::{ClientConfig, LettaClient};
//! use letta::types::{CreateAgentRequest, AgentType};
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//!     // Create client for local Letta server
//!     let config = ClientConfig::new("http://localhost:8283")?;
//!     let client = LettaClient::new(config)?;
//!
//!     // Create an agent using builder pattern
//!     let agent_request = CreateAgentRequest::builder()
//!         .name("My Assistant")
//!         .agent_type(AgentType::MemGPT)
//!         .model("letta/letta-free")  // Shorthand for LLM config
//!         .embedding("letta/letta-free")  // Shorthand for embedding config
//!         .build();
//!
//!     let agent = client.agents().create(agent_request).await?;
//!     println!("Created agent: {}", agent.id);
//!
//!     Ok(())
//! }
//! ```
//!
//! ## Creating and Interacting with Agents
//!
//! ```rust,no_run
//! use letta::{LettaClient, types::*};
//! use futures::StreamExt;
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//!     let client = LettaClient::local()?;
//!
//!     // Create a new agent
//!     let agent_request = CreateAgentRequest {
//!         name: Some("Assistant".to_string()),
//!         agent_type: Some(AgentType::MemGPT),
//!         model: Some("letta/letta-free".to_string()),
//!         embedding: Some("letta/letta-free".to_string()),
//!         ..Default::default()
//!     };
//!
//!     let agent = client.agents().create(agent_request).await?;
//!     println!("Created agent: {}", agent.id);
//!
//!     // Send a simple message
//!     let request = CreateMessagesRequest {
//!         messages: vec![MessageCreate::user("Hello! Can you help me with Rust?")],
//!         ..Default::default()
//!     };
//!     
//!     // Or create a more complex message with multiple content parts
//!     let complex_message = MessageCreate {
//!         role: MessageRole::User,
//!         content: MessageCreateContent::ContentParts(vec![
//!             ContentPart::Text(TextContent {
//!                 text: "Here's an image that shows my error:".to_string(),
//!             }),
//!             ContentPart::Image(ImageContent {
//!                 image_url: ImageUrl {
//!                     url: "https://example.com/error-screenshot.png".to_string(),
//!                     detail: None,
//!                 },
//!             }),
//!         ]),
//!         name: Some("Developer".to_string()),
//!         ..Default::default()
//!     };
//!     
//!     let response = client
//!         .messages()
//!         .create(&agent.id, request)
//!         .await?;
//!
//!     // Process the response messages
//!     for message in response.messages {
//!         match &message {
//!             LettaMessageUnion::ReasoningMessage(m) => {
//!                 println!("Thinking: {}", m.reasoning);
//!             }
//!             LettaMessageUnion::ToolReturnMessage(t) => {
//!                 println!("Tool returned: {:?}", t.tool_return);
//!             }
//!             LettaMessageUnion::AssistantMessage(m) => {
//!                 println!("Assistant: {}", m.content);
//!             }
//!             _ => {} // Other message types
//!         }
//!     }
//!
//!     Ok(())
//! }
//! ```
//!
//! ## Memory Management
//!
//! ```rust,no_run
//! use letta::{LettaClient, types::*};
//! use std::str::FromStr;
//! use futures::StreamExt;
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//!     let client = LettaClient::local()?;
//!     let agent_id = LettaId::from_str("agent-00000000-0000-0000-0000-000000000000")?;
//!
//!     // Get a specific core memory block
//!     let block = client
//!         .memory()
//!         .get_core_memory_block(&agent_id, "human")
//!         .await?;
//!     
//!     println!("Current human memory: {}", block.value);
//!
//!     // Update core memory block
//!     let request = UpdateMemoryBlockRequest {
//!         value: Some("Name: Bob\nRole: Manager".to_string()),
//!         label: None,
//!         limit: None,
//!         name: None,
//!         preserve_on_migration: None,
//!         read_only: None,
//!         description: None,
//!         metadata: None,
//!     };
//!     let updated = client
//!         .memory()
//!         .update_core_memory_block(&agent_id, "human", request)
//!         .await?;
//!     
//!     println!("Updated memory block: {}", updated.id.as_ref().unwrap());
//!
//!     // Add to archival memory
//!     let archival_request = CreateArchivalMemoryRequest {
//!         text: "Important: Project deadline is next Friday".to_string(),
//!     };
//!     let memories = client
//!         .memory()
//!         .create_archival_memory(&agent_id, archival_request)
//!         .await?;
//!     
//!     println!("Inserted {} archival memory passages", memories.len());
//!
//!     // Search archival memory with semantic search
//!     let search_params = PaginationParams {
//!         limit: Some(5),
//!         ..Default::default()
//!     };
//!     let mut stream = client
//!         .memory()
//!         .archival_paginated(&agent_id, Some(search_params));
//!
//!     while let Some(result) = stream.next().await {
//!         let result = result?;
//!         println!("Found: {}", result.text);
//!     }
//!
//!     Ok(())
//! }
//! ```
//!
//! ## Working with Tools
//!
//! ```rust,no_run
//! use letta::{LettaClient, types::*};
//! use serde_json::json;
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//!     let client = LettaClient::local()?;
//!     let agent_id = "agent-00000000-0000-0000-0000-000000000000";
//!
//!     // Create a custom tool
//!     let tool = CreateToolRequest {
//!         description: Some("Get current weather for a location".to_string()),
//!         source_code: r#"
//! def get_weather(location: str) -> str:
//!     """Get weather for a location.
//!     
//!     Args:
//!         location: The location to get weather for
//!         
//!     Returns:
//!         Weather information as a string
//!     """
//!     return f"The weather in {location} is sunny and 72°F"
//! "#.to_string(),
//!         source_type: Some(SourceType::Python),
//!         json_schema: Some(json!({
//!             "name": "get_weather",
//!             "description": "Get current weather for a location",
//!             "parameters": {
//!                 "type": "object",
//!                 "properties": {
//!                     "location": {
//!                         "type": "string",
//!                         "description": "The location to get weather for"
//!                     }
//!                 },
//!                 "required": ["location"]
//!             }
//!         })),
//!         ..Default::default()
//!     };
//!
//!     let created_tool = client.tools().create(tool).await?;
//!
//!     // Tools are automatically available to agents after creation
//!     println!("Created tool: {}", created_tool.name);
//!
//!     Ok(())
//! }
//! ```
//!
//! ## Pagination Support
//!
//! ```rust,no_run
//! use letta::{LettaClient, types::*};
//! use futures::StreamExt;
//! use std::str::FromStr;
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//!     let client = LettaClient::local()?;
//!
//!     // Use pagination to handle large result sets
//!     let params = PaginationParams {
//!         limit: Some(10),
//!         ..Default::default()
//!     };
//!     let mut agent_stream = client
//!         .agents()
//!         .paginated(Some(params));
//!
//!     // Automatically fetches next pages as needed
//!     while let Some(agent) = agent_stream.next().await {
//!         let agent = agent?;
//!         println!("Agent: {} ({})", agent.name, agent.id);
//!     }
//!
//!     // Also works with archival memory
//!     let agent_id = LettaId::from_str("agent-00000000-0000-0000-0000-000000000000")?;
//!     let memory_params = PaginationParams {
//!         limit: Some(20),
//!         ..Default::default()
//!     };
//!     let mut memory_stream = client
//!         .memory()
//!         .archival_paginated(&agent_id, Some(memory_params));
//!
//!     while let Some(memory) = memory_stream.next().await {
//!         let memory = memory?;
//!         println!("Memory: {}", memory.text);
//!     }
//!
//!     Ok(())
//! }
//! ```
//!
//! ## Configuration
//!
//! ### Local Development Server
//!
//! ```rust,no_run
//! use letta::{ClientConfig, LettaClient};
//!
//! // No authentication required for local server
//! let config = ClientConfig::new("http://localhost:8283")?;
//! let client = LettaClient::new(config)?;
//! # Ok::<(), Box<dyn std::error::Error>>(())
//! ```
//!
//! ### Letta Cloud
//!
//! ```rust,no_run
//! use letta::{ClientConfig, LettaClient};
//! use letta::auth::AuthConfig;
//!
//! // Use API key for cloud deployment
//! let config = ClientConfig::new("https://api.letta.com")?
//!     .auth(AuthConfig::bearer("your-api-key"));
//! let client = LettaClient::new(config)?;
//! # Ok::<(), Box<dyn std::error::Error>>(())
//! ```
//!
//! ### Custom Headers
//!
//! ```rust,no_run
//! use letta::{ClientConfig, LettaClient};
//!
//! // Add custom headers like X-Project
//! let config = ClientConfig::new("http://localhost:8283")?
//!     .header("X-Project", "my-project")?;
//! let client = LettaClient::new(config)?;
//! # Ok::<(), Box<dyn std::error::Error>>(())
//! ```
//!
//! ## API Coverage
//!
//! ### Core APIs
//! - ✅ **Agents** - Create, update, delete, and manage AI agents
//! - ✅ **Messages** - Send messages and stream responses with SSE
//! - ✅ **Memory** - Manage core and archival memory with semantic search
//! - ✅ **Tools** - Register and manage agent tools (functions)
//! - ✅ **Sources** - Upload documents and manage knowledge sources
//! - ✅ **Blocks** - Manage memory blocks and persistent storage
//!
//! ### Advanced APIs
//! - ✅ **Groups** - Multi-agent conversations
//! - ✅ **Runs** - Execution tracking and debugging
//! - ✅ **Jobs** - Asynchronous job management
//! - ✅ **Batch** - Batch message processing
//! - ✅ **Templates** - Agent templates for quick deployment
//! - ✅ **Projects** - Project organization
//! - ✅ **Models** - LLM and embedding model configuration
//! - ✅ **Providers** - LLM provider management
//! - ✅ **Identities** - Identity and permissions management
//! - ✅ **Tags** - Tag-based organization
//! - ✅ **Telemetry** - Usage tracking and monitoring
//! - 🚧 **Voice** - Voice conversation support (beta)
//!
//! ## API Sections
//!
//! - [`agents`](crate::api::agents) - Agent management and lifecycle
//! - [`messages`](crate::api::messages) - Real-time messaging with streaming
//! - [`memory`](crate::api::memory) - Memory management (core, archival, blocks)
//! - [`tools`](crate::api::tools) - Tool management and execution
//! - [`sources`](crate::api::sources) - Document and data source management
//! - [`blocks`](crate::api::blocks) - Memory block operations
//! - [`groups`](crate::api::groups) - Multi-agent conversations
//! - [`jobs`](crate::api::jobs) - Asynchronous job management
//! - [`projects`](crate::api::projects) - Project organization
//! - [`templates`](crate::api::templates) - Agent templates
//! - [`runs`](crate::api::runs) - Execution tracking
//! - [`models`](crate::api::models) - Model configuration
//! - [`providers`](crate::api::providers) - LLM provider management
//! - [`identities`](crate::api::identities) - Identity management
//! - [`tags`](crate::api::tags) - Tag-based organization
//! - [`batch`](crate::api::batch) - Batch processing
//! - [`telemetry`](crate::api::telemetry) - Usage tracking
//! - [`voice`](crate::api::voice) - Voice conversations (beta)
//!
//! ## Error Handling
//!
//! All operations return [`Result<T, LettaError>`](crate::error::LettaError) with
//! comprehensive error information and suggestions via [`miette`].

#![deny(missing_docs)]
#![deny(rustdoc::broken_intra_doc_links)]
#![warn(clippy::all)]
#![warn(clippy::pedantic)]
#![warn(clippy::nursery)]
#![allow(clippy::module_name_repetitions)]

pub mod api;
pub mod auth;
pub mod client;
pub mod environment;
pub mod error;
pub mod pagination;
pub mod retry;
pub mod streaming;
pub mod types;
pub mod utils;

#[cfg(test)]
pub mod test_helpers;

#[cfg(feature = "cli")]
#[cfg_attr(docsrs, doc(cfg(feature = "cli")))]
pub mod cli;

// Re-export main types for convenience
pub use client::{ClientBuilder, ClientConfig, LettaClient};
pub use environment::LettaEnvironment;
pub use error::{ErrorContext, LettaError, LettaResult};
pub use types::*;

/// Maximum number of retries for API calls
pub const MAX_RETRIES: u32 = 3;

/// Convenience type alias for Results in this crate.
pub type Result<T> = std::result::Result<T, LettaError>;

// Re-export streaming types
pub use api::messages::{MessageStream, StreamingEvent};