use super::actions::{ComposerPrimaryActions, ComposerState};
use super::attachments::{AttachmentFile, AttachmentImage, ComposerAttachmentStrip};
use super::chip::ContextChip;
use super::controls::{ComposerControl, EffortGlyph, EffortLevel};
use super::voice::VoiceInputPill;
use crate::components::button::{Button, ButtonSize, ButtonVariant};
use crate::components::kbd::Kbd;
use crate::components::textarea;
use crate::t3::press::{Handler, rem};
use crate::t3::shell::{IdentitySize, ProviderIcon};
use crate::{icons::CarbonIcon, theme::color, tokens::ColorRole};
use gpui_kit::component::Sizable as _;
use gpui_kit::component::input::{
Enter, IndentInline, OutdentInline, Textarea as KitTextarea, TextareaState,
};
use gpui_kit::{
AnyElement, App, AppContext as _, ElementId, Entity, InteractiveElement as _, IntoElement,
MouseButton, ParentElement, RenderOnce, SharedString, Styled, Window, div,
prelude::FluentBuilder as _,
};
use std::rc::Rc;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum ComposerMenu {
Model,
Effort,
Permissions,
}
type TextHandler = Rc<dyn Fn(SharedString, &mut Window, &mut App)>;
#[derive(Clone)]
pub struct ComposerTexts {
pub placeholder: SharedString,
pub message: SharedString,
pub attach: SharedString,
pub dictate: SharedString,
pub model: SharedString,
pub effort: SharedString,
pub permissions: SharedString,
pub accept: SharedString,
}
impl Default for ComposerTexts {
fn default() -> Self {
Self {
placeholder: "Ask anything, @ to mention, / for commands".into(),
message: "Message".into(),
attach: "Attach files".into(),
dictate: "Dictate (⌥Space)".into(),
model: "Model".into(),
effort: "Reasoning effort".into(),
permissions: "Permissions".into(),
accept: "to accept".into(),
}
}
}
pub fn enter_action(shift: bool, command: bool, running: bool, can_steer: bool) -> Option<bool> {
if shift {
None
} else if running && command && can_steer {
Some(true)
} else {
Some(false)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum TabAction {
Accept,
FocusNext,
FocusPrev,
}
pub fn tab_action(forward: bool, suggestion_showing: bool) -> TabAction {
match (forward, suggestion_showing) {
(true, true) => TabAction::Accept,
(true, false) => TabAction::FocusNext,
(false, _) => TabAction::FocusPrev,
}
}
#[derive(IntoElement)]
pub struct Composer {
id: ElementId,
value: SharedString,
input: Option<Entity<TextareaState>>,
contexts: Vec<ContextChip>,
attachments: Option<(Vec<AttachmentImage>, Vec<AttachmentFile>)>,
model: SharedString,
provider: SharedString,
effort: EffortLevel,
fast: bool,
permissions: SharedString,
state: ComposerState,
voice: Option<VoiceInputPill>,
banners: Option<AnyElement>,
meter: Option<AnyElement>,
open_control: Option<ComposerMenu>,
send_disabled: bool,
suggestion: Option<SharedString>,
send_label: Option<SharedString>,
texts: ComposerTexts,
on_send: Option<TextHandler>,
on_queue: Option<TextHandler>,
on_steer: Option<TextHandler>,
on_stop: Option<Handler>,
on_attach: Option<Handler>,
on_model_click: Option<Handler>,
on_effort_click: Option<Handler>,
on_permissions_click: Option<Handler>,
on_remove_attachment: Option<TextHandler>,
}
impl Composer {
pub fn new(id: impl Into<ElementId>) -> Self {
Self {
id: id.into(),
value: SharedString::default(),
input: None,
contexts: Vec::new(),
attachments: None,
model: "Model".into(),
provider: "Agent".into(),
effort: EffortLevel::Medium,
fast: false,
permissions: "Ask before edits".into(),
state: ComposerState::Idle,
voice: None,
banners: None,
meter: None,
open_control: None,
send_disabled: false,
suggestion: None,
send_label: None,
texts: ComposerTexts::default(),
on_send: None,
on_queue: None,
on_steer: None,
on_stop: None,
on_attach: None,
on_model_click: None,
on_effort_click: None,
on_permissions_click: None,
on_remove_attachment: None,
}
}
pub fn value(mut self, value: impl Into<SharedString>) -> Self {
self.value = value.into();
self
}
pub fn input(mut self, state: Entity<TextareaState>) -> Self {
self.input = Some(state);
self
}
pub fn contexts(mut self, contexts: Vec<ContextChip>) -> Self {
self.contexts = contexts;
self
}
pub fn attachments(mut self, images: Vec<AttachmentImage>, files: Vec<AttachmentFile>) -> Self {
self.attachments = Some((images, files));
self
}
pub fn model(
mut self,
model: impl Into<SharedString>,
provider: impl Into<SharedString>,
) -> Self {
self.model = model.into();
self.provider = provider.into();
self
}
pub fn effort(mut self, effort: EffortLevel, fast: bool) -> Self {
self.effort = effort;
self.fast = fast;
self
}
pub fn permissions(mut self, permissions: impl Into<SharedString>) -> Self {
self.permissions = permissions.into();
self
}
pub fn state(mut self, state: ComposerState) -> Self {
self.state = state;
self
}
pub fn voice(mut self, voice: VoiceInputPill) -> Self {
self.voice = Some(voice);
self
}
pub fn banners(mut self, banners: impl IntoElement) -> Self {
self.banners = Some(banners.into_any_element());
self
}
pub fn meter(mut self, meter: impl IntoElement) -> Self {
self.meter = Some(meter.into_any_element());
self
}
pub fn open_control(mut self, control: Option<ComposerMenu>) -> Self {
self.open_control = control;
self
}
pub fn send_disabled(mut self, disabled: bool) -> Self {
self.send_disabled = disabled;
self
}
pub fn suggestion(mut self, suggestion: impl Into<SharedString>) -> Self {
self.suggestion = Some(suggestion.into());
self
}
pub fn send_label(mut self, label: impl Into<SharedString>) -> Self {
self.send_label = Some(label.into());
self
}
pub fn texts(mut self, texts: ComposerTexts) -> Self {
self.texts = texts;
self
}
pub fn on_send(mut self, f: impl Fn(SharedString, &mut Window, &mut App) + 'static) -> Self {
self.on_send = Some(Rc::new(f));
self
}
pub fn on_queue(mut self, f: impl Fn(SharedString, &mut Window, &mut App) + 'static) -> Self {
self.on_queue = Some(Rc::new(f));
self
}
pub fn on_steer(mut self, f: impl Fn(SharedString, &mut Window, &mut App) + 'static) -> Self {
self.on_steer = Some(Rc::new(f));
self
}
pub fn on_stop(mut self, f: impl Fn(&mut Window, &mut App) + 'static) -> Self {
self.on_stop = Some(Rc::new(f));
self
}
pub fn on_attach(mut self, f: impl Fn(&mut Window, &mut App) + 'static) -> Self {
self.on_attach = Some(Rc::new(f));
self
}
pub fn on_model_click(mut self, f: impl Fn(&mut Window, &mut App) + 'static) -> Self {
self.on_model_click = Some(Rc::new(f));
self
}
pub fn on_effort_click(mut self, f: impl Fn(&mut Window, &mut App) + 'static) -> Self {
self.on_effort_click = Some(Rc::new(f));
self
}
pub fn on_permissions_click(mut self, f: impl Fn(&mut Window, &mut App) + 'static) -> Self {
self.on_permissions_click = Some(Rc::new(f));
self
}
pub fn on_remove_attachment(
mut self,
f: impl Fn(SharedString, &mut Window, &mut App) + 'static,
) -> Self {
self.on_remove_attachment = Some(Rc::new(f));
self
}
}
fn submit(
state: &Entity<TextareaState>,
f: Option<TextHandler>,
window: &mut Window,
cx: &mut App,
) {
let text = state.read(cx).value().to_string();
if text.trim().is_empty() {
return;
}
if let Some(f) = f {
f(text.into(), window, cx);
}
state.update(cx, |s, cx| s.set_value("", window, cx));
}
impl RenderOnce for Composer {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let id = self.id.clone();
let seed = self.value.clone();
let input = match self.input.clone() {
Some(input) => input,
None => window
.use_keyed_state((id.clone(), "input"), cx, move |window, cx| {
cx.new(|cx| TextareaState::new(window, cx).default_value(seed))
})
.read(cx)
.clone(),
};
let text = input.read(cx).value().to_string();
let empty = text.trim().is_empty();
let within = window
.use_keyed_state((id.clone(), "within"), cx, |_, cx| cx.focus_handle())
.read(cx)
.clone();
let focused = within.contains_focused(window, cx);
let texts = self.texts.clone();
let running = self.state == ComposerState::Running;
let has_ctx = !self.contexts.is_empty() || self.attachments.is_some();
let show_suggestion = self.suggestion.is_some() && text.is_empty();
let send = {
let input = input.clone();
let f = self.on_send.clone();
Rc::new(move |w: &mut Window, cx: &mut App| submit(&input, f.clone(), w, cx)) as Handler
};
let queue = {
let input = input.clone();
let f = self.on_queue.clone();
Rc::new(move |w: &mut Window, cx: &mut App| submit(&input, f.clone(), w, cx)) as Handler
};
let steer = self.on_steer.clone().map(|f| {
let input = input.clone();
Rc::new(move |w: &mut Window, cx: &mut App| submit(&input, Some(f.clone()), w, cx))
as Handler
});
let on_enter = {
let (send, queue, steer) = (send.clone(), queue.clone(), steer.clone());
move |action: &Enter, window: &mut Window, cx: &mut App| {
let Some(steering) =
enter_action(action.shift, action.secondary, running, steer.is_some())
else {
return;
};
cx.stop_propagation();
if steering {
if let Some(f) = steer.as_ref() {
f(window, cx)
}
} else if running {
queue(window, cx)
} else {
send(window, cx)
}
}
};
let on_tab = {
let suggestion = self.suggestion.clone();
let input = input.clone();
move |forward: bool, window: &mut Window, cx: &mut App| {
cx.stop_propagation();
let showing = suggestion.is_some() && input.read(cx).value().is_empty();
match tab_action(forward, showing) {
TabAction::Accept => {
if let Some(s) = suggestion.clone() {
input.update(cx, |state, cx| state.insert(s, window, cx));
}
}
TabAction::FocusNext => textarea::move_focus(true, window, cx),
TabAction::FocusPrev => textarea::move_focus(false, window, cx),
}
}
};
let ctx_row = has_ctx.then(|| {
div()
.flex()
.flex_row()
.flex_wrap()
.items_center()
.gap(rem(8.))
.pt(rem(16.))
.px(rem(16.))
.children(self.contexts.into_iter().map(|c| c.in_composer(true)))
.when_some(self.attachments.clone(), |this, (images, files)| {
let remove = self.on_remove_attachment.clone();
this.child(
ComposerAttachmentStrip::new((id.clone(), "attachments"), images, files)
.in_composer(true)
.when_some(remove, |s, f| {
s.on_remove(move |name, w, cx| f(name, w, cx))
}),
)
})
});
let pad_top = if has_ctx { 12. } else { 16. };
let editor = div()
.relative()
.min_h(rem(72.))
.pt(rem(pad_top - 2.))
.pb(rem(8. - 2.))
.px(rem(16. - 4.))
.text_size(rem(14.))
.line_height(rem(20.))
.text_color(color(ColorRole::TextPrimary))
.when(text.is_empty() && !show_suggestion, |this| {
this.child(
div()
.absolute()
.top(rem(pad_top))
.left(rem(16.))
.right(rem(16.))
.text_color(color(ColorRole::TextHelper))
.child(texts.placeholder.clone()),
)
})
.child(
KitTextarea::new(&input)
.appearance(false)
.with_size(gpui_kit::component::Size::XSmall)
.aria_label(texts.message.clone())
.h(rem(72. - pad_top - 8. + 4.))
.w_full()
.text_size(rem(14.))
.line_height(rem(20.)),
);
let suggestion = show_suggestion.then(|| {
div()
.absolute()
.top(rem(pad_top))
.left(rem(16.))
.right(rem(16.))
.flex()
.flex_row()
.items_center()
.gap(rem(8.))
.text_size(rem(14.))
.line_height(rem(20.))
.child(
div()
.min_w_0()
.overflow_hidden()
.whitespace_nowrap()
.text_ellipsis()
.italic()
.text_color(color(ColorRole::TextSecondary))
.children(self.suggestion.clone()),
)
.child(
div()
.flex()
.flex_row()
.flex_none()
.items_center()
.gap(rem(4.))
.text_size(rem(12.))
.text_color(color(ColorRole::TextHelper))
.child(Kbd::new("Tab"))
.child(texts.accept.clone()),
)
});
let control =
|key: &'static str, menu: ComposerMenu, label: SharedString, f: Option<Handler>| {
ComposerControl::new((id.clone(), key), label)
.active(self.open_control == Some(menu))
.when_some(f, |c, f| c.on_click(move |w, cx| f(w, cx)))
};
let attach = self.on_attach.clone();
let left = div()
.flex()
.flex_row()
.items_center()
.gap(rem(2.))
.min_w_0()
.overflow_hidden()
.child(
Button::new((id.clone(), "attach"), texts.attach.clone())
.icon(CarbonIcon::Add)
.icon_only(true)
.variant(ButtonVariant::Ghost)
.size(ButtonSize::Sm)
.when_some(attach, |b, f| b.on_click(move |_, w, cx| f(w, cx))),
)
.child(
control(
"model",
ComposerMenu::Model,
texts.model.clone(),
self.on_model_click.clone(),
)
.child(ProviderIcon::new(self.provider.clone()).size(IdentitySize::Xs))
.child(self.model.clone()),
)
.child(
control(
"effort",
ComposerMenu::Effort,
texts.effort.clone(),
self.on_effort_click.clone(),
)
.child(EffortGlyph::new(self.effort).fast(self.fast)),
)
.child(
control(
"permissions",
ComposerMenu::Permissions,
texts.permissions.clone(),
self.on_permissions_click.clone(),
)
.icon(CarbonIcon::Locked)
.child(self.permissions.clone()),
);
let primary_state = if self.send_label.is_some() {
ComposerState::Answering
} else {
self.state
};
let mut primary = ComposerPrimaryActions::new((id.clone(), "primary"), primary_state)
.disabled(self.send_disabled || (empty && self.state == ComposerState::Idle))
.empty(empty)
.on_send({
let send = send.clone();
move |w, cx| send(w, cx)
})
.on_queue({
let queue = queue.clone();
move |w, cx| queue(w, cx)
});
if let Some(label) = self.send_label.clone() {
primary = primary.answer_label(label);
}
if let Some(f) = self.on_stop.clone() {
primary = primary.on_stop(move |w, cx| f(w, cx));
}
if let Some(f) = steer.clone() {
primary = primary.on_steer(move |w, cx| f(w, cx));
}
let right = div()
.flex()
.flex_row()
.flex_none()
.items_center()
.gap(rem(2.))
.children(self.meter)
.child(match self.voice {
Some(voice) => voice.into_any_element(),
None => Button::new((id.clone(), "mic"), texts.dictate.clone())
.icon(CarbonIcon::Microphone)
.icon_only(true)
.variant(ButtonVariant::Ghost)
.size(ButtonSize::Sm)
.into_any_element(),
})
.child(primary);
let foot = div()
.flex()
.flex_row()
.items_center()
.justify_between()
.gap(rem(8.))
.min_h(rem(40.))
.px(rem(8.))
.pb(rem(8.))
.child(left)
.child(right);
let frame = color(if focused {
ColorRole::Focus
} else {
ColorRole::BorderSubtle01
});
let tab = Rc::new(on_tab);
let content = div()
.id((id.clone(), "box"))
.track_focus(&within)
.relative()
.flex()
.flex_col()
.w_full()
.bg(color(ColorRole::Field01))
.border_1()
.border_color(frame)
.capture_action(on_enter)
.capture_action({
let tab = tab.clone();
move |_: &IndentInline, window, cx| tab(true, window, cx)
})
.capture_action(move |_: &OutdentInline, window, cx| tab(false, window, cx))
.child(
div()
.flex()
.flex_col()
.w_full()
.on_mouse_down(MouseButton::Left, |_, window, _| window.prevent_default())
.children(ctx_row)
.child(div().relative().child(editor).children(suggestion))
.child(foot),
);
div()
.flex()
.flex_col()
.w_full()
.min_w_0()
.gap(rem(8.))
.children(self.banners)
.child(content)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn tab_accepts_only_a_showing_suggestion() {
assert_eq!(tab_action(true, true), TabAction::Accept);
assert_eq!(tab_action(true, false), TabAction::FocusNext);
assert_eq!(tab_action(false, true), TabAction::FocusPrev);
assert_eq!(tab_action(false, false), TabAction::FocusPrev);
}
#[test]
fn enter_sends_queues_or_steers() {
assert_eq!(enter_action(false, false, false, false), Some(false));
assert_eq!(enter_action(true, false, false, false), None);
assert_eq!(enter_action(false, true, true, true), Some(true));
assert_eq!(enter_action(false, true, true, false), Some(false));
}
}