subspace_chat/agent_chat/
status.rs1use std::time::Duration;
6
7use gpui_kit::{
8 AnimationExt as _, AnyElement, Hsla, IntoElement as _, ParentElement as _, Styled as _,
9};
10
11use super::status_words;
12use super::{AgentHandle, Animation, ChatPanel, Tokens, div};
13
14const SHIMMER_DURATION: Duration = Duration::from_millis(1_800);
16const UNIT_MIN: f32 = 0.;
17const UNIT_MAX: f32 = 1.;
18const PEAK_PHASE: f32 = 0.5;
19const LIGHTNESS_BOOST: f32 = 0.04;
20
21fn shimmer_color(accent: Hsla, phase: f32) -> Hsla {
22 let phase = phase.clamp(UNIT_MIN, UNIT_MAX);
23 let brightness = if phase <= PEAK_PHASE {
24 phase / PEAK_PHASE
25 } else {
26 (UNIT_MAX - phase) / PEAK_PHASE
27 };
28 Hsla {
29 l: (accent.l + brightness * LIGHTNESS_BOOST).min(UNIT_MAX),
30 ..accent
31 }
32}
33
34impl ChatPanel {
35 pub(super) fn render_status(&self, tokens: &Tokens) -> Option<AnyElement> {
38 let turn = self.tab().running()?;
39 let phrase = status_words::phrase(turn.phrase);
40 let accent = tokens.accent;
41 Some(
42 div()
43 .w_full()
44 .text_center()
45 .whitespace_normal()
46 .text_color(accent)
47 .child(format!("{} is {phrase}…", self.engine_label()))
48 .with_animation(
49 ("chat-status", turn.phrase as u64),
50 Animation::new(SHIMMER_DURATION).repeat(),
51 move |line, delta| line.text_color(shimmer_color(accent, delta)),
52 )
53 .into_any_element(),
54 )
55 }
56
57 pub fn is_unavailable(&self) -> bool {
60 runtime_unavailable(
61 self.handle.as_ref().map(AgentHandle::is_stopped),
62 self.error.is_some(),
63 )
64 }
65
66 pub(super) fn end_stopped_turns(&mut self) -> bool {
70 let stopped = self.is_unavailable();
71 super::attention::end_turns(&mut self.tabs, stopped)
72 }
73}
74
75fn runtime_unavailable(stopped: Option<bool>, failed: bool) -> bool {
77 stopped.unwrap_or(failed)
78}
79
80#[cfg(test)]
81mod tests {
82 use super::{runtime_unavailable, shimmer_color};
83 use gpui_kit::Hsla;
84
85 #[test]
86 fn only_a_stopped_or_unstartable_runtime_is_unavailable() {
87 assert!(!runtime_unavailable(None, false), "chat not opened yet");
88 assert!(
89 runtime_unavailable(None, true),
90 "the runtime could not start"
91 );
92 assert!(runtime_unavailable(Some(true), false));
93 assert!(
94 !runtime_unavailable(Some(false), true),
95 "a running chat with a passing error is still available"
96 );
97 }
98
99 #[test]
100 fn shimmer_preserves_accent_hue_and_returns_to_base_color() {
101 let accent = Hsla {
102 h: 0.52,
103 s: 0.8,
104 l: 0.5,
105 a: 1.,
106 };
107
108 assert_eq!(shimmer_color(accent, 0.), accent);
109 assert_eq!(shimmer_color(accent, 1.), accent);
110 assert_eq!(shimmer_color(accent, 0.5).h, accent.h);
111 assert!(shimmer_color(accent, 0.5).l > accent.l);
112 }
113}