hydroxy

Assistant composer

The assistant's message box. Messages written while a reply runs wait in a queue and send in order when it ends. Up recalls earlier messages, @ and / open a picker, and files attach as chips with upload progress. Suggestion cards fill an empty thread.

q3-interviews.pdfPDF, 2.4 MB

Send starts a three second reply. Send again to queue, Ctrl+Enter to steer, Escape to stop, Up to recall.

Usage

use std::time::Duration;

use hydroxy_leptos::assistant_composer::{
    AssistantComposer, AssistantSuggestions, AttachmentFile, AttachmentItem, AttachmentStatus, Suggestion,
};
use hydroxy_leptos::chat_bubble::{ChatBubble, ChatRole};
use leptos::prelude::*;

const REPLIES: [&str; 3] = [
    "Pricing came up in nine of the fourteen interviews, usually as a surprise at renewal rather than at sign-up.",
    "Onboarding was the second theme: people who skipped the setup call churned twice as often.",
    "Support was slowest on integrations. Median first reply was 19 hours, against 3 hours for billing.",
];

// A fake upload: 20% every quarter second.
fn advance(files: RwSignal<Vec<AttachmentItem>>, id: String, progress: u8) {
    files.try_update(|all| {
        if let Some(f) = all.iter_mut().find(|f| f.id == id)
            && matches!(f.status, AttachmentStatus::Uploading(_))
        {
            f.status = if progress >= 100 { AttachmentStatus::Ready } else { AttachmentStatus::Uploading(progress) };
        }
    });
    if progress < 100 {
        set_timeout(move || advance(files, id, progress + 20), Duration::from_millis(250));
    }
}

#[allow(clippy::too_many_lines, reason = "one demo view")]
#[component]
pub fn Demo() -> impl IntoView {
    let (messages, set_messages) = signal(Vec::<(u32, ChatRole, String)>::new());
    let (running, set_running) = signal(false);
    let files_rw = RwSignal::new(vec![AttachmentItem::new("f0", "q3-interviews.pdf", 2_516_582)]);
    let (files, set_files) = files_rw.split();
    let (quote, set_quote) = signal(None::<String>);
    // Each reply gets a number; Stop or a steer bumps it so the old timer ends.
    let run = StoredValue::new(0_u32);
    let next = StoredValue::new(1_u32);
    let upload = StoredValue::new(1_u32);

    let push = move |role: ChatRole, text: String| {
        let id = next.get_value();
        next.update_value(|n| *n += 1);
        set_messages.update(|m| m.push((id, role, text)));
    };
    let start = move |text: String| {
        let n = files.with_untracked(Vec::len);
        let shown = if n > 0 && !text.is_empty() { format!("{text} ({n} attached)") } else { text };
        push(ChatRole::User, shown);
        set_files.set(Vec::new());
        set_quote.set(None);
        run.update_value(|r| *r += 1);
        let mine = run.get_value();
        set_running.set(true);
        set_timeout(
            move || {
                if run.try_get_value() != Some(mine) {
                    return;
                }
                push(ChatRole::Assistant, REPLIES[mine as usize % REPLIES.len()].to_string());
                set_running.set(false);
            },
            Duration::from_secs(3),
        );
    };

    let on_files = move |picked: Vec<AttachmentFile>| {
        for f in picked {
            let id = format!("u{}", upload.get_value());
            upload.update_value(|n| *n += 1);
            // Anything over 10 MB fails, so the failed chip is visible.
            if f.size > 10_000_000 {
                let item =
                    AttachmentItem::new(id, f.name, f.size).status(AttachmentStatus::Failed("over 10 MB".to_string()));
                set_files.update(|all| all.push(item));
            } else {
                let item = AttachmentItem::new(id.clone(), f.name, f.size).status(AttachmentStatus::Uploading(0));
                set_files.update(|all| all.push(item));
                advance(files_rw, id, 20);
            }
        }
    };

    view! {
        <div style="display: grid; gap: var(--space-4); max-width: 640px;">
            <div style="display: grid; gap: var(--space-4); min-height: 120px;">
                {move || {
                    if messages.with(Vec::is_empty) {
                        view! {
                            <AssistantSuggestions
                                items=vec![
                                    Suggestion::new("Summarise the interviews", "Themes across all fourteen", "Summarise the Q3 interviews by theme."),
                                    Suggestion::new("Why did churn rise?", "Look at pricing and onboarding", "Why did churn rise in Q3?"),
                                ]
                                on_pick=start
                            />
                        }
                            .into_any()
                    } else {
                        view! {
                            <For
                                each=move || messages.get()
                                key=|m| m.0
                                children=move |(_, role, text)| {
                                    let reply = role == ChatRole::Assistant;
                                    view! {
                                        <ChatBubble role=role>
                                            {text.clone()}
                                            {reply
                                                .then(|| {
                                                    let t = text.clone();
                                                    view! {
                                                        <button
                                                            type="button"
                                                            class="ox-button"
                                                            data-variant="ghost"
                                                            data-size="sm"
                                                            on:click=move |_| set_quote.set(Some(t.clone()))
                                                        >
                                                            "Quote"
                                                        </button>
                                                    }
                                                })}
                                        </ChatBubble>
                                    }
                                }
                            />
                        }
                            .into_any()
                    }
                }}
            </div>
            <AssistantComposer
                running=running
                placeholder="Ask about the interviews. Type @ for people, / for commands."
                mentions=vec!["Priya Nair".to_string(), "Tom Okafor".to_string(), "Sam Whitlock".to_string()]
                commands=vec!["summarise".to_string(), "compare".to_string(), "cite-sources".to_string()]
                accept="image/*,.pdf,.csv,.md"
                attachments=files
                quote=quote
                on_quote_dismiss=move |()| set_quote.set(None)
                on_send=start
                on_steer=move |text: String| {
                    push(ChatRole::Assistant, "Stopped to read your new message.".to_string());
                    start(text);
                }
                on_cancel=move |()| {
                    run.update_value(|r| *r += 1);
                    set_running.set(false);
                    push(ChatRole::Assistant, "Stopped.".to_string());
                }
                on_add_files=on_files
                on_remove_attachment=move |id: String| set_files.update(|all| all.retain(|f| f.id != id))
            />
            <p style="margin: 0; font: var(--text-sm); color: var(--text-2);">
                "Send starts a three second reply. Send again to queue, Ctrl+Enter to steer, Escape to stop, Up to recall."
            </p>
        </div>
    }
}

Anatomy

Componentdata-partRenders
AssistantComposerrootThe card. data-running, data-dragging while files are dragged over, data-compact
quote / quote-text / quote-dismissQuoted text above the field, with a button to remove it
attachmentsRow of AssistantAttachment chips
field / inputNative textarea (role combobox) that grows with its text
popover / optionListbox for @ and /. data-highlighted on the active option
queueList of AssistantQueueItem rows
actionsRow holding the buttons below
add-attachment / file-inputLabel around a native file input
cancel / steer / sendStop and Steer show while running; send reads Queue
AssistantQueueItemroot / text / steer / removeA queued message with Send now and remove
AssistantAttachmentroot / thumb / body / name / meta / progress / removeA file chip. data-status is uploading, ready or failed; data-kind is the file type
AssistantSuggestionsgridWraps the cards
AssistantSuggestionroot / trigger / title / descriptionOne card; the trigger is a button

Keyboard

KeyDoes
EnterSend, or queue while a reply runs. In the popover: pick the highlighted option
Shift+EnterAdd a line
Ctrl/Cmd+EnterSend now, interrupting the running reply
EscapeClose the popover, or stop the running reply
Up / DownMove through the popover. In an empty field: recall earlier messages
TabPick the highlighted option

Props

PropTypeNotes
runningSignal<bool>A reply is running. Sends queue; when it turns false the next queued message goes to on_send
on_sendCallback<String>Message to send now. Start a reply and set running
on_steerCallback<String>Message that should interrupt the running reply. Falls back to on_send
on_cancelCallback<()>Stop was pressed
on_add_filesCallback<Vec<AttachmentFile>>Names, sizes and types of picked or dropped files. Without it there is no add button
attachments / on_remove_attachmentVec<AttachmentItem> / Callback<String>Chips above the field. An item has id, name, size, mime and a status of Uploading(percent), Ready or Failed(reason)
quote / on_quote_dismissOption<String> / Callback<()>Quoted text above the field
mentions / commandsVec<String>Options after @, and after a leading /
accept / multipleString / boolFile picker filter, and whether several files can be picked
placeholder / label / compactString / String / boolHint text, accessible name, tighter padding
AssistantSuggestions items / on_pickVec<Suggestion> / Callback<String>Cards, and the text to send when one is pressed
Actions
Button
Icon button
Toolbar
Theme menu
Display
Avatar
Badge
Card
Chip
Code block
Markdown
Doc card
Due pill
Kbd
Priority mark
Source chip
Space icon
Status diamond
Tag
Value diff
Carousel
Reveal
Media timeline
QR code
Transcript viewer
Audio waveform
Audio player
Forms
Calendar
Checkbox
Combobox
Date picker
Color picker
Cron builder
Theme editor
Mention input
Signature pad
Image cropper
Date field
Date range picker
Duration field
Quantity stepper
Filter chip
Input
Markdown editor
Radio group
Search field
Segmented control
Select
Slider
Range slider
Rating
Toggle button
File dropzone
Upload queue
Attachment
Switch
Tags input
Number field
Money field
Phone field
Time field
OTP input
Password field
Questionnaire
Availability picker
Voice input
Recording control
Inline edit
Async validated input
Shortcut recorder
Data
Spreadsheet grid
Week scheduler
Virtual list
Stat card
Step track
Kanban board
Sortable list
Filter builder
Data table
Grid
Access editor
File manager
Gantt chart
Log stream
Diff viewer
JSON inspector
Terminal
Node editor
Whiteboard
Navigation
Table of contents
Breadcrumb
Tabs
Wizard
Nav item
Nav section
Tree view
Pagination
Menubar
Navigation menu
Saved views
Layout
Master detail
App shell
Sidebar layout
Resizable panels
Dashboard layout
Dock layout
Feedback
Callout
Check list
Empty state
Progress bar
Toast
Approval card
Skeleton
Notification center
Bars
Bar chart
Diverging bar chart
Span chart
Pictogram chart
Stacked bar chart
Funnel chart
Bullet chart
Waffle chart
Marimekko chart
Dot plot chart
Error bar chart
Pareto chart
Waterfall chart
Lollipop chart
Grouped bar chart
Bar race chart
KPIs and frames
Chart frame
Metric card
Sparkline
Points
Scatter chart
Smith chart
Strip plot
Beeswarm chart
Parallel coordinates
Ternary chart
Bland-Altman chart
Quiver chart
Connected scatter chart
Grids and tables
Heatmap chart
Chart table
Cohort chart
Radial
Radial charts
Radar chart
Polar area chart
Stat ring
Clock chart
Spiral chart
Lines and areas
Bump chart
Slope chart
Line chart
Composed chart
Small multiples
Cycle plot chart
Horizon chart
Stacked area chart
Threshold chart
Split line chart
Linked charts
Confidence band chart
Forecast fan chart
Step chart
Range area chart
Distributions
Contour chart
Histogram
Box plot
Density chart
Ridgeline chart
Violin chart
Q-Q chart
Survival chart
ECDF chart
Raincloud plot
Forest plot
Maps
Geo map
Bubble map
Hexbin map
Cartogram
Flow map
Parts of a whole
Donut chart
Voronoi treemap
Nested donut chart
Treemap chart
Sunburst chart
Bubble pack chart
Icicle chart
Flame graph chart
Pie chart
Word cloud
Relationships
Sankey chart
Network chart
Chord chart
UpSet chart
Venn chart
Alluvial chart
Adjacency matrix chart
Dependency graph chart
Tree chart
Arc diagram
Hive plot
Edge bundling
Finance
Candlestick chart
OHLC chart
Depth chart
Renko chart
Time
Timeline chart
Calendar heatmap chart
Spectrogram chart
Marey chart
Overlays
Tour
Lightbox
Alert dialog
Command palette
Dialog
Menu
Popover
Tooltip
Sheet
Hover card
Context menu
Utilities
Pacer
Hotkeys
Disclosure
Accordion
Collapsible
Chat
Chat bubble
Voice session
Composer
Thread
Thread list
Assistant composer
Assistant MCP
Assistant modal
Assistant thread
Assistant thread list
Message scroller
Insight note
Emoji picker
Tool trace
Model picker
Citation popover
Activity
Activity feed
Comment thread
Timeline
Presence bar
Live cursors
Reaction bar
Guides
Getting started
All components
All charts