lib.rs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. use runtime::{compact_session, CompactionConfig, Session};
  2. #[derive(Debug, Clone, PartialEq, Eq)]
  3. pub struct CommandManifestEntry {
  4. pub name: String,
  5. pub source: CommandSource,
  6. }
  7. #[derive(Debug, Clone, Copy, PartialEq, Eq)]
  8. pub enum CommandSource {
  9. Builtin,
  10. InternalOnly,
  11. FeatureGated,
  12. }
  13. #[derive(Debug, Clone, Default, PartialEq, Eq)]
  14. pub struct CommandRegistry {
  15. entries: Vec<CommandManifestEntry>,
  16. }
  17. impl CommandRegistry {
  18. #[must_use]
  19. pub fn new(entries: Vec<CommandManifestEntry>) -> Self {
  20. Self { entries }
  21. }
  22. #[must_use]
  23. pub fn entries(&self) -> &[CommandManifestEntry] {
  24. &self.entries
  25. }
  26. }
  27. #[derive(Debug, Clone, Copy, PartialEq, Eq)]
  28. pub struct SlashCommandSpec {
  29. pub name: &'static str,
  30. pub summary: &'static str,
  31. pub argument_hint: Option<&'static str>,
  32. pub resume_supported: bool,
  33. }
  34. const SLASH_COMMAND_SPECS: &[SlashCommandSpec] = &[
  35. SlashCommandSpec {
  36. name: "help",
  37. summary: "Show available slash commands",
  38. argument_hint: None,
  39. resume_supported: true,
  40. },
  41. SlashCommandSpec {
  42. name: "status",
  43. summary: "Show current session status",
  44. argument_hint: None,
  45. resume_supported: true,
  46. },
  47. SlashCommandSpec {
  48. name: "compact",
  49. summary: "Compact local session history",
  50. argument_hint: None,
  51. resume_supported: true,
  52. },
  53. SlashCommandSpec {
  54. name: "model",
  55. summary: "Show or switch the active model",
  56. argument_hint: Some("[model]"),
  57. resume_supported: false,
  58. },
  59. SlashCommandSpec {
  60. name: "permissions",
  61. summary: "Show or switch the active permission mode",
  62. argument_hint: Some("[read-only|workspace-write|danger-full-access]"),
  63. resume_supported: false,
  64. },
  65. SlashCommandSpec {
  66. name: "clear",
  67. summary: "Start a fresh local session",
  68. argument_hint: Some("[--confirm]"),
  69. resume_supported: true,
  70. },
  71. SlashCommandSpec {
  72. name: "cost",
  73. summary: "Show cumulative token usage for this session",
  74. argument_hint: None,
  75. resume_supported: true,
  76. },
  77. SlashCommandSpec {
  78. name: "resume",
  79. summary: "Load a saved session into the REPL",
  80. argument_hint: Some("<session-path>"),
  81. resume_supported: false,
  82. },
  83. SlashCommandSpec {
  84. name: "config",
  85. summary: "Inspect Claude config files or merged sections",
  86. argument_hint: Some("[env|hooks|model]"),
  87. resume_supported: true,
  88. },
  89. SlashCommandSpec {
  90. name: "memory",
  91. summary: "Inspect loaded Claude instruction memory files",
  92. argument_hint: None,
  93. resume_supported: true,
  94. },
  95. SlashCommandSpec {
  96. name: "init",
  97. summary: "Create a starter CLAUDE.md for this repo",
  98. argument_hint: None,
  99. resume_supported: true,
  100. },
  101. SlashCommandSpec {
  102. name: "diff",
  103. summary: "Show git diff for current workspace changes",
  104. argument_hint: None,
  105. resume_supported: true,
  106. },
  107. SlashCommandSpec {
  108. name: "version",
  109. summary: "Show CLI version and build information",
  110. argument_hint: None,
  111. resume_supported: true,
  112. },
  113. SlashCommandSpec {
  114. name: "export",
  115. summary: "Export the current conversation to a file",
  116. argument_hint: Some("[file]"),
  117. resume_supported: true,
  118. },
  119. SlashCommandSpec {
  120. name: "session",
  121. summary: "List or switch managed local sessions",
  122. argument_hint: Some("[list|switch <session-id>]"),
  123. resume_supported: false,
  124. },
  125. ];
  126. #[derive(Debug, Clone, PartialEq, Eq)]
  127. pub enum SlashCommand {
  128. Help,
  129. Status,
  130. Compact,
  131. Model {
  132. model: Option<String>,
  133. },
  134. Permissions {
  135. mode: Option<String>,
  136. },
  137. Clear {
  138. confirm: bool,
  139. },
  140. Cost,
  141. Resume {
  142. session_path: Option<String>,
  143. },
  144. Config {
  145. section: Option<String>,
  146. },
  147. Memory,
  148. Init,
  149. Diff,
  150. Version,
  151. Export {
  152. path: Option<String>,
  153. },
  154. Session {
  155. action: Option<String>,
  156. target: Option<String>,
  157. },
  158. Unknown(String),
  159. }
  160. impl SlashCommand {
  161. #[must_use]
  162. pub fn parse(input: &str) -> Option<Self> {
  163. let trimmed = input.trim();
  164. if !trimmed.starts_with('/') {
  165. return None;
  166. }
  167. let mut parts = trimmed.trim_start_matches('/').split_whitespace();
  168. let command = parts.next().unwrap_or_default();
  169. Some(match command {
  170. "help" => Self::Help,
  171. "status" => Self::Status,
  172. "compact" => Self::Compact,
  173. "model" => Self::Model {
  174. model: parts.next().map(ToOwned::to_owned),
  175. },
  176. "permissions" => Self::Permissions {
  177. mode: parts.next().map(ToOwned::to_owned),
  178. },
  179. "clear" => Self::Clear {
  180. confirm: parts.next() == Some("--confirm"),
  181. },
  182. "cost" => Self::Cost,
  183. "resume" => Self::Resume {
  184. session_path: parts.next().map(ToOwned::to_owned),
  185. },
  186. "config" => Self::Config {
  187. section: parts.next().map(ToOwned::to_owned),
  188. },
  189. "memory" => Self::Memory,
  190. "init" => Self::Init,
  191. "diff" => Self::Diff,
  192. "version" => Self::Version,
  193. "export" => Self::Export {
  194. path: parts.next().map(ToOwned::to_owned),
  195. },
  196. "session" => Self::Session {
  197. action: parts.next().map(ToOwned::to_owned),
  198. target: parts.next().map(ToOwned::to_owned),
  199. },
  200. other => Self::Unknown(other.to_string()),
  201. })
  202. }
  203. }
  204. #[must_use]
  205. pub fn slash_command_specs() -> &'static [SlashCommandSpec] {
  206. SLASH_COMMAND_SPECS
  207. }
  208. #[must_use]
  209. pub fn resume_supported_slash_commands() -> Vec<&'static SlashCommandSpec> {
  210. slash_command_specs()
  211. .iter()
  212. .filter(|spec| spec.resume_supported)
  213. .collect()
  214. }
  215. #[must_use]
  216. pub fn render_slash_command_help() -> String {
  217. let mut lines = vec![
  218. "Slash commands".to_string(),
  219. " [resume] means the command also works with --resume SESSION.json".to_string(),
  220. ];
  221. for spec in slash_command_specs() {
  222. let name = match spec.argument_hint {
  223. Some(argument_hint) => format!("/{} {}", spec.name, argument_hint),
  224. None => format!("/{}", spec.name),
  225. };
  226. let resume = if spec.resume_supported {
  227. " [resume]"
  228. } else {
  229. ""
  230. };
  231. lines.push(format!(" {name:<20} {}{}", spec.summary, resume));
  232. }
  233. lines.join("\n")
  234. }
  235. #[derive(Debug, Clone, PartialEq, Eq)]
  236. pub struct SlashCommandResult {
  237. pub message: String,
  238. pub session: Session,
  239. }
  240. #[must_use]
  241. pub fn handle_slash_command(
  242. input: &str,
  243. session: &Session,
  244. compaction: CompactionConfig,
  245. ) -> Option<SlashCommandResult> {
  246. match SlashCommand::parse(input)? {
  247. SlashCommand::Compact => {
  248. let result = compact_session(session, compaction);
  249. let message = if result.removed_message_count == 0 {
  250. "Compaction skipped: session is below the compaction threshold.".to_string()
  251. } else {
  252. format!(
  253. "Compacted {} messages into a resumable system summary.",
  254. result.removed_message_count
  255. )
  256. };
  257. Some(SlashCommandResult {
  258. message,
  259. session: result.compacted_session,
  260. })
  261. }
  262. SlashCommand::Help => Some(SlashCommandResult {
  263. message: render_slash_command_help(),
  264. session: session.clone(),
  265. }),
  266. SlashCommand::Status
  267. | SlashCommand::Model { .. }
  268. | SlashCommand::Permissions { .. }
  269. | SlashCommand::Clear { .. }
  270. | SlashCommand::Cost
  271. | SlashCommand::Resume { .. }
  272. | SlashCommand::Config { .. }
  273. | SlashCommand::Memory
  274. | SlashCommand::Init
  275. | SlashCommand::Diff
  276. | SlashCommand::Version
  277. | SlashCommand::Export { .. }
  278. | SlashCommand::Session { .. }
  279. | SlashCommand::Unknown(_) => None,
  280. }
  281. }
  282. #[cfg(test)]
  283. mod tests {
  284. use super::{
  285. handle_slash_command, render_slash_command_help, resume_supported_slash_commands,
  286. slash_command_specs, SlashCommand,
  287. };
  288. use runtime::{CompactionConfig, ContentBlock, ConversationMessage, MessageRole, Session};
  289. #[test]
  290. fn parses_supported_slash_commands() {
  291. assert_eq!(SlashCommand::parse("/help"), Some(SlashCommand::Help));
  292. assert_eq!(SlashCommand::parse(" /status "), Some(SlashCommand::Status));
  293. assert_eq!(
  294. SlashCommand::parse("/model claude-opus"),
  295. Some(SlashCommand::Model {
  296. model: Some("claude-opus".to_string()),
  297. })
  298. );
  299. assert_eq!(
  300. SlashCommand::parse("/model"),
  301. Some(SlashCommand::Model { model: None })
  302. );
  303. assert_eq!(
  304. SlashCommand::parse("/permissions read-only"),
  305. Some(SlashCommand::Permissions {
  306. mode: Some("read-only".to_string()),
  307. })
  308. );
  309. assert_eq!(
  310. SlashCommand::parse("/clear"),
  311. Some(SlashCommand::Clear { confirm: false })
  312. );
  313. assert_eq!(
  314. SlashCommand::parse("/clear --confirm"),
  315. Some(SlashCommand::Clear { confirm: true })
  316. );
  317. assert_eq!(SlashCommand::parse("/cost"), Some(SlashCommand::Cost));
  318. assert_eq!(
  319. SlashCommand::parse("/resume session.json"),
  320. Some(SlashCommand::Resume {
  321. session_path: Some("session.json".to_string()),
  322. })
  323. );
  324. assert_eq!(
  325. SlashCommand::parse("/config"),
  326. Some(SlashCommand::Config { section: None })
  327. );
  328. assert_eq!(
  329. SlashCommand::parse("/config env"),
  330. Some(SlashCommand::Config {
  331. section: Some("env".to_string())
  332. })
  333. );
  334. assert_eq!(SlashCommand::parse("/memory"), Some(SlashCommand::Memory));
  335. assert_eq!(SlashCommand::parse("/init"), Some(SlashCommand::Init));
  336. assert_eq!(SlashCommand::parse("/diff"), Some(SlashCommand::Diff));
  337. assert_eq!(SlashCommand::parse("/version"), Some(SlashCommand::Version));
  338. assert_eq!(
  339. SlashCommand::parse("/export notes.txt"),
  340. Some(SlashCommand::Export {
  341. path: Some("notes.txt".to_string())
  342. })
  343. );
  344. assert_eq!(
  345. SlashCommand::parse("/session switch abc123"),
  346. Some(SlashCommand::Session {
  347. action: Some("switch".to_string()),
  348. target: Some("abc123".to_string())
  349. })
  350. );
  351. }
  352. #[test]
  353. fn renders_help_from_shared_specs() {
  354. let help = render_slash_command_help();
  355. assert!(help.contains("works with --resume SESSION.json"));
  356. assert!(help.contains("/help"));
  357. assert!(help.contains("/status"));
  358. assert!(help.contains("/compact"));
  359. assert!(help.contains("/model [model]"));
  360. assert!(help.contains("/permissions [read-only|workspace-write|danger-full-access]"));
  361. assert!(help.contains("/clear [--confirm]"));
  362. assert!(help.contains("/cost"));
  363. assert!(help.contains("/resume <session-path>"));
  364. assert!(help.contains("/config [env|hooks|model]"));
  365. assert!(help.contains("/memory"));
  366. assert!(help.contains("/init"));
  367. assert!(help.contains("/diff"));
  368. assert!(help.contains("/version"));
  369. assert!(help.contains("/export [file]"));
  370. assert!(help.contains("/session [list|switch <session-id>]"));
  371. assert_eq!(slash_command_specs().len(), 15);
  372. assert_eq!(resume_supported_slash_commands().len(), 11);
  373. }
  374. #[test]
  375. fn compacts_sessions_via_slash_command() {
  376. let session = Session {
  377. version: 1,
  378. messages: vec![
  379. ConversationMessage::user_text("a ".repeat(200)),
  380. ConversationMessage::assistant(vec![ContentBlock::Text {
  381. text: "b ".repeat(200),
  382. }]),
  383. ConversationMessage::tool_result("1", "bash", "ok ".repeat(200), false),
  384. ConversationMessage::assistant(vec![ContentBlock::Text {
  385. text: "recent".to_string(),
  386. }]),
  387. ],
  388. };
  389. let result = handle_slash_command(
  390. "/compact",
  391. &session,
  392. CompactionConfig {
  393. preserve_recent_messages: 2,
  394. max_estimated_tokens: 1,
  395. },
  396. )
  397. .expect("slash command should be handled");
  398. assert!(result.message.contains("Compacted 2 messages"));
  399. assert_eq!(result.session.messages[0].role, MessageRole::System);
  400. }
  401. #[test]
  402. fn help_command_is_non_mutating() {
  403. let session = Session::new();
  404. let result = handle_slash_command("/help", &session, CompactionConfig::default())
  405. .expect("help command should be handled");
  406. assert_eq!(result.session, session);
  407. assert!(result.message.contains("Slash commands"));
  408. }
  409. #[test]
  410. fn ignores_unknown_or_runtime_bound_slash_commands() {
  411. let session = Session::new();
  412. assert!(handle_slash_command("/unknown", &session, CompactionConfig::default()).is_none());
  413. assert!(handle_slash_command("/status", &session, CompactionConfig::default()).is_none());
  414. assert!(
  415. handle_slash_command("/model claude", &session, CompactionConfig::default()).is_none()
  416. );
  417. assert!(handle_slash_command(
  418. "/permissions read-only",
  419. &session,
  420. CompactionConfig::default()
  421. )
  422. .is_none());
  423. assert!(handle_slash_command("/clear", &session, CompactionConfig::default()).is_none());
  424. assert!(
  425. handle_slash_command("/clear --confirm", &session, CompactionConfig::default())
  426. .is_none()
  427. );
  428. assert!(handle_slash_command("/cost", &session, CompactionConfig::default()).is_none());
  429. assert!(handle_slash_command(
  430. "/resume session.json",
  431. &session,
  432. CompactionConfig::default()
  433. )
  434. .is_none());
  435. assert!(handle_slash_command("/config", &session, CompactionConfig::default()).is_none());
  436. assert!(
  437. handle_slash_command("/config env", &session, CompactionConfig::default()).is_none()
  438. );
  439. assert!(handle_slash_command("/diff", &session, CompactionConfig::default()).is_none());
  440. assert!(handle_slash_command("/version", &session, CompactionConfig::default()).is_none());
  441. assert!(
  442. handle_slash_command("/export note.txt", &session, CompactionConfig::default())
  443. .is_none()
  444. );
  445. assert!(
  446. handle_slash_command("/session list", &session, CompactionConfig::default()).is_none()
  447. );
  448. }
  449. }