| 1 | pub(super) struct PolicyAdapter<'a> { |
| 2 | pub(super) host: super::SharedCommandHost<'a>, |
| 3 | } |
| 4 | use crate::tui::app::AppAction; |
| 5 | use codewhale_command_contract::core_policy::*; |
| 6 | use codewhale_engine::reasoning_preference::ReasoningEffort; |
| 7 | use codewhale_engine::session_manager::{SessionWriteOwner, SessionWriteReceipt}; |
| 8 | use codewhale_engine::tools::github::report; |
| 9 | use codewhale_localization::MessageId; |
| 10 | |
| 11 | pub(super) fn resolve_message(key: &str) -> Option<MessageId> { |
| 12 | Some(match key { |
| 13 | "clear_conversation" => MessageId::ClearConversation, |
| 14 | "clear_conversation_busy" => MessageId::ClearConversationBusy, |
| 15 | "feedback_help" => MessageId::FeedbackHelp, |
| 16 | "feedback_no_session" => MessageId::FeedbackNoSession, |
| 17 | "feedback_draft_requested" => MessageId::FeedbackDraftRequested, |
| 18 | "cmd_effort_description" => MessageId::CmdEffortDescription, |
| 19 | _ => return None, |
| 20 | }) |
| 21 | } |
| 22 | |
| 23 | impl ClearContext for PolicyAdapter<'_> { |
| 24 | fn start_empty_conversation(&mut self) -> Result<ClearedConversation, ClearFailure> { |
| 25 | clear_conversation( |
| 26 | &mut self.host.app.borrow_mut(), |
| 27 | crate::tui::ui::prepare_fresh_session_write, |
| 28 | ) |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | fn clear_conversation( |
| 33 | app: &mut crate::tui::app::App, |
| 34 | prepare_write: impl FnOnce(&str) -> Result<SessionWriteReceipt, String>, |
| 35 | ) -> Result<ClearedConversation, ClearFailure> { |
| 36 | let blockers = app.session_transition_blockers(); |
| 37 | if !blockers.is_empty() { |
| 38 | return Err(ClearFailure::Busy(blockers)); |
| 39 | } |
| 40 | let new_id = uuid::Uuid::new_v4().to_string(); |
| 41 | let queue = crate::tui::ui::prepare_offline_queue_transition(app, &new_id) |
| 42 | .map_err(ClearFailure::Transition)?; |
| 43 | let write_receipt = prepare_write(&new_id).map_err(ClearFailure::Transition)?; |
| 44 | if !crate::commands::groups::core::reset_conversation_state(app) { |
| 45 | return Err(ClearFailure::Busy(Vec::new())); |
| 46 | } |
| 47 | crate::tui::ui::install_offline_queue_transition(app, queue); |
| 48 | app.set_current_session_id(Some(new_id.clone())); |
| 49 | app.session_write_receipt = Some(write_receipt); |
| 50 | app.session_write_owner = Some(SessionWriteOwner::control()); |
| 51 | app.current_session_metadata = None; |
| 52 | app.session_title = None; |
| 53 | app.window_title = None; |
| 54 | app.composer_color = None; |
| 55 | Ok(ClearedConversation { |
| 56 | session_id: new_id, |
| 57 | model: app.model.clone(), |
| 58 | workspace: app.workspace.clone(), |
| 59 | mode: super::to_command_mode(app.mode), |
| 60 | agent_preset: app.agent_preset(), |
| 61 | }) |
| 62 | } |
| 63 | |
| 64 | impl From<ClearedConversation> for AppAction { |
| 65 | fn from(receipt: ClearedConversation) -> Self { |
| 66 | Self::SyncSession { |
| 67 | session_id: Some(receipt.session_id), |
| 68 | messages: Vec::new(), |
| 69 | system_prompt: None, |
| 70 | model: receipt.model, |
| 71 | workspace: receipt.workspace, |
| 72 | mode: super::from_command_mode(receipt.mode), |
| 73 | agent_preset: receipt.agent_preset, |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | fn resolve_supported_effort( |
| 79 | app: &crate::tui::app::App, |
| 80 | value: &str, |
| 81 | ) -> Result<ReasoningEffort, EffortFailure> { |
| 82 | let effort = ReasoningEffort::parse_strict(value).map_err(|_| EffortFailure::Unknown)?; |
| 83 | let requested = if app.auto_model { |
| 84 | effort |
| 85 | } else { |
| 86 | effort.normalize_for_route(app.api_provider, &app.active_route_base_url, &app.model) |
| 87 | }; |
| 88 | let available = crate::tui::model_picker::picker_efforts_for_route( |
| 89 | app.api_provider, |
| 90 | &app.active_route_base_url, |
| 91 | &app.model, |
| 92 | app.auto_model, |
| 93 | ); |
| 94 | if !available.is_empty() && !available.contains(&requested) && !available.contains(&effort) { |
| 95 | return Err(EffortFailure::Unavailable { |
| 96 | model: app.model.clone(), |
| 97 | labels: available |
| 98 | .iter() |
| 99 | .map(|effort| { |
| 100 | effort |
| 101 | .display_label_for_provider(app.api_provider) |
| 102 | .to_owned() |
| 103 | }) |
| 104 | .collect(), |
| 105 | }); |
| 106 | } |
| 107 | Ok(requested) |
| 108 | } |
| 109 | |
| 110 | impl EffortContext for PolicyAdapter<'_> { |
| 111 | fn available_labels(&self) -> Vec<String> { |
| 112 | let app = self.host.app.borrow(); |
| 113 | crate::tui::model_picker::picker_efforts_for_route( |
| 114 | app.api_provider, |
| 115 | &app.active_route_base_url, |
| 116 | &app.model, |
| 117 | app.auto_model, |
| 118 | ) |
| 119 | .iter() |
| 120 | .map(|effort| { |
| 121 | effort |
| 122 | .display_label_for_provider(app.api_provider) |
| 123 | .to_owned() |
| 124 | }) |
| 125 | .collect() |
| 126 | } |
| 127 | fn cycle(&mut self) -> (String, String) { |
| 128 | let mut app = self.host.app.borrow_mut(); |
| 129 | let prev = app |
| 130 | .reasoning_effort |
| 131 | .display_label_for_provider(app.api_provider) |
| 132 | .to_owned(); |
| 133 | let _ = app.cycle_effort(); |
| 134 | ( |
| 135 | prev, |
| 136 | app.reasoning_effort |
| 137 | .display_label_for_provider(app.api_provider) |
| 138 | .to_owned(), |
| 139 | ) |
| 140 | } |
| 141 | fn cycle_checked(&mut self) -> Result<(String, String), String> { |
| 142 | let mut app = self.host.app.borrow_mut(); |
| 143 | let prev = app |
| 144 | .reasoning_effort |
| 145 | .display_label_for_provider(app.api_provider) |
| 146 | .to_owned(); |
| 147 | match app.cycle_effort_checked()? { |
| 148 | crate::tui::app::SettingSelection::Refused => { |
| 149 | Err(app.setting_locked_message(MessageId::SettingSubjectThinking)) |
| 150 | } |
| 151 | crate::tui::app::SettingSelection::Changed |
| 152 | | crate::tui::app::SettingSelection::PersistedSame => Ok(( |
| 153 | prev, |
| 154 | app.reasoning_effort |
| 155 | .display_label_for_provider(app.api_provider) |
| 156 | .to_owned(), |
| 157 | )), |
| 158 | } |
| 159 | } |
| 160 | fn set_supported_effort(&mut self, value: &str) -> Result<String, EffortFailure> { |
| 161 | let mut app = self.host.app.borrow_mut(); |
| 162 | let requested = resolve_supported_effort(&app, value)?; |
| 163 | app.commit_reasoning_effort(requested); |
| 164 | Ok(requested |
| 165 | .display_label_for_provider(app.api_provider) |
| 166 | .to_owned()) |
| 167 | } |
| 168 | fn set_supported_effort_checked(&mut self, value: &str) -> Result<String, EffortChangeFailure> { |
| 169 | let mut app = self.host.app.borrow_mut(); |
| 170 | let requested = |
| 171 | resolve_supported_effort(&app, value).map_err(EffortChangeFailure::Selection)?; |
| 172 | app.commit_reasoning_effort_checked(requested) |
| 173 | .map_err(EffortChangeFailure::Receipt)?; |
| 174 | Ok(requested |
| 175 | .display_label_for_provider(app.api_provider) |
| 176 | .to_owned()) |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | impl FeedbackContext for PolicyAdapter<'_> { |
| 181 | fn has_session(&self) -> bool { |
| 182 | self.host.app.borrow().current_session_id.is_some() |
| 183 | } |
| 184 | fn safe_text(&self, text: &str) -> Result<String, String> { |
| 185 | report::safe_text(text, 1600, &mut Default::default()).map_err(|_| { |
| 186 | self.host |
| 187 | .app |
| 188 | .borrow() |
| 189 | .tr(MessageId::FeedbackUnavailable) |
| 190 | .to_string() |
| 191 | }) |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | impl From<FeedbackAction> for AppAction { |
| 196 | fn from(action: FeedbackAction) -> Self { |
| 197 | use codewhale_commands::core_policy::{FEATURE_URL, SECURITY_POLICY_URL}; |
| 198 | match action { |
| 199 | FeedbackAction::OpenPicker => Self::OpenFeedbackPicker, |
| 200 | FeedbackAction::Feature => Self::OpenExternalUrl { |
| 201 | url: FEATURE_URL.into(), |
| 202 | label: "GitHub feature request".into(), |
| 203 | }, |
| 204 | FeedbackAction::Security => Self::OpenExternalUrl { |
| 205 | url: SECURITY_POLICY_URL.into(), |
| 206 | label: "GitHub security policy".into(), |
| 207 | }, |
| 208 | FeedbackAction::Draft(focus) => { |
| 209 | Self::SendMessage(report::draft_instruction(&focus, None)) |
| 210 | } |
| 211 | FeedbackAction::Review { id, change } => Self::ReviewIssueReport { id, change }, |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | #[cfg(test)] |
| 217 | mod tests { |
| 218 | use super::*; |
| 219 | use crate::tui::{app::App, history::HistoryCell}; |
| 220 | use codewhale_engine::session_manager::SessionManager; |
| 221 | use std::sync::Arc; |
| 222 | |
| 223 | #[test] |
| 224 | fn clear_receipt_failure_keeps_session_and_releases_candidate_lease() { |
| 225 | let workspace = tempfile::tempdir().unwrap(); |
| 226 | let _home = crate::test_support::SealedHome::at(workspace.path()); |
| 227 | let manager = SessionManager::default_location().unwrap(); |
| 228 | let old_id = uuid::Uuid::new_v4().to_string(); |
| 229 | let old_owner = SessionWriteOwner::control(); |
| 230 | let mut app = App::new( |
| 231 | crate::tui::test_support::test_tui_options(workspace.path()), |
| 232 | &Default::default(), |
| 233 | ); |
| 234 | app.set_current_session_id(Some(old_id.clone())); |
| 235 | app.session_write_receipt = Some(manager.expect_absent_session(&old_id).unwrap()); |
| 236 | app.session_write_owner = Some(old_owner.clone()); |
| 237 | app.offline_queue_lease = Some(manager.acquire_offline_queue_lease(&old_id).unwrap()); |
| 238 | let old_lease = Arc::downgrade(app.offline_queue_lease.as_ref().unwrap()); |
| 239 | app.session_title = Some("original".into()); |
| 240 | app.input = "draft".into(); |
| 241 | app.history.push(HistoryCell::User { |
| 242 | content: "old".into(), |
| 243 | }); |
| 244 | let bad_store = workspace.path().join("regular-file"); |
| 245 | std::fs::write(&bad_store, []).unwrap(); |
| 246 | let mut candidate = String::new(); |
| 247 | let result = clear_conversation(&mut app, |id| { |
| 248 | candidate = id.to_owned(); |
| 249 | assert!(manager.acquire_offline_queue_lease(id).is_err()); |
| 250 | SessionManager::new(bad_store) |
| 251 | .and_then(|store| store.expect_absent_session(id)) |
| 252 | .map_err(|error| error.to_string()) |
| 253 | }); |
| 254 | assert!(matches!(result, Err(ClearFailure::Transition(_)))); |
| 255 | assert_eq!(app.current_session_id.as_ref(), Some(&old_id)); |
| 256 | assert!(app.session_write_receipt.is_some()); |
| 257 | assert_eq!(app.session_write_owner, Some(old_owner)); |
| 258 | assert_eq!(app.session_title.as_deref(), Some("original")); |
| 259 | assert_eq!(app.input, "draft"); |
| 260 | assert!( |
| 261 | matches!(app.history.last(), Some(HistoryCell::User { content }) if content == "old") |
| 262 | ); |
| 263 | assert!(Arc::ptr_eq( |
| 264 | &old_lease.upgrade().unwrap(), |
| 265 | app.offline_queue_lease.as_ref().unwrap() |
| 266 | )); |
| 267 | assert!(manager.acquire_offline_queue_lease(&old_id).is_err()); |
| 268 | assert!(manager.acquire_offline_queue_lease(&candidate).is_ok()); |
| 269 | } |
| 270 | } |
| 271 |