lib.rs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. }
  33. const SLASH_COMMAND_SPECS: &[SlashCommandSpec] = &[
  34. SlashCommandSpec {
  35. name: "help",
  36. summary: "Show available slash commands",
  37. argument_hint: None,
  38. },
  39. SlashCommandSpec {
  40. name: "status",
  41. summary: "Show current session status",
  42. argument_hint: None,
  43. },
  44. SlashCommandSpec {
  45. name: "compact",
  46. summary: "Compact local session history",
  47. argument_hint: None,
  48. },
  49. SlashCommandSpec {
  50. name: "model",
  51. summary: "Show or switch the active model",
  52. argument_hint: Some("[model]"),
  53. },
  54. SlashCommandSpec {
  55. name: "permissions",
  56. summary: "Show or switch the active permission mode",
  57. argument_hint: Some("[read-only|workspace-write|danger-full-access]"),
  58. },
  59. SlashCommandSpec {
  60. name: "clear",
  61. summary: "Start a fresh local session",
  62. argument_hint: None,
  63. },
  64. SlashCommandSpec {
  65. name: "cost",
  66. summary: "Show cumulative token usage for this session",
  67. argument_hint: None,
  68. },
  69. SlashCommandSpec {
  70. name: "resume",
  71. summary: "Load a saved session into the REPL",
  72. argument_hint: Some("<session-path>"),
  73. },
  74. SlashCommandSpec {
  75. name: "config",
  76. summary: "Inspect discovered Claude config files",
  77. argument_hint: None,
  78. },
  79. SlashCommandSpec {
  80. name: "memory",
  81. summary: "Inspect loaded Claude instruction memory files",
  82. argument_hint: None,
  83. },
  84. ];
  85. #[derive(Debug, Clone, PartialEq, Eq)]
  86. pub enum SlashCommand {
  87. Help,
  88. Status,
  89. Compact,
  90. Model { model: Option<String> },
  91. Permissions { mode: Option<String> },
  92. Clear,
  93. Cost,
  94. Resume { session_path: Option<String> },
  95. Config,
  96. Memory,
  97. Unknown(String),
  98. }
  99. impl SlashCommand {
  100. #[must_use]
  101. pub fn parse(input: &str) -> Option<Self> {
  102. let trimmed = input.trim();
  103. if !trimmed.starts_with('/') {
  104. return None;
  105. }
  106. let mut parts = trimmed.trim_start_matches('/').split_whitespace();
  107. let command = parts.next().unwrap_or_default();
  108. Some(match command {
  109. "help" => Self::Help,
  110. "status" => Self::Status,
  111. "compact" => Self::Compact,
  112. "model" => Self::Model {
  113. model: parts.next().map(ToOwned::to_owned),
  114. },
  115. "permissions" => Self::Permissions {
  116. mode: parts.next().map(ToOwned::to_owned),
  117. },
  118. "clear" => Self::Clear,
  119. "cost" => Self::Cost,
  120. "resume" => Self::Resume {
  121. session_path: parts.next().map(ToOwned::to_owned),
  122. },
  123. "config" => Self::Config,
  124. "memory" => Self::Memory,
  125. other => Self::Unknown(other.to_string()),
  126. })
  127. }
  128. }
  129. #[must_use]
  130. pub fn slash_command_specs() -> &'static [SlashCommandSpec] {
  131. SLASH_COMMAND_SPECS
  132. }
  133. #[must_use]
  134. pub fn render_slash_command_help() -> String {
  135. let mut lines = vec!["Available commands:".to_string()];
  136. for spec in slash_command_specs() {
  137. let name = match spec.argument_hint {
  138. Some(argument_hint) => format!("/{} {}", spec.name, argument_hint),
  139. None => format!("/{}", spec.name),
  140. };
  141. lines.push(format!(" {name:<20} {}", spec.summary));
  142. }
  143. lines.join("\n")
  144. }
  145. #[derive(Debug, Clone, PartialEq, Eq)]
  146. pub struct SlashCommandResult {
  147. pub message: String,
  148. pub session: Session,
  149. }
  150. #[must_use]
  151. pub fn handle_slash_command(
  152. input: &str,
  153. session: &Session,
  154. compaction: CompactionConfig,
  155. ) -> Option<SlashCommandResult> {
  156. match SlashCommand::parse(input)? {
  157. SlashCommand::Compact => {
  158. let result = compact_session(session, compaction);
  159. let message = if result.removed_message_count == 0 {
  160. "Compaction skipped: session is below the compaction threshold.".to_string()
  161. } else {
  162. format!(
  163. "Compacted {} messages into a resumable system summary.",
  164. result.removed_message_count
  165. )
  166. };
  167. Some(SlashCommandResult {
  168. message,
  169. session: result.compacted_session,
  170. })
  171. }
  172. SlashCommand::Help => Some(SlashCommandResult {
  173. message: render_slash_command_help(),
  174. session: session.clone(),
  175. }),
  176. SlashCommand::Status
  177. | SlashCommand::Model { .. }
  178. | SlashCommand::Permissions { .. }
  179. | SlashCommand::Clear
  180. | SlashCommand::Cost
  181. | SlashCommand::Resume { .. }
  182. | SlashCommand::Config
  183. | SlashCommand::Memory
  184. | SlashCommand::Unknown(_) => None,
  185. }
  186. }
  187. #[cfg(test)]
  188. mod tests {
  189. use super::{
  190. handle_slash_command, render_slash_command_help, slash_command_specs, SlashCommand,
  191. };
  192. use runtime::{CompactionConfig, ContentBlock, ConversationMessage, MessageRole, Session};
  193. #[test]
  194. fn parses_supported_slash_commands() {
  195. assert_eq!(SlashCommand::parse("/help"), Some(SlashCommand::Help));
  196. assert_eq!(SlashCommand::parse(" /status "), Some(SlashCommand::Status));
  197. assert_eq!(
  198. SlashCommand::parse("/model claude-opus"),
  199. Some(SlashCommand::Model {
  200. model: Some("claude-opus".to_string()),
  201. })
  202. );
  203. assert_eq!(
  204. SlashCommand::parse("/model"),
  205. Some(SlashCommand::Model { model: None })
  206. );
  207. assert_eq!(
  208. SlashCommand::parse("/permissions read-only"),
  209. Some(SlashCommand::Permissions {
  210. mode: Some("read-only".to_string()),
  211. })
  212. );
  213. assert_eq!(SlashCommand::parse("/clear"), Some(SlashCommand::Clear));
  214. assert_eq!(SlashCommand::parse("/cost"), Some(SlashCommand::Cost));
  215. assert_eq!(
  216. SlashCommand::parse("/resume session.json"),
  217. Some(SlashCommand::Resume {
  218. session_path: Some("session.json".to_string()),
  219. })
  220. );
  221. assert_eq!(SlashCommand::parse("/config"), Some(SlashCommand::Config));
  222. assert_eq!(SlashCommand::parse("/memory"), Some(SlashCommand::Memory));
  223. }
  224. #[test]
  225. fn renders_help_from_shared_specs() {
  226. let help = render_slash_command_help();
  227. assert!(help.contains("/help"));
  228. assert!(help.contains("/status"));
  229. assert!(help.contains("/compact"));
  230. assert!(help.contains("/model [model]"));
  231. assert!(help.contains("/permissions [read-only|workspace-write|danger-full-access]"));
  232. assert!(help.contains("/clear"));
  233. assert!(help.contains("/cost"));
  234. assert!(help.contains("/resume <session-path>"));
  235. assert!(help.contains("/config"));
  236. assert!(help.contains("/memory"));
  237. assert_eq!(slash_command_specs().len(), 10);
  238. }
  239. #[test]
  240. fn compacts_sessions_via_slash_command() {
  241. let session = Session {
  242. version: 1,
  243. messages: vec![
  244. ConversationMessage::user_text("a ".repeat(200)),
  245. ConversationMessage::assistant(vec![ContentBlock::Text {
  246. text: "b ".repeat(200),
  247. }]),
  248. ConversationMessage::tool_result("1", "bash", "ok ".repeat(200), false),
  249. ConversationMessage::assistant(vec![ContentBlock::Text {
  250. text: "recent".to_string(),
  251. }]),
  252. ],
  253. };
  254. let result = handle_slash_command(
  255. "/compact",
  256. &session,
  257. CompactionConfig {
  258. preserve_recent_messages: 2,
  259. max_estimated_tokens: 1,
  260. },
  261. )
  262. .expect("slash command should be handled");
  263. assert!(result.message.contains("Compacted 2 messages"));
  264. assert_eq!(result.session.messages[0].role, MessageRole::System);
  265. }
  266. #[test]
  267. fn help_command_is_non_mutating() {
  268. let session = Session::new();
  269. let result = handle_slash_command("/help", &session, CompactionConfig::default())
  270. .expect("help command should be handled");
  271. assert_eq!(result.session, session);
  272. assert!(result.message.contains("Available commands:"));
  273. }
  274. #[test]
  275. fn ignores_unknown_or_runtime_bound_slash_commands() {
  276. let session = Session::new();
  277. assert!(handle_slash_command("/unknown", &session, CompactionConfig::default()).is_none());
  278. assert!(handle_slash_command("/status", &session, CompactionConfig::default()).is_none());
  279. assert!(
  280. handle_slash_command("/model claude", &session, CompactionConfig::default()).is_none()
  281. );
  282. assert!(handle_slash_command(
  283. "/permissions read-only",
  284. &session,
  285. CompactionConfig::default()
  286. )
  287. .is_none());
  288. assert!(handle_slash_command("/clear", &session, CompactionConfig::default()).is_none());
  289. assert!(handle_slash_command("/cost", &session, CompactionConfig::default()).is_none());
  290. assert!(handle_slash_command(
  291. "/resume session.json",
  292. &session,
  293. CompactionConfig::default()
  294. )
  295. .is_none());
  296. assert!(handle_slash_command("/config", &session, CompactionConfig::default()).is_none());
  297. }
  298. }