| 1 | //! Shared command result data. Hosts consume actions after command dispatch; |
| 2 | //! constructing an outcome never executes an action. |
| 3 | |
| 4 | use crate::facets::SessionSyncPayload; |
| 5 | |
| 6 | #[derive(Debug, Clone, PartialEq)] |
| 7 | pub struct CommandResult<A> { |
| 8 | pub message: Option<String>, |
| 9 | pub action: Option<A>, |
| 10 | pub is_error: bool, |
| 11 | } |
| 12 | |
| 13 | impl<A> CommandResult<A> { |
| 14 | pub fn ok() -> Self { |
| 15 | Self { |
| 16 | message: None, |
| 17 | action: None, |
| 18 | is_error: false, |
| 19 | } |
| 20 | } |
| 21 | pub fn message(msg: impl Into<String>) -> Self { |
| 22 | Self { |
| 23 | message: Some(msg.into()), |
| 24 | action: None, |
| 25 | is_error: false, |
| 26 | } |
| 27 | } |
| 28 | pub fn action(action: A) -> Self { |
| 29 | Self { |
| 30 | message: None, |
| 31 | action: Some(action), |
| 32 | is_error: false, |
| 33 | } |
| 34 | } |
| 35 | pub fn with_message_and_action(msg: impl Into<String>, action: A) -> Self { |
| 36 | Self { |
| 37 | message: Some(msg.into()), |
| 38 | action: Some(action), |
| 39 | is_error: false, |
| 40 | } |
| 41 | } |
| 42 | pub fn error(msg: impl Into<String>) -> Self { |
| 43 | Self { |
| 44 | message: Some(format!("Error: {}", msg.into())), |
| 45 | action: None, |
| 46 | is_error: true, |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | /// Complete debug-group action vocabulary. This replaces its TUI AppAction |
| 52 | /// references; other command groups' action ownership remains staged. |
| 53 | #[derive(Debug, Clone, PartialEq)] |
| 54 | pub enum DebugAction { |
| 55 | FetchBalance, |
| 56 | CacheWarmup, |
| 57 | PreviewOutboundRequest { |
| 58 | json: bool, |
| 59 | base_prompt_only: bool, |
| 60 | hypothetical_prompt: Option<String>, |
| 61 | }, |
| 62 | OpenTextPager { |
| 63 | title: String, |
| 64 | content: String, |
| 65 | }, |
| 66 | /// Show a unified diff in a pager, rendered as the transcript renders an |
| 67 | /// edit, without copying it into transcript history. |
| 68 | OpenDiffPager { |
| 69 | title: String, |
| 70 | diff: String, |
| 71 | }, |
| 72 | OpenContextInspector, |
| 73 | SetModelContextWindow { |
| 74 | tokens: Option<u32>, |
| 75 | }, |
| 76 | OpenUndoSelector, |
| 77 | UndoLatestRequest { |
| 78 | force: bool, |
| 79 | }, |
| 80 | SendMessage(String), |
| 81 | SyncSession(SessionSyncPayload), |
| 82 | /// Install and persist a conversation rollback; `/retry` resends its |
| 83 | /// input only after both succeed, replacing the removed exchange (#6788). |
| 84 | ConversationUndo { |
| 85 | sync: SessionSyncPayload, |
| 86 | restored_files: usize, |
| 87 | retry_input: Option<String>, |
| 88 | }, |
| 89 | } |
| 90 | |
| 91 | pub const MIN_CONTEXT_WINDOW_LIMIT_TOKENS: u32 = 4_096; |
| 92 | pub const MAX_CONTEXT_WINDOW_LIMIT_TOKENS: u32 = 10_000_000; |
| 93 | |
| 94 | pub fn parse_context_window_limit(raw: &str) -> Result<u32, String> { |
| 95 | let cleaned: String = raw |
| 96 | .trim() |
| 97 | .chars() |
| 98 | .filter(|ch| *ch != '_' && *ch != ',') |
| 99 | .collect(); |
| 100 | if cleaned.is_empty() { |
| 101 | return Err( |
| 102 | "Enter a token count (e.g. 1000000, 1m, 272k) between 4,096 and 10,000,000." |
| 103 | .to_string(), |
| 104 | ); |
| 105 | } |
| 106 | let lower = cleaned.to_ascii_lowercase(); |
| 107 | let parsed_f64 = if let Some(num) = lower.strip_suffix('m') { |
| 108 | num.parse::<f64>() |
| 109 | .map(|value| (value * 1_000_000.0).round()) |
| 110 | .ok() |
| 111 | } else if let Some(num) = lower.strip_suffix('k') { |
| 112 | num.parse::<f64>() |
| 113 | .map(|value| (value * 1_000.0).round()) |
| 114 | .ok() |
| 115 | } else { |
| 116 | lower.parse::<u64>().ok().map(|value| value as f64) |
| 117 | }; |
| 118 | let Some(tokens_f64) = parsed_f64 else { |
| 119 | return Err(format!( |
| 120 | "Invalid context window '{raw}': use a number like 1000000, 1m, or 272k." |
| 121 | )); |
| 122 | }; |
| 123 | if !tokens_f64.is_finite() |
| 124 | || tokens_f64 < f64::from(MIN_CONTEXT_WINDOW_LIMIT_TOKENS) |
| 125 | || tokens_f64 > f64::from(MAX_CONTEXT_WINDOW_LIMIT_TOKENS) |
| 126 | { |
| 127 | return Err(format!( |
| 128 | "Context window must be between 4,096 and 10,000,000 tokens (got '{raw}')." |
| 129 | )); |
| 130 | } |
| 131 | Ok(tokens_f64 as u32) |
| 132 | } |
| 133 | |
| 134 | pub type DebugCommandResult = CommandResult<DebugAction>; |
| 135 | |
| 136 | /// Structural copy cannot request a host action. |
| 137 | pub type StructcopyCommandResult = CommandResult<std::convert::Infallible>; |
| 138 | |
| 139 | /// Complete session-group vocabulary. Hosts execute these requests after dispatch. |
| 140 | #[derive(Debug, Clone, PartialEq)] |
| 141 | pub enum SessionAction { |
| 142 | CompactContext { |
| 143 | focus: Option<String>, |
| 144 | }, |
| 145 | PurgeContext, |
| 146 | LoadSession(std::path::PathBuf), |
| 147 | SyncSession(SessionSyncPayload), |
| 148 | NewSession { |
| 149 | sync: SessionSyncPayload, |
| 150 | force: bool, |
| 151 | draft: Option<String>, |
| 152 | }, |
| 153 | SendMessage(String), |
| 154 | RemoteControl(SessionRemoteControlAction), |
| 155 | OpenExternalUrl { |
| 156 | url: String, |
| 157 | label: String, |
| 158 | }, |
| 159 | } |
| 160 | |
| 161 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 162 | pub enum SessionRemoteControlAction { |
| 163 | Start, |
| 164 | Stop, |
| 165 | } |
| 166 | |
| 167 | pub type SessionCommandResult = CommandResult<SessionAction>; |
| 168 | |
| 169 | /// Complete config-group action vocabulary (FEAT-028 D5). Unifies the FEAT-027 |
| 170 | /// policy action with the auth, picker, view, screen-mode, trust and |
| 171 | /// host-effect actions of the main config slice. Constructing one never |
| 172 | /// executes it; the TUI host adapter converts it to a concrete `AppAction` |
| 173 | /// outside the movable closure. |
| 174 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 175 | pub enum ConfigAction { |
| 176 | /// A permission rule was safely removed; the host reloads active rules. |
| 177 | PermissionRulesChanged, |
| 178 | ApproveAutoReviewRetry { |
| 179 | target: String, |
| 180 | }, |
| 181 | // Auth flows (no host observation needed). |
| 182 | OpenModelConnectionSetup, |
| 183 | StartAccountConnect, |
| 184 | StartAccountLogin, |
| 185 | StartAccountLogout, |
| 186 | StartXaiDeviceLogin, |
| 187 | StartChatgptPkceLogin, |
| 188 | StartChatgptRevoke, |
| 189 | StartOrcarouterPkceLogin, |
| 190 | StartOrcarouterRevoke, |
| 191 | // Views and pickers. |
| 192 | OpenConfigView, |
| 193 | OpenModePicker, |
| 194 | OpenPlanArtifact { |
| 195 | arg: Option<String>, |
| 196 | }, |
| 197 | SetConversationMode { |
| 198 | id: String, |
| 199 | }, |
| 200 | OpenThemePicker, |
| 201 | OpenStatusPicker, |
| 202 | OpenProviderPicker, |
| 203 | StartPluginLogin { |
| 204 | provider: String, |
| 205 | }, |
| 206 | StartPluginLogout { |
| 207 | provider: String, |
| 208 | }, |
| 209 | SetWorkspaceTrust { |
| 210 | trusted: bool, |
| 211 | save: bool, |
| 212 | }, |
| 213 | // Engine/route effects carrying the exact host payload. |
| 214 | ModeChanged(crate::types::CommandMode), |
| 215 | ApprovalPolicyChanged { |
| 216 | policy: Option<String>, |
| 217 | }, |
| 218 | SwitchProvider { |
| 219 | provider: crate::types::CommandProviderId, |
| 220 | model: Option<String>, |
| 221 | }, |
| 222 | CompactionChanged(crate::config::CommandCompactionConfig), |
| 223 | StreamChunkTimeoutChanged(u64), |
| 224 | SubagentRuntimeChanged(crate::config::CommandSubagentRuntimeConfig), |
| 225 | SearchProviderChanged(crate::config::CommandSearchProvider), |
| 226 | PromptSuggestionChanged { |
| 227 | enabled: bool, |
| 228 | }, |
| 229 | AutoReviewReviewerModelChanged { |
| 230 | reviewer_model: Option<String>, |
| 231 | }, |
| 232 | NotificationChanged(crate::config::CommandNotificationUpdate), |
| 233 | } |
| 234 | pub type ConfigCommandResult = CommandResult<ConfigAction>; |
| 235 | /// FEAT-027 compatibility alias; the whole group now standardizes on `ConfigAction`. |
| 236 | pub type ConfigPolicyAction = ConfigAction; |
| 237 | pub type ConfigPolicyCommandResult = ConfigCommandResult; |
| 238 | pub type ConfigStatusCommandResult = CommandResult<std::convert::Infallible>; |
| 239 | |
| 240 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 241 | pub enum CoreAction { |
| 242 | LaunchSubAgent { task: String, max_depth: u32 }, |
| 243 | CancelSubAgent { agent_id: String }, |
| 244 | SendMessage(String), |
| 245 | ListSubAgents, |
| 246 | AgentProfiles { arguments: String }, |
| 247 | Quit, |
| 248 | FetchModels, |
| 249 | SetAdvisorEnabled { enabled: bool }, |
| 250 | SwitchProfile { profile: String }, |
| 251 | } |
| 252 | |
| 253 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 254 | pub enum CoreNavigationAction { |
| 255 | OpenHotbarSetup, |
| 256 | DisableHotbar, |
| 257 | RestoreHotbarDefaults, |
| 258 | OpenLiveTranscript, |
| 259 | OpenTurnInspector, |
| 260 | OpenWorktreeManager, |
| 261 | OpenAgentBoard, |
| 262 | SwitchWorkspace { workspace: std::path::PathBuf }, |
| 263 | VoiceCapture, |
| 264 | } |
| 265 | |
| 266 | pub type CoreNavigationCommandResult = CommandResult<CoreNavigationAction>; |
| 267 | |
| 268 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 269 | pub enum BackgroundAction { |
| 270 | SendMessage(String), |
| 271 | } |
| 272 | |
| 273 | pub type BackgroundCommandResult = CommandResult<BackgroundAction>; |
| 274 |