lm15 1.0.0-rc.7

One request and response model for every LLM provider (OpenAI, Anthropic, Gemini, xAI, Groq, Bedrock, Azure, Vertex, local servers), with visible adaptations. Implements the lm15 contract.
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
//! `connect()`: get ready to make model requests (AUTH-23). Port of
//! lm15-python `lm15/interactive.py`.
//!
//! In order: says where connections are saved; offers the saved connections
//! and "connect another" (subscriptions first; an ambient environment key is
//! offered only as an explicit choice, never taken silently: R2/R3); runs the
//! chosen login or setup; lists the account's models (or takes `model`) and
//! asks; returns a [`BoundClient`] pinned to that connection and model. A
//! completed login is saved before the model picker runs (R6).
//!
//! Never: send a prompt, set a process-wide default, change another router,
//! fall back to a different account. Without a UI and without a terminal it
//! fails before reading any secret: a server attaches an `Auth` to its router.

use std::sync::Arc;

use super::bound::{model_choices, BoundClient, ModelSelection};
use super::engine::Cancel;
use super::manager::{Auth, LoginError, LoginOptions};
use super::terminal::TerminalUi;
use super::types::{
    AuthUi, Connection, LoginMethod, Notice, Prompt, ProviderDescriptor, SelectOption,
};
use crate::errors::{AuthOperation, Lm15Error};
use crate::router::RouterConfig;

const NEW: &str = "__new__";
const MANUAL: &str = "__manual__";

#[derive(Default)]
pub struct ConnectOptions {
    /// Skip the provider picker.
    pub provider: Option<String>,
    /// Skip the model picker.
    pub model: Option<String>,
    /// Default: `Auth::local(None)`.
    pub auth: Option<Auth>,
    /// Default: a terminal UI when stdin and stderr are a terminal.
    pub ui: Option<Arc<dyn AuthUi>>,
    /// Offer only models known to support it (`reasoning`, `vision`, `structured-output`).
    pub capability: Option<&'static str>,
    /// Terminal UI only: open authorization URLs in the browser.
    pub open_browser: bool,
    pub router_config: Option<RouterConfig>,
    /// Offer login methods that exist but have no live receipt yet, labelled as such.
    pub allow_unverified: bool,
}

fn interaction(message: &str) -> Lm15Error {
    AuthOperation::error(
        message,
        "interaction_required",
        "interaction",
        "not_committed",
        "provide_input",
    )
}

async fn ask(ui: &dyn AuthUi, prompt: Prompt) -> Result<String, LoginError> {
    ui.prompt(&prompt, &Cancel::new())
        .await
        .map_err(|_| LoginError::Cancelled)
}

pub async fn connect(options: ConnectOptions) -> Result<BoundClient, LoginError> {
    let ui: Arc<dyn AuthUi> = match options.ui.clone() {
        Some(ui) => ui,
        None => match TerminalUi::if_interactive() {
            Some(terminal) => Arc::new(if options.open_browser { terminal.open_browser() } else { terminal }),
            None => {
                return Err(interaction(
                    "connect() needs a person: no interactive terminal here and no ui was supplied. On a server, attach an Auth with saved connections (RouterConfig::new().auth(...)) instead of calling connect().",
                )
                .into())
            }
        },
    };
    let auth = match options.auth.clone() {
        Some(auth) => auth,
        None => Auth::local(None)?,
    };
    ui.notify(&Notice::info(format!(
        "Connections are saved privately in {}.",
        auth.store().description()
    )));
    let connection = choose_connection(&auth, &ui, &options).await?;
    let selection = choose_model(&auth, ui.as_ref(), &connection, &options).await?;
    ui.notify(&Notice::info(format!(
        "Ready: {} through {}.",
        selection.routed(),
        connection.label
    )));
    Ok(BoundClient::new(
        auth,
        selection,
        options.router_config.clone(),
    )?)
}

async fn choose_connection(
    auth: &Auth,
    ui: &Arc<dyn AuthUi>,
    options: &ConnectOptions,
) -> Result<Connection, LoginError> {
    let wanted = match &options.provider {
        Some(provider) => Some(auth.descriptor(provider)?.id),
        None => None,
    };
    let mut saved: Vec<Connection> = auth
        .connections()?
        .into_iter()
        .filter(|c| wanted.as_ref().is_none_or(|w| &c.provider == w))
        .collect();
    // Subscriptions first (R2): account connections before keys.
    saved.sort_by_key(|c| (c.kind != "account", c.provider.clone()));
    let mut usable = Vec::new();
    for connection in saved {
        let status = auth.status(&connection.provider)?;
        if matches!(status.usability, "ready" | "renewal_due" | "unknown") {
            usable.push(connection);
        }
    }
    if wanted.is_some() && usable.len() == 1 {
        return Ok(usable.remove(0));
    }
    let mut choices: Vec<SelectOption> = usable
        .iter()
        .map(|c| SelectOption::described(&c.id, &c.label, Some(format!("{} · saved", c.provider))))
        .collect();
    choices.push(SelectOption::new(NEW, "Connect another account or API key"));
    if choices.len() == 1 {
        return new_connection(auth, ui, wanted, options).await;
    }
    let answer = ask(
        ui.as_ref(),
        Prompt::select(
            "connection",
            "Use a saved connection, or connect another?",
            choices,
        ),
    )
    .await?;
    if answer == NEW {
        return new_connection(auth, ui, wanted, options).await;
    }
    usable.into_iter().find(|c| c.id == answer).ok_or_else(|| {
        AuthOperation::error(
            "the UI answered with an unknown connection id",
            "invalid_login_state",
            "interaction",
            "not_committed",
            "select_connection",
        )
        .into()
    })
}

async fn new_connection(
    auth: &Auth,
    ui: &Arc<dyn AuthUi>,
    provider: Option<String>,
    options: &ConnectOptions,
) -> Result<Connection, LoginError> {
    let provider = match provider {
        Some(provider) => provider,
        None => {
            let mut descriptors: Vec<ProviderDescriptor> = auth
                .providers()
                .into_iter()
                .filter(|d| d.methods.iter().any(|m| m.availability != "unavailable"))
                .collect();
            let subscription = |d: &ProviderDescriptor| {
                !d.methods
                    .iter()
                    .any(|m| m.subscription && m.availability == "supported")
            };
            descriptors.sort_by_key(|d| (subscription(d), d.label.to_lowercase()));
            let choices = descriptors
                .iter()
                .map(|d| {
                    SelectOption::described(
                        &d.id,
                        &d.label,
                        (d.service != d.label).then(|| d.service.clone()),
                    )
                })
                .collect();
            ask(
                ui.as_ref(),
                Prompt::select("provider", "Which provider?", choices),
            )
            .await?
        }
    };
    let descriptor = auth.descriptor(&provider)?;
    let existing = auth.status(&descriptor.id)?.connection;
    let method = choose_method(ui.as_ref(), &descriptor, options, auth).await?;
    let mut replace = None;
    if let Some(existing) = existing {
        let answer = ask(
            ui.as_ref(),
            Prompt::select(
                "replace",
                &format!(
                    "{} already has a saved connection ({}).",
                    descriptor.label, existing.label
                ),
                vec![
                    SelectOption::new("keep", "Keep it"),
                    SelectOption::new("replace", "Replace it"),
                ],
            ),
        )
        .await?;
        if answer == "keep" {
            return Ok(existing);
        }
        replace = Some(existing.id);
    }
    if method.flow == "form" || method.flow == "source_recipe" {
        let mut answers = std::collections::BTreeMap::new();
        for field in &method.fields {
            let value = if field.kind == "select" && field.options.len() == 1 {
                field.options[0].id.clone()
            } else if field.kind == "select" {
                ask(
                    ui.as_ref(),
                    Prompt::select(&field.id, &field.label, field.options.clone()),
                )
                .await?
            } else if field.kind == "secret" {
                ask(
                    ui.as_ref(),
                    Prompt::Secret {
                        field_id: field.id.clone(),
                        label: field.label.clone(),
                    },
                )
                .await?
            } else {
                ask(ui.as_ref(), Prompt::text(&field.id, &field.label)).await?
            };
            answers.insert(field.id.clone(), value);
        }
        return Ok(auth
            .configure(
                &descriptor.id,
                &method.id,
                answers,
                Default::default(),
                replace.as_deref(),
            )
            .await?);
    }
    let mut login = LoginOptions::new(Arc::clone(ui)).method(method.id.clone());
    login.replace = replace;
    login.allow_unverified = options.allow_unverified;
    auth.login(&descriptor.id, login).await
}

async fn choose_method(
    ui: &dyn AuthUi,
    descriptor: &ProviderDescriptor,
    options: &ConnectOptions,
    auth: &Auth,
) -> Result<LoginMethod, LoginError> {
    let mut methods: Vec<&LoginMethod> = descriptor
        .methods
        .iter()
        .filter(|m| {
            m.availability == "supported"
                || (options.allow_unverified && m.availability == "unverified")
        })
        .collect();
    if methods.is_empty() {
        return Err(AuthOperation::error(
            format!("{}: no login method is available here", descriptor.id),
            "method_unavailable",
            "discovery",
            "not_committed",
            "choose_method",
        )
        .into());
    }
    // Subscriptions first; an ambient key is offered, never assumed (R2).
    methods.sort_by_key(|m| (!m.subscription, m.kind != "account"));
    let mut choices = Vec::new();
    for method in &methods {
        let mut note = method.billing_note.clone();
        if method.availability == "unverified" {
            note = Some(format!(
                "UNVERIFIED — {}",
                method.reason.clone().unwrap_or_default()
            ));
        }
        if method.id == "env" {
            let set: Vec<&String> = method
                .fields
                .first()
                .map(|f| {
                    f.options
                        .iter()
                        .map(|o| &o.id)
                        .filter(|name| auth.env_value(name).is_some_and(|v| !v.is_empty()))
                        .collect()
                })
                .unwrap_or_default();
            let Some(first) = set.first() else { continue }; // nothing to offer
            note = Some(format!(
                "${first} is set in this environment; using it is your explicit choice"
            ));
        }
        choices.push(SelectOption::described(&method.id, &method.label, note));
    }
    let chosen = if choices.len() == 1 {
        choices[0].id.clone()
    } else {
        ask(
            ui,
            Prompt::select(
                "method",
                &format!("How do you want to connect to {}?", descriptor.label),
                choices,
            ),
        )
        .await?
    };
    descriptor.method(&chosen).cloned().ok_or_else(|| {
        AuthOperation::error(
            "the UI answered with an unknown method id",
            "invalid_login_state",
            "interaction",
            "not_committed",
            "choose_method",
        )
        .into()
    })
}

async fn choose_model(
    auth: &Auth,
    ui: &dyn AuthUi,
    connection: &Connection,
    options: &ConnectOptions,
) -> Result<ModelSelection, LoginError> {
    let selection = |model: String| ModelSelection {
        provider: connection.provider.clone(),
        model,
        connection_id: connection.id.clone(),
        identity_generation: connection.identity_generation.clone(),
    };
    if let Some(model) = &options.model {
        return Ok(selection(model.clone()));
    }
    let mut note = None;
    let choices = match model_choices(
        auth,
        &connection.provider,
        options.capability,
        false,
        options.router_config.clone(),
    )
    .await
    {
        Ok(choices) => {
            note = Some("listed by your account just now".to_string());
            choices
        }
        Err(error @ Lm15Error::AuthOperationError(_)) => return Err(error.into()),
        Err(error) => {
            // The catalog is a convenience: say why it is missing, do not pretend.
            ui.notify(&Notice::info(format!(
                "Could not list models for {} ({}); type a model id.",
                connection.provider,
                error.class_name()
            )));
            Vec::new()
        }
    };
    let mut select: Vec<SelectOption> = choices
        .iter()
        .map(|c| SelectOption::described(&c.model, &c.model, note.clone()))
        .collect();
    select.push(SelectOption::new(
        MANUAL,
        "Type a model id (not verified against your account)",
    ));
    if options.capability.is_some() && choices.is_empty() {
        ui.notify(&Notice::info(format!(
            "No model in the list is known to support {:?}; you can still type one.",
            options.capability.unwrap_or_default()
        )));
    }
    let mut answer = ask(
        ui,
        Prompt::select(
            "model",
            &format!("Which {} model?", connection.provider),
            select,
        ),
    )
    .await?;
    if answer == MANUAL {
        answer = ask(ui, Prompt::text("model", "Model id"))
            .await?
            .trim()
            .to_string();
        if answer.is_empty() {
            return Err(interaction("no model id given").into());
        }
    }
    Ok(selection(answer))
}