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
//! Base LLM trait for content generation.
//!
//! This module defines the [`BaseLlm`] trait, which provides a unified interface
//! for interacting with different Large Language Model providers (e.g., `OpenAI`, Anthropic).
//!
//! # Examples
//!
//! ```ignore
//! use radkit::models::{BaseLlm, Thread};
//!
//! async fn generate(llm: &dyn BaseLlm) {
//! let thread = Thread::from_user("Hello, world!");
//! let response = llm.generate_content(thread, None).await.unwrap();
//! println!("Response: {:?}", response);
//! }
//! ```
use Arc;
use crateAgentResult;
use crate;
use crateBaseToolset;
use crate::;
/// Base trait for Large Language Model implementations.
///
/// This trait provides a unified interface for generating content from LLM providers.
/// All implementations must be `Send + Sync` (or equivalent via `MaybeSend + MaybeSync`
/// for WASM compatibility) to support concurrent usage across async tasks.
///
/// # Thread Safety
///
/// Implementations must be safe to share across threads (when not targeting WASM).
/// The trait bounds `MaybeSend + MaybeSync` ensure this portability.
///
/// # Error Handling
///
/// The [`generate_content`](BaseLlm::generate_content) method returns [`AgentResult<LlmResponse>`],
/// which may contain errors including
/// - Network/API errors when communicating with the LLM provider
/// - Authentication/authorization failures
/// - Rate limiting errors
/// - Invalid request parameters
/// - Tool execution failures (when using toolsets)
///
/// Implementors should map provider-specific errors into appropriate [`AgentError`](crate::errors::AgentError) variants.
/// Extension trait providing ergonomic helpers for [`BaseLlm`].
///
/// This trait is automatically implemented for all types that implement [`BaseLlm`],
/// providing convenient methods that accept any type convertible to [`Thread`].
///
/// # Design Pattern
///
/// This follows the standard Rust extension trait pattern used throughout the ecosystem
/// (e.g., `Iterator` + `IteratorExt`, `AsyncRead` + `AsyncReadExt`). The core trait
/// remains object-safe while extension methods provide zero-cost ergonomic improvements.
///
/// # Examples
///
/// ```ignore
/// use radkit::models::{BaseLlm, BaseLlmExt};
///
/// async fn example(llm: &impl BaseLlm) -> Result<(), Box<dyn std::error::Error>> {
/// // All of these work thanks to Into<Thread> implementations:
/// let r1 = llm.generate("What is 2+2?", None).await?;
/// let r2 = llm.generate(String::from("Hello!"), None).await?;
/// let r3 = llm.generate(Thread::from_user("Explain"), None).await?;
///
/// println!("Answer: {}", r1.content().first_text().unwrap_or("No text"));
/// Ok(())
/// }
/// ```
/// Blanket implementation of [`BaseLlmExt`] for all [`BaseLlm`] implementors.
///
/// This ensures every type implementing `BaseLlm` automatically gains the ergonomic
/// `generate` method without any additional implementation work.
/// Blanket [`BaseLlm`] implementation for `Arc<dyn BaseLlm>`.
///
/// Allows passing a shared LLM handle directly anywhere `impl BaseLlm` is expected —
/// most importantly to [`LlmWorker::builder`] — without having to unwrap or clone the
/// inner concrete type:
///
/// ```ignore
/// // runtime.default_llm() returns Arc<dyn BaseLlm>
/// let worker = LlmWorker::<MyOutput>::builder(runtime.default_llm()).build();
/// ```