| 1 | //! Shared conversion between the config-group contract actions and the TUI |
| 2 | //! `AppAction` vocabulary (FEAT-028 D5). |
| 3 | //! |
| 4 | //! The portable handlers and the movable config closure depend only on |
| 5 | //! `codewhale-command-contract`. This adapter module is host-owned: it maps the |
| 6 | //! unified `ConfigAction` to the concrete `AppAction` the event loop applies, |
| 7 | //! and back for the host helpers that still return a TUI `CommandResult`. |
| 8 | //! |
| 9 | |
| 10 | use crate::tui::app::AppAction; |
| 11 | use codewhale_command_contract::config::{ |
| 12 | CommandCapabilityState, CommandCompactionConfig, CommandCompletionSound, |
| 13 | CommandNotificationCondition, CommandNotificationEvent, CommandNotificationMethod, |
| 14 | CommandNotificationUpdate, CommandSearchProvider, CommandSubagentCompletion, |
| 15 | CommandSubagentRuntimeConfig, |
| 16 | }; |
| 17 | use codewhale_command_contract::outcome::ConfigAction; |
| 18 | use codewhale_command_contract::types::{CommandMode, CommandProviderId}; |
| 19 | use codewhale_config::AppMode; |
| 20 | use codewhale_config::ProviderId; |
| 21 | use codewhale_config::notifications::{ |
| 22 | CompletionSound, NotificationCondition, NotificationConfigUpdate, NotificationEvent, |
| 23 | NotificationMethod, SubagentCompletionNotification, |
| 24 | }; |
| 25 | use codewhale_config::route::CapabilityState; |
| 26 | use codewhale_engine::compaction::CompactionConfig; |
| 27 | use codewhale_runtime::config::SearchProvider; |
| 28 | |
| 29 | /// Convert a portable config action to the concrete TUI action it requests. |
| 30 | pub(crate) fn config_action_to_app_action(action: ConfigAction) -> AppAction { |
| 31 | match action { |
| 32 | ConfigAction::PermissionRulesChanged => AppAction::PermissionRulesChanged, |
| 33 | ConfigAction::ApproveAutoReviewRetry { target } => { |
| 34 | AppAction::ApproveAutoReviewRetry { target } |
| 35 | } |
| 36 | ConfigAction::OpenModelConnectionSetup => AppAction::OpenModelConnectionSetup, |
| 37 | ConfigAction::StartAccountConnect => AppAction::StartAccountConnect, |
| 38 | ConfigAction::StartAccountLogin => AppAction::StartAccountLogin, |
| 39 | ConfigAction::StartAccountLogout => AppAction::StartAccountLogout, |
| 40 | ConfigAction::StartXaiDeviceLogin => AppAction::StartXaiDeviceLogin, |
| 41 | ConfigAction::StartChatgptPkceLogin => AppAction::StartChatgptPkceLogin, |
| 42 | ConfigAction::StartChatgptRevoke => AppAction::StartChatgptRevoke, |
| 43 | ConfigAction::StartOrcarouterPkceLogin => AppAction::StartOrcarouterPkceLogin, |
| 44 | ConfigAction::StartOrcarouterRevoke => AppAction::StartOrcarouterRevoke, |
| 45 | ConfigAction::OpenConfigView => AppAction::OpenConfigView, |
| 46 | ConfigAction::OpenModePicker => AppAction::OpenModePicker, |
| 47 | ConfigAction::OpenPlanArtifact { arg } => AppAction::OpenPlanArtifact { arg }, |
| 48 | ConfigAction::SetConversationMode { id } => AppAction::SetConversationMode { id }, |
| 49 | ConfigAction::OpenThemePicker => AppAction::OpenThemePicker, |
| 50 | ConfigAction::OpenStatusPicker => AppAction::OpenStatusPicker, |
| 51 | ConfigAction::OpenProviderPicker => AppAction::OpenProviderPicker, |
| 52 | ConfigAction::StartPluginLogin { provider } => AppAction::StartPluginLogin { provider }, |
| 53 | ConfigAction::StartPluginLogout { provider } => AppAction::StartPluginLogout { provider }, |
| 54 | ConfigAction::SetWorkspaceTrust { trusted, save } => { |
| 55 | AppAction::SetWorkspaceTrust { trusted, save } |
| 56 | } |
| 57 | ConfigAction::ModeChanged(mode) => AppAction::ModeChanged(to_app_mode(mode)), |
| 58 | ConfigAction::ApprovalPolicyChanged { policy } => { |
| 59 | AppAction::ApprovalPolicyPersisted { policy } |
| 60 | } |
| 61 | ConfigAction::SwitchProvider { provider, model } => AppAction::SwitchProvider { |
| 62 | provider: ProviderId::from(provider.0), |
| 63 | model, |
| 64 | }, |
| 65 | ConfigAction::CompactionChanged(config) => { |
| 66 | AppAction::UpdateCompaction(to_compaction_config(config)) |
| 67 | } |
| 68 | ConfigAction::StreamChunkTimeoutChanged(timeout) => { |
| 69 | AppAction::UpdateStreamChunkTimeout(timeout) |
| 70 | } |
| 71 | ConfigAction::SubagentRuntimeChanged(config) => AppAction::UpdateSubagentRuntimeConfig { |
| 72 | enabled: config.enabled, |
| 73 | max_subagents: config.max_subagents, |
| 74 | launch_concurrency: config.launch_concurrency, |
| 75 | max_spawn_depth: config.max_spawn_depth, |
| 76 | api_timeout_secs: config.api_timeout_secs, |
| 77 | heartbeat_timeout_secs: config.heartbeat_timeout_secs, |
| 78 | }, |
| 79 | ConfigAction::SearchProviderChanged(provider) => AppAction::UpdateSearchProvider { |
| 80 | provider: to_search_provider(provider), |
| 81 | }, |
| 82 | ConfigAction::PromptSuggestionChanged { enabled } => { |
| 83 | AppAction::UpdatePromptSuggestion { enabled } |
| 84 | } |
| 85 | ConfigAction::AutoReviewReviewerModelChanged { reviewer_model } => { |
| 86 | AppAction::SetAutoReviewReviewerModel { reviewer_model } |
| 87 | } |
| 88 | ConfigAction::NotificationChanged(update) => AppAction::UpdateNotification { |
| 89 | update: to_notification_update(update), |
| 90 | }, |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | /// Convert a TUI action produced by a moved config helper back to the contract |
| 95 | /// vocabulary. Returns `None` for actions the config group never emits. |
| 96 | pub(crate) fn app_action_to_config_action(action: AppAction) -> Option<ConfigAction> { |
| 97 | Some(match action { |
| 98 | AppAction::PermissionRulesChanged => ConfigAction::PermissionRulesChanged, |
| 99 | AppAction::ApproveAutoReviewRetry { target } => { |
| 100 | ConfigAction::ApproveAutoReviewRetry { target } |
| 101 | } |
| 102 | AppAction::OpenModelConnectionSetup => ConfigAction::OpenModelConnectionSetup, |
| 103 | AppAction::StartAccountConnect => ConfigAction::StartAccountConnect, |
| 104 | AppAction::StartAccountLogin => ConfigAction::StartAccountLogin, |
| 105 | AppAction::StartAccountLogout => ConfigAction::StartAccountLogout, |
| 106 | AppAction::StartXaiDeviceLogin => ConfigAction::StartXaiDeviceLogin, |
| 107 | AppAction::StartChatgptPkceLogin => ConfigAction::StartChatgptPkceLogin, |
| 108 | AppAction::StartChatgptRevoke => ConfigAction::StartChatgptRevoke, |
| 109 | AppAction::StartOrcarouterPkceLogin => ConfigAction::StartOrcarouterPkceLogin, |
| 110 | AppAction::StartOrcarouterRevoke => ConfigAction::StartOrcarouterRevoke, |
| 111 | AppAction::OpenConfigView => ConfigAction::OpenConfigView, |
| 112 | AppAction::OpenModePicker => ConfigAction::OpenModePicker, |
| 113 | AppAction::SetConversationMode { id } => ConfigAction::SetConversationMode { id }, |
| 114 | AppAction::OpenThemePicker => ConfigAction::OpenThemePicker, |
| 115 | AppAction::OpenStatusPicker => ConfigAction::OpenStatusPicker, |
| 116 | AppAction::OpenProviderPicker => ConfigAction::OpenProviderPicker, |
| 117 | AppAction::StartPluginLogin { provider } => ConfigAction::StartPluginLogin { provider }, |
| 118 | AppAction::StartPluginLogout { provider } => ConfigAction::StartPluginLogout { provider }, |
| 119 | AppAction::SetWorkspaceTrust { trusted, save } => { |
| 120 | ConfigAction::SetWorkspaceTrust { trusted, save } |
| 121 | } |
| 122 | AppAction::ModeChanged(mode) => ConfigAction::ModeChanged(from_app_mode(mode)), |
| 123 | AppAction::ApprovalPolicyPersisted { policy } => { |
| 124 | ConfigAction::ApprovalPolicyChanged { policy } |
| 125 | } |
| 126 | AppAction::SwitchProvider { provider, model } => ConfigAction::SwitchProvider { |
| 127 | provider: CommandProviderId(provider.as_str().to_string()), |
| 128 | model, |
| 129 | }, |
| 130 | AppAction::UpdateCompaction(config) => { |
| 131 | ConfigAction::CompactionChanged(from_compaction_config(config)) |
| 132 | } |
| 133 | AppAction::UpdateStreamChunkTimeout(timeout) => { |
| 134 | ConfigAction::StreamChunkTimeoutChanged(timeout) |
| 135 | } |
| 136 | AppAction::UpdateSubagentRuntimeConfig { |
| 137 | enabled, |
| 138 | max_subagents, |
| 139 | launch_concurrency, |
| 140 | max_spawn_depth, |
| 141 | api_timeout_secs, |
| 142 | heartbeat_timeout_secs, |
| 143 | } => ConfigAction::SubagentRuntimeChanged(CommandSubagentRuntimeConfig { |
| 144 | enabled, |
| 145 | max_subagents, |
| 146 | launch_concurrency, |
| 147 | max_spawn_depth, |
| 148 | api_timeout_secs, |
| 149 | heartbeat_timeout_secs, |
| 150 | }), |
| 151 | AppAction::UpdateSearchProvider { provider } => { |
| 152 | ConfigAction::SearchProviderChanged(from_search_provider(provider)) |
| 153 | } |
| 154 | AppAction::UpdatePromptSuggestion { enabled } => { |
| 155 | ConfigAction::PromptSuggestionChanged { enabled } |
| 156 | } |
| 157 | AppAction::SetAutoReviewReviewerModel { reviewer_model } => { |
| 158 | ConfigAction::AutoReviewReviewerModelChanged { reviewer_model } |
| 159 | } |
| 160 | AppAction::UpdateNotification { update } => { |
| 161 | ConfigAction::NotificationChanged(from_notification_update(update)) |
| 162 | } |
| 163 | _ => return None, |
| 164 | }) |
| 165 | } |
| 166 | |
| 167 | fn to_app_mode(mode: CommandMode) -> AppMode { |
| 168 | match mode { |
| 169 | CommandMode::Agent => AppMode::Agent, |
| 170 | CommandMode::Plan => AppMode::Plan, |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | fn from_app_mode(mode: AppMode) -> CommandMode { |
| 175 | match mode { |
| 176 | AppMode::Agent => CommandMode::Agent, |
| 177 | AppMode::Plan => CommandMode::Plan, |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | fn to_capability_state(state: CommandCapabilityState) -> CapabilityState { |
| 182 | match state { |
| 183 | CommandCapabilityState::Supported => CapabilityState::Supported, |
| 184 | CommandCapabilityState::Unsupported => CapabilityState::Unsupported, |
| 185 | CommandCapabilityState::Unknown => CapabilityState::Unknown, |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | fn from_capability_state(state: CapabilityState) -> CommandCapabilityState { |
| 190 | match state { |
| 191 | CapabilityState::Supported => CommandCapabilityState::Supported, |
| 192 | CapabilityState::Unsupported => CommandCapabilityState::Unsupported, |
| 193 | CapabilityState::Unknown => CommandCapabilityState::Unknown, |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | pub(super) fn to_compaction_config(config: CommandCompactionConfig) -> CompactionConfig { |
| 198 | CompactionConfig { |
| 199 | enabled: config.enabled, |
| 200 | token_threshold: config.token_threshold, |
| 201 | model: config.model, |
| 202 | image_input: to_capability_state(config.image_input), |
| 203 | effective_context_window: config.effective_context_window, |
| 204 | cache_summary: config.cache_summary, |
| 205 | focus: config.focus, |
| 206 | runtime_cost_owner: config.runtime_cost_owner, |
| 207 | workspace: config.workspace, |
| 208 | summary_instructions: config.summary_instructions, |
| 209 | retained_user_message_tokens: config.retained_user_message_tokens, |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | pub(super) fn from_compaction_config(config: CompactionConfig) -> CommandCompactionConfig { |
| 214 | CommandCompactionConfig { |
| 215 | enabled: config.enabled, |
| 216 | token_threshold: config.token_threshold, |
| 217 | model: config.model, |
| 218 | image_input: from_capability_state(config.image_input), |
| 219 | effective_context_window: config.effective_context_window, |
| 220 | cache_summary: config.cache_summary, |
| 221 | focus: config.focus, |
| 222 | runtime_cost_owner: config.runtime_cost_owner, |
| 223 | workspace: config.workspace, |
| 224 | summary_instructions: config.summary_instructions, |
| 225 | retained_user_message_tokens: config.retained_user_message_tokens, |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | fn to_search_provider(provider: CommandSearchProvider) -> SearchProvider { |
| 230 | match provider { |
| 231 | CommandSearchProvider::Bing => SearchProvider::Bing, |
| 232 | CommandSearchProvider::DuckDuckGo => SearchProvider::DuckDuckGo, |
| 233 | CommandSearchProvider::Firecrawl => SearchProvider::Firecrawl, |
| 234 | CommandSearchProvider::Tavily => SearchProvider::Tavily, |
| 235 | CommandSearchProvider::Bocha => SearchProvider::Bocha, |
| 236 | CommandSearchProvider::Metaso => SearchProvider::Metaso, |
| 237 | CommandSearchProvider::Searxng => SearchProvider::Searxng, |
| 238 | CommandSearchProvider::Baidu => SearchProvider::Baidu, |
| 239 | CommandSearchProvider::Volcengine => SearchProvider::Volcengine, |
| 240 | CommandSearchProvider::Sofya => SearchProvider::Sofya, |
| 241 | CommandSearchProvider::Serply => SearchProvider::Serply, |
| 242 | CommandSearchProvider::DeepSeek => SearchProvider::DeepSeek, |
| 243 | CommandSearchProvider::SearchApi => SearchProvider::SearchApi, |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | fn from_search_provider(provider: SearchProvider) -> CommandSearchProvider { |
| 248 | match provider { |
| 249 | SearchProvider::Bing => CommandSearchProvider::Bing, |
| 250 | SearchProvider::DuckDuckGo => CommandSearchProvider::DuckDuckGo, |
| 251 | SearchProvider::Firecrawl => CommandSearchProvider::Firecrawl, |
| 252 | SearchProvider::Tavily => CommandSearchProvider::Tavily, |
| 253 | SearchProvider::Bocha => CommandSearchProvider::Bocha, |
| 254 | SearchProvider::Metaso => CommandSearchProvider::Metaso, |
| 255 | SearchProvider::Searxng => CommandSearchProvider::Searxng, |
| 256 | SearchProvider::Baidu => CommandSearchProvider::Baidu, |
| 257 | SearchProvider::Volcengine => CommandSearchProvider::Volcengine, |
| 258 | SearchProvider::Sofya => CommandSearchProvider::Sofya, |
| 259 | SearchProvider::Serply => CommandSearchProvider::Serply, |
| 260 | SearchProvider::DeepSeek => CommandSearchProvider::DeepSeek, |
| 261 | SearchProvider::SearchApi => CommandSearchProvider::SearchApi, |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | fn to_notification_method(method: CommandNotificationMethod) -> NotificationMethod { |
| 266 | match method { |
| 267 | CommandNotificationMethod::Auto => NotificationMethod::Auto, |
| 268 | CommandNotificationMethod::Osc9 => NotificationMethod::Osc9, |
| 269 | CommandNotificationMethod::Bel => NotificationMethod::Bel, |
| 270 | CommandNotificationMethod::Kitty => NotificationMethod::Kitty, |
| 271 | CommandNotificationMethod::Ghostty => NotificationMethod::Ghostty, |
| 272 | CommandNotificationMethod::Off => NotificationMethod::Off, |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | fn from_notification_method(method: NotificationMethod) -> CommandNotificationMethod { |
| 277 | match method { |
| 278 | NotificationMethod::Auto => CommandNotificationMethod::Auto, |
| 279 | NotificationMethod::Osc9 => CommandNotificationMethod::Osc9, |
| 280 | NotificationMethod::Bel => CommandNotificationMethod::Bel, |
| 281 | NotificationMethod::Kitty => CommandNotificationMethod::Kitty, |
| 282 | NotificationMethod::Ghostty => CommandNotificationMethod::Ghostty, |
| 283 | NotificationMethod::Off => CommandNotificationMethod::Off, |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | fn to_completion_sound(sound: CommandCompletionSound) -> CompletionSound { |
| 288 | match sound { |
| 289 | CommandCompletionSound::Off => CompletionSound::Off, |
| 290 | CommandCompletionSound::Beep => CompletionSound::Beep, |
| 291 | CommandCompletionSound::Bell => CompletionSound::Bell, |
| 292 | CommandCompletionSound::Whale => CompletionSound::Whale, |
| 293 | CommandCompletionSound::File => CompletionSound::File, |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | fn from_completion_sound(sound: CompletionSound) -> CommandCompletionSound { |
| 298 | match sound { |
| 299 | CompletionSound::Off => CommandCompletionSound::Off, |
| 300 | CompletionSound::Beep => CommandCompletionSound::Beep, |
| 301 | CompletionSound::Bell => CommandCompletionSound::Bell, |
| 302 | CompletionSound::Whale => CommandCompletionSound::Whale, |
| 303 | CompletionSound::File => CommandCompletionSound::File, |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | fn to_notification_condition(condition: CommandNotificationCondition) -> NotificationCondition { |
| 308 | match condition { |
| 309 | CommandNotificationCondition::Always => NotificationCondition::Always, |
| 310 | CommandNotificationCondition::Unfocused => NotificationCondition::Unfocused, |
| 311 | CommandNotificationCondition::Never => NotificationCondition::Never, |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | fn from_notification_condition(condition: NotificationCondition) -> CommandNotificationCondition { |
| 316 | match condition { |
| 317 | NotificationCondition::Always => CommandNotificationCondition::Always, |
| 318 | NotificationCondition::Unfocused => CommandNotificationCondition::Unfocused, |
| 319 | NotificationCondition::Never => CommandNotificationCondition::Never, |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | fn to_subagent_completion(completion: CommandSubagentCompletion) -> SubagentCompletionNotification { |
| 324 | match completion { |
| 325 | CommandSubagentCompletion::Always => SubagentCompletionNotification::Always, |
| 326 | CommandSubagentCompletion::FinalOnly => SubagentCompletionNotification::FinalOnly, |
| 327 | CommandSubagentCompletion::Off => SubagentCompletionNotification::Off, |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | fn from_subagent_completion( |
| 332 | completion: SubagentCompletionNotification, |
| 333 | ) -> CommandSubagentCompletion { |
| 334 | match completion { |
| 335 | SubagentCompletionNotification::Always => CommandSubagentCompletion::Always, |
| 336 | SubagentCompletionNotification::FinalOnly => CommandSubagentCompletion::FinalOnly, |
| 337 | SubagentCompletionNotification::Off => CommandSubagentCompletion::Off, |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | fn to_notification_event(event: CommandNotificationEvent) -> NotificationEvent { |
| 342 | match event { |
| 343 | CommandNotificationEvent::TurnComplete => NotificationEvent::TurnComplete, |
| 344 | CommandNotificationEvent::SubagentTerminal => NotificationEvent::SubagentTerminal, |
| 345 | CommandNotificationEvent::ApprovalNeeded => NotificationEvent::ApprovalNeeded, |
| 346 | CommandNotificationEvent::InputNeeded => NotificationEvent::InputNeeded, |
| 347 | CommandNotificationEvent::ElevationNeeded => NotificationEvent::ElevationNeeded, |
| 348 | CommandNotificationEvent::ModelNotify => NotificationEvent::ModelNotify, |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | fn from_notification_event(event: NotificationEvent) -> CommandNotificationEvent { |
| 353 | match event { |
| 354 | NotificationEvent::TurnComplete => CommandNotificationEvent::TurnComplete, |
| 355 | NotificationEvent::SubagentTerminal => CommandNotificationEvent::SubagentTerminal, |
| 356 | NotificationEvent::ApprovalNeeded => CommandNotificationEvent::ApprovalNeeded, |
| 357 | NotificationEvent::InputNeeded => CommandNotificationEvent::InputNeeded, |
| 358 | NotificationEvent::ElevationNeeded => CommandNotificationEvent::ElevationNeeded, |
| 359 | NotificationEvent::ModelNotify => CommandNotificationEvent::ModelNotify, |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | fn to_notification_update(update: CommandNotificationUpdate) -> NotificationConfigUpdate { |
| 364 | match update { |
| 365 | CommandNotificationUpdate::Method(method) => { |
| 366 | NotificationConfigUpdate::Method(to_notification_method(method)) |
| 367 | } |
| 368 | CommandNotificationUpdate::ThresholdSecs(value) => { |
| 369 | NotificationConfigUpdate::ThresholdSecs(value) |
| 370 | } |
| 371 | CommandNotificationUpdate::IncludeSummary(value) => { |
| 372 | NotificationConfigUpdate::IncludeSummary(value) |
| 373 | } |
| 374 | CommandNotificationUpdate::Quiet(value) => NotificationConfigUpdate::Quiet(value), |
| 375 | CommandNotificationUpdate::Sound(sound) => { |
| 376 | NotificationConfigUpdate::Sound(sound.map(to_completion_sound)) |
| 377 | } |
| 378 | CommandNotificationUpdate::Condition(condition) => { |
| 379 | NotificationConfigUpdate::Condition(to_notification_condition(condition)) |
| 380 | } |
| 381 | CommandNotificationUpdate::CompletionSound(sound) => { |
| 382 | NotificationConfigUpdate::CompletionSound(to_completion_sound(sound)) |
| 383 | } |
| 384 | CommandNotificationUpdate::SubagentCompletion(completion) => { |
| 385 | NotificationConfigUpdate::SubagentCompletion(to_subagent_completion(completion)) |
| 386 | } |
| 387 | CommandNotificationUpdate::SoundFile(path) => NotificationConfigUpdate::SoundFile(path), |
| 388 | CommandNotificationUpdate::Event(event, enabled) => { |
| 389 | NotificationConfigUpdate::Event(to_notification_event(event), enabled) |
| 390 | } |
| 391 | CommandNotificationUpdate::EventSoundEnabled(enabled) => { |
| 392 | NotificationConfigUpdate::EventSoundEnabled(enabled) |
| 393 | } |
| 394 | CommandNotificationUpdate::EventSoundEvents(events) => { |
| 395 | NotificationConfigUpdate::EventSoundEvents(events) |
| 396 | } |
| 397 | CommandNotificationUpdate::EventSoundMinIntervalMs(value) => { |
| 398 | NotificationConfigUpdate::EventSoundMinIntervalMs(value) |
| 399 | } |
| 400 | CommandNotificationUpdate::EventSoundQuiet(quiet) => { |
| 401 | NotificationConfigUpdate::EventSoundQuiet(quiet) |
| 402 | } |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | fn from_notification_update(update: NotificationConfigUpdate) -> CommandNotificationUpdate { |
| 407 | match update { |
| 408 | NotificationConfigUpdate::Method(method) => { |
| 409 | CommandNotificationUpdate::Method(from_notification_method(method)) |
| 410 | } |
| 411 | NotificationConfigUpdate::ThresholdSecs(value) => { |
| 412 | CommandNotificationUpdate::ThresholdSecs(value) |
| 413 | } |
| 414 | NotificationConfigUpdate::IncludeSummary(value) => { |
| 415 | CommandNotificationUpdate::IncludeSummary(value) |
| 416 | } |
| 417 | NotificationConfigUpdate::Quiet(value) => CommandNotificationUpdate::Quiet(value), |
| 418 | NotificationConfigUpdate::Sound(sound) => { |
| 419 | CommandNotificationUpdate::Sound(sound.map(from_completion_sound)) |
| 420 | } |
| 421 | NotificationConfigUpdate::Condition(condition) => { |
| 422 | CommandNotificationUpdate::Condition(from_notification_condition(condition)) |
| 423 | } |
| 424 | NotificationConfigUpdate::CompletionSound(sound) => { |
| 425 | CommandNotificationUpdate::CompletionSound(from_completion_sound(sound)) |
| 426 | } |
| 427 | NotificationConfigUpdate::SubagentCompletion(completion) => { |
| 428 | CommandNotificationUpdate::SubagentCompletion(from_subagent_completion(completion)) |
| 429 | } |
| 430 | NotificationConfigUpdate::SoundFile(path) => CommandNotificationUpdate::SoundFile(path), |
| 431 | NotificationConfigUpdate::Event(event, enabled) => { |
| 432 | CommandNotificationUpdate::Event(from_notification_event(event), enabled) |
| 433 | } |
| 434 | NotificationConfigUpdate::EventSoundEnabled(enabled) => { |
| 435 | CommandNotificationUpdate::EventSoundEnabled(enabled) |
| 436 | } |
| 437 | NotificationConfigUpdate::EventSoundEvents(events) => { |
| 438 | CommandNotificationUpdate::EventSoundEvents(events) |
| 439 | } |
| 440 | NotificationConfigUpdate::EventSoundMinIntervalMs(value) => { |
| 441 | CommandNotificationUpdate::EventSoundMinIntervalMs(value) |
| 442 | } |
| 443 | NotificationConfigUpdate::EventSoundQuiet(quiet) => { |
| 444 | CommandNotificationUpdate::EventSoundQuiet(quiet) |
| 445 | } |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | /// Render a contract config reply for a host-side caller (interactive UI views |
| 450 | /// and tests) that needs a concrete `CommandResult`. |
| 451 | pub(crate) fn render_config_reply( |
| 452 | reply: codewhale_command_contract::config::ConfigReply, |
| 453 | locale: codewhale_localization::Locale, |
| 454 | ) -> crate::commands::CommandResult { |
| 455 | use codewhale_command_contract::config::ConfigMessage; |
| 456 | |
| 457 | /// Render a message, propagating an unknown key or an invalid replacement |
| 458 | /// contract instead of emitting a raw key or an untranslated template. |
| 459 | fn message_text( |
| 460 | message: ConfigMessage, |
| 461 | locale: codewhale_localization::Locale, |
| 462 | ) -> Result<String, String> { |
| 463 | match message { |
| 464 | ConfigMessage::Literal(text) => Ok(text), |
| 465 | ConfigMessage::Concat(parts) => { |
| 466 | let mut out = String::new(); |
| 467 | for part in parts { |
| 468 | out.push_str(&message_text(part, locale)?); |
| 469 | } |
| 470 | Ok(out) |
| 471 | } |
| 472 | ConfigMessage::Keyed { key, params } => { |
| 473 | let mut resolved: Vec<(String, String)> = Vec::with_capacity(params.len()); |
| 474 | for (name, param) in params { |
| 475 | resolved.push((name, message_text(param, locale)?)); |
| 476 | } |
| 477 | let refs: Vec<(&str, &str)> = resolved |
| 478 | .iter() |
| 479 | .map(|(k, v)| (k.as_str(), v.as_str())) |
| 480 | .collect(); |
| 481 | let Some(id) = crate::commands::contract::config_messages::resolve(&key) else { |
| 482 | return Err("unknown translation key".to_string()); |
| 483 | }; |
| 484 | let template = codewhale_localization::tr(locale, id); |
| 485 | crate::commands::contract::apply_named_replacements(&template, &refs) |
| 486 | .ok_or_else(|| "invalid translation replacement contract".to_string()) |
| 487 | } |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | // Same failure policy as the portable renderer: a failed render is a |
| 492 | // failure with no action, never a successful reply with a raw key. |
| 493 | let message = match reply.message { |
| 494 | None => None, |
| 495 | Some(message) => match message_text(message, locale) { |
| 496 | Ok(text) => Some(text), |
| 497 | Err(error) => { |
| 498 | return crate::commands::CommandResult { |
| 499 | message: Some(format!("Error: {error}")), |
| 500 | action: None, |
| 501 | is_error: true, |
| 502 | }; |
| 503 | } |
| 504 | }, |
| 505 | }; |
| 506 | let message = match (message, reply.is_error) { |
| 507 | (Some(text), true) if !text.starts_with("Error: ") => Some(format!("Error: {text}")), |
| 508 | (other, _) => other, |
| 509 | }; |
| 510 | crate::commands::CommandResult { |
| 511 | message, |
| 512 | action: reply.action.map(config_action_to_app_action), |
| 513 | is_error: reply.is_error, |
| 514 | } |
| 515 | } |
| 516 |