Skip to content

Commit 5260330

Browse files
authored
refactor: Add fields to Os type to reduce dependency injection burden (#298)
1 parent c778373 commit 5260330

31 files changed

+297
-421
lines changed

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

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ use crate::cli::chat::{
55
ChatSession,
66
ChatState,
77
};
8-
use crate::database::Database;
98
use crate::os::Os;
10-
use crate::telemetry::TelemetryThread;
119

1210
#[deny(missing_docs)]
1311
#[derive(Debug, PartialEq, Args)]
@@ -36,15 +34,7 @@ pub struct CompactArgs {
3634
}
3735

3836
impl CompactArgs {
39-
pub async fn execute(
40-
self,
41-
os: &Os,
42-
database: &mut Database,
43-
telemetry: &TelemetryThread,
44-
session: &mut ChatSession,
45-
) -> Result<ChatState, ChatError> {
46-
session
47-
.compact_history(os, database, telemetry, self.prompt, self.show_summary)
48-
.await
37+
pub async fn execute(self, os: &Os, session: &mut ChatSession) -> Result<ChatState, ChatError> {
38+
session.compact_history(os, self.prompt, self.show_summary).await
4939
}
5040
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -782,7 +782,7 @@ mod tests {
782782

783783
#[tokio::test]
784784
async fn test_add_hook() -> Result<()> {
785-
let os = Os::new();
785+
let os = Os::new().await.unwrap();
786786
let mut manager = create_test_context_manager(None).await?;
787787
let hook = Hook::new_inline_hook(HookTrigger::ConversationStart, "echo test".to_string());
788788

@@ -811,7 +811,7 @@ mod tests {
811811

812812
#[tokio::test]
813813
async fn test_remove_hook() -> Result<()> {
814-
let os = Os::new();
814+
let os = Os::new().await.unwrap();
815815
let mut manager = create_test_context_manager(None).await?;
816816
let hook = Hook::new_inline_hook(HookTrigger::ConversationStart, "echo test".to_string());
817817

@@ -829,7 +829,7 @@ mod tests {
829829

830830
#[tokio::test]
831831
async fn test_set_hook_disabled() -> Result<()> {
832-
let os = Os::new();
832+
let os = Os::new().await.unwrap();
833833
let mut manager = create_test_context_manager(None).await?;
834834
let hook = Hook::new_inline_hook(HookTrigger::ConversationStart, "echo test".to_string());
835835

@@ -856,7 +856,7 @@ mod tests {
856856

857857
#[tokio::test]
858858
async fn test_set_all_hooks_disabled() -> Result<()> {
859-
let os = Os::new();
859+
let os = Os::new().await.unwrap();
860860
let mut manager = create_test_context_manager(None).await?;
861861
let hook1 = Hook::new_inline_hook(HookTrigger::ConversationStart, "echo test".to_string());
862862
let hook2 = Hook::new_inline_hook(HookTrigger::ConversationStart, "echo test".to_string());
@@ -877,7 +877,7 @@ mod tests {
877877

878878
#[tokio::test]
879879
async fn test_run_hooks() -> Result<()> {
880-
let os = Os::new();
880+
let os = Os::new().await.unwrap();
881881
let mut manager = create_test_context_manager(None).await?;
882882
let hook1 = Hook::new_inline_hook(HookTrigger::ConversationStart, "echo test".to_string());
883883
let hook2 = Hook::new_inline_hook(HookTrigger::ConversationStart, "echo test".to_string());
@@ -894,7 +894,7 @@ mod tests {
894894

895895
#[tokio::test]
896896
async fn test_hooks_across_profiles() -> Result<()> {
897-
let os = Os::new();
897+
let os = Os::new().await.unwrap();
898898
let mut manager = create_test_context_manager(None).await?;
899899
let hook1 = Hook::new_inline_hook(HookTrigger::ConversationStart, "echo test".to_string());
900900
let hook2 = Hook::new_inline_hook(HookTrigger::ConversationStart, "echo test".to_string());

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

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ use crate::cli::chat::{
1919
ChatSession,
2020
ChatState,
2121
};
22-
use crate::database::Database;
2322
use crate::database::settings::Setting;
2423
use crate::os::Os;
2524
use crate::util::knowledge_store::KnowledgeStore;
@@ -56,13 +55,8 @@ enum OperationResult {
5655
}
5756

5857
impl KnowledgeSubcommand {
59-
pub async fn execute(
60-
self,
61-
os: &Os,
62-
database: &Database,
63-
session: &mut ChatSession,
64-
) -> Result<ChatState, ChatError> {
65-
if !Self::is_feature_enabled(database) {
58+
pub async fn execute(self, os: &Os, session: &mut ChatSession) -> Result<ChatState, ChatError> {
59+
if !Self::is_feature_enabled(os) {
6660
Self::write_feature_disabled_message(session)?;
6761
return Ok(Self::default_chat_state());
6862
}
@@ -74,8 +68,11 @@ impl KnowledgeSubcommand {
7468
Ok(Self::default_chat_state())
7569
}
7670

77-
fn is_feature_enabled(database: &Database) -> bool {
78-
database.settings.get_bool(Setting::EnabledKnowledge).unwrap_or(false)
71+
fn is_feature_enabled(os: &Os) -> bool {
72+
os.database
73+
.settings
74+
.get_bool(Setting::EnabledKnowledge)
75+
.unwrap_or(false)
7976
}
8077

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

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

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@ use crate::cli::chat::{
3535
ChatState,
3636
};
3737
use crate::cli::issue;
38-
use crate::database::Database;
3938
use crate::os::Os;
40-
use crate::telemetry::TelemetryThread;
4139

4240
/// q (Amazon Q Chat)
4341
#[derive(Debug, PartialEq, Parser)]
@@ -85,24 +83,18 @@ pub enum SlashCommand {
8583
}
8684

8785
impl SlashCommand {
88-
pub async fn execute(
89-
self,
90-
os: &mut Os,
91-
database: &mut Database,
92-
telemetry: &TelemetryThread,
93-
session: &mut ChatSession,
94-
) -> Result<ChatState, ChatError> {
86+
pub async fn execute(self, os: &mut Os, session: &mut ChatSession) -> Result<ChatState, ChatError> {
9587
match self {
9688
Self::Quit => Ok(ChatState::Exit),
9789
Self::Clear(args) => args.execute(session).await,
9890
Self::Profile(subcommand) => subcommand.execute(os, session).await,
9991
Self::Context(args) => args.execute(os, session).await,
100-
Self::Knowledge(subcommand) => subcommand.execute(os, database, session).await,
92+
Self::Knowledge(subcommand) => subcommand.execute(os, session).await,
10193
Self::PromptEditor(args) => args.execute(session).await,
102-
Self::Compact(args) => args.execute(os, database, telemetry, session).await,
94+
Self::Compact(args) => args.execute(os, session).await,
10395
Self::Tools(args) => args.execute(session).await,
10496
Self::Issue(args) => {
105-
if let Err(err) = args.execute().await {
97+
if let Err(err) = args.execute(os).await {
10698
return Err(ChatError::Custom(err.to_string().into()));
10799
}
108100

@@ -115,7 +107,7 @@ impl SlashCommand {
115107
Self::Usage(args) => args.execute(os, session).await,
116108
Self::Mcp(args) => args.execute(session).await,
117109
Self::Model(args) => args.execute(session).await,
118-
Self::Subscribe(args) => args.execute(database, session).await,
110+
Self::Subscribe(args) => args.execute(os, session).await,
119111
Self::Persist(subcommand) => subcommand.execute(os, session).await,
120112
// Self::Root(subcommand) => {
121113
// if let Err(err) = subcommand.execute(os, database, telemetry).await {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::cli::chat::{
1414
ChatSession,
1515
ChatState,
1616
};
17-
use crate::database::Database;
17+
use crate::os::Os;
1818

1919
pub struct ModelOption {
2020
pub name: &'static str,
@@ -99,8 +99,8 @@ impl ModelArgs {
9999
}
100100

101101
/// Currently, Sonnet 4 is set as the default model for non-FRA users.
102-
pub fn default_model_id(database: &Database) -> &'static str {
103-
match database.get_auth_profile() {
102+
pub fn default_model_id(os: &Os) -> &'static str {
103+
match os.database.get_auth_profile() {
104104
Ok(Some(profile)) if profile.arn.split(':').nth(3) == Some("eu-central-1") => "CLAUDE_3_7_SONNET_20250219_V1_0",
105105
_ => "CLAUDE_SONNET_4_20250514_V1_0",
106106
}

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

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use crate::cli::chat::{
2020
get_subscription_status_with_spinner,
2121
with_spinner,
2222
};
23-
use crate::database::Database;
23+
use crate::os::Os;
2424
use crate::util::system_info::is_remote;
2525

2626
const SUBSCRIBE_TITLE_TEXT: &str = color_print::cstr! { "<white!,bold>Subscribe to Q Developer Pro</white!,bold>" };
@@ -37,8 +37,8 @@ pub struct SubscribeArgs {
3737
}
3838

3939
impl SubscribeArgs {
40-
pub async fn execute(self, database: &mut Database, session: &mut ChatSession) -> Result<ChatState, ChatError> {
41-
if is_idc_user(database)
40+
pub async fn execute(self, os: &mut Os, session: &mut ChatSession) -> Result<ChatState, ChatError> {
41+
if is_idc_user(&os.database)
4242
.await
4343
.map_err(|e| ChatError::Custom(e.to_string().into()))?
4444
{
@@ -50,7 +50,7 @@ impl SubscribeArgs {
5050
)?;
5151
} else if self.manage {
5252
queue!(session.stderr, style::Print("\n"),)?;
53-
match get_subscription_status_with_spinner(&mut session.stderr, database).await {
53+
match get_subscription_status_with_spinner(os, &mut session.stderr).await {
5454
Ok(status) => {
5555
if status != ActualSubscriptionStatus::Active {
5656
queue!(
@@ -79,7 +79,7 @@ impl SubscribeArgs {
7979

8080
let url = format!(
8181
"https://{}.console.aws.amazon.com/amazonq/developer/home#/subscriptions",
82-
database
82+
os.database
8383
.get_idc_region()
8484
.ok()
8585
.flatten()
@@ -94,7 +94,7 @@ impl SubscribeArgs {
9494
)?;
9595
}
9696
} else {
97-
upgrade_to_pro(database, session).await?;
97+
upgrade_to_pro(os, session).await?;
9898
}
9999

100100
Ok(ChatState::PromptUser {
@@ -103,11 +103,11 @@ impl SubscribeArgs {
103103
}
104104
}
105105

106-
async fn upgrade_to_pro(database: &mut Database, session: &mut ChatSession) -> Result<(), ChatError> {
106+
async fn upgrade_to_pro(os: &mut Os, session: &mut ChatSession) -> Result<(), ChatError> {
107107
queue!(session.stderr, style::Print("\n"),)?;
108108

109109
// Get current subscription status
110-
match get_subscription_status_with_spinner(&mut session.stderr, database).await {
110+
match get_subscription_status_with_spinner(os, &mut session.stderr).await {
111111
Ok(status) => {
112112
if status == ActualSubscriptionStatus::Active {
113113
queue!(
@@ -167,8 +167,12 @@ async fn upgrade_to_pro(database: &mut Database, session: &mut ChatSession) -> R
167167
}
168168

169169
// Create a subscription token and open the webpage
170-
let url = with_spinner(&mut session.stderr, "Preparing to upgrade...", || async {
171-
let r = Client::new(database, None).await?.create_subscription_token().await?;
170+
let r = Client::new(&mut os.database, None)
171+
.await?
172+
.create_subscription_token()
173+
.await?;
174+
175+
let url = with_spinner(&mut session.stderr, "Preparing to upgrade...", || async move {
172176
Ok::<String, ChatError>(r.encoded_verification_url().to_string())
173177
})
174178
.await?;

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -813,7 +813,7 @@ mod tests {
813813

814814
#[tokio::test]
815815
async fn test_profile_ops() -> Result<()> {
816-
let os = Os::new();
816+
let os = Os::new().await.unwrap();
817817
let mut manager = create_test_context_manager(None).await?;
818818

819819
assert_eq!(manager.current_profile, "default");
@@ -852,7 +852,7 @@ mod tests {
852852

853853
#[tokio::test]
854854
async fn test_collect_exceeds_limit() -> Result<()> {
855-
let os = Os::new();
855+
let os = Os::new().await.unwrap();
856856
let mut manager = create_test_context_manager(Some(2)).await?;
857857

858858
os.fs.create_dir_all("test").await?;
@@ -872,7 +872,7 @@ mod tests {
872872

873873
#[tokio::test]
874874
async fn test_path_ops() -> Result<()> {
875-
let os = Os::new();
875+
let os = Os::new().await.unwrap();
876876
let mut manager = create_test_context_manager(None).await?;
877877

878878
// Create some test files for matching.

0 commit comments

Comments
 (0)