Skip to content

Commit 0e034b4

Browse files
feat: Add conditional compilation for Knowledge command using feature flag (#2513)
- Add 'knowledge' feature flag to chat-cli Cargo.toml - Make Knowledge command conditional with #[cfg(feature = "knowledge")] - Restore original database setting logic when feature is enabled - Hide Knowledge command from help when feature is disabled - Fix unused import/variable warnings with conditional compilation - Knowledge is disabled by default, enabled with --features knowledge Co-authored-by: Kenneth S. <[email protected]>
1 parent dd6de52 commit 0e034b4

File tree

5 files changed

+40
-11
lines changed

5 files changed

+40
-11
lines changed

crates/chat-cli/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ workspace = true
1414
[features]
1515
default = []
1616
wayland = ["arboard/wayland-data-control"]
17+
knowledge = []
1718

1819
[[bin]]
1920
name = "test_mcp_server"

crates/chat-cli/src/cli/chat/cli/knowledge.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use crate::cli::chat::{
1919
ChatSession,
2020
ChatState,
2121
};
22+
#[cfg(feature = "knowledge")]
2223
use crate::database::settings::Setting;
2324
use crate::os::Os;
2425
use crate::util::knowledge_store::KnowledgeStore;
@@ -69,10 +70,19 @@ impl KnowledgeSubcommand {
6970
}
7071

7172
fn is_feature_enabled(os: &Os) -> bool {
72-
os.database
73-
.settings
74-
.get_bool(Setting::EnabledKnowledge)
75-
.unwrap_or(false)
73+
// Feature is only available when compiled with the knowledge feature flag
74+
#[cfg(feature = "knowledge")]
75+
{
76+
os.database
77+
.settings
78+
.get_bool(Setting::EnabledKnowledge)
79+
.unwrap_or(false)
80+
}
81+
#[cfg(not(feature = "knowledge"))]
82+
{
83+
let _ = os; // Suppress unused variable warning
84+
false
85+
}
7686
}
7787

7888
fn write_feature_disabled_message(session: &mut ChatSession) -> Result<(), std::io::Error> {

crates/chat-cli/src/cli/chat/cli/mod.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ pub mod compact;
33
pub mod context;
44
pub mod editor;
55
pub mod hooks;
6+
#[cfg(feature = "knowledge")]
67
pub mod knowledge;
78
pub mod mcp;
89
pub mod model;
@@ -19,6 +20,7 @@ use compact::CompactArgs;
1920
use context::ContextSubcommand;
2021
use editor::EditorArgs;
2122
use hooks::HooksArgs;
23+
#[cfg(feature = "knowledge")]
2224
use knowledge::KnowledgeSubcommand;
2325
use mcp::McpArgs;
2426
use model::ModelArgs;
@@ -56,9 +58,9 @@ pub enum SlashCommand {
5658
/// Manage context files for the chat session
5759
#[command(subcommand)]
5860
Context(ContextSubcommand),
59-
/// (Beta) Manage knowledge base for persistent context storage. Requires "q settings
60-
/// chat.enableKnowledge true"
61-
#[command(subcommand, hide = true)]
61+
/// (Beta) Manage knowledge base for persistent context storage
62+
#[cfg(feature = "knowledge")]
63+
#[command(subcommand)]
6264
Knowledge(KnowledgeSubcommand),
6365
/// Open $EDITOR (defaults to vi) to compose a prompt
6466
#[command(name = "editor")]
@@ -117,6 +119,7 @@ impl SlashCommand {
117119
})
118120
},
119121
Self::Context(args) => args.execute(os, session).await,
122+
#[cfg(feature = "knowledge")]
120123
Self::Knowledge(subcommand) => subcommand.execute(os, session).await,
121124
Self::PromptEditor(args) => args.execute(session).await,
122125
Self::Compact(args) => args.execute(os, session).await,
@@ -156,6 +159,7 @@ impl SlashCommand {
156159
Self::Agent(_) => "agent",
157160
Self::Profile => "profile",
158161
Self::Context(_) => "context",
162+
#[cfg(feature = "knowledge")]
159163
Self::Knowledge(_) => "knowledge",
160164
Self::PromptEditor(_) => "editor",
161165
Self::Compact(_) => "compact",
@@ -178,6 +182,7 @@ impl SlashCommand {
178182
match self {
179183
SlashCommand::Agent(sub) => Some(sub.name()),
180184
SlashCommand::Context(sub) => Some(sub.name()),
185+
#[cfg(feature = "knowledge")]
181186
SlashCommand::Knowledge(sub) => Some(sub.name()),
182187
SlashCommand::Tools(arg) => arg.subcommand_name(),
183188
SlashCommand::Prompts(arg) => arg.subcommand_name(),

crates/chat-cli/src/cli/chat/tools/knowledge.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@ use super::{
1313
InvokeOutput,
1414
OutputKind,
1515
};
16+
17+
1618
use crate::cli::agent::{
1719
Agent,
1820
PermissionEvalResult,
1921
};
22+
#[cfg(feature = "knowledge")]
2023
use crate::database::settings::Setting;
2124
use crate::os::Os;
2225
use crate::util::knowledge_store::KnowledgeStore;
@@ -87,10 +90,19 @@ pub struct KnowledgeCancel {
8790
impl Knowledge {
8891
/// Checks if the knowledge feature is enabled in settings
8992
pub fn is_enabled(os: &Os) -> bool {
90-
os.database
91-
.settings
92-
.get_bool(Setting::EnabledKnowledge)
93-
.unwrap_or(false)
93+
// Feature is only available when compiled with the knowledge feature flag
94+
#[cfg(feature = "knowledge")]
95+
{
96+
os.database
97+
.settings
98+
.get_bool(Setting::EnabledKnowledge)
99+
.unwrap_or(false)
100+
}
101+
#[cfg(not(feature = "knowledge"))]
102+
{
103+
let _ = os; // Suppress unused variable warning
104+
false
105+
}
94106
}
95107

96108
pub async fn validate(&mut self, os: &Os) -> Result<()> {

crates/chat-cli/src/util/knowledge_store.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ impl KnowledgeStore {
159159
}
160160

161161
/// Clear all contexts immediately (synchronous operation)
162+
#[allow(dead_code)]
162163
pub async fn clear_immediate(&mut self) -> Result<String, String> {
163164
match self.client.clear_all_immediate().await {
164165
Ok(count) => Ok(format!("✅ Successfully cleared {} knowledge base entries", count)),

0 commit comments

Comments
 (0)