Skip to main content

yapi_core/
auth_guidance.rs

1//! What to tell the user when no model or credential is usable.
2//!
3//! Port of `packages/coding-agent/src/core/auth-guidance.ts` in pi `v1.0.0`.
4
5use crate::docs::Locations;
6
7/// The page on providers and signing in to them, pi's
8/// `join(getDocsPath(), "providers.md")`, where `docs` are.
9pub fn providers_doc(docs: &Locations) -> String {
10    format!("{}/providers.md", docs.pi_docs)
11}
12
13/// pi's `getProviderLoginHelp`.
14pub fn provider_login_help(docs: &Locations) -> String {
15    let docs = &docs.pi_docs;
16    format!(
17        "Use /login to log into a provider via OAuth or API key. See:\n  {docs}/providers.md\n  {docs}/models.md"
18    )
19}
20
21/// pi's `formatNoModelsAvailableMessage`.
22pub fn no_models_available(docs: &Locations) -> String {
23    format!("No models available. {}", provider_login_help(docs))
24}
25
26/// pi's `formatNoModelSelectedMessage`.
27pub fn no_model_selected(docs: &Locations) -> String {
28    format!(
29        "No model selected.\n\n{}\n\nThen use /model to select a model.",
30        provider_login_help(docs)
31    )
32}
33
34/// pi's `formatNoApiKeyFoundMessage`.
35pub fn no_api_key_found(provider: &str, docs: &Locations) -> String {
36    let provider = if provider == "unknown" {
37        "the selected model"
38    } else {
39        provider
40    };
41    format!(
42        "No API key found for {provider}.\n\n{}",
43        provider_login_help(docs)
44    )
45}
46
47#[cfg(test)]
48mod tests {
49    use super::*;
50
51    /// The help names pi's providers and models pages where `docs` are.
52    #[test]
53    fn help_names_doc_pages() {
54        let docs = Locations {
55            pi_docs: "/agent/docs/pi/docs".into(),
56            ..Locations::default()
57        };
58        assert_eq!(
59            provider_login_help(&docs),
60            "Use /login to log into a provider via OAuth or API key. See:\n  /agent/docs/pi/docs/providers.md\n  /agent/docs/pi/docs/models.md"
61        );
62        assert_eq!(providers_doc(&docs), "/agent/docs/pi/docs/providers.md");
63    }
64}