| 1 | use codewhale_command_contract::core_policy::*; |
| 2 | use codewhale_command_contract::handler::{ |
| 3 | CommandCapabilities as Caps, CommandContexts, CommandHandler, |
| 4 | }; |
| 5 | use codewhale_command_contract::metadata::{CommandInfo, RegisterCommand}; |
| 6 | use codewhale_command_contract::outcome::CommandResult; |
| 7 | |
| 8 | pub const SECURITY_POLICY_URL: &str = "https://github.com/codewhale-hq/CodeWhale/security/policy"; |
| 9 | pub const FEATURE_URL: &str = |
| 10 | "https://github.com/codewhale-hq/CodeWhale/issues/new?template=feature_request.yml"; |
| 11 | |
| 12 | pub struct ClearCmd; |
| 13 | impl<A: From<ClearedConversation>> RegisterCommand<CommandResult<A>> for ClearCmd { |
| 14 | fn info() -> &'static CommandInfo { |
| 15 | &CommandInfo { |
| 16 | name: "clear", |
| 17 | aliases: &["qingping"], |
| 18 | usage: "/clear", |
| 19 | description_key: "cmd_clear_description", |
| 20 | } |
| 21 | } |
| 22 | fn handler() -> CommandHandler<CommandResult<A>> { |
| 23 | CommandHandler::Contextual { |
| 24 | capabilities: Caps::CLEAR.union(Caps::PRESENTATION), |
| 25 | handler: |ctx, _| clear(ctx).unwrap_or_else(CommandResult::error), |
| 26 | } |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | fn clear<A: From<ClearedConversation>>( |
| 31 | mut ctx: CommandContexts<'_>, |
| 32 | ) -> Result<CommandResult<A>, String> { |
| 33 | let lifecycle = ctx |
| 34 | .take_clear() |
| 35 | .ok_or("Command capability unavailable: conversation reset")?; |
| 36 | let text = ctx |
| 37 | .into_parts() |
| 38 | .presentation |
| 39 | .ok_or("Command capability unavailable: presentation")?; |
| 40 | let success = text.translate("clear_conversation", &[])?; |
| 41 | match lifecycle.start_empty_conversation() { |
| 42 | Ok(receipt) => Ok(CommandResult::with_message_and_action( |
| 43 | success, |
| 44 | receipt.into(), |
| 45 | )), |
| 46 | Err(ClearFailure::Transition(reason)) => Err(reason), |
| 47 | Err(ClearFailure::Busy(blockers)) => { |
| 48 | let mut message = text.translate("clear_conversation_busy", &[])?; |
| 49 | for blocker in blockers { |
| 50 | message.push_str("\n • "); |
| 51 | message.push_str(&blocker); |
| 52 | } |
| 53 | Err(message) |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | pub struct EffortCmd; |
| 59 | impl<A> RegisterCommand<CommandResult<A>> for EffortCmd { |
| 60 | fn info() -> &'static CommandInfo { |
| 61 | &CommandInfo { |
| 62 | name: "effort", |
| 63 | aliases: &["thinking", "reasoning", "reason"], |
| 64 | usage: "/effort [<none|minimal|low|medium|high|xhigh|ultra|max|auto|off>]", |
| 65 | description_key: "cmd_effort_description", |
| 66 | } |
| 67 | } |
| 68 | fn handler() -> CommandHandler<CommandResult<A>> { |
| 69 | CommandHandler::Contextual { |
| 70 | capabilities: Caps::EFFORT.union(Caps::PRESENTATION), |
| 71 | handler: |ctx, arg| effort(ctx, arg).unwrap_or_else(CommandResult::error), |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | fn effort<A>(mut ctx: CommandContexts<'_>, arg: Option<&str>) -> Result<CommandResult<A>, String> { |
| 77 | let effort = ctx |
| 78 | .take_effort() |
| 79 | .ok_or("Command capability unavailable: reasoning effort")?; |
| 80 | let text = ctx |
| 81 | .into_parts() |
| 82 | .presentation |
| 83 | .ok_or("Command capability unavailable: presentation")?; |
| 84 | let arg = arg.unwrap_or("").trim().to_ascii_lowercase(); |
| 85 | let label_list = |labels: Vec<String>| { |
| 86 | if labels.is_empty() { |
| 87 | "none|minimal|low|medium|high|xhigh|ultra|max|auto|off".to_string() |
| 88 | } else { |
| 89 | labels.join("|") |
| 90 | } |
| 91 | }; |
| 92 | if arg.is_empty() { |
| 93 | let labels = label_list(effort.available_labels()); |
| 94 | let (prev, next) = effort.cycle_checked()?; |
| 95 | return Ok(CommandResult::message(format!( |
| 96 | "Effort: {prev} → {next} (available: {labels})" |
| 97 | ))); |
| 98 | } |
| 99 | match effort.set_supported_effort_checked(&arg) { |
| 100 | Err(EffortChangeFailure::Receipt(message)) => Err(message), |
| 101 | Ok(label) => Ok(CommandResult::message(format!("Effort set to {label}"))), |
| 102 | Err(EffortChangeFailure::Selection(EffortFailure::Unknown)) => Err(format!( |
| 103 | "{} — Usage: /effort <{}> (also /thinking)", |
| 104 | text.translate("cmd_effort_description", &[])?, |
| 105 | label_list(effort.available_labels()) |
| 106 | )), |
| 107 | Err(EffortChangeFailure::Selection(EffortFailure::Unavailable { model, labels })) => { |
| 108 | Err(format!( |
| 109 | "'{arg}' not available for {model} — available: {}", |
| 110 | label_list(labels) |
| 111 | )) |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | pub struct FeedbackCmd; |
| 117 | impl<A: From<FeedbackAction>> RegisterCommand<CommandResult<A>> for FeedbackCmd { |
| 118 | fn info() -> &'static CommandInfo { |
| 119 | &CommandInfo { |
| 120 | name: "feedback", |
| 121 | aliases: &[], |
| 122 | usage: "/feedback [bug [focus]|review <id>|edit <id> <change>|feature|security]", |
| 123 | description_key: "cmd_feedback_description", |
| 124 | } |
| 125 | } |
| 126 | fn handler() -> CommandHandler<CommandResult<A>> { |
| 127 | CommandHandler::Contextual { |
| 128 | capabilities: Caps::FEEDBACK.union(Caps::PRESENTATION), |
| 129 | handler: |ctx, arg| feedback(ctx, arg).unwrap_or_else(CommandResult::error), |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | fn feedback<A: From<FeedbackAction>>( |
| 135 | mut ctx: CommandContexts<'_>, |
| 136 | arg: Option<&str>, |
| 137 | ) -> Result<CommandResult<A>, String> { |
| 138 | let report = ctx |
| 139 | .take_feedback() |
| 140 | .ok_or("Command capability unavailable: feedback")?; |
| 141 | let text = ctx |
| 142 | .into_parts() |
| 143 | .presentation |
| 144 | .ok_or("Command capability unavailable: presentation")?; |
| 145 | let help = || { |
| 146 | text.translate("feedback_help", &[]).map(|intro| format!("{intro}\n\n/feedback bug [focus]\n/feedback review <id>\n/feedback edit <id> <change>\n/feedback feature\n/feedback security")) |
| 147 | }; |
| 148 | let raw = arg.map(str::trim).unwrap_or(""); |
| 149 | if raw.is_empty() { |
| 150 | return Ok(CommandResult::action(FeedbackAction::OpenPicker.into())); |
| 151 | } |
| 152 | let (kind, rest) = raw.split_once(char::is_whitespace).unwrap_or((raw, "")); |
| 153 | let rest = rest.trim(); |
| 154 | Ok(match kind.to_ascii_lowercase().as_str() { |
| 155 | "help" | "--help" | "-h" => CommandResult::message(help()?), |
| 156 | "1" | "bug" | "bug-report" | "bug_report" => { |
| 157 | if !report.has_session() { |
| 158 | return Err(text.translate("feedback_no_session", &[])?); |
| 159 | } |
| 160 | let focus = if rest.is_empty() { |
| 161 | String::new() |
| 162 | } else { |
| 163 | report.safe_text(rest)? |
| 164 | }; |
| 165 | CommandResult::with_message_and_action( |
| 166 | text.translate("feedback_draft_requested", &[])?, |
| 167 | FeedbackAction::Draft(focus).into(), |
| 168 | ) |
| 169 | } |
| 170 | "review" | "edit" => { |
| 171 | if !report.has_session() { |
| 172 | return Err(text.translate("feedback_no_session", &[])?); |
| 173 | }; |
| 174 | let (id, change) = rest.split_once(char::is_whitespace).unwrap_or((rest, "")); |
| 175 | let editing = kind.eq_ignore_ascii_case("edit"); |
| 176 | if id.is_empty() |
| 177 | || (editing && change.trim().is_empty()) |
| 178 | || (!editing && !change.trim().is_empty()) |
| 179 | { |
| 180 | return Err(help()?); |
| 181 | } |
| 182 | let change = if editing { |
| 183 | Some(report.safe_text(change)?) |
| 184 | } else { |
| 185 | None |
| 186 | }; |
| 187 | CommandResult::action( |
| 188 | FeedbackAction::Review { |
| 189 | id: id.into(), |
| 190 | change, |
| 191 | } |
| 192 | .into(), |
| 193 | ) |
| 194 | } |
| 195 | "2" | "feature" | "feature-request" | "feature_request" | "enhancement" |
| 196 | if rest.is_empty() => |
| 197 | { |
| 198 | CommandResult::with_message_and_action( |
| 199 | format!( |
| 200 | "Trying to open GitHub feature request template in your browser. If that fails, open this URL manually:\n\n{FEATURE_URL}" |
| 201 | ), |
| 202 | FeedbackAction::Feature.into(), |
| 203 | ) |
| 204 | } |
| 205 | "3" | "security" | "vulnerability" | "private" if rest.is_empty() => { |
| 206 | CommandResult::with_message_and_action( |
| 207 | format!( |
| 208 | "Review the project's security policy before reporting a vulnerability.\n\nTrying to open it in your browser. If that fails, open this URL manually:\n\n{SECURITY_POLICY_URL}\n\nDo not include sensitive security details in a public issue." |
| 209 | ), |
| 210 | FeedbackAction::Security.into(), |
| 211 | ) |
| 212 | } |
| 213 | _ => CommandResult::error(help()?), |
| 214 | }) |
| 215 | } |
| 216 |