Offers OpenRouter OAuth flow to the user if no API keys are found. Args: io: The InputOutput object for user interaction. analytics: The Analytics object for tracking events. Returns: True if authentication was successful, False otherwise.
(io, analytics)
| 77 | |
| 78 | |
| 79 | def offer_openrouter_oauth(io, analytics): |
| 80 | """ |
| 81 | Offers OpenRouter OAuth flow to the user if no API keys are found. |
| 82 | |
| 83 | Args: |
| 84 | io: The InputOutput object for user interaction. |
| 85 | analytics: The Analytics object for tracking events. |
| 86 | |
| 87 | Returns: |
| 88 | True if authentication was successful, False otherwise. |
| 89 | """ |
| 90 | # No API keys found - Offer OpenRouter OAuth |
| 91 | io.tool_output("OpenRouter provides free and paid access to many LLMs.") |
| 92 | # Use confirm_ask which handles non-interactive cases |
| 93 | if io.confirm_ask( |
| 94 | "Login to OpenRouter or create a free account?", |
| 95 | default="y", |
| 96 | ): |
| 97 | analytics.event("oauth_flow_initiated", provider="openrouter") |
| 98 | openrouter_key = start_openrouter_oauth_flow(io, analytics) |
| 99 | if openrouter_key: |
| 100 | # Successfully got key via OAuth, use the default OpenRouter model |
| 101 | # Ensure OPENROUTER_API_KEY is now set in the environment for later use |
| 102 | os.environ["OPENROUTER_API_KEY"] = openrouter_key |
| 103 | # Track OAuth success leading to model selection |
| 104 | analytics.event("oauth_flow_success") |
| 105 | return True |
| 106 | |
| 107 | # OAuth failed or was cancelled by user implicitly (e.g., closing browser) |
| 108 | # Error messages are handled within start_openrouter_oauth_flow |
| 109 | analytics.event("oauth_flow_failure") |
| 110 | io.tool_error("OpenRouter authentication did not complete successfully.") |
| 111 | # Fall through to the final error message |
| 112 | |
| 113 | return False |
| 114 | |
| 115 | |
| 116 | def select_default_model(args, io, analytics): |