hyuskui

Context

How much of the context window the conversation has used. A pill names the share; resting on it opens a card with the used and total tokens, a bar, and the split by kind.

Loading GPUI…
use std::time::Duration;

use gpui::{div, prelude::*, px, relative, Context, FontWeight, Window};

use crate::theme::ActiveTheme;
use crate::ui::button::{Button, ButtonSize, ButtonVariant};
use crate::ui::hover_card::{HoverCard, HoverCardVariant};

const INPUT: u32 = 8_420;
const OUTPUT: u32 = 1_260;
const REASONING: u32 = 980;
const LIMIT: u32 = 128_000;

/// shadcn's trigger delays: the card opens almost at once and survives a brief exit.
const OPEN_DELAY: Duration = Duration::from_millis(10);
const CLOSE_DELAY: Duration = Duration::from_millis(100);

#[derive(Default)]
pub struct ContextPattern;

impl Render for ContextPattern {
    fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
        let theme = cx.theme();
        let used = INPUT + OUTPUT + REASONING;
        let percent = (used as f32 / LIMIT as f32 * 100.0).round();

        let trigger = Button::new("context-trigger", format!("{percent:.0}% context"))
            .variant(ButtonVariant::Outline)
            .size(ButtonSize::Compact)
            .rounded_full();

        let row = |label: &'static str, value: u32| {
            div()
                .flex()
                .justify_between()
                .gap_3()
                .child(div().text_color(theme.muted_foreground).child(label))
                .child(div().child(tokens(value)))
        };

        // The bar's track is the muted fill, not a tint of the indicator.
        let progress = div()
            .w_full()
            .h(px(8.0))
            .rounded_full()
            .overflow_hidden()
            .bg(theme.muted)
            .child(div().h_full().w(relative(percent / 100.0)).bg(theme.primary));

        let card = div()
            .flex()
            .flex_col()
            .gap_3()
            .line_height(px(20.0))
            .child(
                div()
                    .flex()
                    .items_center()
                    .justify_between()
                    .gap_3()
                    .text_sm()
                    .child(div().font_weight(FontWeight::MEDIUM).child("Context"))
                    .child(
                        div()
                            .text_color(theme.muted_foreground)
                            .child(format!("{} / {}", tokens(used), tokens(LIMIT))),
                    ),
            )
            .child(progress)
            .child(
                div()
                    .flex()
                    .flex_col()
                    .gap_1()
                    .text_sm()
                    .child(row("Input", INPUT))
                    .child(row("Output", OUTPUT))
                    .child(row("Reasoning", REASONING)),
            );

        div().flex().justify_center().py_12().child(
            HoverCard::new("context")
                .variant(HoverCardVariant::Rhea)
                .width(px(256.0))
                .open_delay(OPEN_DELAY)
                .close_delay(CLOSE_DELAY)
                .trigger(trigger)
                .child(card),
        )
    }
}

/// shadcn's `formatTokens`: 980, 8.4k, 128k.
fn tokens(value: u32) -> String {
    if value < 1_000 {
        return value.to_string();
    }
    let k = format!("{:.1}", value as f32 / 1_000.0);
    format!("{}k", k.strip_suffix(".0").unwrap_or(&k))
}

Installation

  1. Add the components

    $ cargo hyuskui add button hover-card

    Uses Button and Hover Card.

  2. Copy the pattern

    Paste the code into src/ai/context.rs and declare the module. The demo plays scripted data on a timer; drive it from your agent's events instead.

    src/ai/context.rs
    use std::time::Duration;
    
    use gpui::{div, prelude::*, px, relative, Context, FontWeight, Window};
    
    use crate::theme::ActiveTheme;
    use crate::ui::button::{Button, ButtonSize, ButtonVariant};
    use crate::ui::hover_card::{HoverCard, HoverCardVariant};
    
    const INPUT: u32 = 8_420;
    const OUTPUT: u32 = 1_260;
    const REASONING: u32 = 980;
    const LIMIT: u32 = 128_000;
    
    /// shadcn's trigger delays: the card opens almost at once and survives a brief exit.
    const OPEN_DELAY: Duration = Duration::from_millis(10);
    const CLOSE_DELAY: Duration = Duration::from_millis(100);
    
    #[derive(Default)]
    pub struct ContextPattern;
    
    impl Render for ContextPattern {
        fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
            let theme = cx.theme();
            let used = INPUT + OUTPUT + REASONING;
            let percent = (used as f32 / LIMIT as f32 * 100.0).round();
    
            let trigger = Button::new("context-trigger", format!("{percent:.0}% context"))
                .variant(ButtonVariant::Outline)
                .size(ButtonSize::Compact)
                .rounded_full();
    
            let row = |label: &'static str, value: u32| {
                div()
                    .flex()
                    .justify_between()
                    .gap_3()
                    .child(div().text_color(theme.muted_foreground).child(label))
                    .child(div().child(tokens(value)))
            };
    
            // The bar's track is the muted fill, not a tint of the indicator.
            let progress = div()
                .w_full()
                .h(px(8.0))
                .rounded_full()
                .overflow_hidden()
                .bg(theme.muted)
                .child(div().h_full().w(relative(percent / 100.0)).bg(theme.primary));
    
            let card = div()
                .flex()
                .flex_col()
                .gap_3()
                .line_height(px(20.0))
                .child(
                    div()
                        .flex()
                        .items_center()
                        .justify_between()
                        .gap_3()
                        .text_sm()
                        .child(div().font_weight(FontWeight::MEDIUM).child("Context"))
                        .child(
                            div()
                                .text_color(theme.muted_foreground)
                                .child(format!("{} / {}", tokens(used), tokens(LIMIT))),
                        ),
                )
                .child(progress)
                .child(
                    div()
                        .flex()
                        .flex_col()
                        .gap_1()
                        .text_sm()
                        .child(row("Input", INPUT))
                        .child(row("Output", OUTPUT))
                        .child(row("Reasoning", REASONING)),
                );
    
            div().flex().justify_center().py_12().child(
                HoverCard::new("context")
                    .variant(HoverCardVariant::Rhea)
                    .width(px(256.0))
                    .open_delay(OPEN_DELAY)
                    .close_delay(CLOSE_DELAY)
                    .trigger(trigger)
                    .child(card),
            )
        }
    }
    
    /// shadcn's `formatTokens`: 980, 8.4k, 128k.
    fn tokens(value: u32) -> String {
        if value < 1_000 {
            return value.to_string();
        }
        let k = format!("{:.1}", value as f32 / 1_000.0);
        format!("{}k", k.strip_suffix(".0").unwrap_or(&k))
    }