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 0c1096d
Show file tree
Hide file tree
Showing 3 changed files with 254 additions and 96 deletions.
63 changes: 46 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,12 +151,51 @@ 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()]),
})
}
}

impl DirectoryContext {
pub fn new(
id: ContextId,
path: &Path,
context_buffers: Vec<ContextBuffer>,
) -> DirectoryContext {
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(),
None => full_path.clone(),
};

let parent = path
.parent()
.and_then(|p| p.file_name())
.map(|p| p.to_string_lossy().into_owned().into());

// TODO: include directory path in text?
let text = context_buffers
.iter()
.map(|b| b.text.clone())
.collect::<Vec<_>>()
.into();

DirectoryContext {
path: path.into(),
context_buffers,
snapshot: ContextSnapshot {
id,
name,
parent,
tooltip: Some(full_path),
icon_path: None,
kind: ContextKind::Directory,
text,
},
}
}

pub fn snapshot(&self) -> ContextSnapshot {
self.snapshot.clone()
}
Expand Down
Loading

0 comments on commit 0c1096d

Please sign in to comment.