Initial public release of Warp.
Repo-Sync-Origin: warpdotdev/warp-internal@12af1d983b
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
//! Data source for the user query menu.
|
||||
|
||||
use itertools::Itertools;
|
||||
use ordered_float::OrderedFloat;
|
||||
use warpui::{AppContext, Entity, SingletonEntity};
|
||||
|
||||
use crate::ai::agent::conversation::AIConversationId;
|
||||
use crate::ai::agent::AIAgentExchangeId;
|
||||
use crate::ai::blocklist::BlocklistAIHistoryModel;
|
||||
use crate::search::data_source::{Query, QueryResult};
|
||||
use crate::search::mixer::DataSourceRunErrorWrapper;
|
||||
use crate::search::SyncDataSource;
|
||||
use crate::terminal::input::user_query::search_item::UserQuerySearchItem;
|
||||
|
||||
/// Action emitted when a query is selected in the user query menu.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct SelectUserQuery {
|
||||
pub exchange_id: AIAgentExchangeId,
|
||||
}
|
||||
|
||||
pub struct UserQueryDataSource {
|
||||
conversation_id: AIConversationId,
|
||||
}
|
||||
|
||||
impl UserQueryDataSource {
|
||||
pub fn new(conversation_id: AIConversationId) -> Self {
|
||||
Self { conversation_id }
|
||||
}
|
||||
|
||||
pub fn set_conversation_id(&mut self, conversation_id: AIConversationId) {
|
||||
self.conversation_id = conversation_id;
|
||||
}
|
||||
}
|
||||
|
||||
impl SyncDataSource for UserQueryDataSource {
|
||||
type Action = SelectUserQuery;
|
||||
|
||||
fn run_query(
|
||||
&self,
|
||||
query: &Query,
|
||||
app: &AppContext,
|
||||
) -> Result<Vec<QueryResult<Self::Action>>, DataSourceRunErrorWrapper> {
|
||||
let history_model = BlocklistAIHistoryModel::as_ref(app);
|
||||
let Some(conversation) = history_model.conversation(&self.conversation_id) else {
|
||||
return Ok(vec![]);
|
||||
};
|
||||
|
||||
let search_query = query.text.trim().to_lowercase();
|
||||
if search_query.is_empty() {
|
||||
// With no search, we just return all queries in chronological order (oldest first).
|
||||
let results: Vec<QueryResult<Self::Action>> = conversation
|
||||
.root_task_exchanges()
|
||||
.filter(|exchange| exchange.has_user_query())
|
||||
.map(|exchange| {
|
||||
let query_text = exchange.format_input_for_copy();
|
||||
QueryResult::from(UserQuerySearchItem::new(exchange.id, query_text))
|
||||
})
|
||||
.collect();
|
||||
Ok(results)
|
||||
} else {
|
||||
// Filter by fuzzy matching and sort by score.
|
||||
let results = conversation
|
||||
.root_task_exchanges()
|
||||
.filter(|exchange| exchange.has_user_query())
|
||||
.filter_map(|exchange| {
|
||||
let query_text = exchange.format_input_for_copy();
|
||||
let match_result =
|
||||
fuzzy_match::match_indices_case_insensitive(&query_text, &search_query)?;
|
||||
|
||||
Some(QueryResult::from(
|
||||
UserQuerySearchItem::new(exchange.id, query_text)
|
||||
.with_query_match_result(Some(match_result.clone()))
|
||||
.with_score(OrderedFloat(match_result.score as f64)),
|
||||
))
|
||||
})
|
||||
.sorted_by(|a, b| b.score().cmp(&a.score()))
|
||||
.collect();
|
||||
Ok(results)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Entity for UserQueryDataSource {
|
||||
type Event = ();
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
//! Inline menu for selecting user queries from a conversation.
|
||||
//! Used by the `/fork-from` slash command to let users select which query to fork from.
|
||||
|
||||
mod data_source;
|
||||
mod search_item;
|
||||
mod view;
|
||||
|
||||
pub use data_source::SelectUserQuery;
|
||||
pub use view::{UserQueryMenuEvent, UserQueryMenuView};
|
||||
|
||||
use warpui::keymap::Keystroke;
|
||||
use warpui::platform::OperatingSystem;
|
||||
|
||||
use crate::terminal::input::inline_menu::{
|
||||
default_navigation_message_items, InlineMenuAction, InlineMenuMessageArgs, InlineMenuRowAction,
|
||||
InlineMenuType,
|
||||
};
|
||||
use crate::terminal::input::message_bar::{Message, MessageItem};
|
||||
|
||||
impl InlineMenuAction for SelectUserQuery {
|
||||
const MENU_TYPE: InlineMenuType = InlineMenuType::UserQueryMenu;
|
||||
|
||||
fn produce_inline_menu_message<T>(args: InlineMenuMessageArgs<'_, Self, T>) -> Option<Message> {
|
||||
let InlineMenuMessageArgs {
|
||||
inline_menu_model, ..
|
||||
} = args;
|
||||
|
||||
let mut items = Vec::new();
|
||||
|
||||
if let Some(item) = inline_menu_model.selected_item() {
|
||||
let exchange_id = item.exchange_id;
|
||||
items.push(MessageItem::clickable(
|
||||
vec![
|
||||
MessageItem::keystroke(Keystroke {
|
||||
key: "enter".to_owned(),
|
||||
..Default::default()
|
||||
}),
|
||||
MessageItem::text(" current pane"),
|
||||
],
|
||||
move |ctx| {
|
||||
ctx.dispatch_typed_action(InlineMenuRowAction::Accept {
|
||||
item: SelectUserQuery { exchange_id },
|
||||
cmd_or_ctrl_enter: false,
|
||||
});
|
||||
},
|
||||
inline_menu_model.mouse_states().accept.clone(),
|
||||
));
|
||||
|
||||
let modifier_keystroke = if OperatingSystem::get().is_mac() {
|
||||
Keystroke {
|
||||
key: "enter".to_owned(),
|
||||
cmd: true,
|
||||
..Default::default()
|
||||
}
|
||||
} else {
|
||||
Keystroke {
|
||||
key: "enter".to_owned(),
|
||||
ctrl: true,
|
||||
shift: true,
|
||||
..Default::default()
|
||||
}
|
||||
};
|
||||
|
||||
items.push(MessageItem::clickable(
|
||||
vec![
|
||||
MessageItem::keystroke(modifier_keystroke),
|
||||
MessageItem::text(" new pane"),
|
||||
],
|
||||
move |ctx| {
|
||||
ctx.dispatch_typed_action(InlineMenuRowAction::Accept {
|
||||
item: SelectUserQuery { exchange_id },
|
||||
cmd_or_ctrl_enter: true,
|
||||
});
|
||||
},
|
||||
inline_menu_model.mouse_states().accept_secondary.clone(),
|
||||
));
|
||||
}
|
||||
|
||||
items.extend(default_navigation_message_items(&args));
|
||||
Some(Message::new(items))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,137 @@
|
||||
//! SearchItem implementation for user query menu items.
|
||||
|
||||
use fuzzy_match::FuzzyMatchResult;
|
||||
use ordered_float::OrderedFloat;
|
||||
use warp_core::ui::color::coloru_with_opacity;
|
||||
use warp_core::ui::theme::Fill;
|
||||
use warp_core::ui::Icon;
|
||||
use warpui::elements::{ConstrainedBox, Container, Highlight, Shrinkable, Text};
|
||||
use warpui::fonts::{Properties, Weight};
|
||||
use warpui::scene::{CornerRadius, Radius};
|
||||
use warpui::text_layout::ClipConfig;
|
||||
use warpui::{AppContext, Element, SingletonEntity};
|
||||
|
||||
use crate::ai::agent::AIAgentExchangeId;
|
||||
use crate::appearance::Appearance;
|
||||
use crate::search::{ItemHighlightState, SearchItem};
|
||||
use crate::terminal::input::inline_menu::styles::{
|
||||
font_size, icon_color, item_background, menu_background_color, primary_text_color, ICON_MARGIN,
|
||||
ITEM_CORNER_RADIUS, ITEM_HORIZONTAL_PADDING,
|
||||
};
|
||||
use crate::terminal::input::user_query::data_source::SelectUserQuery;
|
||||
|
||||
const ICON_PADDING: f32 = 4.;
|
||||
|
||||
/// Search item for rendering a user query in the user query menu.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct UserQuerySearchItem {
|
||||
exchange_id: AIAgentExchangeId,
|
||||
query_text: String,
|
||||
query_match_result: Option<FuzzyMatchResult>,
|
||||
score: OrderedFloat<f64>,
|
||||
}
|
||||
|
||||
impl UserQuerySearchItem {
|
||||
pub fn new(exchange_id: AIAgentExchangeId, query_text: String) -> Self {
|
||||
Self {
|
||||
exchange_id,
|
||||
query_text,
|
||||
query_match_result: None,
|
||||
score: OrderedFloat(0.0),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn with_query_match_result(mut self, result: Option<FuzzyMatchResult>) -> Self {
|
||||
self.query_match_result = result;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn with_score(mut self, score: OrderedFloat<f64>) -> Self {
|
||||
self.score = score;
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl SearchItem for UserQuerySearchItem {
|
||||
type Action = SelectUserQuery;
|
||||
|
||||
fn render_icon(
|
||||
&self,
|
||||
_highlight_state: ItemHighlightState,
|
||||
appearance: &Appearance,
|
||||
) -> Box<dyn Element> {
|
||||
let color = icon_color(appearance);
|
||||
|
||||
// Once this search item is used by multiple sources,
|
||||
// we'll want to set this icon based on which source is generating this item.
|
||||
let icon = Container::new(
|
||||
ConstrainedBox::new(Icon::ArrowSplit.to_warpui_icon(color).finish())
|
||||
.with_width(appearance.monospace_font_size())
|
||||
.with_height(appearance.monospace_font_size())
|
||||
.finish(),
|
||||
)
|
||||
.with_uniform_padding(ICON_PADDING)
|
||||
.with_background(coloru_with_opacity(color.into(), 10))
|
||||
.with_corner_radius(CornerRadius::with_all(Radius::Pixels(ITEM_CORNER_RADIUS)))
|
||||
.finish();
|
||||
|
||||
Container::new(icon).with_margin_right(ICON_MARGIN).finish()
|
||||
}
|
||||
|
||||
fn render_item(
|
||||
&self,
|
||||
_highlight_state: ItemHighlightState,
|
||||
app: &AppContext,
|
||||
) -> Box<dyn Element> {
|
||||
let appearance = Appearance::as_ref(app);
|
||||
let theme = appearance.theme();
|
||||
let background = menu_background_color(app);
|
||||
|
||||
let mut query_text = Text::new_inline(
|
||||
self.query_text.clone(),
|
||||
appearance.ui_font_family(),
|
||||
font_size(appearance),
|
||||
)
|
||||
.with_color(primary_text_color(theme, background.into()).into())
|
||||
.with_clip(ClipConfig::ellipsis());
|
||||
|
||||
if let Some(match_result) = &self.query_match_result {
|
||||
if !match_result.matched_indices.is_empty() {
|
||||
query_text = query_text.with_single_highlight(
|
||||
Highlight::new().with_properties(Properties::default().weight(Weight::Bold)),
|
||||
match_result.matched_indices.clone(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Container::new(Shrinkable::new(1., query_text.finish()).finish())
|
||||
.with_padding_right(ITEM_HORIZONTAL_PADDING)
|
||||
.finish()
|
||||
}
|
||||
|
||||
fn item_background(
|
||||
&self,
|
||||
highlight_state: ItemHighlightState,
|
||||
appearance: &Appearance,
|
||||
) -> Option<Fill> {
|
||||
item_background(highlight_state, appearance)
|
||||
}
|
||||
|
||||
fn score(&self) -> OrderedFloat<f64> {
|
||||
self.score
|
||||
}
|
||||
|
||||
fn accept_result(&self) -> Self::Action {
|
||||
SelectUserQuery {
|
||||
exchange_id: self.exchange_id,
|
||||
}
|
||||
}
|
||||
|
||||
fn execute_result(&self) -> Self::Action {
|
||||
self.accept_result()
|
||||
}
|
||||
|
||||
fn accessibility_label(&self) -> String {
|
||||
format!("Query: {}", self.query_text)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,158 @@
|
||||
//! View for the user query menu.
|
||||
|
||||
use warpui::elements::ChildView;
|
||||
use warpui::{Element, Entity, ModelHandle, View, ViewContext, ViewHandle};
|
||||
|
||||
use crate::ai::agent::conversation::AIConversationId;
|
||||
use crate::ai::agent::AIAgentExchangeId;
|
||||
use crate::ai::blocklist::agent_view::AgentViewController;
|
||||
use crate::search::data_source::Query;
|
||||
use crate::search::mixer::SearchMixer;
|
||||
use crate::terminal::input::buffer_model::{InputBufferModel, InputBufferUpdateEvent};
|
||||
use crate::terminal::input::inline_menu::{InlineMenuEvent, InlineMenuPositioner, InlineMenuView};
|
||||
use crate::terminal::input::suggestions_mode_model::{
|
||||
InputSuggestionsModeEvent, InputSuggestionsModeModel,
|
||||
};
|
||||
use crate::terminal::input::user_query::data_source::{SelectUserQuery, UserQueryDataSource};
|
||||
|
||||
/// Events emitted by UserQueryMenuView.
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum UserQueryMenuEvent {
|
||||
/// User navigated to a query (arrow keys).
|
||||
SelectedQuery { exchange_id: AIAgentExchangeId },
|
||||
/// User accepted a query (hit enter).
|
||||
AcceptedQuery {
|
||||
exchange_id: AIAgentExchangeId,
|
||||
cmd_enter: bool,
|
||||
},
|
||||
/// User dismissed the menu (escape or click).
|
||||
Dismissed,
|
||||
}
|
||||
|
||||
pub struct UserQueryMenuView {
|
||||
menu_view: ViewHandle<InlineMenuView<SelectUserQuery>>,
|
||||
data_source: ModelHandle<UserQueryDataSource>,
|
||||
mixer: ModelHandle<SearchMixer<SelectUserQuery>>,
|
||||
input_suggestions_model: ModelHandle<InputSuggestionsModeModel>,
|
||||
}
|
||||
|
||||
impl UserQueryMenuView {
|
||||
pub fn new(
|
||||
conversation_id: AIConversationId,
|
||||
input_suggestions_model: ModelHandle<InputSuggestionsModeModel>,
|
||||
agent_view_controller: ModelHandle<AgentViewController>,
|
||||
positioner: &ModelHandle<InlineMenuPositioner>,
|
||||
input_buffer_model: &ModelHandle<InputBufferModel>,
|
||||
ctx: &mut ViewContext<Self>,
|
||||
) -> Self {
|
||||
let data_source = ctx.add_model(|_| UserQueryDataSource::new(conversation_id));
|
||||
|
||||
let mixer = ctx.add_model(|ctx| {
|
||||
let mut mixer = SearchMixer::<SelectUserQuery>::new();
|
||||
mixer.add_sync_source(data_source.clone(), []);
|
||||
mixer.run_query(Query::default(), ctx);
|
||||
mixer
|
||||
});
|
||||
|
||||
let menu_view = ctx.add_typed_action_view(|ctx| {
|
||||
InlineMenuView::new(
|
||||
mixer.clone(),
|
||||
positioner.clone(),
|
||||
&input_suggestions_model,
|
||||
agent_view_controller,
|
||||
ctx,
|
||||
)
|
||||
});
|
||||
|
||||
ctx.subscribe_to_view(&menu_view, |_, _, event, ctx| match event {
|
||||
InlineMenuEvent::AcceptedItem {
|
||||
item,
|
||||
cmd_or_ctrl_shift_enter,
|
||||
} => {
|
||||
ctx.emit(UserQueryMenuEvent::AcceptedQuery {
|
||||
exchange_id: item.exchange_id,
|
||||
cmd_enter: *cmd_or_ctrl_shift_enter,
|
||||
});
|
||||
}
|
||||
InlineMenuEvent::SelectedItem { item } => {
|
||||
ctx.emit(UserQueryMenuEvent::SelectedQuery {
|
||||
exchange_id: item.exchange_id,
|
||||
});
|
||||
}
|
||||
InlineMenuEvent::Dismissed => {
|
||||
ctx.emit(UserQueryMenuEvent::Dismissed);
|
||||
}
|
||||
InlineMenuEvent::NoResults | InlineMenuEvent::TabChanged => {}
|
||||
});
|
||||
|
||||
ctx.subscribe_to_model(
|
||||
&input_suggestions_model,
|
||||
|me, input_suggestions_model, event, ctx| {
|
||||
let InputSuggestionsModeEvent::ModeChanged { .. } = event;
|
||||
if let Some(conversation_id) = input_suggestions_model
|
||||
.as_ref(ctx)
|
||||
.user_query_conversation_id()
|
||||
{
|
||||
me.data_source.update(ctx, |ds, _| {
|
||||
ds.set_conversation_id(conversation_id);
|
||||
});
|
||||
me.refresh_results("", ctx);
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
ctx.subscribe_to_model(input_buffer_model, |me, _, event, ctx| {
|
||||
if me.input_suggestions_model.as_ref(ctx).is_user_query_menu() {
|
||||
let InputBufferUpdateEvent { new_content, .. } = event;
|
||||
// The buffer is cleared when the menu opens, so the entire content is the search query
|
||||
me.refresh_results(new_content, ctx);
|
||||
}
|
||||
});
|
||||
|
||||
Self {
|
||||
menu_view,
|
||||
data_source,
|
||||
mixer,
|
||||
input_suggestions_model,
|
||||
}
|
||||
}
|
||||
|
||||
fn refresh_results(&self, search_query: &str, ctx: &mut ViewContext<Self>) {
|
||||
self.mixer.update(ctx, |mixer, ctx| {
|
||||
mixer.run_query(
|
||||
Query {
|
||||
text: search_query.to_owned(),
|
||||
..Default::default()
|
||||
},
|
||||
ctx,
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
pub fn select_up(&self, ctx: &mut ViewContext<Self>) {
|
||||
self.menu_view.update(ctx, |v, ctx| v.select_up(ctx));
|
||||
}
|
||||
|
||||
pub fn select_down(&self, ctx: &mut ViewContext<Self>) {
|
||||
self.menu_view.update(ctx, |v, ctx| v.select_down(ctx));
|
||||
}
|
||||
|
||||
pub fn accept_selected_item(&self, cmd_enter: bool, ctx: &mut ViewContext<Self>) {
|
||||
self.menu_view
|
||||
.update(ctx, |v, ctx| v.accept_selected_item(cmd_enter, ctx));
|
||||
}
|
||||
}
|
||||
|
||||
impl View for UserQueryMenuView {
|
||||
fn ui_name() -> &'static str {
|
||||
"UserQueryMenuView"
|
||||
}
|
||||
|
||||
fn render(&self, _app: &warpui::AppContext) -> Box<dyn Element> {
|
||||
ChildView::new(&self.menu_view).finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl Entity for UserQueryMenuView {
|
||||
type Event = UserQueryMenuEvent;
|
||||
}
|
||||
Reference in New Issue
Block a user