Save conversations to ~/.config/zed/conversations
Still need to implement loading / listing. I'd really be rather write operations to a database. Maybe we should be auto-saving? Integrating with panes? I just did the simple thing for now.
This commit is contained in:
parent
75b5ac8488
commit
882009bc75
4 changed files with 61 additions and 6 deletions
|
@ -200,6 +200,7 @@
|
||||||
"context": "AssistantEditor > Editor",
|
"context": "AssistantEditor > Editor",
|
||||||
"bindings": {
|
"bindings": {
|
||||||
"cmd-enter": "assistant::Assist",
|
"cmd-enter": "assistant::Assist",
|
||||||
|
"cmd-s": "workspace::Save",
|
||||||
"cmd->": "assistant::QuoteSelection",
|
"cmd->": "assistant::QuoteSelection",
|
||||||
"shift-enter": "assistant::Split",
|
"shift-enter": "assistant::Split",
|
||||||
"ctrl-r": "assistant::CycleMessageRole"
|
"ctrl-r": "assistant::CycleMessageRole"
|
||||||
|
|
|
@ -14,6 +14,13 @@ struct OpenAIRequest {
|
||||||
stream: bool,
|
stream: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize)]
|
||||||
|
struct SavedConversation {
|
||||||
|
zed: String,
|
||||||
|
version: String,
|
||||||
|
messages: Vec<RequestMessage>,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
|
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
|
||||||
struct RequestMessage {
|
struct RequestMessage {
|
||||||
role: Role,
|
role: Role,
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use crate::{
|
use crate::{
|
||||||
assistant_settings::{AssistantDockPosition, AssistantSettings},
|
assistant_settings::{AssistantDockPosition, AssistantSettings},
|
||||||
OpenAIRequest, OpenAIResponseStreamEvent, RequestMessage, Role,
|
OpenAIRequest, OpenAIResponseStreamEvent, RequestMessage, Role, SavedConversation,
|
||||||
};
|
};
|
||||||
use anyhow::{anyhow, Result};
|
use anyhow::{anyhow, Result};
|
||||||
use chrono::{DateTime, Local};
|
use chrono::{DateTime, Local};
|
||||||
|
@ -29,11 +29,14 @@ use std::{
|
||||||
borrow::Cow, cell::RefCell, cmp, fmt::Write, io, iter, ops::Range, rc::Rc, sync::Arc,
|
borrow::Cow, cell::RefCell, cmp, fmt::Write, io, iter, ops::Range, rc::Rc, sync::Arc,
|
||||||
time::Duration,
|
time::Duration,
|
||||||
};
|
};
|
||||||
use util::{channel::ReleaseChannel, post_inc, truncate_and_trailoff, ResultExt, TryFutureExt};
|
use util::{
|
||||||
|
channel::ReleaseChannel, paths::CONVERSATIONS_DIR, post_inc, truncate_and_trailoff, ResultExt,
|
||||||
|
TryFutureExt,
|
||||||
|
};
|
||||||
use workspace::{
|
use workspace::{
|
||||||
dock::{DockPosition, Panel},
|
dock::{DockPosition, Panel},
|
||||||
item::Item,
|
item::Item,
|
||||||
pane, Pane, Workspace,
|
pane, Pane, Save, Workspace,
|
||||||
};
|
};
|
||||||
|
|
||||||
const OPENAI_API_URL: &'static str = "https://api.openai.com/v1";
|
const OPENAI_API_URL: &'static str = "https://api.openai.com/v1";
|
||||||
|
@ -47,7 +50,7 @@ actions!(
|
||||||
CycleMessageRole,
|
CycleMessageRole,
|
||||||
QuoteSelection,
|
QuoteSelection,
|
||||||
ToggleFocus,
|
ToggleFocus,
|
||||||
ResetKey
|
ResetKey,
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -70,6 +73,7 @@ pub fn init(cx: &mut AppContext) {
|
||||||
);
|
);
|
||||||
cx.add_action(AssistantEditor::assist);
|
cx.add_action(AssistantEditor::assist);
|
||||||
cx.capture_action(AssistantEditor::cancel_last_assist);
|
cx.capture_action(AssistantEditor::cancel_last_assist);
|
||||||
|
cx.capture_action(AssistantEditor::save);
|
||||||
cx.add_action(AssistantEditor::quote_selection);
|
cx.add_action(AssistantEditor::quote_selection);
|
||||||
cx.capture_action(AssistantEditor::copy);
|
cx.capture_action(AssistantEditor::copy);
|
||||||
cx.capture_action(AssistantEditor::split);
|
cx.capture_action(AssistantEditor::split);
|
||||||
|
@ -215,8 +219,14 @@ impl AssistantPanel {
|
||||||
|
|
||||||
fn add_context(&mut self, cx: &mut ViewContext<Self>) {
|
fn add_context(&mut self, cx: &mut ViewContext<Self>) {
|
||||||
let focus = self.has_focus(cx);
|
let focus = self.has_focus(cx);
|
||||||
let editor = cx
|
let editor = cx.add_view(|cx| {
|
||||||
.add_view(|cx| AssistantEditor::new(self.api_key.clone(), self.languages.clone(), cx));
|
AssistantEditor::new(
|
||||||
|
self.api_key.clone(),
|
||||||
|
self.languages.clone(),
|
||||||
|
self.fs.clone(),
|
||||||
|
cx,
|
||||||
|
)
|
||||||
|
});
|
||||||
self.subscriptions
|
self.subscriptions
|
||||||
.push(cx.subscribe(&editor, Self::handle_assistant_editor_event));
|
.push(cx.subscribe(&editor, Self::handle_assistant_editor_event));
|
||||||
self.pane.update(cx, |pane, cx| {
|
self.pane.update(cx, |pane, cx| {
|
||||||
|
@ -922,6 +932,33 @@ impl Assistant {
|
||||||
None
|
None
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn save(&self, fs: Arc<dyn Fs>, cx: &mut ModelContext<Assistant>) -> Task<Result<()>> {
|
||||||
|
let conversation = SavedConversation {
|
||||||
|
zed: "conversation".into(),
|
||||||
|
version: "0.1".into(),
|
||||||
|
messages: self.open_ai_request_messages(cx),
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut path = CONVERSATIONS_DIR.join(self.summary.as_deref().unwrap_or("conversation-1"));
|
||||||
|
|
||||||
|
cx.background().spawn(async move {
|
||||||
|
while fs.is_file(&path).await {
|
||||||
|
let file_name = path.file_name().ok_or_else(|| anyhow!("no filename"))?;
|
||||||
|
let file_name = file_name.to_string_lossy();
|
||||||
|
|
||||||
|
if let Some((prefix, suffix)) = file_name.rsplit_once('-') {
|
||||||
|
let new_version = suffix.parse::<u32>().ok().unwrap_or(1) + 1;
|
||||||
|
path.set_file_name(format!("{}-{}", prefix, new_version));
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
fs.create_dir(CONVERSATIONS_DIR.as_ref()).await?;
|
||||||
|
fs.atomic_write(path, serde_json::to_string(&conversation).unwrap())
|
||||||
|
.await?;
|
||||||
|
Ok(())
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct PendingCompletion {
|
struct PendingCompletion {
|
||||||
|
@ -941,6 +978,7 @@ struct ScrollPosition {
|
||||||
|
|
||||||
struct AssistantEditor {
|
struct AssistantEditor {
|
||||||
assistant: ModelHandle<Assistant>,
|
assistant: ModelHandle<Assistant>,
|
||||||
|
fs: Arc<dyn Fs>,
|
||||||
editor: ViewHandle<Editor>,
|
editor: ViewHandle<Editor>,
|
||||||
blocks: HashSet<BlockId>,
|
blocks: HashSet<BlockId>,
|
||||||
scroll_position: Option<ScrollPosition>,
|
scroll_position: Option<ScrollPosition>,
|
||||||
|
@ -951,6 +989,7 @@ impl AssistantEditor {
|
||||||
fn new(
|
fn new(
|
||||||
api_key: Rc<RefCell<Option<String>>>,
|
api_key: Rc<RefCell<Option<String>>>,
|
||||||
language_registry: Arc<LanguageRegistry>,
|
language_registry: Arc<LanguageRegistry>,
|
||||||
|
fs: Arc<dyn Fs>,
|
||||||
cx: &mut ViewContext<Self>,
|
cx: &mut ViewContext<Self>,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
let assistant = cx.add_model(|cx| Assistant::new(api_key, language_registry, cx));
|
let assistant = cx.add_model(|cx| Assistant::new(api_key, language_registry, cx));
|
||||||
|
@ -972,6 +1011,7 @@ impl AssistantEditor {
|
||||||
editor,
|
editor,
|
||||||
blocks: Default::default(),
|
blocks: Default::default(),
|
||||||
scroll_position: None,
|
scroll_position: None,
|
||||||
|
fs,
|
||||||
_subscriptions,
|
_subscriptions,
|
||||||
};
|
};
|
||||||
this.update_message_headers(cx);
|
this.update_message_headers(cx);
|
||||||
|
@ -1299,6 +1339,12 @@ impl AssistantEditor {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn save(&mut self, _: &Save, cx: &mut ViewContext<Self>) {
|
||||||
|
self.assistant.update(cx, |assistant, cx| {
|
||||||
|
assistant.save(self.fs.clone(), cx).detach_and_log_err(cx);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
fn cycle_model(&mut self, cx: &mut ViewContext<Self>) {
|
fn cycle_model(&mut self, cx: &mut ViewContext<Self>) {
|
||||||
self.assistant.update(cx, |assistant, cx| {
|
self.assistant.update(cx, |assistant, cx| {
|
||||||
let new_model = match assistant.model.as_str() {
|
let new_model = match assistant.model.as_str() {
|
||||||
|
|
|
@ -5,6 +5,7 @@ use serde::{Deserialize, Serialize};
|
||||||
lazy_static::lazy_static! {
|
lazy_static::lazy_static! {
|
||||||
pub static ref HOME: PathBuf = dirs::home_dir().expect("failed to determine home directory");
|
pub static ref HOME: PathBuf = dirs::home_dir().expect("failed to determine home directory");
|
||||||
pub static ref CONFIG_DIR: PathBuf = HOME.join(".config").join("zed");
|
pub static ref CONFIG_DIR: PathBuf = HOME.join(".config").join("zed");
|
||||||
|
pub static ref CONVERSATIONS_DIR: PathBuf = HOME.join(".config/zed/conversations");
|
||||||
pub static ref LOGS_DIR: PathBuf = HOME.join("Library/Logs/Zed");
|
pub static ref LOGS_DIR: PathBuf = HOME.join("Library/Logs/Zed");
|
||||||
pub static ref SUPPORT_DIR: PathBuf = HOME.join("Library/Application Support/Zed");
|
pub static ref SUPPORT_DIR: PathBuf = HOME.join("Library/Application Support/Zed");
|
||||||
pub static ref LANGUAGES_DIR: PathBuf = HOME.join("Library/Application Support/Zed/languages");
|
pub static ref LANGUAGES_DIR: PathBuf = HOME.join("Library/Application Support/Zed/languages");
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue