Skip to content

Commit 51c48bf

Browse files
fix: remove cw telemetry client for sigv4, revert skipping banner printing to stderr in noninteractive mode (#335)
1 parent 7caaee6 commit 51c48bf

File tree

3 files changed

+35
-26
lines changed

3 files changed

+35
-26
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ impl ApiClient {
9090
.region(endpoint.region.clone())
9191
.credentials_provider(credentials)
9292
.timeout_config(timeout_config(database))
93-
.retry_config(RetryConfig::adaptive())
93+
.retry_config(retry_config())
9494
.load()
9595
.await;
9696

@@ -137,7 +137,7 @@ impl ApiClient {
137137
.region(endpoint.region.clone())
138138
.credentials_provider(credentials_chain)
139139
.timeout_config(timeout_config(database))
140-
.retry_config(RetryConfig::adaptive())
140+
.retry_config(retry_config())
141141
.load()
142142
.await,
143143
)
@@ -463,6 +463,10 @@ fn timeout_config(database: &Database) -> TimeoutConfig {
463463
.build()
464464
}
465465

466+
fn retry_config() -> RetryConfig {
467+
RetryConfig::standard().with_max_attempts(1)
468+
}
469+
466470
pub fn stalled_stream_protection_config() -> StalledStreamProtectionConfig {
467471
StalledStreamProtectionConfig::enabled()
468472
.grace_period(Duration::from_secs(60 * 5))

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

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -884,7 +884,6 @@ impl Default for ChatState {
884884
impl ChatSession {
885885
async fn spawn(&mut self, os: &mut Os) -> Result<()> {
886886
let is_small_screen = self.terminal_width() < GREETING_BREAK_POINT;
887-
let mut interactive_text: Vec<u8> = Vec::new();
888887
if os
889888
.database
890889
.settings
@@ -899,20 +898,20 @@ impl ChatSession {
899898
},
900899
};
901900

902-
execute!(interactive_text, style::Print(welcome_text), style::Print("\n\n"),)?;
901+
execute!(self.stderr, style::Print(welcome_text), style::Print("\n\n"),)?;
903902

904903
let tip = ROTATING_TIPS[usize::try_from(rand::random::<u32>()).unwrap_or(0) % ROTATING_TIPS.len()];
905904
if is_small_screen {
906905
// If the screen is small, print the tip in a single line
907906
execute!(
908-
interactive_text,
907+
self.stderr,
909908
style::Print("💡 ".to_string()),
910909
style::Print(tip),
911910
style::Print("\n")
912911
)?;
913912
} else {
914913
draw_box(
915-
&mut interactive_text,
914+
&mut self.stderr,
916915
"Did you know?",
917916
tip,
918917
GREETING_BREAK_POINT,
@@ -921,7 +920,7 @@ impl ChatSession {
921920
}
922921

923922
execute!(
924-
interactive_text,
923+
self.stderr,
925924
style::Print("\n"),
926925
style::Print(match is_small_screen {
927926
true => SMALL_SCREEN_POPULAR_SHORTCUTS,
@@ -934,27 +933,24 @@ impl ChatSession {
934933
.dark_grey()
935934
)
936935
)?;
937-
execute!(
938-
interactive_text,
939-
style::Print("\n"),
940-
style::SetForegroundColor(Color::Reset)
941-
)?;
936+
execute!(self.stderr, style::Print("\n"), style::SetForegroundColor(Color::Reset))?;
942937
}
943938

944939
if self.all_tools_trusted() {
945940
queue!(
946-
interactive_text,
941+
self.stderr,
947942
style::Print(format!(
948943
"{}{TRUST_ALL_TEXT}\n\n",
949944
if !is_small_screen { "\n" } else { "" }
950945
))
951946
)?;
952947
}
948+
self.stderr.flush()?;
953949

954950
if let Some(ref id) = self.conversation.model {
955951
if let Some(model_option) = MODEL_OPTIONS.iter().find(|option| option.model_id == *id) {
956952
execute!(
957-
interactive_text,
953+
self.stderr,
958954
style::SetForegroundColor(Color::Cyan),
959955
style::Print(format!("🤖 You are chatting with {}\n", model_option.name)),
960956
style::SetForegroundColor(Color::Reset),
@@ -963,11 +959,6 @@ impl ChatSession {
963959
}
964960
}
965961

966-
if self.interactive {
967-
self.stderr.write_all(&interactive_text)?;
968-
self.stderr.flush()?;
969-
}
970-
971962
if let Some(user_input) = self.initial_input.take() {
972963
self.inner = Some(ChatState::HandleInput { input: user_input });
973964
}

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

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ async fn set_start_url_and_region(database: &Database, event: &mut Event) {
364364
struct TelemetryClient {
365365
client_id: Uuid,
366366
telemetry_enabled: bool,
367-
codewhisperer_client: ApiClient,
367+
codewhisperer_client: Option<ApiClient>,
368368
toolkit_telemetry_client: Option<ToolkitTelemetryClient>,
369369
}
370370

@@ -421,23 +421,36 @@ impl TelemetryClient {
421421
})
422422
}
423423

424+
// cw telemetry is only available with bearer token auth.
425+
let codewhisperer_client = if env.get("AMAZON_Q_SIGV4").is_ok() {
426+
None
427+
} else {
428+
Some(ApiClient::new(env, fs, database, None).await?)
429+
};
430+
424431
Ok(Self {
425432
client_id: client_id(env, database, telemetry_enabled)?,
426433
telemetry_enabled,
427434
toolkit_telemetry_client,
428-
codewhisperer_client: ApiClient::new(env, fs, database, None).await?,
435+
codewhisperer_client,
429436
})
430437
}
431438

439+
/// Sends a telemetry event to both the CW and toolkit API's. If the clients do not exist, then
440+
/// telemetry is not sent.
441+
///
442+
/// See [TelemetryClient::new] for which conditions the clients are created for.
432443
async fn send_event(&self, event: Event) {
433-
// This client will exist when telemetry is disabled.
434444
self.send_cw_telemetry_event(&event).await;
435-
436-
// This client won't exist when telemetry is disabled.
437445
self.send_telemetry_toolkit_metric(event).await;
438446
}
439447

440448
async fn send_cw_telemetry_event(&self, event: &Event) {
449+
let Some(codewhisperer_client) = self.codewhisperer_client.clone() else {
450+
trace!("not sending cw metric - client does not exist");
451+
return;
452+
};
453+
441454
if let EventType::ChatAddedMessage {
442455
conversation_id,
443456
message_id,
@@ -466,8 +479,7 @@ impl TelemetryClient {
466479
telemetry_enabled = self.telemetry_enabled,
467480
"Sending cw telemetry event"
468481
);
469-
if let Err(err) = self
470-
.codewhisperer_client
482+
if let Err(err) = codewhisperer_client
471483
.send_telemetry_event(event, user_context, self.telemetry_enabled, model.to_owned())
472484
.await
473485
{
@@ -651,6 +663,8 @@ mod test {
651663
.unwrap();
652664
client
653665
.codewhisperer_client
666+
.as_ref()
667+
.expect("cw telemetry client should exist")
654668
.send_telemetry_event(
655669
TelemetryEvent::ChatAddMessageEvent(
656670
ChatAddMessageEvent::builder()

0 commit comments

Comments
 (0)