assistant2: Add ability to open past threads (#21548)

This PR adds the ability to open past threads in Assistant 2.

There are also some mocked threads in the history for testing purposes.

Release Notes:

- N/A
This commit is contained in:
Marshall Bowers 2024-12-04 14:35:44 -05:00 committed by GitHub
parent 44264ffedc
commit 0bde0f8e2f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 243 additions and 60 deletions

View file

@ -14,12 +14,28 @@ use language_model::{
use language_models::provider::cloud::{MaxMonthlySpendReachedError, PaymentRequiredError};
use serde::{Deserialize, Serialize};
use util::post_inc;
use uuid::Uuid;
#[derive(Debug, Clone, Copy)]
pub enum RequestKind {
Chat,
}
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Serialize, Deserialize)]
pub struct ThreadId(Arc<str>);
impl ThreadId {
pub fn new() -> Self {
Self(Uuid::new_v4().to_string().into())
}
}
impl std::fmt::Display for ThreadId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy, Serialize, Deserialize)]
pub struct MessageId(usize);
@ -39,6 +55,7 @@ pub struct Message {
/// A thread of conversation with the LLM.
pub struct Thread {
id: ThreadId,
messages: Vec<Message>,
next_message_id: MessageId,
completion_count: usize,
@ -52,6 +69,7 @@ pub struct Thread {
impl Thread {
pub fn new(tools: Arc<ToolWorkingSet>, _cx: &mut ModelContext<Self>) -> Self {
Self {
id: ThreadId::new(),
messages: Vec::new(),
next_message_id: MessageId(0),
completion_count: 0,
@ -63,10 +81,18 @@ impl Thread {
}
}
pub fn id(&self) -> &ThreadId {
&self.id
}
pub fn message(&self, id: MessageId) -> Option<&Message> {
self.messages.iter().find(|message| message.id == id)
}
pub fn messages(&self) -> impl Iterator<Item = &Message> {
self.messages.iter()
}
pub fn tools(&self) -> &Arc<ToolWorkingSet> {
&self.tools
}
@ -76,10 +102,19 @@ impl Thread {
}
pub fn insert_user_message(&mut self, text: impl Into<String>, cx: &mut ModelContext<Self>) {
self.insert_message(Role::User, text, cx)
}
pub fn insert_message(
&mut self,
role: Role,
text: impl Into<String>,
cx: &mut ModelContext<Self>,
) {
let id = self.next_message_id.post_inc();
self.messages.push(Message {
id,
role: Role::User,
role,
text: text.into(),
});
cx.emit(ThreadEvent::MessageAdded(id));