Skip to content

Commit

Permalink
assistant panel: Stop animation & show explicit state if canceled (ze…
Browse files Browse the repository at this point in the history
…d-industries#16200)

This fixes a bug by stopping the animation when a completion is canceled
and it also makes the state more explicit, which I think is very
valuable.



https://github.com/user-attachments/assets/9ede9b25-86ac-4901-8434-7407896bb799


Release Notes:

- N/A
  • Loading branch information
mrnugget authored Aug 14, 2024
1 parent 97469cd commit 8b8335f
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 31 deletions.
7 changes: 7 additions & 0 deletions crates/assistant/src/assistant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ pub enum MessageStatus {
Pending,
Done,
Error(SharedString),
Canceled,
}

impl MessageStatus {
Expand All @@ -112,6 +113,7 @@ impl MessageStatus {
Some(proto::context_message_status::Variant::Error(error)) => {
MessageStatus::Error(error.message.into())
}
Some(proto::context_message_status::Variant::Canceled(_)) => MessageStatus::Canceled,
None => MessageStatus::Pending,
}
}
Expand All @@ -135,6 +137,11 @@ impl MessageStatus {
},
)),
},
MessageStatus::Canceled => proto::ContextMessageStatus {
variant: Some(proto::context_message_status::Variant::Canceled(
proto::context_message_status::Canceled {},
)),
},
}
}
}
Expand Down
83 changes: 54 additions & 29 deletions crates/assistant/src/assistant_panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1975,7 +1975,7 @@ impl ContextEditor {
fn cancel(&mut self, _: &editor::actions::Cancel, cx: &mut ViewContext<Self>) {
if self
.context
.update(cx, |context, _| context.cancel_last_assist())
.update(cx, |context, cx| context.cancel_last_assist(cx))
{
return;
}
Expand Down Expand Up @@ -3042,22 +3042,6 @@ impl ContextEditor {
}
});

let trigger = Button::new("show-error", "Error")
.color(Color::Error)
.selected_label_color(Color::Error)
.selected_icon_color(Color::Error)
.icon(IconName::XCircle)
.icon_color(Color::Error)
.icon_size(IconSize::Small)
.icon_position(IconPosition::Start)
.tooltip(move |cx| {
Tooltip::with_meta(
"Error interacting with language model",
None,
"Click for more details",
cx,
)
});
h_flex()
.id(("message_header", message_id.as_u64()))
.pl(cx.gutter_dimensions.full_width())
Expand All @@ -3066,22 +3050,63 @@ impl ContextEditor {
.relative()
.gap_1()
.child(sender)
.children(
if let MessageStatus::Error(error) = message.status.clone() {
.children(match &message.status {
MessageStatus::Error(error) => {
let error_popover_trigger =
Button::new("show-error", "Error")
.color(Color::Error)
.selected_label_color(Color::Error)
.selected_icon_color(Color::Error)
.icon(IconName::XCircle)
.icon_color(Color::Error)
.icon_size(IconSize::Small)
.icon_position(IconPosition::Start)
.tooltip(move |cx| {
Tooltip::with_meta(
"Error interacting with language model",
None,
"Click for more details",
cx,
)
});

Some(
PopoverMenu::new("show-error-popover")
.menu(move |cx| {
Some(cx.new_view(|cx| ErrorPopover {
error: error.clone(),
focus_handle: cx.focus_handle(),
}))
.menu({
let error = error.clone();
move |cx| {
Some(cx.new_view(|cx| ErrorPopover {
error: error.clone(),
focus_handle: cx.focus_handle(),
}))
}
})
.trigger(trigger),
.trigger(error_popover_trigger)
.into_any_element(),
)
} else {
None
},
)
}
MessageStatus::Canceled => Some(
ButtonLike::new("canceled")
.child(
Icon::new(IconName::XCircle).color(Color::Disabled),
)
.child(
Label::new("Canceled")
.size(LabelSize::Small)
.color(Color::Disabled),
)
.tooltip(move |cx| {
Tooltip::with_meta(
"Canceled",
None,
"Interaction with the assistant was canceled",
cx,
)
})
.into_any_element(),
),
_ => None,
})
.into_any_element()
}
}),
Expand Down
13 changes: 11 additions & 2 deletions crates/assistant/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,7 @@ impl PartialEq for ImageAnchor {

struct PendingCompletion {
id: usize,
assistant_message_id: MessageId,
_task: Task<()>,
}

Expand Down Expand Up @@ -1806,6 +1807,7 @@ impl Context {

self.pending_completions.push(PendingCompletion {
id: post_inc(&mut self.completion_count),
assistant_message_id: assistant_message.id,
_task: task,
});

Expand All @@ -1827,8 +1829,15 @@ impl Context {
}
}

pub fn cancel_last_assist(&mut self) -> bool {
self.pending_completions.pop().is_some()
pub fn cancel_last_assist(&mut self, cx: &mut ModelContext<Self>) -> bool {
if let Some(pending_completion) = self.pending_completions.pop() {
self.update_metadata(pending_completion.assistant_message_id, cx, |metadata| {
metadata.status = MessageStatus::Canceled;
});
true
} else {
false
}
}

pub fn cycle_message_roles(&mut self, ids: HashSet<MessageId>, cx: &mut ModelContext<Self>) {
Expand Down
3 changes: 3 additions & 0 deletions crates/proto/proto/zed.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2310,6 +2310,7 @@ message ContextMessageStatus {
Done done = 1;
Pending pending = 2;
Error error = 3;
Canceled canceled = 4;
}

message Done {}
Expand All @@ -2319,6 +2320,8 @@ message ContextMessageStatus {
message Error {
string message = 1;
}

message Canceled {}
}

message ContextMessage {
Expand Down

0 comments on commit 8b8335f

Please sign in to comment.