Skip to content

Commit

Permalink
assistant2: Implement refresh of context on message editor send
Browse files Browse the repository at this point in the history
  • Loading branch information
mgsloan committed Jan 10, 2025
1 parent 0b105ba commit 0b8f6ce
Show file tree
Hide file tree
Showing 3 changed files with 243 additions and 94 deletions.
24 changes: 7 additions & 17 deletions crates/assistant2/src/context.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::path::Path;
use std::rc::Rc;
use std::sync::Arc;

use file_icons::FileIcons;
use gpui::{AppContext, Model, SharedString};
Expand All @@ -11,7 +10,7 @@ use text::BufferId;
use ui::IconName;
use util::post_inc;

use crate::thread::Thread;
use crate::{context_store::buffer_path_log_err, thread::Thread};

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy, Serialize, Deserialize)]
pub struct ContextId(pub(crate) usize);
Expand Down Expand Up @@ -76,15 +75,15 @@ impl Context {
#[derive(Debug)]
pub struct FileContext {
pub id: ContextId,
pub buffer: ContextBuffer,
pub context_buffer: ContextBuffer,
}

#[derive(Debug)]
pub struct DirectoryContext {
#[allow(unused)]
pub path: Rc<Path>,
#[allow(unused)]
pub buffers: Vec<ContextBuffer>,
pub context_buffers: Vec<ContextBuffer>,
pub snapshot: ContextSnapshot,
}

Expand All @@ -108,7 +107,7 @@ pub struct ThreadContext {
// TODO: Model<Buffer> holds onto the buffer even if the file is deleted and closed. Should remove
// the context from the message editor in this case.

#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct ContextBuffer {
#[allow(unused)]
pub id: BufferId,
Expand All @@ -130,18 +129,9 @@ impl Context {
}

impl FileContext {
pub fn path(&self, cx: &AppContext) -> Option<Arc<Path>> {
let buffer = self.buffer.buffer.read(cx);
if let Some(file) = buffer.file() {
Some(file.path().clone())
} else {
log::error!("Buffer that had a path unexpectedly no longer has a path.");
None
}
}

pub fn snapshot(&self, cx: &AppContext) -> Option<ContextSnapshot> {
let path = self.path(cx)?;
let buffer = self.context_buffer.buffer.read(cx);
let path = buffer_path_log_err(buffer)?;
let full_path: SharedString = path.to_string_lossy().into_owned().into();
let name = match path.file_name() {
Some(name) => name.to_string_lossy().into_owned().into(),
Expand All @@ -161,7 +151,7 @@ impl FileContext {
tooltip: Some(full_path),
icon_path,
kind: ContextKind::File,
text: Box::new([self.buffer.text.clone()]),
text: Box::new([self.context_buffer.text.clone()]),
})
}
}
Expand Down
Loading

0 comments on commit 0b8f6ce

Please sign in to comment.