返回 CodeWhale
new.rs
根目录 / crates / tui / src / commands / groups / session / new.rs
1 //! `/new` requests a fresh session; the host resets after Engine acknowledgement.
2
3 use super::{CommandResult, transition_blocked_message};
4
5 use codewhale_command_contract::facets::CommandSessionLifecycleContext;
6 use codewhale_command_contract::handler::{CommandContexts, CommandHandler};
7 use codewhale_command_contract::metadata::{
8 CommandInfo as ContractInfo, RegisterCommand as ContractRegisterCommand,
9 };
10
11 pub(in crate::commands) struct NewCmd;
12
13 // ---------------------------------------------------------------------------
14 // FEAT-023 Phase 4 (D3/D5/D6): portable contextual registration and handler.
15 // ---------------------------------------------------------------------------
16
17 pub(in crate::commands) const CONTRACT_INFO: ContractInfo = ContractInfo {
18 name: "new",
19 aliases: &[],
20 usage: "/new [<mode>] [--force]",
21 description_key: "cmd_new_description",
22 };
23
24 impl ContractRegisterCommand<CommandResult> for NewCmd {
25 fn info() -> &'static ContractInfo {
26 &CONTRACT_INFO
27 }
28 fn handler() -> CommandHandler<CommandResult> {
29 CommandHandler::Contextual {
30 capabilities:
31 codewhale_command_contract::handler::CommandCapabilities::SESSION_LIFECYCLE,
32 handler: new_contextual,
33 }
34 }
35 }
36
37 pub(in crate::commands) fn new_contextual(
38 contexts: CommandContexts<'_>,
39 arg: Option<&str>,
40 ) -> CommandResult {
41 let mut parts = contexts.into_parts();
42 let Some(lifecycle) = parts.lifecycle.as_deref_mut() else {
43 return CommandResult::error(
44 "Command capability unavailable: session_lifecycle".to_string(),
45 );
46 };
47 new_portable(lifecycle, arg)
48 }
49
50 pub(in crate::commands) fn new_portable(
51 lifecycle: &mut dyn CommandSessionLifecycleContext,
52 arg: Option<&str>,
53 ) -> CommandResult {
54 let mut force = false;
55 let mut conversation_mode: Option<&str> = None;
56 for token in arg.unwrap_or_default().split_whitespace() {
57 match token {
58 "--force" | "force" => force = true,
59 other if !other.starts_with('-') && conversation_mode.is_none() => {
60 conversation_mode = Some(other);
61 }
62 other => {
63 return CommandResult::error(format!(
64 "Usage: /new [<mode>] [--force]\n\nUnknown argument: {other}"
65 ));
66 }
67 }
68 }
69 if lifecycle.transition_blocked() {
70 // The shared tail names the exits that clear a blocker; `--force` is
71 // not one of them, and a user who has just read it will try exactly
72 // that. Say what it does here, where the flag exists.
73 let mut message =
74 transition_blocked_message("start a new session", &lifecycle.transition_blockers());
75 message.push_str("\n\n`/new --force` only discards draft or queued input.");
76 return CommandResult::error(message);
77 }
78 match lifecycle.fresh_session(force, conversation_mode) {
79 Ok(receipt) => CommandResult::action(
80 codewhale_command_contract::outcome::SessionAction::NewSession {
81 sync: receipt.sync,
82 force,
83 draft: None,
84 },
85 ),
86 Err(error) => CommandResult::error(error),
87 }
88 }
89
89 lines RUST