返回 CodeWhale
help.rs
根目录 / crates / tui / src / commands / groups / core / help.rs
1 //! `/help` command.
2
3 use codewhale_command_contract::facets::CommandPresentationContext;
4 use codewhale_command_contract::handler::{CommandCapabilities, CommandContexts, CommandHandler};
5 use codewhale_command_contract::metadata::{CommandInfo, RegisterCommand};
6 use codewhale_command_contract::navigation::{
7 CommandNavigationContext, NavigationSkill, NavigationUserHelp,
8 };
9 use codewhale_command_contract::outcome::CoreNavigationCommandResult as CommandResult;
10 use std::fmt::Write;
11
12 pub const COMMAND_INFO: CommandInfo = CommandInfo {
13 name: "help",
14 aliases: &["?", "bangzhu", "帮助"],
15 usage: "/help [command]",
16 description_key: "cmd_help_description",
17 };
18
19 pub struct HelpCmd;
20
21 impl RegisterCommand<CommandResult> for HelpCmd {
22 fn info() -> &'static CommandInfo {
23 &COMMAND_INFO
24 }
25 fn handler() -> CommandHandler<CommandResult> {
26 CommandHandler::Contextual {
27 capabilities: CommandCapabilities::NAVIGATION.union(CommandCapabilities::PRESENTATION),
28 handler: help,
29 }
30 }
31 }
32
33 fn help(contexts: CommandContexts<'_>, topic: Option<&str>) -> CommandResult {
34 let parts = contexts.into_parts();
35 let Some(navigation) = parts.navigation else {
36 return CommandResult::error("Command capability unavailable: navigation");
37 };
38 let Some(presentation) = parts.presentation else {
39 return CommandResult::error("Command capability unavailable: presentation");
40 };
41 render_help(navigation, presentation, topic).unwrap_or_else(CommandResult::error)
42 }
43
44 fn render_help(
45 navigation: &mut dyn CommandNavigationContext,
46 presentation: &dyn CommandPresentationContext,
47 topic: Option<&str>,
48 ) -> Result<CommandResult, String> {
49 if let Some(topic) = topic {
50 let facts = navigation.help_topic(topic);
51 if let Some(command) = facts.user {
52 return Ok(CommandResult::message(user_command_help(
53 presentation,
54 &command,
55 )?));
56 }
57 if let Some(command) = facts.builtin {
58 let mut help = format!(
59 "{}\n\n {}\n\n {} {}",
60 command.name,
61 command.description,
62 presentation.translate("help_usage_label", &[])?,
63 command.usage,
64 );
65 let visible_aliases = command
66 .aliases
67 .iter()
68 .filter(|alias| !facts.shadowed_names.contains(alias))
69 .map(String::as_str)
70 .collect::<Vec<_>>();
71 if !visible_aliases.is_empty() {
72 let _ = write!(
73 help,
74 "\n {} {}",
75 presentation.translate("help_aliases_label", &[])?,
76 visible_aliases.join(", ")
77 );
78 }
79 if command.name == "config" {
80 help.push_str("\n\n Provider context window: set `context_window = 262144` under the active `[providers.<name>]` table to cap a 1M model to 256K. Use `/config context_window` to inspect the configured and effective values.");
81 help.push('\n');
82 help.push_str(&presentation.translate("config_help_discoverable", &[])?);
83 }
84 return Ok(CommandResult::message(help));
85 }
86 if let Some(help) = skill_help(presentation, &navigation.cached_skills(), topic)? {
87 return Ok(CommandResult::message(help));
88 }
89 return Ok(CommandResult::error(
90 presentation.translate("help_unknown_command", &[("topic", topic)])?,
91 ));
92 }
93 if !navigation.help_is_open() {
94 navigation.open_help();
95 }
96 Ok(CommandResult::ok())
97 }
98
99 fn skill_help(
100 presentation: &dyn CommandPresentationContext,
101 skills: &[NavigationSkill],
102 topic: &str,
103 ) -> Result<Option<String>, String> {
104 let needle = topic
105 .trim()
106 .trim_start_matches('$')
107 .trim_start_matches('/')
108 .trim_start_matches("skill ")
109 .trim();
110 if needle.is_empty() {
111 return Ok(None);
112 }
113 let Some(skill) = skills
114 .iter()
115 .find(|skill| skill.name.eq_ignore_ascii_case(needle))
116 else {
117 return Ok(None);
118 };
119 let name = &skill.name;
120 let mut help = format!("${name}");
121 if !skill.description.trim().is_empty() {
122 let _ = write!(help, "\n\n {}", skill.description.trim());
123 }
124 let _ = write!(
125 help,
126 "\n\n {} ${name}\n {} /skill {name}",
127 presentation.translate("help_usage_label", &[])?,
128 presentation.translate("help_usage_label", &[])?,
129 );
130 Ok(Some(help))
131 }
132
133 fn user_command_help(
134 presentation: &dyn CommandPresentationContext,
135 command: &NavigationUserHelp,
136 ) -> Result<String, String> {
137 let mut help = command.name.clone();
138 if let Some(description) = command
139 .description
140 .as_deref()
141 .filter(|description| !description.trim().is_empty())
142 {
143 let _ = write!(help, "\n\n {description}");
144 }
145 let usage = command
146 .usage
147 .clone()
148 .unwrap_or_else(|| format!("/{}", command.name));
149 let _ = write!(
150 help,
151 "\n\n {} {}",
152 presentation.translate("help_usage_label", &[])?,
153 usage
154 );
155 if !command.aliases.is_empty() {
156 let _ = write!(
157 help,
158 "\n {} {}",
159 presentation.translate("help_aliases_label", &[])?,
160 command.aliases.join(", ")
161 );
162 }
163 Ok(help)
164 }
165
165 lines RUST