| 1 | use super::{SharedCommandHost, to_command_mode}; |
| 2 | use crate::tui::app::AppModeUi; |
| 3 | use crate::tui::app::StatusToastLevel; |
| 4 | use crate::tui::views::{HelpView, ModalKind}; |
| 5 | use codewhale_command_contract::navigation::{ |
| 6 | CommandNavigationContext, NavigationBuiltinHelp, NavigationHelpTopic, NavigationLinks, |
| 7 | NavigationOverview, NavigationProviderKind, NavigationProviderLinks, NavigationSkill, |
| 8 | NavigationUserHelp, SubagentRosterDisplay, |
| 9 | }; |
| 10 | use codewhale_localization::MessageId; |
| 11 | |
| 12 | pub(super) struct NavigationAdapter<'a> { |
| 13 | pub(super) host: SharedCommandHost<'a>, |
| 14 | } |
| 15 | |
| 16 | impl CommandNavigationContext for NavigationAdapter<'_> { |
| 17 | fn open_model_reference(&mut self) { |
| 18 | self.host |
| 19 | .app |
| 20 | .borrow_mut() |
| 21 | .view_stack |
| 22 | .push(super::model_reference::view()); |
| 23 | } |
| 24 | |
| 25 | fn toggle_window_pin(&mut self) { |
| 26 | crate::tui::window_control::toggle_pin(&mut self.host.app.borrow_mut()); |
| 27 | } |
| 28 | |
| 29 | fn prepare_subagent_roster(&mut self, display: SubagentRosterDisplay) { |
| 30 | use crate::tui::views::{SubAgentsView, subagent_view_agents}; |
| 31 | let mut app = self.host.app.borrow_mut(); |
| 32 | match display { |
| 33 | SubagentRosterDisplay::Modal |
| 34 | if app.view_stack.top_kind() != Some(ModalKind::SubAgents) => |
| 35 | { |
| 36 | let agents = subagent_view_agents(&app, &app.subagent_cache); |
| 37 | let view = SubAgentsView::for_app(&app, agents); |
| 38 | app.view_stack.push(view); |
| 39 | } |
| 40 | SubagentRosterDisplay::Modal => {} |
| 41 | SubagentRosterDisplay::Transcript => app.agent_roster_print_requested = true, |
| 42 | } |
| 43 | let message = app.tr(MessageId::SubagentsFetching).into_owned(); |
| 44 | app.show_command_result(message, StatusToastLevel::Info); |
| 45 | } |
| 46 | |
| 47 | fn help_topic(&self, topic: &str) -> NavigationHelpTopic { |
| 48 | let app = self.host.app.borrow(); |
| 49 | let builtin = crate::commands::get_command_info(topic); |
| 50 | crate::commands::user_registry::with_registry_for_app(&app, |registry| { |
| 51 | NavigationHelpTopic { |
| 52 | user: registry.get(topic).map(|command| NavigationUserHelp { |
| 53 | name: command.name.clone(), |
| 54 | description: command.description.clone(), |
| 55 | usage: command.display_usage().map(str::to_owned), |
| 56 | aliases: command.aliases.clone(), |
| 57 | }), |
| 58 | shadowed_names: builtin |
| 59 | .map(|command| { |
| 60 | command |
| 61 | .aliases |
| 62 | .iter() |
| 63 | .filter(|alias| registry.get(alias).is_some()) |
| 64 | .map(|alias| (*alias).to_owned()) |
| 65 | .collect() |
| 66 | }) |
| 67 | .unwrap_or_default(), |
| 68 | builtin: builtin.map(|command| NavigationBuiltinHelp { |
| 69 | name: command.name.to_owned(), |
| 70 | usage: command.usage.to_owned(), |
| 71 | description: command.description_for(app.ui_locale).into_owned(), |
| 72 | aliases: command |
| 73 | .aliases |
| 74 | .iter() |
| 75 | .map(|alias| (*alias).to_owned()) |
| 76 | .collect(), |
| 77 | }), |
| 78 | } |
| 79 | }) |
| 80 | } |
| 81 | |
| 82 | fn cached_skills(&self) -> Vec<NavigationSkill> { |
| 83 | self.host |
| 84 | .app |
| 85 | .borrow() |
| 86 | .cached_skills |
| 87 | .iter() |
| 88 | .map(|(name, description)| NavigationSkill { |
| 89 | name: name.clone(), |
| 90 | description: description.clone(), |
| 91 | }) |
| 92 | .collect() |
| 93 | } |
| 94 | |
| 95 | fn help_is_open(&self) -> bool { |
| 96 | self.host.app.borrow().view_stack.top_kind() == Some(ModalKind::Help) |
| 97 | } |
| 98 | |
| 99 | fn open_help(&mut self) { |
| 100 | let mut app = self.host.app.borrow_mut(); |
| 101 | let help = HelpView::new_for_app(&app, false).with_groups_expanded(app.help_expand_groups); |
| 102 | app.view_stack.push(help); |
| 103 | } |
| 104 | |
| 105 | fn home_is_blocked(&self) -> bool { |
| 106 | self.host.app.borrow().session_transition_blocked() |
| 107 | } |
| 108 | |
| 109 | fn restore_home(&mut self) { |
| 110 | let mut app = self.host.app.borrow_mut(); |
| 111 | app.launch.workspace = app.workspace.clone(); |
| 112 | app.launch.restore_card(); |
| 113 | app.launch.visible = true; |
| 114 | app.launch.return_to_session = true; |
| 115 | app.needs_redraw = true; |
| 116 | } |
| 117 | |
| 118 | fn overview(&self) -> NavigationOverview { |
| 119 | let app = self.host.app.borrow(); |
| 120 | NavigationOverview { |
| 121 | model: app.model.clone(), |
| 122 | mode: to_command_mode(app.mode), |
| 123 | mode_label: app.mode.display_name_localized(app.ui_locale).into_owned(), |
| 124 | workspace: app.workspace.clone(), |
| 125 | history_count: app.history.len(), |
| 126 | total_tokens: app.session.displayed_total_conversation_tokens(), |
| 127 | queued_count: app.queued_messages.len(), |
| 128 | subagent_count: app.subagent_cache.len(), |
| 129 | active_skill: app.active_skill.clone(), |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | fn links(&self) -> NavigationLinks { |
| 134 | let app = self.host.app.borrow(); |
| 135 | NavigationLinks { |
| 136 | active_provider: app.api_provider.as_str().to_owned(), |
| 137 | locale_tag: app.ui_locale.tag().to_owned(), |
| 138 | providers: codewhale_config::provider::providers_sorted_for_display() |
| 139 | .into_iter() |
| 140 | .map(|provider| { |
| 141 | let links = provider.credential_help(); |
| 142 | NavigationProviderLinks { |
| 143 | id: provider.id().to_owned(), |
| 144 | name: provider.display_name().to_owned(), |
| 145 | credential_url: links.credential_url.map(str::to_owned), |
| 146 | docs_url: links.docs_url.map(str::to_owned), |
| 147 | guidance: links.guidance.to_owned(), |
| 148 | env_names: provider |
| 149 | .env_vars() |
| 150 | .iter() |
| 151 | .map(|name| (*name).to_owned()) |
| 152 | .collect(), |
| 153 | kind: if provider.kind() == codewhale_config::ProviderKind::Moonshot { |
| 154 | NavigationProviderKind::Moonshot |
| 155 | } else { |
| 156 | NavigationProviderKind::Other |
| 157 | }, |
| 158 | } |
| 159 | }) |
| 160 | .collect(), |
| 161 | kimi_route: codewhale_config::descriptors::defaults::DEFAULT_KIMI_CODE_BASE_URL |
| 162 | .to_owned(), |
| 163 | kimi_console: |
| 164 | codewhale_config::descriptors::defaults::KIMI_CODE_MEMBERSHIP_PLAN_CONSOLE_URL |
| 165 | .to_owned(), |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | pub(super) struct VoiceInputAdapter<'a> { |
| 171 | pub(super) host: SharedCommandHost<'a>, |
| 172 | } |
| 173 | |
| 174 | impl codewhale_command_contract::navigation::CommandVoiceInputContext for VoiceInputAdapter<'_> { |
| 175 | fn recorder_available(&self) -> bool { |
| 176 | codewhale_engine::voice::is_available() |
| 177 | } |
| 178 | fn voice_enabled(&self) -> bool { |
| 179 | self.host.app.borrow().voice_enabled |
| 180 | } |
| 181 | fn set_voice_enabled(&mut self, enabled: bool) { |
| 182 | self.host.app.borrow_mut().voice_enabled = enabled; |
| 183 | } |
| 184 | fn send_enabled(&self) -> bool { |
| 185 | self.host.app.borrow().voice_send_enabled |
| 186 | } |
| 187 | fn set_send_enabled(&mut self, enabled: bool) { |
| 188 | self.host.app.borrow_mut().voice_send_enabled = enabled; |
| 189 | } |
| 190 | fn control_enabled(&self) -> bool { |
| 191 | self.host.app.borrow().voice_control_enabled |
| 192 | } |
| 193 | fn set_control_enabled(&mut self, enabled: bool) { |
| 194 | self.host.app.borrow_mut().voice_control_enabled = enabled; |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | pub(super) fn anchors_path(workspace: &std::path::Path) -> std::path::PathBuf { |
| 199 | let primary = workspace.join(".codewhale").join("anchors.md"); |
| 200 | if primary.symlink_metadata().is_ok() || workspace.join(".codewhale").is_symlink() { |
| 201 | return primary; |
| 202 | } |
| 203 | workspace.join(".deepseek").join("anchors.md") |
| 204 | } |
| 205 | |
| 206 | pub(super) fn read_anchor_document(workspace: &std::path::Path) -> Result<Option<String>, String> { |
| 207 | match codewhale_util::fs_confined::read_to_string(workspace, &anchors_path(workspace)) { |
| 208 | Ok(content) => Ok(Some(content)), |
| 209 | Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(None), |
| 210 | Err(error) => Err(error.to_string()), |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | pub(super) fn append_anchor_bytes( |
| 215 | workspace: &std::path::Path, |
| 216 | parts: &[&[u8]], |
| 217 | ) -> Result<std::path::PathBuf, codewhale_command_contract::navigation::AnchorWriteError> { |
| 218 | use codewhale_command_contract::navigation::AnchorWriteError; |
| 219 | use std::io::Write; |
| 220 | let path = anchors_path(workspace); |
| 221 | let mut file = codewhale_util::fs_confined::open_append(workspace, &path) |
| 222 | .map_err(|error| AnchorWriteError::Open(error.to_string()))?; |
| 223 | for bytes in parts { |
| 224 | file.write_all(bytes) |
| 225 | .map_err(|error| AnchorWriteError::Write(error.to_string()))?; |
| 226 | } |
| 227 | Ok(path) |
| 228 | } |
| 229 | |
| 230 | pub(super) fn replace_anchor_document( |
| 231 | workspace: &std::path::Path, |
| 232 | bytes: &[u8], |
| 233 | ) -> Result<(), String> { |
| 234 | codewhale_util::fs_confined::write(workspace, &anchors_path(workspace), bytes) |
| 235 | .map_err(|error| error.to_string()) |
| 236 | } |
| 237 | |
| 238 | pub(super) fn resolve_message(key: &str) -> Option<MessageId> { |
| 239 | Some(match key { |
| 240 | "cmd_copy_no_output" => MessageId::CmdCopyNoOutput, |
| 241 | "cmd_copy_queued" => MessageId::CmdCopyQueued, |
| 242 | "cmd_copy_queued_no_backup" => MessageId::CmdCopyQueuedNoBackup, |
| 243 | "cmd_copy_success" => MessageId::CmdCopySuccess, |
| 244 | "cmd_copy_success_no_backup" => MessageId::CmdCopySuccessNoBackup, |
| 245 | "cmd_copy_failed" => MessageId::CmdCopyFailed, |
| 246 | "cmd_copy_failed_no_backup" => MessageId::CmdCopyFailedNoBackup, |
| 247 | "cmd_queue_already_editing" => MessageId::CmdQueueAlreadyEditing, |
| 248 | "cmd_queue_already_empty" => MessageId::CmdQueueAlreadyEmpty, |
| 249 | "cmd_queue_cleared" => MessageId::CmdQueueCleared, |
| 250 | "cmd_queue_dropped" => MessageId::CmdQueueDropped, |
| 251 | "cmd_queue_editing_message" => MessageId::CmdQueueEditingMessage, |
| 252 | "cmd_queue_editing_status" => MessageId::CmdQueueEditingStatus, |
| 253 | "cmd_queue_index_min" => MessageId::CmdQueueIndexMin, |
| 254 | "cmd_queue_index_positive" => MessageId::CmdQueueIndexPositive, |
| 255 | "cmd_queue_list_header" => MessageId::CmdQueueListHeader, |
| 256 | "cmd_queue_missing_index" => MessageId::CmdQueueMissingIndex, |
| 257 | "cmd_queue_no_messages" => MessageId::CmdQueueNoMessages, |
| 258 | "cmd_queue_not_found" => MessageId::CmdQueueNotFound, |
| 259 | "cmd_queue_tip" => MessageId::CmdQueueTip, |
| 260 | "cmd_queue_usage" => MessageId::CmdQueueUsage, |
| 261 | "profile_usage" => MessageId::ProfileUsage, |
| 262 | "profile_switching" => MessageId::ProfileSwitching, |
| 263 | "voice_disabled" => MessageId::VoiceDisabled, |
| 264 | "voice_enabled" => MessageId::VoiceEnabled, |
| 265 | "voice_err_no_recorder" => MessageId::VoiceErrNoRecorder, |
| 266 | "voice_send_enabled" => MessageId::VoiceSendEnabled, |
| 267 | "voice_send_disabled" => MessageId::VoiceSendDisabled, |
| 268 | "voice_control_enabled" => MessageId::VoiceControlEnabled, |
| 269 | "voice_control_disabled" => MessageId::VoiceControlDisabled, |
| 270 | "cmd_translate_on" => MessageId::CmdTranslateOn, |
| 271 | "cmd_translate_off" => MessageId::CmdTranslateOff, |
| 272 | "workspace_current" => MessageId::WorkspaceCurrent, |
| 273 | "workspace_not_found" => MessageId::WorkspaceNotFound, |
| 274 | "workspace_not_directory" => MessageId::WorkspaceNotDirectory, |
| 275 | "workspace_switching" => MessageId::WorkspaceSwitching, |
| 276 | "workspace_home_unresolved" => MessageId::WorkspaceHomeUnresolved, |
| 277 | "help_aliases_label" => MessageId::HelpAliasesLabel, |
| 278 | "help_unknown_command" => MessageId::HelpUnknownCommand, |
| 279 | "config_help_discoverable" => MessageId::ConfigHelpDiscoverable, |
| 280 | "home_navigation_busy" => MessageId::HomeNavigationBusy, |
| 281 | "home_dashboard_title" => MessageId::HomeDashboardTitle, |
| 282 | "home_model" => MessageId::HomeModel, |
| 283 | "home_mode" => MessageId::HomeMode, |
| 284 | "home_workspace" => MessageId::HomeWorkspace, |
| 285 | "home_history" => MessageId::HomeHistory, |
| 286 | "home_tokens" => MessageId::HomeTokens, |
| 287 | "home_queued" => MessageId::HomeQueued, |
| 288 | "home_subagents" => MessageId::HomeSubagents, |
| 289 | "home_skill" => MessageId::HomeSkill, |
| 290 | "home_quick_actions" => MessageId::HomeQuickActions, |
| 291 | "home_quick_workspace" => MessageId::HomeQuickWorkspace, |
| 292 | "home_quick_restore" => MessageId::HomeQuickRestore, |
| 293 | "home_quick_tokens" => MessageId::HomeQuickTokens, |
| 294 | "home_quick_links" => MessageId::HomeQuickLinks, |
| 295 | "home_quick_skills" => MessageId::HomeQuickSkills, |
| 296 | "home_quick_config" => MessageId::HomeQuickConfig, |
| 297 | "home_quick_settings" => MessageId::HomeQuickSettings, |
| 298 | "home_quick_model" => MessageId::HomeQuickModel, |
| 299 | "home_quick_subagents" => MessageId::HomeQuickSubagents, |
| 300 | "home_quick_task_list" => MessageId::HomeQuickTaskList, |
| 301 | "home_quick_help" => MessageId::HomeQuickHelp, |
| 302 | "home_mode_tips" => MessageId::HomeModeTips, |
| 303 | "home_agent_mode_tip" => MessageId::HomeAgentModeTip, |
| 304 | "home_agent_mode_review_tip" => MessageId::HomeAgentModeReviewTip, |
| 305 | "home_agent_mode_yolo_tip" => MessageId::HomeAgentModeYoloTip, |
| 306 | "home_operate_mode_tip" => MessageId::HomeOperateModeTip, |
| 307 | "home_operate_mode_fleet_tip" => MessageId::HomeOperateModeFleetTip, |
| 308 | "home_plan_mode_tip" => MessageId::HomePlanModeTip, |
| 309 | "home_plan_mode_checklist_tip" => MessageId::HomePlanModeChecklistTip, |
| 310 | "links_project_title" => MessageId::LinksProjectTitle, |
| 311 | "links_documentation" => MessageId::LinksDocumentation, |
| 312 | "links_community" => MessageId::LinksCommunity, |
| 313 | "links_git_hub" => MessageId::LinksGitHub, |
| 314 | "links_managed_app" => MessageId::LinksManagedApp, |
| 315 | "links_managed_app_note" => MessageId::LinksManagedAppNote, |
| 316 | "links_title" => MessageId::LinksTitle, |
| 317 | "links_dashboard" => MessageId::LinksDashboard, |
| 318 | "links_docs" => MessageId::LinksDocs, |
| 319 | "links_kimi_code_route_note" => MessageId::LinksKimiCodeRouteNote, |
| 320 | "links_tip" => MessageId::LinksTip, |
| 321 | _ => return None, |
| 322 | }) |
| 323 | } |
| 324 |