Show time in assistant messages

Antonio Scandurra created

Change summary

Cargo.lock                        |  1 +
crates/ai/Cargo.toml              |  5 +++--
crates/ai/src/assistant.rs        | 14 +++++++++++++-
crates/theme/src/theme.rs         |  1 +
styles/src/styleTree/assistant.ts |  4 ++++
5 files changed, 22 insertions(+), 3 deletions(-)

Detailed changes

Cargo.lock 🔗

@@ -101,6 +101,7 @@ version = "0.1.0"
 dependencies = [
  "anyhow",
  "assets",
+ "chrono",
  "collections",
  "editor",
  "futures 0.3.28",

crates/ai/Cargo.toml 🔗

@@ -19,11 +19,12 @@ theme = { path = "../theme" }
 util = { path = "../util" }
 workspace = { path = "../workspace" }
 
-serde.workspace = true
-serde_json.workspace = true
 anyhow.workspace = true
+chrono = "0.4"
 futures.workspace = true
 isahc.workspace = true
+serde.workspace = true
+serde_json.workspace = true
 
 [dev-dependencies]
 editor = { path = "../editor", features = ["test-support"] }

crates/ai/src/assistant.rs 🔗

@@ -1,5 +1,6 @@
 use crate::{OpenAIRequest, OpenAIResponseStreamEvent, RequestMessage, Role};
 use anyhow::{anyhow, Result};
+use chrono::{DateTime, Local};
 use collections::HashMap;
 use editor::{Editor, ExcerptId, ExcerptRange, MultiBuffer};
 use futures::{io::BufReader, AsyncBufReadExt, AsyncReadExt, Stream, StreamExt};
@@ -343,6 +344,7 @@ impl Assistant {
         let message = Message {
             role,
             content: content.clone(),
+            sent_at: Local::now(),
         };
         self.messages.push(message.clone());
         self.messages_by_id.insert(excerpt_id, message.clone());
@@ -394,7 +396,16 @@ impl AssistantEditor {
                             };
 
                             Flex::row()
-                                .with_child(sender)
+                                .with_child(sender.aligned())
+                                .with_child(
+                                    Label::new(
+                                        message.sent_at.format("%I:%M%P").to_string(),
+                                        style.sent_at.text.clone(),
+                                    )
+                                    .contained()
+                                    .with_style(style.sent_at.container)
+                                    .aligned(),
+                                )
                                 .aligned()
                                 .left()
                                 .contained()
@@ -461,6 +472,7 @@ impl Item for AssistantEditor {
 struct Message {
     role: Role,
     content: ModelHandle<Buffer>,
+    sent_at: DateTime<Local>,
 }
 
 async fn stream_completion(

crates/theme/src/theme.rs 🔗

@@ -972,6 +972,7 @@ pub struct TerminalStyle {
 pub struct AssistantStyle {
     pub container: ContainerStyle,
     pub header: ContainerStyle,
+    pub sent_at: ContainedText,
     pub user_sender: ContainedText,
     pub assistant_sender: ContainedText,
 }

styles/src/styleTree/assistant.ts 🔗

@@ -18,6 +18,10 @@ export default function assistant(colorScheme: ColorScheme) {
       },
       assistant_sender: {
         ...text(layer, "sans", "accent", { size: "sm", weight: "bold" }),
+      },
+      sent_at: {
+        margin: { top: 2, left: 8 },
+        ...text(layer, "sans", "default", { size: "2xs" }),
       }
     }
 }