Skip to content

Commit 87e53cc

Browse files
committed
add modelin in telemetry
1 parent 6b5fa35 commit 87e53cc

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

crates/chat-cli/src/api_client/clients/client.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ impl Client {
7878
telemetry_event: TelemetryEvent,
7979
user_context: UserContext,
8080
telemetry_enabled: bool,
81+
model_id: Option<String>,
8182
) -> Result<(), ApiClientError> {
8283
match &self.inner {
8384
inner::Inner::Codewhisperer(client) => {
@@ -90,6 +91,7 @@ impl Client {
9091
false => OptOutPreference::OptOut,
9192
})
9293
.set_profile_arn(self.profile.as_ref().map(|p| p.arn.clone()))
94+
.set_model_id(model_id)
9395
.send()
9496
.await;
9597
Ok(())
@@ -159,6 +161,7 @@ mod tests {
159161
.build()
160162
.unwrap(),
161163
false,
164+
Some("model".to_owned()),
162165
)
163166
.await
164167
.unwrap();

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3017,7 +3017,7 @@ impl ChatContext {
30173017
queue!(self.output, style::Print("\n"))?;
30183018
let labels: Vec<&str> = MODEL_OPTIONS.iter().map(|(l, _)| *l).collect();
30193019
let selection: Option<_> = match Select::with_theme(&crate::util::dialoguer_theme())
3020-
.with_prompt("Select the model you want to use for chat")
3020+
.with_prompt("Select a model for this chat session")
30213021
.items(&labels)
30223022
.default(0)
30233023
.interact_on_opt(&dialoguer::console::Term::stdout())
@@ -3031,13 +3031,16 @@ impl ChatContext {
30313031
if let Some(index) = selection {
30323032
let (label, model_opt) = MODEL_OPTIONS[index];
30333033

3034+
self.conversation_state.current_model_id = Some(model_opt.to_string());
3035+
telemetry.update_model_id(self.conversation_state.current_model_id.clone());
3036+
30343037
use crossterm::{
30353038
queue,
30363039
style,
30373040
};
30383041
queue!(self.output, style::Print("\n"), style::Print(format!(" Swtiched model to {}\n\n", label)))?;
30393042
}
3040-
3043+
30413044
ChatState::PromptUser {
30423045
tool_uses: None,
30433046
pending_tool_index: None,

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

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ mod install_method;
66

77
use core::ToolUseEventBuilder;
88
use std::str::FromStr;
9+
use std::sync::{Arc, RwLock};
910

1011
use amzn_codewhisperer_client::types::{
1112
ChatAddMessageEvent,
@@ -131,20 +132,24 @@ impl TelemetryStage {
131132
pub struct TelemetryThread {
132133
handle: Option<JoinHandle<()>>,
133134
tx: mpsc::UnboundedSender<Event>,
135+
current_model_id: Arc<RwLock<Option<String>>>,
134136
}
135137

136138
impl Clone for TelemetryThread {
137139
fn clone(&self) -> Self {
138140
Self {
139141
handle: None,
140142
tx: self.tx.clone(),
143+
current_model_id: Arc::clone(&self.current_model_id),
141144
}
142145
}
143146
}
144147

145148
impl TelemetryThread {
146149
pub async fn new(env: &Env, database: &mut Database) -> Result<Self, TelemetryError> {
147-
let telemetry_client = TelemetryClient::new(env, database).await?;
150+
//todo yifan change to last used modelid
151+
let current_model_id = Arc::new(RwLock::new(None));
152+
let telemetry_client = TelemetryClient::new(env, database, current_model_id.clone()).await?;
148153
let (tx, mut rx) = mpsc::unbounded_channel();
149154
let handle = tokio::spawn(async move {
150155
while let Some(event) = rx.recv().await {
@@ -156,9 +161,14 @@ impl TelemetryThread {
156161
Ok(Self {
157162
handle: Some(handle),
158163
tx,
164+
current_model_id,
159165
})
160166
}
161167

168+
pub fn update_model_id(&self, model_id: Option<String>) {
169+
*self.current_model_id.write().unwrap() = model_id;
170+
}
171+
162172
pub async fn finish(self) -> Result<(), TelemetryError> {
163173
drop(self.tx);
164174
if let Some(handle) = self.handle {
@@ -275,10 +285,11 @@ struct TelemetryClient {
275285
telemetry_enabled: bool,
276286
codewhisperer_client: CodewhispererClient,
277287
toolkit_telemetry_client: Option<ToolkitTelemetryClient>,
288+
current_model_id: Arc<RwLock<Option<String>>>,
278289
}
279290

280291
impl TelemetryClient {
281-
async fn new(env: &Env, database: &mut Database) -> Result<Self, TelemetryError> {
292+
async fn new(env: &Env, database: &mut Database, current_model_id: Arc<RwLock<Option<String>>>) -> Result<Self, TelemetryError> {
282293
let telemetry_enabled = !cfg!(test)
283294
&& env.get_os("Q_DISABLE_TELEMETRY").is_none()
284295
&& database.settings.get_bool(Setting::TelemetryEnabled).unwrap_or(true);
@@ -338,6 +349,7 @@ impl TelemetryClient {
338349
telemetry_enabled,
339350
toolkit_telemetry_client,
340351
codewhisperer_client: CodewhispererClient::new(database, None).await?,
352+
current_model_id,
341353
})
342354
}
343355

@@ -356,6 +368,7 @@ impl TelemetryClient {
356368
..
357369
} = &event.ty
358370
{
371+
let model_id = self.current_model_id.read().unwrap().clone();
359372
let user_context = self.user_context().unwrap();
360373

361374
let chat_add_message_event = match ChatAddMessageEvent::builder()
@@ -376,6 +389,7 @@ impl TelemetryClient {
376389
TelemetryEvent::ChatAddMessageEvent(chat_add_message_event),
377390
user_context,
378391
self.telemetry_enabled,
392+
model_id,
379393
)
380394
.await
381395
{
@@ -451,7 +465,7 @@ mod test {
451465
#[tokio::test]
452466
async fn client_context() {
453467
let mut database = Database::new().await.unwrap();
454-
let client = TelemetryClient::new(&Env::new(), &mut database).await.unwrap();
468+
let client = TelemetryClient::new(&Env::new(), &mut database, Arc::new(RwLock::new(Some("model".to_owned())))).await.unwrap();
455469
let context = client.user_context().unwrap();
456470

457471
assert_eq!(context.ide_category, IdeCategory::Cli);
@@ -511,7 +525,7 @@ mod test {
511525
#[ignore = "needs auth which is not in CI"]
512526
async fn test_without_optout() {
513527
let mut database = Database::new().await.unwrap();
514-
let client = TelemetryClient::new(&Env::new(), &mut database).await.unwrap();
528+
let client = TelemetryClient::new(&Env::new(), &mut database, Arc::new(RwLock::new(Some("model".to_owned())))).await.unwrap();
515529
client
516530
.codewhisperer_client
517531
.send_telemetry_event(
@@ -524,6 +538,7 @@ mod test {
524538
),
525539
client.user_context().unwrap(),
526540
false,
541+
Some("model".to_owned()),
527542
)
528543
.await
529544
.unwrap();

0 commit comments

Comments
 (0)