Skip to content

Commit acdf605

Browse files
chore: update chat imports to be relative, minor code refactoring (#1319)
1 parent 7a8a580 commit acdf605

File tree

8 files changed

+89
-77
lines changed

8 files changed

+89
-77
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -758,8 +758,8 @@ fn validate_profile_name(name: &str) -> Result<()> {
758758
mod tests {
759759
use std::io::Stdout;
760760

761+
use super::super::hooks::HookTrigger;
761762
use super::*;
762-
use crate::cli::chat::hooks::HookTrigger;
763763

764764
// Helper function to create a test ContextManager with Context
765765
pub async fn create_test_context_manager() -> Result<ContextManager> {

crates/q_cli/src/cli/chat/conversation_state.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ use super::consts::{
3434
MAX_CONVERSATION_STATE_HISTORY_LEN,
3535
};
3636
use super::context::ContextManager;
37-
use super::hooks::Hook;
37+
use super::hooks::{
38+
Hook,
39+
HookTrigger,
40+
};
3841
use super::message::{
3942
AssistantMessage,
4043
ToolUseResult,
@@ -49,12 +52,9 @@ use super::token_counter::{
4952
CharCounter,
5053
};
5154
use super::tools::{
55+
InputSchema,
5256
QueuedTool,
5357
ToolSpec,
54-
};
55-
use crate::cli::chat::hooks::HookTrigger;
56-
use crate::cli::chat::tools::{
57-
InputSchema,
5858
serde_value_to_document,
5959
};
6060
/// Tracks state related to an ongoing conversation.

crates/q_cli/src/cli/chat/input_source.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ use rustyline::{
77
KeyEvent,
88
};
99

10+
use super::context::ContextManager;
11+
use super::prompt::rl;
1012
use super::skim_integration::SkimCommandSelector;
11-
use crate::cli::chat::context::ContextManager;
12-
use crate::cli::chat::prompt::rl;
1313

1414
#[derive(Debug)]
1515
pub struct InputSource(inner::Inner);
@@ -18,7 +18,7 @@ mod inner {
1818
use rustyline::Editor;
1919
use rustyline::history::FileHistory;
2020

21-
use crate::cli::chat::prompt::ChatHelper;
21+
use super::super::prompt::ChatHelper;
2222

2323
#[derive(Debug)]
2424
pub enum Inner {

crates/q_cli/src/cli/chat/message.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,12 @@ use tracing::error;
2020

2121
use super::consts::MAX_CURRENT_WORKING_DIRECTORY_LEN;
2222
use super::tools::{
23+
InvokeOutput,
2324
OutputKind,
2425
document_to_serde_value,
25-
};
26-
use super::util::truncate_safe;
27-
use crate::cli::chat::tools::{
28-
InvokeOutput,
2926
serde_value_to_document,
3027
};
28+
use super::util::truncate_safe;
3129

3230
#[derive(Debug, Clone, Serialize, Deserialize)]
3331
pub struct UserMessage {

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@ that may eventually reach memory constraints.
119119
)
120120
}
121121
use input_source::InputSource;
122+
use parse::{
123+
ParseState,
124+
interpret_markdown,
125+
};
122126
use parser::{
123127
RecvErrorKind,
124128
ResponseParser,
@@ -151,18 +155,15 @@ use tracing::{
151155
trace,
152156
warn,
153157
};
154-
use util::animate_output;
158+
use util::{
159+
animate_output,
160+
play_notification_bell,
161+
region_check,
162+
};
155163
use uuid::Uuid;
156164
use winnow::Partial;
157165
use winnow::stream::Offset;
158166

159-
use crate::cli::chat::parse::{
160-
ParseState,
161-
interpret_markdown,
162-
};
163-
use crate::util::region_check;
164-
use crate::util::spinner::play_notification_bell;
165-
166167
const WELCOME_TEXT: &str = color_print::cstr! {"
167168
168169
<em>Welcome to </em>

crates/q_cli/src/cli/chat/tools/gh_issue.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ use eyre::{
1717
use fig_os_shim::Context;
1818
use serde::Deserialize;
1919

20+
use super::super::context::ContextManager;
2021
use super::{
2122
InvokeOutput,
2223
ToolPermission,
2324
};
24-
use crate::cli::chat::context::ContextManager;
2525
use crate::cli::issue::IssueCreator;
2626

2727
#[derive(Debug, Clone, Deserialize)]

crates/q_cli/src/cli/chat/util/mod.rs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,24 @@
11
use std::io::Write;
22
use std::time::Duration;
33

4+
use fig_util::system_info::in_cloudshell;
5+
46
use super::ChatError;
57

8+
const GOV_REGIONS: &[&str] = &["us-gov-east-1", "us-gov-west-1"];
9+
10+
pub fn region_check(capability: &'static str) -> eyre::Result<()> {
11+
let Ok(region) = std::env::var("AWS_REGION") else {
12+
return Ok(());
13+
};
14+
15+
if in_cloudshell() && GOV_REGIONS.contains(&region.as_str()) {
16+
eyre::bail!("AWS GovCloud ({region}) is not supported for {capability}.");
17+
}
18+
19+
Ok(())
20+
}
21+
622
pub fn truncate_safe(s: &str, max_bytes: usize) -> &str {
723
if s.len() <= max_bytes {
824
return s;
@@ -29,6 +45,57 @@ pub fn animate_output(output: &mut impl Write, bytes: &[u8]) -> Result<(), ChatE
2945
Ok(())
3046
}
3147

48+
/// Play the terminal bell notification sound
49+
pub fn play_notification_bell(requires_confirmation: bool) {
50+
// Don't play bell for tools that don't require confirmation
51+
if !requires_confirmation {
52+
return;
53+
}
54+
55+
// Check if we should play the bell based on terminal type
56+
if should_play_bell() {
57+
print!("\x07"); // ASCII bell character
58+
std::io::stdout().flush().unwrap();
59+
}
60+
}
61+
62+
/// Determine if we should play the bell based on terminal type
63+
fn should_play_bell() -> bool {
64+
// Get the TERM environment variable
65+
if let Ok(term) = std::env::var("TERM") {
66+
// List of terminals known to handle bell character well
67+
let bell_compatible_terms = [
68+
"xterm",
69+
"xterm-256color",
70+
"screen",
71+
"screen-256color",
72+
"tmux",
73+
"tmux-256color",
74+
"rxvt",
75+
"rxvt-unicode",
76+
"linux",
77+
"konsole",
78+
"gnome",
79+
"gnome-256color",
80+
"alacritty",
81+
"iterm2",
82+
];
83+
84+
// Check if the current terminal is in the compatible list
85+
for compatible_term in bell_compatible_terms.iter() {
86+
if term.starts_with(compatible_term) {
87+
return true;
88+
}
89+
}
90+
91+
// For other terminals, don't play the bell
92+
return false;
93+
}
94+
95+
// If TERM is not set, default to not playing the bell
96+
false
97+
}
98+
3299
#[cfg(test)]
33100
mod tests {
34101
use super::*;

crates/q_cli/src/util/spinner.rs

Lines changed: 1 addition & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,9 @@ use std::sync::mpsc::{
77
TryRecvError,
88
channel,
99
};
10+
use std::thread;
1011
use std::thread::JoinHandle;
1112
use std::time::Duration;
12-
use std::{
13-
env,
14-
thread,
15-
};
1613

1714
use anstream::{
1815
print,
@@ -52,57 +49,6 @@ pub enum SpinnerComponent {
5249
Spinner,
5350
}
5451

55-
/// Play the terminal bell notification sound
56-
pub fn play_notification_bell(requires_confirmation: bool) {
57-
// Don't play bell for tools that don't require confirmation
58-
if !requires_confirmation {
59-
return;
60-
}
61-
62-
// Check if we should play the bell based on terminal type
63-
if should_play_bell() {
64-
print!("\x07"); // ASCII bell character
65-
stdout().flush().unwrap();
66-
}
67-
}
68-
69-
/// Determine if we should play the bell based on terminal type
70-
fn should_play_bell() -> bool {
71-
// Get the TERM environment variable
72-
if let Ok(term) = env::var("TERM") {
73-
// List of terminals known to handle bell character well
74-
let bell_compatible_terms = [
75-
"xterm",
76-
"xterm-256color",
77-
"screen",
78-
"screen-256color",
79-
"tmux",
80-
"tmux-256color",
81-
"rxvt",
82-
"rxvt-unicode",
83-
"linux",
84-
"konsole",
85-
"gnome",
86-
"gnome-256color",
87-
"alacritty",
88-
"iterm2",
89-
];
90-
91-
// Check if the current terminal is in the compatible list
92-
for compatible_term in bell_compatible_terms.iter() {
93-
if term.starts_with(compatible_term) {
94-
return true;
95-
}
96-
}
97-
98-
// For other terminals, don't play the bell
99-
return false;
100-
}
101-
102-
// If TERM is not set, default to not playing the bell
103-
false
104-
}
105-
10652
impl Spinner {
10753
pub fn new(components: Vec<SpinnerComponent>) -> Self {
10854
let (sender, recv) = channel::<Option<String>>();

0 commit comments

Comments
 (0)