返回 CodeWhale
hf.rs
1 //! `/hf` - Hugging Face MCP and provider concept helpers.
2
3 use codewhale_command_contract::config::CommandConfigContext;
4 use codewhale_command_contract::handler::{CommandCapabilities, CommandContexts, CommandHandler};
5 use codewhale_command_contract::metadata::{CommandInfo, RegisterCommand};
6 use codewhale_command_contract::outcome::CommandResult;
7
8 const COMMAND_INFO: CommandInfo = CommandInfo {
9 name: "hf",
10 aliases: &["huggingface"],
11 usage: "/hf [mcp <status|setup>|concepts]",
12 description_key: "cmd_hf_description",
13 };
14
15 pub struct HfCmd;
16
17 impl<A> RegisterCommand<CommandResult<A>> for HfCmd {
18 fn info() -> &'static CommandInfo {
19 &COMMAND_INFO
20 }
21
22 fn handler() -> CommandHandler<CommandResult<A>> {
23 CommandHandler::Contextual {
24 capabilities: CommandCapabilities::CONFIG,
25 handler: hf,
26 }
27 }
28 }
29
30 const HF_MCP_SETTINGS_URL: &str = "https://huggingface.co/settings/mcp";
31 const HF_MCP_DOCS_URL: &str = "https://huggingface.co/docs/hub/hf-mcp-server";
32 const HF_MCP_SERVER_URL: &str = "https://huggingface.co/mcp";
33
34 const HF_MCP_CONFIG_SKELETON: &str = r#"{
35 "servers": {
36 "huggingface": {
37 "url": "https://huggingface.co/mcp",
38 "headers": {
39 "Authorization": "Bearer ${HF_TOKEN}"
40 }
41 }
42 }
43 }"#;
44
45 /// Explainer shown by `/hf concepts`.
46 const HF_CONCEPTS: &str = "\
47 Codewhale has three distinct Hugging Face surfaces:
48
49 1. Hugging Face provider route - chat inference
50 Switch the active LLM backend to Hugging Face Inference Providers.
51 Use: /provider huggingface
52 Config: provider = \"huggingface\" or [providers.huggingface]
53 Auth: HF_TOKEN or HUGGINGFACE_API_KEY
54
55 2. Hugging Face MCP - Hub, docs, datasets, Spaces, and community tools
56 Connect Codewhale to Hugging Face's MCP server through mcp.json.
57 Use: /hf mcp status or /hf mcp setup
58 Then: /mcp reload or restart Codewhale so model-visible tools reload.
59
60 3. Hugging Face Hub workflows - publish, upload, or manage repositories
61 Use explicit Hub tooling such as huggingface_hub or git-based flows.
62 Codewhale does not upload to the Hub through /hf.";
63
64 fn hf<A>(contexts: CommandContexts<'_>, args: Option<&str>) -> CommandResult<A> {
65 let raw = args.unwrap_or("").trim();
66 if raw.is_empty() {
67 return usage();
68 }
69
70 let mut parts = raw.split_whitespace();
71 let subcommand = parts.next().unwrap_or_default().to_ascii_lowercase();
72 match subcommand.as_str() {
73 "mcp" => match contexts.into_parts().config {
74 Some(config) => hf_mcp(config, parts.next()),
75 None => CommandResult::error("Command capability unavailable: config"),
76 },
77 "concepts" | "explain" => CommandResult::message(HF_CONCEPTS),
78 _ => CommandResult::error(format!(
79 "Unknown /hf subcommand: {subcommand}. Use /hf mcp <status|setup> or /hf concepts."
80 )),
81 }
82 }
83
84 fn usage<A>() -> CommandResult<A> {
85 CommandResult::message(
86 "Usage: /hf mcp <status|setup>\n\
87 /hf concepts\n\n\
88 Hugging Face MCP settings: https://huggingface.co/settings/mcp",
89 )
90 }
91
92 fn hf_mcp<A>(config: &dyn CommandConfigContext, action: Option<&str>) -> CommandResult<A> {
93 match action.unwrap_or("status").to_ascii_lowercase().as_str() {
94 "status" => hf_mcp_status(config),
95 "setup" => CommandResult::message(hf_mcp_setup_message(&config.mcp_config_path())),
96 other => CommandResult::error(format!(
97 "Unknown /hf mcp subcommand: {other}. Use status or setup."
98 )),
99 }
100 }
101
102 fn hf_mcp_status<A>(config: &dyn CommandConfigContext) -> CommandResult<A> {
103 let path = config.mcp_config_path();
104 match config.hugging_face_mcp_server_name() {
105 Ok(server) => {
106 if let Some(server_name) = server {
107 CommandResult::message(format!(
108 "Hugging Face MCP appears configured as `{server_name}` in {}.\n\
109 Run /mcp reload to rebuild the live model-visible tool pool if tools are not visible yet.",
110 path.display()
111 ))
112 } else {
113 CommandResult::message(format!(
114 "Hugging Face MCP is not configured in {}.\n\
115 Run /hf mcp setup for the settings-generated config workflow.",
116 path.display()
117 ))
118 }
119 }
120 Err(err) => CommandResult::error(format!(
121 "Could not read MCP config {}: {err}",
122 path.display()
123 )),
124 }
125 }
126
127 fn hf_mcp_setup_message(path: &std::path::Path) -> String {
128 format!(
129 "Use Hugging Face's settings-generated MCP configuration when available:\n\
130 1. Open {HF_MCP_SETTINGS_URL} while signed in.\n\
131 2. Choose your MCP client and copy the generated configuration snippet.\n\
132 3. Paste the Hugging Face server entry into {}.\n\
133 4. Run /mcp reload to rebuild the live model-visible tool pool.\n\n\
134 Codewhale-compatible placeholder shape:\n\n\
135 ```json\n{HF_MCP_CONFIG_SKELETON}\n```\n\n\
136 The placeholder is intentionally not runnable until your private MCP config has a real token value. \
137 Do not commit real Hugging Face tokens.\n\n\
138 Docs: {HF_MCP_DOCS_URL}\n\
139 Server: {HF_MCP_SERVER_URL}",
140 path.display()
141 )
142 }
143
144 #[cfg(test)]
145 mod tests {
146 use super::*;
147
148 #[test]
149 fn hf_mcp_config_skeleton_keeps_token_placeholder_only() {
150 assert!(HF_MCP_CONFIG_SKELETON.contains("${HF_TOKEN}"));
151 assert!(!HF_MCP_CONFIG_SKELETON.contains("hf_"));
152 assert!(!HF_MCP_CONFIG_SKELETON.contains("Bearer hf_"));
153 serde_json::from_str::<serde_json::Value>(HF_MCP_CONFIG_SKELETON)
154 .expect("skeleton should be valid JSON");
155 }
156
157 #[test]
158 fn hf_concepts_explains_provider_mcp_and_hub_surfaces() {
159 assert!(HF_CONCEPTS.contains("provider route"));
160 assert!(HF_CONCEPTS.contains("Hugging Face MCP"));
161 assert!(HF_CONCEPTS.contains("Hub workflows"));
162 assert!(HF_CONCEPTS.contains("/provider huggingface"));
163 assert!(HF_CONCEPTS.contains("/hf mcp"));
164 }
165
166 #[test]
167 fn hf_usage_and_setup_do_not_advertise_hub_search() {
168 let usage = usage::<()>().message.expect("usage");
169 let setup = hf_mcp_setup_message(std::path::Path::new("mcp.json"));
170
171 assert!(!usage.contains("/hf search"));
172 assert!(!setup.contains("/hf search"));
173 assert!(setup.contains(HF_MCP_SETTINGS_URL));
174 }
175 }
176
176 lines RUST