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
//! # Configuration Helpers for the Open Agent SDK
//!
//! This module provides convenience utilities for configuring connections to different
//! local LLM server providers. It simplifies the process of setting base URLs and model names
//! by providing well-known defaults and environment variable support.
//!
//! ## Supported Providers
//!
//! - **LM Studio**: Popular local model server with GUI
//! - **Ollama**: Command-line focused local model server
//! - **llama.cpp**: C++ inference engine with server mode
//! - **vLLM**: High-performance inference server
//!
//! ## Environment Variables
//!
//! - `OPEN_AGENT_BASE_URL`: Override base URL for any provider
//! - `OPEN_AGENT_MODEL`: Override model name (when prefer_env is true)
//!
//! ## Examples
//!
//! ```rust
//! use open_agent::{Provider, get_base_url, get_model, AgentOptions};
//!
//! // Quick setup with provider defaults
//! let url = get_base_url(Some(Provider::Ollama), None);
//! let model = get_model(Some("llama3:8b"), false);
//!
//! // Build options
//! let options = AgentOptions::builder()
//! .system_prompt("You are a helpful assistant")
//! .model(model.unwrap())
//! .base_url(&url)
//! .build()
//! .unwrap();
//! ```
use env;
use FromStr;
// ============================================================================
// PROVIDER ENUM
// ============================================================================
/// Enum representing supported local LLM server providers.
///
/// Each provider has a default base URL where its API server typically runs.
/// These are convenience shortcuts to avoid hardcoding URLs in application code.
///
/// ## Provider Details
///
/// | Provider | Default URL | Port | Description |
/// |----------|-------------|------|-------------|
/// | LMStudio | http://localhost:1234/v1 | 1234 | GUI-based local server |
/// | Ollama | http://localhost:11434/v1 | 11434 | CLI-focused server |
/// | LlamaCpp | http://localhost:8080/v1 | 8080 | C++ inference engine |
/// | VLLM | http://localhost:8000/v1 | 8000 | High-performance server |
///
/// All providers implement the OpenAI-compatible API standard, making them
/// interchangeable from the SDK's perspective.
// ============================================================================
// FROMSTR IMPLEMENTATION FOR PARSING
// ============================================================================
// ============================================================================
// CONFIGURATION HELPER FUNCTIONS
// ============================================================================
/// Get the base URL for API requests with environment variable support.
///
/// This function implements a priority-based resolution strategy for determining
/// the API base URL, making it easy to override defaults during development or deployment.
///
/// ## Resolution Priority
///
/// 1. **Environment Variable**: `OPEN_AGENT_BASE_URL` (highest priority)
/// 2. **Provider Default**: The provider's default URL if specified
/// 3. **Fallback Parameter**: Explicit fallback value
/// 4. **Ultimate Default**: LM Studio's default URL (http://localhost:1234/v1)
///
/// ## Use Cases
///
/// - **Development**: Use provider defaults for quick setup
/// - **Testing**: Override with environment variable to point to test server
/// - **Production**: Specify explicit fallback for reliability
///
/// # Arguments
///
/// * `provider` - Optional provider enum to use its default URL
/// * `fallback` - Optional explicit fallback URL string
///
/// # Returns
///
/// The resolved base URL as a `String`. Always returns a value (never None).
///
/// # Examples
///
/// ```rust,no_run
/// use open_agent::{get_base_url, Provider};
///
/// // Use Ollama's default (http://localhost:11434/v1)
/// let url = get_base_url(Some(Provider::Ollama), None);
///
/// // With explicit fallback
/// let url = get_base_url(None, Some("http://localhost:1234/v1"));
///
/// // Override via environment (takes precedence over everything)
/// // SAFETY: This is a doctest example showing how env vars work
/// unsafe { std::env::set_var("OPEN_AGENT_BASE_URL", "http://custom-server:8080/v1"); }
/// let url = get_base_url(Some(Provider::Ollama), None);
/// // Returns "http://custom-server:8080/v1" despite provider being set
/// ```
/// Get the model name with optional environment variable override.
///
/// This function provides flexible model name resolution with opt-in environment
/// variable support. Unlike `get_base_url`, environment variable checking is
/// controlled by the `prefer_env` parameter.
///
/// ## Resolution Priority
///
/// If `prefer_env` is `true`:
/// 1. **Environment Variable**: `OPEN_AGENT_MODEL` (if set)
/// 2. **Fallback Parameter**: Explicit fallback value
///
/// If `prefer_env` is `false`:
/// 1. **Fallback Parameter**: Explicit fallback value only
///
/// ## Why Optional Environment Override?
///
/// Model names are often specified explicitly in code for consistency across
/// environments. The `prefer_env` flag gives you control over whether to
/// allow environment overrides.
///
/// # Arguments
///
/// * `fallback` - Optional explicit model name
/// * `prefer_env` - Whether to check environment variable first
///
/// # Returns
///
/// `Some(String)` if a model name was found, `None` if no model specified
///
/// # Examples
///
/// ```rust,no_run
/// use open_agent::get_model;
///
/// // Use explicit model name, allow environment override
/// let model = get_model(Some("llama3:8b"), true);
///
/// // Force specific model (ignore environment)
/// let model = get_model(Some("qwen2.5-32b"), false);
///
/// // Try environment only
/// let model = get_model(None, true);
/// // Returns Some(model) if OPEN_AGENT_MODEL is set, None otherwise
/// ```
// ============================================================================
// TESTS
// ============================================================================