返回 CodeWhale
mod.rs
根目录 / crates / tui / src / commands / groups / debug / mod.rs
1 //! Debug command area: token/cost introspection, cache tooling, undo/retry,
2 //! and the change log.
3
4 mod balance;
5 mod cache;
6 mod change;
7 mod preview_request;
8 mod tokens;
9 mod tool_inspection;
10 mod undo;
11
12 #[cfg(test)]
13 mod tests;
14
15 use crate::commands::CommandResult;
16 use crate::commands::traits::{Command, CommandGroup, CommandInfo, FunctionCommand};
17 use crate::localization::MessageId;
18 use crate::tui::app::App;
19
20 pub struct DebugCommands;
21
22 impl CommandGroup for DebugCommands {
23 fn commands(&self) -> &'static [Box<dyn Command>] {
24 cached_command_list!(vec![
25 Box::new(FunctionCommand::new(&TOKENS_INFO, run_tokens)),
26 Box::new(FunctionCommand::new(&COST_INFO, run_cost)),
27 Box::new(FunctionCommand::new(&BALANCE_INFO, run_balance)),
28 Box::new(FunctionCommand::new(&CACHE_INFO, run_cache)),
29 Box::new(FunctionCommand::new(
30 &PREVIEW_REQUEST_INFO,
31 run_preview_request
32 )),
33 Box::new(FunctionCommand::new(&TOOLS_INFO, run_tools)),
34 Box::new(FunctionCommand::new(&CHANGE_INFO, run_change)),
35 Box::new(FunctionCommand::new(&SYSTEM_INFO, run_system)),
36 Box::new(FunctionCommand::new(&CONTEXT_INFO, run_context)),
37 Box::new(FunctionCommand::new(&EDIT_INFO, run_edit)),
38 Box::new(FunctionCommand::new(&DIFF_INFO, run_diff)),
39 Box::new(FunctionCommand::new(&UNDO_INFO, run_undo)),
40 Box::new(FunctionCommand::new(&RETRY_INFO, run_retry)),
41 ])
42 }
43 }
44
45 static TOKENS_INFO: CommandInfo = CommandInfo {
46 name: "tokens",
47 aliases: &[],
48 usage: "/tokens",
49 description_id: MessageId::CmdTokensDescription,
50 };
51 static COST_INFO: CommandInfo = CommandInfo {
52 name: "cost",
53 aliases: &[],
54 usage: "/cost",
55 description_id: MessageId::CmdCostDescription,
56 };
57 static BALANCE_INFO: CommandInfo = CommandInfo {
58 name: "balance",
59 aliases: &[],
60 usage: "/balance",
61 description_id: MessageId::CmdBalanceDescription,
62 };
63 static CACHE_INFO: CommandInfo = CommandInfo {
64 name: "cache",
65 aliases: &[],
66 usage: "/cache [count|inspect|stats|zones|warmup]",
67 description_id: MessageId::CmdCacheDescription,
68 };
69 static PREVIEW_REQUEST_INFO: CommandInfo = CommandInfo {
70 name: "preview-request",
71 // `dryrun` is the name PR #1099 used; `preview_request` covers the
72 // underscore spelling. Both stay wired so muscle memory keeps working.
73 aliases: &["dryrun", "preview_request"],
74 usage: "/preview-request [json] [--prompt <text>]",
75 description_id: MessageId::CmdPreviewRequestDescription,
76 };
77 static TOOLS_INFO: CommandInfo = CommandInfo {
78 name: "tools",
79 aliases: &["tool-studio"],
80 usage: "/tools [text|json]",
81 description_id: MessageId::CmdToolsDescription,
82 };
83 static CHANGE_INFO: CommandInfo = CommandInfo {
84 name: "change",
85 aliases: &[],
86 usage: "/change [version]",
87 description_id: MessageId::CmdChangeDescription,
88 };
89 static SYSTEM_INFO: CommandInfo = CommandInfo {
90 name: "system",
91 aliases: &["xitong"],
92 usage: "/system",
93 description_id: MessageId::CmdSystemDescription,
94 };
95 static CONTEXT_INFO: CommandInfo = CommandInfo {
96 name: "context",
97 aliases: &["ctx"],
98 usage: "/context [report|json|prompt-json|summary]",
99 description_id: MessageId::CmdContextDescription,
100 };
101 static EDIT_INFO: CommandInfo = CommandInfo {
102 name: "edit",
103 aliases: &[],
104 usage: "/edit",
105 description_id: MessageId::CmdEditDescription,
106 };
107 static DIFF_INFO: CommandInfo = CommandInfo {
108 name: "diff",
109 aliases: &[],
110 usage: "/diff",
111 description_id: MessageId::CmdDiffDescription,
112 };
113 static UNDO_INFO: CommandInfo = CommandInfo {
114 name: "undo",
115 aliases: &[],
116 usage: "/undo",
117 description_id: MessageId::CmdUndoDescription,
118 };
119 static RETRY_INFO: CommandInfo = CommandInfo {
120 name: "retry",
121 aliases: &["chongshi"],
122 usage: "/retry",
123 description_id: MessageId::CmdRetryDescription,
124 };
125
126 fn run_registered(app: &mut App, name: &str, arg: Option<&str>) -> CommandResult {
127 dispatch(app, name, arg).expect("registered debug command should dispatch")
128 }
129
130 fn run_tokens(app: &mut App, arg: Option<&str>) -> CommandResult {
131 run_registered(app, "tokens", arg)
132 }
133 fn run_cost(app: &mut App, arg: Option<&str>) -> CommandResult {
134 run_registered(app, "cost", arg)
135 }
136 fn run_balance(app: &mut App, arg: Option<&str>) -> CommandResult {
137 run_registered(app, "balance", arg)
138 }
139 fn run_cache(app: &mut App, arg: Option<&str>) -> CommandResult {
140 run_registered(app, "cache", arg)
141 }
142 fn run_preview_request(app: &mut App, arg: Option<&str>) -> CommandResult {
143 run_registered(app, "preview-request", arg)
144 }
145 fn run_tools(app: &mut App, arg: Option<&str>) -> CommandResult {
146 run_registered(app, "tools", arg)
147 }
148 fn run_change(app: &mut App, arg: Option<&str>) -> CommandResult {
149 run_registered(app, "change", arg)
150 }
151 fn run_system(app: &mut App, arg: Option<&str>) -> CommandResult {
152 run_registered(app, "system", arg)
153 }
154 fn run_context(app: &mut App, arg: Option<&str>) -> CommandResult {
155 run_registered(app, "context", arg)
156 }
157 fn run_edit(app: &mut App, arg: Option<&str>) -> CommandResult {
158 run_registered(app, "edit", arg)
159 }
160 fn run_diff(app: &mut App, arg: Option<&str>) -> CommandResult {
161 run_registered(app, "diff", arg)
162 }
163 fn run_undo(app: &mut App, arg: Option<&str>) -> CommandResult {
164 run_registered(app, "undo", arg)
165 }
166 fn run_retry(app: &mut App, arg: Option<&str>) -> CommandResult {
167 run_registered(app, "retry", arg)
168 }
169
170 pub(in crate::commands) fn dispatch(
171 app: &mut App,
172 command: &str,
173 arg: Option<&str>,
174 ) -> Option<CommandResult> {
175 let result = match command {
176 "tokens" => tokens::tokens(app),
177 "cost" => tokens::cost(app),
178 "balance" => balance::balance(app),
179 "cache" => cache::cache(app, arg),
180 "preview-request" | "preview_request" | "dryrun" => {
181 preview_request::preview_request(app, arg)
182 }
183 "tools" | "tool-studio" => tool_inspection::tools(app, arg),
184 "change" => change::change(app, arg),
185 "system" | "xitong" => tokens::system_prompt(app),
186 "context" | "ctx" => tokens::context(app, arg),
187 "edit" => undo::edit(app),
188 "diff" => undo::diff(app),
189 "undo" => {
190 // Try surgical patch-undo first; fall back to conversation undo
191 // when there is no snapshot-level change to revert. Never fall
192 // through when the trusted-mode gate refused the file rollback —
193 // that would silently crop the conversation instead of telling
194 // the user to switch modes.
195 let result = undo::patch_undo(app);
196 if result.message.as_deref().is_none_or(|m| {
197 m.starts_with("No snapshots found")
198 || m.starts_with("No older tool or pre-turn")
199 || m.starts_with("No undoable snapshot")
200 || m.starts_with("Snapshot repo")
201 }) {
202 undo::undo_conversation(app)
203 } else {
204 result
205 }
206 }
207 "retry" | "chongshi" => undo::retry(app),
208 _ => return None,
209 };
210 Some(result)
211 }
212
212 lines RUST