assistant2: Add prompt editor (#23436)

This PR adds the Assistant1 experience to Assistant2 as a "prompt
editor".

<img width="1309" alt="Screenshot 2025-01-21 at 7 17 26 PM"
src="https://github.com/user-attachments/assets/3ce2f32b-2b1a-48a8-8e56-4c44e3ac4ce5"
/>

Release Notes:

- N/A
This commit is contained in:
Marshall Bowers 2025-01-21 19:36:55 -05:00 committed by GitHub
parent 3d47f32f0c
commit e59c910845
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 149 additions and 59 deletions

2
Cargo.lock generated
View file

@ -438,7 +438,9 @@ name = "assistant2"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"assistant_context_editor",
"assistant_settings", "assistant_settings",
"assistant_slash_command",
"assistant_tool", "assistant_tool",
"async-watch", "async-watch",
"chrono", "chrono",

View file

@ -227,6 +227,7 @@
"use_key_equivalents": true, "use_key_equivalents": true,
"bindings": { "bindings": {
"cmd-n": "assistant2::NewThread", "cmd-n": "assistant2::NewThread",
"cmd-alt-p": "assistant2::NewPromptEditor",
"cmd-shift-h": "assistant2::OpenHistory", "cmd-shift-h": "assistant2::OpenHistory",
"cmd-alt-/": "assistant2::ToggleModelSelector", "cmd-alt-/": "assistant2::ToggleModelSelector",
"cmd-shift-a": "assistant2::ToggleContextPicker", "cmd-shift-a": "assistant2::ToggleContextPicker",

View file

@ -5,9 +5,10 @@ use crate::{
}; };
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Result};
use assistant_context_editor::{ use assistant_context_editor::{
AssistantPanelDelegate, Context, ContextEditor, ContextEditorToolbarItem, make_lsp_adapter_delegate, AssistantPanelDelegate, Context, ContextEditor,
ContextEditorToolbarItemEvent, ContextId, ContextStore, ContextStoreEvent, InsertDraggedFiles, ContextEditorToolbarItem, ContextEditorToolbarItemEvent, ContextId, ContextStore,
SlashCommandCompletionProvider, ToggleModelSelector, DEFAULT_TAB_TITLE, ContextStoreEvent, InsertDraggedFiles, SlashCommandCompletionProvider, ToggleModelSelector,
DEFAULT_TAB_TITLE,
}; };
use assistant_settings::{AssistantDockPosition, AssistantSettings}; use assistant_settings::{AssistantDockPosition, AssistantSettings};
use assistant_slash_command::SlashCommandWorkingSet; use assistant_slash_command::SlashCommandWorkingSet;
@ -22,12 +23,11 @@ use gpui::{
ParentElement, Pixels, Render, SharedString, StatefulInteractiveElement, Styled, Subscription, ParentElement, Pixels, Render, SharedString, StatefulInteractiveElement, Styled, Subscription,
Task, UpdateGlobal, View, WeakView, Task, UpdateGlobal, View, WeakView,
}; };
use language::{LanguageRegistry, LspAdapterDelegate}; use language::LanguageRegistry;
use language_model::{ use language_model::{
LanguageModelProvider, LanguageModelProviderId, LanguageModelRegistry, ZED_CLOUD_PROVIDER_ID, LanguageModelProvider, LanguageModelProviderId, LanguageModelRegistry, ZED_CLOUD_PROVIDER_ID,
}; };
use language_model_selector::LanguageModelSelector; use language_model_selector::LanguageModelSelector;
use project::lsp_store::LocalLspAdapterDelegate;
use project::Project; use project::Project;
use prompt_library::{open_prompt_library, PromptBuilder, PromptLibrary}; use prompt_library::{open_prompt_library, PromptBuilder, PromptLibrary};
use search::{buffer_search::DivRegistrar, BufferSearchBar}; use search::{buffer_search::DivRegistrar, BufferSearchBar};
@ -1548,27 +1548,3 @@ impl Item for ConfigurationView {
Some("Configuration".into()) Some("Configuration".into())
} }
} }
fn make_lsp_adapter_delegate(
project: &Model<Project>,
cx: &mut AppContext,
) -> Result<Option<Arc<dyn LspAdapterDelegate>>> {
project.update(cx, |project, cx| {
// TODO: Find the right worktree.
let Some(worktree) = project.worktrees(cx).next() else {
return Ok(None::<Arc<dyn LspAdapterDelegate>>);
};
let http_client = project.client().http_client().clone();
project.lsp_store().update(cx, |_, cx| {
Ok(Some(LocalLspAdapterDelegate::new(
project.languages().clone(),
project.environment(),
cx.weak_model(),
&worktree,
http_client,
project.fs().clone(),
cx,
) as Arc<dyn LspAdapterDelegate>))
})
})
}

View file

@ -20,7 +20,9 @@ test-support = [
[dependencies] [dependencies]
anyhow.workspace = true anyhow.workspace = true
assistant_context_editor.workspace = true
assistant_settings.workspace = true assistant_settings.workspace = true
assistant_slash_command.workspace = true
assistant_tool.workspace = true assistant_tool.workspace = true
async-watch.workspace = true async-watch.workspace = true
chrono.workspace = true chrono.workspace = true

View file

@ -36,6 +36,7 @@ actions!(
[ [
ToggleFocus, ToggleFocus,
NewThread, NewThread,
NewPromptEditor,
ToggleContextPicker, ToggleContextPicker,
ToggleModelSelector, ToggleModelSelector,
RemoveAllContext, RemoveAllContext,

View file

@ -1,19 +1,23 @@
use std::sync::Arc; use std::sync::Arc;
use anyhow::Result; use anyhow::Result;
use assistant_context_editor::{make_lsp_adapter_delegate, ContextEditor};
use assistant_settings::{AssistantDockPosition, AssistantSettings}; use assistant_settings::{AssistantDockPosition, AssistantSettings};
use assistant_slash_command::SlashCommandWorkingSet;
use assistant_tool::ToolWorkingSet; use assistant_tool::ToolWorkingSet;
use client::zed_urls; use client::zed_urls;
use fs::Fs; use fs::Fs;
use gpui::{ use gpui::{
prelude::*, px, svg, Action, AnyElement, AppContext, AsyncWindowContext, EventEmitter, prelude::*, px, svg, Action, AnyElement, AppContext, AsyncWindowContext, Corner, EventEmitter,
FocusHandle, FocusableView, FontWeight, Model, Pixels, Task, View, ViewContext, WeakView, FocusHandle, FocusableView, FontWeight, Model, Pixels, Task, View, ViewContext, WeakView,
WindowContext, WindowContext,
}; };
use language::LanguageRegistry; use language::LanguageRegistry;
use prompt_library::PromptBuilder;
use settings::Settings; use settings::Settings;
use time::UtcOffset; use time::UtcOffset;
use ui::{prelude::*, KeyBinding, Tab, Tooltip}; use ui::{prelude::*, ContextMenu, KeyBinding, PopoverMenu, PopoverMenuHandle, Tab, Tooltip};
use util::ResultExt as _;
use workspace::dock::{DockPosition, Panel, PanelEvent}; use workspace::dock::{DockPosition, Panel, PanelEvent};
use workspace::Workspace; use workspace::Workspace;
@ -22,7 +26,7 @@ use crate::message_editor::MessageEditor;
use crate::thread::{Thread, ThreadError, ThreadId}; use crate::thread::{Thread, ThreadError, ThreadId};
use crate::thread_history::{PastThread, ThreadHistory}; use crate::thread_history::{PastThread, ThreadHistory};
use crate::thread_store::ThreadStore; use crate::thread_store::ThreadStore;
use crate::{NewThread, OpenHistory, ToggleFocus}; use crate::{NewPromptEditor, NewThread, OpenHistory, ToggleFocus};
pub fn init(cx: &mut AppContext) { pub fn init(cx: &mut AppContext) {
cx.observe_new_views( cx.observe_new_views(
@ -42,6 +46,12 @@ pub fn init(cx: &mut AppContext) {
workspace.focus_panel::<AssistantPanel>(cx); workspace.focus_panel::<AssistantPanel>(cx);
panel.update(cx, |panel, cx| panel.open_history(cx)); panel.update(cx, |panel, cx| panel.open_history(cx));
} }
})
.register_action(|workspace, _: &NewPromptEditor, cx| {
if let Some(panel) = workspace.panel::<AssistantPanel>(cx) {
workspace.focus_panel::<AssistantPanel>(cx);
panel.update(cx, |panel, cx| panel.new_prompt_editor(workspace, cx));
}
}); });
}, },
) )
@ -50,6 +60,7 @@ pub fn init(cx: &mut AppContext) {
enum ActiveView { enum ActiveView {
Thread, Thread,
PromptEditor,
History, History,
} }
@ -60,10 +71,13 @@ pub struct AssistantPanel {
thread_store: Model<ThreadStore>, thread_store: Model<ThreadStore>,
thread: View<ActiveThread>, thread: View<ActiveThread>,
message_editor: View<MessageEditor>, message_editor: View<MessageEditor>,
context_store: Model<assistant_context_editor::ContextStore>,
context_editor: Option<View<ContextEditor>>,
tools: Arc<ToolWorkingSet>, tools: Arc<ToolWorkingSet>,
local_timezone: UtcOffset, local_timezone: UtcOffset,
active_view: ActiveView, active_view: ActiveView,
history: View<ThreadHistory>, history: View<ThreadHistory>,
new_item_context_menu_handle: PopoverMenuHandle<ContextMenu>,
width: Option<Pixels>, width: Option<Pixels>,
height: Option<Pixels>, height: Option<Pixels>,
} }
@ -71,6 +85,7 @@ pub struct AssistantPanel {
impl AssistantPanel { impl AssistantPanel {
pub fn load( pub fn load(
workspace: WeakView<Workspace>, workspace: WeakView<Workspace>,
prompt_builder: Arc<PromptBuilder>,
cx: AsyncWindowContext, cx: AsyncWindowContext,
) -> Task<Result<View<Self>>> { ) -> Task<Result<View<Self>>> {
cx.spawn(|mut cx| async move { cx.spawn(|mut cx| async move {
@ -82,8 +97,22 @@ impl AssistantPanel {
})? })?
.await?; .await?;
let slash_commands = Arc::new(SlashCommandWorkingSet::default());
let context_store = workspace
.update(&mut cx, |workspace, cx| {
let project = workspace.project().clone();
assistant_context_editor::ContextStore::new(
project,
prompt_builder.clone(),
slash_commands,
tools.clone(),
cx,
)
})?
.await?;
workspace.update(&mut cx, |workspace, cx| { workspace.update(&mut cx, |workspace, cx| {
cx.new_view(|cx| Self::new(workspace, thread_store, tools, cx)) cx.new_view(|cx| Self::new(workspace, thread_store, context_store, tools, cx))
}) })
}) })
} }
@ -91,6 +120,7 @@ impl AssistantPanel {
fn new( fn new(
workspace: &Workspace, workspace: &Workspace,
thread_store: Model<ThreadStore>, thread_store: Model<ThreadStore>,
context_store: Model<assistant_context_editor::ContextStore>,
tools: Arc<ToolWorkingSet>, tools: Arc<ToolWorkingSet>,
cx: &mut ViewContext<Self>, cx: &mut ViewContext<Self>,
) -> Self { ) -> Self {
@ -126,12 +156,15 @@ impl AssistantPanel {
) )
}), }),
message_editor, message_editor,
context_store,
context_editor: None,
tools, tools,
local_timezone: UtcOffset::from_whole_seconds( local_timezone: UtcOffset::from_whole_seconds(
chrono::Local::now().offset().local_minus_utc(), chrono::Local::now().offset().local_minus_utc(),
) )
.unwrap(), .unwrap(),
history: cx.new_view(|cx| ThreadHistory::new(weak_self, thread_store, cx)), history: cx.new_view(|cx| ThreadHistory::new(weak_self, thread_store, cx)),
new_item_context_menu_handle: PopoverMenuHandle::default(),
width: None, width: None,
height: None, height: None,
} }
@ -177,6 +210,31 @@ impl AssistantPanel {
self.message_editor.focus_handle(cx).focus(cx); self.message_editor.focus_handle(cx).focus(cx);
} }
fn new_prompt_editor(&mut self, workspace: &mut Workspace, cx: &mut ViewContext<Self>) {
self.active_view = ActiveView::PromptEditor;
let project = workspace.project().clone();
let context = self
.context_store
.update(cx, |context_store, cx| context_store.create(cx));
let lsp_adapter_delegate = make_lsp_adapter_delegate(&project, cx).log_err().flatten();
self.context_editor = Some(cx.new_view(|cx| {
ContextEditor::for_context(
context,
self.fs.clone(),
self.workspace.clone(),
project,
lsp_adapter_delegate,
cx,
)
}));
if let Some(context_editor) = self.context_editor.as_ref() {
context_editor.focus_handle(cx).focus(cx);
}
}
fn open_history(&mut self, cx: &mut ViewContext<Self>) { fn open_history(&mut self, cx: &mut ViewContext<Self>) {
self.active_view = ActiveView::History; self.active_view = ActiveView::History;
self.history.focus_handle(cx).focus(cx); self.history.focus_handle(cx).focus(cx);
@ -227,6 +285,7 @@ impl FocusableView for AssistantPanel {
fn focus_handle(&self, cx: &AppContext) -> FocusHandle { fn focus_handle(&self, cx: &AppContext) -> FocusHandle {
match self.active_view { match self.active_view {
ActiveView::Thread => self.message_editor.focus_handle(cx), ActiveView::Thread => self.message_editor.focus_handle(cx),
ActiveView::PromptEditor => self.context_editor.as_ref().unwrap().focus_handle(cx),
ActiveView::History => self.history.focus_handle(cx), ActiveView::History => self.history.focus_handle(cx),
} }
} }
@ -314,12 +373,24 @@ impl AssistantPanel {
let thread = self.thread.read(cx); let thread = self.thread.read(cx);
let title = if thread.is_empty() { let title = match self.active_view {
thread.summary_or_default(cx) ActiveView::Thread => {
} else { if thread.is_empty() {
thread thread.summary_or_default(cx)
.summary(cx) } else {
.unwrap_or_else(|| SharedString::from("Loading Summary…")) thread
.summary(cx)
.unwrap_or_else(|| SharedString::from("Loading Summary…"))
}
}
ActiveView::PromptEditor => self
.context_editor
.as_ref()
.map(|context_editor| {
SharedString::from(context_editor.read(cx).title(cx).to_string())
})
.unwrap_or_else(|| SharedString::from("Loading Summary…")),
ActiveView::History => "History".into(),
}; };
h_flex() h_flex()
@ -341,22 +412,20 @@ impl AssistantPanel {
.border_color(cx.theme().colors().border) .border_color(cx.theme().colors().border)
.gap(DynamicSpacing::Base02.rems(cx)) .gap(DynamicSpacing::Base02.rems(cx))
.child( .child(
IconButton::new("new-thread", IconName::Plus) PopoverMenu::new("assistant-toolbar-popover-menu")
.icon_size(IconSize::Small) .trigger(
.style(ButtonStyle::Subtle) IconButton::new("new", IconName::Plus)
.tooltip({ .icon_size(IconSize::Small)
let focus_handle = focus_handle.clone(); .style(ButtonStyle::Subtle)
move |cx| { .tooltip(|cx| Tooltip::text("New…", cx)),
Tooltip::for_action_in( )
"New Thread", .anchor(Corner::TopRight)
&NewThread, .with_handle(self.new_item_context_menu_handle.clone())
&focus_handle, .menu(move |cx| {
cx, Some(ContextMenu::build(cx, |menu, _| {
) menu.action("New Thread", NewThread.boxed_clone())
} .action("New Prompt Editor", NewPromptEditor.boxed_clone())
}) }))
.on_click(move |_event, cx| {
cx.dispatch_action(NewThread.boxed_clone());
}), }),
) )
.child( .child(
@ -635,6 +704,7 @@ impl Render for AssistantPanel {
.child(self.message_editor.clone()), .child(self.message_editor.clone()),
) )
.children(self.render_last_error(cx)), .children(self.render_last_error(cx)),
ActiveView::PromptEditor => parent.children(self.context_editor.clone()),
ActiveView::History => parent.child(self.history.clone()), ActiveView::History => parent.child(self.history.clone()),
}) })
} }

View file

@ -2304,7 +2304,10 @@ impl Context {
let mut request = self.to_completion_request(request_type, cx); let mut request = self.to_completion_request(request_type, cx);
if cx.has_flag::<ToolUseFeatureFlag>() { // Don't attach tools for now; we'll be removing tool use from
// Assistant1 shortly.
#[allow(clippy::overly_complex_bool_expr)]
if false && cx.has_flag::<ToolUseFeatureFlag>() {
request.tools = self request.tools = self
.tools .tools
.tools(cx) .tools(cx)

View file

@ -35,6 +35,7 @@ use language_model::{LanguageModelImage, LanguageModelRegistry, LanguageModelToo
use language_model_selector::{LanguageModelSelector, LanguageModelSelectorPopoverMenu}; use language_model_selector::{LanguageModelSelector, LanguageModelSelectorPopoverMenu};
use multi_buffer::MultiBufferRow; use multi_buffer::MultiBufferRow;
use picker::Picker; use picker::Picker;
use project::lsp_store::LocalLspAdapterDelegate;
use project::{Project, Worktree}; use project::{Project, Worktree};
use rope::Point; use rope::Point;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -3534,6 +3535,30 @@ pub fn humanize_token_count(count: usize) -> String {
} }
} }
pub fn make_lsp_adapter_delegate(
project: &Model<Project>,
cx: &mut AppContext,
) -> Result<Option<Arc<dyn LspAdapterDelegate>>> {
project.update(cx, |project, cx| {
// TODO: Find the right worktree.
let Some(worktree) = project.worktrees(cx).next() else {
return Ok(None::<Arc<dyn LspAdapterDelegate>>);
};
let http_client = project.client().http_client().clone();
project.lsp_store().update(cx, |_, cx| {
Ok(Some(LocalLspAdapterDelegate::new(
project.languages().clone(),
project.environment(),
cx.weak_model(),
&worktree,
http_client,
project.fs().clone(),
cx,
) as Arc<dyn LspAdapterDelegate>))
})
})
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;

View file

@ -362,8 +362,11 @@ fn initialize_panels(prompt_builder: Arc<PromptBuilder>, cx: &mut ViewContext<Wo
workspace_handle.clone(), workspace_handle.clone(),
cx.clone(), cx.clone(),
); );
let assistant_panel = let assistant_panel = assistant::AssistantPanel::load(
assistant::AssistantPanel::load(workspace_handle.clone(), prompt_builder, cx.clone()); workspace_handle.clone(),
prompt_builder.clone(),
cx.clone(),
);
let ( let (
project_panel, project_panel,
@ -427,7 +430,14 @@ fn initialize_panels(prompt_builder: Arc<PromptBuilder>, cx: &mut ViewContext<Wo
} }
}; };
let assistant2_panel = if is_assistant2_enabled { let assistant2_panel = if is_assistant2_enabled {
Some(assistant2::AssistantPanel::load(workspace_handle.clone(), cx.clone()).await?) Some(
assistant2::AssistantPanel::load(
workspace_handle.clone(),
prompt_builder,
cx.clone(),
)
.await?,
)
} else { } else {
None None
}; };