| 1 | use std::path::PathBuf; |
| 2 | |
| 3 | use anyhow::{Result, bail}; |
| 4 | use clap::Args; |
| 5 | use codewhale_config::ConfigStore; |
| 6 | |
| 7 | #[derive(Debug, Args)] |
| 8 | pub struct DispatchArgs { |
| 9 | #[command(flatten)] |
| 10 | computer: crate::computer::ComputerOptions, |
| 11 | /// Task for the remote agent. Required unless inspecting or cancelling a job. |
| 12 | #[arg(value_name = "PROMPT")] |
| 13 | prompt: Vec<String>, |
| 14 | /// Inspect one account job. |
| 15 | #[arg(long, value_name = "ID")] |
| 16 | show: Option<String>, |
| 17 | /// Cancel one account job. |
| 18 | #[arg(long, value_name = "ID")] |
| 19 | cancel: Option<String>, |
| 20 | /// Account dispatch: Agent name or ID (default: the repository-free default Agent). |
| 21 | #[arg(long)] |
| 22 | agent: Option<String>, |
| 23 | /// Account dispatch: task time in seconds (default: what your offer allows). |
| 24 | #[arg(long)] |
| 25 | seconds: Option<u64>, |
| 26 | /// Account dispatch: UTF-8 source context file. |
| 27 | #[arg(long, value_name = "PATH")] |
| 28 | context_file: Option<PathBuf>, |
| 29 | /// Account dispatch: saved account file FILE_ID@VERSION; repeatable. |
| 30 | #[arg(long = "file", value_name = "FILE_ID@VERSION")] |
| 31 | file_refs: Vec<String>, |
| 32 | /// Account dispatch: skip the question (needs --confirm-eu-compute). |
| 33 | #[arg(long)] |
| 34 | yes: bool, |
| 35 | /// Account dispatch: agree that repository code and Work files run on EU compute. |
| 36 | #[arg(long)] |
| 37 | confirm_eu_compute: bool, |
| 38 | /// Account dispatch: replay key for a retry after an uncertain response. |
| 39 | #[arg(long)] |
| 40 | operation_key: Option<String>, |
| 41 | /// Account dispatch: message ID for a retry after an uncertain response. |
| 42 | #[arg(long)] |
| 43 | message_id: Option<String>, |
| 44 | } |
| 45 | |
| 46 | fn unknown_job(id: &str) -> anyhow::Error { |
| 47 | anyhow::anyhow!("Unknown job {id}: it is not a Codewhale account job ID.") |
| 48 | } |
| 49 | |
| 50 | pub fn run(args: DispatchArgs, profile: Option<&str>, config: &mut ConfigStore) -> Result<()> { |
| 51 | use crate::{AccountDispatch, run_dispatch}; |
| 52 | let legacy_work = args.agent.is_some() |
| 53 | || args.seconds.is_some() |
| 54 | || args.context_file.is_some() |
| 55 | || !args.file_refs.is_empty() |
| 56 | || args.message_id.is_some() |
| 57 | || args.yes |
| 58 | || args.confirm_eu_compute |
| 59 | || args.show.is_some() |
| 60 | || args.cancel.is_some(); |
| 61 | if args.computer.selected() || !legacy_work { |
| 62 | if args.agent.is_some() |
| 63 | || args.seconds.is_some() |
| 64 | || args.context_file.is_some() |
| 65 | || !args.file_refs.is_empty() |
| 66 | || args.message_id.is_some() |
| 67 | || args.yes |
| 68 | || args.confirm_eu_compute |
| 69 | { |
| 70 | bail!( |
| 71 | "Cloud conversation dispatch uses --computer or --thread. Agent Work flags cannot be combined with it." |
| 72 | ); |
| 73 | } |
| 74 | if args.show.is_some() && args.cancel.is_some() |
| 75 | || (!args.prompt.is_empty() && (args.show.is_some() || args.cancel.is_some())) |
| 76 | { |
| 77 | bail!("Choose a prompt, --show, or --cancel."); |
| 78 | } |
| 79 | let mut options = args.computer; |
| 80 | if let Some(id) = args.show.as_ref().or(args.cancel.as_ref()) { |
| 81 | if options.thread.as_ref().is_some_and(|thread| thread != id) { |
| 82 | bail!("Choose one conversation ID."); |
| 83 | } |
| 84 | options.thread = Some(id.clone()); |
| 85 | options.stop = args.cancel.is_some(); |
| 86 | } |
| 87 | return run_dispatch( |
| 88 | AccountDispatch::Computer(crate::computer::SessionArgs { |
| 89 | prompt: args.prompt, |
| 90 | options, |
| 91 | operation_key: args.operation_key, |
| 92 | }), |
| 93 | profile, |
| 94 | config, |
| 95 | ); |
| 96 | } |
| 97 | let modes = [ |
| 98 | args.show.is_some(), |
| 99 | args.cancel.is_some(), |
| 100 | !args.prompt.is_empty(), |
| 101 | ]; |
| 102 | if modes.iter().filter(|mode| **mode).count() > 1 { |
| 103 | bail!("Use one of: a prompt, --show <id>, or --cancel <id>."); |
| 104 | } |
| 105 | if let Some(id) = args.show.as_deref() { |
| 106 | if id.starts_with("cloud_") { |
| 107 | return Err(unknown_job(id)); |
| 108 | } |
| 109 | return run_dispatch(AccountDispatch::Status(id.to_string()), profile, config); |
| 110 | } |
| 111 | if let Some(id) = args.cancel.as_deref() { |
| 112 | if id.starts_with("cloud_") { |
| 113 | return Err(unknown_job(id)); |
| 114 | } |
| 115 | return run_dispatch(AccountDispatch::Cancel(id.to_string()), profile, config); |
| 116 | } |
| 117 | if args.prompt.is_empty() { |
| 118 | bail!("Give a task to dispatch, or use --show <id> or --cancel <id>."); |
| 119 | } |
| 120 | run_dispatch( |
| 121 | AccountDispatch::Run { |
| 122 | objective: args.prompt.join(" "), |
| 123 | agent: args.agent, |
| 124 | seconds: args.seconds, |
| 125 | context_file: args.context_file, |
| 126 | file_refs: args.file_refs, |
| 127 | operation_key: args.operation_key, |
| 128 | message_id: args.message_id, |
| 129 | yes: args.yes, |
| 130 | confirm_eu_compute: args.confirm_eu_compute, |
| 131 | }, |
| 132 | profile, |
| 133 | config, |
| 134 | ) |
| 135 | } |
| 136 | |
| 137 | #[cfg(test)] |
| 138 | mod tests { |
| 139 | use super::*; |
| 140 | use crate::tests::{Cli, Commands}; |
| 141 | use clap::Parser; |
| 142 | |
| 143 | fn args(argv: &[&str]) -> DispatchArgs { |
| 144 | let cli = Cli::try_parse_from(argv).unwrap(); |
| 145 | let Some(Commands::Dispatch(args)) = cli.command else { |
| 146 | panic!("expected dispatch command"); |
| 147 | }; |
| 148 | args |
| 149 | } |
| 150 | |
| 151 | #[test] |
| 152 | fn parses_the_obvious_dispatch_command() { |
| 153 | let parsed = args(&[ |
| 154 | "codewhale", |
| 155 | "dispatch", |
| 156 | "fix", |
| 157 | "the", |
| 158 | "flake", |
| 159 | "--seconds", |
| 160 | "600", |
| 161 | ]); |
| 162 | assert_eq!(parsed.prompt, ["fix", "the", "flake"]); |
| 163 | assert_eq!(parsed.seconds, Some(600)); |
| 164 | assert!(Cli::try_parse_from(["codewhale", "dispatch", "--remote", "github"]).is_err()); |
| 165 | } |
| 166 | |
| 167 | #[test] |
| 168 | fn rendered_help_carries_no_provider_brand() { |
| 169 | use clap::CommandFactory; |
| 170 | let help = Cli::command() |
| 171 | .find_subcommand_mut("dispatch") |
| 172 | .expect("dispatch subcommand exists") |
| 173 | .render_help() |
| 174 | .to_string(); |
| 175 | assert!(help.contains("--show"), "{help}"); |
| 176 | assert!(!help.contains("--own-sandbox"), "{help}"); |
| 177 | for banned in ["Daytona", "daytona"] { |
| 178 | assert!( |
| 179 | !help.contains(banned), |
| 180 | "--help must not brand the operator: {banned}" |
| 181 | ); |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 |