| 1 | //! Provider switching: flip between DeepSeek, hosted providers, and self-hosted |
| 2 | //! OpenAI-compatible DeepSeek V4 servers at runtime. |
| 3 | //! |
| 4 | //! `/provider` with no args opens the picker modal (#52). `/provider <name>` |
| 5 | //! keeps the v0.6.6 CLI form for muscle-memory + scripted use. |
| 6 | |
| 7 | use codewhale_command_contract::handler::{CommandCapabilities, CommandContexts, CommandHandler}; |
| 8 | use codewhale_command_contract::metadata::{CommandInfo, RegisterCommand}; |
| 9 | use codewhale_command_contract::model_setup::ProviderAction; |
| 10 | use codewhale_command_contract::model_setup::resolve_action; |
| 11 | use codewhale_command_contract::outcome::CommandResult; |
| 12 | |
| 13 | const COMMAND_INFO: CommandInfo = CommandInfo { |
| 14 | name: "provider", |
| 15 | aliases: &[], |
| 16 | usage: "/provider [setup [name]|name [model]]", |
| 17 | description_key: "cmd_provider_description", |
| 18 | }; |
| 19 | |
| 20 | pub struct ProviderCmd; |
| 21 | |
| 22 | impl<A: for<'a> TryFrom<ProviderAction<'a>, Error = String>> RegisterCommand<CommandResult<A>> |
| 23 | for ProviderCmd |
| 24 | { |
| 25 | fn info() -> &'static CommandInfo { |
| 26 | &COMMAND_INFO |
| 27 | } |
| 28 | fn handler() -> CommandHandler<CommandResult<A>> { |
| 29 | CommandHandler::Contextual { |
| 30 | capabilities: CommandCapabilities::PROVIDER_ROUTES, |
| 31 | handler: provider, |
| 32 | } |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | fn provider<A: for<'a> TryFrom<ProviderAction<'a>, Error = String>>( |
| 37 | mut contexts: CommandContexts<'_>, |
| 38 | arg: Option<&str>, |
| 39 | ) -> CommandResult<A> { |
| 40 | let mut words = arg.unwrap_or_default().split_whitespace(); |
| 41 | let Some(name) = words.next() else { |
| 42 | return resolve_action(ProviderAction::OpenPicker, None); |
| 43 | }; |
| 44 | let model = words.next(); |
| 45 | if name.eq_ignore_ascii_case("setup") { |
| 46 | return resolve_action(ProviderAction::Setup(model), None); |
| 47 | } |
| 48 | let Some(routes) = contexts.take_provider_routes() else { |
| 49 | return CommandResult::error("Command capability unavailable: provider routes"); |
| 50 | }; |
| 51 | if name.eq_ignore_ascii_case("fallback") { |
| 52 | return provider_fallback(routes, model); |
| 53 | } |
| 54 | match routes.resolve_switch(name, model) { |
| 55 | Ok(selection) if selection.already_active => { |
| 56 | CommandResult::message(format!("Already on provider: {}", selection.id.0)) |
| 57 | } |
| 58 | Ok(selection) => resolve_action( |
| 59 | ProviderAction::Switch { |
| 60 | provider: selection.id, |
| 61 | model: selection.model, |
| 62 | }, |
| 63 | None, |
| 64 | ), |
| 65 | Err(reason) => CommandResult::error(reason), |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | fn provider_fallback<A: for<'a> TryFrom<ProviderAction<'a>, Error = String>>( |
| 70 | routes: &dyn codewhale_command_contract::model_setup::CommandProviderRoutesContext, |
| 71 | subcommand: Option<&str>, |
| 72 | ) -> CommandResult<A> { |
| 73 | match subcommand { |
| 74 | Some("reset") => { |
| 75 | let Some((_, primary, _)) = routes.fallback().entries.into_iter().next() else { |
| 76 | return CommandResult::message( |
| 77 | "No fallback providers configured. Add `fallback_providers` to your config.", |
| 78 | ); |
| 79 | }; |
| 80 | let message = format!("Fallback chain reset to primary provider: {}.", primary.0); |
| 81 | resolve_action( |
| 82 | ProviderAction::Switch { |
| 83 | provider: primary, |
| 84 | model: None, |
| 85 | }, |
| 86 | Some(message), |
| 87 | ) |
| 88 | } |
| 89 | Some(other) => CommandResult::error(format!( |
| 90 | "Unknown fallback command '{other}'. Usage: /provider fallback [reset]" |
| 91 | )), |
| 92 | None => { |
| 93 | let fallback = routes.fallback(); |
| 94 | let entries = fallback.entries; |
| 95 | if entries.is_empty() { |
| 96 | return CommandResult::message( |
| 97 | "No fallback providers configured. Add `fallback_providers` to your config.", |
| 98 | ); |
| 99 | } |
| 100 | |
| 101 | let mut lines = vec![ |
| 102 | format!("Current provider: {}", fallback.current), |
| 103 | "Fallback chain:".to_string(), |
| 104 | ]; |
| 105 | for (index, provider, is_current) in entries { |
| 106 | let role = if index == 0 { "primary" } else { "fallback" }; |
| 107 | let marker = if is_current { " <- current" } else { "" }; |
| 108 | lines.push(format!(" [{index}] {} ({role}){marker}", provider.0)); |
| 109 | } |
| 110 | if let Some(reason) = fallback.last_reason { |
| 111 | lines.push(format!("Last fallback: {reason}")); |
| 112 | } |
| 113 | lines.push("Use `/provider fallback reset` to return to the primary provider.".into()); |
| 114 | CommandResult::message(lines.join("\n")) |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 |