Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions ui/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { QuartermasterLayout } from './layout/QuartermasterLayout';
import { ThemeContext } from './layout/ThemeContext';
import { Welcome } from './layout/Welcome';
import { ThreadList, ThreadRow } from './layout/ThreadRow';
import { AgentStepsCard, ReasoningCard } from './layout/Steps';
import { useTheme } from './layout/useTheme';
import { tokensFor } from './theme';

Expand Down Expand Up @@ -37,6 +38,11 @@ const OVERRIDES = {
WelcomeScreen: Welcome,
ThreadListRow: ThreadRow,
ThreadListShell: ThreadList,
// The agent's working is secondary to the answer, and the defaults give it the larger share of
// the screen: a one-line greeting arrived under eight lines of expanded reasoning. These collapse
// it to a line with a preview and put the answer back on top.
AgentStepsCard,
ReasoningCard,
} as const;
const BRAND = { name: 'Quartermaster', logo: { src: '/mark.svg' } } as const;

Expand Down
115 changes: 115 additions & 0 deletions ui/src/layout/Steps.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { describe, expect, test, vi } from 'vitest';
import { AgentStepsCard, ReasoningCard } from './Steps';

const THINKING = [
'Okay, the user asked "how are you". Let me think about the best response.',
'First, I need to respond naturally. Since I am an AI, I do not have feelings.',
'The user is just greeting me, so no tool calls are needed here.',
].join('\n\n');

/**
* These exist because the defaults gave the agent's working more of the screen than the answer:
* a one-line greeting arrived underneath eight lines of expanded reasoning. Collapsing it is only
* correct if nothing is lost, so both halves are tested - the summary that shows, and the content
* that is still one click away.
*/
describe('ReasoningCard', () => {
test('starts collapsed, and says what is behind it', () => {
render(<ReasoningCard content={THINKING} />);
expect(screen.getByRole('button', { expanded: false })).toBeInTheDocument();
// The first sentence, as a preview - not the whole of it.
expect(screen.getByText(/Okay, the user asked/)).toBeInTheDocument();
expect(screen.queryByText(/no tool calls are needed/)).not.toBeInTheDocument();
});

test('a click opens it and nothing has been thrown away', async () => {
render(<ReasoningCard content={THINKING} />);
await userEvent.click(screen.getByRole('button'));
expect(screen.getByRole('button', { expanded: true })).toBeInTheDocument();
expect(screen.getByText(/no tool calls are needed/)).toBeInTheDocument();
});

test('while it is still thinking the newest line shows, not the oldest', () => {
// A preview frozen on the first sentence makes a running agent look stuck.
render(<ReasoningCard content={THINKING} isStreaming />);
expect(screen.getByText(/no tool calls are needed/)).toBeInTheDocument();
expect(screen.getByText('thinking')).toBeInTheDocument();
});

test('the parent keeps control when it asks for it', async () => {
const onToggle = vi.fn();
render(<ReasoningCard content={THINKING} expanded onToggle={onToggle} />);
expect(screen.getByRole('button', { expanded: true })).toBeInTheDocument();
await userEvent.click(screen.getByRole('button'));
expect(onToggle).toHaveBeenCalledOnce();
});

test('the control names itself for a screen reader', () => {
render(<ReasoningCard content={THINKING} />);
// Truncated preview text is not an accessible name; the label has to stand on its own.
expect(screen.getByRole('button', { name: /reasoning, \d+ words\. expand\./i })).toBeInTheDocument();
});

test('empty reasoning does not render an empty preview', () => {
render(<ReasoningCard content="" />);
expect(screen.getByRole('button', { name: /reasoning, 0 words/i })).toBeInTheDocument();
});
});

describe('AgentStepsCard', () => {
test('counts read as English, singular and plural', () => {
const { rerender } = render(
<AgentStepsCard toolCount={1} thinkingCount={1} expanded={false} onToggle={vi.fn()}>
<div />
</AgentStepsCard>,
);
expect(screen.getByText('1 tool call · 1 thought')).toBeInTheDocument();

rerender(
<AgentStepsCard toolCount={3} thinkingCount={2} expanded={false} onToggle={vi.fn()}>
<div />
</AgentStepsCard>,
);
expect(screen.getByText('3 tool calls · 2 thoughts')).toBeInTheDocument();
});

test('a turn that called no tools loses the card, not the information', () => {
// This is most of what made a greeting feel padded: a bordered box announcing that nothing
// happened. The line stays; the box goes.
const { container } = render(
<AgentStepsCard toolCount={0} thinkingCount={1} expanded={false} onToggle={vi.fn()}>
<div />
</AgentStepsCard>,
);
expect(container.querySelector('.qm-steps')).toHaveClass('is-quiet');
expect(screen.getByText('1 thought')).toBeInTheDocument();
});

test('a running turn keeps its card and says it is working', () => {
const { container } = render(
<AgentStepsCard toolCount={0} thinkingCount={1} expanded={false} active onToggle={vi.fn()}>
<div />
</AgentStepsCard>,
);
expect(container.querySelector('.qm-steps')).not.toHaveClass('is-quiet');
expect(screen.getByText('Working')).toBeInTheDocument();
});

test('children are rendered only when it is open', () => {
const { rerender } = render(
<AgentStepsCard toolCount={2} thinkingCount={0} expanded={false} onToggle={vi.fn()}>
<p>the steps themselves</p>
</AgentStepsCard>,
);
expect(screen.queryByText('the steps themselves')).not.toBeInTheDocument();

rerender(
<AgentStepsCard toolCount={2} thinkingCount={0} expanded onToggle={vi.fn()}>
<p>the steps themselves</p>
</AgentStepsCard>,
);
expect(screen.getByText('the steps themselves')).toBeInTheDocument();
});
});
133 changes: 133 additions & 0 deletions ui/src/layout/Steps.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import { useState, type ReactNode } from 'react';
import { BrainIcon, ChevronIcon } from './icons';

/**
* The agent's own working, kept out of the way of the conversation.
*
* The SDK renders reasoning as a bordered card holding the whole of the model's thinking, expanded.
* For a real job that is useful; for "how are you" it produced eight lines of deliberation above a
* one-line answer, and the answer - the thing a person came for - was the smallest item on screen.
* A transcript where the working is larger than the result is a transcript nobody reads.
*
* So the working collapses to one line by default and says how much there is behind it. Nothing is
* removed: it is all one click away, and while a turn is running the latest line stays visible so
* the agent never looks stuck. What changes is only which of the two is given the room.
*/

/** The first sentence, or the first clause long enough to mean something on its own. */
function firstLine(content: string) {
const text = content.replace(/\s+/g, ' ').trim();
if (!text) return '';
const stop = text.search(/[.!?](\s|$)/);
const line = stop > 20 ? text.slice(0, stop + 1) : text;
return line.length > 96 ? `${line.slice(0, 95).trimEnd()}…` : line;
}

/** The last thing it said, for the line that shows while a turn is still running. */
function lastLine(content: string) {
const text = content.replace(/\s+/g, ' ').trim();
return text.length > 96 ? `…${text.slice(-95).trimStart()}` : text;
}

export function ReasoningCard({
content = '',
isStreaming,
expanded,
onToggle,
reasoningTimeText,
headingText,
contentRef,
className,
}: {
content?: string;
isStreaming?: boolean;
expanded?: boolean;
onToggle?: () => void;
reasoningTimeText?: string | null;
headingText?: string;
contentRef?: (node: HTMLDivElement | null) => void;
className?: string;
}) {
/**
* The parent controls `expanded` when it wants to, but it opens reasoning by default and this
* component's whole purpose is that it should not be. Local state takes over when the parent
* offers no handler, so the default is closed and a click still works.
*/
const [openLocally, setOpenLocally] = useState(false);
const controlled = typeof onToggle === 'function' && typeof expanded === 'boolean';
const open = controlled ? expanded : openLocally;
const toggle = controlled ? onToggle : () => setOpenLocally((v) => !v);

const summary = isStreaming ? lastLine(content) : firstLine(content);
const words = content.trim() ? content.trim().split(/\s+/).length : 0;

return (
<div className={`qm-reasoning ${open ? 'is-open' : ''} ${className ?? ''}`}>
<button
type="button"
onClick={toggle}
className="qm-reasoning-head"
aria-expanded={open}
// Read as one control: the label says what it is, the summary is the preview of it.
aria-label={`${headingText ?? 'Reasoning'}, ${words} words. ${open ? 'Collapse' : 'Expand'}.`}
>
<ChevronIcon className={`qm-reasoning-chevron ${open ? 'is-open' : ''}`} />
<BrainIcon className="qm-reasoning-icon" />
<span className="qm-reasoning-label">{headingText ?? 'Reasoning'}</span>
{!open && summary ? <span className="qm-reasoning-summary">{summary}</span> : null}
<span className="qm-reasoning-meta">
{isStreaming ? 'thinking' : reasoningTimeText || (words ? `${words} words` : '')}
</span>
</button>

{open ? (
// Capped rather than unbounded: reasoning can run to thousands of words, and a block that
// pushes the answer off the screen is the problem this component exists to solve.
<div className="qm-reasoning-body" ref={contentRef}>
{content}
</div>
) : null}
</div>
);
}

export function AgentStepsCard({
toolCount,
thinkingCount,
expanded,
active,
onToggle,
children,
className,
}: {
toolCount: number;
thinkingCount: number;
expanded: boolean;
active?: boolean;
onToggle: () => void;
children: ReactNode;
className?: string;
}) {
const parts = [
toolCount ? `${toolCount} tool call${toolCount === 1 ? '' : 's'}` : null,
thinkingCount ? `${thinkingCount} thought${thinkingCount === 1 ? '' : 's'}` : null,
].filter(Boolean);

/**
* A turn that called no tools did not take any steps worth a card. It gets a line instead, so a
* greeting reads as a greeting - which is most of what made the transcript feel padded.
*/
const quiet = toolCount === 0 && !active;

return (
<div className={`qm-steps ${quiet ? 'is-quiet' : ''} ${className ?? ''}`}>
<button type="button" onClick={onToggle} className="qm-steps-head" aria-expanded={expanded}>
<ChevronIcon className={`qm-steps-chevron ${expanded ? 'is-open' : ''}`} />
<span className="qm-steps-label">{active ? 'Working' : 'Agent steps'}</span>
<span className="qm-steps-meta">{parts.join(' · ') || 'no steps'}</span>
{active ? <span className="qm-steps-pulse" aria-hidden="true" /> : null}
</button>
{expanded ? <div className="qm-steps-body">{children}</div> : null}
</div>
);
}
Loading
Loading