Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

fix(assistant-ui): handle empty range in surface element rendering

Steven Enamakel0d301c508c6a
diff --git a/app/src/components/assistant-ui/elements/surfaces.tsx b/app/src/components/assistant-ui/elements/surfaces.tsx
index 101dbd360e..46aaa51db6 100644
--- a/app/src/components/assistant-ui/elements/surfaces.tsx
+++ b/app/src/components/assistant-ui/elements/surfaces.tsx
@@ -1,63 +1,102 @@
-'use client';
+"use client";
/**
- * Shared surface primitives for the assistant-ui elements, vendored from the
- * `elements-surfaces` registry item. Only the pieces the reasoning panel uses
- * are kept. `collapsePanel` is adapted from Base UI's height variable to the
- * Radix `Collapsible` this app ships (`tw-animate-css` provides the
- * `collapsible-down/up` keyframes over `--radix-collapsible-content-height`).
+ * Shared surface primitives for the assistant-ui elements, vendored verbatim
+ * from the `elements-surfaces` registry item
+ * (https://r.assistant-ui.com/elements-surfaces.json). The one local change is
+ * `collapsePanel`, adapted from Base UI to Radix (see below).
*/
-import { cn } from '@/components/assistant-ui/lib/utils';
-import { type ComponentProps, type ReactNode, useLayoutEffect, useRef, useState } from 'react';
+import type { ComponentProps } from "react";
+import { useLayoutEffect, useRef, useState } from "react";
+import { cn } from "@/components/assistant-ui/lib/utils";
+
+export const paper = "bg-background border border-border/60 dark:bg-popover";
+
+export const floating = "bg-background border border-border/60 dark:bg-popover";
+
+export const field = "bg-foreground/[0.04] dark:bg-foreground/[0.06]";
+
+export const fieldInteractive =
+ "bg-foreground/[0.04] transition-colors hover:bg-foreground/[0.07] dark:bg-foreground/[0.06] dark:hover:bg-foreground/[0.09]";
+
+export const pressable =
+ "transition-transform duration-150 ease-[cubic-bezier(0.23,1,0.32,1)] active:scale-[0.96] motion-reduce:transition-none";
+
+export const ghostButton =
+ "flex items-center justify-center rounded-full text-foreground/45 outline-none transition-[background-color,color,scale] duration-150 hover:bg-foreground/[0.06] hover:text-foreground/90 active:scale-[0.96] focus-visible:ring-1 focus-visible:ring-foreground/20 motion-reduce:transition-none dark:hover:bg-foreground/[0.09]";
+
+export const inkButton =
+ "bg-foreground text-background transition-[opacity,scale] duration-150 ease-[cubic-bezier(0.23,1,0.32,1)] hover:opacity-90 active:scale-[0.96] motion-reduce:transition-none";
+
+export const iconSwap =
+ "[grid-area:1/1] transition-[opacity,scale,filter] duration-200 ease-[cubic-bezier(0.2,0,0,1)] motion-reduce:transition-none";
+
+export const iconSwapIn = "scale-100 opacity-100 blur-none";
+
+export const iconSwapOut = "scale-[0.25] opacity-0 blur-[4px]";
export const labelSwap =
- 'col-start-1 row-start-1 flex w-max items-center gap-1.5 leading-none transition-[opacity,filter] duration-300 ease-[cubic-bezier(0.23,1,0.32,1)] motion-reduce:transition-none';
+ "col-start-1 row-start-1 flex w-max items-center gap-1.5 leading-none transition-[opacity,filter] duration-300 ease-[cubic-bezier(0.23,1,0.32,1)] motion-reduce:transition-none";
-export const labelSwapIn = 'opacity-100 blur-none';
+export const labelSwapIn = "opacity-100 blur-none";
-export const labelSwapOut = 'pointer-events-none select-none opacity-0 blur-[2px]';
+export const labelSwapOut =
+ "pointer-events-none select-none opacity-0 blur-[2px]";
+// Local adaptation: upstream animates Base UI's `--collapsible-panel-height`;
+// this app's `ui/collapsible` is Radix, whose content exposes
+// `--radix-collapsible-content-height` to tw-animate-css's collapsible keyframes.
export const collapsePanel =
- 'overflow-hidden ease-[cubic-bezier(0.32,0.72,0,1)] data-[state=closed]:animate-collapsible-up data-[state=open]:animate-collapsible-down motion-reduce:animate-none';
+ "overflow-hidden ease-[cubic-bezier(0.32,0.72,0,1)] data-[state=closed]:animate-collapsible-up data-[state=open]:animate-collapsible-down motion-reduce:animate-none";
-export const mono = 'font-mono text-[11px] tracking-tight';
+export const live = "text-blue-500 dark:text-blue-400";
+
+export const mono = "font-mono text-[11px] tracking-tight";
export function ShimmerLabel({
active = true,
className,
...props
-}: ComponentProps<'span'> & { active?: boolean }) {
+}: ComponentProps<"span"> & { active?: boolean }) {
return (
<span
- data-shimmer={active ? '' : undefined}
- className={cn(active && 'shimmer motion-reduce:animate-none', className)}
+ className={cn(active && "shimmer motion-reduce:animate-none", className)}
{...props}
/>
);
}
/**
- * Two stacked labels that cross-fade, with the wrapper's width animating to
- * whichever layer is active, so "Thinking… 4s" settling into "Thought for 12s"
- * slides rather than jumps.
+ * Scroll region for content that keeps its own whitespace. `whitespace-pre` in
+ * a bounded box clips a long line with no way to reach it, so the rows scroll
+ * instead.
+ *
+ * `codeSurface` wraps all the rows as one block, and the rows are its children.
+ * It cannot go on each row: `min-width: 100%` resolves against the scroll
+ * container's visible width rather than its scroll width, so a per-row width
+ * leaves every row except the longest ending its background at the fold.
*/
+export const codeScroll = "overflow-x-auto";
+
+export const codeSurface = "w-max min-w-full";
+
export function SwapLabel({
active,
children,
className,
}: {
active: 0 | 1;
- children: [ReactNode, ReactNode];
+ children: [React.ReactNode, React.ReactNode];
className?: string;
}) {
- const first = useRef<HTMLSpanElement>(null);
- const second = useRef<HTMLSpanElement>(null);
+ const layers = [useRef<HTMLSpanElement>(null), useRef<HTMLSpanElement>(null)];
const [width, setWidth] = useState<number | null>(null);
useLayoutEffect(() => {
- const target = (active === 0 ? first : second).current;
+ const target = layers[active]?.current;
if (!target) return undefined;
- const measure = () => setWidth(Math.ceil(target.getBoundingClientRect().width));
+ const measure = () =>
+ setWidth(Math.ceil(target.getBoundingClientRect().width));
measure();
const observer = new ResizeObserver(measure);
observer.observe(target);
@@ -66,19 +105,22 @@ export function SwapLabel({
return (
<span
- style={width ? { width } : undefined}
+ style={width === null ? undefined : { width }}
className={cn(
- 'grid overflow-x-clip transition-[width] duration-300 ease-[cubic-bezier(0.23,1,0.32,1)] motion-reduce:transition-none',
- className
- )}>
+ "grid overflow-x-clip transition-[width] duration-300 ease-[cubic-bezier(0.23,1,0.32,1)] motion-reduce:transition-none",
+ className,
+ )}
+ >
{children.map((layer, index) => (
<span
key={index}
- ref={index === 0 ? first : second}
+ ref={layers[index]}
aria-hidden={active !== index}
- data-swap-layer={index === 0 ? 'live' : 'resting'}
- data-active={active === index ? '' : undefined}
- className={cn(labelSwap, active === index ? labelSwapIn : labelSwapOut)}>
+ className={cn(
+ labelSwap,
+ active === index ? labelSwapIn : labelSwapOut,
+ )}
+ >
{layer}
</span>
))}
diff --git a/app/src/components/assistant-ui/utils/range.ts b/app/src/components/assistant-ui/utils/range.ts
index 39bd918a3c..d918f2f8b9 100644
--- a/app/src/components/assistant-ui/utils/range.ts
+++ b/app/src/components/assistant-ui/utils/range.ts
@@ -1,13 +1,22 @@
+// Vendored verbatim from the assistant-ui `elements-range` registry item
+// (https://r.assistant-ui.com/elements-range.json).
+
/**
- * Range normalization for the numeric props the elements take (vendored from
- * the assistant-ui `elements-range` registry item).
+ * Range normalization for the numeric props the elements take.
*
* Elements are driven by a caller's state, so a prop can arrive negative, past
- * the end of its collection, or NaN. Left raw, a negative slice length counts
- * from the end of the array instead of returning nothing.
+ * the end of its collection, or NaN. Left raw, those reach the DOM: a negative
+ * percentage is an invalid CSS width that the browser drops, leaving a bar at
+ * its natural full width, and a negative slice length counts from the end of
+ * the array instead of returning nothing.
*/
-/** Constrains a value to `min…max`. NaN maps to `min`; with inverted bounds `max` wins. */
+/**
+ * Constrains a value to `min…max`. NaN is decided first and maps to `min`.
+ * For any other value, an empty collection can invert the bounds and `max`
+ * wins there: `clamp(3, 1, 0)` is `0`, which is what lets a floor of one item
+ * still yield none.
+ */
export function clamp(value: number, min: number, max: number) {
if (Number.isNaN(value)) return min;
return Math.min(max, Math.max(min, value));
@@ -17,3 +26,35 @@ export function clamp(value: number, min: number, max: number) {
export function take<T>(items: readonly T[], count: number) {
return items.slice(0, Math.floor(clamp(count, 0, items.length)));
}
+
+/** The position `index` names in `items`, for an `index` out of range. */
+export function indexIn<T>(items: readonly T[], index: number) {
+ return Math.floor(clamp(index, 0, Math.max(0, items.length - 1)));
+}
+
+/** The item at `index`, for an `index` that may be out of range. */
+export function at<T>(items: readonly T[], index: number) {
+ if (items.length === 0) return undefined;
+ return items[indexIn(items, index)];
+}
+
+/** `value` as a share of `total`, as a percentage in `0…100`. */
+export function pct(value: number, total: number) {
+ if (!(total > 0)) return 0;
+ return clamp((value / total) * 100, 0, 100);
+}
+
+/**
+ * A `0…100` share as it should be announced. `pct` divides, so a share that
+ * reads as a whole number on screen can still reach `aria-valuenow` carrying
+ * float error, which a screen reader reads out in full.
+ */
+export function announced(share: number) {
+ return Math.round(share * 10) / 10;
+}
+
+/** A count of completed items out of `total`, in `0…total`. */
+export function progressOf(index: number, total: number) {
+ if (!(total > 0)) return 0;
+ return Math.floor(clamp(index, 0, total));
+}